Skip to content

Commit f81e081

Browse files
[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#150112
1 parent 8f10bf3 commit f81e081

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
@@ -243,17 +243,19 @@ static bool ShouldEnableAutolink(const ArgList &Args, const ToolChain &TC,
243243
static const char *addDebugCompDirArg(const ArgList &Args,
244244
ArgStringList &CmdArgs,
245245
const llvm::vfs::FileSystem &VFS) {
246+
std::string DebugCompDir;
246247
if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ,
247-
options::OPT_fdebug_compilation_dir_EQ)) {
248-
if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ))
249-
CmdArgs.push_back(Args.MakeArgString(Twine("-fdebug-compilation-dir=") +
250-
A->getValue()));
248+
options::OPT_fdebug_compilation_dir_EQ))
249+
DebugCompDir = A->getValue();
250+
251+
if (DebugCompDir.empty()) {
252+
if (llvm::ErrorOr<std::string> CWD = VFS.getCurrentWorkingDirectory())
253+
DebugCompDir = std::move(*CWD);
251254
else
252-
A->render(Args, CmdArgs);
253-
} else if (llvm::ErrorOr<std::string> CWD =
254-
VFS.getCurrentWorkingDirectory()) {
255-
CmdArgs.push_back(Args.MakeArgString("-fdebug-compilation-dir=" + *CWD));
255+
return nullptr;
256256
}
257+
CmdArgs.push_back(
258+
Args.MakeArgString("-fdebug-compilation-dir=" + DebugCompDir));
257259
StringRef Path(CmdArgs.back());
258260
return Path.substr(Path.find('=') + 1).data();
259261
}
@@ -542,17 +544,17 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
542544
CmdArgs.push_back("-fcoverage-mcdc");
543545
}
544546

547+
StringRef CoverageCompDir;
545548
if (Arg *A = Args.getLastArg(options::OPT_ffile_compilation_dir_EQ,
546-
options::OPT_fcoverage_compilation_dir_EQ)) {
547-
if (A->getOption().matches(options::OPT_ffile_compilation_dir_EQ))
548-
CmdArgs.push_back(Args.MakeArgString(
549-
Twine("-fcoverage-compilation-dir=") + A->getValue()));
550-
else
551-
A->render(Args, CmdArgs);
552-
} else if (llvm::ErrorOr<std::string> CWD =
553-
D.getVFS().getCurrentWorkingDirectory()) {
554-
CmdArgs.push_back(Args.MakeArgString("-fcoverage-compilation-dir=" + *CWD));
555-
}
549+
options::OPT_fcoverage_compilation_dir_EQ))
550+
CoverageCompDir = A->getValue();
551+
if (CoverageCompDir.empty()) {
552+
if (auto CWD = D.getVFS().getCurrentWorkingDirectory())
553+
CmdArgs.push_back(
554+
Args.MakeArgString(Twine("-fcoverage-compilation-dir=") + *CWD));
555+
} else
556+
CmdArgs.push_back(Args.MakeArgString(Twine("-fcoverage-compilation-dir=") +
557+
CoverageCompDir));
556558

557559
if (Args.hasArg(options::OPT_fprofile_exclude_files_EQ)) {
558560
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)