1875ed548SDimitry Andric //===--- ObjectFilePCHContainerOperations.cpp -----------------------------===//
2875ed548SDimitry Andric //
3875ed548SDimitry Andric // The LLVM Compiler Infrastructure
4875ed548SDimitry Andric //
5875ed548SDimitry Andric // This file is distributed under the University of Illinois Open Source
6875ed548SDimitry Andric // License. See LICENSE.TXT for details.
7875ed548SDimitry Andric //
8875ed548SDimitry Andric //===----------------------------------------------------------------------===//
9875ed548SDimitry Andric
10875ed548SDimitry Andric #include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
11875ed548SDimitry Andric #include "CGDebugInfo.h"
12875ed548SDimitry Andric #include "CodeGenModule.h"
13875ed548SDimitry Andric #include "clang/AST/ASTContext.h"
14875ed548SDimitry Andric #include "clang/AST/DeclObjC.h"
15875ed548SDimitry Andric #include "clang/AST/Expr.h"
16875ed548SDimitry Andric #include "clang/AST/RecursiveASTVisitor.h"
17*b5893f02SDimitry Andric #include "clang/Basic/CodeGenOptions.h"
18875ed548SDimitry Andric #include "clang/Basic/Diagnostic.h"
19875ed548SDimitry Andric #include "clang/Basic/TargetInfo.h"
20875ed548SDimitry Andric #include "clang/CodeGen/BackendUtil.h"
210623d748SDimitry Andric #include "clang/Frontend/CompilerInstance.h"
220623d748SDimitry Andric #include "clang/Lex/HeaderSearch.h"
23e7145dcbSDimitry Andric #include "clang/Lex/Preprocessor.h"
24875ed548SDimitry Andric #include "llvm/ADT/StringRef.h"
25875ed548SDimitry Andric #include "llvm/Bitcode/BitstreamReader.h"
26875ed548SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h"
27875ed548SDimitry Andric #include "llvm/IR/Constants.h"
28875ed548SDimitry Andric #include "llvm/IR/DataLayout.h"
29875ed548SDimitry Andric #include "llvm/IR/LLVMContext.h"
30875ed548SDimitry Andric #include "llvm/IR/Module.h"
31875ed548SDimitry Andric #include "llvm/Object/COFF.h"
32875ed548SDimitry Andric #include "llvm/Object/ObjectFile.h"
33e7145dcbSDimitry Andric #include "llvm/Support/Path.h"
34875ed548SDimitry Andric #include "llvm/Support/TargetRegistry.h"
35875ed548SDimitry Andric #include <memory>
36e7145dcbSDimitry Andric #include <utility>
370623d748SDimitry Andric
38875ed548SDimitry Andric using namespace clang;
39875ed548SDimitry Andric
40875ed548SDimitry Andric #define DEBUG_TYPE "pchcontainer"
41875ed548SDimitry Andric
42875ed548SDimitry Andric namespace {
43875ed548SDimitry Andric class PCHContainerGenerator : public ASTConsumer {
44875ed548SDimitry Andric DiagnosticsEngine &Diags;
45875ed548SDimitry Andric const std::string MainFileName;
46e7145dcbSDimitry Andric const std::string OutputFileName;
47875ed548SDimitry Andric ASTContext *Ctx;
480623d748SDimitry Andric ModuleMap &MMap;
49875ed548SDimitry Andric const HeaderSearchOptions &HeaderSearchOpts;
50875ed548SDimitry Andric const PreprocessorOptions &PreprocessorOpts;
51875ed548SDimitry Andric CodeGenOptions CodeGenOpts;
52875ed548SDimitry Andric const TargetOptions TargetOpts;
53875ed548SDimitry Andric const LangOptions LangOpts;
54875ed548SDimitry Andric std::unique_ptr<llvm::LLVMContext> VMContext;
55875ed548SDimitry Andric std::unique_ptr<llvm::Module> M;
56875ed548SDimitry Andric std::unique_ptr<CodeGen::CodeGenModule> Builder;
57e7145dcbSDimitry Andric std::unique_ptr<raw_pwrite_stream> OS;
58875ed548SDimitry Andric std::shared_ptr<PCHBuffer> Buffer;
59875ed548SDimitry Andric
600623d748SDimitry Andric /// Visit every type and emit debug info for it.
610623d748SDimitry Andric struct DebugTypeVisitor : public RecursiveASTVisitor<DebugTypeVisitor> {
620623d748SDimitry Andric clang::CodeGen::CGDebugInfo &DI;
630623d748SDimitry Andric ASTContext &Ctx;
DebugTypeVisitor__anon266401a60111::PCHContainerGenerator::DebugTypeVisitor64e7145dcbSDimitry Andric DebugTypeVisitor(clang::CodeGen::CGDebugInfo &DI, ASTContext &Ctx)
65e7145dcbSDimitry Andric : DI(DI), Ctx(Ctx) {}
660623d748SDimitry Andric
670623d748SDimitry Andric /// Determine whether this type can be represented in DWARF.
CanRepresent__anon266401a60111::PCHContainerGenerator::DebugTypeVisitor680623d748SDimitry Andric static bool CanRepresent(const Type *Ty) {
690623d748SDimitry Andric return !Ty->isDependentType() && !Ty->isUndeducedType();
700623d748SDimitry Andric }
710623d748SDimitry Andric
VisitImportDecl__anon266401a60111::PCHContainerGenerator::DebugTypeVisitor720623d748SDimitry Andric bool VisitImportDecl(ImportDecl *D) {
734ba319b5SDimitry Andric if (!D->getImportedOwningModule())
744ba319b5SDimitry Andric DI.EmitImportDecl(*D);
750623d748SDimitry Andric return true;
760623d748SDimitry Andric }
770623d748SDimitry Andric
VisitTypeDecl__anon266401a60111::PCHContainerGenerator::DebugTypeVisitor780623d748SDimitry Andric bool VisitTypeDecl(TypeDecl *D) {
79444ed5c5SDimitry Andric // TagDecls may be deferred until after all decls have been merged and we
80444ed5c5SDimitry Andric // know the complete type. Pure forward declarations will be skipped, but
81444ed5c5SDimitry Andric // they don't need to be emitted into the module anyway.
82e7145dcbSDimitry Andric if (auto *TD = dyn_cast<TagDecl>(D))
83e7145dcbSDimitry Andric if (!TD->isCompleteDefinition())
84444ed5c5SDimitry Andric return true;
85444ed5c5SDimitry Andric
860623d748SDimitry Andric QualType QualTy = Ctx.getTypeDeclType(D);
870623d748SDimitry Andric if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
880623d748SDimitry Andric DI.getOrCreateStandaloneType(QualTy, D->getLocation());
890623d748SDimitry Andric return true;
900623d748SDimitry Andric }
910623d748SDimitry Andric
VisitObjCInterfaceDecl__anon266401a60111::PCHContainerGenerator::DebugTypeVisitor920623d748SDimitry Andric bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
930623d748SDimitry Andric QualType QualTy(D->getTypeForDecl(), 0);
940623d748SDimitry Andric if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
950623d748SDimitry Andric DI.getOrCreateStandaloneType(QualTy, D->getLocation());
960623d748SDimitry Andric return true;
970623d748SDimitry Andric }
980623d748SDimitry Andric
VisitFunctionDecl__anon266401a60111::PCHContainerGenerator::DebugTypeVisitor990623d748SDimitry Andric bool VisitFunctionDecl(FunctionDecl *D) {
1000623d748SDimitry Andric if (isa<CXXMethodDecl>(D))
1010623d748SDimitry Andric // This is not yet supported. Constructing the `this' argument
1020623d748SDimitry Andric // mandates a CodeGenFunction.
1030623d748SDimitry Andric return true;
1040623d748SDimitry Andric
1050623d748SDimitry Andric SmallVector<QualType, 16> ArgTypes;
106e7145dcbSDimitry Andric for (auto i : D->parameters())
1070623d748SDimitry Andric ArgTypes.push_back(i->getType());
1080623d748SDimitry Andric QualType RetTy = D->getReturnType();
1090623d748SDimitry Andric QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
1100623d748SDimitry Andric FunctionProtoType::ExtProtoInfo());
1110623d748SDimitry Andric if (CanRepresent(FnTy.getTypePtr()))
1120623d748SDimitry Andric DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
1130623d748SDimitry Andric return true;
1140623d748SDimitry Andric }
1150623d748SDimitry Andric
VisitObjCMethodDecl__anon266401a60111::PCHContainerGenerator::DebugTypeVisitor1160623d748SDimitry Andric bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
1170623d748SDimitry Andric if (!D->getClassInterface())
1180623d748SDimitry Andric return true;
1190623d748SDimitry Andric
1200623d748SDimitry Andric bool selfIsPseudoStrong, selfIsConsumed;
1210623d748SDimitry Andric SmallVector<QualType, 16> ArgTypes;
1220623d748SDimitry Andric ArgTypes.push_back(D->getSelfType(Ctx, D->getClassInterface(),
1230623d748SDimitry Andric selfIsPseudoStrong, selfIsConsumed));
1240623d748SDimitry Andric ArgTypes.push_back(Ctx.getObjCSelType());
125e7145dcbSDimitry Andric for (auto i : D->parameters())
1260623d748SDimitry Andric ArgTypes.push_back(i->getType());
1270623d748SDimitry Andric QualType RetTy = D->getReturnType();
1280623d748SDimitry Andric QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
1290623d748SDimitry Andric FunctionProtoType::ExtProtoInfo());
1300623d748SDimitry Andric if (CanRepresent(FnTy.getTypePtr()))
1310623d748SDimitry Andric DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
1320623d748SDimitry Andric return true;
1330623d748SDimitry Andric }
1340623d748SDimitry Andric };
1350623d748SDimitry Andric
136875ed548SDimitry Andric public:
PCHContainerGenerator(CompilerInstance & CI,const std::string & MainFileName,const std::string & OutputFileName,std::unique_ptr<raw_pwrite_stream> OS,std::shared_ptr<PCHBuffer> Buffer)1370623d748SDimitry Andric PCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName,
138875ed548SDimitry Andric const std::string &OutputFileName,
139e7145dcbSDimitry Andric std::unique_ptr<raw_pwrite_stream> OS,
140875ed548SDimitry Andric std::shared_ptr<PCHBuffer> Buffer)
141e7145dcbSDimitry Andric : Diags(CI.getDiagnostics()), MainFileName(MainFileName),
142e7145dcbSDimitry Andric OutputFileName(OutputFileName), Ctx(nullptr),
1430623d748SDimitry Andric MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
1440623d748SDimitry Andric HeaderSearchOpts(CI.getHeaderSearchOpts()),
1450623d748SDimitry Andric PreprocessorOpts(CI.getPreprocessorOpts()),
146e7145dcbSDimitry Andric TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
147e7145dcbSDimitry Andric OS(std::move(OS)), Buffer(std::move(Buffer)) {
148875ed548SDimitry Andric // The debug info output isn't affected by CodeModel and
149875ed548SDimitry Andric // ThreadModel, but the backend expects them to be nonempty.
150875ed548SDimitry Andric CodeGenOpts.CodeModel = "default";
151875ed548SDimitry Andric CodeGenOpts.ThreadModel = "single";
1520623d748SDimitry Andric CodeGenOpts.DebugTypeExtRefs = true;
153b40b48b8SDimitry Andric // When building a module MainFileName is the name of the modulemap file.
154b40b48b8SDimitry Andric CodeGenOpts.MainFileName =
155b40b48b8SDimitry Andric LangOpts.CurrentModule.empty() ? MainFileName : LangOpts.CurrentModule;
156e7145dcbSDimitry Andric CodeGenOpts.setDebugInfo(codegenoptions::FullDebugInfo);
157e7145dcbSDimitry Andric CodeGenOpts.setDebuggerTuning(CI.getCodeGenOpts().getDebuggerTuning());
158*b5893f02SDimitry Andric CodeGenOpts.DebugPrefixMap =
159*b5893f02SDimitry Andric CI.getInvocation().getCodeGenOpts().DebugPrefixMap;
160875ed548SDimitry Andric }
161875ed548SDimitry Andric
1620623d748SDimitry Andric ~PCHContainerGenerator() override = default;
163875ed548SDimitry Andric
Initialize(ASTContext & Context)164875ed548SDimitry Andric void Initialize(ASTContext &Context) override {
1650623d748SDimitry Andric assert(!Ctx && "initialized multiple times");
1660623d748SDimitry Andric
167875ed548SDimitry Andric Ctx = &Context;
168875ed548SDimitry Andric VMContext.reset(new llvm::LLVMContext());
169875ed548SDimitry Andric M.reset(new llvm::Module(MainFileName, *VMContext));
170e7145dcbSDimitry Andric M->setDataLayout(Ctx->getTargetInfo().getDataLayout());
1710623d748SDimitry Andric Builder.reset(new CodeGen::CodeGenModule(
1720623d748SDimitry Andric *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
173e7145dcbSDimitry Andric
174e7145dcbSDimitry Andric // Prepare CGDebugInfo to emit debug info for a clang module.
175e7145dcbSDimitry Andric auto *DI = Builder->getModuleDebugInfo();
176e7145dcbSDimitry Andric StringRef ModuleName = llvm::sys::path::filename(MainFileName);
17720e90f04SDimitry Andric DI->setPCHDescriptor({ModuleName, "", OutputFileName,
17820e90f04SDimitry Andric ASTFileSignature{{{~0U, ~0U, ~0U, ~0U, ~1U}}}});
179e7145dcbSDimitry Andric DI->setModuleMap(MMap);
1800623d748SDimitry Andric }
1810623d748SDimitry Andric
HandleTopLevelDecl(DeclGroupRef D)1820623d748SDimitry Andric bool HandleTopLevelDecl(DeclGroupRef D) override {
1830623d748SDimitry Andric if (Diags.hasErrorOccurred())
1840623d748SDimitry Andric return true;
1850623d748SDimitry Andric
1860623d748SDimitry Andric // Collect debug info for all decls in this group.
1870623d748SDimitry Andric for (auto *I : D)
1880623d748SDimitry Andric if (!I->isFromASTFile()) {
189e7145dcbSDimitry Andric DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
1900623d748SDimitry Andric DTV.TraverseDecl(I);
1910623d748SDimitry Andric }
1920623d748SDimitry Andric return true;
1930623d748SDimitry Andric }
1940623d748SDimitry Andric
HandleTopLevelDeclInObjCContainer(DeclGroupRef D)1950623d748SDimitry Andric void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
1960623d748SDimitry Andric HandleTopLevelDecl(D);
1970623d748SDimitry Andric }
1980623d748SDimitry Andric
HandleTagDeclDefinition(TagDecl * D)1990623d748SDimitry Andric void HandleTagDeclDefinition(TagDecl *D) override {
2000623d748SDimitry Andric if (Diags.hasErrorOccurred())
2010623d748SDimitry Andric return;
2020623d748SDimitry Andric
203444ed5c5SDimitry Andric if (D->isFromASTFile())
204444ed5c5SDimitry Andric return;
205444ed5c5SDimitry Andric
206e7145dcbSDimitry Andric // Anonymous tag decls are deferred until we are building their declcontext.
207e7145dcbSDimitry Andric if (D->getName().empty())
208e7145dcbSDimitry Andric return;
209e7145dcbSDimitry Andric
210e7145dcbSDimitry Andric // Defer tag decls until their declcontext is complete.
211e7145dcbSDimitry Andric auto *DeclCtx = D->getDeclContext();
212e7145dcbSDimitry Andric while (DeclCtx) {
213e7145dcbSDimitry Andric if (auto *D = dyn_cast<TagDecl>(DeclCtx))
214e7145dcbSDimitry Andric if (!D->isCompleteDefinition())
215e7145dcbSDimitry Andric return;
216e7145dcbSDimitry Andric DeclCtx = DeclCtx->getParent();
217e7145dcbSDimitry Andric }
218e7145dcbSDimitry Andric
219e7145dcbSDimitry Andric DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
220444ed5c5SDimitry Andric DTV.TraverseDecl(D);
2210623d748SDimitry Andric Builder->UpdateCompletedType(D);
2220623d748SDimitry Andric }
2230623d748SDimitry Andric
HandleTagDeclRequiredDefinition(const TagDecl * D)2240623d748SDimitry Andric void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
2250623d748SDimitry Andric if (Diags.hasErrorOccurred())
2260623d748SDimitry Andric return;
2270623d748SDimitry Andric
2280623d748SDimitry Andric if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
2290623d748SDimitry Andric Builder->getModuleDebugInfo()->completeRequiredType(RD);
230875ed548SDimitry Andric }
231875ed548SDimitry Andric
HandleImplicitImportDecl(ImportDecl * D)232042b1c2eSDimitry Andric void HandleImplicitImportDecl(ImportDecl *D) override {
233042b1c2eSDimitry Andric if (!D->getImportedOwningModule())
234042b1c2eSDimitry Andric Builder->getModuleDebugInfo()->EmitImportDecl(*D);
235042b1c2eSDimitry Andric }
236042b1c2eSDimitry Andric
237875ed548SDimitry Andric /// Emit a container holding the serialized AST.
HandleTranslationUnit(ASTContext & Ctx)238875ed548SDimitry Andric void HandleTranslationUnit(ASTContext &Ctx) override {
239875ed548SDimitry Andric assert(M && VMContext && Builder);
240875ed548SDimitry Andric // Delete these on function exit.
241875ed548SDimitry Andric std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext);
242875ed548SDimitry Andric std::unique_ptr<llvm::Module> M = std::move(this->M);
243875ed548SDimitry Andric std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder);
244875ed548SDimitry Andric
245875ed548SDimitry Andric if (Diags.hasErrorOccurred())
246875ed548SDimitry Andric return;
247875ed548SDimitry Andric
248875ed548SDimitry Andric M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple());
249e7145dcbSDimitry Andric M->setDataLayout(Ctx.getTargetInfo().getDataLayout());
250e7145dcbSDimitry Andric
251e7145dcbSDimitry Andric // PCH files don't have a signature field in the control block,
252e7145dcbSDimitry Andric // but LLVM detects DWO CUs by looking for a non-zero DWO id.
25320e90f04SDimitry Andric // We use the lower 64 bits for debug info.
25420e90f04SDimitry Andric uint64_t Signature =
25520e90f04SDimitry Andric Buffer->Signature
25620e90f04SDimitry Andric ? (uint64_t)Buffer->Signature[1] << 32 | Buffer->Signature[0]
25720e90f04SDimitry Andric : ~1ULL;
258e7145dcbSDimitry Andric Builder->getModuleDebugInfo()->setDwoId(Signature);
259875ed548SDimitry Andric
260875ed548SDimitry Andric // Finalize the Builder.
261875ed548SDimitry Andric if (Builder)
262875ed548SDimitry Andric Builder->Release();
263875ed548SDimitry Andric
264875ed548SDimitry Andric // Ensure the target exists.
265875ed548SDimitry Andric std::string Error;
266875ed548SDimitry Andric auto Triple = Ctx.getTargetInfo().getTriple();
267875ed548SDimitry Andric if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
268875ed548SDimitry Andric llvm::report_fatal_error(Error);
269875ed548SDimitry Andric
270875ed548SDimitry Andric // Emit the serialized Clang AST into its own section.
271875ed548SDimitry Andric assert(Buffer->IsComplete && "serialization did not complete");
272875ed548SDimitry Andric auto &SerializedAST = Buffer->Data;
273875ed548SDimitry Andric auto Size = SerializedAST.size();
274875ed548SDimitry Andric auto Int8Ty = llvm::Type::getInt8Ty(*VMContext);
275875ed548SDimitry Andric auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
276875ed548SDimitry Andric auto *Data = llvm::ConstantDataArray::getString(
277875ed548SDimitry Andric *VMContext, StringRef(SerializedAST.data(), Size),
278875ed548SDimitry Andric /*AddNull=*/false);
279875ed548SDimitry Andric auto *ASTSym = new llvm::GlobalVariable(
280875ed548SDimitry Andric *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data,
281875ed548SDimitry Andric "__clang_ast");
282875ed548SDimitry Andric // The on-disk hashtable needs to be aligned.
283875ed548SDimitry Andric ASTSym->setAlignment(8);
284875ed548SDimitry Andric
285875ed548SDimitry Andric // Mach-O also needs a segment name.
286875ed548SDimitry Andric if (Triple.isOSBinFormatMachO())
287875ed548SDimitry Andric ASTSym->setSection("__CLANG,__clangast");
288875ed548SDimitry Andric // COFF has an eight character length limit.
289875ed548SDimitry Andric else if (Triple.isOSBinFormatCOFF())
290875ed548SDimitry Andric ASTSym->setSection("clangast");
291875ed548SDimitry Andric else
292875ed548SDimitry Andric ASTSym->setSection("__clangast");
293875ed548SDimitry Andric
2944ba319b5SDimitry Andric LLVM_DEBUG({
295875ed548SDimitry Andric // Print the IR for the PCH container to the debug output.
296875ed548SDimitry Andric llvm::SmallString<0> Buffer;
297e7145dcbSDimitry Andric clang::EmitBackendOutput(
29895ec533aSDimitry Andric Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, LangOpts,
299e7145dcbSDimitry Andric Ctx.getTargetInfo().getDataLayout(), M.get(),
300e7145dcbSDimitry Andric BackendAction::Backend_EmitLL,
301e7145dcbSDimitry Andric llvm::make_unique<llvm::raw_svector_ostream>(Buffer));
302875ed548SDimitry Andric llvm::dbgs() << Buffer;
303875ed548SDimitry Andric });
304875ed548SDimitry Andric
305875ed548SDimitry Andric // Use the LLVM backend to emit the pch container.
30695ec533aSDimitry Andric clang::EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts,
30795ec533aSDimitry Andric LangOpts, Ctx.getTargetInfo().getDataLayout(),
30895ec533aSDimitry Andric M.get(), BackendAction::Backend_EmitObj,
30995ec533aSDimitry Andric std::move(OS));
310875ed548SDimitry Andric
311875ed548SDimitry Andric // Free the memory for the temporary buffer.
312875ed548SDimitry Andric llvm::SmallVector<char, 0> Empty;
313875ed548SDimitry Andric SerializedAST = std::move(Empty);
314875ed548SDimitry Andric }
315875ed548SDimitry Andric };
316875ed548SDimitry Andric
3170623d748SDimitry Andric } // anonymous namespace
318875ed548SDimitry Andric
319875ed548SDimitry Andric std::unique_ptr<ASTConsumer>
CreatePCHContainerGenerator(CompilerInstance & CI,const std::string & MainFileName,const std::string & OutputFileName,std::unique_ptr<llvm::raw_pwrite_stream> OS,std::shared_ptr<PCHBuffer> Buffer) const320b6c25e0eSDimitry Andric ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
3210623d748SDimitry Andric CompilerInstance &CI, const std::string &MainFileName,
322e7145dcbSDimitry Andric const std::string &OutputFileName,
323e7145dcbSDimitry Andric std::unique_ptr<llvm::raw_pwrite_stream> OS,
324875ed548SDimitry Andric std::shared_ptr<PCHBuffer> Buffer) const {
325e7145dcbSDimitry Andric return llvm::make_unique<PCHContainerGenerator>(
326e7145dcbSDimitry Andric CI, MainFileName, OutputFileName, std::move(OS), Buffer);
327875ed548SDimitry Andric }
328875ed548SDimitry Andric
32944290647SDimitry Andric StringRef
ExtractPCH(llvm::MemoryBufferRef Buffer) const33044290647SDimitry Andric ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
33144290647SDimitry Andric StringRef PCH;
33244290647SDimitry Andric auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer);
33344290647SDimitry Andric if (OFOrErr) {
33444290647SDimitry Andric auto &OF = OFOrErr.get();
33544290647SDimitry Andric bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
336875ed548SDimitry Andric // Find the clang AST section in the container.
33744290647SDimitry Andric for (auto &Section : OF->sections()) {
338875ed548SDimitry Andric StringRef Name;
339875ed548SDimitry Andric Section.getName(Name);
34044290647SDimitry Andric if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) {
34144290647SDimitry Andric Section.getContents(PCH);
34244290647SDimitry Andric return PCH;
343875ed548SDimitry Andric }
344875ed548SDimitry Andric }
345875ed548SDimitry Andric }
34644290647SDimitry Andric handleAllErrors(OFOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
34744290647SDimitry Andric if (EIB.convertToErrorCode() ==
34844290647SDimitry Andric llvm::object::object_error::invalid_file_type)
349875ed548SDimitry Andric // As a fallback, treat the buffer as a raw AST.
35044290647SDimitry Andric PCH = Buffer.getBuffer();
35144290647SDimitry Andric else
35244290647SDimitry Andric EIB.log(llvm::errs());
35344290647SDimitry Andric });
35444290647SDimitry Andric return PCH;
355875ed548SDimitry Andric }
356