[clang] [Clang] Move DeclContext::getEnclosingFunction/castEnclosingFunction out of line (PR #224711)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 18 12:10:09 PDT 2026
https://github.com/DeanSturtevant1 created https://github.com/llvm/llvm-project/pull/224711
In DeclBase.h, clang::FunctionDecl is only forward-declared. Commit c9074cfd0d95 defined getEnclosingFunction() and castEnclosingFunction() inline in DeclBase.h, which instantiates dyn_cast<FunctionDecl> and cast<FunctionDecl> on an incomplete type when DeclBase.h is compiled standalone (e.g. with -fmodules / header units).
Move the non-const definitions to DeclBase.cpp where FunctionDecl is complete, and delegate the const overloads via const_cast, matching getOuterLexicalRecordContext() and getEnclosingNonExpansionStatementContext().
>From a425d783256c39ac70fcfb3bc2315df6343491ad Mon Sep 17 00:00:00 2001
From: Dean Sturtevant <dsturtevant at google.com>
Date: Fri, 18 Sep 2026 15:04:02 -0400
Subject: [PATCH] [Clang] Move
DeclContext::getEnclosingFunction/castEnclosingFunction out of line
In DeclBase.h, clang::FunctionDecl is only forward-declared. Commit c9074cfd0d95 defined getEnclosingFunction() and castEnclosingFunction() inline in DeclBase.h, which instantiates dyn_cast<FunctionDecl> and cast<FunctionDecl> on an incomplete type when DeclBase.h is compiled standalone (e.g. with -fmodules / header units).
Move the non-const definitions to DeclBase.cpp where FunctionDecl is complete, and delegate the const overloads via const_cast, matching getOuterLexicalRecordContext() and getEnclosingNonExpansionStatementContext().
---
clang/include/clang/AST/DeclBase.h | 14 ++++----------
clang/lib/AST/DeclBase.cpp | 8 ++++++++
2 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index a067d87e92a84..8ea2533825dd9 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -2198,20 +2198,14 @@ class DeclContext {
/// In particular, this will return nullptr if the *nearest* enclosing
/// DeclContext that is not an expansion statement is something other
/// than a function (e.g. a CXXRecordDecl, even if it is a local class).
- FunctionDecl *getEnclosingFunction() {
- return dyn_cast<FunctionDecl>(getEnclosingNonExpansionStatementContext());
- }
-
+ FunctionDecl *getEnclosingFunction();
const FunctionDecl *getEnclosingFunction() const {
- return dyn_cast<FunctionDecl>(getEnclosingNonExpansionStatementContext());
- }
-
- FunctionDecl *castEnclosingFunction() {
- return cast<FunctionDecl>(getEnclosingNonExpansionStatementContext());
+ return const_cast<DeclContext *>(this)->getEnclosingFunction();
}
+ FunctionDecl *castEnclosingFunction();
const FunctionDecl *castEnclosingFunction() const {
- return cast<FunctionDecl>(getEnclosingNonExpansionStatementContext());
+ return const_cast<DeclContext *>(this)->castEnclosingFunction();
}
/// Test whether the context supports looking up names.
diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index 70f61fa57a682..548643cea2cce 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -2096,6 +2096,14 @@ DeclContext *DeclContext::getEnclosingNonExpansionStatementContext() {
return DC;
}
+FunctionDecl *DeclContext::getEnclosingFunction() {
+ return dyn_cast<FunctionDecl>(getEnclosingNonExpansionStatementContext());
+}
+
+FunctionDecl *DeclContext::castEnclosingFunction() {
+ return cast<FunctionDecl>(getEnclosingNonExpansionStatementContext());
+}
+
bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
// For non-file contexts, this is equivalent to Equals.
if (!isFileContext())
More information about the cfe-commits
mailing list