13
13
#ifndef LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H
14
14
#define LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H
15
15
16
+ #include " llvm/ADT/ArrayRef.h"
17
+ #include " llvm/ADT/StringRef.h"
18
+ #include " llvm/Bitstream/BitstreamReader.h"
16
19
#include " llvm/Remarks/BitstreamRemarkContainer.h"
17
- #include " llvm/Remarks/BitstreamRemarkParser.h"
18
20
#include " llvm/Remarks/RemarkFormat.h"
19
21
#include " llvm/Remarks/RemarkParser.h"
22
+ #include " llvm/Support/Error.h"
23
+ #include < array>
20
24
#include < cstdint>
21
25
#include < memory>
22
26
#include < optional>
@@ -26,6 +30,91 @@ namespace remarks {
26
30
27
31
struct Remark ;
28
32
33
+ // / Helper to parse a META_BLOCK for a bitstream remark container.
34
+ struct BitstreamMetaParserHelper {
35
+ // / The Bitstream reader.
36
+ BitstreamCursor &Stream;
37
+ // / Reference to the storage for the block info.
38
+ BitstreamBlockInfo &BlockInfo;
39
+ // / The parsed content: depending on the container type, some fields might be
40
+ // / empty.
41
+ std::optional<uint64_t > ContainerVersion;
42
+ std::optional<uint8_t > ContainerType;
43
+ std::optional<StringRef> StrTabBuf;
44
+ std::optional<StringRef> ExternalFilePath;
45
+ std::optional<uint64_t > RemarkVersion;
46
+
47
+ // / Continue parsing with \p Stream. \p Stream is expected to contain a
48
+ // / ENTER_SUBBLOCK to the META_BLOCK at the current position.
49
+ // / \p Stream is expected to have a BLOCKINFO_BLOCK set.
50
+ BitstreamMetaParserHelper (BitstreamCursor &Stream,
51
+ BitstreamBlockInfo &BlockInfo);
52
+
53
+ // / Parse the META_BLOCK and fill the available entries.
54
+ // / This helper does not check for the validity of the fields.
55
+ Error parse ();
56
+ };
57
+
58
+ // / Helper to parse a REMARK_BLOCK for a bitstream remark container.
59
+ struct BitstreamRemarkParserHelper {
60
+ // / The Bitstream reader.
61
+ BitstreamCursor &Stream;
62
+ // / The parsed content: depending on the remark, some fields might be empty.
63
+ std::optional<uint8_t > Type;
64
+ std::optional<uint64_t > RemarkNameIdx;
65
+ std::optional<uint64_t > PassNameIdx;
66
+ std::optional<uint64_t > FunctionNameIdx;
67
+ std::optional<uint64_t > SourceFileNameIdx;
68
+ std::optional<uint32_t > SourceLine;
69
+ std::optional<uint32_t > SourceColumn;
70
+ std::optional<uint64_t > Hotness;
71
+ struct Argument {
72
+ std::optional<uint64_t > KeyIdx;
73
+ std::optional<uint64_t > ValueIdx;
74
+ std::optional<uint64_t > SourceFileNameIdx;
75
+ std::optional<uint32_t > SourceLine;
76
+ std::optional<uint32_t > SourceColumn;
77
+ };
78
+ std::optional<ArrayRef<Argument>> Args;
79
+ // / Avoid re-allocating a vector every time.
80
+ SmallVector<Argument, 8 > TmpArgs;
81
+
82
+ // / Continue parsing with \p Stream. \p Stream is expected to contain a
83
+ // / ENTER_SUBBLOCK to the REMARK_BLOCK at the current position.
84
+ // / \p Stream is expected to have a BLOCKINFO_BLOCK set and to have already
85
+ // / parsed the META_BLOCK.
86
+ BitstreamRemarkParserHelper (BitstreamCursor &Stream);
87
+
88
+ // / Parse the REMARK_BLOCK and fill the available entries.
89
+ // / This helper does not check for the validity of the fields.
90
+ Error parse ();
91
+ };
92
+
93
+ // / Helper to parse any bitstream remark container.
94
+ struct BitstreamParserHelper {
95
+ // / The Bitstream reader.
96
+ BitstreamCursor Stream;
97
+ // / The block info block.
98
+ BitstreamBlockInfo BlockInfo;
99
+ // / Start parsing at \p Buffer.
100
+ BitstreamParserHelper (StringRef Buffer);
101
+ // / Parse the magic number.
102
+ Expected<std::array<char , 4 >> parseMagic ();
103
+ // / Parse the block info block containing all the abbrevs.
104
+ // / This needs to be called before calling any other parsing function.
105
+ Error parseBlockInfoBlock ();
106
+ // / Return true if the next block is a META_BLOCK. This function does not move
107
+ // / the cursor.
108
+ Expected<bool > isMetaBlock ();
109
+ // / Return true if the next block is a REMARK_BLOCK. This function does not
110
+ // / move the cursor.
111
+ Expected<bool > isRemarkBlock ();
112
+ // / Return true if the parser reached the end of the stream.
113
+ bool atEndOfStream () { return Stream.AtEndOfStream (); }
114
+ // / Jump to the end of the stream, skipping everything.
115
+ void skipToEnd () { return Stream.skipToEnd (); }
116
+ };
117
+
29
118
// / Parses and holds the state of the latest parsed remark.
30
119
struct BitstreamRemarkParser : public RemarkParser {
31
120
// / The buffer to parse.
0 commit comments