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