Add warning capabilities in LLVM.
[oota-llvm.git] / include / llvm / Support / 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_Note
35 };
36
37 /// \brief Defines the different supported kind of a diagnostic.
38 /// This enum should be extended with a new ID for each added concrete subclass.
39 enum DiagnosticKind {
40   DK_InlineAsm,
41   DK_StackSize,
42   DK_FirstPluginKind
43 };
44
45 /// \brief Get the next available kind ID for a plugin diagnostic.
46 /// Each time this function is called, it returns a different number.
47 /// Therefore, a plugin that wants to "identify" its own classes
48 /// with a dynamic identifier, just have to use this method to get a new ID
49 /// and assign it to each of its classes.
50 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
51 /// Thus, the plugin identifiers will not conflict with the
52 /// DiagnosticKind values.
53 int getNextAvailablePluginDiagnosticKind();
54
55 /// \brief This is the base abstract class for diagnostic reporting in
56 /// the backend.
57 /// The print method must be overloaded by the subclasses to print a
58 /// user-friendly message in the client of the backend (let us call it a
59 /// frontend).
60 class DiagnosticInfo {
61 private:
62   /// Kind defines the kind of report this is about.
63   const /* DiagnosticKind */ int Kind;
64   /// Severity gives the severity of the diagnostic.
65   const DiagnosticSeverity Severity;
66
67 public:
68   DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
69       : Kind(Kind), Severity(Severity) {}
70
71   virtual ~DiagnosticInfo() {}
72
73   /* DiagnosticKind */ int getKind() const { return Kind; }
74   DiagnosticSeverity getSeverity() const { return Severity; }
75
76   /// Print using the given \p DP a user-friendly message.
77   /// This is the default message that will be printed to the user.
78   /// It is used when the frontend does not directly take advantage
79   /// of the information contained in fields of the subclasses.
80   /// The printed message must not end with '.' nor start with a severity
81   /// keyword.
82   virtual void print(DiagnosticPrinter &DP) const = 0;
83 };
84
85 /// Diagnostic information for inline asm reporting.
86 /// This is basically a message and an optional location.
87 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
88 private:
89   /// Optional line information. 0 if not set.
90   unsigned LocCookie;
91   /// Message to be reported.
92   const Twine &MsgStr;
93   /// Optional origin of the problem.
94   const Instruction *Instr;
95
96 public:
97   /// \p MsgStr is the message to be reported to the frontend.
98   /// This class does not copy \p MsgStr, therefore the reference must be valid
99   /// for the whole life time of the Diagnostic.
100   DiagnosticInfoInlineAsm(const Twine &MsgStr,
101                           DiagnosticSeverity Severity = DS_Error)
102       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
103         Instr(NULL) {}
104
105   /// \p LocCookie if non-zero gives the line number for this report.
106   /// \p MsgStr gives the message.
107   /// This class does not copy \p MsgStr, therefore the reference must be valid
108   /// for the whole life time of the Diagnostic.
109   DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
110                           DiagnosticSeverity Severity = DS_Error)
111       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
112         MsgStr(MsgStr), Instr(NULL) {}
113
114   /// \p Instr gives the original instruction that triggered the diagnostic.
115   /// \p MsgStr gives the message.
116   /// This class does not copy \p MsgStr, therefore the reference must be valid
117   /// for the whole life time of the Diagnostic.
118   /// Same for \p I.
119   DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
120                           DiagnosticSeverity Severity = DS_Error);
121
122   unsigned getLocCookie() const { return LocCookie; }
123   const Twine &getMsgStr() const { return MsgStr; }
124   const Instruction *getInstruction() const { return Instr; }
125
126   /// \see DiagnosticInfo::print.
127   virtual void print(DiagnosticPrinter &DP) const;
128
129   /// Hand rolled RTTI.
130   static bool classof(const DiagnosticInfo *DI) {
131     return DI->getKind() == DK_InlineAsm;
132   }
133 };
134
135 /// Diagnostic information for stack size reporting.
136 /// This is basically a function and a size.
137 class DiagnosticInfoStackSize : public DiagnosticInfo {
138 private:
139   /// The function that is concerned by this stack size diagnostic.
140   const Function &Fn;
141   /// The computed stack size.
142   unsigned StackSize;
143
144 public:
145   /// \p The function that is concerned by this stack size diagnostic.
146   /// \p The computed stack size.
147   DiagnosticInfoStackSize(const Function &Fn, unsigned StackSize,
148                           DiagnosticSeverity Severity = DS_Warning)
149       : DiagnosticInfo(DK_StackSize, Severity), Fn(Fn), StackSize(StackSize) {}
150
151   const Function &getFunction() const { return Fn; }
152   unsigned getStackSize() const { return StackSize; }
153
154   /// \see DiagnosticInfo::print.
155   virtual void print(DiagnosticPrinter &DP) const;
156
157   /// Hand rolled RTTI.
158   static bool classof(const DiagnosticInfo *DI) {
159     return DI->getKind() == DK_StackSize;
160   }
161 };
162
163 } // End namespace llvm
164
165 #endif