Skip to content

Commit 7d62cda

Browse files
authored
Merge pull request #17774 from geoffw0/astcount
Rust: Count number of AST inconsistencies
2 parents bee073d + feed0eb commit 7d62cda

File tree

9 files changed

+113
-35
lines changed

9 files changed

+113
-35
lines changed
Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
import rust
2-
import codeql.rust.elements.internal.generated.ParentChild
3-
4-
query predicate multipleToString(Element e, string s) {
5-
s = strictconcat(e.toString(), ",") and
6-
strictcount(e.toString()) > 1
7-
}
8-
9-
query predicate multipleLocations(Locatable e) { strictcount(e.getLocation()) > 1 }
10-
11-
query predicate multiplePrimaryQlClasses(Element e, string s) {
12-
s = e.getPrimaryQlClasses() and
13-
strictcount(e.getAPrimaryQlClass()) > 1
14-
}
15-
16-
private Element getParent(Element child) { child = getChildAndAccessor(result, _, _) }
17-
18-
query predicate multipleParents(Element child, Element parent) {
19-
parent = getParent(child) and
20-
strictcount(getParent(child)) > 1
21-
}
1+
/**
2+
* @name Abstract syntax tree inconsistencies
3+
* @description Lists the abstract syntax tree inconsistencies in the database. This query is intended for internal use.
4+
* @kind table
5+
* @id rust/diagnostics/ast-consistency
6+
*/
7+
8+
import codeql.rust.AstConsistency

rust/ql/consistency-queries/ExtractionConsistency.ql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* @name Extraction consistency
3+
* @description Lists the extraction inconsistencies (errors) in the database. This query is intended for internal use.
4+
* @kind table
5+
* @id rust/diagnostics/extraction-consistency
6+
*/
7+
18
import codeql.rust.Diagnostics
29

310
query predicate extractionError(ExtractionError ee) { any() }
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Provides classes for recognizing control flow graph inconsistencies.
3+
*/
4+
5+
private import rust
6+
private import codeql.rust.elements.internal.generated.ParentChild
7+
8+
/**
9+
* Holds if `e` has more than one `toString()` result.
10+
*/
11+
query predicate multipleToStrings(Element e, string s) {
12+
s = strictconcat(e.toString(), ", ") and
13+
strictcount(e.toString()) > 1
14+
}
15+
16+
/**
17+
* Holds if `e` has more than one `Location`.
18+
*/
19+
query predicate multipleLocations(Locatable e) { strictcount(e.getLocation()) > 1 }
20+
21+
/**
22+
* Holds if `e` has more than one `getPrimaryQlClasses()` result.
23+
*/
24+
query predicate multiplePrimaryQlClasses(Element e, string s) {
25+
s = strictconcat(e.getPrimaryQlClasses(), ", ") and
26+
strictcount(e.getAPrimaryQlClass()) > 1
27+
}
28+
29+
private Element getParent(Element child) { child = getChildAndAccessor(result, _, _) }
30+
31+
/**
32+
* Holds if `child` has more than one AST parent.
33+
*/
34+
query predicate multipleParents(Element child, Element parent) {
35+
parent = getParent(child) and
36+
strictcount(getParent(child)) > 1
37+
}
38+
39+
/**
40+
* Gets counts of abstract syntax tree inconsistencies of each type.
41+
*/
42+
int getAstInconsistencyCounts(string type) {
43+
// total results from all the AST consistency query predicates.
44+
type = "Multiple toStrings" and
45+
result = count(Element e | multipleToStrings(e, _) | e)
46+
or
47+
type = "Multiple locations" and
48+
result = count(Element e | multipleLocations(e) | e)
49+
or
50+
type = "Multiple primary QL classes" and
51+
result = count(Element e | multiplePrimaryQlClasses(e, _) | e)
52+
or
53+
type = "Multiple parents" and
54+
result = count(Element e | multipleParents(e, _) | e)
55+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Abstract syntax tree inconsistency counts
3+
* @description Counts the number of abstract syntax tree inconsistencies of each type. This query is intended for internal use.
4+
* @kind diagnostic
5+
* @id rust/diagnostics/ast-consistency-counts
6+
*/
7+
8+
import rust
9+
import codeql.rust.AstConsistency as Consistency
10+
11+
// see also `rust/diagnostics/ast-consistency`, which lists the
12+
// individual inconsistency results.
13+
from string type, int num
14+
where num = Consistency::getAstInconsistencyCounts(type)
15+
select type, num

rust/ql/src/queries/summary/Stats.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import rust
6+
import codeql.rust.AstConsistency as AstConsistency
67
private import codeql.rust.controlflow.internal.CfgConsistency as CfgConsistency
78

89
/**
@@ -17,6 +18,13 @@ int getLinesOfUserCode() {
1718
result = sum(File f | exists(f.getRelativePath()) | f.getNumberOfLinesOfCode())
1819
}
1920

21+
/**
22+
* Gets a count of the total number of abstract syntax tree inconsistencies in the database.
23+
*/
24+
int getTotalAstInconsistencies() {
25+
result = sum(string type | | AstConsistency::getAstInconsistencyCounts(type))
26+
}
27+
2028
/**
2129
* Gets a count of the total number of control flow graph inconsistencies in the database.
2230
*/
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @name Summary Statistics
33
* @description A table of summary statistics about a database.
4-
* @kind table
4+
* @kind metric
55
* @id rust/summary/summary-statistics
66
* @tags summary
77
*/
@@ -10,29 +10,29 @@ import rust
1010
import codeql.rust.Diagnostics
1111
import Stats
1212

13-
from string key, string value
13+
from string key, int value
1414
where
15-
key = "Elements extracted" and value = count(Element e | not e instanceof Unextracted).toString()
15+
key = "Elements extracted" and value = count(Element e | not e instanceof Unextracted)
1616
or
17-
key = "Elements unextracted" and value = count(Unextracted e).toString()
17+
key = "Elements unextracted" and value = count(Unextracted e)
1818
or
19-
key = "Extraction errors" and value = count(ExtractionError e).toString()
19+
key = "Extraction errors" and value = count(ExtractionError e)
2020
or
21-
key = "Extraction warnings" and value = count(ExtractionWarning w).toString()
21+
key = "Extraction warnings" and value = count(ExtractionWarning w)
2222
or
23-
key = "Files extracted - total" and value = count(File f | exists(f.getRelativePath())).toString()
23+
key = "Files extracted - total" and value = count(File f | exists(f.getRelativePath()))
2424
or
2525
key = "Files extracted - with errors" and
26-
value =
27-
count(File f | exists(f.getRelativePath()) and not f instanceof SuccessfullyExtractedFile)
28-
.toString()
26+
value = count(File f | exists(f.getRelativePath()) and not f instanceof SuccessfullyExtractedFile)
2927
or
3028
key = "Files extracted - without errors" and
31-
value = count(SuccessfullyExtractedFile f | exists(f.getRelativePath())).toString()
29+
value = count(SuccessfullyExtractedFile f | exists(f.getRelativePath()))
3230
or
33-
key = "Lines of code extracted" and value = getLinesOfCode().toString()
31+
key = "Lines of code extracted" and value = getLinesOfCode()
3432
or
35-
key = "Lines of user code extracted" and value = getLinesOfUserCode().toString()
33+
key = "Lines of user code extracted" and value = getLinesOfUserCode()
3634
or
37-
key = "Inconsistencies - CFG" and value = getTotalCfgInconsistencies().toString()
35+
key = "Inconsistencies - AST" and value = getTotalAstInconsistencies()
36+
or
37+
key = "Inconsistencies - CFG" and value = getTotalCfgInconsistencies()
3838
select key, value
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| Multiple locations | 0 |
2+
| Multiple parents | 0 |
3+
| Multiple primary QL classes | 0 |
4+
| Multiple toStrings | 0 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/diagnostics/AstConsistencyCounts.ql

rust/ql/test/query-tests/diagnostics/SummaryStats.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| Files extracted - total | 7 |
66
| Files extracted - with errors | 2 |
77
| Files extracted - without errors | 5 |
8+
| Inconsistencies - AST | 0 |
89
| Inconsistencies - CFG | 0 |
910
| Lines of code extracted | 59 |
1011
| Lines of user code extracted | 59 |

0 commit comments

Comments
 (0)