diff --git a/clang/lib/Frontend/PrecompiledPreamble.cpp b/clang/lib/Frontend/PrecompiledPreamble.cpp index 9f5be05a1430..e8128e3828ed 100644 --- a/clang/lib/Frontend/PrecompiledPreamble.cpp +++ b/clang/lib/Frontend/PrecompiledPreamble.cpp @@ -234,9 +234,10 @@ private: class PrecompilePreambleAction : public ASTFrontendAction { public: - PrecompilePreambleAction(std::string *InMemStorage, + PrecompilePreambleAction(std::shared_ptr Buffer, bool WritePCHFile, PreambleCallbacks &Callbacks) - : InMemStorage(InMemStorage), Callbacks(Callbacks) {} + : Buffer(std::move(Buffer)), WritePCHFile(WritePCHFile), + Callbacks(Callbacks) {} std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override; @@ -244,6 +245,12 @@ public: bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; } void setEmittedPreamblePCH(ASTWriter &Writer) { + if (FileOS) { + *FileOS << Buffer->Data; + // Make sure it hits disk now. + FileOS->flush(); + } + this->HasEmittedPreamblePCH = true; Callbacks.AfterPCHEmitted(Writer); } @@ -262,7 +269,9 @@ private: friend class PrecompilePreambleConsumer; bool HasEmittedPreamblePCH = false; - std::string *InMemStorage; + std::shared_ptr Buffer; + bool WritePCHFile; // otherwise the PCH is written into the PCHBuffer only. + std::unique_ptr FileOS; // null if in-memory PreambleCallbacks &Callbacks; }; @@ -272,12 +281,11 @@ public: const Preprocessor &PP, InMemoryModuleCache &ModuleCache, StringRef isysroot, - std::unique_ptr Out) - : PCHGenerator(PP, ModuleCache, "", isysroot, - std::make_shared(), + std::shared_ptr Buffer) + : PCHGenerator(PP, ModuleCache, "", isysroot, std::move(Buffer), ArrayRef>(), /*AllowASTWithErrors=*/true), - Action(Action), Out(std::move(Out)) {} + Action(Action) {} bool HandleTopLevelDecl(DeclGroupRef DG) override { Action.Callbacks.HandleTopLevelDecl(DG); @@ -288,15 +296,6 @@ public: PCHGenerator::HandleTranslationUnit(Ctx); if (!hasEmittedPCH()) return; - - // Write the generated bitstream to "Out". - *Out << getPCH(); - // Make sure it hits disk now. - Out->flush(); - // Free the buffer. - llvm::SmallVector Empty; - getPCH() = std::move(Empty); - Action.setEmittedPreamblePCH(getWriter()); } @@ -306,7 +305,6 @@ public: private: PrecompilePreambleAction &Action; - std::unique_ptr Out; }; std::unique_ptr @@ -316,21 +314,18 @@ PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI, if (!GeneratePCHAction::ComputeASTConsumerArguments(CI, Sysroot)) return nullptr; - std::unique_ptr OS; - if (InMemStorage) { - OS = std::make_unique(*InMemStorage); - } else { - std::string OutputFile; - OS = GeneratePCHAction::CreateOutputFile(CI, InFile, OutputFile); + if (WritePCHFile) { + std::string OutputFile; // unused + FileOS = GeneratePCHAction::CreateOutputFile(CI, InFile, OutputFile); + if (!FileOS) + return nullptr; } - if (!OS) - return nullptr; if (!CI.getFrontendOpts().RelocatablePCH) Sysroot.clear(); return std::make_unique( - *this, CI.getPreprocessor(), CI.getModuleCache(), Sysroot, std::move(OS)); + *this, CI.getPreprocessor(), CI.getModuleCache(), Sysroot, Buffer); } template bool moveOnNoError(llvm::ErrorOr Val, T &Output) { @@ -356,9 +351,9 @@ public: S->File = std::move(File); return S; } - static std::unique_ptr inMemory() { + static std::unique_ptr inMemory(std::shared_ptr Buf) { std::unique_ptr S(new PCHStorage()); - S->Memory.emplace(); + S->Memory = std::move(Buf); return S; } @@ -376,11 +371,7 @@ public: } llvm::StringRef memoryContents() const { assert(getKind() == Kind::InMemory); - return *Memory; - } - std::string &memoryBufferForWrite() { - assert(getKind() == Kind::InMemory); - return *Memory; + return StringRef(Memory->Data.data(), Memory->Data.size()); } private: @@ -388,7 +379,7 @@ private: PCHStorage(const PCHStorage &) = delete; PCHStorage &operator=(const PCHStorage &) = delete; - llvm::Optional Memory; + std::shared_ptr Memory; std::unique_ptr File; }; @@ -411,9 +402,10 @@ llvm::ErrorOr PrecompiledPreamble::Build( PreprocessorOptions &PreprocessorOpts = PreambleInvocation->getPreprocessorOpts(); + std::shared_ptr Buffer = std::make_shared(); std::unique_ptr Storage; if (StoreInMemory) { - Storage = PCHStorage::inMemory(); + Storage = PCHStorage::inMemory(Buffer); } else { // Create a temporary file for the precompiled preamble. In rare // circumstances, this can fail. @@ -495,9 +487,10 @@ llvm::ErrorOr PrecompiledPreamble::Build( PreambleInputBuffer.release()); } - std::unique_ptr Act; - Act.reset(new PrecompilePreambleAction( - StoreInMemory ? &Storage->memoryBufferForWrite() : nullptr, Callbacks)); + auto Act = std::make_unique( + std::move(Buffer), + /*WritePCHFile=*/Storage->getKind() == PCHStorage::Kind::TempFile, + Callbacks); if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) return BuildPreambleError::BeginSourceFileFailed; @@ -527,6 +520,7 @@ llvm::ErrorOr PrecompiledPreamble::Build( if (!Act->hasEmittedPreamblePCH()) return BuildPreambleError::CouldntEmitPCH; + Act.reset(); // Frees the PCH buffer frees, unless Storage keeps it in memory. // Keep track of all of the files that the source manager knows about, // so we can verify whether they have changed or not. @@ -790,7 +784,8 @@ void PrecompiledPreamble::setupPreambleStorage( StringRef PCHPath = getInMemoryPreamblePath(); PreprocessorOpts.ImplicitPCHInclude = std::string(PCHPath); - auto Buf = llvm::MemoryBuffer::getMemBuffer(Storage.memoryContents()); + auto Buf = llvm::MemoryBuffer::getMemBuffer( + Storage.memoryContents(), PCHPath, /*RequiresNullTerminator=*/false); VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(Buf), VFS); } } diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index aa9b4f149011..74cc5c73c803 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -5071,7 +5071,8 @@ std::string ASTReader::getOriginalSourceFile( const std::string &ASTFileName, FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { // Open the AST file. - auto Buffer = FileMgr.getBufferForFile(ASTFileName); + auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false, + /*RequiresNullTerminator=*/false); if (!Buffer) { Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << Buffer.getError().message();