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