Skip to content

Commit 315a3a5

Browse files
committed
C#: Add hasQualifiedName/3 including overrides where relevant and re-write some of the existing hasQualifiedName/2 predicates.
1 parent 3856540 commit 315a3a5

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import CIL
66
private import dotnet
7+
private import semmle.code.csharp.Printing
78

89
/**
910
* Something that contains other types.
@@ -52,7 +53,9 @@ class Type extends DotNet::Type, Declaration, TypeContainer, @cil_type {
5253

5354
override predicate hasQualifiedName(string qualifier, string name) {
5455
name = this.getName() and
55-
qualifier = this.getParent().getQualifiedName()
56+
exists(string pqualifier, string pname | this.getParent().hasQualifiedName(pqualifier, pname) |
57+
qualifier = printQualifiedName(pqualifier, pname)
58+
)
5659
}
5760

5861
override Location getALocation() { cil_type_location(this.getUnboundDeclaration(), result) }

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ class Operator extends Callable, Member, Attributable, @operator {
459459
super.hasQualifiedName(qualifier, _) and
460460
name = this.getFunctionName()
461461
}
462+
463+
override predicate hasQualifiedName(string namespace, string type, string name) {
464+
super.hasQualifiedName(namespace, type, _) and
465+
name = this.getFunctionName()
466+
}
462467
}
463468

464469
/** A clone method on a record. */
@@ -996,7 +1001,10 @@ class LocalFunction extends Callable, Modifiable, Attributable, @local_function
9961001
override Callable getEnclosingCallable() { result = this.getStatement().getEnclosingCallable() }
9971002

9981003
override predicate hasQualifiedName(string qualifier, string name) {
999-
qualifier = this.getEnclosingCallable().getQualifiedName() and
1004+
exists(string cqualifier, string type |
1005+
this.getEnclosingCallable().hasQualifiedName(cqualifier, type) and
1006+
qualifier = printQualifiedName(cqualifier, type)
1007+
) and
10001008
name = this.getName()
10011009
}
10021010

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class UnboundGenericType extends ValueOrRefType, UnboundGeneric {
159159
)
160160
or
161161
not exists(this.getDeclaringType()) and
162-
qualifier = this.getNamespace().getQualifiedName() and
162+
qualifier = this.getNamespace().getFullName() and
163163
name0 = this.getUndecoratedName()
164164
)
165165
}
@@ -424,7 +424,7 @@ class ConstructedType extends ValueOrRefType, ConstructedGeneric {
424424
)
425425
or
426426
not exists(this.getDeclaringType()) and
427-
qualifier = this.getNamespace().getQualifiedName() and
427+
qualifier = this.getNamespace().getFullName() and
428428
name0 = this.getUndecoratedName()
429429
)
430430
}
@@ -594,8 +594,8 @@ class ConstructedMethod extends Method, ConstructedGeneric {
594594
result = this.getUndecoratedName() + "<" + getTypeArgumentsNames(this) + ">"
595595
}
596596

597-
override predicate hasQualifiedName(string qualifier, string name) {
598-
qualifier = this.getDeclaringType().getQualifiedName() and
597+
override predicate hasQualifiedName(string qualifier, string type, string name) {
598+
this.getDeclaringType().hasQualifiedName(qualifier, type) and
599599
name = this.getUndecoratedName() + "<" + getTypeArgumentsQualifiedNames(this) + ">"
600600
}
601601

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
import Element
66
import Type
7+
private import semmle.code.csharp.Printing
78

89
/** A declaration. */
910
class Declaration extends NamedElement, @dotnet_declaration {
1011
override predicate hasQualifiedName(string qualifier, string name) {
11-
qualifier = this.getDeclaringType().getQualifiedName() and
12+
exists(string dqualifier, string dname |
13+
this.getDeclaringType().hasQualifiedName(dqualifier, dname) and
14+
qualifier = printQualifiedName(dqualifier, dname)
15+
) and
1216
name = this.getName()
1317
}
1418

@@ -75,6 +79,16 @@ class Member extends Declaration, @dotnet_member {
7579

7680
/** Holds if this member is `static`. */
7781
predicate isStatic() { none() }
82+
83+
/**
84+
* Holds if this member has name `name` and is defined in type `type`
85+
* with qualifier `qualifier`
86+
*/
87+
cached
88+
predicate hasQualifiedName(string qualifier, string type, string name) {
89+
this.getDeclaringType().hasQualifiedName(qualifier, type) and
90+
name = this.getName()
91+
}
7892
}
7993

8094
/** A property. */

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
private import Declaration
6+
private import semmle.code.csharp.Printing
67

78
/** A namespace. */
89
class Namespace extends Declaration, @namespace {
@@ -25,7 +26,10 @@ class Namespace extends Declaration, @namespace {
2526
* `qualifier`=`System.Collections` and `name`=`Generic`.
2627
*/
2728
override predicate hasQualifiedName(string qualifier, string name) {
28-
qualifier = this.getParentNamespace().getQualifiedName() and
29+
exists(string pqualifier, string pname |
30+
this.getParentNamespace().hasQualifiedName(pqualifier, pname) and
31+
qualifier = printQualifiedName(pqualifier, pname)
32+
) and
2933
name = this.getName()
3034
}
3135

@@ -41,6 +45,16 @@ class Namespace extends Declaration, @namespace {
4145
final override string getUndecoratedName() { namespaces(this, result) }
4246

4347
override string getAPrimaryQlClass() { result = "Namespace" }
48+
49+
/**
50+
* Get the fully qualified name of this namespace.
51+
*/
52+
string getFullName() {
53+
exists(string qualifier, string name |
54+
this.hasQualifiedName(qualifier, name) and
55+
result = printQualifiedName(qualifier, name)
56+
)
57+
}
4458
}
4559

4660
/** The global namespace. */

0 commit comments

Comments
 (0)