Rust: Add two more AST consistency checks

This commit is contained in:
Tom Hvitved
2025-01-08 14:16:46 +01:00
parent 033cd1778e
commit 0dccbb9349
2 changed files with 29 additions and 0 deletions

View File

@@ -52,6 +52,27 @@ query predicate multipleParents(Element child, string childClass, Element parent
parentClass = parent.getPrimaryQlClasses()
}
/** Holds if `parent` has multiple children at the same index. */
query predicate multipleChildren(Element parent, int index, Element child1, Element child2) {
child1 = getChildAndAccessor(parent, index, _) and
child2 = getChildAndAccessor(parent, index, _) and
child1 != child2
}
/**
* Holds if `child` has multiple positions amongst the `accessor` children
* of `parent`.
*
* Children are allowed to have multiple positions for _different_ accessors,
* for example in an array repeat expression `[1; 10]`, `1` has positions for
* both `getRepeatOperand()` and `getExpr()`.
*/
query predicate multiplePositions(Element parent, int pos1, int pos2, string accessor, Element child) {
child = getChildAndAccessor(parent, pos1, accessor) and
child = getChildAndAccessor(parent, pos2, accessor) and
pos1 != pos2
}
/**
* Gets counts of abstract syntax tree inconsistencies of each type.
*/
@@ -71,4 +92,10 @@ int getAstInconsistencyCounts(string type) {
or
type = "Multiple parents" and
result = count(Element e | multipleParents(e) | e)
or
type = "Multiple children" and
result = count(Element e | multipleChildren(_, _, e, _) | e)
or
type = "Multiple positions" and
result = count(Element e | multiplePositions(_, _, _, _, e) | e)
}

View File

@@ -1,5 +1,7 @@
| Multiple children | 0 |
| Multiple locations | 0 |
| Multiple parents | 0 |
| Multiple positions | 0 |
| Multiple primary QL classes | 0 |
| Multiple toStrings | 0 |
| No location | 0 |