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.
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();
55 /// \brief This is the base abstract class for diagnostic reporting in
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
60 class DiagnosticInfo {
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;
68 DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
69 : Kind(Kind), Severity(Severity) {}
71 virtual ~DiagnosticInfo() {}
73 /* DiagnosticKind */ int getKind() const { return Kind; }
74 DiagnosticSeverity getSeverity() const { return Severity; }
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
82 virtual void print(DiagnosticPrinter &DP) const = 0;
85 /// Diagnostic information for inline asm reporting.
86 /// This is basically a message and an optional location.
87 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
89 /// Optional line information. 0 if not set.
91 /// Message to be reported.
93 /// Optional origin of the problem.
94 const Instruction *Instr;
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),
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) {}
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.
119 DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
120 DiagnosticSeverity Severity = DS_Error);
122 unsigned getLocCookie() const { return LocCookie; }
123 const Twine &getMsgStr() const { return MsgStr; }
124 const Instruction *getInstruction() const { return Instr; }
126 /// \see DiagnosticInfo::print.
127 virtual void print(DiagnosticPrinter &DP) const;
129 /// Hand rolled RTTI.
130 static bool classof(const DiagnosticInfo *DI) {
131 return DI->getKind() == DK_InlineAsm;
135 /// Diagnostic information for stack size reporting.
136 /// This is basically a function and a size.
137 class DiagnosticInfoStackSize : public DiagnosticInfo {
139 /// The function that is concerned by this stack size diagnostic.
141 /// The computed stack size.
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) {}
151 const Function &getFunction() const { return Fn; }
152 unsigned getStackSize() const { return StackSize; }
154 /// \see DiagnosticInfo::print.
155 virtual void print(DiagnosticPrinter &DP) const;
157 /// Hand rolled RTTI.
158 static bool classof(const DiagnosticInfo *DI) {
159 return DI->getKind() == DK_StackSize;
163 } // End namespace llvm