Skip to content

Commit 60c205f

Browse files
authored
Merge pull request github#17818 from hvitved/rust/summary-stats-perf
Rust: Speedup `SummaryStats.ql`
2 parents 23a1ea7 + c4c936d commit 60c205f

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

rust/ql/lib/codeql/rust/AstConsistency.qll

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,41 @@
55
private import rust
66
private import codeql.rust.elements.internal.generated.ParentChild
77

8+
private predicate multipleToStrings(Element e) { strictcount(e.toString()) > 1 }
9+
810
/**
911
* Holds if `e` has more than one `toString()` result.
1012
*/
1113
query predicate multipleToStrings(Element e, string s) {
12-
s = strictconcat(e.toString(), ", ") and
13-
strictcount(e.toString()) > 1
14+
multipleToStrings(e) and
15+
s = strictconcat(e.toString(), ", ")
1416
}
1517

1618
/**
1719
* Holds if `e` has more than one `Location`.
1820
*/
1921
query predicate multipleLocations(Locatable e) { strictcount(e.getLocation()) > 1 }
2022

23+
private predicate multiplePrimaryQlClasses(Element e) { strictcount(e.getAPrimaryQlClass()) > 1 }
24+
2125
/**
2226
* Holds if `e` has more than one `getPrimaryQlClasses()` result.
2327
*/
2428
query predicate multiplePrimaryQlClasses(Element e, string s) {
25-
s = strictconcat(e.getPrimaryQlClasses(), ", ") and
26-
strictcount(e.getAPrimaryQlClass()) > 1
29+
multiplePrimaryQlClasses(e) and
30+
s = strictconcat(e.getPrimaryQlClasses(), ", ")
2731
}
2832

2933
private Element getParent(Element child) { child = getChildAndAccessor(result, _, _) }
3034

35+
private predicate multipleParents(Element child) { strictcount(getParent(child)) > 1 }
36+
3137
/**
3238
* Holds if `child` has more than one AST parent.
3339
*/
3440
query predicate multipleParents(Element child, Element parent) {
35-
parent = getParent(child) and
36-
strictcount(getParent(child)) > 1
41+
multipleParents(child) and
42+
parent = getParent(child)
3743
}
3844

3945
/**
@@ -42,14 +48,14 @@ query predicate multipleParents(Element child, Element parent) {
4248
int getAstInconsistencyCounts(string type) {
4349
// total results from all the AST consistency query predicates.
4450
type = "Multiple toStrings" and
45-
result = count(Element e | multipleToStrings(e, _) | e)
51+
result = count(Element e | multipleToStrings(e) | e)
4652
or
4753
type = "Multiple locations" and
4854
result = count(Element e | multipleLocations(e) | e)
4955
or
5056
type = "Multiple primary QL classes" and
51-
result = count(Element e | multiplePrimaryQlClasses(e, _) | e)
57+
result = count(Element e | multiplePrimaryQlClasses(e) | e)
5258
or
5359
type = "Multiple parents" and
54-
result = count(Element e | multipleParents(e, _) | e)
60+
result = count(Element e | multipleParents(e) | e)
5561
}

rust/ql/lib/codeql/rust/controlflow/internal/CfgConsistency.qll

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ private import codeql.rust.controlflow.ControlFlowGraph
99
private import codeql.rust.controlflow.internal.ControlFlowGraphImpl as CfgImpl
1010
private import codeql.rust.controlflow.internal.Completion
1111

12-
/**
13-
* All `Expr` nodes are `PostOrderTree`s
14-
*/
15-
query predicate nonPostOrderExpr(Expr e, string cls) {
16-
cls = e.getPrimaryQlClasses() and
12+
private predicate nonPostOrderExpr(Expr e) {
1713
not e instanceof LetExpr and
1814
not e instanceof ParenExpr and
1915
exists(AstNode last, Completion c |
@@ -23,6 +19,14 @@ query predicate nonPostOrderExpr(Expr e, string cls) {
2319
)
2420
}
2521

22+
/**
23+
* All `Expr` nodes are `PostOrderTree`s
24+
*/
25+
query predicate nonPostOrderExpr(Expr e, string cls) {
26+
nonPostOrderExpr(e) and
27+
cls = e.getPrimaryQlClasses()
28+
}
29+
2630
/**
2731
* Holds if CFG scope `scope` lacks an initial AST node. Overrides shared consistency predicate.
2832
*/
@@ -84,11 +88,11 @@ int getCfgInconsistencyCounts(string type) {
8488
result = count(CfgImpl::SplitKind sk | nonUniqueListOrder(sk, _) | sk)
8589
or
8690
type = "Multiple toStrings" and
87-
result = count(CfgNode n | multipleToString(n, _) | n)
91+
result = count(CfgNode n | multipleToString(n) | n)
8892
or
8993
type = "CFG scope lacks initial AST node" and
9094
result = count(CfgScope s | scopeNoFirst(s) | s)
9195
or
9296
type = "Non-PostOrderTree Expr node" and
93-
result = count(Expr e | nonPostOrderExpr(e, _) | e)
97+
result = count(Expr e | nonPostOrderExpr(e) | e)
9498
}

shared/controlflow/codeql/controlflow/Cfg.qll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,10 +1502,13 @@ module MakeWithSplitting<
15021502
strictcount(sk.getListOrder()) > 1
15031503
}
15041504

1505+
/** Holds if `n` has multiple textual representations. */
1506+
predicate multipleToString(Node n) { strictcount(n.toString()) > 1 }
1507+
15051508
/** Holds if `n` has multiple textual representations. */
15061509
query predicate multipleToString(Node n, string s) {
1507-
s = strictconcat(n.toString(), ",") and
1508-
strictcount(n.toString()) > 1
1510+
multipleToString(n) and
1511+
s = strictconcat(n.toString(), ",")
15091512
}
15101513

15111514
/** Holds if CFG scope `scope` lacks an initial AST node. */

0 commit comments

Comments
 (0)