Store in PCH the key function of C++ class to avoid deserializing the complete declaration context in order to compute it.
Progress for rdar://7260160.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@116508 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h
index 40a3647..0a960ab 100644
--- a/include/clang/AST/ASTContext.h
+++ b/include/clang/AST/ASTContext.h
@@ -287,7 +287,9 @@
   /// \brief The current C++ ABI.
   llvm::OwningPtr<CXXABI> ABI;
   CXXABI *createCXXABI(const TargetInfo &T);
-  
+
+  friend class ASTDeclReader;
+
 public:
   const TargetInfo &Target;
   IdentifierTable &Idents;
diff --git a/lib/AST/RecordLayoutBuilder.cpp b/lib/AST/RecordLayoutBuilder.cpp
index c5465a5..9444be3 100644
--- a/lib/AST/RecordLayoutBuilder.cpp
+++ b/lib/AST/RecordLayoutBuilder.cpp
@@ -1712,9 +1712,6 @@
   const CXXMethodDecl *&Entry = KeyFunctions[RD];
   if (!Entry)
     Entry = RecordLayoutBuilder::ComputeKeyFunction(RD);
-  else
-    assert(Entry == RecordLayoutBuilder::ComputeKeyFunction(RD) &&
-           "Key function changed!");
 
   return Entry;
 }
diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp
index 97adbf7..e8fe264 100644
--- a/lib/Serialization/ASTReaderDecl.cpp
+++ b/lib/Serialization/ASTReaderDecl.cpp
@@ -833,6 +833,15 @@
     break;
   }
   }
+
+  // Load the key function to avoid deserializing every method so we can
+  // compute it.
+  if (D->IsDefinition) {
+    CXXMethodDecl *Key
+        = cast_or_null<CXXMethodDecl>(Reader.GetDecl(Record[Idx++]));
+    if (Key)
+      C.KeyFunctions[D] = Key;
+  }
 }
 
 void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
diff --git a/lib/Serialization/ASTWriterDecl.cpp b/lib/Serialization/ASTWriterDecl.cpp
index 3e1ba89..9aaaa48 100644
--- a/lib/Serialization/ASTWriterDecl.cpp
+++ b/lib/Serialization/ASTWriterDecl.cpp
@@ -770,6 +770,11 @@
     Record.push_back(CXXRecNotTemplate);
   }
 
+  // Store the key function to avoid deserializing every method so we can
+  // compute it.
+  if (D->IsDefinition)
+    Writer.AddDeclRef(Context.getKeyFunction(D), Record);
+
   Code = serialization::DECL_CXX_RECORD;
 }
 
diff --git a/test/PCH/check-deserializations.cpp b/test/PCH/check-deserializations.cpp
index ea03984..9f73c95 100644
--- a/test/PCH/check-deserializations.cpp
+++ b/test/PCH/check-deserializations.cpp
@@ -7,6 +7,7 @@
 
 struct S1 {
   void S1_method(); // This should not be deserialized.
+  virtual void S1_keyfunc();
 };