r174034 - Remove elements from Sema.UndefinedInternals as functions are defined. Also

Nick Lewycky nicholas at mxc.ca
Wed Jan 30 19:23:57 PST 2013


Author: nicholas
Date: Wed Jan 30 21:23:57 2013
New Revision: 174034

URL: http://llvm.org/viewvc/llvm-project?rev=174034&view=rev
Log:
Remove elements from Sema.UndefinedInternals as functions are defined. Also
filter the elements before emitting them into a PCH. No user-visible
functionality change, except that PCH files may be smaller?

Modified:
    cfe/trunk/include/clang/Sema/ExternalSemaSource.h
    cfe/trunk/include/clang/Sema/MultiplexExternalSemaSource.h
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/include/clang/Serialization/ASTReader.h
    cfe/trunk/lib/Sema/MultiplexExternalSemaSource.cpp
    cfe/trunk/lib/Sema/Sema.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Serialization/ASTReader.cpp
    cfe/trunk/lib/Serialization/ASTWriter.cpp

Modified: cfe/trunk/include/clang/Sema/ExternalSemaSource.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/ExternalSemaSource.h?rev=174034&r1=174033&r2=174034&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/ExternalSemaSource.h (original)
+++ cfe/trunk/include/clang/Sema/ExternalSemaSource.h Wed Jan 30 21:23:57 2013
@@ -68,7 +68,7 @@ public:
                            SmallVectorImpl<NamespaceDecl *> &Namespaces);
 
   virtual void ReadUndefinedInternals(
-                        llvm::MapVector<NamedDecl*, SourceLocation> &Undefined);
+                         llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined);
   
   /// \brief Do last resort, unqualified lookup on a LookupResult that
   /// Sema cannot find.

Modified: cfe/trunk/include/clang/Sema/MultiplexExternalSemaSource.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/MultiplexExternalSemaSource.h?rev=174034&r1=174033&r2=174034&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/MultiplexExternalSemaSource.h (original)
+++ cfe/trunk/include/clang/Sema/MultiplexExternalSemaSource.h Wed Jan 30 21:23:57 2013
@@ -254,7 +254,7 @@ public:
   virtual void ReadKnownNamespaces(SmallVectorImpl<NamespaceDecl*> &Namespaces);
 
   virtual void ReadUndefinedInternals(
-                        llvm::MapVector<NamedDecl*, SourceLocation> &Undefined);
+                         llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined);
   
   /// \brief Do last resort, unqualified lookup on a LookupResult that
   /// Sema cannot find.

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=174034&r1=174033&r2=174034&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Wed Jan 30 21:23:57 2013
@@ -736,11 +736,15 @@ public:
 
   // Contains the locations of the beginning of unparsed default
   // argument locations.
-  llvm::DenseMap<ParmVarDecl *,SourceLocation> UnparsedDefaultArgLocs;
+  llvm::DenseMap<ParmVarDecl *, SourceLocation> UnparsedDefaultArgLocs;
 
   /// UndefinedInternals - all the used, undefined objects with
   /// internal linkage in this translation unit.
-  llvm::MapVector<NamedDecl*, SourceLocation> UndefinedInternals;
+  llvm::DenseMap<NamedDecl *, SourceLocation> UndefinedInternals;
+
+  /// Obtain a sorted list of functions that are undefined but ODR-used.
+  void getUndefinedInternals(
+    llvm::SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined);
 
   typedef std::pair<ObjCMethodList, ObjCMethodList> GlobalMethods;
   typedef llvm::DenseMap<Selector, GlobalMethods> GlobalMethodPool;

Modified: cfe/trunk/include/clang/Serialization/ASTReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTReader.h?rev=174034&r1=174033&r2=174034&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTReader.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTReader.h Wed Jan 30 21:23:57 2013
@@ -1521,7 +1521,7 @@ public:
                            SmallVectorImpl<NamespaceDecl *> &Namespaces);
 
   virtual void ReadUndefinedInternals(
-                       llvm::MapVector<NamedDecl *, SourceLocation> &Undefined);
+                        llvm::DenseMap<NamedDecl *, SourceLocation> &Undefined);
 
   virtual void ReadTentativeDefinitions(
                  SmallVectorImpl<VarDecl *> &TentativeDefs);

Modified: cfe/trunk/lib/Sema/MultiplexExternalSemaSource.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/MultiplexExternalSemaSource.cpp?rev=174034&r1=174033&r2=174034&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/MultiplexExternalSemaSource.cpp (original)
+++ cfe/trunk/lib/Sema/MultiplexExternalSemaSource.cpp Wed Jan 30 21:23:57 2013
@@ -202,7 +202,7 @@ void MultiplexExternalSemaSource::ReadKn
 }
 
 void MultiplexExternalSemaSource::ReadUndefinedInternals(
-                        llvm::MapVector<NamedDecl*, SourceLocation> &Undefined){
+                         llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined){
   for(size_t i = 0; i < Sources.size(); ++i)
     Sources[i]->ReadUndefinedInternals(Undefined);
 }

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=174034&r1=174033&r2=174034&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Wed Jan 30 21:23:57 2013
@@ -366,46 +366,70 @@ static bool ShouldRemoveFromUnused(Sema 
 }
 
 namespace {
-  struct UndefinedInternal {
-    NamedDecl *decl;
-    FullSourceLoc useLoc;
-
-    UndefinedInternal(NamedDecl *decl, FullSourceLoc useLoc)
-      : decl(decl), useLoc(useLoc) {}
+  struct SortUndefinedInternal {
+    const SourceManager &SM;
+    explicit SortUndefinedInternal(SourceManager &SM) : SM(SM) {}
+
+    bool operator()(const std::pair<NamedDecl *, SourceLocation> &l,
+                    const std::pair<NamedDecl *, SourceLocation> &r) const {
+      if (l.second != r.second)
+        return SM.isBeforeInTranslationUnit(l.second, r.second);
+      return SM.isBeforeInTranslationUnit(l.first->getLocation(),
+                                          r.first->getLocation());
+    }
   };
 }
 
-/// checkUndefinedInternals - Check for undefined objects with internal linkage.
-static void checkUndefinedInternals(Sema &S) {
-  if (S.UndefinedInternals.empty()) return;
-
-  // Collect all the still-undefined entities with internal linkage.
-  SmallVector<UndefinedInternal, 16> undefined;
-  for (llvm::MapVector<NamedDecl*,SourceLocation>::iterator
-         i = S.UndefinedInternals.begin(), e = S.UndefinedInternals.end();
-       i != e; ++i) {
-    NamedDecl *decl = i->first;
+/// Obtains a sorted list of functions that are undefined but ODR-used.
+void Sema::getUndefinedInternals(
+    SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined) {
+  for (llvm::DenseMap<NamedDecl *, SourceLocation>::iterator
+         I = UndefinedInternals.begin(), E = UndefinedInternals.end();
+       I != E; ++I) {
+    NamedDecl *ND = I->first;
 
     // Ignore attributes that have become invalid.
-    if (decl->isInvalidDecl()) continue;
+    if (ND->isInvalidDecl()) continue;
 
     // If we found out that the decl is external, don't warn.
-    if (decl->getLinkage() == ExternalLinkage) continue;
+    if (ND->getLinkage() == ExternalLinkage) continue;
 
     // __attribute__((weakref)) is basically a definition.
-    if (decl->hasAttr<WeakRefAttr>()) continue;
+    if (ND->hasAttr<WeakRefAttr>()) continue;
 
-    if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) {
-      if (fn->isDefined())
+    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
+      if (FD->isDefined())
         continue;
     } else {
-      if (cast<VarDecl>(decl)->hasDefinition() != VarDecl::DeclarationOnly)
+      if (cast<VarDecl>(ND)->hasDefinition() != VarDecl::DeclarationOnly)
         continue;
     }
 
-    S.Diag(decl->getLocation(), diag::warn_undefined_internal)
-      << isa<VarDecl>(decl) << decl;
-    S.Diag(i->second, diag::note_used_here);
+    Undefined.push_back(std::make_pair(ND, I->second));
+  }
+
+  // Sort (in order of use site) so that we're not (as) dependent on
+  // the iteration order through an llvm::DenseMap.
+  std::sort(Undefined.begin(), Undefined.end(),
+            SortUndefinedInternal(Context.getSourceManager()));
+}
+
+/// checkUndefinedInternals - Check for undefined objects with internal linkage.
+static void checkUndefinedInternals(Sema &S) {
+  if (S.UndefinedInternals.empty()) return;
+
+  // Collect all the still-undefined entities with internal linkage.
+  SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
+  S.getUndefinedInternals(Undefined);
+  if (Undefined.empty()) return;
+
+  for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
+         I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
+    NamedDecl *ND = I->first;
+
+    S.Diag(ND->getLocation(), diag::warn_undefined_internal)
+      << isa<VarDecl>(ND) << ND;
+    S.Diag(I->second, diag::note_used_here);
   }
 }
 
@@ -1065,7 +1089,7 @@ void ExternalSemaSource::ReadKnownNamesp
 }
 
 void ExternalSemaSource::ReadUndefinedInternals(
-                      llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
+                       llvm::DenseMap<NamedDecl *, SourceLocation> &Undefined) {
 }
 
 void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const {

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=174034&r1=174033&r2=174034&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Jan 30 21:23:57 2013
@@ -8428,6 +8428,13 @@ Decl *Sema::ActOnFinishFunctionBody(Decl
   if (FD) {
     FD->setBody(Body);
 
+    // The only way to be included in UndefinedInternals is if there is an
+    // ODR-use before the definition. Avoid the expensive map lookup if this
+    // is the first declaration.
+    if (FD->getPreviousDecl() != 0 && FD->getPreviousDecl()->isUsed() &&
+        FD->getLinkage() != ExternalLinkage)
+      UndefinedInternals.erase(FD);
+
     // If the function implicitly returns zero (like 'main') or is naked,
     // don't complain about missing return statements.
     if (FD->hasImplicitReturnZero() || FD->hasAttr<NakedAttr>())

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=174034&r1=174033&r2=174034&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Wed Jan 30 21:23:57 2013
@@ -1,4 +1,4 @@
-//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
+//===--- ASTReader.cpp - AST File Reader ----------------------------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -5963,7 +5963,7 @@ void ASTReader::ReadKnownNamespaces(
 }
 
 void ASTReader::ReadUndefinedInternals(
-                       llvm::MapVector<NamedDecl*, SourceLocation> &Undefined) {
+                        llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
   for (unsigned Idx = 0, N = UndefinedInternals.size(); Idx != N;) {
     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedInternals[Idx++]));
     SourceLocation Loc =

Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=174034&r1=174033&r2=174034&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Wed Jan 30 21:23:57 2013
@@ -3590,10 +3590,11 @@ void ASTWriter::WriteASTCore(Sema &SemaR
 
   // Build a record of all used, undefined objects with internal linkage.
   RecordData UndefinedInternals;
-  for (llvm::MapVector<NamedDecl*, SourceLocation>::iterator
-            I = SemaRef.UndefinedInternals.begin(),
-         IEnd = SemaRef.UndefinedInternals.end();
-       I != IEnd; ++I) {
+
+  SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
+  SemaRef.getUndefinedInternals(Undefined);
+  for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
+         I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
     AddDeclRef(I->first, UndefinedInternals);
     AddSourceLocation(I->second, UndefinedInternals);
   }





More information about the cfe-commits mailing list