Inliner::OptimizationRemark: Fix crash in clang/test/Frontend/optimization-remark...
[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-c/Core.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/IR/DebugLoc.h"
21 #include "llvm/Support/Casting.h"
22
23 namespace llvm {
24
25 // Forward declarations.
26 class DiagnosticPrinter;
27 class Function;
28 class Instruction;
29 class Twine;
30 class Value;
31 class DebugLoc;
32
33 /// \brief Defines the different supported severity of a diagnostic.
34 enum DiagnosticSeverity {
35   DS_Error,
36   DS_Warning,
37   DS_Remark,
38   // A note attaches additional information to one of the previous diagnostic
39   // types.
40   DS_Note
41 };
42
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.
45 enum DiagnosticKind {
46   DK_InlineAsm,
47   DK_StackSize,
48   DK_DebugMetadataVersion,
49   DK_SampleProfile,
50   DK_OptimizationRemark,
51   DK_FirstPluginKind
52 };
53
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();
63
64 /// \brief This is the base abstract class for diagnostic reporting in
65 /// the backend.
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
68 /// frontend).
69 class DiagnosticInfo {
70 private:
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;
75
76 public:
77   DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
78       : Kind(Kind), Severity(Severity) {}
79
80   virtual ~DiagnosticInfo() {}
81
82   /* DiagnosticKind */ int getKind() const { return Kind; }
83   DiagnosticSeverity getSeverity() const { return Severity; }
84
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
90   /// keyword.
91   virtual void print(DiagnosticPrinter &DP) const = 0;
92 };
93
94 /// Diagnostic information for inline asm reporting.
95 /// This is basically a message and an optional location.
96 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
97 private:
98   /// Optional line information. 0 if not set.
99   unsigned LocCookie;
100   /// Message to be reported.
101   const Twine &MsgStr;
102   /// Optional origin of the problem.
103   const Instruction *Instr;
104
105 public:
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),
112         Instr(nullptr) {}
113
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) {}
122
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.
127   /// Same for \p I.
128   DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
129                           DiagnosticSeverity Severity = DS_Error);
130
131   unsigned getLocCookie() const { return LocCookie; }
132   const Twine &getMsgStr() const { return MsgStr; }
133   const Instruction *getInstruction() const { return Instr; }
134
135   /// \see DiagnosticInfo::print.
136   void print(DiagnosticPrinter &DP) const override;
137
138   /// Hand rolled RTTI.
139   static bool classof(const DiagnosticInfo *DI) {
140     return DI->getKind() == DK_InlineAsm;
141   }
142 };
143
144 /// Diagnostic information for stack size reporting.
145 /// This is basically a function and a size.
146 class DiagnosticInfoStackSize : public DiagnosticInfo {
147 private:
148   /// The function that is concerned by this stack size diagnostic.
149   const Function &Fn;
150   /// The computed stack size.
151   unsigned StackSize;
152
153 public:
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) {}
159
160   const Function &getFunction() const { return Fn; }
161   unsigned getStackSize() const { return StackSize; }
162
163   /// \see DiagnosticInfo::print.
164   void print(DiagnosticPrinter &DP) const override;
165
166   /// Hand rolled RTTI.
167   static bool classof(const DiagnosticInfo *DI) {
168     return DI->getKind() == DK_StackSize;
169   }
170 };
171
172 /// Diagnostic information for debug metadata version reporting.
173 /// This is basically a module and a version.
174 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
175 private:
176   /// The module that is concerned by this debug metadata version diagnostic.
177   const Module &M;
178   /// The actual metadata version.
179   unsigned MetadataVersion;
180
181 public:
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) {}
188
189   const Module &getModule() const { return M; }
190   unsigned getMetadataVersion() const { return MetadataVersion; }
191
192   /// \see DiagnosticInfo::print.
193   void print(DiagnosticPrinter &DP) const override;
194
195   /// Hand rolled RTTI.
196   static bool classof(const DiagnosticInfo *DI) {
197     return DI->getKind() == DK_DebugMetadataVersion;
198   }
199 };
200
201 /// Diagnostic information for the sample profiler.
202 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
203 public:
204   DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
205                               const Twine &Msg,
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) {}
217
218   /// \see DiagnosticInfo::print.
219   void print(DiagnosticPrinter &DP) const override;
220
221   /// Hand rolled RTTI.
222   static bool classof(const DiagnosticInfo *DI) {
223     return DI->getKind() == DK_SampleProfile;
224   }
225
226   const char *getFileName() const { return FileName; }
227   unsigned getLineNum() const { return LineNum; }
228   const Twine &getMsg() const { return Msg; }
229
230 private:
231   /// Name of the input file associated with this diagnostic.
232   const char *FileName;
233
234   /// Line number where the diagnostic occurred. If 0, no line number will
235   /// be emitted in the message.
236   unsigned LineNum;
237
238   /// Message to report.
239   const Twine &Msg;
240 };
241
242 /// Diagnostic information for optimization remarks.
243 class DiagnosticInfoOptimizationRemark : public DiagnosticInfo {
244 public:
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) {}
257
258   /// \see DiagnosticInfo::print.
259   void print(DiagnosticPrinter &DP) const override;
260
261   /// Hand rolled RTTI.
262   static bool classof(const DiagnosticInfo *DI) {
263     return DI->getKind() == DK_OptimizationRemark;
264   }
265
266   /// Return true if location information is available for this diagnostic.
267   bool isLocationAvailable() const;
268
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;
273
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;
277
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; }
282
283 private:
284   /// Name of the pass that triggers this report. If this matches the
285   /// regular expression given in -Rpass=regexp, then the remark will
286   /// be emitted.
287   const char *PassName;
288
289   /// Function where this diagnostic is triggered.
290   const Function &Fn;
291
292   /// Debug location where this diagnostic is triggered.
293   DebugLoc DLoc;
294
295   /// Message to report.
296   const Twine &Msg;
297 };
298
299 // Create wrappers for C Binding types (see CBindingWrapping.h).
300 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DiagnosticInfo, LLVMDiagnosticInfoRef)
301
302 } // End namespace llvm
303
304 #endif