[clang] Infer compilation directory in driver

When building with -fdebug-compilation-dir/-fcoverige-compilation-dir,
infer the compilation directory in clang driver, rather than try to
fallback to VFS current working directory lookup during CodeGen. This
allows compilation directory to be used as it is passed via cc1 flag and
the value can be empty to remove dependency on CWD if needed.

Reviewers: adrian-prantl, dwblaikie

Reviewed By: adrian-prantl, dwblaikie

Pull Request: https://github.com/llvm/llvm-project/pull/150112
This commit is contained in:
Steven Wu 2025-07-31 10:39:06 -07:00 committed by GitHub
parent 91e0055c7c
commit 441f5b0e36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 18 deletions

View File

@ -226,17 +226,19 @@ static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC,
static const char *addDebugCompDirArg(const ArgList &Args,
ArgStringList &CmdArgs,
const llvm::vfs::FileSystem &VFS) {
std::string DebugCompDir;
if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ,
options::OPT_fdebug_compilation_dir_EQ)) {
if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ))
CmdArgs.push_back(Args.MakeArgString(Twine("-fdebug-compilation-dir=") +
A->getValue()));
options::OPT_fdebug_compilation_dir_EQ))
DebugCompDir = A->getValue();
if (DebugCompDir.empty()) {
if (llvm::ErrorOr<std::string> CWD = VFS.getCurrentWorkingDirectory())
DebugCompDir = std::move(*CWD);
else
A->render(Args, CmdArgs);
} else if (llvm::ErrorOr<std::string> CWD =
VFS.getCurrentWorkingDirectory()) {
CmdArgs.push_back(Args.MakeArgString("-fdebug-compilation-dir=" + *CWD));
return nullptr;
}
CmdArgs.push_back(
Args.MakeArgString("-fdebug-compilation-dir=" + DebugCompDir));
StringRef Path(CmdArgs.back());
return Path.substr(Path.find('=') + 1).data();
}
@ -525,17 +527,17 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
CmdArgs.push_back("-fcoverage-mcdc");
}
StringRef CoverageCompDir;
if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ,
options::OPT_fcoverage_compilation_dir_EQ)) {
if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ))
CmdArgs.push_back(Args.MakeArgString(
Twine("-fcoverage-compilation-dir=") + A->getValue()));
else
A->render(Args, CmdArgs);
} else if (llvm::ErrorOr<std::string> CWD =
D.getVFS().getCurrentWorkingDirectory()) {
CmdArgs.push_back(Args.MakeArgString("-fcoverage-compilation-dir=" + *CWD));
}
options::OPT_fcoverage_compilation_dir_EQ))
CoverageCompDir = A->getValue();
if (CoverageCompDir.empty()) {
if (auto CWD = D.getVFS().getCurrentWorkingDirectory())
CmdArgs.push_back(
Args.MakeArgString(Twine("-fcoverage-compilation-dir=") + *CWD));
} else
CmdArgs.push_back(Args.MakeArgString(Twine("-fcoverage-compilation-dir=") +
CoverageCompDir));
if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) {
auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ);

View File

@ -8,3 +8,8 @@
// RUN: %clang -### -integrated-as -ffile-compilation-dir=. -x assembler %s 2>&1 | FileCheck -check-prefixes=CHECK-DEBUG-COMPILATION-DIR %s
// CHECK-DEBUG-COMPILATION-DIR: "-fdebug-compilation-dir=."
// CHECK-DEBUG-COMPILATION-DIR-NOT: "-ffile-compilation-dir=."
// RUN: %clang -### -S %s -working-directory %S 2>&1 | FileCheck -check-prefix=CHECK-CWD %s
// RUN: cd %S
// RUN: %clang -### -S %s 2>&1 | FileCheck -check-prefix=CHECK-CWD %s
// CHECK-CWD: -fdebug-compilation-dir={{.*}}Driver