DiagnosticInfo.cpp 15 KB

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