Skip to content

Commit fe83bac

Browse files
committed
C#: Fix up test output
C#: Fix a qltest whereby a tuple type having multiple underlying types was causing an issue with the IR sanity checks. C#: Revert more changes. C#: Fix tests and remove dead code.
1 parent a261cba commit fe83bac

File tree

21 files changed

+774
-921
lines changed

21 files changed

+774
-921
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Method.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ protected static void AddSignatureTypeToId(Context cx, TextWriter trapFile, IMet
204204
if (type.ContainsTypeParameters(cx, method))
205205
type.BuildTypeId(cx, trapFile, (cx0, tb0, type0) => AddSignatureTypeToId(cx, tb0, method, type0));
206206
else
207-
trapFile.WriteSubId(Type.Create(cx, type).TypeRef);
207+
trapFile.WriteSubId(Type.Create(cx, type));
208208
}
209209

210210
protected static void AddParametersToId(Context cx, TextWriter trapFile, IMethodSymbol method)

csharp/extractor/Semmle.Extraction.CSharp/Entities/Parameter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ public override void WriteId(TextWriter trapFile)
8686
trapFile.Write(";parameter");
8787
}
8888

89-
// If we don't have the type of the parameter, do not populate it.
90-
public override bool NeedsPopulation => symbol.Type.TypeKind != TypeKind.Error;
89+
public override bool NeedsPopulation => true;
9190

9291
string Name
9392
{

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/NamedType.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override void Populate(TextWriter trapFile)
2929
return;
3030
}
3131

32-
trapFile.typeref_type((TypeRef)TypeRef, this);
32+
trapFile.typeref_type((NamedTypeRef)TypeRef, this);
3333

3434
if (symbol.IsGenericType)
3535
{
@@ -151,39 +151,34 @@ class NamedTypeFactory : ICachedEntityFactory<INamedTypeSymbol, NamedType>
151151
public NamedType Create(Context cx, INamedTypeSymbol init) => new NamedType(cx, init);
152152
}
153153

154-
public override Type TypeRef => Entities.TypeRef.Create(Context, this, symbol);
154+
public override Type TypeRef => NamedTypeRef.Create(Context, symbol);
155155
}
156156

157-
class TypeRef : Type<ITypeSymbol>
157+
class NamedTypeRef : Type<INamedTypeSymbol>
158158
{
159159
readonly Type referencedType;
160160

161-
public TypeRef(Context cx, Type type, ITypeSymbol symbol) : base(cx, symbol)
161+
public NamedTypeRef(Context cx, INamedTypeSymbol symbol) : base(cx, symbol)
162162
{
163-
referencedType = type;
163+
referencedType = Type.Create(cx, symbol);
164164
}
165165

166-
public static TypeRef Create(Context cx, Type referencedType, ITypeSymbol type) =>
167-
NamedTypeRefFactory.Instance.CreateEntity2(cx, (referencedType, type));
166+
public static NamedTypeRef Create(Context cx, INamedTypeSymbol type) =>
167+
NamedTypeRefFactory.Instance.CreateEntity2(cx, type);
168168

169-
class NamedTypeRefFactory : ICachedEntityFactory<(Type, ITypeSymbol), TypeRef>
169+
class NamedTypeRefFactory : ICachedEntityFactory<INamedTypeSymbol, NamedTypeRef>
170170
{
171171
public static readonly NamedTypeRefFactory Instance = new NamedTypeRefFactory();
172172

173-
public TypeRef Create(Context cx, (Type, ITypeSymbol) init) => new TypeRef(cx, init.Item1, init.Item2);
173+
public NamedTypeRef Create(Context cx, INamedTypeSymbol init) => new NamedTypeRef(cx, init);
174174
}
175175

176176
public override bool NeedsPopulation => true;
177177

178178
public override void WriteId(TextWriter trapFile)
179179
{
180-
void ExpandType(Context cx0, TextWriter tb0, ITypeSymbol sub)
181-
{
182-
sub.BuildTypeId(cx0, tb0, ExpandType);
183-
}
184-
185-
symbol.BuildTypeId(Context, trapFile, ExpandType);
186-
trapFile.Write(";typeref");
180+
trapFile.WriteSubId(referencedType);
181+
trapFile.Write(";typeRef");
187182
}
188183

189184
public override void Populate(TextWriter trapFile)

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TupleType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override void Populate(TextWriter trapFile)
4242
PopulateGenerics();
4343

4444
var underlyingType = NamedType.Create(Context, symbol.TupleUnderlyingType);
45-
trapFile.tuple_underlying_type(this, underlyingType.TypeRef);
45+
trapFile.tuple_underlying_type(this, underlyingType);
4646

4747
int index = 0;
4848
foreach (var element in TupleElements)

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ protected void PopulateType(TextWriter trapFile)
9595
var baseTypes = new List<Type>();
9696
if (symbol.BaseType != null)
9797
{
98-
// !! Do not extend "object" if the base type is missing :-(
9998
Type baseKey = Create(Context, symbol.BaseType);
10099
trapFile.extend(this, baseKey.TypeRef);
101100
if (symbol.TypeKind != TypeKind.Struct)
@@ -269,7 +268,6 @@ public void ExtractRecursive(TextWriter trapFile, IEntity parent)
269268
public static Type Create(Context cx, ITypeSymbol type)
270269
{
271270
type = type.DisambiguateType();
272-
273271
const bool errorTypeIsNull = false;
274272
return type == null || (errorTypeIsNull && type.TypeKind == TypeKind.Error) ?
275273
NullType.Create(cx).Type : (Type)cx.CreateEntity(type);

csharp/extractor/Semmle.Extraction.CSharp/Extractor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ public static ExitCode Run(string[] args)
120120
var syntaxTrees = new List<SyntaxTree>();
121121
var syntaxTreeTasks = ReadSyntaxTrees(
122122
compilerArguments.SourceFiles.
123-
Select(src => canonicalPathCache.GetCanonicalPath(src.Path)).
124-
Where(f => !f.EndsWith(".dll")),
123+
Select(src => canonicalPathCache.GetCanonicalPath(src.Path)),
125124
analyser,
126125
compilerArguments.ParseOptions,
127126
compilerArguments.Encoding,

csharp/extractor/Semmle.Extraction.CSharp/SymbolExtensions.cs

Lines changed: 31 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7-
using System.Security.Cryptography;
8-
using System.Text;
97

108
namespace Semmle.Extraction.CSharp
119
{
@@ -135,34 +133,15 @@ public static bool ContainsTypeParameters(this ITypeSymbol type, Context cx, ISy
135133
/// <param name="subTermAction">The action to apply to syntactic sub terms of this type.</param>
136134
public static void BuildTypeId(this ITypeSymbol type, Context cx, TextWriter trapFile, Action<Context, TextWriter, ITypeSymbol> subTermAction)
137135
{
138-
switch(type.SpecialType)
136+
if (type.SpecialType != SpecialType.None)
139137
{
140-
case SpecialType.System_Object:
141-
case SpecialType.System_Void:
142-
case SpecialType.System_Boolean:
143-
case SpecialType.System_Char:
144-
case SpecialType.System_SByte:
145-
case SpecialType.System_Byte:
146-
case SpecialType.System_Int16:
147-
case SpecialType.System_UInt16:
148-
case SpecialType.System_Int32:
149-
case SpecialType.System_UInt32:
150-
case SpecialType.System_Int64:
151-
case SpecialType.System_UInt64:
152-
case SpecialType.System_Decimal:
153-
case SpecialType.System_Single:
154-
case SpecialType.System_Double:
155-
case SpecialType.System_String:
156-
case SpecialType.System_IntPtr:
157-
case SpecialType.System_UIntPtr:
158-
159-
/*
160-
* Use the keyword ("int" etc) for the built-in types.
161-
* This makes the IDs shorter and means that all built-in types map to
162-
* the same entities (even when using multiple versions of mscorlib).
163-
*/
164-
trapFile.Write(type.ToDisplayString());
165-
return;
138+
/*
139+
* Use the keyword ("int" etc) for the built-in types.
140+
* This makes the IDs shorter and means that all built-in types map to
141+
* the same entities (even when using multiple versions of mscorlib).
142+
*/
143+
trapFile.Write(type.ToDisplayString());
144+
return;
166145
}
167146

168147
using (cx.StackGuard)
@@ -186,7 +165,7 @@ public static void BuildTypeId(this ITypeSymbol type, Context cx, TextWriter tra
186165
case TypeKind.Pointer:
187166
var ptr = (IPointerTypeSymbol)type;
188167
subTermAction(cx, trapFile, ptr.PointedAtType);
189-
trapFile.Write("*");
168+
trapFile.Write('*');
190169
return;
191170
case TypeKind.TypeParameter:
192171
var tp = (ITypeParameterSymbol)type;
@@ -214,28 +193,8 @@ public static void BuildArraySuffix(this IArrayTypeSymbol array, TextWriter trap
214193
trapFile.Write(']');
215194
}
216195

217-
private static void BuildAssembly(IAssemblySymbol asm, TextWriter trapFile, bool extraPrecise = false)
196+
static void BuildNamedTypeId(this INamedTypeSymbol named, Context cx, TextWriter trapFile, Action<Context, TextWriter, ITypeSymbol> subTermAction)
218197
{
219-
var assembly = asm.Identity;
220-
trapFile.Write(assembly.Name);
221-
trapFile.Write('_');
222-
trapFile.Write(assembly.Version.Major);
223-
trapFile.Write('.');
224-
trapFile.Write(assembly.Version.Minor);
225-
trapFile.Write('.');
226-
trapFile.Write(assembly.Version.Build);
227-
if (extraPrecise)
228-
{
229-
trapFile.Write('.');
230-
trapFile.Write(assembly.Version.Revision);
231-
}
232-
trapFile.Write("::");
233-
}
234-
235-
public static void BuildNamedTypeId(this INamedTypeSymbol named, Context cx, TextWriter trapFile, Action<Context, TextWriter, ITypeSymbol> subTermAction)
236-
{
237-
bool prefixAssembly = !(named.ContainingAssembly is null);
238-
239198
if (named.IsTupleType)
240199
{
241200
trapFile.Write('(');
@@ -258,8 +217,6 @@ public static void BuildNamedTypeId(this INamedTypeSymbol named, Context cx, Tex
258217
}
259218
else if (named.ContainingNamespace != null)
260219
{
261-
if (prefixAssembly)
262-
BuildAssembly(named.ContainingAssembly, trapFile);
263220
named.ContainingNamespace.BuildNamespace(cx, trapFile);
264221
}
265222

@@ -290,6 +247,26 @@ public static void BuildNamedTypeId(this INamedTypeSymbol named, Context cx, Tex
290247

291248
static void BuildNamespace(this INamespaceSymbol ns, Context cx, TextWriter trapFile)
292249
{
250+
// Only include the assembly information in each type ID
251+
// for normal extractions. This is because standalone extractions
252+
// lack assembly information or may be ambiguous.
253+
bool prependAssemblyToTypeId = !cx.Extractor.Standalone && ns.ContainingAssembly != null;
254+
255+
if (prependAssemblyToTypeId)
256+
{
257+
// Note that we exclude the revision number as this has
258+
// been observed to be unstable.
259+
var assembly = ns.ContainingAssembly.Identity;
260+
trapFile.Write(assembly.Name);
261+
trapFile.Write('_');
262+
trapFile.Write(assembly.Version.Major);
263+
trapFile.Write('.');
264+
trapFile.Write(assembly.Version.Minor);
265+
trapFile.Write('.');
266+
trapFile.Write(assembly.Version.Build);
267+
trapFile.Write("::");
268+
}
269+
293270
trapFile.WriteSubId(Namespace.Create(cx, ns));
294271
trapFile.Write('.');
295272
}
@@ -379,7 +356,7 @@ public static void BuildNamedTypeDisplayName(this INamedTypeSymbol namedType, Co
379356
}
380357

381358
trapFile.Write(namedType.Name);
382-
if (namedType.IsGenericType && namedType.TypeArguments.Any())
359+
if (namedType.IsGenericType && namedType.TypeKind != TypeKind.Error && namedType.TypeArguments.Any())
383360
{
384361
trapFile.Write('<');
385362
trapFile.BuildList(",", namedType.TypeArguments, (p, tb0) =>
@@ -478,30 +455,6 @@ public static TypeInfo GetTypeInfo(this Context cx, Microsoft.CodeAnalysis.CShar
478455
public static SymbolInfo GetSymbolInfo(this Context cx, Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode node) =>
479456
cx.GetModel(node).GetSymbolInfo(node);
480457

481-
/// <summary>
482-
/// Gets the symbol for a particular syntax node.
483-
/// Throws an exception if the symbol is not found.
484-
/// </summary>
485-
///
486-
/// <remarks>
487-
/// This gives a nicer message than a "null pointer exception",
488-
/// and should be used where we require a symbol to be resolved.
489-
/// </remarks>
490-
///
491-
/// <param name="cx">The extraction context.</param>
492-
/// <param name="node">The syntax node.</param>
493-
/// <returns>The resolved symbol.</returns>
494-
public static ISymbol GetSymbol(this Context cx, Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode node)
495-
{
496-
var info = GetSymbolInfo(cx, node);
497-
if (info.Symbol == null)
498-
{
499-
throw new InternalError(node, "Could not resolve symbol");
500-
}
501-
502-
return info.Symbol;
503-
}
504-
505458
/// <summary>
506459
/// Determines the type of a node, or default
507460
/// if the type could not be determined.
@@ -515,84 +468,11 @@ public static AnnotatedTypeSymbol GetType(this Context cx, Microsoft.CodeAnalysi
515468
return new AnnotatedTypeSymbol(info.Type.DisambiguateType(), info.Nullability.Annotation);
516469
}
517470

518-
/// <summary>
519-
/// Gets the annotated type of an IPropertySymbol.
520-
/// This has not yet been exposed on the public API.
521-
/// </summary>
522-
public static AnnotatedTypeSymbol GetAnnotatedType(this IPropertySymbol symbol) => new AnnotatedTypeSymbol(symbol.Type, symbol.NullableAnnotation);
523-
524-
/// <summary>
525-
/// Gets the annotated type of an IFieldSymbol.
526-
/// This has not yet been exposed on the public API.
527-
/// </summary>
528-
public static AnnotatedTypeSymbol GetAnnotatedType(this IFieldSymbol symbol) => new AnnotatedTypeSymbol(symbol.Type, symbol.NullableAnnotation);
529-
private static bool IsSpecialized(this IMethodSymbol method) =>
530-
method.IsGenericMethod &&
531-
!ReferenceEquals(method, method.OriginalDefinition) &&
532-
method.TypeParameters.Zip(method.TypeArguments, (a, b) => !ReferenceEquals(a, b)).Any(b => b);
533-
534-
/// <summary>
535-
/// Gets the annotated return type of an IMethodSymbol.
536-
/// This has not yet been exposed on the public API.
537-
/// </summary>
538-
public static AnnotatedTypeSymbol GetAnnotatedReturnType(this IMethodSymbol symbol) => new AnnotatedTypeSymbol(symbol.ReturnType, symbol.ReturnNullableAnnotation);
539-
540-
/// <summary>
541-
/// Gets the type annotation for a NullableAnnotation.
542-
/// </summary>
543-
public static Kinds.TypeAnnotation GetTypeAnnotation(this NullableAnnotation na)
544-
{
545-
switch(na)
546-
{
547-
case NullableAnnotation.Annotated:
548-
return Kinds.TypeAnnotation.Annotated;
549-
case NullableAnnotation.NotAnnotated:
550-
return Kinds.TypeAnnotation.NotAnnotated;
551-
default:
552-
return Kinds.TypeAnnotation.None;
553-
}
554-
}
555-
556-
/// <summary>
557-
/// Gets the annotated element type of an IArrayTypeSymbol.
558-
/// This has not yet been exposed on the public API.
559-
/// </summary>
560-
public static AnnotatedTypeSymbol GetAnnotatedElementType(this IArrayTypeSymbol symbol) =>
561-
new AnnotatedTypeSymbol(symbol.ElementType, symbol.ElementNullableAnnotation);
562-
563471
/// <summary>
564472
/// Gets the annotated type arguments of an INamedTypeSymbol.
565473
/// This has not yet been exposed on the public API.
566474
/// </summary>
567475
public static IEnumerable<AnnotatedTypeSymbol> GetAnnotatedTypeArguments(this INamedTypeSymbol symbol) =>
568476
symbol.TypeArguments.Zip(symbol.TypeArgumentsNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
569-
570-
/// <summary>
571-
/// Gets the annotated type arguments of an IMethodSymbol.
572-
/// This has not yet been exposed on the public API.
573-
/// </summary>
574-
public static IEnumerable<AnnotatedTypeSymbol> GetAnnotatedTypeArguments(this IMethodSymbol symbol) =>
575-
symbol.TypeArguments.Zip(symbol.TypeArgumentsNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
576-
577-
/// <summary>
578-
/// Gets the annotated type constraints of an ITypeParameterSymbol.
579-
/// This has not yet been exposed on the public API.
580-
/// </summary>
581-
public static IEnumerable<AnnotatedTypeSymbol> GetAnnotatedTypeConstraints(this ITypeParameterSymbol symbol) =>
582-
symbol.ConstraintTypes.Zip(symbol.ConstraintNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
583-
584-
/// <summary>
585-
/// Creates an AnnotatedTypeSymbol from an ITypeSymbol.
586-
/// Note: not currently used but might be useful.
587-
/// </summary>
588-
public static AnnotatedTypeSymbol WithAnnotation(this ITypeSymbol symbol, NullableAnnotation annotation) =>
589-
new AnnotatedTypeSymbol(symbol, annotation);
590-
591-
/// <summary>
592-
/// Holds if this type looks like an "anonymous" type. Names of anonymous types
593-
/// sometimes collide so they need to be handled separately.
594-
/// </summary>
595-
public static bool IsAnonymous(this INamedTypeSymbol type) =>
596-
type.IsAnonymousType || type.Name.StartsWith("<>");
597477
}
598478
}

csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ internal static void nullability(this TextWriter trapFile, NullabilityEntity nul
388388

389389
internal static void nullability_member(this TextWriter trapFile, NullabilityEntity nullability, int index, NullabilityEntity child)
390390
{
391-
trapFile.WriteTuple(nameof(nullability_member), nullability, index, child);
391+
trapFile.WriteTuple("nullability_member", nullability, index, child);
392392
}
393393

394394
internal static void numlines(this TextWriter trapFile, IEntity label, LineCounts lineCounts)
@@ -481,7 +481,7 @@ internal static void tuple_element(this TextWriter trapFile, TupleType type, int
481481
trapFile.WriteTuple("tuple_element", type, index, field);
482482
}
483483

484-
internal static void tuple_underlying_type(this TextWriter trapFile, TupleType type, Type underlying)
484+
internal static void tuple_underlying_type(this TextWriter trapFile, TupleType type, NamedType underlying)
485485
{
486486
trapFile.WriteTuple("tuple_underlying_type", type, underlying);
487487
}
@@ -526,12 +526,12 @@ internal static void type_parameters(this TextWriter trapFile, TypeParameter par
526526
trapFile.WriteTuple("type_parameters", param, child, typeOrMethod, (int)param.Variance);
527527
}
528528

529-
internal static void typeref_type(this TextWriter trapFile, TypeRef typeref, Type type)
529+
internal static void typeref_type(this TextWriter trapFile, NamedTypeRef typeref, Type type)
530530
{
531531
trapFile.WriteTuple("typeref_type", typeref, type);
532532
}
533533

534-
internal static void typerefs(this TextWriter trapFile, TypeRef type, string name)
534+
internal static void typerefs(this TextWriter trapFile, NamedTypeRef type, string name)
535535
{
536536
trapFile.WriteTuple("typerefs", type, name);
537537
}

0 commit comments

Comments
 (0)