Encode the target triple in the PCH file, and check that target triple when using the PCH file

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68824 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp
index a00fee2..9eb881c 100644
--- a/lib/Frontend/PCHReader.cpp
+++ b/lib/Frontend/PCHReader.cpp
@@ -18,6 +18,7 @@
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/FileManager.h"
+#include "clang/Basic/TargetInfo.h"
 #include "llvm/Bitcode/BitstreamReader.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -238,7 +239,10 @@
 
     // Read and process a record.
     Record.clear();
-    switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record)) {
+    const char *BlobStart = 0;
+    unsigned BlobLen = 0;
+    switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record, 
+                                                   &BlobStart, &BlobLen)) {
     default:  // Default behavior: ignore.
       break;
 
@@ -264,6 +268,16 @@
       if (ParseLanguageOptions(Record))
         return IgnorePCH;
       break;
+
+    case pch::TARGET_TRIPLE:
+      std::string TargetTriple(BlobStart, BlobLen);
+      if (TargetTriple != Context.Target.getTargetTriple()) {
+        Diag(diag::warn_pch_target_triple)
+          << TargetTriple << Context.Target.getTargetTriple();
+        Diag(diag::note_ignoring_pch) << FileName;
+        return IgnorePCH;
+      }
+      break;
     }
   }
 
@@ -319,10 +333,9 @@
         return true;
 
       case IgnorePCH:
-        if (Stream.SkipBlock()) {
-          Error("Malformed block record");
-          return true;
-        }
+        // FIXME: We could consider reading through to the end of this
+        // PCH block, skipping subblocks, to see if there are other
+        // PCH blocks elsewhere.
         return false;
       }
       break;
diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp
index 6feb726..a31c0c7 100644
--- a/lib/Frontend/PCHWriter.cpp
+++ b/lib/Frontend/PCHWriter.cpp
@@ -21,6 +21,7 @@
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Basic/TargetInfo.h"
 #include "llvm/Bitcode/BitstreamWriter.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -328,6 +329,21 @@
 // PCHWriter Implementation
 //===----------------------------------------------------------------------===//
 
+/// \brief Write the target triple (e.g., i686-apple-darwin9).
+void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
+  using namespace llvm;
+  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
+  Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
+  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
+  unsigned TripleAbbrev = S.EmitAbbrev(Abbrev);
+
+  RecordData Record;
+  Record.push_back(pch::TARGET_TRIPLE);
+  const char *Triple = Target.getTargetTriple();
+  S.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
+}
+
+/// \brief Write the LangOptions structure.
 void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
   RecordData Record;
   Record.push_back(LangOpts.Trigraphs);
@@ -815,7 +831,8 @@
   DeclsToEmit.push(Context.getTranslationUnitDecl());
 
   // Write the remaining PCH contents.
-  S.EnterSubblock(pch::PCH_BLOCK_ID, 2);
+  S.EnterSubblock(pch::PCH_BLOCK_ID, 3);
+  WriteTargetTriple(Context.Target);
   WriteLanguageOptions(Context.getLangOptions());
   WriteSourceManagerBlock(Context.getSourceManager());
   WritePreprocessor(PP);