From cc909812a39d26ba4bcc8aaa49096155802c4521 Mon Sep 17 00:00:00 2001 From: Francis Visoiu Mistrih Date: Tue, 16 Jul 2019 15:24:59 +0000 Subject: [PATCH] [Remarks][NFC] Combine ParserFormat and SerializerFormat It's useless to have both. llvm-svn: 366216 --- llvm/include/llvm/IR/RemarkStreamer.h | 4 --- llvm/include/llvm/Remarks/Remark.h | 1 - llvm/include/llvm/Remarks/RemarkFormat.h | 33 +++++++++++++++++++++++ llvm/include/llvm/Remarks/RemarkParser.h | 7 +++-- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 1 + llvm/lib/IR/RemarkStreamer.cpp | 22 +++------------ llvm/lib/Remarks/CMakeLists.txt | 1 + llvm/lib/Remarks/RemarkFormat.cpp | 30 +++++++++++++++++++++ llvm/lib/Remarks/RemarkParser.cpp | 33 ++++++++++++----------- llvm/lib/Remarks/RemarkParserImpl.h | 4 +-- llvm/lib/Remarks/YAMLRemarkParser.h | 4 +-- llvm/tools/llvm-opt-report/OptReport.cpp | 2 +- llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp | 10 +++---- 13 files changed, 100 insertions(+), 52 deletions(-) create mode 100644 llvm/include/llvm/Remarks/RemarkFormat.h create mode 100644 llvm/lib/Remarks/RemarkFormat.cpp diff --git a/llvm/include/llvm/IR/RemarkStreamer.h b/llvm/include/llvm/IR/RemarkStreamer.h index 9b6d82e..c84de9a 100644 --- a/llvm/include/llvm/IR/RemarkStreamer.h +++ b/llvm/include/llvm/IR/RemarkStreamer.h @@ -90,10 +90,6 @@ struct RemarkSetupFormatError : RemarkSetupErrorInfo { using RemarkSetupErrorInfo::RemarkSetupErrorInfo; }; -enum class RemarksSerializerFormat { Unknown, YAML }; - -Expected parseSerializerFormat(StringRef Format); - /// Setup optimization remarks. Expected> setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, diff --git a/llvm/include/llvm/Remarks/Remark.h b/llvm/include/llvm/Remarks/Remark.h index d916728..4241fb1 100644 --- a/llvm/include/llvm/Remarks/Remark.h +++ b/llvm/include/llvm/Remarks/Remark.h @@ -24,7 +24,6 @@ namespace llvm { 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 { diff --git a/llvm/include/llvm/Remarks/RemarkFormat.h b/llvm/include/llvm/Remarks/RemarkFormat.h new file mode 100644 index 0000000..e167d99 --- /dev/null +++ b/llvm/include/llvm/Remarks/RemarkFormat.h @@ -0,0 +1,33 @@ +//===-- 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 parseFormat(StringRef FormatStr); + +} // end namespace remarks +} // end namespace llvm + +#endif /* LLVM_REMARKS_REMARK_FORMAT_H */ diff --git a/llvm/include/llvm/Remarks/RemarkParser.h b/llvm/include/llvm/Remarks/RemarkParser.h index 457b2fb..b956f0c 100644 --- a/llvm/include/llvm/Remarks/RemarkParser.h +++ b/llvm/include/llvm/Remarks/RemarkParser.h @@ -16,6 +16,7 @@ #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 @@ -25,8 +26,6 @@ namespace remarks { 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. @@ -35,11 +34,11 @@ struct 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. diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index 174a9bc..54f6cc2 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -100,6 +100,7 @@ #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" diff --git a/llvm/lib/IR/RemarkStreamer.cpp b/llvm/lib/IR/RemarkStreamer.cpp index 2c3bc84..32adef1 100644 --- a/llvm/lib/IR/RemarkStreamer.cpp +++ b/llvm/lib/IR/RemarkStreamer.cpp @@ -15,6 +15,7 @@ #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/Function.h" #include "llvm/IR/GlobalValue.h" +#include "llvm/Remarks/RemarkFormat.h" using namespace llvm; @@ -112,30 +113,16 @@ char RemarkSetupPatternError::ID = 0; char RemarkSetupFormatError::ID = 0; static std::unique_ptr -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(OS); }; } -Expected -llvm::parseSerializerFormat(StringRef StrFormat) { - auto Format = StringSwitch(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> llvm::setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, @@ -158,8 +145,7 @@ llvm::setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, if (EC) return make_error(errorCodeToError(EC)); - Expected Format = - parseSerializerFormat(RemarksFormat); + Expected Format = remarks::parseFormat(RemarksFormat); if (Error E = Format.takeError()) return make_error(std::move(E)); diff --git a/llvm/lib/Remarks/CMakeLists.txt b/llvm/lib/Remarks/CMakeLists.txt index 7338359..06ddbab 100644 --- a/llvm/lib/Remarks/CMakeLists.txt +++ b/llvm/lib/Remarks/CMakeLists.txt @@ -1,5 +1,6 @@ add_llvm_library(LLVMRemarks Remark.cpp + RemarkFormat.cpp RemarkParser.cpp RemarkStringTable.cpp YAMLRemarkParser.cpp diff --git a/llvm/lib/Remarks/RemarkFormat.cpp b/llvm/lib/Remarks/RemarkFormat.cpp new file mode 100644 index 0000000..bcd0f75 --- /dev/null +++ b/llvm/lib/Remarks/RemarkFormat.cpp @@ -0,0 +1,30 @@ +//===- 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 llvm::remarks::parseFormat(StringRef FormatStr) { + auto Result = StringSwitch(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; +} diff --git a/llvm/lib/Remarks/RemarkParser.cpp b/llvm/lib/Remarks/RemarkParser.cpp index bd83ba4..41ed64d 100644 --- a/llvm/lib/Remarks/RemarkParser.cpp +++ b/llvm/lib/Remarks/RemarkParser.cpp @@ -20,31 +20,35 @@ using namespace llvm; using namespace llvm::remarks; -static std::unique_ptr formatToParserImpl(ParserFormat Format, +static std::unique_ptr formatToParserImpl(Format ParserFormat, StringRef Buf) { - switch (Format) { - case ParserFormat::YAML: + switch (ParserFormat) { + case Format::YAML: return llvm::make_unique(Buf); + case Format::Unknown: + llvm_unreachable("Unhandled llvm::remarks::ParserFormat enum"); + return nullptr; }; - llvm_unreachable("Unhandled llvm::remarks::ParserFormat enum"); } static std::unique_ptr -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(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; @@ -110,9 +114,8 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(remarks::Parser, LLVMRemarkParserRef) extern "C" LLVMRemarkParserRef LLVMRemarkParserCreateYAML(const void *Buf, uint64_t Size) { - return wrap( - new remarks::Parser(remarks::ParserFormat::YAML, - StringRef(static_cast(Buf), Size))); + return wrap(new remarks::Parser( + remarks::Format::YAML, StringRef(static_cast(Buf), Size))); } static void handleYAMLError(remarks::YAMLParserImpl &Impl, Error E) { diff --git a/llvm/lib/Remarks/RemarkParserImpl.h b/llvm/lib/Remarks/RemarkParserImpl.h index 6b9329b..5f8c21d 100644 --- a/llvm/lib/Remarks/RemarkParserImpl.h +++ b/llvm/lib/Remarks/RemarkParserImpl.h @@ -19,13 +19,13 @@ namespace llvm { 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 diff --git a/llvm/lib/Remarks/YAMLRemarkParser.h b/llvm/lib/Remarks/YAMLRemarkParser.h index 9ed18ee..14698bb 100644 --- a/llvm/lib/Remarks/YAMLRemarkParser.h +++ b/llvm/lib/Remarks/YAMLRemarkParser.h @@ -127,11 +127,11 @@ struct YAMLParserImpl : public ParserImpl { YAMLParserImpl(StringRef Buf, Optional 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 diff --git a/llvm/tools/llvm-opt-report/OptReport.cpp b/llvm/tools/llvm-opt-report/OptReport.cpp index b263d9a..80d0b73 100644 --- a/llvm/tools/llvm-opt-report/OptReport.cpp +++ b/llvm/tools/llvm-opt-report/OptReport.cpp @@ -150,7 +150,7 @@ static bool readLocationInfo(LocationInfoTy &LocationInfo) { return false; } - remarks::Parser Parser(remarks::ParserFormat::YAML, (*Buf)->getBuffer()); + remarks::Parser Parser(remarks::Format::YAML, (*Buf)->getBuffer()); while (true) { Expected RemarkOrErr = Parser.getNext(); diff --git a/llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp b/llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp index 6cca4c5..e3c7cdf 100644 --- a/llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp +++ b/llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp @@ -14,7 +14,7 @@ using namespace llvm; template 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 Remark = Parser.getNext(); EXPECT_FALSE(errorToBool(Remark.takeError())); // Check for parsing errors. EXPECT_TRUE(*Remark != nullptr); // At least one remark. @@ -25,7 +25,7 @@ template void parseGood(const char (&Buf)[N]) { template 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 Remark = Parser.getNext(); EXPECT_FALSE(Remark); // Expect an error here. @@ -354,7 +354,7 @@ TEST(YAMLRemarks, Contents) { " - String: ' because its definition is unavailable'\n" "\n"; - remarks::Parser Parser(remarks::ParserFormat::YAML, Buf); + remarks::Parser Parser(remarks::Format::YAML, Buf); Expected RemarkOrErr = Parser.getNext(); EXPECT_FALSE(errorToBool(RemarkOrErr.takeError())); EXPECT_TRUE(*RemarkOrErr != nullptr); @@ -516,7 +516,7 @@ TEST(YAMLRemarks, ContentsStrTab) { 115); remarks::ParsedStringTable StrTab(StrTabBuf); - remarks::Parser Parser(remarks::ParserFormat::YAML, Buf, StrTab); + remarks::Parser Parser(remarks::Format::YAML, Buf, StrTab); Expected RemarkOrErr = Parser.getNext(); EXPECT_FALSE(errorToBool(RemarkOrErr.takeError())); EXPECT_TRUE(*RemarkOrErr != nullptr); @@ -584,7 +584,7 @@ TEST(YAMLRemarks, ParsingBadStringTableIndex) { 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 Remark = Parser.getNext(); EXPECT_FALSE(Remark); // Expect an error here. -- 2.7.4