[cfe-commits] r170150 - in /cfe/trunk: include/clang/Serialization/ASTBitCodes.h lib/Serialization/ASTReader.cpp lib/Serialization/ASTWriter.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Thu Dec 13 13:38:23 PST 2012


Author: akirtzidis
Date: Thu Dec 13 15:38:23 2012
New Revision: 170150

URL: http://llvm.org/viewvc/llvm-project?rev=170150&view=rev
Log:
[PCH] Make the new PCH format (control block) backwards compatible and
don't crash when loading a PCH with the older format.

The introduction of the control block broke compatibility with PCHs from
older versions. This patch allows loading (and rejecting) PCHs from an older
version and allows newer PCHs to be rejected from older clang versions as well.

rdar://12821386

Modified:
    cfe/trunk/include/clang/Serialization/ASTBitCodes.h
    cfe/trunk/lib/Serialization/ASTReader.cpp
    cfe/trunk/lib/Serialization/ASTWriter.cpp

Modified: cfe/trunk/include/clang/Serialization/ASTBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTBitCodes.h?rev=170150&r1=170149&r2=170150&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTBitCodes.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTBitCodes.h Thu Dec 13 15:38:23 2012
@@ -35,7 +35,7 @@
     /// Version 4 of AST files also requires that the version control branch and
     /// revision match exactly, since there is no backward compatibility of
     /// AST files at this time.
-    const unsigned VERSION_MAJOR = 4;
+    const unsigned VERSION_MAJOR = 5;
 
     /// \brief AST file minor version number supported by this version of
     /// Clang.
@@ -323,6 +323,11 @@
       /// NULL-terminated string that corresponds to that identifier.
       IDENTIFIER_OFFSET = 3,
 
+      /// \brief This is so that older clang versions, before the introduction
+      /// of the control block, can read and reject the newer PCH format.
+      /// *DON"T CHANGE THIS NUMBER*.
+      METADATA_OLD_FORMAT = 4,
+
       /// \brief Record code for the identifier table.
       ///
       /// The identifier table is a simple blob that contains
@@ -335,7 +340,7 @@
       /// between offsets (for unresolved identifier IDs) and
       /// IdentifierInfo pointers (for already-resolved identifier
       /// IDs).
-      IDENTIFIER_TABLE = 4,
+      IDENTIFIER_TABLE = 5,
 
       /// \brief Record code for the array of external definitions.
       ///
@@ -345,7 +350,7 @@
       /// reported to the AST consumer after the AST file has been
       /// read, since their presence can affect the semantics of the
       /// program (e.g., for code generation).
-      EXTERNAL_DEFINITIONS = 5,
+      EXTERNAL_DEFINITIONS = 6,
 
       /// \brief Record code for the set of non-builtin, special
       /// types.
@@ -354,33 +359,33 @@
       /// that are constructed during semantic analysis (e.g.,
       /// __builtin_va_list). The SPECIAL_TYPE_* constants provide
       /// offsets into this record.
-      SPECIAL_TYPES = 6,
+      SPECIAL_TYPES = 7,
 
       /// \brief Record code for the extra statistics we gather while
       /// generating an AST file.
-      STATISTICS = 7,
+      STATISTICS = 8,
 
       /// \brief Record code for the array of tentative definitions.
-      TENTATIVE_DEFINITIONS = 8,
+      TENTATIVE_DEFINITIONS = 9,
 
       /// \brief Record code for the array of locally-scoped external
       /// declarations.
-      LOCALLY_SCOPED_EXTERNAL_DECLS = 9,
+      LOCALLY_SCOPED_EXTERNAL_DECLS = 10,
 
       /// \brief Record code for the table of offsets into the
       /// Objective-C method pool.
-      SELECTOR_OFFSETS = 10,
+      SELECTOR_OFFSETS = 11,
 
       /// \brief Record code for the Objective-C method pool,
-      METHOD_POOL = 11,
+      METHOD_POOL = 12,
 
       /// \brief The value of the next __COUNTER__ to dispense.
       /// [PP_COUNTER_VALUE, Val]
-      PP_COUNTER_VALUE = 12,
+      PP_COUNTER_VALUE = 13,
 
       /// \brief Record code for the table of offsets into the block
       /// of source-location information.
-      SOURCE_LOCATION_OFFSETS = 13,
+      SOURCE_LOCATION_OFFSETS = 14,
 
       /// \brief Record code for the set of source location entries
       /// that need to be preloaded by the AST reader.
@@ -388,7 +393,7 @@
       /// This set contains the source location entry for the
       /// predefines buffer and for any file entries that need to be
       /// preloaded.
-      SOURCE_LOCATION_PRELOADS = 14,
+      SOURCE_LOCATION_PRELOADS = 15,
 
       /// \brief Record code for the set of ext_vector type names.
       EXT_VECTOR_DECLS = 16,

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=170150&r1=170149&r2=170150&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Thu Dec 13 15:38:23 2012
@@ -2866,6 +2866,9 @@
     return Failure;
   }
 
+  // This is used for compatibility with older PCH formats.
+  bool HaveReadControlBlock = false;
+
   while (!Stream.AtEndOfStream()) {
     unsigned Code = Stream.ReadCode();
 
@@ -2885,6 +2888,7 @@
       }
       break;
     case CONTROL_BLOCK_ID:
+      HaveReadControlBlock = true;
       switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
       case Success:
         break;
@@ -2897,6 +2901,12 @@
       }
       break;
     case AST_BLOCK_ID:
+      if (!HaveReadControlBlock) {
+        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
+          Diag(diag::warn_pch_version_too_old);
+        return VersionMismatch;
+      }
+
       // Record that we've loaded this module.
       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
       return Success;

Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=170150&r1=170149&r2=170150&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Thu Dec 13 15:38:23 2012
@@ -3568,6 +3568,12 @@
   RecordData Record;
   Stream.EnterSubblock(AST_BLOCK_ID, 5);
 
+  // This is so that older clang versions, before the introduction
+  // of the control block, can read and reject the newer PCH format.
+  Record.clear();
+  Record.push_back(VERSION_MAJOR);
+  Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
+
   // Create a lexical update block containing all of the declarations in the
   // translation unit that do not come from other AST files.
   const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();





More information about the cfe-commits mailing list