xref: /freebsd/contrib/llvm-project/llvm/lib/Remarks/BitstreamRemarkParser.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 
24 namespace llvm {
25 namespace remarks {
26 
27 struct Remark;
28 
29 /// Parses and holds the state of the latest parsed remark.
30 struct 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   /// Create a parser that uses a pre-parsed string table.
52   BitstreamRemarkParser(StringRef Buf, ParsedStringTable StrTab)
53       : RemarkParser(Format::Bitstream), ParserHelper(Buf),
54         StrTab(std::move(StrTab)) {}
55 
56   Expected<std::unique_ptr<Remark>> next() override;
57 
58   static bool classof(const RemarkParser *P) {
59     return P->ParserFormat == Format::Bitstream;
60   }
61 
62   /// Parse and process the metadata of the buffer.
63   Error parseMeta();
64 
65   /// Parse a Bitstream remark.
66   Expected<std::unique_ptr<Remark>> parseRemark();
67 
68 private:
69   /// Helper functions.
70   Error processCommonMeta(BitstreamMetaParserHelper &Helper);
71   Error processStandaloneMeta(BitstreamMetaParserHelper &Helper);
72   Error processSeparateRemarksFileMeta(BitstreamMetaParserHelper &Helper);
73   Error processSeparateRemarksMetaMeta(BitstreamMetaParserHelper &Helper);
74   Expected<std::unique_ptr<Remark>>
75   processRemark(BitstreamRemarkParserHelper &Helper);
76   Error processExternalFilePath(std::optional<StringRef> ExternalFilePath);
77 };
78 
79 Expected<std::unique_ptr<BitstreamRemarkParser>> createBitstreamParserFromMeta(
80     StringRef Buf, std::optional<ParsedStringTable> StrTab = std::nullopt,
81     std::optional<StringRef> ExternalFilePrependPath = std::nullopt);
82 
83 } // end namespace remarks
84 } // end namespace llvm
85 
86 #endif /* LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H */
87