clang 20.0.0git
ObjectFilePCHContainerWriter.cpp
Go to the documentation of this file.
1//===--- ObjectFilePCHContainerWriter.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
10#include "CGDebugInfo.h"
11#include "CodeGenModule.h"
13#include "clang/AST/DeclObjC.h"
14#include "clang/AST/Expr.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/LLVMContext.h"
27#include "llvm/IR/Module.h"
28#include "llvm/MC/TargetRegistry.h"
29#include "llvm/Object/COFF.h"
30#include "llvm/Support/Path.h"
31#include <memory>
32#include <utility>
33
34using namespace clang;
35
36#define DEBUG_TYPE "pchcontainer"
37
38namespace {
39class PCHContainerGenerator : public ASTConsumer {
41 DiagnosticsEngine &Diags;
42 const std::string MainFileName;
43 const std::string OutputFileName;
44 ASTContext *Ctx;
45 ModuleMap &MMap;
47 const HeaderSearchOptions &HeaderSearchOpts;
48 const PreprocessorOptions &PreprocessorOpts;
49 CodeGenOptions CodeGenOpts;
50 const TargetOptions TargetOpts;
51 LangOptions LangOpts;
52 std::unique_ptr<llvm::LLVMContext> VMContext;
53 std::unique_ptr<llvm::Module> M;
54 std::unique_ptr<CodeGen::CodeGenModule> Builder;
55 std::unique_ptr<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> {
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) {
72 DI.EmitImportDecl(*D);
73 return true;
74 }
75
76 bool VisitTypeDecl(TypeDecl *D) {
77 // TagDecls may be deferred until after all decls have been merged and we
78 // know the complete type. Pure forward declarations will be skipped, but
79 // they don't need to be emitted into the module anyway.
80 if (auto *TD = dyn_cast<TagDecl>(D))
81 if (!TD->isCompleteDefinition())
82 return true;
83
84 if (D->hasAttr<NoDebugAttr>())
85 return true;
86
87 QualType QualTy = Ctx.getTypeDeclType(D);
88 if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
90 return true;
91 }
92
93 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
94 QualType QualTy(D->getTypeForDecl(), 0);
95 if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
97 return true;
98 }
99
100 bool VisitFunctionDecl(FunctionDecl *D) {
101 // Skip deduction guides.
102 if (isa<CXXDeductionGuideDecl>(D))
103 return true;
104
105 if (isa<CXXMethodDecl>(D))
106 // This is not yet supported. Constructing the `this' argument
107 // mandates a CodeGenFunction.
108 return true;
109
111 for (auto *i : D->parameters())
112 ArgTypes.push_back(i->getType());
113 QualType RetTy = D->getReturnType();
114 QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
116 if (CanRepresent(FnTy.getTypePtr()))
117 DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
118 return true;
119 }
120
121 bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
122 if (!D->getClassInterface())
123 return true;
124
125 bool selfIsPseudoStrong, selfIsConsumed;
127 ArgTypes.push_back(D->getSelfType(Ctx, D->getClassInterface(),
128 selfIsPseudoStrong, selfIsConsumed));
129 ArgTypes.push_back(Ctx.getObjCSelType());
130 for (auto *i : D->parameters())
131 ArgTypes.push_back(i->getType());
132 QualType RetTy = D->getReturnType();
133 QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
135 if (CanRepresent(FnTy.getTypePtr()))
136 DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
137 return true;
138 }
139 };
140
141public:
142 PCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName,
143 const std::string &OutputFileName,
144 std::unique_ptr<raw_pwrite_stream> OS,
145 std::shared_ptr<PCHBuffer> Buffer)
146 : CI(CI), Diags(CI.getDiagnostics()), MainFileName(MainFileName),
147 OutputFileName(OutputFileName), Ctx(nullptr),
149 FS(&CI.getVirtualFileSystem()),
150 HeaderSearchOpts(CI.getHeaderSearchOpts()),
151 PreprocessorOpts(CI.getPreprocessorOpts()),
152 TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
153 OS(std::move(OS)), Buffer(std::move(Buffer)) {
154 // The debug info output isn't affected by CodeModel and
155 // ThreadModel, but the backend expects them to be nonempty.
156 CodeGenOpts.CodeModel = "default";
157 LangOpts.setThreadModel(LangOptions::ThreadModelKind::Single);
158 CodeGenOpts.DebugTypeExtRefs = true;
159 // When building a module MainFileName is the name of the modulemap file.
160 CodeGenOpts.MainFileName =
161 LangOpts.CurrentModule.empty() ? MainFileName : LangOpts.CurrentModule;
162 CodeGenOpts.setDebugInfo(llvm::codegenoptions::FullDebugInfo);
163 CodeGenOpts.setDebuggerTuning(CI.getCodeGenOpts().getDebuggerTuning());
164 CodeGenOpts.DwarfVersion = CI.getCodeGenOpts().DwarfVersion;
165 CodeGenOpts.DebugCompilationDir =
167 CodeGenOpts.DebugPrefixMap =
169 CodeGenOpts.DebugStrictDwarf = CI.getCodeGenOpts().DebugStrictDwarf;
170 }
171
172 ~PCHContainerGenerator() override = default;
173
174 void Initialize(ASTContext &Context) override {
175 assert(!Ctx && "initialized multiple times");
176
177 Ctx = &Context;
178 VMContext.reset(new llvm::LLVMContext());
179 M.reset(new llvm::Module(MainFileName, *VMContext));
180 M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
181 Builder.reset(new CodeGen::CodeGenModule(
182 *Ctx, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
183
184 // Prepare CGDebugInfo to emit debug info for a clang module.
185 auto *DI = Builder->getModuleDebugInfo();
186 StringRef ModuleName = llvm::sys::path::filename(MainFileName);
187 DI->setPCHDescriptor(
188 {ModuleName, "", OutputFileName, ASTFileSignature::createDISentinel()});
189 DI->setModuleMap(MMap);
190 }
191
192 bool HandleTopLevelDecl(DeclGroupRef D) override {
193 if (Diags.hasErrorOccurred())
194 return true;
195
196 // Collect debug info for all decls in this group.
197 for (auto *I : D)
198 if (!I->isFromASTFile()) {
199 DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
200 DTV.TraverseDecl(I);
201 }
202 return true;
203 }
204
207 }
208
209 void HandleTagDeclDefinition(TagDecl *D) override {
210 if (Diags.hasErrorOccurred())
211 return;
212
213 if (D->isFromASTFile())
214 return;
215
216 // Anonymous tag decls are deferred until we are building their declcontext.
217 if (D->getName().empty())
218 return;
219
220 // Defer tag decls until their declcontext is complete.
221 auto *DeclCtx = D->getDeclContext();
222 while (DeclCtx) {
223 if (auto *D = dyn_cast<TagDecl>(DeclCtx))
224 if (!D->isCompleteDefinition())
225 return;
226 DeclCtx = DeclCtx->getParent();
227 }
228
229 DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
230 DTV.TraverseDecl(D);
231 Builder->UpdateCompletedType(D);
232 }
233
234 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
235 if (Diags.hasErrorOccurred())
236 return;
237
238 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
239 Builder->getModuleDebugInfo()->completeRequiredType(RD);
240 }
241
242 void HandleImplicitImportDecl(ImportDecl *D) override {
244 Builder->getModuleDebugInfo()->EmitImportDecl(*D);
245 }
246
247 /// Emit a container holding the serialized AST.
248 void HandleTranslationUnit(ASTContext &Ctx) override {
249 assert(M && VMContext && Builder);
250 // Delete these on function exit.
251 std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext);
252 std::unique_ptr<llvm::Module> M = std::move(this->M);
253 std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder);
254
255 if (Diags.hasErrorOccurred())
256 return;
257
258 M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple());
259 M->setDataLayout(Ctx.getTargetInfo().getDataLayoutString());
260
261 // PCH files don't have a signature field in the control block,
262 // but LLVM detects DWO CUs by looking for a non-zero DWO id.
263 // We use the lower 64 bits for debug info.
264
265 uint64_t Signature =
266 Buffer->Signature ? Buffer->Signature.truncatedValue() : ~1ULL;
267
268 Builder->getModuleDebugInfo()->setDwoId(Signature);
269
270 // Finalize the Builder.
271 if (Builder)
272 Builder->Release();
273
274 // Ensure the target exists.
275 std::string Error;
276 auto Triple = Ctx.getTargetInfo().getTriple();
277 if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
278 llvm::report_fatal_error(llvm::Twine(Error));
279
280 // Emit the serialized Clang AST into its own section.
281 assert(Buffer->IsComplete && "serialization did not complete");
282 auto &SerializedAST = Buffer->Data;
283 auto Size = SerializedAST.size();
284
285 if (Triple.isOSBinFormatWasm()) {
286 // Emit __clangast in custom section instead of named data segment
287 // to find it while iterating sections.
288 // This could be avoided if all data segements (the wasm sense) were
289 // represented as their own sections (in the llvm sense).
290 // TODO: https://github.com/WebAssembly/tool-conventions/issues/138
291 llvm::NamedMDNode *MD =
292 M->getOrInsertNamedMetadata("wasm.custom_sections");
293 llvm::Metadata *Ops[2] = {
294 llvm::MDString::get(*VMContext, "__clangast"),
295 llvm::MDString::get(*VMContext,
296 StringRef(SerializedAST.data(), Size))};
297 auto *NameAndContent = llvm::MDTuple::get(*VMContext, Ops);
298 MD->addOperand(NameAndContent);
299 } else {
300 auto Int8Ty = llvm::Type::getInt8Ty(*VMContext);
301 auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
302 auto *Data = llvm::ConstantDataArray::getString(
303 *VMContext, StringRef(SerializedAST.data(), Size),
304 /*AddNull=*/false);
305 auto *ASTSym = new llvm::GlobalVariable(
306 *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage,
307 Data, "__clang_ast");
308 // The on-disk hashtable needs to be aligned.
309 ASTSym->setAlignment(llvm::Align(8));
310
311 // Mach-O also needs a segment name.
312 if (Triple.isOSBinFormatMachO())
313 ASTSym->setSection("__CLANG,__clangast");
314 // COFF has an eight character length limit.
315 else if (Triple.isOSBinFormatCOFF())
316 ASTSym->setSection("clangast");
317 else
318 ASTSym->setSection("__clangast");
319 }
320
321 LLVM_DEBUG({
322 // Print the IR for the PCH container to the debug output.
325 CI, Ctx.getTargetInfo().getDataLayoutString(), M.get(),
326 BackendAction::Backend_EmitLL, FS,
327 std::make_unique<llvm::raw_svector_ostream>(Buffer));
328 llvm::dbgs() << Buffer;
329 });
330
331 // Use the LLVM backend to emit the pch container.
333 M.get(), BackendAction::Backend_EmitObj, FS,
334 std::move(OS));
335
336 // Free the memory for the temporary buffer.
338 SerializedAST = std::move(Empty);
339 }
340};
341
342} // anonymous namespace
343
344std::unique_ptr<ASTConsumer>
345ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
346 CompilerInstance &CI, const std::string &MainFileName,
347 const std::string &OutputFileName,
348 std::unique_ptr<llvm::raw_pwrite_stream> OS,
349 std::shared_ptr<PCHBuffer> Buffer) const {
350 return std::make_unique<PCHContainerGenerator>(
351 CI, MainFileName, OutputFileName, std::move(OS), Buffer);
352}
Defines the clang::ASTContext interface.
Defines the Diagnostic-related interfaces.
const Decl * D
Defines the clang::Preprocessor interface.
const char * Data
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Definition: ASTConsumer.h:34
virtual void HandleTagDeclDefinition(TagDecl *D)
HandleTagDeclDefinition - This callback is invoked each time a TagDecl (e.g.
Definition: ASTConsumer.h:73
virtual void HandleTranslationUnit(ASTContext &Ctx)
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: ASTConsumer.h:67
virtual void HandleTagDeclRequiredDefinition(const TagDecl *D)
This callback is invoked the first time each TagDecl is required to be complete.
Definition: ASTConsumer.h:77
virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D)
Handle the specified top-level declaration that occurred inside and ObjC container.
Definition: ASTConsumer.cpp:26
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Definition: ASTConsumer.cpp:18
virtual void Initialize(ASTContext &Context)
Initialize - This is called to initialize the consumer, providing the ASTContext.
Definition: ASTConsumer.h:48
virtual void HandleImplicitImportDecl(ImportDecl *D)
Handle an ImportDecl that was implicitly created due to an inclusion directive.
Definition: ASTConsumer.cpp:28
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
QualType getTypeDeclType(const TypeDecl *Decl, const TypeDecl *PrevDecl=nullptr) const
Return the unique reference to the type for the specified type declaration.
Definition: ASTContext.h:1703
QualType getObjCSelType() const
Retrieve the type that corresponds to the predefined Objective-C 'SEL' type.
Definition: ASTContext.h:2213
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
Definition: ASTContext.h:1681
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:799
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
llvm::SmallVector< std::pair< std::string, std::string >, 0 > DebugPrefixMap
std::string CodeModel
The code model to use (-mcmodel).
std::string DebugCompilationDir
The string to embed in debug information as the current working directory.
std::string MainFileName
The user provided name for the "main file", if non-empty.
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:58
llvm::DIType * getOrCreateStandaloneType(QualType Ty, SourceLocation Loc)
Emit standalone debug info for a type.
void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, QualType FnType, llvm::Function *Fn=nullptr)
Emit debug info for a function declaration.
void EmitImportDecl(const ImportDecl &ID)
Emit an @import declaration.
This class organizes the cross-function state that is used while generating LLVM code.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
TargetOptions & getTargetOpts()
HeaderSearchOptions & getHeaderSearchOpts()
CompilerInvocation & getInvocation()
PreprocessorOptions & getPreprocessorOpts()
llvm::vfs::FileSystem & getVirtualFileSystem() const
LangOptions & getLangOpts()
CodeGenOptions & getCodeGenOpts()
CodeGenOptions & getCodeGenOpts()
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:2104
Module * getImportedOwningModule() const
Get the imported owning module, if this decl is from an imported (non-local) module.
Definition: DeclBase.h:805
bool isFromASTFile() const
Determine whether this declaration came from an AST file (such as a precompiled header or module) rat...
Definition: DeclBase.h:786
SourceLocation getLocation() const
Definition: DeclBase.h:442
DeclContext * getDeclContext()
Definition: DeclBase.h:451
bool hasAttr() const
Definition: DeclBase.h:580
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
bool hasErrorOccurred() const
Definition: Diagnostic.h:866
Represents a function declaration or definition.
Definition: Decl.h:1935
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:821
Describes a module import declaration, which makes the contents of the named module visible in the cu...
Definition: Decl.h:4808
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:499
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:554
Represents an ObjC class declaration.
Definition: DeclObjC.h:1153
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
HeaderSearch & getHeaderSearchInfo() const
A (possibly-)qualified type.
Definition: Type.h:929
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:996
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:7936
Represents a struct/union/class.
Definition: Decl.h:4162
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3578
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1262
const char * getDataLayoutString() const
Definition: TargetInfo.h:1271
Options for controlling the target.
Definition: TargetOptions.h:26
Represents a declaration of a type.
Definition: Decl.h:3384
The base class of the type hierarchy.
Definition: Type.h:1828
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition: Type.h:2706
bool isUndeducedType() const
Determine whether this type is an undeduced type, meaning that it somehow involves a C++11 'auto' typ...
Definition: Type.h:8649
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
void emitBackendOutput(CompilerInstance &CI, StringRef TDesc, llvm::Module *M, BackendAction Action, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, std::unique_ptr< raw_pwrite_stream > OS, BackendConsumer *BC=nullptr)
static ASTFileSignature createDISentinel()
Definition: Module.h:80
Extra information about a function prototype.
Definition: Type.h:5192