| Francis Visoiu Mistrih | 1c4bab3 | 2019-03-05 20:45:17 +0000 | [diff] [blame] | 1 | //===- RemarkParser.cpp --------------------------------------------------===// |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // 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 Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file provides utility methods used by clients that want to use the |
| Francis Visoiu Mistrih | 1c4bab3 | 2019-03-05 20:45:17 +0000 | [diff] [blame] | 10 | // parser for remark diagnostics in LLVM. |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 14 | #include "llvm/Remarks/RemarkParser.h" |
| 15 | #include "YAMLRemarkParser.h" |
| Francis Visoiu Mistrih | 1c4bab3 | 2019-03-05 20:45:17 +0000 | [diff] [blame] | 16 | #include "llvm-c/Remarks.h" |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CBindingWrapping.h" |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 21 | using namespace llvm::remarks; |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 22 | |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 23 | char EndOfFileError::ID = 0; |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 24 | |
| Francis Visoiu Mistrih | 7fee2b8 | 2019-04-24 00:06:24 +0000 | [diff] [blame] | 25 | ParsedStringTable::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 Mistrih | e6ba313 | 2019-07-04 00:30:58 +0000 | [diff] [blame] | 35 | Expected<StringRef> ParsedStringTable::operator[](size_t Index) const { |
| Francis Visoiu Mistrih | 7fee2b8 | 2019-04-24 00:06:24 +0000 | [diff] [blame] | 36 | 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 Mistrih | ab56cf8 | 2019-07-25 00:16:56 +0000 | [diff] [blame] | 50 | Expected<std::unique_ptr<RemarkParser>> |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 51 | llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf) { |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 52 | switch (ParserFormat) { |
| 53 | case Format::YAML: |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 54 | return llvm::make_unique<YAMLRemarkParser>(Buf); |
| 55 | 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 Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 59 | case Format::Unknown: |
| 60 | return createStringError(std::make_error_code(std::errc::invalid_argument), |
| 61 | "Unknown remark parser format."); |
| 62 | } |
| Haojian Wu | 509ad30 | 2019-07-24 07:55:01 +0000 | [diff] [blame] | 63 | llvm_unreachable("unhandled ParseFormat"); |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| Francis Visoiu Mistrih | ab56cf8 | 2019-07-25 00:16:56 +0000 | [diff] [blame] | 66 | Expected<std::unique_ptr<RemarkParser>> |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 67 | llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf, |
| Francis Visoiu Mistrih | 4287c95 | 2019-07-23 22:50:08 +0000 | [diff] [blame] | 68 | ParsedStringTable StrTab) { |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 69 | switch (ParserFormat) { |
| 70 | case Format::YAML: |
| 71 | return createStringError(std::make_error_code(std::errc::invalid_argument), |
| 72 | "The YAML format can't be used with a string " |
| 73 | "table. Use yaml-strtab instead."); |
| 74 | case Format::YAMLStrTab: |
| Francis Visoiu Mistrih | 4287c95 | 2019-07-23 22:50:08 +0000 | [diff] [blame] | 75 | return llvm::make_unique<YAMLStrTabRemarkParser>(Buf, std::move(StrTab)); |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 76 | case Format::Unknown: |
| 77 | return createStringError(std::make_error_code(std::errc::invalid_argument), |
| 78 | "Unknown remark parser format."); |
| 79 | } |
| Haojian Wu | 509ad30 | 2019-07-24 07:55:01 +0000 | [diff] [blame] | 80 | llvm_unreachable("unhandled ParseFormat"); |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| Francis Visoiu Mistrih | 64a5f9e | 2019-07-26 21:02:02 +0000 | [diff] [blame^] | 83 | Expected<std::unique_ptr<RemarkParser>> |
| 84 | llvm::remarks::createRemarkParserFromMeta(Format ParserFormat, StringRef Buf, |
| 85 | Optional<ParsedStringTable> StrTab) { |
| 86 | switch (ParserFormat) { |
| 87 | // Depending on the metadata, the format can be either yaml or yaml-strtab, |
| 88 | // regardless of the input argument. |
| 89 | case Format::YAML: |
| 90 | case Format::YAMLStrTab: |
| 91 | return createYAMLParserFromMeta(Buf, std::move(StrTab)); |
| 92 | case Format::Unknown: |
| 93 | return createStringError(std::make_error_code(std::errc::invalid_argument), |
| 94 | "Unknown remark parser format."); |
| 95 | } |
| 96 | } |
| 97 | |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 98 | // Wrapper that holds the state needed to interact with the C API. |
| 99 | struct CParser { |
| Francis Visoiu Mistrih | ab56cf8 | 2019-07-25 00:16:56 +0000 | [diff] [blame] | 100 | std::unique_ptr<RemarkParser> TheParser; |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 101 | Optional<std::string> Err; |
| 102 | |
| 103 | CParser(Format ParserFormat, StringRef Buf, |
| Francis Visoiu Mistrih | 4287c95 | 2019-07-23 22:50:08 +0000 | [diff] [blame] | 104 | Optional<ParsedStringTable> StrTab = None) |
| 105 | : TheParser(cantFail( |
| 106 | StrTab ? createRemarkParser(ParserFormat, Buf, std::move(*StrTab)) |
| 107 | : createRemarkParser(ParserFormat, Buf))) {} |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 108 | |
| 109 | void handleError(Error E) { Err.emplace(toString(std::move(E))); } |
| 110 | bool hasError() const { return Err.hasValue(); } |
| 111 | const char *getMessage() const { return Err ? Err->c_str() : nullptr; }; |
| 112 | }; |
| 113 | |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 114 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 115 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CParser, LLVMRemarkParserRef) |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 116 | |
| 117 | extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf, |
| 118 | uint64_t Size) { |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 119 | return wrap(new CParser(Format::YAML, |
| 120 | StringRef(static_cast<const char *>(Buf), Size))); |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | extern "C" LLVMRemarkEntryRef |
| 124 | LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser) { |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 125 | CParser &TheCParser = *unwrap(Parser); |
| Francis Visoiu Mistrih | ab56cf8 | 2019-07-25 00:16:56 +0000 | [diff] [blame] | 126 | remarks::RemarkParser &TheParser = *TheCParser.TheParser; |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 127 | |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 128 | Expected<std::unique_ptr<Remark>> MaybeRemark = TheParser.next(); |
| 129 | if (Error E = MaybeRemark.takeError()) { |
| 130 | if (E.isA<EndOfFileError>()) { |
| 131 | consumeError(std::move(E)); |
| 132 | return nullptr; |
| 133 | } |
| 134 | |
| 135 | // Handle the error. Allow it to be checked through HasError and |
| 136 | // GetErrorMessage. |
| 137 | TheCParser.handleError(std::move(E)); |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 138 | return nullptr; |
| 139 | } |
| 140 | |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 141 | // Valid remark. |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 142 | return wrap(MaybeRemark->release()); |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| Francis Visoiu Mistrih | 1c4bab3 | 2019-03-05 20:45:17 +0000 | [diff] [blame] | 145 | extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) { |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 146 | return unwrap(Parser)->hasError(); |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | extern "C" const char * |
| Francis Visoiu Mistrih | 1c4bab3 | 2019-03-05 20:45:17 +0000 | [diff] [blame] | 150 | LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) { |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 151 | return unwrap(Parser)->getMessage(); |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| Francis Visoiu Mistrih | 1c4bab3 | 2019-03-05 20:45:17 +0000 | [diff] [blame] | 154 | extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser) { |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 155 | delete unwrap(Parser); |
| 156 | } |