Skip to content

Commit

Permalink
[clangd] Allow to override resource dir in ClangdServer.
Browse files Browse the repository at this point in the history
Reviewers: bkramer, krasimir, klimek

Reviewed By: klimek

Subscribers: klimek, cfe-commits

Tags: #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D34470

llvm-svn: 306530
  • Loading branch information
ilya-biryukov committed Jun 28, 2017
1 parent 7b3a38e commit a46f7a9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
15 changes: 11 additions & 4 deletions clang-tools-extra/clangd/ClangdServer.cpp
Expand Up @@ -33,6 +33,11 @@ std::vector<tooling::Replacement> formatCode(StringRef Code, StringRef Filename,
return std::vector<tooling::Replacement>(Result.begin(), Result.end());
}

std::string getStandardResourceDir() {
static int Dummy; // Just an address in this process.
return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
}

} // namespace

size_t clangd::positionToOffset(StringRef Code, Position P) {
Expand Down Expand Up @@ -141,8 +146,10 @@ void ClangdScheduler::addToEnd(std::function<void()> Request) {
ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB,
DiagnosticsConsumer &DiagConsumer,
FileSystemProvider &FSProvider,
bool RunSynchronously)
bool RunSynchronously,
llvm::Optional<StringRef> ResourceDir)
: CDB(CDB), DiagConsumer(DiagConsumer), FSProvider(FSProvider),
ResourceDir(ResourceDir ? ResourceDir->str() : getStandardResourceDir()),
PCHs(std::make_shared<PCHContainerOperations>()),
WorkScheduler(RunSynchronously) {}

Expand All @@ -158,7 +165,7 @@ void ClangdServer::addDocument(PathRef File, StringRef Contents) {
"No contents inside a file that was scheduled for reparse");
auto TaggedFS = FSProvider.getTaggedFileSystem(FileStr);
Units.runOnUnit(
FileStr, *FileContents.Draft, CDB, PCHs, TaggedFS.Value,
FileStr, *FileContents.Draft, ResourceDir, CDB, PCHs, TaggedFS.Value,
[&](ClangdUnit const &Unit) {
DiagConsumer.onDiagnosticsReady(
FileStr, make_tagged(Unit.getLocalDiagnostics(), TaggedFS.Tag));
Expand Down Expand Up @@ -201,8 +208,8 @@ ClangdServer::codeComplete(PathRef File, Position Pos,
// It would be nice to use runOnUnitWithoutReparse here, but we can't
// guarantee the correctness of code completion cache here if we don't do the
// reparse.
Units.runOnUnit(File, *OverridenContents, CDB, PCHs, TaggedFS.Value,
[&](ClangdUnit &Unit) {
Units.runOnUnit(File, *OverridenContents, ResourceDir, CDB, PCHs,
TaggedFS.Value, [&](ClangdUnit &Unit) {
Result = Unit.codeComplete(*OverridenContents, Pos,
TaggedFS.Value);
});
Expand Down
8 changes: 7 additions & 1 deletion clang-tools-extra/clangd/ClangdServer.h
Expand Up @@ -150,9 +150,14 @@ class ClangdServer {
/// a vfs::FileSystem provided by \p FSProvider. Results of code
/// completion/diagnostics also include a tag, that \p FSProvider returns
/// along with the vfs::FileSystem.
/// When \p ResourceDir is set, it will be used to search for internal headers
/// (overriding defaults and -resource-dir compiler flag, if set). If \p
/// ResourceDir is None, ClangdServer will attempt to set it to a standard
/// location, obtained via CompilerInvocation::GetResourcePath.
ClangdServer(GlobalCompilationDatabase &CDB,
DiagnosticsConsumer &DiagConsumer,
FileSystemProvider &FSProvider, bool RunSynchronously);
FileSystemProvider &FSProvider, bool RunSynchronously,
llvm::Optional<StringRef> ResourceDir = llvm::None);

/// Add a \p File to the list of tracked C++ files or update the contents if
/// \p File is already tracked. Also schedules parsing of the AST for it on a
Expand Down Expand Up @@ -199,6 +204,7 @@ class ClangdServer {
FileSystemProvider &FSProvider;
DraftStore DraftMgr;
ClangdUnitStore Units;
std::string ResourceDir;
std::shared_ptr<PCHContainerOperations> PCHs;
// WorkScheduler has to be the last member, because its destructor has to be
// called before all other members to stop the worker thread that references
Expand Down
6 changes: 2 additions & 4 deletions clang-tools-extra/clangd/ClangdUnit.cpp
Expand Up @@ -19,6 +19,7 @@ using namespace clang::clangd;
using namespace clang;

ClangdUnit::ClangdUnit(PathRef FileName, StringRef Contents,
StringRef ResourceDir,
std::shared_ptr<PCHContainerOperations> PCHs,
std::vector<tooling::CompileCommand> Commands,
IntrusiveRefCntPtr<vfs::FileSystem> VFS)
Expand All @@ -27,10 +28,7 @@ ClangdUnit::ClangdUnit(PathRef FileName, StringRef Contents,

// Inject the resource dir.
// FIXME: Don't overwrite it if it's already there.
static int Dummy; // Just an address in this process.
std::string ResourceDir =
CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
Commands.front().CommandLine.push_back("-resource-dir=" + ResourceDir);
Commands.front().CommandLine.push_back("-resource-dir=" + std::string(ResourceDir));

IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
CompilerInstance::createDiagnostics(new DiagnosticOptions);
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/ClangdUnit.h
Expand Up @@ -44,7 +44,7 @@ struct DiagWithFixIts {
/// would want to perform on parsed C++ files.
class ClangdUnit {
public:
ClangdUnit(PathRef FileName, StringRef Contents,
ClangdUnit(PathRef FileName, StringRef Contents, StringRef ResourceDir,
std::shared_ptr<PCHContainerOperations> PCHs,
std::vector<tooling::CompileCommand> Commands,
IntrusiveRefCntPtr<vfs::FileSystem> VFS);
Expand Down
23 changes: 13 additions & 10 deletions clang-tools-extra/clangd/ClangdUnitStore.h
Expand Up @@ -30,12 +30,13 @@ class ClangdUnitStore {
/// store, ClangdUnit::reparse will be called with the new contents before
/// running \p Action.
template <class Func>
void runOnUnit(PathRef File, StringRef FileContents,
void runOnUnit(PathRef File, StringRef FileContents, StringRef ResourceDir,
GlobalCompilationDatabase &CDB,
std::shared_ptr<PCHContainerOperations> PCHs,
IntrusiveRefCntPtr<vfs::FileSystem> VFS, Func Action) {
runOnUnitImpl(File, FileContents, CDB, PCHs, /*ReparseBeforeAction=*/true,
VFS, std::forward<Func>(Action));
runOnUnitImpl(File, FileContents, ResourceDir, CDB, PCHs,
/*ReparseBeforeAction=*/true, VFS,
std::forward<Func>(Action));
}

/// Run specified \p Action on the ClangdUnit for \p File.
Expand All @@ -44,18 +45,19 @@ class ClangdUnitStore {
/// store, the \p Action will be run directly on it.
template <class Func>
void runOnUnitWithoutReparse(PathRef File, StringRef FileContents,
StringRef ResourceDir,
GlobalCompilationDatabase &CDB,
std::shared_ptr<PCHContainerOperations> PCHs,
IntrusiveRefCntPtr<vfs::FileSystem> VFS,
Func Action) {
runOnUnitImpl(File, FileContents, CDB, PCHs, /*ReparseBeforeAction=*/false,
VFS, std::forward<Func>(Action));
runOnUnitImpl(File, FileContents, ResourceDir, CDB, PCHs,
/*ReparseBeforeAction=*/false, VFS,
std::forward<Func>(Action));
}

/// Run the specified \p Action on the ClangdUnit for \p File.
/// Unit for \p File should exist in the store.
template <class Func>
void runOnExistingUnit(PathRef File, Func Action) {
template <class Func> void runOnExistingUnit(PathRef File, Func Action) {
std::lock_guard<std::mutex> Lock(Mutex);

auto It = OpenedFiles.find(File);
Expand All @@ -71,7 +73,7 @@ class ClangdUnitStore {
/// Run specified \p Action on the ClangdUnit for \p File.
template <class Func>
void runOnUnitImpl(PathRef File, StringRef FileContents,
GlobalCompilationDatabase &CDB,
StringRef ResourceDir, GlobalCompilationDatabase &CDB,
std::shared_ptr<PCHContainerOperations> PCHs,
bool ReparseBeforeAction,
IntrusiveRefCntPtr<vfs::FileSystem> VFS, Func Action) {
Expand All @@ -85,8 +87,9 @@ class ClangdUnitStore {
auto It = OpenedFiles.find(File);
if (It == OpenedFiles.end()) {
It = OpenedFiles
.insert(std::make_pair(
File, ClangdUnit(File, FileContents, PCHs, Commands, VFS)))
.insert(std::make_pair(File, ClangdUnit(File, FileContents,
ResourceDir, PCHs,
Commands, VFS)))
.first;
} else if (ReparseBeforeAction) {
It->second.reparse(FileContents, VFS);
Expand Down

0 comments on commit a46f7a9

Please sign in to comment.