RemarkStreamer.cpp 2.3 KB

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