Skip to content

[clang][bytecode] Fix a variable scope problem with continue/break jumps #107738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,23 @@ template <class Emitter> class LoopScope final : public LabelScope<Emitter> {

LoopScope(Compiler<Emitter> *Ctx, LabelTy BreakLabel, LabelTy ContinueLabel)
: LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
OldContinueLabel(Ctx->ContinueLabel) {
OldContinueLabel(Ctx->ContinueLabel),
OldLabelVarScope(Ctx->LabelVarScope) {
this->Ctx->BreakLabel = BreakLabel;
this->Ctx->ContinueLabel = ContinueLabel;
this->Ctx->LabelVarScope = this->Ctx->VarScope;
}

~LoopScope() {
this->Ctx->BreakLabel = OldBreakLabel;
this->Ctx->ContinueLabel = OldContinueLabel;
this->Ctx->LabelVarScope = OldLabelVarScope;
}

private:
OptLabelTy OldBreakLabel;
OptLabelTy OldContinueLabel;
VariableScope<Emitter> *OldLabelVarScope;
};

// Sets the context for a switch scope, mapping labels.
Expand All @@ -140,22 +144,26 @@ template <class Emitter> class SwitchScope final : public LabelScope<Emitter> {
OptLabelTy DefaultLabel)
: LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
OldDefaultLabel(this->Ctx->DefaultLabel),
OldCaseLabels(std::move(this->Ctx->CaseLabels)) {
OldCaseLabels(std::move(this->Ctx->CaseLabels)),
OldLabelVarScope(Ctx->LabelVarScope) {
this->Ctx->BreakLabel = BreakLabel;
this->Ctx->DefaultLabel = DefaultLabel;
this->Ctx->CaseLabels = std::move(CaseLabels);
this->Ctx->LabelVarScope = this->Ctx->VarScope;
}

~SwitchScope() {
this->Ctx->BreakLabel = OldBreakLabel;
this->Ctx->DefaultLabel = OldDefaultLabel;
this->Ctx->CaseLabels = std::move(OldCaseLabels);
this->Ctx->LabelVarScope = OldLabelVarScope;
}

private:
OptLabelTy OldBreakLabel;
OptLabelTy OldDefaultLabel;
CaseMap OldCaseLabels;
VariableScope<Emitter> *OldLabelVarScope;
};

template <class Emitter> class StmtExprScope final {
Expand Down Expand Up @@ -4734,7 +4742,9 @@ bool Compiler<Emitter>::visitBreakStmt(const BreakStmt *S) {
if (!BreakLabel)
return false;

this->emitCleanup();
for (VariableScope<Emitter> *C = VarScope; C != LabelVarScope;
C = C->getParent())
C->emitDestruction();
return this->jump(*BreakLabel);
}

Expand All @@ -4743,7 +4753,9 @@ bool Compiler<Emitter>::visitContinueStmt(const ContinueStmt *S) {
if (!ContinueLabel)
return false;

this->emitCleanup();
for (VariableScope<Emitter> *C = VarScope; C != LabelVarScope;
C = C->getParent())
C->emitDestruction();
return this->jump(*ContinueLabel);
}

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
/// Switch case mapping.
CaseMap CaseLabels;

/// Scope to cleanup until when chumping to one of the labels.
VariableScope<Emitter> *LabelVarScope = nullptr;
/// Point to break to.
OptLabelTy BreakLabel;
/// Point to continue to.
Expand Down
32 changes: 19 additions & 13 deletions clang/test/AST/ByteCode/new-delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,22 +618,28 @@ namespace OperatorNewDelete {

constexpr bool mismatched(int alloc_kind, int dealloc_kind) {
int *p;

if (alloc_kind == 0)
p = new int; // both-note {{allocation performed here}}
else if (alloc_kind == 1)
p = new int[1]; // both-note {{allocation performed here}}
else if (alloc_kind == 2)
switch (alloc_kind) {
case 0:
p = new int; // both-note {{heap allocation performed here}}
break;
case 1:
p = new int[1]; // both-note {{heap allocation performed here}}
break;
case 2:
p = std::allocator<int>().allocate(1);


if (dealloc_kind == 0)
break;
}
switch (dealloc_kind) {
case 0:
delete p; // both-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
else if (dealloc_kind == 1)
break;
case 1:
delete[] p; // both-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
else if (dealloc_kind == 2)
std::allocator<int>().deallocate(p); // both-note 2{{in call to}}

break;
case 2:
std::allocator<int>().deallocate(p); // both-note 2{{in call}}
break;
}
return true;
}
static_assert(mismatched(0, 2)); // both-error {{constant expression}} \
Expand Down
Loading