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/ADT/ArrayRef.h"
19 #include "llvm/Support/Casting.h"
23 // Forward declarations.
24 class DiagnosticPrinter;
31 /// \brief Defines the different supported severity of a diagnostic.
32 enum DiagnosticSeverity {
36 // A note attaches additional information to one of the previous diagnostic
41 /// \brief Defines the different supported kind of a diagnostic.
42 /// This enum should be extended with a new ID for each added concrete subclass.
46 DK_DebugMetadataVersion,
48 DK_OptimizationRemark,
52 /// \brief Get the next available kind ID for a plugin diagnostic.
53 /// Each time this function is called, it returns a different number.
54 /// Therefore, a plugin that wants to "identify" its own classes
55 /// with a dynamic identifier, just have to use this method to get a new ID
56 /// and assign it to each of its classes.
57 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
58 /// Thus, the plugin identifiers will not conflict with the
59 /// DiagnosticKind values.
60 int getNextAvailablePluginDiagnosticKind();
62 /// \brief This is the base abstract class for diagnostic reporting in
64 /// The print method must be overloaded by the subclasses to print a
65 /// user-friendly message in the client of the backend (let us call it a
67 class DiagnosticInfo {
69 /// Kind defines the kind of report this is about.
70 const /* DiagnosticKind */ int Kind;
71 /// Severity gives the severity of the diagnostic.
72 const DiagnosticSeverity Severity;
75 DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
76 : Kind(Kind), Severity(Severity) {}
78 virtual ~DiagnosticInfo() {}
80 /* DiagnosticKind */ int getKind() const { return Kind; }
81 DiagnosticSeverity getSeverity() const { return Severity; }
83 /// Print using the given \p DP a user-friendly message.
84 /// This is the default message that will be printed to the user.
85 /// It is used when the frontend does not directly take advantage
86 /// of the information contained in fields of the subclasses.
87 /// The printed message must not end with '.' nor start with a severity
89 virtual void print(DiagnosticPrinter &DP) const = 0;
92 /// Diagnostic information for inline asm reporting.
93 /// This is basically a message and an optional location.
94 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
96 /// Optional line information. 0 if not set.
98 /// Message to be reported.
100 /// Optional origin of the problem.
101 const Instruction *Instr;
104 /// \p MsgStr is the message to be reported to the frontend.
105 /// This class does not copy \p MsgStr, therefore the reference must be valid
106 /// for the whole life time of the Diagnostic.
107 DiagnosticInfoInlineAsm(const Twine &MsgStr,
108 DiagnosticSeverity Severity = DS_Error)
109 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
112 /// \p LocCookie if non-zero gives the line number for this report.
113 /// \p MsgStr gives the message.
114 /// This class does not copy \p MsgStr, therefore the reference must be valid
115 /// for the whole life time of the Diagnostic.
116 DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
117 DiagnosticSeverity Severity = DS_Error)
118 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
119 MsgStr(MsgStr), Instr(nullptr) {}
121 /// \p Instr gives the original instruction that triggered the diagnostic.
122 /// \p MsgStr gives the message.
123 /// This class does not copy \p MsgStr, therefore the reference must be valid
124 /// for the whole life time of the Diagnostic.
126 DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
127 DiagnosticSeverity Severity = DS_Error);
129 unsigned getLocCookie() const { return LocCookie; }
130 const Twine &getMsgStr() const { return MsgStr; }
131 const Instruction *getInstruction() const { return Instr; }
133 /// \see DiagnosticInfo::print.
134 void print(DiagnosticPrinter &DP) const override;
136 /// Hand rolled RTTI.
137 static bool classof(const DiagnosticInfo *DI) {
138 return DI->getKind() == DK_InlineAsm;
142 /// Diagnostic information for stack size reporting.
143 /// This is basically a function and a size.
144 class DiagnosticInfoStackSize : public DiagnosticInfo {
146 /// The function that is concerned by this stack size diagnostic.
148 /// The computed stack size.
152 /// \p The function that is concerned by this stack size diagnostic.
153 /// \p The computed stack size.
154 DiagnosticInfoStackSize(const Function &Fn, unsigned StackSize,
155 DiagnosticSeverity Severity = DS_Warning)
156 : DiagnosticInfo(DK_StackSize, Severity), Fn(Fn), StackSize(StackSize) {}
158 const Function &getFunction() const { return Fn; }
159 unsigned getStackSize() const { return StackSize; }
161 /// \see DiagnosticInfo::print.
162 void print(DiagnosticPrinter &DP) const override;
164 /// Hand rolled RTTI.
165 static bool classof(const DiagnosticInfo *DI) {
166 return DI->getKind() == DK_StackSize;
170 /// Diagnostic information for debug metadata version reporting.
171 /// This is basically a module and a version.
172 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
174 /// The module that is concerned by this debug metadata version diagnostic.
176 /// The actual metadata version.
177 unsigned MetadataVersion;
180 /// \p The module that is concerned by this debug metadata version diagnostic.
181 /// \p The actual metadata version.
182 DiagnosticInfoDebugMetadataVersion(const Module &M, unsigned MetadataVersion,
183 DiagnosticSeverity Severity = DS_Warning)
184 : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M),
185 MetadataVersion(MetadataVersion) {}
187 const Module &getModule() const { return M; }
188 unsigned getMetadataVersion() const { return MetadataVersion; }
190 /// \see DiagnosticInfo::print.
191 void print(DiagnosticPrinter &DP) const override;
193 /// Hand rolled RTTI.
194 static bool classof(const DiagnosticInfo *DI) {
195 return DI->getKind() == DK_DebugMetadataVersion;
199 /// Diagnostic information for the sample profiler.
200 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
202 DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
204 DiagnosticSeverity Severity = DS_Error)
205 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
206 LineNum(LineNum), Msg(Msg) {}
207 DiagnosticInfoSampleProfile(const char *FileName, const Twine &Msg,
208 DiagnosticSeverity Severity = DS_Error)
209 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
210 LineNum(0), Msg(Msg) {}
211 DiagnosticInfoSampleProfile(const Twine &Msg,
212 DiagnosticSeverity Severity = DS_Error)
213 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(nullptr),
214 LineNum(0), Msg(Msg) {}
216 /// \see DiagnosticInfo::print.
217 void print(DiagnosticPrinter &DP) const override;
219 /// Hand rolled RTTI.
220 static bool classof(const DiagnosticInfo *DI) {
221 return DI->getKind() == DK_SampleProfile;
224 const char *getFileName() const { return FileName; }
225 unsigned getLineNum() const { return LineNum; }
226 const Twine &getMsg() const { return Msg; }
229 /// Name of the input file associated with this diagnostic.
230 const char *FileName;
232 /// Line number where the diagnostic occured. If 0, no line number will
233 /// be emitted in the message.
236 /// Message to report.
240 /// Diagnostic information for optimization remarks.
241 class DiagnosticInfoOptimizationRemark : public DiagnosticInfo {
243 /// \p PassName is the name of the pass emitting this diagnostic. If
244 /// this name matches the regular expression given in -Rpass=, then the
245 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
246 /// is being emitted. \p DLoc is the location information to use in the
247 /// diagnostic. If line table information is available, the diagnostic
248 /// will include the source code location. \p Msg is the message to show.
249 /// Note that this class does not copy this message, so this reference
250 /// must be valid for the whole life time of the diagnostic.
251 DiagnosticInfoOptimizationRemark(const char *PassName, const Function &Fn,
252 const DebugLoc &DLoc, const Twine &Msg)
253 : DiagnosticInfo(DK_OptimizationRemark, DS_Remark), PassName(PassName),
254 Fn(Fn), DLoc(DLoc), Msg(Msg) {}
256 /// \see DiagnosticInfo::print.
257 void print(DiagnosticPrinter &DP) const override;
259 /// Hand rolled RTTI.
260 static bool classof(const DiagnosticInfo *DI) {
261 return DI->getKind() == DK_OptimizationRemark;
264 /// Return true if location information is available for this diagnostic.
265 bool isLocationAvailable() const;
267 /// Return a string with the location information for this diagnostic
268 /// in the format "file:line:col". If location information is not available,
269 /// it returns "<unknown>:0:0".
270 const StringRef getLocationStr() const;
272 /// Return location information for this diagnostic in three parts:
273 /// the source file name, line number and column.
274 void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
276 const char *getPassName() const { return PassName; }
277 const Function &getFunction() const { return Fn; }
278 const DebugLoc &getDebugLoc() const { return DLoc; }
279 const Twine &getMsg() const { return Msg; }
282 /// Name of the pass that triggers this report. If this matches the
283 /// regular expression given in -Rpass=regexp, then the remark will
285 const char *PassName;
287 /// Function where this diagnostic is triggered.
290 /// Debug location where this diagnostic is triggered.
291 const DebugLoc &DLoc;
293 /// Message to report.
297 } // End namespace llvm