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