RemarkStreamer.cpp 2.3 KB

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