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