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