[llvm] r348735 - [TextAPI][elfabi] Make TBE handlers functions that return Errors

Armando Montanez via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 9 18:36:33 PST 2018


Author: amontanez
Date: Sun Dec  9 18:36:33 2018
New Revision: 348735

URL: http://llvm.org/viewvc/llvm-project?rev=348735&view=rev
Log:
[TextAPI][elfabi] Make TBE handlers functions that return Errors

Since TBEHandler doesn't maintain state or otherwise have any need to be
a class right now, the read and write functions have been moved out and
turned into standalone functions. Additionally, the TBE read function
has been updated to return an Expected value for better error handling.
Tests have been updated to reflect these changes.

Differential Revision: https://reviews.llvm.org/D55450

Modified:
    llvm/trunk/include/llvm/TextAPI/ELF/TBEHandler.h
    llvm/trunk/lib/TextAPI/ELF/TBEHandler.cpp
    llvm/trunk/unittests/TextAPI/CMakeLists.txt
    llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp

Modified: llvm/trunk/include/llvm/TextAPI/ELF/TBEHandler.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/TextAPI/ELF/TBEHandler.h?rev=348735&r1=348734&r2=348735&view=diff
==============================================================================
--- llvm/trunk/include/llvm/TextAPI/ELF/TBEHandler.h (original)
+++ llvm/trunk/include/llvm/TextAPI/ELF/TBEHandler.h Sun Dec  9 18:36:33 2018
@@ -17,6 +17,7 @@
 #define LLVM_TEXTAPI_ELF_TBEHANDLER_H
 
 #include "llvm/Support/VersionTuple.h"
+#include "llvm/Support/Error.h"
 #include <memory>
 
 namespace llvm {
@@ -32,14 +33,12 @@ class ELFStub;
 
 const VersionTuple TBEVersionCurrent(1, 0);
 
-class TBEHandler {
-public:
-  /// Attempts to read an ELF interface file from a StringRef buffer.
-  std::unique_ptr<ELFStub> readFile(StringRef Buf);
-
-  /// Attempts to write an ELF interface file to a raw_ostream.
-  Error writeFile(raw_ostream &OS, const ELFStub &Stub);
-};
+/// Attempts to read an ELF interface file from a StringRef buffer.
+Expected<std::unique_ptr<ELFStub>> readTBEFromBuffer(StringRef Buf);
+
+/// Attempts to write an ELF interface file to a raw_ostream.
+Error writeTBEToOutputStream(raw_ostream &OS, const ELFStub &Stub);
+
 } // end namespace elfabi
 } // end namespace llvm
 

Modified: llvm/trunk/lib/TextAPI/ELF/TBEHandler.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TextAPI/ELF/TBEHandler.cpp?rev=348735&r1=348734&r2=348735&view=diff
==============================================================================
--- llvm/trunk/lib/TextAPI/ELF/TBEHandler.cpp (original)
+++ llvm/trunk/lib/TextAPI/ELF/TBEHandler.cpp Sun Dec  9 18:36:33 2018
@@ -142,16 +142,17 @@ template <> struct MappingTraits<ELFStub
 } // end namespace yaml
 } // end namespace llvm
 
-std::unique_ptr<ELFStub> TBEHandler::readFile(StringRef Buf) {
+Expected<std::unique_ptr<ELFStub>> elfabi::readTBEFromBuffer(StringRef Buf) {
   yaml::Input YamlIn(Buf);
   std::unique_ptr<ELFStub> Stub(new ELFStub());
   YamlIn >> *Stub;
-  if (YamlIn.error())
-    return nullptr;
+  if (std::error_code Err = YamlIn.error())
+    return createStringError(Err, "YAML failed reading as TBE");
+
   return Stub;
 }
 
-Error TBEHandler::writeFile(raw_ostream &OS, const ELFStub &Stub) {
+Error elfabi::writeTBEToOutputStream(raw_ostream &OS, const ELFStub &Stub) {
   yaml::Output YamlOut(OS, NULL, /*WrapColumn =*/0);
 
   YamlOut << const_cast<ELFStub &>(Stub);

Modified: llvm/trunk/unittests/TextAPI/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/TextAPI/CMakeLists.txt?rev=348735&r1=348734&r2=348735&view=diff
==============================================================================
--- llvm/trunk/unittests/TextAPI/CMakeLists.txt (original)
+++ llvm/trunk/unittests/TextAPI/CMakeLists.txt Sun Dec  9 18:36:33 2018
@@ -1,4 +1,5 @@
 set(LLVM_LINK_COMPONENTS
+  TestingSupport
   TextAPI
 )
 

Modified: llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp?rev=348735&r1=348734&r2=348735&view=diff
==============================================================================
--- llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp (original)
+++ llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp Sun Dec  9 18:36:33 2018
@@ -11,6 +11,7 @@
 #include "llvm/TextAPI/ELF/ELFStub.h"
 #include "llvm/TextAPI/ELF/TBEHandler.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Testing/Support/Error.h"
 #include "gtest/gtest.h"
 #include <string>
 
@@ -18,16 +19,6 @@ using namespace llvm;
 using namespace llvm::ELF;
 using namespace llvm::elfabi;
 
-std::unique_ptr<ELFStub> readFromBuffer(const char Data[]) {
-  TBEHandler Handler;
-
-  StringRef Buf(Data);
-
-  std::unique_ptr<ELFStub> Stub = Handler.readFile(Buf);
-  EXPECT_NE(Stub.get(), nullptr);
-  return Stub;
-}
-
 void compareByLine(StringRef LHS, StringRef RHS) {
   StringRef Line1;
   StringRef Line2;
@@ -52,9 +43,9 @@ TEST(ElfYamlTextAPI, YAMLReadableTBE) {
                       "Symbols:\n"
                       "  foo: { Type: Func, Undefined: true }\n"
                       "...\n";
-  StringRef Buf = StringRef(Data);
-  TBEHandler Handler;
-  std::unique_ptr<ELFStub> Stub = Handler.readFile(Buf);
+  Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
+  ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
+  std::unique_ptr<ELFStub> Stub = std::move(StubOrErr.get());
   EXPECT_NE(Stub.get(), nullptr);
   EXPECT_EQ(Stub->Arch, (uint16_t)llvm::ELF::EM_X86_64);
   EXPECT_STREQ(Stub->SoName.c_str(), "test.so");
@@ -77,7 +68,10 @@ TEST(ElfYamlTextAPI, YAMLReadsTBESymbols
                       "  not: { Type: File, Undefined: true, Size: 111, "
                       "Warning: \'All fields populated!\' }\n"
                       "...\n";
-  std::unique_ptr<ELFStub> Stub = readFromBuffer(Data);
+  Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
+  ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
+  std::unique_ptr<ELFStub> Stub = std::move(StubOrErr.get());
+  EXPECT_NE(Stub.get(), nullptr);
   EXPECT_EQ(Stub->Symbols.size(), 5u);
 
   auto Iterator = Stub->Symbols.begin();
@@ -126,12 +120,14 @@ TEST(ElfYamlTextAPI, YAMLReadsNoTBESyms)
                       "Arch: x86_64\n"
                       "Symbols: {}\n"
                       "...\n";
-  std::unique_ptr<ELFStub> Stub = readFromBuffer(Data);
+  Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
+  ASSERT_THAT_ERROR(StubOrErr.takeError(), Succeeded());
+  std::unique_ptr<ELFStub> Stub = std::move(StubOrErr.get());
+  EXPECT_NE(Stub.get(), nullptr);
   EXPECT_EQ(0u, Stub->Symbols.size());
 }
 
 TEST(ElfYamlTextAPI, YAMLUnreadableTBE) {
-  TBEHandler Handler;
   // Can't read: wrong format/version.
   const char Data[] = "--- !tapi-tbz\n"
                       "TbeVersion: z.3\n"
@@ -139,9 +135,8 @@ TEST(ElfYamlTextAPI, YAMLUnreadableTBE)
                       "Arch: x86_64\n"
                       "Symbols:\n"
                       "  foo: { Type: Func, Undefined: true }\n";
-  StringRef Buf = StringRef(Data);
-  std::unique_ptr<ELFStub> Stub = Handler.readFile(Buf);
-  EXPECT_EQ(Stub.get(), nullptr);
+  Expected<std::unique_ptr<ELFStub>> StubOrErr = readTBEFromBuffer(Data);
+  ASSERT_THAT_ERROR(StubOrErr.takeError(), Failed());
 }
 
 TEST(ElfYamlTextAPI, YAMLWritesTBESymbols) {
@@ -185,8 +180,7 @@ TEST(ElfYamlTextAPI, YAMLWritesTBESymbol
 
   std::string Result;
   raw_string_ostream OS(Result);
-  TBEHandler Handler;
-  EXPECT_FALSE(Handler.writeFile(OS, Moved));
+  ASSERT_THAT_ERROR(writeTBEToOutputStream(OS, Moved), Succeeded());
   Result = OS.str();
   compareByLine(Result.c_str(), Expected);
 }
@@ -212,8 +206,7 @@ TEST(ElfYamlTextAPI, YAMLWritesNoTBESyms
 
   std::string Result;
   raw_string_ostream OS(Result);
-  TBEHandler Handler;
-  EXPECT_FALSE(Handler.writeFile(OS, Stub));
+  ASSERT_THAT_ERROR(writeTBEToOutputStream(OS, Stub), Succeeded());
   Result = OS.str();
   compareByLine(Result.c_str(), Expected);
 }




More information about the llvm-commits mailing list