| 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" |
| Francis Visoiu Mistrih | a85d9ef | 2019-09-09 17:43:50 +0000 | [diff] [blame^] | 15 | #include "BitstreamRemarkParser.h" |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 16 | #include "YAMLRemarkParser.h" |
| Francis Visoiu Mistrih | 1c4bab3 | 2019-03-05 20:45:17 +0000 | [diff] [blame] | 17 | #include "llvm-c/Remarks.h" |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CBindingWrapping.h" |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 22 | using namespace llvm::remarks; |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 23 | |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 24 | char EndOfFileError::ID = 0; |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 25 | |
| Francis Visoiu Mistrih | 7fee2b8 | 2019-04-24 00:06:24 +0000 | [diff] [blame] | 26 | ParsedStringTable::ParsedStringTable(StringRef InBuffer) : Buffer(InBuffer) { |
| 27 | while (!InBuffer.empty()) { |
| 28 | // Strings are separated by '\0' bytes. |
| 29 | std::pair<StringRef, StringRef> Split = InBuffer.split('\0'); |
| 30 | // We only store the offset from the beginning of the buffer. |
| 31 | Offsets.push_back(Split.first.data() - Buffer.data()); |
| 32 | InBuffer = Split.second; |
| 33 | } |
| 34 | } |
| 35 | |
| Francis Visoiu Mistrih | e6ba313 | 2019-07-04 00:30:58 +0000 | [diff] [blame] | 36 | Expected<StringRef> ParsedStringTable::operator[](size_t Index) const { |
| Francis Visoiu Mistrih | 7fee2b8 | 2019-04-24 00:06:24 +0000 | [diff] [blame] | 37 | if (Index >= Offsets.size()) |
| 38 | return createStringError( |
| 39 | std::make_error_code(std::errc::invalid_argument), |
| 40 | "String with index %u is out of bounds (size = %u).", Index, |
| 41 | Offsets.size()); |
| 42 | |
| 43 | size_t Offset = Offsets[Index]; |
| 44 | // If it's the last offset, we can't use the next offset to know the size of |
| 45 | // the string. |
| 46 | size_t NextOffset = |
| 47 | (Index == Offsets.size() - 1) ? Buffer.size() : Offsets[Index + 1]; |
| 48 | return StringRef(Buffer.data() + Offset, NextOffset - Offset - 1); |
| 49 | } |
| 50 | |
| Francis Visoiu Mistrih | ab56cf8 | 2019-07-25 00:16:56 +0000 | [diff] [blame] | 51 | Expected<std::unique_ptr<RemarkParser>> |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 52 | llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf) { |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 53 | switch (ParserFormat) { |
| 54 | case Format::YAML: |
| Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 55 | return std::make_unique<YAMLRemarkParser>(Buf); |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 56 | case Format::YAMLStrTab: |
| 57 | return createStringError( |
| 58 | std::make_error_code(std::errc::invalid_argument), |
| 59 | "The YAML with string table format requires a parsed string table."); |
| Francis Visoiu Mistrih | 84e8097 | 2019-07-31 00:13:51 +0000 | [diff] [blame] | 60 | case Format::Bitstream: |
| Francis Visoiu Mistrih | a85d9ef | 2019-09-09 17:43:50 +0000 | [diff] [blame^] | 61 | return std::make_unique<BitstreamRemarkParser>(Buf); |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 62 | case Format::Unknown: |
| 63 | return createStringError(std::make_error_code(std::errc::invalid_argument), |
| 64 | "Unknown remark parser format."); |
| 65 | } |
| Haojian Wu | 509ad30 | 2019-07-24 07:55:01 +0000 | [diff] [blame] | 66 | llvm_unreachable("unhandled ParseFormat"); |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| Francis Visoiu Mistrih | ab56cf8 | 2019-07-25 00:16:56 +0000 | [diff] [blame] | 69 | Expected<std::unique_ptr<RemarkParser>> |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 70 | llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf, |
| Francis Visoiu Mistrih | 4287c95 | 2019-07-23 22:50:08 +0000 | [diff] [blame] | 71 | ParsedStringTable StrTab) { |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 72 | 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 Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 78 | return std::make_unique<YAMLStrTabRemarkParser>(Buf, std::move(StrTab)); |
| Francis Visoiu Mistrih | 84e8097 | 2019-07-31 00:13:51 +0000 | [diff] [blame] | 79 | case Format::Bitstream: |
| Francis Visoiu Mistrih | a85d9ef | 2019-09-09 17:43:50 +0000 | [diff] [blame^] | 80 | return std::make_unique<BitstreamRemarkParser>(Buf, std::move(StrTab)); |
| Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 81 | case Format::Unknown: |
| 82 | return createStringError(std::make_error_code(std::errc::invalid_argument), |
| 83 | "Unknown remark parser format."); |
| 84 | } |
| Haojian Wu | 509ad30 | 2019-07-24 07:55:01 +0000 | [diff] [blame] | 85 | llvm_unreachable("unhandled ParseFormat"); |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| Francis Visoiu Mistrih | 64a5f9e | 2019-07-26 21:02:02 +0000 | [diff] [blame] | 88 | Expected<std::unique_ptr<RemarkParser>> |
| 89 | llvm::remarks::createRemarkParserFromMeta(Format ParserFormat, StringRef Buf, |
| 90 | Optional<ParsedStringTable> StrTab) { |
| 91 | switch (ParserFormat) { |
| 92 | // Depending on the metadata, the format can be either yaml or yaml-strtab, |
| 93 | // regardless of the input argument. |
| 94 | case Format::YAML: |
| 95 | case Format::YAMLStrTab: |
| 96 | return createYAMLParserFromMeta(Buf, std::move(StrTab)); |
| Francis Visoiu Mistrih | 84e8097 | 2019-07-31 00:13:51 +0000 | [diff] [blame] | 97 | case Format::Bitstream: |
| Francis Visoiu Mistrih | a85d9ef | 2019-09-09 17:43:50 +0000 | [diff] [blame^] | 98 | return createBitstreamParserFromMeta(Buf, std::move(StrTab)); |
| Francis Visoiu Mistrih | 64a5f9e | 2019-07-26 21:02:02 +0000 | [diff] [blame] | 99 | case Format::Unknown: |
| 100 | return createStringError(std::make_error_code(std::errc::invalid_argument), |
| 101 | "Unknown remark parser format."); |
| 102 | } |
| Francis Visoiu Mistrih | f5a3383 | 2019-07-26 22:42:54 +0000 | [diff] [blame] | 103 | llvm_unreachable("unhandled ParseFormat"); |
| Francis Visoiu Mistrih | 64a5f9e | 2019-07-26 21:02:02 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| Benjamin Kramer | dc5f805 | 2019-08-23 19:59:23 +0000 | [diff] [blame] | 106 | namespace { |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 107 | // Wrapper that holds the state needed to interact with the C API. |
| 108 | struct CParser { |
| Francis Visoiu Mistrih | ab56cf8 | 2019-07-25 00:16:56 +0000 | [diff] [blame] | 109 | std::unique_ptr<RemarkParser> TheParser; |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 110 | Optional<std::string> Err; |
| 111 | |
| 112 | CParser(Format ParserFormat, StringRef Buf, |
| Francis Visoiu Mistrih | 4287c95 | 2019-07-23 22:50:08 +0000 | [diff] [blame] | 113 | Optional<ParsedStringTable> StrTab = None) |
| 114 | : TheParser(cantFail( |
| 115 | StrTab ? createRemarkParser(ParserFormat, Buf, std::move(*StrTab)) |
| 116 | : createRemarkParser(ParserFormat, Buf))) {} |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 117 | |
| 118 | void handleError(Error E) { Err.emplace(toString(std::move(E))); } |
| 119 | bool hasError() const { return Err.hasValue(); } |
| 120 | const char *getMessage() const { return Err ? Err->c_str() : nullptr; }; |
| 121 | }; |
| Benjamin Kramer | dc5f805 | 2019-08-23 19:59:23 +0000 | [diff] [blame] | 122 | } // namespace |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 123 | |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 124 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 125 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CParser, LLVMRemarkParserRef) |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 126 | |
| 127 | extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf, |
| 128 | uint64_t Size) { |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 129 | return wrap(new CParser(Format::YAML, |
| 130 | StringRef(static_cast<const char *>(Buf), Size))); |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| Francis Visoiu Mistrih | a85d9ef | 2019-09-09 17:43:50 +0000 | [diff] [blame^] | 133 | extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateBitstream(const void *Buf, |
| 134 | uint64_t Size) { |
| 135 | return wrap(new CParser(Format::Bitstream, |
| 136 | StringRef(static_cast<const char *>(Buf), Size))); |
| 137 | } |
| 138 | |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 139 | extern "C" LLVMRemarkEntryRef |
| 140 | LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser) { |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 141 | CParser &TheCParser = *unwrap(Parser); |
| Francis Visoiu Mistrih | ab56cf8 | 2019-07-25 00:16:56 +0000 | [diff] [blame] | 142 | remarks::RemarkParser &TheParser = *TheCParser.TheParser; |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 143 | |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 144 | Expected<std::unique_ptr<Remark>> MaybeRemark = TheParser.next(); |
| 145 | if (Error E = MaybeRemark.takeError()) { |
| 146 | if (E.isA<EndOfFileError>()) { |
| 147 | consumeError(std::move(E)); |
| 148 | return nullptr; |
| 149 | } |
| 150 | |
| 151 | // Handle the error. Allow it to be checked through HasError and |
| 152 | // GetErrorMessage. |
| 153 | TheCParser.handleError(std::move(E)); |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 154 | return nullptr; |
| 155 | } |
| 156 | |
| Francis Visoiu Mistrih | 5a05cc0 | 2019-03-19 21:11:07 +0000 | [diff] [blame] | 157 | // Valid remark. |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 158 | return wrap(MaybeRemark->release()); |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| Francis Visoiu Mistrih | 1c4bab3 | 2019-03-05 20:45:17 +0000 | [diff] [blame] | 161 | extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) { |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 162 | return unwrap(Parser)->hasError(); |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | extern "C" const char * |
| Francis Visoiu Mistrih | 1c4bab3 | 2019-03-05 20:45:17 +0000 | [diff] [blame] | 166 | LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) { |
| Francis Visoiu Mistrih | 94bad22 | 2019-07-16 15:25:05 +0000 | [diff] [blame] | 167 | return unwrap(Parser)->getMessage(); |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| Francis Visoiu Mistrih | 1c4bab3 | 2019-03-05 20:45:17 +0000 | [diff] [blame] | 170 | extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser) { |
| Francis Visoiu Mistrih | 2e76cab | 2018-10-10 18:43:42 +0000 | [diff] [blame] | 171 | delete unwrap(Parser); |
| 172 | } |