diff --git a/src/CppAst.Tests/TestStructs.cs b/src/CppAst.Tests/TestStructs.cs index b8c8bb8..eac3f7b 100644 --- a/src/CppAst.Tests/TestStructs.cs +++ b/src/CppAst.Tests/TestStructs.cs @@ -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(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); + } + } + ); + } + + } } diff --git a/src/CppAst/CppModelBuilder.cs b/src/CppAst/CppModelBuilder.cs index 48de64d..a511733 100644 --- a/src/CppAst/CppModelBuilder.cs +++ b/src/CppAst/CppModelBuilder.cs @@ -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); @@ -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) {