[PATCH] D29877: Warn about unused static file scope function template declarations.

Vassil Vassilev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 2 11:10:53 PST 2017


v.g.vassilev updated this revision to Diff 90360.
v.g.vassilev added a comment.

Rebase + ping.


https://reviews.llvm.org/D29877

Files:
  lib/Sema/Sema.cpp
  lib/Sema/SemaDecl.cpp
  test/SemaCXX/warn-unused-filescoped.cpp


Index: test/SemaCXX/warn-unused-filescoped.cpp
===================================================================
--- test/SemaCXX/warn-unused-filescoped.cpp
+++ test/SemaCXX/warn-unused-filescoped.cpp
@@ -32,6 +32,14 @@
   inline void bar(int, int) { }
 };
 
+namespace test8 {
+// Should ignore overloaded operators.
+template <typename PT1, typename PT2>
+struct S {};
+template <typename PT1, typename PT2>
+static bool operator==(S<PT1, PT2> lhs, S<PT1, PT2> rhs) { }
+}
+
 namespace pr19713 {
 #if __cplusplus >= 201103L
   static constexpr int constexpr1() { return 1; }
@@ -200,6 +208,11 @@
 static void func() {}
 }
 
+namespace test9 {
+template<typename T>
+static void completeRedeclChainForTemplateSpecialization() { } // expected-warning {{unused}}
+}
+
 namespace pr19713 {
 #if __cplusplus >= 201103L
   // FIXME: We should warn on both of these.
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -1493,6 +1493,10 @@
       // 'static inline' functions are defined in headers; don't warn.
       if (FD->isInlined() && !isMainFileLoc(*this, FD->getLocation()))
         return false;
+      // 'static operator' functions are defined in headers; don't warn.
+      if (FD->isOverloadedOperator() &&
+          !isMainFileLoc(*this, FD->getLocation()))
+        return false;
     }
 
     if (FD->doesThisDeclarationHaveABody() &&
@@ -8844,6 +8848,7 @@
     if (FunctionTemplate) {
       if (NewFD->isInvalidDecl())
         FunctionTemplate->setInvalidDecl();
+      MarkUnusedFileScopedDecl(NewFD);
       return FunctionTemplate;
     }
   }
Index: lib/Sema/Sema.cpp
===================================================================
--- lib/Sema/Sema.cpp
+++ lib/Sema/Sema.cpp
@@ -469,6 +469,13 @@
     return true;
 
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+    // If this is a function template, we should remove if it has no
+    // specializations.
+    if (FunctionTemplateDecl *Template = FD->getDescribedFunctionTemplate()) {
+      if (std::distance(Template->spec_begin(), Template->spec_end()))
+        return true;
+    }
+
     // UnusedFileScopedDecls stores the first declaration.
     // The declaration may have become definition so check again.
     const FunctionDecl *DeclToCheck;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29877.90360.patch
Type: text/x-patch
Size: 2347 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170302/11a4a712/attachment-0001.bin>


More information about the cfe-commits mailing list