DiagnosticHandler.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // -pass-remarks-missed
  46. // Command line flag to enable emitOptimizationRemarkMissed()
  47. static cl::opt<PassRemarksOpt, true, cl::parser<std::string>> PassRemarksMissed(
  48. "pass-remarks-missed", cl::value_desc("pattern"),
  49. cl::desc("Enable missed optimization remarks from passes whose name match "
  50. "the given regular expression"),
  51. cl::Hidden, cl::location(PassRemarksMissedOptLoc), cl::ValueRequired);
  52. // -pass-remarks-analysis
  53. // Command line flag to enable emitOptimizationRemarkAnalysis()
  54. static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
  55. PassRemarksAnalysis(
  56. "pass-remarks-analysis", cl::value_desc("pattern"),
  57. cl::desc(
  58. "Enable optimization analysis remarks from passes whose name match "
  59. "the given regular expression"),
  60. cl::Hidden, cl::location(PassRemarksAnalysisOptLoc), cl::ValueRequired);
  61. }
  62. bool DiagnosticHandler::isAnalysisRemarkEnabled(StringRef PassName) const {
  63. return (PassRemarksAnalysisOptLoc.Pattern &&
  64. PassRemarksAnalysisOptLoc.Pattern->match(PassName));
  65. }
  66. bool DiagnosticHandler::isMissedOptRemarkEnabled(StringRef PassName) const {
  67. return (PassRemarksMissedOptLoc.Pattern &&
  68. PassRemarksMissedOptLoc.Pattern->match(PassName));
  69. }
  70. bool DiagnosticHandler::isPassedOptRemarkEnabled(StringRef PassName) const {
  71. return (PassRemarksPassedOptLoc.Pattern &&
  72. PassRemarksPassedOptLoc.Pattern->match(PassName));
  73. }
  74. bool DiagnosticHandler::isAnyRemarkEnabled() const {
  75. return (PassRemarksPassedOptLoc.Pattern || PassRemarksMissedOptLoc.Pattern ||
  76. PassRemarksAnalysisOptLoc.Pattern);
  77. }