LLVM  10.0.0svn
YAMLRemarkParser.cpp
Go to the documentation of this file.
1 //===- 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"
17 #include "llvm/Support/Endian.h"
18 #include "llvm/Support/Path.h"
19 
20 using namespace llvm;
21 using namespace llvm::remarks;
22 
23 char YAMLParseError::ID = 0;
24 
25 static void handleDiagnostic(const SMDiagnostic &Diag, void *Ctx) {
26  assert(Ctx && "Expected non-null Ctx in diagnostic handler.");
27  std::string &Message = *static_cast<std::string *>(Ctx);
28  assert(Message.empty() && "Expected an empty string.");
29  raw_string_ostream OS(Message);
30  Diag.print(/*ProgName=*/nullptr, OS, /*ShowColors*/ false,
31  /*ShowKindLabels*/ true);
32  OS << '\n';
33  OS.flush();
34 }
35 
37  yaml::Stream &Stream, yaml::Node &Node) {
38  // 1) Set up a diagnostic handler to avoid errors being printed out to
39  // stderr.
40  // 2) Use the stream to print the error with the associated node.
41  // 3) The stream will use the source manager to print the error, which will
42  // call the diagnostic handler.
43  // 4) The diagnostic handler will stream the error directly into this object's
44  // Message member, which is used when logging is asked for.
45  auto OldDiagHandler = SM.getDiagHandler();
46  auto OldDiagCtx = SM.getDiagContext();
47  SM.setDiagHandler(handleDiagnostic, &Message);
48  Stream.printError(&Node, Twine(Msg) + Twine('\n'));
49  // Restore the old handlers.
50  SM.setDiagHandler(OldDiagHandler, OldDiagCtx);
51 }
52 
53 static SourceMgr setupSM(std::string &LastErrorMessage) {
54  SourceMgr SM;
55  SM.setDiagHandler(handleDiagnostic, &LastErrorMessage);
56  return SM;
57 }
58 
59 // Parse the magic number. This function returns true if this represents remark
60 // metadata, false otherwise.
62  if (!Buf.consume_front(remarks::Magic))
63  return false;
64 
65  if (Buf.size() < 1 || !Buf.consume_front(StringRef("\0", 1)))
66  return createStringError(std::errc::illegal_byte_sequence,
67  "Expecting \\0 after magic number.");
68  return true;
69 }
70 
72  if (Buf.size() < sizeof(uint64_t))
73  return createStringError(std::errc::illegal_byte_sequence,
74  "Expecting version number.");
75 
76  uint64_t Version =
77  support::endian::read<uint64_t, support::little, support::unaligned>(
78  Buf.data());
79  if (Version != remarks::CurrentRemarkVersion)
80  return createStringError(std::errc::illegal_byte_sequence,
81  "Mismatching remark version. Got %" PRId64
82  ", expected %" PRId64 ".",
84  Buf = Buf.drop_front(sizeof(uint64_t));
85  return Version;
86 }
87 
89  if (Buf.size() < sizeof(uint64_t))
90  return createStringError(std::errc::illegal_byte_sequence,
91  "Expecting string table size.");
92  uint64_t StrTabSize =
93  support::endian::read<uint64_t, support::little, support::unaligned>(
94  Buf.data());
95  Buf = Buf.drop_front(sizeof(uint64_t));
96  return StrTabSize;
97 }
98 
100  uint64_t StrTabSize) {
101  if (Buf.size() < StrTabSize)
102  return createStringError(std::errc::illegal_byte_sequence,
103  "Expecting string table.");
104 
105  // Attach the string table to the parser.
106  ParsedStringTable Result(StringRef(Buf.data(), StrTabSize));
107  Buf = Buf.drop_front(StrTabSize);
108  return Expected<ParsedStringTable>(std::move(Result));
109 }
110 
114  Optional<StringRef> ExternalFilePrependPath) {
115  // We now have a magic number. The metadata has to be correct.
116  Expected<bool> isMeta = parseMagic(Buf);
117  if (!isMeta)
118  return isMeta.takeError();
119  // If it's not recognized as metadata, roll back.
120  std::unique_ptr<MemoryBuffer> SeparateBuf;
121  if (*isMeta) {
123  if (!Version)
124  return Version.takeError();
125 
126  Expected<uint64_t> StrTabSize = parseStrTabSize(Buf);
127  if (!StrTabSize)
128  return StrTabSize.takeError();
129 
130  // If the size of string table is not 0, try to build one.
131  if (*StrTabSize != 0) {
132  if (StrTab)
133  return createStringError(std::errc::illegal_byte_sequence,
134  "String table already provided.");
135  Expected<ParsedStringTable> MaybeStrTab = parseStrTab(Buf, *StrTabSize);
136  if (!MaybeStrTab)
137  return MaybeStrTab.takeError();
138  StrTab = std::move(*MaybeStrTab);
139  }
140  // If it starts with "---", there is no external file.
141  if (!Buf.startswith("---")) {
142  // At this point, we expect Buf to contain the external file path.
143  StringRef ExternalFilePath = Buf;
144  SmallString<80> FullPath;
145  if (ExternalFilePrependPath)
146  FullPath = *ExternalFilePrependPath;
147  sys::path::append(FullPath, ExternalFilePath);
148 
149  // Try to open the file and start parsing from there.
151  MemoryBuffer::getFile(FullPath);
152  if (std::error_code EC = BufferOrErr.getError())
153  return createFileError(FullPath, EC);
154 
155  // Keep the buffer alive.
156  SeparateBuf = std::move(*BufferOrErr);
157  Buf = SeparateBuf->getBuffer();
158  }
159  }
160 
161  std::unique_ptr<YAMLRemarkParser> Result =
162  StrTab
163  ? std::make_unique<YAMLStrTabRemarkParser>(Buf, std::move(*StrTab))
164  : std::make_unique<YAMLRemarkParser>(Buf);
165  if (SeparateBuf)
166  Result->SeparateBuf = std::move(SeparateBuf);
167  return std::move(Result);
168 }
169 
171  : YAMLRemarkParser(Buf, None) {}
172 
177 
179  return make_error<YAMLParseError>(Message, SM, Stream, Node);
180 }
181 
183  if (LastErrorMessage.empty())
184  return Error::success();
185  Error E = make_error<YAMLParseError>(LastErrorMessage);
186  LastErrorMessage.clear();
187  return E;
188 }
189 
192  if (Error E = error())
193  return std::move(E);
194 
195  yaml::Node *YAMLRoot = RemarkEntry.getRoot();
196  if (!YAMLRoot) {
197  return createStringError(std::make_error_code(std::errc::invalid_argument),
198  "not a valid YAML file.");
199  }
200 
201  auto *Root = dyn_cast<yaml::MappingNode>(YAMLRoot);
202  if (!Root)
203  return error("document root is not of mapping type.", *YAMLRoot);
204 
205  std::unique_ptr<Remark> Result = std::make_unique<Remark>();
206  Remark &TheRemark = *Result;
207 
208  // First, the type. It needs special handling since is not part of the
209  // key-value stream.
210  Expected<Type> T = parseType(*Root);
211  if (!T)
212  return T.takeError();
213  else
214  TheRemark.RemarkType = *T;
215 
216  // Then, parse the fields, one by one.
217  for (yaml::KeyValueNode &RemarkField : *Root) {
218  Expected<StringRef> MaybeKey = parseKey(RemarkField);
219  if (!MaybeKey)
220  return MaybeKey.takeError();
221  StringRef KeyName = *MaybeKey;
222 
223  if (KeyName == "Pass") {
224  if (Expected<StringRef> MaybeStr = parseStr(RemarkField))
225  TheRemark.PassName = *MaybeStr;
226  else
227  return MaybeStr.takeError();
228  } else if (KeyName == "Name") {
229  if (Expected<StringRef> MaybeStr = parseStr(RemarkField))
230  TheRemark.RemarkName = *MaybeStr;
231  else
232  return MaybeStr.takeError();
233  } else if (KeyName == "Function") {
234  if (Expected<StringRef> MaybeStr = parseStr(RemarkField))
235  TheRemark.FunctionName = *MaybeStr;
236  else
237  return MaybeStr.takeError();
238  } else if (KeyName == "Hotness") {
239  if (Expected<unsigned> MaybeU = parseUnsigned(RemarkField))
240  TheRemark.Hotness = *MaybeU;
241  else
242  return MaybeU.takeError();
243  } else if (KeyName == "DebugLoc") {
244  if (Expected<RemarkLocation> MaybeLoc = parseDebugLoc(RemarkField))
245  TheRemark.Loc = *MaybeLoc;
246  else
247  return MaybeLoc.takeError();
248  } else if (KeyName == "Args") {
249  auto *Args = dyn_cast<yaml::SequenceNode>(RemarkField.getValue());
250  if (!Args)
251  return error("wrong value type for key.", RemarkField);
252 
253  for (yaml::Node &Arg : *Args) {
254  if (Expected<Argument> MaybeArg = parseArg(Arg))
255  TheRemark.Args.push_back(*MaybeArg);
256  else
257  return MaybeArg.takeError();
258  }
259  } else {
260  return error("unknown key.", RemarkField);
261  }
262  }
263 
264  // Check if any of the mandatory fields are missing.
265  if (TheRemark.RemarkType == Type::Unknown || TheRemark.PassName.empty() ||
266  TheRemark.RemarkName.empty() || TheRemark.FunctionName.empty())
267  return error("Type, Pass, Name or Function missing.",
268  *RemarkEntry.getRoot());
269 
270  return std::move(Result);
271 }
272 
275  .Case("!Passed", remarks::Type::Passed)
276  .Case("!Missed", remarks::Type::Missed)
277  .Case("!Analysis", remarks::Type::Analysis)
278  .Case("!AnalysisFPCommute", remarks::Type::AnalysisFPCommute)
279  .Case("!AnalysisAliasing", remarks::Type::AnalysisAliasing)
280  .Case("!Failure", remarks::Type::Failure)
283  return error("expected a remark tag.", Node);
284  return Type;
285 }
286 
288  if (auto *Key = dyn_cast<yaml::ScalarNode>(Node.getKey()))
289  return Key->getRawValue();
290 
291  return error("key is not a string.", Node);
292 }
293 
295  auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
296  if (!Value)
297  return error("expected a value of scalar type.", Node);
298  StringRef Result = Value->getRawValue();
299 
300  if (Result.front() == '\'')
301  Result = Result.drop_front();
302 
303  if (Result.back() == '\'')
304  Result = Result.drop_back();
305 
306  return Result;
307 }
308 
311  auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
312  if (!Value)
313  return error("expected a value of scalar type.", Node);
314  unsigned UnsignedValue = 0;
315  if (Value->getValue(Tmp).getAsInteger(10, UnsignedValue))
316  return error("expected a value of integer type.", *Value);
317  return UnsignedValue;
318 }
319 
322  auto *DebugLoc = dyn_cast<yaml::MappingNode>(Node.getValue());
323  if (!DebugLoc)
324  return error("expected a value of mapping type.", Node);
325 
327  Optional<unsigned> Line;
328  Optional<unsigned> Column;
329 
330  for (yaml::KeyValueNode &DLNode : *DebugLoc) {
331  Expected<StringRef> MaybeKey = parseKey(DLNode);
332  if (!MaybeKey)
333  return MaybeKey.takeError();
334  StringRef KeyName = *MaybeKey;
335 
336  if (KeyName == "File") {
337  if (Expected<StringRef> MaybeStr = parseStr(DLNode))
338  File = *MaybeStr;
339  else
340  return MaybeStr.takeError();
341  } else if (KeyName == "Column") {
342  if (Expected<unsigned> MaybeU = parseUnsigned(DLNode))
343  Column = *MaybeU;
344  else
345  return MaybeU.takeError();
346  } else if (KeyName == "Line") {
347  if (Expected<unsigned> MaybeU = parseUnsigned(DLNode))
348  Line = *MaybeU;
349  else
350  return MaybeU.takeError();
351  } else {
352  return error("unknown entry in DebugLoc map.", DLNode);
353  }
354  }
355 
356  // If any of the debug loc fields is missing, return an error.
357  if (!File || !Line || !Column)
358  return error("DebugLoc node incomplete.", Node);
359 
360  return RemarkLocation{*File, *Line, *Column};
361 }
362 
364  auto *ArgMap = dyn_cast<yaml::MappingNode>(&Node);
365  if (!ArgMap)
366  return error("expected a value of mapping type.", Node);
367 
368  Optional<StringRef> KeyStr;
369  Optional<StringRef> ValueStr;
371 
372  for (yaml::KeyValueNode &ArgEntry : *ArgMap) {
373  Expected<StringRef> MaybeKey = parseKey(ArgEntry);
374  if (!MaybeKey)
375  return MaybeKey.takeError();
376  StringRef KeyName = *MaybeKey;
377 
378  // Try to parse debug locs.
379  if (KeyName == "DebugLoc") {
380  // Can't have multiple DebugLoc entries per argument.
381  if (Loc)
382  return error("only one DebugLoc entry is allowed per argument.",
383  ArgEntry);
384 
385  if (Expected<RemarkLocation> MaybeLoc = parseDebugLoc(ArgEntry)) {
386  Loc = *MaybeLoc;
387  continue;
388  } else
389  return MaybeLoc.takeError();
390  }
391 
392  // If we already have a string, error out.
393  if (ValueStr)
394  return error("only one string entry is allowed per argument.", ArgEntry);
395 
396  // Try to parse the value.
397  if (Expected<StringRef> MaybeStr = parseStr(ArgEntry))
398  ValueStr = *MaybeStr;
399  else
400  return MaybeStr.takeError();
401 
402  // Keep the key from the string.
403  KeyStr = KeyName;
404  }
405 
406  if (!KeyStr)
407  return error("argument key is missing.", *ArgMap);
408  if (!ValueStr)
409  return error("argument value is missing.", *ArgMap);
410 
411  return Argument{*KeyStr, *ValueStr, Loc};
412 }
413 
415  if (YAMLIt == Stream.end())
416  return make_error<EndOfFileError>();
417 
419  if (!MaybeResult) {
420  // Avoid garbage input, set the iterator to the end.
421  YAMLIt = Stream.end();
422  return MaybeResult.takeError();
423  }
424 
425  ++YAMLIt;
426 
427  return std::move(*MaybeResult);
428 }
429 
431  auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
432  if (!Value)
433  return error("expected a value of scalar type.", Node);
434  StringRef Result;
435  // If we have a string table, parse it as an unsigned.
436  unsigned StrID = 0;
437  if (Expected<unsigned> MaybeStrID = parseUnsigned(Node))
438  StrID = *MaybeStrID;
439  else
440  return MaybeStrID.takeError();
441 
442  if (Expected<StringRef> Str = (*StrTab)[StrID])
443  Result = *Str;
444  else
445  return Str.takeError();
446 
447  if (Result.front() == '\'')
448  Result = Result.drop_front();
449 
450  if (Result.back() == '\'')
451  Result = Result.drop_back();
452 
453  return Result;
454 }
const NoneType None
Definition: None.h:23
Expected< unsigned > parseUnsigned(yaml::KeyValueNode &Node)
Parse one value to an unsigned.
Regular YAML to Remark parser.
Represents either an error or a value T.
Definition: ErrorOr.h:56
SourceMgr SM
Source manager for better error messages.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
StringRef getRawTag() const
Get the tag as it was written in the document.
Definition: YAMLParser.h:153
void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true) const
Definition: SourceMgr.cpp:368
DiagHandlerTy getDiagHandler() const
Definition: SourceMgr.h:124
static Expected< bool > parseMagic(StringRef &Buf)
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:270
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:663
yaml::document_iterator YAMLIt
Iterator in the YAML stream.
Represents a YAML sequence created from either a block sequence for a flow sequence.
Definition: YAMLParser.h:452
Node * getKey()
Parse and return the key.
A debug info location.
Definition: DebugLoc.h:33
document_iterator begin()
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
YAMLParseError(StringRef Message, SourceMgr &SM, yaml::Stream &Stream, yaml::Node &Node)
document_iterator end()
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:455
Expected< Argument > parseArg(yaml::Node &Node)
Parse an argument.
Expected< RemarkLocation > parseDebugLoc(yaml::KeyValueNode &Node)
Parse a debug location.
A remark type used for both emission and parsing.
Definition: Remark.h:67
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
Expected< StringRef > parseKey(yaml::KeyValueNode &Node)
Parse one key to a string.
LLVM_NODISCARD StringRef drop_front(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with the first N elements dropped.
Definition: StringRef.h:634
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
std::error_code make_error_code(BitcodeError E)
Expected< Type > parseType(yaml::MappingNode &Node)
Parse the type of a remark to an enum type.
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
Optional< ParsedStringTable > StrTab
The string table used for parsing strings.
Key
PAL metadata keys.
A key and value pair.
Definition: YAMLParser.h:279
Node * getRoot()
Parse and return the root level node.
Definition: YAMLParser.h:534
Expected< std::unique_ptr< Remark > > parseRemark(yaml::Document &Remark)
Parse a YAML remark to a remarks::Remark object.
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:25
constexpr StringLiteral Magic("REMARKS")
In-memory representation of the string table parsed from a buffer (e.g.
Definition: RemarkParser.h:59
static Expected< uint64_t > parseVersion(StringRef &Buf)
Expected< StringRef > parseStr(yaml::KeyValueNode &Node) override
Parse one value to a string.
Type
The type of the remark.
Definition: Remark.h:54
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)
Specify a diagnostic handler to be invoked every time PrintMessage is called.
Definition: SourceMgr.h:119
virtual Expected< StringRef > parseStr(yaml::KeyValueNode &Node)
Parse one value to a string.
void printError(Node *N, const Twine &Msg)
Instrumentation for Order File
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
The debug location used to track a remark back to the source file.
Definition: Remark.h:30
std::error_code getError() const
Definition: ErrorOr.h:159
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:41
LLVM_NODISCARD char back() const
back - Get the last character in the string.
Definition: StringRef.h:155
A scalar node is an opaque datum that can be presented as a series of zero or more Unicode scalar val...
Definition: YAMLParser.h:206
Expected< std::unique_ptr< Remark > > next() override
If no error occurs, this returns a valid Remark object.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
Node * getValue()
Parse and return the value.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
This class represents a YAML stream potentially containing multiple documents.
Definition: YAMLParser.h:83
static void handleDiagnostic(const SMDiagnostic &Diag, void *Ctx)
Error error()
Create a YAMLParseError error from an existing error generated by the YAML parser.
A key-value pair with a debug location that is used to display the remarks at the right place in the ...
Definition: Remark.h:42
static Expected< uint64_t > parseStrTabSize(StringRef &Buf)
Parser used to parse a raw buffer to remarks::Remark objects.
Definition: RemarkParser.h:39
std::string LastErrorMessage
Last error message that can come from the YAML parser diagnostics.
void * getDiagContext() const
Definition: SourceMgr.h:125
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
Represents a YAML map created from either a block map for a flow map.
Definition: YAMLParser.h:404
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:136
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM_NODISCARD char front() const
front - Get the first character in the string.
Definition: StringRef.h:148
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:503
LLVM Value Representation.
Definition: Value.h:74
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition: Error.h:1265
yaml::Stream Stream
Stream for yaml parsing.
LLVM_NODISCARD StringRef drop_back(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with the last N elements dropped.
Definition: StringRef.h:642
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
static SourceMgr setupSM(std::string &LastErrorMessage)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
constexpr uint64_t CurrentRemarkVersion
The current version of the remark entry.
Definition: Remark.h:27
const uint64_t Version
Definition: InstrProf.h:980
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
A YAML Stream is a sequence of Documents.
Definition: YAMLParser.h:522
Expected< std::unique_ptr< YAMLRemarkParser > > createYAMLParserFromMeta(StringRef Buf, Optional< ParsedStringTable > StrTab=None, Optional< StringRef > ExternalFilePrependPath=None)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1197
static Expected< ParsedStringTable > parseStrTab(StringRef &Buf, uint64_t StrTabSize)
Abstract base class for all Nodes.
Definition: YAMLParser.h:113
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:261