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