[clang] Add test for CWG190 "Layout-compatible POD-struct types" (#121668)
[llvm-project.git] / llvm / lib / Remarks / YAMLRemarkParser.h
blob8ef72e16be74e3599cbfe1ae9affa427249cd024
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_REMARKS_YAML_REMARK_PARSER_H
14 #define LLVM_REMARKS_YAML_REMARK_PARSER_H
16 #include "llvm/Remarks/Remark.h"
17 #include "llvm/Remarks/RemarkParser.h"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/Support/YAMLParser.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <optional>
24 #include <string>
26 namespace llvm {
27 namespace remarks {
29 class YAMLParseError : public ErrorInfo<YAMLParseError> {
30 public:
31 static char ID;
33 YAMLParseError(StringRef Message, SourceMgr &SM, yaml::Stream &Stream,
34 yaml::Node &Node);
36 YAMLParseError(StringRef Message) : Message(std::string(Message)) {}
38 void log(raw_ostream &OS) const override { OS << Message; }
39 std::error_code convertToErrorCode() const override {
40 return inconvertibleErrorCode();
43 private:
44 std::string Message;
47 /// Regular YAML to Remark parser.
48 struct YAMLRemarkParser : public RemarkParser {
49 /// The string table used for parsing strings.
50 std::optional<ParsedStringTable> StrTab;
51 /// Last error message that can come from the YAML parser diagnostics.
52 /// We need this for catching errors in the constructor.
53 std::string LastErrorMessage;
54 /// Source manager for better error messages.
55 SourceMgr SM;
56 /// Stream for yaml parsing.
57 yaml::Stream Stream;
58 /// Iterator in the YAML stream.
59 yaml::document_iterator YAMLIt;
60 /// If we parse remark metadata in separate mode, we need to open a new file
61 /// and parse that.
62 std::unique_ptr<MemoryBuffer> SeparateBuf;
64 YAMLRemarkParser(StringRef Buf);
66 Expected<std::unique_ptr<Remark>> next() override;
68 static bool classof(const RemarkParser *P) {
69 return P->ParserFormat == Format::YAML;
72 protected:
73 YAMLRemarkParser(StringRef Buf, std::optional<ParsedStringTable> StrTab);
74 /// Create a YAMLParseError error from an existing error generated by the YAML
75 /// parser.
76 /// If there is no error, this returns Success.
77 Error error();
78 /// Create a YAMLParseError error referencing a specific node.
79 Error error(StringRef Message, yaml::Node &Node);
80 /// Parse a YAML remark to a remarks::Remark object.
81 Expected<std::unique_ptr<Remark>> parseRemark(yaml::Document &Remark);
82 /// Parse the type of a remark to an enum type.
83 Expected<Type> parseType(yaml::MappingNode &Node);
84 /// Parse one key to a string.
85 Expected<StringRef> parseKey(yaml::KeyValueNode &Node);
86 /// Parse one value to a string.
87 virtual Expected<StringRef> parseStr(yaml::KeyValueNode &Node);
88 /// Parse one value to an unsigned.
89 Expected<unsigned> parseUnsigned(yaml::KeyValueNode &Node);
90 /// Parse a debug location.
91 Expected<RemarkLocation> parseDebugLoc(yaml::KeyValueNode &Node);
92 /// Parse an argument.
93 Expected<Argument> parseArg(yaml::Node &Node);
96 /// YAML with a string table to Remark parser.
97 struct YAMLStrTabRemarkParser : public YAMLRemarkParser {
98 YAMLStrTabRemarkParser(StringRef Buf, ParsedStringTable StrTab)
99 : YAMLRemarkParser(Buf, std::move(StrTab)) {}
101 static bool classof(const RemarkParser *P) {
102 return P->ParserFormat == Format::YAMLStrTab;
105 protected:
106 /// Parse one value to a string.
107 Expected<StringRef> parseStr(yaml::KeyValueNode &Node) override;
110 Expected<std::unique_ptr<YAMLRemarkParser>> createYAMLParserFromMeta(
111 StringRef Buf, std::optional<ParsedStringTable> StrTab = std::nullopt,
112 std::optional<StringRef> ExternalFilePrependPath = std::nullopt);
114 } // end namespace remarks
115 } // end namespace llvm
117 #endif /* LLVM_REMARKS_YAML_REMARK_PARSER_H */