1//===-- BitstreamRemarkParser.h - Parser for Bitstream remarks --*- 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 the impementation of the Bitstream remark parser.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H
14#define LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H
15
16#include "llvm/Remarks/BitstreamRemarkContainer.h"
17#include "llvm/Remarks/BitstreamRemarkParser.h"
18#include "llvm/Remarks/RemarkFormat.h"
19#include "llvm/Remarks/RemarkParser.h"
20#include <cstdint>
21#include <memory>
22#include <optional>
23
24namespace llvm {
25namespace remarks {
26
27struct Remark;
28
29/// Parses and holds the state of the latest parsed remark.
30struct BitstreamRemarkParser : public RemarkParser {
31 /// The buffer to parse.
32 BitstreamParserHelper ParserHelper;
33 /// The string table used for parsing strings.
34 std::optional<ParsedStringTable> StrTab;
35 /// Temporary remark buffer used when the remarks are stored separately.
36 std::unique_ptr<MemoryBuffer> TmpRemarkBuffer;
37 /// The common metadata used to decide how to parse the buffer.
38 /// This is filled when parsing the metadata block.
39 uint64_t ContainerVersion = 0;
40 uint64_t RemarkVersion = 0;
41 BitstreamRemarkContainerType ContainerType =
42 BitstreamRemarkContainerType::Standalone;
43 /// Wether the parser is ready to parse remarks.
44 bool ReadyToParseRemarks = false;
45
46 /// Create a parser that expects to find a string table embedded in the
47 /// stream.
48 explicit BitstreamRemarkParser(StringRef Buf)
49 : RemarkParser(Format::Bitstream), ParserHelper(Buf) {}
50
51 Expected<std::unique_ptr<Remark>> next() override;
52
53 static bool classof(const RemarkParser *P) {
54 return P->ParserFormat == Format::Bitstream;
55 }
56
57 /// Parse and process the metadata of the buffer.
58 Error parseMeta();
59
60 /// Parse a Bitstream remark.
61 Expected<std::unique_ptr<Remark>> parseRemark();
62
63private:
64 /// Helper functions.
65 Error processCommonMeta(BitstreamMetaParserHelper &Helper);
66 Error processStandaloneMeta(BitstreamMetaParserHelper &Helper);
67 Error processSeparateRemarksFileMeta(BitstreamMetaParserHelper &Helper);
68 Error processSeparateRemarksMetaMeta(BitstreamMetaParserHelper &Helper);
69 Expected<std::unique_ptr<Remark>>
70 processRemark(BitstreamRemarkParserHelper &Helper);
71 Error processExternalFilePath(std::optional<StringRef> ExternalFilePath);
72};
73
74Expected<std::unique_ptr<BitstreamRemarkParser>> createBitstreamParserFromMeta(
75 StringRef Buf,
76 std::optional<StringRef> ExternalFilePrependPath = std::nullopt);
77
78} // end namespace remarks
79} // end namespace llvm
80
81#endif /* LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H */
82