1 //===- llvm/Support/DiagnosticInfo.cpp - Diagnostic Definitions -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the different classes involved in low level diagnostics.
11 // Diagnostics reporting is still done as part of the LLVMContext.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/DiagnosticInfo.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DebugInfoMetadata.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/DiagnosticPrinter.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/IR/Instruction.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/Metadata.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/IR/Value.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/Regex.h"
37 #include "llvm/Support/ScopedPrinter.h"
38 #include "llvm/Support/raw_ostream.h"
46 int llvm::getNextAvailablePluginDiagnosticKind() {
47 static std::atomic<int> PluginKindID(DK_FirstPluginKind);
48 return ++PluginKindID;
51 const char *OptimizationRemarkAnalysis::AlwaysPrint = "";
53 DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
55 DiagnosticSeverity Severity)
56 : DiagnosticInfo(DK_InlineAsm, Severity), MsgStr(MsgStr), Instr(&I) {
57 if (const MDNode *SrcLoc = I.getMetadata("srcloc")) {
58 if (SrcLoc->getNumOperands() != 0)
60 mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0)))
61 LocCookie = CI->getZExtValue();
65 void DiagnosticInfoInlineAsm::print(DiagnosticPrinter &DP) const {
68 DP << " at line " << getLocCookie();
71 void DiagnosticInfoResourceLimit::print(DiagnosticPrinter &DP) const {
72 DP << getResourceName() << " limit";
74 if (getResourceLimit() != 0)
75 DP << " of " << getResourceLimit();
77 DP << " exceeded (" << getResourceSize() << ") in " << getFunction();
80 void DiagnosticInfoDebugMetadataVersion::print(DiagnosticPrinter &DP) const {
81 DP << "ignoring debug info with an invalid version (" << getMetadataVersion()
82 << ") in " << getModule();
85 void DiagnosticInfoIgnoringInvalidDebugMetadata::print(
86 DiagnosticPrinter &DP) const {
87 DP << "ignoring invalid debug info in " << getModule().getModuleIdentifier();
90 void DiagnosticInfoSampleProfile::print(DiagnosticPrinter &DP) const {
91 if (!FileName.empty()) {
94 DP << ":" << getLineNum();
100 void DiagnosticInfoPGOProfile::print(DiagnosticPrinter &DP) const {
102 DP << getFileName() << ": ";
106 void DiagnosticInfo::anchor() {}
107 void DiagnosticInfoStackSize::anchor() {}
108 void DiagnosticInfoWithLocationBase::anchor() {}
109 void DiagnosticInfoIROptimization::anchor() {}
111 DiagnosticLocation::DiagnosticLocation(const DebugLoc &DL) {
114 File = DL->getFile();
115 Line = DL->getLine();
116 Column = DL->getColumn();
119 DiagnosticLocation::DiagnosticLocation(const DISubprogram *SP) {
123 File = SP->getFile();
124 Line = SP->getScopeLine();
128 StringRef DiagnosticLocation::getRelativePath() const {
129 return File->getFilename();
132 std::string DiagnosticLocation::getAbsolutePath() const {
133 StringRef Name = File->getFilename();
134 if (sys::path::is_absolute(Name))
135 return std::string(Name);
137 SmallString<128> Path;
138 sys::path::append(Path, File->getDirectory(), Name);
139 return sys::path::remove_leading_dotslash(Path).str();
142 std::string DiagnosticInfoWithLocationBase::getAbsolutePath() const {
143 return Loc.getAbsolutePath();
146 void DiagnosticInfoWithLocationBase::getLocation(StringRef &RelativePath,
148 unsigned &Column) const {
149 RelativePath = Loc.getRelativePath();
150 Line = Loc.getLine();
151 Column = Loc.getColumn();
154 const std::string DiagnosticInfoWithLocationBase::getLocationStr() const {
155 StringRef Filename("<unknown>");
158 if (isLocationAvailable())
159 getLocation(Filename, Line, Column);
160 return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
163 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
165 : Key(std::string(Key)) {
166 if (auto *F = dyn_cast<Function>(V)) {
167 if (DISubprogram *SP = F->getSubprogram())
170 else if (auto *I = dyn_cast<Instruction>(V))
171 Loc = I->getDebugLoc();
173 // Only include names that correspond to user variables. FIXME: We should use
174 // debug info if available to get the name of the user variable.
175 if (isa<llvm::Argument>(V) || isa<GlobalValue>(V))
176 Val = std::string(GlobalValue::dropLLVMManglingEscape(V->getName()));
177 else if (isa<Constant>(V)) {
178 raw_string_ostream OS(Val);
179 V->printAsOperand(OS, /*PrintType=*/false);
180 } else if (auto *I = dyn_cast<Instruction>(V))
181 Val = I->getOpcodeName();
184 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, const Type *T)
185 : Key(std::string(Key)) {
186 raw_string_ostream OS(Val);
190 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, StringRef S)
191 : Key(std::string(Key)), Val(S.str()) {}
193 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, int N)
194 : Key(std::string(Key)), Val(itostr(N)) {}
196 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, float N)
197 : Key(std::string(Key)), Val(llvm::to_string(N)) {}
199 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, long N)
200 : Key(std::string(Key)), Val(itostr(N)) {}
202 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, long long N)
203 : Key(std::string(Key)), Val(itostr(N)) {}
205 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, unsigned N)
206 : Key(std::string(Key)), Val(utostr(N)) {}
208 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
210 : Key(std::string(Key)), Val(utostr(N)) {}
212 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
213 unsigned long long N)
214 : Key(std::string(Key)), Val(utostr(N)) {}
216 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, DebugLoc Loc)
217 : Key(std::string(Key)), Loc(Loc) {
219 Val = (Loc->getFilename() + ":" + Twine(Loc.getLine()) + ":" +
220 Twine(Loc.getCol())).str();
222 Val = "<UNKNOWN LOCATION>";
226 void DiagnosticInfoOptimizationBase::print(DiagnosticPrinter &DP) const {
227 DP << getLocationStr() << ": " << getMsg();
229 DP << " (hotness: " << *Hotness << ")";
232 OptimizationRemark::OptimizationRemark(const char *PassName,
233 StringRef RemarkName,
234 const DiagnosticLocation &Loc,
235 const Value *CodeRegion)
236 : DiagnosticInfoIROptimization(
237 DK_OptimizationRemark, DS_Remark, PassName, RemarkName,
238 *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
240 OptimizationRemark::OptimizationRemark(const char *PassName,
241 StringRef RemarkName,
242 const Instruction *Inst)
243 : DiagnosticInfoIROptimization(DK_OptimizationRemark, DS_Remark, PassName,
244 RemarkName, *Inst->getParent()->getParent(),
245 Inst->getDebugLoc(), Inst->getParent()) {}
247 static const BasicBlock *getFirstFunctionBlock(const Function *Func) {
248 return Func->empty() ? nullptr : &Func->front();
251 OptimizationRemark::OptimizationRemark(const char *PassName,
252 StringRef RemarkName,
253 const Function *Func)
254 : DiagnosticInfoIROptimization(DK_OptimizationRemark, DS_Remark, PassName,
255 RemarkName, *Func, Func->getSubprogram(),
256 getFirstFunctionBlock(Func)) {}
258 bool OptimizationRemark::isEnabled() const {
259 const Function &Fn = getFunction();
260 LLVMContext &Ctx = Fn.getContext();
261 return Ctx.getDiagHandlerPtr()->isPassedOptRemarkEnabled(getPassName());
264 OptimizationRemarkMissed::OptimizationRemarkMissed(
265 const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
266 const Value *CodeRegion)
267 : DiagnosticInfoIROptimization(
268 DK_OptimizationRemarkMissed, DS_Remark, PassName, RemarkName,
269 *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
271 OptimizationRemarkMissed::OptimizationRemarkMissed(const char *PassName,
272 StringRef RemarkName,
273 const Instruction *Inst)
274 : DiagnosticInfoIROptimization(DK_OptimizationRemarkMissed, DS_Remark,
275 PassName, RemarkName,
276 *Inst->getParent()->getParent(),
277 Inst->getDebugLoc(), Inst->getParent()) {}
279 bool OptimizationRemarkMissed::isEnabled() const {
280 const Function &Fn = getFunction();
281 LLVMContext &Ctx = Fn.getContext();
282 return Ctx.getDiagHandlerPtr()->isMissedOptRemarkEnabled(getPassName());
285 OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(
286 const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
287 const Value *CodeRegion)
288 : DiagnosticInfoIROptimization(
289 DK_OptimizationRemarkAnalysis, DS_Remark, PassName, RemarkName,
290 *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
292 OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(const char *PassName,
293 StringRef RemarkName,
294 const Instruction *Inst)
295 : DiagnosticInfoIROptimization(DK_OptimizationRemarkAnalysis, DS_Remark,
296 PassName, RemarkName,
297 *Inst->getParent()->getParent(),
298 Inst->getDebugLoc(), Inst->getParent()) {}
300 OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(
301 enum DiagnosticKind Kind, const char *PassName, StringRef RemarkName,
302 const DiagnosticLocation &Loc, const Value *CodeRegion)
303 : DiagnosticInfoIROptimization(Kind, DS_Remark, PassName, RemarkName,
304 *cast<BasicBlock>(CodeRegion)->getParent(),
307 bool OptimizationRemarkAnalysis::isEnabled() const {
308 const Function &Fn = getFunction();
309 LLVMContext &Ctx = Fn.getContext();
310 return Ctx.getDiagHandlerPtr()->isAnalysisRemarkEnabled(getPassName()) ||
314 void DiagnosticInfoMIRParser::print(DiagnosticPrinter &DP) const {
318 DiagnosticInfoOptimizationFailure::DiagnosticInfoOptimizationFailure(
319 const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
320 const Value *CodeRegion)
321 : DiagnosticInfoIROptimization(
322 DK_OptimizationFailure, DS_Warning, PassName, RemarkName,
323 *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
325 bool DiagnosticInfoOptimizationFailure::isEnabled() const {
326 // Only print warnings.
327 return getSeverity() == DS_Warning;
330 void DiagnosticInfoUnsupported::print(DiagnosticPrinter &DP) const {
332 raw_string_ostream OS(Str);
334 OS << getLocationStr() << ": in function " << getFunction().getName() << ' '
335 << *getFunction().getFunctionType() << ": " << Msg << '\n';
340 void DiagnosticInfoISelFallback::print(DiagnosticPrinter &DP) const {
341 DP << "Instruction selection used fallback path for " << getFunction();
344 void DiagnosticInfoOptimizationBase::insert(StringRef S) {
345 Args.emplace_back(S);
348 void DiagnosticInfoOptimizationBase::insert(Argument A) {
349 Args.push_back(std::move(A));
352 void DiagnosticInfoOptimizationBase::insert(setIsVerbose V) {
356 void DiagnosticInfoOptimizationBase::insert(setExtraArgs EA) {
357 FirstExtraArgIndex = Args.size();
360 std::string DiagnosticInfoOptimizationBase::getMsg() const {
362 raw_string_ostream OS(Str);
363 for (const DiagnosticInfoOptimizationBase::Argument &Arg :
364 make_range(Args.begin(), FirstExtraArgIndex == -1
366 : Args.begin() + FirstExtraArgIndex))
371 DiagnosticInfoMisExpect::DiagnosticInfoMisExpect(const Instruction *Inst,
373 : DiagnosticInfoWithLocationBase(DK_MisExpect, DS_Warning,
374 *Inst->getParent()->getParent(),
375 Inst->getDebugLoc()),
378 void DiagnosticInfoMisExpect::print(DiagnosticPrinter &DP) const {
379 DP << getLocationStr() << ": " << getMsg();
382 void OptimizationRemarkAnalysisFPCommute::anchor() {}
383 void OptimizationRemarkAnalysisAliasing::anchor() {}