blob: b397eb352a60a8f5289c1ec3926e4a868bda5f85 [file] [log] [blame]
Adrian Prantlbc068582015-07-08 01:00:30 +00001//===--- 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"
Adrian Prantl03914062015-09-18 22:10:59 +000021#include "clang/Frontend/CompilerInstance.h"
Adrian Prantl9402cef2015-09-20 16:51:35 +000022#include "clang/Lex/Preprocessor.h"
23#include "clang/Lex/HeaderSearch.h"
Adrian Prantlbc068582015-07-08 01:00:30 +000024#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>
Hans Wennborg7eb54642015-09-10 17:07:54 +000036
Adrian Prantlbc068582015-07-08 01:00:30 +000037using namespace clang;
38
39#define DEBUG_TYPE "pchcontainer"
40
41namespace {
Adrian Prantl5a88e1a2015-07-09 19:46:39 +000042class PCHContainerGenerator : public ASTConsumer {
Adrian Prantlbc068582015-07-08 01:00:30 +000043 DiagnosticsEngine &Diags;
44 const std::string MainFileName;
45 ASTContext *Ctx;
Adrian Prantl9402cef2015-09-20 16:51:35 +000046 ModuleMap &MMap;
Adrian Prantlbc068582015-07-08 01:00:30 +000047 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
Adrian Prantl4aa2b3a2015-09-08 19:20:27 +000058 /// 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
Adrian Prantl85d938a2015-09-21 17:48:37 +000070 bool VisitImportDecl(ImportDecl *D) {
71 auto *Import = cast<ImportDecl>(D);
72 if (!Import->getImportedOwningModule())
73 DI.EmitImportDecl(*Import);
74 return true;
75 }
76
Adrian Prantl4aa2b3a2015-09-08 19:20:27 +000077 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
Adrian Prantl748a6cd2015-09-08 20:41:52 +000084 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 }
Adrian Prantl4aa2b3a2015-09-08 19:20:27 +0000126 };
127
Adrian Prantlbc068582015-07-08 01:00:30 +0000128public:
Adrian Prantl1e63b2b2015-09-19 21:42:52 +0000129 PCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName,
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000130 const std::string &OutputFileName,
131 raw_pwrite_stream *OS,
132 std::shared_ptr<PCHBuffer> Buffer)
Adrian Prantl1e63b2b2015-09-19 21:42:52 +0000133 : Diags(CI.getDiagnostics()), Ctx(nullptr),
Adrian Prantl9402cef2015-09-20 16:51:35 +0000134 MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
Adrian Prantl1e63b2b2015-09-19 21:42:52 +0000135 HeaderSearchOpts(CI.getHeaderSearchOpts()),
Adrian Prantl03914062015-09-18 22:10:59 +0000136 PreprocessorOpts(CI.getPreprocessorOpts()),
137 TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()), OS(OS),
138 Buffer(Buffer) {
Adrian Prantlbc068582015-07-08 01:00:30 +0000139 // 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";
Adrian Prantl6b21ab22015-08-27 19:46:20 +0000143 CodeGenOpts.DebugTypeExtRefs = true;
Adrian Prantlbc068582015-07-08 01:00:30 +0000144 CodeGenOpts.setDebugInfo(CodeGenOptions::FullDebugInfo);
Adrian Prantlbc068582015-07-08 01:00:30 +0000145 }
146
Hans Wennborg7eb54642015-09-10 17:07:54 +0000147 ~PCHContainerGenerator() override = default;
Adrian Prantlbc068582015-07-08 01:00:30 +0000148
149 void Initialize(ASTContext &Context) override {
Richard Smith293534b2015-08-18 20:39:29 +0000150 assert(!Ctx && "initialized multiple times");
Richard Smith0f99d6a2015-08-09 08:48:41 +0000151
Adrian Prantlbc068582015-07-08 01:00:30 +0000152 Ctx = &Context;
153 VMContext.reset(new llvm::LLVMContext());
154 M.reset(new llvm::Module(MainFileName, *VMContext));
Eric Christopher964a5f32015-08-05 23:48:05 +0000155 M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
Mehdi Aminica3cf9e2015-07-24 16:04:29 +0000156 Builder.reset(new CodeGen::CodeGenModule(
157 *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
Adrian Prantl9402cef2015-09-20 16:51:35 +0000158 Builder->getModuleDebugInfo()->setModuleMap(MMap);
Adrian Prantlbc068582015-07-08 01:00:30 +0000159 }
160
Adrian Prantl4aa2b3a2015-09-08 19:20:27 +0000161 bool HandleTopLevelDecl(DeclGroupRef D) override {
Adrian Prantlabdd6fc2015-10-23 16:51:32 +0000162 if (Diags.hasErrorOccurred())
Adrian Prantl4aa2b3a2015-09-08 19:20:27 +0000163 return true;
164
165 // Collect debug info for all decls in this group.
166 for (auto *I : D)
167 if (!I->isFromASTFile()) {
168 DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
169 DTV.TraverseDecl(I);
170 }
171 return true;
172 }
173
Adrian Prantld43fe0b2015-10-23 17:02:22 +0000174 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
175 HandleTopLevelDecl(D);
176 }
177
Adrian Prantl4aa2b3a2015-09-08 19:20:27 +0000178 void HandleTagDeclDefinition(TagDecl *D) override {
179 if (Diags.hasErrorOccurred())
180 return;
181
182 Builder->UpdateCompletedType(D);
183 }
184
185 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
186 if (Diags.hasErrorOccurred())
187 return;
188
Adrian Prantl8bd4c132015-09-19 00:10:25 +0000189 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
190 Builder->getModuleDebugInfo()->completeRequiredType(RD);
Adrian Prantl4aa2b3a2015-09-08 19:20:27 +0000191 }
192
Adrian Prantlbc068582015-07-08 01:00:30 +0000193 /// Emit a container holding the serialized AST.
194 void HandleTranslationUnit(ASTContext &Ctx) override {
195 assert(M && VMContext && Builder);
196 // Delete these on function exit.
197 std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext);
198 std::unique_ptr<llvm::Module> M = std::move(this->M);
199 std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder);
200
201 if (Diags.hasErrorOccurred())
202 return;
203
204 M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple());
Eric Christopher964a5f32015-08-05 23:48:05 +0000205 M->setDataLayout(Ctx.getTargetInfo().getDataLayoutString());
Adrian Prantla5206ce2015-09-22 23:26:43 +0000206 Builder->getModuleDebugInfo()->setDwoId(Buffer->Signature);
Adrian Prantlbc068582015-07-08 01:00:30 +0000207
208 // Finalize the Builder.
209 if (Builder)
210 Builder->Release();
211
212 // Ensure the target exists.
213 std::string Error;
214 auto Triple = Ctx.getTargetInfo().getTriple();
215 if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
216 llvm::report_fatal_error(Error);
217
218 // Emit the serialized Clang AST into its own section.
219 assert(Buffer->IsComplete && "serialization did not complete");
220 auto &SerializedAST = Buffer->Data;
221 auto Size = SerializedAST.size();
222 auto Int8Ty = llvm::Type::getInt8Ty(*VMContext);
223 auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000224 auto *Data = llvm::ConstantDataArray::getString(
225 *VMContext, StringRef(SerializedAST.data(), Size),
226 /*AddNull=*/false);
Adrian Prantlbc068582015-07-08 01:00:30 +0000227 auto *ASTSym = new llvm::GlobalVariable(
228 *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data,
229 "__clang_ast");
230 // The on-disk hashtable needs to be aligned.
231 ASTSym->setAlignment(8);
232
233 // Mach-O also needs a segment name.
234 if (Triple.isOSBinFormatMachO())
235 ASTSym->setSection("__CLANG,__clangast");
236 // COFF has an eight character length limit.
237 else if (Triple.isOSBinFormatCOFF())
238 ASTSym->setSection("clangast");
239 else
240 ASTSym->setSection("__clangast");
241
242 DEBUG({
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000243 // Print the IR for the PCH container to the debug output.
244 llvm::SmallString<0> Buffer;
245 llvm::raw_svector_ostream OS(Buffer);
246 clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Eric Christopher964a5f32015-08-05 23:48:05 +0000247 Ctx.getTargetInfo().getDataLayoutString(),
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000248 M.get(), BackendAction::Backend_EmitLL, &OS);
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000249 llvm::dbgs() << Buffer;
250 });
Adrian Prantlbc068582015-07-08 01:00:30 +0000251
252 // Use the LLVM backend to emit the pch container.
253 clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Eric Christopher964a5f32015-08-05 23:48:05 +0000254 Ctx.getTargetInfo().getDataLayoutString(),
Adrian Prantlbc068582015-07-08 01:00:30 +0000255 M.get(), BackendAction::Backend_EmitObj, OS);
256
257 // Make sure the pch container hits disk.
258 OS->flush();
259
260 // Free the memory for the temporary buffer.
261 llvm::SmallVector<char, 0> Empty;
262 SerializedAST = std::move(Empty);
263 }
264};
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000265
Hans Wennborg7eb54642015-09-10 17:07:54 +0000266} // anonymous namespace
Adrian Prantlbc068582015-07-08 01:00:30 +0000267
268std::unique_ptr<ASTConsumer>
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000269ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
Adrian Prantl1e63b2b2015-09-19 21:42:52 +0000270 CompilerInstance &CI, const std::string &MainFileName,
271 const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
272 std::shared_ptr<PCHBuffer> Buffer) const {
273 return llvm::make_unique<PCHContainerGenerator>(CI, MainFileName,
Adrian Prantl03914062015-09-18 22:10:59 +0000274 OutputFileName, OS, Buffer);
Adrian Prantlbc068582015-07-08 01:00:30 +0000275}
276
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000277void ObjectFilePCHContainerReader::ExtractPCH(
Adrian Prantlbc068582015-07-08 01:00:30 +0000278 llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
279 if (auto OF = llvm::object::ObjectFile::createObjectFile(Buffer)) {
280 auto *Obj = OF.get().get();
281 bool IsCOFF = isa<llvm::object::COFFObjectFile>(Obj);
282 // Find the clang AST section in the container.
283 for (auto &Section : OF->get()->sections()) {
284 StringRef Name;
285 Section.getName(Name);
286 if ((!IsCOFF && Name == "__clangast") ||
287 ( IsCOFF && Name == "clangast")) {
288 StringRef Buf;
289 Section.getContents(Buf);
290 StreamFile.init((const unsigned char *)Buf.begin(),
291 (const unsigned char *)Buf.end());
292 return;
293 }
294 }
295 }
296
297 // As a fallback, treat the buffer as a raw AST.
298 StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
299 (const unsigned char *)Buffer.getBufferEnd());
Adrian Prantlbc068582015-07-08 01:00:30 +0000300}