Skip to content

Commit 94bc857

Browse files
committed
UPSTREAM: [clang-repl] Always clean up scope and context for TopLevelStmtDecl (llvm#150215)
This fixes an issue introduced by llvm#84150, where failing to pop compound scope, function scope info, and decl context after a failed statement could lead to an inconsistent internal state. (cherry picked from commit 38a977d)
1 parent d7d3797 commit 94bc857

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

clang/lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6023,11 +6023,10 @@ Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() {
60236023
Scope::CompoundStmtScope);
60246024
TopLevelStmtDecl *TLSD = Actions.ActOnStartTopLevelStmtDecl(getCurScope());
60256025
StmtResult R = ParseStatementOrDeclaration(Stmts, SubStmtCtx);
6026+
Actions.ActOnFinishTopLevelStmtDecl(TLSD, R.get());
60266027
if (!R.isUsable())
60276028
return nullptr;
60286029

6029-
Actions.ActOnFinishTopLevelStmtDecl(TLSD, R.get());
6030-
60316030
if (Tok.is(tok::annot_repl_input_end) &&
60326031
Tok.getAnnotationValue() != nullptr) {
60336032
ConsumeAnnotationToken();

clang/lib/Sema/SemaDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20250,7 +20250,8 @@ TopLevelStmtDecl *Sema::ActOnStartTopLevelStmtDecl(Scope *S) {
2025020250
}
2025120251

2025220252
void Sema::ActOnFinishTopLevelStmtDecl(TopLevelStmtDecl *D, Stmt *Statement) {
20253-
D->setStmt(Statement);
20253+
if (Statement)
20254+
D->setStmt(Statement);
2025420255
PopCompoundScope();
2025520256
PopFunctionScopeInfo();
2025620257
PopDeclContext();

clang/test/Interpreter/fail.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ extern "C" int printf(const char *, ...);
1818
int i = 42;
1919
auto r1 = printf("i = %d\n", i);
2020
// CHECK: i = 42
21+
22+
1aap = 42; // expected-error {{invalid digit 'a' in decimal constant}}
23+
1aap = 42; i = 5; // expected-error {{invalid digit 'a' in decimal constant}}
24+
25+
printf("i = %d\n", i);
26+
// CHECK: i = 42
27+
2128
%quit

0 commit comments

Comments
 (0)