[cfe-commits] r111026 - in /cfe/trunk: include/clang/Sema/Sema.h lib/Sema/Sema.cpp lib/Sema/SemaDecl.cpp lib/Sema/SemaTemplateInstantiateDecl.cpp test/Sema/warn-unused-function.c test/SemaCXX/warn-unused-filescoped.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Fri Aug 13 11:42:29 PDT 2010


Author: akirtzidis
Date: Fri Aug 13 13:42:29 2010
New Revision: 111026

URL: http://llvm.org/viewvc/llvm-project?rev=111026&view=rev
Log:
Expand the unused warnings for functions. Warn for:

-static function declarations
-functions in anonymous namespace
-class methods in anonymous namespace
-class method specializations in anonymous namespace
-function specializations in anonymous namespace

Added:
    cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp
Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/Sema.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
    cfe/trunk/test/Sema/warn-unused-function.c

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=111026&r1=111025&r2=111026&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Fri Aug 13 13:42:29 2010
@@ -349,7 +349,8 @@
   std::vector<VarDecl *> TentativeDefinitions;
 
   /// \brief The set of file scoped decls seen so far that have not been used
-  /// and must warn if not used.
+  /// and must warn if not used. For functions only contains the first
+  /// declaration.
   std::vector<const DeclaratorDecl*> UnusedFileScopedDecls;
 
   class AccessedEntity {
@@ -1877,6 +1878,7 @@
                                             MultiStmtArg Handlers);
   void DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock);
 
+  bool ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const;
   void MarkUnusedFileScopedDecl(const DeclaratorDecl *D);
 
   /// DiagnoseUnusedExprResult - If the statement passed in is an expression

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=111026&r1=111025&r2=111026&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Fri Aug 13 13:42:29 2010
@@ -235,6 +235,24 @@
 void Sema::DeleteStmt(StmtTy *S) {
 }
 
+/// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
+static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
+  if (D->isUsed())
+    return true;
+
+  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+    // For functions, UnusedFileScopedDecls stores the first declaration.
+    // Later redecls may add new information resulting in not having to warn,
+    // so check again.
+    const FunctionDecl *DeclToCheck;
+    if (!FD->hasBody(DeclToCheck))
+      DeclToCheck = FD->getMostRecentDeclaration();
+    if (DeclToCheck != FD)
+      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
+  }
+  return false;
+}
+
 /// ActOnEndOfTranslationUnit - This is called at the very end of the
 /// translation unit when EOF is reached and all but the top-level scope is
 /// popped.
@@ -263,10 +281,10 @@
     }
   
   // Remove file scoped decls that turned out to be used.
-  UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(), 
-                                             UnusedFileScopedDecls.end(), 
-                             std::bind2nd(std::mem_fun(&DeclaratorDecl::isUsed),
-                                          true)), 
+  UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(),
+                                             UnusedFileScopedDecls.end(),
+                              std::bind1st(std::ptr_fun(ShouldRemoveFromUnused),
+                                           this)),
                               UnusedFileScopedDecls.end());
 
   if (!CompleteTranslationUnit)
@@ -334,9 +352,13 @@
   for (std::vector<const DeclaratorDecl*>::iterator
          I = UnusedFileScopedDecls.begin(),
          E = UnusedFileScopedDecls.end(); I != E; ++I) {
-    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
-      Diag(FD->getLocation(), diag::warn_unused_function) << FD->getDeclName();
-    else
+    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
+      const FunctionDecl *DiagD;
+      if (!FD->hasBody(DiagD))
+        DiagD = FD;
+      Diag(DiagD->getLocation(), diag::warn_unused_function)
+            << DiagD->getDeclName();
+    } else
       Diag((*I)->getLocation(), diag::warn_unused_variable)
             << cast<VarDecl>(*I)->getDeclName();
   }

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=111026&r1=111025&r2=111026&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Aug 13 13:42:29 2010
@@ -521,25 +521,40 @@
   F.done();
 }
 
-static bool ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) {
+bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
+  assert(D);
+  if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>())
+    return false;
+  if (D->getLinkage() == ExternalLinkage)
+    return false;
+
+  // Ignore class templates.
+  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D))
+    if (MD->getParent()->getDescribedClassTemplate())
+      return false;
+
+  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+    if (FD->isThisDeclarationADefinition())
+      return !Context.DeclMustBeEmitted(FD);
+    return true;
+   }
+
+   return false;
+ }
+
+ void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) {
+  if (!D)
+    return;
+
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
-    // Warn for static, non-inlined function definitions that
-    // have not been used.
-    // FIXME: Also include static functions declared but not defined.
-    return (!FD->isInvalidDecl() 
-         && !FD->isInlined() && FD->getLinkage() == InternalLinkage
-         && !FD->isUsed() && !FD->hasAttr<UnusedAttr>()
-         && !FD->hasAttr<ConstructorAttr>()
-         && !FD->hasAttr<DestructorAttr>());
+    const FunctionDecl *First = FD->getFirstDeclaration();
+    if (FD != First && ShouldWarnIfUnusedFileScopedDecl(First))
+      return; // First should already be in the vector.
   }
-  
-  return false;
-}
 
-void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) {
-  if (ShouldWarnIfUnusedFileScopedDecl(D))
-    UnusedFileScopedDecls.push_back(D);
-}
+   if (ShouldWarnIfUnusedFileScopedDecl(D))
+     UnusedFileScopedDecls.push_back(D);
+ }
 
 static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) {
   if (D->isInvalidDecl())
@@ -3638,8 +3653,7 @@
   if (FunctionTemplate)
     return FunctionTemplate;
 
-  if (IsFunctionDefinition)
-    MarkUnusedFileScopedDecl(NewFD);
+  MarkUnusedFileScopedDecl(NewFD);
 
   return NewFD;
 }

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=111026&r1=111025&r2=111026&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Fri Aug 13 13:42:29 2010
@@ -1204,6 +1204,8 @@
       PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
     PrincipalDecl->setNonMemberOperator();
 
+  SemaRef.MarkUnusedFileScopedDecl(Function);
+
   return Function;
 }
 
@@ -1415,6 +1417,8 @@
     else
       Owner->addDecl(DeclToAdd);
   }
+  
+  SemaRef.MarkUnusedFileScopedDecl(Method);
 
   return Method;
 }

Modified: cfe/trunk/test/Sema/warn-unused-function.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unused-function.c?rev=111026&r1=111025&r2=111026&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unused-function.c (original)
+++ cfe/trunk/test/Sema/warn-unused-function.c Fri Aug 13 13:42:29 2010
@@ -35,3 +35,12 @@
 
 __attribute__((destructor)) static void bar3(void);
 void bar3(void) { }
+
+static void f10(void); // expected-warning{{unused}}
+static void f10(void);
+
+static void f11(void);
+static void f11(void) { }  // expected-warning{{unused}}
+
+static void f12(void) { }  // expected-warning{{unused}}
+static void f12(void);

Added: cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp?rev=111026&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp Fri Aug 13 13:42:29 2010
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wunused %s
+
+static void f1(); // expected-warning{{unused}}
+
+namespace {
+  void f2();  // expected-warning{{unused}}
+
+  void f3() { }  // expected-warning{{unused}}
+
+  struct S {
+    void m1() { }  // expected-warning{{unused}}
+    void m2();  // expected-warning{{unused}}
+    void m3();
+  };
+
+  template <typename T>
+  struct TS {
+    void m();
+  };
+  template <> void TS<int>::m() { }  // expected-warning{{unused}}
+
+  template <typename T>
+  void tf() { }
+  template <> void tf<int>() { }  // expected-warning{{unused}}
+}
+
+void S::m3() { }  // expected-warning{{unused}}





More information about the cfe-commits mailing list