DebugInfo: Support namespace aliases as DW_TAG_imported_declaration instead of DW_TAG...
[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
30 /// \brief Defines the different supported severity of a diagnostic.
31 enum DiagnosticSeverity {
32   DS_Error,
33   DS_Warning,
34   DS_Remark,
35   // A note attaches additional information to one of the previous diagnostic
36   // types.
37   DS_Note
38 };
39
40 /// \brief Defines the different supported kind of a diagnostic.
41 /// This enum should be extended with a new ID for each added concrete subclass.
42 enum DiagnosticKind {
43   DK_InlineAsm,
44   DK_StackSize,
45   DK_DebugMetadataVersion,
46   DK_SampleProfile,
47   DK_FirstPluginKind
48 };
49
50 /// \brief Get the next available kind ID for a plugin diagnostic.
51 /// Each time this function is called, it returns a different number.
52 /// Therefore, a plugin that wants to "identify" its own classes
53 /// with a dynamic identifier, just have to use this method to get a new ID
54 /// and assign it to each of its classes.
55 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
56 /// Thus, the plugin identifiers will not conflict with the
57 /// DiagnosticKind values.
58 int getNextAvailablePluginDiagnosticKind();
59
60 /// \brief This is the base abstract class for diagnostic reporting in
61 /// the backend.
62 /// The print method must be overloaded by the subclasses to print a
63 /// user-friendly message in the client of the backend (let us call it a
64 /// frontend).
65 class DiagnosticInfo {
66 private:
67   /// Kind defines the kind of report this is about.
68   const /* DiagnosticKind */ int Kind;
69   /// Severity gives the severity of the diagnostic.
70   const DiagnosticSeverity Severity;
71
72 public:
73   DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
74       : Kind(Kind), Severity(Severity) {}
75
76   virtual ~DiagnosticInfo() {}
77
78   /* DiagnosticKind */ int getKind() const { return Kind; }
79   DiagnosticSeverity getSeverity() const { return Severity; }
80
81   /// Print using the given \p DP a user-friendly message.
82   /// This is the default message that will be printed to the user.
83   /// It is used when the frontend does not directly take advantage
84   /// of the information contained in fields of the subclasses.
85   /// The printed message must not end with '.' nor start with a severity
86   /// keyword.
87   virtual void print(DiagnosticPrinter &DP) const = 0;
88 };
89
90 /// Diagnostic information for inline asm reporting.
91 /// This is basically a message and an optional location.
92 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
93 private:
94   /// Optional line information. 0 if not set.
95   unsigned LocCookie;
96   /// Message to be reported.
97   const Twine &MsgStr;
98   /// Optional origin of the problem.
99   const Instruction *Instr;
100
101 public:
102   /// \p MsgStr is the message to be reported to the frontend.
103   /// This class does not copy \p MsgStr, therefore the reference must be valid
104   /// for the whole life time of the Diagnostic.
105   DiagnosticInfoInlineAsm(const Twine &MsgStr,
106                           DiagnosticSeverity Severity = DS_Error)
107       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
108         Instr(NULL) {}
109
110   /// \p LocCookie if non-zero gives the line number for this report.
111   /// \p MsgStr gives the message.
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(unsigned LocCookie, const Twine &MsgStr,
115                           DiagnosticSeverity Severity = DS_Error)
116       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
117         MsgStr(MsgStr), Instr(NULL) {}
118
119   /// \p Instr gives the original instruction that triggered the diagnostic.
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   /// Same for \p I.
124   DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
125                           DiagnosticSeverity Severity = DS_Error);
126
127   unsigned getLocCookie() const { return LocCookie; }
128   const Twine &getMsgStr() const { return MsgStr; }
129   const Instruction *getInstruction() const { return Instr; }
130
131   /// \see DiagnosticInfo::print.
132   void print(DiagnosticPrinter &DP) const override;
133
134   /// Hand rolled RTTI.
135   static bool classof(const DiagnosticInfo *DI) {
136     return DI->getKind() == DK_InlineAsm;
137   }
138 };
139
140 /// Diagnostic information for stack size reporting.
141 /// This is basically a function and a size.
142 class DiagnosticInfoStackSize : public DiagnosticInfo {
143 private:
144   /// The function that is concerned by this stack size diagnostic.
145   const Function &Fn;
146   /// The computed stack size.
147   unsigned StackSize;
148
149 public:
150   /// \p The function that is concerned by this stack size diagnostic.
151   /// \p The computed stack size.
152   DiagnosticInfoStackSize(const Function &Fn, unsigned StackSize,
153                           DiagnosticSeverity Severity = DS_Warning)
154       : DiagnosticInfo(DK_StackSize, Severity), Fn(Fn), StackSize(StackSize) {}
155
156   const Function &getFunction() const { return Fn; }
157   unsigned getStackSize() const { return StackSize; }
158
159   /// \see DiagnosticInfo::print.
160   void print(DiagnosticPrinter &DP) const override;
161
162   /// Hand rolled RTTI.
163   static bool classof(const DiagnosticInfo *DI) {
164     return DI->getKind() == DK_StackSize;
165   }
166 };
167
168 /// Diagnostic information for debug metadata version reporting.
169 /// This is basically a module and a version.
170 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
171 private:
172   /// The module that is concerned by this debug metadata version diagnostic.
173   const Module &M;
174   /// The actual metadata version.
175   unsigned MetadataVersion;
176
177 public:
178   /// \p The module that is concerned by this debug metadata version diagnostic.
179   /// \p The actual metadata version.
180   DiagnosticInfoDebugMetadataVersion(const Module &M, unsigned MetadataVersion,
181                           DiagnosticSeverity Severity = DS_Warning)
182       : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M),
183         MetadataVersion(MetadataVersion) {}
184
185   const Module &getModule() const { return M; }
186   unsigned getMetadataVersion() const { return MetadataVersion; }
187
188   /// \see DiagnosticInfo::print.
189   void print(DiagnosticPrinter &DP) const override;
190
191   /// Hand rolled RTTI.
192   static bool classof(const DiagnosticInfo *DI) {
193     return DI->getKind() == DK_DebugMetadataVersion;
194   }
195 };
196
197 /// Diagnostic information for the sample profiler.
198 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
199 public:
200   DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
201                               const Twine &Msg,
202                               DiagnosticSeverity Severity = DS_Error)
203       : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
204         LineNum(LineNum), Msg(Msg) {}
205   DiagnosticInfoSampleProfile(const char *FileName, const Twine &Msg,
206                               DiagnosticSeverity Severity = DS_Error)
207       : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
208         LineNum(0), Msg(Msg) {}
209   DiagnosticInfoSampleProfile(const Twine &Msg,
210                               DiagnosticSeverity Severity = DS_Error)
211       : DiagnosticInfo(DK_SampleProfile, Severity), FileName(NULL),
212         LineNum(0), Msg(Msg) {}
213
214   /// \see DiagnosticInfo::print.
215   void print(DiagnosticPrinter &DP) const override;
216
217   /// Hand rolled RTTI.
218   static bool classof(const DiagnosticInfo *DI) {
219     return DI->getKind() == DK_SampleProfile;
220   }
221
222   const char *getFileName() const { return FileName; }
223   unsigned getLineNum() const { return LineNum; }
224   const Twine &getMsg() const { return Msg; }
225
226 private:
227   /// Name of the input file associated with this diagnostic.
228   const char *FileName;
229
230   /// Line number where the diagnostic occured. If 0, no line number will
231   /// be emitted in the message.
232   unsigned LineNum;
233
234   /// Message to report.
235   const Twine &Msg;
236 };
237
238 } // End namespace llvm
239
240 #endif