C#: Remove non-essential changes

This commit is contained in:
Calum Grant
2019-10-24 18:53:29 +01:00
parent a0fa7dad79
commit f00276a82c
9 changed files with 37 additions and 62 deletions

View File

@@ -19,7 +19,7 @@ namespace Semmle.Extraction.CSharp.Entities
if (symbol.IsGenericMethod && !IsSourceDeclaration)
{
trapFile.Write('<');
trapFile.BuildList(",", symbol.TypeArguments, (ta, tb0) => AddSignatureTypeToId(Context, tb0, symbol, ta, TypeIdentifierContext.MethodName));
trapFile.BuildList(",", symbol.TypeArguments, (ta, tb0) => AddSignatureTypeToId(Context, tb0, symbol, ta));
trapFile.Write('>');
}
trapFile.Write(";localfunction");

View File

@@ -129,7 +129,7 @@ namespace Semmle.Extraction.CSharp.Entities
// Type arguments with different nullability can result in
// a constructed method with different nullability of its parameters and return type,
// so we need to create a distinct database entity for it.
trapFile.BuildList(",", m.symbol.GetAnnotatedTypeArguments(), (ta, tb0) => { AddSignatureTypeToId(m.Context, tb0, m.symbol, ta.Symbol, TypeIdentifierContext.MethodName); trapFile.Write((int)ta.Nullability); });
trapFile.BuildList(",", m.symbol.GetAnnotatedTypeArguments(), (ta, tb0) => { AddSignatureTypeToId(m.Context, tb0, m.symbol, ta.Symbol); trapFile.Write((int)ta.Nullability); });
trapFile.Write('>');
}
}
@@ -199,10 +199,10 @@ namespace Semmle.Extraction.CSharp.Entities
/// to make the reference to <code>#3</code> in the label definition <code>#4</code> for
/// <code>T</code> valid.
/// </summary>
protected static void AddSignatureTypeToId(Context cx, TextWriter trapFile, IMethodSymbol method, ITypeSymbol type, TypeIdentifierContext tic)
protected static void AddSignatureTypeToId(Context cx, TextWriter trapFile, IMethodSymbol method, ITypeSymbol type)
{
if (type.ContainsTypeParameters(cx, method))
type.BuildTypeId(cx, trapFile, (cx0, tb0, type0, _) => AddSignatureTypeToId(cx, tb0, method, type0, tic), tic, method);
type.BuildTypeId(cx, trapFile, (cx0, tb0, type0) => AddSignatureTypeToId(cx, tb0, method, type0));
else
trapFile.WriteSubId(Type.Create(cx, type).TypeRef);
}
@@ -215,13 +215,13 @@ namespace Semmle.Extraction.CSharp.Entities
if (method.MethodKind == MethodKind.ReducedExtension)
{
trapFile.WriteSeparator(",", ref index);
AddSignatureTypeToId(cx, trapFile, method, method.ReceiverType, TypeIdentifierContext.MethodParam);
AddSignatureTypeToId(cx, trapFile, method, method.ReceiverType);
}
foreach (var param in method.Parameters)
{
trapFile.WriteSeparator(",", ref index);
AddSignatureTypeToId(cx, trapFile, method, param.Type, TypeIdentifierContext.MethodParam);
AddSignatureTypeToId(cx, trapFile, method, param.Type);
switch (param.RefKind)
{
case RefKind.Out:

View File

@@ -111,7 +111,7 @@ namespace Semmle.Extraction.CSharp.Entities
public override void WriteId(TextWriter trapFile)
{
symbol.BuildTypeId(Context, trapFile, (cx0, tb0, sub, _) => tb0.WriteSubId(Create(cx0, sub)), TypeIdentifierContext.TypeName, symbol);
symbol.BuildTypeId(Context, trapFile, (cx0, tb0, sub) => tb0.WriteSubId(Create(cx0, sub)));
trapFile.Write(";type");
}
@@ -177,12 +177,12 @@ namespace Semmle.Extraction.CSharp.Entities
public override void WriteId(TextWriter trapFile)
{
void ExpandType(Context cx0, TextWriter tb0, ITypeSymbol sub, ISymbol gc)
void ExpandType(Context cx0, TextWriter tb0, ITypeSymbol sub)
{
sub.BuildTypeId(cx0, tb0, ExpandType, TypeIdentifierContext.TypeRef, gc);
sub.BuildTypeId(cx0, tb0, ExpandType);
}
symbol.BuildTypeId(Context, trapFile, ExpandType, TypeIdentifierContext.TypeRef, symbol);
symbol.BuildTypeId(Context, trapFile, ExpandType);
trapFile.Write(";typeref");
}

View File

@@ -32,7 +32,7 @@ namespace Semmle.Extraction.CSharp.Entities
public override void WriteId(TextWriter trapFile)
{
symbol.BuildTypeId(Context, trapFile, (cx0, tb0, sub, _) => tb0.WriteSubId(Create(cx0, sub)), TypeIdentifierContext.TypeName, symbol);
symbol.BuildTypeId(Context, trapFile, (cx0, tb0, sub) => tb0.WriteSubId(Create(cx0, sub)));
trapFile.Write(";tuple");
}

View File

@@ -270,9 +270,6 @@ namespace Semmle.Extraction.CSharp.Entities
{
type = type.DisambiguateType();
if (type is INamedTypeSymbol nt && nt.IsEvilTwin())
type = nt.ConstructedFrom;
const bool errorTypeIsNull = false;
return type == null || (errorTypeIsNull && type.TypeKind == TypeKind.Error) ?
NullType.Create(cx).Type : (Type)cx.CreateEntity(type);

View File

@@ -51,7 +51,7 @@ namespace Semmle.Extraction.CSharp.Entities
public override void WriteId(TextWriter trapFile)
{
AddSignatureTypeToId(Context, trapFile, symbol, symbol.ReturnType, TypeIdentifierContext.MethodParam); // Needed for op_explicit(), which differs only by return type.
AddSignatureTypeToId(Context, trapFile, symbol, symbol.ReturnType); // Needed for op_explicit(), which differs only by return type.
trapFile.Write(' ');
BuildMethodId(this, trapFile);
}

View File

@@ -27,19 +27,6 @@ namespace Semmle.Extraction.CSharp
}
}
/// <summary>
/// Marks where a type identifier is being generated, which could change
/// the way that the identifier is generated.
/// </summary>
public enum TypeIdentifierContext
{
TypeName,
TypeRef,
AnonymousType,
MethodName,
MethodParam
}
static class SymbolExtensions
{
/// <summary>
@@ -146,7 +133,7 @@ namespace Semmle.Extraction.CSharp
/// <param name="cx">The extraction context.</param>
/// <param name="trapFile">The trap builder used to store the result.</param>
/// <param name="subTermAction">The action to apply to syntactic sub terms of this type.</param>
public static void BuildTypeId(this ITypeSymbol type, Context cx, TextWriter trapFile, Action<Context, TextWriter, ITypeSymbol, ISymbol> subTermAction, TypeIdentifierContext tic, ISymbol genericContext)
public static void BuildTypeId(this ITypeSymbol type, Context cx, TextWriter trapFile, Action<Context, TextWriter, ITypeSymbol> subTermAction)
{
switch(type.SpecialType)
{
@@ -184,7 +171,7 @@ namespace Semmle.Extraction.CSharp
{
case TypeKind.Array:
var array = (IArrayTypeSymbol)type;
subTermAction(cx, trapFile, array.ElementType, genericContext);
subTermAction(cx, trapFile, array.ElementType);
array.BuildArraySuffix(trapFile);
return;
case TypeKind.Class:
@@ -194,29 +181,16 @@ namespace Semmle.Extraction.CSharp
case TypeKind.Delegate:
case TypeKind.Error:
var named = (INamedTypeSymbol)type;
named.BuildNamedTypeId(cx, trapFile, subTermAction, tic, genericContext);
named.BuildNamedTypeId(cx, trapFile, subTermAction);
return;
case TypeKind.Pointer:
var ptr = (IPointerTypeSymbol)type;
subTermAction(cx, trapFile, ptr.PointedAtType, genericContext);
subTermAction(cx, trapFile, ptr.PointedAtType);
trapFile.Write("*");
return;
case TypeKind.TypeParameter:
var tp = (ITypeParameterSymbol)type;
switch(tp.TypeParameterKind)
{
case TypeParameterKind.Method:
if (!Equals(genericContext, tp.DeclaringMethod))
trapFile.WriteSubId(Method.Create(cx, tp.DeclaringMethod));
trapFile.Write("!!");
break;
case TypeParameterKind.Type:
if (!Equals(genericContext,tp.DeclaringType))
subTermAction(cx, trapFile, tp.DeclaringType, genericContext);
trapFile.Write("!");
break;
}
trapFile.Write(tp.Ordinal);
trapFile.Write(tp.Name);
return;
case TypeKind.Dynamic:
trapFile.Write("dynamic");
@@ -258,11 +232,10 @@ namespace Semmle.Extraction.CSharp
trapFile.Write("::");
}
public static void BuildNamedTypeId(this INamedTypeSymbol named, Context cx, TextWriter trapFile, Action<Context, TextWriter, ITypeSymbol, ISymbol> subTermAction, TypeIdentifierContext tic, ISymbol genericContext)
public static void BuildNamedTypeId(this INamedTypeSymbol named, Context cx, TextWriter trapFile, Action<Context, TextWriter, ITypeSymbol> subTermAction)
{
bool prefixAssembly = false;
if (named.IsAnonymous()) prefixAssembly = true;
else if(tic == TypeIdentifierContext.TypeName && cx.Extractor.TrapIdentifiers != TrapIdenfierMode.Imprecise) prefixAssembly = true;
if (named.ContainingAssembly is null) prefixAssembly = false;
if (prefixAssembly)
@@ -276,7 +249,7 @@ namespace Semmle.Extraction.CSharp
{
trapFile.Write(f.Name);
trapFile.Write(":");
subTermAction(cx, tb0, f.Type, genericContext);
subTermAction(cx, tb0, f.Type);
}
);
trapFile.Write(")");
@@ -285,16 +258,16 @@ namespace Semmle.Extraction.CSharp
if (named.ContainingType != null)
{
subTermAction(cx, trapFile, named.ContainingType, genericContext);
subTermAction(cx, trapFile, named.ContainingType);
trapFile.Write('.');
}
else if (named.ContainingNamespace != null && !named.IsConstructedGeneric())
else if (named.ContainingNamespace != null)
{
named.ContainingNamespace.BuildNamespace(cx, trapFile);
}
if (named.IsAnonymousType)
named.BuildAnonymousName(cx, trapFile, subTermAction, true, genericContext);
named.BuildAnonymousName(cx, trapFile, subTermAction, true);
else if (named.TypeParameters.IsEmpty)
trapFile.Write(named.Name);
else if (IsReallyUnbound(named))
@@ -305,14 +278,14 @@ namespace Semmle.Extraction.CSharp
}
else
{
subTermAction(cx, trapFile, named.ConstructedFrom, genericContext);
subTermAction(cx, trapFile, named.ConstructedFrom);
trapFile.Write('<');
// Encode the nullability of the type arguments in the label.
// Type arguments with different nullability can result in
// a constructed type with different nullability of its members and methods,
// so we need to create a distinct database entity for it.
trapFile.BuildList(",", named.GetAnnotatedTypeArguments(),
(ta, tb0) => subTermAction(cx, tb0, ta.Symbol, genericContext)
(ta, tb0) => subTermAction(cx, tb0, ta.Symbol)
);
trapFile.Write('>');
}
@@ -324,16 +297,16 @@ namespace Semmle.Extraction.CSharp
trapFile.Write('.');
}
static void BuildAnonymousName(this ITypeSymbol type, Context cx, TextWriter trapFile, Action<Context, TextWriter, ITypeSymbol, ISymbol> subTermAction, bool includeParamName, ISymbol genericContext)
static void BuildAnonymousName(this ITypeSymbol type, Context cx, TextWriter trapFile, Action<Context, TextWriter, ITypeSymbol> subTermAction, bool includeParamName)
{
var buildParam = includeParamName
? (prop, tb0) =>
{
tb0.Write(prop.Name);
tb0.Write(' ');
subTermAction(cx, tb0, prop.Type, genericContext);
subTermAction(cx, tb0, prop.Type);
}
: (Action<IPropertySymbol, TextWriter>)((prop, tb0) => subTermAction(cx, tb0, prop.Type, genericContext));
: (Action<IPropertySymbol, TextWriter>)((prop, tb0) => subTermAction(cx, tb0, prop.Type));
int memberCount = type.GetMembers().OfType<IPropertySymbol>().Count();
int hackTypeNumber = memberCount == 1 ? 1 : 0;
trapFile.Write("<>__AnonType");
@@ -405,7 +378,7 @@ namespace Semmle.Extraction.CSharp
if (namedType.IsAnonymousType)
{
namedType.BuildAnonymousName(cx, trapFile, (cx0, tb0, sub, _) => sub.BuildDisplayName(cx0, tb0), false, namedType);
namedType.BuildAnonymousName(cx, trapFile, (cx0, tb0, sub) => sub.BuildDisplayName(cx0, tb0), false);
}
trapFile.Write(namedType.Name);
@@ -617,5 +590,12 @@ namespace Semmle.Extraction.CSharp
/// </summary>
public static AnnotatedTypeSymbol WithAnnotation(this ITypeSymbol symbol, NullableAnnotation annotation) =>
new AnnotatedTypeSymbol(symbol, annotation);
/// <summary>
/// Holds if this type looks like an "anonymous" type. Names of anonymous types
/// sometimes collide so they need to be handled separately.
/// </summary>
public static bool IsAnonymous(this INamedTypeSymbol type) =>
type.IsAnonymousType || type.Name.StartsWith("<>");
}
}

View File

@@ -201,9 +201,6 @@ methodTypeArguments
| NullableRefTypes.cs:67:10:67:21 | GenericFn | 0 | MyClass |
| NullableRefTypes.cs:67:10:67:21 | GenericFn | 0 | MyClass! |
constructedTypes
| AsyncStreams.cs:34:15:34:37 | IAsyncEnumerable<int> | 0 | int | _ |
| AsyncStreams.cs:39:15:39:37 | IAsyncEnumerator<T> | 0 | T | _ |
| AsyncStreams.cs:39:15:39:37 | IAsyncEnumerator<int> | 0 | int | _ |
| NullableRefTypes.cs:54:11:54:33 | Generic<MyClass, MyClass, IDisposable, MyClass> | 0 | MyClass | _ |
| NullableRefTypes.cs:54:11:54:33 | Generic<MyClass, MyClass, IDisposable, MyClass> | 1 | MyClass | _ |
| NullableRefTypes.cs:54:11:54:33 | Generic<MyClass, MyClass, IDisposable, MyClass> | 2 | IDisposable | _ |

View File

@@ -47,12 +47,13 @@ query predicate methodTypeArguments(ConstructedGeneric generic, int arg, string
query predicate constructedTypes(AnnotatedConstructedType at, int i, string arg, string nullability) {
arg = at.getTypeArgument(i).toString() and
at.getLocation() instanceof SourceLocation and
at.getLocation().getFile().getBaseName() = "NullableRefTypes.cs" and
nullability = at.getAnnotations().getNullability().toString()
}
query predicate nullableTypeParameters(TypeParameter p) {
p.getConstraints().hasNullableRefTypeConstraint()
and p.getLocation().getFile().getBaseName() = "NullableRefTypes.cs"
}
query predicate annotatedTypeConstraints(TypeParameter p, AnnotatedType t) {