Satiate the sanitizer build bot
[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_IR_DIAGNOSTICINFO_H
16 #define LLVM_IR_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 LLVMContextImpl;
30 class Twine;
31 class Value;
32 class DebugLoc;
33
34 /// \brief Defines the different supported severity of a diagnostic.
35 enum DiagnosticSeverity {
36   DS_Error,
37   DS_Warning,
38   DS_Remark,
39   // A note attaches additional information to one of the previous diagnostic
40   // types.
41   DS_Note
42 };
43
44 /// \brief Defines the different supported kind of a diagnostic.
45 /// This enum should be extended with a new ID for each added concrete subclass.
46 enum DiagnosticKind {
47   DK_InlineAsm,
48   DK_StackSize,
49   DK_DebugMetadataVersion,
50   DK_SampleProfile,
51   DK_OptimizationRemark,
52   DK_OptimizationRemarkMissed,
53   DK_OptimizationRemarkAnalysis,
54   DK_OptimizationFailure,
55   DK_FirstPluginKind
56 };
57
58 /// \brief Get the next available kind ID for a plugin diagnostic.
59 /// Each time this function is called, it returns a different number.
60 /// Therefore, a plugin that wants to "identify" its own classes
61 /// with a dynamic identifier, just have to use this method to get a new ID
62 /// and assign it to each of its classes.
63 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
64 /// Thus, the plugin identifiers will not conflict with the
65 /// DiagnosticKind values.
66 int getNextAvailablePluginDiagnosticKind();
67
68 /// \brief This is the base abstract class for diagnostic reporting in
69 /// the backend.
70 /// The print method must be overloaded by the subclasses to print a
71 /// user-friendly message in the client of the backend (let us call it a
72 /// frontend).
73 class DiagnosticInfo {
74 private:
75   /// Kind defines the kind of report this is about.
76   const /* DiagnosticKind */ int Kind;
77   /// Severity gives the severity of the diagnostic.
78   const DiagnosticSeverity Severity;
79
80 public:
81   DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
82       : Kind(Kind), Severity(Severity) {}
83
84   virtual ~DiagnosticInfo() {}
85
86   /* DiagnosticKind */ int getKind() const { return Kind; }
87   DiagnosticSeverity getSeverity() const { return Severity; }
88
89   /// Print using the given \p DP a user-friendly message.
90   /// This is the default message that will be printed to the user.
91   /// It is used when the frontend does not directly take advantage
92   /// of the information contained in fields of the subclasses.
93   /// The printed message must not end with '.' nor start with a severity
94   /// keyword.
95   virtual void print(DiagnosticPrinter &DP) const = 0;
96 };
97
98 /// Diagnostic information for inline asm reporting.
99 /// This is basically a message and an optional location.
100 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
101 private:
102   /// Optional line information. 0 if not set.
103   unsigned LocCookie;
104   /// Message to be reported.
105   const Twine &MsgStr;
106   /// Optional origin of the problem.
107   const Instruction *Instr;
108
109 public:
110   /// \p MsgStr is the message to be reported to the frontend.
111   /// This class does not copy \p MsgStr, therefore the reference must be valid
112   /// for the whole life time of the Diagnostic.
113   DiagnosticInfoInlineAsm(const Twine &MsgStr,
114                           DiagnosticSeverity Severity = DS_Error)
115       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
116         Instr(nullptr) {}
117
118   /// \p LocCookie if non-zero gives the line number for this report.
119   /// \p MsgStr gives the message.
120   /// This class does not copy \p MsgStr, therefore the reference must be valid
121   /// for the whole life time of the Diagnostic.
122   DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
123                           DiagnosticSeverity Severity = DS_Error)
124       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
125         MsgStr(MsgStr), Instr(nullptr) {}
126
127   /// \p Instr gives the original instruction that triggered the diagnostic.
128   /// \p MsgStr gives the message.
129   /// This class does not copy \p MsgStr, therefore the reference must be valid
130   /// for the whole life time of the Diagnostic.
131   /// Same for \p I.
132   DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
133                           DiagnosticSeverity Severity = DS_Error);
134
135   unsigned getLocCookie() const { return LocCookie; }
136   const Twine &getMsgStr() const { return MsgStr; }
137   const Instruction *getInstruction() const { return Instr; }
138
139   /// \see DiagnosticInfo::print.
140   void print(DiagnosticPrinter &DP) const override;
141
142   static bool classof(const DiagnosticInfo *DI) {
143     return DI->getKind() == DK_InlineAsm;
144   }
145 };
146
147 /// Diagnostic information for stack size reporting.
148 /// This is basically a function and a size.
149 class DiagnosticInfoStackSize : public DiagnosticInfo {
150 private:
151   /// The function that is concerned by this stack size diagnostic.
152   const Function &Fn;
153   /// The computed stack size.
154   unsigned StackSize;
155
156 public:
157   /// \p The function that is concerned by this stack size diagnostic.
158   /// \p The computed stack size.
159   DiagnosticInfoStackSize(const Function &Fn, unsigned StackSize,
160                           DiagnosticSeverity Severity = DS_Warning)
161       : DiagnosticInfo(DK_StackSize, Severity), Fn(Fn), StackSize(StackSize) {}
162
163   const Function &getFunction() const { return Fn; }
164   unsigned getStackSize() const { return StackSize; }
165
166   /// \see DiagnosticInfo::print.
167   void print(DiagnosticPrinter &DP) const override;
168
169   static bool classof(const DiagnosticInfo *DI) {
170     return DI->getKind() == DK_StackSize;
171   }
172 };
173
174 /// Diagnostic information for debug metadata version reporting.
175 /// This is basically a module and a version.
176 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
177 private:
178   /// The module that is concerned by this debug metadata version diagnostic.
179   const Module &M;
180   /// The actual metadata version.
181   unsigned MetadataVersion;
182
183 public:
184   /// \p The module that is concerned by this debug metadata version diagnostic.
185   /// \p The actual metadata version.
186   DiagnosticInfoDebugMetadataVersion(const Module &M, unsigned MetadataVersion,
187                           DiagnosticSeverity Severity = DS_Warning)
188       : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M),
189         MetadataVersion(MetadataVersion) {}
190
191   const Module &getModule() const { return M; }
192   unsigned getMetadataVersion() const { return MetadataVersion; }
193
194   /// \see DiagnosticInfo::print.
195   void print(DiagnosticPrinter &DP) const override;
196
197   static bool classof(const DiagnosticInfo *DI) {
198     return DI->getKind() == DK_DebugMetadataVersion;
199   }
200 };
201
202 /// Diagnostic information for the sample profiler.
203 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
204 public:
205   DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
206                               const Twine &Msg,
207                               DiagnosticSeverity Severity = DS_Error)
208       : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
209         LineNum(LineNum), Msg(Msg) {}
210   DiagnosticInfoSampleProfile(const char *FileName, const Twine &Msg,
211                               DiagnosticSeverity Severity = DS_Error)
212       : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
213         LineNum(0), Msg(Msg) {}
214   DiagnosticInfoSampleProfile(const Twine &Msg,
215                               DiagnosticSeverity Severity = DS_Error)
216       : DiagnosticInfo(DK_SampleProfile, Severity), FileName(nullptr),
217         LineNum(0), Msg(Msg) {}
218
219   /// \see DiagnosticInfo::print.
220   void print(DiagnosticPrinter &DP) const override;
221
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 /// Common features for diagnostics dealing with optimization remarks.
243 class DiagnosticInfoOptimizationBase : public DiagnosticInfo {
244 public:
245   /// \p PassName is the name of the pass emitting this diagnostic.
246   /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
247   /// the location information to use in the diagnostic. If line table
248   /// information is available, the diagnostic will include the source code
249   /// location. \p Msg is the message to show. Note that this class does not
250   /// copy this message, so this reference must be valid for the whole life time
251   /// of the diagnostic.
252   DiagnosticInfoOptimizationBase(enum DiagnosticKind Kind,
253                                  enum DiagnosticSeverity Severity,
254                                  const char *PassName, const Function &Fn,
255                                  const DebugLoc &DLoc, const Twine &Msg)
256       : DiagnosticInfo(Kind, Severity), PassName(PassName), Fn(Fn), DLoc(DLoc),
257         Msg(Msg) {}
258
259   /// \see DiagnosticInfo::print.
260   void print(DiagnosticPrinter &DP) const override;
261
262   static bool classof(const DiagnosticInfo *DI) {
263     return DI->getKind() == DK_OptimizationRemark;
264   }
265
266   /// Return true if this optimization remark is enabled by one of
267   /// of the LLVM command line flags (-pass-remarks, -pass-remarks-missed,
268   /// or -pass-remarks-analysis). Note that this only handles the LLVM
269   /// flags. We cannot access Clang flags from here (they are handled
270   /// in BackendConsumer::OptimizationRemarkHandler).
271   virtual bool isEnabled() const = 0;
272
273   /// Return true if location information is available for this diagnostic.
274   bool isLocationAvailable() const;
275
276   /// Return a string with the location information for this diagnostic
277   /// in the format "file:line:col". If location information is not available,
278   /// it returns "<unknown>:0:0".
279   const std::string getLocationStr() const;
280
281   /// Return location information for this diagnostic in three parts:
282   /// the source file name, line number and column.
283   void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
284
285   const char *getPassName() const { return PassName; }
286   const Function &getFunction() const { return Fn; }
287   const DebugLoc &getDebugLoc() const { return DLoc; }
288   const Twine &getMsg() const { return Msg; }
289
290 private:
291   /// Name of the pass that triggers this report. If this matches the
292   /// regular expression given in -Rpass=regexp, then the remark will
293   /// be emitted.
294   const char *PassName;
295
296   /// Function where this diagnostic is triggered.
297   const Function &Fn;
298
299   /// Debug location where this diagnostic is triggered.
300   DebugLoc DLoc;
301
302   /// Message to report.
303   const Twine &Msg;
304 };
305
306 /// Diagnostic information for applied optimization remarks.
307 class DiagnosticInfoOptimizationRemark : public DiagnosticInfoOptimizationBase {
308 public:
309   /// \p PassName is the name of the pass emitting this diagnostic. If
310   /// this name matches the regular expression given in -Rpass=, then the
311   /// diagnostic will be emitted. \p Fn is the function where the diagnostic
312   /// is being emitted. \p DLoc is the location information to use in the
313   /// diagnostic. If line table information is available, the diagnostic
314   /// will include the source code location. \p Msg is the message to show.
315   /// Note that this class does not copy this message, so this reference
316   /// must be valid for the whole life time of the diagnostic.
317   DiagnosticInfoOptimizationRemark(const char *PassName, const Function &Fn,
318                                    const DebugLoc &DLoc, const Twine &Msg)
319       : DiagnosticInfoOptimizationBase(DK_OptimizationRemark, DS_Remark,
320                                        PassName, Fn, DLoc, Msg) {}
321
322   static bool classof(const DiagnosticInfo *DI) {
323     return DI->getKind() == DK_OptimizationRemark;
324   }
325
326   /// \see DiagnosticInfoOptimizationBase::isEnabled.
327   virtual bool isEnabled() const override;
328 };
329
330 /// Diagnostic information for missed-optimization remarks.
331 class DiagnosticInfoOptimizationRemarkMissed
332     : public DiagnosticInfoOptimizationBase {
333 public:
334   /// \p PassName is the name of the pass emitting this diagnostic. If
335   /// this name matches the regular expression given in -Rpass-missed=, then the
336   /// diagnostic will be emitted. \p Fn is the function where the diagnostic
337   /// is being emitted. \p DLoc is the location information to use in the
338   /// diagnostic. If line table information is available, the diagnostic
339   /// will include the source code location. \p Msg is the message to show.
340   /// Note that this class does not copy this message, so this reference
341   /// must be valid for the whole life time of the diagnostic.
342   DiagnosticInfoOptimizationRemarkMissed(const char *PassName,
343                                          const Function &Fn,
344                                          const DebugLoc &DLoc, const Twine &Msg)
345       : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkMissed, DS_Remark,
346                                        PassName, Fn, DLoc, Msg) {}
347
348   static bool classof(const DiagnosticInfo *DI) {
349     return DI->getKind() == DK_OptimizationRemarkMissed;
350   }
351
352   /// \see DiagnosticInfoOptimizationBase::isEnabled.
353   virtual bool isEnabled() const override;
354 };
355
356 /// Diagnostic information for optimization analysis remarks.
357 class DiagnosticInfoOptimizationRemarkAnalysis
358     : public DiagnosticInfoOptimizationBase {
359 public:
360   /// \p PassName is the name of the pass emitting this diagnostic. If
361   /// this name matches the regular expression given in -Rpass-analysis=, then
362   /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
363   /// is being emitted. \p DLoc is the location information to use in the
364   /// diagnostic. If line table information is available, the diagnostic will
365   /// include the source code location. \p Msg is the message to show. Note that
366   /// this class does not copy this message, so this reference must be valid for
367   /// the whole life time of the diagnostic.
368   DiagnosticInfoOptimizationRemarkAnalysis(const char *PassName,
369                                            const Function &Fn,
370                                            const DebugLoc &DLoc,
371                                            const Twine &Msg)
372       : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkAnalysis, DS_Remark,
373                                        PassName, Fn, DLoc, Msg) {}
374
375   static bool classof(const DiagnosticInfo *DI) {
376     return DI->getKind() == DK_OptimizationRemarkAnalysis;
377   }
378
379   /// \see DiagnosticInfoOptimizationBase::isEnabled.
380   virtual bool isEnabled() const override;
381 };
382
383 // Create wrappers for C Binding types (see CBindingWrapping.h).
384 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DiagnosticInfo, LLVMDiagnosticInfoRef)
385
386 /// Emit an optimization-applied message. \p PassName is the name of the pass
387 /// emitting the message. If -Rpass= is given and \p PassName matches the
388 /// regular expression in -Rpass, then the remark will be emitted. \p Fn is
389 /// the function triggering the remark, \p DLoc is the debug location where
390 /// the diagnostic is generated. \p Msg is the message string to use.
391 void emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
392                             const Function &Fn, const DebugLoc &DLoc,
393                             const Twine &Msg);
394
395 /// Emit an optimization-missed message. \p PassName is the name of the
396 /// pass emitting the message. If -Rpass-missed= is given and \p PassName
397 /// matches the regular expression in -Rpass, then the remark will be
398 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the
399 /// debug location where the diagnostic is generated. \p Msg is the
400 /// message string to use.
401 void emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
402                                   const Function &Fn, const DebugLoc &DLoc,
403                                   const Twine &Msg);
404
405 /// Emit an optimization analysis remark message. \p PassName is the name of
406 /// the pass emitting the message. If -Rpass-analysis= is given and \p
407 /// PassName matches the regular expression in -Rpass, then the remark will be
408 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the debug
409 /// location where the diagnostic is generated. \p Msg is the message string
410 /// to use.
411 void emitOptimizationRemarkAnalysis(LLVMContext &Ctx, const char *PassName,
412                                     const Function &Fn, const DebugLoc &DLoc,
413                                     const Twine &Msg);
414
415 /// Diagnostic information for optimization failures.
416 class DiagnosticInfoOptimizationFailure
417     : public DiagnosticInfoOptimizationBase {
418 public:
419   /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
420   /// the location information to use in the diagnostic. If line table
421   /// information is available, the diagnostic will include the source code
422   /// location. \p Msg is the message to show. Note that this class does not
423   /// copy this message, so this reference must be valid for the whole life time
424   /// of the diagnostic.
425   DiagnosticInfoOptimizationFailure(const Function &Fn, const DebugLoc &DLoc,
426                                     const Twine &Msg)
427       : DiagnosticInfoOptimizationBase(DK_OptimizationFailure, DS_Warning,
428                                        nullptr, Fn, DLoc, Msg) {}
429
430   static bool classof(const DiagnosticInfo *DI) {
431     return DI->getKind() == DK_OptimizationFailure;
432   }
433
434   /// \see DiagnosticInfoOptimizationBase::isEnabled.
435   virtual bool isEnabled() const override;
436 };
437
438 /// Emit a warning when loop vectorization is specified but fails. \p Fn is the
439 /// function triggering the warning, \p DLoc is the debug location where the
440 /// diagnostic is generated. \p Msg is the message string to use.
441 void emitLoopVectorizeWarning(LLVMContext &Ctx, const Function &Fn,
442                               const DebugLoc &DLoc, const Twine &Msg);
443
444 /// Emit a warning when loop interleaving is specified but fails. \p Fn is the
445 /// function triggering the warning, \p DLoc is the debug location where the
446 /// diagnostic is generated. \p Msg is the message string to use.
447 void emitLoopInterleaveWarning(LLVMContext &Ctx, const Function &Fn,
448                                const DebugLoc &DLoc, const Twine &Msg);
449
450 } // End namespace llvm
451
452 #endif