[C++11] More 'nullptr' conversion or in some cases just using a boolean check instead...
[oota-llvm.git] / include / llvm / IR / DiagnosticInfo.h
1 //===- llvm/Support/DiagnosticInfo.h - Diagnostic Declaration ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the different classes involved in low level diagnostics.
11 //
12 // Diagnostics reporting is still done as part of the LLVMContext.
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_DIAGNOSTICINFO_H
16 #define LLVM_SUPPORT_DIAGNOSTICINFO_H
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/Support/Casting.h"
20
21 namespace llvm {
22
23 // Forward declarations.
24 class DiagnosticPrinter;
25 class Function;
26 class Instruction;
27 class Twine;
28 class Value;
29 class DebugLoc;
30
31 /// \brief Defines the different supported severity of a diagnostic.
32 enum DiagnosticSeverity {
33   DS_Error,
34   DS_Warning,
35   DS_Remark,
36   // A note attaches additional information to one of the previous diagnostic
37   // types.
38   DS_Note
39 };
40
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.
43 enum DiagnosticKind {
44   DK_InlineAsm,
45   DK_StackSize,
46   DK_DebugMetadataVersion,
47   DK_SampleProfile,
48   DK_OptimizationRemark,
49   DK_FirstPluginKind
50 };
51
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();
61
62 /// \brief This is the base abstract class for diagnostic reporting in
63 /// the backend.
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
66 /// frontend).
67 class DiagnosticInfo {
68 private:
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;
73
74 public:
75   DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
76       : Kind(Kind), Severity(Severity) {}
77
78   virtual ~DiagnosticInfo() {}
79
80   /* DiagnosticKind */ int getKind() const { return Kind; }
81   DiagnosticSeverity getSeverity() const { return Severity; }
82
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
88   /// keyword.
89   virtual void print(DiagnosticPrinter &DP) const = 0;
90 };
91
92 /// Diagnostic information for inline asm reporting.
93 /// This is basically a message and an optional location.
94 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
95 private:
96   /// Optional line information. 0 if not set.
97   unsigned LocCookie;
98   /// Message to be reported.
99   const Twine &MsgStr;
100   /// Optional origin of the problem.
101   const Instruction *Instr;
102
103 public:
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),
110         Instr(nullptr) {}
111
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) {}
120
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.
125   /// Same for \p I.
126   DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
127                           DiagnosticSeverity Severity = DS_Error);
128
129   unsigned getLocCookie() const { return LocCookie; }
130   const Twine &getMsgStr() const { return MsgStr; }
131   const Instruction *getInstruction() const { return Instr; }
132
133   /// \see DiagnosticInfo::print.
134   void print(DiagnosticPrinter &DP) const override;
135
136   /// Hand rolled RTTI.
137   static bool classof(const DiagnosticInfo *DI) {
138     return DI->getKind() == DK_InlineAsm;
139   }
140 };
141
142 /// Diagnostic information for stack size reporting.
143 /// This is basically a function and a size.
144 class DiagnosticInfoStackSize : public DiagnosticInfo {
145 private:
146   /// The function that is concerned by this stack size diagnostic.
147   const Function &Fn;
148   /// The computed stack size.
149   unsigned StackSize;
150
151 public:
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) {}
157
158   const Function &getFunction() const { return Fn; }
159   unsigned getStackSize() const { return StackSize; }
160
161   /// \see DiagnosticInfo::print.
162   void print(DiagnosticPrinter &DP) const override;
163
164   /// Hand rolled RTTI.
165   static bool classof(const DiagnosticInfo *DI) {
166     return DI->getKind() == DK_StackSize;
167   }
168 };
169
170 /// Diagnostic information for debug metadata version reporting.
171 /// This is basically a module and a version.
172 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
173 private:
174   /// The module that is concerned by this debug metadata version diagnostic.
175   const Module &M;
176   /// The actual metadata version.
177   unsigned MetadataVersion;
178
179 public:
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) {}
186
187   const Module &getModule() const { return M; }
188   unsigned getMetadataVersion() const { return MetadataVersion; }
189
190   /// \see DiagnosticInfo::print.
191   void print(DiagnosticPrinter &DP) const override;
192
193   /// Hand rolled RTTI.
194   static bool classof(const DiagnosticInfo *DI) {
195     return DI->getKind() == DK_DebugMetadataVersion;
196   }
197 };
198
199 /// Diagnostic information for the sample profiler.
200 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
201 public:
202   DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
203                               const Twine &Msg,
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) {}
215
216   /// \see DiagnosticInfo::print.
217   void print(DiagnosticPrinter &DP) const override;
218
219   /// Hand rolled RTTI.
220   static bool classof(const DiagnosticInfo *DI) {
221     return DI->getKind() == DK_SampleProfile;
222   }
223
224   const char *getFileName() const { return FileName; }
225   unsigned getLineNum() const { return LineNum; }
226   const Twine &getMsg() const { return Msg; }
227
228 private:
229   /// Name of the input file associated with this diagnostic.
230   const char *FileName;
231
232   /// Line number where the diagnostic occured. If 0, no line number will
233   /// be emitted in the message.
234   unsigned LineNum;
235
236   /// Message to report.
237   const Twine &Msg;
238 };
239
240 /// Diagnostic information for optimization remarks.
241 class DiagnosticInfoOptimizationRemark : public DiagnosticInfo {
242 public:
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) {}
255
256   /// \see DiagnosticInfo::print.
257   void print(DiagnosticPrinter &DP) const override;
258
259   /// Hand rolled RTTI.
260   static bool classof(const DiagnosticInfo *DI) {
261     return DI->getKind() == DK_OptimizationRemark;
262   }
263
264   /// Return true if location information is available for this diagnostic.
265   bool isLocationAvailable() const;
266
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;
271
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;
275
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; }
280
281 private:
282   /// Name of the pass that triggers this report. If this matches the
283   /// regular expression given in -Rpass=regexp, then the remark will
284   /// be emitted.
285   const char *PassName;
286
287   /// Function where this diagnostic is triggered.
288   const Function &Fn;
289
290   /// Debug location where this diagnostic is triggered.
291   const DebugLoc &DLoc;
292
293   /// Message to report.
294   const Twine &Msg;
295 };
296
297 } // End namespace llvm
298
299 #endif