xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/ObjectFilePCHContainerWriter.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric //===--- ObjectFilePCHContainerWriter.cpp -----------------------------===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric 
9*700637cbSDimitry Andric #include "clang/CodeGen/ObjectFilePCHContainerWriter.h"
10*700637cbSDimitry Andric #include "CGDebugInfo.h"
11*700637cbSDimitry Andric #include "CodeGenModule.h"
12*700637cbSDimitry Andric #include "clang/AST/ASTContext.h"
13*700637cbSDimitry Andric #include "clang/AST/DeclObjC.h"
14*700637cbSDimitry Andric #include "clang/AST/Expr.h"
15*700637cbSDimitry Andric #include "clang/AST/RecursiveASTVisitor.h"
16*700637cbSDimitry Andric #include "clang/Basic/CodeGenOptions.h"
17*700637cbSDimitry Andric #include "clang/Basic/Diagnostic.h"
18*700637cbSDimitry Andric #include "clang/Basic/TargetInfo.h"
19*700637cbSDimitry Andric #include "clang/CodeGen/BackendUtil.h"
20*700637cbSDimitry Andric #include "clang/Frontend/CompilerInstance.h"
21*700637cbSDimitry Andric #include "clang/Lex/HeaderSearch.h"
22*700637cbSDimitry Andric #include "clang/Lex/Preprocessor.h"
23*700637cbSDimitry Andric #include "llvm/ADT/StringRef.h"
24*700637cbSDimitry Andric #include "llvm/IR/Constants.h"
25*700637cbSDimitry Andric #include "llvm/IR/DataLayout.h"
26*700637cbSDimitry Andric #include "llvm/IR/LLVMContext.h"
27*700637cbSDimitry Andric #include "llvm/IR/Module.h"
28*700637cbSDimitry Andric #include "llvm/MC/TargetRegistry.h"
29*700637cbSDimitry Andric #include "llvm/Object/COFF.h"
30*700637cbSDimitry Andric #include "llvm/Support/Path.h"
31*700637cbSDimitry Andric #include <memory>
32*700637cbSDimitry Andric #include <utility>
33*700637cbSDimitry Andric 
34*700637cbSDimitry Andric using namespace clang;
35*700637cbSDimitry Andric 
36*700637cbSDimitry Andric #define DEBUG_TYPE "pchcontainer"
37*700637cbSDimitry Andric 
38*700637cbSDimitry Andric namespace {
39*700637cbSDimitry Andric class PCHContainerGenerator : public ASTConsumer {
40*700637cbSDimitry Andric   CompilerInstance &CI;
41*700637cbSDimitry Andric   DiagnosticsEngine &Diags;
42*700637cbSDimitry Andric   const std::string MainFileName;
43*700637cbSDimitry Andric   const std::string OutputFileName;
44*700637cbSDimitry Andric   ASTContext *Ctx;
45*700637cbSDimitry Andric   ModuleMap &MMap;
46*700637cbSDimitry Andric   IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
47*700637cbSDimitry Andric   const HeaderSearchOptions &HeaderSearchOpts;
48*700637cbSDimitry Andric   const PreprocessorOptions &PreprocessorOpts;
49*700637cbSDimitry Andric   CodeGenOptions CodeGenOpts;
50*700637cbSDimitry Andric   const TargetOptions TargetOpts;
51*700637cbSDimitry Andric   LangOptions LangOpts;
52*700637cbSDimitry Andric   std::unique_ptr<llvm::LLVMContext> VMContext;
53*700637cbSDimitry Andric   std::unique_ptr<llvm::Module> M;
54*700637cbSDimitry Andric   std::unique_ptr<CodeGen::CodeGenModule> Builder;
55*700637cbSDimitry Andric   std::unique_ptr<raw_pwrite_stream> OS;
56*700637cbSDimitry Andric   std::shared_ptr<PCHBuffer> Buffer;
57*700637cbSDimitry Andric 
58*700637cbSDimitry Andric   /// Visit every type and emit debug info for it.
59*700637cbSDimitry Andric   struct DebugTypeVisitor : public RecursiveASTVisitor<DebugTypeVisitor> {
60*700637cbSDimitry Andric     clang::CodeGen::CGDebugInfo &DI;
61*700637cbSDimitry Andric     ASTContext &Ctx;
DebugTypeVisitor__anon9aef00700111::PCHContainerGenerator::DebugTypeVisitor62*700637cbSDimitry Andric     DebugTypeVisitor(clang::CodeGen::CGDebugInfo &DI, ASTContext &Ctx)
63*700637cbSDimitry Andric         : DI(DI), Ctx(Ctx) {}
64*700637cbSDimitry Andric 
65*700637cbSDimitry Andric     /// Determine whether this type can be represented in DWARF.
CanRepresent__anon9aef00700111::PCHContainerGenerator::DebugTypeVisitor66*700637cbSDimitry Andric     static bool CanRepresent(const Type *Ty) {
67*700637cbSDimitry Andric       return !Ty->isDependentType() && !Ty->isUndeducedType();
68*700637cbSDimitry Andric     }
69*700637cbSDimitry Andric 
VisitImportDecl__anon9aef00700111::PCHContainerGenerator::DebugTypeVisitor70*700637cbSDimitry Andric     bool VisitImportDecl(ImportDecl *D) {
71*700637cbSDimitry Andric       if (!D->getImportedOwningModule())
72*700637cbSDimitry Andric         DI.EmitImportDecl(*D);
73*700637cbSDimitry Andric       return true;
74*700637cbSDimitry Andric     }
75*700637cbSDimitry Andric 
VisitTypeDecl__anon9aef00700111::PCHContainerGenerator::DebugTypeVisitor76*700637cbSDimitry Andric     bool VisitTypeDecl(TypeDecl *D) {
77*700637cbSDimitry Andric       // TagDecls may be deferred until after all decls have been merged and we
78*700637cbSDimitry Andric       // know the complete type. Pure forward declarations will be skipped, but
79*700637cbSDimitry Andric       // they don't need to be emitted into the module anyway.
80*700637cbSDimitry Andric       if (auto *TD = dyn_cast<TagDecl>(D))
81*700637cbSDimitry Andric         if (!TD->isCompleteDefinition())
82*700637cbSDimitry Andric           return true;
83*700637cbSDimitry Andric 
84*700637cbSDimitry Andric       if (D->hasAttr<NoDebugAttr>())
85*700637cbSDimitry Andric         return true;
86*700637cbSDimitry Andric 
87*700637cbSDimitry Andric       QualType QualTy = Ctx.getTypeDeclType(D);
88*700637cbSDimitry Andric       if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
89*700637cbSDimitry Andric         DI.getOrCreateStandaloneType(QualTy, D->getLocation());
90*700637cbSDimitry Andric       return true;
91*700637cbSDimitry Andric     }
92*700637cbSDimitry Andric 
VisitObjCInterfaceDecl__anon9aef00700111::PCHContainerGenerator::DebugTypeVisitor93*700637cbSDimitry Andric     bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
94*700637cbSDimitry Andric       QualType QualTy(D->getTypeForDecl(), 0);
95*700637cbSDimitry Andric       if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
96*700637cbSDimitry Andric         DI.getOrCreateStandaloneType(QualTy, D->getLocation());
97*700637cbSDimitry Andric       return true;
98*700637cbSDimitry Andric     }
99*700637cbSDimitry Andric 
VisitFunctionDecl__anon9aef00700111::PCHContainerGenerator::DebugTypeVisitor100*700637cbSDimitry Andric     bool VisitFunctionDecl(FunctionDecl *D) {
101*700637cbSDimitry Andric       // Skip deduction guides.
102*700637cbSDimitry Andric       if (isa<CXXDeductionGuideDecl>(D))
103*700637cbSDimitry Andric         return true;
104*700637cbSDimitry Andric 
105*700637cbSDimitry Andric       if (isa<CXXMethodDecl>(D))
106*700637cbSDimitry Andric         // This is not yet supported. Constructing the `this' argument
107*700637cbSDimitry Andric         // mandates a CodeGenFunction.
108*700637cbSDimitry Andric         return true;
109*700637cbSDimitry Andric 
110*700637cbSDimitry Andric       SmallVector<QualType, 16> ArgTypes;
111*700637cbSDimitry Andric       for (auto *i : D->parameters())
112*700637cbSDimitry Andric         ArgTypes.push_back(i->getType());
113*700637cbSDimitry Andric       QualType RetTy = D->getReturnType();
114*700637cbSDimitry Andric       QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
115*700637cbSDimitry Andric                                           FunctionProtoType::ExtProtoInfo());
116*700637cbSDimitry Andric       if (CanRepresent(FnTy.getTypePtr()))
117*700637cbSDimitry Andric         DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
118*700637cbSDimitry Andric       return true;
119*700637cbSDimitry Andric     }
120*700637cbSDimitry Andric 
VisitObjCMethodDecl__anon9aef00700111::PCHContainerGenerator::DebugTypeVisitor121*700637cbSDimitry Andric     bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
122*700637cbSDimitry Andric       if (!D->getClassInterface())
123*700637cbSDimitry Andric         return true;
124*700637cbSDimitry Andric 
125*700637cbSDimitry Andric       bool selfIsPseudoStrong, selfIsConsumed;
126*700637cbSDimitry Andric       SmallVector<QualType, 16> ArgTypes;
127*700637cbSDimitry Andric       ArgTypes.push_back(D->getSelfType(Ctx, D->getClassInterface(),
128*700637cbSDimitry Andric                                         selfIsPseudoStrong, selfIsConsumed));
129*700637cbSDimitry Andric       ArgTypes.push_back(Ctx.getObjCSelType());
130*700637cbSDimitry Andric       for (auto *i : D->parameters())
131*700637cbSDimitry Andric         ArgTypes.push_back(i->getType());
132*700637cbSDimitry Andric       QualType RetTy = D->getReturnType();
133*700637cbSDimitry Andric       QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
134*700637cbSDimitry Andric                                           FunctionProtoType::ExtProtoInfo());
135*700637cbSDimitry Andric       if (CanRepresent(FnTy.getTypePtr()))
136*700637cbSDimitry Andric         DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
137*700637cbSDimitry Andric       return true;
138*700637cbSDimitry Andric     }
139*700637cbSDimitry Andric   };
140*700637cbSDimitry Andric 
141*700637cbSDimitry 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)142*700637cbSDimitry Andric   PCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName,
143*700637cbSDimitry Andric                         const std::string &OutputFileName,
144*700637cbSDimitry Andric                         std::unique_ptr<raw_pwrite_stream> OS,
145*700637cbSDimitry Andric                         std::shared_ptr<PCHBuffer> Buffer)
146*700637cbSDimitry Andric       : CI(CI), Diags(CI.getDiagnostics()), MainFileName(MainFileName),
147*700637cbSDimitry Andric         OutputFileName(OutputFileName), Ctx(nullptr),
148*700637cbSDimitry Andric         MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
149*700637cbSDimitry Andric         FS(&CI.getVirtualFileSystem()),
150*700637cbSDimitry Andric         HeaderSearchOpts(CI.getHeaderSearchOpts()),
151*700637cbSDimitry Andric         PreprocessorOpts(CI.getPreprocessorOpts()),
152*700637cbSDimitry Andric         TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
153*700637cbSDimitry Andric         OS(std::move(OS)), Buffer(std::move(Buffer)) {
154*700637cbSDimitry Andric     // The debug info output isn't affected by CodeModel and
155*700637cbSDimitry Andric     // ThreadModel, but the backend expects them to be nonempty.
156*700637cbSDimitry Andric     CodeGenOpts.CodeModel = "default";
157*700637cbSDimitry Andric     LangOpts.setThreadModel(LangOptions::ThreadModelKind::Single);
158*700637cbSDimitry Andric     CodeGenOpts.DebugTypeExtRefs = true;
159*700637cbSDimitry Andric     // When building a module MainFileName is the name of the modulemap file.
160*700637cbSDimitry Andric     CodeGenOpts.MainFileName =
161*700637cbSDimitry Andric         LangOpts.CurrentModule.empty() ? MainFileName : LangOpts.CurrentModule;
162*700637cbSDimitry Andric     CodeGenOpts.setDebugInfo(llvm::codegenoptions::FullDebugInfo);
163*700637cbSDimitry Andric     CodeGenOpts.setDebuggerTuning(CI.getCodeGenOpts().getDebuggerTuning());
164*700637cbSDimitry Andric     CodeGenOpts.DwarfVersion = CI.getCodeGenOpts().DwarfVersion;
165*700637cbSDimitry Andric     CodeGenOpts.DebugCompilationDir =
166*700637cbSDimitry Andric         CI.getInvocation().getCodeGenOpts().DebugCompilationDir;
167*700637cbSDimitry Andric     CodeGenOpts.DebugPrefixMap =
168*700637cbSDimitry Andric         CI.getInvocation().getCodeGenOpts().DebugPrefixMap;
169*700637cbSDimitry Andric     CodeGenOpts.DebugStrictDwarf = CI.getCodeGenOpts().DebugStrictDwarf;
170*700637cbSDimitry Andric   }
171*700637cbSDimitry Andric 
172*700637cbSDimitry Andric   ~PCHContainerGenerator() override = default;
173*700637cbSDimitry Andric 
Initialize(ASTContext & Context)174*700637cbSDimitry Andric   void Initialize(ASTContext &Context) override {
175*700637cbSDimitry Andric     assert(!Ctx && "initialized multiple times");
176*700637cbSDimitry Andric 
177*700637cbSDimitry Andric     Ctx = &Context;
178*700637cbSDimitry Andric     VMContext.reset(new llvm::LLVMContext());
179*700637cbSDimitry Andric     M.reset(new llvm::Module(MainFileName, *VMContext));
180*700637cbSDimitry Andric     M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
181*700637cbSDimitry Andric     Builder.reset(new CodeGen::CodeGenModule(
182*700637cbSDimitry Andric         *Ctx, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
183*700637cbSDimitry Andric 
184*700637cbSDimitry Andric     // Prepare CGDebugInfo to emit debug info for a clang module.
185*700637cbSDimitry Andric     auto *DI = Builder->getModuleDebugInfo();
186*700637cbSDimitry Andric     StringRef ModuleName = llvm::sys::path::filename(MainFileName);
187*700637cbSDimitry Andric     DI->setPCHDescriptor(
188*700637cbSDimitry Andric         {ModuleName, "", OutputFileName, ASTFileSignature::createDISentinel()});
189*700637cbSDimitry Andric     DI->setModuleMap(MMap);
190*700637cbSDimitry Andric   }
191*700637cbSDimitry Andric 
HandleTopLevelDecl(DeclGroupRef D)192*700637cbSDimitry Andric   bool HandleTopLevelDecl(DeclGroupRef D) override {
193*700637cbSDimitry Andric     if (Diags.hasErrorOccurred())
194*700637cbSDimitry Andric       return true;
195*700637cbSDimitry Andric 
196*700637cbSDimitry Andric     // Collect debug info for all decls in this group.
197*700637cbSDimitry Andric     for (auto *I : D)
198*700637cbSDimitry Andric       if (!I->isFromASTFile()) {
199*700637cbSDimitry Andric         DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
200*700637cbSDimitry Andric         DTV.TraverseDecl(I);
201*700637cbSDimitry Andric       }
202*700637cbSDimitry Andric     return true;
203*700637cbSDimitry Andric   }
204*700637cbSDimitry Andric 
HandleTopLevelDeclInObjCContainer(DeclGroupRef D)205*700637cbSDimitry Andric   void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
206*700637cbSDimitry Andric     HandleTopLevelDecl(D);
207*700637cbSDimitry Andric   }
208*700637cbSDimitry Andric 
HandleTagDeclDefinition(TagDecl * D)209*700637cbSDimitry Andric   void HandleTagDeclDefinition(TagDecl *D) override {
210*700637cbSDimitry Andric     if (Diags.hasErrorOccurred())
211*700637cbSDimitry Andric       return;
212*700637cbSDimitry Andric 
213*700637cbSDimitry Andric     if (D->isFromASTFile())
214*700637cbSDimitry Andric       return;
215*700637cbSDimitry Andric 
216*700637cbSDimitry Andric     // Anonymous tag decls are deferred until we are building their declcontext.
217*700637cbSDimitry Andric     if (D->getName().empty())
218*700637cbSDimitry Andric       return;
219*700637cbSDimitry Andric 
220*700637cbSDimitry Andric     // Defer tag decls until their declcontext is complete.
221*700637cbSDimitry Andric     auto *DeclCtx = D->getDeclContext();
222*700637cbSDimitry Andric     while (DeclCtx) {
223*700637cbSDimitry Andric       if (auto *D = dyn_cast<TagDecl>(DeclCtx))
224*700637cbSDimitry Andric         if (!D->isCompleteDefinition())
225*700637cbSDimitry Andric           return;
226*700637cbSDimitry Andric       DeclCtx = DeclCtx->getParent();
227*700637cbSDimitry Andric     }
228*700637cbSDimitry Andric 
229*700637cbSDimitry Andric     DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
230*700637cbSDimitry Andric     DTV.TraverseDecl(D);
231*700637cbSDimitry Andric     Builder->UpdateCompletedType(D);
232*700637cbSDimitry Andric   }
233*700637cbSDimitry Andric 
HandleTagDeclRequiredDefinition(const TagDecl * D)234*700637cbSDimitry Andric   void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
235*700637cbSDimitry Andric     if (Diags.hasErrorOccurred())
236*700637cbSDimitry Andric       return;
237*700637cbSDimitry Andric 
238*700637cbSDimitry Andric     if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
239*700637cbSDimitry Andric       Builder->getModuleDebugInfo()->completeRequiredType(RD);
240*700637cbSDimitry Andric   }
241*700637cbSDimitry Andric 
HandleImplicitImportDecl(ImportDecl * D)242*700637cbSDimitry Andric   void HandleImplicitImportDecl(ImportDecl *D) override {
243*700637cbSDimitry Andric     if (!D->getImportedOwningModule())
244*700637cbSDimitry Andric       Builder->getModuleDebugInfo()->EmitImportDecl(*D);
245*700637cbSDimitry Andric   }
246*700637cbSDimitry Andric 
247*700637cbSDimitry Andric   /// Emit a container holding the serialized AST.
HandleTranslationUnit(ASTContext & Ctx)248*700637cbSDimitry Andric   void HandleTranslationUnit(ASTContext &Ctx) override {
249*700637cbSDimitry Andric     assert(M && VMContext && Builder);
250*700637cbSDimitry Andric     // Delete these on function exit.
251*700637cbSDimitry Andric     std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext);
252*700637cbSDimitry Andric     std::unique_ptr<llvm::Module> M = std::move(this->M);
253*700637cbSDimitry Andric     std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder);
254*700637cbSDimitry Andric 
255*700637cbSDimitry Andric     if (Diags.hasErrorOccurred())
256*700637cbSDimitry Andric       return;
257*700637cbSDimitry Andric 
258*700637cbSDimitry Andric     M->setTargetTriple(Ctx.getTargetInfo().getTriple());
259*700637cbSDimitry Andric     M->setDataLayout(Ctx.getTargetInfo().getDataLayoutString());
260*700637cbSDimitry Andric 
261*700637cbSDimitry Andric     // PCH files don't have a signature field in the control block,
262*700637cbSDimitry Andric     // but LLVM detects DWO CUs by looking for a non-zero DWO id.
263*700637cbSDimitry Andric     // We use the lower 64 bits for debug info.
264*700637cbSDimitry Andric 
265*700637cbSDimitry Andric     uint64_t Signature =
266*700637cbSDimitry Andric         Buffer->Signature ? Buffer->Signature.truncatedValue() : ~1ULL;
267*700637cbSDimitry Andric 
268*700637cbSDimitry Andric     Builder->getModuleDebugInfo()->setDwoId(Signature);
269*700637cbSDimitry Andric 
270*700637cbSDimitry Andric     // Finalize the Builder.
271*700637cbSDimitry Andric     if (Builder)
272*700637cbSDimitry Andric       Builder->Release();
273*700637cbSDimitry Andric 
274*700637cbSDimitry Andric     // Ensure the target exists.
275*700637cbSDimitry Andric     std::string Error;
276*700637cbSDimitry Andric     auto Triple = Ctx.getTargetInfo().getTriple();
277*700637cbSDimitry Andric     if (!llvm::TargetRegistry::lookupTarget(Triple, Error))
278*700637cbSDimitry Andric       llvm::report_fatal_error(llvm::Twine(Error));
279*700637cbSDimitry Andric 
280*700637cbSDimitry Andric     // Emit the serialized Clang AST into its own section.
281*700637cbSDimitry Andric     assert(Buffer->IsComplete && "serialization did not complete");
282*700637cbSDimitry Andric     auto &SerializedAST = Buffer->Data;
283*700637cbSDimitry Andric     auto Size = SerializedAST.size();
284*700637cbSDimitry Andric 
285*700637cbSDimitry Andric     if (Triple.isOSBinFormatWasm()) {
286*700637cbSDimitry Andric       // Emit __clangast in custom section instead of named data segment
287*700637cbSDimitry Andric       // to find it while iterating sections.
288*700637cbSDimitry Andric       // This could be avoided if all data segements (the wasm sense) were
289*700637cbSDimitry Andric       // represented as their own sections (in the llvm sense).
290*700637cbSDimitry Andric       // TODO: https://github.com/WebAssembly/tool-conventions/issues/138
291*700637cbSDimitry Andric       llvm::NamedMDNode *MD =
292*700637cbSDimitry Andric           M->getOrInsertNamedMetadata("wasm.custom_sections");
293*700637cbSDimitry Andric       llvm::Metadata *Ops[2] = {
294*700637cbSDimitry Andric           llvm::MDString::get(*VMContext, "__clangast"),
295*700637cbSDimitry Andric           llvm::MDString::get(*VMContext,
296*700637cbSDimitry Andric                               StringRef(SerializedAST.data(), Size))};
297*700637cbSDimitry Andric       auto *NameAndContent = llvm::MDTuple::get(*VMContext, Ops);
298*700637cbSDimitry Andric       MD->addOperand(NameAndContent);
299*700637cbSDimitry Andric     } else {
300*700637cbSDimitry Andric       auto Int8Ty = llvm::Type::getInt8Ty(*VMContext);
301*700637cbSDimitry Andric       auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
302*700637cbSDimitry Andric       auto *Data = llvm::ConstantDataArray::getString(
303*700637cbSDimitry Andric           *VMContext, StringRef(SerializedAST.data(), Size),
304*700637cbSDimitry Andric           /*AddNull=*/false);
305*700637cbSDimitry Andric       auto *ASTSym = new llvm::GlobalVariable(
306*700637cbSDimitry Andric           *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage,
307*700637cbSDimitry Andric           Data, "__clang_ast");
308*700637cbSDimitry Andric       // The on-disk hashtable needs to be aligned.
309*700637cbSDimitry Andric       ASTSym->setAlignment(llvm::Align(8));
310*700637cbSDimitry Andric 
311*700637cbSDimitry Andric       // Mach-O also needs a segment name.
312*700637cbSDimitry Andric       if (Triple.isOSBinFormatMachO())
313*700637cbSDimitry Andric         ASTSym->setSection("__CLANG,__clangast");
314*700637cbSDimitry Andric       // COFF has an eight character length limit.
315*700637cbSDimitry Andric       else if (Triple.isOSBinFormatCOFF())
316*700637cbSDimitry Andric         ASTSym->setSection("clangast");
317*700637cbSDimitry Andric       else
318*700637cbSDimitry Andric         ASTSym->setSection("__clangast");
319*700637cbSDimitry Andric     }
320*700637cbSDimitry Andric 
321*700637cbSDimitry Andric     LLVM_DEBUG({
322*700637cbSDimitry Andric       // Print the IR for the PCH container to the debug output.
323*700637cbSDimitry Andric       llvm::SmallString<0> Buffer;
324*700637cbSDimitry Andric       clang::emitBackendOutput(
325*700637cbSDimitry Andric           CI, CodeGenOpts, Ctx.getTargetInfo().getDataLayoutString(), M.get(),
326*700637cbSDimitry Andric           BackendAction::Backend_EmitLL, FS,
327*700637cbSDimitry Andric           std::make_unique<llvm::raw_svector_ostream>(Buffer));
328*700637cbSDimitry Andric       llvm::dbgs() << Buffer;
329*700637cbSDimitry Andric     });
330*700637cbSDimitry Andric 
331*700637cbSDimitry Andric     // Use the LLVM backend to emit the pch container.
332*700637cbSDimitry Andric     clang::emitBackendOutput(CI, CodeGenOpts,
333*700637cbSDimitry Andric                              Ctx.getTargetInfo().getDataLayoutString(), M.get(),
334*700637cbSDimitry Andric                              BackendAction::Backend_EmitObj, FS, std::move(OS));
335*700637cbSDimitry Andric 
336*700637cbSDimitry Andric     // Free the memory for the temporary buffer.
337*700637cbSDimitry Andric     llvm::SmallVector<char, 0> Empty;
338*700637cbSDimitry Andric     SerializedAST = std::move(Empty);
339*700637cbSDimitry Andric   }
340*700637cbSDimitry Andric };
341*700637cbSDimitry Andric 
342*700637cbSDimitry Andric } // anonymous namespace
343*700637cbSDimitry Andric 
344*700637cbSDimitry 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) const345*700637cbSDimitry Andric ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
346*700637cbSDimitry Andric     CompilerInstance &CI, const std::string &MainFileName,
347*700637cbSDimitry Andric     const std::string &OutputFileName,
348*700637cbSDimitry Andric     std::unique_ptr<llvm::raw_pwrite_stream> OS,
349*700637cbSDimitry Andric     std::shared_ptr<PCHBuffer> Buffer) const {
350*700637cbSDimitry Andric   return std::make_unique<PCHContainerGenerator>(
351*700637cbSDimitry Andric       CI, MainFileName, OutputFileName, std::move(OS), Buffer);
352*700637cbSDimitry Andric }
353