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-c/Core.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/IR/DebugLoc.h"
21 #include "llvm/Support/Casting.h"
25 // Forward declarations.
26 class DiagnosticPrinter;
29 class LLVMContextImpl;
34 /// \brief Defines the different supported severity of a diagnostic.
35 enum DiagnosticSeverity {
39 // A note attaches additional information to one of the previous diagnostic
44 /// \brief Defines the different supported kind of a diagnostic.
45 /// This enum should be extended with a new ID for each added concrete subclass.
49 DK_DebugMetadataVersion,
51 DK_OptimizationRemark,
52 DK_OptimizationRemarkMissed,
53 DK_OptimizationRemarkAnalysis,
57 /// \brief Get the next available kind ID for a plugin diagnostic.
58 /// Each time this function is called, it returns a different number.
59 /// Therefore, a plugin that wants to "identify" its own classes
60 /// with a dynamic identifier, just have to use this method to get a new ID
61 /// and assign it to each of its classes.
62 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
63 /// Thus, the plugin identifiers will not conflict with the
64 /// DiagnosticKind values.
65 int getNextAvailablePluginDiagnosticKind();
67 /// \brief This is the base abstract class for diagnostic reporting in
69 /// The print method must be overloaded by the subclasses to print a
70 /// user-friendly message in the client of the backend (let us call it a
72 class DiagnosticInfo {
74 /// Kind defines the kind of report this is about.
75 const /* DiagnosticKind */ int Kind;
76 /// Severity gives the severity of the diagnostic.
77 const DiagnosticSeverity Severity;
80 DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
81 : Kind(Kind), Severity(Severity) {}
83 virtual ~DiagnosticInfo() {}
85 /* DiagnosticKind */ int getKind() const { return Kind; }
86 DiagnosticSeverity getSeverity() const { return Severity; }
88 /// Print using the given \p DP a user-friendly message.
89 /// This is the default message that will be printed to the user.
90 /// It is used when the frontend does not directly take advantage
91 /// of the information contained in fields of the subclasses.
92 /// The printed message must not end with '.' nor start with a severity
94 virtual void print(DiagnosticPrinter &DP) const = 0;
97 /// Diagnostic information for inline asm reporting.
98 /// This is basically a message and an optional location.
99 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
101 /// Optional line information. 0 if not set.
103 /// Message to be reported.
105 /// Optional origin of the problem.
106 const Instruction *Instr;
109 /// \p MsgStr is the message to be reported to the frontend.
110 /// This class does not copy \p MsgStr, therefore the reference must be valid
111 /// for the whole life time of the Diagnostic.
112 DiagnosticInfoInlineAsm(const Twine &MsgStr,
113 DiagnosticSeverity Severity = DS_Error)
114 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
117 /// \p LocCookie if non-zero gives the line number for this report.
118 /// \p MsgStr gives the message.
119 /// This class does not copy \p MsgStr, therefore the reference must be valid
120 /// for the whole life time of the Diagnostic.
121 DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
122 DiagnosticSeverity Severity = DS_Error)
123 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
124 MsgStr(MsgStr), Instr(nullptr) {}
126 /// \p Instr gives the original instruction that triggered the diagnostic.
127 /// \p MsgStr gives the message.
128 /// This class does not copy \p MsgStr, therefore the reference must be valid
129 /// for the whole life time of the Diagnostic.
131 DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
132 DiagnosticSeverity Severity = DS_Error);
134 unsigned getLocCookie() const { return LocCookie; }
135 const Twine &getMsgStr() const { return MsgStr; }
136 const Instruction *getInstruction() const { return Instr; }
138 /// \see DiagnosticInfo::print.
139 void print(DiagnosticPrinter &DP) const override;
141 /// Hand rolled RTTI.
142 static bool classof(const DiagnosticInfo *DI) {
143 return DI->getKind() == DK_InlineAsm;
147 /// Diagnostic information for stack size reporting.
148 /// This is basically a function and a size.
149 class DiagnosticInfoStackSize : public DiagnosticInfo {
151 /// The function that is concerned by this stack size diagnostic.
153 /// The computed stack size.
157 /// \p The function that is concerned by this stack size diagnostic.
158 /// \p The computed stack size.
159 DiagnosticInfoStackSize(const Function &Fn, unsigned StackSize,
160 DiagnosticSeverity Severity = DS_Warning)
161 : DiagnosticInfo(DK_StackSize, Severity), Fn(Fn), StackSize(StackSize) {}
163 const Function &getFunction() const { return Fn; }
164 unsigned getStackSize() const { return StackSize; }
166 /// \see DiagnosticInfo::print.
167 void print(DiagnosticPrinter &DP) const override;
169 /// Hand rolled RTTI.
170 static bool classof(const DiagnosticInfo *DI) {
171 return DI->getKind() == DK_StackSize;
175 /// Diagnostic information for debug metadata version reporting.
176 /// This is basically a module and a version.
177 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
179 /// The module that is concerned by this debug metadata version diagnostic.
181 /// The actual metadata version.
182 unsigned MetadataVersion;
185 /// \p The module that is concerned by this debug metadata version diagnostic.
186 /// \p The actual metadata version.
187 DiagnosticInfoDebugMetadataVersion(const Module &M, unsigned MetadataVersion,
188 DiagnosticSeverity Severity = DS_Warning)
189 : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M),
190 MetadataVersion(MetadataVersion) {}
192 const Module &getModule() const { return M; }
193 unsigned getMetadataVersion() const { return MetadataVersion; }
195 /// \see DiagnosticInfo::print.
196 void print(DiagnosticPrinter &DP) const override;
198 /// Hand rolled RTTI.
199 static bool classof(const DiagnosticInfo *DI) {
200 return DI->getKind() == DK_DebugMetadataVersion;
204 /// Diagnostic information for the sample profiler.
205 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
207 DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
209 DiagnosticSeverity Severity = DS_Error)
210 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
211 LineNum(LineNum), Msg(Msg) {}
212 DiagnosticInfoSampleProfile(const char *FileName, const Twine &Msg,
213 DiagnosticSeverity Severity = DS_Error)
214 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
215 LineNum(0), Msg(Msg) {}
216 DiagnosticInfoSampleProfile(const Twine &Msg,
217 DiagnosticSeverity Severity = DS_Error)
218 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(nullptr),
219 LineNum(0), Msg(Msg) {}
221 /// \see DiagnosticInfo::print.
222 void print(DiagnosticPrinter &DP) const override;
224 /// Hand rolled RTTI.
225 static bool classof(const DiagnosticInfo *DI) {
226 return DI->getKind() == DK_SampleProfile;
229 const char *getFileName() const { return FileName; }
230 unsigned getLineNum() const { return LineNum; }
231 const Twine &getMsg() const { return Msg; }
234 /// Name of the input file associated with this diagnostic.
235 const char *FileName;
237 /// Line number where the diagnostic occurred. If 0, no line number will
238 /// be emitted in the message.
241 /// Message to report.
245 /// Common features for diagnostics dealing with optimization remarks.
246 class DiagnosticInfoOptimizationRemarkBase : public DiagnosticInfo {
248 /// \p PassName is the name of the pass emitting this diagnostic.
249 /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
250 /// the location information to use in the diagnostic. If line table
251 /// information is available, the diagnostic will include the source code
252 /// location. \p Msg is the message to show. Note that this class does not
253 /// copy this message, so this reference must be valid for the whole life time
254 /// of the diagnostic.
255 DiagnosticInfoOptimizationRemarkBase(enum DiagnosticKind Kind,
256 const char *PassName, const Function &Fn,
257 const DebugLoc &DLoc, const Twine &Msg)
258 : DiagnosticInfo(Kind, DS_Remark), PassName(PassName), Fn(Fn), DLoc(DLoc),
261 /// \see DiagnosticInfo::print.
262 void print(DiagnosticPrinter &DP) const override;
264 /// Hand rolled RTTI.
265 static bool classof(const DiagnosticInfo *DI) {
266 return DI->getKind() == DK_OptimizationRemark;
269 /// Return true if this optimization remark is enabled by one of
270 /// of the LLVM command line flags (-pass-remarks, -pass-remarks-missed,
271 /// or -pass-remarks-analysis). Note that this only handles the LLVM
272 /// flags. We cannot access Clang flags from here (they are handled
273 /// in BackendConsumer::OptimizationRemarkHandler).
274 virtual bool isEnabled() const = 0;
276 /// Return true if location information is available for this diagnostic.
277 bool isLocationAvailable() const;
279 /// Return a string with the location information for this diagnostic
280 /// in the format "file:line:col". If location information is not available,
281 /// it returns "<unknown>:0:0".
282 const std::string getLocationStr() const;
284 /// Return location information for this diagnostic in three parts:
285 /// the source file name, line number and column.
286 void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
288 const char *getPassName() const { return PassName; }
289 const Function &getFunction() const { return Fn; }
290 const DebugLoc &getDebugLoc() const { return DLoc; }
291 const Twine &getMsg() const { return Msg; }
294 /// Name of the pass that triggers this report. If this matches the
295 /// regular expression given in -Rpass=regexp, then the remark will
297 const char *PassName;
299 /// Function where this diagnostic is triggered.
302 /// Debug location where this diagnostic is triggered.
305 /// Message to report.
309 /// Diagnostic information for applied optimization remarks.
310 class DiagnosticInfoOptimizationRemark
311 : public DiagnosticInfoOptimizationRemarkBase {
313 /// \p PassName is the name of the pass emitting this diagnostic. If
314 /// this name matches the regular expression given in -Rpass=, then the
315 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
316 /// is being emitted. \p DLoc is the location information to use in the
317 /// diagnostic. If line table information is available, the diagnostic
318 /// will include the source code location. \p Msg is the message to show.
319 /// Note that this class does not copy this message, so this reference
320 /// must be valid for the whole life time of the diagnostic.
321 DiagnosticInfoOptimizationRemark(const char *PassName, const Function &Fn,
322 const DebugLoc &DLoc, const Twine &Msg)
323 : DiagnosticInfoOptimizationRemarkBase(DK_OptimizationRemark, PassName,
327 static bool classof(const DiagnosticInfo *DI) {
328 return DI->getKind() == DK_OptimizationRemark;
331 /// \see DiagnosticInfoOptimizationRemarkBase::isEnabled.
332 virtual bool isEnabled() const override;
335 /// Diagnostic information for missed-optimization remarks.
336 class DiagnosticInfoOptimizationRemarkMissed
337 : public DiagnosticInfoOptimizationRemarkBase {
339 /// \p PassName is the name of the pass emitting this diagnostic. If
340 /// this name matches the regular expression given in -Rpass-missed=, then the
341 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
342 /// is being emitted. \p DLoc is the location information to use in the
343 /// diagnostic. If line table information is available, the diagnostic
344 /// will include the source code location. \p Msg is the message to show.
345 /// Note that this class does not copy this message, so this reference
346 /// must be valid for the whole life time of the diagnostic.
347 DiagnosticInfoOptimizationRemarkMissed(const char *PassName,
349 const DebugLoc &DLoc, const Twine &Msg)
350 : DiagnosticInfoOptimizationRemarkBase(DK_OptimizationRemarkMissed,
351 PassName, Fn, DLoc, Msg) {}
354 static bool classof(const DiagnosticInfo *DI) {
355 return DI->getKind() == DK_OptimizationRemarkMissed;
358 /// \see DiagnosticInfoOptimizationRemarkBase::isEnabled.
359 virtual bool isEnabled() const override;
362 /// Diagnostic information for optimization analysis remarks.
363 class DiagnosticInfoOptimizationRemarkAnalysis
364 : public DiagnosticInfoOptimizationRemarkBase {
366 /// \p PassName is the name of the pass emitting this diagnostic. If
367 /// this name matches the regular expression given in -Rpass-analysis=, then
368 /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
369 /// is being emitted. \p DLoc is the location information to use in the
370 /// diagnostic. If line table information is available, the diagnostic will
371 /// include the source code location. \p Msg is the message to show. Note that
372 /// this class does not copy this message, so this reference must be valid for
373 /// the whole life time of the diagnostic.
374 DiagnosticInfoOptimizationRemarkAnalysis(const char *PassName,
376 const DebugLoc &DLoc,
378 : DiagnosticInfoOptimizationRemarkBase(DK_OptimizationRemarkAnalysis,
379 PassName, Fn, DLoc, Msg) {}
382 static bool classof(const DiagnosticInfo *DI) {
383 return DI->getKind() == DK_OptimizationRemarkAnalysis;
386 /// \see DiagnosticInfoOptimizationRemarkBase::isEnabled.
387 virtual bool isEnabled() const override;
390 // Create wrappers for C Binding types (see CBindingWrapping.h).
391 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DiagnosticInfo, LLVMDiagnosticInfoRef)
393 /// Emit an optimization-applied message. \p PassName is the name of the pass
394 /// emitting the message. If -Rpass= is given and \p PassName matches the
395 /// regular expression in -Rpass, then the remark will be emitted. \p Fn is
396 /// the function triggering the remark, \p DLoc is the debug location where
397 /// the diagnostic is generated. \p Msg is the message string to use.
398 void emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
399 const Function &Fn, const DebugLoc &DLoc,
402 /// Emit an optimization-missed message. \p PassName is the name of the
403 /// pass emitting the message. If -Rpass-missed= is given and \p PassName
404 /// matches the regular expression in -Rpass, then the remark will be
405 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the
406 /// debug location where the diagnostic is generated. \p Msg is the
407 /// message string to use.
408 void emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
409 const Function &Fn, const DebugLoc &DLoc,
412 /// Emit an optimization analysis remark message. \p PassName is the name of
413 /// the pass emitting the message. If -Rpass-analysis= is given and \p
414 /// PassName matches the regular expression in -Rpass, then the remark will be
415 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the debug
416 /// location where the diagnostic is generated. \p Msg is the message string
418 void emitOptimizationRemarkAnalysis(LLVMContext &Ctx, const char *PassName,
419 const Function &Fn, const DebugLoc &DLoc,
422 } // End namespace llvm