RemarkStreamer.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Remarks/RemarkStreamer.h ----------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the main interface for streaming remarks.
  15. //
  16. // This is used to stream any llvm::remarks::Remark to an open file taking
  17. // advantage of all the serialization capabilities developed for remarks (e.g.
  18. // metadata in a section, bitstream format, etc.).
  19. //
  20. // Typically, a specialized remark emitter should hold a reference to the main
  21. // remark streamer set up in the LLVMContext, and should convert specialized
  22. // diagnostics to llvm::remarks::Remark objects as they get emitted.
  23. //
  24. // Specialized remark emitters can be components like:
  25. // * Remarks from LLVM (M)IR passes
  26. // * Remarks from the frontend
  27. // * Remarks from an intermediate IR
  28. //
  29. // This allows for composition between specialized remark emitters throughout
  30. // the compilation pipeline, that end up in the same file, using the same format
  31. // and serialization techniques.
  32. //
  33. //===----------------------------------------------------------------------===//
  34. #ifndef LLVM_REMARKS_REMARKSTREAMER_H
  35. #define LLVM_REMARKS_REMARKSTREAMER_H
  36. #include "llvm/Remarks/RemarkSerializer.h"
  37. #include "llvm/Support/Error.h"
  38. #include "llvm/Support/Regex.h"
  39. #include <memory>
  40. #include <optional>
  41. namespace llvm {
  42. class raw_ostream;
  43. namespace remarks {
  44. class RemarkStreamer final {
  45. /// The regex used to filter remarks based on the passes that emit them.
  46. std::optional<Regex> PassFilter;
  47. /// The object used to serialize the remarks to a specific format.
  48. std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer;
  49. /// The filename that the remark diagnostics are emitted to.
  50. const std::optional<std::string> Filename;
  51. public:
  52. RemarkStreamer(std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer,
  53. std::optional<StringRef> Filename = std::nullopt);
  54. /// Return the filename that the remark diagnostics are emitted to.
  55. std::optional<StringRef> getFilename() const {
  56. return Filename ? std::optional<StringRef>(*Filename) : std::nullopt;
  57. }
  58. /// Return stream that the remark diagnostics are emitted to.
  59. raw_ostream &getStream() { return RemarkSerializer->OS; }
  60. /// Return the serializer used for this stream.
  61. remarks::RemarkSerializer &getSerializer() { return *RemarkSerializer; }
  62. /// Set a pass filter based on a regex \p Filter.
  63. /// Returns an error if the regex is invalid.
  64. Error setFilter(StringRef Filter);
  65. /// Check wether the string matches the filter.
  66. bool matchesFilter(StringRef Str);
  67. /// Check if the remarks also need to have associated metadata in a section.
  68. bool needsSection() const;
  69. };
  70. } // end namespace remarks
  71. } // end namespace llvm
  72. #endif // LLVM_REMARKS_REMARKSTREAMER_H
  73. #ifdef __GNUC__
  74. #pragma GCC diagnostic pop
  75. #endif