Path: blob/main/contrib/llvm-project/llvm/lib/Remarks/YAMLRemarkParser.h
35262 views
//===-- YAMLRemarkParser.h - Parser for YAML remarks ------------*- C++/-*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file provides the impementation of the YAML remark parser.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_REMARKS_YAML_REMARK_PARSER_H13#define LLVM_REMARKS_YAML_REMARK_PARSER_H1415#include "llvm/Remarks/Remark.h"16#include "llvm/Remarks/RemarkParser.h"17#include "llvm/Support/Error.h"18#include "llvm/Support/MemoryBuffer.h"19#include "llvm/Support/SourceMgr.h"20#include "llvm/Support/YAMLParser.h"21#include "llvm/Support/raw_ostream.h"22#include <optional>23#include <string>2425namespace llvm {26namespace remarks {2728class YAMLParseError : public ErrorInfo<YAMLParseError> {29public:30static char ID;3132YAMLParseError(StringRef Message, SourceMgr &SM, yaml::Stream &Stream,33yaml::Node &Node);3435YAMLParseError(StringRef Message) : Message(std::string(Message)) {}3637void log(raw_ostream &OS) const override { OS << Message; }38std::error_code convertToErrorCode() const override {39return inconvertibleErrorCode();40}4142private:43std::string Message;44};4546/// Regular YAML to Remark parser.47struct YAMLRemarkParser : public RemarkParser {48/// The string table used for parsing strings.49std::optional<ParsedStringTable> StrTab;50/// Last error message that can come from the YAML parser diagnostics.51/// We need this for catching errors in the constructor.52std::string LastErrorMessage;53/// Source manager for better error messages.54SourceMgr SM;55/// Stream for yaml parsing.56yaml::Stream Stream;57/// Iterator in the YAML stream.58yaml::document_iterator YAMLIt;59/// If we parse remark metadata in separate mode, we need to open a new file60/// and parse that.61std::unique_ptr<MemoryBuffer> SeparateBuf;6263YAMLRemarkParser(StringRef Buf);6465Expected<std::unique_ptr<Remark>> next() override;6667static bool classof(const RemarkParser *P) {68return P->ParserFormat == Format::YAML;69}7071protected:72YAMLRemarkParser(StringRef Buf, std::optional<ParsedStringTable> StrTab);73/// Create a YAMLParseError error from an existing error generated by the YAML74/// parser.75/// If there is no error, this returns Success.76Error error();77/// Create a YAMLParseError error referencing a specific node.78Error error(StringRef Message, yaml::Node &Node);79/// Parse a YAML remark to a remarks::Remark object.80Expected<std::unique_ptr<Remark>> parseRemark(yaml::Document &Remark);81/// Parse the type of a remark to an enum type.82Expected<Type> parseType(yaml::MappingNode &Node);83/// Parse one key to a string.84Expected<StringRef> parseKey(yaml::KeyValueNode &Node);85/// Parse one value to a string.86virtual Expected<StringRef> parseStr(yaml::KeyValueNode &Node);87/// Parse one value to an unsigned.88Expected<unsigned> parseUnsigned(yaml::KeyValueNode &Node);89/// Parse a debug location.90Expected<RemarkLocation> parseDebugLoc(yaml::KeyValueNode &Node);91/// Parse an argument.92Expected<Argument> parseArg(yaml::Node &Node);93};9495/// YAML with a string table to Remark parser.96struct YAMLStrTabRemarkParser : public YAMLRemarkParser {97YAMLStrTabRemarkParser(StringRef Buf, ParsedStringTable StrTab)98: YAMLRemarkParser(Buf, std::move(StrTab)) {}99100static bool classof(const RemarkParser *P) {101return P->ParserFormat == Format::YAMLStrTab;102}103104protected:105/// Parse one value to a string.106Expected<StringRef> parseStr(yaml::KeyValueNode &Node) override;107};108109Expected<std::unique_ptr<YAMLRemarkParser>> createYAMLParserFromMeta(110StringRef Buf, std::optional<ParsedStringTable> StrTab = std::nullopt,111std::optional<StringRef> ExternalFilePrependPath = std::nullopt);112113} // end namespace remarks114} // end namespace llvm115116#endif /* LLVM_REMARKS_YAML_REMARK_PARSER_H */117118119