@@ -10,6 +10,32 @@ def type_struct(entries, ast, tu):
1010 pretty_print_ast (ast , 0 , sys .stdout )
1111 return "TODO"
1212
13+
14+ def test_match (desc , ast ):
15+
16+ for key in ast .keys ():
17+ if key not in desc :
18+ return False
19+ elif type (ast [key ]) != type (desc [key ]):
20+ return False
21+ elif isinstance (ast [key ], str ):
22+ if ast [key ] != desc [key ]:
23+ return False
24+
25+ elif isinstance (ast [key ], list ):
26+ if len (ast [key ]) != len (desc [key ]):
27+ return False
28+
29+ for descIt , astIt in zip (desc [key ], ast [key ]):
30+ if not test_match (descIt , astIt ):
31+ return False
32+ elif isinstance (ast [key ], dict ):
33+ if not test_match (desc [key ], ast [key ]):
34+ return False
35+
36+ return True
37+
38+
1339def type_struct_or_union (entries , decl , tu ):
1440 fields = []
1541 for child in decl .get_children ():
@@ -19,6 +45,47 @@ def type_struct_or_union(entries, decl, tu):
1945 "name" : child .spelling ,
2046 "type" : type_from_ast (entries , child .type , tu )
2147 }
48+
49+ #NOTE: fields whose type is anonymous have two decl, one for the _anonymous type_ and one the the _named field_.
50+ # so for example, a field:
51+ #
52+ # struct { int x; } foo;
53+ #
54+ # would genereate:
55+ #
56+ # {
57+ # "name": "",
58+ # "type": {
59+ # "kind": "struct",
60+ # "fields": [
61+ # {
62+ # "name": "x",
63+ # "type": {
64+ # "kind": "i32"
65+ # }
66+ # }
67+ # ]
68+ # }
69+ # },
70+ # {
71+ # "name": "foo",
72+ # "type": {
73+ # "kind": "struct",
74+ # "fields": [
75+ # {
76+ # "name": "x",
77+ # "type": {
78+ # "kind": "i32"
79+ # }
80+ # }
81+ # ]
82+ # }
83+ # }
84+ #
85+ # so here if previous field was an anonymous decl with a matching type, we remove the anonymous decl.
86+ if len (fields ) > 0 and fields [- 1 ]["name" ] == "" and test_match (fields [- 1 ]["type" ], field ["type" ]):
87+ fields = fields [:- 1 ]
88+
2289 fields .append (field )
2390 elif child .kind == cindex .CursorKind .STRUCT_DECL or child .kind == cindex .CursorKind .UNION_DECL :
2491 field = {
0 commit comments