1 //===- llvm/Support/DiagnosticInfo.h - Diagnostic Declaration ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the different classes involved in low level diagnostics.
12 // Diagnostics reporting is still done as part of the LLVMContext.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_DIAGNOSTICINFO_H
16 #define LLVM_SUPPORT_DIAGNOSTICINFO_H
18 #include "llvm-c/Core.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/IR/DebugLoc.h"
21 #include "llvm/Support/Casting.h"
25 // Forward declarations.
26 class DiagnosticPrinter;
33 /// \brief Defines the different supported severity of a diagnostic.
34 enum DiagnosticSeverity {
38 // A note attaches additional information to one of the previous diagnostic
43 /// \brief Defines the different supported kind of a diagnostic.
44 /// This enum should be extended with a new ID for each added concrete subclass.
48 DK_DebugMetadataVersion,
50 DK_OptimizationRemark,
54 /// \brief Get the next available kind ID for a plugin diagnostic.
55 /// Each time this function is called, it returns a different number.
56 /// Therefore, a plugin that wants to "identify" its own classes
57 /// with a dynamic identifier, just have to use this method to get a new ID
58 /// and assign it to each of its classes.
59 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
60 /// Thus, the plugin identifiers will not conflict with the
61 /// DiagnosticKind values.
62 int getNextAvailablePluginDiagnosticKind();
64 /// \brief This is the base abstract class for diagnostic reporting in
66 /// The print method must be overloaded by the subclasses to print a
67 /// user-friendly message in the client of the backend (let us call it a
69 class DiagnosticInfo {
71 /// Kind defines the kind of report this is about.
72 const /* DiagnosticKind */ int Kind;
73 /// Severity gives the severity of the diagnostic.
74 const DiagnosticSeverity Severity;
77 DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
78 : Kind(Kind), Severity(Severity) {}
80 virtual ~DiagnosticInfo() {}
82 /* DiagnosticKind */ int getKind() const { return Kind; }
83 DiagnosticSeverity getSeverity() const { return Severity; }
85 /// Print using the given \p DP a user-friendly message.
86 /// This is the default message that will be printed to the user.
87 /// It is used when the frontend does not directly take advantage
88 /// of the information contained in fields of the subclasses.
89 /// The printed message must not end with '.' nor start with a severity
91 virtual void print(DiagnosticPrinter &DP) const = 0;
94 /// Diagnostic information for inline asm reporting.
95 /// This is basically a message and an optional location.
96 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
98 /// Optional line information. 0 if not set.
100 /// Message to be reported.
102 /// Optional origin of the problem.
103 const Instruction *Instr;
106 /// \p MsgStr is the message to be reported to the frontend.
107 /// This class does not copy \p MsgStr, therefore the reference must be valid
108 /// for the whole life time of the Diagnostic.
109 DiagnosticInfoInlineAsm(const Twine &MsgStr,
110 DiagnosticSeverity Severity = DS_Error)
111 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
114 /// \p LocCookie if non-zero gives the line number for this report.
115 /// \p MsgStr gives the message.
116 /// This class does not copy \p MsgStr, therefore the reference must be valid
117 /// for the whole life time of the Diagnostic.
118 DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
119 DiagnosticSeverity Severity = DS_Error)
120 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
121 MsgStr(MsgStr), Instr(nullptr) {}
123 /// \p Instr gives the original instruction that triggered the diagnostic.
124 /// \p MsgStr gives the message.
125 /// This class does not copy \p MsgStr, therefore the reference must be valid
126 /// for the whole life time of the Diagnostic.
128 DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
129 DiagnosticSeverity Severity = DS_Error);
131 unsigned getLocCookie() const { return LocCookie; }
132 const Twine &getMsgStr() const { return MsgStr; }
133 const Instruction *getInstruction() const { return Instr; }
135 /// \see DiagnosticInfo::print.
136 void print(DiagnosticPrinter &DP) const override;
138 /// Hand rolled RTTI.
139 static bool classof(const DiagnosticInfo *DI) {
140 return DI->getKind() == DK_InlineAsm;
144 /// Diagnostic information for stack size reporting.
145 /// This is basically a function and a size.
146 class DiagnosticInfoStackSize : public DiagnosticInfo {
148 /// The function that is concerned by this stack size diagnostic.
150 /// The computed stack size.
154 /// \p The function that is concerned by this stack size diagnostic.
155 /// \p The computed stack size.
156 DiagnosticInfoStackSize(const Function &Fn, unsigned StackSize,
157 DiagnosticSeverity Severity = DS_Warning)
158 : DiagnosticInfo(DK_StackSize, Severity), Fn(Fn), StackSize(StackSize) {}
160 const Function &getFunction() const { return Fn; }
161 unsigned getStackSize() const { return StackSize; }
163 /// \see DiagnosticInfo::print.
164 void print(DiagnosticPrinter &DP) const override;
166 /// Hand rolled RTTI.
167 static bool classof(const DiagnosticInfo *DI) {
168 return DI->getKind() == DK_StackSize;
172 /// Diagnostic information for debug metadata version reporting.
173 /// This is basically a module and a version.
174 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
176 /// The module that is concerned by this debug metadata version diagnostic.
178 /// The actual metadata version.
179 unsigned MetadataVersion;
182 /// \p The module that is concerned by this debug metadata version diagnostic.
183 /// \p The actual metadata version.
184 DiagnosticInfoDebugMetadataVersion(const Module &M, unsigned MetadataVersion,
185 DiagnosticSeverity Severity = DS_Warning)
186 : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M),
187 MetadataVersion(MetadataVersion) {}
189 const Module &getModule() const { return M; }
190 unsigned getMetadataVersion() const { return MetadataVersion; }
192 /// \see DiagnosticInfo::print.
193 void print(DiagnosticPrinter &DP) const override;
195 /// Hand rolled RTTI.
196 static bool classof(const DiagnosticInfo *DI) {
197 return DI->getKind() == DK_DebugMetadataVersion;
201 /// Diagnostic information for the sample profiler.
202 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
204 DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
206 DiagnosticSeverity Severity = DS_Error)
207 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
208 LineNum(LineNum), Msg(Msg) {}
209 DiagnosticInfoSampleProfile(const char *FileName, const Twine &Msg,
210 DiagnosticSeverity Severity = DS_Error)
211 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
212 LineNum(0), Msg(Msg) {}
213 DiagnosticInfoSampleProfile(const Twine &Msg,
214 DiagnosticSeverity Severity = DS_Error)
215 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(nullptr),
216 LineNum(0), Msg(Msg) {}
218 /// \see DiagnosticInfo::print.
219 void print(DiagnosticPrinter &DP) const override;
221 /// Hand rolled RTTI.
222 static bool classof(const DiagnosticInfo *DI) {
223 return DI->getKind() == DK_SampleProfile;
226 const char *getFileName() const { return FileName; }
227 unsigned getLineNum() const { return LineNum; }
228 const Twine &getMsg() const { return Msg; }
231 /// Name of the input file associated with this diagnostic.
232 const char *FileName;
234 /// Line number where the diagnostic occurred. If 0, no line number will
235 /// be emitted in the message.
238 /// Message to report.
242 /// Diagnostic information for optimization remarks.
243 class DiagnosticInfoOptimizationRemark : public DiagnosticInfo {
245 /// \p PassName is the name of the pass emitting this diagnostic. If
246 /// this name matches the regular expression given in -Rpass=, then the
247 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
248 /// is being emitted. \p DLoc is the location information to use in the
249 /// diagnostic. If line table information is available, the diagnostic
250 /// will include the source code location. \p Msg is the message to show.
251 /// Note that this class does not copy this message, so this reference
252 /// must be valid for the whole life time of the diagnostic.
253 DiagnosticInfoOptimizationRemark(const char *PassName, const Function &Fn,
254 const DebugLoc &DLoc, const Twine &Msg)
255 : DiagnosticInfo(DK_OptimizationRemark, DS_Remark), PassName(PassName),
256 Fn(Fn), DLoc(DLoc), Msg(Msg) {}
258 /// \see DiagnosticInfo::print.
259 void print(DiagnosticPrinter &DP) const override;
261 /// Hand rolled RTTI.
262 static bool classof(const DiagnosticInfo *DI) {
263 return DI->getKind() == DK_OptimizationRemark;
266 /// Return true if location information is available for this diagnostic.
267 bool isLocationAvailable() const;
269 /// Return a string with the location information for this diagnostic
270 /// in the format "file:line:col". If location information is not available,
271 /// it returns "<unknown>:0:0".
272 const std::string getLocationStr() const;
274 /// Return location information for this diagnostic in three parts:
275 /// the source file name, line number and column.
276 void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
278 const char *getPassName() const { return PassName; }
279 const Function &getFunction() const { return Fn; }
280 const DebugLoc &getDebugLoc() const { return DLoc; }
281 const Twine &getMsg() const { return Msg; }
284 /// Name of the pass that triggers this report. If this matches the
285 /// regular expression given in -Rpass=regexp, then the remark will
287 const char *PassName;
289 /// Function where this diagnostic is triggered.
292 /// Debug location where this diagnostic is triggered.
295 /// Message to report.
299 // Create wrappers for C Binding types (see CBindingWrapping.h).
300 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DiagnosticInfo, LLVMDiagnosticInfoRef)
302 } // End namespace llvm