From 16b7e4e60292acaa8aa301948219c5be5faaf890 Mon Sep 17 00:00:00 2001 From: Sergej Jaskiewicz Date: Wed, 9 Sep 2020 01:53:01 +0300 Subject: [PATCH] [llvm] [unittest] Allow getting a C string from the TempDir helper class The TempDir.path() member function returns a StringRef. We've been calling the data() method on that StringRef, which does not guarantee to return a null-terminated string (required by chdir and other POSIX functions). Introduce the c_str() method in the TempDir class, which returns the proper string without the need to create a copy of the path at use site. --- include/llvm/Testing/Support/SupportHelpers.h | 3 +++ unittests/Support/LockFileManagerTest.cpp | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/llvm/Testing/Support/SupportHelpers.h b/include/llvm/Testing/Support/SupportHelpers.h index 3517361041b..2419fc95d81 100644 --- a/include/llvm/Testing/Support/SupportHelpers.h +++ b/include/llvm/Testing/Support/SupportHelpers.h @@ -152,6 +152,9 @@ public: /// The path to the temporary directory. StringRef path() const { return Path; } + /// The null-terminated C string pointing to the path. + const char *c_str() { return Path.c_str(); } + /// Creates a new path by appending the argument to the path of the managed /// directory using the native path separator. SmallString<128> path(StringRef component) const { diff --git a/unittests/Support/LockFileManagerTest.cpp b/unittests/Support/LockFileManagerTest.cpp index 587e442be19..0b5a0d982a8 100644 --- a/unittests/Support/LockFileManagerTest.cpp +++ b/unittests/Support/LockFileManagerTest.cpp @@ -81,7 +81,7 @@ TEST(LockFileManagerTest, RelativePath) { char PathBuf[1024]; const char *OrigPath = getcwd(PathBuf, 1024); - ASSERT_FALSE(chdir(LockFileManagerTestDir.path().data())); + ASSERT_FALSE(chdir(LockFileManagerTestDir.c_str())); TempDir inner("inner"); SmallString<64> LockedFile(inner.path());