Skip to content

Commit 9ccf5c2

Browse files
committed
Ensure that log-potential-typedef-remappings doesn't include typedefs from untraversed files
1 parent 1d189ce commit 9ccf5c2

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,6 +2976,10 @@ void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType, bool onlyHa
29762976
{
29772977
// Nothing to do for builtin types
29782978
}
2979+
else if (underlyingType is DecltypeType decltypeType)
2980+
{
2981+
ForUnderlyingType(typedefDecl, decltypeType.UnderlyingType, onlyHandleRemappings);
2982+
}
29792983
else if (underlyingType is DependentNameType dependentNameType)
29802984
{
29812985
// Nothing to do for dependent name types
@@ -3003,13 +3007,23 @@ void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType, bool onlyHa
30033007

30043008
if (underlyingName != typedefName)
30053009
{
3006-
if (!_validNameRemappings.TryGetValue(underlyingName, out var remappings))
3010+
if (!_allValidNameRemappings.TryGetValue(underlyingName, out var allRemappings))
30073011
{
3008-
remappings = new HashSet<string>();
3009-
_validNameRemappings[underlyingName] = remappings;
3012+
allRemappings = new HashSet<string>();
3013+
_allValidNameRemappings[underlyingName] = allRemappings;
30103014
}
3015+
_ = allRemappings.Add(typedefName);
30113016

3012-
_ = remappings.Add(typedefName);
3017+
3018+
if (!onlyHandleRemappings)
3019+
{
3020+
if (!_traversedValidNameRemappings.TryGetValue(underlyingName, out var traversedRemappings))
3021+
{
3022+
traversedRemappings = new HashSet<string>();
3023+
_traversedValidNameRemappings[underlyingName] = traversedRemappings;
3024+
}
3025+
_ = traversedRemappings.Add(typedefName);
3026+
}
30133027
}
30143028
}
30153029
else if (underlyingType is TemplateSpecializationType templateSpecializationType)

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public sealed partial class PInvokeGenerator : IDisposable
3737
private readonly Dictionary<NamedDecl, string> _cursorNames;
3838
private readonly Dictionary<(NamedDecl namedDecl, bool truncateForFunctionParameters), string> _cursorQualifiedNames;
3939
private readonly Dictionary<(Cursor cursor, Cursor context, Type type), (string typeName, string nativeTypeName)> _typeNames;
40-
private readonly Dictionary<string, HashSet<string>> _validNameRemappings;
40+
private readonly Dictionary<string, HashSet<string>> _allValidNameRemappings;
41+
private readonly Dictionary<string, HashSet<string>> _traversedValidNameRemappings;
4142
private readonly Dictionary<CXXMethodDecl, uint> _overloadIndices;
4243
private readonly Dictionary<Cursor, uint> _isExcluded;
4344
private readonly Dictionary<string, bool> _isTopLevelClassUnsafe;
@@ -100,13 +101,14 @@ public PInvokeGenerator(PInvokeGeneratorConfiguration config, Func<string, Strea
100101
_cursorNames = new Dictionary<NamedDecl, string>();
101102
_cursorQualifiedNames = new Dictionary<(NamedDecl, bool), string>();
102103
_typeNames = new Dictionary<(Cursor, Cursor, Type), (string, string)>();
103-
_validNameRemappings = new Dictionary<string, HashSet<string>>() {
104+
_allValidNameRemappings = new Dictionary<string, HashSet<string>>() {
104105
["intptr_t"] = new HashSet<string>() { "IntPtr", "nint" },
105106
["ptrdiff_t"] = new HashSet<string>() { "IntPtr", "nint" },
106107
["size_t"] = new HashSet<string>() { "UIntPtr", "nuint" },
107108
["uintptr_t"] = new HashSet<string>() { "UIntPtr", "nuint" },
108109
["_GUID"] = new HashSet<string>() { "Guid" },
109110
};
111+
_traversedValidNameRemappings = new Dictionary<string, HashSet<string>>();
110112
_overloadIndices = new Dictionary<CXXMethodDecl, uint>();
111113
_isExcluded = new Dictionary<Cursor, uint>();
112114
_isTopLevelClassUnsafe = new Dictionary<string, bool>();
@@ -1239,7 +1241,7 @@ public void GenerateBindings(TranslationUnit translationUnit, string filePath, s
12391241

12401242
if (_config.LogPotentialTypedefRemappings)
12411243
{
1242-
foreach (var kvp in _validNameRemappings)
1244+
foreach (var kvp in _traversedValidNameRemappings)
12431245
{
12441246
var name = kvp.Key;
12451247
var remappings = kvp.Value;
@@ -1248,7 +1250,14 @@ public void GenerateBindings(TranslationUnit translationUnit, string filePath, s
12481250
{
12491251
AddDiagnostic(DiagnosticLevel.Info, $"Potential missing remapping '{name}'. {GetFoundRemappingString(name, remappings)}");
12501252
}
1251-
else if (!remappings.Contains(remappedName) && (name != remappedName) && !_config.ForceRemappedNames.Contains(name))
1253+
}
1254+
1255+
foreach (var kvp in _allValidNameRemappings)
1256+
{
1257+
var name = kvp.Key;
1258+
var remappings = kvp.Value;
1259+
1260+
if (_config.RemappedNames.TryGetValue(name, out var remappedName) && !remappings.Contains(remappedName) && (name != remappedName) && !_config.ForceRemappedNames.Contains(name))
12521261
{
12531262
AddDiagnostic(DiagnosticLevel.Info, $"Potential invalid remapping '{name}={remappedName}'. {GetFoundRemappingString(name, remappings)}");
12541263
}
@@ -1258,7 +1267,7 @@ public void GenerateBindings(TranslationUnit translationUnit, string filePath, s
12581267
{
12591268
var remappedName = _config.RemappedNames[name];
12601269

1261-
if (!_validNameRemappings.ContainsKey(name) && (name != remappedName) && !_config.ForceRemappedNames.Contains(name))
1270+
if (!_allValidNameRemappings.ContainsKey(name) && (name != remappedName) && !_config.ForceRemappedNames.Contains(name))
12621271
{
12631272
AddDiagnostic(DiagnosticLevel.Info, $"Potential invalid remapping '{name}={remappedName}'. No remappings were found.");
12641273
}

0 commit comments

Comments
 (0)