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 <cassert>
16#include <optional>
17
18using namespace llvm;
19using namespace llvm::remarks;
20
21static cl::opt<cl::boolOrDefault> EnableRemarksSection(
22 "remarks-section",
23 cl::desc(
24 "Emit a section containing remark diagnostics metadata. By default, "
25 "this is enabled for the following formats: bitstream."),
26 cl::init(Val: cl::BOU_UNSET), cl::Hidden);
27
28RemarkStreamer::RemarkStreamer(
29 std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer,
30 std::optional<StringRef> FilenameIn)
31 : RemarkSerializer(std::move(RemarkSerializer)),
32 Filename(FilenameIn ? std::optional<std::string>(FilenameIn->str())
33 : std::nullopt) {}
34
35RemarkStreamer::~RemarkStreamer() {
36 // Ensure that llvm::finalizeOptimizationRemarks was called before the
37 // RemarkStreamer is destroyed.
38 assert(!RemarkSerializer &&
39 "RemarkSerializer must be released before RemarkStreamer is "
40 "destroyed. Ensure llvm::finalizeOptimizationRemarks is called.");
41}
42
43Error RemarkStreamer::setFilter(StringRef Filter) {
44 Regex R = Regex(Filter);
45 std::string RegexError;
46 if (!R.isValid(Error&: RegexError))
47 return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument),
48 S: RegexError.data());
49 PassFilter = std::move(R);
50 return Error::success();
51}
52
53bool RemarkStreamer::matchesFilter(StringRef Str) {
54 if (PassFilter)
55 return PassFilter->match(String: Str);
56 // No filter means all strings pass.
57 return true;
58}
59
60bool RemarkStreamer::needsSection() const {
61 if (EnableRemarksSection == cl::BOU_TRUE)
62 return true;
63
64 if (EnableRemarksSection == cl::BOU_FALSE)
65 return false;
66
67 assert(EnableRemarksSection == cl::BOU_UNSET);
68
69 // Enable remark sections by default for bitstream remarks (so dsymutil can
70 // find all remarks for a linked binary)
71 return RemarkSerializer->SerializerFormat == Format::Bitstream;
72}
73