Skip to content

Commit 38a977d

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

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
@@ -5695,11 +5695,10 @@ Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() {
56955695
Scope::CompoundStmtScope);
56965696
TopLevelStmtDecl *TLSD = Actions.ActOnStartTopLevelStmtDecl(getCurScope());
56975697
StmtResult R = ParseStatementOrDeclaration(Stmts, SubStmtCtx);
5698+
Actions.ActOnFinishTopLevelStmtDecl(TLSD, R.get());
56985699
if (!R.isUsable())
56995700
R = Actions.ActOnNullStmt(Tok.getLocation());
57005701

5701-
Actions.ActOnFinishTopLevelStmtDecl(TLSD, R.get());
5702-
57035702
if (Tok.is(tok::annot_repl_input_end) &&
57045703
Tok.getAnnotationValue() != nullptr) {
57055704
ConsumeAnnotationToken();

clang/lib/Sema/SemaDecl.cpp

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

2057520575
void Sema::ActOnFinishTopLevelStmtDecl(TopLevelStmtDecl *D, Stmt *Statement) {
20576-
D->setStmt(Statement);
20576+
if (Statement)
20577+
D->setStmt(Statement);
2057720578
PopCompoundScope();
2057820579
PopFunctionScopeInfo();
2057920580
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)