Skip to content

Commit 49e3b2f

Browse files
committed
Ensure we still do the nativeTypeName == remappedName check in all paths
1 parent 554b1c0 commit 49e3b2f

2 files changed

Lines changed: 51 additions & 54 deletions

File tree

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,6 @@ private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl)
635635
}
636636

637637
var fieldDecl = indirectFieldDecl.AnonField;
638-
var tmpName = GetRemappedCursorName(fieldDecl);
639638
var anonymousRecordDecl = fieldDecl.Parent;
640639

641640
var rootRecordDecl = anonymousRecordDecl;
@@ -861,7 +860,7 @@ private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl)
861860
_outputBuilder.WriteDivider();
862861
}
863862

864-
private void VisitLabelDecl(LabelDecl labelDecl)
863+
private static void VisitLabelDecl(LabelDecl labelDecl)
865864
{
866865
// This should have already been handled as a statement
867866
}

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,79 +1190,77 @@ private string GetRemappedTypeName(Cursor cursor, Cursor context, Type type, out
11901190
var name = GetTypeName(cursor, context, type, out nativeTypeName);
11911191
var remappedName = GetRemappedName(name, cursor, tryRemapOperatorName: false, out var wasRemapped);
11921192

1193-
if (wasRemapped)
1193+
if (!wasRemapped)
11941194
{
1195-
return remappedName;
1196-
}
1195+
var canonicalType = type.CanonicalType;
11971196

1198-
var canonicalType = type.CanonicalType;
1199-
1200-
if ((canonicalType is ConstantArrayType constantArrayType) && (constantArrayType.ElementType is RecordType))
1201-
{
1202-
canonicalType = constantArrayType.ElementType;
1203-
}
1204-
1205-
if ((canonicalType is RecordType recordType) && remappedName.StartsWith("__AnonymousRecord_"))
1206-
{
1207-
var recordDecl = recordType.Decl;
1208-
remappedName = "_Anonymous";
1197+
if ((canonicalType is ConstantArrayType constantArrayType) && (constantArrayType.ElementType is RecordType))
1198+
{
1199+
canonicalType = constantArrayType.ElementType;
1200+
}
12091201

1210-
if (recordDecl.Parent is RecordDecl parentRecordDecl)
1202+
if ((canonicalType is RecordType recordType) && remappedName.StartsWith("__AnonymousRecord_"))
12111203
{
1212-
var matchingField = parentRecordDecl.Fields.Where((fieldDecl) => fieldDecl.Type.CanonicalType == recordType).FirstOrDefault();
1204+
var recordDecl = recordType.Decl;
1205+
remappedName = "_Anonymous";
12131206

1214-
if (matchingField != null)
1207+
if (recordDecl.Parent is RecordDecl parentRecordDecl)
12151208
{
1216-
remappedName = "_";
1217-
remappedName += GetRemappedCursorName(matchingField);
1218-
}
1219-
else
1220-
{
1221-
var index = 0;
1209+
var matchingField = parentRecordDecl.Fields.Where((fieldDecl) => fieldDecl.Type.CanonicalType == recordType).FirstOrDefault();
12221210

1223-
if (parentRecordDecl.AnonymousRecords.Count > 1)
1211+
if (matchingField != null)
12241212
{
1225-
index = parentRecordDecl.AnonymousRecords.IndexOf(cursor) + 1;
1213+
remappedName = "_";
1214+
remappedName += GetRemappedCursorName(matchingField);
12261215
}
1227-
1228-
while (parentRecordDecl.IsAnonymousStructOrUnion && (parentRecordDecl.IsUnion == recordType.Decl.IsUnion))
1216+
else
12291217
{
1230-
index += 1;
1218+
var index = 0;
12311219

1232-
if (parentRecordDecl.Parent is RecordDecl parentRecordDeclParent)
1220+
if (parentRecordDecl.AnonymousRecords.Count > 1)
12331221
{
1234-
if (parentRecordDeclParent.AnonymousRecords.Count > 0)
1222+
index = parentRecordDecl.AnonymousRecords.IndexOf(cursor) + 1;
1223+
}
1224+
1225+
while (parentRecordDecl.IsAnonymousStructOrUnion && (parentRecordDecl.IsUnion == recordType.Decl.IsUnion))
1226+
{
1227+
index += 1;
1228+
1229+
if (parentRecordDecl.Parent is RecordDecl parentRecordDeclParent)
12351230
{
1236-
index += parentRecordDeclParent.AnonymousRecords.Count - 1;
1231+
if (parentRecordDeclParent.AnonymousRecords.Count > 0)
1232+
{
1233+
index += parentRecordDeclParent.AnonymousRecords.Count - 1;
1234+
}
1235+
parentRecordDecl = parentRecordDeclParent;
12371236
}
1238-
parentRecordDecl = parentRecordDeclParent;
12391237
}
1240-
}
12411238

1242-
if (index != 0)
1243-
{
1244-
remappedName += index.ToString();
1239+
if (index != 0)
1240+
{
1241+
remappedName += index.ToString();
1242+
}
12451243
}
1246-
}
1247-
}
1248-
1249-
remappedName += $"_e__{(recordDecl.IsUnion ? "Union" : "Struct")}";
1250-
}
1251-
else if ((canonicalType is EnumType enumType) && remappedName.StartsWith("__AnonymousEnum_"))
1252-
{
1253-
remappedName = GetRemappedTypeName(enumType.Decl, context: null, enumType.Decl.IntegerType, out _);
1254-
}
1255-
else if (cursor is EnumDecl enumDecl)
1256-
{
1257-
var enumDeclName = GetRemappedCursorName(enumDecl);
1244+
}
12581245

1259-
if (enumDecl.Enumerators.Any((enumConstantDecl) => IsForceDwordOrForceUInt(enumDeclName, enumConstantDecl)))
1246+
remappedName += $"_e__{(recordDecl.IsUnion ? "Union" : "Struct")}";
1247+
}
1248+
else if ((canonicalType is EnumType enumType) && remappedName.StartsWith("__AnonymousEnum_"))
12601249
{
1261-
remappedName = "uint";
1250+
remappedName = GetRemappedTypeName(enumType.Decl, context: null, enumType.Decl.IntegerType, out _);
12621251
}
1252+
else if (cursor is EnumDecl enumDecl)
1253+
{
1254+
var enumDeclName = GetRemappedCursorName(enumDecl);
12631255

1264-
WithType("*", ref remappedName, ref nativeTypeName);
1265-
WithType(enumDeclName, ref remappedName, ref nativeTypeName);
1256+
if (enumDecl.Enumerators.Any((enumConstantDecl) => IsForceDwordOrForceUInt(enumDeclName, enumConstantDecl)))
1257+
{
1258+
remappedName = "uint";
1259+
}
1260+
1261+
WithType("*", ref remappedName, ref nativeTypeName);
1262+
WithType(enumDeclName, ref remappedName, ref nativeTypeName);
1263+
}
12661264
}
12671265

12681266
if (nativeTypeName.Equals(remappedName) || nativeTypeName.Replace(" ", "").Equals(remappedName))

0 commit comments

Comments
 (0)