OSDN Git Service

am 0d041145: am 19c6fbb3: Merge "Adds the ability to run the llvm test suite in-tree."
[android-x86/external-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 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_FirstPluginKind
55 };
56
57 /// \brief Get the next available kind ID for a plugin diagnostic.
58 /// Each time this function is called, it returns a different number.
59 /// Therefore, a plugin that wants to "identify" its own classes
60 /// with a dynamic identifier, just have to use this method to get a new ID
61 /// and assign it to each of its classes.
62 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
63 /// Thus, the plugin identifiers will not conflict with the
64 /// DiagnosticKind values.
65 int getNextAvailablePluginDiagnosticKind();
66
67 /// \brief This is the base abstract class for diagnostic reporting in
68 /// the backend.
69 /// The print method must be overloaded by the subclasses to print a
70 /// user-friendly message in the client of the backend (let us call it a
71 /// frontend).
72 class DiagnosticInfo {
73 private:
74   /// Kind defines the kind of report this is about.
75   const /* DiagnosticKind */ int Kind;
76   /// Severity gives the severity of the diagnostic.
77   const DiagnosticSeverity Severity;
78
79 public:
80   DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
81       : Kind(Kind), Severity(Severity) {}
82
83   virtual ~DiagnosticInfo() {}
84
85   /* DiagnosticKind */ int getKind() const { return Kind; }
86   DiagnosticSeverity getSeverity() const { return Severity; }
87
88   /// Print using the given \p DP a user-friendly message.
89   /// This is the default message that will be printed to the user.
90   /// It is used when the frontend does not directly take advantage
91   /// of the information contained in fields of the subclasses.
92   /// The printed message must not end with '.' nor start with a severity
93   /// keyword.
94   virtual void print(DiagnosticPrinter &DP) const = 0;
95 };
96
97 /// Diagnostic information for inline asm reporting.
98 /// This is basically a message and an optional location.
99 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
100 private:
101   /// Optional line information. 0 if not set.
102   unsigned LocCookie;
103   /// Message to be reported.
104   const Twine &MsgStr;
105   /// Optional origin of the problem.
106   const Instruction *Instr;
107
108 public:
109   /// \p MsgStr is the message to be reported to the frontend.
110   /// This class does not copy \p MsgStr, therefore the reference must be valid
111   /// for the whole life time of the Diagnostic.
112   DiagnosticInfoInlineAsm(const Twine &MsgStr,
113                           DiagnosticSeverity Severity = DS_Error)
114       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
115         Instr(nullptr) {}
116
117   /// \p LocCookie if non-zero gives the line number for this report.
118   /// \p MsgStr gives the message.
119   /// This class does not copy \p MsgStr, therefore the reference must be valid
120   /// for the whole life time of the Diagnostic.
121   DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
122                           DiagnosticSeverity Severity = DS_Error)
123       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
124         MsgStr(MsgStr), Instr(nullptr) {}
125
126   /// \p Instr gives the original instruction that triggered the diagnostic.
127   /// \p MsgStr gives the message.
128   /// This class does not copy \p MsgStr, therefore the reference must be valid
129   /// for the whole life time of the Diagnostic.
130   /// Same for \p I.
131   DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
132                           DiagnosticSeverity Severity = DS_Error);
133
134   unsigned getLocCookie() const { return LocCookie; }
135   const Twine &getMsgStr() const { return MsgStr; }
136   const Instruction *getInstruction() const { return Instr; }
137
138   /// \see DiagnosticInfo::print.
139   void print(DiagnosticPrinter &DP) const override;
140
141   /// Hand rolled RTTI.
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   /// Hand rolled RTTI.
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   /// Hand rolled RTTI.
199   static bool classof(const DiagnosticInfo *DI) {
200     return DI->getKind() == DK_DebugMetadataVersion;
201   }
202 };
203
204 /// Diagnostic information for the sample profiler.
205 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
206 public:
207   DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
208                               const Twine &Msg,
209                               DiagnosticSeverity Severity = DS_Error)
210       : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
211         LineNum(LineNum), Msg(Msg) {}
212   DiagnosticInfoSampleProfile(const char *FileName, const Twine &Msg,
213                               DiagnosticSeverity Severity = DS_Error)
214       : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
215         LineNum(0), Msg(Msg) {}
216   DiagnosticInfoSampleProfile(const Twine &Msg,
217                               DiagnosticSeverity Severity = DS_Error)
218       : DiagnosticInfo(DK_SampleProfile, Severity), FileName(nullptr),
219         LineNum(0), Msg(Msg) {}
220
221   /// \see DiagnosticInfo::print.
222   void print(DiagnosticPrinter &DP) const override;
223
224   /// Hand rolled RTTI.
225   static bool classof(const DiagnosticInfo *DI) {
226     return DI->getKind() == DK_SampleProfile;
227   }
228
229   const char *getFileName() const { return FileName; }
230   unsigned getLineNum() const { return LineNum; }
231   const Twine &getMsg() const { return Msg; }
232
233 private:
234   /// Name of the input file associated with this diagnostic.
235   const char *FileName;
236
237   /// Line number where the diagnostic occurred. If 0, no line number will
238   /// be emitted in the message.
239   unsigned LineNum;
240
241   /// Message to report.
242   const Twine &Msg;
243 };
244
245 /// Common features for diagnostics dealing with optimization remarks.
246 class DiagnosticInfoOptimizationRemarkBase : public DiagnosticInfo {
247 public:
248   /// \p PassName is the name of the pass emitting this diagnostic.
249   /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
250   /// the location information to use in the diagnostic. If line table
251   /// information is available, the diagnostic will include the source code
252   /// location. \p Msg is the message to show. Note that this class does not
253   /// copy this message, so this reference must be valid for the whole life time
254   /// of the diagnostic.
255   DiagnosticInfoOptimizationRemarkBase(enum DiagnosticKind Kind,
256                                        const char *PassName, const Function &Fn,
257                                        const DebugLoc &DLoc, const Twine &Msg)
258       : DiagnosticInfo(Kind, DS_Remark), PassName(PassName), Fn(Fn), DLoc(DLoc),
259         Msg(Msg) {}
260
261   /// \see DiagnosticInfo::print.
262   void print(DiagnosticPrinter &DP) const override;
263
264   /// Hand rolled RTTI.
265   static bool classof(const DiagnosticInfo *DI) {
266     return DI->getKind() == DK_OptimizationRemark;
267   }
268
269   /// Return true if this optimization remark is enabled by one of
270   /// of the LLVM command line flags (-pass-remarks, -pass-remarks-missed,
271   /// or -pass-remarks-analysis). Note that this only handles the LLVM
272   /// flags. We cannot access Clang flags from here (they are handled
273   /// in BackendConsumer::OptimizationRemarkHandler).
274   virtual bool isEnabled() const = 0;
275
276   /// Return true if location information is available for this diagnostic.
277   bool isLocationAvailable() const;
278
279   /// Return a string with the location information for this diagnostic
280   /// in the format "file:line:col". If location information is not available,
281   /// it returns "<unknown>:0:0".
282   const std::string getLocationStr() const;
283
284   /// Return location information for this diagnostic in three parts:
285   /// the source file name, line number and column.
286   void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
287
288   const char *getPassName() const { return PassName; }
289   const Function &getFunction() const { return Fn; }
290   const DebugLoc &getDebugLoc() const { return DLoc; }
291   const Twine &getMsg() const { return Msg; }
292
293 private:
294   /// Name of the pass that triggers this report. If this matches the
295   /// regular expression given in -Rpass=regexp, then the remark will
296   /// be emitted.
297   const char *PassName;
298
299   /// Function where this diagnostic is triggered.
300   const Function &Fn;
301
302   /// Debug location where this diagnostic is triggered.
303   DebugLoc DLoc;
304
305   /// Message to report.
306   const Twine &Msg;
307 };
308
309 /// Diagnostic information for applied optimization remarks.
310 class DiagnosticInfoOptimizationRemark
311     : public DiagnosticInfoOptimizationRemarkBase {
312 public:
313   /// \p PassName is the name of the pass emitting this diagnostic. If
314   /// this name matches the regular expression given in -Rpass=, then the
315   /// diagnostic will be emitted. \p Fn is the function where the diagnostic
316   /// is being emitted. \p DLoc is the location information to use in the
317   /// diagnostic. If line table information is available, the diagnostic
318   /// will include the source code location. \p Msg is the message to show.
319   /// Note that this class does not copy this message, so this reference
320   /// must be valid for the whole life time of the diagnostic.
321   DiagnosticInfoOptimizationRemark(const char *PassName, const Function &Fn,
322                                    const DebugLoc &DLoc, const Twine &Msg)
323       : DiagnosticInfoOptimizationRemarkBase(DK_OptimizationRemark, PassName,
324                                              Fn, DLoc, Msg) {}
325
326   /// Hand rolled RTTI
327   static bool classof(const DiagnosticInfo *DI) {
328     return DI->getKind() == DK_OptimizationRemark;
329   }
330
331   /// \see DiagnosticInfoOptimizationRemarkBase::isEnabled.
332   virtual bool isEnabled() const override;
333 };
334
335 /// Diagnostic information for missed-optimization remarks.
336 class DiagnosticInfoOptimizationRemarkMissed
337     : public DiagnosticInfoOptimizationRemarkBase {
338 public:
339   /// \p PassName is the name of the pass emitting this diagnostic. If
340   /// this name matches the regular expression given in -Rpass-missed=, then the
341   /// diagnostic will be emitted. \p Fn is the function where the diagnostic
342   /// is being emitted. \p DLoc is the location information to use in the
343   /// diagnostic. If line table information is available, the diagnostic
344   /// will include the source code location. \p Msg is the message to show.
345   /// Note that this class does not copy this message, so this reference
346   /// must be valid for the whole life time of the diagnostic.
347   DiagnosticInfoOptimizationRemarkMissed(const char *PassName,
348                                          const Function &Fn,
349                                          const DebugLoc &DLoc, const Twine &Msg)
350       : DiagnosticInfoOptimizationRemarkBase(DK_OptimizationRemarkMissed,
351                                              PassName, Fn, DLoc, Msg) {}
352
353   /// Hand rolled RTTI
354   static bool classof(const DiagnosticInfo *DI) {
355     return DI->getKind() == DK_OptimizationRemarkMissed;
356   }
357
358   /// \see DiagnosticInfoOptimizationRemarkBase::isEnabled.
359   virtual bool isEnabled() const override;
360 };
361
362 /// Diagnostic information for optimization analysis remarks.
363 class DiagnosticInfoOptimizationRemarkAnalysis
364     : public DiagnosticInfoOptimizationRemarkBase {
365 public:
366   /// \p PassName is the name of the pass emitting this diagnostic. If
367   /// this name matches the regular expression given in -Rpass-analysis=, then
368   /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
369   /// is being emitted. \p DLoc is the location information to use in the
370   /// diagnostic. If line table information is available, the diagnostic will
371   /// include the source code location. \p Msg is the message to show. Note that
372   /// this class does not copy this message, so this reference must be valid for
373   /// the whole life time of the diagnostic.
374   DiagnosticInfoOptimizationRemarkAnalysis(const char *PassName,
375                                            const Function &Fn,
376                                            const DebugLoc &DLoc,
377                                            const Twine &Msg)
378       : DiagnosticInfoOptimizationRemarkBase(DK_OptimizationRemarkAnalysis,
379                                              PassName, Fn, DLoc, Msg) {}
380
381   /// Hand rolled RTTI
382   static bool classof(const DiagnosticInfo *DI) {
383     return DI->getKind() == DK_OptimizationRemarkAnalysis;
384   }
385
386   /// \see DiagnosticInfoOptimizationRemarkBase::isEnabled.
387   virtual bool isEnabled() const override;
388 };
389
390 // Create wrappers for C Binding types (see CBindingWrapping.h).
391 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DiagnosticInfo, LLVMDiagnosticInfoRef)
392
393 /// Emit an optimization-applied message. \p PassName is the name of the pass
394 /// emitting the message. If -Rpass= is given and \p PassName matches the
395 /// regular expression in -Rpass, then the remark will be emitted. \p Fn is
396 /// the function triggering the remark, \p DLoc is the debug location where
397 /// the diagnostic is generated. \p Msg is the message string to use.
398 void emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
399                             const Function &Fn, const DebugLoc &DLoc,
400                             const Twine &Msg);
401
402 /// Emit an optimization-missed message. \p PassName is the name of the
403 /// pass emitting the message. If -Rpass-missed= is given and \p PassName
404 /// matches the regular expression in -Rpass, then the remark will be
405 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the
406 /// debug location where the diagnostic is generated. \p Msg is the
407 /// message string to use.
408 void emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
409                                   const Function &Fn, const DebugLoc &DLoc,
410                                   const Twine &Msg);
411
412 /// Emit an optimization analysis remark message. \p PassName is the name of
413 /// the pass emitting the message. If -Rpass-analysis= is given and \p
414 /// PassName matches the regular expression in -Rpass, then the remark will be
415 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the debug
416 /// location where the diagnostic is generated. \p Msg is the message string
417 /// to use.
418 void emitOptimizationRemarkAnalysis(LLVMContext &Ctx, const char *PassName,
419                                     const Function &Fn, const DebugLoc &DLoc,
420                                     const Twine &Msg);
421
422 } // End namespace llvm
423
424 #endif