[cfe-commits] r147535 - in /cfe/trunk: lib/Serialization/ASTReaderDecl.cpp test/Modules/Inputs/redecl-merge-left.h test/Modules/Inputs/redecl-merge-right.h test/Modules/redecl-merge.m

Douglas Gregor dgregor at apple.com
Wed Jan 4 09:21:36 PST 2012


Author: dgregor
Date: Wed Jan  4 11:21:36 2012
New Revision: 147535

URL: http://llvm.org/viewvc/llvm-project?rev=147535&view=rev
Log:
Implement declaration merging for variables in disjoint modules.

Modified:
    cfe/trunk/lib/Serialization/ASTReaderDecl.cpp
    cfe/trunk/test/Modules/Inputs/redecl-merge-left.h
    cfe/trunk/test/Modules/Inputs/redecl-merge-right.h
    cfe/trunk/test/Modules/redecl-merge.m

Modified: cfe/trunk/lib/Serialization/ASTReaderDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReaderDecl.cpp?rev=147535&r1=147534&r2=147535&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReaderDecl.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReaderDecl.cpp Wed Jan  4 11:21:36 2012
@@ -880,8 +880,12 @@
 }
 
 void ASTDeclReader::VisitVarDecl(VarDecl *VD) {
-  VisitRedeclarable(VD);
+  // Record the declaration -> global ID mapping.
+  Reader.DeclToID[VD] = ThisDeclID;
+  
+  RedeclarableResult Redecl = VisitRedeclarable(VD);
   VisitDeclaratorDecl(VD);
+  
   VD->VarDeclBits.SClass = (StorageClass)Record[Idx++];
   VD->VarDeclBits.SClassAsWritten = (StorageClass)Record[Idx++];
   VD->VarDeclBits.ThreadSpecified = Record[Idx++];
@@ -890,6 +894,9 @@
   VD->VarDeclBits.NRVOVariable = Record[Idx++];
   VD->VarDeclBits.CXXForRangeDecl = Record[Idx++];
   VD->VarDeclBits.ARCPseudoStrong = Record[Idx++];
+  
+  mergeRedeclarable(VD, Redecl);
+  
   if (uint64_t Val = Record[Idx++]) {
     VD->setInit(Reader.ReadExpr(F));
     if (Val > 1) {
@@ -1707,12 +1714,20 @@
   }
   
   // Functions with the same type and linkage match.
+  // FIXME: This needs to cope with function templates, merging of 
+  //prototyped/non-prototyped functions, etc.
   if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) {
     FunctionDecl *FuncY = cast<FunctionDecl>(Y);
     return (FuncX->getLinkage() == FuncY->getLinkage()) &&
       FuncX->getASTContext().hasSameType(FuncX->getType(), FuncY->getType());
   }
   
+  // Variables with the same type and linkage match.
+  if (VarDecl *VarX = dyn_cast<VarDecl>(X)) {
+    VarDecl *VarY = cast<VarDecl>(Y);
+    return (VarX->getLinkage() == VarY->getLinkage()) &&
+      VarX->getASTContext().hasSameType(VarX->getType(), VarY->getType());
+  }
   
   // FIXME: Many other cases to implement.
   return false;

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=147535&r1=147534&r2=147535&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/redecl-merge-left.h (original)
+++ cfe/trunk/test/Modules/Inputs/redecl-merge-left.h Wed Jan  4 11:21:36 2012
@@ -63,6 +63,21 @@
 int func1(int);
 int func2(int);
 
+
+
+
+
+
+
+
+
+
+// Spacing matters!
+extern int var1;
+extern float var2;
+
+extern double var3;
+
 #ifdef __cplusplus
 template<typename T> class Vector;
 

Modified: cfe/trunk/test/Modules/Inputs/redecl-merge-right.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/redecl-merge-right.h?rev=147535&r1=147534&r2=147535&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/redecl-merge-right.h (original)
+++ cfe/trunk/test/Modules/Inputs/redecl-merge-right.h Wed Jan  4 11:21:36 2012
@@ -69,6 +69,15 @@
 int func1(int);
 static int func2(int);
 
+
+
+
+// Spacing matters!
+extern int var1;
+extern int var2;
+
+static double var3;
+
 #ifdef __cplusplus
 template<typename T> class Vector { 
 public:

Modified: cfe/trunk/test/Modules/redecl-merge.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/redecl-merge.m?rev=147535&r1=147534&r2=147535&view=diff
==============================================================================
--- cfe/trunk/test/Modules/redecl-merge.m (original)
+++ cfe/trunk/test/Modules/redecl-merge.m Wed Jan  4 11:21:36 2012
@@ -72,6 +72,14 @@
   func2(i); // expected-error{{call to 'func2' is ambiguous}}
 }
 
+void testVarMerge(int i) {
+  var1 = i;
+  // in other files: expected-note 2{{candidate found by name lookup is 'var2'}}
+  var2 = i; // expected-error{{reference to 'var2' is ambiguous}}
+  // in other files: expected-note 2{{candidate found by name lookup is 'var3'}}
+  var3 = i; // expected-error{{reference to 'var3' is ambiguous}}
+}
+
 // Test redeclarations of entities in explicit submodules, to make
 // sure we're maintaining the declaration chains even when normal name
 // lookup can't see what we're looking for.





More information about the cfe-commits mailing list