Skip to content

Commit 82f80c0

Browse files
committed
[clangd] Replace --project-root with --strong-workspace-mode
1 parent d0b4357 commit 82f80c0

File tree

8 files changed

+34
-30
lines changed

8 files changed

+34
-30
lines changed

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ ClangdServer::Options::operator TUScheduler::Options() const {
208208
Opts.UpdateDebounce = UpdateDebounce;
209209
Opts.ContextProvider = ContextProvider;
210210
Opts.PreambleThrottler = PreambleThrottler;
211-
Opts.FallbackProjectRoot = FallbackProjectRoot;
211+
Opts.StrongWorkspaceMode = StrongWorkspaceMode;
212212
return Opts;
213213
}
214214

clang-tools-extra/clangd/ClangdServer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ class ClangdServer {
152152
/// FIXME: If not set, should use the current working directory.
153153
std::optional<std::string> WorkspaceRoot;
154154

155-
/// If set, fallback command uses this path as its current working directory
156-
/// instead of the file's parent path.
157-
std::optional<std::string> FallbackProjectRoot;
155+
/// Sets an alterante mode of operation. Current effects are:
156+
/// - Using the current working directory as the working directory for fallback commands
157+
bool StrongWorkspaceMode;
158158

159159
/// The resource directory is used to find internal headers, overriding
160160
/// defaults and -resource-dir compiler flag).

clang-tools-extra/clangd/GlobalCompilationDatabase.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void actOnAllParentDirectories(PathRef FileName,
5555
} // namespace
5656

5757
tooling::CompileCommand
58-
GlobalCompilationDatabase::getFallbackCommand(PathRef File, std::optional<std::string> ProjectRoot) const {
58+
GlobalCompilationDatabase::getFallbackCommand(PathRef File, bool StrongWorkspaceMode) 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,7 +64,12 @@ GlobalCompilationDatabase::getFallbackCommand(PathRef File, std::optional<std::s
6464
if (FileExtension.empty() || FileExtension == ".h")
6565
Argv.push_back("-xobjective-c++-header");
6666
Argv.push_back(std::string(File));
67-
tooling::CompileCommand Cmd(ProjectRoot ? *ProjectRoot : llvm::sys::path::parent_path(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,
6873
llvm::sys::path::filename(File), std::move(Argv),
6974
/*Output=*/"");
7075
Cmd.Heuristic = "clangd fallback";
@@ -797,8 +802,8 @@ OverlayCDB::getCompileCommand(PathRef File) const {
797802
return Cmd;
798803
}
799804

800-
tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File, std::optional<std::string> ProjectRoot) const {
801-
auto Cmd = DelegatingCDB::getFallbackCommand(File, ProjectRoot);
805+
tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File, bool StrongWorkspaceMode) const {
806+
auto Cmd = DelegatingCDB::getFallbackCommand(File, StrongWorkspaceMode);
802807
std::lock_guard<std::mutex> Lock(Mutex);
803808
Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
804809
FallbackFlags.end());
@@ -877,10 +882,10 @@ DelegatingCDB::getProjectModules(PathRef File) const {
877882
return Base->getProjectModules(File);
878883
}
879884

880-
tooling::CompileCommand DelegatingCDB::getFallbackCommand(PathRef File, std::optional<std::string> ProjectRoot) const {
885+
tooling::CompileCommand DelegatingCDB::getFallbackCommand(PathRef File, bool StrongWorkspaceMode) const {
881886
if (!Base)
882-
return GlobalCompilationDatabase::getFallbackCommand(File, ProjectRoot);
883-
return Base->getFallbackCommand(File, ProjectRoot);
887+
return GlobalCompilationDatabase::getFallbackCommand(File, StrongWorkspaceMode);
888+
return Base->getFallbackCommand(File, StrongWorkspaceMode);
884889
}
885890

886891
bool DelegatingCDB::blockUntilIdle(Deadline D) const {

clang-tools-extra/clangd/GlobalCompilationDatabase.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class GlobalCompilationDatabase {
5555
/// Makes a guess at how to build a file.
5656
/// The default implementation just runs clang on the file.
5757
/// Clangd should treat the results as unreliable.
58-
virtual tooling::CompileCommand getFallbackCommand(PathRef File, std::optional<std::string> ProjectRoot = std::nullopt) const;
58+
virtual tooling::CompileCommand getFallbackCommand(PathRef File, bool StrongWorkspaceMode = false) const;
5959

6060
/// If the CDB does any asynchronous work, wait for it to complete.
6161
/// For use in tests.
@@ -86,7 +86,7 @@ class DelegatingCDB : public GlobalCompilationDatabase {
8686
std::unique_ptr<ProjectModules>
8787
getProjectModules(PathRef File) const override;
8888

89-
tooling::CompileCommand getFallbackCommand(PathRef File, std::optional<std::string> ProjectRoot = std::nullopt) const override;
89+
tooling::CompileCommand getFallbackCommand(PathRef File, bool StrongWorkspaceMode = false) const override;
9090

9191
bool blockUntilIdle(Deadline D) const override;
9292

@@ -200,7 +200,7 @@ class OverlayCDB : public DelegatingCDB {
200200

201201
std::optional<tooling::CompileCommand>
202202
getCompileCommand(PathRef File) const override;
203-
tooling::CompileCommand getFallbackCommand(PathRef File, std::optional<std::string> ProjectRoot = std::nullopt) const override;
203+
tooling::CompileCommand getFallbackCommand(PathRef File, bool StrongWorkspaceMode = false) const override;
204204

205205
/// Sets or clears the compilation command for a particular file.
206206
/// Returns true if the command was changed (including insertion and removal),

clang-tools-extra/clangd/TUScheduler.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ class ASTWorker {
723723
const GlobalCompilationDatabase &CDB;
724724
/// Callback invoked when preamble or main file AST is built.
725725
ParsingCallbacks &Callbacks;
726-
std::optional<std::string> FallbackProjectRoot;
726+
bool StrongWorkspaceMode;
727727

728728
Semaphore &Barrier;
729729
/// Whether the 'onMainAST' callback ran for the current FileInputs.
@@ -841,14 +841,14 @@ ASTWorker::ASTWorker(PathRef FileName, const GlobalCompilationDatabase &CDB,
841841
: IdleASTs(LRUCache), HeaderIncluders(HeaderIncluders), RunSync(RunSync),
842842
UpdateDebounce(Opts.UpdateDebounce), FileName(FileName),
843843
ContextProvider(Opts.ContextProvider), CDB(CDB), Callbacks(Callbacks),
844-
FallbackProjectRoot(Opts.FallbackProjectRoot),
844+
StrongWorkspaceMode(Opts.StrongWorkspaceMode),
845845
Barrier(Barrier), Done(false), Status(FileName, Callbacks),
846846
PreamblePeer(FileName, Callbacks, Opts.StorePreamblesInMemory, RunSync,
847847
Opts.PreambleThrottler, Status, HeaderIncluders, *this) {
848848
// Set a fallback command because compile command can be accessed before
849849
// `Inputs` is initialized. Other fields are only used after initialization
850850
// from client inputs.
851-
FileInputs.CompileCommand = CDB.getFallbackCommand(FileName, FallbackProjectRoot);
851+
FileInputs.CompileCommand = CDB.getFallbackCommand(FileName, StrongWorkspaceMode);
852852
}
853853

854854
ASTWorker::~ASTWorker() {
@@ -890,7 +890,7 @@ void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags,
890890
if (Cmd)
891891
Inputs.CompileCommand = std::move(*Cmd);
892892
else
893-
Inputs.CompileCommand = CDB.getFallbackCommand(FileName, FallbackProjectRoot);
893+
Inputs.CompileCommand = CDB.getFallbackCommand(FileName, StrongWorkspaceMode);
894894

895895
bool InputsAreTheSame =
896896
std::tie(FileInputs.CompileCommand, FileInputs.Contents) ==

clang-tools-extra/clangd/TUScheduler.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,8 @@ class TUScheduler {
237237
/// If the path is empty, context sholud be "generic".
238238
std::function<Context(PathRef)> ContextProvider;
239239

240-
/// If set, fallback command uses this path as its current working directory
241-
/// instead of the file's parent path.
242-
std::optional<std::string> FallbackProjectRoot;
240+
/// Sets an alterante mode of operation. See ClangdServer::Options::StrongWorkspaceMode.
241+
bool StrongWorkspaceMode;
243242
};
244243

245244
TUScheduler(const GlobalCompilationDatabase &CDB, const Options &Opts,

clang-tools-extra/clangd/tool/Check.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class Checker {
187187
Cmd.Heuristic.empty() ? "from CDB" : Cmd.Heuristic, Cmd.Directory,
188188
printArgv(Cmd.CommandLine));
189189
} else {
190-
Cmd = CDB->getFallbackCommand(File, Opts.FallbackProjectRoot);
190+
Cmd = CDB->getFallbackCommand(File, Opts.StrongWorkspaceMode);
191191
log("Generic fallback command is: [{0}] {1}", Cmd.Directory,
192192
printArgv(Cmd.CommandLine));
193193
}

clang-tools-extra/clangd/tool/ClangdMain.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,13 @@ opt<bool> EnableConfig{
500500
init(true),
501501
};
502502

503-
opt<Path> ProjectRoot{
504-
"project-root",
505-
cat(Misc),
506-
desc("Path to use as the current working directory for fallback commands."),
507-
init(""),
508-
ValueOptional,
503+
opt<bool> StrongWorkspaceMode{
504+
"strong-workspace-mode",
505+
cat(Features),
506+
desc(
507+
"An alternate mode of operation for clangd, operating more closely to the workspace.\n"
508+
"When enabled, fallback commands use the workspace directory as their working directory instead of the parent folder."),
509+
init(false),
509510
};
510511

511512
opt<bool> UseDirtyHeaders{"use-dirty-headers", cat(Misc),
@@ -915,8 +916,7 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var
915916
}
916917
if (!ResourceDir.empty())
917918
Opts.ResourceDir = ResourceDir;
918-
if (!ProjectRoot.empty())
919-
Opts.FallbackProjectRoot = ProjectRoot;
919+
Opts.StrongWorkspaceMode = StrongWorkspaceMode;
920920
Opts.BuildDynamicSymbolIndex = true;
921921
#if CLANGD_ENABLE_REMOTE
922922
if (RemoteIndexAddress.empty() != ProjectRoot.empty()) {

0 commit comments

Comments
 (0)