[clang] 1a3dcd0 - [clang][Sema][NFC] Share constexpr statement traversal (#239210)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 16:01:22 PST 2026


Author: Chen Miao
Date: 2026-11-06T08:01:15+08:00
New Revision: 1a3dcd02c08a98a32164338ca91eeb3705649e37

URL: https://github.com/llvm/llvm-project/commit/1a3dcd02c08a98a32164338ca91eeb3705649e37
DIFF: https://github.com/llvm/llvm-project/commit/1a3dcd02c08a98a32164338ca91eeb3705649e37.diff

LOG: [clang][Sema][NFC] Share constexpr statement traversal (#239210)

Share child-statement traversal in CheckConstexprFunctionStmt and
simplify boolean result forwarding. Preserve language-version checks,
traversal order, and diagnostic locations.

Added: 
    

Modified: 
    clang/lib/Sema/SemaDeclCXX.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 9ddb077037397..8492b289736e8 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -2268,6 +2268,16 @@ CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S,
                            SourceLocation &Cxx1yLoc, SourceLocation &Cxx2aLoc,
                            SourceLocation &Cxx2bLoc,
                            Sema::CheckConstexprKind Kind) {
+  auto CheckChildren = [&] {
+    for (Stmt *SubStmt : S->children()) {
+      if (SubStmt &&
+          !CheckConstexprFunctionStmt(SemaRef, Dcl, SubStmt, ReturnStmts,
+                                      Cxx1yLoc, Cxx2aLoc, Cxx2bLoc, Kind))
+        return false;
+    }
+    return true;
+  };
+
   // - its function-body shall be [...] a compound-statement that contains only
   switch (S->getStmtClass()) {
   case Stmt::NullStmtClass:
@@ -2298,9 +2308,7 @@ CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S,
     //   - using-directives,
     //   - typedef declarations and alias-declarations that do not define
     //     classes or enumerations,
-    if (!CheckConstexprDeclStmt(SemaRef, Dcl, DS, Cxx1yLoc, Kind))
-      return false;
-    return true;
+    return CheckConstexprDeclStmt(SemaRef, Dcl, DS, Cxx1yLoc, Kind);
   }
 
   case Stmt::ReturnStmtClass:
@@ -2322,19 +2330,11 @@ CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S,
         SemaRef, Dcl, cast<AttributedStmt>(S)->getSubStmt(), ReturnStmts,
         Cxx1yLoc, Cxx2aLoc, Cxx2bLoc, Kind);
 
-  case Stmt::CompoundStmtClass: {
+  case Stmt::CompoundStmtClass:
     // C++1y allows compound-statements.
     if (!Cxx1yLoc.isValid())
       Cxx1yLoc = S->getBeginLoc();
-
-    CompoundStmt *CompStmt = cast<CompoundStmt>(S);
-    for (auto *BodyIt : CompStmt->body()) {
-      if (!CheckConstexprFunctionStmt(SemaRef, Dcl, BodyIt, ReturnStmts,
-                                      Cxx1yLoc, Cxx2aLoc, Cxx2bLoc, Kind))
-        return false;
-    }
-    return true;
-  }
+    return CheckChildren();
 
   case Stmt::IfStmtClass: {
     // C++1y allows if-statements.
@@ -2361,15 +2361,7 @@ CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S,
     // because they don't make sense without variable mutation.
     if (!SemaRef.getLangOpts().CPlusPlus14)
       break;
-    if (!Cxx1yLoc.isValid())
-      Cxx1yLoc = S->getBeginLoc();
-    for (Stmt *SubStmt : S->children()) {
-      if (SubStmt &&
-          !CheckConstexprFunctionStmt(SemaRef, Dcl, SubStmt, ReturnStmts,
-                                      Cxx1yLoc, Cxx2aLoc, Cxx2bLoc, Kind))
-        return false;
-    }
-    return true;
+    [[fallthrough]];
 
   case Stmt::SwitchStmtClass:
   case Stmt::CaseStmtClass:
@@ -2379,26 +2371,14 @@ CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S,
     // mutation, we can reasonably allow them in C++11 as an extension.
     if (!Cxx1yLoc.isValid())
       Cxx1yLoc = S->getBeginLoc();
-    for (Stmt *SubStmt : S->children()) {
-      if (SubStmt &&
-          !CheckConstexprFunctionStmt(SemaRef, Dcl, SubStmt, ReturnStmts,
-                                      Cxx1yLoc, Cxx2aLoc, Cxx2bLoc, Kind))
-        return false;
-    }
-    return true;
+    return CheckChildren();
 
   case Stmt::LabelStmtClass:
   case Stmt::GotoStmtClass:
   case Stmt::IndirectGotoStmtClass:
     if (Cxx2bLoc.isInvalid())
       Cxx2bLoc = S->getBeginLoc();
-    for (Stmt *SubStmt : S->children()) {
-      if (SubStmt &&
-          !CheckConstexprFunctionStmt(SemaRef, Dcl, SubStmt, ReturnStmts,
-                                      Cxx1yLoc, Cxx2aLoc, Cxx2bLoc, Kind))
-        return false;
-    }
-    return true;
+    return CheckChildren();
 
   case Stmt::GCCAsmStmtClass:
   case Stmt::MSAsmStmtClass:
@@ -2406,22 +2386,14 @@ CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S,
   case Stmt::CXXTryStmtClass:
     if (Cxx2aLoc.isInvalid())
       Cxx2aLoc = S->getBeginLoc();
-    for (Stmt *SubStmt : S->children()) {
-      if (SubStmt &&
-          !CheckConstexprFunctionStmt(SemaRef, Dcl, SubStmt, ReturnStmts,
-                                      Cxx1yLoc, Cxx2aLoc, Cxx2bLoc, Kind))
-        return false;
-    }
-    return true;
+    return CheckChildren();
 
   case Stmt::CXXCatchStmtClass:
     // Do not bother checking the language mode (already covered by the
     // try block check).
-    if (!CheckConstexprFunctionStmt(
-            SemaRef, Dcl, cast<CXXCatchStmt>(S)->getHandlerBlock(), ReturnStmts,
-            Cxx1yLoc, Cxx2aLoc, Cxx2bLoc, Kind))
-      return false;
-    return true;
+    return CheckConstexprFunctionStmt(
+        SemaRef, Dcl, cast<CXXCatchStmt>(S)->getHandlerBlock(), ReturnStmts,
+        Cxx1yLoc, Cxx2aLoc, Cxx2bLoc, Kind);
 
   default:
     if (!isa<Expr>(S))


        


More information about the cfe-commits mailing list