[PATCH] D77176: [clangd] Force delayed-template-parsing off in code completion.

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 31 14:53:14 PDT 2020


sammccall created this revision.
sammccall added a reviewer: kadircet.
Herald added subscribers: cfe-commits, usaxena95, arphaman, jkorous, MaskRay, ilya-biryukov.
Herald added a project: clang.

It prevents code completion entirely in affected method bodies.
The main reason it's turned on is for compatibility with headers, so we turn it
off for the main file only. This is allowed because it's a compatible langopt.

Fixes https://github.com/clangd/clangd/issues/302


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77176

Files:
  clang-tools-extra/clangd/CodeComplete.cpp
  clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp


Index: clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -127,7 +127,6 @@
   Annotations Test(Text);
   auto TU = TestTU::withCode(Test.code());
   // To make sure our tests for completiopns inside templates work on Windows.
-  TU.ExtraArgs = {"-fno-delayed-template-parsing"};
   TU.Filename = FilePath.str();
   return completions(TU, Test.point(), std::move(IndexSymbols),
                      std::move(Opts));
@@ -2660,6 +2659,20 @@
   EXPECT_THAT(Signatures, Contains(Sig("x() -> auto")));
 }
 
+TEST(CompletionTest, DelayedTemplateParsing) {
+  Annotations Test(R"cpp(
+    int xxx;
+    template <typename T> int foo() { return xx^; }
+  )cpp");
+  auto TU = TestTU::withCode(Test.code());
+  // Even though delayed-template-parsing is on, we will disable it to provide
+  // completion in templates.
+  TU.ExtraArgs.push_back("-fdelayed-template-parsing");
+
+  EXPECT_THAT(completions(TU, Test.point()).Completions,
+              Contains(Named("xxx")));
+}
+
 TEST(CompletionTest, CompletionRange) {
   const char *WithRange = "auto x = [[abc]]^";
   auto Completions = completions(WithRange);
Index: clang-tools-extra/clangd/CodeComplete.cpp
===================================================================
--- clang-tools-extra/clangd/CodeComplete.cpp
+++ clang-tools-extra/clangd/CodeComplete.cpp
@@ -1072,6 +1072,10 @@
   FrontendOpts.SkipFunctionBodies = true;
   // Disable typo correction in Sema.
   CI->getLangOpts()->SpellChecking = false;
+  // Code completion won't trigger in delayed template bodies.
+  // This is on-by-default in windows to allow parsing SDK headers; we're only
+  // disabling it for the main-file (not preamble).
+  CI->getLangOpts()->DelayedTemplateParsing = false;
   // Setup code completion.
   FrontendOpts.CodeCompleteOpts = Options;
   FrontendOpts.CodeCompletionAt.FileName = std::string(Input.FileName);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77176.254005.patch
Type: text/x-patch
Size: 2063 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200331/9138929b/attachment-0001.bin>


More information about the cfe-commits mailing list