1 //===--- ObjectFilePCHContainerOperations.cpp -----------------------------===//
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
7 //===----------------------------------------------------------------------===//
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/Object/COFF.h"
31 #include "llvm/Object/ObjectFile.h"
32 #include "llvm/Support/Path.h"
33 #include "llvm/Support/TargetRegistry.h"
37 using namespace clang;
39 #define DEBUG_TYPE "pchcontainer"
42 class PCHContainerGenerator : public ASTConsumer {
43 DiagnosticsEngine &Diags;
44 const std::string MainFileName;
45 const std::string OutputFileName;
48 const HeaderSearchOptions &HeaderSearchOpts;
49 const PreprocessorOptions &PreprocessorOpts;
50 CodeGenOptions CodeGenOpts;
51 const TargetOptions TargetOpts;
52 const 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;
59 /// Visit every type and emit debug info for it.
60 struct DebugTypeVisitor : public RecursiveASTVisitor<DebugTypeVisitor> {
61 clang::CodeGen::CGDebugInfo &DI;
63 DebugTypeVisitor(clang::CodeGen::CGDebugInfo &DI, ASTContext &Ctx)
66 /// Determine whether this type can be represented in DWARF.
67 static bool CanRepresent(const Type *Ty) {
68 return !Ty->isDependentType() && !Ty->isUndeducedType();
71 bool VisitImportDecl(ImportDecl *D) {
72 if (!D->getImportedOwningModule())
73 DI.EmitImportDecl(*D);
77 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 if (auto *TD = dyn_cast<TagDecl>(D))
82 if (!TD->isCompleteDefinition())
85 QualType QualTy = Ctx.getTypeDeclType(D);
86 if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
87 DI.getOrCreateStandaloneType(QualTy, D->getLocation());
91 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
92 QualType QualTy(D->getTypeForDecl(), 0);
93 if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
94 DI.getOrCreateStandaloneType(QualTy, D->getLocation());
98 bool VisitFunctionDecl(FunctionDecl *D) {
99 if (isa<CXXMethodDecl>(D))
100 // This is not yet supported. Constructing the `this' argument
101 // mandates a CodeGenFunction.
104 SmallVector<QualType, 16> ArgTypes;
105 for (auto i : D->parameters())
106 ArgTypes.push_back(i->getType());
107 QualType RetTy = D->getReturnType();
108 QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
109 FunctionProtoType::ExtProtoInfo());
110 if (CanRepresent(FnTy.getTypePtr()))
111 DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
115 bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
116 if (!D->getClassInterface())
119 bool selfIsPseudoStrong, selfIsConsumed;
120 SmallVector<QualType, 16> ArgTypes;
121 ArgTypes.push_back(D->getSelfType(Ctx, D->getClassInterface(),
122 selfIsPseudoStrong, selfIsConsumed));
123 ArgTypes.push_back(Ctx.getObjCSelType());
124 for (auto i : D->parameters())
125 ArgTypes.push_back(i->getType());
126 QualType RetTy = D->getReturnType();
127 QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
128 FunctionProtoType::ExtProtoInfo());
129 if (CanRepresent(FnTy.getTypePtr()))
130 DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
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 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 CodeGenOpts.CodeModel = "default";
150 CodeGenOpts.ThreadModel = "single";
151 CodeGenOpts.DebugTypeExtRefs = true;
152 // When building a module MainFileName is the name of the modulemap file.
153 CodeGenOpts.MainFileName =
154 LangOpts.CurrentModule.empty() ? MainFileName : LangOpts.CurrentModule;
155 CodeGenOpts.setDebugInfo(codegenoptions::FullDebugInfo);
156 CodeGenOpts.setDebuggerTuning(CI.getCodeGenOpts().getDebuggerTuning());
157 CodeGenOpts.DebugPrefixMap =
158 CI.getInvocation().getCodeGenOpts().DebugPrefixMap;
161 ~PCHContainerGenerator() override = default;
163 void Initialize(ASTContext &Context) override {
164 assert(!Ctx && "initialized multiple times");
167 VMContext.reset(new llvm::LLVMContext());
168 M.reset(new llvm::Module(MainFileName, *VMContext));
169 M->setDataLayout(Ctx->getTargetInfo().getDataLayout());
170 Builder.reset(new CodeGen::CodeGenModule(
171 *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
173 // Prepare CGDebugInfo to emit debug info for a clang module.
174 auto *DI = Builder->getModuleDebugInfo();
175 StringRef ModuleName = llvm::sys::path::filename(MainFileName);
176 DI->setPCHDescriptor({ModuleName, "", OutputFileName,
177 ASTFileSignature{{{~0U, ~0U, ~0U, ~0U, ~1U}}}});
178 DI->setModuleMap(MMap);
181 bool HandleTopLevelDecl(DeclGroupRef D) override {
182 if (Diags.hasErrorOccurred())
185 // Collect debug info for all decls in this group.
187 if (!I->isFromASTFile()) {
188 DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
194 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
195 HandleTopLevelDecl(D);
198 void HandleTagDeclDefinition(TagDecl *D) override {
199 if (Diags.hasErrorOccurred())
202 if (D->isFromASTFile())
205 // Anonymous tag decls are deferred until we are building their declcontext.
206 if (D->getName().empty())
209 // Defer tag decls until their declcontext is complete.
210 auto *DeclCtx = D->getDeclContext();
212 if (auto *D = dyn_cast<TagDecl>(DeclCtx))
213 if (!D->isCompleteDefinition())
215 DeclCtx = DeclCtx->getParent();
218 DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
220 Builder->UpdateCompletedType(D);
223 void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
224 if (Diags.hasErrorOccurred())
227 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
228 Builder->getModuleDebugInfo()->completeRequiredType(RD);
231 void HandleImplicitImportDecl(ImportDecl *D) override {
232 if (!D->getImportedOwningModule())
233 Builder->getModuleDebugInfo()->EmitImportDecl(*D);
236 /// Emit a container holding the serialized AST.
237 void HandleTranslationUnit(ASTContext &Ctx) override {
238 assert(M && VMContext && Builder);
239 // Delete these on function exit.
240 std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext);
241 std::unique_ptr<llvm::Module> M = std::move(this->M);
242 std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder);
244 if (Diags.hasErrorOccurred())
247 M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple());
248 M->setDataLayout(Ctx.getTargetInfo().getDataLayout());
250 // PCH files don't have a signature field in the control block,
251 // but LLVM detects DWO CUs by looking for a non-zero DWO id.
252 // We use the lower 64 bits for debug info.
255 ? (uint64_t)Buffer->Signature[1] << 32 | Buffer->Signature[0]
257 Builder->getModuleDebugInfo()->setDwoId(Signature);
259 // Finalize the Builder.
263 // Ensure the target exists.
265 auto Triple = Ctx.getTargetInfo().getTriple();
266 if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
267 llvm::report_fatal_error(Error);
269 // Emit the serialized Clang AST into its own section.
270 assert(Buffer->IsComplete && "serialization did not complete");
271 auto &SerializedAST = Buffer->Data;
272 auto Size = SerializedAST.size();
273 auto Int8Ty = llvm::Type::getInt8Ty(*VMContext);
274 auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
275 auto *Data = llvm::ConstantDataArray::getString(
276 *VMContext, StringRef(SerializedAST.data(), Size),
278 auto *ASTSym = new llvm::GlobalVariable(
279 *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data,
281 // The on-disk hashtable needs to be aligned.
282 ASTSym->setAlignment(llvm::Align(8));
284 // Mach-O also needs a segment name.
285 if (Triple.isOSBinFormatMachO())
286 ASTSym->setSection("__CLANG,__clangast");
287 // COFF has an eight character length limit.
288 else if (Triple.isOSBinFormatCOFF())
289 ASTSym->setSection("clangast");
291 ASTSym->setSection("__clangast");
294 // Print the IR for the PCH container to the debug output.
295 llvm::SmallString<0> Buffer;
296 clang::EmitBackendOutput(
297 Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, LangOpts,
298 Ctx.getTargetInfo().getDataLayout(), M.get(),
299 BackendAction::Backend_EmitLL,
300 std::make_unique<llvm::raw_svector_ostream>(Buffer));
301 llvm::dbgs() << Buffer;
304 // Use the LLVM backend to emit the pch container.
305 clang::EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts,
306 LangOpts, Ctx.getTargetInfo().getDataLayout(),
307 M.get(), BackendAction::Backend_EmitObj,
310 // Free the memory for the temporary buffer.
311 llvm::SmallVector<char, 0> Empty;
312 SerializedAST = std::move(Empty);
316 } // anonymous namespace
318 std::unique_ptr<ASTConsumer>
319 ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
320 CompilerInstance &CI, const std::string &MainFileName,
321 const std::string &OutputFileName,
322 std::unique_ptr<llvm::raw_pwrite_stream> OS,
323 std::shared_ptr<PCHBuffer> Buffer) const {
324 return std::make_unique<PCHContainerGenerator>(
325 CI, MainFileName, OutputFileName, std::move(OS), Buffer);
329 ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
331 auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer);
333 auto &OF = OFOrErr.get();
334 bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
335 // Find the clang AST section in the container.
336 for (auto &Section : OF->sections()) {
338 if (Expected<StringRef> NameOrErr = Section.getName())
341 consumeError(NameOrErr.takeError());
343 if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) {
344 if (Expected<StringRef> E = Section.getContents())
347 handleAllErrors(E.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
348 EIB.log(llvm::errs());
355 handleAllErrors(OFOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
356 if (EIB.convertToErrorCode() ==
357 llvm::object::object_error::invalid_file_type)
358 // As a fallback, treat the buffer as a raw AST.
359 PCH = Buffer.getBuffer();
361 EIB.log(llvm::errs());