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