namespace llvm {
namespace remarks {
-struct ParserImpl;
-struct ParsedStringTable;
-
class EndOfFileError : public ErrorInfo<EndOfFileError> {
public:
static char ID;
struct ParsedStringTable {
/// The buffer mapped from the section contents.
StringRef Buffer;
- /// Collection of offsets in the buffer for each string entry.
- SmallVector<size_t, 8> Offsets;
+ /// This object has high changes to be std::move'd around, so don't use a
+ /// SmallVector for once.
+ std::vector<size_t> Offsets;
- Expected<StringRef> operator[](size_t Index) const;
ParsedStringTable(StringRef Buffer);
+ /// Disable copy.
+ ParsedStringTable(const ParsedStringTable &) = delete;
+ ParsedStringTable &operator=(const ParsedStringTable &) = delete;
+ /// Should be movable.
+ ParsedStringTable(ParsedStringTable &&) = default;
+ ParsedStringTable &operator=(ParsedStringTable &&) = default;
+
+ size_t size() const { return Offsets.size(); }
+ Expected<StringRef> operator[](size_t Index) const;
};
Expected<std::unique_ptr<Parser>> createRemarkParser(Format ParserFormat,
StringRef Buf);
-Expected<std::unique_ptr<Parser>>
-createRemarkParser(Format ParserFormat, StringRef Buf,
- const ParsedStringTable &StrTab);
+Expected<std::unique_ptr<Parser>> createRemarkParser(Format ParserFormat,
+ StringRef Buf,
+ ParsedStringTable StrTab);
} // end namespace remarks
} // end namespace llvm
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
-#include "llvm/Support/Allocator.h"
+#include "llvm/Remarks/RemarkParser.h"
#include <vector>
namespace llvm {
/// This table can be for example serialized in a section to be consumed after
/// the compilation.
struct StringTable {
- /// Allocator holding all the memory used by the map.
- BumpPtrAllocator Allocator;
/// The string table containing all the unique strings used in the output.
/// It maps a string to an unique ID.
- StringMap<unsigned, BumpPtrAllocator &> StrTab;
+ StringMap<unsigned, BumpPtrAllocator> StrTab;
/// Total size of the string table when serialized.
size_t SerializedSize = 0;
- StringTable() : Allocator(), StrTab(Allocator) {}
+ StringTable() = default;
+
+ /// Disable copy.
+ StringTable(const StringTable &) = delete;
+ StringTable &operator=(const StringTable &) = delete;
+ /// Should be movable.
+ StringTable(StringTable &&) = default;
+ StringTable &operator=(StringTable &&) = default;
+
+ /// Construct a string table from a ParsedStringTable.
+ StringTable(const ParsedStringTable &Other);
+
/// Add a string to the table. It returns an unique ID of the string.
std::pair<unsigned, StringRef> add(StringRef Str);
/// Serialize the string table to a stream. It is serialized as a little
Expected<std::unique_ptr<Parser>>
llvm::remarks::createRemarkParser(Format ParserFormat, StringRef Buf,
- const ParsedStringTable &StrTab) {
+ ParsedStringTable StrTab) {
switch (ParserFormat) {
case Format::YAML:
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:
- return llvm::make_unique<YAMLStrTabRemarkParser>(Buf, StrTab);
+ return llvm::make_unique<YAMLStrTabRemarkParser>(Buf, std::move(StrTab));
case Format::Unknown:
return createStringError(std::make_error_code(std::errc::invalid_argument),
"Unknown remark parser format.");
Optional<std::string> Err;
CParser(Format ParserFormat, StringRef Buf,
- Optional<const ParsedStringTable *> StrTab = None)
- : TheParser(cantFail(StrTab
- ? createRemarkParser(ParserFormat, Buf, **StrTab)
- : createRemarkParser(ParserFormat, Buf))) {}
+ Optional<ParsedStringTable> StrTab = None)
+ : TheParser(cantFail(
+ StrTab ? createRemarkParser(ParserFormat, Buf, std::move(*StrTab))
+ : createRemarkParser(ParserFormat, Buf))) {}
void handleError(Error E) { Err.emplace(toString(std::move(E))); }
bool hasError() const { return Err.hasValue(); }
//===----------------------------------------------------------------------===//
#include "llvm/Remarks/RemarkStringTable.h"
+#include "llvm/Remarks/RemarkParser.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/Error.h"
#include <vector>
using namespace llvm;
using namespace llvm::remarks;
+StringTable::StringTable(const ParsedStringTable &Other) : StrTab() {
+ for (unsigned i = 0, e = Other.size(); i < e; ++i)
+ if (Expected<StringRef> MaybeStr = Other[i])
+ add(*MaybeStr);
+ else
+ llvm_unreachable("Unexpected error while building remarks string table.");
+}
+
std::pair<unsigned, StringRef> StringTable::add(StringRef Str) {
size_t NextID = StrTab.size();
auto KV = StrTab.insert({Str, NextID});
: YAMLRemarkParser(Buf, None) {}
YAMLRemarkParser::YAMLRemarkParser(StringRef Buf,
- Optional<const ParsedStringTable *> StrTab)
- : Parser{Format::YAML}, StrTab(StrTab), LastErrorMessage(),
+ Optional<ParsedStringTable> StrTab)
+ : Parser{Format::YAML}, StrTab(std::move(StrTab)), LastErrorMessage(),
SM(setupSM(LastErrorMessage)), Stream(Buf, SM), YAMLIt(Stream.begin()) {}
Error YAMLRemarkParser::error(StringRef Message, yaml::Node &Node) {
else
return MaybeStrID.takeError();
- if (Expected<StringRef> Str = (**StrTab)[StrID])
+ if (Expected<StringRef> Str = (*StrTab)[StrID])
Result = *Str;
else
return Str.takeError();
/// Regular YAML to Remark parser.
struct YAMLRemarkParser : public Parser {
/// The string table used for parsing strings.
- Optional<const ParsedStringTable *> StrTab;
+ Optional<ParsedStringTable> StrTab;
/// Last error message that can come from the YAML parser diagnostics.
/// We need this for catching errors in the constructor.
std::string LastErrorMessage;
}
protected:
- YAMLRemarkParser(StringRef Buf, Optional<const ParsedStringTable *> StrTab);
+ YAMLRemarkParser(StringRef Buf, Optional<ParsedStringTable> StrTab);
/// Create a YAMLParseError error from an existing error generated by the YAML
/// parser.
/// If there is no error, this returns Success.
/// YAML with a string table to Remark parser.
struct YAMLStrTabRemarkParser : public YAMLRemarkParser {
- YAMLStrTabRemarkParser(StringRef Buf, const ParsedStringTable &StrTab)
- : YAMLRemarkParser(Buf, &StrTab) {}
+ YAMLStrTabRemarkParser(StringRef Buf, ParsedStringTable StrTab)
+ : YAMLRemarkParser(Buf, std::move(StrTab)) {}
static bool classof(const Parser *P) {
return P->ParserFormat == Format::YAMLStrTab;
remarks::ParsedStringTable StrTab(StrTabBuf);
Expected<std::unique_ptr<remarks::Parser>> MaybeParser =
- remarks::createRemarkParser(remarks::Format::YAMLStrTab, Buf, StrTab);
+ remarks::createRemarkParser(remarks::Format::YAMLStrTab, Buf,
+ std::move(StrTab));
EXPECT_FALSE(errorToBool(MaybeParser.takeError()));
EXPECT_TRUE(*MaybeParser != nullptr);
remarks::ParsedStringTable StrTab(StrTabBuf);
Expected<std::unique_ptr<remarks::Parser>> MaybeParser =
- remarks::createRemarkParser(remarks::Format::YAMLStrTab, Buf, StrTab);
+ remarks::createRemarkParser(remarks::Format::YAMLStrTab, Buf,
+ std::move(StrTab));
EXPECT_FALSE(errorToBool(MaybeParser.takeError()));
EXPECT_TRUE(*MaybeParser != nullptr);