unified: Add meta and diagmetric queries for static name binding · microsoft/codeql@360aec3 · GitHub
Skip to content

Commit 360aec3

Browse files
committed
unified: Add meta and diagmetric queries for static name binding
1 parent c1b19fb commit 360aec3

5 files changed

Lines changed: 157 additions & 0 deletions

File tree

‎shared/util/codeql/util/ReportStats.qll‎

Lines changed: 37 additions & 0 deletions
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
private import unified
2+
private import codeql.util.ReportStats
3+
private import codeql.unified.internal.StaticNameBinding
4+
private import codeql.unified.internal.LocalNameBinding
5+
private import codeql.unified.internal.NameBindingPlugin
6+
7+
/** Stats about identifiers that static name binding could resolve. */
8+
module StaticNameResolutionStats implements EntityStatsSig {
9+
class Candidate extends Identifier {
10+
Candidate() {
11+
this = getIdentifierFromRef(_) and
12+
not this instanceof NameDeclaration
13+
// TODO: exclude names we know are not static references, e.g. unqualified instance-field access,
14+
// currently blocked on getting static name binding to report this information.
15+
}
16+
17+
NameBindingNode getTarget() {
18+
(
19+
exists(NameDeclaration decl |
20+
result.isIdentifier(decl) and
21+
trackNameDeclaration(decl).isIdentifier(this)
22+
)
23+
or
24+
result.isModuleScopeNode(_) and
25+
result.(NamespaceNode).ref().isIdentifier(this)
26+
) and
27+
// Do not consider a type extension to be a valid target
28+
// TODO: Fix in the AST mapping: type extensions should reference their type, not declare it
29+
not exists(ClassLikeDeclaration cls |
30+
cls.hasModifier("extension") and
31+
result.isIdentifier(cls.getName())
32+
)
33+
}
34+
35+
predicate isOk() { exists(this.getTarget()) }
36+
}
37+
38+
string getOkText() { result = "statically resolvable names" }
39+
40+
string getNotOkText() { result = "statically unresolvable names" }
41+
}
42+
43+
module StaticNameResolutionStatsReport = EntityReportStats<StaticNameResolutionStats>;
44+
45+
/** Stats about which files are covered by a module manifest. */
46+
module FilesCoveredByModuleManifestStats implements EntityStatsSig {
47+
class Candidate extends File {
48+
Candidate() { this.getExtension() = "swift" }
49+
50+
ModuleScopeRepr getAModule() { result.getAnIncludedFile() = this }
51+
52+
predicate isOk() { exists(this.getAModule()) }
53+
}
54+
55+
string getOkText() { result = "files covered by a module manifest" }
56+
57+
string getNotOkText() { result = "files not covered by any module manifest" }
58+
}
59+
60+
module FilesCoveredByModuleManifestStatsReport =
61+
EntityReportStats<FilesCoveredByModuleManifestStats>;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @name Unified extractor/analysis information
3+
* @description Information about the extraction and analysis for a database
4+
* @kind metric
5+
* @tags summary telemetry
6+
* @id unified/telemetry/extraction-information
7+
*/
8+
9+
private import unified
10+
private import codeql.unified.internal.AnalysisQuality
11+
12+
from string key, float value
13+
where
14+
(
15+
StaticNameResolutionStatsReport::keyValuePair(key, value) or
16+
FilesCoveredByModuleManifestStatsReport::keyValuePair(key, value)
17+
) and
18+
/* Infinity */
19+
value != 1.0 / 0.0 and
20+
/* -Infinity */
21+
value != -1.0 / 0.0 and
22+
/* NaN */
23+
value != 0.0 / 0.0
24+
select key, value
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @name Files covered by module manifest
3+
* @description Files that are included from a module manifest
4+
* @kind problem
5+
* @problem.severity recommendation
6+
* @id unified/diagnostic/files-covered-by-module-manifest
7+
* @tags meta
8+
* @precision very-low
9+
*/
10+
11+
import unified
12+
import codeql.unified.internal.StaticNameBinding
13+
import codeql.unified.internal.NameBindingPlugin
14+
import codeql.unified.internal.AnalysisQuality
15+
16+
from FilesCoveredByModuleManifestStats::Candidate c, ModuleScopeRepr mod
17+
where c.isOk() and mod = c.getAModule()
18+
select c, "File included in $@", mod, mod.toString()
Lines changed: 17 additions & 0 deletions

0 commit comments

Comments
 (0)