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