From: Ted Kremenek Date: Wed, 30 Nov 2011 21:22:09 +0000 (+0000) Subject: Don't run -Wunreachable-code on template instantiations. Different instantiations... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5dfee06daa359bbe0f3c9de055b8a02d61a05173;p=clang Don't run -Wunreachable-code on template instantiations. Different instantiations may produce different unreachable code results, and it is very difficult for us to prove that ALL instantiations of a template have specific unreachable code. If we come up with a better solution, then we can revisit this, but this approach will at least greatly reduce the noise of this warning for code that makes use of templates. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145520 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/AnalysisBasedWarnings.cpp b/lib/Sema/AnalysisBasedWarnings.cpp index 8b2e269708..54a26b3253 100644 --- a/lib/Sema/AnalysisBasedWarnings.cpp +++ b/lib/Sema/AnalysisBasedWarnings.cpp @@ -914,8 +914,14 @@ AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, } // Warning: check for unreachable code - if (P.enableCheckUnreachable) - CheckUnreachable(S, AC); + if (P.enableCheckUnreachable) { + // Only check for unreachable code on non-template instantiations. + // Different template instantiations can effectively change the control-flow + // and it is very difficult to prove that a snippet of code in a template + // is unreachable for all instantiations. + if (S.ActiveTemplateInstantiations.empty()) + CheckUnreachable(S, AC); + } // Check for thread safety violations if (P.enableThreadSafetyAnalysis) { diff --git a/test/SemaCXX/warn-unreachable.cpp b/test/SemaCXX/warn-unreachable.cpp index 604a3c0da3..fa740bac65 100644 --- a/test/SemaCXX/warn-unreachable.cpp +++ b/test/SemaCXX/warn-unreachable.cpp @@ -76,3 +76,25 @@ void test6() { S (halt()); // expected-warning {{will never be executed}} } + +// Don't warn about unreachable code in template instantiations, as +// they may only be unreachable in that specific instantiation. +void isUnreachable(); + +template void test_unreachable_templates() { + T::foo(); + isUnreachable(); // no-warning +} + +struct TestUnreachableA { + static void foo() __attribute__((noreturn)); +}; +struct TestUnreachableB { + static void foo(); +}; + +void test_unreachable_templates_harness() { + test_unreachable_templates(); + test_unreachable_templates(); +} +