2019-03-05 21:45:17 +01:00
|
|
|
//===- RemarkParser.cpp --------------------------------------------------===//
|
2018-10-10 20:43:42 +02:00
|
|
|
//
|
2019-01-19 11:56:40 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-10 20:43:42 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides utility methods used by clients that want to use the
|
2019-03-05 21:45:17 +01:00
|
|
|
// parser for remark diagnostics in LLVM.
|
2018-10-10 20:43:42 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-19 22:11:07 +01:00
|
|
|
#include "llvm/Remarks/RemarkParser.h"
|
|
|
|
#include "YAMLRemarkParser.h"
|
2019-03-05 21:45:17 +01:00
|
|
|
#include "llvm-c/Remarks.h"
|
2018-10-10 20:43:42 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2019-03-19 22:11:07 +01:00
|
|
|
#include "llvm/Support/CBindingWrapping.h"
|
2018-10-10 20:43:42 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
2019-03-19 22:11:07 +01:00
|
|
|
using namespace llvm::remarks;
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-07-16 17:25:05 +02:00
|
|
|
char EndOfFileError::ID = 0;
|
2019-03-19 19:21:43 +01:00
|
|
|
|
[Remarks] Add string deduplication using a string table
* Add support for uniquing strings in the remark streamer and emitting the string table in the remarks section.
* Add parsing support for the string table in the RemarkParser.
From this remark:
```
--- !Missed
Pass: inline
Name: NoDefinition
DebugLoc: { File: 'test-suite/SingleSource/UnitTests/2002-04-17-PrintfChar.c',
Line: 7, Column: 3 }
Function: printArgsNoRet
Args:
- Callee: printf
- String: ' will not be inlined into '
- Caller: printArgsNoRet
DebugLoc: { File: 'test-suite/SingleSource/UnitTests/2002-04-17-PrintfChar.c',
Line: 6, Column: 0 }
- String: ' because its definition is unavailable'
...
```
to:
```
--- !Missed
Pass: 0
Name: 1
DebugLoc: { File: 3, Line: 7, Column: 3 }
Function: 2
Args:
- Callee: 4
- String: 5
- Caller: 2
DebugLoc: { File: 3, Line: 6, Column: 0 }
- String: 6
...
```
And the string table in the .remarks/__remarks section containing:
```
inline\0NoDefinition\0printArgsNoRet\0
test-suite/SingleSource/UnitTests/2002-04-17-PrintfChar.c\0printf\0
will not be inlined into \0 because its definition is unavailable\0
```
This is mostly supposed to be used for testing purposes, but it gives us
a 2x reduction in the remark size, and is an incremental change for the
updates to the remarks file format.
Differential Revision: https://reviews.llvm.org/D60227
llvm-svn: 359050
2019-04-24 02:06:24 +02:00
|
|
|
ParsedStringTable::ParsedStringTable(StringRef InBuffer) : Buffer(InBuffer) {
|
|
|
|
while (!InBuffer.empty()) {
|
|
|
|
// Strings are separated by '\0' bytes.
|
|
|
|
std::pair<StringRef, StringRef> Split = InBuffer.split('\0');
|
|
|
|
// We only store the offset from the beginning of the buffer.
|
|
|
|
Offsets.push_back(Split.first.data() - Buffer.data());
|
|
|
|
InBuffer = Split.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 02:30:58 +02:00
|
|
|
Expected<StringRef> ParsedStringTable::operator[](size_t Index) const {
|
[Remarks] Add string deduplication using a string table
* Add support for uniquing strings in the remark streamer and emitting the string table in the remarks section.
* Add parsing support for the string table in the RemarkParser.
From this remark:
```
--- !Missed
Pass: inline
Name: NoDefinition
DebugLoc: { File: 'test-suite/SingleSource/UnitTests/2002-04-17-PrintfChar.c',
Line: 7, Column: 3 }
Function: printArgsNoRet
Args:
- Callee: printf
- String: ' will not be inlined into '
- Caller: printArgsNoRet
DebugLoc: { File: 'test-suite/SingleSource/UnitTests/2002-04-17-PrintfChar.c',
Line: 6, Column: 0 }
- String: ' because its definition is unavailable'
...
```
to:
```
--- !Missed
Pass: 0
Name: 1
DebugLoc: { File: 3, Line: 7, Column: 3 }
Function: 2
Args:
- Callee: 4
- String: 5
- Caller: 2
DebugLoc: { File: 3, Line: 6, Column: 0 }
- String: 6
...
```
And the string table in the .remarks/__remarks section containing:
```
inline\0NoDefinition\0printArgsNoRet\0
test-suite/SingleSource/UnitTests/2002-04-17-PrintfChar.c\0printf\0
will not be inlined into \0 because its definition is unavailable\0
```
This is mostly supposed to be used for testing purposes, but it gives us
a 2x reduction in the remark size, and is an incremental change for the
updates to the remarks file format.
Differential Revision: https://reviews.llvm.org/D60227
llvm-svn: 359050
2019-04-24 02:06:24 +02:00
|
|
|
if (Index >= Offsets.size())
|
|
|
|
return createStringError(
|
|
|
|
std::make_error_code(std::errc::invalid_argument),
|
|
|
|
"String with index %u is out of bounds (size = %u).", Index,
|
|
|
|
Offsets.size());
|
|
|
|
|
|
|
|
size_t Offset = Offsets[Index];
|
|
|
|
// If it's the last offset, we can't use the next offset to know the size of
|
|
|
|
// the string.
|
|
|
|
size_t NextOffset =
|
|
|
|
(Index == Offsets.size() - 1) ? Buffer.size() : Offsets[Index + 1];
|
|
|
|
return StringRef(Buffer.data() + Offset, NextOffset - Offset - 1);
|
|
|
|
}
|
|
|
|
|
2019-07-23 22:42:46 +02:00
|
|
|
Expected<std::unique_ptr<Parser>>
|
|
|
|
llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf) {
|
|
|
|
switch (ParserFormat) {
|
|
|
|
case Format::YAML:
|
|
|
|
return llvm::make_unique<YAMLRemarkParser>(Buf);
|
|
|
|
case Format::YAMLStrTab:
|
|
|
|
return createStringError(
|
|
|
|
std::make_error_code(std::errc::invalid_argument),
|
|
|
|
"The YAML with string table format requires a parsed string table.");
|
|
|
|
case Format::Unknown:
|
|
|
|
return createStringError(std::make_error_code(std::errc::invalid_argument),
|
|
|
|
"Unknown remark parser format.");
|
|
|
|
}
|
2019-07-24 09:55:01 +02:00
|
|
|
llvm_unreachable("unhandled ParseFormat");
|
2019-07-23 22:42:46 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 17:25:05 +02:00
|
|
|
Expected<std::unique_ptr<Parser>>
|
|
|
|
llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf,
|
2019-07-24 00:50:08 +02:00
|
|
|
ParsedStringTable StrTab) {
|
2019-07-16 17:25:05 +02:00
|
|
|
switch (ParserFormat) {
|
|
|
|
case Format::YAML:
|
2019-07-23 22:42:46 +02:00
|
|
|
return createStringError(std::make_error_code(std::errc::invalid_argument),
|
|
|
|
"The YAML format can't be used with a string "
|
|
|
|
"table. Use yaml-strtab instead.");
|
|
|
|
case Format::YAMLStrTab:
|
2019-07-24 00:50:08 +02:00
|
|
|
return llvm::make_unique<YAMLStrTabRemarkParser>(Buf, std::move(StrTab));
|
2019-07-16 17:25:05 +02:00
|
|
|
case Format::Unknown:
|
|
|
|
return createStringError(std::make_error_code(std::errc::invalid_argument),
|
|
|
|
"Unknown remark parser format.");
|
|
|
|
}
|
2019-07-24 09:55:01 +02:00
|
|
|
llvm_unreachable("unhandled ParseFormat");
|
2019-07-16 17:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper that holds the state needed to interact with the C API.
|
|
|
|
struct CParser {
|
|
|
|
std::unique_ptr<Parser> TheParser;
|
|
|
|
Optional<std::string> Err;
|
|
|
|
|
|
|
|
CParser(Format ParserFormat, StringRef Buf,
|
2019-07-24 00:50:08 +02:00
|
|
|
Optional<ParsedStringTable> StrTab = None)
|
|
|
|
: TheParser(cantFail(
|
|
|
|
StrTab ? createRemarkParser(ParserFormat, Buf, std::move(*StrTab))
|
|
|
|
: createRemarkParser(ParserFormat, Buf))) {}
|
2019-07-16 17:25:05 +02:00
|
|
|
|
|
|
|
void handleError(Error E) { Err.emplace(toString(std::move(E))); }
|
|
|
|
bool hasError() const { return Err.hasValue(); }
|
|
|
|
const char *getMessage() const { return Err ? Err->c_str() : nullptr; };
|
|
|
|
};
|
|
|
|
|
2019-03-19 19:21:43 +01:00
|
|
|
// Create wrappers for C Binding types (see CBindingWrapping.h).
|
2019-07-16 17:25:05 +02:00
|
|
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(CParser, LLVMRemarkParserRef)
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-03-19 22:11:07 +01:00
|
|
|
extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
|
|
|
|
uint64_t Size) {
|
2019-07-16 17:25:05 +02:00
|
|
|
return wrap(new CParser(Format::YAML,
|
|
|
|
StringRef(static_cast<const char *>(Buf), Size)));
|
2019-03-19 22:11:07 +01:00
|
|
|
}
|
2018-10-10 20:43:42 +02:00
|
|
|
|
2019-03-19 22:11:07 +01:00
|
|
|
extern "C" LLVMRemarkEntryRef
|
|
|
|
LLVMRemarkParserGetNext(LLVMRemarkParserRef Parser) {
|
2019-07-16 17:25:05 +02:00
|
|
|
CParser &TheCParser = *unwrap(Parser);
|
|
|
|
remarks::Parser &TheParser = *TheCParser.TheParser;
|
|
|
|
|
|
|
|
Expected<std::unique_ptr<Remark>> MaybeRemark = TheParser.next();
|
|
|
|
if (Error E = MaybeRemark.takeError()) {
|
|
|
|
if (E.isA<EndOfFileError>()) {
|
|
|
|
consumeError(std::move(E));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the error. Allow it to be checked through HasError and
|
|
|
|
// GetErrorMessage.
|
|
|
|
TheCParser.handleError(std::move(E));
|
2019-03-19 19:09:51 +01:00
|
|
|
return nullptr;
|
2019-03-19 19:21:43 +01:00
|
|
|
}
|
|
|
|
|
2019-03-19 22:11:07 +01:00
|
|
|
// Valid remark.
|
2019-07-16 17:25:05 +02:00
|
|
|
return wrap(MaybeRemark->release());
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
extern "C" LLVMBool LLVMRemarkParserHasError(LLVMRemarkParserRef Parser) {
|
2019-07-16 17:25:05 +02:00
|
|
|
return unwrap(Parser)->hasError();
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" const char *
|
2019-03-05 21:45:17 +01:00
|
|
|
LLVMRemarkParserGetErrorMessage(LLVMRemarkParserRef Parser) {
|
2019-07-16 17:25:05 +02:00
|
|
|
return unwrap(Parser)->getMessage();
|
2018-10-10 20:43:42 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 21:45:17 +01:00
|
|
|
extern "C" void LLVMRemarkParserDispose(LLVMRemarkParserRef Parser) {
|
2018-10-10 20:43:42 +02:00
|
|
|
delete unwrap(Parser);
|
|
|
|
}
|