Skip to content

Commit

Permalink
Update DllImportGenerator style to match repo conventions (#59753)
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung authored Sep 30, 2021
1 parent 752533d commit 853297a
Show file tree
Hide file tree
Showing 35 changed files with 375 additions and 376 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ unsafe ref struct ArrayMarshaller<T>
private IntPtr _allocatedMemory;

public ArrayMarshaller(int sizeOfNativeElement)
:this()
: this()
{
_sizeOfNativeElement = sizeOfNativeElement;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public MarshalUsingAttribute()
}

public MarshalUsingAttribute(Type nativeType)
:this()
: this()
{
NativeType = nativeType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;

// Make sure the method has the DllImportAttribute
AttributeData? dllImportAttr;
if (!TryGetAttribute(methodSymbol, dllImportAttrType, out dllImportAttr))
if (!TryGetAttribute(methodSymbol, dllImportAttrType, out AttributeData? dllImportAttr))
return;

// Register code fixes with two options for the fix - using preprocessor or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ private void PrepareForAnalysis(CompilationStartAnalysisContext context)

private class PerCompilationAnalyzer
{
private readonly INamedTypeSymbol GeneratedMarshallingAttribute;
private readonly INamedTypeSymbol BlittableTypeAttribute;
private readonly INamedTypeSymbol NativeMarshallingAttribute;
private readonly INamedTypeSymbol MarshalUsingAttribute;
private readonly INamedTypeSymbol GenericContiguousCollectionMarshallerAttribute;
private readonly INamedTypeSymbol SpanOfByte;
private readonly INamedTypeSymbol StructLayoutAttribute;
private readonly INamedTypeSymbol _generatedMarshallingAttribute;
private readonly INamedTypeSymbol _blittableTypeAttribute;
private readonly INamedTypeSymbol _nativeMarshallingAttribute;
private readonly INamedTypeSymbol _marshalUsingAttribute;
private readonly INamedTypeSymbol _genericContiguousCollectionMarshallerAttribute;
private readonly INamedTypeSymbol _spanOfByte;
private readonly INamedTypeSymbol _structLayoutAttribute;

public PerCompilationAnalyzer(INamedTypeSymbol generatedMarshallingAttribute,
INamedTypeSymbol blittableTypeAttribute,
Expand All @@ -241,13 +241,13 @@ public PerCompilationAnalyzer(INamedTypeSymbol generatedMarshallingAttribute,
INamedTypeSymbol spanOfByte,
INamedTypeSymbol structLayoutAttribute)
{
GeneratedMarshallingAttribute = generatedMarshallingAttribute;
BlittableTypeAttribute = blittableTypeAttribute;
NativeMarshallingAttribute = nativeMarshallingAttribute;
MarshalUsingAttribute = marshalUsingAttribute;
GenericContiguousCollectionMarshallerAttribute = genericContiguousCollectionMarshallerAttribute;
SpanOfByte = spanOfByte;
StructLayoutAttribute = structLayoutAttribute;
_generatedMarshallingAttribute = generatedMarshallingAttribute;
_blittableTypeAttribute = blittableTypeAttribute;
_nativeMarshallingAttribute = nativeMarshallingAttribute;
_marshalUsingAttribute = marshalUsingAttribute;
_genericContiguousCollectionMarshallerAttribute = genericContiguousCollectionMarshallerAttribute;
_spanOfByte = spanOfByte;
_structLayoutAttribute = structLayoutAttribute;
}

public void AnalyzeTypeDefinition(SymbolAnalysisContext context)
Expand All @@ -258,17 +258,17 @@ public void AnalyzeTypeDefinition(SymbolAnalysisContext context)
AttributeData? nativeMarshallingAttributeData = null;
foreach (var attr in type.GetAttributes())
{
if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, GeneratedMarshallingAttribute))
if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, _generatedMarshallingAttribute))
{
// If the type has the GeneratedMarshallingAttribute,
// we let the source generator handle error checking.
return;
}
else if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, BlittableTypeAttribute))
else if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, _blittableTypeAttribute))
{
blittableTypeAttributeData = attr;
}
else if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, NativeMarshallingAttribute))
else if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, _nativeMarshallingAttribute))
{
nativeMarshallingAttributeData = attr;
}
Expand All @@ -281,7 +281,7 @@ public void AnalyzeTypeDefinition(SymbolAnalysisContext context)
CannotHaveMultipleMarshallingAttributesRule,
type.ToDisplayString()));
}
else if (blittableTypeAttributeData is not null && (!type.HasOnlyBlittableFields() || type.IsAutoLayout(StructLayoutAttribute)))
else if (blittableTypeAttributeData is not null && (!type.HasOnlyBlittableFields() || type.IsAutoLayout(_structLayoutAttribute)))
{
context.ReportDiagnostic(
blittableTypeAttributeData.CreateDiagnostic(
Expand All @@ -290,7 +290,7 @@ public void AnalyzeTypeDefinition(SymbolAnalysisContext context)
}
else if (nativeMarshallingAttributeData is not null)
{
AnalyzeNativeMarshalerType(context, type, nativeMarshallingAttributeData, isNativeMarshallingAttribute:true);
AnalyzeNativeMarshalerType(context, type, nativeMarshallingAttributeData, isNativeMarshallingAttribute: true);
}
}

Expand All @@ -307,7 +307,7 @@ private bool HasMultipleMarshallingAttributes(AttributeData? blittableTypeAttrib

public void AnalyzeElement(SymbolAnalysisContext context)
{
AttributeData? attrData = context.Symbol.GetAttributes().FirstOrDefault(attr => SymbolEqualityComparer.Default.Equals(MarshalUsingAttribute, attr.AttributeClass));
AttributeData? attrData = context.Symbol.GetAttributes().FirstOrDefault(attr => SymbolEqualityComparer.Default.Equals(_marshalUsingAttribute, attr.AttributeClass));
if (attrData is not null)
{
if (context.Symbol is IParameterSymbol param)
Expand All @@ -324,7 +324,7 @@ public void AnalyzeElement(SymbolAnalysisContext context)
public void AnalyzeReturnType(SymbolAnalysisContext context)
{
var method = (IMethodSymbol)context.Symbol;
AttributeData? attrData = method.GetReturnTypeAttributes().FirstOrDefault(attr => SymbolEqualityComparer.Default.Equals(MarshalUsingAttribute, attr.AttributeClass));
AttributeData? attrData = method.GetReturnTypeAttributes().FirstOrDefault(attr => SymbolEqualityComparer.Default.Equals(_marshalUsingAttribute, attr.AttributeClass));
if (attrData is not null)
{
AnalyzeNativeMarshalerType(context, method.ReturnType, attrData, isNativeMarshallingAttribute: false);
Expand Down Expand Up @@ -364,12 +364,12 @@ private void AnalyzeNativeMarshalerType(SymbolAnalysisContext context, ITypeSymb
DiagnosticDescriptor requiredShapeRule = NativeTypeMustHaveRequiredShapeRule;

ManualTypeMarshallingHelper.NativeTypeMarshallingVariant variant = ManualTypeMarshallingHelper.NativeTypeMarshallingVariant.Standard;
if (marshalerType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(GenericContiguousCollectionMarshallerAttribute, a.AttributeClass)))
if (marshalerType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(_genericContiguousCollectionMarshallerAttribute, a.AttributeClass)))
{
variant = ManualTypeMarshallingHelper.NativeTypeMarshallingVariant.ContiguousCollection;
requiredShapeRule = CollectionNativeTypeMustHaveRequiredShapeRule;
if (!ManualTypeMarshallingHelper.TryGetManagedValuesProperty(marshalerType, out _)
|| !ManualTypeMarshallingHelper.HasNativeValueStorageProperty(marshalerType, SpanOfByte))
|| !ManualTypeMarshallingHelper.HasNativeValueStorageProperty(marshalerType, _spanOfByte))
{
context.ReportDiagnostic(
GetDiagnosticLocations(context, marshalerType, nativeMarshalerAttributeData).CreateDiagnostic(
Expand Down Expand Up @@ -425,7 +425,7 @@ private void AnalyzeNativeMarshalerType(SymbolAnalysisContext context, ITypeSymb

hasConstructor = hasConstructor || ManualTypeMarshallingHelper.IsManagedToNativeConstructor(ctor, type, variant);

if (!hasStackallocConstructor && ManualTypeMarshallingHelper.IsStackallocConstructor(ctor, type, SpanOfByte, variant))
if (!hasStackallocConstructor && ManualTypeMarshallingHelper.IsStackallocConstructor(ctor, type, _spanOfByte, variant))
{
hasStackallocConstructor = true;
IFieldSymbol stackAllocSizeField = nativeType.GetMembers("StackBufferSize").OfType<IFieldSymbol>().FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ internal static class Comparers
/// <typeparam name="T">The type of immutable array element.</typeparam>
internal class ImmutableArraySequenceEqualComparer<T> : IEqualityComparer<ImmutableArray<T>>
{
private readonly IEqualityComparer<T> elementComparer;
private readonly IEqualityComparer<T> _elementComparer;

/// <summary>
/// Creates an <see cref="ImmutableArraySequenceEqualComparer{T}"/> with a custom comparer for the elements of the collection.
/// </summary>
/// <param name="elementComparer">The comparer instance for the collection elements.</param>
public ImmutableArraySequenceEqualComparer(IEqualityComparer<T> elementComparer)
{
this.elementComparer = elementComparer;
_elementComparer = elementComparer;
}

public bool Equals(ImmutableArray<T> x, ImmutableArray<T> y)
{
return x.SequenceEqual(y, elementComparer);
return x.SequenceEqual(y, _elementComparer);
}

public int GetHashCode(ImmutableArray<T> obj)
Expand All @@ -76,18 +76,18 @@ public int GetHashCode(SyntaxNode obj)

internal class CustomValueTupleElementComparer<T, U> : IEqualityComparer<(T, U)>
{
private readonly IEqualityComparer<T> item1Comparer;
private readonly IEqualityComparer<U> item2Comparer;
private readonly IEqualityComparer<T> _item1Comparer;
private readonly IEqualityComparer<U> _item2Comparer;

public CustomValueTupleElementComparer(IEqualityComparer<T> item1Comparer, IEqualityComparer<U> item2Comparer)
{
this.item1Comparer = item1Comparer;
this.item2Comparer = item2Comparer;
_item1Comparer = item1Comparer;
_item2Comparer = item2Comparer;
}

public bool Equals((T, U) x, (T, U) y)
{
return item1Comparer.Equals(x.Item1, y.Item1) && item2Comparer.Equals(x.Item2, y.Item2);
return _item1Comparer.Equals(x.Item1, y.Item1) && _item2Comparer.Equals(x.Item2, y.Item2);
}

public int GetHashCode((T, U) obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static IDisposable SourceGenerationStartStop(int methodCount)
[Event(StartSourceGenerationEventId, Level = EventLevel.Informational, Keywords = Keywords.SourceGeneration)]
public void SourceGenerationStart(int methodCount)
{
this.WriteEvent(StartSourceGenerationEventId, methodCount);
WriteEvent(StartSourceGenerationEventId, methodCount);
}

/// <summary>
Expand All @@ -54,7 +54,7 @@ public void SourceGenerationStart(int methodCount)
[Event(StopSourceGenerationEventId, Level = EventLevel.Informational, Keywords = Keywords.SourceGeneration)]
public void SourceGenerationStop()
{
this.WriteEvent(StopSourceGenerationEventId);
WriteEvent(StopSourceGenerationEventId);
}

private class StartStopEvent : IDisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public sealed class DllImportGenerator : IIncrementalGenerator
private const string GeneratedDllImport = nameof(GeneratedDllImport);
private const string GeneratedDllImportAttribute = nameof(GeneratedDllImportAttribute);

private static readonly Version MinimumSupportedFrameworkVersion = new Version(5, 0);
private static readonly Version s_minimumSupportedFrameworkVersion = new Version(5, 0);

internal sealed record IncrementalStubGenerationContext(DllImportStubContext StubContext, ImmutableArray<AttributeSyntax> ForwardedAttributes, GeneratedDllImportData DllImportData, ImmutableArray<Diagnostic> Diagnostics)
{
Expand Down Expand Up @@ -57,10 +57,10 @@ public enum StepName

public record ExecutedStepInfo(StepName Step, object Input);

private List<ExecutedStepInfo> executedSteps = new();
public IEnumerable<ExecutedStepInfo> ExecutedSteps => executedSteps;
private readonly List<ExecutedStepInfo> _executedSteps = new();
public IEnumerable<ExecutedStepInfo> ExecutedSteps => _executedSteps;

internal void RecordExecutedStep(ExecutedStepInfo step) => executedSteps.Add(step);
internal void RecordExecutedStep(ExecutedStepInfo step) => _executedSteps.Add(step);
}

/// <summary>
Expand Down Expand Up @@ -106,7 +106,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
Diagnostic.Create(
GeneratorDiagnostics.TargetFrameworkNotSupported,
Location.None,
MinimumSupportedFrameworkVersion.ToString(2)));
s_minimumSupportedFrameworkVersion.ToString(2)));
}
});

Expand Down Expand Up @@ -294,7 +294,7 @@ private static bool IsSupportedTargetFramework(Compilation compilation, out Vers
// .NET Standard
"netstandard" => false,
// .NET Core (when version < 5.0) or .NET
"System.Runtime" or "System.Private.CoreLib" => version >= MinimumSupportedFrameworkVersion,
"System.Runtime" or "System.Private.CoreLib" => version >= s_minimumSupportedFrameworkVersion,
_ => false,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ internal record StubEnvironment(

internal sealed class DllImportStubContext : IEquatable<DllImportStubContext>
{
// We don't need the warnings around not setting the various
// non-nullable fields/properties on this type in the constructor
// since we always use a property initializer.
// We don't need the warnings around not setting the various
// non-nullable fields/properties on this type in the constructor
// since we always use a property initializer.
#pragma warning disable 8618
private DllImportStubContext()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Microsoft.Interop
{
internal class ForwarderMarshallingGeneratorFactory : IMarshallingGeneratorFactory
{
private static readonly Forwarder Forwarder = new Forwarder();
private static readonly Forwarder s_forwarder = new Forwarder();

public IMarshallingGenerator Create(TypePositionInfo info, StubCodeContext context) => Forwarder;
public IMarshallingGenerator Create(TypePositionInfo info, StubCodeContext context) => s_forwarder;
}
}
Loading

0 comments on commit 853297a

Please sign in to comment.