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_IR_DIAGNOSTICINFO_H
16 #define LLVM_IR_DIAGNOSTICINFO_H
18 #include "llvm-c/Core.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/IR/DebugLoc.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Support/Casting.h"
27 // Forward declarations.
28 class DiagnosticPrinter;
31 class LLVMContextImpl;
37 /// \brief Defines the different supported severity of a diagnostic.
38 enum DiagnosticSeverity {
42 // A note attaches additional information to one of the previous diagnostic
47 /// \brief Defines the different supported kind of a diagnostic.
48 /// This enum should be extended with a new ID for each added concrete subclass.
54 DK_DebugMetadataVersion,
56 DK_OptimizationRemark,
57 DK_OptimizationRemarkMissed,
58 DK_OptimizationRemarkAnalysis,
59 DK_OptimizationFailure,
64 /// \brief Get the next available kind ID for a plugin diagnostic.
65 /// Each time this function is called, it returns a different number.
66 /// Therefore, a plugin that wants to "identify" its own classes
67 /// with a dynamic identifier, just have to use this method to get a new ID
68 /// and assign it to each of its classes.
69 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
70 /// Thus, the plugin identifiers will not conflict with the
71 /// DiagnosticKind values.
72 int getNextAvailablePluginDiagnosticKind();
74 /// \brief This is the base abstract class for diagnostic reporting in
76 /// The print method must be overloaded by the subclasses to print a
77 /// user-friendly message in the client of the backend (let us call it a
79 class DiagnosticInfo {
81 /// Kind defines the kind of report this is about.
82 const /* DiagnosticKind */ int Kind;
83 /// Severity gives the severity of the diagnostic.
84 const DiagnosticSeverity Severity;
87 DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
88 : Kind(Kind), Severity(Severity) {}
90 virtual ~DiagnosticInfo() {}
92 /* DiagnosticKind */ int getKind() const { return Kind; }
93 DiagnosticSeverity getSeverity() const { return Severity; }
95 /// Print using the given \p DP a user-friendly message.
96 /// This is the default message that will be printed to the user.
97 /// It is used when the frontend does not directly take advantage
98 /// of the information contained in fields of the subclasses.
99 /// The printed message must not end with '.' nor start with a severity
101 virtual void print(DiagnosticPrinter &DP) const = 0;
104 typedef std::function<void(const DiagnosticInfo &)> DiagnosticHandlerFunction;
106 /// Diagnostic information for inline asm reporting.
107 /// This is basically a message and an optional location.
108 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
110 /// Optional line information. 0 if not set.
112 /// Message to be reported.
114 /// Optional origin of the problem.
115 const Instruction *Instr;
118 /// \p MsgStr is the message to be reported to the frontend.
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(const Twine &MsgStr,
122 DiagnosticSeverity Severity = DS_Error)
123 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
126 /// \p LocCookie if non-zero gives the line number for this report.
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.
130 DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
131 DiagnosticSeverity Severity = DS_Error)
132 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
133 MsgStr(MsgStr), Instr(nullptr) {}
135 /// \p Instr gives the original instruction that triggered the diagnostic.
136 /// \p MsgStr gives the message.
137 /// This class does not copy \p MsgStr, therefore the reference must be valid
138 /// for the whole life time of the Diagnostic.
140 DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
141 DiagnosticSeverity Severity = DS_Error);
143 unsigned getLocCookie() const { return LocCookie; }
144 const Twine &getMsgStr() const { return MsgStr; }
145 const Instruction *getInstruction() const { return Instr; }
147 /// \see DiagnosticInfo::print.
148 void print(DiagnosticPrinter &DP) const override;
150 static bool classof(const DiagnosticInfo *DI) {
151 return DI->getKind() == DK_InlineAsm;
155 /// Diagnostic information for stack size reporting.
156 /// This is basically a function and a size.
157 class DiagnosticInfoStackSize : public DiagnosticInfo {
159 /// The function that is concerned by this stack size diagnostic.
161 /// The computed stack size.
165 /// \p The function that is concerned by this stack size diagnostic.
166 /// \p The computed stack size.
167 DiagnosticInfoStackSize(const Function &Fn, unsigned StackSize,
168 DiagnosticSeverity Severity = DS_Warning)
169 : DiagnosticInfo(DK_StackSize, Severity), Fn(Fn), StackSize(StackSize) {}
171 const Function &getFunction() const { return Fn; }
172 unsigned getStackSize() const { return StackSize; }
174 /// \see DiagnosticInfo::print.
175 void print(DiagnosticPrinter &DP) const override;
177 static bool classof(const DiagnosticInfo *DI) {
178 return DI->getKind() == DK_StackSize;
182 /// Diagnostic information for debug metadata version reporting.
183 /// This is basically a module and a version.
184 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
186 /// The module that is concerned by this debug metadata version diagnostic.
188 /// The actual metadata version.
189 unsigned MetadataVersion;
192 /// \p The module that is concerned by this debug metadata version diagnostic.
193 /// \p The actual metadata version.
194 DiagnosticInfoDebugMetadataVersion(const Module &M, unsigned MetadataVersion,
195 DiagnosticSeverity Severity = DS_Warning)
196 : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M),
197 MetadataVersion(MetadataVersion) {}
199 const Module &getModule() const { return M; }
200 unsigned getMetadataVersion() const { return MetadataVersion; }
202 /// \see DiagnosticInfo::print.
203 void print(DiagnosticPrinter &DP) const override;
205 static bool classof(const DiagnosticInfo *DI) {
206 return DI->getKind() == DK_DebugMetadataVersion;
210 /// Diagnostic information for the sample profiler.
211 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
213 DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
215 DiagnosticSeverity Severity = DS_Error)
216 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
217 LineNum(LineNum), Msg(Msg) {}
218 DiagnosticInfoSampleProfile(const char *FileName, const Twine &Msg,
219 DiagnosticSeverity Severity = DS_Error)
220 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
221 LineNum(0), Msg(Msg) {}
222 DiagnosticInfoSampleProfile(const Twine &Msg,
223 DiagnosticSeverity Severity = DS_Error)
224 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(nullptr),
225 LineNum(0), Msg(Msg) {}
227 /// \see DiagnosticInfo::print.
228 void print(DiagnosticPrinter &DP) const override;
230 static bool classof(const DiagnosticInfo *DI) {
231 return DI->getKind() == DK_SampleProfile;
234 const char *getFileName() const { return FileName; }
235 unsigned getLineNum() const { return LineNum; }
236 const Twine &getMsg() const { return Msg; }
239 /// Name of the input file associated with this diagnostic.
240 const char *FileName;
242 /// Line number where the diagnostic occurred. If 0, no line number will
243 /// be emitted in the message.
246 /// Message to report.
250 /// Common features for diagnostics dealing with optimization remarks.
251 class DiagnosticInfoOptimizationBase : public DiagnosticInfo {
253 /// \p PassName is the name of the pass emitting this diagnostic.
254 /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
255 /// the location information to use in the diagnostic. If line table
256 /// information is available, the diagnostic will include the source code
257 /// location. \p Msg is the message to show. Note that this class does not
258 /// copy this message, so this reference must be valid for the whole life time
259 /// of the diagnostic.
260 DiagnosticInfoOptimizationBase(enum DiagnosticKind Kind,
261 enum DiagnosticSeverity Severity,
262 const char *PassName, const Function &Fn,
263 const DebugLoc &DLoc, const Twine &Msg)
264 : DiagnosticInfo(Kind, Severity), PassName(PassName), Fn(Fn), DLoc(DLoc),
267 /// \see DiagnosticInfo::print.
268 void print(DiagnosticPrinter &DP) const override;
270 static bool classof(const DiagnosticInfo *DI) {
271 return DI->getKind() == DK_OptimizationRemark;
274 /// Return true if this optimization remark is enabled by one of
275 /// of the LLVM command line flags (-pass-remarks, -pass-remarks-missed,
276 /// or -pass-remarks-analysis). Note that this only handles the LLVM
277 /// flags. We cannot access Clang flags from here (they are handled
278 /// in BackendConsumer::OptimizationRemarkHandler).
279 virtual bool isEnabled() const = 0;
281 /// Return true if location information is available for this diagnostic.
282 bool isLocationAvailable() const;
284 /// Return a string with the location information for this diagnostic
285 /// in the format "file:line:col". If location information is not available,
286 /// it returns "<unknown>:0:0".
287 const std::string getLocationStr() const;
289 /// Return location information for this diagnostic in three parts:
290 /// the source file name, line number and column.
291 void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
293 const char *getPassName() const { return PassName; }
294 const Function &getFunction() const { return Fn; }
295 const DebugLoc &getDebugLoc() const { return DLoc; }
296 const Twine &getMsg() const { return Msg; }
299 /// Name of the pass that triggers this report. If this matches the
300 /// regular expression given in -Rpass=regexp, then the remark will
302 const char *PassName;
304 /// Function where this diagnostic is triggered.
307 /// Debug location where this diagnostic is triggered.
310 /// Message to report.
314 /// Diagnostic information for applied optimization remarks.
315 class DiagnosticInfoOptimizationRemark : public DiagnosticInfoOptimizationBase {
317 /// \p PassName is the name of the pass emitting this diagnostic. If
318 /// this name matches the regular expression given in -Rpass=, then the
319 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
320 /// is being emitted. \p DLoc is the location information to use in the
321 /// diagnostic. If line table information is available, the diagnostic
322 /// will include the source code location. \p Msg is the message to show.
323 /// Note that this class does not copy this message, so this reference
324 /// must be valid for the whole life time of the diagnostic.
325 DiagnosticInfoOptimizationRemark(const char *PassName, const Function &Fn,
326 const DebugLoc &DLoc, const Twine &Msg)
327 : DiagnosticInfoOptimizationBase(DK_OptimizationRemark, DS_Remark,
328 PassName, Fn, DLoc, Msg) {}
330 static bool classof(const DiagnosticInfo *DI) {
331 return DI->getKind() == DK_OptimizationRemark;
334 /// \see DiagnosticInfoOptimizationBase::isEnabled.
335 bool isEnabled() const override;
338 /// Diagnostic information for missed-optimization remarks.
339 class DiagnosticInfoOptimizationRemarkMissed
340 : public DiagnosticInfoOptimizationBase {
342 /// \p PassName is the name of the pass emitting this diagnostic. If
343 /// this name matches the regular expression given in -Rpass-missed=, then the
344 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
345 /// is being emitted. \p DLoc is the location information to use in the
346 /// diagnostic. If line table information is available, the diagnostic
347 /// will include the source code location. \p Msg is the message to show.
348 /// Note that this class does not copy this message, so this reference
349 /// must be valid for the whole life time of the diagnostic.
350 DiagnosticInfoOptimizationRemarkMissed(const char *PassName,
352 const DebugLoc &DLoc, const Twine &Msg)
353 : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkMissed, DS_Remark,
354 PassName, Fn, DLoc, Msg) {}
356 static bool classof(const DiagnosticInfo *DI) {
357 return DI->getKind() == DK_OptimizationRemarkMissed;
360 /// \see DiagnosticInfoOptimizationBase::isEnabled.
361 bool isEnabled() const override;
364 /// Diagnostic information for optimization analysis remarks.
365 class DiagnosticInfoOptimizationRemarkAnalysis
366 : public DiagnosticInfoOptimizationBase {
368 /// \p PassName is the name of the pass emitting this diagnostic. If
369 /// this name matches the regular expression given in -Rpass-analysis=, then
370 /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
371 /// is being emitted. \p DLoc is the location information to use in the
372 /// diagnostic. If line table information is available, the diagnostic will
373 /// include the source code location. \p Msg is the message to show. Note that
374 /// this class does not copy this message, so this reference must be valid for
375 /// the whole life time of the diagnostic.
376 DiagnosticInfoOptimizationRemarkAnalysis(const char *PassName,
378 const DebugLoc &DLoc,
380 : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkAnalysis, DS_Remark,
381 PassName, Fn, DLoc, Msg) {}
383 static bool classof(const DiagnosticInfo *DI) {
384 return DI->getKind() == DK_OptimizationRemarkAnalysis;
387 /// \see DiagnosticInfoOptimizationBase::isEnabled.
388 bool isEnabled() const override;
391 /// Diagnostic information for machine IR parser.
392 class DiagnosticInfoMIRParser : public DiagnosticInfo {
393 const SMDiagnostic &Diagnostic;
396 DiagnosticInfoMIRParser(DiagnosticSeverity Severity,
397 const SMDiagnostic &Diagnostic)
398 : DiagnosticInfo(DK_MIRParser, Severity), Diagnostic(Diagnostic) {}
400 const SMDiagnostic &getDiagnostic() const { return Diagnostic; }
402 void print(DiagnosticPrinter &DP) const override;
404 static bool classof(const DiagnosticInfo *DI) {
405 return DI->getKind() == DK_MIRParser;
409 // Create wrappers for C Binding types (see CBindingWrapping.h).
410 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DiagnosticInfo, LLVMDiagnosticInfoRef)
412 /// Emit an optimization-applied message. \p PassName is the name of the pass
413 /// emitting the message. If -Rpass= is given and \p PassName matches the
414 /// regular expression in -Rpass, then the remark will be emitted. \p Fn is
415 /// the function triggering the remark, \p DLoc is the debug location where
416 /// the diagnostic is generated. \p Msg is the message string to use.
417 void emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
418 const Function &Fn, const DebugLoc &DLoc,
421 /// Emit an optimization-missed message. \p PassName is the name of the
422 /// pass emitting the message. If -Rpass-missed= is given and \p PassName
423 /// matches the regular expression in -Rpass, then the remark will be
424 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the
425 /// debug location where the diagnostic is generated. \p Msg is the
426 /// message string to use.
427 void emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
428 const Function &Fn, const DebugLoc &DLoc,
431 /// Emit an optimization analysis remark message. \p PassName is the name of
432 /// the pass emitting the message. If -Rpass-analysis= is given and \p
433 /// PassName matches the regular expression in -Rpass, then the remark will be
434 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the debug
435 /// location where the diagnostic is generated. \p Msg is the message string
437 void emitOptimizationRemarkAnalysis(LLVMContext &Ctx, const char *PassName,
438 const Function &Fn, const DebugLoc &DLoc,
441 /// Diagnostic information for optimization failures.
442 class DiagnosticInfoOptimizationFailure
443 : public DiagnosticInfoOptimizationBase {
445 /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
446 /// the location information to use in the diagnostic. If line table
447 /// information is available, the diagnostic will include the source code
448 /// location. \p Msg is the message to show. Note that this class does not
449 /// copy this message, so this reference must be valid for the whole life time
450 /// of the diagnostic.
451 DiagnosticInfoOptimizationFailure(const Function &Fn, const DebugLoc &DLoc,
453 : DiagnosticInfoOptimizationBase(DK_OptimizationFailure, DS_Warning,
454 nullptr, Fn, DLoc, Msg) {}
456 static bool classof(const DiagnosticInfo *DI) {
457 return DI->getKind() == DK_OptimizationFailure;
460 /// \see DiagnosticInfoOptimizationBase::isEnabled.
461 bool isEnabled() const override;
464 /// Emit a warning when loop vectorization is specified but fails. \p Fn is the
465 /// function triggering the warning, \p DLoc is the debug location where the
466 /// diagnostic is generated. \p Msg is the message string to use.
467 void emitLoopVectorizeWarning(LLVMContext &Ctx, const Function &Fn,
468 const DebugLoc &DLoc, const Twine &Msg);
470 /// Emit a warning when loop interleaving is specified but fails. \p Fn is the
471 /// function triggering the warning, \p DLoc is the debug location where the
472 /// diagnostic is generated. \p Msg is the message string to use.
473 void emitLoopInterleaveWarning(LLVMContext &Ctx, const Function &Fn,
474 const DebugLoc &DLoc, const Twine &Msg);
476 } // End namespace llvm