[Remarks][NFC] Combine ParserFormat and SerializerFormat
authorFrancis Visoiu Mistrih <francisvm@yahoo.com>
Tue, 16 Jul 2019 15:24:59 +0000 (15:24 +0000)
committerFrancis Visoiu Mistrih <francisvm@yahoo.com>
Tue, 16 Jul 2019 15:24:59 +0000 (15:24 +0000)
It's useless to have both.

llvm-svn: 366216

13 files changed:
llvm/include/llvm/IR/RemarkStreamer.h
llvm/include/llvm/Remarks/Remark.h
llvm/include/llvm/Remarks/RemarkFormat.h [new file with mode: 0644]
llvm/include/llvm/Remarks/RemarkParser.h
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
llvm/lib/IR/RemarkStreamer.cpp
llvm/lib/Remarks/CMakeLists.txt
llvm/lib/Remarks/RemarkFormat.cpp [new file with mode: 0644]
llvm/lib/Remarks/RemarkParser.cpp
llvm/lib/Remarks/RemarkParserImpl.h
llvm/lib/Remarks/YAMLRemarkParser.h
llvm/tools/llvm-opt-report/OptReport.cpp
llvm/unittests/Remarks/YAMLRemarksParsingTest.cpp

index 9b6d82e..c84de9a 100644 (file)
@@ -90,10 +90,6 @@ struct RemarkSetupFormatError : RemarkSetupErrorInfo<RemarkSetupFormatError> {
   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,
index d916728..4241fb1 100644 (file)
@@ -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 (file)
index 0000000..e167d99
--- /dev/null
@@ -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<Format> parseFormat(StringRef FormatStr);
+
+} // end namespace remarks
+} // end namespace llvm
+
+#endif /* LLVM_REMARKS_REMARK_FORMAT_H */
index 457b2fb..b956f0c 100644 (file)
@@ -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 <memory>
 
@@ -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.
index 174a9bc..54f6cc2 100644 (file)
 #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"
index 2c3bc84..32adef1 100644 (file)
@@ -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<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,
@@ -158,8 +145,7 @@ llvm::setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
   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));
 
index 7338359..06ddbab 100644 (file)
@@ -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 (file)
index 0000000..bcd0f75
--- /dev/null
@@ -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<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;
+}
index bd83ba4..41ed64d 100644 (file)
 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;
 
@@ -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<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) {
index 6b9329b..5f8c21d 100644 (file)
@@ -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
index 9ed18ee..14698bb 100644 (file)
@@ -127,11 +127,11 @@ struct YAMLParserImpl : public ParserImpl {
 
   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
index b263d9a..80d0b73 100644 (file)
@@ -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<const remarks::Remark *> RemarkOrErr = Parser.getNext();
index 6cca4c5..e3c7cdf 100644 (file)
@@ -14,7 +14,7 @@
 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.
@@ -25,7 +25,7 @@ template <size_t N> void parseGood(const char (&Buf)[N]) {
 
 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.
 
@@ -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<const remarks::Remark *> 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<const remarks::Remark *> 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<const remarks::Remark *> Remark = Parser.getNext();
   EXPECT_FALSE(Remark); // Expect an error here.