From a71a7d8a1ce4474e7bdb680658fb58b6caf391d3 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Wed, 24 Oct 2012 20:05:57 +0000 Subject: [PATCH] (De-)serialize the preprocessor options, including macros defined, -include'd files, etc. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@166614 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Lex/Preprocessor.h | 4 ++ include/clang/Serialization/ASTBitCodes.h | 5 ++- include/clang/Serialization/ASTReader.h | 12 ++++++ lib/Serialization/ASTReader.cpp | 45 +++++++++++++++++++++++ lib/Serialization/ASTWriter.cpp | 33 +++++++++++++++-- 5 files changed, 95 insertions(+), 4 deletions(-) diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index 6ba7e40962..64c72858c4 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -414,6 +414,10 @@ public: /// \param Target Information about the target. void Initialize(const TargetInfo &Target); + /// \brief Retrieve the preprocessor options used to initialize this + /// preprocessor. + PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; } + DiagnosticsEngine &getDiagnostics() const { return *Diags; } void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; } diff --git a/include/clang/Serialization/ASTBitCodes.h b/include/clang/Serialization/ASTBitCodes.h index 9c28dc937f..4dba7b5739 100644 --- a/include/clang/Serialization/ASTBitCodes.h +++ b/include/clang/Serialization/ASTBitCodes.h @@ -270,7 +270,10 @@ namespace clang { FILE_SYSTEM_OPTIONS = 9, /// \brief Record code for the headers search options table. - HEADER_SEARCH_OPTIONS = 10 + HEADER_SEARCH_OPTIONS = 10, + + /// \brief Record code for the preprocessor options table. + PREPROCESSOR_OPTIONS = 11 }; /// \brief Record types that occur within the input-files block diff --git a/include/clang/Serialization/ASTReader.h b/include/clang/Serialization/ASTReader.h index cc01c8b8b0..d285ca504a 100644 --- a/include/clang/Serialization/ASTReader.h +++ b/include/clang/Serialization/ASTReader.h @@ -73,6 +73,7 @@ class MacroDefinition; class NamedDecl; class OpaqueValueExpr; class Preprocessor; +class PreprocessorOptions; class Sema; class SwitchCase; class ASTDeserializationListener; @@ -148,6 +149,15 @@ public: return false; } + /// \brief Receives the preprocessor options. + /// + /// \returns true to indicate the preprocessor options are invalid, or false + /// otherwise. + virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, + bool Complain) { + return false; + } + /// \brief Receives the contents of the predefines buffer. /// /// \param Buffers Information about the predefines buffers. @@ -947,6 +957,8 @@ private: ASTReaderListener &Listener); static bool ParseHeaderSearchOptions(const RecordData &Record, bool Complain, ASTReaderListener &Listener); + static bool ParsePreprocessorOptions(const RecordData &Record, bool Complain, + ASTReaderListener &Listener); struct RecordLocation { RecordLocation(ModuleFile *M, uint64_t O) diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 1462aa1964..ccadfbd07a 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -30,6 +30,7 @@ #include "clang/Lex/MacroInfo.h" #include "clang/Lex/PreprocessingRecord.h" #include "clang/Lex/Preprocessor.h" +#include "clang/Lex/PreprocessorOptions.h" #include "clang/Lex/HeaderSearch.h" #include "clang/Lex/HeaderSearchOptions.h" #include "clang/Basic/OnDiskHashTable.h" @@ -2035,6 +2036,15 @@ ASTReader::ReadControlBlock(ModuleFile &F, break; } + case PREPROCESSOR_OPTIONS: { + bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0; + if (Listener && &F == *ModuleMgr.begin() && + ParsePreprocessorOptions(Record, Complain, *Listener) && + !DisableValidation) + return ConfigurationMismatch; + break; + } + case ORIGINAL_FILE: F.OriginalSourceFileID = FileID::get(Record[0]); F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen); @@ -3509,6 +3519,11 @@ bool ASTReader::isAcceptableASTFile(StringRef Filename, return false; break; + case PREPROCESSOR_OPTIONS: + if (ParsePreprocessorOptions(Record, false, Validator)) + return false; + break; + default: // No other validation to perform. break; @@ -3914,6 +3929,36 @@ bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, return Listener.ReadHeaderSearchOptions(HSOpts, Complain); } +bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, + bool Complain, + ASTReaderListener &Listener) { + PreprocessorOptions PPOpts; + unsigned Idx = 0; + + // Macro definitions/undefs + for (unsigned N = Record[Idx++]; N; --N) { + std::string Macro = ReadString(Record, Idx); + bool IsUndef = Record[Idx++]; + PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); + } + + // Includes + for (unsigned N = Record[Idx++]; N; --N) { + PPOpts.Includes.push_back(ReadString(Record, Idx)); + } + + // Macro Includes + for (unsigned N = Record[Idx++]; N; --N) { + PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); + } + + PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); + PPOpts.ImplicitPTHInclude = ReadString(Record, Idx); + PPOpts.ObjCXXARCStandardLibrary = + static_cast(Record[Idx++]); + return Listener.ReadPreprocessorOptions(PPOpts, Complain); +} + std::pair ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { GlobalPreprocessedEntityMapType::iterator diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index 3af8a87b30..50e6763308 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -29,6 +29,7 @@ #include "clang/Lex/MacroInfo.h" #include "clang/Lex/PreprocessingRecord.h" #include "clang/Lex/Preprocessor.h" +#include "clang/Lex/PreprocessorOptions.h" #include "clang/Lex/HeaderSearch.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/FileSystemStatCache.h" @@ -780,7 +781,8 @@ void ASTWriter::WriteBlockInfoBlock() { RECORD(DIAGNOSTIC_OPTIONS); RECORD(FILE_SYSTEM_OPTIONS); RECORD(HEADER_SEARCH_OPTIONS); - + RECORD(PREPROCESSOR_OPTIONS); + BLOCK(INPUT_FILES_BLOCK); RECORD(INPUT_FILE); @@ -1130,6 +1132,32 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context, Record.push_back(HSOpts.UseLibcxx); Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record); + // Preprocessor options. + Record.clear(); + const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts(); + + // Macro definitions. + Record.push_back(PPOpts.Macros.size()); + for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { + AddString(PPOpts.Macros[I].first, Record); + Record.push_back(PPOpts.Macros[I].second); + } + + // Includes + Record.push_back(PPOpts.Includes.size()); + for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I) + AddString(PPOpts.Includes[I], Record); + + // Macro includes + Record.push_back(PPOpts.MacroIncludes.size()); + for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I) + AddString(PPOpts.MacroIncludes[I], Record); + + AddString(PPOpts.ImplicitPCHInclude, Record); + AddString(PPOpts.ImplicitPTHInclude, Record); + Record.push_back(static_cast(PPOpts.ObjCXXARCStandardLibrary)); + Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record); + // Original file name and file ID SourceManager &SM = Context.getSourceManager(); if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { @@ -1146,11 +1174,10 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context, const char *MainFileNameStr = MainFilePath.c_str(); MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr, isysroot); - RecordData Record; + Record.clear(); Record.push_back(ORIGINAL_FILE); Record.push_back(SM.getMainFileID().getOpaqueValue()); Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr); - Record.clear(); } // Original PCH directory -- 2.40.0