Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ExactSpelling from GeneratedDllImport #65343

Merged
merged 9 commits into from
Feb 19, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,47 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
methodSymbol,
dllImportAttr!,
generatedDllImportAttrType,
entryPointSuffix: null,
cancelToken),
equivalenceKey: ConvertToGeneratedDllImportKey),
context.Diagnostics);

DllImportData dllImportData = methodSymbol.GetDllImportData()!;
if (!dllImportData.ExactSpelling)
{
if (dllImportData.CharacterSet is CharSet.None or CharSet.Ansi or CharSet.Auto)
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
{
context.RegisterCodeFix(
CodeAction.Create(
string.Format(Resources.ConvertToGeneratedDllImportWithSuffix, "A"),
cancelToken => ConvertToGeneratedDllImport(
context.Document,
methodSyntax,
methodSymbol,
dllImportAttr!,
generatedDllImportAttrType,
entryPointSuffix: 'A',
cancelToken),
equivalenceKey: ConvertToGeneratedDllImportKey + "A"),
context.Diagnostics);
}
if (dllImportData.CharacterSet is CharSet.Unicode or CharSet.Auto)
{
context.RegisterCodeFix(
CodeAction.Create(
string.Format(Resources.ConvertToGeneratedDllImportWithSuffix, "W"),
cancelToken => ConvertToGeneratedDllImport(
context.Document,
methodSyntax,
methodSymbol,
dllImportAttr!,
generatedDllImportAttrType,
entryPointSuffix: 'W',
cancelToken),
equivalenceKey: ConvertToGeneratedDllImportKey + "W"),
context.Diagnostics);
}
}
}

private async Task<Document> ConvertToGeneratedDllImport(
Expand All @@ -90,6 +128,7 @@ private async Task<Document> ConvertToGeneratedDllImport(
IMethodSymbol methodSymbol,
AttributeData dllImportAttr,
INamedTypeSymbol generatedDllImportAttrType,
char? entryPointSuffix,
CancellationToken cancellationToken)
{
DocumentEditor editor = await DocumentEditor.CreateAsync(doc, cancellationToken).ConfigureAwait(false);
Expand All @@ -104,6 +143,8 @@ private async Task<Document> ConvertToGeneratedDllImport(
dllImportSyntax,
methodSymbol.GetDllImportData()!,
generatedDllImportAttrType,
methodSymbol.Name,
entryPointSuffix,
out SyntaxNode? unmanagedCallConvAttributeMaybe);

// Add annotation about potential behavioural and compatibility changes
Expand Down Expand Up @@ -137,6 +178,8 @@ private SyntaxNode GetGeneratedDllImportAttribute(
AttributeSyntax dllImportSyntax,
DllImportData dllImportData,
INamedTypeSymbol generatedDllImportAttrType,
string methodName,
char? entryPointSuffix,
out SyntaxNode? unmanagedCallConvAttributeMaybe)
{
unmanagedCallConvAttributeMaybe = null;
Expand All @@ -147,6 +190,7 @@ private SyntaxNode GetGeneratedDllImportAttribute(

// Update attribute arguments for GeneratedDllImport
List<SyntaxNode> argumentsToRemove = new List<SyntaxNode>();
AttributeArgumentSyntax? entryPointAttributeArgument = null;
foreach (SyntaxNode argument in generator.GetAttributeArguments(generatedDllImportSyntax))
{
if (argument is not AttributeArgumentSyntax attrArg)
Expand Down Expand Up @@ -181,9 +225,49 @@ private SyntaxNode GetGeneratedDllImportAttribute(
argumentsToRemove.Add(argument);
}
}
else if (IsMatchingNamedArg(attrArg, nameof(DllImportAttribute.ExactSpelling)))
{
argumentsToRemove.Add(argument);
}
else if (IsMatchingNamedArg(attrArg, nameof(DllImportAttribute.EntryPoint)))
{
entryPointAttributeArgument = attrArg;
if (!dllImportData.ExactSpelling && entryPointSuffix.HasValue)
{
if (entryPointAttributeArgument.Expression.IsKind(SyntaxKind.StringLiteralExpression))
{
string? entryPoint = (string?)((LiteralExpressionSyntax)entryPointAttributeArgument.Expression).Token.Value;
if (entryPoint is not null)
{
entryPointAttributeArgument = entryPointAttributeArgument.WithExpression(
SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression,
SyntaxFactory.Literal(entryPoint + entryPointSuffix)));
}
}
else
{
entryPointAttributeArgument = entryPointAttributeArgument.WithExpression(
SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression,
entryPointAttributeArgument.Expression,
SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression,
SyntaxFactory.Literal(entryPointSuffix.ToString()))));
}
}
argumentsToRemove.Add(attrArg);
}
}

if (entryPointSuffix.HasValue && entryPointAttributeArgument is null)
{
entryPointAttributeArgument = (AttributeArgumentSyntax)generator.AttributeArgument("EntryPoint",
generator.LiteralExpression(methodName + entryPointSuffix.Value));
}

generatedDllImportSyntax = generator.RemoveNodes(generatedDllImportSyntax, argumentsToRemove);
if (entryPointAttributeArgument is not null)
{
generatedDllImportSyntax = generator.AddAttributeArguments(generatedDllImportSyntax, new[] { entryPointAttributeArgument });
}
return SortDllImportAttributeArguments((AttributeSyntax)generatedDllImportSyntax, generator);
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@
<value>Conversion to 'GeneratedDllImport' may change behavior and compatibility. See {0} for more information.</value>
<comment>{0} is a documentation link</comment>
</data>
<data name="ConvertToGeneratedDllImportWithSuffix" xml:space="preserve">
<value>Convert to 'GeneratedDllImport' with '{0}' suffix</value>
</data>
<data name="CustomTypeMarshallingManagedToNativeUnsupported" xml:space="preserve">
<value>The specified parameter needs to be marshalled from managed to native, but the native type '{0}' does not support it.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ partial class Test
[GeneratedDllImport(""DoesNotExist"",
CharSet = CharSet.Unicode,
EntryPoint = ""UserDefinedEntryPoint"",
ExactSpelling = true,
SetLastError = true)]
public static partial void Method();
}
Expand All @@ -215,16 +214,20 @@ partial class Test
[GeneratedDllImport(nameof(Test),
CharSet = (CharSet)2,
EntryPoint = EntryPointName,
ExactSpelling = 0 != 1,
SetLastError = IsFalse)]
public static partial void Method1();

[GeneratedDllImport(nameof(Test),
CharSet = (CharSet)Two,
EntryPoint = EntryPointName,
ExactSpelling = One != Two,
SetLastError = !IsTrue)]
public static partial void Method2();

[GeneratedDllImport(nameof(Test),
CharSet = (CharSet)2,
EntryPoint = EntryPointName,
SetLastError = 0 != 1)]
public static partial void Method3();
}
";

Expand Down
Loading