[lld] r182970 - [lld][WinLink] Fix use-after-return and add unit tests.
Rui Ueyama
ruiu at google.com
Thu May 30 16:17:58 PDT 2013
Author: ruiu
Date: Thu May 30 18:17:58 2013
New Revision: 182970
URL: http://llvm.org/viewvc/llvm-project?rev=182970&view=rev
Log:
[lld][WinLink] Fix use-after-return and add unit tests.
Added:
lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp
Modified:
lld/trunk/lib/Driver/WinLinkDriver.cpp
lld/trunk/unittests/DriverTests/CMakeLists.txt
Modified: lld/trunk/lib/Driver/WinLinkDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkDriver.cpp?rev=182970&r1=182969&r2=182970&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkDriver.cpp (original)
+++ lld/trunk/lib/Driver/WinLinkDriver.cpp Thu May 30 18:17:58 2013
@@ -74,18 +74,19 @@ llvm::COFF::WindowsSubsystem strToWinSub
}
// Add ".obj" extension if the given path name has no file extension.
-StringRef canonicalizeInputFileName(StringRef path) {
+std::string canonicalizeInputFileName(std::string path) {
if (llvm::sys::path::extension(path).empty())
- return path.str() + ".obj";
+ return path.append(".obj");
return path;
}
// Replace a file extension with ".exe". If the given file has no
// extension, just add ".exe".
-StringRef getDefaultOutputFileName(StringRef path) {
+std::string getDefaultOutputFileName(std::string path) {
StringRef ext = llvm::sys::path::extension(path);
- StringRef filename = ext.empty() ? path : path.drop_back(ext.size());
- return filename.str() + ".exe";
+ if (!ext.empty())
+ path.erase(path.size() - ext.size());
+ return path.append(".exe");
}
} // namespace
Modified: lld/trunk/unittests/DriverTests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/CMakeLists.txt?rev=182970&r1=182969&r2=182970&view=diff
==============================================================================
--- lld/trunk/unittests/DriverTests/CMakeLists.txt (original)
+++ lld/trunk/unittests/DriverTests/CMakeLists.txt Thu May 30 18:17:58 2013
@@ -1,5 +1,6 @@
add_lld_unittest(DriverTests
UniversalDriverTest.cpp
+ WinLinkDriverTest.cpp
)
target_link_libraries(DriverTests
Added: lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp?rev=182970&view=auto
==============================================================================
--- lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp (added)
+++ lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp Thu May 30 18:17:58 2013
@@ -0,0 +1,90 @@
+//===- lld/unittest/WinLinkDriverTest.cpp ---------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief Windows link.exe driver tests.
+///
+//===----------------------------------------------------------------------===//
+
+#include <stdarg.h>
+
+#include "gtest/gtest.h"
+
+#include "lld/Driver/Driver.h"
+#include "lld/Driver/LinkerInput.h"
+#include "lld/ReaderWriter/PECOFFTargetInfo.h"
+
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/COFF.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+using namespace lld;
+
+namespace {
+
+class ParserTest : public testing::Test {
+protected:
+ void SetUp() {
+ os.reset(new raw_string_ostream(diags));
+ }
+
+ void parse(const char *args, ...) {
+ std::vector<const char *> vec;
+ vec.push_back("link.exe");
+ vec.push_back(args);
+ va_list ap;
+ va_start(ap, args);
+ while (const char *arg = va_arg(ap, const char *))
+ vec.push_back(arg);
+ va_end(ap);
+ EXPECT_FALSE(WinLinkDriver::parse(vec.size(), &vec[0], info, *os));
+ }
+
+ PECOFFTargetInfo info;
+ std::string diags;
+ std::unique_ptr<raw_string_ostream> os;
+};
+
+TEST_F(ParserTest, Basic) {
+ parse("-subsystem", "console", "-out", "a.exe", "a.obj", "b.obj", "c.obj",
+ nullptr);
+
+ EXPECT_EQ(llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI, info.getSubsystem());
+ EXPECT_EQ("a.exe", info.outputPath());
+
+ const std::vector<LinkerInput> &inputFiles = info.inputFiles();
+ EXPECT_EQ((size_t)3, inputFiles.size());
+ EXPECT_EQ("a.obj", inputFiles[0].getPath());
+ EXPECT_EQ("b.obj", inputFiles[1].getPath());
+ EXPECT_EQ("c.obj", inputFiles[2].getPath());
+}
+
+TEST_F(ParserTest, NoFileEXtension) {
+ parse("foo", "bar", nullptr);
+
+ EXPECT_EQ("foo.exe", info.outputPath());
+
+ const std::vector<LinkerInput> &inputFiles = info.inputFiles();
+ EXPECT_EQ((size_t)2, inputFiles.size());
+ EXPECT_EQ("foo.obj", inputFiles[0].getPath());
+ EXPECT_EQ("bar.obj", inputFiles[1].getPath());
+}
+
+TEST_F(ParserTest, NonStandardFileExtension) {
+ parse("foo.o", nullptr);
+
+ EXPECT_EQ("foo.exe", info.outputPath());
+
+ const std::vector<LinkerInput> &inputFiles = info.inputFiles();
+ EXPECT_EQ((size_t)1, inputFiles.size());
+ EXPECT_EQ("foo.o", inputFiles[0].getPath());
+}
+
+} // end anonymous namespace
More information about the llvm-commits
mailing list