r298477 - Suppress warning on unreachable [[clang::fallthrough]] within a template instantiation.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 21 18:49:20 PDT 2017


Author: rsmith
Date: Tue Mar 21 20:49:19 2017
New Revision: 298477

URL: http://llvm.org/viewvc/llvm-project?rev=298477&view=rev
Log:
Suppress warning on unreachable [[clang::fallthrough]] within a template instantiation.

We don't know whether some other instantiation of the template might be able to
reach the annotation, so warning on it has a high chance of false positives.

Patch by Ahmed Asadi!

Differential Revision: https://reviews.llvm.org/D31069

Added:
    cfe/trunk/test/SemaCXX/P30636.cpp
Modified:
    cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp

Modified: cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp?rev=298477&r1=298476&r2=298477&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp (original)
+++ cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp Tue Mar 21 20:49:19 2017
@@ -972,7 +972,8 @@ namespace {
       }
     }
 
-    bool checkFallThroughIntoBlock(const CFGBlock &B, int &AnnotatedCnt) {
+    bool checkFallThroughIntoBlock(const CFGBlock &B, int &AnnotatedCnt,
+                                   bool IsTemplateInstantiation) {
       assert(!ReachableBlocks.empty() && "ReachableBlocks empty");
 
       int UnannotatedCnt = 0;
@@ -1002,8 +1003,12 @@ namespace {
                ElemIt != ElemEnd; ++ElemIt) {
             if (Optional<CFGStmt> CS = ElemIt->getAs<CFGStmt>()) {
               if (const AttributedStmt *AS = asFallThroughAttr(CS->getStmt())) {
-                S.Diag(AS->getLocStart(),
-                       diag::warn_fallthrough_attr_unreachable);
+                // Don't issue a warning for an unreachable fallthrough
+                // attribute in template instantiations as it may not be
+                // unreachable in all instantiations of the template.
+                if (!IsTemplateInstantiation)
+                  S.Diag(AS->getLocStart(),
+                         diag::warn_fallthrough_attr_unreachable);
                 markFallthroughVisited(AS);
                 ++AnnotatedCnt;
                 break;
@@ -1164,7 +1169,11 @@ static void DiagnoseSwitchLabelsFallthro
 
     int AnnotatedCnt;
 
-    if (!FM.checkFallThroughIntoBlock(*B, AnnotatedCnt))
+    bool IsTemplateInstantiation = false;
+    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(AC.getDecl()))
+      IsTemplateInstantiation = Function->isTemplateInstantiation();
+    if (!FM.checkFallThroughIntoBlock(*B, AnnotatedCnt,
+                                      IsTemplateInstantiation))
       continue;
 
     S.Diag(Label->getLocStart(),

Added: cfe/trunk/test/SemaCXX/P30636.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/P30636.cpp?rev=298477&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/P30636.cpp (added)
+++ cfe/trunk/test/SemaCXX/P30636.cpp Tue Mar 21 20:49:19 2017
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wimplicit-fallthrough %s
+// expected-no-diagnostics
+
+template<bool param>
+int fallthrough_template(int i)
+{
+  switch (i) {
+    case 1:
+      if (param)
+        return 3;
+      [[clang::fallthrough]]; // no warning here, for an unreachable annotation (in the fallthrough_template<true> case) in a template function
+    case 2:
+      return 4;
+    default:
+      return 5;
+  }
+}
+                                      
+template int fallthrough_template<true>(int);
+




More information about the cfe-commits mailing list