clang 23.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
15
16#include "llvm/IR/DiagnosticInfo.h"
17#include "llvm/Support/Timer.h"
18
19namespace llvm {
20 class DiagnosticInfoDontCall;
21}
22
23namespace clang {
24class ASTContext;
25class CodeGenAction;
27
29 virtual void anchor();
31 DiagnosticsEngine &Diags;
32 const CodeGenOptions &CodeGenOpts;
33 const TargetOptions &TargetOpts;
34 const LangOptions &LangOpts;
35 std::unique_ptr<raw_pwrite_stream> AsmOutStream;
36 ASTContext *Context = nullptr;
38
39 llvm::Timer LLVMIRGeneration;
40 unsigned LLVMIRGenerationRefCount = 0;
41
42 bool TimerIsEnabled = false;
43
44 BackendAction Action;
45
46 std::unique_ptr<CodeGenerator> Gen;
47
49
50 // A map from mangled names to their function's source location, used for
51 // backend diagnostics as the Clang AST may be unavailable. We actually use
52 // the mangled name's hash as the key because mangled names can be very
53 // long and take up lots of space. Using a hash can cause name collision,
54 // but that is rare and the consequences are pointing to a wrong source
55 // location which is not severe. This is a vector instead of an actual map
56 // because we optimize for time building this map rather than time
57 // retrieving an entry, as backend diagnostics are uncommon.
58 std::vector<std::pair<llvm::hash_code, FullSourceLoc>>
59 ManglingFullSourceLocs;
60
61
62 // This is here so that the diagnostic printer knows the module a diagnostic
63 // refers to.
64 llvm::Module *CurLinkModule = nullptr;
65
66public:
69 llvm::LLVMContext &C, SmallVector<LinkModule, 4> LinkModules,
70 StringRef InFile, std::unique_ptr<raw_pwrite_stream> OS,
71 CoverageSourceInfo *CoverageInfo,
72 llvm::Module *CurLinkModule = nullptr);
73
74 llvm::Module *getModule() const;
75 std::unique_ptr<llvm::Module> takeModule();
76
78
80 void Initialize(ASTContext &Ctx) override;
81 bool HandleTopLevelDecl(DeclGroupRef D) override;
83 void HandleInterestingDecl(DeclGroupRef D) override;
84 void HandleTranslationUnit(ASTContext &C) override;
85 void HandleTagDeclDefinition(TagDecl *D) override;
86 void HandleTagDeclRequiredDefinition(const TagDecl *D) override;
87 void CompleteTentativeDefinition(VarDecl *D) override;
89 void AssignInheritanceModel(CXXRecordDecl *RD) override;
90 void HandleVTable(CXXRecordDecl *RD) override;
91
92 // Links each entry in LinkModules into our module. Returns true on error.
93 bool LinkInModules(llvm::Module *M);
94
95 /// Get the best possible source location to represent a diagnostic that
96 /// may have associated debug info.
98 const llvm::DiagnosticInfoWithLocationBase &D,
99 bool &BadDebugInfo, StringRef &Filename,
100 unsigned &Line, unsigned &Column) const;
101
102 std::optional<FullSourceLoc> getFunctionSourceLocation(
103 const llvm::Function &F) const;
104
105 void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
106 /// Specialized handler for InlineAsm diagnostic.
107 /// \return True if the diagnostic has been successfully reported, false
108 /// otherwise.
109 bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
110 /// Specialized handler for diagnostics reported using SMDiagnostic.
111 void SrcMgrDiagHandler(const llvm::DiagnosticInfoSrcMgr &D);
112 /// Specialized handler for StackSize diagnostic.
113 /// \return True if the diagnostic has been successfully reported, false
114 /// otherwise.
115 bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
116 /// Specialized handler for ResourceLimit diagnostic.
117 /// \return True if the diagnostic has been successfully reported, false
118 /// otherwise.
119 bool ResourceLimitDiagHandler(const llvm::DiagnosticInfoResourceLimit &D);
120
121 /// Specialized handler for unsupported backend feature diagnostic.
122 void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D);
123 /// Specialized handlers for optimization remarks.
124 /// Note that these handlers only accept remarks and they always handle
125 /// them.
126 void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
127 unsigned DiagID);
128 void
129 OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D);
131 const llvm::OptimizationRemarkAnalysisFPCommute &D);
133 const llvm::OptimizationRemarkAnalysisAliasing &D);
135 const llvm::DiagnosticInfoOptimizationFailure &D);
136 void DontCallDiagHandler(const llvm::DiagnosticInfoDontCall &D);
137 /// Specialized handler for misexpect warnings.
138 /// Note that misexpect remarks are emitted through ORE
139 void MisExpectDiagHandler(const llvm::DiagnosticInfoMisExpect &D);
140};
141
142} // namespace clang
143#endif
ASTConsumer()=default
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
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.
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)
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.
BackendConsumer(CompilerInstance &CI, BackendAction Action, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, llvm::LLVMContext &C, SmallVector< LinkModule, 4 > LinkModules, StringRef InFile, std::unique_ptr< raw_pwrite_stream > OS, CoverageSourceInfo *CoverageInfo, llvm::Module *CurLinkModule=nullptr)
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.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
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:780
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
A SourceLocation and its associated SourceManager.
Represents a function declaration or definition.
Definition Decl.h:2018
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3739
Options for controlling the target.
Represents a variable declaration or definition.
Definition Decl.h:924
The JSON file list parser is used to communicate input to InstallAPI.
BackendAction
Definition BackendUtil.h:33
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30