DiagnosticInfo.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. #include "llvm/IR/DiagnosticInfo.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/ADT/Twine.h"
  16. #include "llvm/ADT/iterator_range.h"
  17. #include "llvm/IR/BasicBlock.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/DebugInfoMetadata.h"
  20. #include "llvm/IR/DerivedTypes.h"
  21. #include "llvm/IR/DiagnosticPrinter.h"
  22. #include "llvm/IR/Function.h"
  23. #include "llvm/IR/GlobalValue.h"
  24. #include "llvm/IR/Instruction.h"
  25. #include "llvm/IR/Instructions.h"
  26. #include "llvm/IR/LLVMContext.h"
  27. #include "llvm/IR/Metadata.h"
  28. #include "llvm/IR/Module.h"
  29. #include "llvm/IR/Type.h"
  30. #include "llvm/IR/Value.h"
  31. #include "llvm/Support/Casting.h"
  32. #include "llvm/Support/ErrorHandling.h"
  33. #include "llvm/Support/InstructionCost.h"
  34. #include "llvm/Support/Path.h"
  35. #include "llvm/Support/ScopedPrinter.h"
  36. #include "llvm/Support/raw_ostream.h"
  37. #include <atomic>
  38. #include <string>
  39. using namespace llvm;
  40. int llvm::getNextAvailablePluginDiagnosticKind() {
  41. static std::atomic<int> PluginKindID(DK_FirstPluginKind);
  42. return ++PluginKindID;
  43. }
  44. const char *OptimizationRemarkAnalysis::AlwaysPrint = "";
  45. DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
  46. const Twine &MsgStr,
  47. DiagnosticSeverity Severity)
  48. : DiagnosticInfo(DK_InlineAsm, Severity), MsgStr(MsgStr), Instr(&I) {
  49. if (const MDNode *SrcLoc = I.getMetadata("srcloc")) {
  50. if (SrcLoc->getNumOperands() != 0)
  51. if (const auto *CI =
  52. mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0)))
  53. LocCookie = CI->getZExtValue();
  54. }
  55. }
  56. void DiagnosticInfoInlineAsm::print(DiagnosticPrinter &DP) const {
  57. DP << getMsgStr();
  58. if (getLocCookie())
  59. DP << " at line " << getLocCookie();
  60. }
  61. DiagnosticInfoResourceLimit::DiagnosticInfoResourceLimit(
  62. const Function &Fn, const char *ResourceName, uint64_t ResourceSize,
  63. uint64_t ResourceLimit, DiagnosticSeverity Severity, DiagnosticKind Kind)
  64. : DiagnosticInfoWithLocationBase(Kind, Severity, Fn, Fn.getSubprogram()),
  65. Fn(Fn), ResourceName(ResourceName), ResourceSize(ResourceSize),
  66. ResourceLimit(ResourceLimit) {}
  67. void DiagnosticInfoResourceLimit::print(DiagnosticPrinter &DP) const {
  68. DP << getLocationStr() << ": " << getResourceName() << " ("
  69. << getResourceSize() << ") exceeds limit (" << getResourceLimit()
  70. << ") in function '" << getFunction() << '\'';
  71. }
  72. void DiagnosticInfoDebugMetadataVersion::print(DiagnosticPrinter &DP) const {
  73. DP << "ignoring debug info with an invalid version (" << getMetadataVersion()
  74. << ") in " << getModule();
  75. }
  76. void DiagnosticInfoIgnoringInvalidDebugMetadata::print(
  77. DiagnosticPrinter &DP) const {
  78. DP << "ignoring invalid debug info in " << getModule().getModuleIdentifier();
  79. }
  80. void DiagnosticInfoSampleProfile::print(DiagnosticPrinter &DP) const {
  81. if (!FileName.empty()) {
  82. DP << getFileName();
  83. if (LineNum > 0)
  84. DP << ":" << getLineNum();
  85. DP << ": ";
  86. }
  87. DP << getMsg();
  88. }
  89. void DiagnosticInfoPGOProfile::print(DiagnosticPrinter &DP) const {
  90. if (getFileName())
  91. DP << getFileName() << ": ";
  92. DP << getMsg();
  93. }
  94. void DiagnosticInfo::anchor() {}
  95. void DiagnosticInfoStackSize::anchor() {}
  96. void DiagnosticInfoWithLocationBase::anchor() {}
  97. void DiagnosticInfoIROptimization::anchor() {}
  98. DiagnosticLocation::DiagnosticLocation(const DebugLoc &DL) {
  99. if (!DL)
  100. return;
  101. File = DL->getFile();
  102. Line = DL->getLine();
  103. Column = DL->getColumn();
  104. }
  105. DiagnosticLocation::DiagnosticLocation(const DISubprogram *SP) {
  106. if (!SP)
  107. return;
  108. File = SP->getFile();
  109. Line = SP->getScopeLine();
  110. Column = 0;
  111. }
  112. StringRef DiagnosticLocation::getRelativePath() const {
  113. return File->getFilename();
  114. }
  115. std::string DiagnosticLocation::getAbsolutePath() const {
  116. StringRef Name = File->getFilename();
  117. if (sys::path::is_absolute(Name))
  118. return std::string(Name);
  119. SmallString<128> Path;
  120. sys::path::append(Path, File->getDirectory(), Name);
  121. return sys::path::remove_leading_dotslash(Path).str();
  122. }
  123. std::string DiagnosticInfoWithLocationBase::getAbsolutePath() const {
  124. return Loc.getAbsolutePath();
  125. }
  126. void DiagnosticInfoWithLocationBase::getLocation(StringRef &RelativePath,
  127. unsigned &Line,
  128. unsigned &Column) const {
  129. RelativePath = Loc.getRelativePath();
  130. Line = Loc.getLine();
  131. Column = Loc.getColumn();
  132. }
  133. std::string DiagnosticInfoWithLocationBase::getLocationStr() const {
  134. StringRef Filename("<unknown>");
  135. unsigned Line = 0;
  136. unsigned Column = 0;
  137. if (isLocationAvailable())
  138. getLocation(Filename, Line, Column);
  139. return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
  140. }
  141. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
  142. const Value *V)
  143. : Key(std::string(Key)) {
  144. if (auto *F = dyn_cast<Function>(V)) {
  145. if (DISubprogram *SP = F->getSubprogram())
  146. Loc = SP;
  147. }
  148. else if (auto *I = dyn_cast<Instruction>(V))
  149. Loc = I->getDebugLoc();
  150. // Only include names that correspond to user variables. FIXME: We should use
  151. // debug info if available to get the name of the user variable.
  152. if (isa<llvm::Argument>(V) || isa<GlobalValue>(V))
  153. Val = std::string(GlobalValue::dropLLVMManglingEscape(V->getName()));
  154. else if (isa<Constant>(V)) {
  155. raw_string_ostream OS(Val);
  156. V->printAsOperand(OS, /*PrintType=*/false);
  157. } else if (auto *I = dyn_cast<Instruction>(V))
  158. Val = I->getOpcodeName();
  159. }
  160. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, const Type *T)
  161. : Key(std::string(Key)) {
  162. raw_string_ostream OS(Val);
  163. OS << *T;
  164. }
  165. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, StringRef S)
  166. : Key(std::string(Key)), Val(S.str()) {}
  167. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, int N)
  168. : Key(std::string(Key)), Val(itostr(N)) {}
  169. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, float N)
  170. : Key(std::string(Key)), Val(llvm::to_string(N)) {}
  171. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, long N)
  172. : Key(std::string(Key)), Val(itostr(N)) {}
  173. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, long long N)
  174. : Key(std::string(Key)), Val(itostr(N)) {}
  175. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, unsigned N)
  176. : Key(std::string(Key)), Val(utostr(N)) {}
  177. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
  178. unsigned long N)
  179. : Key(std::string(Key)), Val(utostr(N)) {}
  180. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
  181. unsigned long long N)
  182. : Key(std::string(Key)), Val(utostr(N)) {}
  183. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
  184. ElementCount EC)
  185. : Key(std::string(Key)) {
  186. raw_string_ostream OS(Val);
  187. EC.print(OS);
  188. }
  189. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
  190. InstructionCost C)
  191. : Key(std::string(Key)) {
  192. raw_string_ostream OS(Val);
  193. C.print(OS);
  194. }
  195. DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, DebugLoc Loc)
  196. : Key(std::string(Key)), Loc(Loc) {
  197. if (Loc) {
  198. Val = (Loc->getFilename() + ":" + Twine(Loc.getLine()) + ":" +
  199. Twine(Loc.getCol())).str();
  200. } else {
  201. Val = "<UNKNOWN LOCATION>";
  202. }
  203. }
  204. void DiagnosticInfoOptimizationBase::print(DiagnosticPrinter &DP) const {
  205. DP << getLocationStr() << ": " << getMsg();
  206. if (Hotness)
  207. DP << " (hotness: " << *Hotness << ")";
  208. }
  209. OptimizationRemark::OptimizationRemark(const char *PassName,
  210. StringRef RemarkName,
  211. const DiagnosticLocation &Loc,
  212. const Value *CodeRegion)
  213. : DiagnosticInfoIROptimization(
  214. DK_OptimizationRemark, DS_Remark, PassName, RemarkName,
  215. *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
  216. OptimizationRemark::OptimizationRemark(const char *PassName,
  217. StringRef RemarkName,
  218. const Instruction *Inst)
  219. : DiagnosticInfoIROptimization(DK_OptimizationRemark, DS_Remark, PassName,
  220. RemarkName, *Inst->getParent()->getParent(),
  221. Inst->getDebugLoc(), Inst->getParent()) {}
  222. static const BasicBlock *getFirstFunctionBlock(const Function *Func) {
  223. return Func->empty() ? nullptr : &Func->front();
  224. }
  225. OptimizationRemark::OptimizationRemark(const char *PassName,
  226. StringRef RemarkName,
  227. const Function *Func)
  228. : DiagnosticInfoIROptimization(DK_OptimizationRemark, DS_Remark, PassName,
  229. RemarkName, *Func, Func->getSubprogram(),
  230. getFirstFunctionBlock(Func)) {}
  231. bool OptimizationRemark::isEnabled() const {
  232. const Function &Fn = getFunction();
  233. LLVMContext &Ctx = Fn.getContext();
  234. return Ctx.getDiagHandlerPtr()->isPassedOptRemarkEnabled(getPassName());
  235. }
  236. OptimizationRemarkMissed::OptimizationRemarkMissed(
  237. const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
  238. const Value *CodeRegion)
  239. : DiagnosticInfoIROptimization(
  240. DK_OptimizationRemarkMissed, DS_Remark, PassName, RemarkName,
  241. *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
  242. OptimizationRemarkMissed::OptimizationRemarkMissed(const char *PassName,
  243. StringRef RemarkName,
  244. const Instruction *Inst)
  245. : DiagnosticInfoIROptimization(DK_OptimizationRemarkMissed, DS_Remark,
  246. PassName, RemarkName,
  247. *Inst->getParent()->getParent(),
  248. Inst->getDebugLoc(), Inst->getParent()) {}
  249. OptimizationRemarkMissed::OptimizationRemarkMissed(const char *PassName,
  250. StringRef RemarkName,
  251. const Function *Func)
  252. : DiagnosticInfoIROptimization(
  253. DK_OptimizationRemarkMissed, DS_Remark, PassName, RemarkName, *Func,
  254. Func->getSubprogram(), getFirstFunctionBlock(Func)) {}
  255. bool OptimizationRemarkMissed::isEnabled() const {
  256. const Function &Fn = getFunction();
  257. LLVMContext &Ctx = Fn.getContext();
  258. return Ctx.getDiagHandlerPtr()->isMissedOptRemarkEnabled(getPassName());
  259. }
  260. OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(
  261. const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
  262. const Value *CodeRegion)
  263. : DiagnosticInfoIROptimization(
  264. DK_OptimizationRemarkAnalysis, DS_Remark, PassName, RemarkName,
  265. *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
  266. OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(const char *PassName,
  267. StringRef RemarkName,
  268. const Instruction *Inst)
  269. : DiagnosticInfoIROptimization(DK_OptimizationRemarkAnalysis, DS_Remark,
  270. PassName, RemarkName,
  271. *Inst->getParent()->getParent(),
  272. Inst->getDebugLoc(), Inst->getParent()) {}
  273. OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(
  274. enum DiagnosticKind Kind, const char *PassName, StringRef RemarkName,
  275. const DiagnosticLocation &Loc, const Value *CodeRegion)
  276. : DiagnosticInfoIROptimization(Kind, DS_Remark, PassName, RemarkName,
  277. *cast<BasicBlock>(CodeRegion)->getParent(),
  278. Loc, CodeRegion) {}
  279. OptimizationRemarkAnalysis::OptimizationRemarkAnalysis(const char *PassName,
  280. StringRef RemarkName,
  281. const Function *Func)
  282. : DiagnosticInfoIROptimization(
  283. DK_OptimizationRemarkAnalysis, DS_Remark, PassName, RemarkName, *Func,
  284. Func->getSubprogram(), getFirstFunctionBlock(Func)) {}
  285. bool OptimizationRemarkAnalysis::isEnabled() const {
  286. const Function &Fn = getFunction();
  287. LLVMContext &Ctx = Fn.getContext();
  288. return Ctx.getDiagHandlerPtr()->isAnalysisRemarkEnabled(getPassName()) ||
  289. shouldAlwaysPrint();
  290. }
  291. void DiagnosticInfoMIRParser::print(DiagnosticPrinter &DP) const {
  292. DP << Diagnostic;
  293. }
  294. void DiagnosticInfoSrcMgr::print(DiagnosticPrinter &DP) const {
  295. DP << Diagnostic;
  296. }
  297. DiagnosticInfoOptimizationFailure::DiagnosticInfoOptimizationFailure(
  298. const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
  299. const Value *CodeRegion)
  300. : DiagnosticInfoIROptimization(
  301. DK_OptimizationFailure, DS_Warning, PassName, RemarkName,
  302. *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
  303. bool DiagnosticInfoOptimizationFailure::isEnabled() const {
  304. // Only print warnings.
  305. return getSeverity() == DS_Warning;
  306. }
  307. void DiagnosticInfoUnsupported::print(DiagnosticPrinter &DP) const {
  308. std::string Str;
  309. raw_string_ostream OS(Str);
  310. OS << getLocationStr() << ": in function " << getFunction().getName() << ' '
  311. << *getFunction().getFunctionType() << ": " << Msg << '\n';
  312. OS.flush();
  313. DP << Str;
  314. }
  315. void DiagnosticInfoISelFallback::print(DiagnosticPrinter &DP) const {
  316. DP << "Instruction selection used fallback path for " << getFunction();
  317. }
  318. void DiagnosticInfoOptimizationBase::insert(StringRef S) {
  319. Args.emplace_back(S);
  320. }
  321. void DiagnosticInfoOptimizationBase::insert(Argument A) {
  322. Args.push_back(std::move(A));
  323. }
  324. void DiagnosticInfoOptimizationBase::insert(setIsVerbose V) {
  325. IsVerbose = true;
  326. }
  327. void DiagnosticInfoOptimizationBase::insert(setExtraArgs EA) {
  328. FirstExtraArgIndex = Args.size();
  329. }
  330. std::string DiagnosticInfoOptimizationBase::getMsg() const {
  331. std::string Str;
  332. raw_string_ostream OS(Str);
  333. for (const DiagnosticInfoOptimizationBase::Argument &Arg :
  334. make_range(Args.begin(), FirstExtraArgIndex == -1
  335. ? Args.end()
  336. : Args.begin() + FirstExtraArgIndex))
  337. OS << Arg.Val;
  338. return OS.str();
  339. }
  340. DiagnosticInfoMisExpect::DiagnosticInfoMisExpect(const Instruction *Inst,
  341. Twine &Msg)
  342. : DiagnosticInfoWithLocationBase(DK_MisExpect, DS_Warning,
  343. *Inst->getParent()->getParent(),
  344. Inst->getDebugLoc()),
  345. Msg(Msg) {}
  346. void DiagnosticInfoMisExpect::print(DiagnosticPrinter &DP) const {
  347. DP << getLocationStr() << ": " << getMsg();
  348. }
  349. void OptimizationRemarkAnalysisFPCommute::anchor() {}
  350. void OptimizationRemarkAnalysisAliasing::anchor() {}
  351. void llvm::diagnoseDontCall(const CallInst &CI) {
  352. const auto *F =
  353. dyn_cast<Function>(CI.getCalledOperand()->stripPointerCasts());
  354. if (!F)
  355. return;
  356. for (int i = 0; i != 2; ++i) {
  357. auto AttrName = i == 0 ? "dontcall-error" : "dontcall-warn";
  358. auto Sev = i == 0 ? DS_Error : DS_Warning;
  359. if (F->hasFnAttribute(AttrName)) {
  360. unsigned LocCookie = 0;
  361. auto A = F->getFnAttribute(AttrName);
  362. if (MDNode *MD = CI.getMetadata("srcloc"))
  363. LocCookie =
  364. mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue();
  365. DiagnosticInfoDontCall D(F->getName(), A.getValueAsString(), Sev,
  366. LocCookie);
  367. F->getContext().diagnose(D);
  368. }
  369. }
  370. }
  371. void DiagnosticInfoDontCall::print(DiagnosticPrinter &DP) const {
  372. DP << "call to " << getFunctionName() << " marked \"dontcall-";
  373. if (getSeverity() == DiagnosticSeverity::DS_Error)
  374. DP << "error\"";
  375. else
  376. DP << "warn\"";
  377. if (!getNote().empty())
  378. DP << ": " << getNote();
  379. }