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,
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();
59 /// \brief This is the base abstract class for diagnostic reporting in
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
64 class DiagnosticInfo {
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;
72 DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
73 : Kind(Kind), Severity(Severity) {}
75 virtual ~DiagnosticInfo() {}
77 /* DiagnosticKind */ int getKind() const { return Kind; }
78 DiagnosticSeverity getSeverity() const { return Severity; }
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
86 virtual void print(DiagnosticPrinter &DP) const = 0;
89 /// Diagnostic information for inline asm reporting.
90 /// This is basically a message and an optional location.
91 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
93 /// Optional line information. 0 if not set.
95 /// Message to be reported.
97 /// Optional origin of the problem.
98 const Instruction *Instr;
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),
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) {}
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.
123 DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
124 DiagnosticSeverity Severity = DS_Error);
126 unsigned getLocCookie() const { return LocCookie; }
127 const Twine &getMsgStr() const { return MsgStr; }
128 const Instruction *getInstruction() const { return Instr; }
130 /// \see DiagnosticInfo::print.
131 virtual void print(DiagnosticPrinter &DP) const;
133 /// Hand rolled RTTI.
134 static bool classof(const DiagnosticInfo *DI) {
135 return DI->getKind() == DK_InlineAsm;
139 /// Diagnostic information for stack size reporting.
140 /// This is basically a function and a size.
141 class DiagnosticInfoStackSize : public DiagnosticInfo {
143 /// The function that is concerned by this stack size diagnostic.
145 /// The computed stack size.
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) {}
155 const Function &getFunction() const { return Fn; }
156 unsigned getStackSize() const { return StackSize; }
158 /// \see DiagnosticInfo::print.
159 virtual void print(DiagnosticPrinter &DP) const;
161 /// Hand rolled RTTI.
162 static bool classof(const DiagnosticInfo *DI) {
163 return DI->getKind() == DK_StackSize;
167 /// Diagnostic information for debug metadata version reporting.
168 /// This is basically a module and a version.
169 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
171 /// The module that is concerned by this debug metadata version diagnostic.
173 /// The actual metadata version.
174 unsigned MetadataVersion;
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) {}
184 const Module &getModule() const { return M; }
185 unsigned getMetadataVersion() const { return MetadataVersion; }
187 /// \see DiagnosticInfo::print.
188 virtual void print(DiagnosticPrinter &DP) const;
190 /// Hand rolled RTTI.
191 static bool classof(const DiagnosticInfo *DI) {
192 return DI->getKind() == DK_DebugMetadataVersion;
196 } // End namespace llvm