1*67e74705SXin Li //===--- ObjectFilePCHContainerOperations.cpp -----------------------------===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li
10*67e74705SXin Li #include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
11*67e74705SXin Li #include "CGDebugInfo.h"
12*67e74705SXin Li #include "CodeGenModule.h"
13*67e74705SXin Li #include "clang/AST/ASTContext.h"
14*67e74705SXin Li #include "clang/AST/DeclObjC.h"
15*67e74705SXin Li #include "clang/AST/Expr.h"
16*67e74705SXin Li #include "clang/AST/RecursiveASTVisitor.h"
17*67e74705SXin Li #include "clang/Basic/Diagnostic.h"
18*67e74705SXin Li #include "clang/Basic/TargetInfo.h"
19*67e74705SXin Li #include "clang/CodeGen/BackendUtil.h"
20*67e74705SXin Li #include "clang/Frontend/CodeGenOptions.h"
21*67e74705SXin Li #include "clang/Frontend/CompilerInstance.h"
22*67e74705SXin Li #include "clang/Lex/HeaderSearch.h"
23*67e74705SXin Li #include "clang/Lex/Preprocessor.h"
24*67e74705SXin Li #include "clang/Serialization/ASTWriter.h"
25*67e74705SXin Li #include "llvm/ADT/StringRef.h"
26*67e74705SXin Li #include "llvm/Bitcode/BitstreamReader.h"
27*67e74705SXin Li #include "llvm/DebugInfo/DWARF/DWARFContext.h"
28*67e74705SXin Li #include "llvm/IR/Constants.h"
29*67e74705SXin Li #include "llvm/IR/DataLayout.h"
30*67e74705SXin Li #include "llvm/IR/LLVMContext.h"
31*67e74705SXin Li #include "llvm/IR/Module.h"
32*67e74705SXin Li #include "llvm/Object/COFF.h"
33*67e74705SXin Li #include "llvm/Object/ObjectFile.h"
34*67e74705SXin Li #include "llvm/Support/Path.h"
35*67e74705SXin Li #include "llvm/Support/TargetRegistry.h"
36*67e74705SXin Li #include <memory>
37*67e74705SXin Li #include <utility>
38*67e74705SXin Li
39*67e74705SXin Li using namespace clang;
40*67e74705SXin Li
41*67e74705SXin Li #define DEBUG_TYPE "pchcontainer"
42*67e74705SXin Li
43*67e74705SXin Li namespace {
44*67e74705SXin Li class PCHContainerGenerator : public ASTConsumer {
45*67e74705SXin Li DiagnosticsEngine &Diags;
46*67e74705SXin Li const std::string MainFileName;
47*67e74705SXin Li const std::string OutputFileName;
48*67e74705SXin Li ASTContext *Ctx;
49*67e74705SXin Li ModuleMap &MMap;
50*67e74705SXin Li const HeaderSearchOptions &HeaderSearchOpts;
51*67e74705SXin Li const PreprocessorOptions &PreprocessorOpts;
52*67e74705SXin Li CodeGenOptions CodeGenOpts;
53*67e74705SXin Li const TargetOptions TargetOpts;
54*67e74705SXin Li const LangOptions LangOpts;
55*67e74705SXin Li std::unique_ptr<llvm::LLVMContext> VMContext;
56*67e74705SXin Li std::unique_ptr<llvm::Module> M;
57*67e74705SXin Li std::unique_ptr<CodeGen::CodeGenModule> Builder;
58*67e74705SXin Li raw_pwrite_stream *OS;
59*67e74705SXin Li std::shared_ptr<PCHBuffer> Buffer;
60*67e74705SXin Li
61*67e74705SXin Li /// Visit every type and emit debug info for it.
62*67e74705SXin Li struct DebugTypeVisitor : public RecursiveASTVisitor<DebugTypeVisitor> {
63*67e74705SXin Li clang::CodeGen::CGDebugInfo &DI;
64*67e74705SXin Li ASTContext &Ctx;
DebugTypeVisitor__anon2a3cd20f0111::PCHContainerGenerator::DebugTypeVisitor65*67e74705SXin Li DebugTypeVisitor(clang::CodeGen::CGDebugInfo &DI, ASTContext &Ctx)
66*67e74705SXin Li : DI(DI), Ctx(Ctx) {}
67*67e74705SXin Li
68*67e74705SXin Li /// Determine whether this type can be represented in DWARF.
CanRepresent__anon2a3cd20f0111::PCHContainerGenerator::DebugTypeVisitor69*67e74705SXin Li static bool CanRepresent(const Type *Ty) {
70*67e74705SXin Li return !Ty->isDependentType() && !Ty->isUndeducedType();
71*67e74705SXin Li }
72*67e74705SXin Li
VisitImportDecl__anon2a3cd20f0111::PCHContainerGenerator::DebugTypeVisitor73*67e74705SXin Li bool VisitImportDecl(ImportDecl *D) {
74*67e74705SXin Li auto *Import = cast<ImportDecl>(D);
75*67e74705SXin Li if (!Import->getImportedOwningModule())
76*67e74705SXin Li DI.EmitImportDecl(*Import);
77*67e74705SXin Li return true;
78*67e74705SXin Li }
79*67e74705SXin Li
VisitTypeDecl__anon2a3cd20f0111::PCHContainerGenerator::DebugTypeVisitor80*67e74705SXin Li bool VisitTypeDecl(TypeDecl *D) {
81*67e74705SXin Li // TagDecls may be deferred until after all decls have been merged and we
82*67e74705SXin Li // know the complete type. Pure forward declarations will be skipped, but
83*67e74705SXin Li // they don't need to be emitted into the module anyway.
84*67e74705SXin Li if (auto *TD = dyn_cast<TagDecl>(D))
85*67e74705SXin Li if (!TD->isCompleteDefinition())
86*67e74705SXin Li return true;
87*67e74705SXin Li
88*67e74705SXin Li QualType QualTy = Ctx.getTypeDeclType(D);
89*67e74705SXin Li if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
90*67e74705SXin Li DI.getOrCreateStandaloneType(QualTy, D->getLocation());
91*67e74705SXin Li return true;
92*67e74705SXin Li }
93*67e74705SXin Li
VisitObjCInterfaceDecl__anon2a3cd20f0111::PCHContainerGenerator::DebugTypeVisitor94*67e74705SXin Li bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
95*67e74705SXin Li QualType QualTy(D->getTypeForDecl(), 0);
96*67e74705SXin Li if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
97*67e74705SXin Li DI.getOrCreateStandaloneType(QualTy, D->getLocation());
98*67e74705SXin Li return true;
99*67e74705SXin Li }
100*67e74705SXin Li
VisitFunctionDecl__anon2a3cd20f0111::PCHContainerGenerator::DebugTypeVisitor101*67e74705SXin Li bool VisitFunctionDecl(FunctionDecl *D) {
102*67e74705SXin Li if (isa<CXXMethodDecl>(D))
103*67e74705SXin Li // This is not yet supported. Constructing the `this' argument
104*67e74705SXin Li // mandates a CodeGenFunction.
105*67e74705SXin Li return true;
106*67e74705SXin Li
107*67e74705SXin Li SmallVector<QualType, 16> ArgTypes;
108*67e74705SXin Li for (auto i : D->parameters())
109*67e74705SXin Li ArgTypes.push_back(i->getType());
110*67e74705SXin Li QualType RetTy = D->getReturnType();
111*67e74705SXin Li QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
112*67e74705SXin Li FunctionProtoType::ExtProtoInfo());
113*67e74705SXin Li if (CanRepresent(FnTy.getTypePtr()))
114*67e74705SXin Li DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
115*67e74705SXin Li return true;
116*67e74705SXin Li }
117*67e74705SXin Li
VisitObjCMethodDecl__anon2a3cd20f0111::PCHContainerGenerator::DebugTypeVisitor118*67e74705SXin Li bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
119*67e74705SXin Li if (!D->getClassInterface())
120*67e74705SXin Li return true;
121*67e74705SXin Li
122*67e74705SXin Li bool selfIsPseudoStrong, selfIsConsumed;
123*67e74705SXin Li SmallVector<QualType, 16> ArgTypes;
124*67e74705SXin Li ArgTypes.push_back(D->getSelfType(Ctx, D->getClassInterface(),
125*67e74705SXin Li selfIsPseudoStrong, selfIsConsumed));
126*67e74705SXin Li ArgTypes.push_back(Ctx.getObjCSelType());
127*67e74705SXin Li for (auto i : D->parameters())
128*67e74705SXin Li ArgTypes.push_back(i->getType());
129*67e74705SXin Li QualType RetTy = D->getReturnType();
130*67e74705SXin Li QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
131*67e74705SXin Li FunctionProtoType::ExtProtoInfo());
132*67e74705SXin Li if (CanRepresent(FnTy.getTypePtr()))
133*67e74705SXin Li DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
134*67e74705SXin Li return true;
135*67e74705SXin Li }
136*67e74705SXin Li };
137*67e74705SXin Li
138*67e74705SXin Li public:
PCHContainerGenerator(CompilerInstance & CI,const std::string & MainFileName,const std::string & OutputFileName,raw_pwrite_stream * OS,std::shared_ptr<PCHBuffer> Buffer)139*67e74705SXin Li PCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName,
140*67e74705SXin Li const std::string &OutputFileName,
141*67e74705SXin Li raw_pwrite_stream *OS,
142*67e74705SXin Li std::shared_ptr<PCHBuffer> Buffer)
143*67e74705SXin Li : Diags(CI.getDiagnostics()), MainFileName(MainFileName),
144*67e74705SXin Li OutputFileName(OutputFileName), Ctx(nullptr),
145*67e74705SXin Li MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
146*67e74705SXin Li HeaderSearchOpts(CI.getHeaderSearchOpts()),
147*67e74705SXin Li PreprocessorOpts(CI.getPreprocessorOpts()),
148*67e74705SXin Li TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()), OS(OS),
149*67e74705SXin Li Buffer(std::move(Buffer)) {
150*67e74705SXin Li // The debug info output isn't affected by CodeModel and
151*67e74705SXin Li // ThreadModel, but the backend expects them to be nonempty.
152*67e74705SXin Li CodeGenOpts.CodeModel = "default";
153*67e74705SXin Li CodeGenOpts.ThreadModel = "single";
154*67e74705SXin Li CodeGenOpts.DebugTypeExtRefs = true;
155*67e74705SXin Li CodeGenOpts.setDebugInfo(codegenoptions::FullDebugInfo);
156*67e74705SXin Li CodeGenOpts.setDebuggerTuning(CI.getCodeGenOpts().getDebuggerTuning());
157*67e74705SXin Li }
158*67e74705SXin Li
159*67e74705SXin Li ~PCHContainerGenerator() override = default;
160*67e74705SXin Li
Initialize(ASTContext & Context)161*67e74705SXin Li void Initialize(ASTContext &Context) override {
162*67e74705SXin Li assert(!Ctx && "initialized multiple times");
163*67e74705SXin Li
164*67e74705SXin Li Ctx = &Context;
165*67e74705SXin Li VMContext.reset(new llvm::LLVMContext());
166*67e74705SXin Li M.reset(new llvm::Module(MainFileName, *VMContext));
167*67e74705SXin Li M->setDataLayout(Ctx->getTargetInfo().getDataLayout());
168*67e74705SXin Li Builder.reset(new CodeGen::CodeGenModule(
169*67e74705SXin Li *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
170*67e74705SXin Li
171*67e74705SXin Li // Prepare CGDebugInfo to emit debug info for a clang module.
172*67e74705SXin Li auto *DI = Builder->getModuleDebugInfo();
173*67e74705SXin Li StringRef ModuleName = llvm::sys::path::filename(MainFileName);
174*67e74705SXin Li DI->setPCHDescriptor({ModuleName, "", OutputFileName, ~1ULL});
175*67e74705SXin Li DI->setModuleMap(MMap);
176*67e74705SXin Li }
177*67e74705SXin Li
HandleTopLevelDecl(DeclGroupRef D)178*67e74705SXin Li bool HandleTopLevelDecl(DeclGroupRef D) override {
179*67e74705SXin Li if (Diags.hasErrorOccurred())
180*67e74705SXin Li return true;
181*67e74705SXin Li
182*67e74705SXin Li // Collect debug info for all decls in this group.
183*67e74705SXin Li for (auto *I : D)
184*67e74705SXin Li if (!I->isFromASTFile()) {
185*67e74705SXin Li DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
186*67e74705SXin Li DTV.TraverseDecl(I);
187*67e74705SXin Li }
188*67e74705SXin Li return true;
189*67e74705SXin Li }
190*67e74705SXin Li
HandleTopLevelDeclInObjCContainer(DeclGroupRef D)191*67e74705SXin Li void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
192*67e74705SXin Li HandleTopLevelDecl(D);
193*67e74705SXin Li }
194*67e74705SXin Li
HandleTagDeclDefinition(TagDecl * D)195*67e74705SXin Li void HandleTagDeclDefinition(TagDecl *D) override {
196*67e74705SXin Li if (Diags.hasErrorOccurred())
197*67e74705SXin Li return;
198*67e74705SXin Li
199*67e74705SXin Li if (D->isFromASTFile())
200*67e74705SXin Li return;
201*67e74705SXin Li
202*67e74705SXin Li // Anonymous tag decls are deferred until we are building their declcontext.
203*67e74705SXin Li if (D->getName().empty())
204*67e74705SXin Li return;
205*67e74705SXin Li
206*67e74705SXin Li // Defer tag decls until their declcontext is complete.
207*67e74705SXin Li auto *DeclCtx = D->getDeclContext();
208*67e74705SXin Li while (DeclCtx) {
209*67e74705SXin Li if (auto *D = dyn_cast<TagDecl>(DeclCtx))
210*67e74705SXin Li if (!D->isCompleteDefinition())
211*67e74705SXin Li return;
212*67e74705SXin Li DeclCtx = DeclCtx->getParent();
213*67e74705SXin Li }
214*67e74705SXin Li
215*67e74705SXin Li DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
216*67e74705SXin Li DTV.TraverseDecl(D);
217*67e74705SXin Li Builder->UpdateCompletedType(D);
218*67e74705SXin Li }
219*67e74705SXin Li
HandleTagDeclRequiredDefinition(const TagDecl * D)220*67e74705SXin Li void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
221*67e74705SXin Li if (Diags.hasErrorOccurred())
222*67e74705SXin Li return;
223*67e74705SXin Li
224*67e74705SXin Li if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
225*67e74705SXin Li Builder->getModuleDebugInfo()->completeRequiredType(RD);
226*67e74705SXin Li }
227*67e74705SXin Li
228*67e74705SXin Li /// Emit a container holding the serialized AST.
HandleTranslationUnit(ASTContext & Ctx)229*67e74705SXin Li void HandleTranslationUnit(ASTContext &Ctx) override {
230*67e74705SXin Li assert(M && VMContext && Builder);
231*67e74705SXin Li // Delete these on function exit.
232*67e74705SXin Li std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext);
233*67e74705SXin Li std::unique_ptr<llvm::Module> M = std::move(this->M);
234*67e74705SXin Li std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder);
235*67e74705SXin Li
236*67e74705SXin Li if (Diags.hasErrorOccurred())
237*67e74705SXin Li return;
238*67e74705SXin Li
239*67e74705SXin Li M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple());
240*67e74705SXin Li M->setDataLayout(Ctx.getTargetInfo().getDataLayout());
241*67e74705SXin Li
242*67e74705SXin Li // PCH files don't have a signature field in the control block,
243*67e74705SXin Li // but LLVM detects DWO CUs by looking for a non-zero DWO id.
244*67e74705SXin Li uint64_t Signature = Buffer->Signature ? Buffer->Signature : ~1ULL;
245*67e74705SXin Li Builder->getModuleDebugInfo()->setDwoId(Signature);
246*67e74705SXin Li
247*67e74705SXin Li // Finalize the Builder.
248*67e74705SXin Li if (Builder)
249*67e74705SXin Li Builder->Release();
250*67e74705SXin Li
251*67e74705SXin Li // Ensure the target exists.
252*67e74705SXin Li std::string Error;
253*67e74705SXin Li auto Triple = Ctx.getTargetInfo().getTriple();
254*67e74705SXin Li if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
255*67e74705SXin Li llvm::report_fatal_error(Error);
256*67e74705SXin Li
257*67e74705SXin Li // Emit the serialized Clang AST into its own section.
258*67e74705SXin Li assert(Buffer->IsComplete && "serialization did not complete");
259*67e74705SXin Li auto &SerializedAST = Buffer->Data;
260*67e74705SXin Li auto Size = SerializedAST.size();
261*67e74705SXin Li auto Int8Ty = llvm::Type::getInt8Ty(*VMContext);
262*67e74705SXin Li auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
263*67e74705SXin Li auto *Data = llvm::ConstantDataArray::getString(
264*67e74705SXin Li *VMContext, StringRef(SerializedAST.data(), Size),
265*67e74705SXin Li /*AddNull=*/false);
266*67e74705SXin Li auto *ASTSym = new llvm::GlobalVariable(
267*67e74705SXin Li *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data,
268*67e74705SXin Li "__clang_ast");
269*67e74705SXin Li // The on-disk hashtable needs to be aligned.
270*67e74705SXin Li ASTSym->setAlignment(8);
271*67e74705SXin Li
272*67e74705SXin Li // Mach-O also needs a segment name.
273*67e74705SXin Li if (Triple.isOSBinFormatMachO())
274*67e74705SXin Li ASTSym->setSection("__CLANG,__clangast");
275*67e74705SXin Li // COFF has an eight character length limit.
276*67e74705SXin Li else if (Triple.isOSBinFormatCOFF())
277*67e74705SXin Li ASTSym->setSection("clangast");
278*67e74705SXin Li else
279*67e74705SXin Li ASTSym->setSection("__clangast");
280*67e74705SXin Li
281*67e74705SXin Li DEBUG({
282*67e74705SXin Li // Print the IR for the PCH container to the debug output.
283*67e74705SXin Li llvm::SmallString<0> Buffer;
284*67e74705SXin Li llvm::raw_svector_ostream OS(Buffer);
285*67e74705SXin Li clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
286*67e74705SXin Li Ctx.getTargetInfo().getDataLayout(), M.get(),
287*67e74705SXin Li BackendAction::Backend_EmitLL, &OS);
288*67e74705SXin Li llvm::dbgs() << Buffer;
289*67e74705SXin Li });
290*67e74705SXin Li
291*67e74705SXin Li // Use the LLVM backend to emit the pch container.
292*67e74705SXin Li clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
293*67e74705SXin Li Ctx.getTargetInfo().getDataLayout(), M.get(),
294*67e74705SXin Li BackendAction::Backend_EmitObj, OS);
295*67e74705SXin Li
296*67e74705SXin Li // Make sure the pch container hits disk.
297*67e74705SXin Li OS->flush();
298*67e74705SXin Li
299*67e74705SXin Li // Free the memory for the temporary buffer.
300*67e74705SXin Li llvm::SmallVector<char, 0> Empty;
301*67e74705SXin Li SerializedAST = std::move(Empty);
302*67e74705SXin Li }
303*67e74705SXin Li };
304*67e74705SXin Li
305*67e74705SXin Li } // anonymous namespace
306*67e74705SXin Li
307*67e74705SXin Li std::unique_ptr<ASTConsumer>
CreatePCHContainerGenerator(CompilerInstance & CI,const std::string & MainFileName,const std::string & OutputFileName,llvm::raw_pwrite_stream * OS,std::shared_ptr<PCHBuffer> Buffer) const308*67e74705SXin Li ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
309*67e74705SXin Li CompilerInstance &CI, const std::string &MainFileName,
310*67e74705SXin Li const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
311*67e74705SXin Li std::shared_ptr<PCHBuffer> Buffer) const {
312*67e74705SXin Li return llvm::make_unique<PCHContainerGenerator>(CI, MainFileName,
313*67e74705SXin Li OutputFileName, OS, Buffer);
314*67e74705SXin Li }
315*67e74705SXin Li
ExtractPCH(llvm::MemoryBufferRef Buffer,llvm::BitstreamReader & StreamFile) const316*67e74705SXin Li void ObjectFilePCHContainerReader::ExtractPCH(
317*67e74705SXin Li llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
318*67e74705SXin Li if (auto OF = llvm::object::ObjectFile::createObjectFile(Buffer)) {
319*67e74705SXin Li auto *Obj = OF.get().get();
320*67e74705SXin Li bool IsCOFF = isa<llvm::object::COFFObjectFile>(Obj);
321*67e74705SXin Li // Find the clang AST section in the container.
322*67e74705SXin Li for (auto &Section : OF->get()->sections()) {
323*67e74705SXin Li StringRef Name;
324*67e74705SXin Li Section.getName(Name);
325*67e74705SXin Li if ((!IsCOFF && Name == "__clangast") ||
326*67e74705SXin Li ( IsCOFF && Name == "clangast")) {
327*67e74705SXin Li StringRef Buf;
328*67e74705SXin Li Section.getContents(Buf);
329*67e74705SXin Li StreamFile.init((const unsigned char *)Buf.begin(),
330*67e74705SXin Li (const unsigned char *)Buf.end());
331*67e74705SXin Li return;
332*67e74705SXin Li }
333*67e74705SXin Li }
334*67e74705SXin Li }
335*67e74705SXin Li
336*67e74705SXin Li // As a fallback, treat the buffer as a raw AST.
337*67e74705SXin Li StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
338*67e74705SXin Li (const unsigned char *)Buffer.getBufferEnd());
339*67e74705SXin Li }
340