Skip to content

Commit 7c141b9

Browse files
authored
Merge pull request #15089 from github/koesie10/csharp-model-editor-generics
C#: Fix names of generic types/methods in model editor queries
2 parents c8b4a21 + 96feb2c commit 7c141b9

File tree

7 files changed

+50
-20
lines changed

7 files changed

+50
-20
lines changed

csharp/ql/lib/semmle/code/csharp/dataflow/internal/ExternalFlow.qll

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ private predicate elementSpec(
284284
UnboundValueOrRefType t
285285
) {
286286
elementSpec(namespace, type, subtypes, name, signature, ext) and
287-
QN::hasQualifiedName(t, namespace, type)
287+
hasQualifiedTypeName(t, namespace, type)
288288
}
289289

290290
private class UnboundValueOrRefType extends ValueOrRefType {
@@ -352,7 +352,7 @@ Declaration interpretBaseDeclaration(string namespace, string type, string name,
352352
exists(UnboundValueOrRefType t | elementSpec(namespace, type, _, name, signature, _, t) |
353353
result =
354354
any(Declaration d |
355-
QN::hasQualifiedName(d, namespace, type, name) and
355+
hasQualifiedMethodName(d, namespace, type, name) and
356356
(
357357
signature = ""
358358
or
@@ -458,6 +458,19 @@ private module QualifiedNameInput implements QualifiedNameInputSig {
458458

459459
private module QN = QualifiedName<QualifiedNameInput>;
460460

461+
/** Holds if declaration `d` has the qualified name `qualifier`.`name`. */
462+
predicate hasQualifiedTypeName(Type t, string namespace, string type) {
463+
QN::hasQualifiedName(t, namespace, type)
464+
}
465+
466+
/**
467+
* Holds if declaration `d` has name `name` and is defined in type `type`
468+
* with namespace `namespace`.
469+
*/
470+
predicate hasQualifiedMethodName(Declaration d, string namespace, string type, string name) {
471+
QN::hasQualifiedName(d, namespace, type, name)
472+
}
473+
461474
pragma[nomagic]
462475
private string parameterQualifiedType(Parameter p) {
463476
exists(string qualifier, string name |

csharp/ql/src/utils/modeleditor/ApplicationModeEndpoints.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ where
1818
usage = aUsage(endpoint) and
1919
type = supportedType(endpoint) and
2020
classification = methodClassification(usage)
21-
select usage, endpoint.getNamespace(), endpoint.getTypeName(), endpoint.getName(),
21+
select usage, endpoint.getNamespace(), endpoint.getTypeName(), endpoint.getEndpointName(),
2222
endpoint.getParameterTypes(), supported, endpoint.dllName(), endpoint.dllVersion(), type,
2323
classification

csharp/ql/src/utils/modeleditor/FrameworkModeEndpoints.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ from PublicEndpointFromSource endpoint, boolean supported, string type
1414
where
1515
supported = isSupported(endpoint) and
1616
type = supportedType(endpoint)
17-
select endpoint, endpoint.getNamespace(), endpoint.getTypeName(), endpoint.getName(),
17+
select endpoint, endpoint.getNamespace(), endpoint.getTypeName(), endpoint.getEndpointName(),
1818
endpoint.getParameterTypes(), supported, endpoint.getFile().getBaseName(), type

csharp/ql/src/utils/modeleditor/ModelEditor.qll

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ class Endpoint extends Callable {
3434
* Gets the unbound type name of this endpoint.
3535
*/
3636
bindingset[this]
37-
string getTypeName() { result = nestedName(this.getDeclaringType().getUnboundDeclaration()) }
37+
string getTypeName() {
38+
result = qualifiedTypeName(this.getNamespace(), this.getDeclaringType().getUnboundDeclaration())
39+
}
40+
41+
/**
42+
* Gets the qualified name of this endpoint.
43+
*/
44+
bindingset[this]
45+
string getEndpointName() {
46+
result = qualifiedCallableName(this.getNamespace(), this.getTypeName(), this)
47+
}
3848

3949
/**
4050
* Gets the parameter types of this endpoint.
@@ -107,15 +117,15 @@ string methodClassification(Call method) {
107117
}
108118

109119
/**
110-
* Gets the nested name of the type `t`.
111-
*
112-
* If the type is not a nested type, the result is the same as `getName()`.
113-
* Otherwise the name of the nested type is prefixed with a `+` and appended to
114-
* the name of the enclosing type, which might be a nested type as well.
120+
* Gets the fully qualified name of the type `t`.
115121
*/
116-
private string nestedName(Type t) {
117-
not exists(t.getDeclaringType().getUnboundDeclaration()) and
118-
result = t.getName()
119-
or
120-
nestedName(t.getDeclaringType().getUnboundDeclaration()) + "+" + t.getName() = result
122+
private string qualifiedTypeName(string namespace, Type t) {
123+
exists(string type | hasQualifiedTypeName(t, namespace, type) | result = type)
124+
}
125+
126+
/**
127+
* Gets the fully qualified name of the callable `c`.
128+
*/
129+
private string qualifiedCallableName(string namespace, string type, Callable c) {
130+
exists(string name | hasQualifiedMethodName(c, namespace, type, name) | result = name)
121131
}

csharp/ql/test/utils/modeleditor/FrameworkModeEndpoints.expected

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
| PublicClass.cs:40:19:40:29 | sourceStuff | GitHub.CodeQL | PublicClass | sourceStuff | () | true | PublicClass.cs | source |
88
| PublicClass.cs:45:17:45:25 | sinkStuff | GitHub.CodeQL | PublicClass | sinkStuff | (System.String) | true | PublicClass.cs | sink |
99
| PublicClass.cs:50:17:50:28 | neutralStuff | GitHub.CodeQL | PublicClass | neutralStuff | (System.String) | true | PublicClass.cs | neutral |
10-
| PublicGenericClass.cs:7:17:7:21 | stuff | GitHub.CodeQL | PublicGenericClass`2 | stuff | (T) | false | PublicGenericClass.cs | |
11-
| PublicGenericClass.cs:12:17:12:26 | stuff2`1 | GitHub.CodeQL | PublicGenericClass`2 | stuff2`1 | (T2) | false | PublicGenericClass.cs | |
12-
| PublicGenericInterface.cs:7:10:7:14 | stuff | GitHub.CodeQL | PublicGenericInterface`1 | stuff | (T) | false | PublicGenericInterface.cs | |
13-
| PublicGenericInterface.cs:9:10:9:19 | stuff2`1 | GitHub.CodeQL | PublicGenericInterface`1 | stuff2`1 | (T2) | false | PublicGenericInterface.cs | |
14-
| PublicGenericInterface.cs:11:17:11:27 | staticStuff | GitHub.CodeQL | PublicGenericInterface`1 | staticStuff | (System.String) | false | PublicGenericInterface.cs | |
10+
| PublicGenericClass.cs:7:17:7:21 | stuff | GitHub.CodeQL | PublicGenericClass<T,T2> | stuff | (T) | false | PublicGenericClass.cs | |
11+
| PublicGenericClass.cs:12:17:12:26 | stuff2`1 | GitHub.CodeQL | PublicGenericClass<T,T2> | stuff2<T2> | (T2) | false | PublicGenericClass.cs | |
12+
| PublicGenericClass.cs:17:18:17:36 | summaryStuff`1 | GitHub.CodeQL | PublicGenericClass<T,T2> | summaryStuff<TNode> | (TNode) | true | PublicGenericClass.cs | summary |
13+
| PublicGenericInterface.cs:7:10:7:14 | stuff | GitHub.CodeQL | PublicGenericInterface<T> | stuff | (T) | false | PublicGenericInterface.cs | |
14+
| PublicGenericInterface.cs:9:10:9:19 | stuff2`1 | GitHub.CodeQL | PublicGenericInterface<T> | stuff2<T2> | (T2) | false | PublicGenericInterface.cs | |
15+
| PublicGenericInterface.cs:11:17:11:27 | staticStuff | GitHub.CodeQL | PublicGenericInterface<T> | staticStuff | (System.String) | false | PublicGenericInterface.cs | |
1516
| PublicInterface.cs:7:10:7:14 | stuff | GitHub.CodeQL | PublicInterface | stuff | (System.String) | false | PublicInterface.cs | |
1617
| PublicInterface.cs:9:29:9:31 | get_PublicProperty | GitHub.CodeQL | PublicInterface | get_PublicProperty | () | false | PublicInterface.cs | |
1718
| PublicInterface.cs:9:34:9:36 | set_PublicProperty | GitHub.CodeQL | PublicInterface | set_PublicProperty | (System.String) | false | PublicInterface.cs | |

csharp/ql/test/utils/modeleditor/FrameworkModeEndpoints.ext.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extensions:
1616
extensible: summaryModel
1717
data:
1818
- ["GitHub.CodeQL","PublicClass",true,"summaryStuff","(System.String)","","Argument[0]","ReturnValue","taint","manual"]
19+
- ["GitHub.CodeQL","PublicGenericClass<T,T2>",true,"summaryStuff<TNode>","(TNode)","","Argument[0]","ReturnValue","value","manual"]
1920

2021
- addsTo:
2122
pack: codeql/csharp-all

csharp/ql/test/utils/modeleditor/PublicGenericClass.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ public void stuff2<T2>(T2 arg)
1313
{
1414
Console.WriteLine(arg);
1515
}
16+
17+
public TNode summaryStuff<TNode>(TNode arg)
18+
{
19+
return arg;
20+
}
1621
}

0 commit comments

Comments
 (0)