https://github.com/ahatanak updated 
https://github.com/llvm/llvm-project/pull/242121

>From 5f62ac88ae0454fe69b099b70b8b2c53327938f7 Mon Sep 17 00:00:00 2001
From: Akira Hatanaka <[email protected]>
Date: Thu, 12 Nov 2026 16:50:08 -0800
Subject: [PATCH 1/2] [Sema] Don't warn about fallthrough annotations after
 analyzer_noreturn calls

-Wunreachable-code-fallthrough diagnosed a fallthrough annotation right
after a call to an 'analyzer_noreturn' function as unreachable, even
though the call can return.

Make FallthroughMapper::fillReachableBlocks also add the unreachable
successor of a block that ends in an 'analyzer_noreturn' call to the set
of reachable blocks. That successor is the code immediately following
the call.

rdar://188741139
---
 clang/lib/Sema/AnalysisBasedWarnings.cpp      | 15 +++++++-
 .../Sema/warn-unreachable-analyzer-noreturn.c | 36 ++++++++++++++++++-
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp 
b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index de5eb3f093e41b..afeb1d54e17e74 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -1250,9 +1250,22 @@ class FallthroughMapper : public 
DynamicRecursiveASTVisitor {
     while (!BlockQueue.empty()) {
       const CFGBlock *P = BlockQueue.front();
       BlockQueue.pop_front();
-      for (const CFGBlock *B : P->succs()) {
+      // The code after an 'analyzer_noreturn' call isn't dead, since the call
+      // can return. The CFG keeps that code as the alternate successor of the
+      // exit edge, so follow it too.
+      bool FollowAlternate = P->hasOnlyAnalyzerNoReturnElement();
+      for (const CFGBlock::AdjacentBlock &Succ : P->succs()) {
+        const CFGBlock *B = Succ.getReachableBlock();
         if (B && ReachableBlocks.insert(B).second)
           BlockQueue.push_back(B);
+        if (!FollowAlternate)
+          continue;
+        const CFGBlock *Alt = Succ.getPossiblyUnreachableBlock();
+        assert(B && Alt != B &&
+               "noreturn edge should have a reachable block and a distinct "
+               "alternate");
+        if (Alt && ReachableBlocks.insert(Alt).second)
+          BlockQueue.push_back(Alt);
       }
     }
   }
diff --git a/clang/test/Sema/warn-unreachable-analyzer-noreturn.c 
b/clang/test/Sema/warn-unreachable-analyzer-noreturn.c
index 128d9440c216c3..04fa8620528366 100644
--- a/clang/test/Sema/warn-unreachable-analyzer-noreturn.c
+++ b/clang/test/Sema/warn-unreachable-analyzer-noreturn.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -Wunreachable-code -Wreturn-type 
-Wimplicit-fallthrough %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wunreachable-code 
-Wunreachable-code-fallthrough -Wreturn-type -Wimplicit-fallthrough %s
 
 // 'analyzer_noreturn' calls can return, so the code after them is not
 // unreachable. -Wreturn-type and -Wimplicit-fallthrough do treat them as
@@ -36,3 +36,37 @@ void fallthrough(int x) {
     break;
   }
 }
+
+void annotated_fallthrough(int x) {
+  switch (x) {
+  case 1:
+    analyzer_stop("bad");
+    __attribute__((fallthrough)); // no warning
+  case 2:
+    use(2);
+    break;
+  }
+}
+
+void annotated_fallthrough_after_code(int x) {
+  switch (x) {
+  case 1:
+    analyzer_stop("bad");
+    use(1);
+    __attribute__((fallthrough)); // no warning
+  case 2:
+    use(2);
+    break;
+  }
+}
+
+void annotated_fallthrough_after_real(int x) {
+  switch (x) {
+  case 1:
+    real_stop();
+    __attribute__((fallthrough)); // expected-warning {{fallthrough annotation 
in unreachable code}}
+  case 2:
+    use(2);
+    break;
+  }
+}

>From 716398d7d85eded93b8a5b712b3475665c193ed2 Mon Sep 17 00:00:00 2001
From: Akira Hatanaka <[email protected]>
Date: Sun, 15 Nov 2026 11:13:38 -0800
Subject: [PATCH 2/2] Remove redundant assert

---
 clang/lib/Sema/AnalysisBasedWarnings.cpp | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp 
b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index afeb1d54e17e74..308051b1d8ba3e 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -1261,9 +1261,6 @@ class FallthroughMapper : public 
DynamicRecursiveASTVisitor {
         if (!FollowAlternate)
           continue;
         const CFGBlock *Alt = Succ.getPossiblyUnreachableBlock();
-        assert(B && Alt != B &&
-               "noreturn edge should have a reachable block and a distinct "
-               "alternate");
         if (Alt && ReachableBlocks.insert(Alt).second)
           BlockQueue.push_back(Alt);
       }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to