xref: /freebsd-src/contrib/llvm-project/llvm/lib/Remarks/BitstreamRemarkParser.h (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
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/ADT/SmallVector.h"
18 #include "llvm/Remarks/BitstreamRemarkParser.h"
19 #include "llvm/Remarks/RemarkFormat.h"
20 #include "llvm/Remarks/RemarkParser.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <memory>
23 #include <string>
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;
38   uint64_t RemarkVersion;
39   BitstreamRemarkContainerType ContainerType;
40   /// Wether the parser is ready to parse remarks.
41   bool ReadyToParseRemarks = false;
42 
43   /// Create a parser that expects to find a string table embedded in the
44   /// stream.
45   BitstreamRemarkParser(StringRef Buf)
46       : RemarkParser(Format::Bitstream), ParserHelper(Buf) {}
47 
48   /// Create a parser that uses a pre-parsed string table.
49   BitstreamRemarkParser(StringRef Buf, ParsedStringTable StrTab)
50       : RemarkParser(Format::Bitstream), ParserHelper(Buf),
51         StrTab(std::move(StrTab)) {}
52 
53   Expected<std::unique_ptr<Remark>> next() override;
54 
55   static bool classof(const RemarkParser *P) {
56     return P->ParserFormat == Format::Bitstream;
57   }
58 
59   /// Parse and process the metadata of the buffer.
60   Error parseMeta();
61 
62   /// Parse a Bitstream remark.
63   Expected<std::unique_ptr<Remark>> parseRemark();
64 
65 private:
66   /// Helper functions.
67   Error processCommonMeta(BitstreamMetaParserHelper &Helper);
68   Error processStandaloneMeta(BitstreamMetaParserHelper &Helper);
69   Error processSeparateRemarksFileMeta(BitstreamMetaParserHelper &Helper);
70   Error processSeparateRemarksMetaMeta(BitstreamMetaParserHelper &Helper);
71   Expected<std::unique_ptr<Remark>>
72   processRemark(BitstreamRemarkParserHelper &Helper);
73   Error processExternalFilePath(Optional<StringRef> ExternalFilePath);
74 };
75 
76 Expected<std::unique_ptr<BitstreamRemarkParser>> createBitstreamParserFromMeta(
77     StringRef Buf, Optional<ParsedStringTable> StrTab = None,
78     Optional<StringRef> ExternalFilePrependPath = None);
79 
80 } // end namespace remarks
81 } // end namespace llvm
82 
83 #endif /* LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H */
84