xref: /freebsd/contrib/llvm-project/llvm/lib/Remarks/BitstreamRemarkParser.h (revision 8bcb0991864975618c09697b1aca10683346d9f0)
1*8bcb0991SDimitry Andric //===-- BitstreamRemarkParser.h - Parser for Bitstream remarks --*- C++/-*-===//
2*8bcb0991SDimitry Andric //
3*8bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*8bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*8bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*8bcb0991SDimitry Andric //
7*8bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
8*8bcb0991SDimitry Andric //
9*8bcb0991SDimitry Andric // This file provides the impementation of the Bitstream remark parser.
10*8bcb0991SDimitry Andric //
11*8bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
12*8bcb0991SDimitry Andric 
13*8bcb0991SDimitry Andric #ifndef LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H
14*8bcb0991SDimitry Andric #define LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H
15*8bcb0991SDimitry Andric 
16*8bcb0991SDimitry Andric #include "llvm/ADT/Optional.h"
17*8bcb0991SDimitry Andric #include "llvm/ADT/SmallVector.h"
18*8bcb0991SDimitry Andric #include "llvm/Remarks/BitstreamRemarkParser.h"
19*8bcb0991SDimitry Andric #include "llvm/Remarks/RemarkFormat.h"
20*8bcb0991SDimitry Andric #include "llvm/Remarks/RemarkParser.h"
21*8bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h"
22*8bcb0991SDimitry Andric #include <memory>
23*8bcb0991SDimitry Andric #include <string>
24*8bcb0991SDimitry Andric 
25*8bcb0991SDimitry Andric namespace llvm {
26*8bcb0991SDimitry Andric namespace remarks {
27*8bcb0991SDimitry Andric /// Parses and holds the state of the latest parsed remark.
28*8bcb0991SDimitry Andric struct BitstreamRemarkParser : public RemarkParser {
29*8bcb0991SDimitry Andric   /// The buffer to parse.
30*8bcb0991SDimitry Andric   BitstreamParserHelper ParserHelper;
31*8bcb0991SDimitry Andric   /// The string table used for parsing strings.
32*8bcb0991SDimitry Andric   Optional<ParsedStringTable> StrTab;
33*8bcb0991SDimitry Andric   /// Temporary remark buffer used when the remarks are stored separately.
34*8bcb0991SDimitry Andric   std::unique_ptr<MemoryBuffer> TmpRemarkBuffer;
35*8bcb0991SDimitry Andric   /// The common metadata used to decide how to parse the buffer.
36*8bcb0991SDimitry Andric   /// This is filled when parsing the metadata block.
37*8bcb0991SDimitry Andric   uint64_t ContainerVersion;
38*8bcb0991SDimitry Andric   uint64_t RemarkVersion;
39*8bcb0991SDimitry Andric   BitstreamRemarkContainerType ContainerType;
40*8bcb0991SDimitry Andric   /// Wether the parser is ready to parse remarks.
41*8bcb0991SDimitry Andric   bool ReadyToParseRemarks = false;
42*8bcb0991SDimitry Andric 
43*8bcb0991SDimitry Andric   /// Create a parser that expects to find a string table embedded in the
44*8bcb0991SDimitry Andric   /// stream.
45*8bcb0991SDimitry Andric   BitstreamRemarkParser(StringRef Buf)
46*8bcb0991SDimitry Andric       : RemarkParser(Format::Bitstream), ParserHelper(Buf) {}
47*8bcb0991SDimitry Andric 
48*8bcb0991SDimitry Andric   /// Create a parser that uses a pre-parsed string table.
49*8bcb0991SDimitry Andric   BitstreamRemarkParser(StringRef Buf, ParsedStringTable StrTab)
50*8bcb0991SDimitry Andric       : RemarkParser(Format::Bitstream), ParserHelper(Buf),
51*8bcb0991SDimitry Andric         StrTab(std::move(StrTab)) {}
52*8bcb0991SDimitry Andric 
53*8bcb0991SDimitry Andric   Expected<std::unique_ptr<Remark>> next() override;
54*8bcb0991SDimitry Andric 
55*8bcb0991SDimitry Andric   static bool classof(const RemarkParser *P) {
56*8bcb0991SDimitry Andric     return P->ParserFormat == Format::Bitstream;
57*8bcb0991SDimitry Andric   }
58*8bcb0991SDimitry Andric 
59*8bcb0991SDimitry Andric   /// Parse and process the metadata of the buffer.
60*8bcb0991SDimitry Andric   Error parseMeta();
61*8bcb0991SDimitry Andric 
62*8bcb0991SDimitry Andric   /// Parse a Bitstream remark.
63*8bcb0991SDimitry Andric   Expected<std::unique_ptr<Remark>> parseRemark();
64*8bcb0991SDimitry Andric 
65*8bcb0991SDimitry Andric private:
66*8bcb0991SDimitry Andric   /// Helper functions.
67*8bcb0991SDimitry Andric   Error processCommonMeta(BitstreamMetaParserHelper &Helper);
68*8bcb0991SDimitry Andric   Error processStandaloneMeta(BitstreamMetaParserHelper &Helper);
69*8bcb0991SDimitry Andric   Error processSeparateRemarksFileMeta(BitstreamMetaParserHelper &Helper);
70*8bcb0991SDimitry Andric   Error processSeparateRemarksMetaMeta(BitstreamMetaParserHelper &Helper);
71*8bcb0991SDimitry Andric   Expected<std::unique_ptr<Remark>>
72*8bcb0991SDimitry Andric   processRemark(BitstreamRemarkParserHelper &Helper);
73*8bcb0991SDimitry Andric   Error processExternalFilePath(Optional<StringRef> ExternalFilePath);
74*8bcb0991SDimitry Andric };
75*8bcb0991SDimitry Andric 
76*8bcb0991SDimitry Andric Expected<std::unique_ptr<BitstreamRemarkParser>> createBitstreamParserFromMeta(
77*8bcb0991SDimitry Andric     StringRef Buf, Optional<ParsedStringTable> StrTab = None,
78*8bcb0991SDimitry Andric     Optional<StringRef> ExternalFilePrependPath = None);
79*8bcb0991SDimitry Andric 
80*8bcb0991SDimitry Andric } // end namespace remarks
81*8bcb0991SDimitry Andric } // end namespace llvm
82*8bcb0991SDimitry Andric 
83*8bcb0991SDimitry Andric #endif /* LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H */
84