| 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: bitstream."), |
| 25 | cl::init(Val: 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(Error&: RegexError)) |
| 38 | return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument), |
| 39 | S: 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(String: 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 | switch (RemarkSerializer->SerializerFormat) { |
| 67 | case remarks::Format::Bitstream: |
| 68 | return true; |
| 69 | default: |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 |