|
| 1 | +/** Provides a set of checks that the AST is actually a tree. */ |
| 2 | + |
| 3 | +private import codeql.swift.printast.PrintAstNode |
| 4 | + |
| 5 | +/** Checks that no child has more than one parent. */ |
| 6 | +query predicate doubleParents( |
| 7 | + PrintAstNode parent1, string label1, PrintAstNode parent2, string label2, PrintAstNode child |
| 8 | +) { |
| 9 | + parent1 != parent2 and |
| 10 | + parent1.hasChild(child, _, label1) and |
| 11 | + parent2.hasChild(child, _, label2) |
| 12 | +} |
| 13 | + |
| 14 | +/** Checks that no two children share the same index. */ |
| 15 | +query predicate doubleChildren( |
| 16 | + PrintAstNode parent, int index, string label1, PrintAstNode child1, string label2, |
| 17 | + PrintAstNode child2 |
| 18 | +) { |
| 19 | + child1 != child2 and |
| 20 | + parent.hasChild(child1, index, label1) and |
| 21 | + parent.hasChild(child2, index, label2) |
| 22 | +} |
| 23 | + |
| 24 | +/** Checks that no child is under different indexes. */ |
| 25 | +query predicate doubleIndexes( |
| 26 | + PrintAstNode parent, int index1, string label1, int index2, string label2, PrintAstNode child |
| 27 | +) { |
| 28 | + index1 != index2 and |
| 29 | + parent.hasChild(child, index1, label1) and |
| 30 | + parent.hasChild(child, index2, label2) |
| 31 | +} |
| 32 | + |
| 33 | +private predicate isChildOf(PrintAstNode parent, PrintAstNode child) { |
| 34 | + parent.hasChild(child, _, _) |
| 35 | +} |
| 36 | + |
| 37 | +/** Checks that there is no back edge. */ |
| 38 | +query predicate parentChildLoops(PrintAstNode parent, PrintAstNode child) { |
| 39 | + isChildOf(parent, child) and isChildOf*(child, parent) |
| 40 | +} |
0 commit comments