Skip to content

Commit 41b4416

Browse files
committed
C#: Address review comments part 1.
1 parent fe83bac commit 41b4416

File tree

3 files changed

+63
-51
lines changed

3 files changed

+63
-51
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public static Method Create(Context cx, IMethodSymbol methodDecl)
264264

265265
var methodKind = methodDecl.MethodKind;
266266

267-
if(methodKind == MethodKind.ExplicitInterfaceImplementation)
267+
if (methodKind == MethodKind.ExplicitInterfaceImplementation)
268268
{
269269
// Retrieve the original method kind
270270
methodKind = methodDecl.ExplicitInterfaceImplementations.Select(m => m.MethodKind).FirstOrDefault();
@@ -348,7 +348,9 @@ protected void PopulateGenerics(TextWriter trapFile)
348348
child++;
349349
}
350350

351-
trapFile.type_nullability(this, NullabilityEntity.Create(Context, new Nullability(symbol)));
351+
var nullability = new Nullability(symbol);
352+
if (!nullability.IsOblivious)
353+
trapFile.type_nullability(this, NullabilityEntity.Create(Context, nullability));
352354
}
353355
else
354356
{

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public static Nullability Create(AnnotatedTypeSymbol ts)
3030
return new Nullability(ts);
3131
}
3232

33+
public bool IsOblivious => Annotation == 0 && NullableParameters.Length == 0;
34+
3335
static readonly Nullability oblivious = new Nullability(NullableAnnotation.Disabled);
3436
static readonly Nullability annotated = new Nullability(NullableAnnotation.Annotated);
3537
static readonly Nullability notannotated = new Nullability(NullableAnnotation.NotAnnotated);
@@ -113,7 +115,7 @@ public override void Populate(TextWriter trapFile)
113115
trapFile.nullability(this, symbol.Annotation);
114116

115117
int i = 0;
116-
foreach(var s in symbol.NullableParameters)
118+
foreach (var s in symbol.NullableParameters)
117119
{
118120
trapFile.nullability_member(this, i, Create(Context, s));
119121
i++;

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

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private module Annotations {
1818
/** An annotation on a type. */
1919
class TypeAnnotation extends TAnnotation {
2020
/** Gets the bit position in the bit-field, also used to order the annotations in the text format. */
21-
abstract int getBit();
21+
int getBit() { none() }
2222

2323
/** Gets the string prefixing the type, if any. */
2424
string getPrefix() { none() }
@@ -57,6 +57,41 @@ private module Annotations {
5757
override int getBit() { result = 6 }
5858
}
5959

60+
/**
61+
* A structured type annotation representing type nullability.
62+
* For example, `IDictionary<string!,string?>?` has nullability `<!,?>?`.
63+
*/
64+
class Nullability extends TypeAnnotation, TNullability {
65+
@nullability nullability;
66+
67+
Nullability() { this = TNullability(nullability) }
68+
69+
override string toString() { result = getMemberString() + getSelfNullability() }
70+
71+
language[monotonicAggregates]
72+
private string getMemberString() {
73+
if nullability_member(nullability, _, _)
74+
then
75+
result = "<" +
76+
concat(int i, Nullability child |
77+
nullability_member(nullability, i, getNullability(child))
78+
|
79+
child.toString(), "," order by i
80+
) + ">"
81+
else result = ""
82+
}
83+
84+
/**
85+
* Gets a string representing the nullability, disregarding child nullability.
86+
* For example, `IDictionary<string!,string?>?` has nullability `?`.
87+
*/
88+
string getSelfNullability() { none() }
89+
90+
override int getBit() { none() }
91+
}
92+
93+
@nullability getNullability(Nullability n) { n = TNullability(result) }
94+
6095
private newtype TAnnotations =
6196
TAnnotationFlags(int flags, Nullability n) {
6297
exists(Element e |
@@ -75,9 +110,6 @@ private module Annotations {
75110
this = TAnnotationsNoFlags(result)
76111
}
77112

78-
/** Gets the nullability with no additional flags. */
79-
Nullability getNoFlagsNullability() { this = TAnnotationsNoFlags(result) }
80-
81113
/** Gets text to be displayed before the type. */
82114
string getTypePrefix() {
83115
result = concat(TypeAnnotation a |
@@ -114,44 +146,19 @@ private module Annotations {
114146
}
115147
}
116148

149+
/** Gets the nullability with no additional flags. */
150+
Nullability getNoFlagsNullability(TypeAnnotations annotations) {
151+
annotations = TAnnotationsNoFlags(result)
152+
}
153+
117154
/**
118155
* Gets the `i`th child of type annotations `annotations`.
119156
* This is used to represent structured datatypes, where the structure
120157
* of the type annotation mirrors the structure of the annotated type.
121158
*/
122159
bindingset[i]
123160
TypeAnnotations getChild(TypeAnnotations annotations, int i) {
124-
result.getNoFlagsNullability() = getChildNullability(annotations.getNullability(), i)
125-
}
126-
127-
/**
128-
* A structured type annotation representing type nullability.
129-
* For example, `IDictionary<string!,string?>?` has nullability `<!,?>?`.
130-
*/
131-
abstract class Nullability extends TypeAnnotation, TNullability {
132-
@nullability nullability;
133-
134-
Nullability() { this = TNullability(nullability) }
135-
136-
@nullability getNullability() { result = nullability }
137-
138-
override string toString() { result = getMemberString() + getSelfNullability() }
139-
140-
language[monotonicAggregates]
141-
private string getMemberString() {
142-
if nullability_member(nullability, _, _)
143-
then
144-
result = "<" +
145-
concat(int i, Nullability child |
146-
nullability_member(nullability, i, child.getNullability())
147-
|
148-
child.toString(), "," order by i
149-
) + ">"
150-
else result = ""
151-
}
152-
153-
/** Gets a string representing the nullability, disregarding child nullability. */
154-
string getSelfNullability() { none() }
161+
getNoFlagsNullability(result) = getChildNullability(annotations.getNullability(), i)
155162
}
156163

157164
/**
@@ -162,8 +169,8 @@ private module Annotations {
162169
*/
163170
bindingset[i]
164171
Nullability getChildNullability(Nullability n, int i) {
165-
if nullability_member(n.getNullability(), i, _)
166-
then nullability_member(n.getNullability(), i, result.getNullability())
172+
if nullability_member(getNullability(n), i, _)
173+
then nullability_member(getNullability(n), i, getNullability(result))
167174
else result = n
168175
}
169176

@@ -176,8 +183,6 @@ private module Annotations {
176183
ObliviousNullability() { nullability instanceof @oblivious }
177184

178185
override string getSelfNullability() { result = "_" }
179-
180-
override int getBit() { none() }
181186
}
182187

183188
/**
@@ -220,7 +225,7 @@ private module Annotations {
220225
annotations = TAnnotationFlags(getElementTypeFlags(element), getElementNullability(element))
221226
or
222227
not exists(getElementTypeFlags(element)) and
223-
annotations.getNoFlagsNullability() = getElementNullability(element)
228+
getNoFlagsNullability(annotations) = getElementNullability(element)
224229
) and
225230
(
226231
type = element.(Assignable).getType()
@@ -250,16 +255,18 @@ private Annotations::Nullability getTypeParameterNullability(
250255
TypeParameterConstraints constraints, Type type
251256
) {
252257
if specific_type_parameter_nullability(constraints, getTypeRef(type), _)
253-
then specific_type_parameter_nullability(constraints, getTypeRef(type), result.getNullability())
258+
then
259+
specific_type_parameter_nullability(constraints, getTypeRef(type),
260+
Annotations::getNullability(result))
254261
else (
255-
specific_type_parameter_constraints(constraints, type) and
262+
specific_type_parameter_constraints(constraints, getTypeRef(type)) and
256263
result instanceof Annotations::NoNullability
257264
)
258265
}
259266

260267
private Annotations::Nullability getElementNullability(@has_type_annotation element) {
261268
if type_nullability(element, _)
262-
then type_nullability(element, result.getNullability())
269+
then type_nullability(element, Annotations::getNullability(result))
263270
else result instanceof Annotations::NoNullability
264271
}
265272

@@ -272,10 +279,10 @@ private newtype TAnnotatedType =
272279
annotations = Annotations::getChild(c.getAnnotations(), i)
273280
)
274281
or
275-
annotations.getNoFlagsNullability() = getTypeParameterNullability(_, type)
282+
Annotations::getNoFlagsNullability(annotations) = getTypeParameterNullability(_, type)
276283
or
277284
// All types have at least one annotated type
278-
annotations.getNoFlagsNullability() instanceof Annotations::NoNullability
285+
Annotations::getNoFlagsNullability(annotations) instanceof Annotations::NoNullability
279286
or
280287
exists(AnnotatedArrayType at |
281288
type = at.getType().(ArrayType).getElementType() and
@@ -344,16 +351,17 @@ class AnnotatedType extends TAnnotatedType {
344351
/** Holds if this annotated type applies to element `e`. */
345352
predicate appliesTo(Element e) { Annotations::elementTypeAnnotations(e, type, annotations) }
346353

347-
/** Holds if this annotated type is the `ith` type argument of constructed generic 'g'. */
354+
/** Holds if this annotated type is the `i`th type argument of constructed generic 'g'. */
348355
predicate appliesToTypeArgument(ConstructedGeneric g, int i) {
349-
this.getAnnotations().getNoFlagsNullability() = Annotations::getChildNullability(getElementNullability(g),
356+
Annotations::getNoFlagsNullability(this.getAnnotations()) = Annotations::getChildNullability(getElementNullability(g),
350357
i) and
351358
this.getType() = g.getTypeArgument(i)
352359
}
353360

354361
/** Holds if this annotated type applies to type parameter constraints `constraints`. */
355362
predicate appliesToTypeConstraint(TypeParameterConstraints constraints) {
356-
this.getAnnotations().getNoFlagsNullability() = getTypeParameterNullability(constraints, type)
363+
Annotations::getNoFlagsNullability(this.getAnnotations()) = getTypeParameterNullability(constraints,
364+
type)
357365
}
358366
}
359367

0 commit comments

Comments
 (0)