DiagnosticHandler.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //===- DiagnosticHandler.h - DiagnosticHandler class for LLVM -------------===//
  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. //
  10. //===----------------------------------------------------------------------===//
  11. #include "llvm/IR/DiagnosticHandler.h"
  12. #include "llvm/Support/CommandLine.h"
  13. #include "llvm/Support/Regex.h"
  14. using namespace llvm;
  15. namespace {
  16. /// Regular expression corresponding to the value given in one of the
  17. /// -pass-remarks* command line flags. Passes whose name matches this regexp
  18. /// will emit a diagnostic when calling the associated diagnostic function
  19. /// (emitOptimizationRemark, emitOptimizationRemarkMissed or
  20. /// emitOptimizationRemarkAnalysis).
  21. struct PassRemarksOpt {
  22. std::shared_ptr<Regex> Pattern;
  23. void operator=(const std::string &Val) {
  24. // Create a regexp object to match pass names for emitOptimizationRemark.
  25. if (!Val.empty()) {
  26. Pattern = std::make_shared<Regex>(Val);
  27. std::string RegexError;
  28. if (!Pattern->isValid(RegexError))
  29. report_fatal_error(Twine("Invalid regular expression '") + Val +
  30. "' in -pass-remarks: " + RegexError,
  31. false);
  32. }
  33. }
  34. };
  35. static PassRemarksOpt PassRemarksPassedOptLoc;
  36. static PassRemarksOpt PassRemarksMissedOptLoc;
  37. static PassRemarksOpt PassRemarksAnalysisOptLoc;
  38. // -pass-remarks
  39. // Command line flag to enable emitOptimizationRemark()
  40. static cl::opt<PassRemarksOpt, true, cl::parser<std::string>> PassRemarks(
  41. "pass-remarks", cl::value_desc("pattern"),
  42. cl::desc("Enable optimization remarks from passes whose name match "
  43. "the given regular expression"),
  44. cl::Hidden, cl::location(PassRemarksPassedOptLoc), cl::ValueRequired,
  45. cl::ZeroOrMore);
  46. // -pass-remarks-missed
  47. // Command line flag to enable emitOptimizationRemarkMissed()
  48. static cl::opt<PassRemarksOpt, true, cl::parser<std::string>> PassRemarksMissed(
  49. "pass-remarks-missed", cl::value_desc("pattern"),
  50. cl::desc("Enable missed optimization remarks from passes whose name match "
  51. "the given regular expression"),
  52. cl::Hidden, cl::location(PassRemarksMissedOptLoc), cl::ValueRequired,
  53. cl::ZeroOrMore);
  54. // -pass-remarks-analysis
  55. // Command line flag to enable emitOptimizationRemarkAnalysis()
  56. static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
  57. PassRemarksAnalysis(
  58. "pass-remarks-analysis", cl::value_desc("pattern"),
  59. cl::desc(
  60. "Enable optimization analysis remarks from passes whose name match "
  61. "the given regular expression"),
  62. cl::Hidden, cl::location(PassRemarksAnalysisOptLoc), cl::ValueRequired,
  63. cl::ZeroOrMore);
  64. }
  65. bool DiagnosticHandler::isAnalysisRemarkEnabled(StringRef PassName) const {
  66. return (PassRemarksAnalysisOptLoc.Pattern &&
  67. PassRemarksAnalysisOptLoc.Pattern->match(PassName));
  68. }
  69. bool DiagnosticHandler::isMissedOptRemarkEnabled(StringRef PassName) const {
  70. return (PassRemarksMissedOptLoc.Pattern &&
  71. PassRemarksMissedOptLoc.Pattern->match(PassName));
  72. }
  73. bool DiagnosticHandler::isPassedOptRemarkEnabled(StringRef PassName) const {
  74. return (PassRemarksPassedOptLoc.Pattern &&
  75. PassRemarksPassedOptLoc.Pattern->match(PassName));
  76. }
  77. bool DiagnosticHandler::isAnyRemarkEnabled() const {
  78. return (PassRemarksPassedOptLoc.Pattern || PassRemarksMissedOptLoc.Pattern ||
  79. PassRemarksAnalysisOptLoc.Pattern);
  80. }