15
15
using ClangSharp . Interop ;
16
16
using ClangSharp . XML ;
17
17
using static ClangSharp . Interop . CX_AttrKind ;
18
- using static ClangSharp . Interop . CXBinaryOperatorKind ;
19
18
using static ClangSharp . Interop . CX_CXXAccessSpecifier ;
20
19
using static ClangSharp . Interop . CX_StmtClass ;
21
20
using static ClangSharp . Interop . CX_UnaryExprOrTypeTrait ;
22
- using static ClangSharp . Interop . CXUnaryOperatorKind ;
21
+ using static ClangSharp . Interop . CXBinaryOperatorKind ;
23
22
using static ClangSharp . Interop . CXCallingConv ;
24
23
using static ClangSharp . Interop . CXDiagnosticSeverity ;
25
24
using static ClangSharp . Interop . CXEvalResultKind ;
26
25
using static ClangSharp . Interop . CXTemplateArgumentKind ;
27
26
using static ClangSharp . Interop . CXTranslationUnit_Flags ;
28
27
using static ClangSharp . Interop . CXTypeKind ;
28
+ using static ClangSharp . Interop . CXUnaryOperatorKind ;
29
29
30
30
namespace ClangSharp ;
31
31
@@ -61,6 +61,7 @@ public sealed partial class PInvokeGenerator : IDisposable
61
61
private readonly Dictionary < string , bool > _topLevelClassIsUnsafe ;
62
62
private readonly Dictionary < string , HashSet < string > > _topLevelClassUsings ;
63
63
private readonly Dictionary < string , List < string > > _topLevelClassAttributes ;
64
+ private readonly Dictionary < CXFile , ( nuint Address , nuint Length ) > _fileContents ;
64
65
private readonly HashSet < string > _topLevelClassNames ;
65
66
private readonly HashSet < string > _usedRemappings ;
66
67
private readonly string _placeholderMacroType ;
@@ -158,6 +159,7 @@ public PInvokeGenerator(PInvokeGeneratorConfiguration config, Func<string, Strea
158
159
_topLevelClassIsUnsafe = [ ] ;
159
160
_topLevelClassNames = [ ] ;
160
161
_topLevelClassAttributes = [ ] ;
162
+ _fileContents = [ ] ;
161
163
_topLevelClassUsings = [ ] ;
162
164
_usedRemappings = [ ] ;
163
165
_filePath = "" ;
@@ -1697,14 +1699,8 @@ public void GenerateBindings(TranslationUnit translationUnit, string filePath, s
1697
1699
{
1698
1700
if ( _config . GenerateMacroBindings )
1699
1701
{
1700
- var translationUnitHandle = translationUnit . Handle ;
1701
-
1702
- var file = translationUnitHandle . GetFile ( _filePath ) ;
1703
- var fileContents = translationUnitHandle . GetFileContents ( file , out var size ) ;
1704
1702
var fileContentsBuilder = _fileContentsBuilder ;
1705
1703
1706
- _ = fileContentsBuilder . Append ( Encoding . UTF8 . GetString ( fileContents ) ) ;
1707
-
1708
1704
foreach ( var cursor in translationUnit . TranslationUnitDecl . CursorChildren )
1709
1705
{
1710
1706
if ( cursor is PreprocessedEntity preprocessedEntity )
@@ -1716,7 +1712,10 @@ public void GenerateBindings(TranslationUnit translationUnit, string filePath, s
1716
1712
var unsavedFileContents = fileContentsBuilder . ToString ( ) ;
1717
1713
_ = fileContentsBuilder . Clear ( ) ;
1718
1714
1719
- using var unsavedFile = CXUnsavedFile . Create ( _filePath , unsavedFileContents ) ;
1715
+ var translationUnitHandle = translationUnit . Handle ;
1716
+ var file = translationUnitHandle . GetFile ( _filePath ) ;
1717
+
1718
+ using var unsavedFile = CXUnsavedFile . Create ( _filePath , translationUnitHandle , file , unsavedFileContents ) ;
1720
1719
var unsavedFiles = new CXUnsavedFile [ ] { unsavedFile } ;
1721
1720
1722
1721
translationFlags = _translationFlags & ~ CXTranslationUnit_DetailedPreprocessingRecord ;
@@ -3418,7 +3417,19 @@ private string GetRemappedTypeName(Cursor? cursor, Cursor? context, Type type, o
3418
3417
return remappedName ;
3419
3418
}
3420
3419
3421
- private static string GetSourceRangeContents ( CXTranslationUnit translationUnit , CXSourceRange sourceRange )
3420
+ private unsafe ReadOnlySpan < byte > GetFileContents ( CXTranslationUnit translationUnit , CXFile file )
3421
+ {
3422
+ if ( ! _fileContents . TryGetValue ( file , out var fileContentsMetadata ) )
3423
+ {
3424
+ var fileContents = translationUnit . GetFileContents ( file , out _ ) ;
3425
+ fileContentsMetadata = ( ( nuint ) Unsafe . AsPointer ( ref MemoryMarshal . GetReference ( fileContents ) ) , ( uint ) fileContents . Length ) ;
3426
+ _fileContents . Add ( file , fileContentsMetadata ) ;
3427
+ }
3428
+
3429
+ return new ReadOnlySpan < byte > ( ( byte * ) fileContentsMetadata . Address , ( int ) fileContentsMetadata . Length ) ;
3430
+ }
3431
+
3432
+ private string GetSourceRangeContents ( CXTranslationUnit translationUnit , CXSourceRange sourceRange )
3422
3433
{
3423
3434
sourceRange . Start . GetFileLocation ( out var startFile , out _ , out _ , out var startOffset ) ;
3424
3435
sourceRange . End . GetFileLocation ( out var endFile , out _ , out _ , out var endOffset ) ;
@@ -3428,9 +3439,9 @@ private static string GetSourceRangeContents(CXTranslationUnit translationUnit,
3428
3439
return string . Empty ;
3429
3440
}
3430
3441
3431
- var fileContents = translationUnit . GetFileContents ( startFile , out _ ) ;
3432
- fileContents = fileContents . Slice ( unchecked ( ( int ) startOffset ) , unchecked ( ( int ) ( endOffset - startOffset ) ) ) ;
3433
- return Encoding . UTF8 . GetString ( fileContents ) ;
3442
+ var contents = GetFileContents ( translationUnit , startFile ) ;
3443
+ contents = contents . Slice ( unchecked ( ( int ) startOffset ) , unchecked ( ( int ) ( endOffset - startOffset ) ) ) ;
3444
+ return Encoding . UTF8 . GetString ( contents ) ;
3434
3445
}
3435
3446
3436
3447
private string GetTargetTypeName ( Cursor cursor , out string nativeTypeName )
0 commit comments