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