]> granicus.if.org Git - clang/commitdiff
patch to suggest 'static' function should be 'static inline'
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 27 Jun 2012 19:43:29 +0000 (19:43 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 27 Jun 2012 19:43:29 +0000 (19:43 +0000)
when it appears to be unused and occurs in a header.
// rdar://11202617

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@159282 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/Sema.cpp
test/SemaCXX/warn-static-function-inheader.cpp [new file with mode: 0644]
test/SemaCXX/warn-static-function-inheader.h [new file with mode: 0644]

index 38a22a7fce32bc9877759d99f414d5fb31157359..fb5405025438325beb1b88bf746d34ad40f77f19 100644 (file)
@@ -170,6 +170,10 @@ def warn_used_but_marked_unused: Warning<"%0 was marked unused but was used">,
 def warn_unneeded_internal_decl : Warning<
   "%select{function|variable}0 %1 is not needed and will not be emitted">,
   InGroup<UnneededInternalDecl>, DefaultIgnore;
+def warn_unneeded_static_internal_decl : Warning<
+  "'static' function %0 declared in header file "
+  "should be declared 'static inline'">,
+  InGroup<UnneededInternalDecl>, DefaultIgnore;
 def warn_unneeded_member_function : Warning<
   "member function %0 is not needed and will not be emitted">,
   InGroup<UnneededMemberFunction>, DefaultIgnore;
index 9d18a5c59638957d888181b4a33585f61fb6fafd..42d5fb7481099fbe84cf9905cbbb2d02fe21e7db 100644 (file)
@@ -682,9 +682,17 @@ void Sema::ActOnEndOfTranslationUnit() {
           if (isa<CXXMethodDecl>(DiagD))
             Diag(DiagD->getLocation(), diag::warn_unneeded_member_function)
                   << DiagD->getDeclName();
-          else
-            Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
-                  << /*function*/0 << DiagD->getDeclName();
+          else {
+            if (FD->getStorageClassAsWritten() == SC_Static &&
+                !FD->isInlineSpecified() &&
+                !SourceMgr.isFromMainFile(
+                   SourceMgr.getExpansionLoc(FD->getLocation())))
+              Diag(DiagD->getLocation(), diag::warn_unneeded_static_internal_decl)
+                << DiagD->getDeclName();
+            else
+              Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
+                   << /*function*/0 << DiagD->getDeclName();
+          }
         } else {
           Diag(DiagD->getLocation(),
                isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function
diff --git a/test/SemaCXX/warn-static-function-inheader.cpp b/test/SemaCXX/warn-static-function-inheader.cpp
new file mode 100644 (file)
index 0000000..30386d9
--- /dev/null
@@ -0,0 +1,12 @@
+#include "warn-static-function-inheader.h"
+// RUN: %clang_cc1 -fsyntax-only -verify -Wall %s
+// rdar://11202617
+
+static void another(void) { // expected-warning {{function 'another' is not needed and will not be emitted}}
+}
+
+template <typename T>
+void foo(void) {
+        thing();
+       another();
+}
diff --git a/test/SemaCXX/warn-static-function-inheader.h b/test/SemaCXX/warn-static-function-inheader.h
new file mode 100644 (file)
index 0000000..1c9e00b
--- /dev/null
@@ -0,0 +1,3 @@
+static void thing(void) { // expected-warning {{'static' function 'thing' declared in header file should be declared 'static inline'}}
+}
+