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

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


Author: akirtzidis
Date: Fri Aug 13 13:42:40 2010
New Revision: 111027

URL: http://llvm.org/viewvc/llvm-project?rev=111027&view=rev
Log:
The unused warnings extravaganza continues. Warn for:

-static variables
-variables in anonymous namespace (fixes rdar://7794535)
-static data members in anonymous namespace
-static data members specializations in anonymous namespace

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/SemaCXX/warn-unused-filescoped.cpp

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=111027&r1=111026&r2=111027&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Fri Aug 13 13:42:40 2010
@@ -349,8 +349,7 @@
   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. For functions only contains the first
-  /// declaration.
+  /// and must warn if not used. Only contains the first declaration.
   std::vector<const DeclaratorDecl*> UnusedFileScopedDecls;
 
   class AccessedEntity {

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=111027&r1=111026&r2=111027&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Fri Aug 13 13:42:40 2010
@@ -241,15 +241,33 @@
     return true;
 
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
-    // For functions, UnusedFileScopedDecls stores the first declaration.
+    // UnusedFileScopedDecls stores the first declaration.
+    // The declaration may have become definition so check again.
+    const FunctionDecl *DeclToCheck;
+    if (FD->hasBody(DeclToCheck))
+      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
+
     // 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();
+    DeclToCheck = FD->getMostRecentDeclaration();
     if (DeclToCheck != FD)
       return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
   }
+
+  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
+    // UnusedFileScopedDecls stores the first declaration.
+    // The declaration may have become definition so check again.
+    const VarDecl *DeclToCheck = VD->getDefinition(); 
+    if (DeclToCheck)
+      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
+
+    // Later redecls may add new information resulting in not having to warn,
+    // so check again.
+    DeclToCheck = VD->getMostRecentDeclaration();
+    if (DeclToCheck != VD)
+      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
+  }
+
   return false;
 }
 
@@ -358,9 +376,13 @@
         DiagD = FD;
       Diag(DiagD->getLocation(), diag::warn_unused_function)
             << DiagD->getDeclName();
-    } else
-      Diag((*I)->getLocation(), diag::warn_unused_variable)
-            << cast<VarDecl>(*I)->getDeclName();
+    } else {
+      const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
+      if (!DiagD)
+        DiagD = cast<VarDecl>(*I);
+      Diag(DiagD->getLocation(), diag::warn_unused_variable)
+            << DiagD->getDeclName();
+    }
   }
 }
 

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=111027&r1=111026&r2=111027&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Aug 13 13:42:40 2010
@@ -523,15 +523,17 @@
 
 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 (D->getDeclContext()->isDependentContext())
+    return false;
+
+  // We warn for unused decls internal to the translation unit.
+  if (D->getLinkage() == ExternalLinkage)
+    return false;
 
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     if (FD->isThisDeclarationADefinition())
@@ -539,6 +541,10 @@
     return true;
    }
 
+  if (const VarDecl *VD = dyn_cast<VarDecl>(D))
+    if (VD->isFileVarDecl())
+      return !Context.DeclMustBeEmitted(VD);
+
    return false;
  }
 
@@ -552,6 +558,12 @@
       return; // First should already be in the vector.
   }
 
+  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
+    const VarDecl *First = VD->getFirstDeclaration();
+    if (VD != First && ShouldWarnIfUnusedFileScopedDecl(First))
+      return; // First should already be in the vector.
+  }
+
    if (ShouldWarnIfUnusedFileScopedDecl(D))
      UnusedFileScopedDecls.push_back(D);
  }
@@ -2758,6 +2770,8 @@
   if (NewVD->getLinkage() == ExternalLinkage && !DC->isRecord())
     AddPushedVisibilityAttribute(NewVD);
 
+  MarkUnusedFileScopedDecl(NewVD);
+
   return NewVD;
 }
 

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=111027&r1=111026&r2=111027&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Fri Aug 13 13:42:40 2010
@@ -449,7 +449,9 @@
   // Diagnose unused local variables.
   if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
     SemaRef.DiagnoseUnusedDecl(Var);
-  
+
+  SemaRef.MarkUnusedFileScopedDecl(Var);
+
   return Var;
 }
 

Modified: cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp?rev=111027&r1=111026&r2=111027&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-filescoped.cpp Fri Aug 13 13:42:40 2010
@@ -25,3 +25,19 @@
 }
 
 void S::m3() { }  // expected-warning{{unused}}
+
+static int x1;  // expected-warning{{unused}}
+
+namespace {
+  int x2;  // expected-warning{{unused}}
+  
+  struct S2 {
+    static int x;  // expected-warning{{unused}}
+  };
+
+  template <typename T>
+  struct TS2 {
+    static int x;
+  };
+  template <> int TS2<int>::x;  // expected-warning{{unused}}
+}





More information about the cfe-commits mailing list