Skip to content

Commit 5334afc

Browse files
committed
[clang][Interp] Don't forget to visit condition variable decls
We did this for if statements, but switch and loop constructs need to do it as well.
1 parent f1d0a48 commit 5334afc

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

clang/lib/AST/Interp/ByteCodeStmtGen.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ bool ByteCodeStmtGen<Emitter>::visitWhileStmt(const WhileStmt *S) {
423423
LoopScope<Emitter> LS(this, EndLabel, CondLabel);
424424

425425
this->emitLabel(CondLabel);
426+
427+
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
428+
if (!visitDeclStmt(CondDecl))
429+
return false;
430+
426431
if (!this->visitBool(Cond))
427432
return false;
428433
if (!this->jumpFalse(EndLabel))
@@ -487,6 +492,10 @@ bool ByteCodeStmtGen<Emitter>::visitForStmt(const ForStmt *S) {
487492
if (Init && !this->visitStmt(Init))
488493
return false;
489494
this->emitLabel(CondLabel);
495+
496+
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
497+
if (!visitDeclStmt(CondDecl))
498+
return false;
490499
if (Cond) {
491500
if (!this->visitBool(Cond))
492501
return false;
@@ -585,17 +594,21 @@ bool ByteCodeStmtGen<Emitter>::visitContinueStmt(const ContinueStmt *S) {
585594
template <class Emitter>
586595
bool ByteCodeStmtGen<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
587596
const Expr *Cond = S->getCond();
588-
PrimType CondT = this->classifyPrim(Cond->getType());
589597

590598
LabelTy EndLabel = this->getLabel();
591599
OptLabelTy DefaultLabel = std::nullopt;
592-
unsigned CondVar = this->allocateLocalPrimitive(Cond, CondT, true, false);
593600

594601
if (const auto *CondInit = S->getInit())
595602
if (!visitStmt(CondInit))
596603
return false;
597604

605+
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
606+
if (!visitDeclStmt(CondDecl))
607+
return false;
608+
598609
// Initialize condition variable.
610+
PrimType CondT = this->classifyPrim(Cond->getType());
611+
unsigned CondVar = this->allocateLocalPrimitive(Cond, CondT, true, false);
599612
if (!this->visit(Cond))
600613
return false;
601614
if (!this->emitSetLocal(CondT, CondVar, S))

clang/test/SemaCXX/decomposed-condition.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -std=c++1z -Wno-binding-in-condition -verify %s
2+
// RUN: %clang_cc1 -std=c++1z -Wno-binding-in-condition -verify %s -fexperimental-new-constant-interpreter
23

34
struct X {
45
bool flag;

0 commit comments

Comments
 (0)