[clang] 62e6255 - [clang][Interp] Add missing fallthrough in loops

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 2 03:04:36 PDT 2024


Author: Timm Bäder
Date: 2024-07-02T11:51:07+02:00
New Revision: 62e6255a58eb0e9bb31e366a9e30d5c1eaadd004

URL: https://github.com/llvm/llvm-project/commit/62e6255a58eb0e9bb31e366a9e30d5c1eaadd004
DIFF: https://github.com/llvm/llvm-project/commit/62e6255a58eb0e9bb31e366a9e30d5c1eaadd004.diff

LOG: [clang][Interp] Add missing fallthrough in loops

Added: 
    

Modified: 
    clang/lib/AST/Interp/Compiler.cpp
    clang/test/AST/Interp/literals.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/Compiler.cpp b/clang/lib/AST/Interp/Compiler.cpp
index 77c7e20632881..3d9c72e2b6dee 100644
--- a/clang/lib/AST/Interp/Compiler.cpp
+++ b/clang/lib/AST/Interp/Compiler.cpp
@@ -4139,6 +4139,7 @@ bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) {
   LabelTy EndLabel = this->getLabel();  // Label after the loop.
   LoopScope<Emitter> LS(this, EndLabel, CondLabel);
 
+  this->fallthrough(CondLabel);
   this->emitLabel(CondLabel);
 
   if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
@@ -4174,12 +4175,14 @@ template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
   LoopScope<Emitter> LS(this, EndLabel, CondLabel);
   LocalScope<Emitter> Scope(this);
 
+  this->fallthrough(StartLabel);
   this->emitLabel(StartLabel);
   {
     DestructorScope<Emitter> DS(Scope);
 
     if (!this->visitLoopBody(Body))
       return false;
+    this->fallthrough(CondLabel);
     this->emitLabel(CondLabel);
     if (!this->visitBool(Cond))
       return false;
@@ -4187,6 +4190,7 @@ template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
   if (!this->jumpTrue(StartLabel))
     return false;
 
+  this->fallthrough(EndLabel);
   this->emitLabel(EndLabel);
   return true;
 }
@@ -4207,6 +4211,7 @@ bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
 
   if (Init && !this->visitStmt(Init))
     return false;
+  this->fallthrough(CondLabel);
   this->emitLabel(CondLabel);
 
   if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
@@ -4225,6 +4230,7 @@ bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
 
     if (Body && !this->visitLoopBody(Body))
       return false;
+    this->fallthrough(IncLabel);
     this->emitLabel(IncLabel);
     if (Inc && !this->discard(Inc))
       return false;
@@ -4232,6 +4238,7 @@ bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
 
   if (!this->jump(CondLabel))
     return false;
+  this->fallthrough(EndLabel);
   this->emitLabel(EndLabel);
   return true;
 }
@@ -4263,6 +4270,7 @@ bool Compiler<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) {
     return false;
 
   // Now the condition as well as the loop variable assignment.
+  this->fallthrough(CondLabel);
   this->emitLabel(CondLabel);
   if (!this->visitBool(Cond))
     return false;
@@ -4279,13 +4287,16 @@ bool Compiler<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) {
 
     if (!this->visitLoopBody(Body))
       return false;
+  this->fallthrough(IncLabel);
     this->emitLabel(IncLabel);
     if (!this->discard(Inc))
       return false;
   }
+
   if (!this->jump(CondLabel))
     return false;
 
+  this->fallthrough(EndLabel);
   this->emitLabel(EndLabel);
   return true;
 }

diff  --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index a7a602ec355d5..3abaf89e8bd01 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -1243,3 +1243,16 @@ namespace Extern {
   static_assert(&ExternNonLiteralVarDecl() == &nl, "");
 #endif
 }
+
+#if __cplusplus >= 201402L
+constexpr int StmtExprEval() {
+  if (({
+    while (0);
+    true;
+  })) {
+    return 2;
+  }
+  return 1;
+}
+static_assert(StmtExprEval() == 2, "");
+#endif


        


More information about the cfe-commits mailing list