Skip to content

Commit 09a0ab2

Browse files
committed
Ensure that GenerateBindings clears stale state to improve subsequent performance and ensure correctness
1 parent d431b90 commit 09a0ab2

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,17 @@ public void GenerateBindings(TranslationUnit translationUnit, string filePath, s
16701670
_clangCommandLineArgs = clangCommandLineArgs;
16711671
_translationFlags = translationFlags;
16721672

1673+
// We need to clear any cached state from a previous translation unit as
1674+
// native handle IDs or other info may have been reused if it was disposed.
1675+
1676+
_context.Clear();
1677+
_cursorNames.Clear();
1678+
_cursorQualifiedNames.Clear();
1679+
_typeNames.Clear();
1680+
_overloadIndices.Clear();
1681+
_isExcluded.Clear();
1682+
_fileContents.Clear();
1683+
16731684
if (translationUnit.Handle.NumDiagnostics != 0)
16741685
{
16751686
var errorDiagnostics = new StringBuilder();
@@ -3465,7 +3476,7 @@ private unsafe ReadOnlySpan<byte> GetFileContents(CXTranslationUnit translationU
34653476
{
34663477
var fileContents = translationUnit.GetFileContents(file, out _);
34673478
fileContentsMetadata = ((nuint)Unsafe.AsPointer(ref MemoryMarshal.GetReference(fileContents)), (uint)fileContents.Length);
3468-
_fileContents.Add(file, fileContentsMetadata);
3479+
_fileContents[file] = fileContentsMetadata;
34693480
}
34703481

34713482
return new ReadOnlySpan<byte>((byte*)fileContentsMetadata.Address, (int)fileContentsMetadata.Length);
@@ -3481,8 +3492,8 @@ private string GetSourceRangeContents(CXTranslationUnit translationUnit, CXSourc
34813492
return string.Empty;
34823493
}
34833494

3484-
var contents = GetFileContents(translationUnit, startFile);
3485-
contents = contents.Slice(unchecked((int)startOffset), unchecked((int)(endOffset - startOffset)));
3495+
var contents1 = GetFileContents(translationUnit, startFile);
3496+
var contents = contents1.Slice(unchecked((int)startOffset), unchecked((int)(endOffset - startOffset)));
34863497
return Encoding.UTF8.GetString(contents);
34873498
}
34883499

0 commit comments

Comments
 (0)