[clang] [Clang] Fix a number of issues involving expansion statements (PR #217110)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 21 04:50:24 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());
+  }
----------------
cor3ntin wrote:

If you want to be super pedantic, `getEnclosingNonExpansionStatementContextAsFunctionDecl` - but i tyhink either are better than status quo

https://github.com/llvm/llvm-project/pull/217110


More information about the cfe-commits mailing list