annotate clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp @ 152:e8a9b4f4d755

pull from 146
author anatofuz
date Wed, 11 Mar 2020 18:29:16 +0900
parents 1d019706d866
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- ObjectFilePCHContainerOperations.cpp -----------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
anatofuz
parents:
diff changeset
10 #include "CGDebugInfo.h"
anatofuz
parents:
diff changeset
11 #include "CodeGenModule.h"
anatofuz
parents:
diff changeset
12 #include "clang/AST/ASTContext.h"
anatofuz
parents:
diff changeset
13 #include "clang/AST/DeclObjC.h"
anatofuz
parents:
diff changeset
14 #include "clang/AST/Expr.h"
anatofuz
parents:
diff changeset
15 #include "clang/AST/RecursiveASTVisitor.h"
anatofuz
parents:
diff changeset
16 #include "clang/Basic/CodeGenOptions.h"
anatofuz
parents:
diff changeset
17 #include "clang/Basic/Diagnostic.h"
anatofuz
parents:
diff changeset
18 #include "clang/Basic/TargetInfo.h"
anatofuz
parents:
diff changeset
19 #include "clang/CodeGen/BackendUtil.h"
anatofuz
parents:
diff changeset
20 #include "clang/Frontend/CompilerInstance.h"
anatofuz
parents:
diff changeset
21 #include "clang/Lex/HeaderSearch.h"
anatofuz
parents:
diff changeset
22 #include "clang/Lex/Preprocessor.h"
anatofuz
parents:
diff changeset
23 #include "llvm/ADT/StringRef.h"
anatofuz
parents:
diff changeset
24 #include "llvm/Bitstream/BitstreamReader.h"
anatofuz
parents:
diff changeset
25 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
anatofuz
parents:
diff changeset
26 #include "llvm/IR/Constants.h"
anatofuz
parents:
diff changeset
27 #include "llvm/IR/DataLayout.h"
anatofuz
parents:
diff changeset
28 #include "llvm/IR/LLVMContext.h"
anatofuz
parents:
diff changeset
29 #include "llvm/IR/Module.h"
anatofuz
parents:
diff changeset
30 #include "llvm/Object/COFF.h"
anatofuz
parents:
diff changeset
31 #include "llvm/Object/ObjectFile.h"
anatofuz
parents:
diff changeset
32 #include "llvm/Support/Path.h"
anatofuz
parents:
diff changeset
33 #include "llvm/Support/TargetRegistry.h"
anatofuz
parents:
diff changeset
34 #include <memory>
anatofuz
parents:
diff changeset
35 #include <utility>
anatofuz
parents:
diff changeset
36
anatofuz
parents:
diff changeset
37 using namespace clang;
anatofuz
parents:
diff changeset
38
anatofuz
parents:
diff changeset
39 #define DEBUG_TYPE "pchcontainer"
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 namespace {
anatofuz
parents:
diff changeset
42 class PCHContainerGenerator : public ASTConsumer {
anatofuz
parents:
diff changeset
43 DiagnosticsEngine &Diags;
anatofuz
parents:
diff changeset
44 const std::string MainFileName;
anatofuz
parents:
diff changeset
45 const std::string OutputFileName;
anatofuz
parents:
diff changeset
46 ASTContext *Ctx;
anatofuz
parents:
diff changeset
47 ModuleMap &MMap;
anatofuz
parents:
diff changeset
48 const HeaderSearchOptions &HeaderSearchOpts;
anatofuz
parents:
diff changeset
49 const PreprocessorOptions &PreprocessorOpts;
anatofuz
parents:
diff changeset
50 CodeGenOptions CodeGenOpts;
anatofuz
parents:
diff changeset
51 const TargetOptions TargetOpts;
anatofuz
parents:
diff changeset
52 const LangOptions LangOpts;
anatofuz
parents:
diff changeset
53 std::unique_ptr<llvm::LLVMContext> VMContext;
anatofuz
parents:
diff changeset
54 std::unique_ptr<llvm::Module> M;
anatofuz
parents:
diff changeset
55 std::unique_ptr<CodeGen::CodeGenModule> Builder;
anatofuz
parents:
diff changeset
56 std::unique_ptr<raw_pwrite_stream> OS;
anatofuz
parents:
diff changeset
57 std::shared_ptr<PCHBuffer> Buffer;
anatofuz
parents:
diff changeset
58
anatofuz
parents:
diff changeset
59 /// Visit every type and emit debug info for it.
anatofuz
parents:
diff changeset
60 struct DebugTypeVisitor : public RecursiveASTVisitor<DebugTypeVisitor> {
anatofuz
parents:
diff changeset
61 clang::CodeGen::CGDebugInfo &DI;
anatofuz
parents:
diff changeset
62 ASTContext &Ctx;
anatofuz
parents:
diff changeset
63 DebugTypeVisitor(clang::CodeGen::CGDebugInfo &DI, ASTContext &Ctx)
anatofuz
parents:
diff changeset
64 : DI(DI), Ctx(Ctx) {}
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 /// Determine whether this type can be represented in DWARF.
anatofuz
parents:
diff changeset
67 static bool CanRepresent(const Type *Ty) {
anatofuz
parents:
diff changeset
68 return !Ty->isDependentType() && !Ty->isUndeducedType();
anatofuz
parents:
diff changeset
69 }
anatofuz
parents:
diff changeset
70
anatofuz
parents:
diff changeset
71 bool VisitImportDecl(ImportDecl *D) {
anatofuz
parents:
diff changeset
72 if (!D->getImportedOwningModule())
anatofuz
parents:
diff changeset
73 DI.EmitImportDecl(*D);
anatofuz
parents:
diff changeset
74 return true;
anatofuz
parents:
diff changeset
75 }
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 bool VisitTypeDecl(TypeDecl *D) {
anatofuz
parents:
diff changeset
78 // TagDecls may be deferred until after all decls have been merged and we
anatofuz
parents:
diff changeset
79 // know the complete type. Pure forward declarations will be skipped, but
anatofuz
parents:
diff changeset
80 // they don't need to be emitted into the module anyway.
anatofuz
parents:
diff changeset
81 if (auto *TD = dyn_cast<TagDecl>(D))
anatofuz
parents:
diff changeset
82 if (!TD->isCompleteDefinition())
anatofuz
parents:
diff changeset
83 return true;
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 QualType QualTy = Ctx.getTypeDeclType(D);
anatofuz
parents:
diff changeset
86 if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
anatofuz
parents:
diff changeset
87 DI.getOrCreateStandaloneType(QualTy, D->getLocation());
anatofuz
parents:
diff changeset
88 return true;
anatofuz
parents:
diff changeset
89 }
anatofuz
parents:
diff changeset
90
anatofuz
parents:
diff changeset
91 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
anatofuz
parents:
diff changeset
92 QualType QualTy(D->getTypeForDecl(), 0);
anatofuz
parents:
diff changeset
93 if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
anatofuz
parents:
diff changeset
94 DI.getOrCreateStandaloneType(QualTy, D->getLocation());
anatofuz
parents:
diff changeset
95 return true;
anatofuz
parents:
diff changeset
96 }
anatofuz
parents:
diff changeset
97
anatofuz
parents:
diff changeset
98 bool VisitFunctionDecl(FunctionDecl *D) {
anatofuz
parents:
diff changeset
99 if (isa<CXXMethodDecl>(D))
anatofuz
parents:
diff changeset
100 // This is not yet supported. Constructing the `this' argument
anatofuz
parents:
diff changeset
101 // mandates a CodeGenFunction.
anatofuz
parents:
diff changeset
102 return true;
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 SmallVector<QualType, 16> ArgTypes;
anatofuz
parents:
diff changeset
105 for (auto i : D->parameters())
anatofuz
parents:
diff changeset
106 ArgTypes.push_back(i->getType());
anatofuz
parents:
diff changeset
107 QualType RetTy = D->getReturnType();
anatofuz
parents:
diff changeset
108 QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
anatofuz
parents:
diff changeset
109 FunctionProtoType::ExtProtoInfo());
anatofuz
parents:
diff changeset
110 if (CanRepresent(FnTy.getTypePtr()))
anatofuz
parents:
diff changeset
111 DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
anatofuz
parents:
diff changeset
112 return true;
anatofuz
parents:
diff changeset
113 }
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
anatofuz
parents:
diff changeset
116 if (!D->getClassInterface())
anatofuz
parents:
diff changeset
117 return true;
anatofuz
parents:
diff changeset
118
anatofuz
parents:
diff changeset
119 bool selfIsPseudoStrong, selfIsConsumed;
anatofuz
parents:
diff changeset
120 SmallVector<QualType, 16> ArgTypes;
anatofuz
parents:
diff changeset
121 ArgTypes.push_back(D->getSelfType(Ctx, D->getClassInterface(),
anatofuz
parents:
diff changeset
122 selfIsPseudoStrong, selfIsConsumed));
anatofuz
parents:
diff changeset
123 ArgTypes.push_back(Ctx.getObjCSelType());
anatofuz
parents:
diff changeset
124 for (auto i : D->parameters())
anatofuz
parents:
diff changeset
125 ArgTypes.push_back(i->getType());
anatofuz
parents:
diff changeset
126 QualType RetTy = D->getReturnType();
anatofuz
parents:
diff changeset
127 QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
anatofuz
parents:
diff changeset
128 FunctionProtoType::ExtProtoInfo());
anatofuz
parents:
diff changeset
129 if (CanRepresent(FnTy.getTypePtr()))
anatofuz
parents:
diff changeset
130 DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
anatofuz
parents:
diff changeset
131 return true;
anatofuz
parents:
diff changeset
132 }
anatofuz
parents:
diff changeset
133 };
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 public:
anatofuz
parents:
diff changeset
136 PCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName,
anatofuz
parents:
diff changeset
137 const std::string &OutputFileName,
anatofuz
parents:
diff changeset
138 std::unique_ptr<raw_pwrite_stream> OS,
anatofuz
parents:
diff changeset
139 std::shared_ptr<PCHBuffer> Buffer)
anatofuz
parents:
diff changeset
140 : Diags(CI.getDiagnostics()), MainFileName(MainFileName),
anatofuz
parents:
diff changeset
141 OutputFileName(OutputFileName), Ctx(nullptr),
anatofuz
parents:
diff changeset
142 MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
anatofuz
parents:
diff changeset
143 HeaderSearchOpts(CI.getHeaderSearchOpts()),
anatofuz
parents:
diff changeset
144 PreprocessorOpts(CI.getPreprocessorOpts()),
anatofuz
parents:
diff changeset
145 TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
anatofuz
parents:
diff changeset
146 OS(std::move(OS)), Buffer(std::move(Buffer)) {
anatofuz
parents:
diff changeset
147 // The debug info output isn't affected by CodeModel and
anatofuz
parents:
diff changeset
148 // ThreadModel, but the backend expects them to be nonempty.
anatofuz
parents:
diff changeset
149 CodeGenOpts.CodeModel = "default";
anatofuz
parents:
diff changeset
150 CodeGenOpts.ThreadModel = "single";
anatofuz
parents:
diff changeset
151 CodeGenOpts.DebugTypeExtRefs = true;
anatofuz
parents:
diff changeset
152 // When building a module MainFileName is the name of the modulemap file.
anatofuz
parents:
diff changeset
153 CodeGenOpts.MainFileName =
anatofuz
parents:
diff changeset
154 LangOpts.CurrentModule.empty() ? MainFileName : LangOpts.CurrentModule;
anatofuz
parents:
diff changeset
155 CodeGenOpts.setDebugInfo(codegenoptions::FullDebugInfo);
anatofuz
parents:
diff changeset
156 CodeGenOpts.setDebuggerTuning(CI.getCodeGenOpts().getDebuggerTuning());
anatofuz
parents:
diff changeset
157 CodeGenOpts.DebugPrefixMap =
anatofuz
parents:
diff changeset
158 CI.getInvocation().getCodeGenOpts().DebugPrefixMap;
anatofuz
parents:
diff changeset
159 }
anatofuz
parents:
diff changeset
160
anatofuz
parents:
diff changeset
161 ~PCHContainerGenerator() override = default;
anatofuz
parents:
diff changeset
162
anatofuz
parents:
diff changeset
163 void Initialize(ASTContext &Context) override {
anatofuz
parents:
diff changeset
164 assert(!Ctx && "initialized multiple times");
anatofuz
parents:
diff changeset
165
anatofuz
parents:
diff changeset
166 Ctx = &Context;
anatofuz
parents:
diff changeset
167 VMContext.reset(new llvm::LLVMContext());
anatofuz
parents:
diff changeset
168 M.reset(new llvm::Module(MainFileName, *VMContext));
anatofuz
parents:
diff changeset
169 M->setDataLayout(Ctx->getTargetInfo().getDataLayout());
anatofuz
parents:
diff changeset
170 Builder.reset(new CodeGen::CodeGenModule(
anatofuz
parents:
diff changeset
171 *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
anatofuz
parents:
diff changeset
172
anatofuz
parents:
diff changeset
173 // Prepare CGDebugInfo to emit debug info for a clang module.
anatofuz
parents:
diff changeset
174 auto *DI = Builder->getModuleDebugInfo();
anatofuz
parents:
diff changeset
175 StringRef ModuleName = llvm::sys::path::filename(MainFileName);
anatofuz
parents:
diff changeset
176 DI->setPCHDescriptor({ModuleName, "", OutputFileName,
anatofuz
parents:
diff changeset
177 ASTFileSignature{{{~0U, ~0U, ~0U, ~0U, ~1U}}}});
anatofuz
parents:
diff changeset
178 DI->setModuleMap(MMap);
anatofuz
parents:
diff changeset
179 }
anatofuz
parents:
diff changeset
180
anatofuz
parents:
diff changeset
181 bool HandleTopLevelDecl(DeclGroupRef D) override {
anatofuz
parents:
diff changeset
182 if (Diags.hasErrorOccurred())
anatofuz
parents:
diff changeset
183 return true;
anatofuz
parents:
diff changeset
184
anatofuz
parents:
diff changeset
185 // Collect debug info for all decls in this group.
anatofuz
parents:
diff changeset
186 for (auto *I : D)
anatofuz
parents:
diff changeset
187 if (!I->isFromASTFile()) {
anatofuz
parents:
diff changeset
188 DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
anatofuz
parents:
diff changeset
189 DTV.TraverseDecl(I);
anatofuz
parents:
diff changeset
190 }
anatofuz
parents:
diff changeset
191 return true;
anatofuz
parents:
diff changeset
192 }
anatofuz
parents:
diff changeset
193
anatofuz
parents:
diff changeset
194 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
anatofuz
parents:
diff changeset
195 HandleTopLevelDecl(D);
anatofuz
parents:
diff changeset
196 }
anatofuz
parents:
diff changeset
197
anatofuz
parents:
diff changeset
198 void HandleTagDeclDefinition(TagDecl *D) override {
anatofuz
parents:
diff changeset
199 if (Diags.hasErrorOccurred())
anatofuz
parents:
diff changeset
200 return;
anatofuz
parents:
diff changeset
201
anatofuz
parents:
diff changeset
202 if (D->isFromASTFile())
anatofuz
parents:
diff changeset
203 return;
anatofuz
parents:
diff changeset
204
anatofuz
parents:
diff changeset
205 // Anonymous tag decls are deferred until we are building their declcontext.
anatofuz
parents:
diff changeset
206 if (D->getName().empty())
anatofuz
parents:
diff changeset
207 return;
anatofuz
parents:
diff changeset
208
anatofuz
parents:
diff changeset
209 // Defer tag decls until their declcontext is complete.
anatofuz
parents:
diff changeset
210 auto *DeclCtx = D->getDeclContext();
anatofuz
parents:
diff changeset
211 while (DeclCtx) {
anatofuz
parents:
diff changeset
212 if (auto *D = dyn_cast<TagDecl>(DeclCtx))
anatofuz
parents:
diff changeset
213 if (!D->isCompleteDefinition())
anatofuz
parents:
diff changeset
214 return;
anatofuz
parents:
diff changeset
215 DeclCtx = DeclCtx->getParent();
anatofuz
parents:
diff changeset
216 }
anatofuz
parents:
diff changeset
217
anatofuz
parents:
diff changeset
218 DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
anatofuz
parents:
diff changeset
219 DTV.TraverseDecl(D);
anatofuz
parents:
diff changeset
220 Builder->UpdateCompletedType(D);
anatofuz
parents:
diff changeset
221 }
anatofuz
parents:
diff changeset
222
anatofuz
parents:
diff changeset
223 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
anatofuz
parents:
diff changeset
224 if (Diags.hasErrorOccurred())
anatofuz
parents:
diff changeset
225 return;
anatofuz
parents:
diff changeset
226
anatofuz
parents:
diff changeset
227 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
anatofuz
parents:
diff changeset
228 Builder->getModuleDebugInfo()->completeRequiredType(RD);
anatofuz
parents:
diff changeset
229 }
anatofuz
parents:
diff changeset
230
anatofuz
parents:
diff changeset
231 void HandleImplicitImportDecl(ImportDecl *D) override {
anatofuz
parents:
diff changeset
232 if (!D->getImportedOwningModule())
anatofuz
parents:
diff changeset
233 Builder->getModuleDebugInfo()->EmitImportDecl(*D);
anatofuz
parents:
diff changeset
234 }
anatofuz
parents:
diff changeset
235
anatofuz
parents:
diff changeset
236 /// Emit a container holding the serialized AST.
anatofuz
parents:
diff changeset
237 void HandleTranslationUnit(ASTContext &Ctx) override {
anatofuz
parents:
diff changeset
238 assert(M && VMContext && Builder);
anatofuz
parents:
diff changeset
239 // Delete these on function exit.
anatofuz
parents:
diff changeset
240 std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext);
anatofuz
parents:
diff changeset
241 std::unique_ptr<llvm::Module> M = std::move(this->M);
anatofuz
parents:
diff changeset
242 std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder);
anatofuz
parents:
diff changeset
243
anatofuz
parents:
diff changeset
244 if (Diags.hasErrorOccurred())
anatofuz
parents:
diff changeset
245 return;
anatofuz
parents:
diff changeset
246
anatofuz
parents:
diff changeset
247 M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple());
anatofuz
parents:
diff changeset
248 M->setDataLayout(Ctx.getTargetInfo().getDataLayout());
anatofuz
parents:
diff changeset
249
anatofuz
parents:
diff changeset
250 // PCH files don't have a signature field in the control block,
anatofuz
parents:
diff changeset
251 // but LLVM detects DWO CUs by looking for a non-zero DWO id.
anatofuz
parents:
diff changeset
252 // We use the lower 64 bits for debug info.
anatofuz
parents:
diff changeset
253 uint64_t Signature =
anatofuz
parents:
diff changeset
254 Buffer->Signature
anatofuz
parents:
diff changeset
255 ? (uint64_t)Buffer->Signature[1] << 32 | Buffer->Signature[0]
anatofuz
parents:
diff changeset
256 : ~1ULL;
anatofuz
parents:
diff changeset
257 Builder->getModuleDebugInfo()->setDwoId(Signature);
anatofuz
parents:
diff changeset
258
anatofuz
parents:
diff changeset
259 // Finalize the Builder.
anatofuz
parents:
diff changeset
260 if (Builder)
anatofuz
parents:
diff changeset
261 Builder->Release();
anatofuz
parents:
diff changeset
262
anatofuz
parents:
diff changeset
263 // Ensure the target exists.
anatofuz
parents:
diff changeset
264 std::string Error;
anatofuz
parents:
diff changeset
265 auto Triple = Ctx.getTargetInfo().getTriple();
anatofuz
parents:
diff changeset
266 if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
anatofuz
parents:
diff changeset
267 llvm::report_fatal_error(Error);
anatofuz
parents:
diff changeset
268
anatofuz
parents:
diff changeset
269 // Emit the serialized Clang AST into its own section.
anatofuz
parents:
diff changeset
270 assert(Buffer->IsComplete && "serialization did not complete");
anatofuz
parents:
diff changeset
271 auto &SerializedAST = Buffer->Data;
anatofuz
parents:
diff changeset
272 auto Size = SerializedAST.size();
anatofuz
parents:
diff changeset
273 auto Int8Ty = llvm::Type::getInt8Ty(*VMContext);
anatofuz
parents:
diff changeset
274 auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
anatofuz
parents:
diff changeset
275 auto *Data = llvm::ConstantDataArray::getString(
anatofuz
parents:
diff changeset
276 *VMContext, StringRef(SerializedAST.data(), Size),
anatofuz
parents:
diff changeset
277 /*AddNull=*/false);
anatofuz
parents:
diff changeset
278 auto *ASTSym = new llvm::GlobalVariable(
anatofuz
parents:
diff changeset
279 *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data,
anatofuz
parents:
diff changeset
280 "__clang_ast");
anatofuz
parents:
diff changeset
281 // The on-disk hashtable needs to be aligned.
anatofuz
parents:
diff changeset
282 ASTSym->setAlignment(llvm::Align(8));
anatofuz
parents:
diff changeset
283
anatofuz
parents:
diff changeset
284 // Mach-O also needs a segment name.
anatofuz
parents:
diff changeset
285 if (Triple.isOSBinFormatMachO())
anatofuz
parents:
diff changeset
286 ASTSym->setSection("__CLANG,__clangast");
anatofuz
parents:
diff changeset
287 // COFF has an eight character length limit.
anatofuz
parents:
diff changeset
288 else if (Triple.isOSBinFormatCOFF())
anatofuz
parents:
diff changeset
289 ASTSym->setSection("clangast");
anatofuz
parents:
diff changeset
290 else
anatofuz
parents:
diff changeset
291 ASTSym->setSection("__clangast");
anatofuz
parents:
diff changeset
292
anatofuz
parents:
diff changeset
293 LLVM_DEBUG({
anatofuz
parents:
diff changeset
294 // Print the IR for the PCH container to the debug output.
anatofuz
parents:
diff changeset
295 llvm::SmallString<0> Buffer;
anatofuz
parents:
diff changeset
296 clang::EmitBackendOutput(
anatofuz
parents:
diff changeset
297 Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, LangOpts,
anatofuz
parents:
diff changeset
298 Ctx.getTargetInfo().getDataLayout(), M.get(),
anatofuz
parents:
diff changeset
299 BackendAction::Backend_EmitLL,
anatofuz
parents:
diff changeset
300 std::make_unique<llvm::raw_svector_ostream>(Buffer));
anatofuz
parents:
diff changeset
301 llvm::dbgs() << Buffer;
anatofuz
parents:
diff changeset
302 });
anatofuz
parents:
diff changeset
303
anatofuz
parents:
diff changeset
304 // Use the LLVM backend to emit the pch container.
anatofuz
parents:
diff changeset
305 clang::EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts,
anatofuz
parents:
diff changeset
306 LangOpts, Ctx.getTargetInfo().getDataLayout(),
anatofuz
parents:
diff changeset
307 M.get(), BackendAction::Backend_EmitObj,
anatofuz
parents:
diff changeset
308 std::move(OS));
anatofuz
parents:
diff changeset
309
anatofuz
parents:
diff changeset
310 // Free the memory for the temporary buffer.
anatofuz
parents:
diff changeset
311 llvm::SmallVector<char, 0> Empty;
anatofuz
parents:
diff changeset
312 SerializedAST = std::move(Empty);
anatofuz
parents:
diff changeset
313 }
anatofuz
parents:
diff changeset
314 };
anatofuz
parents:
diff changeset
315
anatofuz
parents:
diff changeset
316 } // anonymous namespace
anatofuz
parents:
diff changeset
317
anatofuz
parents:
diff changeset
318 std::unique_ptr<ASTConsumer>
anatofuz
parents:
diff changeset
319 ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
anatofuz
parents:
diff changeset
320 CompilerInstance &CI, const std::string &MainFileName,
anatofuz
parents:
diff changeset
321 const std::string &OutputFileName,
anatofuz
parents:
diff changeset
322 std::unique_ptr<llvm::raw_pwrite_stream> OS,
anatofuz
parents:
diff changeset
323 std::shared_ptr<PCHBuffer> Buffer) const {
anatofuz
parents:
diff changeset
324 return std::make_unique<PCHContainerGenerator>(
anatofuz
parents:
diff changeset
325 CI, MainFileName, OutputFileName, std::move(OS), Buffer);
anatofuz
parents:
diff changeset
326 }
anatofuz
parents:
diff changeset
327
anatofuz
parents:
diff changeset
328 StringRef
anatofuz
parents:
diff changeset
329 ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
anatofuz
parents:
diff changeset
330 StringRef PCH;
anatofuz
parents:
diff changeset
331 auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer);
anatofuz
parents:
diff changeset
332 if (OFOrErr) {
anatofuz
parents:
diff changeset
333 auto &OF = OFOrErr.get();
anatofuz
parents:
diff changeset
334 bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
anatofuz
parents:
diff changeset
335 // Find the clang AST section in the container.
anatofuz
parents:
diff changeset
336 for (auto &Section : OF->sections()) {
anatofuz
parents:
diff changeset
337 StringRef Name;
anatofuz
parents:
diff changeset
338 if (Expected<StringRef> NameOrErr = Section.getName())
anatofuz
parents:
diff changeset
339 Name = *NameOrErr;
anatofuz
parents:
diff changeset
340 else
anatofuz
parents:
diff changeset
341 consumeError(NameOrErr.takeError());
anatofuz
parents:
diff changeset
342
anatofuz
parents:
diff changeset
343 if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) {
anatofuz
parents:
diff changeset
344 if (Expected<StringRef> E = Section.getContents())
anatofuz
parents:
diff changeset
345 return *E;
anatofuz
parents:
diff changeset
346 else {
anatofuz
parents:
diff changeset
347 handleAllErrors(E.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
anatofuz
parents:
diff changeset
348 EIB.log(llvm::errs());
anatofuz
parents:
diff changeset
349 });
anatofuz
parents:
diff changeset
350 return "";
anatofuz
parents:
diff changeset
351 }
anatofuz
parents:
diff changeset
352 }
anatofuz
parents:
diff changeset
353 }
anatofuz
parents:
diff changeset
354 }
anatofuz
parents:
diff changeset
355 handleAllErrors(OFOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
anatofuz
parents:
diff changeset
356 if (EIB.convertToErrorCode() ==
anatofuz
parents:
diff changeset
357 llvm::object::object_error::invalid_file_type)
anatofuz
parents:
diff changeset
358 // As a fallback, treat the buffer as a raw AST.
anatofuz
parents:
diff changeset
359 PCH = Buffer.getBuffer();
anatofuz
parents:
diff changeset
360 else
anatofuz
parents:
diff changeset
361 EIB.log(llvm::errs());
anatofuz
parents:
diff changeset
362 });
anatofuz
parents:
diff changeset
363 return PCH;
anatofuz
parents:
diff changeset
364 }