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 "llvm/IR/DiagnosticInfo.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/ADT/iterator_range.h"
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DebugInfoMetadata.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/DiagnosticPrinter.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/GlobalValue.h"
27 #include "llvm/IR/Instruction.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Metadata.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/Type.h"
32 #include "llvm/IR/Value.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/Regex.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Support/ScopedPrinter.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 DiagnosticLocation::DiagnosticLocation(const DebugLoc
&DL
) {
109 Filename
= DL
->getFilename();
110 Line
= DL
->getLine();
111 Column
= DL
->getColumn();
114 DiagnosticLocation::DiagnosticLocation(const DISubprogram
*SP
) {
117 Filename
= SP
->getFilename();
118 Line
= SP
->getScopeLine();
122 void DiagnosticInfoWithLocationBase::getLocation(StringRef
*Filename
,
124 unsigned *Column
) const {
125 *Filename
= Loc
.getFilename();
126 *Line
= Loc
.getLine();
127 *Column
= Loc
.getColumn();
130 const std::string
DiagnosticInfoWithLocationBase::getLocationStr() const {
131 StringRef
Filename("<unknown>");
134 if (isLocationAvailable())
135 getLocation(&Filename
, &Line
, &Column
);
136 return (Filename
+ ":" + Twine(Line
) + ":" + Twine(Column
)).str();
139 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key
, const Value
*V
)
141 if (auto *F
= dyn_cast
<Function
>(V
)) {
142 if (DISubprogram
*SP
= F
->getSubprogram())
145 else if (auto *I
= dyn_cast
<Instruction
>(V
))
146 Loc
= I
->getDebugLoc();
148 // Only include names that correspond to user variables. FIXME: We should use
149 // debug info if available to get the name of the user variable.
150 if (isa
<llvm::Argument
>(V
) || isa
<GlobalValue
>(V
))
151 Val
= GlobalValue::dropLLVMManglingEscape(V
->getName());
152 else if (isa
<Constant
>(V
)) {
153 raw_string_ostream
OS(Val
);
154 V
->printAsOperand(OS
, /*PrintType=*/false);
155 } else if (auto *I
= dyn_cast
<Instruction
>(V
))
156 Val
= I
->getOpcodeName();
159 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key
, const Type
*T
)
161 raw_string_ostream
OS(Val
);
165 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key
, StringRef S
)
166 : Key(Key
), Val(S
.str()) {}
168 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key
, int N
)
169 : Key(Key
), Val(itostr(N
)) {}
171 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key
, float N
)
172 : Key(Key
), Val(llvm::to_string(N
)) {}
174 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key
, long N
)
175 : Key(Key
), Val(itostr(N
)) {}
177 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key
, long long N
)
178 : Key(Key
), Val(itostr(N
)) {}
180 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key
, unsigned N
)
181 : Key(Key
), Val(utostr(N
)) {}
183 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key
,
185 : Key(Key
), Val(utostr(N
)) {}
187 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key
,
188 unsigned long long N
)
189 : Key(Key
), Val(utostr(N
)) {}
191 DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key
, DebugLoc Loc
)
192 : Key(Key
), Loc(Loc
) {
194 Val
= (Loc
->getFilename() + ":" + Twine(Loc
.getLine()) + ":" +
195 Twine(Loc
.getCol())).str();
197 Val
= "<UNKNOWN LOCATION>";
201 void DiagnosticInfoOptimizationBase::print(DiagnosticPrinter
&DP
) const {
202 DP
<< getLocationStr() << ": " << getMsg();
204 DP
<< " (hotness: " << *Hotness
<< ")";
207 OptimizationRemark::OptimizationRemark(const char *PassName
,
208 StringRef RemarkName
,
209 const DiagnosticLocation
&Loc
,
210 const Value
*CodeRegion
)
211 : DiagnosticInfoIROptimization(
212 DK_OptimizationRemark
, DS_Remark
, PassName
, RemarkName
,
213 *cast
<BasicBlock
>(CodeRegion
)->getParent(), Loc
, CodeRegion
) {}
215 OptimizationRemark::OptimizationRemark(const char *PassName
,
216 StringRef RemarkName
,
217 const Instruction
*Inst
)
218 : DiagnosticInfoIROptimization(DK_OptimizationRemark
, DS_Remark
, PassName
,
219 RemarkName
, *Inst
->getParent()->getParent(),
220 Inst
->getDebugLoc(), Inst
->getParent()) {}
222 // Helper to allow for an assert before attempting to return an invalid
224 static const BasicBlock
&getFirstFunctionBlock(const Function
*Func
) {
225 assert(!Func
->empty() && "Function does not have a body");
226 return Func
->front();
229 OptimizationRemark::OptimizationRemark(const char *PassName
,
230 StringRef RemarkName
,
231 const Function
*Func
)
232 : DiagnosticInfoIROptimization(DK_OptimizationRemark
, DS_Remark
, PassName
,
233 RemarkName
, *Func
, Func
->getSubprogram(),
234 &getFirstFunctionBlock(Func
)) {}
236 bool OptimizationRemark::isEnabled() const {
237 const Function
&Fn
= getFunction();
238 LLVMContext
&Ctx
= Fn
.getContext();
239 return Ctx
.getDiagHandlerPtr()->isPassedOptRemarkEnabled(getPassName());
242 OptimizationRemarkMissed::OptimizationRemarkMissed(
243 const char *PassName
, StringRef RemarkName
, const DiagnosticLocation
&Loc
,
244 const Value
*CodeRegion
)
245 : DiagnosticInfoIROptimization(
246 DK_OptimizationRemarkMissed
, DS_Remark
, PassName
, RemarkName
,
247 *cast
<BasicBlock
>(CodeRegion
)->getParent(), Loc
, CodeRegion
) {}
249 OptimizationRemarkMissed::OptimizationRemarkMissed(const char *PassName
,
250 StringRef RemarkName
,
251 const Instruction
*Inst
)
252 : DiagnosticInfoIROptimization(DK_OptimizationRemarkMissed
, DS_Remark
,
253 PassName
, RemarkName
,
254 *Inst
->getParent()->getParent(),
255 Inst
->getDebugLoc(), Inst
->getParent()) {}
257 bool OptimizationRemarkMissed::isEnabled() const {
258 const Function
&Fn
= getFunction();
259 LLVMContext
&Ctx
= Fn
.getContext();
260 return Ctx
.getDiagHandlerPtr()->isMissedOptRemarkEnabled(getPassName());
263 OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(
264 const char *PassName
, StringRef RemarkName
, const DiagnosticLocation
&Loc
,
265 const Value
*CodeRegion
)
266 : DiagnosticInfoIROptimization(
267 DK_OptimizationRemarkAnalysis
, DS_Remark
, PassName
, RemarkName
,
268 *cast
<BasicBlock
>(CodeRegion
)->getParent(), Loc
, CodeRegion
) {}
270 OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(const char *PassName
,
271 StringRef RemarkName
,
272 const Instruction
*Inst
)
273 : DiagnosticInfoIROptimization(DK_OptimizationRemarkAnalysis
, DS_Remark
,
274 PassName
, RemarkName
,
275 *Inst
->getParent()->getParent(),
276 Inst
->getDebugLoc(), Inst
->getParent()) {}
278 OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(
279 enum DiagnosticKind Kind
, const char *PassName
, StringRef RemarkName
,
280 const DiagnosticLocation
&Loc
, const Value
*CodeRegion
)
281 : DiagnosticInfoIROptimization(Kind
, DS_Remark
, PassName
, RemarkName
,
282 *cast
<BasicBlock
>(CodeRegion
)->getParent(),
285 bool OptimizationRemarkAnalysis::isEnabled() const {
286 const Function
&Fn
= getFunction();
287 LLVMContext
&Ctx
= Fn
.getContext();
288 return Ctx
.getDiagHandlerPtr()->isAnalysisRemarkEnabled(getPassName()) ||
292 void DiagnosticInfoMIRParser::print(DiagnosticPrinter
&DP
) const {
296 DiagnosticInfoOptimizationFailure::DiagnosticInfoOptimizationFailure(
297 const char *PassName
, StringRef RemarkName
, const DiagnosticLocation
&Loc
,
298 const Value
*CodeRegion
)
299 : DiagnosticInfoIROptimization(
300 DK_OptimizationFailure
, DS_Warning
, PassName
, RemarkName
,
301 *cast
<BasicBlock
>(CodeRegion
)->getParent(), Loc
, CodeRegion
) {}
303 bool DiagnosticInfoOptimizationFailure::isEnabled() const {
304 // Only print warnings.
305 return getSeverity() == DS_Warning
;
308 void DiagnosticInfoUnsupported::print(DiagnosticPrinter
&DP
) const {
310 raw_string_ostream
OS(Str
);
312 OS
<< getLocationStr() << ": in function " << getFunction().getName() << ' '
313 << *getFunction().getFunctionType() << ": " << Msg
<< '\n';
318 void DiagnosticInfoISelFallback::print(DiagnosticPrinter
&DP
) const {
319 DP
<< "Instruction selection used fallback path for " << getFunction();
322 void DiagnosticInfoOptimizationBase::insert(StringRef S
) {
323 Args
.emplace_back(S
);
326 void DiagnosticInfoOptimizationBase::insert(Argument A
) {
327 Args
.push_back(std::move(A
));
330 void DiagnosticInfoOptimizationBase::insert(setIsVerbose V
) {
334 void DiagnosticInfoOptimizationBase::insert(setExtraArgs EA
) {
335 FirstExtraArgIndex
= Args
.size();
338 std::string
DiagnosticInfoOptimizationBase::getMsg() const {
340 raw_string_ostream
OS(Str
);
341 for (const DiagnosticInfoOptimizationBase::Argument
&Arg
:
342 make_range(Args
.begin(), FirstExtraArgIndex
== -1
344 : Args
.begin() + FirstExtraArgIndex
))
352 void MappingTraits
<DiagnosticInfoOptimizationBase
*>::mapping(
353 IO
&io
, DiagnosticInfoOptimizationBase
*&OptDiag
) {
354 assert(io
.outputting() && "input not yet implemented");
356 if (io
.mapTag("!Passed",
357 (OptDiag
->getKind() == DK_OptimizationRemark
||
358 OptDiag
->getKind() == DK_MachineOptimizationRemark
)))
362 (OptDiag
->getKind() == DK_OptimizationRemarkMissed
||
363 OptDiag
->getKind() == DK_MachineOptimizationRemarkMissed
)))
367 (OptDiag
->getKind() == DK_OptimizationRemarkAnalysis
||
368 OptDiag
->getKind() == DK_MachineOptimizationRemarkAnalysis
)))
370 else if (io
.mapTag("!AnalysisFPCommute",
371 OptDiag
->getKind() ==
372 DK_OptimizationRemarkAnalysisFPCommute
))
374 else if (io
.mapTag("!AnalysisAliasing",
375 OptDiag
->getKind() ==
376 DK_OptimizationRemarkAnalysisAliasing
))
378 else if (io
.mapTag("!Failure", OptDiag
->getKind() == DK_OptimizationFailure
))
381 llvm_unreachable("Unknown remark type");
383 // These are read-only for now.
384 DiagnosticLocation DL
= OptDiag
->getLocation();
386 GlobalValue::dropLLVMManglingEscape(OptDiag
->getFunction().getName());
388 StringRef
PassName(OptDiag
->PassName
);
389 io
.mapRequired("Pass", PassName
);
390 io
.mapRequired("Name", OptDiag
->RemarkName
);
391 if (!io
.outputting() || DL
.isValid())
392 io
.mapOptional("DebugLoc", DL
);
393 io
.mapRequired("Function", FN
);
394 io
.mapOptional("Hotness", OptDiag
->Hotness
);
395 io
.mapOptional("Args", OptDiag
->Args
);
398 template <> struct MappingTraits
<DiagnosticLocation
> {
399 static void mapping(IO
&io
, DiagnosticLocation
&DL
) {
400 assert(io
.outputting() && "input not yet implemented");
402 StringRef File
= DL
.getFilename();
403 unsigned Line
= DL
.getLine();
404 unsigned Col
= DL
.getColumn();
406 io
.mapRequired("File", File
);
407 io
.mapRequired("Line", Line
);
408 io
.mapRequired("Column", Col
);
411 static const bool flow
= true;
414 // Implement this as a mapping for now to get proper quotation for the value.
415 template <> struct MappingTraits
<DiagnosticInfoOptimizationBase::Argument
> {
416 static void mapping(IO
&io
, DiagnosticInfoOptimizationBase::Argument
&A
) {
417 assert(io
.outputting() && "input not yet implemented");
418 io
.mapRequired(A
.Key
.data(), A
.Val
);
420 io
.mapOptional("DebugLoc", A
.Loc
);
424 } // end namespace yaml
425 } // end namespace llvm
427 LLVM_YAML_IS_SEQUENCE_VECTOR(DiagnosticInfoOptimizationBase::Argument
)