Skip to content

Commit 15dbf22

Browse files
committed
Fix parsing of anonymous structs
Anonymous struct were expected to be skipped, but it was not the case and invalid code was generated. Bug fixed and Unit test addded. Fix issue #71
1 parent 930ecdc commit 15dbf22

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/CodeTree.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,23 @@ CodeTree::generate_cxx_for_type(std::ostream& o,
472472
for(const auto& f: t.fields){
473473
auto accessor_gen = check_veto_list_for_var_or_field(f, false);
474474
if(accessor_gen != accessor_mode_t::none){
475-
generate_accessor_cxx(o, &t, f, accessor_gen == accessor_mode_t::getter, 2);
475+
//skip field with anomymous struct types:
476+
bool anonymous = false;
477+
CXType field_type = clang_getCursorType(f);
478+
if(field_type.kind != CXType_Invalid){
479+
auto def = clang_getTypeDeclaration(field_type);
480+
if(!clang_Cursor_isNull(def) && clang_Cursor_isAnonymous(def)){
481+
anonymous = true;
482+
}
483+
}
484+
if(anonymous){
485+
if(verbose > 1){
486+
std::cerr << "No accessor generated for field " << f
487+
<< " because its type is anonymous.\n";
488+
}
489+
} else{
490+
generate_accessor_cxx(o, &t, f, accessor_gen == accessor_mode_t::getter, 2);
491+
}
476492
}
477493
}
478494
}
@@ -853,6 +869,7 @@ CodeTree::generate_accessor_cxx(std::ostream& o, const TypeRcd* type_rcd,
853869
const CXCursor& cursor, bool getter_only,
854870
int nindents){
855871

872+
856873
FunctionWrapper helper(cxx_to_julia_, MethodRcd(cursor), type_rcd, type_map_,
857874
cxxwrap_version_, "", "", nindents);
858875

@@ -1182,7 +1199,8 @@ CodeTree::visit_class(CXCursor cursor){
11821199
<< cursor << ")\n";
11831200

11841201

1185-
if(str(clang_getCursorSpelling(cursor)).size() == 0){
1202+
//FIXME: is the first check needed?
1203+
if(str(clang_getCursorSpelling(cursor)).size() == 0 || clang_Cursor_isAnonymous(cursor)){
11861204
if(verbose > 0){
11871205
std::cerr << "Skipping anonymous struct found in "
11881206
<< clang_getCursorLocation(cursor)
@@ -1530,6 +1548,7 @@ CodeTree::register_type(const CXType& type, int* pItype, int* pIenum){
15301548
<< type.kind
15311549
<< ".\n";
15321550

1551+
15331552
check_for_stl(type);
15341553

15351554
std::string type_name = fully_qualified_name(type);
@@ -1542,6 +1561,15 @@ CodeTree::register_type(const CXType& type, int* pItype, int* pIenum){
15421561
return false;
15431562
}
15441563

1564+
1565+
if(defs.size() > 0 && clang_Cursor_isAnonymous(defs[0])){
1566+
if(verbose > 2){
1567+
std::cerr << "Type " << type << " and its possible instance not mapped because the type is anonymous.\n";
1568+
};
1569+
return false;
1570+
}
1571+
1572+
15451573
bool maintype = true;
15461574
for(const auto& c: defs){
15471575
if(clang_Cursor_isNull(c)){

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using Serialization
55
tests = [ "TestSizet", "TestCtorDefVal", "TestAccessAndDelete", "TestNoFinalizer", "TestInheritance",
66
"TestPropagation", "TestTemplate1", "TestTemplate2", "TestVarField", "TestStdString", "TestStringView",
77
"TestStdVector", "TestOperators", "TestEnum", "TestPointers", "TestEmptyClass", "TestUsingType", "TestNamespace",
8-
"TestOrder", "TestAutoAdd", "TestAbstractClass"
8+
"TestOrder", "TestAutoAdd", "TestAbstractClass", "TestAnonymousStruct"
99
]
1010

1111
# Switch to test examples

0 commit comments

Comments
 (0)