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
24#include "llvm/ADT/ArrayRef.h"
25#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/Hashing.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/IR/DiagnosticHandler.h"
29#include <memory>
30#include <optional>
31#include <utility>
32#include <vector>
33
34namespace llvm {
35class DIFile;
36class DiagnosticInfo;
37class DiagnosticInfoDontCall;
38class DiagnosticInfoInlineAsm;
39class DiagnosticInfoMisExpect;
40class DiagnosticInfoOptimizationBase;
41class DiagnosticInfoOptimizationFailure;
42class DiagnosticInfoResourceLimit;
43class DiagnosticInfoSrcMgr;
44class DiagnosticInfoStackSize;
45class DiagnosticInfoUnsupported;
46class DiagnosticInfoUnsupportedTargetIntrinsic;
47class DiagnosticInfoWithLocationBase;
48class Function;
49class Module;
50class OptimizationRemarkAnalysisAliasing;
51class OptimizationRemarkAnalysisFPCommute;
52} // namespace llvm
53
54namespace clang {
55class CodeGenOptions;
57class SourceManager;
58
59/// Owns the state needed to translate LLVM backend diagnostics into clang
60/// diagnostics, and implements the translation itself. This is not itself an
61/// llvm::DiagnosticHandler so that it can be installed/updated independently
62/// of the llvm::LLVMContext's handler lifetime; use
63/// BackendDiagnosticConsumer::createDiagnosticHandler() to get an
64/// llvm::DiagnosticHandler that forwards into it.
66public:
68 const CodeGenOptions &CodeGenOpts)
69 : Diags(Diags), CodeGenOpts(CodeGenOpts) {}
70
71 /// The SourceManager used to translate backend-reported file:line:col
72 /// locations back into clang SourceLocations. May be null (e.g. when
73 /// compiling a raw LLVM IR input file with no clang AST), in which case
74 /// diagnostics are still reported, just without clang-level source
75 /// locations.
76 void setSourceManager(SourceManager *SM) { this->SM = SM; }
77
78 /// Set the module currently being linked in, used to name DK_Linker
79 /// diagnostics.
80 void setCurLinkModule(llvm::Module *M) { CurLinkModule = M; }
81
82 /// Record the source location of the function with the given mangled
83 /// name, used to approximate a location for backend diagnostics (e.g.
84 /// stack-size warnings) that don't carry debug-info locations of their
85 /// own.
86 void addFunctionSourceLocation(StringRef MangledName, FullSourceLoc Loc);
87
88 std::optional<FullSourceLoc>
89 getFunctionSourceLocation(const llvm::Function &F) const;
90
91 /// Set the source file of each DIFile that clang created, or std::nullopt
92 /// for a DIFile shared by several files, which identifies none of them.
93 /// Debug locations in other DIFiles, e.g. from LLVM IR input, are matched to
94 /// source files by name.
96 llvm::DenseMap<const llvm::DIFile *, OptionalFileEntryRef> Sources) {
97 DIFileSources = std::move(Sources);
98 }
99
100 /// Get the best possible source location to represent a diagnostic that
101 /// may have associated debug info.
102 const FullSourceLoc
103 getBestLocationFromDebugLoc(const llvm::DiagnosticInfoWithLocationBase &D,
104 bool &BadDebugInfo, StringRef &Filename,
105 unsigned &Line, unsigned &Column) const;
106
107 /// Create an llvm::DiagnosticHandler that forwards diagnostics to this
108 /// consumer. The returned handler must not outlive this consumer.
109 std::unique_ptr<llvm::DiagnosticHandler> createDiagnosticHandler();
110
111 /// This is invoked when the backend needs to report something to the
112 /// user.
113 void handleDiagnostics(const llvm::DiagnosticInfo &DI);
114
115private:
116 /// Specialized handler for InlineAsm diagnostic.
117 /// \return True if the diagnostic has been successfully reported, false
118 /// otherwise.
119 bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
120 /// Specialized handler for diagnostics reported using SMDiagnostic.
121 void SrcMgrDiagHandler(const llvm::DiagnosticInfoSrcMgr &D);
122 /// Specialized handler for StackSize diagnostic.
123 /// \return True if the diagnostic has been successfully reported, false
124 /// otherwise.
125 bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
126 /// Specialized handler for ResourceLimit diagnostic.
127 /// \return True if the diagnostic has been successfully reported, false
128 /// otherwise.
129 bool ResourceLimitDiagHandler(const llvm::DiagnosticInfoResourceLimit &D);
130 /// Specialized handler for unsupported backend feature diagnostic.
131 void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D);
132 /// Specialized handler for unsupported target intrinsic diagnostic.
133 void UnsupportedTargetIntrinsicDiagHandler(
134 const llvm::DiagnosticInfoUnsupportedTargetIntrinsic &D);
135 /// Specialized handlers for optimization remarks.
136 /// Note that these handlers only accept remarks and they always handle
137 /// them.
138 void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
139 unsigned DiagID);
140 void OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D);
141 void
142 OptimizationRemarkHandler(const llvm::OptimizationRemarkAnalysisFPCommute &D);
143 void
144 OptimizationRemarkHandler(const llvm::OptimizationRemarkAnalysisAliasing &D);
145 void
146 OptimizationFailureHandler(const llvm::DiagnosticInfoOptimizationFailure &D);
147 void DontCallDiagHandler(const llvm::DiagnosticInfoDontCall &D);
148 /// Specialized handler for misexpect warnings.
149 /// Note that misexpect remarks are emitted through ORE
150 void MisExpectDiagHandler(const llvm::DiagnosticInfoMisExpect &D);
151
152 /// Find the source file for a debug location in \p File, falling back to
153 /// looking up \p Names in order if \p File is not in DIFileSources.
154 OptionalFileEntryRef getSourceFile(const llvm::DIFile *File,
155 ArrayRef<StringRef> Names) const;
156
157 DiagnosticsEngine &Diags;
158 const CodeGenOptions &CodeGenOpts;
159 SourceManager *SM = nullptr;
160 llvm::Module *CurLinkModule = nullptr;
161
162 // A map from mangled names to their function's source location, used for
163 // backend diagnostics as the clang AST may be unavailable. We actually use
164 // the mangled name's hash as the key because mangled names can be very
165 // long and take up lots of space. Using a hash can cause name collision,
166 // but that is rare and the consequences are pointing to a wrong source
167 // location which is not severe. This is a vector instead of an actual map
168 // because we optimize for time building this map rather than time
169 // retrieving an entry, as backend diagnostics are uncommon.
170 std::vector<std::pair<llvm::hash_code, FullSourceLoc>> ManglingFullSourceLocs;
171
172 llvm::DenseMap<const llvm::DIFile *, OptionalFileEntryRef> DIFileSources;
173};
174
175} // namespace clang
176
177#endif // LLVM_CLANG_CODEGENUTILS_BACKENDDIAGNOSTICHANDLER_H
Defines interfaces for clang::FileEntry and clang::FileEntryRef.
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.
void setDIFileSources(llvm::DenseMap< const llvm::DIFile *, OptionalFileEntryRef > Sources)
Set the source file of each DIFile that clang created, or std::nullopt for a DIFile shared by several...
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:240
A SourceLocation and its associated SourceManager.
This class handles loading and caching of source files into memory.
Top level wrappers for InstallAPI frontend operations.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:196
@ 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