1 //===- RemarkParser.cpp --------------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file provides utility methods used by clients that want to use the 10 // parser for remark diagnostics in LLVM. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Remarks/RemarkParser.h" 15 #include "YAMLRemarkParser.h" 16 #include "llvm-c/Remarks.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/Support/CBindingWrapping.h" 19 20 using namespace llvm; 21 using namespace llvm::remarks; 22 23 char EndOfFileError::ID = 0; 24 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 35 Expected<StringRef> ParsedStringTable::operator[](size_t Index) const { 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 50 Expected<std::unique_ptr<Parser>> 51 llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf) { 52 switch (ParserFormat) { 53 case Format::YAML: 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."); 59 case Format::Unknown: 60 return createStringError(std::make_error_code(std::errc::invalid_argument), 61 "Unknown remark parser format."); 62 } 63 } 64 65 Expected<std::unique_ptr<Parser>> 66 llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf, 67 ParsedStringTable StrTab) { 68 switch (ParserFormat) { 69 case Format::YAML: 70 return createStringError(std::make_error_code(std::errc::invalid_argument), 71 "The YAML format can't be used with a string " 72 "table. Use yaml-strtab instead."); 73 case Format::YAMLStrTab: 74 return llvm::make_unique<YAMLStrTabRemarkParser>(Buf, std::move(StrTab)); 75 case Format::Unknown: 76 return createStringError(std::make_error_code(std::errc::invalid_argument), 77 "Unknown remark parser format."); 78 } 79 } 80 81 // Wrapper that holds the state needed to interact with the C API. 82 struct CParser { 83 std::unique_ptr<Parser> TheParser; 84 Optional<std::string> Err; 85 86 CParser(Format ParserFormat, StringRef Buf, 87 Optional<ParsedStringTable> StrTab = None) 88 : TheParser(cantFail( 89 StrTab ? createRemarkParser(ParserFormat, Buf, std::move(*StrTab)) 90 : createRemarkParser(ParserFormat, Buf))) {} 91 92 void handleError(Error E) { Err.emplace(toString(std::move(E))); } 93 bool hasError() const { return Err.hasValue(); } 94 const char *getMessage() const { return Err ? Err->c_str() : nullptr; }; 95 }; 96 97 // Create wrappers for C Binding types (see CBindingWrapping.h). 98 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CParser, LLVMRemarkParserRef) 99 100 extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf, 101 uint64_t Size) { 102 return wrap(new CParser(Format::YAML, 103 StringRef(static_cast<const char *>(Buf), Size))); 104 } 105 106 extern "C" LLVMRemarkEntryRef 107 LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser) { 108 CParser &TheCParser = *unwrap(Parser); 109 remarks::Parser &TheParser = *TheCParser.TheParser; 110 111 Expected<std::unique_ptr<Remark>> MaybeRemark = TheParser.next(); 112 if (Error E = MaybeRemark.takeError()) { 113 if (E.isA<EndOfFileError>()) { 114 consumeError(std::move(E)); 115 return nullptr; 116 } 117 118 // Handle the error. Allow it to be checked through HasError and 119 // GetErrorMessage. 120 TheCParser.handleError(std::move(E)); 121 return nullptr; 122 } 123 124 // Valid remark. 125 return wrap(MaybeRemark->release()); 126 } 127 128 extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) { 129 return unwrap(Parser)->hasError(); 130 } 131 132 extern "C" const char * 133 LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) { 134 return unwrap(Parser)->getMessage(); 135 } 136 137 extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser) { 138 delete unwrap(Parser); 139 } 140