[clang] [sema] enhance error handling for compound stmt body in `StmtExpr` (PR #113760)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 29 23:34:09 PDT 2024
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/113760
>From 1ea7857b255f9a260ece14891f4a3a423468a605 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sat, 26 Oct 2024 21:19:04 +0800
Subject: [PATCH 1/2] [sema] enhance error handling for compound stmt body in
`StmtExpr`
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.
---
clang/lib/Parse/ParseStmt.cpp | 9 +++++++++
clang/test/SemaCXX/gh113468.cpp | 12 ++++++++++++
2 files changed, 21 insertions(+)
create mode 100644 clang/test/SemaCXX/gh113468.cpp
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 60d647da48f053..6d0de38095b999 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -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)) {
@@ -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
diff --git a/clang/test/SemaCXX/gh113468.cpp b/clang/test/SemaCXX/gh113468.cpp
new file mode 100644
index 00000000000000..94551986b0efaa
--- /dev/null
+++ b/clang/test/SemaCXX/gh113468.cpp
@@ -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;
+}
>From 07a72906d468857c2ad2e91b59fb26a57c1b76d0 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Wed, 30 Oct 2024 14:33:57 +0800
Subject: [PATCH 2/2] add-release-note
---
clang/docs/ReleaseNotes.rst | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 28bb83a1c9d60f..9b4ffbacbdfdee 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
More information about the cfe-commits
mailing list