using RemarkSetupErrorInfo<RemarkSetupFormatError>::RemarkSetupErrorInfo;
};
-enum class RemarksSerializerFormat { Unknown, YAML };
-
-Expected<RemarksSerializerFormat> parseSerializerFormat(StringRef Format);
-
/// Setup optimization remarks.
Expected<std::unique_ptr<ToolOutputFile>>
setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
namespace remarks {
constexpr uint64_t Version = 0;
-constexpr StringRef Magic("REMARKS", 7);
/// The debug location used to track a remark back to the source file.
struct RemarkLocation {
--- /dev/null
+//===-- llvm/Remarks/RemarkFormat.h - The format of 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 defines utilities to deal with the format of remarks.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_REMARKS_REMARK_FORMAT_H
+#define LLVM_REMARKS_REMARK_FORMAT_H
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Error.h"
+
+namespace llvm {
+namespace remarks {
+
+constexpr StringRef Magic("REMARKS", 7);
+
+/// The format used for serializing/deserializing remarks.
+enum class Format { Unknown, YAML };
+
+/// Parse and validate a string for the remark format.
+Expected<Format> parseFormat(StringRef FormatStr);
+
+} // end namespace remarks
+} // end namespace llvm
+
+#endif /* LLVM_REMARKS_REMARK_FORMAT_H */
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Remarks/Remark.h"
+#include "llvm/Remarks/RemarkFormat.h"
#include "llvm/Support/Error.h"
#include <memory>
struct ParserImpl;
struct ParsedStringTable;
-enum class ParserFormat { YAML };
-
/// Parser used to parse a raw buffer to remarks::Remark objects.
struct Parser {
/// The hidden implementation of the parser.
/// Create a parser parsing \p Buffer to Remark objects.
/// This constructor should be only used for parsing remarks without a string
/// table.
- Parser(ParserFormat Format, StringRef Buffer);
+ Parser(Format ParserFormat, StringRef Buffer);
/// Create a parser parsing \p Buffer to Remark objects, using \p StrTab as a
/// string table.
- Parser(ParserFormat Format, StringRef Buffer,
+ Parser(Format ParserFormat, StringRef Buffer,
const ParsedStringTable &StrTab);
// Needed because ParserImpl is an incomplete type.
#include "llvm/MC/SectionKind.h"
#include "llvm/Pass.h"
#include "llvm/Remarks/Remark.h"
+#include "llvm/Remarks/RemarkFormat.h"
#include "llvm/Remarks/RemarkStringTable.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
+#include "llvm/Remarks/RemarkFormat.h"
using namespace llvm;
char RemarkSetupFormatError::ID = 0;
static std::unique_ptr<remarks::Serializer>
-formatToSerializer(RemarksSerializerFormat RemarksFormat, raw_ostream &OS) {
+formatToSerializer(remarks::Format RemarksFormat, raw_ostream &OS) {
switch (RemarksFormat) {
default:
llvm_unreachable("Unknown remark serializer format.");
return nullptr;
- case RemarksSerializerFormat::YAML:
+ case remarks::Format::YAML:
return llvm::make_unique<remarks::YAMLSerializer>(OS);
};
}
-Expected<RemarksSerializerFormat>
-llvm::parseSerializerFormat(StringRef StrFormat) {
- auto Format = StringSwitch<RemarksSerializerFormat>(StrFormat)
- .Cases("", "yaml", RemarksSerializerFormat::YAML)
- .Default(RemarksSerializerFormat::Unknown);
-
- if (Format == RemarksSerializerFormat::Unknown)
- return createStringError(std::make_error_code(std::errc::invalid_argument),
- "Unknown remark serializer format: '%s'",
- StrFormat.data());
-
- return Format;
-}
-
Expected<std::unique_ptr<ToolOutputFile>>
llvm::setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
StringRef RemarksPasses, StringRef RemarksFormat,
if (EC)
return make_error<RemarkSetupFileError>(errorCodeToError(EC));
- Expected<RemarksSerializerFormat> Format =
- parseSerializerFormat(RemarksFormat);
+ Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);
if (Error E = Format.takeError())
return make_error<RemarkSetupFormatError>(std::move(E));
add_llvm_library(LLVMRemarks
Remark.cpp
+ RemarkFormat.cpp
RemarkParser.cpp
RemarkStringTable.cpp
YAMLRemarkParser.cpp
--- /dev/null
+//===- RemarkFormat.cpp --------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Implementation of utilities to handle the different remark formats.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Remarks/RemarkFormat.h"
+#include "llvm/ADT/StringSwitch.h"
+
+using namespace llvm;
+using namespace llvm::remarks;
+
+Expected<Format> llvm::remarks::parseFormat(StringRef FormatStr) {
+ auto Result = StringSwitch<Format>(FormatStr)
+ .Cases("", "yaml", Format::YAML)
+ .Default(Format::Unknown);
+
+ if (Result == Format::Unknown)
+ return createStringError(std::make_error_code(std::errc::invalid_argument),
+ "Unknown remark serializer format: '%s'",
+ FormatStr.data());
+
+ return Result;
+}
using namespace llvm;
using namespace llvm::remarks;
-static std::unique_ptr<ParserImpl> formatToParserImpl(ParserFormat Format,
+static std::unique_ptr<ParserImpl> formatToParserImpl(Format ParserFormat,
StringRef Buf) {
- switch (Format) {
- case ParserFormat::YAML:
+ switch (ParserFormat) {
+ case Format::YAML:
return llvm::make_unique<YAMLParserImpl>(Buf);
+ case Format::Unknown:
+ llvm_unreachable("Unhandled llvm::remarks::ParserFormat enum");
+ return nullptr;
};
- llvm_unreachable("Unhandled llvm::remarks::ParserFormat enum");
}
static std::unique_ptr<ParserImpl>
-formatToParserImpl(ParserFormat Format, StringRef Buf,
+formatToParserImpl(Format ParserFormat, StringRef Buf,
const ParsedStringTable &StrTab) {
- switch (Format) {
- case ParserFormat::YAML:
+ switch (ParserFormat) {
+ case Format::YAML:
return llvm::make_unique<YAMLParserImpl>(Buf, &StrTab);
+ case Format::Unknown:
+ llvm_unreachable("Unhandled llvm::remarks::ParserFormat enum");
+ return nullptr;
};
- llvm_unreachable("Unhandled llvm::remarks::ParserFormat enum");
}
-Parser::Parser(ParserFormat Format, StringRef Buf)
- : Impl(formatToParserImpl(Format, Buf)) {}
+Parser::Parser(Format ParserFormat, StringRef Buf)
+ : Impl(formatToParserImpl(ParserFormat, Buf)) {}
-Parser::Parser(ParserFormat Format, StringRef Buf,
+Parser::Parser(Format ParserFormat, StringRef Buf,
const ParsedStringTable &StrTab)
- : Impl(formatToParserImpl(Format, Buf, StrTab)) {}
+ : Impl(formatToParserImpl(ParserFormat, Buf, StrTab)) {}
Parser::~Parser() = default;
extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf,
uint64_t Size) {
- return wrap(
- new remarks::Parser(remarks::ParserFormat::YAML,
- StringRef(static_cast<const char *>(Buf), Size)));
+ return wrap(new remarks::Parser(
+ remarks::Format::YAML, StringRef(static_cast<const char *>(Buf), Size)));
}
static void handleYAMLError(remarks::YAMLParserImpl &Impl, Error E) {
namespace remarks {
/// This is used as a base for any parser implementation.
struct ParserImpl {
- explicit ParserImpl(ParserFormat Format) : Format(Format) {}
+ explicit ParserImpl(Format ParserFormat) : ParserFormat(ParserFormat) {}
// Virtual destructor prevents mismatched deletes
virtual ~ParserImpl() {}
// The parser format. This is used as a tag to safely cast between
// implementations.
- ParserFormat Format;
+ Format ParserFormat;
};
} // end namespace remarks
} // end namespace llvm
YAMLParserImpl(StringRef Buf,
Optional<const ParsedStringTable *> StrTab = None)
- : ParserImpl{ParserFormat::YAML}, YAMLParser(Buf, StrTab),
+ : ParserImpl{Format::YAML}, YAMLParser(Buf, StrTab),
YAMLIt(YAMLParser.Stream.begin()), HasErrors(false) {}
static bool classof(const ParserImpl *PI) {
- return PI->Format == ParserFormat::YAML;
+ return PI->ParserFormat == Format::YAML;
}
};
} // end namespace remarks
return false;
}
- remarks::Parser Parser(remarks::ParserFormat::YAML, (*Buf)->getBuffer());
+ remarks::Parser Parser(remarks::Format::YAML, (*Buf)->getBuffer());
while (true) {
Expected<const remarks::Remark *> RemarkOrErr = Parser.getNext();
using namespace llvm;
template <size_t N> void parseGood(const char (&Buf)[N]) {
- remarks::Parser Parser(remarks::ParserFormat::YAML, {Buf, N - 1});
+ remarks::Parser Parser(remarks::Format::YAML, {Buf, N - 1});
Expected<const remarks::Remark *> Remark = Parser.getNext();
EXPECT_FALSE(errorToBool(Remark.takeError())); // Check for parsing errors.
EXPECT_TRUE(*Remark != nullptr); // At least one remark.
template <size_t N>
bool parseExpectError(const char (&Buf)[N], const char *Error) {
- remarks::Parser Parser(remarks::ParserFormat::YAML, {Buf, N - 1});
+ remarks::Parser Parser(remarks::Format::YAML, {Buf, N - 1});
Expected<const remarks::Remark *> Remark = Parser.getNext();
EXPECT_FALSE(Remark); // Expect an error here.
" - String: ' because its definition is unavailable'\n"
"\n";
- remarks::Parser Parser(remarks::ParserFormat::YAML, Buf);
+ remarks::Parser Parser(remarks::Format::YAML, Buf);
Expected<const remarks::Remark *> RemarkOrErr = Parser.getNext();
EXPECT_FALSE(errorToBool(RemarkOrErr.takeError()));
EXPECT_TRUE(*RemarkOrErr != nullptr);
115);
remarks::ParsedStringTable StrTab(StrTabBuf);
- remarks::Parser Parser(remarks::ParserFormat::YAML, Buf, StrTab);
+ remarks::Parser Parser(remarks::Format::YAML, Buf, StrTab);
Expected<const remarks::Remark *> RemarkOrErr = Parser.getNext();
EXPECT_FALSE(errorToBool(RemarkOrErr.takeError()));
EXPECT_TRUE(*RemarkOrErr != nullptr);
StringRef StrTabBuf = StringRef("inline");
remarks::ParsedStringTable StrTab(StrTabBuf);
- remarks::Parser Parser(remarks::ParserFormat::YAML, Buf, StrTab);
+ remarks::Parser Parser(remarks::Format::YAML, Buf, StrTab);
Expected<const remarks::Remark *> Remark = Parser.getNext();
EXPECT_FALSE(Remark); // Expect an error here.