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

Skip to content

Commit 76454ed

Browse files
committed
C#: Fix formatting of arrays and NullableTypes
1 parent abf43da commit 76454ed

5 files changed

Lines changed: 131 additions & 32 deletions

File tree

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -174,31 +174,44 @@ private newtype TAnnotatedType =
174174

175175
/** A type with additional information. */
176176
class AnnotatedType extends TAnnotatedType {
177-
Type underlyingType;
177+
Type type;
178178

179179
Annotations::TypeAnnotations annotations;
180180

181-
AnnotatedType() { this = TAnnotatedTypeNullability(underlyingType, annotations) }
181+
AnnotatedType() { this = TAnnotatedTypeNullability(type, annotations) }
182182

183183
/** Gets a textual representation of this annotated type. */
184184
string toString() {
185-
result = annotations.getTypePrefix() + underlyingType.toStringWithTypes() + annotations.getTypeSuffix()
185+
result = annotations.getTypePrefix() + getUnderlyingType().toStringWithTypes() + annotations.getTypeSuffix()
186186
}
187187

188188
/** Gets the location of this annotated type. */
189-
Location getLocation() { result = underlyingType.getLocation() }
189+
Location getLocation() { result = type.getLocation() }
190+
191+
/**
192+
* Gets the unannotated type, for example `string` in `string?`.
193+
* Note that this might be a nullable value type (`System.Nullable`).
194+
*/
195+
final Type getType() { result = type }
190196

191197
/**
192198
* Gets the underlying type, for example `string` in `string?`
193-
* Note that this might itself be a nullable value type (`System.Nullable`).
199+
* or `int` in `int?`. This also gets the underlying type of
200+
* nullable value types (`System.Nullable`).
194201
*/
195-
Type getUnderlyingType() { result = underlyingType }
202+
final Type getUnderlyingType() {
203+
if type instanceof NullableType
204+
then result = type.(NullableType).getUnderlyingType()
205+
else result = type
206+
}
196207

197208
/** Gets the type annotation set of this annotated type. */
198209
private Annotations::TypeAnnotations getAnnotations() { result = annotations }
199210

200211
/** Gets a type annotation of this annotated type. */
201-
private Annotations::TypeAnnotation getAnAnnotation() { result = getAnnotations().getAnAnnotation() }
212+
private Annotations::TypeAnnotation getAnAnnotation() {
213+
result = getAnnotations().getAnAnnotation()
214+
}
202215

203216
/** Holds if the type is a non-nullable reference, for example, `string` in a nullable-enabled context. */
204217
predicate isNonNullableRefType() {
@@ -214,23 +227,20 @@ class AnnotatedType extends TAnnotatedType {
214227
/** Holds if the type is a `ref readonly`, for example the return type of `ref readonly int F()`. */
215228
predicate isReadonlyRef() { this.getAnAnnotation() instanceof Annotations::ReadonlyRefType }
216229

217-
/** Holds if the type is an `out`, for example parameter p in `void F(out int p)`. */
230+
/** Holds if the type is an `out`, for example parameter `p` in `void F(out int p)`. */
218231
predicate isOut() { this.getAnAnnotation() instanceof Annotations::OutType }
219232

220233
/** Holds if this annotated type applies to element `e`. */
221-
predicate appliesTo(Element e) {
222-
Annotations::elementTypeAnnotations(e, this.getUnderlyingType(), annotations)
223-
}
234+
predicate appliesTo(Element e) { Annotations::elementTypeAnnotations(e, type, annotations) }
224235

225236
/** Holds if this annotated type applies to type parameter constraints `constraints`. */
226237
predicate appliesToTypeConstraint(TypeParameterConstraints constraints) {
227-
annotations = Annotations::TAnnotationFlags(getTypeParameterFlags(constraints,
228-
this.getUnderlyingType()))
238+
annotations = Annotations::TAnnotationFlags(getTypeParameterFlags(constraints, type))
229239
}
230240

231241
/** Holds if this annotated type applies to the `i`th type argument of constructed generic `g`. */
232242
predicate appliesToTypeArgument(ConstructedGeneric g, int i) {
233-
this.getUnderlyingType() = g.getTypeArgument(i) and
243+
type = g.getTypeArgument(i) and
234244
this.getAnnotations() = Annotations::TAnnotationFlags(getTypeArgumentFlags(g, i))
235245
}
236246
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class TypeParameterConstraints extends Element, @type_parameter_constraints {
217217
*/
218218
deprecated Class getClassConstraint() { result = getATypeConstraint() }
219219

220-
/** Gets a type constraint, if any. */
220+
/** Gets a specific type constraint, if any. */
221221
Type getATypeConstraint() { specific_type_parameter_constraints(this, getTypeRef(result)) }
222222

223223
/** Gets an annotated specific type constraint, if any. */

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,7 @@ class DelegateType extends RefType, Parameterizable, @delegate_type {
743743
deprecated predicate returnsRef() { this.getAnnotatedReturnType().isRef() }
744744

745745
/** Holds if this delegate returns a `ref readonly`. */
746-
deprecated predicate returnsRefReadonly() {
747-
this.getAnnotatedReturnType().isReadonlyRef()
748-
}
746+
deprecated predicate returnsRefReadonly() { this.getAnnotatedReturnType().isReadonlyRef() }
749747
}
750748

751749
/**
@@ -808,13 +806,22 @@ class ArrayType extends DotNet::ArrayType, RefType, @array_type {
808806
if i = getRank() - 1 then result = "" else result = "," + getRankString(i + 1)
809807
}
810808

811-
private string getDimensionString()
812-
{
813-
result = "[" + getRankString(0) + "]"
809+
private string getDimensionString(AnnotatedType elementType) {
810+
exists(AnnotatedType et, string res |
811+
et = getAnnotatedElementType() and
812+
res = "[" + getRankString(0) + "]" and
813+
if et.getUnderlyingType() instanceof ArrayType and not et.isNullableRefType()
814+
then result = res + et.getUnderlyingType().(ArrayType).getDimensionString(elementType)
815+
else (
816+
result = res and elementType = et
817+
)
818+
)
814819
}
815820

816821
override string toStringWithTypes() {
817-
result = this.getAnnotatedElementType().toString() + this.getDimensionString()
822+
exists(AnnotatedType elementType |
823+
result = elementType.toString() + this.getDimensionString(elementType)
824+
)
818825
}
819826

820827
override Type getChild(int n) { result = getElementType() and n = 0 }

csharp/ql/test/library-tests/csharp8/NullableRefTypes.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,44 @@ class RefTypes
107107
ref MyClass RefProperty => ref Property!;
108108
}
109109

110+
class ToStringWithTypes
111+
{
112+
MyStruct? a;
113+
MyStruct[]? b;
114+
MyStruct?[] c;
115+
MyStruct?[]? d;
116+
117+
MyClass? e;
118+
MyClass?[] f;
119+
MyClass[]? g;
120+
MyClass?[]? h;
121+
122+
MyClass[,,]?[,][] i;
123+
MyClass[,,][,][] j;
124+
MyClass[,,,][][,][,,] k;
125+
MyClass?[,,,][][,]?[,,] l;
126+
}
127+
110128
#nullable disable
111129

130+
class ToStringWithTypes2
131+
{
132+
MyStruct? a;
133+
MyStruct[]? b;
134+
MyStruct?[] c;
135+
MyStruct?[]? d;
136+
137+
MyClass? e;
138+
MyClass?[] f;
139+
MyClass[]? g;
140+
MyClass?[]? h;
141+
142+
MyClass[,,]?[,][] i;
143+
MyClass[,,][,][] j;
144+
MyClass[,,,][][,][,,] k;
145+
MyClass?[,,,][][,]?[,,] l;
146+
}
147+
112148
class DisabledNullability
113149
{
114150
MyClass f1;

csharp/ql/test/library-tests/csharp8/NullableRefTypes.expected

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ assignableTypes
5959
| NullableRefTypes.cs:22:16:22:17 | G1 | NullableRefTypes.cs:6:7:6:13 | MyClass?[]! |
6060
| NullableRefTypes.cs:23:17:23:18 | G2 | NullableRefTypes.cs:6:7:6:13 | MyClass?[]? |
6161
| NullableRefTypes.cs:24:16:24:17 | G3 | NullableRefTypes.cs:6:7:6:13 | MyClass?[]! |
62-
| NullableRefTypes.cs:25:18:25:18 | H | NullableRefTypes.cs:6:7:6:13 | MyClass?[]![]! |
62+
| NullableRefTypes.cs:25:18:25:18 | H | NullableRefTypes.cs:6:7:6:13 | MyClass?[][]! |
6363
| NullableRefTypes.cs:26:38:26:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass![]?[]! |
64-
| NullableRefTypes.cs:27:38:27:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass?[]![]! |
64+
| NullableRefTypes.cs:27:38:27:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass?[][]! |
6565
| NullableRefTypes.cs:32:20:32:20 | a | NullableRefTypes.cs:6:7:6:13 | MyClass! |
6666
| NullableRefTypes.cs:32:31:32:31 | b | NullableRefTypes.cs:6:7:6:13 | MyClass? |
6767
| NullableRefTypes.cs:37:17:37:17 | a | NullableRefTypes.cs:6:7:6:13 | MyClass! |
@@ -90,17 +90,61 @@ assignableTypes
9090
| NullableRefTypes.cs:104:51:104:52 | p2 | NullableRefTypes.cs:6:7:6:13 | out MyClass? |
9191
| NullableRefTypes.cs:106:14:106:21 | Property | NullableRefTypes.cs:6:7:6:13 | MyClass? |
9292
| NullableRefTypes.cs:107:17:107:27 | RefProperty | NullableRefTypes.cs:6:7:6:13 | ref MyClass! |
93-
| NullableRefTypes.cs:114:13:114:14 | f1 | NullableRefTypes.cs:6:7:6:13 | MyClass |
94-
| NullableRefTypes.cs:115:13:115:13 | P | NullableRefTypes.cs:6:7:6:13 | MyClass |
95-
| NullableRefTypes.cs:116:24:116:24 | p | NullableRefTypes.cs:6:7:6:13 | MyClass |
96-
| NullableRefTypes.cs:118:17:118:17 | a | NullableRefTypes.cs:6:7:6:13 | MyClass |
93+
| NullableRefTypes.cs:112:15:112:15 | a | NullableRefTypes.cs:159:8:159:15 | MyStruct? |
94+
| NullableRefTypes.cs:113:17:113:17 | b | NullableRefTypes.cs:159:8:159:15 | MyStruct![]? |
95+
| NullableRefTypes.cs:114:17:114:17 | c | NullableRefTypes.cs:159:8:159:15 | MyStruct?[]! |
96+
| NullableRefTypes.cs:115:18:115:18 | d | NullableRefTypes.cs:159:8:159:15 | MyStruct?[]? |
97+
| NullableRefTypes.cs:117:14:117:14 | e | NullableRefTypes.cs:6:7:6:13 | MyClass? |
98+
| NullableRefTypes.cs:118:16:118:16 | f | NullableRefTypes.cs:6:7:6:13 | MyClass?[]! |
99+
| NullableRefTypes.cs:119:16:119:16 | g | NullableRefTypes.cs:6:7:6:13 | MyClass![]? |
100+
| NullableRefTypes.cs:120:17:120:17 | h | NullableRefTypes.cs:6:7:6:13 | MyClass?[]? |
101+
| NullableRefTypes.cs:122:23:122:23 | i | NullableRefTypes.cs:6:7:6:13 | MyClass![,,]?[,][]! |
102+
| NullableRefTypes.cs:123:22:123:22 | j | NullableRefTypes.cs:6:7:6:13 | MyClass![,,][,][]! |
103+
| NullableRefTypes.cs:124:27:124:27 | k | NullableRefTypes.cs:6:7:6:13 | MyClass![,,,][][,][,,]! |
104+
| NullableRefTypes.cs:125:29:125:29 | l | NullableRefTypes.cs:6:7:6:13 | MyClass?[,,,][][,]?[,,]! |
105+
| NullableRefTypes.cs:132:15:132:15 | a | NullableRefTypes.cs:159:8:159:15 | MyStruct? |
106+
| NullableRefTypes.cs:133:17:133:17 | b | NullableRefTypes.cs:159:8:159:15 | MyStruct![]? |
107+
| NullableRefTypes.cs:134:17:134:17 | c | NullableRefTypes.cs:159:8:159:15 | MyStruct?[] |
108+
| NullableRefTypes.cs:135:18:135:18 | d | NullableRefTypes.cs:159:8:159:15 | MyStruct?[]? |
109+
| NullableRefTypes.cs:137:14:137:14 | e | NullableRefTypes.cs:6:7:6:13 | MyClass? |
110+
| NullableRefTypes.cs:138:16:138:16 | f | NullableRefTypes.cs:6:7:6:13 | MyClass?[] |
111+
| NullableRefTypes.cs:139:16:139:16 | g | NullableRefTypes.cs:6:7:6:13 | MyClass[]? |
112+
| NullableRefTypes.cs:140:17:140:17 | h | NullableRefTypes.cs:6:7:6:13 | MyClass?[]? |
113+
| NullableRefTypes.cs:142:23:142:23 | i | NullableRefTypes.cs:6:7:6:13 | MyClass[,,]?[,][] |
114+
| NullableRefTypes.cs:143:22:143:22 | j | NullableRefTypes.cs:6:7:6:13 | MyClass[,,][,][] |
115+
| NullableRefTypes.cs:144:27:144:27 | k | NullableRefTypes.cs:6:7:6:13 | MyClass[,,,][][,][,,] |
116+
| NullableRefTypes.cs:145:29:145:29 | l | NullableRefTypes.cs:6:7:6:13 | MyClass?[,,,][][,]?[,,] |
117+
| NullableRefTypes.cs:150:13:150:14 | f1 | NullableRefTypes.cs:6:7:6:13 | MyClass |
118+
| NullableRefTypes.cs:151:13:151:13 | P | NullableRefTypes.cs:6:7:6:13 | MyClass |
119+
| NullableRefTypes.cs:152:24:152:24 | p | NullableRefTypes.cs:6:7:6:13 | MyClass |
120+
| NullableRefTypes.cs:154:17:154:17 | a | NullableRefTypes.cs:6:7:6:13 | MyClass |
97121
arrayElements
98122
| NullableRefTypes.cs:22:16:22:17 | G1 | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass? |
99123
| NullableRefTypes.cs:23:17:23:18 | G2 | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass? |
100124
| NullableRefTypes.cs:24:16:24:17 | G3 | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass? |
101125
| NullableRefTypes.cs:25:18:25:18 | H | NullableRefTypes.cs:6:7:6:13 | MyClass[][] | NullableRefTypes.cs:6:7:6:13 | MyClass?[]! |
102126
| NullableRefTypes.cs:26:38:26:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass[][] | NullableRefTypes.cs:6:7:6:13 | MyClass![]? |
103127
| NullableRefTypes.cs:27:38:27:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass[][] | NullableRefTypes.cs:6:7:6:13 | MyClass?[]! |
128+
| NullableRefTypes.cs:113:17:113:17 | b | NullableRefTypes.cs:159:8:159:15 | MyStruct[] | NullableRefTypes.cs:159:8:159:15 | MyStruct! |
129+
| NullableRefTypes.cs:114:17:114:17 | c | NullableRefTypes.cs:159:8:159:15 | Nullable | NullableRefTypes.cs:159:8:159:15 | MyStruct? |
130+
| NullableRefTypes.cs:115:18:115:18 | d | NullableRefTypes.cs:159:8:159:15 | Nullable | NullableRefTypes.cs:159:8:159:15 | MyStruct? |
131+
| NullableRefTypes.cs:118:16:118:16 | f | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass? |
132+
| NullableRefTypes.cs:119:16:119:16 | g | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass! |
133+
| NullableRefTypes.cs:120:17:120:17 | h | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass? |
134+
| NullableRefTypes.cs:122:23:122:23 | i | NullableRefTypes.cs:6:7:6:13 | MyClass[,,][][,] | NullableRefTypes.cs:6:7:6:13 | MyClass![,,]?[]! |
135+
| NullableRefTypes.cs:123:22:123:22 | j | NullableRefTypes.cs:6:7:6:13 | MyClass[][,][,,] | NullableRefTypes.cs:6:7:6:13 | MyClass![,][]! |
136+
| NullableRefTypes.cs:124:27:124:27 | k | NullableRefTypes.cs:6:7:6:13 | MyClass[,,][,][][,,,] | NullableRefTypes.cs:6:7:6:13 | MyClass![][,][,,]! |
137+
| NullableRefTypes.cs:125:29:125:29 | l | NullableRefTypes.cs:6:7:6:13 | MyClass[,][][,,,][,,] | NullableRefTypes.cs:6:7:6:13 | MyClass?[,,,][][,]? |
138+
| NullableRefTypes.cs:133:17:133:17 | b | NullableRefTypes.cs:159:8:159:15 | MyStruct[] | NullableRefTypes.cs:159:8:159:15 | MyStruct! |
139+
| NullableRefTypes.cs:134:17:134:17 | c | NullableRefTypes.cs:159:8:159:15 | Nullable | NullableRefTypes.cs:159:8:159:15 | MyStruct? |
140+
| NullableRefTypes.cs:135:18:135:18 | d | NullableRefTypes.cs:159:8:159:15 | Nullable | NullableRefTypes.cs:159:8:159:15 | MyStruct? |
141+
| NullableRefTypes.cs:138:16:138:16 | f | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass? |
142+
| NullableRefTypes.cs:139:16:139:16 | g | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass |
143+
| NullableRefTypes.cs:140:17:140:17 | h | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass? |
144+
| NullableRefTypes.cs:142:23:142:23 | i | NullableRefTypes.cs:6:7:6:13 | MyClass[,,][][,] | NullableRefTypes.cs:6:7:6:13 | MyClass[,,]?[] |
145+
| NullableRefTypes.cs:143:22:143:22 | j | NullableRefTypes.cs:6:7:6:13 | MyClass[][,][,,] | NullableRefTypes.cs:6:7:6:13 | MyClass[,][] |
146+
| NullableRefTypes.cs:144:27:144:27 | k | NullableRefTypes.cs:6:7:6:13 | MyClass[,,][,][][,,,] | NullableRefTypes.cs:6:7:6:13 | MyClass[][,][,,] |
147+
| NullableRefTypes.cs:145:29:145:29 | l | NullableRefTypes.cs:6:7:6:13 | MyClass[,][][,,,][,,] | NullableRefTypes.cs:6:7:6:13 | MyClass?[,,,][][,]? |
104148
returnTypes
105149
| NullableRefTypes.cs:6:7:6:13 | MyClass | Void |
106150
| NullableRefTypes.cs:13:19:13:22 | get_C | MyClass? |
@@ -142,10 +186,12 @@ returnTypes
142186
| NullableRefTypes.cs:102:26:102:36 | ReturnsRef6 | readonly MyClass! |
143187
| NullableRefTypes.cs:104:10:104:20 | Parameters1 | Void! |
144188
| NullableRefTypes.cs:107:32:107:44 | get_RefProperty | MyClass! |
145-
| NullableRefTypes.cs:112:7:112:25 | DisabledNullability | Void |
146-
| NullableRefTypes.cs:115:18:115:30 | get_P | MyClass |
147-
| NullableRefTypes.cs:116:13:116:14 | Fn | MyClass |
148-
| NullableRefTypes.cs:123:8:123:15 | MyStruct | Void |
189+
| NullableRefTypes.cs:110:7:110:23 | ToStringWithTypes | Void |
190+
| NullableRefTypes.cs:130:7:130:24 | ToStringWithTypes2 | Void |
191+
| NullableRefTypes.cs:148:7:148:25 | DisabledNullability | Void |
192+
| NullableRefTypes.cs:151:18:151:30 | get_P | MyClass |
193+
| NullableRefTypes.cs:152:13:152:14 | Fn | MyClass |
194+
| NullableRefTypes.cs:159:8:159:15 | MyStruct | Void |
149195
typeArguments
150196
| NullableRefTypes.cs:51:12:51:15 | Q | 0 | MyClass |
151197
| NullableRefTypes.cs:54:11:54:33 | Generic<MyClass,MyClass,IDisposable,MyClass> | 0 | MyClass? |

0 commit comments

Comments
 (0)