Add 'remark' diagnostic type in LLVM
[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_FirstPluginKind
47 };
48
49 /// \brief Get the next available kind ID for a plugin diagnostic.
50 /// Each time this function is called, it returns a different number.
51 /// Therefore, a plugin that wants to "identify" its own classes
52 /// with a dynamic identifier, just have to use this method to get a new ID
53 /// and assign it to each of its classes.
54 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
55 /// Thus, the plugin identifiers will not conflict with the
56 /// DiagnosticKind values.
57 int getNextAvailablePluginDiagnosticKind();
58
59 /// \brief This is the base abstract class for diagnostic reporting in
60 /// the backend.
61 /// The print method must be overloaded by the subclasses to print a
62 /// user-friendly message in the client of the backend (let us call it a
63 /// frontend).
64 class DiagnosticInfo {
65 private:
66   /// Kind defines the kind of report this is about.
67   const /* DiagnosticKind */ int Kind;
68   /// Severity gives the severity of the diagnostic.
69   const DiagnosticSeverity Severity;
70
71 public:
72   DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
73       : Kind(Kind), Severity(Severity) {}
74
75   virtual ~DiagnosticInfo() {}
76
77   /* DiagnosticKind */ int getKind() const { return Kind; }
78   DiagnosticSeverity getSeverity() const { return Severity; }
79
80   /// Print using the given \p DP a user-friendly message.
81   /// This is the default message that will be printed to the user.
82   /// It is used when the frontend does not directly take advantage
83   /// of the information contained in fields of the subclasses.
84   /// The printed message must not end with '.' nor start with a severity
85   /// keyword.
86   virtual void print(DiagnosticPrinter &DP) const = 0;
87 };
88
89 /// Diagnostic information for inline asm reporting.
90 /// This is basically a message and an optional location.
91 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
92 private:
93   /// Optional line information. 0 if not set.
94   unsigned LocCookie;
95   /// Message to be reported.
96   const Twine &MsgStr;
97   /// Optional origin of the problem.
98   const Instruction *Instr;
99
100 public:
101   /// \p MsgStr is the message to be reported to the frontend.
102   /// This class does not copy \p MsgStr, therefore the reference must be valid
103   /// for the whole life time of the Diagnostic.
104   DiagnosticInfoInlineAsm(const Twine &MsgStr,
105                           DiagnosticSeverity Severity = DS_Error)
106       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
107         Instr(NULL) {}
108
109   /// \p LocCookie if non-zero gives the line number for this report.
110   /// \p MsgStr gives the message.
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(unsigned LocCookie, const Twine &MsgStr,
114                           DiagnosticSeverity Severity = DS_Error)
115       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
116         MsgStr(MsgStr), Instr(NULL) {}
117
118   /// \p Instr gives the original instruction that triggered the diagnostic.
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   /// Same for \p I.
123   DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
124                           DiagnosticSeverity Severity = DS_Error);
125
126   unsigned getLocCookie() const { return LocCookie; }
127   const Twine &getMsgStr() const { return MsgStr; }
128   const Instruction *getInstruction() const { return Instr; }
129
130   /// \see DiagnosticInfo::print.
131   virtual void print(DiagnosticPrinter &DP) const;
132
133   /// Hand rolled RTTI.
134   static bool classof(const DiagnosticInfo *DI) {
135     return DI->getKind() == DK_InlineAsm;
136   }
137 };
138
139 /// Diagnostic information for stack size reporting.
140 /// This is basically a function and a size.
141 class DiagnosticInfoStackSize : public DiagnosticInfo {
142 private:
143   /// The function that is concerned by this stack size diagnostic.
144   const Function &Fn;
145   /// The computed stack size.
146   unsigned StackSize;
147
148 public:
149   /// \p The function that is concerned by this stack size diagnostic.
150   /// \p The computed stack size.
151   DiagnosticInfoStackSize(const Function &Fn, unsigned StackSize,
152                           DiagnosticSeverity Severity = DS_Warning)
153       : DiagnosticInfo(DK_StackSize, Severity), Fn(Fn), StackSize(StackSize) {}
154
155   const Function &getFunction() const { return Fn; }
156   unsigned getStackSize() const { return StackSize; }
157
158   /// \see DiagnosticInfo::print.
159   virtual void print(DiagnosticPrinter &DP) const;
160
161   /// Hand rolled RTTI.
162   static bool classof(const DiagnosticInfo *DI) {
163     return DI->getKind() == DK_StackSize;
164   }
165 };
166
167 /// Diagnostic information for debug metadata version reporting.
168 /// This is basically a module and a version.
169 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
170 private:
171   /// The module that is concerned by this debug metadata version diagnostic.
172   const Module &M;
173   /// The actual metadata version.
174   unsigned MetadataVersion;
175
176 public:
177   /// \p The module that is concerned by this debug metadata version diagnostic.
178   /// \p The actual metadata version.
179   DiagnosticInfoDebugMetadataVersion(const Module &M, unsigned MetadataVersion,
180                           DiagnosticSeverity Severity = DS_Warning)
181       : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M),
182         MetadataVersion(MetadataVersion) {}
183
184   const Module &getModule() const { return M; }
185   unsigned getMetadataVersion() const { return MetadataVersion; }
186
187   /// \see DiagnosticInfo::print.
188   virtual void print(DiagnosticPrinter &DP) const;
189
190   /// Hand rolled RTTI.
191   static bool classof(const DiagnosticInfo *DI) {
192     return DI->getKind() == DK_DebugMetadataVersion;
193   }
194 };
195
196 } // End namespace llvm
197
198 #endif