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,
54 DK_OptimizationWarning,
58 /// \brief Get the next available kind ID for a plugin diagnostic.
59 /// Each time this function is called, it returns a different number.
60 /// Therefore, a plugin that wants to "identify" its own classes
61 /// with a dynamic identifier, just have to use this method to get a new ID
62 /// and assign it to each of its classes.
63 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
64 /// Thus, the plugin identifiers will not conflict with the
65 /// DiagnosticKind values.
66 int getNextAvailablePluginDiagnosticKind();
68 /// \brief This is the base abstract class for diagnostic reporting in
70 /// The print method must be overloaded by the subclasses to print a
71 /// user-friendly message in the client of the backend (let us call it a
73 class DiagnosticInfo {
75 /// Kind defines the kind of report this is about.
76 const /* DiagnosticKind */ int Kind;
77 /// Severity gives the severity of the diagnostic.
78 const DiagnosticSeverity Severity;
81 DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
82 : Kind(Kind), Severity(Severity) {}
84 virtual ~DiagnosticInfo() {}
86 /* DiagnosticKind */ int getKind() const { return Kind; }
87 DiagnosticSeverity getSeverity() const { return Severity; }
89 /// Print using the given \p DP a user-friendly message.
90 /// This is the default message that will be printed to the user.
91 /// It is used when the frontend does not directly take advantage
92 /// of the information contained in fields of the subclasses.
93 /// The printed message must not end with '.' nor start with a severity
95 virtual void print(DiagnosticPrinter &DP) const = 0;
98 /// Diagnostic information for inline asm reporting.
99 /// This is basically a message and an optional location.
100 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
102 /// Optional line information. 0 if not set.
104 /// Message to be reported.
106 /// Optional origin of the problem.
107 const Instruction *Instr;
110 /// \p MsgStr is the message to be reported to the frontend.
111 /// This class does not copy \p MsgStr, therefore the reference must be valid
112 /// for the whole life time of the Diagnostic.
113 DiagnosticInfoInlineAsm(const Twine &MsgStr,
114 DiagnosticSeverity Severity = DS_Error)
115 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
118 /// \p LocCookie if non-zero gives the line number for this report.
119 /// \p MsgStr gives the message.
120 /// This class does not copy \p MsgStr, therefore the reference must be valid
121 /// for the whole life time of the Diagnostic.
122 DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
123 DiagnosticSeverity Severity = DS_Error)
124 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
125 MsgStr(MsgStr), Instr(nullptr) {}
127 /// \p Instr gives the original instruction that triggered the diagnostic.
128 /// \p MsgStr gives the message.
129 /// This class does not copy \p MsgStr, therefore the reference must be valid
130 /// for the whole life time of the Diagnostic.
132 DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
133 DiagnosticSeverity Severity = DS_Error);
135 unsigned getLocCookie() const { return LocCookie; }
136 const Twine &getMsgStr() const { return MsgStr; }
137 const Instruction *getInstruction() const { return Instr; }
139 /// \see DiagnosticInfo::print.
140 void print(DiagnosticPrinter &DP) const override;
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 static bool classof(const DiagnosticInfo *DI) {
170 return DI->getKind() == DK_StackSize;
174 /// Diagnostic information for debug metadata version reporting.
175 /// This is basically a module and a version.
176 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
178 /// The module that is concerned by this debug metadata version diagnostic.
180 /// The actual metadata version.
181 unsigned MetadataVersion;
184 /// \p The module that is concerned by this debug metadata version diagnostic.
185 /// \p The actual metadata version.
186 DiagnosticInfoDebugMetadataVersion(const Module &M, unsigned MetadataVersion,
187 DiagnosticSeverity Severity = DS_Warning)
188 : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M),
189 MetadataVersion(MetadataVersion) {}
191 const Module &getModule() const { return M; }
192 unsigned getMetadataVersion() const { return MetadataVersion; }
194 /// \see DiagnosticInfo::print.
195 void print(DiagnosticPrinter &DP) const override;
197 static bool classof(const DiagnosticInfo *DI) {
198 return DI->getKind() == DK_DebugMetadataVersion;
202 /// Diagnostic information for the sample profiler.
203 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
205 DiagnosticInfoSampleProfile(const char *FileName, unsigned LineNum,
207 DiagnosticSeverity Severity = DS_Error)
208 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
209 LineNum(LineNum), Msg(Msg) {}
210 DiagnosticInfoSampleProfile(const char *FileName, const Twine &Msg,
211 DiagnosticSeverity Severity = DS_Error)
212 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
213 LineNum(0), Msg(Msg) {}
214 DiagnosticInfoSampleProfile(const Twine &Msg,
215 DiagnosticSeverity Severity = DS_Error)
216 : DiagnosticInfo(DK_SampleProfile, Severity), FileName(nullptr),
217 LineNum(0), Msg(Msg) {}
219 /// \see DiagnosticInfo::print.
220 void print(DiagnosticPrinter &DP) const override;
222 static bool classof(const DiagnosticInfo *DI) {
223 return DI->getKind() == DK_SampleProfile;
226 const char *getFileName() const { return FileName; }
227 unsigned getLineNum() const { return LineNum; }
228 const Twine &getMsg() const { return Msg; }
231 /// Name of the input file associated with this diagnostic.
232 const char *FileName;
234 /// Line number where the diagnostic occurred. If 0, no line number will
235 /// be emitted in the message.
238 /// Message to report.
242 /// Common features for diagnostics dealing with optimization remarks.
243 class DiagnosticInfoOptimizationBase : public DiagnosticInfo {
245 /// \p PassName is the name of the pass emitting this diagnostic.
246 /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
247 /// the location information to use in the diagnostic. If line table
248 /// information is available, the diagnostic will include the source code
249 /// location. \p Msg is the message to show. Note that this class does not
250 /// copy this message, so this reference must be valid for the whole life time
251 /// of the diagnostic.
252 DiagnosticInfoOptimizationBase(enum DiagnosticKind Kind,
253 enum DiagnosticSeverity Severity,
254 const char *PassName, const Function &Fn,
255 const DebugLoc &DLoc, const Twine &Msg)
256 : DiagnosticInfo(Kind, Severity), PassName(PassName), Fn(Fn), DLoc(DLoc),
259 /// \see DiagnosticInfo::print.
260 void print(DiagnosticPrinter &DP) const override;
262 static bool classof(const DiagnosticInfo *DI) {
263 return DI->getKind() == DK_OptimizationRemark;
266 /// Return true if this optimization remark is enabled by one of
267 /// of the LLVM command line flags (-pass-remarks, -pass-remarks-missed,
268 /// or -pass-remarks-analysis). Note that this only handles the LLVM
269 /// flags. We cannot access Clang flags from here (they are handled
270 /// in BackendConsumer::OptimizationRemarkHandler).
271 virtual bool isEnabled() const = 0;
273 /// Return true if location information is available for this diagnostic.
274 bool isLocationAvailable() const;
276 /// Return a string with the location information for this diagnostic
277 /// in the format "file:line:col". If location information is not available,
278 /// it returns "<unknown>:0:0".
279 const std::string getLocationStr() const;
281 /// Return location information for this diagnostic in three parts:
282 /// the source file name, line number and column.
283 void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
285 const char *getPassName() const { return PassName; }
286 const Function &getFunction() const { return Fn; }
287 const DebugLoc &getDebugLoc() const { return DLoc; }
288 const Twine &getMsg() const { return Msg; }
291 /// Name of the pass that triggers this report. If this matches the
292 /// regular expression given in -Rpass=regexp, then the remark will
294 const char *PassName;
296 /// Function where this diagnostic is triggered.
299 /// Debug location where this diagnostic is triggered.
302 /// Message to report.
306 /// Diagnostic information for applied optimization remarks.
307 class DiagnosticInfoOptimizationRemark : public DiagnosticInfoOptimizationBase {
309 /// \p PassName is the name of the pass emitting this diagnostic. If
310 /// this name matches the regular expression given in -Rpass=, then the
311 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
312 /// is being emitted. \p DLoc is the location information to use in the
313 /// diagnostic. If line table information is available, the diagnostic
314 /// will include the source code location. \p Msg is the message to show.
315 /// Note that this class does not copy this message, so this reference
316 /// must be valid for the whole life time of the diagnostic.
317 DiagnosticInfoOptimizationRemark(const char *PassName, const Function &Fn,
318 const DebugLoc &DLoc, const Twine &Msg)
319 : DiagnosticInfoOptimizationBase(DK_OptimizationRemark, DS_Remark,
320 PassName, Fn, DLoc, Msg) {}
322 static bool classof(const DiagnosticInfo *DI) {
323 return DI->getKind() == DK_OptimizationRemark;
326 /// \see DiagnosticInfoOptimizationBase::isEnabled.
327 virtual bool isEnabled() const override;
330 /// Diagnostic information for missed-optimization remarks.
331 class DiagnosticInfoOptimizationRemarkMissed
332 : public DiagnosticInfoOptimizationBase {
334 /// \p PassName is the name of the pass emitting this diagnostic. If
335 /// this name matches the regular expression given in -Rpass-missed=, then the
336 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
337 /// is being emitted. \p DLoc is the location information to use in the
338 /// diagnostic. If line table information is available, the diagnostic
339 /// will include the source code location. \p Msg is the message to show.
340 /// Note that this class does not copy this message, so this reference
341 /// must be valid for the whole life time of the diagnostic.
342 DiagnosticInfoOptimizationRemarkMissed(const char *PassName,
344 const DebugLoc &DLoc, const Twine &Msg)
345 : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkMissed, DS_Remark,
346 PassName, Fn, DLoc, Msg) {}
348 static bool classof(const DiagnosticInfo *DI) {
349 return DI->getKind() == DK_OptimizationRemarkMissed;
352 /// \see DiagnosticInfoOptimizationBase::isEnabled.
353 virtual bool isEnabled() const override;
356 /// Diagnostic information for optimization analysis remarks.
357 class DiagnosticInfoOptimizationRemarkAnalysis
358 : public DiagnosticInfoOptimizationBase {
360 /// \p PassName is the name of the pass emitting this diagnostic. If
361 /// this name matches the regular expression given in -Rpass-analysis=, then
362 /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
363 /// is being emitted. \p DLoc is the location information to use in the
364 /// diagnostic. If line table information is available, the diagnostic will
365 /// include the source code location. \p Msg is the message to show. Note that
366 /// this class does not copy this message, so this reference must be valid for
367 /// the whole life time of the diagnostic.
368 DiagnosticInfoOptimizationRemarkAnalysis(const char *PassName,
370 const DebugLoc &DLoc,
372 : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkAnalysis, DS_Remark,
373 PassName, Fn, DLoc, Msg) {}
375 static bool classof(const DiagnosticInfo *DI) {
376 return DI->getKind() == DK_OptimizationRemarkAnalysis;
379 /// \see DiagnosticInfoOptimizationBase::isEnabled.
380 virtual bool isEnabled() const override;
383 // Create wrappers for C Binding types (see CBindingWrapping.h).
384 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DiagnosticInfo, LLVMDiagnosticInfoRef)
386 /// Emit an optimization-applied message. \p PassName is the name of the pass
387 /// emitting the message. If -Rpass= is given and \p PassName matches the
388 /// regular expression in -Rpass, then the remark will be emitted. \p Fn is
389 /// the function triggering the remark, \p DLoc is the debug location where
390 /// the diagnostic is generated. \p Msg is the message string to use.
391 void emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
392 const Function &Fn, const DebugLoc &DLoc,
395 /// Emit an optimization-missed message. \p PassName is the name of the
396 /// pass emitting the message. If -Rpass-missed= is given and \p PassName
397 /// matches the regular expression in -Rpass, then the remark will be
398 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the
399 /// debug location where the diagnostic is generated. \p Msg is the
400 /// message string to use.
401 void emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
402 const Function &Fn, const DebugLoc &DLoc,
405 /// Emit an optimization analysis remark message. \p PassName is the name of
406 /// the pass emitting the message. If -Rpass-analysis= is given and \p
407 /// PassName matches the regular expression in -Rpass, then the remark will be
408 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the debug
409 /// location where the diagnostic is generated. \p Msg is the message string
411 void emitOptimizationRemarkAnalysis(LLVMContext &Ctx, const char *PassName,
412 const Function &Fn, const DebugLoc &DLoc,
415 /// Diagnostic information for optimization warnings.
416 class DiagnosticInfoOptimizationWarning
417 : public DiagnosticInfoOptimizationBase {
419 /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
420 /// the location information to use in the diagnostic. If line table
421 /// information is available, the diagnostic will include the source code
422 /// location. \p Msg is the message to show. Note that this class does not
423 /// copy this message, so this reference must be valid for the whole life time
424 /// of the diagnostic.
425 DiagnosticInfoOptimizationWarning(const Function &Fn, const DebugLoc &DLoc,
427 : DiagnosticInfoOptimizationBase(DK_OptimizationWarning, DS_Warning,
428 nullptr, Fn, DLoc, Msg) {}
430 static bool classof(const DiagnosticInfo *DI) {
431 return DI->getKind() == DK_OptimizationWarning;
434 /// \see DiagnosticInfoOptimizationBase::isEnabled.
435 virtual bool isEnabled() const override;
438 /// Emit a warning when loop vectorization is specified but fails. \p Fn is the
439 /// function triggering the warning, \p DLoc is the debug location where the
440 /// diagnostic is generated. \p Msg is the message string to use.
441 void emitLoopVectorizeWarning(LLVMContext &Ctx, const Function &Fn,
442 const DebugLoc &DLoc, const Twine &Msg);
444 /// Emit a warning when loop interleaving is specified but fails. \p Fn is the
445 /// function triggering the warning, \p DLoc is the debug location where the
446 /// diagnostic is generated. \p Msg is the message string to use.
447 void emitLoopInterleaveWarning(LLVMContext &Ctx, const Function &Fn,
448 const DebugLoc &DLoc, const Twine &Msg);
450 } // End namespace llvm