Skip to content

Commit e66cd07

Browse files
Automerge: [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: llvm/llvm-project#150112
2 parents 0d925f2 + 441f5b0 commit e66cd07

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,19 @@ static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC,
226226
static const char *addDebugCompDirArg(const ArgList &Args,
227227
ArgStringList &CmdArgs,
228228
const llvm::vfs::FileSystem &VFS) {
229+
std::string DebugCompDir;
229230
if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ,
230-
options::OPT_fdebug_compilation_dir_EQ)) {
231-
if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ))
232-
CmdArgs.push_back(Args.MakeArgString(Twine("-fdebug-compilation-dir=") +
233-
A->getValue()));
231+
options::OPT_fdebug_compilation_dir_EQ))
232+
DebugCompDir = A->getValue();
233+
234+
if (DebugCompDir.empty()) {
235+
if (llvm::ErrorOr<std::string> CWD = VFS.getCurrentWorkingDirectory())
236+
DebugCompDir = std::move(*CWD);
234237
else
235-
A->render(Args, CmdArgs);
236-
} else if (llvm::ErrorOr<std::string> CWD =
237-
VFS.getCurrentWorkingDirectory()) {
238-
CmdArgs.push_back(Args.MakeArgString("-fdebug-compilation-dir=" + *CWD));
238+
return nullptr;
239239
}
240+
CmdArgs.push_back(
241+
Args.MakeArgString("-fdebug-compilation-dir=" + DebugCompDir));
240242
StringRef Path(CmdArgs.back());
241243
return Path.substr(Path.find('=') + 1).data();
242244
}
@@ -525,17 +527,17 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
525527
CmdArgs.push_back("-fcoverage-mcdc");
526528
}
527529

530+
StringRef CoverageCompDir;
528531
if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ,
529-
options::OPT_fcoverage_compilation_dir_EQ)) {
530-
if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ))
531-
CmdArgs.push_back(Args.MakeArgString(
532-
Twine("-fcoverage-compilation-dir=") + A->getValue()));
533-
else
534-
A->render(Args, CmdArgs);
535-
} else if (llvm::ErrorOr<std::string> CWD =
536-
D.getVFS().getCurrentWorkingDirectory()) {
537-
CmdArgs.push_back(Args.MakeArgString("-fcoverage-compilation-dir=" + *CWD));
538-
}
532+
options::OPT_fcoverage_compilation_dir_EQ))
533+
CoverageCompDir = A->getValue();
534+
if (CoverageCompDir.empty()) {
535+
if (auto CWD = D.getVFS().getCurrentWorkingDirectory())
536+
CmdArgs.push_back(
537+
Args.MakeArgString(Twine("-fcoverage-compilation-dir=") + *CWD));
538+
} else
539+
CmdArgs.push_back(Args.MakeArgString(Twine("-fcoverage-compilation-dir=") +
540+
CoverageCompDir));
539541

540542
if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) {
541543
auto *Arg = Args.getLastArg(options::OPT_fprofile_exclude_files_EQ);

clang/test/Driver/compilation-dir.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@
88
// RUN: %clang -### -integrated-as -ffile-compilation-dir=. -x assembler %s 2>&1 | FileCheck -check-prefixes=CHECK-DEBUG-COMPILATION-DIR %s
99
// CHECK-DEBUG-COMPILATION-DIR: "-fdebug-compilation-dir=."
1010
// CHECK-DEBUG-COMPILATION-DIR-NOT: "-ffile-compilation-dir=."
11+
12+
// RUN: %clang -### -S %s -working-directory %S 2>&1 | FileCheck -check-prefix=CHECK-CWD %s
13+
// RUN: cd %S
14+
// RUN: %clang -### -S %s 2>&1 | FileCheck -check-prefix=CHECK-CWD %s
15+
// CHECK-CWD: -fdebug-compilation-dir={{.*}}Driver

0 commit comments

Comments
 (0)