Skip to content

Commit 32219e5

Browse files
committed
Python: Add basic call-graph metric queries
For use with dist-compare
1 parent 1d5ef38 commit 32219e5

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

python/ql/src/meta/MetaMetrics.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Helpers to generating meta metrics, that is, metrics about the CodeQL analysis and extractor.
3+
*/
4+
5+
import python
6+
7+
private import semmle.python.filters.GeneratedCode
8+
private import semmle.python.filters.Tests
9+
10+
/**
11+
* Gets the root folder of the snapshot.
12+
*
13+
* This is selected as the location for project-wide metrics.
14+
*/
15+
Folder projectRoot() { result.getRelativePath() = "" }
16+
17+
/** A file we ignore because it is a test file, part of a third-part library, or compiled/generated/bundled code. */
18+
class IgnoredFile extends File {
19+
IgnoredFile() {
20+
any(TestScope ts).getLocation().getFile() = this
21+
or
22+
this instanceof GeneratedFile
23+
or
24+
// outside source root (inspired by `Scope.inSource`)
25+
not exists(this.getRelativePath())
26+
}
27+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Provides predicates for measuring the quality of the call graph, that is,
3+
* the number of calls that could be resolved to a callee.
4+
*/
5+
6+
import python
7+
import meta.MetaMetrics
8+
9+
/**
10+
* A call that is (possibly) relevant for analysis quality.
11+
* See `IgnoredFile` for details on what is excluded.
12+
*/
13+
class RelevantCall extends Call {
14+
RelevantCall() { not this.getLocation().getFile() instanceof IgnoredFile }
15+
}
16+
17+
/** Provides classes for call-graph resolution by using points-to */
18+
module PointsTo {
19+
/** A call that can be resolved by points-to. */
20+
class ResolvableCall extends RelevantCall {
21+
Value target;
22+
23+
ResolvableCall() { target.getACall() = this.getAFlowNode() }
24+
25+
/** Gets a resolved target of this call */
26+
Value getTarget() { result = target }
27+
}
28+
29+
/** A call that cannot be resolved by points-to. */
30+
class UnresolvableCall extends RelevantCall {
31+
UnresolvableCall() { not this instanceof ResolvableCall }
32+
}
33+
34+
/**
35+
* A call that can be resolved by points-to, where the resolved target is relevant.
36+
* Relevant targets include:
37+
* - builtins
38+
* - standard library
39+
* - source code of the project
40+
*/
41+
class ResolvableCallRelevantTarget extends ResolvableCall {
42+
ResolvableCallRelevantTarget() {
43+
target.isBuiltin()
44+
or
45+
exists(File file |
46+
file = target.(CallableValue).getScope().getLocation().getFile()
47+
or
48+
file = target.(ClassValue).getScope().getLocation().getFile()
49+
|
50+
file.inStdlib()
51+
or
52+
// part of the source code of the project
53+
exists(file.getRelativePath())
54+
)
55+
}
56+
}
57+
58+
/**
59+
* A call that can be resolved by points-to, where resolved target is not considered relevant.
60+
* See `ResolvableCallRelevantTarget` for definition of relevance.
61+
*/
62+
class ResolvableCallIrrelevantTarget extends ResolvableCall {
63+
ResolvableCallIrrelevantTarget() { not this instanceof ResolvableCallRelevantTarget }
64+
}
65+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Ratio of resolvable call by points-to
3+
* @description The percentage (relevant) calls that can be resolved to a target.
4+
* @kind metric
5+
* @metricType project
6+
* @metricAggregate sum min max avg
7+
* @tags meta
8+
* @id py/meta/points-to-resolvable-call-ratio
9+
*/
10+
11+
import python
12+
import CallGraphQuality
13+
14+
select projectRoot(), 100.0 * count(PointsTo::ResolvableCall call) / count(RelevantCall call).(float)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Resolvable calls by points-to
3+
* @description The number of (relevant) calls that could be resolved to its target.
4+
* @kind metric
5+
* @metricType project
6+
* @metricAggregate sum
7+
* @tags meta
8+
* @id py/meta/points-to-resolvable-calls
9+
*/
10+
11+
import python
12+
import CallGraphQuality
13+
14+
select projectRoot(), count(PointsTo::ResolvableCall call)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Resolvable calls by points-to, to relevant target
3+
* @description The number of (relevant) calls that could be resolved to its target that is relevant.
4+
* @kind metric
5+
* @metricType project
6+
* @metricAggregate sum
7+
* @tags meta
8+
* @id py/meta/points-to-resolvable-calls-relevant-target
9+
*/
10+
11+
import python
12+
import CallGraphQuality
13+
14+
select projectRoot(), count(PointsTo::ResolvableCallRelevantTarget call)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Resolvable call candidates
3+
* @description The number (relevant) calls in the program.
4+
* @kind metric
5+
* @metricType project
6+
* @metricAggregate sum
7+
* @tags meta
8+
* @id py/meta/resolvable-call-candidates
9+
*/
10+
11+
import python
12+
import CallGraphQuality
13+
14+
select projectRoot(), count(RelevantCall call)

0 commit comments

Comments
 (0)