YAMLRemarkSerializer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- YAMLRemarkSerializer.h - YAML Remark serialization ---*- 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 provides an interface for serializing remarks to YAML.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_REMARKS_YAMLREMARKSERIALIZER_H
  18. #define LLVM_REMARKS_YAMLREMARKSERIALIZER_H
  19. #include "llvm/Remarks/RemarkSerializer.h"
  20. #include "llvm/Support/YAMLTraits.h"
  21. #include <optional>
  22. namespace llvm {
  23. namespace remarks {
  24. /// Serialize the remarks to YAML. One remark entry looks like this:
  25. /// --- !<TYPE>
  26. /// Pass: <PASSNAME>
  27. /// Name: <REMARKNAME>
  28. /// DebugLoc: { File: <SOURCEFILENAME>, Line: <SOURCELINE>,
  29. /// Column: <SOURCECOLUMN> }
  30. /// Function: <FUNCTIONNAME>
  31. /// Args:
  32. /// - <KEY>: <VALUE>
  33. /// DebugLoc: { File: <FILE>, Line: <LINE>, Column: <COL> }
  34. /// ...
  35. struct YAMLRemarkSerializer : public RemarkSerializer {
  36. /// The YAML streamer.
  37. yaml::Output YAMLOutput;
  38. YAMLRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
  39. std::optional<StringTable> StrTab = std::nullopt);
  40. void emit(const Remark &Remark) override;
  41. std::unique_ptr<MetaSerializer> metaSerializer(
  42. raw_ostream &OS,
  43. std::optional<StringRef> ExternalFilename = std::nullopt) override;
  44. static bool classof(const RemarkSerializer *S) {
  45. return S->SerializerFormat == Format::YAML;
  46. }
  47. protected:
  48. YAMLRemarkSerializer(Format SerializerFormat, raw_ostream &OS,
  49. SerializerMode Mode,
  50. std::optional<StringTable> StrTab = std::nullopt);
  51. };
  52. struct YAMLMetaSerializer : public MetaSerializer {
  53. std::optional<StringRef> ExternalFilename;
  54. YAMLMetaSerializer(raw_ostream &OS, std::optional<StringRef> ExternalFilename)
  55. : MetaSerializer(OS), ExternalFilename(ExternalFilename) {}
  56. void emit() override;
  57. };
  58. /// Serialize the remarks to YAML using a string table. An remark entry looks
  59. /// like the regular YAML remark but instead of string entries it's using
  60. /// numbers that map to an index in the string table.
  61. struct YAMLStrTabRemarkSerializer : public YAMLRemarkSerializer {
  62. /// Wether we already emitted the metadata in standalone mode.
  63. /// This should be set to true after the first invocation of `emit`.
  64. bool DidEmitMeta = false;
  65. YAMLStrTabRemarkSerializer(raw_ostream &OS, SerializerMode Mode)
  66. : YAMLRemarkSerializer(Format::YAMLStrTab, OS, Mode) {
  67. // We always need a string table for this type of serializer.
  68. StrTab.emplace();
  69. }
  70. YAMLStrTabRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
  71. StringTable StrTab)
  72. : YAMLRemarkSerializer(Format::YAMLStrTab, OS, Mode, std::move(StrTab)) {}
  73. /// Override to emit the metadata if necessary.
  74. void emit(const Remark &Remark) override;
  75. std::unique_ptr<MetaSerializer> metaSerializer(
  76. raw_ostream &OS,
  77. std::optional<StringRef> ExternalFilename = std::nullopt) override;
  78. static bool classof(const RemarkSerializer *S) {
  79. return S->SerializerFormat == Format::YAMLStrTab;
  80. }
  81. };
  82. struct YAMLStrTabMetaSerializer : public YAMLMetaSerializer {
  83. /// The string table is part of the metadata.
  84. const StringTable &StrTab;
  85. YAMLStrTabMetaSerializer(raw_ostream &OS,
  86. std::optional<StringRef> ExternalFilename,
  87. const StringTable &StrTab)
  88. : YAMLMetaSerializer(OS, ExternalFilename), StrTab(StrTab) {}
  89. void emit() override;
  90. };
  91. } // end namespace remarks
  92. } // end namespace llvm
  93. #endif // LLVM_REMARKS_YAMLREMARKSERIALIZER_H
  94. #ifdef __GNUC__
  95. #pragma GCC diagnostic pop
  96. #endif