Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:25

0001 //===-- llvm/Remarks/Remark.h - The remark type -----------------*- C++/-*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file provides an interface for parsing remarks in LLVM.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_REMARKS_REMARKPARSER_H
0014 #define LLVM_REMARKS_REMARKPARSER_H
0015 
0016 #include "llvm/ADT/StringRef.h"
0017 #include "llvm/Remarks/RemarkFormat.h"
0018 #include "llvm/Support/Error.h"
0019 #include <memory>
0020 #include <optional>
0021 
0022 namespace llvm {
0023 namespace remarks {
0024 
0025 struct Remark;
0026 
0027 class EndOfFileError : public ErrorInfo<EndOfFileError> {
0028 public:
0029   static char ID;
0030 
0031   EndOfFileError() = default;
0032 
0033   void log(raw_ostream &OS) const override { OS << "End of file reached."; }
0034   std::error_code convertToErrorCode() const override {
0035     return inconvertibleErrorCode();
0036   }
0037 };
0038 
0039 /// Parser used to parse a raw buffer to remarks::Remark objects.
0040 struct RemarkParser {
0041   /// The format of the parser.
0042   Format ParserFormat;
0043   /// Path to prepend when opening an external remark file.
0044   std::string ExternalFilePrependPath;
0045 
0046   RemarkParser(Format ParserFormat) : ParserFormat(ParserFormat) {}
0047 
0048   /// If no error occurs, this returns a valid Remark object.
0049   /// If an error of type EndOfFileError occurs, it is safe to recover from it
0050   /// by stopping the parsing.
0051   /// If any other error occurs, it should be propagated to the user.
0052   /// The pointer should never be null.
0053   virtual Expected<std::unique_ptr<Remark>> next() = 0;
0054 
0055   virtual ~RemarkParser() = default;
0056 };
0057 
0058 /// In-memory representation of the string table parsed from a buffer (e.g. the
0059 /// remarks section).
0060 struct ParsedStringTable {
0061   /// The buffer mapped from the section contents.
0062   StringRef Buffer;
0063   /// This object has high changes to be std::move'd around, so don't use a
0064   /// SmallVector for once.
0065   std::vector<size_t> Offsets;
0066 
0067   ParsedStringTable(StringRef Buffer);
0068   /// Disable copy.
0069   ParsedStringTable(const ParsedStringTable &) = delete;
0070   ParsedStringTable &operator=(const ParsedStringTable &) = delete;
0071   /// Should be movable.
0072   ParsedStringTable(ParsedStringTable &&) = default;
0073   ParsedStringTable &operator=(ParsedStringTable &&) = default;
0074 
0075   size_t size() const { return Offsets.size(); }
0076   Expected<StringRef> operator[](size_t Index) const;
0077 };
0078 
0079 Expected<std::unique_ptr<RemarkParser>> createRemarkParser(Format ParserFormat,
0080                                                            StringRef Buf);
0081 
0082 Expected<std::unique_ptr<RemarkParser>>
0083 createRemarkParser(Format ParserFormat, StringRef Buf,
0084                    ParsedStringTable StrTab);
0085 
0086 Expected<std::unique_ptr<RemarkParser>> createRemarkParserFromMeta(
0087     Format ParserFormat, StringRef Buf,
0088     std::optional<ParsedStringTable> StrTab = std::nullopt,
0089     std::optional<StringRef> ExternalFilePrependPath = std::nullopt);
0090 
0091 } // end namespace remarks
0092 } // end namespace llvm
0093 
0094 #endif // LLVM_REMARKS_REMARKPARSER_H