Skip to content

Commit acb7dfa

Browse files
authored
[clang][bytecode] Create local scopes for if then/else statements (llvm#120852)
In case those aren't compound statements.
1 parent 7b86fbb commit acb7dfa

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4974,20 +4974,35 @@ template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
49744974
LabelTy LabelEnd = this->getLabel();
49754975
if (!this->jumpFalse(LabelElse))
49764976
return false;
4977-
if (!visitStmt(IS->getThen()))
4978-
return false;
4977+
{
4978+
LocalScope<Emitter> ThenScope(this);
4979+
if (!visitStmt(IS->getThen()))
4980+
return false;
4981+
if (!ThenScope.destroyLocals())
4982+
return false;
4983+
}
49794984
if (!this->jump(LabelEnd))
49804985
return false;
49814986
this->emitLabel(LabelElse);
4982-
if (!visitStmt(Else))
4983-
return false;
4987+
{
4988+
LocalScope<Emitter> ElseScope(this);
4989+
if (!visitStmt(Else))
4990+
return false;
4991+
if (!ElseScope.destroyLocals())
4992+
return false;
4993+
}
49844994
this->emitLabel(LabelEnd);
49854995
} else {
49864996
LabelTy LabelEnd = this->getLabel();
49874997
if (!this->jumpFalse(LabelEnd))
49884998
return false;
4989-
if (!visitStmt(IS->getThen()))
4990-
return false;
4999+
{
5000+
LocalScope<Emitter> ThenScope(this);
5001+
if (!visitStmt(IS->getThen()))
5002+
return false;
5003+
if (!ThenScope.destroyLocals())
5004+
return false;
5005+
}
49915006
this->emitLabel(LabelEnd);
49925007
}
49935008

clang/test/AST/ByteCode/if.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,30 @@ namespace IfScope {
7676
}
7777
static_assert(foo() == 13, "");
7878
}
79+
80+
namespace IfScope2 {
81+
struct __bit_iterator {
82+
unsigned __ctz_;
83+
};
84+
constexpr void __fill_n_bool(__bit_iterator) {}
85+
86+
constexpr void fill_n(__bit_iterator __first) {
87+
if (false)
88+
__fill_n_bool(__first);
89+
else
90+
__fill_n_bool(__first);
91+
}
92+
93+
struct bitset{
94+
constexpr void reset() {
95+
auto m = __bit_iterator(8);
96+
fill_n(m);
97+
}
98+
};
99+
consteval bool foo() {
100+
bitset v;
101+
v.reset();
102+
return true;
103+
}
104+
static_assert(foo());
105+
}

0 commit comments

Comments
 (0)