YAMLRemarkSerializer.h 3.7 KB

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