[clang] [clang][Driver] NFC: Share -B and --ld-path lookup helpers (PR #238477)
Aditya Sinha via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 3 22:06:29 PST 2026
https://github.com/AdityaSinha149 updated https://github.com/llvm/llvm-project/pull/238477
>From 5fd02a61297602f561c9fd9cc699228b82388782 Mon Sep 17 00:00:00 2001
From: AdityaSinha149 <adsinha at amd.com>
Date: Wed, 4 Nov 2026 11:24:43 +0530
Subject: [PATCH] [clang][Driver] NFC: Share -B and --ld-path lookup helpers
---
clang/include/clang/Driver/CommonArgs.h | 14 ++++++
clang/lib/Driver/Driver.cpp | 39 ++-------------
clang/lib/Driver/ToolChain.cpp | 16 +++----
clang/lib/Driver/ToolChains/CommonArgs.cpp | 56 ++++++++++++++++++++++
4 files changed, 82 insertions(+), 43 deletions(-)
diff --git a/clang/include/clang/Driver/CommonArgs.h b/clang/include/clang/Driver/CommonArgs.h
index 138e18f8641b90..70fff756f801a0 100644
--- a/clang/include/clang/Driver/CommonArgs.h
+++ b/clang/include/clang/Driver/CommonArgs.h
@@ -15,10 +15,12 @@
#include "clang/Driver/Multilib.h"
#include "clang/Driver/Tool.h"
#include "clang/Driver/ToolChain.h"
+#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/CodeGen.h"
+#include <optional>
namespace clang {
namespace driver {
@@ -37,6 +39,18 @@ struct OffloadJobsOpt {
OffloadJobsOpt parseOffloadJobs(const llvm::opt::ArgList &Args);
+bool ScanDirForExecutable(SmallString<128> &Dir, StringRef Name);
+
+void addPrefixDirs(const llvm::opt::ArgList &Args,
+ SmallVectorImpl<std::string> &PrefixDirs);
+
+std::optional<std::string>
+findProgramInPrefixDirs(StringRef Name, ArrayRef<std::string> PrefixDirs);
+
+std::optional<std::string>
+resolveLdPath(StringRef LdPath,
+ llvm::function_ref<std::string(StringRef)> GetProgramPath);
+
void addPathIfExists(const Driver &D, const Twine &Path,
ToolChain::path_list &Paths);
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 37fb69baf6a3eb..486b9841e0e84b 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1697,20 +1697,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
TargetTriple = A->getValue();
if (const Arg *A = Args.getLastArg(options::OPT_ccc_install_dir))
Dir = A->getValue();
- for (const Arg *A : Args.filtered(options::OPT_B)) {
- A->claim();
- PrefixDirs.push_back(A->getValue(0));
- }
- if (std::optional<std::string> CompilerPathValue =
- llvm::sys::Process::GetEnv("COMPILER_PATH")) {
- StringRef CompilerPath = *CompilerPathValue;
- while (!CompilerPath.empty()) {
- std::pair<StringRef, StringRef> Split =
- CompilerPath.split(llvm::sys::EnvPathSeparator);
- PrefixDirs.push_back(std::string(Split.first));
- CompilerPath = Split.second;
- }
- }
+ tools::addPrefixDirs(Args, PrefixDirs);
if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ))
SysRoot = A->getValue();
if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ))
@@ -5971,31 +5958,15 @@ void Driver::generatePrefixedToolNames(
Names.emplace_back(Tool);
}
-static bool ScanDirForExecutable(SmallString<128> &Dir, StringRef Name) {
- llvm::sys::path::append(Dir, Name);
- if (llvm::sys::fs::can_execute(Twine(Dir)))
- return true;
- llvm::sys::path::remove_filename(Dir);
- return false;
-}
-
std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
SmallVector<std::string, 2> TargetSpecificExecutables;
generatePrefixedToolNames(Name, TC, TargetSpecificExecutables);
// Respect a limited subset of the '-Bprefix' functionality in GCC by
// attempting to use this prefix when looking for program paths.
- for (const auto &PrefixDir : PrefixDirs) {
- if (llvm::sys::fs::is_directory(PrefixDir)) {
- SmallString<128> P(PrefixDir);
- if (ScanDirForExecutable(P, Name))
- return std::string(P);
- } else {
- SmallString<128> P((PrefixDir + Name).str());
- if (llvm::sys::fs::can_execute(Twine(P)))
- return std::string(P);
- }
- }
+ if (std::optional<std::string> P =
+ tools::findProgramInPrefixDirs(Name, PrefixDirs))
+ return *P;
const ToolChain::path_list &List = TC.getProgramPaths();
for (const auto &TargetSpecificExecutable : TargetSpecificExecutables) {
@@ -6008,7 +5979,7 @@ std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
// of gcc in the program path
for (const auto &Path : List) {
SmallString<128> P(Path);
- if (ScanDirForExecutable(P, TargetSpecificExecutable))
+ if (tools::ScanDirForExecutable(P, TargetSpecificExecutable))
return std::string(P);
}
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index b3e7043a64fe56..6c1e5a98cf6ea5 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -1321,15 +1321,13 @@ std::string ToolChain::GetLinkerPath(bool *LinkerIsLLD) const {
// -fuse-ld=lld can be used with --ld-path= to inform clang that the binary
// that --ld-path= points to is lld.
if (const Arg *A = Args.getLastArg(options::OPT_ld_path_EQ)) {
- std::string Path(A->getValue());
- if (!Path.empty()) {
- if (llvm::sys::path::parent_path(Path).empty())
- Path = GetProgramPath(A->getValue());
- if (llvm::sys::fs::can_execute(Path)) {
- if (LinkerIsLLD)
- *LinkerIsLLD = UseLinker == "lld";
- return std::string(Path);
- }
+ if (std::optional<std::string> Path =
+ tools::resolveLdPath(A->getValue(), [&](StringRef Name) {
+ return GetProgramPath(Name.str().c_str());
+ })) {
+ if (LinkerIsLLD)
+ *LinkerIsLLD = UseLinker == "lld";
+ return *Path;
}
getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
return GetProgramPath(getDefaultLinker());
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 0590c297edf397..67f13f3fdb1476 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -82,6 +82,62 @@ OffloadJobsOpt tools::parseOffloadJobs(const ArgList &Args) {
return {OffloadJobsOpt::Kind::Fixed, A, Val, unsigned(NumThreads)};
}
+bool tools::ScanDirForExecutable(SmallString<128> &Dir, StringRef Name) {
+ llvm::sys::path::append(Dir, Name);
+ if (llvm::sys::fs::can_execute(Twine(Dir)))
+ return true;
+ llvm::sys::path::remove_filename(Dir);
+ return false;
+}
+
+void tools::addPrefixDirs(const ArgList &Args,
+ SmallVectorImpl<std::string> &PrefixDirs) {
+ for (const Arg *A : Args.filtered(options::OPT_B)) {
+ A->claim();
+ PrefixDirs.push_back(A->getValue(0));
+ }
+ if (std::optional<std::string> CompilerPathValue =
+ llvm::sys::Process::GetEnv("COMPILER_PATH")) {
+ StringRef CompilerPath = *CompilerPathValue;
+ while (!CompilerPath.empty()) {
+ std::pair<StringRef, StringRef> Split =
+ CompilerPath.split(llvm::sys::EnvPathSeparator);
+ PrefixDirs.push_back(std::string(Split.first));
+ CompilerPath = Split.second;
+ }
+ }
+}
+
+std::optional<std::string>
+tools::findProgramInPrefixDirs(StringRef Name,
+ ArrayRef<std::string> PrefixDirs) {
+ for (const auto &PrefixDir : PrefixDirs) {
+ if (llvm::sys::fs::is_directory(PrefixDir)) {
+ SmallString<128> P(PrefixDir);
+ if (ScanDirForExecutable(P, Name))
+ return std::string(P);
+ } else {
+ SmallString<128> P((PrefixDir + Name).str());
+ if (llvm::sys::fs::can_execute(Twine(P)))
+ return std::string(P);
+ }
+ }
+ return std::nullopt;
+}
+
+std::optional<std::string> tools::resolveLdPath(
+ StringRef LdPath,
+ llvm::function_ref<std::string(StringRef)> GetProgramPath) {
+ std::string Path(LdPath);
+ if (Path.empty())
+ return std::nullopt;
+ if (llvm::sys::path::parent_path(Path).empty())
+ Path = GetProgramPath(Path);
+ if (!llvm::sys::fs::can_execute(Path))
+ return std::nullopt;
+ return Path;
+}
+
static bool useFramePointerForTargetByDefault(const llvm::opt::ArgList &Args,
const llvm::Triple &Triple) {
if (Args.hasArg(options::OPT_pg) && !Args.hasArg(options::OPT_mfentry))
More information about the cfe-commits
mailing list