Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f909965

Browse files
committed
C#: Do not extract type annotations that can be deduced. Put specific_type_parameter_annotation on the side of specific_type_parameter_constraints.
1 parent 31655c2 commit f909965

13 files changed

Lines changed: 6098 additions & 2305 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ protected void ExtractGenerics()
342342
{
343343
Context.Emit(Tuples.is_constructed(this));
344344
Context.Emit(Tuples.constructed_generic(this, Method.Create(Context, ConstructedFromSymbol)));
345-
foreach (var tp in symbol.TypeArguments)
345+
foreach (var tp in symbol.GetAnnotatedTypeArguments())
346346
{
347-
Context.Emit(Tuples.type_argument_annotation(this, child, (Kinds.TypeAnnotation)symbol.TypeArgumentsNullableAnnotations[child]));
348-
Context.Emit(Tuples.type_arguments(Type.Create(Context, tp), child++, this));
347+
Context.Emit(Tuples.type_arguments(Type.Create(Context, tp.Symbol), child, this));
348+
if(tp.Nullability.NeedsExtraction())
349+
Context.Emit(Tuples.type_argument_annotation(this, child, (Kinds.TypeAnnotation)symbol.TypeArgumentsNullableAnnotations[child]));
350+
child++;
349351
}
350352
}
351353
else

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ protected void ExtractAttributes()
2929

3030
protected void ExtractNullability(NullableAnnotation annotation)
3131
{
32-
Context.Emit(Tuples.type_annotation(this, (Kinds.TypeAnnotation)annotation));
32+
if (annotation.NeedsExtraction())
33+
Context.Emit(Tuples.type_annotation(this, (Kinds.TypeAnnotation)annotation));
3334
}
3435

3536
protected void ExtractRefKind(RefKind kind)

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ public override void Populate()
4444
if(symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated)
4545
Context.Emit(Tuples.general_type_parameter_constraints(constraints, 5));
4646

47-
foreach (var abase in symbol.ConstraintTypes.Zip(symbol.ConstraintNullableAnnotations, (type, nullability) => (Type:type, Nullability:nullability)))
47+
foreach (var abase in symbol.GetAnnotatedTypeConstraints())
4848
{
49-
if (abase.Type.TypeKind != TypeKind.Interface)
50-
baseType = abase.Type;
51-
var t = Create(Context, abase.Type);
52-
Context.Emit(Tuples.specific_type_parameter_constraints(constraints, t.TypeRef, (Kinds.TypeAnnotation)abase.Nullability));
49+
if (abase.Symbol.TypeKind != TypeKind.Interface)
50+
baseType = abase.Symbol;
51+
var t = Create(Context, abase.Symbol);
52+
Context.Emit(Tuples.specific_type_parameter_constraints(constraints, t.TypeRef));
53+
if (abase.Nullability.NeedsExtraction())
54+
Context.Emit(Tuples.specific_type_parameter_annotation(constraints, t.TypeRef, abase.Nullability));
5355
}
5456

5557
Context.Emit(Tuples.types(this, Semmle.Extraction.Kinds.TypeKind.TYPE_PARAMETER, symbol.Name));

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,13 @@ public static AnnotatedTypeSymbol GetType(this Context cx, Microsoft.CodeAnalysi
503503
/// </summary>
504504
public static AnnotatedTypeSymbol GetAnnotatedReturnType(this IMethodSymbol symbol) => new AnnotatedTypeSymbol(symbol.ReturnType, symbol.ReturnNullableAnnotation);
505505

506+
/// <summary>
507+
/// Holds if the annotation should be extracted.
508+
/// The "Disabled" and "NotApplicable" annotations can be inferred so
509+
/// do not need to be stored in the database.
510+
/// </summary>
511+
public static bool NeedsExtraction(this NullableAnnotation info) => info == NullableAnnotation.Annotated || info == NullableAnnotation.NotAnnotated;
512+
506513
/// <summary>
507514
/// Gets the annotated element type of an IArrayTypeSymbol.
508515
/// This has not yet been exposed on the public API.
@@ -517,6 +524,20 @@ public static AnnotatedTypeSymbol GetAnnotatedElementType(this IArrayTypeSymbol
517524
public static IEnumerable<AnnotatedTypeSymbol> GetAnnotatedTypeArguments(this INamedTypeSymbol symbol) =>
518525
symbol.TypeArguments.Zip(symbol.TypeArgumentsNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
519526

527+
/// <summary>
528+
/// Gets the annotated type arguments of an IMethodSymbol.
529+
/// This has not yet been exposed on the public API.
530+
/// </summary>
531+
public static IEnumerable<AnnotatedTypeSymbol> GetAnnotatedTypeArguments(this IMethodSymbol symbol) =>
532+
symbol.TypeArguments.Zip(symbol.TypeArgumentsNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
533+
534+
/// <summary>
535+
/// Gets the annotated type constraints of an ITypeParameterSymbol.
536+
/// This has not yet been exposed on the public API.
537+
/// </summary>
538+
public static IEnumerable<AnnotatedTypeSymbol> GetAnnotatedTypeConstraints(this ITypeParameterSymbol symbol) =>
539+
symbol.ConstraintTypes.Zip(symbol.ConstraintNullableAnnotations, (t, a) => new AnnotatedTypeSymbol(t, a));
540+
520541
/// <summary>
521542
/// Creates an AnnotatedTypeSymbol from an ITypeSymbol.
522543
/// </summary>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ public static Tuple metadata_handle(IEntity entity, Location assembly, int handl
190190

191191
internal static Tuple statements(Statement stmt, StmtKind kind) => new Tuple("statements", stmt, kind);
192192

193-
internal static Tuple specific_type_parameter_constraints(TypeParameterConstraints constraints, Type baseType, Kinds.TypeAnnotation annotation) => new Tuple("specific_type_parameter_constraints", constraints, baseType, (int)annotation);
193+
internal static Tuple specific_type_parameter_constraints(TypeParameterConstraints constraints, Type baseType) => new Tuple("specific_type_parameter_constraints", constraints, baseType);
194+
195+
internal static Tuple specific_type_parameter_annotation(TypeParameterConstraints constraints, Type baseType, Microsoft.CodeAnalysis.NullableAnnotation annotation) => new Tuple("specific_type_parameter_annotation", constraints, baseType, (int)annotation);
194196

195197
internal static Tuple successors(IEntity from, IEntity to) => new Tuple("successors", from, to);
196198

csharp/ql/src/semmle/code/csharp/AnnotatedType.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ private int getTypeArgumentFlags(ConstructedGeneric generic, int argument) {
178178
}
179179

180180
private int getTypeParameterFlags(TypeParameterConstraints constraints, Type type) {
181-
specific_type_parameter_constraints(constraints, getTypeRef(type), _) and
181+
specific_type_parameter_annotation(constraints, getTypeRef(type), _) and
182182
result = sum(int b |
183-
specific_type_parameter_constraints(constraints, getTypeRef(type), b)
183+
specific_type_parameter_annotation(constraints, getTypeRef(type), b)
184184
|
185185
getBitMask(b)
186186
)

csharp/ql/src/semmle/code/csharp/Generics.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,16 @@ class TypeParameter extends DotNet::TypeParameter, Type, @type_parameter {
203203
class TypeParameterConstraints extends Element, @type_parameter_constraints {
204204
/** Gets a specific interface constraint, if any. */
205205
Interface getAnInterfaceConstraint() {
206-
specific_type_parameter_constraints(this, getTypeRef(result), _)
206+
specific_type_parameter_constraints(this, getTypeRef(result))
207207
}
208208

209209
/** Gets a specific type parameter constraint, if any. */
210210
TypeParameter getATypeParameterConstraint() {
211-
specific_type_parameter_constraints(this, getTypeRef(result), _)
211+
specific_type_parameter_constraints(this, getTypeRef(result))
212212
}
213213

214214
/** Gets the specific class constraint, if any. */
215-
Class getClassConstraint() { specific_type_parameter_constraints(this, getTypeRef(result), _) }
215+
Class getClassConstraint() { specific_type_parameter_constraints(this, getTypeRef(result)) }
216216

217217
/** Gets an annotated specific type constraint, if any. */
218218
AnnotatedType getAnAnnotatedTypeConstraint() {

csharp/ql/src/semmlecode.csharp.dbscheme

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,6 @@ type_mention_location(
485485
* A direct annotation on an entity, for example `string? x;`.
486486
*
487487
* Annotations:
488-
* 0 = reftype nullability is not applicable
489-
* 1 = reftype nullability disabled
490488
* 2 = reftype is not annotated "!"
491489
* 3 = reftype is annotated "?"
492490
* 4 = readonly ref type / in parameter
@@ -501,9 +499,6 @@ type_mention_location(
501499
*/
502500
type_annotation(int id: @has_type_annotation ref, int annotation: int ref);
503501

504-
/** The annotation of the callable's receiver. */
505-
receiver_type_annotation(int callable: @callable ref, int annotation: int ref);
506-
507502
/** The annotation of type arguments of a constructed type or method. */
508503
type_argument_annotation(int constructedgeneric: @generic ref, int position: int ref, int annotation: int ref);
509504

@@ -547,9 +542,12 @@ general_type_parameter_constraints(
547542

548543
specific_type_parameter_constraints(
549544
int id: @type_parameter_constraints ref,
550-
int base_id: @type_or_ref ref,
551-
int nullability: int ref);
545+
int base_id: @type_or_ref ref);
552546

547+
specific_type_parameter_annotation(
548+
int id: @type_parameter_constraints ref,
549+
int base_id: @type_or_ref ref,
550+
int annotation: int ref);
553551

554552
/** MODIFIERS */
555553

0 commit comments

Comments
 (0)