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
9#include "clang/CodeGen/ObjectFilePCHContainerWriter.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/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 {
40 CompilerInstance &CI;
41 DiagnosticsEngine &Diags;
42 const std::string MainFileName;
43 const std::string OutputFileName;
44 ASTContext *Ctx;
45 ModuleMap &MMap;
46 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
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> {
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
70 bool VisitImportDecl(ImportDecl *D) {
71 if (!D->getImportedOwningModule())
72 DI.EmitImportDecl(ID: *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>(Val: D))
81 if (!TD->isCompleteDefinition())
82 return true;
83
84 if (D->hasAttr<NoDebugAttr>())
85 return true;
86
87 QualType QualTy = Ctx.getTypeDeclType(Decl: D);
88 if (!QualTy.isNull() && CanRepresent(Ty: QualTy.getTypePtr()))
89 DI.getOrCreateStandaloneType(Ty: QualTy, Loc: D->getLocation());
90 return true;
91 }
92
93 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
94 QualType QualTy(D->getTypeForDecl(), 0);
95 if (!QualTy.isNull() && CanRepresent(Ty: QualTy.getTypePtr()))
96 DI.getOrCreateStandaloneType(Ty: QualTy, Loc: D->getLocation());
97 return true;
98 }
99
100 bool VisitFunctionDecl(FunctionDecl *D) {
101 // Skip deduction guides.
102 if (isa<CXXDeductionGuideDecl>(Val: D))
103 return true;
104
105 if (isa<CXXMethodDecl>(Val: D))
106 // This is not yet supported. Constructing the `this' argument
107 // mandates a CodeGenFunction.
108 return true;
109
110 SmallVector<QualType, 16> ArgTypes;
111 for (auto *i : D->parameters())
112 ArgTypes.push_back(Elt: i->getType());
113 QualType RetTy = D->getReturnType();
114 QualType FnTy = Ctx.getFunctionType(ResultTy: RetTy, Args: ArgTypes,
115 EPI: FunctionProtoType::ExtProtoInfo());
116 if (CanRepresent(Ty: FnTy.getTypePtr()))
117 DI.EmitFunctionDecl(GD: D, Loc: D->getLocation(), FnType: FnTy);
118 return true;
119 }
120
121 bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
122 if (!D->getClassInterface())
123 return true;
124
125 bool selfIsPseudoStrong, selfIsConsumed;
126 SmallVector<QualType, 16> ArgTypes;
127 ArgTypes.push_back(Elt: D->getSelfType(Context&: Ctx, OID: D->getClassInterface(),
128 selfIsPseudoStrong, selfIsConsumed));
129 ArgTypes.push_back(Elt: Ctx.getObjCSelType());
130 for (auto *i : D->parameters())
131 ArgTypes.push_back(Elt: i->getType());
132 QualType RetTy = D->getReturnType();
133 QualType FnTy = Ctx.getFunctionType(ResultTy: RetTy, Args: ArgTypes,
134 EPI: FunctionProtoType::ExtProtoInfo());
135 if (CanRepresent(Ty: FnTy.getTypePtr()))
136 DI.EmitFunctionDecl(GD: D, Loc: D->getLocation(), FnType: 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),
148 MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
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 =
166 CI.getInvocation().getCodeGenOpts().DebugCompilationDir;
167 CodeGenOpts.DebugPrefixMap =
168 CI.getInvocation().getCodeGenOpts().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(p: new llvm::LLVMContext());
179 M.reset(p: new llvm::Module(MainFileName, *VMContext));
180 M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
181 Builder.reset(p: 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(path: 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(D: I);
201 }
202 return true;
203 }
204
205 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
206 HandleTopLevelDecl(D);
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>(Val: 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(TD: 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>(Val: D))
239 Builder->getModuleDebugInfo()->completeRequiredType(RD);
240 }
241
242 void HandleImplicitImportDecl(ImportDecl *D) override {
243 if (!D->getImportedOwningModule())
244 Builder->getModuleDebugInfo()->EmitImportDecl(ID: *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());
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(TheTriple: Triple, Error))
278 llvm::report_fatal_error(reason: 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(Name: "wasm.custom_sections");
293 llvm::Metadata *Ops[2] = {
294 llvm::MDString::get(Context&: *VMContext, Str: "__clangast"),
295 llvm::MDString::get(Context&: *VMContext,
296 Str: StringRef(SerializedAST.data(), Size))};
297 auto *NameAndContent = llvm::MDTuple::get(Context&: *VMContext, MDs: Ops);
298 MD->addOperand(M: NameAndContent);
299 } else {
300 auto Int8Ty = llvm::Type::getInt8Ty(C&: *VMContext);
301 auto *Ty = llvm::ArrayType::get(ElementType: Int8Ty, NumElements: Size);
302 auto *Data = llvm::ConstantDataArray::getString(
303 Context&: *VMContext, Initializer: 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.
323 llvm::SmallString<0> Buffer;
324 clang::emitBackendOutput(
325 CI, CodeGenOpts, 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.
332 clang::emitBackendOutput(CI, CGOpts&: CodeGenOpts,
333 TDesc: Ctx.getTargetInfo().getDataLayoutString(), M: M.get(),
334 Action: BackendAction::Backend_EmitObj, VFS: FS, OS: std::move(OS));
335
336 // Free the memory for the temporary buffer.
337 llvm::SmallVector<char, 0> Empty;
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 args&: CI, args: MainFileName, args: OutputFileName, args: std::move(OS), args&: Buffer);
352}
353