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/ADT/ArrayRef.h"
19 #include "llvm/IR/DebugLoc.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Support/Casting.h"
26 // Forward declarations.
27 class DiagnosticPrinter;
30 class LLVMContextImpl;
36 /// \brief Defines the different supported severity of a diagnostic.
37 enum DiagnosticSeverity {
41 // A note attaches additional information to one of the previous diagnostic
46 /// \brief Defines the different supported kind of a diagnostic.
47 /// This enum should be extended with a new ID for each added concrete subclass.
53 DK_DebugMetadataVersion,
55 DK_OptimizationRemark,
56 DK_OptimizationRemarkMissed,
57 DK_OptimizationRemarkAnalysis,
58 DK_OptimizationRemarkAnalysisFPCommute,
59 DK_OptimizationRemarkAnalysisAliasing,
60 DK_OptimizationFailure,
66 /// \brief Get the next available kind ID for a plugin diagnostic.
67 /// Each time this function is called, it returns a different number.
68 /// Therefore, a plugin that wants to "identify" its own classes
69 /// with a dynamic identifier, just have to use this method to get a new ID
70 /// and assign it to each of its classes.
71 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
72 /// Thus, the plugin identifiers will not conflict with the
73 /// DiagnosticKind values.
74 int getNextAvailablePluginDiagnosticKind();
76 /// \brief This is the base abstract class for diagnostic reporting in
78 /// The print method must be overloaded by the subclasses to print a
79 /// user-friendly message in the client of the backend (let us call it a
81 class DiagnosticInfo {
83 /// Kind defines the kind of report this is about.
84 const /* DiagnosticKind */ int Kind;
85 /// Severity gives the severity of the diagnostic.
86 const DiagnosticSeverity Severity;
89 DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
90 : Kind(Kind), Severity(Severity) {}
92 virtual ~DiagnosticInfo() {}
94 /* DiagnosticKind */ int getKind() const { return Kind; }
95 DiagnosticSeverity getSeverity() const { return Severity; }
97 /// Print using the given \p DP a user-friendly message.
98 /// This is the default message that will be printed to the user.
99 /// It is used when the frontend does not directly take advantage
100 /// of the information contained in fields of the subclasses.
101 /// The printed message must not end with '.' nor start with a severity
103 virtual void print(DiagnosticPrinter &DP) const = 0;
105 static const char *AlwaysPrint;
108 typedef std::function<void(const DiagnosticInfo &)> DiagnosticHandlerFunction;
110 /// Diagnostic information for inline asm reporting.
111 /// This is basically a message and an optional location.
112 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
114 /// Optional line information. 0 if not set.
116 /// Message to be reported.
118 /// Optional origin of the problem.
119 const Instruction *Instr;
122 /// \p MsgStr is the message to be reported to the frontend.
123 /// This class does not copy \p MsgStr, therefore the reference must be valid
124 /// for the whole life time of the Diagnostic.
125 DiagnosticInfoInlineAsm(const Twine &MsgStr,
126 DiagnosticSeverity Severity = DS_Error)
127 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
130 /// \p LocCookie if non-zero gives the line number for this report.
131 /// \p MsgStr gives the message.
132 /// This class does not copy \p MsgStr, therefore the reference must be valid
133 /// for the whole life time of the Diagnostic.
134 DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
135 DiagnosticSeverity Severity = DS_Error)
136 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
137 MsgStr(MsgStr), Instr(nullptr) {}
139 /// \p Instr gives the original instruction that triggered the diagnostic.
140 /// \p MsgStr gives the message.
141 /// This class does not copy \p MsgStr, therefore the reference must be valid
142 /// for the whole life time of the Diagnostic.
144 DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
145 DiagnosticSeverity Severity = DS_Error);
147 unsigned getLocCookie() const { return LocCookie; }
148 const Twine &getMsgStr() const { return MsgStr; }
149 const Instruction *getInstruction() const { return Instr; }
151 /// \see DiagnosticInfo::print.
152 void print(DiagnosticPrinter &DP) const override;
154 static bool classof(const DiagnosticInfo *DI) {
155 return DI->getKind() == DK_InlineAsm;
159 /// Diagnostic information for stack size reporting.
160 /// This is basically a function and a size.
161 class DiagnosticInfoStackSize : public DiagnosticInfo {
163 /// The function that is concerned by this stack size diagnostic.
165 /// The computed stack size.
169 /// \p The function that is concerned by this stack size diagnostic.
170 /// \p The computed stack size.
171 DiagnosticInfoStackSize(const Function &Fn, unsigned StackSize,
172 DiagnosticSeverity Severity = DS_Warning)
173 : DiagnosticInfo(DK_StackSize, Severity), Fn(Fn), StackSize(StackSize) {}
175 const Function &getFunction() const { return Fn; }
176 unsigned getStackSize() const { return StackSize; }
178 /// \see DiagnosticInfo::print.
179 void print(DiagnosticPrinter &DP) const override;
181 static bool classof(const DiagnosticInfo *DI) {
182 return DI->getKind() == DK_StackSize;
186 /// Diagnostic information for debug metadata version reporting.
187 /// This is basically a module and a version.
188 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
190 /// The module that is concerned by this debug metadata version diagnostic.
192 /// The actual metadata version.
193 unsigned MetadataVersion;
196 /// \p The module that is concerned by this debug metadata version diagnostic.
197 /// \p The actual metadata version.
198 DiagnosticInfoDebugMetadataVersion(const Module &M, unsigned MetadataVersion,
199 DiagnosticSeverity Severity = DS_Warning)
200 : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M),
201 MetadataVersion(MetadataVersion) {}
203 const Module &getModule() const { return M; }
204 unsigned getMetadataVersion() const { return MetadataVersion; }
206 /// \see DiagnosticInfo::print.
207 void print(DiagnosticPrinter &DP) const override;
209 static bool classof(const DiagnosticInfo *DI) {
210 return DI->getKind() == DK_DebugMetadataVersion;
214 /// Diagnostic information for the sample profiler.
215 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
217 DiagnosticInfoSampleProfile(StringRef FileName, unsigned LineNum,
219 DiagnosticSeverity Severity = DS_Error)
220 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
221 LineNum(LineNum), Msg(Msg) {}
222 DiagnosticInfoSampleProfile(StringRef FileName, const Twine &Msg,
223 DiagnosticSeverity Severity = DS_Error)
224 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
225 LineNum(0), Msg(Msg) {}
226 DiagnosticInfoSampleProfile(const Twine &Msg,
227 DiagnosticSeverity Severity = DS_Error)
228 : DiagnosticInfo(DK_SampleProfile, Severity), LineNum(0), Msg(Msg) {}
230 /// \see DiagnosticInfo::print.
231 void print(DiagnosticPrinter &DP) const override;
233 static bool classof(const DiagnosticInfo *DI) {
234 return DI->getKind() == DK_SampleProfile;
237 StringRef getFileName() const { return FileName; }
238 unsigned getLineNum() const { return LineNum; }
239 const Twine &getMsg() const { return Msg; }
242 /// Name of the input file associated with this diagnostic.
245 /// Line number where the diagnostic occurred. If 0, no line number will
246 /// be emitted in the message.
249 /// Message to report.
253 /// Diagnostic information for the PGO profiler.
254 class DiagnosticInfoPGOProfile : public DiagnosticInfo {
256 DiagnosticInfoPGOProfile(const char *FileName, const Twine &Msg,
257 DiagnosticSeverity Severity = DS_Error)
258 : DiagnosticInfo(DK_PGOProfile, Severity), FileName(FileName), Msg(Msg) {}
260 /// \see DiagnosticInfo::print.
261 void print(DiagnosticPrinter &DP) const override;
263 static bool classof(const DiagnosticInfo *DI) {
264 return DI->getKind() == DK_PGOProfile;
267 const char *getFileName() const { return FileName; }
268 const Twine &getMsg() const { return Msg; }
271 /// Name of the input file associated with this diagnostic.
272 const char *FileName;
274 /// Message to report.
278 /// Common features for diagnostics dealing with optimization remarks.
279 class DiagnosticInfoOptimizationBase : public DiagnosticInfo {
281 /// \p PassName is the name of the pass emitting this diagnostic.
282 /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
283 /// the location information to use in the diagnostic. If line table
284 /// information is available, the diagnostic will include the source code
285 /// location. \p Msg is the message to show. Note that this class does not
286 /// copy this message, so this reference must be valid for the whole life time
287 /// of the diagnostic.
288 DiagnosticInfoOptimizationBase(enum DiagnosticKind Kind,
289 enum DiagnosticSeverity Severity,
290 const char *PassName, const Function &Fn,
291 const DebugLoc &DLoc, const Twine &Msg)
292 : DiagnosticInfo(Kind, Severity), PassName(PassName), Fn(Fn), DLoc(DLoc),
295 /// \see DiagnosticInfo::print.
296 void print(DiagnosticPrinter &DP) const override;
298 /// Return true if this optimization remark is enabled by one of
299 /// of the LLVM command line flags (-pass-remarks, -pass-remarks-missed,
300 /// or -pass-remarks-analysis). Note that this only handles the LLVM
301 /// flags. We cannot access Clang flags from here (they are handled
302 /// in BackendConsumer::OptimizationRemarkHandler).
303 virtual bool isEnabled() const = 0;
305 /// Return true if location information is available for this diagnostic.
306 bool isLocationAvailable() const;
308 /// Return a string with the location information for this diagnostic
309 /// in the format "file:line:col". If location information is not available,
310 /// it returns "<unknown>:0:0".
311 const std::string getLocationStr() const;
313 /// Return location information for this diagnostic in three parts:
314 /// the source file name, line number and column.
315 void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
317 const char *getPassName() const { return PassName; }
318 const Function &getFunction() const { return Fn; }
319 const DebugLoc &getDebugLoc() const { return DLoc; }
320 const Twine &getMsg() const { return Msg; }
323 /// Name of the pass that triggers this report. If this matches the
324 /// regular expression given in -Rpass=regexp, then the remark will
326 const char *PassName;
328 /// Function where this diagnostic is triggered.
331 /// Debug location where this diagnostic is triggered.
334 /// Message to report.
338 /// Diagnostic information for applied optimization remarks.
339 class DiagnosticInfoOptimizationRemark : public DiagnosticInfoOptimizationBase {
341 /// \p PassName is the name of the pass emitting this diagnostic. If
342 /// this name matches the regular expression given in -Rpass=, then the
343 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
344 /// is being emitted. \p DLoc is the location information to use in the
345 /// diagnostic. If line table information is available, the diagnostic
346 /// will include the source code location. \p Msg is the message to show.
347 /// Note that this class does not copy this message, so this reference
348 /// must be valid for the whole life time of the diagnostic.
349 DiagnosticInfoOptimizationRemark(const char *PassName, const Function &Fn,
350 const DebugLoc &DLoc, const Twine &Msg)
351 : DiagnosticInfoOptimizationBase(DK_OptimizationRemark, DS_Remark,
352 PassName, Fn, DLoc, Msg) {}
354 static bool classof(const DiagnosticInfo *DI) {
355 return DI->getKind() == DK_OptimizationRemark;
358 /// \see DiagnosticInfoOptimizationBase::isEnabled.
359 bool isEnabled() const override;
362 /// Diagnostic information for missed-optimization remarks.
363 class DiagnosticInfoOptimizationRemarkMissed
364 : public DiagnosticInfoOptimizationBase {
366 /// \p PassName is the name of the pass emitting this diagnostic. If
367 /// this name matches the regular expression given in -Rpass-missed=, then the
368 /// 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
371 /// will include the source code location. \p Msg is the message to show.
372 /// Note that this class does not copy this message, so this reference
373 /// must be valid for the whole life time of the diagnostic.
374 DiagnosticInfoOptimizationRemarkMissed(const char *PassName,
376 const DebugLoc &DLoc, const Twine &Msg)
377 : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkMissed, DS_Remark,
378 PassName, Fn, DLoc, Msg) {}
380 static bool classof(const DiagnosticInfo *DI) {
381 return DI->getKind() == DK_OptimizationRemarkMissed;
384 /// \see DiagnosticInfoOptimizationBase::isEnabled.
385 bool isEnabled() const override;
388 /// Diagnostic information for optimization analysis remarks.
389 class DiagnosticInfoOptimizationRemarkAnalysis
390 : public DiagnosticInfoOptimizationBase {
392 /// \p PassName is the name of the pass emitting this diagnostic. If
393 /// this name matches the regular expression given in -Rpass-analysis=, then
394 /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
395 /// is being emitted. \p DLoc is the location information to use in the
396 /// diagnostic. If line table information is available, the diagnostic will
397 /// include the source code location. \p Msg is the message to show. Note that
398 /// this class does not copy this message, so this reference must be valid for
399 /// the whole life time of the diagnostic.
400 DiagnosticInfoOptimizationRemarkAnalysis(const char *PassName,
402 const DebugLoc &DLoc,
404 : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkAnalysis, DS_Remark,
405 PassName, Fn, DLoc, Msg) {}
407 static bool classof(const DiagnosticInfo *DI) {
408 return DI->getKind() == DK_OptimizationRemarkAnalysis;
411 /// \see DiagnosticInfoOptimizationBase::isEnabled.
412 bool isEnabled() const override;
415 DiagnosticInfoOptimizationRemarkAnalysis(enum DiagnosticKind Kind,
416 const char *PassName,
418 const DebugLoc &DLoc,
420 : DiagnosticInfoOptimizationBase(Kind, DS_Remark, PassName, Fn, DLoc,
424 /// Diagnostic information for optimization analysis remarks related to
425 /// floating-point non-commutativity.
426 class DiagnosticInfoOptimizationRemarkAnalysisFPCommute
427 : public DiagnosticInfoOptimizationRemarkAnalysis {
429 /// \p PassName is the name of the pass emitting this diagnostic. If
430 /// this name matches the regular expression given in -Rpass-analysis=, then
431 /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
432 /// is being emitted. \p DLoc is the location information to use in the
433 /// diagnostic. If line table information is available, the diagnostic will
434 /// include the source code location. \p Msg is the message to show. The
435 /// front-end will append its own message related to options that address
436 /// floating-point non-commutativity. Note that this class does not copy this
437 /// message, so this reference must be valid for the whole life time of the
439 DiagnosticInfoOptimizationRemarkAnalysisFPCommute(const char *PassName,
441 const DebugLoc &DLoc,
443 : DiagnosticInfoOptimizationRemarkAnalysis(
444 DK_OptimizationRemarkAnalysisFPCommute, PassName, Fn, DLoc, Msg) {}
446 static bool classof(const DiagnosticInfo *DI) {
447 return DI->getKind() == DK_OptimizationRemarkAnalysisFPCommute;
451 /// Diagnostic information for optimization analysis remarks related to
452 /// pointer aliasing.
453 class DiagnosticInfoOptimizationRemarkAnalysisAliasing
454 : public DiagnosticInfoOptimizationRemarkAnalysis {
456 /// \p PassName is the name of the pass emitting this diagnostic. If
457 /// this name matches the regular expression given in -Rpass-analysis=, then
458 /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
459 /// is being emitted. \p DLoc is the location information to use in the
460 /// diagnostic. If line table information is available, the diagnostic will
461 /// include the source code location. \p Msg is the message to show. The
462 /// front-end will append its own message related to options that address
463 /// pointer aliasing legality. Note that this class does not copy this
464 /// message, so this reference must be valid for the whole life time of the
466 DiagnosticInfoOptimizationRemarkAnalysisAliasing(const char *PassName,
468 const DebugLoc &DLoc,
470 : DiagnosticInfoOptimizationRemarkAnalysis(
471 DK_OptimizationRemarkAnalysisAliasing, PassName, Fn, DLoc, Msg) {}
473 static bool classof(const DiagnosticInfo *DI) {
474 return DI->getKind() == DK_OptimizationRemarkAnalysisAliasing;
478 /// Diagnostic information for machine IR parser.
479 class DiagnosticInfoMIRParser : public DiagnosticInfo {
480 const SMDiagnostic &Diagnostic;
483 DiagnosticInfoMIRParser(DiagnosticSeverity Severity,
484 const SMDiagnostic &Diagnostic)
485 : DiagnosticInfo(DK_MIRParser, Severity), Diagnostic(Diagnostic) {}
487 const SMDiagnostic &getDiagnostic() const { return Diagnostic; }
489 void print(DiagnosticPrinter &DP) const override;
491 static bool classof(const DiagnosticInfo *DI) {
492 return DI->getKind() == DK_MIRParser;
496 // Create wrappers for C Binding types (see CBindingWrapping.h).
497 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DiagnosticInfo, LLVMDiagnosticInfoRef)
499 /// Emit an optimization-applied message. \p PassName is the name of the pass
500 /// emitting the message. If -Rpass= is given and \p PassName matches the
501 /// regular expression in -Rpass, then the remark will be emitted. \p Fn is
502 /// the function triggering the remark, \p DLoc is the debug location where
503 /// the diagnostic is generated. \p Msg is the message string to use.
504 void emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
505 const Function &Fn, const DebugLoc &DLoc,
508 /// Emit an optimization-missed message. \p PassName is the name of the
509 /// pass emitting the message. If -Rpass-missed= is given and \p PassName
510 /// matches the regular expression in -Rpass, then the remark will be
511 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the
512 /// debug location where the diagnostic is generated. \p Msg is the
513 /// message string to use.
514 void emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
515 const Function &Fn, const DebugLoc &DLoc,
518 /// Emit an optimization analysis remark message. \p PassName is the name of
519 /// the pass emitting the message. If -Rpass-analysis= is given and \p
520 /// PassName matches the regular expression in -Rpass, then the remark will be
521 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the debug
522 /// location where the diagnostic is generated. \p Msg is the message string
524 void emitOptimizationRemarkAnalysis(LLVMContext &Ctx, const char *PassName,
525 const Function &Fn, const DebugLoc &DLoc,
528 /// Emit an optimization analysis remark related to messages about
529 /// floating-point non-commutativity. \p PassName is the name of the pass
530 /// emitting the message. If -Rpass-analysis= is given and \p PassName matches
531 /// the regular expression in -Rpass, then the remark will be emitted. \p Fn is
532 /// the function triggering the remark, \p DLoc is the debug location where the
533 /// diagnostic is generated. \p Msg is the message string to use.
534 void emitOptimizationRemarkAnalysisFPCommute(LLVMContext &Ctx,
535 const char *PassName,
537 const DebugLoc &DLoc,
540 /// Emit an optimization analysis remark related to messages about
541 /// pointer aliasing. \p PassName is the name of the pass emitting the message.
542 /// If -Rpass-analysis= is given and \p PassName matches the regular expression
543 /// in -Rpass, then the remark will be emitted. \p Fn is the function triggering
544 /// the remark, \p DLoc is the debug location where the diagnostic is generated.
545 /// \p Msg is the message string to use.
546 void emitOptimizationRemarkAnalysisAliasing(LLVMContext &Ctx,
547 const char *PassName,
549 const DebugLoc &DLoc,
552 /// Diagnostic information for optimization failures.
553 class DiagnosticInfoOptimizationFailure
554 : public DiagnosticInfoOptimizationBase {
556 /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
557 /// the location information to use in the diagnostic. If line table
558 /// information is available, the diagnostic will include the source code
559 /// location. \p Msg is the message to show. Note that this class does not
560 /// copy this message, so this reference must be valid for the whole life time
561 /// of the diagnostic.
562 DiagnosticInfoOptimizationFailure(const Function &Fn, const DebugLoc &DLoc,
564 : DiagnosticInfoOptimizationBase(DK_OptimizationFailure, DS_Warning,
565 nullptr, Fn, DLoc, Msg) {}
567 static bool classof(const DiagnosticInfo *DI) {
568 return DI->getKind() == DK_OptimizationFailure;
571 /// \see DiagnosticInfoOptimizationBase::isEnabled.
572 bool isEnabled() const override;
575 /// Emit a warning when loop vectorization is specified but fails. \p Fn is the
576 /// function triggering the warning, \p DLoc is the debug location where the
577 /// diagnostic is generated. \p Msg is the message string to use.
578 void emitLoopVectorizeWarning(LLVMContext &Ctx, const Function &Fn,
579 const DebugLoc &DLoc, const Twine &Msg);
581 /// Emit a warning when loop interleaving is specified but fails. \p Fn is the
582 /// function triggering the warning, \p DLoc is the debug location where the
583 /// diagnostic is generated. \p Msg is the message string to use.
584 void emitLoopInterleaveWarning(LLVMContext &Ctx, const Function &Fn,
585 const DebugLoc &DLoc, const Twine &Msg);
587 } // End namespace llvm