Skip to content

Commit 5f5e57a

Browse files
committed
Support: hoist lld's executable name code to Path
Instead of using custom code to find the program name throughout the codebase, write one function as a path helper to consistently determine the program name. This globally correctly finds target names with dots in them (ie freebsd13.2).
1 parent 1feb00a commit 5f5e57a

File tree

17 files changed

+36
-21
lines changed

17 files changed

+36
-21
lines changed

lld/COFF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
6868
auto *ctx = new COFFLinkerContext;
6969

7070
ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
71-
ctx->e.logName = args::getFilenameWithoutExe(args[0]);
71+
ctx->e.logName = sys::path::program_name(args[0]);
7272
ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now"
7373
" (use /errorlimit:0 to see all errors)";
7474

lld/Common/Args.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,3 @@ std::vector<StringRef> lld::args::getLines(MemoryBufferRef mb) {
8585
}
8686
return ret;
8787
}
88-
89-
StringRef lld::args::getFilenameWithoutExe(StringRef path) {
90-
if (path.ends_with_insensitive(".exe"))
91-
return sys::path::stem(path);
92-
return sys::path::filename(path);
93-
}

lld/ELF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
143143

144144
SharedFile::vernauxNum = 0;
145145
};
146-
ctx->e.logName = args::getFilenameWithoutExe(args[0]);
146+
ctx->e.logName = sys::path::program_name(args[0]);
147147
ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use "
148148
"--error-limit=0 to see all errors)";
149149

lld/MachO/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
14281428
InputFile::resetIdCount();
14291429
};
14301430

1431-
ctx->e.logName = args::getFilenameWithoutExe(argsArr[0]);
1431+
ctx->e.logName = sys::path::program_name(argsArr[0]);
14321432

14331433
MachOOptTable parser;
14341434
InputArgList args = parser.parse(argsArr.slice(1));

lld/include/lld/Common/Args.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ uint64_t getZOptionValue(llvm::opt::InputArgList &args, int id, StringRef key,
3838

3939
std::vector<StringRef> getLines(MemoryBufferRef mb);
4040

41-
StringRef getFilenameWithoutExe(StringRef path);
42-
4341
} // namespace args
4442
} // namespace lld
4543

lld/wasm/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
8989
auto *ctx = new CommonLinkerContext;
9090

9191
ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
92-
ctx->e.logName = args::getFilenameWithoutExe(args[0]);
92+
ctx->e.logName = sys::path::program_name(args[0]);
9393
ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use "
9494
"-error-limit=0 to see all errors)";
9595

lldb/tools/driver/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ int main(int argc, char const *argv[]) {
754754
ArrayRef<const char *> arg_arr = ArrayRef(argv + 1, argc - 1);
755755
opt::InputArgList input_args =
756756
T.ParseArgs(arg_arr, MissingArgIndex, MissingArgCount);
757-
llvm::StringRef argv0 = llvm::sys::path::filename(argv[0]);
757+
llvm::StringRef argv0 = llvm::sys::path::program_name(argv[0]);
758758

759759
if (input_args.hasArg(OPT_help)) {
760760
printHelp(T, argv0);

llvm/include/llvm/Support/Path.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,21 @@ StringRef stem(StringRef path, Style style = Style::native);
390390
/// @result The extension of \a path.
391391
StringRef extension(StringRef path, Style style = Style::native);
392392

393+
/// Get the program's name
394+
///
395+
/// If the path ends with the string ".exe", returns the stem of
396+
/// \a path. Otherwise returns the filename of \a path.
397+
///
398+
/// @code
399+
/// /foo/prog.exe => prog
400+
/// /bar/prog => prog
401+
/// /foo/prog1.2 => prog1.2
402+
/// @endcode
403+
///
404+
/// @param path Input path.
405+
/// @result The filename of \a path without any ".exe" component.
406+
StringRef program_name(StringRef path, Style style = Style::native);
407+
393408
/// Check whether the given char is a path separator on the host OS.
394409
///
395410
/// @param value a character

llvm/lib/Support/Path.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,13 @@ StringRef extension(StringRef path, Style style) {
600600
return fname.substr(pos);
601601
}
602602

603+
StringRef program_name(StringRef path, Style style) {
604+
// In the future this may need to be extended to other program suffixes.
605+
if (path.ends_with_insensitive(".exe"))
606+
return stem(path, style);
607+
return filename(path, style);
608+
}
609+
603610
bool is_separator(char value, Style style) {
604611
if (value == '/')
605612
return true;

llvm/tools/llvm-ar/llvm-ar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,8 @@ int llvm_ar_main(int argc, char **argv, const llvm::ToolContext &) {
15081508
llvm::InitializeAllTargetMCs();
15091509
llvm::InitializeAllAsmParsers();
15101510

1511-
Stem = sys::path::stem(ToolName);
1511+
Stem = sys::path::program_name(ToolName);
1512+
15121513
auto Is = [](StringRef Tool) {
15131514
// We need to recognize the following filenames.
15141515
//

llvm/tools/llvm-cov/llvm-cov.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int main(int argc, const char **argv) {
5959
InitLLVM X(argc, argv);
6060

6161
// If argv[0] is or ends with 'gcov', always be gcov compatible
62-
if (sys::path::stem(argv[0]).ends_with_insensitive("gcov"))
62+
if (sys::path::program_name(argv[0]).ends_with_insensitive("gcov"))
6363
return gcovMain(argc, argv);
6464

6565
// Check if we are invoking a specific tool command.

llvm/tools/llvm-driver/llvm-driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static int findTool(int Argc, char **Argv, const char *Argv0) {
4848
return 0;
4949
}
5050

51-
StringRef Stem = sys::path::stem(ToolName);
51+
StringRef Stem = sys::path::program_name(ToolName);
5252
auto Is = [=](StringRef Tool) {
5353
auto IsImpl = [=](StringRef Stem) {
5454
auto I = Stem.rfind_insensitive(Tool);

llvm/tools/llvm-objcopy/llvm-objcopy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static ErrorSuccess reportWarning(Error E) {
7474
}
7575

7676
static Expected<DriverConfig> getDriverConfig(ArrayRef<const char *> Args) {
77-
StringRef Stem = sys::path::stem(ToolName);
77+
StringRef Stem = sys::path::program_name(ToolName);
7878
auto Is = [=](StringRef Tool) {
7979
// We need to recognize the following filenames:
8080
//

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3297,7 +3297,7 @@ int llvm_objdump_main(int argc, char **argv, const llvm::ToolContext &) {
32973297
std::unique_ptr<CommonOptTable> T;
32983298
OptSpecifier Unknown, HelpFlag, HelpHiddenFlag, VersionFlag;
32993299

3300-
StringRef Stem = sys::path::stem(ToolName);
3300+
StringRef Stem = sys::path::program_name(ToolName);
33013301
auto Is = [=](StringRef Tool) {
33023302
// We need to recognize the following filenames:
33033303
//

llvm/tools/llvm-rc/llvm-rc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ void preprocess(StringRef Src, StringRef Dst, const RcOptions &Opts,
276276
}
277277

278278
static std::pair<bool, std::string> isWindres(llvm::StringRef Argv0) {
279-
StringRef ProgName = llvm::sys::path::stem(Argv0);
279+
StringRef ProgName = sys::path::program_name(Argv0);
280280
// x86_64-w64-mingw32-windres -> x86_64-w64-mingw32, windres
281281
// llvm-rc -> "", llvm-rc
282282
// aarch64-w64-mingw32-llvm-windres-10.exe -> aarch64-w64-mingw32, llvm-windres

llvm/tools/llvm-readobj/llvm-readobj.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ int llvm_readobj_main(int argc, char **argv, const llvm::ToolContext &) {
657657
return 0;
658658
}
659659

660-
if (sys::path::stem(argv[0]).contains("readelf"))
660+
if (sys::path::filename(argv[0]).contains("readelf"))
661661
opts::Output = opts::GNU;
662662
parseOptions(Args);
663663

llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ int llvm_symbolizer_main(int argc, char **argv, const llvm::ToolContext &) {
432432
sys::InitializeCOMRAII COM(sys::COMThreadingMode::MultiThreaded);
433433

434434
ToolName = argv[0];
435-
bool IsAddr2Line = sys::path::stem(ToolName).contains("addr2line");
435+
bool IsAddr2Line = sys::path::filename(ToolName).contains("addr2line");
436436
BumpPtrAllocator A;
437437
StringSaver Saver(A);
438438
SymbolizerOptTable Tbl;

0 commit comments

Comments
 (0)