-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17774 from geoffw0/astcount
Rust: Count number of AST inconsistencies
- Loading branch information
Showing
9 changed files
with
113 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,8 @@ | ||
import rust | ||
import codeql.rust.elements.internal.generated.ParentChild | ||
|
||
query predicate multipleToString(Element e, string s) { | ||
s = strictconcat(e.toString(), ",") and | ||
strictcount(e.toString()) > 1 | ||
} | ||
|
||
query predicate multipleLocations(Locatable e) { strictcount(e.getLocation()) > 1 } | ||
|
||
query predicate multiplePrimaryQlClasses(Element e, string s) { | ||
s = e.getPrimaryQlClasses() and | ||
strictcount(e.getAPrimaryQlClass()) > 1 | ||
} | ||
|
||
private Element getParent(Element child) { child = getChildAndAccessor(result, _, _) } | ||
|
||
query predicate multipleParents(Element child, Element parent) { | ||
parent = getParent(child) and | ||
strictcount(getParent(child)) > 1 | ||
} | ||
/** | ||
* @name Abstract syntax tree inconsistencies | ||
* @description Lists the abstract syntax tree inconsistencies in the database. This query is intended for internal use. | ||
* @kind table | ||
* @id rust/diagnostics/ast-consistency | ||
*/ | ||
|
||
import codeql.rust.AstConsistency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Provides classes for recognizing control flow graph inconsistencies. | ||
*/ | ||
|
||
private import rust | ||
private import codeql.rust.elements.internal.generated.ParentChild | ||
|
||
/** | ||
* Holds if `e` has more than one `toString()` result. | ||
*/ | ||
query predicate multipleToStrings(Element e, string s) { | ||
s = strictconcat(e.toString(), ", ") and | ||
strictcount(e.toString()) > 1 | ||
} | ||
|
||
/** | ||
* Holds if `e` has more than one `Location`. | ||
*/ | ||
query predicate multipleLocations(Locatable e) { strictcount(e.getLocation()) > 1 } | ||
|
||
/** | ||
* Holds if `e` has more than one `getPrimaryQlClasses()` result. | ||
*/ | ||
query predicate multiplePrimaryQlClasses(Element e, string s) { | ||
s = strictconcat(e.getPrimaryQlClasses(), ", ") and | ||
strictcount(e.getAPrimaryQlClass()) > 1 | ||
} | ||
|
||
private Element getParent(Element child) { child = getChildAndAccessor(result, _, _) } | ||
|
||
/** | ||
* Holds if `child` has more than one AST parent. | ||
*/ | ||
query predicate multipleParents(Element child, Element parent) { | ||
parent = getParent(child) and | ||
strictcount(getParent(child)) > 1 | ||
} | ||
|
||
/** | ||
* Gets counts of abstract syntax tree inconsistencies of each type. | ||
*/ | ||
int getAstInconsistencyCounts(string type) { | ||
// total results from all the AST consistency query predicates. | ||
type = "Multiple toStrings" and | ||
result = count(Element e | multipleToStrings(e, _) | e) | ||
or | ||
type = "Multiple locations" and | ||
result = count(Element e | multipleLocations(e) | e) | ||
or | ||
type = "Multiple primary QL classes" and | ||
result = count(Element e | multiplePrimaryQlClasses(e, _) | e) | ||
or | ||
type = "Multiple parents" and | ||
result = count(Element e | multipleParents(e, _) | e) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @name Abstract syntax tree inconsistency counts | ||
* @description Counts the number of abstract syntax tree inconsistencies of each type. This query is intended for internal use. | ||
* @kind diagnostic | ||
* @id rust/diagnostics/ast-consistency-counts | ||
*/ | ||
|
||
import rust | ||
import codeql.rust.AstConsistency as Consistency | ||
|
||
// see also `rust/diagnostics/ast-consistency`, which lists the | ||
// individual inconsistency results. | ||
from string type, int num | ||
where num = Consistency::getAstInconsistencyCounts(type) | ||
select type, num |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
rust/ql/test/query-tests/diagnostics/AstConsistencyCounts.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
| Multiple locations | 0 | | ||
| Multiple parents | 0 | | ||
| Multiple primary QL classes | 0 | | ||
| Multiple toStrings | 0 | |
1 change: 1 addition & 0 deletions
1
rust/ql/test/query-tests/diagnostics/AstConsistencyCounts.qlref
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
queries/diagnostics/AstConsistencyCounts.ql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters