Skip to content

Commit c255101

Browse files
committed
Merging r371027:
------------------------------------------------------------------------ r371027 | hans | 2019-09-05 10:43:00 +0200 (Thu, 05 Sep 2019) | 20 lines Revert r361885 "[Driver] Fix -working-directory issues" This made clang unable to open files using relative paths on network shares on Windows (PR43204). On the bug it was pointed out that createPhysicalFileSystem() is not terribly mature, and using it is risky. Reverting for now until there's a clear way forward. > Currently the `-working-directory` option does not actually impact the working > directory for all of the clang driver, it only impacts how files are looked up > to make sure they exist. This means that that clang passes the wrong paths > to -fdebug-compilation-dir and -coverage-notes-file. > > This patch fixes that by changing all the places in the driver where we convert > to absolute paths to use the VFS, and then calling setCurrentWorkingDirectory on > the VFS. This also changes the default VFS for `Driver` to use a virtualized > working directory, instead of changing the process's working directory. > > Differential Revision: https://reviews.llvm.org/D62271 This also revertes the part of r369938 which checked that -working-directory works. ------------------------------------------------------------------------ llvm-svn: 371060
1 parent ff382fe commit c255101

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ def err_no_external_assembler : Error<
9191
"there is no external assembler that can be used on this platform">;
9292
def err_drv_unable_to_remove_file : Error<
9393
"unable to remove file: %0">;
94-
def err_drv_unable_to_set_working_directory : Error <
95-
"unable to set working directory: %0">;
9694
def err_drv_command_failure : Error<
9795
"unable to execute command: %0">;
9896
def err_drv_invalid_darwin_version : Error<

clang/lib/Driver/Driver.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
133133

134134
// Provide a sane fallback if no VFS is specified.
135135
if (!this->VFS)
136-
this->VFS = llvm::vfs::createPhysicalFileSystem().release();
136+
this->VFS = llvm::vfs::getRealFileSystem();
137137

138138
Name = llvm::sys::path::filename(ClangExecutable);
139139
Dir = llvm::sys::path::parent_path(ClangExecutable);
@@ -1010,11 +1010,6 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
10101010
}
10111011
}
10121012

1013-
// Check for working directory option before accessing any files
1014-
if (Arg *WD = Args.getLastArg(options::OPT_working_directory))
1015-
if (VFS->setCurrentWorkingDirectory(WD->getValue()))
1016-
Diag(diag::err_drv_unable_to_set_working_directory) << WD->getValue();
1017-
10181013
// FIXME: This stuff needs to go into the Compilation, not the driver.
10191014
bool CCCPrintPhases;
10201015

@@ -1995,11 +1990,20 @@ bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
19951990
if (Value == "-")
19961991
return true;
19971992

1998-
if (getVFS().exists(Value))
1993+
SmallString<64> Path(Value);
1994+
if (Arg *WorkDir = Args.getLastArg(options::OPT_working_directory)) {
1995+
if (!llvm::sys::path::is_absolute(Path)) {
1996+
SmallString<64> Directory(WorkDir->getValue());
1997+
llvm::sys::path::append(Directory, Value);
1998+
Path.assign(Directory);
1999+
}
2000+
}
2001+
2002+
if (getVFS().exists(Path))
19992003
return true;
20002004

20012005
if (IsCLMode()) {
2002-
if (!llvm::sys::path::is_absolute(Twine(Value)) &&
2006+
if (!llvm::sys::path::is_absolute(Twine(Path)) &&
20032007
llvm::sys::Process::FindInEnvPath("LIB", Value))
20042008
return true;
20052009

@@ -2025,12 +2029,12 @@ bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
20252029
if (getOpts().findNearest(Value, Nearest, IncludedFlagsBitmask,
20262030
ExcludedFlagsBitmask) <= 1) {
20272031
Diag(clang::diag::err_drv_no_such_file_with_suggestion)
2028-
<< Value << Nearest;
2032+
<< Path << Nearest;
20292033
return false;
20302034
}
20312035
}
20322036

2033-
Diag(clang::diag::err_drv_no_such_file) << Value;
2037+
Diag(clang::diag::err_drv_no_such_file) << Path;
20342038
return false;
20352039
}
20362040

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -603,15 +603,16 @@ static bool shouldUseLeafFramePointer(const ArgList &Args,
603603
}
604604

605605
/// Add a CC1 option to specify the debug compilation directory.
606-
static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs,
607-
const llvm::vfs::FileSystem &VFS) {
606+
static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
608607
if (Arg *A = Args.getLastArg(options::OPT_fdebug_compilation_dir)) {
609608
CmdArgs.push_back("-fdebug-compilation-dir");
610609
CmdArgs.push_back(A->getValue());
611-
} else if (llvm::ErrorOr<std::string> CWD =
612-
VFS.getCurrentWorkingDirectory()) {
613-
CmdArgs.push_back("-fdebug-compilation-dir");
614-
CmdArgs.push_back(Args.MakeArgString(*CWD));
610+
} else {
611+
SmallString<128> cwd;
612+
if (!llvm::sys::fs::current_path(cwd)) {
613+
CmdArgs.push_back("-fdebug-compilation-dir");
614+
CmdArgs.push_back(Args.MakeArgString(cwd));
615+
}
615616
}
616617
}
617618

@@ -877,8 +878,13 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
877878
else
878879
OutputFilename = llvm::sys::path::filename(Output.getBaseInput());
879880
SmallString<128> CoverageFilename = OutputFilename;
880-
if (llvm::sys::path::is_relative(CoverageFilename))
881-
(void)D.getVFS().makeAbsolute(CoverageFilename);
881+
if (llvm::sys::path::is_relative(CoverageFilename)) {
882+
SmallString<128> Pwd;
883+
if (!llvm::sys::fs::current_path(Pwd)) {
884+
llvm::sys::path::append(Pwd, CoverageFilename);
885+
CoverageFilename.swap(Pwd);
886+
}
887+
}
882888
llvm::sys::path::replace_extension(CoverageFilename, "gcno");
883889
CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
884890

@@ -4365,7 +4371,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
43654371
CmdArgs.push_back("-fno-autolink");
43664372

43674373
// Add in -fdebug-compilation-dir if necessary.
4368-
addDebugCompDirArg(Args, CmdArgs, D.getVFS());
4374+
addDebugCompDirArg(Args, CmdArgs);
43694375

43704376
addDebugPrefixMapArg(D, Args, CmdArgs);
43714377

@@ -6092,7 +6098,7 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
60926098
DebugInfoKind = (WantDebug ? codegenoptions::LimitedDebugInfo
60936099
: codegenoptions::NoDebugInfo);
60946100
// Add the -fdebug-compilation-dir flag if needed.
6095-
addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS());
6101+
addDebugCompDirArg(Args, CmdArgs);
60966102

60976103
addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs);
60986104

clang/test/Driver/working-directory.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
// RUN: %clang -### -working-directory /no/such/dir/ input 2>&1 | FileCheck %s
2-
// RUN: %clang -### -working-directory %p/Inputs no_such_file.cpp -c 2>&1 | FileCheck %s --check-prefix=CHECK_NO_FILE
3-
// RUN: %clang -### -working-directory %p/Inputs pchfile.cpp -c 2>&1 | FileCheck %s --check-prefix=CHECK_WORKS
42

5-
// CHECK: unable to set working directory: /no/such/dir/
6-
7-
// CHECK_NO_FILE: no such file or directory: 'no_such_file.cpp'
8-
9-
// CHECK_WORKS: "-coverage-notes-file" "{{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs{{/|\\\\}}pchfile.gcno"
10-
// CHECK_WORKS: "-working-directory" "{{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs"
11-
// CHECK_WORKS: "-fdebug-compilation-dir" "{{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs"
3+
//CHECK: no such file or directory: '/no/such/dir/input'

0 commit comments

Comments
 (0)