@@ -55,7 +55,7 @@ void actOnAllParentDirectories(PathRef FileName,
5555} // namespace
5656
5757tooling::CompileCommand
58- GlobalCompilationDatabase::getFallbackCommand (PathRef File, bool StrongWorkspaceMode ) const {
58+ GlobalCompilationDatabase::getFallbackCommand (PathRef File) const {
5959 std::vector<std::string> Argv = {" clang" };
6060 // Clang treats .h files as C by default and files without extension as linker
6161 // input, resulting in unhelpful diagnostics.
@@ -64,14 +64,10 @@ GlobalCompilationDatabase::getFallbackCommand(PathRef File, bool StrongWorkspace
6464 if (FileExtension.empty () || FileExtension == " .h" )
6565 Argv.push_back (" -xobjective-c++-header" );
6666 Argv.push_back (std::string (File));
67- SmallString<256 > WorkingDir;
68- if (StrongWorkspaceMode)
69- llvm::sys::fs::current_path (WorkingDir);
70- else
71- WorkingDir = llvm::sys::path::parent_path (File);
72- tooling::CompileCommand Cmd (WorkingDir,
73- llvm::sys::path::filename (File), std::move (Argv),
74- /* Output=*/ " " );
67+ tooling::CompileCommand Cmd (
68+ WorkingDirectory ? *WorkingDirectory : llvm::sys::path::parent_path (File),
69+ llvm::sys::path::filename (File), std::move (Argv),
70+ /* Output=*/ " " );
7571 Cmd.Heuristic = " clangd fallback" ;
7672 return Cmd;
7773}
@@ -354,7 +350,8 @@ bool DirectoryBasedGlobalCompilationDatabase::DirectoryCache::load(
354350
355351DirectoryBasedGlobalCompilationDatabase::
356352 DirectoryBasedGlobalCompilationDatabase (const Options &Opts)
357- : Opts(Opts), Broadcaster(std::make_unique<BroadcastThread>(*this )) {
353+ : GlobalCompilationDatabase(Opts.WorkingDirectory), Opts(Opts),
354+ Broadcaster(std::make_unique<BroadcastThread>(*this )) {
358355 if (!this ->Opts .ContextProvider )
359356 this ->Opts .ContextProvider = [](llvm::StringRef) {
360357 return Context::current ().clone ();
@@ -465,6 +462,17 @@ DirectoryBasedGlobalCompilationDatabase::lookupCDB(
465462 return Result;
466463}
467464
465+ void DirectoryBasedGlobalCompilationDatabase::Options::applyWorkingDirectory (
466+ const std::optional<std::string> &&WorkingDirectory) {
467+ if (WorkingDirectory)
468+ this ->WorkingDirectory = *WorkingDirectory;
469+ else {
470+ SmallString<256 > CWD;
471+ llvm::sys::fs::current_path (CWD);
472+ this ->WorkingDirectory = std::string (CWD);
473+ }
474+ }
475+
468476// The broadcast thread announces files with new compile commands to the world.
469477// Primarily this is used to enqueue them for background indexing.
470478//
@@ -764,8 +772,9 @@ DirectoryBasedGlobalCompilationDatabase::getProjectModules(PathRef File) const {
764772
765773OverlayCDB::OverlayCDB (const GlobalCompilationDatabase *Base,
766774 std::vector<std::string> FallbackFlags,
767- CommandMangler Mangler)
768- : DelegatingCDB(Base), Mangler(std::move(Mangler)),
775+ CommandMangler Mangler,
776+ std::optional<std::string> WorkingDirectory)
777+ : DelegatingCDB(Base, WorkingDirectory), Mangler(std::move(Mangler)),
769778 FallbackFlags(std::move(FallbackFlags)) {}
770779
771780std::optional<tooling::CompileCommand>
@@ -802,8 +811,8 @@ OverlayCDB::getCompileCommand(PathRef File) const {
802811 return Cmd;
803812}
804813
805- tooling::CompileCommand OverlayCDB::getFallbackCommand (PathRef File, bool StrongWorkspaceMode ) const {
806- auto Cmd = DelegatingCDB::getFallbackCommand (File, StrongWorkspaceMode );
814+ tooling::CompileCommand OverlayCDB::getFallbackCommand (PathRef File) const {
815+ auto Cmd = DelegatingCDB::getFallbackCommand (File);
807816 std::lock_guard<std::mutex> Lock (Mutex);
808817 Cmd.CommandLine .insert (Cmd.CommandLine .end (), FallbackFlags.begin (),
809818 FallbackFlags.end ());
@@ -849,16 +858,18 @@ OverlayCDB::getProjectModules(PathRef File) const {
849858 return MDB;
850859}
851860
852- DelegatingCDB::DelegatingCDB (const GlobalCompilationDatabase *Base)
853- : Base(Base) {
861+ DelegatingCDB::DelegatingCDB (const GlobalCompilationDatabase *Base,
862+ std::optional<std::string> WorkingDirectory)
863+ : GlobalCompilationDatabase(WorkingDirectory), Base(Base) {
854864 if (Base)
855865 BaseChanged = Base->watch ([this ](const std::vector<std::string> Changes) {
856866 OnCommandChanged.broadcast (Changes);
857867 });
858868}
859869
860- DelegatingCDB::DelegatingCDB (std::unique_ptr<GlobalCompilationDatabase> Base)
861- : DelegatingCDB(Base.get()) {
870+ DelegatingCDB::DelegatingCDB (std::unique_ptr<GlobalCompilationDatabase> Base,
871+ std::optional<std::string> WorkingDirectory)
872+ : DelegatingCDB(Base.get(), WorkingDirectory) {
862873 BaseOwner = std::move (Base);
863874}
864875
@@ -882,10 +893,10 @@ DelegatingCDB::getProjectModules(PathRef File) const {
882893 return Base->getProjectModules (File);
883894}
884895
885- tooling::CompileCommand DelegatingCDB::getFallbackCommand (PathRef File, bool StrongWorkspaceMode ) const {
896+ tooling::CompileCommand DelegatingCDB::getFallbackCommand (PathRef File) const {
886897 if (!Base)
887- return GlobalCompilationDatabase::getFallbackCommand (File, StrongWorkspaceMode );
888- return Base->getFallbackCommand (File, StrongWorkspaceMode );
898+ return GlobalCompilationDatabase::getFallbackCommand (File);
899+ return Base->getFallbackCommand (File);
889900}
890901
891902bool DelegatingCDB::blockUntilIdle (Deadline D) const {
0 commit comments