[clang-tools-extra] r306530 - [clangd] Allow to override resource dir in ClangdServer.

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 28 03:34:50 PDT 2017


Author: ibiryukov
Date: Wed Jun 28 03:34:50 2017
New Revision: 306530

URL: http://llvm.org/viewvc/llvm-project?rev=306530&view=rev
Log:
[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

Modified:
    clang-tools-extra/trunk/clangd/ClangdServer.cpp
    clang-tools-extra/trunk/clangd/ClangdServer.h
    clang-tools-extra/trunk/clangd/ClangdUnit.cpp
    clang-tools-extra/trunk/clangd/ClangdUnit.h
    clang-tools-extra/trunk/clangd/ClangdUnitStore.h

Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=306530&r1=306529&r2=306530&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Wed Jun 28 03:34:50 2017
@@ -33,6 +33,11 @@ std::vector<tooling::Replacement> format
   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::func
 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 F
            "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,
   // 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);
                   });

Modified: clang-tools-extra/trunk/clangd/ClangdServer.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.h?rev=306530&r1=306529&r2=306530&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdServer.h (original)
+++ clang-tools-extra/trunk/clangd/ClangdServer.h Wed Jun 28 03:34:50 2017
@@ -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

Modified: clang-tools-extra/trunk/clangd/ClangdUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdUnit.cpp?rev=306530&r1=306529&r2=306530&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdUnit.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdUnit.cpp Wed Jun 28 03:34:50 2017
@@ -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,
 
   // 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);

Modified: clang-tools-extra/trunk/clangd/ClangdUnit.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdUnit.h?rev=306530&r1=306529&r2=306530&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdUnit.h (original)
+++ clang-tools-extra/trunk/clangd/ClangdUnit.h Wed Jun 28 03:34:50 2017
@@ -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);

Modified: clang-tools-extra/trunk/clangd/ClangdUnitStore.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdUnitStore.h?rev=306530&r1=306529&r2=306530&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdUnitStore.h (original)
+++ clang-tools-extra/trunk/clangd/ClangdUnitStore.h Wed Jun 28 03:34:50 2017
@@ -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);




More information about the cfe-commits mailing list