[llvm-branch-commits] [cfe-branch] r115676 - in /cfe/branches/Apple/whitney: lib/Serialization/ASTReader.cpp lib/Serialization/ASTWriter.cpp test/PCH/Inputs/chain-remap-types1.h test/PCH/Inputs/chain-remap-types2.h test/PCH/chain-remap-types.m tools/libclang/CIndexUSRs.cpp

Daniel Dunbar daniel at zuster.org
Tue Oct 5 13:55:46 PDT 2010


Author: ddunbar
Date: Tue Oct  5 15:55:46 2010
New Revision: 115676

URL: http://llvm.org/viewvc/llvm-project?rev=115676&view=rev
Log:
Merge r115647:
--
Author: Douglas Gregor <dgregor at apple.com>
Date:   Tue Oct 5 18:37:06 2010 +0000

    Fix a marvelous chained AST writing bug, where we end up with the
    following amusing sequence:
      - AST writing schedules writing a type X* that it had never seen
      before
      - AST writing starts writing another declaration, ends up
      deserializing X* from a prior AST file. Now we have two type IDs for
      the same type!
      - AST writer tries to write X*. It only has the lower-numbered ID
      from the the prior AST file, so references to the higher-numbered ID
      that was scheduled for writing go off into lalaland.

    To fix this, keep the higher-numbered ID so we end up writing the type
    twice. Since this issue occurs so rarely, and type records are
    generally rather small, I deemed this better than the alternative: to
    keep a separate mapping from the higher-numbered IDs to the
    lower-numbered IDs, which we would end up having to check whenever we
    want to deserialize any type.

    Fixes <rdar://problem/8511624>, I think.

Added:
    cfe/branches/Apple/whitney/test/PCH/Inputs/chain-remap-types1.h
    cfe/branches/Apple/whitney/test/PCH/Inputs/chain-remap-types2.h
    cfe/branches/Apple/whitney/test/PCH/chain-remap-types.m
Modified:
    cfe/branches/Apple/whitney/lib/Serialization/ASTReader.cpp
    cfe/branches/Apple/whitney/lib/Serialization/ASTWriter.cpp
    cfe/branches/Apple/whitney/tools/libclang/CIndexUSRs.cpp

Modified: cfe/branches/Apple/whitney/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Serialization/ASTReader.cpp?rev=115676&r1=115675&r2=115676&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Serialization/ASTReader.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Serialization/ASTReader.cpp Tue Oct  5 15:55:46 2010
@@ -3074,6 +3074,9 @@
   assert(Index < TypesLoaded.size() && "Type index out-of-range");
   if (TypesLoaded[Index].isNull()) {
     TypesLoaded[Index] = ReadTypeRecord(Index);
+    if (TypesLoaded[Index].isNull())
+      return QualType();
+
     TypesLoaded[Index]->setFromAST();
     TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
     if (DeserializationListener)

Modified: cfe/branches/Apple/whitney/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/lib/Serialization/ASTWriter.cpp?rev=115676&r1=115675&r2=115676&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/branches/Apple/whitney/lib/Serialization/ASTWriter.cpp Tue Oct  5 15:55:46 2010
@@ -1418,10 +1418,7 @@
   if (Idx.getIndex() == 0) // we haven't seen this type before.
     Idx = TypeIdx(NextTypeID++);
 
-  // If this type comes from a previously-loaded PCH/AST file, don't try to
-  // write the type again.
-  if (Idx.getIndex() < FirstTypeID)
-    return;
+  assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
 
   // Record the offset for this type.
   unsigned Index = Idx.getIndex() - FirstTypeID;
@@ -2871,7 +2868,7 @@
   if (D == 0) {
     return 0;
   }
-
+  assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
   DeclID &ID = DeclIDs[D];
   if (ID == 0) {
     // We haven't seen this declaration before. Give it a new ID and
@@ -3129,7 +3126,14 @@
 }
 
 void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
-  TypeIdxs[T] = Idx;
+  // Always take the highest-numbered type index. This copes with an interesting
+  // case for chained AST writing where we schedule writing the type and then,
+  // later, deserialize the type from another AST. In this case, we want to 
+  // keep the higher-numbered entry so that we can properly write it out to
+  // the AST file.
+  TypeIdx &StoredIdx = TypeIdxs[T];
+  if (Idx.getIndex() >= StoredIdx.getIndex())
+    StoredIdx = Idx;
 }
 
 void ASTWriter::DeclRead(DeclID ID, const Decl *D) {

Added: cfe/branches/Apple/whitney/test/PCH/Inputs/chain-remap-types1.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/test/PCH/Inputs/chain-remap-types1.h?rev=115676&view=auto
==============================================================================
--- cfe/branches/Apple/whitney/test/PCH/Inputs/chain-remap-types1.h (added)
+++ cfe/branches/Apple/whitney/test/PCH/Inputs/chain-remap-types1.h Tue Oct  5 15:55:46 2010
@@ -0,0 +1,10 @@
+ at class X;
+
+struct Y {
+  X *my_X;
+};
+
+ at interface X {
+}
+ at property X *prop;
+ at end

Added: cfe/branches/Apple/whitney/test/PCH/Inputs/chain-remap-types2.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/test/PCH/Inputs/chain-remap-types2.h?rev=115676&view=auto
==============================================================================
--- cfe/branches/Apple/whitney/test/PCH/Inputs/chain-remap-types2.h (added)
+++ cfe/branches/Apple/whitney/test/PCH/Inputs/chain-remap-types2.h Tue Oct  5 15:55:46 2010
@@ -0,0 +1,8 @@
+void h(X*);
+
+ at interface X (Blah) {
+}
+ at end
+
+void g(X*);
+

Added: cfe/branches/Apple/whitney/test/PCH/chain-remap-types.m
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/test/PCH/chain-remap-types.m?rev=115676&view=auto
==============================================================================
--- cfe/branches/Apple/whitney/test/PCH/chain-remap-types.m (added)
+++ cfe/branches/Apple/whitney/test/PCH/chain-remap-types.m Tue Oct  5 15:55:46 2010
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -emit-pch -x objective-c-header -o %t1 %S/Inputs/chain-remap-types1.h
+// RUN: %clang_cc1 -emit-pch -x objective-c-header -o %t2 %S/Inputs/chain-remap-types2.h -include-pch %t1 -chained-pch
+// RUN: %clang_cc1 -include-pch %t2 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -ast-print -include-pch %t2 %s | FileCheck %s
+
+// CHECK: @class X;
+// CHECK: struct Y 
+// CHECK: @property ( assign,readwrite ) X * prop
+// CHECK: void h(X *);
+// CHECK: @interface X(Blah)
+// CHECK: void g(X *);
+

Modified: cfe/branches/Apple/whitney/tools/libclang/CIndexUSRs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/whitney/tools/libclang/CIndexUSRs.cpp?rev=115676&r1=115675&r2=115676&view=diff
==============================================================================
--- cfe/branches/Apple/whitney/tools/libclang/CIndexUSRs.cpp (original)
+++ cfe/branches/Apple/whitney/tools/libclang/CIndexUSRs.cpp Tue Oct  5 15:55:46 2010
@@ -699,7 +699,8 @@
     break;
 
   case TemplateArgument::Declaration:
-    Visit(Arg.getAsDecl());
+    if (Decl *D = Arg.getAsDecl())
+      Visit(D);
     break;
       
   case TemplateArgument::Template:





More information about the llvm-branch-commits mailing list