From: Argyrios Kyrtzidis Date: Thu, 13 Dec 2012 21:38:23 +0000 (+0000) Subject: [PCH] Make the new PCH format (control block) backwards compatible and X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5e24f2d83099bb557b1d39c6420787eeaf072a81;p=clang [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@170150 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Serialization/ASTBitCodes.h b/include/clang/Serialization/ASTBitCodes.h index ce9009fd78..4273e14787 100644 --- a/include/clang/Serialization/ASTBitCodes.h +++ b/include/clang/Serialization/ASTBitCodes.h @@ -35,7 +35,7 @@ namespace clang { /// 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 @@ namespace clang { /// 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 @@ namespace clang { /// 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 @@ namespace clang { /// 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 @@ namespace clang { /// 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 @@ namespace clang { /// 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, diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 5c25f58403..c0976ee1e6 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -2866,6 +2866,9 @@ ASTReader::ReadASTCore(StringRef FileName, 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 @@ ASTReader::ReadASTCore(StringRef FileName, } break; case CONTROL_BLOCK_ID: + HaveReadControlBlock = true; switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) { case Success: break; @@ -2897,6 +2901,12 @@ ASTReader::ReadASTCore(StringRef FileName, } 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; diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index ed0a272faa..1b39f551bf 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -3568,6 +3568,12 @@ void ASTWriter::WriteASTCore(Sema &SemaRef, 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();