2019-03-05 21:45:17 +01:00
|
|
|
//===- RemarkParser.cpp --------------------------------------------------===//
|
2018-10-10 20:43:42 +02:00
|
|
|
//
|
2019-01-19 11:56:40 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-10 20:43:42 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides utility methods used by clients that want to use the
|
2019-03-05 21:45:17 +01:00
|
|
|
// parser for remark diagnostics in LLVM.
|
2018-10-10 20:43:42 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
#include "llvm-c/Remarks.h"
|
2018-10-10 20:43:42 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2019-03-05 21:50:35 +01:00
|
|
|
struct YAMLRemarkParser {
|
2018-10-10 20:43:42 +02:00
|
|
|
/// Source manager for better error messages.
|
|
|
|
SourceMgr SM;
|
|
|
|
/// Stream for yaml parsing.
|
|
|
|
yaml::Stream Stream;
|
|
|
|
/// Storage for the error stream.
|
|
|
|
std::string ErrorString;
|
|
|
|
/// The error stream.
|
|
|
|
raw_string_ostream ErrorStream;
|
|
|
|
/// Iterator in the YAML stream.
|
|
|
|
yaml::document_iterator DI;
|
|
|
|
/// The parsed remark (if any).
|
2019-03-05 21:45:17 +01:00
|
|
|
Optional<LLVMRemarkEntry> LastRemark;
|
2018-10-10 20:43:42 +02:00
|
|
|
/// Temporary parsing buffer for the arguments.
|
2019-03-05 21:45:17 +01:00
|
|
|
SmallVector<LLVMRemarkArg, 8> TmpArgs;
|
2018-10-10 20:43:42 +02:00
|
|
|
/// The state used by the parser to parse a remark entry. Invalidated with
|
|
|
|
/// every call to `parseYAMLElement`.
|
|
|
|
struct ParseState {
|
|
|
|
/// Temporary parsing buffer for the arguments.
|
2019-03-05 21:45:17 +01:00
|
|
|
SmallVectorImpl<LLVMRemarkArg> *Args;
|
2018-10-10 20:43:42 +02:00
|
|
|
StringRef Type;
|
|
|
|
StringRef Pass;
|
|
|
|
StringRef Name;
|
|
|
|
StringRef Function;
|
|
|
|
/// Optional.
|
|
|
|
Optional<StringRef> File;
|
|
|
|
Optional<unsigned> Line;
|
|
|
|
Optional<unsigned> Column;
|
|
|
|
Optional<unsigned> Hotness;
|
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
ParseState(SmallVectorImpl<LLVMRemarkArg> &Args) : Args(&Args) {}
|
2018-10-10 20:43:42 +02:00
|
|
|
/// Use Args only as a **temporary** buffer.
|
|
|
|
~ParseState() { Args->clear(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
ParseState State;
|
|
|
|
|
|
|
|
/// Set to `true` if we had any errors during parsing.
|
|
|
|
bool HadAnyErrors = false;
|
|
|
|
|
2019-03-05 21:50:35 +01:00
|
|
|
YAMLRemarkParser(StringRef Buf)
|
2018-10-10 20:43:42 +02:00
|
|
|
: SM(), Stream(Buf, SM), ErrorString(), ErrorStream(ErrorString),
|
|
|
|
DI(Stream.begin()), LastRemark(), TmpArgs(), State(TmpArgs) {
|
2019-03-05 21:50:35 +01:00
|
|
|
SM.setDiagHandler(YAMLRemarkParser::HandleDiagnostic, this);
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a YAML element.
|
|
|
|
Error parseYAMLElement(yaml::Document &Remark);
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Parse one key to a string.
|
|
|
|
/// otherwise.
|
|
|
|
Error parseKey(StringRef &Result, yaml::KeyValueNode &Node);
|
|
|
|
/// Parse one value to a string.
|
|
|
|
Error parseValue(StringRef &Result, yaml::KeyValueNode &Node);
|
|
|
|
/// Parse one value to an unsigned.
|
|
|
|
Error parseValue(Optional<unsigned> &Result, yaml::KeyValueNode &Node);
|
|
|
|
/// Parse a debug location.
|
|
|
|
Error parseDebugLoc(Optional<StringRef> &File, Optional<unsigned> &Line,
|
|
|
|
Optional<unsigned> &Column, yaml::KeyValueNode &Node);
|
|
|
|
/// Parse an argument.
|
2019-03-05 21:45:17 +01:00
|
|
|
Error parseArg(SmallVectorImpl<LLVMRemarkArg> &TmpArgs, yaml::Node &Node);
|
2018-10-10 20:43:42 +02:00
|
|
|
|
|
|
|
/// Handle a diagnostic from the YAML stream. Records the error in the
|
2019-03-05 21:50:35 +01:00
|
|
|
/// YAMLRemarkParser class.
|
2018-10-10 20:43:42 +02:00
|
|
|
static void HandleDiagnostic(const SMDiagnostic &Diag, void *Ctx) {
|
|
|
|
assert(Ctx && "Expected non-null Ctx in diagnostic handler.");
|
2019-03-05 21:50:35 +01:00
|
|
|
auto *Parser = static_cast<YAMLRemarkParser *>(Ctx);
|
2018-10-10 20:43:42 +02:00
|
|
|
Diag.print(/*ProgName=*/nullptr, Parser->ErrorStream, /*ShowColors*/ false,
|
|
|
|
/*ShowKindLabels*/ true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ParseError : public ErrorInfo<ParseError> {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
ParseError(StringRef Message, yaml::Node &Node)
|
|
|
|
: Message(Message), Node(Node) {}
|
|
|
|
|
|
|
|
void log(raw_ostream &OS) const override { OS << Message; }
|
|
|
|
std::error_code convertToErrorCode() const override {
|
|
|
|
return inconvertibleErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef getMessage() const { return Message; }
|
|
|
|
yaml::Node &getNode() const { return Node; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
StringRef Message; // No need to hold a full copy of the buffer.
|
|
|
|
yaml::Node &Node;
|
|
|
|
};
|
|
|
|
|
|
|
|
char ParseError::ID = 0;
|
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
static LLVMRemarkStringRef toRemarkStr(StringRef Str) {
|
2018-10-10 20:43:42 +02:00
|
|
|
return {Str.data(), static_cast<uint32_t>(Str.size())};
|
|
|
|
}
|
|
|
|
|
2019-03-05 21:50:35 +01:00
|
|
|
Error YAMLRemarkParser::parseKey(StringRef &Result, yaml::KeyValueNode &Node) {
|
2018-10-10 20:43:42 +02:00
|
|
|
auto *Key = dyn_cast<yaml::ScalarNode>(Node.getKey());
|
|
|
|
if (!Key)
|
|
|
|
return make_error<ParseError>("key is not a string.", Node);
|
|
|
|
|
|
|
|
Result = Key->getRawValue();
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2019-03-05 21:50:35 +01:00
|
|
|
Error YAMLRemarkParser::parseValue(StringRef &Result,
|
|
|
|
yaml::KeyValueNode &Node) {
|
2018-10-10 20:43:42 +02:00
|
|
|
auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return make_error<ParseError>("expected a value of scalar type.", Node);
|
|
|
|
Result = Value->getRawValue();
|
|
|
|
|
|
|
|
if (Result.front() == '\'')
|
|
|
|
Result = Result.drop_front();
|
|
|
|
|
|
|
|
if (Result.back() == '\'')
|
|
|
|
Result = Result.drop_back();
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2019-03-05 21:50:35 +01:00
|
|
|
Error YAMLRemarkParser::parseValue(Optional<unsigned> &Result,
|
|
|
|
yaml::KeyValueNode &Node) {
|
2018-10-10 20:43:42 +02:00
|
|
|
SmallVector<char, 4> Tmp;
|
|
|
|
auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
|
|
|
|
if (!Value)
|
|
|
|
return make_error<ParseError>("expected a value of scalar type.", Node);
|
|
|
|
unsigned UnsignedValue = 0;
|
|
|
|
if (Value->getValue(Tmp).getAsInteger(10, UnsignedValue))
|
|
|
|
return make_error<ParseError>("expected a value of integer type.", *Value);
|
|
|
|
Result = UnsignedValue;
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2019-03-05 21:50:35 +01:00
|
|
|
Error YAMLRemarkParser::parseDebugLoc(Optional<StringRef> &File,
|
|
|
|
Optional<unsigned> &Line,
|
|
|
|
Optional<unsigned> &Column,
|
|
|
|
yaml::KeyValueNode &Node) {
|
2018-10-10 20:43:42 +02:00
|
|
|
auto *DebugLoc = dyn_cast<yaml::MappingNode>(Node.getValue());
|
|
|
|
if (!DebugLoc)
|
|
|
|
return make_error<ParseError>("expected a value of mapping type.", Node);
|
|
|
|
|
|
|
|
for (yaml::KeyValueNode &DLNode : *DebugLoc) {
|
|
|
|
StringRef KeyName;
|
|
|
|
if (Error E = parseKey(KeyName, DLNode))
|
|
|
|
return E;
|
|
|
|
if (KeyName == "File") {
|
|
|
|
File = StringRef(); // Set the optional to contain a default constructed
|
|
|
|
// value, to be passed to the parsing function.
|
|
|
|
if (Error E = parseValue(*File, DLNode))
|
|
|
|
return E;
|
|
|
|
} else if (KeyName == "Column") {
|
|
|
|
if (Error E = parseValue(Column, DLNode))
|
|
|
|
return E;
|
|
|
|
} else if (KeyName == "Line") {
|
|
|
|
if (Error E = parseValue(Line, DLNode))
|
|
|
|
return E;
|
|
|
|
} else {
|
|
|
|
return make_error<ParseError>("unknown entry in DebugLoc map.", DLNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any of the debug loc fields is missing, return an error.
|
|
|
|
if (!File || !Line || !Column)
|
|
|
|
return make_error<ParseError>("DebugLoc node incomplete.", Node);
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2019-03-05 21:50:35 +01:00
|
|
|
Error YAMLRemarkParser::parseArg(SmallVectorImpl<LLVMRemarkArg> &Args,
|
|
|
|
yaml::Node &Node) {
|
2018-10-10 20:43:42 +02:00
|
|
|
auto *ArgMap = dyn_cast<yaml::MappingNode>(&Node);
|
|
|
|
if (!ArgMap)
|
|
|
|
return make_error<ParseError>("expected a value of mapping type.", Node);
|
|
|
|
|
|
|
|
StringRef ValueStr;
|
|
|
|
StringRef KeyStr;
|
|
|
|
Optional<StringRef> File;
|
|
|
|
Optional<unsigned> Line;
|
|
|
|
Optional<unsigned> Column;
|
|
|
|
|
|
|
|
for (yaml::KeyValueNode &ArgEntry : *ArgMap) {
|
|
|
|
StringRef KeyName;
|
|
|
|
if (Error E = parseKey(KeyName, ArgEntry))
|
|
|
|
return E;
|
|
|
|
|
|
|
|
// Try to parse debug locs.
|
|
|
|
if (KeyName == "DebugLoc") {
|
|
|
|
// Can't have multiple DebugLoc entries per argument.
|
|
|
|
if (File || Line || Column)
|
|
|
|
return make_error<ParseError>(
|
|
|
|
"only one DebugLoc entry is allowed per argument.", ArgEntry);
|
|
|
|
|
|
|
|
if (Error E = parseDebugLoc(File, Line, Column, ArgEntry))
|
|
|
|
return E;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we already have a string, error out.
|
|
|
|
if (!ValueStr.empty())
|
|
|
|
return make_error<ParseError>(
|
|
|
|
"only one string entry is allowed per argument.", ArgEntry);
|
|
|
|
|
|
|
|
// Try to parse a string.
|
|
|
|
if (Error E = parseValue(ValueStr, ArgEntry))
|
|
|
|
return E;
|
|
|
|
|
|
|
|
// Keep the key from the string.
|
|
|
|
KeyStr = KeyName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (KeyStr.empty())
|
|
|
|
return make_error<ParseError>("argument key is missing.", *ArgMap);
|
|
|
|
if (ValueStr.empty())
|
|
|
|
return make_error<ParseError>("argument value is missing.", *ArgMap);
|
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
Args.push_back(LLVMRemarkArg{
|
|
|
|
toRemarkStr(KeyStr), toRemarkStr(ValueStr),
|
|
|
|
LLVMRemarkDebugLoc{toRemarkStr(File.getValueOr(StringRef())),
|
|
|
|
Line.getValueOr(0), Column.getValueOr(0)}});
|
2018-10-10 20:43:42 +02:00
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2019-03-05 21:50:35 +01:00
|
|
|
Error YAMLRemarkParser::parseYAMLElement(yaml::Document &Remark) {
|
2018-10-10 20:43:42 +02:00
|
|
|
// Parsing a new remark, clear the previous one.
|
|
|
|
LastRemark = None;
|
|
|
|
State = ParseState(TmpArgs);
|
|
|
|
|
|
|
|
auto *Root = dyn_cast<yaml::MappingNode>(Remark.getRoot());
|
|
|
|
if (!Root)
|
|
|
|
return make_error<ParseError>("document root is not of mapping type.",
|
|
|
|
*Remark.getRoot());
|
|
|
|
|
|
|
|
State.Type = Root->getRawTag();
|
|
|
|
|
|
|
|
for (yaml::KeyValueNode &RemarkField : *Root) {
|
|
|
|
StringRef KeyName;
|
|
|
|
if (Error E = parseKey(KeyName, RemarkField))
|
|
|
|
return E;
|
|
|
|
|
|
|
|
if (KeyName == "Pass") {
|
|
|
|
if (Error E = parseValue(State.Pass, RemarkField))
|
|
|
|
return E;
|
|
|
|
} else if (KeyName == "Name") {
|
|
|
|
if (Error E = parseValue(State.Name, RemarkField))
|
|
|
|
return E;
|
|
|
|
} else if (KeyName == "Function") {
|
|
|
|
if (Error E = parseValue(State.Function, RemarkField))
|
|
|
|
return E;
|
|
|
|
} else if (KeyName == "Hotness") {
|
|
|
|
if (Error E = parseValue(State.Hotness, RemarkField))
|
|
|
|
return E;
|
|
|
|
} else if (KeyName == "DebugLoc") {
|
|
|
|
if (Error E =
|
|
|
|
parseDebugLoc(State.File, State.Line, State.Column, RemarkField))
|
|
|
|
return E;
|
|
|
|
} else if (KeyName == "Args") {
|
|
|
|
auto *Args = dyn_cast<yaml::SequenceNode>(RemarkField.getValue());
|
|
|
|
if (!Args)
|
|
|
|
return make_error<ParseError>("wrong value type for key.", RemarkField);
|
|
|
|
|
|
|
|
for (yaml::Node &Arg : *Args)
|
|
|
|
if (Error E = parseArg(*State.Args, Arg))
|
|
|
|
return E;
|
|
|
|
} else {
|
|
|
|
return make_error<ParseError>("unknown key.", RemarkField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the YAML parsing failed, don't even continue parsing. We might
|
|
|
|
// encounter malformed YAML.
|
|
|
|
if (Stream.failed())
|
|
|
|
return make_error<ParseError>("YAML parsing failed.", *Remark.getRoot());
|
|
|
|
|
|
|
|
// Check if any of the mandatory fields are missing.
|
|
|
|
if (State.Type.empty() || State.Pass.empty() || State.Name.empty() ||
|
|
|
|
State.Function.empty())
|
|
|
|
return make_error<ParseError>("Type, Pass, Name or Function missing.",
|
|
|
|
*Remark.getRoot());
|
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
LastRemark = LLVMRemarkEntry{
|
|
|
|
toRemarkStr(State.Type),
|
|
|
|
toRemarkStr(State.Pass),
|
|
|
|
toRemarkStr(State.Name),
|
|
|
|
toRemarkStr(State.Function),
|
|
|
|
LLVMRemarkDebugLoc{toRemarkStr(State.File.getValueOr(StringRef())),
|
|
|
|
State.Line.getValueOr(0), State.Column.getValueOr(0)},
|
2018-10-10 20:43:42 +02:00
|
|
|
State.Hotness.getValueOr(0),
|
|
|
|
static_cast<uint32_t>(State.Args->size()),
|
|
|
|
State.Args->data()};
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Create wrappers for C Binding types (see CBindingWrapping.h).
|
2019-03-05 21:50:35 +01:00
|
|
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(YAMLRemarkParser, LLVMRemarkParserRef)
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
extern "C" LLVMRemarkParserRef LLVMRemarkParserCreate(const void *Buf,
|
|
|
|
uint64_t Size) {
|
2018-10-10 20:43:42 +02:00
|
|
|
return wrap(
|
2019-03-05 21:50:35 +01:00
|
|
|
new YAMLRemarkParser(StringRef(static_cast<const char *>(Buf), Size)));
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
extern "C" LLVMRemarkEntry *
|
|
|
|
LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser) {
|
2019-03-05 21:50:35 +01:00
|
|
|
YAMLRemarkParser &TheParser = *unwrap(Parser);
|
2018-10-10 20:43:42 +02:00
|
|
|
// Check for EOF.
|
|
|
|
if (TheParser.HadAnyErrors || TheParser.DI == TheParser.Stream.end())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Try to parse an entry.
|
|
|
|
if (Error E = TheParser.parseYAMLElement(*TheParser.DI)) {
|
|
|
|
handleAllErrors(std::move(E), [&](const ParseError &PE) {
|
|
|
|
TheParser.Stream.printError(&PE.getNode(),
|
|
|
|
Twine(PE.getMessage()) + Twine('\n'));
|
|
|
|
TheParser.HadAnyErrors = true;
|
|
|
|
});
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move on.
|
|
|
|
++TheParser.DI;
|
|
|
|
|
|
|
|
// Return the just-parsed remark.
|
2019-03-05 21:45:17 +01:00
|
|
|
if (Optional<LLVMRemarkEntry> &Entry = TheParser.LastRemark)
|
2018-10-10 20:43:42 +02:00
|
|
|
return &*Entry;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) {
|
2018-10-10 20:43:42 +02:00
|
|
|
return unwrap(Parser)->HadAnyErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" const char *
|
2019-03-05 21:45:17 +01:00
|
|
|
LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) {
|
2018-10-10 20:43:42 +02:00
|
|
|
return unwrap(Parser)->ErrorStream.str().c_str();
|
|
|
|
}
|
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser) {
|
2018-10-10 20:43:42 +02:00
|
|
|
delete unwrap(Parser);
|
|
|
|
}
|