blob: 3cbb4932e0ab89aba8f77f1127facb21383ce798 [file] [log] [blame]
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +00001//===- YAMLRemarkParser.cpp -----------------------------------------------===//
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 utility methods used by clients that want to use the
10// parser for remark diagnostics in LLVM.
11//
12//===----------------------------------------------------------------------===//
13
14#include "YAMLRemarkParser.h"
15#include "llvm/ADT/StringSwitch.h"
16#include "llvm/Remarks/RemarkParser.h"
17
18using namespace llvm;
19using namespace llvm::remarks;
20
21char YAMLParseError::ID = 0;
22
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +000023static void handleDiagnostic(const SMDiagnostic &Diag, void *Ctx) {
24 assert(Ctx && "Expected non-null Ctx in diagnostic handler.");
25 std::string &Message = *static_cast<std::string *>(Ctx);
26 assert(Message.empty() && "Expected an empty string.");
27 raw_string_ostream OS(Message);
28 Diag.print(/*ProgName=*/nullptr, OS, /*ShowColors*/ false,
29 /*ShowKindLabels*/ true);
30 OS << '\n';
31 OS.flush();
32}
33
34YAMLParseError::YAMLParseError(StringRef Msg, SourceMgr &SM,
35 yaml::Stream &Stream, yaml::Node &Node) {
36 // 1) Set up a diagnostic handler to avoid errors being printed out to
37 // stderr.
38 // 2) Use the stream to print the error with the associated node.
39 // 3) The stream will use the source manager to print the error, which will
40 // call the diagnostic handler.
41 // 4) The diagnostic handler will stream the error directly into this object's
42 // Message member, which is used when logging is asked for.
43 auto OldDiagHandler = SM.getDiagHandler();
44 auto OldDiagCtx = SM.getDiagContext();
45 SM.setDiagHandler(handleDiagnostic, &Message);
46 Stream.printError(&Node, Twine(Msg) + Twine('\n'));
47 // Restore the old handlers.
48 SM.setDiagHandler(OldDiagHandler, OldDiagCtx);
49}
50
51static SourceMgr setupSM(std::string &LastErrorMessage) {
52 SourceMgr SM;
53 SM.setDiagHandler(handleDiagnostic, &LastErrorMessage);
54 return SM;
55}
56
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +000057YAMLRemarkParser::YAMLRemarkParser(StringRef Buf)
58 : YAMLRemarkParser(Buf, None) {}
59
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +000060YAMLRemarkParser::YAMLRemarkParser(StringRef Buf,
61 Optional<const ParsedStringTable *> StrTab)
62 : Parser{Format::YAML}, StrTab(StrTab), LastErrorMessage(),
63 SM(setupSM(LastErrorMessage)), Stream(Buf, SM), YAMLIt(Stream.begin()) {}
64
65Error YAMLRemarkParser::error(StringRef Message, yaml::Node &Node) {
66 return make_error<YAMLParseError>(Message, SM, Stream, Node);
67}
68
69Error YAMLRemarkParser::error() {
70 if (LastErrorMessage.empty())
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000071 return Error::success();
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +000072 Error E = make_error<YAMLParseError>(LastErrorMessage);
73 LastErrorMessage.clear();
74 return E;
75}
76
77Expected<std::unique_ptr<Remark>>
78YAMLRemarkParser::parseRemark(yaml::Document &RemarkEntry) {
79 if (Error E = error())
80 return std::move(E);
81
82 yaml::Node *YAMLRoot = RemarkEntry.getRoot();
83 if (!YAMLRoot) {
84 return createStringError(std::make_error_code(std::errc::invalid_argument),
85 "not a valid YAML file.");
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000086 }
87
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +000088 auto *Root = dyn_cast<yaml::MappingNode>(YAMLRoot);
89 if (!Root)
90 return error("document root is not of mapping type.", *YAMLRoot);
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000091
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +000092 std::unique_ptr<Remark> Result = llvm::make_unique<Remark>();
93 Remark &TheRemark = *Result;
94
95 // First, the type. It needs special handling since is not part of the
96 // key-value stream.
97 Expected<Type> T = parseType(*Root);
98 if (!T)
99 return T.takeError();
100 else
101 TheRemark.RemarkType = *T;
102
103 // Then, parse the fields, one by one.
104 for (yaml::KeyValueNode &RemarkField : *Root) {
105 Expected<StringRef> MaybeKey = parseKey(RemarkField);
106 if (!MaybeKey)
107 return MaybeKey.takeError();
108 StringRef KeyName = *MaybeKey;
109
110 if (KeyName == "Pass") {
111 if (Expected<StringRef> MaybeStr = parseStr(RemarkField))
112 TheRemark.PassName = *MaybeStr;
113 else
114 return MaybeStr.takeError();
115 } else if (KeyName == "Name") {
116 if (Expected<StringRef> MaybeStr = parseStr(RemarkField))
117 TheRemark.RemarkName = *MaybeStr;
118 else
119 return MaybeStr.takeError();
120 } else if (KeyName == "Function") {
121 if (Expected<StringRef> MaybeStr = parseStr(RemarkField))
122 TheRemark.FunctionName = *MaybeStr;
123 else
124 return MaybeStr.takeError();
125 } else if (KeyName == "Hotness") {
126 if (Expected<unsigned> MaybeU = parseUnsigned(RemarkField))
127 TheRemark.Hotness = *MaybeU;
128 else
129 return MaybeU.takeError();
130 } else if (KeyName == "DebugLoc") {
131 if (Expected<RemarkLocation> MaybeLoc = parseDebugLoc(RemarkField))
132 TheRemark.Loc = *MaybeLoc;
133 else
134 return MaybeLoc.takeError();
135 } else if (KeyName == "Args") {
136 auto *Args = dyn_cast<yaml::SequenceNode>(RemarkField.getValue());
137 if (!Args)
138 return error("wrong value type for key.", RemarkField);
139
140 for (yaml::Node &Arg : *Args) {
141 if (Expected<Argument> MaybeArg = parseArg(Arg))
142 TheRemark.Args.push_back(*MaybeArg);
143 else
144 return MaybeArg.takeError();
145 }
146 } else {
147 return error("unknown key.", RemarkField);
148 }
Francis Visoiu Mistrih7fee2b82019-04-24 00:06:24 +0000149 }
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000150
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000151 // Check if any of the mandatory fields are missing.
152 if (TheRemark.RemarkType == Type::Unknown || TheRemark.PassName.empty() ||
153 TheRemark.RemarkName.empty() || TheRemark.FunctionName.empty())
154 return error("Type, Pass, Name or Function missing.",
155 *RemarkEntry.getRoot());
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000156
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000157 return std::move(Result);
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000158}
159
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000160Expected<Type> YAMLRemarkParser::parseType(yaml::MappingNode &Node) {
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000161 auto Type = StringSwitch<remarks::Type>(Node.getRawTag())
Francis Visoiu Mistrih56167182019-03-19 21:32:03 +0000162 .Case("!Passed", remarks::Type::Passed)
163 .Case("!Missed", remarks::Type::Missed)
164 .Case("!Analysis", remarks::Type::Analysis)
165 .Case("!AnalysisFPCommute", remarks::Type::AnalysisFPCommute)
166 .Case("!AnalysisAliasing", remarks::Type::AnalysisAliasing)
167 .Case("!Failure", remarks::Type::Failure)
168 .Default(remarks::Type::Unknown);
169 if (Type == remarks::Type::Unknown)
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000170 return error("expected a remark tag.", Node);
171 return Type;
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000172}
173
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000174Expected<StringRef> YAMLRemarkParser::parseKey(yaml::KeyValueNode &Node) {
175 if (auto *Key = dyn_cast<yaml::ScalarNode>(Node.getKey()))
176 return Key->getRawValue();
177
178 return error("key is not a string.", Node);
179}
180
181Expected<StringRef> YAMLRemarkParser::parseStr(yaml::KeyValueNode &Node) {
182 auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
183 if (!Value)
184 return error("expected a value of scalar type.", Node);
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +0000185 StringRef Result = Value->getRawValue();
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000186
187 if (Result.front() == '\'')
188 Result = Result.drop_front();
189
190 if (Result.back() == '\'')
191 Result = Result.drop_back();
192
193 return Result;
194}
195
196Expected<unsigned> YAMLRemarkParser::parseUnsigned(yaml::KeyValueNode &Node) {
197 SmallVector<char, 4> Tmp;
198 auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
199 if (!Value)
200 return error("expected a value of scalar type.", Node);
201 unsigned UnsignedValue = 0;
202 if (Value->getValue(Tmp).getAsInteger(10, UnsignedValue))
203 return error("expected a value of integer type.", *Value);
204 return UnsignedValue;
205}
206
207Expected<RemarkLocation>
208YAMLRemarkParser::parseDebugLoc(yaml::KeyValueNode &Node) {
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000209 auto *DebugLoc = dyn_cast<yaml::MappingNode>(Node.getValue());
210 if (!DebugLoc)
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000211 return error("expected a value of mapping type.", Node);
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000212
213 Optional<StringRef> File;
214 Optional<unsigned> Line;
215 Optional<unsigned> Column;
216
217 for (yaml::KeyValueNode &DLNode : *DebugLoc) {
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000218 Expected<StringRef> MaybeKey = parseKey(DLNode);
219 if (!MaybeKey)
220 return MaybeKey.takeError();
221 StringRef KeyName = *MaybeKey;
222
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000223 if (KeyName == "File") {
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000224 if (Expected<StringRef> MaybeStr = parseStr(DLNode))
225 File = *MaybeStr;
226 else
227 return MaybeStr.takeError();
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000228 } else if (KeyName == "Column") {
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000229 if (Expected<unsigned> MaybeU = parseUnsigned(DLNode))
230 Column = *MaybeU;
231 else
232 return MaybeU.takeError();
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000233 } else if (KeyName == "Line") {
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000234 if (Expected<unsigned> MaybeU = parseUnsigned(DLNode))
235 Line = *MaybeU;
236 else
237 return MaybeU.takeError();
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000238 } else {
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000239 return error("unknown entry in DebugLoc map.", DLNode);
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000240 }
241 }
242
243 // If any of the debug loc fields is missing, return an error.
244 if (!File || !Line || !Column)
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000245 return error("DebugLoc node incomplete.", Node);
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000246
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000247 return RemarkLocation{*File, *Line, *Column};
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000248}
249
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000250Expected<Argument> YAMLRemarkParser::parseArg(yaml::Node &Node) {
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000251 auto *ArgMap = dyn_cast<yaml::MappingNode>(&Node);
252 if (!ArgMap)
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000253 return error("expected a value of mapping type.", Node);
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000254
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000255 Optional<StringRef> KeyStr;
256 Optional<StringRef> ValueStr;
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000257 Optional<RemarkLocation> Loc;
258
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000259 for (yaml::KeyValueNode &ArgEntry : *ArgMap) {
260 Expected<StringRef> MaybeKey = parseKey(ArgEntry);
261 if (!MaybeKey)
262 return MaybeKey.takeError();
263 StringRef KeyName = *MaybeKey;
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000264
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000265 // Try to parse debug locs.
266 if (KeyName == "DebugLoc") {
267 // Can't have multiple DebugLoc entries per argument.
268 if (Loc)
269 return error("only one DebugLoc entry is allowed per argument.",
270 ArgEntry);
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000271
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000272 if (Expected<RemarkLocation> MaybeLoc = parseDebugLoc(ArgEntry)) {
273 Loc = *MaybeLoc;
274 continue;
275 } else
276 return MaybeLoc.takeError();
277 }
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000278
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000279 // If we already have a string, error out.
280 if (ValueStr)
281 return error("only one string entry is allowed per argument.", ArgEntry);
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000282
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000283 // Try to parse the value.
284 if (Expected<StringRef> MaybeStr = parseStr(ArgEntry))
285 ValueStr = *MaybeStr;
286 else
287 return MaybeStr.takeError();
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000288
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000289 // Keep the key from the string.
290 KeyStr = KeyName;
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000291 }
292
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000293 if (!KeyStr)
294 return error("argument key is missing.", *ArgMap);
295 if (!ValueStr)
296 return error("argument value is missing.", *ArgMap);
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000297
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000298 return Argument{*KeyStr, *ValueStr, Loc};
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000299}
300
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000301Expected<std::unique_ptr<Remark>> YAMLRemarkParser::next() {
302 if (YAMLIt == Stream.end())
303 return make_error<EndOfFileError>();
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000304
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000305 Expected<std::unique_ptr<Remark>> MaybeResult = parseRemark(*YAMLIt);
306 if (!MaybeResult) {
307 // Avoid garbage input, set the iterator to the end.
308 YAMLIt = Stream.end();
309 return MaybeResult.takeError();
310 }
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000311
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000312 ++YAMLIt;
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000313
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000314 return std::move(*MaybeResult);
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000315}
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +0000316
317Expected<StringRef> YAMLStrTabRemarkParser::parseStr(yaml::KeyValueNode &Node) {
318 auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
319 if (!Value)
320 return error("expected a value of scalar type.", Node);
321 StringRef Result;
322 // If we have a string table, parse it as an unsigned.
323 unsigned StrID = 0;
324 if (Expected<unsigned> MaybeStrID = parseUnsigned(Node))
325 StrID = *MaybeStrID;
326 else
327 return MaybeStrID.takeError();
328
329 if (Expected<StringRef> Str = (**StrTab)[StrID])
330 Result = *Str;
331 else
332 return Str.takeError();
333
334 if (Result.front() == '\'')
335 Result = Result.drop_front();
336
337 if (Result.back() == '\'')
338 Result = Result.drop_back();
339
340 return Result;
341}