LLVM 22.0.0git
DiagnosticInfo.cpp
Go to the documentation of this file.
1//===- llvm/IR/DiagnosticInfo.cpp - Diagnostic Definitions ------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the different classes involved in low level diagnostics.
10//
11// Diagnostics reporting is still done as part of the LLVMContext.
12//===----------------------------------------------------------------------===//
13
16#include "llvm/ADT/Twine.h"
19#include "llvm/IR/BasicBlock.h"
20#include "llvm/IR/Constants.h"
24#include "llvm/IR/Function.h"
25#include "llvm/IR/GlobalValue.h"
26#include "llvm/IR/Instruction.h"
29#include "llvm/IR/LLVMContext.h"
30#include "llvm/IR/Metadata.h"
31#include "llvm/IR/Module.h"
32#include "llvm/IR/Type.h"
33#include "llvm/IR/Value.h"
37#include "llvm/Support/Path.h"
40#include <atomic>
41#include <string>
42
43using namespace llvm;
44
46 static std::atomic<int> PluginKindID(DK_FirstPluginKind);
47 return ++PluginKindID;
48}
49
51
55
59
61 const Twine &MsgStr,
62 DiagnosticSeverity Severity)
63 : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
64 MsgStr(MsgStr) {}
65
67 const Twine &MsgStr,
68 DiagnosticSeverity Severity)
69 : DiagnosticInfo(DK_InlineAsm, Severity), MsgStr(MsgStr), Instr(&I) {
70 if (const MDNode *SrcLoc = I.getMetadata("srcloc")) {
71 if (SrcLoc->getNumOperands() != 0)
72 if (const auto *CI =
73 mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0)))
74 LocCookie = CI->getZExtValue();
75 }
76}
77
79 DP << getMsgStr();
80 if (getLocCookie())
81 DP << " at line " << getLocCookie();
82}
83
87
94
100
102 DP << getLocationStr() << ": " << MsgStr << " in function '" << getFunction()
103 << '\'';
104}
105
107 const Function &Fn, const char *ResourceName, uint64_t ResourceSize,
108 uint64_t ResourceLimit, DiagnosticSeverity Severity, DiagnosticKind Kind)
109 : DiagnosticInfoWithLocationBase(Kind, Severity, Fn, Fn.getSubprogram()),
110 Fn(Fn), ResourceName(ResourceName), ResourceSize(ResourceSize),
111 ResourceLimit(ResourceLimit) {}
112
114 DP << getLocationStr() << ": " << getResourceName() << " ("
115 << getResourceSize() << ") exceeds limit (" << getResourceLimit()
116 << ") in function '" << getFunction() << '\'';
117}
118
120 DP << "ignoring debug info with an invalid version (" << getMetadataVersion()
121 << ") in " << getModule();
122}
123
125 DiagnosticPrinter &DP) const {
126 DP << "ignoring invalid debug info in " << getModule().getModuleIdentifier();
127}
128
130 if (!FileName.empty()) {
131 DP << getFileName();
132 if (LineNum > 0)
133 DP << ":" << getLineNum();
134 DP << ": ";
135 }
136 DP << getMsg();
137}
138
140 if (getFileName())
141 DP << getFileName() << ": ";
142 DP << getMsg();
143}
144
145void DiagnosticInfo::anchor() {}
146void DiagnosticInfoStackSize::anchor() {}
147void DiagnosticInfoWithLocationBase::anchor() {}
148void DiagnosticInfoIROptimization::anchor() {}
149
151 if (!DL)
152 return;
153 File = DL->getFile();
154 Line = DL->getLine();
155 Column = DL->getColumn();
156}
157
159 if (!SP)
160 return;
161
162 File = SP->getFile();
163 Line = SP->getScopeLine();
164 Column = 0;
165}
166
168 return File->getFilename();
169}
170
172 StringRef Name = File->getFilename();
173 if (sys::path::is_absolute(Name))
174 return std::string(Name);
175
176 SmallString<128> Path;
177 sys::path::append(Path, File->getDirectory(), Name);
179}
180
182 return Loc.getAbsolutePath();
183}
184
186 unsigned &Line,
187 unsigned &Column) const {
188 RelativePath = Loc.getRelativePath();
189 Line = Loc.getLine();
190 Column = Loc.getColumn();
191}
192
194 StringRef Filename("<unknown>");
195 unsigned Line = 0;
196 unsigned Column = 0;
198 getLocation(Filename, Line, Column);
199 return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
200}
201
203 const Value *V)
204 : Key(std::string(Key)) {
205 if (auto *F = dyn_cast<Function>(V)) {
206 if (DISubprogram *SP = F->getSubprogram())
207 Loc = SP;
208 }
209 else if (auto *I = dyn_cast<Instruction>(V))
210 Loc = I->getDebugLoc();
211
212 // Only include names that correspond to user variables. FIXME: We should use
213 // debug info if available to get the name of the user variable.
215 Val = std::string(GlobalValue::dropLLVMManglingEscape(V->getName()));
216 else if (isa<Constant>(V)) {
218 V->printAsOperand(OS, /*PrintType=*/false);
219 } else if (auto *II = dyn_cast<IntrinsicInst>(V)) {
221 OS << "call " << II->getCalledFunction()->getName();
222 } else if (auto *I = dyn_cast<Instruction>(V)) {
223 Val = I->getOpcodeName();
224 } else if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
225 if (auto *S = dyn_cast<MDString>(MD->getMetadata()))
226 Val = S->getString();
227 }
228}
229
235
238
241
244
247
250
253
257
261
268
275
282
284 : Key(std::string(Key)), Loc(Loc) {
285 if (Loc) {
286 Val = (Loc->getFilename() + ":" + Twine(Loc.getLine()) + ":" +
287 Twine(Loc.getCol())).str();
288 } else {
289 Val = "<UNKNOWN LOCATION>";
290 }
291}
292
294 DP << getLocationStr() << ": " << getMsg();
295 if (Hotness)
296 DP << " (hotness: " << *Hotness << ")";
297}
298
306
313
314static const BasicBlock *getFirstFunctionBlock(const Function *Func) {
315 return Func->empty() ? nullptr : &Func->front();
316}
317
324
326 const Function &Fn = getFunction();
327 LLVMContext &Ctx = Fn.getContext();
329}
330
337
345
352
354 const Function &Fn = getFunction();
355 LLVMContext &Ctx = Fn.getContext();
357}
358
365
373
379
386
388 const Function &Fn = getFunction();
389 LLVMContext &Ctx = Fn.getContext();
392}
393
395 DP << Diagnostic;
396}
397
399 DP << Diagnostic;
400}
401
408
410 // Only print warnings.
411 return getSeverity() == DS_Warning;
412}
413
415 std::string Str;
416 raw_string_ostream OS(Str);
417
418 OS << getLocationStr() << ": in function " << getFunction().getName() << ' '
419 << *getFunction().getFunctionType() << ": " << Msg << '\n';
420 OS.flush();
421 DP << Str;
422}
423
425 DP << Msg;
426}
427
429 DP << "Instruction selection used fallback path for " << getFunction();
430}
431
433 Args.emplace_back(S);
434}
435
437 Args.push_back(std::move(A));
438}
439
443
447
449 std::string Str;
450 raw_string_ostream OS(Str);
452 make_range(Args.begin(), FirstExtraArgIndex == -1
453 ? Args.end()
454 : Args.begin() + FirstExtraArgIndex))
455 OS << Arg.Val;
456 return Str;
457}
458
465
467 DP << getLocationStr() << ": " << getMsg();
468}
469
470void OptimizationRemarkAnalysisFPCommute::anchor() {}
471void OptimizationRemarkAnalysisAliasing::anchor() {}
472
474 const auto *F =
476
477 if (!F)
478 return;
479
480 for (int i = 0; i != 2; ++i) {
481 auto AttrName = i == 0 ? "dontcall-error" : "dontcall-warn";
482 auto Sev = i == 0 ? DS_Error : DS_Warning;
483
484 if (F->hasFnAttribute(AttrName)) {
485 uint64_t LocCookie = 0;
486 auto A = F->getFnAttribute(AttrName);
487 if (MDNode *MD = CI.getMetadata("srcloc"))
488 LocCookie =
489 mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue();
490 DiagnosticInfoDontCall D(F->getName(), A.getValueAsString(), Sev,
491 LocCookie);
492 F->getContext().diagnose(D);
493 }
494 }
495}
496
498 DP << "call to " << demangle(getFunctionName()) << " marked \"dontcall-";
500 DP << "error\"";
501 else
502 DP << "warn\"";
503 if (!getNote().empty())
504 DP << ": " << getNote();
505}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static DISubprogram * getSubprogram(bool IsDistinct, Ts &&...Args)
static const BasicBlock * getFirstFunctionBlock(const Function *Func)
Module.h This file contains the declarations for the Module class.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
Return the first found DebugLoc that has a DILocation, given a range of instructions.
This file contains the declarations for metadata subclasses.
#define T
uint64_t IntrinsicInst * II
#define P(N)
if(PassOpts->AAPipeline)
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
This file contains some functions that are useful when dealing with strings.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Value * getCalledOperand() const
This class represents a function call, abstracting a target machine's calling convention.
Subprogram description. Uses SubclassData1.
A debug info location.
Definition DebugLoc.h:124
void print(DiagnosticPrinter &DP) const override
StringRef getFunctionName() const
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
void print(DiagnosticPrinter &DP) const override
const Twine & getMsgStr() const
void print(DiagnosticPrinter &DP) const override
DiagnosticInfoIROptimization(enum DiagnosticKind Kind, enum DiagnosticSeverity Severity, const char *PassName, StringRef RemarkName, const Function &Fn, const DiagnosticLocation &Loc, const BasicBlock *CodeRegion=nullptr)
PassName is the name of the pass emitting this diagnostic.
const Function & getFunction() const
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
void print(DiagnosticPrinter &DP) const override
DiagnosticInfoInlineAsm(uint64_t LocCookie, const Twine &MsgStr LLVM_LIFETIME_BOUND, DiagnosticSeverity Severity=DS_Error)
LocCookie if non-zero gives the line number for this report.
const Twine & getMsgStr() const
void print(DiagnosticPrinter &DP) const override
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
const Twine & getMsg() const
void print(DiagnosticPrinter &DP) const override
DiagnosticInfoMisExpect(const Instruction *Inst, const Twine &Msg LLVM_LIFETIME_BOUND)
int FirstExtraArgIndex
If positive, the index of the first argument that only appear in the optimization records and not in ...
const char * PassName
Name of the pass that triggers this report.
StringRef RemarkName
Textual identifier for the remark (single-word, CamelCase).
void print(DiagnosticPrinter &DP) const override
bool IsVerbose
The remark is expected to be noisy.
std::optional< uint64_t > Hotness
If profile information is available, this is the number of times the corresponding code was executed ...
SmallVector< Argument, 4 > Args
Arguments collected via the streaming interface.
DiagnosticInfoOptimizationFailure(const Function &Fn, const DiagnosticLocation &Loc, const Twine &Msg)
Fn is the function where the diagnostic is being emitted.
const char * getFileName() const
void print(DiagnosticPrinter &DP) const override
const Twine & getMsg() const
DiagnosticInfoRegAllocFailure(const Twine &MsgStr, const Function &Fn, const DiagnosticLocation &DL, DiagnosticSeverity Severity=DS_Error)
MsgStr is the message to be reported to the frontend.
void print(DiagnosticPrinter &DP) const override
const Function & getFunction() const
void print(DiagnosticPrinter &DP) const override
const char * getResourceName() const
DiagnosticInfoResourceLimit(const Function &Fn, const char *ResourceName, uint64_t ResourceSize, uint64_t ResourceLimit, DiagnosticSeverity Severity=DS_Warning, DiagnosticKind Kind=DK_ResourceLimit)
The function that is concerned by this stack size diagnostic.
void print(DiagnosticPrinter &DP) const override
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
std::string getLocationStr() const
Return a string with the location information for this diagnostic in the format "file:line:col".
std::string getAbsolutePath() const
Return the absolute path tot the file.
bool isLocationAvailable() const
Return true if location information is available for this diagnostic.
const Function & getFunction() const
DiagnosticLocation getLocation() const
DiagnosticInfoWithLocationBase(enum DiagnosticKind Kind, enum DiagnosticSeverity Severity, const Function &Fn, const DiagnosticLocation &Loc)
Fn is the function where the diagnostic is being emitted.
This is the base abstract class for diagnostic reporting in the backend.
DiagnosticSeverity getSeverity() const
LLVM_ABI std::string getAbsolutePath() const
Return the full path to the file.
LLVM_ABI StringRef getRelativePath() const
Return the file name relative to the compilation directory.
Interface for custom diagnostic printing.
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:209
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI const DiagnosticHandler * getDiagHandlerPtr() const
getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by setDiagnosticHandler.
Metadata node.
Definition Metadata.h:1078
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition Module.h:252
OptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const BasicBlock *CodeRegion)
PassName is the name of the pass emitting this diagnostic.
bool isEnabled() const override
OptimizationRemarkMissed(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const BasicBlock *CodeRegion)
PassName is the name of the pass emitting this diagnostic.
bool isEnabled() const override
OptimizationRemark(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const BasicBlock *CodeRegion)
PassName is the name of the pass emitting this diagnostic.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:225
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:701
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
A raw_ostream that writes to an std::string.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
LLVM_ABI StringRef remove_leading_dotslash(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
LLVM_ABI bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition Path.cpp:671
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:456
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
LLVM_ABI void diagnoseDontCall(const CallInst &CI)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
std::string utostr(uint64_t X, bool isNeg=false)
DiagnosticKind
Defines the different supported kind of a diagnostic.
@ DK_OptimizationRemarkAnalysis
@ DK_OptimizationRemarkMissed
@ DK_OptimizationRemark
@ DK_InlineAsm
@ DK_OptimizationFailure
@ DK_MisExpect
@ DK_FirstPluginKind
@ DK_RegAllocFailure
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
const char * to_string(ThinOrFullLTOPhase Phase)
Definition Pass.cpp:301
LLVM_ABI int getNextAvailablePluginDiagnosticKind()
Get the next available kind ID for a plugin diagnostic.
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
DEMANGLE_ABI std::string demangle(std::string_view MangledName)
Attempt to demangle a string using different demangling schemes.
Definition Demangle.cpp:20
std::string itostr(int64_t X)
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
#define N
virtual bool isPassedOptRemarkEnabled(StringRef PassName) const
Return true if passed optimization remarks are enabled, override to provide different implementation.
virtual bool isAnalysisRemarkEnabled(StringRef PassName) const
Return true if analysis remarks are enabled, override to provide different implementation.
virtual bool isMissedOptRemarkEnabled(StringRef PassName) const
Return true if missed optimization remarks are enabled, override to provide different implementation.
Used in the streaming interface as the general argument type.
When an instance of this is inserted into the stream, the arguments following will not appear in the ...
Used to set IsVerbose via the stream interface.