[clang] [Clang] Fix a number of issues involving expansion statements (PR #217110)
Ambrose Leeb via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 20 10:33:06 PDT 2026
================
@@ -2188,6 +2188,30 @@ class DeclContext {
}
}
+ /// Test whether we're directly inside a function or method, but ignoring
+ /// any intervening expansion statements.
+ bool isInsideFunctionOrMethod() const {
+ return getEnclosingNonExpansionStatementContext()->isFunctionOrMethod();
+ }
+
+ /// Cast this to a FunctionDecl if it is one, ignoring any intervening
+ /// expansion statements. Returns nullptr if this is not a function.
+ FunctionDecl *getAsFunctionDecl() {
+ return dyn_cast<FunctionDecl>(getEnclosingNonExpansionStatementContext());
+ }
+
+ const FunctionDecl *getAsFunctionDecl() const {
+ return dyn_cast<FunctionDecl>(getEnclosingNonExpansionStatementContext());
+ }
+
+ FunctionDecl *castAsFunctionDecl() {
+ return cast<FunctionDecl>(getEnclosingNonExpansionStatementContext());
+ }
+
+ const FunctionDecl *castAsFunctionDecl() const {
+ return cast<FunctionDecl>(getEnclosingNonExpansionStatementContext());
+ }
----------------
Sirraide wrote:
See it’s hard to come up w/ a good name for these imo: I considered `getEnclosingFuctionDecl()`, but the issue with that is that it doesn’t always do that (e.g. if we’re inside a local class, we wouldn’t want this to return the function containing the local class since this helper is used to determine whether we’re currently directly inside a function, which we’re not if we’re in a class).
`getEnclosingContextAsFunctionDecl()` would also be somewhat incorrect since there may be multiple nested expansion statement context, in which case the immediately enclosing context would still be an expansion statement, not a function, but if you think this still clearer than `getAsFunctionDecl()` then I’d be fine w/ switching to this
https://github.com/llvm/llvm-project/pull/217110
More information about the cfe-commits
mailing list