summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Korous <jkorous@apple.com>2019-10-15 17:51:59 +0000
committerJan Korous <jkorous@apple.com>2019-10-15 17:51:59 +0000
commit92d65244d538fe82dc83492b2521e757bb84e96f (patch)
treed65c20c0c9e8a8bd92c319ddf142a5a44d24935c
parent943e5a44883539848588e33bffbe2a1773dc4b43 (diff)
downloadclang-92d65244d538fe82dc83492b2521e757bb84e96f.tar.gz
Reland [Driver] Fix -working-directory issues
Don't change the default VFS in Driver, update tests & reland. This reverts commit 999f8a7416f8edc54ef92e715fd23c532bcc74d4. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@374926 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Basic/DiagnosticDriverKinds.td2
-rw-r--r--lib/Driver/Driver.cpp22
-rw-r--r--lib/Driver/ToolChains/Clang.cpp35
-rw-r--r--test/Driver/gen-cdb-fragment.c8
-rw-r--r--test/Driver/working-directory.c13
5 files changed, 45 insertions, 35 deletions
diff --git a/include/clang/Basic/DiagnosticDriverKinds.td b/include/clang/Basic/DiagnosticDriverKinds.td
index 95d67b11da..5ff03e1335 100644
--- a/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/include/clang/Basic/DiagnosticDriverKinds.td
@@ -91,6 +91,8 @@ def err_no_external_assembler : Error<
"there is no external assembler that can be used on this platform">;
def err_drv_unable_to_remove_file : Error<
"unable to remove file: %0">;
+def err_drv_unable_to_set_working_directory : Error <
+ "unable to set working directory: %0">;
def err_drv_command_failure : Error<
"unable to execute command: %0">;
def err_drv_invalid_darwin_version : Error<
diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp
index 222efd5249..7a0defe0ba 100644
--- a/lib/Driver/Driver.cpp
+++ b/lib/Driver/Driver.cpp
@@ -1012,6 +1012,11 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
}
}
+ // Check for working directory option before accessing any files
+ if (Arg *WD = Args.getLastArg(options::OPT_working_directory))
+ if (VFS->setCurrentWorkingDirectory(WD->getValue()))
+ Diag(diag::err_drv_unable_to_set_working_directory) << WD->getValue();
+
// FIXME: This stuff needs to go into the Compilation, not the driver.
bool CCCPrintPhases;
@@ -1993,20 +1998,11 @@ bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
if (Value == "-")
return true;
- SmallString<64> Path(Value);
- if (Arg *WorkDir = Args.getLastArg(options::OPT_working_directory)) {
- if (!llvm::sys::path::is_absolute(Path)) {
- SmallString<64> Directory(WorkDir->getValue());
- llvm::sys::path::append(Directory, Value);
- Path.assign(Directory);
- }
- }
-
- if (getVFS().exists(Path))
+ if (getVFS().exists(Value))
return true;
if (IsCLMode()) {
- if (!llvm::sys::path::is_absolute(Twine(Path)) &&
+ if (!llvm::sys::path::is_absolute(Twine(Value)) &&
llvm::sys::Process::FindInEnvPath("LIB", Value))
return true;
@@ -2032,12 +2028,12 @@ bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
if (getOpts().findNearest(Value, Nearest, IncludedFlagsBitmask,
ExcludedFlagsBitmask) <= 1) {
Diag(clang::diag::err_drv_no_such_file_with_suggestion)
- << Path << Nearest;
+ << Value << Nearest;
return false;
}
}
- Diag(clang::diag::err_drv_no_such_file) << Path;
+ Diag(clang::diag::err_drv_no_such_file) << Value;
return false;
}
diff --git a/lib/Driver/ToolChains/Clang.cpp b/lib/Driver/ToolChains/Clang.cpp
index ad3f181a58..711d3537c1 100644
--- a/lib/Driver/ToolChains/Clang.cpp
+++ b/lib/Driver/ToolChains/Clang.cpp
@@ -519,16 +519,15 @@ getFramePointerKind(const ArgList &Args, const llvm::Triple &Triple) {
}
/// Add a CC1 option to specify the debug compilation directory.
-static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
+static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs,
+ const llvm::vfs::FileSystem &VFS) {
if (Arg *A = Args.getLastArg(options::OPT_fdebug_compilation_dir)) {
CmdArgs.push_back("-fdebug-compilation-dir");
CmdArgs.push_back(A->getValue());
- } else {
- SmallString<128> cwd;
- if (!llvm::sys::fs::current_path(cwd)) {
- CmdArgs.push_back("-fdebug-compilation-dir");
- CmdArgs.push_back(Args.MakeArgString(cwd));
- }
+ } else if (llvm::ErrorOr<std::string> CWD =
+ VFS.getCurrentWorkingDirectory()) {
+ CmdArgs.push_back("-fdebug-compilation-dir");
+ CmdArgs.push_back(Args.MakeArgString(*CWD));
}
}
@@ -808,13 +807,8 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
else
OutputFilename = llvm::sys::path::filename(Output.getBaseInput());
SmallString<128> CoverageFilename = OutputFilename;
- if (llvm::sys::path::is_relative(CoverageFilename)) {
- SmallString<128> Pwd;
- if (!llvm::sys::fs::current_path(Pwd)) {
- llvm::sys::path::append(Pwd, CoverageFilename);
- CoverageFilename.swap(Pwd);
- }
- }
+ if (llvm::sys::path::is_relative(CoverageFilename))
+ (void)D.getVFS().makeAbsolute(CoverageFilename);
llvm::sys::path::replace_extension(CoverageFilename, "gcno");
CmdArgs.push_back("-coverage-notes-file");
@@ -1937,13 +1931,14 @@ void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
CompilationDatabase = std::move(File);
}
auto &CDB = *CompilationDatabase;
- SmallString<128> Buf;
- if (llvm::sys::fs::current_path(Buf))
- Buf = ".";
- CDB << "{ \"directory\": \"" << escape(Buf) << "\"";
+ auto CWD = D.getVFS().getCurrentWorkingDirectory();
+ if (!CWD)
+ CWD = ".";
+ CDB << "{ \"directory\": \"" << escape(*CWD) << "\"";
CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\"";
CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\"";
+ SmallString<128> Buf;
Buf = "-x";
Buf += types::getTypeName(Input.getType());
CDB << ", \"" << escape(Buf) << "\"";
@@ -4377,7 +4372,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("-fno-autolink");
// Add in -fdebug-compilation-dir if necessary.
- addDebugCompDirArg(Args, CmdArgs);
+ addDebugCompDirArg(Args, CmdArgs, D.getVFS());
addDebugPrefixMapArg(D, Args, CmdArgs);
@@ -6138,7 +6133,7 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
DebugInfoKind = (WantDebug ? codegenoptions::LimitedDebugInfo
: codegenoptions::NoDebugInfo);
// Add the -fdebug-compilation-dir flag if needed.
- addDebugCompDirArg(Args, CmdArgs);
+ addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS());
addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs);
diff --git a/test/Driver/gen-cdb-fragment.c b/test/Driver/gen-cdb-fragment.c
index f12a0e018d..41d69894a7 100644
--- a/test/Driver/gen-cdb-fragment.c
+++ b/test/Driver/gen-cdb-fragment.c
@@ -15,6 +15,14 @@
// RUN: %clang -target x86_64-apple-macos10.15 -S %s -o - -gen-cdb-fragment-path %t.cdb
// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+// Working directory arg is respected.
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -working-directory %t.cdb -c %s -o - -gen-cdb-fragment-path "."
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+// RUN: cat %t.cdb/*.json | FileCheck --check-prefix=CHECK-CWD %s
+// CHECK-CWD: "directory": "{{.*}}.cdb"
+
// -### does not emit the CDB fragment
// RUN: rm -rf %t.cdb
// RUN: mkdir %t.cdb
diff --git a/test/Driver/working-directory.c b/test/Driver/working-directory.c
index 15ba8f00bd..7cfe22d542 100644
--- a/test/Driver/working-directory.c
+++ b/test/Driver/working-directory.c
@@ -1,3 +1,12 @@
-// RUN: %clang -### -working-directory /no/such/dir/ input 2>&1 | FileCheck %s
+// RUN: %clang -### -coverage -working-directory /no/such/dir/ input 2>&1 | FileCheck %s
+// RUN: %clang -### -coverage -working-directory %p/Inputs no_such_file.cpp -c 2>&1 | FileCheck %s --check-prefix=CHECK_NO_FILE
+// RUN: %clang -### -coverage -working-directory %p/Inputs pchfile.cpp -c 2>&1 | FileCheck %s --check-prefix=CHECK_WORKS
+// RUN: fail please
-//CHECK: no such file or directory: '/no/such/dir/input'
+// CHECK: unable to set working directory: /no/such/dir/
+
+// CHECK_NO_FILE: no such file or directory: 'no_such_file.cpp'
+
+// CHECK_WORKS: "-coverage-notes-file" "{{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs{{/|\\\\}}pchfile.gcno"
+// CHECK_WORKS: "-working-directory" "{{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs"
+// CHECK_WORKS: "-fdebug-compilation-dir" "{{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs"