summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIlya Biryukov <ibiryukov@google.com>2017-06-28 10:34:50 +0000
committerIlya Biryukov <ibiryukov@google.com>2017-06-28 10:34:50 +0000
commita46f7a9bf44a6da745e36aa86114fe58ce07d35f (patch)
tree68ae462cc62025b638b04da5cc4805b9c2afe476
parent7b3a38ec306c49861869dc9d2a2bd99af82b3280 (diff)
downloadbcm5719-llvm-a46f7a9bf44a6da745e36aa86114fe58ce07d35f.tar.gz
bcm5719-llvm-a46f7a9bf44a6da745e36aa86114fe58ce07d35f.zip
[clangd] Allow to override resource dir in ClangdServer.
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
-rw-r--r--clang-tools-extra/clangd/ClangdServer.cpp15
-rw-r--r--clang-tools-extra/clangd/ClangdServer.h8
-rw-r--r--clang-tools-extra/clangd/ClangdUnit.cpp6
-rw-r--r--clang-tools-extra/clangd/ClangdUnit.h2
-rw-r--r--clang-tools-extra/clangd/ClangdUnitStore.h23
5 files changed, 34 insertions, 20 deletions
diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp
index 73c1ae548f4..490780a42f3 100644
--- a/clang-tools-extra/clangd/ClangdServer.cpp
+++ b/clang-tools-extra/clangd/ClangdServer.cpp
@@ -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) {
@@ -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) {}
@@ -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));
@@ -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);
});
diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h
index e989842cada..a8eba812540 100644
--- a/clang-tools-extra/clangd/ClangdServer.h
+++ b/clang-tools-extra/clangd/ClangdServer.h
@@ -150,9 +150,14 @@ public:
/// 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
@@ -199,6 +204,7 @@ private:
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
diff --git a/clang-tools-extra/clangd/ClangdUnit.cpp b/clang-tools-extra/clangd/ClangdUnit.cpp
index 218b2c75be2..b1e0d4955d7 100644
--- a/clang-tools-extra/clangd/ClangdUnit.cpp
+++ b/clang-tools-extra/clangd/ClangdUnit.cpp
@@ -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)
@@ -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);
diff --git a/clang-tools-extra/clangd/ClangdUnit.h b/clang-tools-extra/clangd/ClangdUnit.h
index a11ca024c7c..736a8e2bc48 100644
--- a/clang-tools-extra/clangd/ClangdUnit.h
+++ b/clang-tools-extra/clangd/ClangdUnit.h
@@ -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);
diff --git a/clang-tools-extra/clangd/ClangdUnitStore.h b/clang-tools-extra/clangd/ClangdUnitStore.h
index baca85c4372..c980ba7d51a 100644
--- a/clang-tools-extra/clangd/ClangdUnitStore.h
+++ b/clang-tools-extra/clangd/ClangdUnitStore.h
@@ -30,12 +30,13 @@ public:
/// 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.
@@ -44,18 +45,19 @@ public:
/// 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);
@@ -71,7 +73,7 @@ private:
/// 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) {
@@ -85,8 +87,9 @@ private:
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);
OpenPOWER on IntegriCloud