From 67ab0fee837e4438b9ac986c66538b58b4ab3eaf Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Fri, 9 Jan 2026 19:23:19 +0000 Subject: [PATCH] C++: Add a few helper predicates. --- cpp/ql/lib/semmle/code/cpp/Element.qll | 5 +++++ cpp/ql/lib/semmle/code/cpp/exprs/Expr.qll | 2 +- cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll | 7 +++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cpp/ql/lib/semmle/code/cpp/Element.qll b/cpp/ql/lib/semmle/code/cpp/Element.qll index b30503d2c94..ffc0ede4daf 100644 --- a/cpp/ql/lib/semmle/code/cpp/Element.qll +++ b/cpp/ql/lib/semmle/code/cpp/Element.qll @@ -192,6 +192,11 @@ class Element extends ElementBase { */ predicate isAffectedByMacro() { affectedByMacro(this) } + /** Holds if this element is affected by the expansion of `mi`. */ + predicate isAffectedByMacro(MacroInvocation mi) { + affectedbymacroexpansion(underlyingElement(this), unresolveElement(mi)) + } + private Element getEnclosingElementPref() { enclosingfunction(underlyingElement(this), unresolveElement(result)) or result.(Function) = stmtEnclosingElement(this) or diff --git a/cpp/ql/lib/semmle/code/cpp/exprs/Expr.qll b/cpp/ql/lib/semmle/code/cpp/exprs/Expr.qll index 60e2635f338..851d987b1fd 100644 --- a/cpp/ql/lib/semmle/code/cpp/exprs/Expr.qll +++ b/cpp/ql/lib/semmle/code/cpp/exprs/Expr.qll @@ -23,7 +23,7 @@ class Expr extends StmtParent, @expr { predicate hasChild(Expr e, int n) { e = this.getChild(n) } /** Gets the enclosing function of this expression, if any. */ - Function getEnclosingFunction() { result = exprEnclosingElement(this) } + override Function getEnclosingFunction() { result = exprEnclosingElement(this) } /** Gets the nearest enclosing set of curly braces around this expression in the source, if any. */ BlockStmt getEnclosingBlock() { result = this.getEnclosingStmt().getEnclosingBlock() } diff --git a/cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll b/cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll index bd3a54c6159..cd750461244 100644 --- a/cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll +++ b/cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll @@ -20,7 +20,7 @@ class Stmt extends StmtParent, @stmt { predicate hasChild(Element e, int n) { this.getChild(n) = e } /** Gets the enclosing function of this statement, if any. */ - Function getEnclosingFunction() { result = stmtEnclosingElement(this) } + override Function getEnclosingFunction() { result = stmtEnclosingElement(this) } /** * Gets the nearest enclosing block of this statement in the source, if any. @@ -159,7 +159,10 @@ private class TStmtParent = @stmt or @expr; * * This is normally a statement, but may be a `StmtExpr`. */ -class StmtParent extends ControlFlowNode, TStmtParent { } +class StmtParent extends ControlFlowNode, TStmtParent { + /** Gets the enclosing function of this element, if any. */ + Function getEnclosingFunction() { none() } +} /** * A C/C++ 'expression' statement.