[clang] [Clang] fix -Wimplicit-fallthrough false positive after try CFG changes (PR #183194)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 24 14:51:50 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Oleksandr Tarasiuk (a-tarasyuk)
<details>
<summary>Changes</summary>
Fixes #<!-- -->182822
---
This patch fixes a _false positive_ regression of `-Wimplicit-fallthrough`, caused by a CFG `try` change in 41c566e, which led to `CXXTryStmt` predecessor edges being considered in `switch` _fallthrough_ analysis and incorrectly treated as normal _fallthrough_.
---
Full diff: https://github.com/llvm/llvm-project/pull/183194.diff
2 Files Affected:
- (modified) clang/lib/Sema/AnalysisBasedWarnings.cpp (+2-2)
- (modified) clang/test/SemaCXX/switch-implicit-fallthrough.cpp (+17-2)
``````````diff
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index 5c49e601840cf..1e604b6b6e26f 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -1252,8 +1252,8 @@ class FallthroughMapper : public DynamicRecursiveASTVisitor {
continue;
const Stmt *Term = P->getTerminatorStmt();
- if (isa_and_nonnull<SwitchStmt>(Term))
- continue; // Switch statement, good.
+ if (isa_and_nonnull<SwitchStmt, CXXTryStmt>(Term))
+ continue; // switch/try terminators are not switch fallthrough edges
const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(P->getLabel());
if (SW && SW->getSubStmt() == B.getLabel() && P->begin() == P->end())
diff --git a/clang/test/SemaCXX/switch-implicit-fallthrough.cpp b/clang/test/SemaCXX/switch-implicit-fallthrough.cpp
index 750663717b66a..8b26d7766ca79 100644
--- a/clang/test/SemaCXX/switch-implicit-fallthrough.cpp
+++ b/clang/test/SemaCXX/switch-implicit-fallthrough.cpp
@@ -1,5 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wimplicit-fallthrough -Wunreachable-code-fallthrough %s
-
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wimplicit-fallthrough -Wunreachable-code-fallthrough -fcxx-exceptions %s
int fallthrough(int n) {
switch (n / 10) {
@@ -292,6 +291,22 @@ void fallthrough_in_lambda() {
};
}
+namespace GH182822 {
+ void test(int n) {
+ switch (n) {
+ case 0:
+ try {
+ if (n) {
+ try {
+ } catch (int) {
+ }
+ }
+ } catch (...) {
+ }
+ }
+ }
+}
+
namespace PR18983 {
void fatal() __attribute__((noreturn));
int num();
``````````
</details>
https://github.com/llvm/llvm-project/pull/183194
More information about the cfe-commits
mailing list