Skip to content

Commit

Permalink
Fix issue for union declared within a struct to be declared as a field (
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Nov 29, 2022
1 parent 3791fb8 commit 0894624
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/CppAst.Tests/TestStructs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,41 @@ public void TestAnonymous()
}
);
}


[Test]
public void TestUnion()
{
ParseAssert(@"
struct HelloWorld
{
int a;
union {
int c;
int d;
};
int b;
};
",
compilation =>
{
Assert.False(compilation.HasErrors);

Assert.AreEqual(1, compilation.Classes.Count);

{
var cppStruct = compilation.Classes[0];
Assert.AreEqual(3, cppStruct.Fields.Count);
Assert.AreEqual(string.Empty, cppStruct.Fields[1].Name);
Assert.IsInstanceOf<CppClass>(cppStruct.Fields[1].Type);
var cppUnion = ((CppClass)cppStruct.Fields[1].Type);
Assert.AreEqual(CppClassKind.Union, ((CppClass)cppStruct.Fields[1].Type).ClassKind);
Assert.AreEqual(2, cppUnion.Fields.Count);
}
}
);
}


}
}
19 changes: 18 additions & 1 deletion src/CppAst/CppModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,22 @@ private CXChildVisitResult VisitMember(CXCursor cursor, CXCursor parent, void* d
case CXCursorKind.CXCursor_ClassDecl:
case CXCursorKind.CXCursor_StructDecl:
case CXCursorKind.CXCursor_UnionDecl:
element = VisitClassDecl(cursor, data);
{
var cppClass = VisitClassDecl(cursor, data);
// Empty struct/class/union declaration are considered as fields
if (string.IsNullOrEmpty(cppClass.Name))
{
var containerContext = GetOrCreateDeclarationContainer(parent, data);
var cppField = new CppField(cppClass, string.Empty);
containerContext.DeclarationContainer.Fields.Add(cppField);
element = cppField;
}
else
{
element = cppClass;
}
break;
}

case CXCursorKind.CXCursor_EnumDecl:
element = VisitEnumDecl(cursor, data);
Expand Down Expand Up @@ -483,6 +497,9 @@ private CppComment GetComment(CXComment cxComment)
break;
case CppCommentKind.VerbatimBlockLine:
var text = cxComment.VerbatimBlockLineComment_Text.ToString();

// For some reason, VerbatimBlockLineComment_Text can return the rest of the file instead of just the line
// So we explicitly trim the line here
var indexOfLine = text.IndexOf('\n');
if (indexOfLine >= 0)
{
Expand Down

0 comments on commit 0894624

Please sign in to comment.