23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
28 using namespace clang;
33 std::unique_ptr<const llvm::DataLayout> TD;
39 unsigned HandlingTopLevelDecls;
40 struct HandlingTopLevelDeclRAII {
41 CodeGeneratorImpl &Self;
42 HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self) : Self(Self) {
43 ++Self.HandlingTopLevelDecls;
45 ~HandlingTopLevelDeclRAII() {
46 if (--Self.HandlingTopLevelDecls == 0)
47 Self.EmitDeferredDecls();
54 std::unique_ptr<llvm::Module> M;
55 std::unique_ptr<CodeGen::CodeGenModule>
Builder;
66 : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO),
67 PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
68 CoverageInfo(CoverageInfo),
69 M(new llvm::
Module(ModuleName, C)) {}
71 ~CodeGeneratorImpl()
override {
73 assert(DeferredInlineMethodDefinitions.empty() ||
74 Diags.hasErrorOccurred());
77 llvm::Module* GetModule()
override {
81 const Decl *GetDeclForMangledName(StringRef MangledName)
override {
83 if (!
Builder->lookupRepresentativeDecl(MangledName, Result))
86 if (
auto FD = dyn_cast<FunctionDecl>(D)) {
89 }
else if (
auto TD = dyn_cast<TagDecl>(D)) {
90 if (
auto Def = TD->getDefinition())
96 llvm::Module *ReleaseModule()
override {
return M.release(); }
101 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
102 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
104 new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
108 CodeGenOpts, *M, *TD,
109 Diags, CoverageInfo));
111 for (
size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
112 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
115 void HandleCXXStaticMemberVarInstantiation(
VarDecl *VD)
override {
116 if (Diags.hasErrorOccurred())
119 Builder->HandleCXXStaticMemberVarInstantiation(VD);
123 if (Diags.hasErrorOccurred())
126 HandlingTopLevelDeclRAII HandlingDecl(*
this);
135 void EmitDeferredDecls() {
136 if (DeferredInlineMethodDefinitions.empty())
142 HandlingTopLevelDeclRAII HandlingDecl(*
this);
143 for (
unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I)
144 Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
145 DeferredInlineMethodDefinitions.clear();
148 void HandleInlineMethodDefinition(
CXXMethodDecl *D)
override {
149 if (Diags.hasErrorOccurred())
162 DeferredInlineMethodDefinitions.push_back(D);
168 Builder->AddDeferredUnusedCoverageMapping(D);
175 void HandleTagDeclDefinition(
TagDecl *D)
override {
176 if (Diags.hasErrorOccurred())
179 Builder->UpdateCompletedType(D);
183 if (Ctx->getLangOpts().MSVCCompat) {
185 if (
VarDecl *VD = dyn_cast<VarDecl>(Member)) {
186 if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
187 Ctx->DeclMustBeEmitted(VD)) {
195 void HandleTagDeclRequiredDefinition(
const TagDecl *D)
override {
196 if (Diags.hasErrorOccurred())
200 if (
const RecordDecl *RD = dyn_cast<RecordDecl>(D))
201 DI->completeRequiredType(RD);
204 void HandleTranslationUnit(
ASTContext &Ctx)
override {
205 if (Diags.hasErrorOccurred()) {
216 void CompleteTentativeDefinition(
VarDecl *D)
override {
217 if (Diags.hasErrorOccurred())
220 Builder->EmitTentativeDefinition(D);
224 if (Diags.hasErrorOccurred())
230 void HandleLinkerOptionPragma(llvm::StringRef Opts)
override {
231 Builder->AppendLinkerOptions(Opts);
234 void HandleDetectMismatch(llvm::StringRef Name,
235 llvm::StringRef
Value)
override {
236 Builder->AddDetectMismatch(Name, Value);
239 void HandleDependentLibrary(llvm::StringRef Lib)
override {
245 void CodeGenerator::anchor() { }
252 return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
253 PreprocessorOpts, CGO, C, CoverageInfo);
Defines the clang::ASTContext interface.
Stores additional source code information like skipped ranges which is required by the coverage mappi...
GlobalDecl getCanonicalDecl() const
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
const Decl * getDecl() const
Describes a module or submodule.
const CXXRecordDecl * getParent() const
Concrete class used by the front-end to report problems and issues.
The result type of a method or function.
ClassTemplateDecl * getDescribedClassTemplate() const
Retrieves the class template that is described by this class declaration.
bool doesThisDeclarationHaveABody() const
TagDecl - Represents the declaration of a struct/union/class/enum.
Represents a static or instance method of a struct/union/class.
Defines the Diagnostic-related interfaces.
Represents a C++ struct/union/class.
BoundNodesTreeBuilder *const Builder
CodeGenerator * CreateLLVMCodeGen(DiagnosticsEngine &Diags, const std::string &ModuleName, const HeaderSearchOptions &HeaderSearchOpts, const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO, llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo=nullptr)
Defines the clang::TargetInfo interface.