xref: /freebsd-src/contrib/llvm-project/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp (revision fcaf7f8644a9988098ac6be2165bce3ea4786e91)
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         : DI(DI), Ctx(Ctx) {}
65 
66     /// Determine whether this type can be represented in DWARF.
67     static bool CanRepresent(const Type *Ty) {
68       return !Ty->isDependentType() && !Ty->isUndeducedType();
69     }
70 
71     bool VisitImportDecl(ImportDecl *D) {
72       if (!D->getImportedOwningModule())
73         DI.EmitImportDecl(*D);
74       return true;
75     }
76 
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())
83           return true;
84 
85       QualType QualTy = Ctx.getTypeDeclType(D);
86       if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
87         DI.getOrCreateStandaloneType(QualTy, D->getLocation());
88       return true;
89     }
90 
91     bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
92       QualType QualTy(D->getTypeForDecl(), 0);
93       if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
94         DI.getOrCreateStandaloneType(QualTy, D->getLocation());
95       return true;
96     }
97 
98     bool VisitFunctionDecl(FunctionDecl *D) {
99       // Skip deduction guides.
100       if (isa<CXXDeductionGuideDecl>(D))
101         return true;
102 
103       if (isa<CXXMethodDecl>(D))
104         // This is not yet supported. Constructing the `this' argument
105         // mandates a CodeGenFunction.
106         return true;
107 
108       SmallVector<QualType, 16> ArgTypes;
109       for (auto i : D->parameters())
110         ArgTypes.push_back(i->getType());
111       QualType RetTy = D->getReturnType();
112       QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
113                                           FunctionProtoType::ExtProtoInfo());
114       if (CanRepresent(FnTy.getTypePtr()))
115         DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
116       return true;
117     }
118 
119     bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
120       if (!D->getClassInterface())
121         return true;
122 
123       bool selfIsPseudoStrong, selfIsConsumed;
124       SmallVector<QualType, 16> ArgTypes;
125       ArgTypes.push_back(D->getSelfType(Ctx, D->getClassInterface(),
126                                         selfIsPseudoStrong, selfIsConsumed));
127       ArgTypes.push_back(Ctx.getObjCSelType());
128       for (auto i : D->parameters())
129         ArgTypes.push_back(i->getType());
130       QualType RetTy = D->getReturnType();
131       QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
132                                           FunctionProtoType::ExtProtoInfo());
133       if (CanRepresent(FnTy.getTypePtr()))
134         DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
135       return true;
136     }
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         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     CodeGenOpts.CodeModel = "default";
154     LangOpts.setThreadModel(LangOptions::ThreadModelKind::Single);
155     CodeGenOpts.DebugTypeExtRefs = true;
156     // When building a module MainFileName is the name of the modulemap file.
157     CodeGenOpts.MainFileName =
158         LangOpts.CurrentModule.empty() ? MainFileName : LangOpts.CurrentModule;
159     CodeGenOpts.setDebugInfo(codegenoptions::FullDebugInfo);
160     CodeGenOpts.setDebuggerTuning(CI.getCodeGenOpts().getDebuggerTuning());
161     CodeGenOpts.DebugPrefixMap =
162         CI.getInvocation().getCodeGenOpts().DebugPrefixMap;
163     CodeGenOpts.DebugStrictDwarf = CI.getCodeGenOpts().DebugStrictDwarf;
164   }
165 
166   ~PCHContainerGenerator() override = default;
167 
168   void Initialize(ASTContext &Context) override {
169     assert(!Ctx && "initialized multiple times");
170 
171     Ctx = &Context;
172     VMContext.reset(new llvm::LLVMContext());
173     M.reset(new llvm::Module(MainFileName, *VMContext));
174     M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
175     Builder.reset(new CodeGen::CodeGenModule(
176         *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
177 
178     // Prepare CGDebugInfo to emit debug info for a clang module.
179     auto *DI = Builder->getModuleDebugInfo();
180     StringRef ModuleName = llvm::sys::path::filename(MainFileName);
181     DI->setPCHDescriptor(
182         {ModuleName, "", OutputFileName, ASTFileSignature::createDISentinel()});
183     DI->setModuleMap(MMap);
184   }
185 
186   bool HandleTopLevelDecl(DeclGroupRef D) override {
187     if (Diags.hasErrorOccurred())
188       return true;
189 
190     // Collect debug info for all decls in this group.
191     for (auto *I : D)
192       if (!I->isFromASTFile()) {
193         DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
194         DTV.TraverseDecl(I);
195       }
196     return true;
197   }
198 
199   void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
200     HandleTopLevelDecl(D);
201   }
202 
203   void HandleTagDeclDefinition(TagDecl *D) override {
204     if (Diags.hasErrorOccurred())
205       return;
206 
207     if (D->isFromASTFile())
208       return;
209 
210     // Anonymous tag decls are deferred until we are building their declcontext.
211     if (D->getName().empty())
212       return;
213 
214     // Defer tag decls until their declcontext is complete.
215     auto *DeclCtx = D->getDeclContext();
216     while (DeclCtx) {
217       if (auto *D = dyn_cast<TagDecl>(DeclCtx))
218         if (!D->isCompleteDefinition())
219           return;
220       DeclCtx = DeclCtx->getParent();
221     }
222 
223     DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
224     DTV.TraverseDecl(D);
225     Builder->UpdateCompletedType(D);
226   }
227 
228   void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
229     if (Diags.hasErrorOccurred())
230       return;
231 
232     if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
233       Builder->getModuleDebugInfo()->completeRequiredType(RD);
234   }
235 
236   void HandleImplicitImportDecl(ImportDecl *D) override {
237     if (!D->getImportedOwningModule())
238       Builder->getModuleDebugInfo()->EmitImportDecl(*D);
239   }
240 
241   /// Emit a container holding the serialized AST.
242   void HandleTranslationUnit(ASTContext &Ctx) override {
243     assert(M && VMContext && Builder);
244     // Delete these on function exit.
245     std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext);
246     std::unique_ptr<llvm::Module> M = std::move(this->M);
247     std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder);
248 
249     if (Diags.hasErrorOccurred())
250       return;
251 
252     M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple());
253     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     uint64_t Signature =
260         Buffer->Signature ? Buffer->Signature.truncatedValue() : ~1ULL;
261 
262     Builder->getModuleDebugInfo()->setDwoId(Signature);
263 
264     // Finalize the Builder.
265     if (Builder)
266       Builder->Release();
267 
268     // Ensure the target exists.
269     std::string Error;
270     auto Triple = Ctx.getTargetInfo().getTriple();
271     if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
272       llvm::report_fatal_error(llvm::Twine(Error));
273 
274     // Emit the serialized Clang AST into its own section.
275     assert(Buffer->IsComplete && "serialization did not complete");
276     auto &SerializedAST = Buffer->Data;
277     auto Size = SerializedAST.size();
278 
279     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       llvm::NamedMDNode *MD =
286           M->getOrInsertNamedMetadata("wasm.custom_sections");
287       llvm::Metadata *Ops[2] = {
288           llvm::MDString::get(*VMContext, "__clangast"),
289           llvm::MDString::get(*VMContext,
290                               StringRef(SerializedAST.data(), Size))};
291       auto *NameAndContent = llvm::MDTuple::get(*VMContext, Ops);
292       MD->addOperand(NameAndContent);
293     } else {
294       auto Int8Ty = llvm::Type::getInt8Ty(*VMContext);
295       auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
296       auto *Data = llvm::ConstantDataArray::getString(
297           *VMContext, StringRef(SerializedAST.data(), Size),
298           /*AddNull=*/false);
299       auto *ASTSym = new llvm::GlobalVariable(
300           *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage,
301           Data, "__clang_ast");
302       // The on-disk hashtable needs to be aligned.
303       ASTSym->setAlignment(llvm::Align(8));
304 
305       // Mach-O also needs a segment name.
306       if (Triple.isOSBinFormatMachO())
307         ASTSym->setSection("__CLANG,__clangast");
308       // COFF has an eight character length limit.
309       else if (Triple.isOSBinFormatCOFF())
310         ASTSym->setSection("clangast");
311       else
312         ASTSym->setSection("__clangast");
313     }
314 
315     LLVM_DEBUG({
316       // Print the IR for the PCH container to the debug output.
317       llvm::SmallString<0> Buffer;
318       clang::EmitBackendOutput(
319           Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, LangOpts,
320           Ctx.getTargetInfo().getDataLayoutString(), M.get(),
321           BackendAction::Backend_EmitLL,
322           std::make_unique<llvm::raw_svector_ostream>(Buffer));
323       llvm::dbgs() << Buffer;
324     });
325 
326     // Use the LLVM backend to emit the pch container.
327     clang::EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts,
328                              LangOpts,
329                              Ctx.getTargetInfo().getDataLayoutString(), M.get(),
330                              BackendAction::Backend_EmitObj, std::move(OS));
331 
332     // Free the memory for the temporary buffer.
333     llvm::SmallVector<char, 0> Empty;
334     SerializedAST = std::move(Empty);
335   }
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     std::shared_ptr<PCHBuffer> Buffer) const {
346   return std::make_unique<PCHContainerGenerator>(
347       CI, MainFileName, OutputFileName, std::move(OS), Buffer);
348 }
349 
350 StringRef
351 ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
352   StringRef PCH;
353   auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer);
354   if (OFOrErr) {
355     auto &OF = OFOrErr.get();
356     bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
357     // Find the clang AST section in the container.
358     for (auto &Section : OF->sections()) {
359       StringRef Name;
360       if (Expected<StringRef> NameOrErr = Section.getName())
361         Name = *NameOrErr;
362       else
363         consumeError(NameOrErr.takeError());
364 
365       if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) {
366         if (Expected<StringRef> E = Section.getContents())
367           return *E;
368         else {
369           handleAllErrors(E.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
370             EIB.log(llvm::errs());
371           });
372           return "";
373         }
374       }
375     }
376   }
377   handleAllErrors(OFOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
378     if (EIB.convertToErrorCode() ==
379         llvm::object::object_error::invalid_file_type)
380       // As a fallback, treat the buffer as a raw AST.
381       PCH = Buffer.getBuffer();
382     else
383       EIB.log(llvm::errs());
384   });
385   return PCH;
386 }
387