xref: /freebsd/contrib/llvm-project/llvm/lib/Remarks/RemarkStreamer.cpp (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 //===- llvm/Remarks/RemarkStreamer.cpp - Remark Streamer -*- 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 contains the implementation of the main remark streamer.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Remarks/RemarkStreamer.h"
14 #include "llvm/Support/CommandLine.h"
15 #include <optional>
16 
17 using namespace llvm;
18 using namespace llvm::remarks;
19 
20 static cl::opt<cl::boolOrDefault> EnableRemarksSection(
21     "remarks-section",
22     cl::desc(
23         "Emit a section containing remark diagnostics metadata. By default, "
24         "this is enabled for the following formats: yaml-strtab, bitstream."),
25     cl::init(cl::BOU_UNSET), cl::Hidden);
26 
27 RemarkStreamer::RemarkStreamer(
28     std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer,
29     std::optional<StringRef> FilenameIn)
30     : RemarkSerializer(std::move(RemarkSerializer)),
31       Filename(FilenameIn ? std::optional<std::string>(FilenameIn->str())
32                           : std::nullopt) {}
33 
34 Error RemarkStreamer::setFilter(StringRef Filter) {
35   Regex R = Regex(Filter);
36   std::string RegexError;
37   if (!R.isValid(RegexError))
38     return createStringError(std::make_error_code(std::errc::invalid_argument),
39                              RegexError.data());
40   PassFilter = std::move(R);
41   return Error::success();
42 }
43 
44 bool RemarkStreamer::matchesFilter(StringRef Str) {
45   if (PassFilter)
46     return PassFilter->match(Str);
47   // No filter means all strings pass.
48   return true;
49 }
50 
51 bool RemarkStreamer::needsSection() const {
52   if (EnableRemarksSection == cl::BOU_TRUE)
53     return true;
54 
55   if (EnableRemarksSection == cl::BOU_FALSE)
56     return false;
57 
58   assert(EnableRemarksSection == cl::BOU_UNSET);
59 
60   // We only need a section if we're in separate mode.
61   if (RemarkSerializer->Mode != remarks::SerializerMode::Separate)
62     return false;
63 
64   // Only some formats need a section:
65   // * bitstream
66   // * yaml-strtab
67   switch (RemarkSerializer->SerializerFormat) {
68   case remarks::Format::YAMLStrTab:
69   case remarks::Format::Bitstream:
70     return true;
71   default:
72     return false;
73   }
74 }
75