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