|
| 1 | +import ql |
| 2 | +import codeql_ql.ast.internal.AstNodes |
| 3 | + |
| 4 | +private class TScope = |
| 5 | + TClass or TAggregate or TQuantifier or TSelect or TPredicate or TNewTypeBranch; |
| 6 | + |
| 7 | +/** A variable scope. */ |
| 8 | +class VariableScope extends TScope, AstNode { |
| 9 | + /** Gets the outer scope, if any. */ |
| 10 | + VariableScope getOuterScope() { result = scopeOf(this) } |
| 11 | + |
| 12 | + /** Gets a variable declared directly in this scope. */ |
| 13 | + VarDecl getADeclaration() { result.getParent() = this } |
| 14 | + |
| 15 | + /** Holds if this scope contains declaration `decl`, either directly or inherited. */ |
| 16 | + predicate contains(VarDecl decl) { |
| 17 | + decl = this.getADeclaration() |
| 18 | + or |
| 19 | + this.getOuterScope().contains(decl) and |
| 20 | + not this.getADeclaration().getName() = decl.getName() |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +private AstNode parent(AstNode child) { |
| 25 | + result = child.getParent() and |
| 26 | + not child instanceof VariableScope |
| 27 | +} |
| 28 | + |
| 29 | +VariableScope scopeOf(AstNode n) { result = parent*(n.getParent()) } |
| 30 | + |
| 31 | +predicate resolveVariable(Identifier i, VarDecl decl) { |
| 32 | + scopeOf(i).contains(decl) and |
| 33 | + decl.getName() = i.getName() |
| 34 | +} |
| 35 | + |
| 36 | +module VarConsistency { |
| 37 | + query predicate multipleVarDecls(VarAccess v, VarDecl decl) { |
| 38 | + decl = v.getDeclaration() and |
| 39 | + strictcount(v.getDeclaration()) > 1 |
| 40 | + } |
| 41 | +} |
0 commit comments