1 //===- llvm/Support/DiagnosticInfo.cpp - Diagnostic Definitions -*- 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 defines the different classes involved in low level diagnostics.
12 // Diagnostics reporting is still done as part of the LLVMContext.
13 //===----------------------------------------------------------------------===//
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DebugInfo.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Instruction.h"
23 #include "llvm/IR/Metadata.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Regex.h"
34 /// \brief Regular expression corresponding to the value given in one of the
35 /// -pass-remarks* command line flags. Passes whose name matches this regexp
36 /// will emit a diagnostic when calling the associated diagnostic function
37 /// (emitOptimizationRemark, emitOptimizationRemarkMissed or
38 /// emitOptimizationRemarkAnalysis).
39 struct PassRemarksOpt {
40 std::shared_ptr<Regex> Pattern;
42 void operator=(const std::string &Val) {
43 // Create a regexp object to match pass names for emitOptimizationRemark.
45 Pattern = std::make_shared<Regex>(Val);
46 std::string RegexError;
47 if (!Pattern->isValid(RegexError))
48 report_fatal_error("Invalid regular expression '" + Val +
49 "' in -pass-remarks: " + RegexError,
55 static PassRemarksOpt PassRemarksOptLoc;
56 static PassRemarksOpt PassRemarksMissedOptLoc;
57 static PassRemarksOpt PassRemarksAnalysisOptLoc;
60 // Command line flag to enable emitOptimizationRemark()
61 static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
62 PassRemarks("pass-remarks", cl::value_desc("pattern"),
63 cl::desc("Enable optimization remarks from passes whose name match "
64 "the given regular expression"),
65 cl::Hidden, cl::location(PassRemarksOptLoc), cl::ValueRequired,
68 // -pass-remarks-missed
69 // Command line flag to enable emitOptimizationRemarkMissed()
70 static cl::opt<PassRemarksOpt, true, cl::parser<std::string>> PassRemarksMissed(
71 "pass-remarks-missed", cl::value_desc("pattern"),
72 cl::desc("Enable missed optimization remarks from passes whose name match "
73 "the given regular expression"),
74 cl::Hidden, cl::location(PassRemarksMissedOptLoc), cl::ValueRequired,
77 // -pass-remarks-analysis
78 // Command line flag to enable emitOptimizationRemarkAnalysis()
79 static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
81 "pass-remarks-analysis", cl::value_desc("pattern"),
83 "Enable optimization analysis remarks from passes whose name match "
84 "the given regular expression"),
85 cl::Hidden, cl::location(PassRemarksAnalysisOptLoc), cl::ValueRequired,
89 int llvm::getNextAvailablePluginDiagnosticKind() {
90 static std::atomic<int> PluginKindID(DK_FirstPluginKind);
91 return ++PluginKindID;
94 const char *DiagnosticInfo::AlwaysPrint = "";
96 DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
98 DiagnosticSeverity Severity)
99 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
101 if (const MDNode *SrcLoc = I.getMetadata("srcloc")) {
102 if (SrcLoc->getNumOperands() != 0)
104 mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0)))
105 LocCookie = CI->getZExtValue();
109 void DiagnosticInfoInlineAsm::print(DiagnosticPrinter &DP) const {
112 DP << " at line " << getLocCookie();
115 void DiagnosticInfoStackSize::print(DiagnosticPrinter &DP) const {
116 DP << "stack size limit exceeded (" << getStackSize() << ") in "
120 void DiagnosticInfoDebugMetadataVersion::print(DiagnosticPrinter &DP) const {
121 DP << "ignoring debug info with an invalid version (" << getMetadataVersion()
122 << ") in " << getModule();
125 void DiagnosticInfoSampleProfile::print(DiagnosticPrinter &DP) const {
126 if (getFileName() && getLineNum() > 0)
127 DP << getFileName() << ":" << getLineNum() << ": ";
128 else if (getFileName())
129 DP << getFileName() << ": ";
133 bool DiagnosticInfoOptimizationBase::isLocationAvailable() const {
134 return getDebugLoc();
137 void DiagnosticInfoOptimizationBase::getLocation(StringRef *Filename,
139 unsigned *Column) const {
140 DILocation *L = getDebugLoc();
141 assert(L != nullptr && "debug location is invalid");
142 *Filename = L->getFilename();
143 *Line = L->getLine();
144 *Column = L->getColumn();
147 const std::string DiagnosticInfoOptimizationBase::getLocationStr() const {
148 StringRef Filename("<unknown>");
151 if (isLocationAvailable())
152 getLocation(&Filename, &Line, &Column);
153 return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
156 void DiagnosticInfoOptimizationBase::print(DiagnosticPrinter &DP) const {
157 DP << getLocationStr() << ": " << getMsg();
160 bool DiagnosticInfoOptimizationRemark::isEnabled() const {
161 return PassRemarksOptLoc.Pattern &&
162 PassRemarksOptLoc.Pattern->match(getPassName());
165 bool DiagnosticInfoOptimizationRemarkMissed::isEnabled() const {
166 return PassRemarksMissedOptLoc.Pattern &&
167 PassRemarksMissedOptLoc.Pattern->match(getPassName());
170 bool DiagnosticInfoOptimizationRemarkAnalysis::isEnabled() const {
171 return getPassName() == DiagnosticInfo::AlwaysPrint ||
172 (PassRemarksAnalysisOptLoc.Pattern &&
173 PassRemarksAnalysisOptLoc.Pattern->match(getPassName()));
176 void DiagnosticInfoMIRParser::print(DiagnosticPrinter &DP) const {
180 void llvm::emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
181 const Function &Fn, const DebugLoc &DLoc,
183 Ctx.diagnose(DiagnosticInfoOptimizationRemark(PassName, Fn, DLoc, Msg));
186 void llvm::emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
188 const DebugLoc &DLoc,
190 Ctx.diagnose(DiagnosticInfoOptimizationRemarkMissed(PassName, Fn, DLoc, Msg));
193 void llvm::emitOptimizationRemarkAnalysis(LLVMContext &Ctx,
194 const char *PassName,
196 const DebugLoc &DLoc,
199 DiagnosticInfoOptimizationRemarkAnalysis(PassName, Fn, DLoc, Msg));
202 void llvm::emitOptimizationRemarkAnalysisFPCommute(LLVMContext &Ctx,
203 const char *PassName,
205 const DebugLoc &DLoc,
207 Ctx.diagnose(DiagnosticInfoOptimizationRemarkAnalysisFPCommute(PassName, Fn,
211 void llvm::emitOptimizationRemarkAnalysisAliasing(LLVMContext &Ctx,
212 const char *PassName,
214 const DebugLoc &DLoc,
216 Ctx.diagnose(DiagnosticInfoOptimizationRemarkAnalysisAliasing(PassName, Fn,
220 bool DiagnosticInfoOptimizationFailure::isEnabled() const {
221 // Only print warnings.
222 return getSeverity() == DS_Warning;
225 void llvm::emitLoopVectorizeWarning(LLVMContext &Ctx, const Function &Fn,
226 const DebugLoc &DLoc, const Twine &Msg) {
227 Ctx.diagnose(DiagnosticInfoOptimizationFailure(
228 Fn, DLoc, Twine("loop not vectorized: " + Msg)));
231 void llvm::emitLoopInterleaveWarning(LLVMContext &Ctx, const Function &Fn,
232 const DebugLoc &DLoc, const Twine &Msg) {
233 Ctx.diagnose(DiagnosticInfoOptimizationFailure(
234 Fn, DLoc, Twine("loop not interleaved: " + Msg)));