Skip to content

[sema] enhance error handling for compound stmt body in StmtExpr #113760

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
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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ Bug Fixes in This Version
- The warning emitted for an unsupported register variable type now points to
the unsupported type instead of the ``register`` keyword (#GH109776).
- Fixed a crash when emit ctor for global variant with flexible array init (#GH113187).
- Fixed a crash when GNU statement expression contains invalid statement (#GH113468).

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,7 @@ StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
ParsedStmtContext::Compound |
(isStmtExpr ? ParsedStmtContext::InStmtExpr : ParsedStmtContext());

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

if (R.isUsable())
Stmts.push_back(R.get());
LastIsError = R.isInvalid();
}
// StmtExpr needs to do copy initialization for last statement.
// If last statement is invalid, the last statement in `Stmts` will be
// incorrect. Then the whole compound statement should also be marked as
// invalid to prevent subsequent errors.
if (isStmtExpr && LastIsError && !Stmts.empty())
return StmtError();

// Warn the user that using option `-ffp-eval-method=source` on a
// 32-bit target and feature `sse` disabled, or using
// `pragma clang fp eval_method=source` and feature `sse` disabled, is not
Expand Down
12 changes: 12 additions & 0 deletions clang/test/SemaCXX/gh113468.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s

constexpr int expr() {
if (({
int f;
f = 0;
if (f)
break; // expected-error {{'break' statement not in loop or switch statement}}
}))
return 2;
return 1;
}
Loading