]> granicus.if.org Git - clang/commitdiff
Encode the Clang branch and Subversion revision into a PCH file, and
authorDouglas Gregor <dgregor@apple.com>
Mon, 5 Oct 2009 21:07:28 +0000 (21:07 +0000)
committerDouglas Gregor <dgregor@apple.com>
Mon, 5 Oct 2009 21:07:28 +0000 (21:07 +0000)
assume that PCH files from different Clang revisions are not
compatible. Addresses <rdar://problem/7266572>.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83323 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticFrontendKinds.td
include/clang/Frontend/PCHBitCodes.h
lib/Frontend/PCHReader.cpp
lib/Frontend/PCHWriter.cpp

index 385b1c2a0b8153ed2d7afd59ac7f9e86401bdcb6..e5c73270fdfce9b8dd17404b54ad9da8560fc7c3 100644 (file)
@@ -136,6 +136,8 @@ def warn_pch_version_too_old : Error<
     "PCH file uses an older PCH format that is no longer supported">;
 def warn_pch_version_too_new : Error<
     "PCH file uses a newer PCH format that cannot be read">;
+def warn_pch_different_branch : Error<
+    "PCH file built from a different branch (%0) than the compiler (%1)">;
 def warn_cmdline_conflicting_macro_def : Error<
     "definition of the macro '%0' conflicts with the definition used to "
     "build the precompiled header">;
index 2655217a833d68fa3f4ca85cb9325940ca8c6736..716780e68a5289bd0defeed83124bc2c57e1b31d 100644 (file)
@@ -29,7 +29,11 @@ namespace clang {
     /// incompatible with previous versions (such that a reader
     /// designed for the previous version could not support reading
     /// the new version), this number should be increased.
-    const unsigned VERSION_MAJOR = 2;
+    ///
+    /// Version 3 of PCH files also requires that the Subversion branch and
+    /// revision match exactly, since there is no backward compatibility of
+    /// PCH files at this time.
+    const unsigned VERSION_MAJOR = 3;
 
     /// \brief PCH minor version number supported by this version of
     /// Clang.
@@ -218,7 +222,11 @@ namespace clang {
 
       /// \brief Record code for the sorted array of source ranges where
       /// comments were encountered in the source code.
-      COMMENT_RANGES = 20
+      COMMENT_RANGES = 20,
+      
+      /// \brief Record code for the Subversion branch and revision information
+      /// of the compiler used to build this PCH file.
+      SVN_BRANCH_REVISION = 21
     };
 
     /// \brief Record types used within a source manager block.
index ead00ee75ffd80be2ad4dd93ea0359ef4a144c77..e61668dd31778a7e9e9ab60b4f8a752460c64111 100644 (file)
@@ -26,6 +26,7 @@
 #include "clang/Basic/SourceManagerInternals.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/TargetInfo.h"
+#include "clang/Basic/Version.h"
 #include "llvm/Bitcode/BitstreamReader.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -1340,9 +1341,7 @@ PCHReader::ReadPCHBlock() {
     case pch::SOURCE_LOCATION_OFFSETS:
       SLocOffsets = (const uint32_t *)BlobStart;
       TotalNumSLocEntries = Record[0];
-      SourceMgr.PreallocateSLocEntries(this,
-                                                   TotalNumSLocEntries,
-                                                   Record[1]);
+      SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
       break;
 
     case pch::SOURCE_LOCATION_PRELOADS:
@@ -1377,6 +1376,23 @@ PCHReader::ReadPCHBlock() {
       Comments = (SourceRange *)BlobStart;
       NumComments = BlobLen / sizeof(SourceRange);
       break;
+        
+    case pch::SVN_BRANCH_REVISION: {
+      unsigned CurRevision = getClangSubversionRevision();
+      if (Record[0] && CurRevision && Record[0] != CurRevision) {
+        Diag(Record[0] < CurRevision? diag::warn_pch_version_too_old
+                                    : diag::warn_pch_version_too_new);
+        return IgnorePCH;
+      }
+      
+      const char *CurBranch = getClangSubversionPath();
+      if (strncmp(CurBranch, BlobStart, BlobLen)) {
+        std::string PCHBranch(BlobStart, BlobLen);
+        Diag(diag::warn_pch_different_branch) << PCHBranch << CurBranch;
+        return IgnorePCH;
+      }
+      break;
+    }
     }
   }
   Error("premature end of bitstream in PCH file");
index 08a1661e1d377f4dfac9fe5c62d34136e216be5b..64a678ea450bf05037a6ae3379fe781eeb6578ba 100644 (file)
@@ -394,7 +394,8 @@ void PCHWriter::WriteBlockInfoBlock() {
   RECORD(STAT_CACHE);
   RECORD(EXT_VECTOR_DECLS);
   RECORD(COMMENT_RANGES);
-
+  RECORD(SVN_BRANCH_REVISION);
+  
   // SourceManager Block.
   BLOCK(SOURCE_MANAGER_BLOCK);
   RECORD(SM_SLOC_FILE_ENTRY);
@@ -564,6 +565,17 @@ void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
     Record.push_back(pch::ORIGINAL_FILE_NAME);
     Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
   }
+  
+  // Subversion branch/version information.
+  BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
+  SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
+  SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
+  SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
+  unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
+  Record.clear();
+  Record.push_back(pch::SVN_BRANCH_REVISION);
+  Record.push_back(getClangSubversionRevision());
+  Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
 }
 
 /// \brief Write the LangOptions structure.