1 //===-- RemarkSerializer.h - Remark serialization interface -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file provides an interface for serializing remarks to different formats.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_REMARKS_REMARK_SERIALIZER_H
14 #define LLVM_REMARKS_REMARK_SERIALIZER_H
16 #include "llvm/Remarks/Remark.h"
17 #include "llvm/Remarks/RemarkFormat.h"
18 #include "llvm/Remarks/RemarkStringTable.h"
19 #include "llvm/Support/raw_ostream.h"
24 enum class SerializerMode
{
25 Separate
, // A mode where the metadata is serialized separately from the
26 // remarks. Typically, this is used when the remarks need to be
27 // streamed to a side file and the metadata is embedded into the
28 // final result of the compilation.
29 Standalone
// A mode where everything can be retrieved in the same
30 // file/buffer. Typically, this is used for storing remarks for
34 struct MetaSerializer
;
36 /// This is the base class for a remark serializer.
37 /// It includes support for using a string table while emitting.
38 struct RemarkSerializer
{
39 /// The open raw_ostream that the remark diagnostics are emitted to.
41 /// The serialization mode.
43 /// The string table containing all the unique strings used in the output.
44 /// The table can be serialized to be consumed after the compilation.
45 Optional
<StringTable
> StrTab
;
47 RemarkSerializer(raw_ostream
&OS
, SerializerMode Mode
)
48 : OS(OS
), Mode(Mode
), StrTab() {}
50 /// This is just an interface.
51 virtual ~RemarkSerializer() = default;
52 /// Emit a remark to the stream.
53 virtual void emit(const Remark
&Remark
) = 0;
54 /// Return the corresponding metadata serializer.
55 virtual std::unique_ptr
<MetaSerializer
>
56 metaSerializer(raw_ostream
&OS
,
57 Optional
<StringRef
> ExternalFilename
= None
) = 0;
60 /// This is the base class for a remark metadata serializer.
61 struct MetaSerializer
{
62 /// The open raw_ostream that the metadata is emitted to.
65 MetaSerializer(raw_ostream
&OS
) : OS(OS
) {}
67 /// This is just an interface.
68 virtual ~MetaSerializer() = default;
69 virtual void emit() = 0;
72 /// Create a remark serializer.
73 Expected
<std::unique_ptr
<RemarkSerializer
>>
74 createRemarkSerializer(Format RemarksFormat
, SerializerMode Mode
, raw_ostream
&OS
);
76 /// Create a remark serializer that uses a pre-filled string table.
77 Expected
<std::unique_ptr
<RemarkSerializer
>>
78 createRemarkSerializer(Format RemarksFormat
, SerializerMode Mode
,
79 raw_ostream
&OS
, remarks::StringTable StrTab
);
81 } // end namespace remarks
82 } // end namespace llvm
84 #endif /* LLVM_REMARKS_REMARK_SERIALIZER_H */