1*0b57cec5SDimitry Andric //===-- YAMLRemarkParser.h - Parser for YAML remarks ------------*- C++/-*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file provides the impementation of the YAML remark parser. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #ifndef LLVM_REMARKS_YAML_REMARK_PARSER_H 14*0b57cec5SDimitry Andric #define LLVM_REMARKS_YAML_REMARK_PARSER_H 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric #include "llvm/ADT/Optional.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 18*0b57cec5SDimitry Andric #include "llvm/Remarks/Remark.h" 19*0b57cec5SDimitry Andric #include "llvm/Remarks/RemarkParser.h" 20*0b57cec5SDimitry Andric #include "llvm/Support/Error.h" 21*0b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h" 22*0b57cec5SDimitry Andric #include "llvm/Support/YAMLParser.h" 23*0b57cec5SDimitry Andric #include "llvm/Support/YAMLTraits.h" 24*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 25*0b57cec5SDimitry Andric #include <string> 26*0b57cec5SDimitry Andric 27*0b57cec5SDimitry Andric namespace llvm { 28*0b57cec5SDimitry Andric namespace remarks { 29*0b57cec5SDimitry Andric 30*0b57cec5SDimitry Andric class YAMLParseError : public ErrorInfo<YAMLParseError> { 31*0b57cec5SDimitry Andric public: 32*0b57cec5SDimitry Andric static char ID; 33*0b57cec5SDimitry Andric 34*0b57cec5SDimitry Andric YAMLParseError(StringRef Message, SourceMgr &SM, yaml::Stream &Stream, 35*0b57cec5SDimitry Andric yaml::Node &Node); 36*0b57cec5SDimitry Andric 37*0b57cec5SDimitry Andric YAMLParseError(StringRef Message) : Message(Message) {} 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric void log(raw_ostream &OS) const override { OS << Message; } 40*0b57cec5SDimitry Andric std::error_code convertToErrorCode() const override { 41*0b57cec5SDimitry Andric return inconvertibleErrorCode(); 42*0b57cec5SDimitry Andric } 43*0b57cec5SDimitry Andric 44*0b57cec5SDimitry Andric private: 45*0b57cec5SDimitry Andric std::string Message; 46*0b57cec5SDimitry Andric }; 47*0b57cec5SDimitry Andric 48*0b57cec5SDimitry Andric /// Regular YAML to Remark parser. 49*0b57cec5SDimitry Andric struct YAMLRemarkParser : public Parser { 50*0b57cec5SDimitry Andric /// The string table used for parsing strings. 51*0b57cec5SDimitry Andric Optional<const ParsedStringTable *> StrTab; 52*0b57cec5SDimitry Andric /// Last error message that can come from the YAML parser diagnostics. 53*0b57cec5SDimitry Andric /// We need this for catching errors in the constructor. 54*0b57cec5SDimitry Andric std::string LastErrorMessage; 55*0b57cec5SDimitry Andric /// Source manager for better error messages. 56*0b57cec5SDimitry Andric SourceMgr SM; 57*0b57cec5SDimitry Andric /// Stream for yaml parsing. 58*0b57cec5SDimitry Andric yaml::Stream Stream; 59*0b57cec5SDimitry Andric /// Iterator in the YAML stream. 60*0b57cec5SDimitry Andric yaml::document_iterator YAMLIt; 61*0b57cec5SDimitry Andric 62*0b57cec5SDimitry Andric YAMLRemarkParser(StringRef Buf, 63*0b57cec5SDimitry Andric Optional<const ParsedStringTable *> StrTab = None); 64*0b57cec5SDimitry Andric 65*0b57cec5SDimitry Andric Expected<std::unique_ptr<Remark>> next() override; 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric static bool classof(const Parser *P) { 68*0b57cec5SDimitry Andric return P->ParserFormat == Format::YAML; 69*0b57cec5SDimitry Andric } 70*0b57cec5SDimitry Andric 71*0b57cec5SDimitry Andric private: 72*0b57cec5SDimitry Andric /// Create a YAMLParseError error from an existing error generated by the YAML 73*0b57cec5SDimitry Andric /// parser. 74*0b57cec5SDimitry Andric /// If there is no error, this returns Success. 75*0b57cec5SDimitry Andric Error error(); 76*0b57cec5SDimitry Andric /// Create a YAMLParseError error referencing a specific node. 77*0b57cec5SDimitry Andric Error error(StringRef Message, yaml::Node &Node); 78*0b57cec5SDimitry Andric /// Parse a YAML remark to a remarks::Remark object. 79*0b57cec5SDimitry Andric Expected<std::unique_ptr<Remark>> parseRemark(yaml::Document &Remark); 80*0b57cec5SDimitry Andric /// Parse the type of a remark to an enum type. 81*0b57cec5SDimitry Andric Expected<Type> parseType(yaml::MappingNode &Node); 82*0b57cec5SDimitry Andric /// Parse one key to a string. 83*0b57cec5SDimitry Andric Expected<StringRef> parseKey(yaml::KeyValueNode &Node); 84*0b57cec5SDimitry Andric /// Parse one value to a string. 85*0b57cec5SDimitry Andric Expected<StringRef> parseStr(yaml::KeyValueNode &Node); 86*0b57cec5SDimitry Andric /// Parse one value to an unsigned. 87*0b57cec5SDimitry Andric Expected<unsigned> parseUnsigned(yaml::KeyValueNode &Node); 88*0b57cec5SDimitry Andric /// Parse a debug location. 89*0b57cec5SDimitry Andric Expected<RemarkLocation> parseDebugLoc(yaml::KeyValueNode &Node); 90*0b57cec5SDimitry Andric /// Parse an argument. 91*0b57cec5SDimitry Andric Expected<Argument> parseArg(yaml::Node &Node); 92*0b57cec5SDimitry Andric }; 93*0b57cec5SDimitry Andric } // end namespace remarks 94*0b57cec5SDimitry Andric } // end namespace llvm 95*0b57cec5SDimitry Andric 96*0b57cec5SDimitry Andric #endif /* LLVM_REMARKS_YAML_REMARK_PARSER_H */ 97