clang 24.0.0git
BackendDiagnosticHandler.h
Go to the documentation of this file.
1//===--- BackendDiagnosticHandler.h - Shared Backend Diagnostics -C++ -*-===//
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// This file declares BackendDiagnosticConsumer, which turns LLVM backend
10// diagnostics (llvm::DiagnosticInfo) into clang::DiagnosticsEngine
11// diagnostics. It is shared between classic CodeGen (BackendConsumer) and
12// CIR (CIRGenConsumer) so that both LLVM-emitting pipelines report backend
13// diagnostics (optimization remarks, inline-asm errors, unsupported
14// features, etc.) through the same clang diagnostics machinery instead of
15// falling back to LLVM's default stderr-printing handler.
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_CLANG_CODEGENUTILS_BACKENDDIAGNOSTICHANDLER_H
20#define LLVM_CLANG_CODEGENUTILS_BACKENDDIAGNOSTICHANDLER_H
21
23#include "llvm/ADT/Hashing.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/IR/DiagnosticHandler.h"
26#include <memory>
27#include <optional>
28#include <vector>
29
30namespace llvm {
31class DiagnosticInfo;
32class DiagnosticInfoDontCall;
33class DiagnosticInfoInlineAsm;
34class DiagnosticInfoMisExpect;
35class DiagnosticInfoOptimizationBase;
36class DiagnosticInfoOptimizationFailure;
37class DiagnosticInfoResourceLimit;
38class DiagnosticInfoSrcMgr;
39class DiagnosticInfoStackSize;
40class DiagnosticInfoUnsupported;
41class DiagnosticInfoUnsupportedTargetIntrinsic;
42class DiagnosticInfoWithLocationBase;
43class Function;
44class Module;
45class OptimizationRemarkAnalysisAliasing;
46class OptimizationRemarkAnalysisFPCommute;
47} // namespace llvm
48
49namespace clang {
50class CodeGenOptions;
52class SourceManager;
53
54/// Owns the state needed to translate LLVM backend diagnostics into clang
55/// diagnostics, and implements the translation itself. This is not itself an
56/// llvm::DiagnosticHandler so that it can be installed/updated independently
57/// of the llvm::LLVMContext's handler lifetime; use
58/// BackendDiagnosticConsumer::createDiagnosticHandler() to get an
59/// llvm::DiagnosticHandler that forwards into it.
61public:
63 const CodeGenOptions &CodeGenOpts)
64 : Diags(Diags), CodeGenOpts(CodeGenOpts) {}
65
66 /// The SourceManager used to translate backend-reported file:line:col
67 /// locations back into clang SourceLocations. May be null (e.g. when
68 /// compiling a raw LLVM IR input file with no clang AST), in which case
69 /// diagnostics are still reported, just without clang-level source
70 /// locations.
71 void setSourceManager(SourceManager *SM) { this->SM = SM; }
72
73 /// Set the module currently being linked in, used to name DK_Linker
74 /// diagnostics.
75 void setCurLinkModule(llvm::Module *M) { CurLinkModule = M; }
76
77 /// Record the source location of the function with the given mangled
78 /// name, used to approximate a location for backend diagnostics (e.g.
79 /// stack-size warnings) that don't carry debug-info locations of their
80 /// own.
81 void addFunctionSourceLocation(StringRef MangledName, FullSourceLoc Loc);
82
83 std::optional<FullSourceLoc>
84 getFunctionSourceLocation(const llvm::Function &F) const;
85
86 /// Get the best possible source location to represent a diagnostic that
87 /// may have associated debug info.
88 const FullSourceLoc
89 getBestLocationFromDebugLoc(const llvm::DiagnosticInfoWithLocationBase &D,
90 bool &BadDebugInfo, StringRef &Filename,
91 unsigned &Line, unsigned &Column) const;
92
93 /// Create an llvm::DiagnosticHandler that forwards diagnostics to this
94 /// consumer. The returned handler must not outlive this consumer.
95 std::unique_ptr<llvm::DiagnosticHandler> createDiagnosticHandler();
96
97 /// This is invoked when the backend needs to report something to the
98 /// user.
99 void handleDiagnostics(const llvm::DiagnosticInfo &DI);
100
101private:
102 /// Specialized handler for InlineAsm diagnostic.
103 /// \return True if the diagnostic has been successfully reported, false
104 /// otherwise.
105 bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
106 /// Specialized handler for diagnostics reported using SMDiagnostic.
107 void SrcMgrDiagHandler(const llvm::DiagnosticInfoSrcMgr &D);
108 /// Specialized handler for StackSize diagnostic.
109 /// \return True if the diagnostic has been successfully reported, false
110 /// otherwise.
111 bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
112 /// Specialized handler for ResourceLimit diagnostic.
113 /// \return True if the diagnostic has been successfully reported, false
114 /// otherwise.
115 bool ResourceLimitDiagHandler(const llvm::DiagnosticInfoResourceLimit &D);
116 /// Specialized handler for unsupported backend feature diagnostic.
117 void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D);
118 /// Specialized handler for unsupported target intrinsic diagnostic.
119 void UnsupportedTargetIntrinsicDiagHandler(
120 const llvm::DiagnosticInfoUnsupportedTargetIntrinsic &D);
121 /// Specialized handlers for optimization remarks.
122 /// Note that these handlers only accept remarks and they always handle
123 /// them.
124 void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
125 unsigned DiagID);
126 void OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D);
127 void
128 OptimizationRemarkHandler(const llvm::OptimizationRemarkAnalysisFPCommute &D);
129 void
130 OptimizationRemarkHandler(const llvm::OptimizationRemarkAnalysisAliasing &D);
131 void
132 OptimizationFailureHandler(const llvm::DiagnosticInfoOptimizationFailure &D);
133 void DontCallDiagHandler(const llvm::DiagnosticInfoDontCall &D);
134 /// Specialized handler for misexpect warnings.
135 /// Note that misexpect remarks are emitted through ORE
136 void MisExpectDiagHandler(const llvm::DiagnosticInfoMisExpect &D);
137
138 DiagnosticsEngine &Diags;
139 const CodeGenOptions &CodeGenOpts;
140 SourceManager *SM = nullptr;
141 llvm::Module *CurLinkModule = nullptr;
142
143 // A map from mangled names to their function's source location, used for
144 // backend diagnostics as the clang AST may be unavailable. We actually use
145 // the mangled name's hash as the key because mangled names can be very
146 // long and take up lots of space. Using a hash can cause name collision,
147 // but that is rare and the consequences are pointing to a wrong source
148 // location which is not severe. This is a vector instead of an actual map
149 // because we optimize for time building this map rather than time
150 // retrieving an entry, as backend diagnostics are uncommon.
151 std::vector<std::pair<llvm::hash_code, FullSourceLoc>> ManglingFullSourceLocs;
152};
153
154} // namespace clang
155
156#endif // LLVM_CLANG_CODEGENUTILS_BACKENDDIAGNOSTICHANDLER_H
Defines the clang::SourceLocation class and associated facilities.
BackendDiagnosticConsumer(DiagnosticsEngine &Diags, const CodeGenOptions &CodeGenOpts)
void addFunctionSourceLocation(StringRef MangledName, FullSourceLoc Loc)
Record the source location of the function with the given mangled name, used to approximate a locatio...
void setCurLinkModule(llvm::Module *M)
Set the module currently being linked in, used to name DK_Linker diagnostics.
std::unique_ptr< llvm::DiagnosticHandler > createDiagnosticHandler()
Create an llvm::DiagnosticHandler that forwards diagnostics to this consumer.
void setSourceManager(SourceManager *SM)
The SourceManager used to translate backend-reported file:line:col locations back into clang SourceLo...
std::optional< FullSourceLoc > getFunctionSourceLocation(const llvm::Function &F) const
void handleDiagnostics(const llvm::DiagnosticInfo &DI)
This is invoked when the backend needs to report something to the user.
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.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:232
A SourceLocation and its associated SourceManager.
This class handles loading and caching of source files into memory.
Top level wrappers for InstallAPI frontend operations.
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30