Skip to content

Commit ef50e57

Browse files
authored
Merge pull request github#11083 from michaelnebel/csharp/telemetry
C#: Telemetry query updates.
2 parents 313f600 + d580722 commit ef50e57

File tree

8 files changed

+58
-59
lines changed

8 files changed

+58
-59
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,8 @@ class RecordClass extends RecordType, Class {
824824
*/
825825
class AnonymousClass extends Class {
826826
AnonymousClass() { anonymous_types(this) }
827+
828+
override string getAPrimaryQlClass() { result = "AnonymousClass" }
827829
}
828830

829831
/**

csharp/ql/src/Telemetry/ExternalApi.qll

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ class TestLibrary extends RefType {
2828
* An external API from either the C# Standard Library or a 3rd party library.
2929
*/
3030
class ExternalApi extends DotNet::Callable {
31-
ExternalApi() { this.isUnboundDeclaration() and this.fromLibrary() }
31+
ExternalApi() {
32+
this.isUnboundDeclaration() and
33+
this.fromLibrary() and
34+
this.(Modifiable).isEffectivelyPublic()
35+
}
3236

3337
/**
3438
* Gets the unbound type, name and parameter types of this API.
3539
*/
40+
bindingset[this]
3641
private string getSignature() {
3742
result =
3843
this.getDeclaringType().getUnboundDeclaration() + "." + this.getName() + "(" +
@@ -42,41 +47,33 @@ class ExternalApi extends DotNet::Callable {
4247
/**
4348
* Gets the namespace of this API.
4449
*/
45-
private string getNamespace() { this.getDeclaringType().hasQualifiedName(result, _) }
46-
47-
/**
48-
* Gets the assembly file name containing this API.
49-
*/
50-
private string getAssembly() { result = this.getFile().getBaseName() }
50+
bindingset[this]
51+
string getNamespace() { this.getDeclaringType().hasQualifiedName(result, _) }
5152

5253
/**
53-
* Gets the assembly file name and namespace of this API.
54+
* Gets the namespace and signature of this API.
5455
*/
55-
string getInfoPrefix() { result = this.getAssembly() + "#" + this.getNamespace() }
56-
57-
/**
58-
* Gets the assembly file name, namespace and signature of this API.
59-
*/
60-
string getInfo() { result = this.getInfoPrefix() + "#" + this.getSignature() }
61-
62-
/** Gets a call to this API callable. */
63-
DispatchCall getACall() {
64-
this = result.getADynamicTarget().getUnboundDeclaration()
65-
or
66-
this = result.getAStaticTarget().getUnboundDeclaration()
67-
}
56+
bindingset[this]
57+
string getApiName() { result = this.getNamespace() + "#" + this.getSignature() }
6858

6959
/** Gets a node that is an input to a call to this API. */
7060
private ArgumentNode getAnInput() {
71-
result.getCall().(DataFlowDispatch::NonDelegateDataFlowCall).getDispatchCall() = this.getACall()
61+
result
62+
.getCall()
63+
.(DataFlowDispatch::NonDelegateDataFlowCall)
64+
.getATarget(_)
65+
.getUnboundDeclaration() = this
7266
}
7367

7468
/** Gets a node that is an output from a call to this API. */
7569
private DataFlow::Node getAnOutput() {
76-
exists(DataFlowDispatch::NonDelegateDataFlowCall call, DataFlowImplCommon::ReturnKindExt ret |
77-
result = ret.getAnOutNode(call)
70+
exists(
71+
Call c, DataFlowDispatch::NonDelegateDataFlowCall dc, DataFlowImplCommon::ReturnKindExt ret
72+
|
73+
dc.getDispatchCall().getCall() = c and
74+
c.getTarget().getUnboundDeclaration() = this
7875
|
79-
this.getACall() = call.getDispatchCall()
76+
result = ret.getAnOutNode(dc)
8077
)
8178
}
8279

@@ -125,30 +122,30 @@ signature predicate relevantApi(ExternalApi api);
125122
* for restricting the number or returned results based on a certain limit.
126123
*/
127124
module Results<relevantApi/1 getRelevantUsages> {
128-
private int getUsages(string apiInfo) {
125+
private int getUsages(string apiName) {
129126
result =
130-
strictcount(DispatchCall c, ExternalApi api |
131-
c = api.getACall() and
132-
apiInfo = api.getInfo() and
127+
strictcount(Call c, ExternalApi api |
128+
c.getTarget().getUnboundDeclaration() = api and
129+
apiName = api.getApiName() and
133130
getRelevantUsages(api)
134131
)
135132
}
136133

137-
private int getOrder(string apiInfo) {
138-
apiInfo =
139-
rank[result](string info, int usages |
140-
usages = getUsages(info)
134+
private int getOrder(string apiName) {
135+
apiName =
136+
rank[result](string name, int usages |
137+
usages = getUsages(name)
141138
|
142-
info order by usages desc, info
139+
name order by usages desc, name
143140
)
144141
}
145142

146143
/**
147-
* Holds if there exists an API with `apiInfo` that is being used `usages` times
144+
* Holds if there exists an API with `apiName` that is being used `usages` times
148145
* and if it is in the top results (guarded by resultLimit).
149146
*/
150-
predicate restrict(string apiInfo, int usages) {
151-
usages = getUsages(apiInfo) and
152-
getOrder(apiInfo) <= resultLimit()
147+
predicate restrict(string apiName, int usages) {
148+
usages = getUsages(apiName) and
149+
getOrder(apiName) <= resultLimit()
153150
}
154151
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name External libraries
3-
* @description A list of external libraries used in the code
3+
* @description A list of external libraries used in the code given by their namespace.
44
* @kind metric
55
* @tags summary telemetry
66
* @id csharp/telemetry/external-libs
@@ -10,23 +10,23 @@ private import csharp
1010
private import semmle.code.csharp.dispatch.Dispatch
1111
private import ExternalApi
1212

13-
private predicate getRelevantUsages(string info, int usages) {
13+
private predicate getRelevantUsages(string namespace, int usages) {
1414
usages =
15-
strictcount(DispatchCall c, ExternalApi api |
16-
c = api.getACall() and
17-
api.getInfoPrefix() = info and
15+
strictcount(Call c, ExternalApi api |
16+
c.getTarget().getUnboundDeclaration() = api and
17+
api.getNamespace() = namespace and
1818
not api.isUninteresting()
1919
)
2020
}
2121

22-
private int getOrder(string info) {
23-
info =
22+
private int getOrder(string namespace) {
23+
namespace =
2424
rank[result](string i, int usages | getRelevantUsages(i, usages) | i order by usages desc, i)
2525
}
2626

27-
from ExternalApi api, string info, int usages
27+
from ExternalApi api, string namespace, int usages
2828
where
29-
info = api.getInfoPrefix() and
30-
getRelevantUsages(info, usages) and
31-
getOrder(info) <= resultLimit()
32-
select info, usages order by usages desc
29+
namespace = api.getNamespace() and
30+
getRelevantUsages(namespace, usages) and
31+
getOrder(namespace) <= resultLimit()
32+
select namespace, usages order by usages desc

csharp/ql/src/meta/frameworks/UnsupportedExternalAPIs.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ private import semmle.code.csharp.dispatch.Dispatch
1313
private import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
1414
private import Telemetry.ExternalApi
1515

16-
from DispatchCall c, ExternalApi api
16+
from Call c, ExternalApi api
1717
where
18-
c = api.getACall() and
18+
c.getTarget().getUnboundDeclaration() = api and
1919
not api.isUninteresting() and
2020
not api.isSupported() and
2121
not api instanceof FlowSummaryImpl::Public::NegativeSummarizedCallable
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| System.Private.CoreLib.dll#System | 5 |
2-
| System.Private.CoreLib.dll#System.Collections.Generic | 2 |
1+
| System | 5 |
2+
| System.Collections.Generic | 2 |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| System.Private.CoreLib.dll#System.Collections.Generic#List<>.Add(T) | 2 |
1+
| System.Collections.Generic#List<>.Add(T) | 2 |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| System.Web.cs#System.Web#HttpResponse.Write(System.Object) | 2 |
2-
| System.Web.cs#System.Web#HttpResponse.WriteFile(System.String) | 1 |
1+
| System.Web#HttpResponse.Write(System.Object) | 2 |
2+
| System.Web#HttpResponse.WriteFile(System.String) | 1 |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| System.Console.dll#System#Console.ReadLine() | 2 |
2-
| System.Console.dll#System#Console.Read() | 1 |
1+
| System#Console.ReadLine() | 2 |
2+
| System#Console.Read() | 1 |

0 commit comments

Comments
 (0)