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 168bcb0991SDimitry Andric #include "llvm/ADT/Optional.h" 17e8d8bef9SDimitry Andric #include "llvm/Remarks/BitstreamRemarkContainer.h" 188bcb0991SDimitry Andric #include "llvm/Remarks/BitstreamRemarkParser.h" 198bcb0991SDimitry Andric #include "llvm/Remarks/RemarkFormat.h" 208bcb0991SDimitry Andric #include "llvm/Remarks/RemarkParser.h" 21e8d8bef9SDimitry Andric #include <cstdint> 228bcb0991SDimitry Andric #include <memory> 238bcb0991SDimitry Andric 248bcb0991SDimitry Andric namespace llvm { 258bcb0991SDimitry Andric namespace remarks { 26*1fd87a68SDimitry Andric 27*1fd87a68SDimitry Andric struct Remark; 28*1fd87a68SDimitry 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. 348bcb0991SDimitry Andric 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. 48480093f4SDimitry 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. 528bcb0991SDimitry 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 588bcb0991SDimitry 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); 768bcb0991SDimitry Andric Error processExternalFilePath(Optional<StringRef> ExternalFilePath); 778bcb0991SDimitry Andric }; 788bcb0991SDimitry Andric 798bcb0991SDimitry Andric Expected<std::unique_ptr<BitstreamRemarkParser>> createBitstreamParserFromMeta( 808bcb0991SDimitry Andric StringRef Buf, Optional<ParsedStringTable> StrTab = None, 818bcb0991SDimitry Andric Optional<StringRef> ExternalFilePrependPath = None); 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