blob: 98f6534906572ce91cf0e7f98b726b570ed7c570 [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 Mistrih94bad222019-07-16 15:25:05 +000023char EndOfFileError::ID = 0;
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +000024
Francis Visoiu Mistrih7fee2b82019-04-24 00:06:24 +000025ParsedStringTable::ParsedStringTable(StringRef InBuffer) : Buffer(InBuffer) {
26 while (!InBuffer.empty()) {
27 // Strings are separated by '\0' bytes.
28 std::pair<StringRef, StringRef> Split = InBuffer.split('\0');
29 // We only store the offset from the beginning of the buffer.
30 Offsets.push_back(Split.first.data() - Buffer.data());
31 InBuffer = Split.second;
32 }
33}
34
Francis Visoiu Mistrihe6ba3132019-07-04 00:30:58 +000035Expected<StringRef> ParsedStringTable::operator[](size_t Index) const {
Francis Visoiu Mistrih7fee2b82019-04-24 00:06:24 +000036 if (Index >= Offsets.size())
37 return createStringError(
38 std::make_error_code(std::errc::invalid_argument),
39 "String with index %u is out of bounds (size = %u).", Index,
40 Offsets.size());
41
42 size_t Offset = Offsets[Index];
43 // If it's the last offset, we can't use the next offset to know the size of
44 // the string.
45 size_t NextOffset =
46 (Index == Offsets.size() - 1) ? Buffer.size() : Offsets[Index + 1];
47 return StringRef(Buffer.data() + Offset, NextOffset - Offset - 1);
48}
49
Francis Visoiu Mistrihab56cf82019-07-25 00:16:56 +000050Expected<std::unique_ptr<RemarkParser>>
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +000051llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf) {
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +000052 switch (ParserFormat) {
53 case Format::YAML:
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000054 return std::make_unique<YAMLRemarkParser>(Buf);
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +000055 case Format::YAMLStrTab:
56 return createStringError(
57 std::make_error_code(std::errc::invalid_argument),
58 "The YAML with string table format requires a parsed string table.");
Francis Visoiu Mistrih84e80972019-07-31 00:13:51 +000059 case Format::Bitstream:
60 return createStringError(std::make_error_code(std::errc::invalid_argument),
61 "Parsing bitstream remarks is not supported.");
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +000062 case Format::Unknown:
63 return createStringError(std::make_error_code(std::errc::invalid_argument),
64 "Unknown remark parser format.");
65 }
Haojian Wu509ad302019-07-24 07:55:01 +000066 llvm_unreachable("unhandled ParseFormat");
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +000067}
68
Francis Visoiu Mistrihab56cf82019-07-25 00:16:56 +000069Expected<std::unique_ptr<RemarkParser>>
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +000070llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf,
Francis Visoiu Mistrih4287c952019-07-23 22:50:08 +000071 ParsedStringTable StrTab) {
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +000072 switch (ParserFormat) {
73 case Format::YAML:
74 return createStringError(std::make_error_code(std::errc::invalid_argument),
75 "The YAML format can't be used with a string "
76 "table. Use yaml-strtab instead.");
77 case Format::YAMLStrTab:
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000078 return std::make_unique<YAMLStrTabRemarkParser>(Buf, std::move(StrTab));
Francis Visoiu Mistrih84e80972019-07-31 00:13:51 +000079 case Format::Bitstream:
80 return createStringError(std::make_error_code(std::errc::invalid_argument),
81 "Parsing bitstream remarks is not supported.");
Francis Visoiu Mistrihc5b5cc42019-07-23 20:42:46 +000082 case Format::Unknown:
83 return createStringError(std::make_error_code(std::errc::invalid_argument),
84 "Unknown remark parser format.");
85 }
Haojian Wu509ad302019-07-24 07:55:01 +000086 llvm_unreachable("unhandled ParseFormat");
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +000087}
88
Francis Visoiu Mistrih64a5f9e2019-07-26 21:02:02 +000089Expected<std::unique_ptr<RemarkParser>>
90llvm::remarks::createRemarkParserFromMeta(Format ParserFormat, StringRef Buf,
91 Optional<ParsedStringTable> StrTab) {
92 switch (ParserFormat) {
93 // Depending on the metadata, the format can be either yaml or yaml-strtab,
94 // regardless of the input argument.
95 case Format::YAML:
96 case Format::YAMLStrTab:
97 return createYAMLParserFromMeta(Buf, std::move(StrTab));
Francis Visoiu Mistrih84e80972019-07-31 00:13:51 +000098 case Format::Bitstream:
99 return createStringError(std::make_error_code(std::errc::invalid_argument),
100 "Parsing bitstream remarks is not supported.");
Francis Visoiu Mistrih64a5f9e2019-07-26 21:02:02 +0000101 case Format::Unknown:
102 return createStringError(std::make_error_code(std::errc::invalid_argument),
103 "Unknown remark parser format.");
104 }
Francis Visoiu Mistrihf5a33832019-07-26 22:42:54 +0000105 llvm_unreachable("unhandled ParseFormat");
Francis Visoiu Mistrih64a5f9e2019-07-26 21:02:02 +0000106}
107
Benjamin Kramerdc5f8052019-08-23 19:59:23 +0000108namespace {
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000109// Wrapper that holds the state needed to interact with the C API.
110struct CParser {
Francis Visoiu Mistrihab56cf82019-07-25 00:16:56 +0000111 std::unique_ptr<RemarkParser> TheParser;
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000112 Optional<std::string> Err;
113
114 CParser(Format ParserFormat, StringRef Buf,
Francis Visoiu Mistrih4287c952019-07-23 22:50:08 +0000115 Optional<ParsedStringTable> StrTab = None)
116 : TheParser(cantFail(
117 StrTab ? createRemarkParser(ParserFormat, Buf, std::move(*StrTab))
118 : createRemarkParser(ParserFormat, Buf))) {}
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000119
120 void handleError(Error E) { Err.emplace(toString(std::move(E))); }
121 bool hasError() const { return Err.hasValue(); }
122 const char *getMessage() const { return Err ? Err->c_str() : nullptr; };
123};
Benjamin Kramerdc5f8052019-08-23 19:59:23 +0000124} // namespace
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000125
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000126// Create wrappers for C Binding types (see CBindingWrapping.h).
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000127DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CParser, LLVMRemarkParserRef)
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000128
129extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
130 uint64_t Size) {
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000131 return wrap(new CParser(Format::YAML,
132 StringRef(static_cast<const char *>(Buf), Size)));
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000133}
134
135extern "C" LLVMRemarkEntryRef
136LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser) {
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000137 CParser &TheCParser = *unwrap(Parser);
Francis Visoiu Mistrihab56cf82019-07-25 00:16:56 +0000138 remarks::RemarkParser &TheParser = *TheCParser.TheParser;
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000139
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000140 Expected<std::unique_ptr<Remark>> MaybeRemark = TheParser.next();
141 if (Error E = MaybeRemark.takeError()) {
142 if (E.isA<EndOfFileError>()) {
143 consumeError(std::move(E));
144 return nullptr;
145 }
146
147 // Handle the error. Allow it to be checked through HasError and
148 // GetErrorMessage.
149 TheCParser.handleError(std::move(E));
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000150 return nullptr;
151 }
152
Francis Visoiu Mistrih5a05cc02019-03-19 21:11:07 +0000153 // Valid remark.
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000154 return wrap(MaybeRemark->release());
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +0000155}
156
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000157extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) {
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000158 return unwrap(Parser)->hasError();
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +0000159}
160
161extern "C" const char *
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000162LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) {
Francis Visoiu Mistrih94bad222019-07-16 15:25:05 +0000163 return unwrap(Parser)->getMessage();
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +0000164}
165
Francis Visoiu Mistrih1c4bab32019-03-05 20:45:17 +0000166extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser) {
Francis Visoiu Mistrih2e76cab2018-10-10 18:43:42 +0000167 delete unwrap(Parser);
168}