[PATCH] D31069: Don't warn about an unreachable fallthrough annotation in a template function

Ahmed Asadi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 16 21:46:29 PDT 2017


ahmedasadi updated this revision to Diff 92104.
ahmedasadi added a comment.

Added a test case.


https://reviews.llvm.org/D31069

Files:
  lib/Sema/AnalysisBasedWarnings.cpp
  test/SemaCXX/P30636.cpp


Index: test/SemaCXX/P30636.cpp
===================================================================
--- test/SemaCXX/P30636.cpp
+++ test/SemaCXX/P30636.cpp
@@ -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);
+
Index: lib/Sema/AnalysisBasedWarnings.cpp
===================================================================
--- lib/Sema/AnalysisBasedWarnings.cpp
+++ lib/Sema/AnalysisBasedWarnings.cpp
@@ -972,7 +972,7 @@
       }
     }
 
-    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 +1002,12 @@
                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 +1168,10 @@
 
     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(),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31069.92104.patch
Type: text/x-patch
Size: 2466 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170317/0c18e181/attachment.bin>


More information about the cfe-commits mailing list