18bcb0991SDimitry Andric //===-- BitstreamRemarkParser.h - Parser for Bitstream remarks --*- C++/-*-===// 28bcb0991SDimitry Andric // 38bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 48bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 58bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 68bcb0991SDimitry Andric // 78bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 88bcb0991SDimitry Andric // 98bcb0991SDimitry Andric // This file provides the impementation of the Bitstream remark parser. 108bcb0991SDimitry Andric // 118bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 128bcb0991SDimitry Andric 138bcb0991SDimitry Andric #ifndef LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H 148bcb0991SDimitry Andric #define LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H 158bcb0991SDimitry Andric 16e8d8bef9SDimitry Andric #include "llvm/Remarks/BitstreamRemarkContainer.h" 178bcb0991SDimitry Andric #include "llvm/Remarks/BitstreamRemarkParser.h" 188bcb0991SDimitry Andric #include "llvm/Remarks/RemarkFormat.h" 198bcb0991SDimitry Andric #include "llvm/Remarks/RemarkParser.h" 20e8d8bef9SDimitry Andric #include <cstdint> 218bcb0991SDimitry Andric #include <memory> 22*bdd1243dSDimitry Andric #include <optional> 238bcb0991SDimitry Andric 248bcb0991SDimitry Andric namespace llvm { 258bcb0991SDimitry Andric namespace remarks { 261fd87a68SDimitry Andric 271fd87a68SDimitry Andric struct Remark; 281fd87a68SDimitry Andric 298bcb0991SDimitry Andric /// Parses and holds the state of the latest parsed remark. 308bcb0991SDimitry Andric struct BitstreamRemarkParser : public RemarkParser { 318bcb0991SDimitry Andric /// The buffer to parse. 328bcb0991SDimitry Andric BitstreamParserHelper ParserHelper; 338bcb0991SDimitry Andric /// The string table used for parsing strings. 34*bdd1243dSDimitry Andric std::optional<ParsedStringTable> StrTab; 358bcb0991SDimitry Andric /// Temporary remark buffer used when the remarks are stored separately. 368bcb0991SDimitry Andric std::unique_ptr<MemoryBuffer> TmpRemarkBuffer; 378bcb0991SDimitry Andric /// The common metadata used to decide how to parse the buffer. 388bcb0991SDimitry Andric /// This is filled when parsing the metadata block. 39480093f4SDimitry Andric uint64_t ContainerVersion = 0; 40480093f4SDimitry Andric uint64_t RemarkVersion = 0; 41480093f4SDimitry Andric BitstreamRemarkContainerType ContainerType = 42480093f4SDimitry Andric BitstreamRemarkContainerType::Standalone; 438bcb0991SDimitry Andric /// Wether the parser is ready to parse remarks. 448bcb0991SDimitry Andric bool ReadyToParseRemarks = false; 458bcb0991SDimitry Andric 468bcb0991SDimitry Andric /// Create a parser that expects to find a string table embedded in the 478bcb0991SDimitry Andric /// stream. BitstreamRemarkParserBitstreamRemarkParser48480093f4SDimitry Andric explicit BitstreamRemarkParser(StringRef Buf) 498bcb0991SDimitry Andric : RemarkParser(Format::Bitstream), ParserHelper(Buf) {} 508bcb0991SDimitry Andric 518bcb0991SDimitry Andric /// Create a parser that uses a pre-parsed string table. BitstreamRemarkParserBitstreamRemarkParser528bcb0991SDimitry Andric BitstreamRemarkParser(StringRef Buf, ParsedStringTable StrTab) 538bcb0991SDimitry Andric : RemarkParser(Format::Bitstream), ParserHelper(Buf), 548bcb0991SDimitry Andric StrTab(std::move(StrTab)) {} 558bcb0991SDimitry Andric 568bcb0991SDimitry Andric Expected<std::unique_ptr<Remark>> next() override; 578bcb0991SDimitry Andric classofBitstreamRemarkParser588bcb0991SDimitry Andric static bool classof(const RemarkParser *P) { 598bcb0991SDimitry Andric return P->ParserFormat == Format::Bitstream; 608bcb0991SDimitry Andric } 618bcb0991SDimitry Andric 628bcb0991SDimitry Andric /// Parse and process the metadata of the buffer. 638bcb0991SDimitry Andric Error parseMeta(); 648bcb0991SDimitry Andric 658bcb0991SDimitry Andric /// Parse a Bitstream remark. 668bcb0991SDimitry Andric Expected<std::unique_ptr<Remark>> parseRemark(); 678bcb0991SDimitry Andric 688bcb0991SDimitry Andric private: 698bcb0991SDimitry Andric /// Helper functions. 708bcb0991SDimitry Andric Error processCommonMeta(BitstreamMetaParserHelper &Helper); 718bcb0991SDimitry Andric Error processStandaloneMeta(BitstreamMetaParserHelper &Helper); 728bcb0991SDimitry Andric Error processSeparateRemarksFileMeta(BitstreamMetaParserHelper &Helper); 738bcb0991SDimitry Andric Error processSeparateRemarksMetaMeta(BitstreamMetaParserHelper &Helper); 748bcb0991SDimitry Andric Expected<std::unique_ptr<Remark>> 758bcb0991SDimitry Andric processRemark(BitstreamRemarkParserHelper &Helper); 76*bdd1243dSDimitry Andric Error processExternalFilePath(std::optional<StringRef> ExternalFilePath); 778bcb0991SDimitry Andric }; 788bcb0991SDimitry Andric 798bcb0991SDimitry Andric Expected<std::unique_ptr<BitstreamRemarkParser>> createBitstreamParserFromMeta( 80*bdd1243dSDimitry Andric StringRef Buf, std::optional<ParsedStringTable> StrTab = std::nullopt, 81*bdd1243dSDimitry Andric std::optional<StringRef> ExternalFilePrependPath = std::nullopt); 828bcb0991SDimitry Andric 838bcb0991SDimitry Andric } // end namespace remarks 848bcb0991SDimitry Andric } // end namespace llvm 858bcb0991SDimitry Andric 868bcb0991SDimitry Andric #endif /* LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H */ 87