Skip to content

Commit 0cfd73c

Browse files
committed
Adjust QL getName to the extracted undecorated names
1 parent 8df7706 commit 0cfd73c

File tree

2 files changed

+70
-50
lines changed

2 files changed

+70
-50
lines changed

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

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,28 @@ class UnboundGenericType extends ValueOrRefType, UnboundGeneric {
105105

106106
override Location getALocation() { type_location(this, result) }
107107

108-
/** Gets the name of this generic type without the `<...>` brackets. */
109-
string getNameWithoutBrackets() {
110-
result = getName().prefix(getName().length() - getNumberOfTypeParameters() - 1)
108+
override UnboundGenericType getUnboundDeclaration() {
109+
result = ValueOrRefType.super.getUnboundDeclaration()
110+
}
111+
112+
final override Type getChild(int n) { result = getTypeParameter(n) }
113+
114+
private string getTypeParameterCommas() {
115+
result = strictconcat(int i | exists(this.getTypeParameter(i)) | "", ",")
111116
}
112117

113118
override string toStringWithTypes() {
114-
result = getNameWithoutBrackets() + "<" + this.typeParametersToString() + ">"
119+
result = this.getNameWithoutBrackets() + "<" + this.typeParametersToString() + ">"
115120
}
116121

117-
override UnboundGenericType getUnboundDeclaration() {
118-
result = ValueOrRefType.super.getUnboundDeclaration()
122+
final override string getName() {
123+
result = this.getNameWithoutBrackets() + "<" + this.getTypeParameterCommas() + ">"
119124
}
120125

121-
final override Type getChild(int n) { result = getTypeParameter(n) }
126+
final override predicate hasQualifiedName(string qualifier, string name) {
127+
super.hasQualifiedName(qualifier, _) and
128+
name = this.getNameWithoutBrackets() + "<" + this.getTypeParameterCommas() + ">"
129+
}
122130
}
123131

124132
/**
@@ -360,22 +368,51 @@ class ConstructedType extends ValueOrRefType, ConstructedGeneric {
360368

361369
override UnboundGenericType getUnboundGeneric() { constructed_generic(this, getTypeRef(result)) }
362370

363-
override string toStringWithTypes() {
371+
final override Type getChild(int n) { result = getTypeArgument(n) }
372+
373+
language[monotonicAggregates]
374+
private string getTypeArgumentsString() {
364375
result =
365-
getUnboundGeneric().getNameWithoutBrackets() + "<" + this.getTypeArgumentsString() + ">"
376+
strictconcat(Type t, int i | t = this.getTypeArgument(i) | t.toString(), ", " order by i)
366377
}
367378

368-
final override Type getChild(int n) { result = getTypeArgument(n) }
379+
language[monotonicAggregates]
380+
private string getTypeArgumentsNames() {
381+
result =
382+
strictconcat(Type t, int i | t = this.getTypeArgument(i) | t.getName(), ", " order by i)
383+
}
369384

370385
language[monotonicAggregates]
371-
private string getTypeArgumentsString() {
386+
private string getTypeArgumentsQualifiedNames() {
372387
result =
373-
concat(int i |
374-
exists(this.getTypeArgument(i))
388+
strictconcat(Type t, int i |
389+
t = this.getTypeArgument(i)
375390
|
376-
this.getTypeArgument(i).toString(), ", " order by i
391+
t.getQualifiedName(), "," order by i
377392
)
378393
}
394+
395+
final override string toStringWithTypes() {
396+
exists(string undecorated |
397+
types(this, _, undecorated) and
398+
result = undecorated + "<" + this.getTypeArgumentsString() + ">"
399+
)
400+
}
401+
402+
final override string getName() {
403+
exists(string undecorated |
404+
types(this, _, undecorated) and
405+
result = undecorated + "<" + this.getTypeArgumentsNames() + ">"
406+
)
407+
}
408+
409+
final override predicate hasQualifiedName(string qualifier, string name) {
410+
exists(string undecorated |
411+
super.hasQualifiedName(qualifier, _) and
412+
types(this, _, undecorated) and
413+
name = undecorated + "<" + this.getTypeArgumentsQualifiedNames() + ">"
414+
)
415+
}
379416
}
380417

381418
/**

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

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,8 @@ private predicate isObjectClass(Class c) { c instanceof ObjectType }
5555
* Either a value type (`ValueType`) or a reference type (`RefType`).
5656
*/
5757
class ValueOrRefType extends DotNet::ValueOrRefType, Type, Attributable, @value_or_ref_type {
58-
/** Gets the name of this type without `<...>` brackets, in case it is a constructed type. */
59-
private string getNameWithoutBrackets() {
60-
exists(UnboundGenericType unbound, string name |
61-
unbound = this.(ConstructedType).getUnboundDeclaration() and
62-
name = unbound.getName() and
63-
result = name.prefix(name.length() - unbound.getNumberOfTypeParameters() - 1)
64-
)
65-
or
66-
not this instanceof ConstructedType and
67-
result = this.getName()
68-
}
69-
70-
language[monotonicAggregates]
71-
private string getQualifiedTypeArguments() {
72-
result =
73-
strictconcat(Type t, int i |
74-
t = this.(ConstructedType).getTypeArgument(i)
75-
|
76-
t.getQualifiedName(), "," order by i
77-
)
78-
}
58+
/** Gets the name of this type without `<...>` brackets, in case it is a generic type. */
59+
string getNameWithoutBrackets() { types(this, _, result) }
7960

8061
/**
8162
* Holds if this type has the qualified name `qualifier`.`name`.
@@ -84,21 +65,14 @@ class ValueOrRefType extends DotNet::ValueOrRefType, Type, Attributable, @value_
8465
* `qualifier`=`System.IO` and `name`=`IOException`.
8566
*/
8667
override predicate hasQualifiedName(string qualifier, string name) {
87-
exists(string name0 |
88-
not this instanceof ConstructedType and
89-
name = name0
90-
or
91-
name = name0 + "<" + this.getQualifiedTypeArguments() + ">"
92-
|
93-
exists(string enclosing |
94-
this.getDeclaringType().hasQualifiedName(qualifier, enclosing) and
95-
name0 = enclosing + "+" + this.getNameWithoutBrackets()
96-
)
97-
or
98-
not exists(this.getDeclaringType()) and
99-
qualifier = this.getNamespace().getQualifiedName() and
100-
name0 = this.getNameWithoutBrackets()
68+
exists(string enclosing |
69+
this.getDeclaringType().hasQualifiedName(qualifier, enclosing) and
70+
name = enclosing + "+" + this.getNameWithoutBrackets()
10171
)
72+
or
73+
not exists(this.getDeclaringType()) and
74+
qualifier = this.getNamespace().getQualifiedName() and
75+
name = this.getNameWithoutBrackets()
10276
}
10377

10478
/** Gets the namespace containing this type. */
@@ -963,6 +937,15 @@ class NullableType extends ValueType, DotNet::ConstructedGeneric, @nullable_type
963937
override Type getTypeArgument(int p) { p = 0 and result = getUnderlyingType() }
964938

965939
override string getAPrimaryQlClass() { result = "NullableType" }
940+
941+
final override string getName() {
942+
result = "Nullable<" + this.getUnderlyingType().getName() + ">"
943+
}
944+
945+
final override predicate hasQualifiedName(string qualifier, string name) {
946+
qualifier = "System" and
947+
name = "Nullable<" + this.getUnderlyingType().getQualifiedName() + ">"
948+
}
966949
}
967950

968951
/**
@@ -1046,7 +1029,7 @@ class PointerType extends DotNet::PointerType, Type, @pointer_type {
10461029

10471030
override Type getChild(int n) { result = getReferentType() and n = 0 }
10481031

1049-
override string getName() { result = DotNet::PointerType.super.getName() }
1032+
override string getName() { types(this, _, result) }
10501033

10511034
override Location getALocation() { result = getReferentType().getALocation() }
10521035

0 commit comments

Comments
 (0)