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