Skip to content

Commit cad0940

Browse files
authored
[sema] enhance error handling for compound stmt body in StmtExpr (llvm#113760)
Mark the whole StmtExpr invalid when the last statement in compound statement is invalid. Because the last statement need to do copy initialization, it causes subsequent errors to simply ignore last invalid statement. Fixed: llvm#113468
1 parent 44d0e95 commit cad0940

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ Bug Fixes in This Version
470470
- The warning emitted for an unsupported register variable type now points to
471471
the unsupported type instead of the ``register`` keyword (#GH109776).
472472
- Fixed a crash when emit ctor for global variant with flexible array init (#GH113187).
473+
- Fixed a crash when GNU statement expression contains invalid statement (#GH113468).
473474

474475
Bug Fixes to Compiler Builtins
475476
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Parse/ParseStmt.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,7 @@ StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
12431243
ParsedStmtContext::Compound |
12441244
(isStmtExpr ? ParsedStmtContext::InStmtExpr : ParsedStmtContext());
12451245

1246+
bool LastIsError = false;
12461247
while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
12471248
Tok.isNot(tok::eof)) {
12481249
if (Tok.is(tok::annot_pragma_unused)) {
@@ -1299,7 +1300,15 @@ StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
12991300

13001301
if (R.isUsable())
13011302
Stmts.push_back(R.get());
1303+
LastIsError = R.isInvalid();
13021304
}
1305+
// StmtExpr needs to do copy initialization for last statement.
1306+
// If last statement is invalid, the last statement in `Stmts` will be
1307+
// incorrect. Then the whole compound statement should also be marked as
1308+
// invalid to prevent subsequent errors.
1309+
if (isStmtExpr && LastIsError && !Stmts.empty())
1310+
return StmtError();
1311+
13031312
// Warn the user that using option `-ffp-eval-method=source` on a
13041313
// 32-bit target and feature `sse` disabled, or using
13051314
// `pragma clang fp eval_method=source` and feature `sse` disabled, is not

clang/test/SemaCXX/gh113468.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
2+
3+
constexpr int expr() {
4+
if (({
5+
int f;
6+
f = 0;
7+
if (f)
8+
break; // expected-error {{'break' statement not in loop or switch statement}}
9+
}))
10+
return 2;
11+
return 1;
12+
}

0 commit comments

Comments
 (0)