1 //===- llvm/Support/DiagnosticInfo.h - Diagnostic Declaration ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the different classes involved in low level diagnostics.
12 // Diagnostics reporting is still done as part of the LLVMContext.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_DIAGNOSTICINFO_H
16 #define LLVM_SUPPORT_DIAGNOSTICINFO_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/Support/Casting.h"
23 // Forward declarations.
24 class DiagnosticPrinter;
30 /// \brief Defines the different supported severity of a diagnostic.
31 enum DiagnosticSeverity {
35 // A note attaches additional information to one of the previous diagnostic
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.
45 DK_DebugMetadataVersion,
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();
60 /// \brief This is the base abstract class for diagnostic reporting in
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
65 class DiagnosticInfo {
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;
73 DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
74 : Kind(Kind), Severity(Severity) {}
76 virtual ~DiagnosticInfo() {}
78 /* DiagnosticKind */ int getKind() const { return Kind; }
79 DiagnosticSeverity getSeverity() const { return Severity; }
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
87 virtual void print(DiagnosticPrinter &DP) const = 0;
90 /// Diagnostic information for inline asm reporting.
91 /// This is basically a message and an optional location.
92 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
94 /// Optional line information. 0 if not set.
96 /// Message to be reported.
98 /// Optional origin of the problem.
99 const Instruction *Instr;
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),
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) {}
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.
124 DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
125 DiagnosticSeverity Severity = DS_Error);
127 unsigned getLocCookie() const { return LocCookie; }
128 const Twine &getMsgStr() const { return MsgStr; }
129 const Instruction *getInstruction() const { return Instr; }
131 /// \see DiagnosticInfo::print.
132 void print(DiagnosticPrinter &DP) const override;
134 /// Hand rolled RTTI.
135 static bool classof(const DiagnosticInfo *DI) {
136 return DI->getKind() == DK_InlineAsm;
140 /// Diagnostic information for stack size reporting.
141 /// This is basically a function and a size.
142 class DiagnosticInfoStackSize : public DiagnosticInfo {
144 /// The function that is concerned by this stack size diagnostic.
146 /// The computed stack size.
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) {}
156 const Function &getFunction() const { return Fn; }
157 unsigned getStackSize() const { return StackSize; }
159 /// \see DiagnosticInfo::print.
160 void print(DiagnosticPrinter &DP) const override;
162 /// Hand rolled RTTI.
163 static bool classof(const DiagnosticInfo *DI) {
164 return DI->getKind() == DK_StackSize;
168 /// Diagnostic information for debug metadata version reporting.
169 /// This is basically a module and a version.
170 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
172 /// The module that is concerned by this debug metadata version diagnostic.
174 /// The actual metadata version.
175 unsigned MetadataVersion;
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) {}
185 const Module &getModule() const { return M; }
186 unsigned getMetadataVersion() const { return MetadataVersion; }
188 /// \see DiagnosticInfo::print.
189 void print(DiagnosticPrinter &DP) const override;
191 /// Hand rolled RTTI.
192 static bool classof(const DiagnosticInfo *DI) {
193 return DI->getKind() == DK_DebugMetadataVersion;
197 /// Diagnostic information for the sample profiler.
198 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
200 DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
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) {}
214 /// \see DiagnosticInfo::print.
215 void print(DiagnosticPrinter &DP) const override;
217 /// Hand rolled RTTI.
218 static bool classof(const DiagnosticInfo *DI) {
219 return DI->getKind() == DK_SampleProfile;
222 const char *getFileName() const { return FileName; }
223 unsigned getLineNum() const { return LineNum; }
224 const Twine &getMsg() const { return Msg; }
227 /// Name of the input file associated with this diagnostic.
228 const char *FileName;
230 /// Line number where the diagnostic occured. If 0, no line number will
231 /// be emitted in the message.
234 /// Message to report.
238 } // End namespace llvm