1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 04:02:41 +01:00
llvm-mirror/lib/Remarks/RemarkParser.cpp
Francis Visoiu Mistrih aad252295b Reland "[Remarks] Add a new Remark / RemarkParser abstraction"
This adds a Remark class that allows us to share code when working with
remarks.

The C API has been updated to reflect this. Instead of the parser
generating C structs, it's now using a C++ object that is used through
opaque pointers in C. This gives us much more flexibility on what
changes we can make to the internal state of the object and interacts
much better with scenarios where the library is used through dlopen.

* C API updates:
  * move from C structs to opaque pointers and functions
  * the remark type is now an enum instead of a string
* unit tests updates:
  * use mostly the C++ API
  * keep one test for the C API
  * rename to YAMLRemarksParsingTest
* a typo was fixed: AnalysisFPCompute -> AnalysisFPCommute.
* a new error message was added: "expected a remark tag."
* llvm-opt-report has been updated to use the C++ parser instead of the
C API

Differential Revision: https://reviews.llvm.org/D59049

Original llvm-svn: 356491

llvm-svn: 356519
2019-03-19 21:11:07 +00:00

117 lines
3.9 KiB
C++

//===- RemarkParser.cpp --------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file provides utility methods used by clients that want to use the
// parser for remark diagnostics in LLVM.
//
//===----------------------------------------------------------------------===//
#include "llvm/Remarks/RemarkParser.h"
#include "YAMLRemarkParser.h"
#include "llvm-c/Remarks.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/CBindingWrapping.h"
using namespace llvm;
using namespace llvm::remarks;
Parser::Parser(StringRef Buf) : Impl(llvm::make_unique<YAMLParserImpl>(Buf)) {}
Parser::~Parser() = default;
static Expected<const Remark *> getNextYAML(YAMLParserImpl &Impl) {
YAMLRemarkParser &YAMLParser = Impl.YAMLParser;
// Check for EOF.
if (Impl.YAMLIt == Impl.YAMLParser.Stream.end())
return nullptr;
auto CurrentIt = Impl.YAMLIt;
// 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);
}
// Move on.
++Impl.YAMLIt;
// Return the just-parsed remark.
if (const Optional<YAMLRemarkParser::ParseState> &State = YAMLParser.State)
return &State->TheRemark;
else
return createStringError(std::make_error_code(std::errc::invalid_argument),
"unexpected error while parsing.");
}
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.");
}
// Create wrappers for C Binding types (see CBindingWrapping.h).
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(remarks::Parser, LLVMRemarkParserRef)
extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
uint64_t Size) {
return wrap(
new remarks::Parser(StringRef(static_cast<const char *>(Buf), Size)));
}
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'));
},
[&](const ErrorInfoBase &EIB) { EIB.log(Impl.YAMLParser.ErrorStream); });
Impl.HasErrors = true;
}
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.");
return nullptr;
}
if (*RemarkOrErr == nullptr)
return nullptr;
// Valid remark.
return wrap(*RemarkOrErr);
}
extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) {
if (auto *Impl =
dyn_cast<remarks::YAMLParserImpl>(unwrap(Parser)->Impl.get()))
return Impl->HasErrors;
llvm_unreachable("unkown parser implementation.");
}
extern "C" const char *
LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) {
if (auto *Impl =
dyn_cast<remarks::YAMLParserImpl>(unwrap(Parser)->Impl.get()))
return Impl->YAMLParser.ErrorStream.str().c_str();
llvm_unreachable("unkown parser implementation.");
}
extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser) {
delete unwrap(Parser);
}