[clang-tools-extra] r369005 - [clangd] Don't use Bind() where C++14 move capture works

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 15 07:16:06 PDT 2019


Author: d0k
Date: Thu Aug 15 07:16:06 2019
New Revision: 369005

URL: http://llvm.org/viewvc/llvm-project?rev=369005&view=rev
Log:
[clangd] Don't use Bind() where C++14 move capture works

Modified:
    clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
    clang-tools-extra/trunk/clangd/ClangdLSPServer.h
    clang-tools-extra/trunk/clangd/ClangdServer.cpp
    clang-tools-extra/trunk/clangd/TUScheduler.cpp
    clang-tools-extra/trunk/clangd/unittests/TUSchedulerTests.cpp

Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp?rev=369005&r1=369004&r2=369005&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp Thu Aug 15 07:16:06 2019
@@ -613,9 +613,10 @@ void ClangdLSPServer::onCommand(const Ex
           llvm::inconvertibleErrorCode(),
           "trying to apply a code action for a non-added file"));
 
-    auto Action = [this, ApplyEdit, ReplyAfterApplyingEdit](
-                      decltype(Reply) Reply, URIForFile File, std::string Code,
-                      llvm::Expected<Tweak::Effect> R) {
+    auto Action = [this, ApplyEdit, ReplyAfterApplyingEdit,
+                   Reply = std::move(Reply), File = Params.tweakArgs->file,
+                   Code = std::move(*Code)](
+                      llvm::Expected<Tweak::Effect> R) mutable {
       if (!R)
         return Reply(R.takeError());
 
@@ -636,8 +637,7 @@ void ClangdLSPServer::onCommand(const Ex
     };
     Server->applyTweak(Params.tweakArgs->file.file(),
                        Params.tweakArgs->selection, Params.tweakArgs->tweakID,
-                       Bind(Action, std::move(Reply), Params.tweakArgs->file,
-                            std::move(*Code)));
+                       std::move(Action));
   } else {
     // We should not get here because ExecuteCommandParams would not have
     // parsed in the first place and this handler should not be called. But if
@@ -653,17 +653,15 @@ void ClangdLSPServer::onWorkspaceSymbol(
     Callback<std::vector<SymbolInformation>> Reply) {
   Server->workspaceSymbols(
       Params.query, CCOpts.Limit,
-      Bind(
-          [this](decltype(Reply) Reply,
-                 llvm::Expected<std::vector<SymbolInformation>> Items) {
-            if (!Items)
-              return Reply(Items.takeError());
-            for (auto &Sym : *Items)
-              Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds);
-
-            Reply(std::move(*Items));
-          },
-          std::move(Reply)));
+      [Reply = std::move(Reply),
+       this](llvm::Expected<std::vector<SymbolInformation>> Items) mutable {
+        if (!Items)
+          return Reply(Items.takeError());
+        for (auto &Sym : *Items)
+          Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds);
+
+        Reply(std::move(*Items));
+      });
 }
 
 void ClangdLSPServer::onPrepareRename(const TextDocumentPositionParams &Params,
@@ -680,19 +678,16 @@ void ClangdLSPServer::onRename(const Ren
     return Reply(llvm::make_error<LSPError>(
         "onRename called for non-added file", ErrorCode::InvalidParams));
 
-  Server->rename(
-      File, Params.position, Params.newName, /*WantFormat=*/true,
-      Bind(
-          [File, Code, Params](decltype(Reply) Reply,
-                               llvm::Expected<std::vector<TextEdit>> Edits) {
-            if (!Edits)
-              return Reply(Edits.takeError());
-
-            WorkspaceEdit WE;
-            WE.changes = {{Params.textDocument.uri.uri(), *Edits}};
-            Reply(WE);
-          },
-          std::move(Reply)));
+  Server->rename(File, Params.position, Params.newName, /*WantFormat=*/true,
+                 [File, Code, Params, Reply = std::move(Reply)](
+                     llvm::Expected<std::vector<TextEdit>> Edits) mutable {
+                   if (!Edits)
+                     return Reply(Edits.takeError());
+
+                   WorkspaceEdit WE;
+                   WE.changes = {{Params.textDocument.uri.uri(), *Edits}};
+                   Reply(WE);
+                 });
 }
 
 void ClangdLSPServer::onDocumentDidClose(
@@ -796,18 +791,16 @@ void ClangdLSPServer::onDocumentSymbol(c
   URIForFile FileURI = Params.textDocument.uri;
   Server->documentSymbols(
       Params.textDocument.uri.file(),
-      Bind(
-          [this, FileURI](decltype(Reply) Reply,
-                          llvm::Expected<std::vector<DocumentSymbol>> Items) {
-            if (!Items)
-              return Reply(Items.takeError());
-            adjustSymbolKinds(*Items, SupportedSymbolKinds);
-            if (SupportsHierarchicalDocumentSymbol)
-              return Reply(std::move(*Items));
-            else
-              return Reply(flattenSymbolHierarchy(*Items, FileURI));
-          },
-          std::move(Reply)));
+      [this, FileURI, Reply = std::move(Reply)](
+          llvm::Expected<std::vector<DocumentSymbol>> Items) mutable {
+        if (!Items)
+          return Reply(Items.takeError());
+        adjustSymbolKinds(*Items, SupportedSymbolKinds);
+        if (SupportsHierarchicalDocumentSymbol)
+          return Reply(std::move(*Items));
+        else
+          return Reply(flattenSymbolHierarchy(*Items, FileURI));
+      });
 }
 
 static llvm::Optional<Command> asCommand(const CodeAction &Action) {
@@ -846,9 +839,9 @@ void ClangdLSPServer::onCodeAction(const
 
   // Now enumerate the semantic code actions.
   auto ConsumeActions =
-      [this](decltype(Reply) Reply, URIForFile File, std::string Code,
-             Range Selection, std::vector<CodeAction> FixIts,
-             llvm::Expected<std::vector<ClangdServer::TweakRef>> Tweaks) {
+      [Reply = std::move(Reply), File, Code = std::move(*Code),
+       Selection = Params.range, FixIts = std::move(FixIts), this](
+          llvm::Expected<std::vector<ClangdServer::TweakRef>> Tweaks) mutable {
         if (!Tweaks)
           return Reply(Tweaks.takeError());
 
@@ -867,10 +860,7 @@ void ClangdLSPServer::onCodeAction(const
         return Reply(llvm::json::Array(Commands));
       };
 
-  Server->enumerateTweaks(File.file(), Params.range,
-                          Bind(ConsumeActions, std::move(Reply), File,
-                               std::move(*Code), Params.range,
-                               std::move(FixIts)));
+  Server->enumerateTweaks(File.file(), Params.range, std::move(ConsumeActions));
 }
 
 void ClangdLSPServer::onCompletion(const CompletionParams &Params,
@@ -882,43 +872,39 @@ void ClangdLSPServer::onCompletion(const
     return Reply(CompletionList());
   }
   Server->codeComplete(Params.textDocument.uri.file(), Params.position, CCOpts,
-                       Bind(
-                           [this](decltype(Reply) Reply,
-                                  llvm::Expected<CodeCompleteResult> List) {
-                             if (!List)
-                               return Reply(List.takeError());
-                             CompletionList LSPList;
-                             LSPList.isIncomplete = List->HasMore;
-                             for (const auto &R : List->Completions) {
-                               CompletionItem C = R.render(CCOpts);
-                               C.kind = adjustKindToCapability(
-                                   C.kind, SupportedCompletionItemKinds);
-                               LSPList.items.push_back(std::move(C));
-                             }
-                             return Reply(std::move(LSPList));
-                           },
-                           std::move(Reply)));
+                       [Reply = std::move(Reply),
+                        this](llvm::Expected<CodeCompleteResult> List) mutable {
+                         if (!List)
+                           return Reply(List.takeError());
+                         CompletionList LSPList;
+                         LSPList.isIncomplete = List->HasMore;
+                         for (const auto &R : List->Completions) {
+                           CompletionItem C = R.render(CCOpts);
+                           C.kind = adjustKindToCapability(
+                               C.kind, SupportedCompletionItemKinds);
+                           LSPList.items.push_back(std::move(C));
+                         }
+                         return Reply(std::move(LSPList));
+                       });
 }
 
 void ClangdLSPServer::onSignatureHelp(const TextDocumentPositionParams &Params,
                                       Callback<SignatureHelp> Reply) {
   Server->signatureHelp(Params.textDocument.uri.file(), Params.position,
-                        Bind(
-                            [this](decltype(Reply) Reply,
-                                   llvm::Expected<SignatureHelp> Signature) {
-                              if (!Signature)
-                                return Reply(Signature.takeError());
-                              if (SupportsOffsetsInSignatureHelp)
-                                return Reply(std::move(*Signature));
-                              // Strip out the offsets from signature help for
-                              // clients that only support string labels.
-                              for (auto &SigInfo : Signature->signatures) {
-                                for (auto &Param : SigInfo.parameters)
-                                  Param.labelOffsets.reset();
-                              }
-                              return Reply(std::move(*Signature));
-                            },
-                            std::move(Reply)));
+                        [Reply = std::move(Reply), this](
+                            llvm::Expected<SignatureHelp> Signature) mutable {
+                          if (!Signature)
+                            return Reply(Signature.takeError());
+                          if (SupportsOffsetsInSignatureHelp)
+                            return Reply(std::move(*Signature));
+                          // Strip out the offsets from signature help for
+                          // clients that only support string labels.
+                          for (auto &SigInfo : Signature->signatures) {
+                            for (auto &Param : SigInfo.parameters)
+                              Param.labelOffsets.reset();
+                          }
+                          return Reply(std::move(*Signature));
+                        });
 }
 
 // Go to definition has a toggle function: if def and decl are distinct, then
@@ -945,20 +931,18 @@ void ClangdLSPServer::onGoToDefinition(c
                                        Callback<std::vector<Location>> Reply) {
   Server->locateSymbolAt(
       Params.textDocument.uri.file(), Params.position,
-      Bind(
-          [&, Params](decltype(Reply) Reply,
-                      llvm::Expected<std::vector<LocatedSymbol>> Symbols) {
-            if (!Symbols)
-              return Reply(Symbols.takeError());
-            std::vector<Location> Defs;
-            for (auto &S : *Symbols) {
-              if (Location *Toggle = getToggle(Params, S))
-                return Reply(std::vector<Location>{std::move(*Toggle)});
-              Defs.push_back(S.Definition.getValueOr(S.PreferredDeclaration));
-            }
-            Reply(std::move(Defs));
-          },
-          std::move(Reply)));
+      [Params, Reply = std::move(Reply)](
+          llvm::Expected<std::vector<LocatedSymbol>> Symbols) mutable {
+        if (!Symbols)
+          return Reply(Symbols.takeError());
+        std::vector<Location> Defs;
+        for (auto &S : *Symbols) {
+          if (Location *Toggle = getToggle(Params, S))
+            return Reply(std::vector<Location>{std::move(*Toggle)});
+          Defs.push_back(S.Definition.getValueOr(S.PreferredDeclaration));
+        }
+        Reply(std::move(Defs));
+      });
 }
 
 void ClangdLSPServer::onGoToDeclaration(
@@ -966,20 +950,18 @@ void ClangdLSPServer::onGoToDeclaration(
     Callback<std::vector<Location>> Reply) {
   Server->locateSymbolAt(
       Params.textDocument.uri.file(), Params.position,
-      Bind(
-          [&, Params](decltype(Reply) Reply,
-                      llvm::Expected<std::vector<LocatedSymbol>> Symbols) {
-            if (!Symbols)
-              return Reply(Symbols.takeError());
-            std::vector<Location> Decls;
-            for (auto &S : *Symbols) {
-              if (Location *Toggle = getToggle(Params, S))
-                return Reply(std::vector<Location>{std::move(*Toggle)});
-              Decls.push_back(std::move(S.PreferredDeclaration));
-            }
-            Reply(std::move(Decls));
-          },
-          std::move(Reply)));
+      [Params, Reply = std::move(Reply)](
+          llvm::Expected<std::vector<LocatedSymbol>> Symbols) mutable {
+        if (!Symbols)
+          return Reply(Symbols.takeError());
+        std::vector<Location> Decls;
+        for (auto &S : *Symbols) {
+          if (Location *Toggle = getToggle(Params, S))
+            return Reply(std::vector<Location>{std::move(*Toggle)});
+          Decls.push_back(std::move(S.PreferredDeclaration));
+        }
+        Reply(std::move(Decls));
+      });
 }
 
 void ClangdLSPServer::onSwitchSourceHeader(
@@ -1001,30 +983,26 @@ void ClangdLSPServer::onDocumentHighligh
 void ClangdLSPServer::onHover(const TextDocumentPositionParams &Params,
                               Callback<llvm::Optional<Hover>> Reply) {
   Server->findHover(Params.textDocument.uri.file(), Params.position,
-                    Bind(
-                        [this](decltype(Reply) Reply,
-                               llvm::Expected<llvm::Optional<HoverInfo>> H) {
-                          if (!H)
-                            return Reply(H.takeError());
-                          if (!*H)
-                            return Reply(llvm::None);
-
-                          Hover R;
-                          R.contents.kind = HoverContentFormat;
-                          R.range = (*H)->SymRange;
-                          switch (HoverContentFormat) {
-                          case MarkupKind::PlainText:
-                            R.contents.value =
-                                (*H)->present().renderAsPlainText();
-                            return Reply(std::move(R));
-                          case MarkupKind::Markdown:
-                            R.contents.value =
-                                (*H)->present().renderAsMarkdown();
-                            return Reply(std::move(R));
-                          };
-                          llvm_unreachable("unhandled MarkupKind");
-                        },
-                        std::move(Reply)));
+                    [Reply = std::move(Reply), this](
+                        llvm::Expected<llvm::Optional<HoverInfo>> H) mutable {
+                      if (!H)
+                        return Reply(H.takeError());
+                      if (!*H)
+                        return Reply(llvm::None);
+
+                      Hover R;
+                      R.contents.kind = HoverContentFormat;
+                      R.range = (*H)->SymRange;
+                      switch (HoverContentFormat) {
+                      case MarkupKind::PlainText:
+                        R.contents.value = (*H)->present().renderAsPlainText();
+                        return Reply(std::move(R));
+                      case MarkupKind::Markdown:
+                        R.contents.value = (*H)->present().renderAsMarkdown();
+                        return Reply(std::move(R));
+                      };
+                      llvm_unreachable("unhandled MarkupKind");
+                    });
 }
 
 void ClangdLSPServer::onTypeHierarchy(

Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.h?rev=369005&r1=369004&r2=369005&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdLSPServer.h (original)
+++ clang-tools-extra/trunk/clangd/ClangdLSPServer.h Thu Aug 15 07:16:06 2019
@@ -154,21 +154,21 @@ private:
   template <typename Response>
   void call(StringRef Method, llvm::json::Value Params, Callback<Response> CB) {
     // Wrap the callback with LSP conversion and error-handling.
-    auto HandleReply = [](decltype(CB) CB,
-                          llvm::Expected<llvm::json::Value> RawResponse) {
-      Response Rsp;
-      if (!RawResponse) {
-        CB(RawResponse.takeError());
-      } else if (fromJSON(*RawResponse, Rsp)) {
-        CB(std::move(Rsp));
-      } else {
-        elog("Failed to decode {0} response", *RawResponse);
-        CB(llvm::make_error<LSPError>("failed to decode reponse",
-                                      ErrorCode::InvalidParams));
-      }
-    };
-    callRaw(Method, std::move(Params),
-            Bind(std::move(HandleReply), std::move(CB)));
+    auto HandleReply =
+        [CB = std::move(CB)](
+            llvm::Expected<llvm::json::Value> RawResponse) mutable {
+          Response Rsp;
+          if (!RawResponse) {
+            CB(RawResponse.takeError());
+          } else if (fromJSON(*RawResponse, Rsp)) {
+            CB(std::move(Rsp));
+          } else {
+            elog("Failed to decode {0} response", *RawResponse);
+            CB(llvm::make_error<LSPError>("failed to decode reponse",
+                                          ErrorCode::InvalidParams));
+          }
+        };
+    callRaw(Method, std::move(Params), std::move(HandleReply));
   }
   void callRaw(StringRef Method, llvm::json::Value Params,
                Callback<llvm::json::Value> CB);

Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=369005&r1=369004&r2=369005&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Thu Aug 15 07:16:06 2019
@@ -190,10 +190,9 @@ void ClangdServer::codeComplete(PathRef
   if (!CodeCompleteOpts.Index) // Respect overridden index.
     CodeCompleteOpts.Index = Index;
 
-  auto FS = FSProvider.getFileSystem();
-  auto Task = [Pos, FS, CodeCompleteOpts,
-               this](Path File, Callback<CodeCompleteResult> CB,
-                     llvm::Expected<InputsAndPreamble> IP) {
+  auto Task = [Pos, FS = FSProvider.getFileSystem(), CodeCompleteOpts,
+               File = File.str(), CB = std::move(CB),
+               this](llvm::Expected<InputsAndPreamble> IP) mutable {
     if (!IP)
       return CB(IP.takeError());
     if (isCancelled())
@@ -241,16 +240,15 @@ void ClangdServer::codeComplete(PathRef
       (Opts.RunParser == CodeCompleteOptions::AlwaysParse)
           ? TUScheduler::Stale
           : TUScheduler::StaleOrAbsent,
-      Bind(Task, File.str(), std::move(CB)));
+      std::move(Task));
 }
 
 void ClangdServer::signatureHelp(PathRef File, Position Pos,
                                  Callback<SignatureHelp> CB) {
 
-  auto FS = FSProvider.getFileSystem();
-  auto *Index = this->Index;
-  auto Action = [Pos, FS, Index](Path File, Callback<SignatureHelp> CB,
-                                 llvm::Expected<InputsAndPreamble> IP) {
+  auto Action = [Pos, FS = FSProvider.getFileSystem(), File = File.str(),
+                 CB = std::move(CB),
+                 this](llvm::Expected<InputsAndPreamble> IP) mutable {
     if (!IP)
       return CB(IP.takeError());
 
@@ -264,7 +262,7 @@ void ClangdServer::signatureHelp(PathRef
   // completion inserted a header to make the symbol available, then using
   // the old preamble would yield useless results.
   WorkScheduler.runWithPreamble("SignatureHelp", File, TUScheduler::Consistent,
-                                Bind(Action, File.str(), std::move(CB)));
+                                std::move(Action));
 }
 
 llvm::Expected<tooling::Replacements>
@@ -305,8 +303,8 @@ ClangdServer::formatOnType(llvm::StringR
 
 void ClangdServer::prepareRename(PathRef File, Position Pos,
                                  Callback<llvm::Optional<Range>> CB) {
-  auto Action = [Pos, this](Path File, Callback<llvm::Optional<Range>> CB,
-                            llvm::Expected<InputsAndAST> InpAST) {
+  auto Action = [Pos, File = File.str(), CB = std::move(CB),
+                 this](llvm::Expected<InputsAndAST> InpAST) mutable {
     if (!InpAST)
       return CB(InpAST.takeError());
     auto &AST = InpAST->AST;
@@ -328,15 +326,14 @@ void ClangdServer::prepareRename(PathRef
     // Return null if the "rename" is not valid at the position.
     CB(llvm::None);
   };
-  WorkScheduler.runWithAST("PrepareRename", File,
-                           Bind(Action, File.str(), std::move(CB)));
+  WorkScheduler.runWithAST("PrepareRename", File, std::move(Action));
 }
 
 void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
                           bool WantFormat, Callback<std::vector<TextEdit>> CB) {
-  auto Action = [Pos, WantFormat, this](Path File, std::string NewName,
-                                        Callback<std::vector<TextEdit>> CB,
-                                        llvm::Expected<InputsAndAST> InpAST) {
+  auto Action = [File = File.str(), NewName = NewName.str(), Pos, WantFormat,
+                 CB = std::move(CB),
+                 this](llvm::Expected<InputsAndAST> InpAST) mutable {
     if (!InpAST)
       return CB(InpAST.takeError());
     auto Changes = renameWithinFile(InpAST->AST, File, Pos, NewName, Index);
@@ -359,8 +356,7 @@ void ClangdServer::rename(PathRef File,
     return CB(std::move(Edits));
   };
 
-  WorkScheduler.runWithAST(
-      "Rename", File, Bind(Action, File.str(), NewName.str(), std::move(CB)));
+  WorkScheduler.runWithAST("Rename", File, std::move(Action));
 }
 
 static llvm::Expected<Tweak::Selection>
@@ -376,8 +372,8 @@ tweakSelection(const Range &Sel, const I
 
 void ClangdServer::enumerateTweaks(PathRef File, Range Sel,
                                    Callback<std::vector<TweakRef>> CB) {
-  auto Action = [this, Sel](decltype(CB) CB, std::string File,
-                            Expected<InputsAndAST> InpAST) {
+  auto Action = [File = File.str(), Sel, CB = std::move(CB),
+                 this](Expected<InputsAndAST> InpAST) mutable {
     if (!InpAST)
       return CB(InpAST.takeError());
     auto Selection = tweakSelection(Sel, *InpAST);
@@ -390,14 +386,13 @@ void ClangdServer::enumerateTweaks(PathR
     CB(std::move(Res));
   };
 
-  WorkScheduler.runWithAST("EnumerateTweaks", File,
-                           Bind(Action, std::move(CB), File.str()));
+  WorkScheduler.runWithAST("EnumerateTweaks", File, std::move(Action));
 }
 
 void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
                               Callback<Tweak::Effect> CB) {
-  auto Action = [Sel](decltype(CB) CB, std::string File, std::string TweakID,
-                      Expected<InputsAndAST> InpAST) {
+  auto Action = [File = File.str(), Sel, TweakID = TweakID.str(),
+                 CB = std::move(CB)](Expected<InputsAndAST> InpAST) mutable {
     if (!InpAST)
       return CB(InpAST.takeError());
     auto Selection = tweakSelection(Sel, *InpAST);
@@ -422,15 +417,13 @@ void ClangdServer::applyTweak(PathRef Fi
     }
     return CB(std::move(*Effect));
   };
-  WorkScheduler.runWithAST(
-      "ApplyTweak", File,
-      Bind(Action, std::move(CB), File.str(), TweakID.str()));
+  WorkScheduler.runWithAST("ApplyTweak", File, std::move(Action));
 }
 
 void ClangdServer::dumpAST(PathRef File,
                            llvm::unique_function<void(std::string)> Callback) {
-  auto Action = [](decltype(Callback) Callback,
-                   llvm::Expected<InputsAndAST> InpAST) {
+  auto Action = [Callback = std::move(Callback)](
+                    llvm::Expected<InputsAndAST> InpAST) mutable {
     if (!InpAST) {
       llvm::consumeError(InpAST.takeError());
       return Callback("<no-ast>");
@@ -444,19 +437,19 @@ void ClangdServer::dumpAST(PathRef File,
     Callback(Result);
   };
 
-  WorkScheduler.runWithAST("DumpAST", File, Bind(Action, std::move(Callback)));
+  WorkScheduler.runWithAST("DumpAST", File, std::move(Action));
 }
 
 void ClangdServer::locateSymbolAt(PathRef File, Position Pos,
                                   Callback<std::vector<LocatedSymbol>> CB) {
-  auto Action = [Pos, this](decltype(CB) CB,
-                            llvm::Expected<InputsAndAST> InpAST) {
+  auto Action = [Pos, CB = std::move(CB),
+                 this](llvm::Expected<InputsAndAST> InpAST) mutable {
     if (!InpAST)
       return CB(InpAST.takeError());
     CB(clangd::locateSymbolAt(InpAST->AST, Pos, Index));
   };
 
-  WorkScheduler.runWithAST("Definitions", File, Bind(Action, std::move(CB)));
+  WorkScheduler.runWithAST("Definitions", File, std::move(Action));
 }
 
 llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) {
@@ -536,20 +529,20 @@ ClangdServer::formatCode(llvm::StringRef
 
 void ClangdServer::findDocumentHighlights(
     PathRef File, Position Pos, Callback<std::vector<DocumentHighlight>> CB) {
-  auto Action = [Pos](Callback<std::vector<DocumentHighlight>> CB,
-                      llvm::Expected<InputsAndAST> InpAST) {
-    if (!InpAST)
-      return CB(InpAST.takeError());
-    CB(clangd::findDocumentHighlights(InpAST->AST, Pos));
-  };
+  auto Action =
+      [Pos, CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
+        if (!InpAST)
+          return CB(InpAST.takeError());
+        CB(clangd::findDocumentHighlights(InpAST->AST, Pos));
+      };
 
-  WorkScheduler.runWithAST("Highlights", File, Bind(Action, std::move(CB)));
+  WorkScheduler.runWithAST("Highlights", File, std::move(Action));
 }
 
 void ClangdServer::findHover(PathRef File, Position Pos,
                              Callback<llvm::Optional<HoverInfo>> CB) {
-  auto Action = [Pos, this](decltype(CB) CB, Path File,
-                            llvm::Expected<InputsAndAST> InpAST) {
+  auto Action = [File = File.str(), Pos, CB = std::move(CB),
+                 this](llvm::Expected<InputsAndAST> InpAST) mutable {
     if (!InpAST)
       return CB(InpAST.takeError());
     format::FormatStyle Style = getFormatStyleForFile(
@@ -557,23 +550,21 @@ void ClangdServer::findHover(PathRef Fil
     CB(clangd::getHover(InpAST->AST, Pos, std::move(Style), Index));
   };
 
-  WorkScheduler.runWithAST("Hover", File,
-                           Bind(Action, std::move(CB), File.str()));
+  WorkScheduler.runWithAST("Hover", File, std::move(Action));
 }
 
 void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve,
                                  TypeHierarchyDirection Direction,
                                  Callback<Optional<TypeHierarchyItem>> CB) {
-  std::string FileCopy = File; // copy will be captured by the lambda
-  auto Action = [FileCopy, Pos, Resolve, Direction,
-                 this](decltype(CB) CB, Expected<InputsAndAST> InpAST) {
+  auto Action = [File = File.str(), Pos, Resolve, Direction, CB = std::move(CB),
+                 this](Expected<InputsAndAST> InpAST) mutable {
     if (!InpAST)
       return CB(InpAST.takeError());
     CB(clangd::getTypeHierarchy(InpAST->AST, Pos, Resolve, Direction, Index,
-                                FileCopy));
+                                File));
   };
 
-  WorkScheduler.runWithAST("Type Hierarchy", File, Bind(Action, std::move(CB)));
+  WorkScheduler.runWithAST("Type Hierarchy", File, std::move(Action));
 }
 
 void ClangdServer::resolveTypeHierarchy(
@@ -594,48 +585,45 @@ void ClangdServer::workspaceSymbols(
   std::string QueryCopy = Query;
   WorkScheduler.run(
       "getWorkspaceSymbols",
-      Bind(
-          [QueryCopy, Limit, this](decltype(CB) CB) {
-            CB(clangd::getWorkspaceSymbols(QueryCopy, Limit, Index,
-                                           WorkspaceRoot.getValueOr("")));
-          },
-          std::move(CB)));
+      [QueryCopy, Limit, CB = std::move(CB), this]() mutable {
+        CB(clangd::getWorkspaceSymbols(QueryCopy, Limit, Index,
+                                       WorkspaceRoot.getValueOr("")));
+      });
 }
 
 void ClangdServer::documentSymbols(llvm::StringRef File,
                                    Callback<std::vector<DocumentSymbol>> CB) {
-  auto Action = [](Callback<std::vector<DocumentSymbol>> CB,
-                   llvm::Expected<InputsAndAST> InpAST) {
-    if (!InpAST)
-      return CB(InpAST.takeError());
-    CB(clangd::getDocumentSymbols(InpAST->AST));
-  };
-  WorkScheduler.runWithAST("documentSymbols", File,
-                           Bind(Action, std::move(CB)));
+  auto Action =
+      [CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
+        if (!InpAST)
+          return CB(InpAST.takeError());
+        CB(clangd::getDocumentSymbols(InpAST->AST));
+      };
+  WorkScheduler.runWithAST("documentSymbols", File, std::move(Action));
 }
 
 void ClangdServer::findReferences(PathRef File, Position Pos, uint32_t Limit,
                                   Callback<std::vector<Location>> CB) {
-  auto Action = [Pos, Limit, this](Callback<std::vector<Location>> CB,
-                                   llvm::Expected<InputsAndAST> InpAST) {
+  auto Action = [Pos, Limit, CB = std::move(CB),
+                 this](llvm::Expected<InputsAndAST> InpAST) mutable {
     if (!InpAST)
       return CB(InpAST.takeError());
     CB(clangd::findReferences(InpAST->AST, Pos, Limit, Index));
   };
 
-  WorkScheduler.runWithAST("References", File, Bind(Action, std::move(CB)));
+  WorkScheduler.runWithAST("References", File, std::move(Action));
 }
 
 void ClangdServer::symbolInfo(PathRef File, Position Pos,
                               Callback<std::vector<SymbolDetails>> CB) {
-  auto Action = [Pos](Callback<std::vector<SymbolDetails>> CB,
-                      llvm::Expected<InputsAndAST> InpAST) {
-    if (!InpAST)
-      return CB(InpAST.takeError());
-    CB(clangd::getSymbolInfo(InpAST->AST, Pos));
-  };
+  auto Action =
+      [Pos, CB = std::move(CB)](llvm::Expected<InputsAndAST> InpAST) mutable {
+        if (!InpAST)
+          return CB(InpAST.takeError());
+        CB(clangd::getSymbolInfo(InpAST->AST, Pos));
+      };
 
-  WorkScheduler.runWithAST("SymbolInfo", File, Bind(Action, std::move(CB)));
+  WorkScheduler.runWithAST("SymbolInfo", File, std::move(Action));
 }
 
 std::vector<std::pair<Path, std::size_t>>

Modified: clang-tools-extra/trunk/clangd/TUScheduler.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/TUScheduler.cpp?rev=369005&r1=369004&r2=369005&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/TUScheduler.cpp (original)
+++ clang-tools-extra/trunk/clangd/TUScheduler.cpp Thu Aug 15 07:16:06 2019
@@ -507,7 +507,7 @@ void ASTWorker::update(ParseInputs Input
 void ASTWorker::runWithAST(
     llvm::StringRef Name,
     llvm::unique_function<void(llvm::Expected<InputsAndAST>)> Action) {
-  auto Task = [=](decltype(Action) Action) {
+  auto Task = [=, Action = std::move(Action)]() mutable {
     if (isCancelled())
       return Action(llvm::make_error<CancelledError>());
     llvm::Optional<std::unique_ptr<ParsedAST>> AST = IdleASTs.take(this);
@@ -533,8 +533,7 @@ void ASTWorker::runWithAST(
           "invalid AST", llvm::errc::invalid_argument));
     Action(InputsAndAST{*CurrentInputs, **AST});
   };
-  startTask(Name, Bind(Task, std::move(Action)),
-            /*UpdateType=*/None);
+  startTask(Name, std::move(Task), /*UpdateType=*/None);
 }
 
 std::shared_ptr<const PreambleData>
@@ -559,11 +558,9 @@ void ASTWorker::getCurrentPreamble(
   }
   assert(!RunSync && "Running synchronously, but queue is non-empty!");
   Requests.insert(LastUpdate.base(),
-                  Request{Bind(
-                              [this](decltype(Callback) Callback) {
-                                Callback(getPossiblyStalePreamble());
-                              },
-                              std::move(Callback)),
+                  Request{[Callback = std::move(Callback), this]() mutable {
+                            Callback(getPossiblyStalePreamble());
+                          },
                           "GetPreamble", steady_clock::now(),
                           Context::current().clone(),
                           /*UpdateType=*/None});
@@ -945,20 +942,20 @@ void TUScheduler::runWithPreamble(llvm::
   if (Consistency == Consistent) {
     std::promise<std::shared_ptr<const PreambleData>> Promise;
     ConsistentPreamble = Promise.get_future();
-    It->second->Worker->getCurrentPreamble(Bind(
-        [](decltype(Promise) Promise,
-           std::shared_ptr<const PreambleData> Preamble) {
+    It->second->Worker->getCurrentPreamble(
+        [Promise = std::move(Promise)](
+            std::shared_ptr<const PreambleData> Preamble) mutable {
           Promise.set_value(std::move(Preamble));
-        },
-        std::move(Promise)));
+        });
   }
 
   std::shared_ptr<const ASTWorker> Worker = It->second->Worker.lock();
-  auto Task = [Worker, Consistency,
-               this](std::string Name, std::string File, std::string Contents,
-                     tooling::CompileCommand Command, Context Ctx,
-                     decltype(ConsistentPreamble) ConsistentPreamble,
-                     decltype(Action) Action) mutable {
+  auto Task = [Worker, Consistency, Name = Name.str(), File = File.str(),
+               Contents = It->second->Contents,
+               Command = Worker->getCurrentCompileCommand(),
+               Ctx = Context::current().derive(kFileBeingProcessed, File),
+               ConsistentPreamble = std::move(ConsistentPreamble),
+               Action = std::move(Action), this]() mutable {
     std::shared_ptr<const PreambleData> Preamble;
     if (ConsistentPreamble.valid()) {
       Preamble = ConsistentPreamble.get();
@@ -979,12 +976,8 @@ void TUScheduler::runWithPreamble(llvm::
     Action(InputsAndPreamble{Contents, Command, Preamble.get()});
   };
 
-  PreambleTasks->runAsync(
-      "task:" + llvm::sys::path::filename(File),
-      Bind(Task, std::string(Name), std::string(File), It->second->Contents,
-           Worker->getCurrentCompileCommand(),
-           Context::current().derive(kFileBeingProcessed, File),
-           std::move(ConsistentPreamble), std::move(Action)));
+  PreambleTasks->runAsync("task:" + llvm::sys::path::filename(File),
+                          std::move(Task));
 }
 
 std::vector<std::pair<Path, std::size_t>>

Modified: clang-tools-extra/trunk/clangd/unittests/TUSchedulerTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TUSchedulerTests.cpp?rev=369005&r1=369004&r2=369005&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/TUSchedulerTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/TUSchedulerTests.cpp Thu Aug 15 07:16:06 2019
@@ -83,14 +83,12 @@ protected:
                        WantDiagnostics WD,
                        llvm::unique_function<void(std::vector<Diag>)> CB) {
     Path OrigFile = File.str();
-    WithContextValue Ctx(
-        DiagsCallbackKey,
-        Bind(
-            [OrigFile](decltype(CB) CB, PathRef File, std::vector<Diag> Diags) {
-              assert(File == OrigFile);
-              CB(std::move(Diags));
-            },
-            std::move(CB)));
+    WithContextValue Ctx(DiagsCallbackKey,
+                         [OrigFile, CB = std::move(CB)](
+                             PathRef File, std::vector<Diag> Diags) mutable {
+                           assert(File == OrigFile);
+                           CB(std::move(Diags));
+                         });
     S.update(File, std::move(Inputs), WD);
   }
 




More information about the cfe-commits mailing list