Skip to content

Commit f10871b

Browse files
committed
Add tests for program_name path function.
Simple clang test for now, tests in lld and other tools coming soon.
1 parent 5f5e57a commit f10871b

File tree

7 files changed

+52
-12
lines changed

7 files changed

+52
-12
lines changed

clang/lib/Driver/ToolChain.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ static const DriverSuffix *FindDriverSuffix(StringRef ProgName, size_t &Pos) {
326326
/// Normalize the program name from argv[0] by stripping the file extension if
327327
/// present and lower-casing the string on Windows.
328328
static std::string normalizeProgramName(llvm::StringRef Argv0) {
329-
std::string ProgName = std::string(llvm::sys::path::filename(Argv0));
329+
std::string ProgName = std::string(sys::path::program_name(Argv0));
330330
if (is_style_windows(llvm::sys::path::Style::native)) {
331331
// Transform to lowercase for case insensitive file systems.
332332
std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(),
@@ -345,13 +345,6 @@ static const DriverSuffix *parseDriverSuffix(StringRef ProgName, size_t &Pos) {
345345
// added via -target as implicit first argument.
346346
const DriverSuffix *DS = FindDriverSuffix(ProgName, Pos);
347347

348-
if (!DS && ProgName.endswith(".exe")) {
349-
// Try again after stripping the executable suffix:
350-
// clang++.exe -> clang++
351-
ProgName = ProgName.drop_back(StringRef(".exe").size());
352-
DS = FindDriverSuffix(ProgName, Pos);
353-
}
354-
355348
if (!DS) {
356349
// Try again after stripping any trailing version number:
357350
// clang++3.5 -> clang++

clang/unittests/Driver/ToolChainTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,11 @@ TEST(ToolChainTest, GetTargetAndMode) {
430430
EXPECT_STREQ(Res.DriverMode, "--driver-mode=g++");
431431
EXPECT_TRUE(Res.TargetIsValid);
432432

433+
Res = ToolChain::getTargetAndModeFromProgramName(
434+
"x86_64-unknown-freebsd13.2-clang-c++");
435+
EXPECT_TRUE(Res.TargetPrefix == "x86_64-unknown-freebsd13.2");
436+
EXPECT_TRUE(Res.TargetIsValid);
437+
433438
Res = ToolChain::getTargetAndModeFromProgramName(
434439
"x86_64-linux-gnu-clang-c++-tot");
435440
EXPECT_TRUE(Res.TargetPrefix == "x86_64-linux-gnu");

llvm/test/tools/llvm-ar/tool-name.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
# RUN: mkdir %t
66
# RUN: ln -s llvm-ar %t/llvm-ar-9
77
# RUN: ln -s llvm-ar %t/ar.exe
8+
# RUN: ln -s llvm-ar %t/x86_64-portbld-freebsd13.2-llvm-ar
9+
# RUN: ln -s llvm-ar %t/x86_64-portbld-freebsd13.2-llvm-ar.exe
810
# RUN: ln -s llvm-ar %t/arm-pokymllib32-linux-gnueabi-llvm-ar-9
911

1012
# RUN: llvm-ar h | FileCheck %s --check-prefix=DEFAULT
1113
# RUN: %t/llvm-ar-9 h | FileCheck %s --check-prefix=VERSION
1214
# RUN: %t/ar.exe h | FileCheck %s --check-prefix=SUFFIX
15+
# RUN: %t/x86_64-portbld-freebsd13.2-llvm-ar h | FileCheck %s --check-prefix=TARGETDOT
16+
# RUN: %t/x86_64-portbld-freebsd13.2-llvm-ar.exe h | FileCheck %s --check-prefix=TARGETDOT
1317
## Ensure that the "lib" substring does not result in misidentification as the
1418
## llvm-lib tool.
1519
# RUN: %t/arm-pokymllib32-linux-gnueabi-llvm-ar-9 h | FileCheck %s --check-prefix=ARM
1620

1721
# DEFAULT: USAGE: llvm-ar{{ }}
1822
# VERSION: USAGE: llvm-ar-9{{ }}
1923
# SUFFIX: USAGE: ar{{ }}
24+
# TARGETDOT: USAGE: x86_64-portbld-freebsd13.2-llvm-ar{{ }}
2025
# ARM: USAGE: arm-pokymllib32-linux-gnueabi-llvm-ar-9{{ }}

llvm/test/tools/llvm-objcopy/tool-name.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
# RUN: mkdir %t
66

77
# RUN: ln -s llvm-objcopy %t/llvm-objcopy-11.exe
8-
# RUN: ln -s llvm-objcopy %t/powerpc64-unknown-freebsd13-objcopy
8+
# RUN: ln -s llvm-objcopy %t/powerpc64-unknown-freebsd13.0-objcopy
99

1010
# RUN: llvm-objcopy --help | FileCheck --check-prefix=OBJCOPY %s
1111
# RUN: %t/llvm-objcopy-11.exe --help | FileCheck --check-prefix=OBJCOPY %s
12-
# RUN: %t/powerpc64-unknown-freebsd13-objcopy --help | FileCheck --check-prefix=OBJCOPY %s
12+
# RUN: %t/powerpc64-unknown-freebsd13.0-objcopy --help | FileCheck --check-prefix=OBJCOPY %s
1313

1414
# OBJCOPY: OVERVIEW: llvm-objcopy tool
1515

llvm/test/tools/llvm-objdump/tool-name.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
# RUN: mkdir %t
66

77
# RUN: ln -s llvm-objdump %t/llvm-otool-11.exe
8-
# RUN: ln -s llvm-objdump %t/powerpc64-unknown-freebsd13-objdump
8+
# RUN: ln -s llvm-objdump %t/powerpc64-unknown-freebsd13.0-objdump
99

1010
# RUN: %t/llvm-otool-11.exe --help | FileCheck --check-prefix=OTOOL %s
11-
# RUN: %t/powerpc64-unknown-freebsd13-objdump --help | \
11+
# RUN: %t/powerpc64-unknown-freebsd13.0-objdump --help | \
1212
# RUN: FileCheck --check-prefix=OBJDUMP %s
1313

1414
# OBJDUMP: OVERVIEW: llvm object file dumper

llvm/test/tools/llvm-ranlib/tool-name.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
# RUN: mkdir %t
66
# RUN: ln -s llvm-ranlib %t/llvm-ranlib-9
77
# RUN: ln -s llvm-ranlib %t/ranlib.exe
8+
# RUN: ln -s llvm-ranlib %t/x86_64-unknown-freebsd13.2-llvm-ranlib
89

910
# RUN: llvm-ranlib -h | FileCheck %s --check-prefix=DEFAULT
1011
# RUN: %t/llvm-ranlib-9 -h | FileCheck %s --check-prefix=VERSION
1112
# RUN: %t/ranlib.exe -h | FileCheck %s --check-prefix=SUFFIX
13+
# RUN: %t/x86_64-unknown-freebsd13.2-llvm-ranlib -h | FileCheck %s --check-prefix=TRIPLE
1214

1315
# DEFAULT: USAGE: llvm-ranlib{{ }}
1416
# VERSION: USAGE: llvm-ranlib-9{{ }}
1517
# SUFFIX: USAGE: ranlib{{ }}
18+
# TRIPLE: USAGE: x86_64-unknown-freebsd13.2-llvm-ranlib{{ }}

llvm/unittests/Support/Path.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,40 @@ TEST(Support, ReplacePathPrefix) {
16961696
EXPECT_EQ(Path, "C:\\old/foo\\bar");
16971697
}
16981698

1699+
TEST(Support, FindProgramName) {
1700+
StringRef WindowsProgName =
1701+
path::program_name("C:\\Test\\foo.exe", path::Style::windows);
1702+
EXPECT_EQ(WindowsProgName, "foo");
1703+
1704+
StringRef WindowsProgNameManyDots = path::program_name(
1705+
"C:\\Test.7\\x86_64-freebsd14.0-clang.exe", path::Style::windows);
1706+
EXPECT_EQ(WindowsProgNameManyDots, "x86_64-freebsd14.0-clang");
1707+
1708+
StringRef PosixProgName =
1709+
path::program_name("/var/empty/clang.exe", path::Style::posix);
1710+
EXPECT_EQ(PosixProgName, "clang");
1711+
1712+
StringRef PosixProgNameManyDotsExe = path::program_name(
1713+
"/llvm-test16.4/x86_64-portbld-freebsd13.2-llvm-ar.exe",
1714+
path::Style::posix);
1715+
EXPECT_EQ(PosixProgNameManyDotsExe, "x86_64-portbld-freebsd13.2-llvm-ar");
1716+
1717+
StringRef PosixProgNameManyDots = path::program_name(
1718+
"/llvm-test16.4/x86_64-portbld-freebsd13.2-llvm-ar", path::Style::posix);
1719+
EXPECT_EQ(PosixProgNameManyDots, "x86_64-portbld-freebsd13.2-llvm-ar");
1720+
1721+
StringRef PosixProgNameSh =
1722+
path::program_name("/llvm-test16.4/x86_64-portbld-freebsd13.2-llvm-ar.sh",
1723+
path::Style::posix);
1724+
EXPECT_EQ(PosixProgNameSh, "x86_64-portbld-freebsd13.2-llvm-ar.sh");
1725+
1726+
// TODO: determine if this is correct. What happens on windows with an executable
1727+
// named ".exe"?
1728+
StringRef OnlyExe =
1729+
path::program_name("/var/empty/.exe", path::Style::posix);
1730+
EXPECT_EQ(OnlyExe, "");
1731+
}
1732+
16991733
TEST_F(FileSystemTest, OpenFileForRead) {
17001734
// Create a temp file.
17011735
int FileDescriptor;

0 commit comments

Comments
 (0)