clang 20.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 std::unique_ptr<raw_pwrite_stream> AsmOutStream;
38 ASTContext *Context;
40
41 llvm::Timer LLVMIRGeneration;
42 unsigned LLVMIRGenerationRefCount;
43
44 /// True if we've finished generating IR. This prevents us from generating
45 /// additional LLVM IR after emitting output in HandleTranslationUnit. This
46 /// can happen when Clang plugins trigger additional AST deserialization.
47 bool IRGenFinished = false;
48
49 bool TimerIsEnabled = false;
50
51 std::unique_ptr<CodeGenerator> Gen;
52
54
55 // A map from mangled names to their function's source location, used for
56 // backend diagnostics as the Clang AST may be unavailable. We actually use
57 // the mangled name's hash as the key because mangled names can be very
58 // long and take up lots of space. Using a hash can cause name collision,
59 // but that is rare and the consequences are pointing to a wrong source
60 // location which is not severe. This is a vector instead of an actual map
61 // because we optimize for time building this map rather than time
62 // retrieving an entry, as backend diagnostics are uncommon.
63 std::vector<std::pair<llvm::hash_code, FullSourceLoc>>
64 ManglingFullSourceLocs;
65
66
67 // This is here so that the diagnostic printer knows the module a diagnostic
68 // refers to.
69 llvm::Module *CurLinkModule = nullptr;
70
71public:
74 const HeaderSearchOptions &HeaderSearchOpts,
75 const PreprocessorOptions &PPOpts,
76 const CodeGenOptions &CodeGenOpts,
77 const TargetOptions &TargetOpts, const LangOptions &LangOpts,
78 const std::string &InFile,
80 std::unique_ptr<raw_pwrite_stream> OS, llvm::LLVMContext &C,
81 CoverageSourceInfo *CoverageInfo = nullptr);
82
83 // This constructor is used in installing an empty BackendConsumer
84 // to use the clang diagnostic handler for IR input files. It avoids
85 // initializing the OS field.
88 const HeaderSearchOptions &HeaderSearchOpts,
89 const PreprocessorOptions &PPOpts,
90 const CodeGenOptions &CodeGenOpts,
91 const TargetOptions &TargetOpts, const LangOptions &LangOpts,
92 llvm::Module *Module, SmallVector<LinkModule, 4> LinkModules,
93 llvm::LLVMContext &C,
94 CoverageSourceInfo *CoverageInfo = nullptr);
95
96 llvm::Module *getModule() const;
97 std::unique_ptr<llvm::Module> takeModule();
98
100
102 void Initialize(ASTContext &Ctx) override;
103 bool HandleTopLevelDecl(DeclGroupRef D) override;
105 void HandleInterestingDecl(DeclGroupRef D) override;
106 void HandleTranslationUnit(ASTContext &C) override;
107 void HandleTagDeclDefinition(TagDecl *D) override;
108 void HandleTagDeclRequiredDefinition(const TagDecl *D) override;
109 void CompleteTentativeDefinition(VarDecl *D) override;
111 void AssignInheritanceModel(CXXRecordDecl *RD) override;
112 void HandleVTable(CXXRecordDecl *RD) override;
113
114 // Links each entry in LinkModules into our module. Returns true on error.
115 bool LinkInModules(llvm::Module *M);
116
117 /// Get the best possible source location to represent a diagnostic that
118 /// may have associated debug info.
120 const llvm::DiagnosticInfoWithLocationBase &D,
121 bool &BadDebugInfo, StringRef &Filename,
122 unsigned &Line, unsigned &Column) const;
123
124 std::optional<FullSourceLoc> getFunctionSourceLocation(
125 const llvm::Function &F) const;
126
127 void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
128 /// Specialized handler for InlineAsm diagnostic.
129 /// \return True if the diagnostic has been successfully reported, false
130 /// otherwise.
131 bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
132 /// Specialized handler for diagnostics reported using SMDiagnostic.
133 void SrcMgrDiagHandler(const llvm::DiagnosticInfoSrcMgr &D);
134 /// Specialized handler for StackSize diagnostic.
135 /// \return True if the diagnostic has been successfully reported, false
136 /// otherwise.
137 bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
138 /// Specialized handler for ResourceLimit diagnostic.
139 /// \return True if the diagnostic has been successfully reported, false
140 /// otherwise.
141 bool ResourceLimitDiagHandler(const llvm::DiagnosticInfoResourceLimit &D);
142
143 /// Specialized handler for unsupported backend feature diagnostic.
144 void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D);
145 /// Specialized handlers for optimization remarks.
146 /// Note that these handlers only accept remarks and they always handle
147 /// them.
148 void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
149 unsigned DiagID);
150 void
151 OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D);
153 const llvm::OptimizationRemarkAnalysisFPCommute &D);
155 const llvm::OptimizationRemarkAnalysisAliasing &D);
157 const llvm::DiagnosticInfoOptimizationFailure &D);
158 void DontCallDiagHandler(const llvm::DiagnosticInfoDontCall &D);
159 /// Specialized handler for misexpect warnings.
160 /// Note that misexpect remarks are emitted through ORE
161 void MisExpectDiagHandler(const llvm::DiagnosticInfoMisExpect &D);
162};
163
164} // namespace clang
165#endif
const Decl * D
StringRef Filename
Definition: Format.cpp:3032
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Definition: ASTConsumer.h:34
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
llvm::Module * getModule() const
void CompleteExternalDeclaration(DeclaratorDecl *D) override
CompleteExternalDeclaration - Callback invoked at the end of a translation unit to notify the consume...
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.
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 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 std::string &InFile, SmallVector< LinkModule, 4 > LinkModules, std::unique_ptr< raw_pwrite_stream > OS, llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo=nullptr)
bool LinkInModules(llvm::Module *M)
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)
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, llvm::Module *Module, SmallVector< LinkModule, 4 > LinkModules, llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo=nullptr)
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:52
Stores additional source code information like skipped ranges which is required by the coverage mappi...
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:735
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
A SourceLocation and its associated SourceManager.
Represents a function declaration or definition.
Definition: Decl.h:1935
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:499
Describes a module or submodule.
Definition: Module.h:115
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:3564
Options for controlling the target.
Definition: TargetOptions.h:26
Represents a variable declaration or definition.
Definition: Decl.h:882
The JSON file list parser is used to communicate input to InstallAPI.
BackendAction
Definition: BackendUtil.h:35
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30