| 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 | |
| 14 | #include "llvm/IR/DiagnosticInfo.h" |
| 15 | #include "llvm/ADT/StringExtras.h" |
| 16 | #include "llvm/ADT/Twine.h" |
| 17 | #include "llvm/ADT/iterator_range.h" |
| 18 | #include "llvm/Demangle/Demangle.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/Instructions.h" |
| 28 | #include "llvm/IR/IntrinsicInst.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" |
| 34 | #include "llvm/Support/Casting.h" |
| 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/InstructionCost.h" |
| 37 | #include "llvm/Support/Path.h" |
| 38 | #include "llvm/Support/ScopedPrinter.h" |
| 39 | #include "llvm/Support/raw_ostream.h" |
| 40 | #include <atomic> |
| 41 | #include <string> |
| 42 | |
| 43 | using namespace llvm; |
| 44 | |
| 45 | int llvm::getNextAvailablePluginDiagnosticKind() { |
| 46 | static std::atomic<int> PluginKindID(DK_FirstPluginKind); |
| 47 | return ++PluginKindID; |
| 48 | } |
| 49 | |
| 50 | const char *OptimizationRemarkAnalysis:: = "" ; |
| 51 | |
| 52 | void DiagnosticInfoGeneric::print(DiagnosticPrinter &DP) const { |
| 53 | DP << getMsgStr(); |
| 54 | } |
| 55 | |
| 56 | void DiagnosticInfoGenericWithLoc::print(DiagnosticPrinter &DP) const { |
| 57 | DP << getLocationStr() << ": " << getMsgStr(); |
| 58 | } |
| 59 | |
| 60 | DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(uint64_t LocCookie, |
| 61 | const Twine &MsgStr, |
| 62 | DiagnosticSeverity Severity) |
| 63 | : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie), |
| 64 | MsgStr(MsgStr) {} |
| 65 | |
| 66 | DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I, |
| 67 | const Twine &MsgStr, |
| 68 | DiagnosticSeverity Severity) |
| 69 | : DiagnosticInfo(DK_InlineAsm, Severity), MsgStr(MsgStr), Instr(&I) { |
| 70 | if (const MDNode *SrcLoc = I.getMetadata(Kind: "srcloc" )) { |
| 71 | if (SrcLoc->getNumOperands() != 0) |
| 72 | if (const auto *CI = |
| 73 | mdconst::dyn_extract<ConstantInt>(MD: SrcLoc->getOperand(I: 0))) |
| 74 | LocCookie = CI->getZExtValue(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void DiagnosticInfoInlineAsm::print(DiagnosticPrinter &DP) const { |
| 79 | DP << getMsgStr(); |
| 80 | if (getLocCookie()) |
| 81 | DP << " at line " << getLocCookie(); |
| 82 | } |
| 83 | |
| 84 | void DiagnosticInfoLegalizationFailure::print(DiagnosticPrinter &DP) const { |
| 85 | DP << getLocationStr() << ": " << getMsgStr(); |
| 86 | } |
| 87 | |
| 88 | DiagnosticInfoRegAllocFailure::DiagnosticInfoRegAllocFailure( |
| 89 | const Twine &MsgStr, const Function &Fn, const DiagnosticLocation &DL, |
| 90 | DiagnosticSeverity Severity) |
| 91 | : DiagnosticInfoWithLocationBase(DK_RegAllocFailure, Severity, Fn, |
| 92 | DL.isValid() ? DL : Fn.getSubprogram()), |
| 93 | MsgStr(MsgStr) {} |
| 94 | |
| 95 | DiagnosticInfoRegAllocFailure::DiagnosticInfoRegAllocFailure( |
| 96 | const Twine &MsgStr, const Function &Fn, DiagnosticSeverity Severity) |
| 97 | : DiagnosticInfoWithLocationBase(DK_RegAllocFailure, Severity, Fn, |
| 98 | Fn.getSubprogram()), |
| 99 | MsgStr(MsgStr) {} |
| 100 | |
| 101 | void DiagnosticInfoRegAllocFailure::print(DiagnosticPrinter &DP) const { |
| 102 | DP << getLocationStr() << ": " << MsgStr << " in function '" << getFunction() |
| 103 | << '\''; |
| 104 | } |
| 105 | |
| 106 | DiagnosticInfoResourceLimit::DiagnosticInfoResourceLimit( |
| 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 | |
| 113 | void DiagnosticInfoResourceLimit::print(DiagnosticPrinter &DP) const { |
| 114 | DP << getLocationStr() << ": " << getResourceName() << " (" |
| 115 | << getResourceSize() << ") exceeds limit (" << getResourceLimit() |
| 116 | << ") in function '" << getFunction() << '\''; |
| 117 | } |
| 118 | |
| 119 | void DiagnosticInfoDebugMetadataVersion::print(DiagnosticPrinter &DP) const { |
| 120 | DP << "ignoring debug info with an invalid version (" << getMetadataVersion() |
| 121 | << ") in " << getModule(); |
| 122 | } |
| 123 | |
| 124 | void DiagnosticInfoIgnoringInvalidDebugMetadata::print( |
| 125 | DiagnosticPrinter &DP) const { |
| 126 | DP << "ignoring invalid debug info in " << getModule().getModuleIdentifier(); |
| 127 | } |
| 128 | |
| 129 | void DiagnosticInfoSampleProfile::print(DiagnosticPrinter &DP) const { |
| 130 | if (!FileName.empty()) { |
| 131 | DP << getFileName(); |
| 132 | if (LineNum > 0) |
| 133 | DP << ":" << getLineNum(); |
| 134 | DP << ": " ; |
| 135 | } |
| 136 | DP << getMsg(); |
| 137 | } |
| 138 | |
| 139 | void DiagnosticInfoPGOProfile::print(DiagnosticPrinter &DP) const { |
| 140 | if (getFileName()) |
| 141 | DP << getFileName() << ": " ; |
| 142 | DP << getMsg(); |
| 143 | } |
| 144 | |
| 145 | void DiagnosticInfo::anchor() {} |
| 146 | void DiagnosticInfoStackSize::anchor() {} |
| 147 | void DiagnosticInfoWithLocationBase::anchor() {} |
| 148 | void DiagnosticInfoIROptimization::anchor() {} |
| 149 | |
| 150 | DiagnosticLocation::DiagnosticLocation(const DebugLoc &DL) { |
| 151 | if (!DL) |
| 152 | return; |
| 153 | File = DL->getFile(); |
| 154 | Line = DL->getLine(); |
| 155 | Column = DL->getColumn(); |
| 156 | } |
| 157 | |
| 158 | DiagnosticLocation::DiagnosticLocation(const DISubprogram *SP) { |
| 159 | if (!SP) |
| 160 | return; |
| 161 | |
| 162 | File = SP->getFile(); |
| 163 | Line = SP->getScopeLine(); |
| 164 | Column = 0; |
| 165 | } |
| 166 | |
| 167 | StringRef DiagnosticLocation::getRelativePath() const { |
| 168 | return File->getFilename(); |
| 169 | } |
| 170 | |
| 171 | std::string DiagnosticLocation::getAbsolutePath() const { |
| 172 | StringRef Name = File->getFilename(); |
| 173 | if (sys::path::is_absolute(path: Name)) |
| 174 | return std::string(Name); |
| 175 | |
| 176 | SmallString<128> Path; |
| 177 | sys::path::append(path&: Path, a: File->getDirectory(), b: Name); |
| 178 | return sys::path::remove_leading_dotslash(path: Path).str(); |
| 179 | } |
| 180 | |
| 181 | std::string DiagnosticInfoWithLocationBase::getAbsolutePath() const { |
| 182 | return Loc.getAbsolutePath(); |
| 183 | } |
| 184 | |
| 185 | void DiagnosticInfoWithLocationBase::getLocation(StringRef &RelativePath, |
| 186 | unsigned &Line, |
| 187 | unsigned &Column) const { |
| 188 | RelativePath = Loc.getRelativePath(); |
| 189 | Line = Loc.getLine(); |
| 190 | Column = Loc.getColumn(); |
| 191 | } |
| 192 | |
| 193 | std::string DiagnosticInfoWithLocationBase::getLocationStr() const { |
| 194 | StringRef Filename("<unknown>" ); |
| 195 | unsigned Line = 0; |
| 196 | unsigned Column = 0; |
| 197 | if (isLocationAvailable()) |
| 198 | getLocation(RelativePath&: Filename, Line, Column); |
| 199 | return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str(); |
| 200 | } |
| 201 | |
| 202 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, |
| 203 | const Value *V) |
| 204 | : Key(std::string(Key)) { |
| 205 | if (auto *F = dyn_cast<Function>(Val: V)) { |
| 206 | if (DISubprogram *SP = F->getSubprogram()) |
| 207 | Loc = SP; |
| 208 | } |
| 209 | else if (auto *I = dyn_cast<Instruction>(Val: 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. |
| 214 | if (isa<llvm::Argument>(Val: V) || isa<GlobalValue>(Val: V)) |
| 215 | Val = std::string(GlobalValue::dropLLVMManglingEscape(Name: V->getName())); |
| 216 | else if (isa<Constant>(Val: V)) { |
| 217 | raw_string_ostream OS(Val); |
| 218 | V->printAsOperand(O&: OS, /*PrintType=*/false); |
| 219 | } else if (auto *II = dyn_cast<IntrinsicInst>(Val: V)) { |
| 220 | raw_string_ostream(Val) << "call " << II->getCalledFunction()->getName(); |
| 221 | } else if (auto *I = dyn_cast<Instruction>(Val: V)) { |
| 222 | Val = I->getOpcodeName(); |
| 223 | } else if (auto *MD = dyn_cast<MetadataAsValue>(Val: V)) { |
| 224 | if (auto *S = dyn_cast<MDString>(Val: MD->getMetadata())) |
| 225 | Val = S->getString(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, const Type *T) |
| 230 | : Key(std::string(Key)) { |
| 231 | raw_string_ostream(Val) << *T; |
| 232 | } |
| 233 | |
| 234 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, StringRef S) |
| 235 | : Key(std::string(Key)), Val(S.str()) {} |
| 236 | |
| 237 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, int N) |
| 238 | : Key(std::string(Key)), Val(itostr(X: N)) {} |
| 239 | |
| 240 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, float N) |
| 241 | : Key(std::string(Key)), Val(llvm::to_string(Value: N)) {} |
| 242 | |
| 243 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, long N) |
| 244 | : Key(std::string(Key)), Val(itostr(X: N)) {} |
| 245 | |
| 246 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, long long N) |
| 247 | : Key(std::string(Key)), Val(itostr(X: N)) {} |
| 248 | |
| 249 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, unsigned N) |
| 250 | : Key(std::string(Key)), Val(utostr(X: N)) {} |
| 251 | |
| 252 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, |
| 253 | unsigned long N) |
| 254 | : Key(std::string(Key)), Val(utostr(X: N)) {} |
| 255 | |
| 256 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, |
| 257 | unsigned long long N) |
| 258 | : Key(std::string(Key)), Val(utostr(X: N)) {} |
| 259 | |
| 260 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, |
| 261 | ElementCount EC) |
| 262 | : Key(std::string(Key)) { |
| 263 | raw_string_ostream OS(Val); |
| 264 | EC.print(OS); |
| 265 | } |
| 266 | |
| 267 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, |
| 268 | InstructionCost C) |
| 269 | : Key(std::string(Key)) { |
| 270 | raw_string_ostream OS(Val); |
| 271 | C.print(OS); |
| 272 | } |
| 273 | |
| 274 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, |
| 275 | BranchProbability P) |
| 276 | : Key(std::string(Key)) { |
| 277 | raw_string_ostream OS(Val); |
| 278 | P.print(OS); |
| 279 | } |
| 280 | |
| 281 | DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, DebugLoc Loc) |
| 282 | : Key(std::string(Key)), Loc(Loc) { |
| 283 | if (Loc) { |
| 284 | Val = (Loc->getFilename() + ":" + Twine(Loc.getLine()) + ":" + |
| 285 | Twine(Loc.getCol())).str(); |
| 286 | } else { |
| 287 | Val = "<UNKNOWN LOCATION>" ; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void DiagnosticInfoOptimizationBase::print(DiagnosticPrinter &DP) const { |
| 292 | DP << getLocationStr() << ": " << getMsg(); |
| 293 | if (Hotness) |
| 294 | DP << " (hotness: " << *Hotness << ")" ; |
| 295 | } |
| 296 | |
| 297 | OptimizationRemark::(const char *PassName, |
| 298 | StringRef , |
| 299 | const DiagnosticLocation &Loc, |
| 300 | const BasicBlock *CodeRegion) |
| 301 | : DiagnosticInfoIROptimization(DK_OptimizationRemark, DS_Remark, PassName, |
| 302 | RemarkName, *CodeRegion->getParent(), Loc, |
| 303 | CodeRegion) {} |
| 304 | |
| 305 | OptimizationRemark::(const char *PassName, |
| 306 | StringRef , |
| 307 | const Instruction *Inst) |
| 308 | : DiagnosticInfoIROptimization(DK_OptimizationRemark, DS_Remark, PassName, |
| 309 | RemarkName, *Inst->getParent()->getParent(), |
| 310 | Inst->getDebugLoc(), Inst->getParent()) {} |
| 311 | |
| 312 | static const BasicBlock *getFirstFunctionBlock(const Function *Func) { |
| 313 | return Func->empty() ? nullptr : &Func->front(); |
| 314 | } |
| 315 | |
| 316 | OptimizationRemark::(const char *PassName, |
| 317 | StringRef , |
| 318 | const Function *Func) |
| 319 | : DiagnosticInfoIROptimization(DK_OptimizationRemark, DS_Remark, PassName, |
| 320 | RemarkName, *Func, Func->getSubprogram(), |
| 321 | getFirstFunctionBlock(Func)) {} |
| 322 | |
| 323 | bool OptimizationRemark::() const { |
| 324 | const Function &Fn = getFunction(); |
| 325 | LLVMContext &Ctx = Fn.getContext(); |
| 326 | return Ctx.getDiagHandlerPtr()->isPassedOptRemarkEnabled(PassName: getPassName()); |
| 327 | } |
| 328 | |
| 329 | OptimizationRemarkMissed::( |
| 330 | const char *PassName, StringRef , const DiagnosticLocation &Loc, |
| 331 | const BasicBlock *CodeRegion) |
| 332 | : DiagnosticInfoIROptimization(DK_OptimizationRemarkMissed, DS_Remark, |
| 333 | PassName, RemarkName, |
| 334 | *CodeRegion->getParent(), Loc, CodeRegion) {} |
| 335 | |
| 336 | OptimizationRemarkMissed::(const char *PassName, |
| 337 | StringRef , |
| 338 | const Instruction *Inst) |
| 339 | : DiagnosticInfoIROptimization(DK_OptimizationRemarkMissed, DS_Remark, |
| 340 | PassName, RemarkName, |
| 341 | *Inst->getParent()->getParent(), |
| 342 | Inst->getDebugLoc(), Inst->getParent()) {} |
| 343 | |
| 344 | OptimizationRemarkMissed::(const char *PassName, |
| 345 | StringRef , |
| 346 | const Function *Func) |
| 347 | : DiagnosticInfoIROptimization( |
| 348 | DK_OptimizationRemarkMissed, DS_Remark, PassName, RemarkName, *Func, |
| 349 | Func->getSubprogram(), getFirstFunctionBlock(Func)) {} |
| 350 | |
| 351 | bool OptimizationRemarkMissed::() const { |
| 352 | const Function &Fn = getFunction(); |
| 353 | LLVMContext &Ctx = Fn.getContext(); |
| 354 | return Ctx.getDiagHandlerPtr()->isMissedOptRemarkEnabled(PassName: getPassName()); |
| 355 | } |
| 356 | |
| 357 | OptimizationRemarkAnalysis::( |
| 358 | const char *PassName, StringRef , const DiagnosticLocation &Loc, |
| 359 | const BasicBlock *CodeRegion) |
| 360 | : DiagnosticInfoIROptimization(DK_OptimizationRemarkAnalysis, DS_Remark, |
| 361 | PassName, RemarkName, |
| 362 | *CodeRegion->getParent(), Loc, CodeRegion) {} |
| 363 | |
| 364 | OptimizationRemarkAnalysis::(const char *PassName, |
| 365 | StringRef , |
| 366 | const Instruction *Inst) |
| 367 | : DiagnosticInfoIROptimization(DK_OptimizationRemarkAnalysis, DS_Remark, |
| 368 | PassName, RemarkName, |
| 369 | *Inst->getParent()->getParent(), |
| 370 | Inst->getDebugLoc(), Inst->getParent()) {} |
| 371 | |
| 372 | OptimizationRemarkAnalysis::( |
| 373 | enum DiagnosticKind Kind, const char *PassName, StringRef , |
| 374 | const DiagnosticLocation &Loc, const BasicBlock *CodeRegion) |
| 375 | : DiagnosticInfoIROptimization(Kind, DS_Remark, PassName, RemarkName, |
| 376 | *CodeRegion->getParent(), Loc, CodeRegion) {} |
| 377 | |
| 378 | OptimizationRemarkAnalysis::(const char *PassName, |
| 379 | StringRef , |
| 380 | const Function *Func) |
| 381 | : DiagnosticInfoIROptimization( |
| 382 | DK_OptimizationRemarkAnalysis, DS_Remark, PassName, RemarkName, *Func, |
| 383 | Func->getSubprogram(), getFirstFunctionBlock(Func)) {} |
| 384 | |
| 385 | bool OptimizationRemarkAnalysis::() const { |
| 386 | const Function &Fn = getFunction(); |
| 387 | LLVMContext &Ctx = Fn.getContext(); |
| 388 | return Ctx.getDiagHandlerPtr()->isAnalysisRemarkEnabled(PassName: getPassName()) || |
| 389 | shouldAlwaysPrint(); |
| 390 | } |
| 391 | |
| 392 | void DiagnosticInfoMIRParser::print(DiagnosticPrinter &DP) const { |
| 393 | DP << Diagnostic; |
| 394 | } |
| 395 | |
| 396 | void DiagnosticInfoSrcMgr::print(DiagnosticPrinter &DP) const { |
| 397 | DP << Diagnostic; |
| 398 | } |
| 399 | |
| 400 | DiagnosticInfoOptimizationFailure::DiagnosticInfoOptimizationFailure( |
| 401 | const char *PassName, StringRef , const DiagnosticLocation &Loc, |
| 402 | const BasicBlock *CodeRegion) |
| 403 | : DiagnosticInfoIROptimization(DK_OptimizationFailure, DS_Warning, PassName, |
| 404 | RemarkName, *CodeRegion->getParent(), Loc, |
| 405 | CodeRegion) {} |
| 406 | |
| 407 | bool DiagnosticInfoOptimizationFailure::isEnabled() const { |
| 408 | // Only print warnings. |
| 409 | return getSeverity() == DS_Warning; |
| 410 | } |
| 411 | |
| 412 | void DiagnosticInfoUnsupported::print(DiagnosticPrinter &DP) const { |
| 413 | std::string Str; |
| 414 | raw_string_ostream(Str) << getLocationStr() << ": in function " |
| 415 | << getFunction().getName() << ' ' |
| 416 | << *getFunction().getFunctionType() << ": " << Msg |
| 417 | << '\n'; |
| 418 | DP << Str; |
| 419 | } |
| 420 | |
| 421 | void DiagnosticInfoInstrumentation::print(DiagnosticPrinter &DP) const { |
| 422 | DP << Msg; |
| 423 | } |
| 424 | |
| 425 | void DiagnosticInfoISelFallback::print(DiagnosticPrinter &DP) const { |
| 426 | DP << "Instruction selection used fallback path for " << getFunction(); |
| 427 | } |
| 428 | |
| 429 | void DiagnosticInfoOptimizationBase::insert(StringRef S) { |
| 430 | Args.emplace_back(Args&: S); |
| 431 | } |
| 432 | |
| 433 | void DiagnosticInfoOptimizationBase::insert(Argument A) { |
| 434 | Args.push_back(Elt: std::move(A)); |
| 435 | } |
| 436 | |
| 437 | void DiagnosticInfoOptimizationBase::insert(setIsVerbose V) { |
| 438 | IsVerbose = true; |
| 439 | } |
| 440 | |
| 441 | void DiagnosticInfoOptimizationBase::(setExtraArgs EA) { |
| 442 | FirstExtraArgIndex = Args.size(); |
| 443 | } |
| 444 | |
| 445 | std::string DiagnosticInfoOptimizationBase::getMsg() const { |
| 446 | std::string Str; |
| 447 | raw_string_ostream OS(Str); |
| 448 | for (const DiagnosticInfoOptimizationBase::Argument &Arg : |
| 449 | make_range(x: Args.begin(), y: FirstExtraArgIndex == -1 |
| 450 | ? Args.end() |
| 451 | : Args.begin() + FirstExtraArgIndex)) |
| 452 | OS << Arg.Val; |
| 453 | return Str; |
| 454 | } |
| 455 | |
| 456 | DiagnosticInfoMisExpect::DiagnosticInfoMisExpect(const Instruction *Inst, |
| 457 | const Twine &Msg) |
| 458 | : DiagnosticInfoWithLocationBase(DK_MisExpect, DS_Warning, |
| 459 | *Inst->getParent()->getParent(), |
| 460 | Inst->getDebugLoc()), |
| 461 | Msg(Msg) {} |
| 462 | |
| 463 | void DiagnosticInfoMisExpect::print(DiagnosticPrinter &DP) const { |
| 464 | DP << getLocationStr() << ": " << getMsg(); |
| 465 | } |
| 466 | |
| 467 | void OptimizationRemarkAnalysisFPCommute::() {} |
| 468 | void OptimizationRemarkAnalysisAliasing::() {} |
| 469 | |
| 470 | void llvm::diagnoseDontCall(const CallInst &CI) { |
| 471 | const auto *F = |
| 472 | dyn_cast<Function>(Val: CI.getCalledOperand()->stripPointerCasts()); |
| 473 | |
| 474 | if (!F) |
| 475 | return; |
| 476 | |
| 477 | for (int i = 0; i != 2; ++i) { |
| 478 | auto AttrName = i == 0 ? "dontcall-error" : "dontcall-warn" ; |
| 479 | auto Sev = i == 0 ? DS_Error : DS_Warning; |
| 480 | |
| 481 | if (F->hasFnAttribute(Kind: AttrName)) { |
| 482 | uint64_t LocCookie = 0; |
| 483 | auto A = F->getFnAttribute(Kind: AttrName); |
| 484 | if (MDNode *MD = CI.getMetadata(Kind: "srcloc" )) |
| 485 | LocCookie = |
| 486 | mdconst::extract<ConstantInt>(MD: MD->getOperand(I: 0))->getZExtValue(); |
| 487 | DiagnosticInfoDontCall D(F->getName(), A.getValueAsString(), Sev, |
| 488 | LocCookie); |
| 489 | F->getContext().diagnose(DI: D); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | void DiagnosticInfoDontCall::print(DiagnosticPrinter &DP) const { |
| 495 | DP << "call to " << demangle(MangledName: getFunctionName()) << " marked \"dontcall-" ; |
| 496 | if (getSeverity() == DiagnosticSeverity::DS_Error) |
| 497 | DP << "error\"" ; |
| 498 | else |
| 499 | DP << "warn\"" ; |
| 500 | if (!getNote().empty()) |
| 501 | DP << ": " << getNote(); |
| 502 | } |
| 503 | |