<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/80898>80898</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Python] Access init values when parsing C++ header with clanglib
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
sebaleme
</td>
</tr>
</table>
<pre>
Hi,
I am using the python bindings from libclang in order to parse a C++ header file and generate a json file. The idea is to get a dictionary with the values defined in the header. Until now, I was not able to get the init values defined in the header:
```
namespace Parameters
{
struct test
{
unsigned long value3{9999UL};
double floating_val{1.987};
};
/// @brief sequence counter incremented for each output message
unsigned long counter{7777UL};
/// @brief sequence counter incremented for each output message
bool is_true{false};
/// @brief sequence counter incremented for each output message
float floating_val{1.852F};
test test_instance{};
} // namespace Parameters
```
I am using the following python code to parse the header:
```
def walk(node, origin_dict, current_dict):
parent = False
var_name = ""
if node.kind == cl.CursorKind.VAR_DECL:
var_name = str(node.type.spelling) + "_" + str(node.spelling)
current_dict[var_name] = node.xdata
if node.kind == cl.CursorKind.FIELD_DECL:
var_name = str(node.type.spelling) + "_" + str(node.spelling)
current_dict[var_name] = node.xdata
if node.kind == cl.CursorKind.STRUCT_DECL:
var_name = "struct_" + str(node.spelling)
current_dict[var_name] = {}
parent = True
if node.kind == cl.CursorKind.NAMESPACE:
var_name = "namespace_" + str(node.spelling)
current_dict[var_name] = {}
parent = True
for subnode in node.get_children():
if parent:
walk(subnode, origin_dict, current_dict[var_name])
else:
walk(subnode, origin_dict, current_dict)
def main():
file_dict = {}
index = cl.Index.create()
tu = index.parse(
filepath,
args=[
"/usr/include/",
"/usr/include/c++/9/",
"/usr/include/x86_64-linux-gnu/",
],
)
print("Translation unit:", tu.spelling)
walk(tu.cursor, file_dict, file_dict)
with open(OUTPUT_PATH + jsonfile_name, "w", encoding="utf-8") as outputfile:
json.dump(file_dict, outputfile, indent=4)
```
However, `node.xdata` is wrong, and I always get 0.
```
{
"namespace_Parameters": {
"struct_test": {
"unsigned long_value3": 0,
"double_floating_val": 0
},
"unsigned long_counter": 0,
"bool_is_true": 0,
"float_floating_val": 0,
"test_test_instance": 0
}
}
```
I don t know which python interface could give me the data from the AST. Because when I generate it with clang++, I do have the information:
```
|-VarDecl 0x55f769af3520 <line:19:1, col:29> col:15 counter 'unsigned long' listinit
| |-InitListExpr 0x55f769af3600 <col:22, col:29> 'unsigned long'
| | `-IntegerLiteral 0x55f769af3588 <col:23> 'unsigned long' 7777
| `-FullComment 0x55f769af3de0 <line:18:4, col:63>
| |-ParagraphComment 0x55f769af3d30 <col:4>
| | `-TextComment 0x55f769af3d00 <col:4> Text=" "
| `-BlockCommandComment 0x55f769af3d50 <col:5, col:63> Name="brief"
| `-ParagraphComment 0x55f769af3db0 <col:11, col:63>
| `-TextComment 0x55f769af3d80 <col:11, col:63> Text=" sequence counter incremented for each output message"
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8WFtz6jgS_jXipSsuIwM2DzxwCXVSm51NzSHzSgm7bTRHSKwkh-Tfb7VsLgaSs2drZikqsVH31xd1tz9ZOCcrjThhwxkbLnqi9ltjJw43QuEOextTfEy-ScbnLF6weNr8fQKxg9pJXYHfIuw__NZo2EhdSF05KK3ZgZKbXAldgdRgbIEWvIG9sA5BwJzxGeMz2KKglVIqBKELqFCjFZ5E_nRGh4UIVlsEWaAA6QikQg8CCpl7abSwH3CQfhsceROqRgcFllJjQZbp18ZIBK_aSwXaHBifwxMchANtPIiNwiMsiUst_ZdILGnTwEZx-w23WuzQ7UWO8CKs2KFH61rBdNZcOG_r3INH569WoNZhJwpQRleN_YSls_F4PH59ZumCJUdJAChMTV6XyggvdbV-E4qls340ztJL0c5185cvmy-wQbyxEktw-O8adY6Qm1p7tCB1bnGH2mMBpbGAIt-Cqf2-9rBD50SFDVjX5VadpbM0TdOuz3-D8Y0xCqRbe1sjS2elUA7_Zp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