[cfe-commits] r111025 - in /cfe/trunk: include/clang/Frontend/PCHBitCodes.h include/clang/Frontend/PCHReader.h include/clang/Sema/Sema.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp lib/Sema/Sema.cpp lib/Sema/SemaDecl.cpp

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


Author: akirtzidis
Date: Fri Aug 13 13:42:17 2010
New Revision: 111025

URL: http://llvm.org/viewvc/llvm-project?rev=111025&view=rev
Log:
Change Sema's UnusedStaticFuncs to UnusedFileScopedDecls to allow also keeping track of unused file scoped variables.

    This is only preparation, currently only static function definitions are tracked, as before.

Modified:
    cfe/trunk/include/clang/Frontend/PCHBitCodes.h
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp
    cfe/trunk/lib/Sema/Sema.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp

Modified: cfe/trunk/include/clang/Frontend/PCHBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHBitCodes.h?rev=111025&r1=111024&r2=111025&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHBitCodes.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHBitCodes.h Fri Aug 13 13:42:17 2010
@@ -221,8 +221,8 @@
       /// information of the compiler used to build this PCH file.
       VERSION_CONTROL_BRANCH_REVISION = 21,
       
-      /// \brief Record code for the array of unused static functions.
-      UNUSED_STATIC_FUNCS = 22,
+      /// \brief Record code for the array of unused file scoped decls.
+      UNUSED_FILESCOPED_DECLS = 22,
       
       /// \brief Record code for the table of offsets to macro definition
       /// entries in the preprocessing record.

Modified: cfe/trunk/include/clang/Frontend/PCHReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHReader.h?rev=111025&r1=111024&r2=111025&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Fri Aug 13 13:42:17 2010
@@ -381,9 +381,8 @@
   /// file.
   llvm::SmallVector<uint64_t, 16> TentativeDefinitions;
       
-  /// \brief The set of unused static functions stored in the the PCH
-  /// file.
-  llvm::SmallVector<uint64_t, 16> UnusedStaticFuncs;
+  /// \brief The set of unused file scoped decls stored in the the PCH file.
+  llvm::SmallVector<uint64_t, 16> UnusedFileScopedDecls;
   
   /// \brief The set of weak undeclared identifiers stored in the the PCH file.
   llvm::SmallVector<uint64_t, 64> WeakUndeclaredIdentifiers;

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=111025&r1=111024&r2=111025&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Fri Aug 13 13:42:17 2010
@@ -348,8 +348,9 @@
   /// \brief All the tentative definitions encountered in the TU.
   std::vector<VarDecl *> TentativeDefinitions;
 
-  /// \brief The set of static functions seen so far that have not been used.
-  std::vector<FunctionDecl*> UnusedStaticFuncs;
+  /// \brief The set of file scoped decls seen so far that have not been used
+  /// and must warn if not used.
+  std::vector<const DeclaratorDecl*> UnusedFileScopedDecls;
 
   class AccessedEntity {
   public:
@@ -1876,6 +1877,8 @@
                                             MultiStmtArg Handlers);
   void DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock);
 
+  void MarkUnusedFileScopedDecl(const DeclaratorDecl *D);
+
   /// DiagnoseUnusedExprResult - If the statement passed in is an expression
   /// whose result is unused, warn.
   void DiagnoseUnusedExprResult(const Stmt *S);

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=111025&r1=111024&r2=111025&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Fri Aug 13 13:42:17 2010
@@ -1637,13 +1637,13 @@
                                     Record.begin(), Record.end());
       break;
 
-    case pch::UNUSED_STATIC_FUNCS:
+    case pch::UNUSED_FILESCOPED_DECLS:
       // Optimization for the first block.
-      if (UnusedStaticFuncs.empty())
-        UnusedStaticFuncs.swap(Record);
+      if (UnusedFileScopedDecls.empty())
+        UnusedFileScopedDecls.swap(Record);
       else
-        UnusedStaticFuncs.insert(UnusedStaticFuncs.end(),
-                                 Record.begin(), Record.end());
+        UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
+                                     Record.begin(), Record.end());
       break;
 
     case pch::WEAK_UNDECLARED_IDENTIFIERS:
@@ -3150,11 +3150,11 @@
     SemaObj->TentativeDefinitions.push_back(Var);
   }
 
-  // If there were any unused static functions, deserialize them and add to
-  // Sema's list of unused static functions.
-  for (unsigned I = 0, N = UnusedStaticFuncs.size(); I != N; ++I) {
-    FunctionDecl *FD = cast<FunctionDecl>(GetDecl(UnusedStaticFuncs[I]));
-    SemaObj->UnusedStaticFuncs.push_back(FD);
+  // If there were any unused file scoped decls, deserialize them and add to
+  // Sema's list of unused file scoped decls.
+  for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
+    DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
+    SemaObj->UnusedFileScopedDecls.push_back(D);
   }
 
   // If there were any weak undeclared identifiers, deserialize them and add to

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=111025&r1=111024&r2=111025&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Fri Aug 13 13:42:17 2010
@@ -626,7 +626,7 @@
   RECORD(SPECIAL_TYPES);
   RECORD(STATISTICS);
   RECORD(TENTATIVE_DEFINITIONS);
-  RECORD(UNUSED_STATIC_FUNCS);
+  RECORD(UNUSED_FILESCOPED_DECLS);
   RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
   RECORD(SELECTOR_OFFSETS);
   RECORD(METHOD_POOL);
@@ -636,7 +636,6 @@
   RECORD(STAT_CACHE);
   RECORD(EXT_VECTOR_DECLS);
   RECORD(VERSION_CONTROL_BRANCH_REVISION);
-  RECORD(UNUSED_STATIC_FUNCS);
   RECORD(MACRO_DEFINITION_OFFSETS);
   RECORD(CHAINED_METADATA);
   RECORD(REFERENCED_SELECTOR_POOL);
@@ -2204,10 +2203,10 @@
     AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
   }
 
-  // Build a record containing all of the static unused functions in this file.
-  RecordData UnusedStaticFuncs;
-  for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i)
-    AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
+  // Build a record containing all of the file scoped decls in this file.
+  RecordData UnusedFileScopedDecls;
+  for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
+    AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
 
   RecordData WeakUndeclaredIdentifiers;
   if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
@@ -2333,9 +2332,9 @@
   if (!TentativeDefinitions.empty())
     Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
 
-  // Write the record containing unused static functions.
-  if (!UnusedStaticFuncs.empty())
-    Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
+  // Write the record containing unused file scoped decls.
+  if (!UnusedFileScopedDecls.empty())
+    Stream.EmitRecord(pch::UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
 
   // Write the record containing weak undeclared identifiers.
   if (!WeakUndeclaredIdentifiers.empty())
@@ -2436,11 +2435,11 @@
       AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
   }
 
-  // Build a record containing all of the static unused functions in this file.
-  RecordData UnusedStaticFuncs;
-  for (unsigned i=0, e = SemaRef.UnusedStaticFuncs.size(); i !=e; ++i) {
-    if (SemaRef.UnusedStaticFuncs[i]->getPCHLevel() == 0)
-      AddDeclRef(SemaRef.UnusedStaticFuncs[i], UnusedStaticFuncs);
+  // Build a record containing all of the file scoped decls in this file.
+  RecordData UnusedFileScopedDecls;
+  for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
+    if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
+      AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
   }
 
   // We write the entire table, overwriting the tables from the chain.
@@ -2558,9 +2557,9 @@
   if (!TentativeDefinitions.empty())
     Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
 
-  // Write the record containing unused static functions.
-  if (!UnusedStaticFuncs.empty())
-    Stream.EmitRecord(pch::UNUSED_STATIC_FUNCS, UnusedStaticFuncs);
+  // Write the record containing unused file scoped decls.
+  if (!UnusedFileScopedDecls.empty())
+    Stream.EmitRecord(pch::UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
 
   // Write the record containing weak undeclared identifiers.
   if (!WeakUndeclaredIdentifiers.empty())

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=111025&r1=111024&r2=111025&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Fri Aug 13 13:42:17 2010
@@ -262,12 +262,12 @@
         break;
     }
   
-  // Remove functions that turned out to be used.
-  UnusedStaticFuncs.erase(std::remove_if(UnusedStaticFuncs.begin(), 
-                                         UnusedStaticFuncs.end(), 
-                             std::bind2nd(std::mem_fun(&FunctionDecl::isUsed),
+  // 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)), 
-                          UnusedStaticFuncs.end());
+                              UnusedFileScopedDecls.end());
 
   if (!CompleteTranslationUnit)
     return;
@@ -330,14 +330,16 @@
 
   }
   
-  // Output warning for unused functions.
-  for (std::vector<FunctionDecl*>::iterator
-       F = UnusedStaticFuncs.begin(),
-       FEnd = UnusedStaticFuncs.end();
-       F != FEnd;
-       ++F)
-    Diag((*F)->getLocation(), diag::warn_unused_function) << (*F)->getDeclName();
-  
+  // Output warning for unused file scoped decls.
+  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
+      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=111025&r1=111024&r2=111025&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Aug 13 13:42:17 2010
@@ -521,6 +521,26 @@
   F.done();
 }
 
+static bool ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) {
+  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>());
+  }
+  
+  return false;
+}
+
+void Sema::MarkUnusedFileScopedDecl(const DeclaratorDecl *D) {
+  if (ShouldWarnIfUnusedFileScopedDecl(D))
+    UnusedFileScopedDecls.push_back(D);
+}
+
 static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) {
   if (D->isInvalidDecl())
     return false;
@@ -3618,17 +3638,9 @@
   if (FunctionTemplate)
     return FunctionTemplate;
 
-  
-  // Keep track of static, non-inlined function definitions that
-  // have not been used. We will warn later.
-  // FIXME: Also include static functions declared but not defined.
-  if (!NewFD->isInvalidDecl() && IsFunctionDefinition 
-      && !NewFD->isInlined() && NewFD->getLinkage() == InternalLinkage
-      && !NewFD->isUsed() && !NewFD->hasAttr<UnusedAttr>()
-      && !NewFD->hasAttr<ConstructorAttr>()
-      && !NewFD->hasAttr<DestructorAttr>())
-    UnusedStaticFuncs.push_back(NewFD);
-  
+  if (IsFunctionDefinition)
+    MarkUnusedFileScopedDecl(NewFD);
+
   return NewFD;
 }
 
@@ -4891,7 +4903,7 @@
   // deletion in some later function.
   if (getDiagnostics().hasErrorOccurred())
     ExprTemporaries.clear();
-  
+
   return D;
 }
 





More information about the cfe-commits mailing list