2019-03-19 22:11:07 +01:00
|
|
|
//===-- YAMLRemarkParser.h - Parser for YAML remarks ------------*- C++/-*-===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides the impementation of the YAML remark parser.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_REMARKS_YAML_REMARK_PARSER_H
|
|
|
|
#define LLVM_REMARKS_YAML_REMARK_PARSER_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Remarks/Remark.h"
|
[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
|
|
|
#include "llvm/Remarks/RemarkParser.h"
|
2019-03-19 22:11:07 +01:00
|
|
|
#include "llvm/Support/Error.h"
|
2019-07-26 23:02:02 +02:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2019-03-19 22:11:07 +01:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/YAMLParser.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace remarks {
|
|
|
|
|
|
|
|
class YAMLParseError : public ErrorInfo<YAMLParseError> {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2019-07-16 17:25:05 +02:00
|
|
|
YAMLParseError(StringRef Message, SourceMgr &SM, yaml::Stream &Stream,
|
|
|
|
yaml::Node &Node);
|
|
|
|
|
|
|
|
YAMLParseError(StringRef Message) : Message(Message) {}
|
2019-03-19 22:11:07 +01:00
|
|
|
|
|
|
|
void log(raw_ostream &OS) const override { OS << Message; }
|
|
|
|
std::error_code convertToErrorCode() const override {
|
|
|
|
return inconvertibleErrorCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-07-16 17:25:05 +02:00
|
|
|
std::string Message;
|
2019-03-19 22:11:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Regular YAML to Remark parser.
|
2019-07-25 02:16:56 +02:00
|
|
|
struct YAMLRemarkParser : public RemarkParser {
|
2019-07-16 17:25:05 +02:00
|
|
|
/// The string table used for parsing strings.
|
2019-07-24 00:50:08 +02:00
|
|
|
Optional<ParsedStringTable> StrTab;
|
2019-07-16 17:25:05 +02:00
|
|
|
/// Last error message that can come from the YAML parser diagnostics.
|
|
|
|
/// We need this for catching errors in the constructor.
|
|
|
|
std::string LastErrorMessage;
|
|
|
|
/// Source manager for better error messages.
|
|
|
|
SourceMgr SM;
|
|
|
|
/// Stream for yaml parsing.
|
|
|
|
yaml::Stream Stream;
|
2019-03-19 22:11:07 +01:00
|
|
|
/// Iterator in the YAML stream.
|
|
|
|
yaml::document_iterator YAMLIt;
|
2019-07-26 23:02:02 +02:00
|
|
|
/// If we parse remark metadata in separate mode, we need to open a new file
|
|
|
|
/// and parse that.
|
|
|
|
std::unique_ptr<MemoryBuffer> SeparateBuf;
|
2019-03-19 22:11:07 +01:00
|
|
|
|
2019-07-23 22:42:46 +02:00
|
|
|
YAMLRemarkParser(StringRef Buf);
|
2019-07-16 17:25:05 +02:00
|
|
|
|
|
|
|
Expected<std::unique_ptr<Remark>> next() override;
|
2019-03-19 22:11:07 +01:00
|
|
|
|
2019-07-25 02:16:56 +02:00
|
|
|
static bool classof(const RemarkParser *P) {
|
2019-07-16 17:25:05 +02:00
|
|
|
return P->ParserFormat == Format::YAML;
|
2019-03-19 22:11:07 +01:00
|
|
|
}
|
2019-07-16 17:25:05 +02:00
|
|
|
|
2019-07-23 22:42:46 +02:00
|
|
|
protected:
|
2019-07-24 00:50:08 +02:00
|
|
|
YAMLRemarkParser(StringRef Buf, Optional<ParsedStringTable> StrTab);
|
2019-07-16 17:25:05 +02:00
|
|
|
/// Create a YAMLParseError error from an existing error generated by the YAML
|
|
|
|
/// parser.
|
|
|
|
/// If there is no error, this returns Success.
|
|
|
|
Error error();
|
|
|
|
/// Create a YAMLParseError error referencing a specific node.
|
|
|
|
Error error(StringRef Message, yaml::Node &Node);
|
|
|
|
/// Parse a YAML remark to a remarks::Remark object.
|
|
|
|
Expected<std::unique_ptr<Remark>> parseRemark(yaml::Document &Remark);
|
|
|
|
/// Parse the type of a remark to an enum type.
|
|
|
|
Expected<Type> parseType(yaml::MappingNode &Node);
|
|
|
|
/// Parse one key to a string.
|
|
|
|
Expected<StringRef> parseKey(yaml::KeyValueNode &Node);
|
|
|
|
/// Parse one value to a string.
|
2019-07-23 22:42:46 +02:00
|
|
|
virtual Expected<StringRef> parseStr(yaml::KeyValueNode &Node);
|
2019-07-16 17:25:05 +02:00
|
|
|
/// Parse one value to an unsigned.
|
|
|
|
Expected<unsigned> parseUnsigned(yaml::KeyValueNode &Node);
|
|
|
|
/// Parse a debug location.
|
|
|
|
Expected<RemarkLocation> parseDebugLoc(yaml::KeyValueNode &Node);
|
|
|
|
/// Parse an argument.
|
|
|
|
Expected<Argument> parseArg(yaml::Node &Node);
|
2019-03-19 22:11:07 +01:00
|
|
|
};
|
2019-07-23 22:42:46 +02:00
|
|
|
|
|
|
|
/// YAML with a string table to Remark parser.
|
|
|
|
struct YAMLStrTabRemarkParser : public YAMLRemarkParser {
|
2019-07-24 00:50:08 +02:00
|
|
|
YAMLStrTabRemarkParser(StringRef Buf, ParsedStringTable StrTab)
|
|
|
|
: YAMLRemarkParser(Buf, std::move(StrTab)) {}
|
2019-07-23 22:42:46 +02:00
|
|
|
|
2019-07-25 02:16:56 +02:00
|
|
|
static bool classof(const RemarkParser *P) {
|
2019-07-23 22:42:46 +02:00
|
|
|
return P->ParserFormat == Format::YAMLStrTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Parse one value to a string.
|
|
|
|
Expected<StringRef> parseStr(yaml::KeyValueNode &Node) override;
|
|
|
|
};
|
2019-07-26 23:02:02 +02:00
|
|
|
|
|
|
|
Expected<std::unique_ptr<YAMLRemarkParser>>
|
|
|
|
createYAMLParserFromMeta(StringRef Buf,
|
2019-10-16 17:40:59 +02:00
|
|
|
Optional<ParsedStringTable> StrTab = None,
|
|
|
|
Optional<StringRef> ExternalFilePrependPath = None);
|
2019-07-26 23:02:02 +02:00
|
|
|
|
2019-03-19 22:11:07 +01:00
|
|
|
} // end namespace remarks
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif /* LLVM_REMARKS_YAML_REMARK_PARSER_H */
|