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