Skip to content

Commit

Permalink
Remove GeneratedDllImportAttribute.PreserveSig (#65164)
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung authored Feb 11, 2022
1 parent 1ba0394 commit 49fea67
Show file tree
Hide file tree
Showing 13 changed files with 14 additions and 506 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,12 @@ public static object BindToMoniker(string monikerName)

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2050:UnrecognizedReflectionPattern",
Justification = "The calling method is annotated with RequiresUnreferencedCode")]
[GeneratedDllImport(Interop.Libraries.Ole32, PreserveSig = false)]
[GeneratedDllImport(Interop.Libraries.Ole32)]
private static partial int MkParseDisplayName(IntPtr pbc, [MarshalAs(UnmanagedType.LPWStr)] string szUserName, out uint pchEaten, out IntPtr ppmk);

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2050:UnrecognizedReflectionPattern",
Justification = "The calling method is annotated with RequiresUnreferencedCode")]
[GeneratedDllImport(Interop.Libraries.Ole32, PreserveSig = false)]
[GeneratedDllImport(Interop.Libraries.Ole32)]
private static partial int BindMoniker(IntPtr pmk, uint grfOpt, ref Guid iidResult, out IntPtr ppvResult);

[SupportedOSPlatform("windows")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
[GeneratedDllImport(Interop.Libraries.Kernel32, CharSet = CharSet.Unicode, PreserveSig = true)]
[GeneratedDllImport(Interop.Libraries.Kernel32, CharSet = CharSet.Unicode)]
internal static partial int GetUserDefaultLCID();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ sealed class GeneratedDllImportAttribute : Attribute
public CharSet CharSet { get; set; }
public string? EntryPoint { get; set; }
public bool ExactSpelling { get; set; }
public bool PreserveSig { get; set; }
public bool SetLastError { get; set; }

public GeneratedDllImportAttribute(string dllName)
{
this.Value = dllName;
LibraryName = dllName;
}

public string Value { get; private set; }
public string LibraryName { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ namespace Microsoft.Interop
[Generator]
public sealed class DllImportGenerator : IIncrementalGenerator
{
private const string GeneratedDllImport = nameof(GeneratedDllImport);
private const string GeneratedDllImportAttribute = nameof(GeneratedDllImportAttribute);

internal sealed record IncrementalStubGenerationContext(
StubEnvironment Environment,
DllImportStubContext StubContext,
Expand Down Expand Up @@ -351,7 +348,6 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu
CharSet charSet = CharSet.Ansi;
string? entryPoint = null;
bool exactSpelling = false; // VB has different and unusual default behavior here.
bool preserveSig = true;
bool setLastError = false;

// All other data on attribute is defined as NamedArguments.
Expand All @@ -360,7 +356,7 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu
switch (namedArg.Key)
{
default:
Debug.Fail($"An unknown member was found on {GeneratedDllImport}");
Debug.Fail($"An unknown member was found on {attrData.AttributeClass}");
continue;
case nameof(GeneratedDllImportData.CharSet):
userDefinedValues |= DllImportMember.CharSet;
Expand All @@ -374,10 +370,6 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu
userDefinedValues |= DllImportMember.ExactSpelling;
exactSpelling = (bool)namedArg.Value.Value!;
break;
case nameof(GeneratedDllImportData.PreserveSig):
userDefinedValues |= DllImportMember.PreserveSig;
preserveSig = (bool)namedArg.Value.Value!;
break;
case nameof(GeneratedDllImportData.SetLastError):
userDefinedValues |= DllImportMember.SetLastError;
setLastError = (bool)namedArg.Value.Value!;
Expand All @@ -391,7 +383,6 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu
CharSet = charSet,
EntryPoint = entryPoint,
ExactSpelling = exactSpelling,
PreserveSig = preserveSig,
SetLastError = setLastError,
};
}
Expand Down Expand Up @@ -584,12 +575,6 @@ private static AttributeSyntax CreateDllImportAttributeForTarget(GeneratedDllImp
ExpressionSyntax value = CreateBoolExpressionSyntax(targetDllImportData.ExactSpelling);
newAttributeArgs.Add(AttributeArgument(name, null, value));
}
if (targetDllImportData.IsUserDefined.HasFlag(DllImportMember.PreserveSig))
{
NameEqualsSyntax name = NameEquals(nameof(DllImportAttribute.PreserveSig));
ExpressionSyntax value = CreateBoolExpressionSyntax(targetDllImportData.PreserveSig);
newAttributeArgs.Add(AttributeArgument(name, null, value));
}
if (targetDllImportData.IsUserDefined.HasFlag(DllImportMember.SetLastError))
{
NameEqualsSyntax name = NameEquals(nameof(DllImportAttribute.SetLastError));
Expand Down Expand Up @@ -629,9 +614,6 @@ static ExpressionSyntax CreateEnumExpressionSyntax<T>(T value) where T : Enum
private static GeneratedDllImportData GetTargetDllImportDataFromStubData(GeneratedDllImportData dllImportData, string originalMethodName, bool forwardAll)
{
DllImportMember membersToForward = DllImportMember.All
// https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.dllimportattribute.preservesig
// If PreserveSig=false (default is true), the P/Invoke stub checks/converts a returned HRESULT to an exception.
& ~DllImportMember.PreserveSig
// https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.dllimportattribute.setlasterror
// If SetLastError=true (default is false), the P/Invoke stub gets/caches the last error after invoking the native function.
& ~DllImportMember.SetLastError;
Expand All @@ -646,7 +628,6 @@ private static GeneratedDllImportData GetTargetDllImportDataFromStubData(Generat
EntryPoint = dllImportData.EntryPoint,
ExactSpelling = dllImportData.ExactSpelling,
SetLastError = dllImportData.SetLastError,
PreserveSig = dllImportData.PreserveSig,
IsUserDefined = dllImportData.IsUserDefined & membersToForward
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,35 +218,8 @@ private static (ImmutableArray<TypePositionInfo>, IMarshallingGeneratorFactory)

IMarshallingGeneratorFactory elementFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, new AttributedMarshallingModelOptions(options, runtimeMarshallingDisabled));
// We don't need to include the later generator factories for collection elements
// as the later generator factories only apply to parameters or to the synthetic return value for PreserveSig support.
// as the later generator factories only apply to parameters.
generatorFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, elementFactory, new AttributedMarshallingModelOptions(options, runtimeMarshallingDisabled));
if (!dllImportData.PreserveSig)
{
// Create type info for native out param
if (!method.ReturnsVoid)
{
// Transform the managed return type info into an out parameter and add it as the last param
TypePositionInfo nativeOutInfo = retTypeInfo with
{
InstanceIdentifier = PInvokeStubCodeGenerator.ReturnIdentifier,
RefKind = RefKind.Out,
RefKindSyntax = SyntaxKind.OutKeyword,
ManagedIndex = TypePositionInfo.ReturnIndex,
NativeIndex = typeInfos.Count
};
typeInfos.Add(nativeOutInfo);
}

// Use a marshalling generator that supports the HRESULT return->exception marshalling.
generatorFactory = new NoPreserveSigMarshallingGeneratorFactory(generatorFactory);

// Create type info for native HRESULT return
retTypeInfo = new TypePositionInfo(SpecialTypeInfo.Int32, NoMarshallingInfo.Instance);
retTypeInfo = retTypeInfo with
{
NativeIndex = TypePositionInfo.ReturnIndex
};
}

generatorFactory = new ByValueContentsMarshalKindValidator(generatorFactory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public enum DllImportMember
CharSet = 1 << 0,
EntryPoint = 1 << 1,
ExactSpelling = 1 << 2,
PreserveSig = 1 << 3,
SetLastError = 1 << 4,
SetLastError = 1 << 3,
All = ~None
}

Expand All @@ -39,7 +38,6 @@ public sealed record GeneratedDllImportData(string ModuleName)
public CharSet CharSet { get; init; }
public string? EntryPoint { get; init; }
public bool ExactSpelling { get; init; }
public bool PreserveSig { get; init; }
public bool SetLastError { get; init; }
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 49fea67

Please sign in to comment.