[lld] r227902 - ELF: Improve linker script unit tests.

Rui Ueyama ruiu at google.com
Mon Feb 2 16:42:36 PST 2015


Author: ruiu
Date: Mon Feb  2 18:42:36 2015
New Revision: 227902

URL: http://llvm.org/viewvc/llvm-project?rev=227902&view=rev
Log:
ELF: Improve linker script unit tests.

This patch is to enable to write unit tests for linker script with
less boilerplate code.

Modified:
    lld/trunk/include/lld/Driver/Driver.h
    lld/trunk/lib/Driver/GnuLdDriver.cpp
    lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp

Modified: lld/trunk/include/lld/Driver/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Driver/Driver.h?rev=227902&r1=227901&r2=227902&view=diff
==============================================================================
--- lld/trunk/include/lld/Driver/Driver.h (original)
+++ lld/trunk/include/lld/Driver/Driver.h Mon Feb  2 18:42:36 2015
@@ -82,6 +82,10 @@ public:
                                           std::unique_ptr<MemoryBuffer> mb,
                                           raw_ostream &diag);
 
+  /// A factory method to create an instance of ELFLinkingContext.
+  static std::unique_ptr<ELFLinkingContext>
+  createELFLinkingContext(llvm::Triple triple);
+
 private:
   static llvm::Triple getDefaultTarget(const char *progName);
   static bool applyEmulation(llvm::Triple &triple,
@@ -137,7 +141,6 @@ private:
 /// Driver for lld unit tests
 class CoreDriver : public Driver {
 public:
-
   /// Parses command line arguments same as lld-core and performs link.
   /// Returns true iff there was an error.
   static bool link(int argc, const char *argv[],

Modified: lld/trunk/lib/Driver/GnuLdDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/GnuLdDriver.cpp?rev=227902&r1=227901&r2=227902&view=diff
==============================================================================
--- lld/trunk/lib/Driver/GnuLdDriver.cpp (original)
+++ lld/trunk/lib/Driver/GnuLdDriver.cpp Mon Feb  2 18:42:36 2015
@@ -311,22 +311,21 @@ bool GnuLdDriver::applyEmulation(llvm::T
   return true;
 }
 
-#define LLVM_TARGET(targetName) \
-  if ((p = elf::targetName##LinkingContext::create(triple))) return p;
-
 std::unique_ptr<ELFLinkingContext>
-createELFLinkingContext(llvm::Triple triple) {
+GnuLdDriver::createELFLinkingContext(llvm::Triple triple) {
   std::unique_ptr<ELFLinkingContext> p;
   // FIXME: #include "llvm/Config/Targets.def"
+#define LLVM_TARGET(targetName) \
+  if ((p = elf::targetName##LinkingContext::create(triple))) return p;
   LLVM_TARGET(AArch64)
   LLVM_TARGET(ARM)
   LLVM_TARGET(Hexagon)
   LLVM_TARGET(Mips)
   LLVM_TARGET(X86)
   LLVM_TARGET(X86_64)
+#undef LLVM_TARGET
   return nullptr;
 }
-#undef LLVM_TARGET
 
 bool GnuLdDriver::parse(int argc, const char *argv[],
                         std::unique_ptr<ELFLinkingContext> &context,

Modified: lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp?rev=227902&r1=227901&r2=227902&view=diff
==============================================================================
--- lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp (original)
+++ lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp Mon Feb  2 18:42:36 2015
@@ -20,12 +20,31 @@ using namespace llvm;
 using namespace lld;
 
 namespace {
+
 class GnuLdParserTest
     : public ParserTest<GnuLdDriver, std::unique_ptr<ELFLinkingContext>> {
 protected:
   const LinkingContext *linkingContext() override { return _context.get(); }
 };
-}
+
+class LinkerScriptTest : public testing::Test {
+protected:
+  void parse(StringRef script) {
+    llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
+    _ctx = std::move(GnuLdDriver::createELFLinkingContext(triple));
+    std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
+      script, "foo.so");
+    std::string s;
+    raw_string_ostream out(s);
+    std::error_code ec = GnuLdDriver::evalLinkerScript(
+      *_ctx, std::move(mb), out);
+    EXPECT_FALSE(ec);
+  };
+
+  std::unique_ptr<ELFLinkingContext> _ctx;
+};
+
+} // anonymous namespace
 
 TEST_F(GnuLdParserTest, Empty) {
   EXPECT_FALSE(parse("ld", nullptr));
@@ -162,58 +181,28 @@ TEST_F(GnuLdParserTest, AsNeeded) {
 
 // Linker script
 
-TEST_F(GnuLdParserTest, LinkerScriptGroup) {
-  parse("ld", "a.o", nullptr);
-  std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
-    "GROUP(/x /y)", "foo.so");
-  std::string s;
-  raw_string_ostream out(s);
-  std::error_code ec = GnuLdDriver::evalLinkerScript(
-    *_context, std::move(mb), out);
-  EXPECT_FALSE(ec);
-  std::vector<std::unique_ptr<Node>> &nodes = _context->getNodes();
-  EXPECT_EQ((size_t)4, nodes.size());
-  EXPECT_EQ("/x", cast<FileNode>(nodes[1].get())->getFile()->path());
-  EXPECT_EQ("/y", cast<FileNode>(nodes[2].get())->getFile()->path());
-  EXPECT_EQ(2, cast<GroupEnd>(nodes[3].get())->getSize());
-}
-
-TEST_F(GnuLdParserTest, LinkerScriptSearchDir) {
-  parse("ld", "a.o", nullptr);
-  std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
-    "SEARCH_DIR(\"/foo/bar\")", "foo.so");
-  std::string s;
-  raw_string_ostream out(s);
-  std::error_code ec = GnuLdDriver::evalLinkerScript(
-    *_context, std::move(mb), out);
-  EXPECT_FALSE(ec);
-  std::vector<StringRef> searchPaths = _context->getSearchPaths();
-  EXPECT_EQ((size_t)2, searchPaths.size());
-  EXPECT_EQ("/foo/bar", searchPaths[1]);
-}
-
-TEST_F(GnuLdParserTest, LinkerScriptEntry) {
-  parse("ld", "a.o", nullptr);
-  std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
-    "ENTRY(blah)", "foo.so");
-  std::string s;
-  raw_string_ostream out(s);
-  std::error_code ec = GnuLdDriver::evalLinkerScript(
-    *_context, std::move(mb), out);
-  EXPECT_FALSE(ec);
-  StringRef entrySymbol = _context->entrySymbolName();
-  EXPECT_EQ("blah", entrySymbol);
-}
-
-TEST_F(GnuLdParserTest, LinkerScriptOutput) {
-  parse("ld", "a.o", nullptr);
-  std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
-    "OUTPUT(\"/path/to/output\")", "foo.so");
-  std::string s;
-  raw_string_ostream out(s);
-  std::error_code ec = GnuLdDriver::evalLinkerScript(
-    *_context, std::move(mb), out);
-  EXPECT_FALSE(ec);
-  StringRef output = _context->outputPath();
-  EXPECT_EQ("/path/to/output", output);
+TEST_F(LinkerScriptTest, Group) {
+  parse("GROUP(/x /y)");
+  std::vector<std::unique_ptr<Node>> &nodes = _ctx->getNodes();
+  EXPECT_EQ((size_t)3, nodes.size());
+  EXPECT_EQ("/x", cast<FileNode>(nodes[0].get())->getFile()->path());
+  EXPECT_EQ("/y", cast<FileNode>(nodes[1].get())->getFile()->path());
+  EXPECT_EQ(2, cast<GroupEnd>(nodes[2].get())->getSize());
+}
+
+TEST_F(LinkerScriptTest, SearchDir) {
+  parse("SEARCH_DIR(\"/foo/bar\")");
+  std::vector<StringRef> paths = _ctx->getSearchPaths();
+  EXPECT_EQ((size_t)1, paths.size());
+  EXPECT_EQ("/foo/bar", paths[0]);
+}
+
+TEST_F(LinkerScriptTest, Entry) {
+  parse("ENTRY(blah)");
+  EXPECT_EQ("blah", _ctx->entrySymbolName());
+}
+
+TEST_F(LinkerScriptTest, Output) {
+  parse("OUTPUT(\"/path/to/output\")");
+  EXPECT_EQ("/path/to/output", _ctx->outputPath());
 }





More information about the llvm-commits mailing list