clang 19.0.0git
BackendConsumer.h
Go to the documentation of this file.
1//===--- BackendConsumer.h - LLVM BackendConsumer Header File -------------===//
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#ifndef LLVM_CLANG_LIB_CODEGEN_BACKENDCONSUMER_H
10#define LLVM_CLANG_LIB_CODEGEN_BACKENDCONSUMER_H
11
14
15#include "llvm/IR/DiagnosticInfo.h"
16#include "llvm/Support/Timer.h"
17
18namespace llvm {
19 class DiagnosticInfoDontCall;
20}
21
22namespace clang {
23class ASTContext;
24class CodeGenAction;
25class CoverageSourceInfo;
26
28 using LinkModule = CodeGenAction::LinkModule;
29
30 virtual void anchor();
31 DiagnosticsEngine &Diags;
32 BackendAction Action;
33 const HeaderSearchOptions &HeaderSearchOpts;
34 const CodeGenOptions &CodeGenOpts;
35 const TargetOptions &TargetOpts;
36 const LangOptions &LangOpts;
37 const FileManager &FileMgr;
38 std::unique_ptr<raw_pwrite_stream> AsmOutStream;
39 ASTContext *Context;
41
42 llvm::Timer LLVMIRGeneration;
43 unsigned LLVMIRGenerationRefCount;
44
45 /// True if we've finished generating IR. This prevents us from generating
46 /// additional LLVM IR after emitting output in HandleTranslationUnit. This
47 /// can happen when Clang plugins trigger additional AST deserialization.
48 bool IRGenFinished = false;
49
50 bool TimerIsEnabled = false;
51
52 std::unique_ptr<CodeGenerator> Gen;
53
55
56 // A map from mangled names to their function's source location, used for
57 // backend diagnostics as the Clang AST may be unavailable. We actually use
58 // the mangled name's hash as the key because mangled names can be very
59 // long and take up lots of space. Using a hash can cause name collision,
60 // but that is rare and the consequences are pointing to a wrong source
61 // location which is not severe. This is a vector instead of an actual map
62 // because we optimize for time building this map rather than time
63 // retrieving an entry, as backend diagnostics are uncommon.
64 std::vector<std::pair<llvm::hash_code, FullSourceLoc>>
65 ManglingFullSourceLocs;
66
67
68 // This is here so that the diagnostic printer knows the module a diagnostic
69 // refers to.
70 llvm::Module *CurLinkModule = nullptr;
71
72public:
75 const HeaderSearchOptions &HeaderSearchOpts,
76 const PreprocessorOptions &PPOpts,
77 const CodeGenOptions &CodeGenOpts,
78 const TargetOptions &TargetOpts, const LangOptions &LangOpts,
79 const FileManager &FileMgr, const std::string &InFile,
81 std::unique_ptr<raw_pwrite_stream> OS, llvm::LLVMContext &C,
82 CoverageSourceInfo *CoverageInfo = nullptr);
83
84 // This constructor is used in installing an empty BackendConsumer
85 // to use the clang diagnostic handler for IR input files. It avoids
86 // initializing the OS field.
89 const HeaderSearchOptions &HeaderSearchOpts,
90 const PreprocessorOptions &PPOpts,
91 const CodeGenOptions &CodeGenOpts,
92 const TargetOptions &TargetOpts, const LangOptions &LangOpts,
93 const FileManager &FileMgr, llvm::Module *Module,
94 SmallVector<LinkModule, 4> LinkModules, llvm::LLVMContext &C,
95 CoverageSourceInfo *CoverageInfo = nullptr);
96
97 llvm::Module *getModule() const;
98 std::unique_ptr<llvm::Module> takeModule();
99
101
103 void Initialize(ASTContext &Ctx) override;
104 bool HandleTopLevelDecl(DeclGroupRef D) override;
106 void HandleInterestingDecl(DeclGroupRef D) override;
107 void HandleTranslationUnit(ASTContext &C) override;
108 void HandleTagDeclDefinition(TagDecl *D) override;
109 void HandleTagDeclRequiredDefinition(const TagDecl *D) override;
110 void CompleteTentativeDefinition(VarDecl *D) override;
111 void CompleteExternalDeclaration(VarDecl *D) override;
112 void AssignInheritanceModel(CXXRecordDecl *RD) override;
113 void HandleVTable(CXXRecordDecl *RD) override;
114
115 // Links each entry in LinkModules into our module. Returns true on error.
116 bool LinkInModules(llvm::Module *M, bool ShouldLinkFiles = true);
117
118 // Load a bitcode module from -mlink-builtin-bitcode option using
119 // methods from a BackendConsumer instead of CompilerInstance
120 bool ReloadModules(llvm::Module *M);
121
122 /// Get the best possible source location to represent a diagnostic that
123 /// may have associated debug info.
125 const llvm::DiagnosticInfoWithLocationBase &D,
126 bool &BadDebugInfo, StringRef &Filename,
127 unsigned &Line, unsigned &Column) const;
128
129 std::optional<FullSourceLoc> getFunctionSourceLocation(
130 const llvm::Function &F) const;
131
132 void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
133 /// Specialized handler for InlineAsm diagnostic.
134 /// \return True if the diagnostic has been successfully reported, false
135 /// otherwise.
136 bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
137 /// Specialized handler for diagnostics reported using SMDiagnostic.
138 void SrcMgrDiagHandler(const llvm::DiagnosticInfoSrcMgr &D);
139 /// Specialized handler for StackSize diagnostic.
140 /// \return True if the diagnostic has been successfully reported, false
141 /// otherwise.
142 bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
143 /// Specialized handler for ResourceLimit diagnostic.
144 /// \return True if the diagnostic has been successfully reported, false
145 /// otherwise.
146 bool ResourceLimitDiagHandler(const llvm::DiagnosticInfoResourceLimit &D);
147
148 /// Specialized handler for unsupported backend feature diagnostic.
149 void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D);
150 /// Specialized handlers for optimization remarks.
151 /// Note that these handlers only accept remarks and they always handle
152 /// them.
153 void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
154 unsigned DiagID);
155 void
156 OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D);
158 const llvm::OptimizationRemarkAnalysisFPCommute &D);
160 const llvm::OptimizationRemarkAnalysisAliasing &D);
162 const llvm::DiagnosticInfoOptimizationFailure &D);
163 void DontCallDiagHandler(const llvm::DiagnosticInfoDontCall &D);
164 /// Specialized handler for misexpect warnings.
165 /// Note that misexpect remarks are emitted through ORE
166 void MisExpectDiagHandler(const llvm::DiagnosticInfoMisExpect &D);
167};
168
169} // namespace clang
170#endif
StringRef Filename
Definition: Format.cpp:2980
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Definition: ASTConsumer.h:33
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
llvm::Module * getModule() const
void OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D)
bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D)
Specialized handler for StackSize diagnostic.
void HandleVTable(CXXRecordDecl *RD) override
Callback involved at the end of a translation unit to notify the consumer that a vtable for the given...
void HandleTagDeclDefinition(TagDecl *D) override
HandleTagDeclDefinition - This callback is invoked each time a TagDecl (e.g.
bool HandleTopLevelDecl(DeclGroupRef D) override
HandleTopLevelDecl - Handle the specified top-level declaration.
void Initialize(ASTContext &Ctx) override
Initialize - This is called to initialize the consumer, providing the ASTContext.
void HandleInlineFunctionDefinition(FunctionDecl *D) override
This callback is invoked each time an inline (method or friend) function definition in a class is com...
void OptimizationFailureHandler(const llvm::DiagnosticInfoOptimizationFailure &D)
void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI)
This function is invoked when the backend needs to report something to the user.
void HandleTagDeclRequiredDefinition(const TagDecl *D) override
This callback is invoked the first time each TagDecl is required to be complete.
void HandleInterestingDecl(DeclGroupRef D) override
HandleInterestingDecl - Handle the specified interesting declaration.
void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override
HandleCXXStaticMemberVarInstantiation - Tell the consumer that this.
std::optional< FullSourceLoc > getFunctionSourceLocation(const llvm::Function &F) const
bool ResourceLimitDiagHandler(const llvm::DiagnosticInfoResourceLimit &D)
Specialized handler for ResourceLimit diagnostic.
std::unique_ptr< llvm::Module > takeModule()
void AssignInheritanceModel(CXXRecordDecl *RD) override
Callback invoked when an MSInheritanceAttr has been attached to a CXXRecordDecl.
BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, const HeaderSearchOptions &HeaderSearchOpts, const PreprocessorOptions &PPOpts, const CodeGenOptions &CodeGenOpts, const TargetOptions &TargetOpts, const LangOptions &LangOpts, const FileManager &FileMgr, llvm::Module *Module, SmallVector< LinkModule, 4 > LinkModules, llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo=nullptr)
bool LinkInModules(llvm::Module *M, bool ShouldLinkFiles=true)
bool ReloadModules(llvm::Module *M)
void HandleTranslationUnit(ASTContext &C) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
void CompleteTentativeDefinition(VarDecl *D) override
CompleteTentativeDefinition - Callback invoked at the end of a translation unit to notify the consume...
void CompleteExternalDeclaration(VarDecl *D) override
CompleteExternalDeclaration - Callback invoked at the end of a translation unit to notify the consume...
void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D)
Specialized handler for unsupported backend feature diagnostic.
bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D)
Specialized handler for InlineAsm diagnostic.
BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, const HeaderSearchOptions &HeaderSearchOpts, const PreprocessorOptions &PPOpts, const CodeGenOptions &CodeGenOpts, const TargetOptions &TargetOpts, const LangOptions &LangOpts, const FileManager &FileMgr, const std::string &InFile, SmallVector< LinkModule, 4 > LinkModules, std::unique_ptr< raw_pwrite_stream > OS, llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo=nullptr)
const FullSourceLoc getBestLocationFromDebugLoc(const llvm::DiagnosticInfoWithLocationBase &D, bool &BadDebugInfo, StringRef &Filename, unsigned &Line, unsigned &Column) const
Get the best possible source location to represent a diagnostic that may have associated debug info.
void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID)
Specialized handlers for optimization remarks.
void DontCallDiagHandler(const llvm::DiagnosticInfoDontCall &D)
void MisExpectDiagHandler(const llvm::DiagnosticInfoMisExpect &D)
Specialized handler for misexpect warnings.
CodeGenerator * getCodeGenerator()
void SrcMgrDiagHandler(const llvm::DiagnosticInfoSrcMgr &D)
Specialized handler for diagnostics reported using SMDiagnostic.
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
The primary public interface to the Clang code generator.
Definition: ModuleBuilder.h:48
Stores additional source code information like skipped ranges which is required by the coverage mappi...
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
A SourceLocation and its associated SourceManager.
Represents a function declaration or definition.
Definition: Decl.h:1959
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:418
Describes a module or submodule.
Definition: Module.h:105
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3549
Options for controlling the target.
Definition: TargetOptions.h:26
Represents a variable declaration or definition.
Definition: Decl.h:918
The JSON file list parser is used to communicate input to InstallAPI.
BackendAction
Definition: BackendUtil.h:35
YAML serialization mapping.
Definition: Dominators.h:30