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"
26 // Forward declarations.
27 class DiagnosticPrinter;
30 class LLVMContextImpl;
35 /// \brief Defines the different supported severity of a diagnostic.
36 enum DiagnosticSeverity {
40 // A note attaches additional information to one of the previous diagnostic
45 /// \brief Defines the different supported kind of a diagnostic.
46 /// This enum should be extended with a new ID for each added concrete subclass.
50 DK_DebugMetadataVersion,
52 DK_OptimizationRemark,
53 DK_OptimizationRemarkMissed,
54 DK_OptimizationRemarkAnalysis,
55 DK_OptimizationFailure,
59 /// \brief Get the next available kind ID for a plugin diagnostic.
60 /// Each time this function is called, it returns a different number.
61 /// Therefore, a plugin that wants to "identify" its own classes
62 /// with a dynamic identifier, just have to use this method to get a new ID
63 /// and assign it to each of its classes.
64 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
65 /// Thus, the plugin identifiers will not conflict with the
66 /// DiagnosticKind values.
67 int getNextAvailablePluginDiagnosticKind();
69 /// \brief This is the base abstract class for diagnostic reporting in
71 /// The print method must be overloaded by the subclasses to print a
72 /// user-friendly message in the client of the backend (let us call it a
74 class DiagnosticInfo {
76 /// Kind defines the kind of report this is about.
77 const /* DiagnosticKind */ int Kind;
78 /// Severity gives the severity of the diagnostic.
79 const DiagnosticSeverity Severity;
82 DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
83 : Kind(Kind), Severity(Severity) {}
85 virtual ~DiagnosticInfo() {}
87 /* DiagnosticKind */ int getKind() const { return Kind; }
88 DiagnosticSeverity getSeverity() const { return Severity; }
90 /// Print using the given \p DP a user-friendly message.
91 /// This is the default message that will be printed to the user.
92 /// It is used when the frontend does not directly take advantage
93 /// of the information contained in fields of the subclasses.
94 /// The printed message must not end with '.' nor start with a severity
96 virtual void print(DiagnosticPrinter &DP) const = 0;
99 /// Diagnostic information for inline asm reporting.
100 /// This is basically a message and an optional location.
101 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
103 /// Optional line information. 0 if not set.
105 /// Message to be reported.
107 /// Optional origin of the problem.
108 const Instruction *Instr;
111 /// \p MsgStr is the message to be reported to the frontend.
112 /// This class does not copy \p MsgStr, therefore the reference must be valid
113 /// for the whole life time of the Diagnostic.
114 DiagnosticInfoInlineAsm(const Twine &MsgStr,
115 DiagnosticSeverity Severity = DS_Error)
116 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
119 /// \p LocCookie if non-zero gives the line number for this report.
120 /// \p MsgStr gives the message.
121 /// This class does not copy \p MsgStr, therefore the reference must be valid
122 /// for the whole life time of the Diagnostic.
123 DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
124 DiagnosticSeverity Severity = DS_Error)
125 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
126 MsgStr(MsgStr), Instr(nullptr) {}
128 /// \p Instr gives the original instruction that triggered the diagnostic.
129 /// \p MsgStr gives the message.
130 /// This class does not copy \p MsgStr, therefore the reference must be valid
131 /// for the whole life time of the Diagnostic.
133 DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
134 DiagnosticSeverity Severity = DS_Error);
136 unsigned getLocCookie() const { return LocCookie; }
137 const Twine &getMsgStr() const { return MsgStr; }
138 const Instruction *getInstruction() const { return Instr; }
140 /// \see DiagnosticInfo::print.
141 void print(DiagnosticPrinter &DP) const override;
143 static bool classof(const DiagnosticInfo *DI) {
144 return DI->getKind() == DK_InlineAsm;
148 /// Diagnostic information for stack size reporting.
149 /// This is basically a function and a size.
150 class DiagnosticInfoStackSize : public DiagnosticInfo {
152 /// The function that is concerned by this stack size diagnostic.
154 /// The computed stack size.
158 /// \p The function that is concerned by this stack size diagnostic.
159 /// \p The computed stack size.
160 DiagnosticInfoStackSize(const Function &Fn, unsigned StackSize,
161 DiagnosticSeverity Severity = DS_Warning)
162 : DiagnosticInfo(DK_StackSize, Severity), Fn(Fn), StackSize(StackSize) {}
164 const Function &getFunction() const { return Fn; }
165 unsigned getStackSize() const { return StackSize; }
167 /// \see DiagnosticInfo::print.
168 void print(DiagnosticPrinter &DP) const override;
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 static bool classof(const DiagnosticInfo *DI) {
199 return DI->getKind() == DK_DebugMetadataVersion;
203 /// Diagnostic information for the sample profiler.
204 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
206 DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
208 DiagnosticSeverity Severity = DS_Error)
209 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
210 LineNum(LineNum), Msg(Msg) {}
211 DiagnosticInfoSampleProfile(const char *FileName, const Twine &Msg,
212 DiagnosticSeverity Severity = DS_Error)
213 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
214 LineNum(0), Msg(Msg) {}
215 DiagnosticInfoSampleProfile(const Twine &Msg,
216 DiagnosticSeverity Severity = DS_Error)
217 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(nullptr),
218 LineNum(0), Msg(Msg) {}
220 /// \see DiagnosticInfo::print.
221 void print(DiagnosticPrinter &DP) const override;
223 static bool classof(const DiagnosticInfo *DI) {
224 return DI->getKind() == DK_SampleProfile;
227 const char *getFileName() const { return FileName; }
228 unsigned getLineNum() const { return LineNum; }
229 const Twine &getMsg() const { return Msg; }
232 /// Name of the input file associated with this diagnostic.
233 const char *FileName;
235 /// Line number where the diagnostic occurred. If 0, no line number will
236 /// be emitted in the message.
239 /// Message to report.
243 /// Common features for diagnostics dealing with optimization remarks.
244 class DiagnosticInfoOptimizationBase : public DiagnosticInfo {
246 /// \p PassName is the name of the pass emitting this diagnostic.
247 /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
248 /// the location information to use in the diagnostic. If line table
249 /// information is available, the diagnostic will include the source code
250 /// location. \p Msg is the message to show. Note that this class does not
251 /// copy this message, so this reference must be valid for the whole life time
252 /// of the diagnostic.
253 DiagnosticInfoOptimizationBase(enum DiagnosticKind Kind,
254 enum DiagnosticSeverity Severity,
255 const char *PassName, const Function &Fn,
256 const DebugLoc &DLoc, const Twine &Msg)
257 : DiagnosticInfo(Kind, Severity), PassName(PassName), Fn(Fn), DLoc(DLoc),
260 /// \see DiagnosticInfo::print.
261 void print(DiagnosticPrinter &DP) const override;
263 static bool classof(const DiagnosticInfo *DI) {
264 return DI->getKind() == DK_OptimizationRemark;
267 /// Return true if this optimization remark is enabled by one of
268 /// of the LLVM command line flags (-pass-remarks, -pass-remarks-missed,
269 /// or -pass-remarks-analysis). Note that this only handles the LLVM
270 /// flags. We cannot access Clang flags from here (they are handled
271 /// in BackendConsumer::OptimizationRemarkHandler).
272 virtual bool isEnabled() const = 0;
274 /// Return true if location information is available for this diagnostic.
275 bool isLocationAvailable() const;
277 /// Return a string with the location information for this diagnostic
278 /// in the format "file:line:col". If location information is not available,
279 /// it returns "<unknown>:0:0".
280 const std::string getLocationStr() const;
282 /// Return location information for this diagnostic in three parts:
283 /// the source file name, line number and column.
284 void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
286 const char *getPassName() const { return PassName; }
287 const Function &getFunction() const { return Fn; }
288 const DebugLoc &getDebugLoc() const { return DLoc; }
289 const Twine &getMsg() const { return Msg; }
292 /// Name of the pass that triggers this report. If this matches the
293 /// regular expression given in -Rpass=regexp, then the remark will
295 const char *PassName;
297 /// Function where this diagnostic is triggered.
300 /// Debug location where this diagnostic is triggered.
303 /// Message to report.
307 /// Diagnostic information for applied optimization remarks.
308 class DiagnosticInfoOptimizationRemark : public DiagnosticInfoOptimizationBase {
310 /// \p PassName is the name of the pass emitting this diagnostic. If
311 /// this name matches the regular expression given in -Rpass=, then the
312 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
313 /// is being emitted. \p DLoc is the location information to use in the
314 /// diagnostic. If line table information is available, the diagnostic
315 /// will include the source code location. \p Msg is the message to show.
316 /// Note that this class does not copy this message, so this reference
317 /// must be valid for the whole life time of the diagnostic.
318 DiagnosticInfoOptimizationRemark(const char *PassName, const Function &Fn,
319 const DebugLoc &DLoc, const Twine &Msg)
320 : DiagnosticInfoOptimizationBase(DK_OptimizationRemark, DS_Remark,
321 PassName, Fn, DLoc, Msg) {}
323 static bool classof(const DiagnosticInfo *DI) {
324 return DI->getKind() == DK_OptimizationRemark;
327 /// \see DiagnosticInfoOptimizationBase::isEnabled.
328 bool isEnabled() const override;
331 /// Diagnostic information for missed-optimization remarks.
332 class DiagnosticInfoOptimizationRemarkMissed
333 : public DiagnosticInfoOptimizationBase {
335 /// \p PassName is the name of the pass emitting this diagnostic. If
336 /// this name matches the regular expression given in -Rpass-missed=, then the
337 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
338 /// is being emitted. \p DLoc is the location information to use in the
339 /// diagnostic. If line table information is available, the diagnostic
340 /// will include the source code location. \p Msg is the message to show.
341 /// Note that this class does not copy this message, so this reference
342 /// must be valid for the whole life time of the diagnostic.
343 DiagnosticInfoOptimizationRemarkMissed(const char *PassName,
345 const DebugLoc &DLoc, const Twine &Msg)
346 : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkMissed, DS_Remark,
347 PassName, Fn, DLoc, Msg) {}
349 static bool classof(const DiagnosticInfo *DI) {
350 return DI->getKind() == DK_OptimizationRemarkMissed;
353 /// \see DiagnosticInfoOptimizationBase::isEnabled.
354 bool isEnabled() const override;
357 /// Diagnostic information for optimization analysis remarks.
358 class DiagnosticInfoOptimizationRemarkAnalysis
359 : public DiagnosticInfoOptimizationBase {
361 /// \p PassName is the name of the pass emitting this diagnostic. If
362 /// this name matches the regular expression given in -Rpass-analysis=, then
363 /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
364 /// is being emitted. \p DLoc is the location information to use in the
365 /// diagnostic. If line table information is available, the diagnostic will
366 /// include the source code location. \p Msg is the message to show. Note that
367 /// this class does not copy this message, so this reference must be valid for
368 /// the whole life time of the diagnostic.
369 DiagnosticInfoOptimizationRemarkAnalysis(const char *PassName,
371 const DebugLoc &DLoc,
373 : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkAnalysis, DS_Remark,
374 PassName, Fn, DLoc, Msg) {}
376 static bool classof(const DiagnosticInfo *DI) {
377 return DI->getKind() == DK_OptimizationRemarkAnalysis;
380 /// \see DiagnosticInfoOptimizationBase::isEnabled.
381 bool isEnabled() const override;
384 // Create wrappers for C Binding types (see CBindingWrapping.h).
385 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DiagnosticInfo, LLVMDiagnosticInfoRef)
387 /// Emit an optimization-applied message. \p PassName is the name of the pass
388 /// emitting the message. If -Rpass= is given and \p PassName matches the
389 /// regular expression in -Rpass, then the remark will be emitted. \p Fn is
390 /// the function triggering the remark, \p DLoc is the debug location where
391 /// the diagnostic is generated. \p Msg is the message string to use.
392 void emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
393 const Function &Fn, const DebugLoc &DLoc,
396 /// Emit an optimization-missed message. \p PassName is the name of the
397 /// pass emitting the message. If -Rpass-missed= is given and \p PassName
398 /// matches the regular expression in -Rpass, then the remark will be
399 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the
400 /// debug location where the diagnostic is generated. \p Msg is the
401 /// message string to use.
402 void emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
403 const Function &Fn, const DebugLoc &DLoc,
406 /// Emit an optimization analysis remark message. \p PassName is the name of
407 /// the pass emitting the message. If -Rpass-analysis= is given and \p
408 /// PassName matches the regular expression in -Rpass, then the remark will be
409 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the debug
410 /// location where the diagnostic is generated. \p Msg is the message string
412 void emitOptimizationRemarkAnalysis(LLVMContext &Ctx, const char *PassName,
413 const Function &Fn, const DebugLoc &DLoc,
416 /// Diagnostic information for optimization failures.
417 class DiagnosticInfoOptimizationFailure
418 : public DiagnosticInfoOptimizationBase {
420 /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
421 /// the location information to use in the diagnostic. If line table
422 /// information is available, the diagnostic will include the source code
423 /// location. \p Msg is the message to show. Note that this class does not
424 /// copy this message, so this reference must be valid for the whole life time
425 /// of the diagnostic.
426 DiagnosticInfoOptimizationFailure(const Function &Fn, const DebugLoc &DLoc,
428 : DiagnosticInfoOptimizationBase(DK_OptimizationFailure, DS_Warning,
429 nullptr, Fn, DLoc, Msg) {}
431 static bool classof(const DiagnosticInfo *DI) {
432 return DI->getKind() == DK_OptimizationFailure;
435 /// \see DiagnosticInfoOptimizationBase::isEnabled.
436 bool isEnabled() const override;
439 /// Emit a warning when loop vectorization is specified but fails. \p Fn is the
440 /// function triggering the warning, \p DLoc is the debug location where the
441 /// diagnostic is generated. \p Msg is the message string to use.
442 void emitLoopVectorizeWarning(LLVMContext &Ctx, const Function &Fn,
443 const DebugLoc &DLoc, const Twine &Msg);
445 /// Emit a warning when loop interleaving is specified but fails. \p Fn is the
446 /// function triggering the warning, \p DLoc is the debug location where the
447 /// diagnostic is generated. \p Msg is the message string to use.
448 void emitLoopInterleaveWarning(LLVMContext &Ctx, const Function &Fn,
449 const DebugLoc &DLoc, const Twine &Msg);
451 } // End namespace llvm