From 008eda080786e110914f4aa57d31342098046c94 Mon Sep 17 00:00:00 2001 From: Ilya Biryukov Date: Tue, 11 Sep 2018 07:29:09 +0000 Subject: [PATCH] [Tooling] Restore working dir in ClangTool. Summary: And add an option to disable this behavior. The option is only used in AllTUsExecutor to avoid races when running concurrently on multiple threads. This fixes PR38869 introduced by r340937. Reviewers: ioeric, steveire Reviewed By: ioeric Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D51864 llvm-svn: 341910 --- clang/include/clang/Tooling/Tooling.h | 7 +++++++ clang/lib/Tooling/AllTUsExecution.cpp | 14 +++++++++++++- clang/lib/Tooling/Tooling.cpp | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h index be9ecad9..bbcccc5 100644 --- a/clang/include/clang/Tooling/Tooling.h +++ b/clang/include/clang/Tooling/Tooling.h @@ -356,6 +356,11 @@ public: /// append them to ASTs. int buildASTs(std::vector> &ASTs); + /// Sets whether working directory should be restored after calling run(). By + /// default, working directory is restored. However, it could be useful to + /// turn this off when running on multiple threads to avoid the raciness. + void setRestoreWorkingDir(bool RestoreCWD); + /// Returns the file manager used in the tool. /// /// The file manager is shared between all translation units. @@ -380,6 +385,8 @@ private: ArgumentsAdjuster ArgsAdjuster; DiagnosticConsumer *DiagConsumer = nullptr; + + bool RestoreCWD = true; }; template diff --git a/clang/lib/Tooling/AllTUsExecution.cpp b/clang/lib/Tooling/AllTUsExecution.cpp index b761556..a7af541 100644 --- a/clang/lib/Tooling/AllTUsExecution.cpp +++ b/clang/lib/Tooling/AllTUsExecution.cpp @@ -104,7 +104,12 @@ llvm::Error AllTUsToolExecutor::execute( { llvm::ThreadPool Pool(ThreadCount == 0 ? llvm::hardware_concurrency() : ThreadCount); - + llvm::SmallString<128> InitialWorkingDir; + if (auto EC = llvm::sys::fs::current_path(InitialWorkingDir)) { + InitialWorkingDir = ""; + llvm::errs() << "Error while getting current working directory: " + << EC.message() << "\n"; + } for (std::string File : Files) { Pool.async( [&](std::string Path) { @@ -116,12 +121,19 @@ llvm::Error AllTUsToolExecutor::execute( for (const auto &FileAndContent : OverlayFiles) Tool.mapVirtualFile(FileAndContent.first(), FileAndContent.second); + // Do not restore working dir from multiple threads to avoid races. + Tool.setRestoreWorkingDir(false); if (Tool.run(Action.first.get())) AppendError(llvm::Twine("Failed to run action on ") + Path + "\n"); }, File); } + if (!InitialWorkingDir.empty()) { + if (auto EC = llvm::sys::fs::set_current_path(InitialWorkingDir)) + llvm::errs() << "Error while restoring working directory: " + << EC.message() << "\n"; + } } if (!ErrorMsg.empty()) diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp index 1ec285c..395d2d7 100644 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -441,6 +441,17 @@ int ClangTool::run(ToolAction *Action) { AbsolutePaths.push_back(std::move(*AbsPath)); } + // Remember the working directory in case we need to restore it. + std::string InitialWorkingDir; + if (RestoreCWD) { + if (auto CWD = OverlayFileSystem->getCurrentWorkingDirectory()) { + InitialWorkingDir = std::move(*CWD); + } else { + llvm::errs() << "Could not get working directory: " + << CWD.getError().message() << "\n"; + } + } + for (llvm::StringRef File : AbsolutePaths) { // Currently implementations of CompilationDatabase::getCompileCommands can // change the state of the file system (e.g. prepare generated headers), so @@ -508,6 +519,13 @@ int ClangTool::run(ToolAction *Action) { } } } + + if (!InitialWorkingDir.empty()) { + if (auto EC = + OverlayFileSystem->setCurrentWorkingDirectory(InitialWorkingDir)) + llvm::errs() << "Error when trying to restore working dir: " + << EC.message() << "\n"; + } return ProcessingFailed ? 1 : (FileSkipped ? 2 : 0); } @@ -544,6 +562,10 @@ int ClangTool::buildASTs(std::vector> &ASTs) { return run(&Action); } +void ClangTool::setRestoreWorkingDir(bool RestoreCWD) { + this->RestoreCWD = RestoreCWD; +} + namespace clang { namespace tooling { -- 2.7.4