Skip to content

Commit c717e34

Browse files
committed
C#: Move qualified name computation into QualifiedName.qll
1 parent e75562e commit c717e34

File tree

11 files changed

+224
-136
lines changed

11 files changed

+224
-136
lines changed

csharp/ql/lib/semmle/code/cil/Declaration.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import CIL
66
private import dotnet
77
private import semmle.code.csharp.Member as CS
8+
private import semmle.code.csharp.commons.QualifiedName
89

910
/**
1011
* A declaration. Either a member (`Member`) or a variable (`Variable`).
@@ -23,6 +24,14 @@ class Declaration extends DotNet::Declaration, Element, @cil_declaration {
2324
}
2425

2526
override Declaration getUnboundDeclaration() { result = this }
27+
28+
override predicate hasQualifiedName(string qualifier, string name) {
29+
exists(string dqualifier, string dname |
30+
this.getDeclaringType().hasQualifiedName(dqualifier, dname) and
31+
qualifier = getQualifiedName(dqualifier, dname)
32+
) and
33+
name = this.getName()
34+
}
2635
}
2736

2837
private CS::Declaration toCSharpNonTypeParameter(Declaration d) { result.matchesHandle(d) }

csharp/ql/lib/semmle/code/csharp/Callable.qll

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -472,16 +472,6 @@ class Operator extends Callable, Member, Attributable, Overridable, @operator {
472472
override string toString() { result = Callable.super.toString() }
473473

474474
override Parameter getRawParameter(int i) { result = this.getParameter(i) }
475-
476-
override predicate hasQualifiedName(string qualifier, string name) {
477-
super.hasQualifiedName(qualifier, _) and
478-
name = this.getFunctionName()
479-
}
480-
481-
override predicate hasQualifiedName(string namespace, string type, string name) {
482-
super.hasQualifiedName(namespace, type, _) and
483-
name = this.getFunctionName()
484-
}
485475
}
486476

487477
/** A clone method on a record. */
@@ -1113,14 +1103,6 @@ class LocalFunction extends Callable, Modifiable, Attributable, @local_function
11131103

11141104
override Callable getEnclosingCallable() { result = this.getStatement().getEnclosingCallable() }
11151105

1116-
override predicate hasQualifiedName(string qualifier, string name) {
1117-
exists(string cqualifier, string type |
1118-
this.getEnclosingCallable().hasQualifiedName(cqualifier, type) and
1119-
qualifier = getQualifiedName(cqualifier, type)
1120-
) and
1121-
name = this.getName()
1122-
}
1123-
11241106
override Location getALocation() { result = this.getStatement().getALocation() }
11251107

11261108
override Parameter getRawParameter(int i) { result = this.getParameter(i) }

csharp/ql/lib/semmle/code/csharp/Element.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*/
44

55
import Location
6-
import Member
76
private import semmle.code.csharp.ExprOrStmtParent
87
private import dotnet
8+
private import commons.QualifiedName
99

1010
/**
1111
* A program element. Either a control flow element (`ControlFlowElement`), an

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

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,6 @@ private string getTypeArgumentsNames(ConstructedGeneric cg) {
101101
result = strictconcat(Type t, int i | t = cg.getTypeArgument(i) | t.getName(), "," order by i)
102102
}
103103

104-
bindingset[t]
105-
private string getFullName(Type t) {
106-
exists(string qualifier, string name |
107-
t.hasQualifiedName(qualifier, name) and
108-
result = getQualifiedName(qualifier, name)
109-
)
110-
}
111-
112-
/** Gets the concatenation of the `getFullName` of type arguments. */
113-
language[monotonicAggregates]
114-
private string getTypeArgumentsQualifiedNames(ConstructedGeneric cg) {
115-
result = strictconcat(Type t, int i | t = cg.getTypeArgument(i) | getFullName(t), "," order by i)
116-
}
117-
118104
/**
119105
* An unbound generic type. This is a generic type with type parameters
120106
* (for example `List<T>`) or elided type parameters (for example `List<>`).
@@ -161,19 +147,6 @@ class UnboundGenericType extends ValueOrRefType, UnboundGeneric {
161147
final override string getName() {
162148
result = this.getUndecoratedName() + "<" + getTypeParameterCommas(this) + ">"
163149
}
164-
165-
final override predicate hasQualifiedName(string qualifier, string name) {
166-
exists(string name0 | name = name0 + "<" + getTypeParameterCommas(this) + ">" |
167-
exists(string enclosing |
168-
this.getDeclaringType().hasQualifiedName(qualifier, enclosing) and
169-
name0 = enclosing + "+" + this.getUndecoratedName()
170-
)
171-
or
172-
not exists(this.getDeclaringType()) and
173-
qualifier = this.getNamespace().getFullName() and
174-
name0 = this.getUndecoratedName()
175-
)
176-
}
177150
}
178151

179152
/**
@@ -240,11 +213,6 @@ class TypeParameter extends DotNet::TypeParameter, Type, @type_parameter {
240213
/** Gets the generic that defines this type parameter. */
241214
UnboundGeneric getGeneric() { type_parameters(this, _, result, _) }
242215

243-
final override predicate hasQualifiedName(string qualifier, string name) {
244-
qualifier = "" and
245-
name = this.getName()
246-
}
247-
248216
override string getAPrimaryQlClass() { result = "TypeParameter" }
249217
}
250218

@@ -440,19 +408,6 @@ class ConstructedType extends ValueOrRefType, ConstructedGeneric {
440408
final override string getName() {
441409
result = this.getUndecoratedName() + "<" + getTypeArgumentsNames(this) + ">"
442410
}
443-
444-
override predicate hasQualifiedName(string qualifier, string name) {
445-
exists(string name0 | name = name0 + "<" + getTypeArgumentsQualifiedNames(this) + ">" |
446-
exists(string enclosing |
447-
this.getDeclaringType().hasQualifiedName(qualifier, enclosing) and
448-
name0 = enclosing + "+" + this.getUndecoratedName()
449-
)
450-
or
451-
not exists(this.getDeclaringType()) and
452-
qualifier = this.getNamespace().getFullName() and
453-
name0 = this.getUndecoratedName()
454-
)
455-
}
456411
}
457412

458413
/**
@@ -624,11 +579,6 @@ class ConstructedMethod extends Method, ConstructedGeneric {
624579
result = this.getUndecoratedName() + "<" + getTypeArgumentsNames(this) + ">"
625580
}
626581

627-
override predicate hasQualifiedName(string namespace, string type, string name) {
628-
this.getDeclaringType().hasQualifiedName(namespace, type) and
629-
name = this.getUndecoratedName() + "<" + getTypeArgumentsQualifiedNames(this) + ">"
630-
}
631-
632582
final override string getUndecoratedName() { methods(this, result, _, _, _) }
633583
}
634584

csharp/ql/lib/semmle/code/csharp/Member.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import Variable
77
private import dotnet
88
private import Implements
99
private import TypeRef
10+
private import commons.QualifiedName
11+
12+
private module QualifiedNameInput implements QualifiedNameInputSig { }
1013

1114
/**
1215
* A declaration.
@@ -21,6 +24,10 @@ class Declaration extends DotNet::Declaration, Element, @declaration {
2124

2225
override string toString() { result = this.getName() }
2326

27+
override predicate hasQualifiedName(string qualifier, string name) {
28+
QualifiedName<QualifiedNameInput>::hasQualifiedName(this, qualifier, name)
29+
}
30+
2431
/**
2532
* Gets the fully qualified name of this declaration, including types, for example
2633
* the fully qualified name with types of `M` on line 3 is `N.C.M(int, string)` in
@@ -199,6 +206,10 @@ class Member extends DotNet::Member, Modifiable, @member {
199206
override predicate isRequired() { Modifiable.super.isRequired() }
200207

201208
override predicate isFile() { Modifiable.super.isFile() }
209+
210+
final override predicate hasQualifiedName(string namespace, string type, string name) {
211+
QualifiedName<QualifiedNameInput>::hasQualifiedName(this, namespace, type, name)
212+
}
202213
}
203214

204215
private class TOverridable = @virtualizable or @callable_accessor;

csharp/ql/lib/semmle/code/csharp/Namespace.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ private import dotnet
77
/**
88
* A type container. Either a namespace (`Namespace`) or a type (`Type`).
99
*/
10-
class TypeContainer extends DotNet::NamedElement, Element, @type_container { }
10+
class TypeContainer extends Declaration, @type_container { }
1111

1212
/**
1313
* A namespace, for example
@@ -30,6 +30,10 @@ class Namespace extends DotNet::Namespace, TypeContainer, Declaration, @namespac
3030
parent_namespace(result, this)
3131
}
3232

33+
override predicate hasQualifiedName(string qualifier, string name) {
34+
DotNet::Namespace.super.hasQualifiedName(qualifier, name)
35+
}
36+
3337
/**
3438
* Gets a type directly declared in this namespace, if any.
3539
* For example, the class `File` in

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

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,6 @@ private predicate isObjectClass(Class c) { c instanceof ObjectType }
5656
* Either a value type (`ValueType`) or a reference type (`RefType`).
5757
*/
5858
class ValueOrRefType extends DotNet::ValueOrRefType, Type, Attributable, @value_or_ref_type {
59-
/**
60-
* Holds if this type has the qualified name `qualifier`.`name`.
61-
*
62-
* For example the class `System.IO.IOException` has
63-
* `qualifier`=`System.IO` and `name`=`IOException`.
64-
*/
65-
override predicate hasQualifiedName(string qualifier, string name) {
66-
exists(string enclosing |
67-
this.getDeclaringType().hasQualifiedName(qualifier, enclosing) and
68-
name = enclosing + "+" + this.getUndecoratedName()
69-
)
70-
or
71-
not exists(this.getDeclaringType()) and
72-
qualifier = this.getNamespace().getFullName() and
73-
name = this.getUndecoratedName()
74-
}
75-
7659
/** Gets the namespace containing this type. */
7760
Namespace getNamespace() {
7861
if exists(this.getDeclaringType())
@@ -409,11 +392,6 @@ class NonNestedType extends ValueOrRefType {
409392
* The `void` type.
410393
*/
411394
class VoidType extends ValueOrRefType, @void_type {
412-
override predicate hasQualifiedName(string qualifier, string name) {
413-
qualifier = "System" and
414-
name = "Void"
415-
}
416-
417395
final override string getName() { result = "Void" }
418396

419397
final override string getUndecoratedName() { result = "Void" }
@@ -1028,11 +1006,6 @@ class NullableType extends ValueType, ConstructedType, @nullable_type {
10281006
override Type getTypeArgument(int p) { p = 0 and result = this.getUnderlyingType() }
10291007

10301008
override string getAPrimaryQlClass() { result = "NullableType" }
1031-
1032-
final override predicate hasQualifiedName(string qualifier, string name) {
1033-
qualifier = "System" and
1034-
name = "Nullable<" + this.getUnderlyingType().getQualifiedName() + ">"
1035-
}
10361009
}
10371010

10381011
/**
@@ -1102,13 +1075,6 @@ class ArrayType extends DotNet::ArrayType, RefType, @array_type {
11021075
not type_location(this, _) and
11031076
result = this.getElementType().getALocation()
11041077
}
1105-
1106-
final override predicate hasQualifiedName(string qualifier, string name) {
1107-
exists(Type elementType, string name0 |
1108-
elementType.hasQualifiedName(qualifier, name0) and
1109-
name = name0 + this.getDimensionString(elementType)
1110-
)
1111-
}
11121078
}
11131079

11141080
/**
@@ -1137,13 +1103,6 @@ class PointerType extends DotNet::PointerType, Type, @pointer_type {
11371103
override string toString() { result = DotNet::PointerType.super.toString() }
11381104

11391105
override string getAPrimaryQlClass() { result = "PointerType" }
1140-
1141-
final override predicate hasQualifiedName(string qualifier, string name) {
1142-
exists(string name0 |
1143-
this.getReferentType().hasQualifiedName(qualifier, name0) and
1144-
name = name0 + "*"
1145-
)
1146-
}
11471106
}
11481107

11491108
/**
@@ -1230,10 +1189,6 @@ class TupleType extends ValueType, @tuple_type {
12301189

12311190
override Type getChild(int i) { result = this.getUnderlyingType().getChild(i) }
12321191

1233-
final override predicate hasQualifiedName(string qualifier, string name) {
1234-
this.getUnderlyingType().hasQualifiedName(qualifier, name)
1235-
}
1236-
12371192
override string getAPrimaryQlClass() { result = "TupleType" }
12381193
}
12391194

csharp/ql/lib/semmle/code/csharp/Variable.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ class LocalScopeVariable extends Variable, @local_scope_variable {
7575
* Holds if this local variable or parameter is `scoped`.
7676
*/
7777
predicate isScoped() { scoped_annotation(this, _) }
78-
79-
override predicate hasQualifiedName(string qualifier, string name) { none() }
8078
}
8179

8280
/**

0 commit comments

Comments
 (0)