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-19 19:09:51 +01:00
|
|
|
#include "llvm/Remarks/RemarkParser.h"
|
|
|
|
#include "YAMLRemarkParser.h"
|
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"
|
2019-03-19 19:09:51 +01:00
|
|
|
#include "llvm/Support/CBindingWrapping.h"
|
2018-10-10 20:43:42 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
2019-03-19 19:09:51 +01:00
|
|
|
using namespace llvm::remarks;
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
Parser::Parser(StringRef Buf) : Impl(llvm::make_unique<YAMLParserImpl>(Buf)) {}
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
Parser::~Parser() = default;
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
static Expected<const Remark *> getNextYAML(YAMLParserImpl &Impl) {
|
|
|
|
YAMLRemarkParser &YAMLParser = Impl.YAMLParser;
|
|
|
|
// Check for EOF.
|
|
|
|
if (Impl.YAMLIt == Impl.YAMLParser.Stream.end())
|
|
|
|
return nullptr;
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
auto CurrentIt = Impl.YAMLIt;
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
// Try to parse an entry.
|
|
|
|
if (Error E = YAMLParser.parseYAMLElement(*CurrentIt)) {
|
|
|
|
// Set the iterator to the end, in case the user calls getNext again.
|
|
|
|
Impl.YAMLIt = Impl.YAMLParser.Stream.end();
|
|
|
|
return std::move(E);
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
// Move on.
|
|
|
|
++Impl.YAMLIt;
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
// Return the just-parsed remark.
|
|
|
|
if (const Optional<YAMLRemarkParser::ParseState> &State = YAMLParser.State)
|
|
|
|
return &State->Remark;
|
|
|
|
else
|
|
|
|
return createStringError(std::make_error_code(std::errc::invalid_argument),
|
|
|
|
"unexpected error while parsing.");
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
Expected<const Remark *> Parser::getNext() const {
|
|
|
|
if (auto *Impl = dyn_cast<YAMLParserImpl>(this->Impl.get()))
|
|
|
|
return getNextYAML(*Impl);
|
|
|
|
llvm_unreachable("Get next called with an unknown parsing implementation.");
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create wrappers for C Binding types (see CBindingWrapping.h).
|
2019-03-19 19:09:51 +01:00
|
|
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(remarks::Parser, LLVMRemarkParserRef)
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
|
|
|
|
uint64_t Size) {
|
2018-10-10 20:43:42 +02:00
|
|
|
return wrap(
|
2019-03-19 19:09:51 +01:00
|
|
|
new remarks::Parser(StringRef(static_cast<const char *>(Buf), Size)));
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
static void handleYAMLError(remarks::YAMLParserImpl &Impl, Error E) {
|
|
|
|
handleAllErrors(
|
|
|
|
std::move(E),
|
|
|
|
[&](const YAMLParseError &PE) {
|
|
|
|
Impl.YAMLParser.Stream.printError(&PE.getNode(),
|
|
|
|
Twine(PE.getMessage()) + Twine('\n'));
|
|
|
|
Impl.HasErrors = true;
|
|
|
|
},
|
|
|
|
[&](const StringError &PE) { PE.log(Impl.YAMLParser.ErrorStream); });
|
|
|
|
}
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
extern "C" LLVMRemarkEntryRef
|
|
|
|
LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser) {
|
|
|
|
remarks::Parser &TheParser = *unwrap(Parser);
|
|
|
|
|
|
|
|
Expected<const remarks::Remark *> RemarkOrErr = TheParser.getNext();
|
|
|
|
if (!RemarkOrErr) {
|
|
|
|
// Error during parsing.
|
|
|
|
if (auto *Impl = dyn_cast<remarks::YAMLParserImpl>(TheParser.Impl.get()))
|
|
|
|
handleYAMLError(*Impl, RemarkOrErr.takeError());
|
|
|
|
else
|
|
|
|
llvm_unreachable("unkown parser implementation.");
|
2018-10-10 20:43:42 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-19 19:09:51 +01:00
|
|
|
if (*RemarkOrErr == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
// Valid remark.
|
|
|
|
return wrap(*RemarkOrErr);
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) {
|
2019-03-19 19:09:51 +01:00
|
|
|
if (auto *Impl =
|
|
|
|
dyn_cast<remarks::YAMLParserImpl>(unwrap(Parser)->Impl.get()))
|
|
|
|
return Impl->HasErrors;
|
|
|
|
llvm_unreachable("unkown parser implementation.");
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" const char *
|
2019-03-05 21:45:17 +01:00
|
|
|
LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) {
|
2019-03-19 19:09:51 +01:00
|
|
|
if (auto *Impl =
|
|
|
|
dyn_cast<remarks::YAMLParserImpl>(unwrap(Parser)->Impl.get()))
|
|
|
|
return Impl->YAMLParser.ErrorStream.str().c_str();
|
|
|
|
llvm_unreachable("unkown parser implementation.");
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|