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_OptimizationRemarkAnalysisFPCommute,
60 DK_OptimizationRemarkAnalysisAliasing,
61 DK_OptimizationFailure,
67 /// \brief Get the next available kind ID for a plugin diagnostic.
68 /// Each time this function is called, it returns a different number.
69 /// Therefore, a plugin that wants to "identify" its own classes
70 /// with a dynamic identifier, just have to use this method to get a new ID
71 /// and assign it to each of its classes.
72 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
73 /// Thus, the plugin identifiers will not conflict with the
74 /// DiagnosticKind values.
75 int getNextAvailablePluginDiagnosticKind();
77 /// \brief This is the base abstract class for diagnostic reporting in
79 /// The print method must be overloaded by the subclasses to print a
80 /// user-friendly message in the client of the backend (let us call it a
82 class DiagnosticInfo {
84 /// Kind defines the kind of report this is about.
85 const /* DiagnosticKind */ int Kind;
86 /// Severity gives the severity of the diagnostic.
87 const DiagnosticSeverity Severity;
90 DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
91 : Kind(Kind), Severity(Severity) {}
93 virtual ~DiagnosticInfo() {}
95 /* DiagnosticKind */ int getKind() const { return Kind; }
96 DiagnosticSeverity getSeverity() const { return Severity; }
98 /// Print using the given \p DP a user-friendly message.
99 /// This is the default message that will be printed to the user.
100 /// It is used when the frontend does not directly take advantage
101 /// of the information contained in fields of the subclasses.
102 /// The printed message must not end with '.' nor start with a severity
104 virtual void print(DiagnosticPrinter &DP) const = 0;
106 static const char *AlwaysPrint;
109 typedef std::function<void(const DiagnosticInfo &)> DiagnosticHandlerFunction;
111 /// Diagnostic information for inline asm reporting.
112 /// This is basically a message and an optional location.
113 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
115 /// Optional line information. 0 if not set.
117 /// Message to be reported.
119 /// Optional origin of the problem.
120 const Instruction *Instr;
123 /// \p MsgStr is the message to be reported to the frontend.
124 /// This class does not copy \p MsgStr, therefore the reference must be valid
125 /// for the whole life time of the Diagnostic.
126 DiagnosticInfoInlineAsm(const Twine &MsgStr,
127 DiagnosticSeverity Severity = DS_Error)
128 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
131 /// \p LocCookie if non-zero gives the line number for this report.
132 /// \p MsgStr gives the message.
133 /// This class does not copy \p MsgStr, therefore the reference must be valid
134 /// for the whole life time of the Diagnostic.
135 DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
136 DiagnosticSeverity Severity = DS_Error)
137 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
138 MsgStr(MsgStr), Instr(nullptr) {}
140 /// \p Instr gives the original instruction that triggered the diagnostic.
141 /// \p MsgStr gives the message.
142 /// This class does not copy \p MsgStr, therefore the reference must be valid
143 /// for the whole life time of the Diagnostic.
145 DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
146 DiagnosticSeverity Severity = DS_Error);
148 unsigned getLocCookie() const { return LocCookie; }
149 const Twine &getMsgStr() const { return MsgStr; }
150 const Instruction *getInstruction() const { return Instr; }
152 /// \see DiagnosticInfo::print.
153 void print(DiagnosticPrinter &DP) const override;
155 static bool classof(const DiagnosticInfo *DI) {
156 return DI->getKind() == DK_InlineAsm;
160 /// Diagnostic information for stack size reporting.
161 /// This is basically a function and a size.
162 class DiagnosticInfoStackSize : public DiagnosticInfo {
164 /// The function that is concerned by this stack size diagnostic.
166 /// The computed stack size.
170 /// \p The function that is concerned by this stack size diagnostic.
171 /// \p The computed stack size.
172 DiagnosticInfoStackSize(const Function &Fn, unsigned StackSize,
173 DiagnosticSeverity Severity = DS_Warning)
174 : DiagnosticInfo(DK_StackSize, Severity), Fn(Fn), StackSize(StackSize) {}
176 const Function &getFunction() const { return Fn; }
177 unsigned getStackSize() const { return StackSize; }
179 /// \see DiagnosticInfo::print.
180 void print(DiagnosticPrinter &DP) const override;
182 static bool classof(const DiagnosticInfo *DI) {
183 return DI->getKind() == DK_StackSize;
187 /// Diagnostic information for debug metadata version reporting.
188 /// This is basically a module and a version.
189 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
191 /// The module that is concerned by this debug metadata version diagnostic.
193 /// The actual metadata version.
194 unsigned MetadataVersion;
197 /// \p The module that is concerned by this debug metadata version diagnostic.
198 /// \p The actual metadata version.
199 DiagnosticInfoDebugMetadataVersion(const Module &M, unsigned MetadataVersion,
200 DiagnosticSeverity Severity = DS_Warning)
201 : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M),
202 MetadataVersion(MetadataVersion) {}
204 const Module &getModule() const { return M; }
205 unsigned getMetadataVersion() const { return MetadataVersion; }
207 /// \see DiagnosticInfo::print.
208 void print(DiagnosticPrinter &DP) const override;
210 static bool classof(const DiagnosticInfo *DI) {
211 return DI->getKind() == DK_DebugMetadataVersion;
215 /// Diagnostic information for the sample profiler.
216 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
218 DiagnosticInfoSampleProfile(StringRef FileName, unsigned LineNum,
220 DiagnosticSeverity Severity = DS_Error)
221 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
222 LineNum(LineNum), Msg(Msg) {}
223 DiagnosticInfoSampleProfile(StringRef FileName, const Twine &Msg,
224 DiagnosticSeverity Severity = DS_Error)
225 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
226 LineNum(0), Msg(Msg) {}
227 DiagnosticInfoSampleProfile(const Twine &Msg,
228 DiagnosticSeverity Severity = DS_Error)
229 : DiagnosticInfo(DK_SampleProfile, Severity), LineNum(0), Msg(Msg) {}
231 /// \see DiagnosticInfo::print.
232 void print(DiagnosticPrinter &DP) const override;
234 static bool classof(const DiagnosticInfo *DI) {
235 return DI->getKind() == DK_SampleProfile;
238 StringRef getFileName() const { return FileName; }
239 unsigned getLineNum() const { return LineNum; }
240 const Twine &getMsg() const { return Msg; }
243 /// Name of the input file associated with this diagnostic.
246 /// Line number where the diagnostic occurred. If 0, no line number will
247 /// be emitted in the message.
250 /// Message to report.
254 /// Diagnostic information for the PGO profiler.
255 class DiagnosticInfoPGOProfile : public DiagnosticInfo {
257 DiagnosticInfoPGOProfile(const char *FileName, const Twine &Msg,
258 DiagnosticSeverity Severity = DS_Error)
259 : DiagnosticInfo(DK_PGOProfile, Severity), FileName(FileName), Msg(Msg) {}
261 /// \see DiagnosticInfo::print.
262 void print(DiagnosticPrinter &DP) const override;
264 static bool classof(const DiagnosticInfo *DI) {
265 return DI->getKind() == DK_PGOProfile;
268 const char *getFileName() const { return FileName; }
269 const Twine &getMsg() const { return Msg; }
272 /// Name of the input file associated with this diagnostic.
273 const char *FileName;
275 /// Message to report.
279 /// Common features for diagnostics dealing with optimization remarks.
280 class DiagnosticInfoOptimizationBase : public DiagnosticInfo {
282 /// \p PassName is the name of the pass emitting this diagnostic.
283 /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
284 /// the location information to use in the diagnostic. If line table
285 /// information is available, the diagnostic will include the source code
286 /// location. \p Msg is the message to show. Note that this class does not
287 /// copy this message, so this reference must be valid for the whole life time
288 /// of the diagnostic.
289 DiagnosticInfoOptimizationBase(enum DiagnosticKind Kind,
290 enum DiagnosticSeverity Severity,
291 const char *PassName, const Function &Fn,
292 const DebugLoc &DLoc, const Twine &Msg)
293 : DiagnosticInfo(Kind, Severity), PassName(PassName), Fn(Fn), DLoc(DLoc),
296 /// \see DiagnosticInfo::print.
297 void print(DiagnosticPrinter &DP) const override;
299 /// Return true if this optimization remark is enabled by one of
300 /// of the LLVM command line flags (-pass-remarks, -pass-remarks-missed,
301 /// or -pass-remarks-analysis). Note that this only handles the LLVM
302 /// flags. We cannot access Clang flags from here (they are handled
303 /// in BackendConsumer::OptimizationRemarkHandler).
304 virtual bool isEnabled() const = 0;
306 /// Return true if location information is available for this diagnostic.
307 bool isLocationAvailable() const;
309 /// Return a string with the location information for this diagnostic
310 /// in the format "file:line:col". If location information is not available,
311 /// it returns "<unknown>:0:0".
312 const std::string getLocationStr() const;
314 /// Return location information for this diagnostic in three parts:
315 /// the source file name, line number and column.
316 void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
318 const char *getPassName() const { return PassName; }
319 const Function &getFunction() const { return Fn; }
320 const DebugLoc &getDebugLoc() const { return DLoc; }
321 const Twine &getMsg() const { return Msg; }
324 /// Name of the pass that triggers this report. If this matches the
325 /// regular expression given in -Rpass=regexp, then the remark will
327 const char *PassName;
329 /// Function where this diagnostic is triggered.
332 /// Debug location where this diagnostic is triggered.
335 /// Message to report.
339 /// Diagnostic information for applied optimization remarks.
340 class DiagnosticInfoOptimizationRemark : 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=, 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 DiagnosticInfoOptimizationRemark(const char *PassName, const Function &Fn,
351 const DebugLoc &DLoc, const Twine &Msg)
352 : DiagnosticInfoOptimizationBase(DK_OptimizationRemark, DS_Remark,
353 PassName, Fn, DLoc, Msg) {}
355 static bool classof(const DiagnosticInfo *DI) {
356 return DI->getKind() == DK_OptimizationRemark;
359 /// \see DiagnosticInfoOptimizationBase::isEnabled.
360 bool isEnabled() const override;
363 /// Diagnostic information for missed-optimization remarks.
364 class DiagnosticInfoOptimizationRemarkMissed
365 : public DiagnosticInfoOptimizationBase {
367 /// \p PassName is the name of the pass emitting this diagnostic. If
368 /// this name matches the regular expression given in -Rpass-missed=, then the
369 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
370 /// is being emitted. \p DLoc is the location information to use in the
371 /// diagnostic. If line table information is available, the diagnostic
372 /// will include the source code location. \p Msg is the message to show.
373 /// Note that this class does not copy this message, so this reference
374 /// must be valid for the whole life time of the diagnostic.
375 DiagnosticInfoOptimizationRemarkMissed(const char *PassName,
377 const DebugLoc &DLoc, const Twine &Msg)
378 : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkMissed, DS_Remark,
379 PassName, Fn, DLoc, Msg) {}
381 static bool classof(const DiagnosticInfo *DI) {
382 return DI->getKind() == DK_OptimizationRemarkMissed;
385 /// \see DiagnosticInfoOptimizationBase::isEnabled.
386 bool isEnabled() const override;
389 /// Diagnostic information for optimization analysis remarks.
390 class DiagnosticInfoOptimizationRemarkAnalysis
391 : public DiagnosticInfoOptimizationBase {
393 /// \p PassName is the name of the pass emitting this diagnostic. If
394 /// this name matches the regular expression given in -Rpass-analysis=, then
395 /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
396 /// is being emitted. \p DLoc is the location information to use in the
397 /// diagnostic. If line table information is available, the diagnostic will
398 /// include the source code location. \p Msg is the message to show. Note that
399 /// this class does not copy this message, so this reference must be valid for
400 /// the whole life time of the diagnostic.
401 DiagnosticInfoOptimizationRemarkAnalysis(const char *PassName,
403 const DebugLoc &DLoc,
405 : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkAnalysis, DS_Remark,
406 PassName, Fn, DLoc, Msg) {}
408 static bool classof(const DiagnosticInfo *DI) {
409 return DI->getKind() == DK_OptimizationRemarkAnalysis;
412 /// \see DiagnosticInfoOptimizationBase::isEnabled.
413 bool isEnabled() const override;
416 DiagnosticInfoOptimizationRemarkAnalysis(enum DiagnosticKind Kind,
417 const char *PassName,
419 const DebugLoc &DLoc,
421 : DiagnosticInfoOptimizationBase(Kind, DS_Remark, PassName, Fn, DLoc,
425 /// Diagnostic information for optimization analysis remarks related to
426 /// floating-point non-commutativity.
427 class DiagnosticInfoOptimizationRemarkAnalysisFPCommute
428 : public DiagnosticInfoOptimizationRemarkAnalysis {
430 /// \p PassName is the name of the pass emitting this diagnostic. If
431 /// this name matches the regular expression given in -Rpass-analysis=, then
432 /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
433 /// is being emitted. \p DLoc is the location information to use in the
434 /// diagnostic. If line table information is available, the diagnostic will
435 /// include the source code location. \p Msg is the message to show. The
436 /// front-end will append its own message related to options that address
437 /// floating-point non-commutativity. Note that this class does not copy this
438 /// message, so this reference must be valid for the whole life time of the
440 DiagnosticInfoOptimizationRemarkAnalysisFPCommute(const char *PassName,
442 const DebugLoc &DLoc,
444 : DiagnosticInfoOptimizationRemarkAnalysis(
445 DK_OptimizationRemarkAnalysisFPCommute, PassName, Fn, DLoc, Msg) {}
447 static bool classof(const DiagnosticInfo *DI) {
448 return DI->getKind() == DK_OptimizationRemarkAnalysisFPCommute;
452 /// Diagnostic information for optimization analysis remarks related to
453 /// pointer aliasing.
454 class DiagnosticInfoOptimizationRemarkAnalysisAliasing
455 : public DiagnosticInfoOptimizationRemarkAnalysis {
457 /// \p PassName is the name of the pass emitting this diagnostic. If
458 /// this name matches the regular expression given in -Rpass-analysis=, then
459 /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
460 /// is being emitted. \p DLoc is the location information to use in the
461 /// diagnostic. If line table information is available, the diagnostic will
462 /// include the source code location. \p Msg is the message to show. The
463 /// front-end will append its own message related to options that address
464 /// pointer aliasing legality. Note that this class does not copy this
465 /// message, so this reference must be valid for the whole life time of the
467 DiagnosticInfoOptimizationRemarkAnalysisAliasing(const char *PassName,
469 const DebugLoc &DLoc,
471 : DiagnosticInfoOptimizationRemarkAnalysis(
472 DK_OptimizationRemarkAnalysisAliasing, PassName, Fn, DLoc, Msg) {}
474 static bool classof(const DiagnosticInfo *DI) {
475 return DI->getKind() == DK_OptimizationRemarkAnalysisAliasing;
479 /// Diagnostic information for machine IR parser.
480 class DiagnosticInfoMIRParser : public DiagnosticInfo {
481 const SMDiagnostic &Diagnostic;
484 DiagnosticInfoMIRParser(DiagnosticSeverity Severity,
485 const SMDiagnostic &Diagnostic)
486 : DiagnosticInfo(DK_MIRParser, Severity), Diagnostic(Diagnostic) {}
488 const SMDiagnostic &getDiagnostic() const { return Diagnostic; }
490 void print(DiagnosticPrinter &DP) const override;
492 static bool classof(const DiagnosticInfo *DI) {
493 return DI->getKind() == DK_MIRParser;
497 // Create wrappers for C Binding types (see CBindingWrapping.h).
498 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DiagnosticInfo, LLVMDiagnosticInfoRef)
500 /// Emit an optimization-applied message. \p PassName is the name of the pass
501 /// emitting the message. If -Rpass= is given and \p PassName matches the
502 /// regular expression in -Rpass, then the remark will be emitted. \p Fn is
503 /// the function triggering the remark, \p DLoc is the debug location where
504 /// the diagnostic is generated. \p Msg is the message string to use.
505 void emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
506 const Function &Fn, const DebugLoc &DLoc,
509 /// Emit an optimization-missed message. \p PassName is the name of the
510 /// pass emitting the message. If -Rpass-missed= is given and \p PassName
511 /// matches the regular expression in -Rpass, then the remark will be
512 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the
513 /// debug location where the diagnostic is generated. \p Msg is the
514 /// message string to use.
515 void emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
516 const Function &Fn, const DebugLoc &DLoc,
519 /// Emit an optimization analysis remark message. \p PassName is the name of
520 /// the pass emitting the message. If -Rpass-analysis= is given and \p
521 /// PassName matches the regular expression in -Rpass, then the remark will be
522 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the debug
523 /// location where the diagnostic is generated. \p Msg is the message string
525 void emitOptimizationRemarkAnalysis(LLVMContext &Ctx, const char *PassName,
526 const Function &Fn, const DebugLoc &DLoc,
529 /// Emit an optimization analysis remark related to messages about
530 /// floating-point non-commutativity. \p PassName is the name of the pass
531 /// emitting the message. If -Rpass-analysis= is given and \p PassName matches
532 /// the regular expression in -Rpass, then the remark will be emitted. \p Fn is
533 /// the function triggering the remark, \p DLoc is the debug location where the
534 /// diagnostic is generated. \p Msg is the message string to use.
535 void emitOptimizationRemarkAnalysisFPCommute(LLVMContext &Ctx,
536 const char *PassName,
538 const DebugLoc &DLoc,
541 /// Emit an optimization analysis remark related to messages about
542 /// pointer aliasing. \p PassName is the name of the pass emitting the message.
543 /// If -Rpass-analysis= is given and \p PassName matches the regular expression
544 /// in -Rpass, then the remark will be emitted. \p Fn is the function triggering
545 /// the remark, \p DLoc is the debug location where the diagnostic is generated.
546 /// \p Msg is the message string to use.
547 void emitOptimizationRemarkAnalysisAliasing(LLVMContext &Ctx,
548 const char *PassName,
550 const DebugLoc &DLoc,
553 /// Diagnostic information for optimization failures.
554 class DiagnosticInfoOptimizationFailure
555 : public DiagnosticInfoOptimizationBase {
557 /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
558 /// the location information to use in the diagnostic. If line table
559 /// information is available, the diagnostic will include the source code
560 /// location. \p Msg is the message to show. Note that this class does not
561 /// copy this message, so this reference must be valid for the whole life time
562 /// of the diagnostic.
563 DiagnosticInfoOptimizationFailure(const Function &Fn, const DebugLoc &DLoc,
565 : DiagnosticInfoOptimizationBase(DK_OptimizationFailure, DS_Warning,
566 nullptr, Fn, DLoc, Msg) {}
568 static bool classof(const DiagnosticInfo *DI) {
569 return DI->getKind() == DK_OptimizationFailure;
572 /// \see DiagnosticInfoOptimizationBase::isEnabled.
573 bool isEnabled() const override;
576 /// Emit a warning when loop vectorization is specified but fails. \p Fn is the
577 /// function triggering the warning, \p DLoc is the debug location where the
578 /// diagnostic is generated. \p Msg is the message string to use.
579 void emitLoopVectorizeWarning(LLVMContext &Ctx, const Function &Fn,
580 const DebugLoc &DLoc, const Twine &Msg);
582 /// Emit a warning when loop interleaving is specified but fails. \p Fn is the
583 /// function triggering the warning, \p DLoc is the debug location where the
584 /// diagnostic is generated. \p Msg is the message string to use.
585 void emitLoopInterleaveWarning(LLVMContext &Ctx, const Function &Fn,
586 const DebugLoc &DLoc, const Twine &Msg);
588 } // End namespace llvm