RemarkStreamer.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //===- llvm/Remarks/RemarkStreamer.h ----------------------------*- 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 declares the main interface for streaming remarks.
  10. //
  11. // This is used to stream any llvm::remarks::Remark to an open file taking
  12. // advantage of all the serialization capabilities developed for remarks (e.g.
  13. // metadata in a section, bitstream format, etc.).
  14. //
  15. // Typically, a specialized remark emitter should hold a reference to the main
  16. // remark streamer set up in the LLVMContext, and should convert specialized
  17. // diagnostics to llvm::remarks::Remark objects as they get emitted.
  18. //
  19. // Specialized remark emitters can be components like:
  20. // * Remarks from LLVM (M)IR passes
  21. // * Remarks from the frontend
  22. // * Remarks from an intermediate IR
  23. //
  24. // This allows for composition between specialized remark emitters throughout
  25. // the compilation pipeline, that end up in the same file, using the same format
  26. // and serialization techniques.
  27. //
  28. //===----------------------------------------------------------------------===//
  29. #ifndef LLVM_REMARKS_REMARKSTREAMER_H
  30. #define LLVM_REMARKS_REMARKSTREAMER_H
  31. #include "llvm/ADT/Optional.h"
  32. #include "llvm/Remarks/RemarkSerializer.h"
  33. #include "llvm/Support/Error.h"
  34. #include "llvm/Support/Regex.h"
  35. #include "llvm/Support/raw_ostream.h"
  36. #include <memory>
  37. namespace llvm {
  38. namespace remarks {
  39. class RemarkStreamer final {
  40. /// The regex used to filter remarks based on the passes that emit them.
  41. Optional<Regex> PassFilter;
  42. /// The object used to serialize the remarks to a specific format.
  43. std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer;
  44. /// The filename that the remark diagnostics are emitted to.
  45. const Optional<std::string> Filename;
  46. public:
  47. RemarkStreamer(std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer,
  48. Optional<StringRef> Filename = None);
  49. /// Return the filename that the remark diagnostics are emitted to.
  50. Optional<StringRef> getFilename() const {
  51. return Filename ? Optional<StringRef>(*Filename) : None;
  52. }
  53. /// Return stream that the remark diagnostics are emitted to.
  54. raw_ostream &getStream() { return RemarkSerializer->OS; }
  55. /// Return the serializer used for this stream.
  56. remarks::RemarkSerializer &getSerializer() { return *RemarkSerializer; }
  57. /// Set a pass filter based on a regex \p Filter.
  58. /// Returns an error if the regex is invalid.
  59. Error setFilter(StringRef Filter);
  60. /// Check wether the string matches the filter.
  61. bool matchesFilter(StringRef Str);
  62. /// Check if the remarks also need to have associated metadata in a section.
  63. bool needsSection() const;
  64. };
  65. } // end namespace remarks
  66. } // end namespace llvm
  67. #endif // LLVM_REMARKS_REMARKSTREAMER_H