[clang] a1333ad - [Clang] Fix false-positive fallthrough warning (#234848)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 02:54:46 PST 2026
Author: Adinesh Reddy
Date: 2026-11-06T10:54:41Z
New Revision: a1333adb0c1e5a7fc0ae610bb3adaaef5684374d
URL: https://github.com/llvm/llvm-project/commit/a1333adb0c1e5a7fc0ae610bb3adaaef5684374d
DIFF: https://github.com/llvm/llvm-project/commit/a1333adb0c1e5a7fc0ae610bb3adaaef5684374d.diff
LOG: [Clang] Fix false-positive fallthrough warning (#234848)
substatement of a case label can be wrapped in an AttributedStmt when
attributes like [[likely]] or [[unlikely]] are used. Because of this,
directly comparing the substatement pointer with the next block's label
can fail even when they refer to the same underlying case label. I put
helper function stripAttributedStmts() to unwrap nested AttributedStmt
and return underlying statement and used it in the comparisons for
SwitchCase and LabelStmt in checkFallThroughIntoBlock() to get correct
comparision check even with attrbuted labels.
- Added regression tests in
`clang/test/SemaCXX/switch-implicit-fallthrough.cpp` for the reported
issue.
#LLM usage
I used LLM to help understand the existing Clang code, investigate the
root cause, and phrase the PR description. I wrote the implementation
and regression tests myself, then reviewed and tested the changes
locally.
Fixes #230802.
Added:
Modified:
clang/docs/ReleaseNotes.md
clang/lib/Sema/AnalysisBasedWarnings.cpp
clang/test/SemaCXX/switch-implicit-fallthrough.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index ea9fbe00ae3fe..c946e8e3ff640 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -489,6 +489,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
### Improvements to Clang's diagnostics
+- Fixed a false-positive `-Wimplicit-fallthrough` warning when
+ consecutive switch labels share a body and are annotated with
+ attributes like `[[unlikely]]`. (#GH230802)
+
- Fixed missing `-Wuninitialized-explicit-init` warnings for C file-scope
tentative definitions of structure types which contain a field marked with
the `require_explicit_initialization` attribute. (#GH232483)
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index a2f7581473caf..f2b5572590568 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -1276,11 +1276,13 @@ class FallthroughMapper : public DynamicRecursiveASTVisitor {
continue; // Switch statement, good.
const SwitchCase *SW = dyn_cast_or_null<SwitchCase>(P->getLabel());
- if (SW && SW->getSubStmt() == B.getLabel() && P->begin() == P->end())
+ if (SW && stripAttributedStmt(SW->getSubStmt()) == B.getLabel() &&
+ P->begin() == P->end())
continue; // Previous case label has no statements, good.
const LabelStmt *L = dyn_cast_or_null<LabelStmt>(P->getLabel());
- if (L && L->getSubStmt() == B.getLabel() && P->begin() == P->end())
+ if (L && stripAttributedStmt(L->getSubStmt()) == B.getLabel() &&
+ P->begin() == P->end())
continue; // Case label is preceded with a normal label, good.
if (!ReachableBlocks.count(P)) {
@@ -1354,6 +1356,13 @@ class FallthroughMapper : public DynamicRecursiveASTVisitor {
}
private:
+ static const Stmt *stripAttributedStmt(const Stmt *S) {
+ if (const auto *AS = dyn_cast_or_null<AttributedStmt>(S)) {
+ assert(!isa<AttributedStmt>(AS->getSubStmt()));
+ return AS->getSubStmt();
+ }
+ return S;
+ }
static const AttributedStmt *asFallThroughAttr(const Stmt *S) {
if (const AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(S)) {
diff --git a/clang/test/SemaCXX/switch-implicit-fallthrough.cpp b/clang/test/SemaCXX/switch-implicit-fallthrough.cpp
index 750663717b66a..200f027c5b3cf 100644
--- a/clang/test/SemaCXX/switch-implicit-fallthrough.cpp
+++ b/clang/test/SemaCXX/switch-implicit-fallthrough.cpp
@@ -253,6 +253,87 @@ int fallthrough_covered_enums(Enum e) {
return n;
}
+// Attributes on consecutive switch labels should not introduce false fallthrough.
+int fallthrough_attributed_labels(int n) {
+ switch (n) {
+ [[unlikely]] case 0:
+ [[unlikely]] case 1:
+ [[likely]] case 2:
+ case 3:
+ [[unlikely]] case 4:
+ [[unlikely]] default:
+ return -1;
+ }
+ return n;
+}
+
+// Fallthrough into attributed labels without an annotation should warn.
+int fallthrough_into_attributed_labels(int n) {
+ switch (n) {
+ case 0:
+ n++;
+ [[unlikely]] case 1: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
+ n++;
+ break;
+ [[unlikely]] case 2:
+ n++;
+ [[unlikely]] default: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
+ return 2;
+ }
+ return n;
+}
+
+// Consecutive ordinary labels do not introduce fallthrough.
+int fallthrough_ordinary_labels(int n) {
+ switch (n) {
+ case 0:
+ case 1:
+ return 0;
+ case 2:
+ return 1;
+ default:
+ return 2;
+ }
+}
+
+// An explicit fallthrough annotation should suppress the warning.
+int fallthrough_explicit_annotation(int n) {
+ switch (n) {
+ case 0:
+ ++n;
+ [[clang::fallthrough]];
+ case 1:
+ return 0;
+ default:
+ return 1;
+ }
+}
+
+// Genuine fallthrough without an annotation should warn.
+int fallthrough_without_annotation(int n) {
+ switch (n) {
+ case 0:
+ ++n;
+ // expected-warning at +1 {{unannotated fall-through between switch labels}}
+ case 1: // expected-note 2 {{insert}}
+ return 0;
+ default:
+ return 1;
+ }
+}
+
+// An attributed case label immediately follows an ordinary label.
+int fallthrough_label_attributed_case(int n) {
+ switch (n) {
+ case 0:
+ ordinary_label:
+ [[likely]] case 1:
+ return 0;
+ default:
+ return 1;
+ }
+}
+
// Fallthrough annotations in local classes used to generate "fallthrough
// annotation does not directly precede switch label" warning.
void fallthrough_in_local_class() {
More information about the cfe-commits
mailing list