Author: Alan Zhao Date: 2022-08-15T15:24:39-04:00 New Revision: ff8aadf58d1a0ea7d8fa2b9a7a17ff0c6059baa5
URL: https://github.com/llvm/llvm-project/commit/ff8aadf58d1a0ea7d8fa2b9a7a17ff0c6059baa5 DIFF: https://github.com/llvm/llvm-project/commit/ff8aadf58d1a0ea7d8fa2b9a7a17ff0c6059baa5.diff LOG: [clang][diagnostics] Don't warn about unreachable code in constexpr if The point of a constexpr if statement is to determine which branch to take at compile time, so warning on unreachable code is meaningless in these situations. Fixes #57123. Reviewed By: thakis Differential Revision: https://reviews.llvm.org/D131818 Added: Modified: clang/lib/Analysis/ReachableCode.cpp clang/test/SemaCXX/unreachable-code.cpp Removed: ################################################################################ diff --git a/clang/lib/Analysis/ReachableCode.cpp b/clang/lib/Analysis/ReachableCode.cpp index 652bef34cebb7..ce0f4f83ba04f 100644 --- a/clang/lib/Analysis/ReachableCode.cpp +++ b/clang/lib/Analysis/ReachableCode.cpp @@ -299,6 +299,12 @@ static bool shouldTreatSuccessorsAsReachable(const CFGBlock *B, 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); diff --git a/clang/test/SemaCXX/unreachable-code.cpp b/clang/test/SemaCXX/unreachable-code.cpp index 6a95f767bef02..410ed9ae8861b 100644 --- a/clang/test/SemaCXX/unreachable-code.cpp +++ b/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 @@ void f(int a) { } } + +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}} + } + } +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits