blob: 144f08f6feb97befc2c9de09fea1650b49af2713 [file] [log] [blame]
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +00001//===- RemarkParser.cpp --------------------------------------------------===//
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +00002//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file provides utility methods used by clients that want to use the
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000010// parser for remark diagnostics in LLVM.
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +000011//
12//===----------------------------------------------------------------------===//
13
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000014#include "llvm/Remarks/RemarkParser.h"
15#include "YAMLRemarkParser.h"
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +000016#include "llvm-c/Remarks.h"
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +000017#include "llvm/ADT/STLExtras.h"
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000018#include "llvm/Support/CBindingWrapping.h"
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +000019
20using namespace llvm;
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000021using namespace llvm::remarks;
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +000022
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000023Parser::Parser(StringRef Buf) : Impl(llvm::make_unique<YAMLParserImpl>(Buf)) {}
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +000024
Francis Visoiu Mistrih7fee2b82019-04-24 00:06:24 +000025Parser::Parser(StringRef Buf, StringRef StrTabBuf)
26 : Impl(llvm::make_unique<YAMLParserImpl>(Buf, StrTabBuf)) {}
27
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000028Parser::~Parser() = default;
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +000029
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000030static Expected<const Remark *> getNextYAML(YAMLParserImpl &Impl) {
31 YAMLRemarkParser &YAMLParser = Impl.YAMLParser;
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +000032 // Check for EOF.
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000033 if (Impl.YAMLIt == Impl.YAMLParser.Stream.end())
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +000034 return nullptr;
35
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000036 auto CurrentIt = Impl.YAMLIt;
37
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +000038 // Try to parse an entry.
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000039 if (Error E = YAMLParser.parseYAMLElement(*CurrentIt)) {
40 // Set the iterator to the end, in case the user calls getNext again.
41 Impl.YAMLIt = Impl.YAMLParser.Stream.end();
42 return std::move(E);
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +000043 }
44
45 // Move on.
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000046 ++Impl.YAMLIt;
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +000047
48 // Return the just-parsed remark.
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000049 if (const Optional<YAMLRemarkParser::ParseState> &State = YAMLParser.State)
50 return &State->TheRemark;
51 else
52 return createStringError(std::make_error_code(std::errc::invalid_argument),
53 "unexpected error while parsing.");
54}
55
56Expected<const Remark *> Parser::getNext() const {
57 if (auto *Impl = dyn_cast<YAMLParserImpl>(this->Impl.get()))
58 return getNextYAML(*Impl);
59 llvm_unreachable("Get next called with an unknown parsing implementation.");
60}
61
Francis Visoiu Mistrih7fee2b82019-04-24 00:06:24 +000062ParsedStringTable::ParsedStringTable(StringRef InBuffer) : Buffer(InBuffer) {
63 while (!InBuffer.empty()) {
64 // Strings are separated by '\0' bytes.
65 std::pair<StringRef, StringRef> Split = InBuffer.split('\0');
66 // We only store the offset from the beginning of the buffer.
67 Offsets.push_back(Split.first.data() - Buffer.data());
68 InBuffer = Split.second;
69 }
70}
71
72Expected<StringRef> ParsedStringTable::operator[](size_t Index) {
73 if (Index >= Offsets.size())
74 return createStringError(
75 std::make_error_code(std::errc::invalid_argument),
76 "String with index %u is out of bounds (size = %u).", Index,
77 Offsets.size());
78
79 size_t Offset = Offsets[Index];
80 // If it's the last offset, we can't use the next offset to know the size of
81 // the string.
82 size_t NextOffset =
83 (Index == Offsets.size() - 1) ? Buffer.size() : Offsets[Index + 1];
84 return StringRef(Buffer.data() + Offset, NextOffset - Offset - 1);
85}
86
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000087// Create wrappers for C Binding types (see CBindingWrapping.h).
88DEFINE_SIMPLE_CONVERSION_FUNCTIONS(remarks::Parser, LLVMRemarkParserRef)
89
90extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
91 uint64_t Size) {
92 return wrap(
93 new remarks::Parser(StringRef(static_cast<const char *>(Buf), Size)));
94}
95
96static void handleYAMLError(remarks::YAMLParserImpl &Impl, Error E) {
97 handleAllErrors(
98 std::move(E),
99 [&](const YAMLParseError &PE) {
100 Impl.YAMLParser.Stream.printError(&PE.getNode(),
101 Twine(PE.getMessage()) + Twine('\n'));
102 },
103 [&](const ErrorInfoBase &EIB) { EIB.log(Impl.YAMLParser.ErrorStream); });
104 Impl.HasErrors = true;
105}
106
107extern "C" LLVMRemarkEntryRef
108LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser) {
109 remarks::Parser &TheParser = *unwrap(Parser);
110
111 Expected<const remarks::Remark *> RemarkOrErr = TheParser.getNext();
112 if (!RemarkOrErr) {
113 // Error during parsing.
114 if (auto *Impl = dyn_cast<remarks::YAMLParserImpl>(TheParser.Impl.get()))
115 handleYAMLError(*Impl, RemarkOrErr.takeError());
116 else
117 llvm_unreachable("unkown parser implementation.");
118 return nullptr;
119 }
120
121 if (*RemarkOrErr == nullptr)
122 return nullptr;
123 // Valid remark.
124 return wrap(*RemarkOrErr);
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +0000125}
126
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000127extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) {
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000128 if (auto *Impl =
129 dyn_cast<remarks::YAMLParserImpl>(unwrap(Parser)->Impl.get()))
130 return Impl->HasErrors;
131 llvm_unreachable("unkown parser implementation.");
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +0000132}
133
134extern "C" const char *
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000135LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) {
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000136 if (auto *Impl =
137 dyn_cast<remarks::YAMLParserImpl>(unwrap(Parser)->Impl.get()))
138 return Impl->YAMLParser.ErrorStream.str().c_str();
139 llvm_unreachable("unkown parser implementation.");
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +0000140}
141
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000142extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser) {
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +0000143 delete unwrap(Parser);
144}