[PATCH] D131818: [clang][diagnostics] Don't warn about unreachable code in constexpr if

Alan Zhao via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 15 12:24:53 PDT 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rGff8aadf58d1a: [clang][diagnostics] Don't warn about unreachable code in constexpr if (authored by ayzhao).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131818/new/

https://reviews.llvm.org/D131818

Files:
  clang/lib/Analysis/ReachableCode.cpp
  clang/test/SemaCXX/unreachable-code.cpp


Index: clang/test/SemaCXX/unreachable-code.cpp
===================================================================
--- clang/test/SemaCXX/unreachable-code.cpp
+++ clang/test/SemaCXX/unreachable-code.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -Wunreachable-code-aggressive -fblocks -verify %s
+// RUN: %clang_cc1 -std=c++17 -fcxx-exceptions -fexceptions -fsyntax-only -Wunreachable-code-aggressive -fblocks -verify %s
 
 int j;
 int bar();
@@ -99,3 +99,34 @@
 }
 
 }
+
+namespace gh57123 {
+  bool foo() {
+    if constexpr (true) {
+      if (true)
+        return true;
+      else
+        return false; // expected-warning {{will never be executed}}
+    }
+    else
+      return false; // no-warning
+  }
+
+  bool bar() {
+    if (true)
+      return true;
+    else
+      return false; // expected-warning {{will never be executed}}
+  }
+
+  bool baz() {
+    if constexpr (true)
+      return true;
+    else {
+      if (true)
+        return true;
+      else
+        return false; // expected-warning {{will never be executed}}
+    }
+  }
+}
Index: clang/lib/Analysis/ReachableCode.cpp
===================================================================
--- clang/lib/Analysis/ReachableCode.cpp
+++ clang/lib/Analysis/ReachableCode.cpp
@@ -299,6 +299,12 @@
     if (isa<BinaryOperator>(Term)) {
       return isConfigurationValue(Term, PP);
     }
+    // Do not treat constexpr if statement successors as unreachable in warnings
+    // since the point of these statements is to determine branches at compile
+    // time.
+    if (const auto *IS = dyn_cast<IfStmt>(Term);
+        IS != nullptr && IS->isConstexpr())
+      return true;
   }
 
   const Stmt *Cond = B->getTerminatorCondition(/* stripParens */ false);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131818.452776.patch
Type: text/x-patch
Size: 1776 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220815/e934bd75/attachment.bin>


More information about the cfe-commits mailing list