Skip to content

Commit 7f1be9f

Browse files
Merge pull request #316 from tannergooding/main
Ensure that log-potential-typedef-remappings doesn't include typedefs from every file
2 parents 82ebd16 + 34171b2 commit 7f1be9f

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ private void VisitDecl(Decl decl)
2222
{
2323
if (IsExcluded(decl))
2424
{
25-
if (decl.Kind == CX_DeclKind.CX_DeclKind_Typedef)
26-
{
27-
VisitTypedefDecl((TypedefDecl)decl, onlyHandleRemappings: true);
28-
}
2925
return;
3026
}
3127

@@ -138,7 +134,7 @@ private void VisitDecl(Decl decl)
138134

139135
case CX_DeclKind.CX_DeclKind_Typedef:
140136
{
141-
VisitTypedefDecl((TypedefDecl)decl, onlyHandleRemappings: false);
137+
VisitTypedefDecl((TypedefDecl)decl);
142138
break;
143139
}
144140

@@ -2830,13 +2826,13 @@ private void VisitTypeAliasDecl(TypeAliasDecl typeAliasDecl)
28302826
// Nothing to generate for type alias declarations
28312827
}
28322828

2833-
private void VisitTypedefDecl(TypedefDecl typedefDecl, bool onlyHandleRemappings)
2829+
private void VisitTypedefDecl(TypedefDecl typedefDecl)
28342830
{
2835-
ForUnderlyingType(typedefDecl, typedefDecl.UnderlyingType, onlyHandleRemappings);
2831+
ForUnderlyingType(typedefDecl, typedefDecl.UnderlyingType);
28362832

2837-
void ForFunctionProtoType(TypedefDecl typedefDecl, FunctionProtoType functionProtoType, Type parentType, bool onlyHandleRemappings)
2833+
void ForFunctionProtoType(TypedefDecl typedefDecl, FunctionProtoType functionProtoType, Type parentType)
28382834
{
2839-
if (!_config.ExcludeFnptrCodegen || onlyHandleRemappings)
2835+
if (!_config.ExcludeFnptrCodegen)
28402836
{
28412837
return;
28422838
}
@@ -2883,43 +2879,43 @@ void ForFunctionProtoType(TypedefDecl typedefDecl, FunctionProtoType functionPro
28832879
StopUsingOutputBuilder();
28842880
}
28852881

2886-
void ForPointeeType(TypedefDecl typedefDecl, Type parentType, Type pointeeType, bool onlyHandleRemappings)
2882+
void ForPointeeType(TypedefDecl typedefDecl, Type parentType, Type pointeeType)
28872883
{
28882884
if (pointeeType is AttributedType attributedType)
28892885
{
2890-
ForPointeeType(typedefDecl, attributedType, attributedType.ModifiedType, onlyHandleRemappings);
2886+
ForPointeeType(typedefDecl, attributedType, attributedType.ModifiedType);
28912887
}
28922888
else if (pointeeType is ElaboratedType elaboratedType)
28932889
{
2894-
ForPointeeType(typedefDecl, elaboratedType, elaboratedType.NamedType, onlyHandleRemappings);
2890+
ForPointeeType(typedefDecl, elaboratedType, elaboratedType.NamedType);
28952891
}
28962892
else if (pointeeType is FunctionProtoType functionProtoType)
28972893
{
2898-
ForFunctionProtoType(typedefDecl, functionProtoType, parentType, onlyHandleRemappings);
2894+
ForFunctionProtoType(typedefDecl, functionProtoType, parentType);
28992895
}
29002896
else if (pointeeType is PointerType pointerType)
29012897
{
2902-
ForPointeeType(typedefDecl, pointerType, pointerType.PointeeType, onlyHandleRemappings);
2898+
ForPointeeType(typedefDecl, pointerType, pointerType.PointeeType);
29032899
}
29042900
else if (pointeeType is TypedefType typedefType)
29052901
{
2906-
ForPointeeType(typedefDecl, typedefType, typedefType.Decl.UnderlyingType, onlyHandleRemappings);
2902+
ForPointeeType(typedefDecl, typedefType, typedefType.Decl.UnderlyingType);
29072903
}
29082904
else if (pointeeType is not ConstantArrayType and not BuiltinType and not TagType and not TemplateTypeParmType)
29092905
{
29102906
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported pointee type: '{pointeeType.TypeClassSpelling}'. Generating bindings may be incomplete.", typedefDecl);
29112907
}
29122908
}
29132909

2914-
void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType, bool onlyHandleRemappings)
2910+
void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType)
29152911
{
29162912
if (underlyingType is ArrayType arrayType)
29172913
{
29182914
// Nothing to do for array types
29192915
}
29202916
else if (underlyingType is AttributedType attributedType)
29212917
{
2922-
ForUnderlyingType(typedefDecl, attributedType.ModifiedType, onlyHandleRemappings);
2918+
ForUnderlyingType(typedefDecl, attributedType.ModifiedType);
29232919
}
29242920
else if (underlyingType is BuiltinType builtinType)
29252921
{
@@ -2931,19 +2927,19 @@ void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType, bool onlyHa
29312927
}
29322928
else if (underlyingType is ElaboratedType elaboratedType)
29332929
{
2934-
ForUnderlyingType(typedefDecl, elaboratedType.NamedType, onlyHandleRemappings);
2930+
ForUnderlyingType(typedefDecl, elaboratedType.NamedType);
29352931
}
29362932
else if (underlyingType is FunctionProtoType functionProtoType)
29372933
{
2938-
ForFunctionProtoType(typedefDecl, functionProtoType, parentType: null, onlyHandleRemappings);
2934+
ForFunctionProtoType(typedefDecl, functionProtoType, parentType: null);
29392935
}
29402936
else if (underlyingType is PointerType pointerType)
29412937
{
2942-
ForPointeeType(typedefDecl, parentType: null, pointerType.PointeeType, onlyHandleRemappings);
2938+
ForPointeeType(typedefDecl, parentType: null, pointeeType: pointerType.PointeeType);
29432939
}
29442940
else if (underlyingType is ReferenceType referenceType)
29452941
{
2946-
ForPointeeType(typedefDecl, parentType: null, referenceType.PointeeType, onlyHandleRemappings);
2942+
ForPointeeType(typedefDecl, parentType: null, pointeeType: referenceType.PointeeType);
29472943
}
29482944
else if (underlyingType is TagType underlyingTagType)
29492945
{
@@ -2965,7 +2961,7 @@ void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType, bool onlyHa
29652961
{
29662962
if (templateSpecializationType.IsTypeAlias)
29672963
{
2968-
ForUnderlyingType(typedefDecl, templateSpecializationType.AliasedType, onlyHandleRemappings);
2964+
ForUnderlyingType(typedefDecl, templateSpecializationType.AliasedType);
29692965
}
29702966
else
29712967
{
@@ -2978,7 +2974,7 @@ void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType, bool onlyHa
29782974
}
29792975
else if (underlyingType is TypedefType typedefType)
29802976
{
2981-
ForUnderlyingType(typedefDecl, typedefType.Decl.UnderlyingType, onlyHandleRemappings);
2977+
ForUnderlyingType(typedefDecl, typedefType.Decl.UnderlyingType);
29822978
}
29832979
else
29842980
{
@@ -3636,7 +3632,7 @@ private bool IsPrimitiveValue(Type type)
36363632
{
36373633
return IsPrimitiveValue(autoType.CanonicalType);
36383634
}
3639-
else if (type is BuiltinType builtinType)
3635+
else if (type is BuiltinType)
36403636
{
36413637
switch (type.Kind)
36423638
{

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,6 +3007,24 @@ private string GetTypeName(Cursor cursor, Cursor context, Type rootType, Type ty
30073007
// can be treated correctly. Otherwise, they will resolve to a particular
30083008
// platform size, based on whatever parameters were passed into clang.
30093009

3010+
var underlyingType = typedefType.Decl.UnderlyingType;
3011+
3012+
if (underlyingType.AsTagDecl is TagDecl underlyingTagDecl)
3013+
{
3014+
var underlyingName = GetCursorName(underlyingTagDecl);
3015+
3016+
if (underlyingName != result.typeName)
3017+
{
3018+
if (!_validNameRemappings.TryGetValue(underlyingName, out var remappings))
3019+
{
3020+
remappings = new HashSet<string>();
3021+
_validNameRemappings[underlyingName] = remappings;
3022+
}
3023+
3024+
_ = remappings.Add(result.typeName);
3025+
}
3026+
}
3027+
30103028
var remappedName = GetRemappedName(result.typeName, cursor, tryRemapOperatorName: false, out var wasRemapped, skipUsing: true);
30113029
result.typeName = wasRemapped ? remappedName : GetTypeName(cursor, context, rootType, typedefType.Decl.UnderlyingType, ignoreTransparentStructsWhereRequired, out _);
30123030
}

0 commit comments

Comments
 (0)