diff --git a/ql/src/codeql_ql/ast/Ast.qll b/ql/src/codeql_ql/ast/Ast.qll index a6061b60730..d4c1c465ec3 100644 --- a/ql/src/codeql_ql/ast/Ast.qll +++ b/ql/src/codeql_ql/ast/Ast.qll @@ -49,6 +49,7 @@ class AstNode extends TAstNode { /** * Gets the parent in the AST for this node. */ + cached AstNode getParent() { result.getAChild(_) = this } /** @@ -74,12 +75,14 @@ class AstNode extends TAstNode { predicate hasAnnotation(string name) { this.getAnAnnotation().getName() = name } /** Gets an annotation of this AST node. */ - Annotation getAnAnnotation() { toQL(this).getParent() = toQL(result).getParent() } + Annotation getAnAnnotation() { + toQL(this).getParent() = pragma[only_bind_out](toQL(result)).getParent() + } /** * Gets the predicate that contains this AST node. */ - pragma[noinline] + cached Predicate getEnclosingPredicate() { this = getANodeInPredicate(result) } } @@ -2231,6 +2234,8 @@ class Annotation extends TAnnotation, AstNode { /** Gets the node corresponding to the field `name`. */ string getName() { result = annot.getName().getValue() } + override AstNode getParent() { result = AstNode.super.getParent() } + override AstNode getAChild(string pred) { result = super.getAChild(pred) or diff --git a/ql/src/codeql_ql/ast/internal/AstNodes.qll b/ql/src/codeql_ql/ast/internal/AstNodes.qll index 1609eadb45e..c3d22be6346 100644 --- a/ql/src/codeql_ql/ast/internal/AstNodes.qll +++ b/ql/src/codeql_ql/ast/internal/AstNodes.qll @@ -126,6 +126,7 @@ private QL::AstNode toGenerateYAML(AST::AstNode n) { /** * Gets the underlying TreeSitter entity for a given AST node. */ +cached QL::AstNode toQL(AST::AstNode n) { result = toQLExpr(n) or