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