Skip to content

Commit b0685e1

Browse files
authored
[DllImportGenerator] Remove DLLIMPORTGENERATOR_ENABLED define (#63464)
1 parent 98c5328 commit b0685e1

File tree

6 files changed

+59
-304
lines changed

6 files changed

+59
-304
lines changed

eng/generators.targets

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@
8484
<DllImportGenerator_UseInternalUnsafeType>true</DllImportGenerator_UseInternalUnsafeType>
8585
<DefineConstants>$(DefineConstants);DLLIMPORTGENERATOR_INTERNALUNSAFE</DefineConstants>
8686
</PropertyGroup>
87-
88-
<PropertyGroup>
89-
<DefineConstants>$(DefineConstants);DLLIMPORTGENERATOR_ENABLED</DefineConstants>
90-
</PropertyGroup>
9187
</Target>
9288

9389
<Import Project="$(LibrariesProjectRoot)System.Runtime.InteropServices/gen/DllImportGenerator/Microsoft.Interop.DllImportGenerator.props" />

src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/Analyzers/ConvertToGeneratedDllImportFixer.cs

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public sealed class ConvertToGeneratedDllImportFixer : CodeFixProvider
2626

2727
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
2828

29-
public const string NoPreprocessorDefinesKey = "ConvertToGeneratedDllImport";
30-
public const string WithPreprocessorDefinesKey = "ConvertToGeneratedDllImportPreprocessor";
29+
private const string ConvertToGeneratedDllImportKey = "ConvertToGeneratedDllImport";
3130

3231
private static readonly string[] s_preferredAttributeArgumentOrder =
3332
{
@@ -70,33 +69,18 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
7069
if (!TryGetAttribute(methodSymbol, dllImportAttrType, out AttributeData? dllImportAttr))
7170
return;
7271

73-
// Register code fixes with two options for the fix - using preprocessor or not.
72+
// Register code fix
7473
context.RegisterCodeFix(
7574
CodeAction.Create(
76-
Resources.ConvertToGeneratedDllImportNoPreprocessor,
75+
Resources.ConvertToGeneratedDllImport,
7776
cancelToken => ConvertToGeneratedDllImport(
7877
context.Document,
7978
methodSyntax,
8079
methodSymbol,
8180
dllImportAttr!,
8281
generatedDllImportAttrType,
83-
usePreprocessorDefines: false,
8482
cancelToken),
85-
equivalenceKey: NoPreprocessorDefinesKey),
86-
context.Diagnostics);
87-
88-
context.RegisterCodeFix(
89-
CodeAction.Create(
90-
Resources.ConvertToGeneratedDllImportWithPreprocessor,
91-
cancelToken => ConvertToGeneratedDllImport(
92-
context.Document,
93-
methodSyntax,
94-
methodSymbol,
95-
dllImportAttr!,
96-
generatedDllImportAttrType,
97-
usePreprocessorDefines: true,
98-
cancelToken),
99-
equivalenceKey: WithPreprocessorDefinesKey),
83+
equivalenceKey: ConvertToGeneratedDllImportKey),
10084
context.Diagnostics);
10185
}
10286

@@ -106,7 +90,6 @@ private async Task<Document> ConvertToGeneratedDllImport(
10690
IMethodSymbol methodSymbol,
10791
AttributeData dllImportAttr,
10892
INamedTypeSymbol generatedDllImportAttrType,
109-
bool usePreprocessorDefines,
11093
CancellationToken cancellationToken)
11194
{
11295
DocumentEditor editor = await DocumentEditor.CreateAsync(doc, cancellationToken).ConfigureAwait(false);
@@ -142,49 +125,8 @@ private async Task<Document> ConvertToGeneratedDllImport(
142125
.WithIsExtern(false)
143126
.WithPartial(true));
144127

145-
if (!usePreprocessorDefines)
146-
{
147-
// Replace the original method with the updated one
148-
editor.ReplaceNode(methodSyntax, generatedDeclaration);
149-
}
150-
else
151-
{
152-
// #if DLLIMPORTGENERATOR_ENABLED
153-
generatedDeclaration = generatedDeclaration.WithLeadingTrivia(
154-
generatedDeclaration.GetLeadingTrivia()
155-
.AddRange(new[] {
156-
SyntaxFactory.Trivia(SyntaxFactory.IfDirectiveTrivia(SyntaxFactory.IdentifierName("DLLIMPORTGENERATOR_ENABLED"), isActive: true, branchTaken: true, conditionValue: true)),
157-
SyntaxFactory.ElasticMarker
158-
}));
159-
160-
// #else
161-
generatedDeclaration = generatedDeclaration.WithTrailingTrivia(
162-
generatedDeclaration.GetTrailingTrivia()
163-
.AddRange(new[] {
164-
SyntaxFactory.Trivia(SyntaxFactory.ElseDirectiveTrivia(isActive: false, branchTaken: false)),
165-
SyntaxFactory.ElasticMarker
166-
}));
167-
168-
// Sort attribute arguments so that GeneratedDllImport and DllImport match
169-
MethodDeclarationSyntax updatedDeclaration = (MethodDeclarationSyntax)generator.ReplaceNode(methodSyntax, dllImportSyntax, SortDllImportAttributeArguments(dllImportSyntax, generator));
170-
171-
// Remove existing leading trivia - it will be on the GeneratedDllImport method
172-
updatedDeclaration = updatedDeclaration.WithLeadingTrivia();
173-
174-
// #endif
175-
updatedDeclaration = updatedDeclaration.WithTrailingTrivia(
176-
methodSyntax.GetTrailingTrivia()
177-
.AddRange(new[] {
178-
SyntaxFactory.Trivia(SyntaxFactory.EndIfDirectiveTrivia(isActive: true)),
179-
SyntaxFactory.ElasticMarker
180-
}));
181-
182-
// Add the GeneratedDllImport method
183-
editor.InsertBefore(methodSyntax, generatedDeclaration);
184-
185-
// Replace the original method with the updated DllImport method
186-
editor.ReplaceNode(methodSyntax, updatedDeclaration);
187-
}
128+
// Replace the original method with the updated one
129+
editor.ReplaceNode(methodSyntax, generatedDeclaration);
188130

189131
return editor.GetChangedDocument();
190132
}

src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/Resources.Designer.cs

Lines changed: 10 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/Resources.resx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,22 @@
171171
<data name="ConstantAndElementCountInfoDisallowed" xml:space="preserve">
172172
<value>Only one of 'ConstantElementCount' or 'ElementCountInfo' may be used in a 'MarshalUsingAttribute' for a given 'ElementIndirectionLevel'</value>
173173
</data>
174+
<data name="ConvertToGeneratedDllImport" xml:space="preserve">
175+
<value>Convert to 'GeneratedDllImport'</value>
176+
</data>
174177
<data name="ConvertToGeneratedDllImportDescription" xml:space="preserve">
175178
<value>Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time</value>
176179
</data>
177180
<data name="ConvertToGeneratedDllImportMessage" xml:space="preserve">
178181
<value>Mark the method '{0}' with 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time</value>
179182
</data>
180-
<data name="ConvertToGeneratedDllImportNoPreprocessor" xml:space="preserve">
181-
<value>Convert to 'GeneratedDllImport'</value>
182-
</data>
183183
<data name="ConvertToGeneratedDllImportTitle" xml:space="preserve">
184184
<value>Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time</value>
185185
</data>
186186
<data name="ConvertToGeneratedDllImportWarning" xml:space="preserve">
187187
<value>Conversion to 'GeneratedDllImport' may change behavior and compatibility. See {0} for more information.</value>
188188
<comment>{0} is a documentation link</comment>
189189
</data>
190-
<data name="ConvertToGeneratedDllImportWithPreprocessor" xml:space="preserve">
191-
<value>Convert to 'GeneratedDllImport' under a preprocessor define</value>
192-
</data>
193190
<data name="CustomTypeMarshallingManagedToNativeUnsupported" xml:space="preserve">
194191
<value>The specified parameter needs to be marshalled from managed to native, but the native type '{0}' does not support it.</value>
195192
</data>

0 commit comments

Comments
 (0)