Skip to content

Commit ea504cd

Browse files
committed
C#: Use correct names for generic types/methods in model editor queries
1 parent 79f5a6a commit ea504cd

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

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.getMethodName(),
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.getMethodName(),
1818
endpoint.getParameterTypes(), supported, endpoint.getFile().getBaseName(), type

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** Provides classes and predicates related to handling APIs for the VS Code extension. */
22

33
private import csharp
4+
private import semmle.code.csharp.commons.QualifiedName
45
private import semmle.code.csharp.dataflow.FlowSummary
56
private import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
67
private import semmle.code.csharp.dataflow.internal.ExternalFlow
@@ -34,7 +35,17 @@ class Endpoint extends Callable {
3435
* Gets the unbound type name of this endpoint.
3536
*/
3637
bindingset[this]
37-
string getTypeName() { result = nestedName(this.getDeclaringType().getUnboundDeclaration()) }
38+
string getTypeName() {
39+
result = qualifiedTypeName(this.getNamespace(), this.getDeclaringType().getUnboundDeclaration())
40+
}
41+
42+
/**
43+
* Gets the qualified method name of this endpoint.
44+
*/
45+
bindingset[this]
46+
string getMethodName() {
47+
result = qualifiedCallableName(this.getNamespace(), this.getTypeName(), this)
48+
}
3849

3950
/**
4051
* Gets the parameter types of this endpoint.
@@ -107,15 +118,25 @@ string methodClassification(Call method) {
107118
}
108119

109120
/**
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.
121+
* Gets the fully qualified name of the type `t`.
115122
*/
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
123+
private string qualifiedTypeName(string namespace, Type t) {
124+
exists(string type | QN::hasQualifiedName(t, namespace, type) | result = type)
121125
}
126+
127+
/**
128+
* Gets the fully qualified name of the callable `c`.
129+
*/
130+
private string qualifiedCallableName(string namespace, string type, Callable c) {
131+
exists(string name | QN::hasQualifiedName(c, namespace, type, name) | result = name)
132+
}
133+
134+
private module QualifiedNameInput implements QualifiedNameInputSig {
135+
string getUnboundGenericSuffix(UnboundGeneric ug) {
136+
result =
137+
"<" + strictconcat(int i, string s | s = ug.getTypeParameter(i).getName() | s, "," order by i)
138+
+ ">"
139+
}
140+
}
141+
142+
private module QN = QualifiedName<QualifiedNameInput>;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +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-
| PublicGenericClass.cs:17:18:17:36 | summaryStuff`1 | GitHub.CodeQL | PublicGenericClass`2 | summaryStuff`1 | (TNode) | true | PublicGenericClass.cs | summary |
13-
| PublicGenericInterface.cs:7:10:7:14 | stuff | GitHub.CodeQL | PublicGenericInterface`1 | stuff | (T) | false | PublicGenericInterface.cs | |
14-
| PublicGenericInterface.cs:9:10:9:19 | stuff2`1 | GitHub.CodeQL | PublicGenericInterface`1 | stuff2`1 | (T2) | false | PublicGenericInterface.cs | |
15-
| 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 | |
1616
| PublicInterface.cs:7:10:7:14 | stuff | GitHub.CodeQL | PublicInterface | stuff | (System.String) | false | PublicInterface.cs | |
1717
| PublicInterface.cs:9:29:9:31 | get_PublicProperty | GitHub.CodeQL | PublicInterface | get_PublicProperty | () | false | PublicInterface.cs | |
1818
| PublicInterface.cs:9:34:9:36 | set_PublicProperty | GitHub.CodeQL | PublicInterface | set_PublicProperty | (System.String) | false | PublicInterface.cs | |

0 commit comments

Comments
 (0)