Author: DeanSturtevant1
Date: 2026-09-18T20:00:35Z
New Revision: 25e00ff449d6c2a2d21a0b109f576be88847ce1f

URL: 
https://github.com/llvm/llvm-project/commit/25e00ff449d6c2a2d21a0b109f576be88847ce1f
DIFF: 
https://github.com/llvm/llvm-project/commit/25e00ff449d6c2a2d21a0b109f576be88847ce1f.diff

LOG: [Clang] Move DeclContext::getEnclosingFunction/castEnclosingFunction out 
of line (#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().

Change prepared by Jetski and reviewed by me.

Added: 
    

Modified: 
    clang/include/clang/AST/DeclBase.h
    clang/lib/AST/DeclBase.cpp

Removed: 
    


################################################################################
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())


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to