C#: Compare symbols using SymbolEqualityComparer.

This commit is contained in:
Calum Grant
2019-11-22 12:51:19 +00:00
parent ca195e9340
commit 5f6527a183
13 changed files with 29 additions and 24 deletions

View File

@@ -23,6 +23,7 @@ namespace Semmle.Extraction.CIL.Entities
public Attribute(Context cx, IEntity @object, CustomAttributeHandle handle) : base(cx)
{
attrib = cx.mdReader.GetCustomAttribute(handle);
this.handle = handle;
this.@object = @object;
}

View File

@@ -24,7 +24,7 @@ namespace Semmle.Extraction.CSharp.Entities
// But for properties/indexers that implement explicit interfaces, Roslyn
// does not properly populate `AssociatedSymbol`
var props = symbol.ContainingType.GetMembers().OfType<IPropertySymbol>();
props = props.Where(p => symbol.Equals(p.GetMethod) || symbol.Equals(p.SetMethod));
props = props.Where(p => SymbolEqualityComparer.Default.Equals(symbol, p.GetMethod) || SymbolEqualityComparer.Default.Equals(symbol, p.SetMethod));
return props.SingleOrDefault();
}
}
@@ -47,12 +47,12 @@ namespace Semmle.Extraction.CSharp.Entities
var parent = Property.Create(Context, prop);
int kind;
Accessor unboundAccessor;
if (symbol.Equals(prop.GetMethod))
if (SymbolEqualityComparer.Default.Equals(symbol, prop.GetMethod))
{
kind = 1;
unboundAccessor = Create(Context, prop.OriginalDefinition.GetMethod);
}
else if (symbol.Equals(prop.SetMethod))
else if (SymbolEqualityComparer.Default.Equals(symbol, prop.SetMethod))
{
kind = 2;
unboundAccessor = Create(Context, prop.OriginalDefinition.SetMethod);

View File

@@ -20,7 +20,7 @@ namespace Semmle.Extraction.CSharp.Entities
static new Destructor OriginalDefinition(Context cx, Destructor original, IMethodSymbol symbol)
{
return symbol.OriginalDefinition == null || Equals(symbol.OriginalDefinition, symbol) ? original : Create(cx, symbol.OriginalDefinition);
return symbol.OriginalDefinition == null || SymbolEqualityComparer.Default.Equals(symbol.OriginalDefinition, symbol) ? original : Create(cx, symbol.OriginalDefinition);
}
public new static Destructor Create(Context cx, IMethodSymbol symbol) =>

View File

@@ -28,12 +28,12 @@ namespace Semmle.Extraction.CSharp.Entities
var parent = Event.Create(Context, @event);
int kind;
EventAccessor unboundAccessor;
if (symbol.Equals(@event.AddMethod))
if (SymbolEqualityComparer.Default.Equals(symbol, @event.AddMethod))
{
kind = 1;
unboundAccessor = Create(Context, @event.OriginalDefinition.AddMethod);
}
else if (symbol.Equals(@event.RemoveMethod))
else if (SymbolEqualityComparer.Default.Equals(symbol, @event.RemoveMethod))
{
kind = 2;
unboundAccessor = Create(Context, @event.OriginalDefinition.RemoveMethod);

View File

@@ -106,7 +106,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
{
var collectionInfo = cx.GetModel(Syntax).GetCollectionInitializerSymbolInfo(i);
var addMethod = Method.Create(cx, collectionInfo.Symbol as IMethodSymbol);
var voidType = Entities.Type.Create(cx, new AnnotatedTypeSymbol(cx.Compilation.GetSpecialType(SpecialType.System_Void), NullableAnnotation.NotApplicable));
var voidType = Entities.Type.Create(cx, new AnnotatedTypeSymbol(cx.Compilation.GetSpecialType(SpecialType.System_Void), NullableAnnotation.None));
var invocation = new Expression(new ExpressionInfo(cx, voidType, cx.Create(i.GetLocation()), ExprKind.METHOD_INVOCATION, this, child++, false, null));

View File

@@ -40,7 +40,7 @@ namespace Semmle.Extraction.CSharp.Entities
foreach (var p in parameters.Zip(originalParameters, (paramSymbol, originalParam) => new { paramSymbol, originalParam }))
{
var original = Equals(p.paramSymbol, p.originalParam) ? null : Parameter.Create(Context, p.originalParam, originalMethod);
var original = SymbolEqualityComparer.Default.Equals(p.paramSymbol, p.originalParam) ? null : Parameter.Create(Context, p.originalParam, originalMethod);
Parameter.Create(Context, p.paramSymbol, this, original);
}
@@ -117,7 +117,7 @@ namespace Semmle.Extraction.CSharp.Entities
if (m.symbol.IsGenericMethod)
{
if (Equals(m.symbol, m.symbol.OriginalDefinition))
if (SymbolEqualityComparer.Default.Equals(m.symbol, m.symbol.OriginalDefinition))
{
trapFile.Write('`');
trapFile.Write(m.symbol.TypeParameters.Length);
@@ -318,7 +318,7 @@ namespace Semmle.Extraction.CSharp.Entities
/// <summary>
/// Whether this method has unbound type parameters.
/// </summary>
public bool IsUnboundGeneric => IsGeneric && Equals(symbol.ConstructedFrom, symbol);
public bool IsUnboundGeneric => IsGeneric && SymbolEqualityComparer.Default.Equals(symbol.ConstructedFrom, symbol);
public bool IsBoundGeneric => IsGeneric && !IsUnboundGeneric;

View File

@@ -277,7 +277,7 @@ namespace Semmle.Extraction.CSharp.Entities
if (other == null || other.GetType() != typeof(ConstructedExtensionParameter))
return false;
return Equals(symbol, other.symbol) && Equals(ConstructedType, other.ConstructedType);
return SymbolEqualityComparer.Default.Equals(symbol, other.symbol) && SymbolEqualityComparer.Default.Equals(ConstructedType, other.ConstructedType);
}
public static ConstructedExtensionParameter Create(Context cx, Method method, Parameter parameter) =>

View File

@@ -32,7 +32,7 @@ namespace Semmle.Extraction.CSharp.Entities
public bool IsOblivious => Annotation == 0 && NullableParameters.Length == 0;
static readonly Nullability oblivious = new Nullability(NullableAnnotation.Disabled);
static readonly Nullability oblivious = new Nullability(NullableAnnotation.None);
static readonly Nullability annotated = new Nullability(NullableAnnotation.Annotated);
static readonly Nullability notannotated = new Nullability(NullableAnnotation.NotAnnotated);
@@ -244,14 +244,14 @@ namespace Semmle.Extraction.CSharp.Entities
/// This has not yet been exposed on the public API.
/// </summary>
public static IEnumerable<AnnotatedTypeSymbol> GetAnnotatedTypeArguments(this INamedTypeSymbol symbol) =>
symbol.TypeArguments.Zip(symbol.TypeArgumentsNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
symbol.TypeArguments.Zip(symbol.TypeArgumentNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
/// <summary>
/// Gets the annotated type arguments of an IMethodSymbol.
/// This has not yet been exposed on the public API.
/// </summary>
public static IEnumerable<AnnotatedTypeSymbol> GetAnnotatedTypeArguments(this IMethodSymbol symbol) =>
symbol.TypeArguments.Zip(symbol.TypeArgumentsNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
symbol.TypeArguments.Zip(symbol.TypeArgumentNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
/// <summary>
/// Gets the annotated type constraints of an ITypeParameterSymbol.

View File

@@ -36,7 +36,7 @@ namespace Semmle.Extraction.CSharp.Entities
public static bool ConstructedOrParentIsConstructed(INamedTypeSymbol symbol)
{
return !Equals(symbol, symbol.OriginalDefinition) ||
return !SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition) ||
symbol.ContainingType != null && ConstructedOrParentIsConstructed(symbol.ContainingType);
}
@@ -155,7 +155,7 @@ namespace Semmle.Extraction.CSharp.Entities
{
var param = invokeMethod.Parameters[i];
var originalParam = invokeMethod.OriginalDefinition.Parameters[i];
var originalParamEntity = Equals(param, originalParam) ? null :
var originalParamEntity = SymbolEqualityComparer.Default.Equals(param, originalParam) ? null :
DelegateTypeParameter.Create(Context, originalParam, Create(Context, ((INamedTypeSymbol)symbol).OriginalDefinition));
DelegateTypeParameter.Create(Context, param, this, originalParamEntity);
}

View File

@@ -115,7 +115,7 @@ namespace Semmle.Extraction.CSharp
case TypeKind.TypeParameter:
var tp = (ITypeParameterSymbol)type;
var declaringGen = tp.TypeParameterKind == TypeParameterKind.Method ? tp.DeclaringMethod : (ISymbol)tp.DeclaringType;
return Equals(declaringGen, declaringGeneric);
return SymbolEqualityComparer.Default.Equals(declaringGen, declaringGeneric);
default:
return false;
}
@@ -373,7 +373,7 @@ namespace Semmle.Extraction.CSharp
}
public static bool IsReallyUnbound(this INamedTypeSymbol type) =>
Equals(type.ConstructedFrom, type) || type.IsUnboundGenericType;
SymbolEqualityComparer.Default.Equals(type.ConstructedFrom, type) || type.IsUnboundGenericType;
public static bool IsReallyBound(this INamedTypeSymbol type) => !IsReallyUnbound(type);
@@ -413,13 +413,13 @@ namespace Semmle.Extraction.CSharp
/// <summary>
/// Holds if this symbol is a source declaration.
/// </summary>
public static bool IsSourceDeclaration(this ISymbol symbol) => Equals(symbol, symbol.OriginalDefinition);
public static bool IsSourceDeclaration(this ISymbol symbol) => SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition);
/// <summary>
/// Holds if this method is a source declaration.
/// </summary>
public static bool IsSourceDeclaration(this IMethodSymbol method) =>
IsSourceDeclaration((ISymbol)method) && Equals(method, method.ConstructedFrom) && method.ReducedFrom == null;
IsSourceDeclaration((ISymbol)method) && SymbolEqualityComparer.Default.Equals(method, method.ConstructedFrom) && method.ReducedFrom == null;
/// <summary>
/// Holds if this parameter is a source declaration.
@@ -477,6 +477,6 @@ namespace Semmle.Extraction.CSharp
/// This has not yet been exposed on the public API.
/// </summary>
public static IEnumerable<AnnotatedTypeSymbol> GetAnnotatedTypeArguments(this INamedTypeSymbol symbol) =>
symbol.TypeArguments.Zip(symbol.TypeArgumentsNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
symbol.TypeArguments.Zip(symbol.TypeArgumentNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
}
}

View File

@@ -282,7 +282,9 @@ namespace Semmle.Extraction
/// of the symbol is a constructed generic.
/// </summary>
/// <param name="symbol">The symbol to populate.</param>
public bool Defines(ISymbol symbol) => !Equals(symbol, symbol.OriginalDefinition) || Scope.InScope(symbol);
public bool Defines(ISymbol symbol) =>
!SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition) ||
Scope.InScope(symbol);
/// <summary>
/// Whether the current extraction context defines a given file.

View File

@@ -36,7 +36,7 @@ namespace Semmle.Extraction.Entities
}
public override bool NeedsPopulation =>
!Equals(assembly, Context.Compilation.Assembly) || !Context.IsGlobalContext;
!SymbolEqualityComparer.Default.Equals(assembly, Context.Compilation.Assembly) || !Context.IsGlobalContext;
public override int GetHashCode() =>
symbol == null ? 91187354 : symbol.GetHashCode();

View File

@@ -46,7 +46,9 @@ namespace Semmle.Extraction
public bool InFileScope(string path) => path == filepath;
public bool InScope(ISymbol symbol) => Equals(symbol.ContainingAssembly, assembly) || Equals(symbol, assembly);
public bool InScope(ISymbol symbol) =>
SymbolEqualityComparer.Default.Equals(symbol.ContainingAssembly, assembly) ||
SymbolEqualityComparer.Default.Equals(symbol, assembly);
}
/// <summary>