Skip to content

Commit ab110bd

Browse files
authored
Update ClangSharp to 16.0.0 (#83)
* Update ClangSharp to 16.0.0 * FIX tests to handle squash in new way.
1 parent 75d8e14 commit ab110bd

File tree

5 files changed

+59
-57
lines changed

5 files changed

+59
-57
lines changed

src/CppAst.Tests/InlineTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public void ParseAssert(string text, Action<CppCompilation> assertCompilation, C
1010
{
1111
if (assertCompilation == null) throw new ArgumentNullException(nameof(assertCompilation));
1212

13-
options = options ?? new CppParserOptions();
13+
options ??= new CppParserOptions();
1414
var currentDirectory = Environment.CurrentDirectory;
1515
var headerFilename = $"{TestContext.CurrentContext.Test.FullName}-{TestContext.CurrentContext.Test.ID}.h";
1616
var headerFile = Path.Combine(currentDirectory, headerFilename);

src/CppAst.Tests/TestTypeAliases.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void TestSquash()
102102

103103
Assert.AreEqual(1, compilation.Classes.Count);
104104
Assert.AreEqual(1, compilation.Typedefs.Count);
105-
Assert.AreEqual("", compilation.Classes[0].Name);
105+
Assert.AreEqual("MyStruct", compilation.Classes[0].Name);
106106
Assert.AreEqual("MyStruct", compilation.Typedefs[0].Name);
107107
},
108108
new CppParserOptions() { AutoSquashTypedef = false }

src/CppAst.Tests/TestTypedefs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ typedef struct {
102102

103103
Assert.AreEqual(1, compilation.Classes.Count);
104104
Assert.AreEqual(1, compilation.Typedefs.Count);
105-
Assert.AreEqual("", compilation.Classes[0].Name);
105+
Assert.AreEqual("MyStruct", compilation.Classes[0].Name);
106106
Assert.AreEqual("MyStruct", compilation.Typedefs[0].Name);
107107
},
108108
new CppParserOptions() { AutoSquashTypedef = false }

src/CppAst/CppAst.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
<None Include="../../img/cppast.png" Pack="true" PackagePath="/logo.png" />
2828
<None Include="../../readme.md" Pack="true" PackagePath="/" />
2929

30-
<PackageReference Include="ClangSharp" Version="15.0.2" />
30+
<PackageReference Include="ClangSharp" Version="16.0.0" />
3131
</ItemGroup>
3232

3333
<ItemGroup>
34-
<PackageReference Include="MinVer" Version="4.2.0">
34+
<PackageReference Include="MinVer" Version="4.3.0">
3535
<PrivateAssets>all</PrivateAssets>
3636
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3737
</PackageReference>

src/CppAst/CppModelBuilder.cs

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private CppContainerContext GetOrCreateDeclarationContainer(CXCursor cursor, voi
182182
{
183183
cppClass.TemplateKind = CppTemplateKind.TemplateSpecializedClass;
184184
}
185-
185+
186186

187187
//Just use low level api to call ClangSharp
188188
var tempArgsCount = cursor.NumTemplateArguments;
@@ -194,11 +194,11 @@ private CppContainerContext GetOrCreateDeclarationContainer(CXCursor cursor, voi
194194
cppClass.TemplateParameters.Add(param);
195195
}
196196

197-
if(cppClass.TemplateKind == CppTemplateKind.TemplateSpecializedClass)
197+
if (cppClass.TemplateKind == CppTemplateKind.TemplateSpecializedClass)
198198
{
199199
Debug.Assert(cppClass.SpecializedTemplate.TemplateParameters.Count == tempArgsCount);
200200
}
201-
201+
202202

203203
for (uint i = 0; i < tempArgsCount; i++)
204204
{
@@ -323,44 +323,45 @@ private CXChildVisitResult VisitMember(CXCursor cursor, CXCursor parent, void* d
323323
case CXCursorKind.CXCursor_ClassDecl:
324324
case CXCursorKind.CXCursor_StructDecl:
325325
case CXCursorKind.CXCursor_UnionDecl:
326-
{
327-
bool isAnonymous = cursor.IsAnonymous;
328-
var cppClass = VisitClassDecl(cursor, data);
329-
// Empty struct/class/union declaration are considered as fields
330-
if (isAnonymous)
331326
{
332-
Debug.Assert(string.IsNullOrEmpty(cppClass.Name));
333-
var containerContext = GetOrCreateDeclarationContainer(parent, data);
334-
335-
// We try to recover the offset from the previous field
336-
// Might not be always correct (with alignment rules),
337-
// but not sure how to recover the offset without recalculating the entire offsets
338-
var offset = 0;
339-
var cppClassContainer = containerContext.Container as CppClass;
340-
if (cppClassContainer is object && cppClassContainer.Fields.Count > 0)
327+
bool isAnonymous = cursor.IsAnonymous;
328+
var cppClass = VisitClassDecl(cursor, data);
329+
// Empty struct/class/union declaration are considered as fields
330+
if (isAnonymous)
341331
{
342-
var lastField = cppClassContainer.Fields[cppClassContainer.Fields.Count - 1];
343-
offset = (int)lastField.Offset + lastField.Type.SizeOf;
344-
}
332+
cppClass.Name = string.Empty;
333+
Debug.Assert(string.IsNullOrEmpty(cppClass.Name));
334+
var containerContext = GetOrCreateDeclarationContainer(parent, data);
335+
336+
// We try to recover the offset from the previous field
337+
// Might not be always correct (with alignment rules),
338+
// but not sure how to recover the offset without recalculating the entire offsets
339+
var offset = 0;
340+
var cppClassContainer = containerContext.Container as CppClass;
341+
if (cppClassContainer is object && cppClassContainer.Fields.Count > 0)
342+
{
343+
var lastField = cppClassContainer.Fields[cppClassContainer.Fields.Count - 1];
344+
offset = (int)lastField.Offset + lastField.Type.SizeOf;
345+
}
345346

346-
// Create an anonymous field for the type
347-
var cppField = new CppField(cppClass, string.Empty)
347+
// Create an anonymous field for the type
348+
var cppField = new CppField(cppClass, string.Empty)
349+
{
350+
Visibility = containerContext.CurrentVisibility,
351+
StorageQualifier = GetStorageQualifier(cursor),
352+
IsAnonymous = true,
353+
Offset = offset,
354+
};
355+
ParseAttributes(cursor, cppField, true);
356+
containerContext.DeclarationContainer.Fields.Add(cppField);
357+
element = cppField;
358+
}
359+
else
348360
{
349-
Visibility = containerContext.CurrentVisibility,
350-
StorageQualifier = GetStorageQualifier(cursor),
351-
IsAnonymous = true,
352-
Offset = offset,
353-
};
354-
ParseAttributes(cursor, cppField, true);
355-
containerContext.DeclarationContainer.Fields.Add(cppField);
356-
element = cppField;
357-
}
358-
else
359-
{
360-
element = cppClass;
361+
element = cppClass;
362+
}
363+
break;
361364
}
362-
break;
363-
}
364365

365366
case CXCursorKind.CXCursor_EnumDecl:
366367
element = VisitEnumDecl(cursor, data);
@@ -372,7 +373,7 @@ private CXChildVisitResult VisitMember(CXCursor cursor, CXCursor parent, void* d
372373

373374
case CXCursorKind.CXCursor_TypeAliasDecl:
374375
case CXCursorKind.CXCursor_TypeAliasTemplateDecl:
375-
376+
376377
element = VisitTypeAliasDecl(cursor, data);
377378
break;
378379

@@ -440,7 +441,7 @@ private CXChildVisitResult VisitMember(CXCursor cursor, CXCursor parent, void* d
440441

441442
var attrContainer = cppDeclaration as ICppAttributeContainer;
442443
//Only handle commnet attribute when we need
443-
if(attrContainer != null && ParseCommentAttributeEnabled)
444+
if (attrContainer != null && ParseCommentAttributeEnabled)
444445
{
445446
TryToParseAttributesFromComment(cppDeclaration.Comment, attrContainer);
446447
}
@@ -706,7 +707,7 @@ private CppMacro ParseMacro(CXCursor cursor)
706707
var tokens = tu.Tokenize(range);
707708

708709
var name = GetCursorSpelling(cursor);
709-
if(name.StartsWith("__cppast"))
710+
if (name.StartsWith("__cppast"))
710711
{
711712
//cppast system macros, just ignore here
712713
return null;
@@ -1453,10 +1454,10 @@ private List<CppAttribute> ParseSystemAndAnnotateAttributeInCursor(CXCursor curs
14531454
}
14541455
break;
14551456
case CXCursorKind.CXCursor_FirstAttr:
1456-
{
1457+
{
14571458
var attrKind = argCursor.AttrKind;
14581459
var attrKindSpelling = argCursor.AttrKindSpelling.ToLower();
1459-
1460+
14601461
var attribute = new CppAttribute(attrKindSpelling, AttributeKind.CxxSystemAttribute)
14611462
{
14621463
Span = sourceSpan,
@@ -1485,7 +1486,7 @@ private List<CppAttribute> ParseSystemAndAnnotateAttributeInCursor(CXCursor curs
14851486
collectAttributes.Add(attribute);
14861487
}
14871488
break;
1488-
1489+
14891490
// Don't generate a warning for unsupported cursor
14901491
default:
14911492
break;
@@ -1501,17 +1502,17 @@ private void TryToParseAttributesFromComment(CppComment comment, ICppAttributeCo
15011502
{
15021503
if (comment == null) return;
15031504

1504-
if(comment is CppCommentText ctxt)
1505+
if (comment is CppCommentText ctxt)
15051506
{
15061507
var txt = ctxt.Text.Trim();
1507-
if(txt.StartsWith("[[") && txt.EndsWith("]]"))
1508+
if (txt.StartsWith("[[") && txt.EndsWith("]]"))
15081509
{
15091510
attrContainer.Attributes.Add(new CppAttribute("comment", AttributeKind.CommentAttribute)
15101511
{
15111512
Arguments = txt,
15121513
Scope = "",
15131514
IsVariadic = false,
1514-
}) ;
1515+
});
15151516
}
15161517
}
15171518

@@ -1545,16 +1546,16 @@ private void ParseAttributes(CXCursor cursor, ICppAttributeContainer attrContain
15451546
}
15461547

15471548
//Parse attributes contains in cursor
1548-
if(attrContainer is CppFunction)
1549+
if (attrContainer is CppFunction)
15491550
{
15501551
var func = attrContainer as CppFunction;
15511552
CppTokenUtil.ParseFunctionAttributes(cursor, func.Name, ref tokenAttributes);
1552-
}
1553+
}
15531554
else
15541555
{
15551556
CppTokenUtil.ParseCursorAttributs(cursor, ref tokenAttributes);
15561557
}
1557-
1558+
15581559
attrContainer.TokenAttributes.AddRange(tokenAttributes);
15591560
}
15601561

@@ -1572,7 +1573,7 @@ private CppType VisitTypeAliasDecl(CXCursor cursor, void* data)
15721573
var kind = cursor.Kind;
15731574

15741575
CXCursor usedCursor = cursor;
1575-
if(kind == CXCursorKind.CXCursor_TypeAliasTemplateDecl)
1576+
if (kind == CXCursorKind.CXCursor_TypeAliasTemplateDecl)
15761577
{
15771578
usedCursor = cursor.TemplatedDecl;
15781579
}
@@ -1644,7 +1645,8 @@ private CppType VisitTypeDefDecl(CXCursor cursor, void* data)
16441645
private CppType VisitElaboratedDecl(CXCursor cursor, CXType type, CXCursor parent, void* data)
16451646
{
16461647
var fulltypeDefName = clang.getCursorUSR(cursor).CString;
1647-
if (_typedefs.TryGetValue(fulltypeDefName, out var typeRef)) {
1648+
if (_typedefs.TryGetValue(fulltypeDefName, out var typeRef))
1649+
{
16481650
return typeRef;
16491651
}
16501652

@@ -1724,7 +1726,7 @@ private CppType GetCppTypeInternal(CXCursor cursor, CXType type, CXCursor parent
17241726
return CppPrimitiveType.UnsignedInt;
17251727

17261728
case CXTypeKind.CXType_ULong:
1727-
return CppPrimitiveType.UnsignedInt;
1729+
return type.SizeOf == 8 ? CppPrimitiveType.UnsignedLongLong : CppPrimitiveType.UnsignedInt;
17281730

17291731
case CXTypeKind.CXType_ULongLong:
17301732
return CppPrimitiveType.UnsignedLongLong;
@@ -1895,7 +1897,7 @@ private List<CppType> ParseTemplateSpecializedArguments(CXCursor cursor, CXType
18951897
for (var templateIndex = 0; templateIndex < numTemplateArguments; ++templateIndex)
18961898
{
18971899
var templateArg = type.GetTemplateArgument((uint)templateIndex);
1898-
1900+
18991901
switch (templateArg.kind)
19001902
{
19011903
case CXTemplateArgumentKind.CXTemplateArgumentKind_Type:

0 commit comments

Comments
 (0)