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