1 //===-- RemarkSerializer.h - Remark serialization interface -----*- 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 different formats.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_REMARKS_REMARK_SERIALIZER_H
14 #define LLVM_REMARKS_REMARK_SERIALIZER_H
15 
16 #include "llvm/Remarks/Remark.h"
17 #include "llvm/Remarks/RemarkStringTable.h"
18 #include "llvm/Support/YAMLTraits.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 namespace llvm {
22 namespace remarks {
23 
24 /// This is the base class for a remark serializer.
25 /// It includes support for using a string table while emitting.
26 struct Serializer {
27   /// The open raw_ostream that the remark diagnostics are emitted to.
28   raw_ostream &OS;
29   /// The string table containing all the unique strings used in the output.
30   /// The table can be serialized to be consumed after the compilation.
31   Optional<StringTable> StrTab;
32 
33   Serializer(raw_ostream &OS) : OS(OS), StrTab() {}
34 
35   /// This is just an interface.
36   virtual ~Serializer() = default;
37   virtual void emit(const Remark &Remark) = 0;
38 };
39 
40 /// Wether the serializer should use a string table while emitting.
41 enum class UseStringTable { No, Yes };
42 
43 /// Serialize the remarks to YAML. One remark entry looks like this:
44 /// --- !<TYPE>
45 /// Pass:            <PASSNAME>
46 /// Name:            <REMARKNAME>
47 /// DebugLoc:        { File: <SOURCEFILENAME>, Line: <SOURCELINE>,
48 ///                    Column: <SOURCECOLUMN> }
49 /// Function:        <FUNCTIONNAME>
50 /// Args:
51 ///   - <KEY>: <VALUE>
52 ///     DebugLoc:        { File: <FILE>, Line: <LINE>, Column: <COL> }
53 /// ...
54 struct YAMLSerializer : public Serializer {
55   /// The YAML streamer.
56   yaml::Output YAMLOutput;
57 
58   YAMLSerializer(raw_ostream &OS,
59                  UseStringTable UseStringTable = remarks::UseStringTable::No);
60 
61   /// Emit a remark to the stream.
62   void emit(const Remark &Remark) override;
63 };
64 
65 } // end namespace remarks
66 } // end namespace llvm
67 
68 #endif /* LLVM_REMARKS_REMARK_SERIALIZER_H */
69