@@ -145,7 +145,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
145
145
( data , ct ) =>
146
146
{
147
147
IncrementalTracker ? . RecordExecutedStep ( new IncrementalityTracker . ExecutedStepInfo ( IncrementalityTracker . StepName . CalculateStubInformation , data ) ) ;
148
- return ( data . Syntax , StubContext : CalculateStubInformation ( data . Syntax , data . Symbol , data . Environment , ct ) ) ;
148
+ return ( data . Syntax , StubContext : CalculateStubInformation ( data . Symbol , data . Environment , ct ) ) ;
149
149
}
150
150
)
151
151
. WithComparer ( Comparers . CalculatedContextWithSyntax )
@@ -244,6 +244,17 @@ private static SyntaxTokenList StripTriviaFromModifiers(SyntaxTokenList tokenLis
244
244
return new SyntaxTokenList ( strippedTokens ) ;
245
245
}
246
246
247
+ private static SyntaxTokenList AddToModifiers ( SyntaxTokenList modifiers , SyntaxKind modifierToAdd )
248
+ {
249
+ if ( modifiers . IndexOf ( modifierToAdd ) >= 0 )
250
+ return modifiers ;
251
+
252
+ int idx = modifiers . IndexOf ( SyntaxKind . PartialKeyword ) ;
253
+ return idx >= 0
254
+ ? modifiers . Insert ( idx , Token ( modifierToAdd ) )
255
+ : modifiers . Add ( Token ( modifierToAdd ) ) ;
256
+ }
257
+
247
258
private static TypeDeclarationSyntax CreateTypeDeclarationWithoutTrivia ( TypeDeclarationSyntax typeDeclaration )
248
259
{
249
260
return TypeDeclaration (
@@ -279,6 +290,9 @@ private static MemberDeclarationSyntax WrapMethodInContainingScopes(DllImportStu
279
290
MemberDeclarationSyntax containingType = CreateTypeDeclarationWithoutTrivia ( stub . StubContainingTypes . First ( ) )
280
291
. AddMembers ( stubMethod ) ;
281
292
293
+ // Mark containing type as unsafe such that all the generated functions will be in an unsafe context.
294
+ containingType = containingType . WithModifiers ( AddToModifiers ( containingType . Modifiers , SyntaxKind . UnsafeKeyword ) ) ;
295
+
282
296
// Add type to the remaining containing types (skipping the first which was handled above)
283
297
foreach ( TypeDeclarationSyntax typeDecl in stub . StubContainingTypes . Skip ( 1 ) )
284
298
{
@@ -339,8 +353,6 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu
339
353
bool setLastError = false ;
340
354
bool throwOnUnmappableChar = false ;
341
355
342
- var stubDllImportData = new GeneratedDllImportData ( attrData . ConstructorArguments [ 0 ] . Value ! . ToString ( ) ) ;
343
-
344
356
// All other data on attribute is defined as NamedArguments.
345
357
foreach ( KeyValuePair < string , TypedConstant > namedArg in attrData . NamedArguments )
346
358
{
@@ -398,7 +410,7 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu
398
410
} ;
399
411
}
400
412
401
- private static IncrementalStubGenerationContext CalculateStubInformation ( MethodDeclarationSyntax syntax , IMethodSymbol symbol , StubEnvironment environment , CancellationToken ct )
413
+ private static IncrementalStubGenerationContext CalculateStubInformation ( IMethodSymbol symbol , StubEnvironment environment , CancellationToken ct )
402
414
{
403
415
INamedTypeSymbol ? lcidConversionAttrType = environment . Compilation . GetTypeByMetadataName ( TypeNames . LCIDConversionAttribute ) ;
404
416
INamedTypeSymbol ? suppressGCTransitionAttrType = environment . Compilation . GetTypeByMetadataName ( TypeNames . SuppressGCTransitionAttribute ) ;
@@ -506,6 +518,10 @@ private static IncrementalStubGenerationContext CalculateStubInformation(MethodD
506
518
dllImport = dllImport . AddAttributeLists ( AttributeList ( SeparatedList ( forwardedAttributes ) ) ) ;
507
519
}
508
520
521
+ dllImport = dllImport . WithLeadingTrivia (
522
+ Comment ( "//" ) ,
523
+ Comment ( "// Local P/Invoke" ) ,
524
+ Comment ( "//" ) ) ;
509
525
code = code . AddStatements ( dllImport ) ;
510
526
511
527
return ( PrintGeneratedSource ( originalSyntax , dllImportStub . StubContext , code ) , dllImportStub . Diagnostics . AddRange ( diagnostics . Diagnostics ) ) ;
@@ -514,7 +530,7 @@ private static IncrementalStubGenerationContext CalculateStubInformation(MethodD
514
530
private MemberDeclarationSyntax PrintForwarderStub ( MethodDeclarationSyntax userDeclaredMethod , IncrementalStubGenerationContext stub )
515
531
{
516
532
SyntaxTokenList modifiers = StripTriviaFromModifiers ( userDeclaredMethod . Modifiers ) ;
517
- modifiers = modifiers . Insert ( modifiers . IndexOf ( SyntaxKind . PartialKeyword ) , Token ( SyntaxKind . ExternKeyword ) ) ;
533
+ modifiers = AddToModifiers ( modifiers , SyntaxKind . ExternKeyword ) ;
518
534
// Create stub function
519
535
MethodDeclarationSyntax stubMethod = MethodDeclaration ( stub . StubContext . StubReturnType , userDeclaredMethod . Identifier )
520
536
. WithModifiers ( modifiers )
0 commit comments