r174895 - [Modules] Cope better with top-level declarations loaded after being declared in the current translation unit <rdar://problem/13189985>.

Douglas Gregor dgregor at apple.com
Mon Feb 11 10:16:19 PST 2013


Author: dgregor
Date: Mon Feb 11 12:16:18 2013
New Revision: 174895

URL: http://llvm.org/viewvc/llvm-project?rev=174895&view=rev
Log:
[Modules] Cope better with top-level declarations loaded after being declared in the current translation unit <rdar://problem/13189985>.

These two related tweaks to keep the information associated with a
given identifier correct when the identifier has been given some
top-level information (say, a top-level declaration) and more
information is then loaded from a module. The first ensures that an
identifier that was "interesting" before being loaded from an AST is
considered to be different from its on-disk counterpart. Otherwise, we
lose such changes when writing the current translation unit as a
module.

Second, teach the code that injects AST-loaded names into the
identifier chain for name lookup to keep the most recent declaration,
so that we don't end up confusing our declaration chains by having a
different declaration in there.

Added:
    cfe/trunk/test/Modules/Inputs/redecl-merge-bottom-prefix.h
    cfe/trunk/test/Modules/redecl-merge2.m
Modified:
    cfe/trunk/lib/Sema/IdentifierResolver.cpp
    cfe/trunk/lib/Serialization/ASTReader.cpp
    cfe/trunk/test/Modules/Inputs/module.map
    cfe/trunk/test/Modules/Inputs/redecl-merge-bottom.h
    cfe/trunk/test/Modules/Inputs/redecl-merge-left.h

Modified: cfe/trunk/lib/Sema/IdentifierResolver.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/IdentifierResolver.cpp?rev=174895&r1=174894&r2=174895&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/IdentifierResolver.cpp (original)
+++ cfe/trunk/lib/Sema/IdentifierResolver.cpp Mon Feb 11 12:16:18 2013
@@ -302,6 +302,14 @@ static DeclMatchKind compareDeclarations
 
   // If the declarations are redeclarations of each other, keep the newest one.
   if (Existing->getCanonicalDecl() == New->getCanonicalDecl()) {
+    // If either of these is the most recent declaration, use it.
+    Decl *MostRecent = Existing->getMostRecentDecl();
+    if (Existing == MostRecent)
+      return DMK_Ignore;
+
+    if (New == MostRecent)
+      return DMK_Replace;
+
     // If the existing declaration is somewhere in the previous declaration
     // chain of the new declaration, then prefer the new declaration.
     for (Decl::redecl_iterator RD = New->redecls_begin(), 

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=174895&r1=174894&r2=174895&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Mon Feb 11 12:16:18 2013
@@ -457,6 +457,16 @@ ASTIdentifierLookupTraitBase::ReadKey(co
   return StringRef((const char*) d, n-1);
 }
 
+/// \brief Whether the given identifier is "interesting".
+static bool isInterestingIdentifier(IdentifierInfo &II) {
+  return II.isPoisoned() ||
+         II.isExtensionToken() ||
+         II.getObjCOrBuiltinID() ||
+         II.hasRevertedTokenIDToIdentifier() ||
+         II.hadMacroDefinition() ||
+         II.getFETokenInfo<void>();
+}
+
 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
                                                    const unsigned char* d,
                                                    unsigned DataLen) {
@@ -477,8 +487,13 @@ IdentifierInfo *ASTIdentifierLookupTrait
       KnownII = II;
     }
     Reader.SetIdentifierInfo(ID, II);
-    II->setIsFromAST();
-    Reader.markIdentifierUpToDate(II);    
+    if (!II->isFromAST()) {
+      bool WasInteresting = isInterestingIdentifier(*II);
+      II->setIsFromAST();
+      if (WasInteresting)
+        II->setChangedSinceDeserialization();
+    }
+    Reader.markIdentifierUpToDate(II);
     return II;
   }
 
@@ -506,7 +521,12 @@ IdentifierInfo *ASTIdentifierLookupTrait
     KnownII = II;
   }
   Reader.markIdentifierUpToDate(II);
-  II->setIsFromAST();
+  if (!II->isFromAST()) {
+    bool WasInteresting = isInterestingIdentifier(*II);
+    II->setIsFromAST();
+    if (WasInteresting)
+      II->setChangedSinceDeserialization();
+  }
 
   // Set or check the various bits in the IdentifierInfo structure.
   // Token IDs are read-only.

Modified: cfe/trunk/test/Modules/Inputs/module.map
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/module.map?rev=174895&r1=174894&r2=174895&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/module.map (original)
+++ cfe/trunk/test/Modules/Inputs/module.map Mon Feb 11 12:16:18 2013
@@ -86,6 +86,10 @@ module redecl_merge_right {
   export *
 }
 module redecl_merge_bottom { 
+  explicit module prefix {
+    header "redecl-merge-bottom-prefix.h"
+  }
+
   header "redecl-merge-bottom.h" 
   export *
 }

Added: cfe/trunk/test/Modules/Inputs/redecl-merge-bottom-prefix.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/redecl-merge-bottom-prefix.h?rev=174895&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/redecl-merge-bottom-prefix.h (added)
+++ cfe/trunk/test/Modules/Inputs/redecl-merge-bottom-prefix.h Mon Feb 11 12:16:18 2013
@@ -0,0 +1,4 @@
+// A class that is declared in the 'bottom' module, then loaded from
+// one of the modules it depends on. It needs to be visible when this
+// module is loaded.
+ at class DeclaredThenLoaded;

Modified: cfe/trunk/test/Modules/Inputs/redecl-merge-bottom.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/redecl-merge-bottom.h?rev=174895&r1=174894&r2=174895&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/redecl-merge-bottom.h (original)
+++ cfe/trunk/test/Modules/Inputs/redecl-merge-bottom.h Mon Feb 11 12:16:18 2013
@@ -18,3 +18,8 @@ struct S3;
 
 void refers_to_C4(C4*);
 
+ at interface UnrelatedToDeclaredThenLoaded
+- declaredThenLoadedMethod;
+ at end
+
+ at class DeclaredThenLoaded;

Modified: cfe/trunk/test/Modules/Inputs/redecl-merge-left.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/redecl-merge-left.h?rev=174895&r1=174894&r2=174895&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/redecl-merge-left.h (original)
+++ cfe/trunk/test/Modules/Inputs/redecl-merge-left.h Mon Feb 11 12:16:18 2013
@@ -82,3 +82,11 @@ extern double var3;
 // top level.
 typedef void funcptr_with_id(int id);
 
+// A class that is declared in the 'bottom' module, then loaded from
+// one of the modules it depends on.
+ at interface DeclaredThenLoaded
+- declaredThenLoadedMethod;
+ at end
+
+ at class DeclaredThenLoaded;
+

Added: cfe/trunk/test/Modules/redecl-merge2.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/redecl-merge2.m?rev=174895&view=auto
==============================================================================
--- cfe/trunk/test/Modules/redecl-merge2.m (added)
+++ cfe/trunk/test/Modules/redecl-merge2.m Mon Feb 11 12:16:18 2013
@@ -0,0 +1,8 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -fmodules -fmodules-cache-path=%t -I %S/Inputs %s -verify -Wno-objc-root-class
+// expected-no-diagnostics
+
+ at import redecl_merge_bottom.prefix;
+
+DeclaredThenLoaded *dtl;
+





More information about the cfe-commits mailing list