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

Summary:
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

Reviewers: kadircet

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D77176
This commit is contained in:
Sam McCall 2020-03-31 23:33:56 +02:00
parent d307174e1d
commit 038f03cb5e
2 changed files with 18 additions and 1 deletions

View File

@ -1072,6 +1072,10 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
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);

View File

@ -127,7 +127,6 @@ CodeCompleteResult completions(llvm::StringRef Text,
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 @@ TEST(CompletionTest, NoCrashWithIncompleteLambda) {
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);