1//===-- CodeCompleteTests.cpp -----------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ASTSignals.h"
10#include "Annotations.h"
11#include "ClangdServer.h"
12#include "CodeComplete.h"
13#include "Compiler.h"
14#include "Feature.h"
15#include "Matchers.h"
16#include "Protocol.h"
17#include "Quality.h"
18#include "SourceCode.h"
19#include "SyncAPI.h"
20#include "TestFS.h"
21#include "TestIndex.h"
22#include "TestTU.h"
23#include "index/Index.h"
24#include "index/MemIndex.h"
25#include "index/SymbolOrigin.h"
26#include "support/Threading.h"
27#include "clang/Sema/CodeCompleteConsumer.h"
28#include "clang/Tooling/CompilationDatabase.h"
29#include "llvm/ADT/StringRef.h"
30#include "llvm/Support/Error.h"
31#include "llvm/Support/Path.h"
32#include "llvm/Testing/Annotations/Annotations.h"
33#include "llvm/Testing/Support/Error.h"
34#include "llvm/Testing/Support/SupportHelpers.h"
35#include "gmock/gmock.h"
36#include "gtest/gtest.h"
37#include <condition_variable>
38#include <functional>
39#include <mutex>
40#include <vector>
41
42namespace clang {
43namespace clangd {
44
45namespace {
46using ::llvm::Failed;
47using ::testing::AllOf;
48using ::testing::Contains;
49using ::testing::ElementsAre;
50using ::testing::Field;
51using ::testing::HasSubstr;
52using ::testing::IsEmpty;
53using ::testing::Not;
54using ::testing::UnorderedElementsAre;
55using ContextKind = CodeCompletionContext::Kind;
56
57// GMock helpers for matching completion items.
58MATCHER_P(named, Name, "") { return arg.Name == Name; }
59MATCHER_P(mainFileRefs, Refs, "") { return arg.MainFileRefs == Refs; }
60MATCHER_P(scopeRefs, Refs, "") { return arg.ScopeRefsInFile == Refs; }
61MATCHER_P(nameStartsWith, Prefix, "") {
62 return llvm::StringRef(arg.Name).starts_with(Prefix);
63}
64MATCHER_P(filterText, F, "") { return arg.FilterText == F; }
65MATCHER_P(scope, S, "") { return arg.Scope == S; }
66MATCHER_P(qualifier, Q, "") { return arg.RequiredQualifier == Q; }
67MATCHER_P(labeled, Label, "") {
68 return arg.RequiredQualifier + arg.Name + arg.Signature == Label;
69}
70MATCHER_P(sigHelpLabeled, Label, "") { return arg.label == Label; }
71MATCHER_P(kind, K, "") { return arg.Kind == K; }
72MATCHER_P(doc, D, "") {
73 return arg.Documentation && arg.Documentation->asPlainText() == D;
74}
75MATCHER_P(returnType, D, "") { return arg.ReturnType == D; }
76MATCHER_P(hasInclude, IncludeHeader, "") {
77 return !arg.Includes.empty() && arg.Includes[0].Header == IncludeHeader;
78}
79MATCHER_P(insertInclude, IncludeHeader, "") {
80 return !arg.Includes.empty() && arg.Includes[0].Header == IncludeHeader &&
81 bool(arg.Includes[0].Insertion);
82}
83MATCHER_P(insertIncludeText, InsertedText, "") {
84 return !arg.Includes.empty() && arg.Includes[0].Insertion &&
85 arg.Includes[0].Insertion->newText == InsertedText;
86}
87MATCHER(insertInclude, "") {
88 return !arg.Includes.empty() && bool(arg.Includes[0].Insertion);
89}
90MATCHER_P(snippetSuffix, Text, "") { return arg.SnippetSuffix == Text; }
91MATCHER_P(origin, OriginSet, "") { return arg.Origin == OriginSet; }
92MATCHER_P(signature, S, "") { return arg.Signature == S; }
93MATCHER_P(replacesRange, Range, "") {
94 return arg.CompletionTokenRange == Range;
95}
96
97// Shorthand for Contains(named(Name)).
98Matcher<const std::vector<CodeCompletion> &> has(std::string Name) {
99 return Contains(matcher: named(gmock_p0: std::move(Name)));
100}
101Matcher<const std::vector<CodeCompletion> &> has(std::string Name,
102 CompletionItemKind K) {
103 return Contains(matcher: AllOf(matchers: named(gmock_p0: std::move(Name)), matchers: kind(gmock_p0: K)));
104}
105MATCHER(isDocumented, "") { return arg.Documentation.has_value(); }
106MATCHER(deprecated, "") { return arg.Deprecated; }
107
108std::unique_ptr<SymbolIndex> memIndex(std::vector<Symbol> Symbols) {
109 SymbolSlab::Builder Slab;
110 for (const auto &Sym : Symbols)
111 Slab.insert(S: Sym);
112 return MemIndex::build(Symbols: std::move(Slab).build(), Refs: RefSlab(), Relations: RelationSlab());
113}
114
115// Runs code completion.
116// If IndexSymbols is non-empty, an index will be built and passed to opts.
117CodeCompleteResult completions(const TestTU &TU, Position Point,
118 std::vector<Symbol> IndexSymbols = {},
119 clangd::CodeCompleteOptions Opts = {}) {
120 std::unique_ptr<SymbolIndex> OverrideIndex;
121 if (!IndexSymbols.empty()) {
122 assert(!Opts.Index && "both Index and IndexSymbols given!");
123 OverrideIndex = memIndex(Symbols: std::move(IndexSymbols));
124 Opts.Index = OverrideIndex.get();
125 }
126
127 MockFS FS;
128 auto Inputs = TU.inputs(FS);
129 IgnoreDiagnostics Diags;
130 auto CI = buildCompilerInvocation(Inputs, D&: Diags);
131 if (!CI) {
132 ADD_FAILURE() << "Couldn't build CompilerInvocation";
133 return {};
134 }
135 auto Preamble = buildPreamble(FileName: testPath(File: TU.Filename), CI: *CI, Inputs,
136 /*InMemory=*/StoreInMemory: true, /*Callback=*/PreambleCallback: nullptr);
137 return codeComplete(FileName: testPath(File: TU.Filename), Pos: Point, Preamble: Preamble.get(), ParseInput: Inputs,
138 Opts);
139}
140
141// Runs code completion.
142CodeCompleteResult completions(llvm::StringRef Text,
143 std::vector<Symbol> IndexSymbols = {},
144 clangd::CodeCompleteOptions Opts = {},
145 PathRef FilePath = "foo.cpp") {
146 Annotations Test(Text);
147 auto TU = TestTU::withCode(Code: Test.code());
148 // To make sure our tests for completiopns inside templates work on Windows.
149 TU.Filename = FilePath.str();
150 return completions(TU, Point: Test.point(), IndexSymbols: std::move(IndexSymbols),
151 Opts: std::move(Opts));
152}
153
154// Runs code completion without the clang parser.
155CodeCompleteResult completionsNoCompile(llvm::StringRef Text,
156 std::vector<Symbol> IndexSymbols = {},
157 clangd::CodeCompleteOptions Opts = {},
158 PathRef FilePath = "foo.cpp") {
159 std::unique_ptr<SymbolIndex> OverrideIndex;
160 if (!IndexSymbols.empty()) {
161 assert(!Opts.Index && "both Index and IndexSymbols given!");
162 OverrideIndex = memIndex(Symbols: std::move(IndexSymbols));
163 Opts.Index = OverrideIndex.get();
164 }
165
166 MockFS FS;
167 Annotations Test(Text);
168 ParseInputs ParseInput{.CompileCommand: tooling::CompileCommand(), .TFS: &FS, .Contents: Test.code().str()};
169 return codeComplete(FileName: FilePath, Pos: Test.point(), /*Preamble=*/nullptr, ParseInput,
170 Opts);
171}
172
173Symbol withReferences(int N, Symbol S) {
174 S.References = N;
175 return S;
176}
177
178#if CLANGD_DECISION_FOREST
179TEST(DecisionForestRankingModel, NameMatchSanityTest) {
180 clangd::CodeCompleteOptions Opts;
181 Opts.RankingModel = CodeCompleteOptions::DecisionForest;
182 auto Results = completions(
183 Text: R"cpp(
184struct MemberAccess {
185 int ABG();
186 int AlphaBetaGamma();
187};
188int func() { MemberAccess().ABG^ }
189)cpp",
190 /*IndexSymbols=*/{}, Opts);
191 EXPECT_THAT(Results.Completions,
192 ElementsAre(named("ABG"), named("AlphaBetaGamma")));
193}
194
195TEST(DecisionForestRankingModel, ReferencesAffectRanking) {
196 clangd::CodeCompleteOptions Opts;
197 Opts.RankingModel = CodeCompleteOptions::DecisionForest;
198 constexpr int NumReferences = 100000;
199 EXPECT_THAT(
200 completions("int main() { clang^ }",
201 {ns("clangA"), withReferences(NumReferences, func("clangD"))},
202 Opts)
203 .Completions,
204 ElementsAre(named("clangD"), named("clangA")));
205 EXPECT_THAT(
206 completions("int main() { clang^ }",
207 {withReferences(NumReferences, ns("clangA")), func("clangD")},
208 Opts)
209 .Completions,
210 ElementsAre(named("clangA"), named("clangD")));
211}
212#endif // CLANGD_DECISION_FOREST
213
214TEST(DecisionForestRankingModel, DecisionForestScorerCallbackTest) {
215 clangd::CodeCompleteOptions Opts;
216 constexpr float MagicNumber = 1234.5678f;
217 Opts.RankingModel = CodeCompleteOptions::DecisionForest;
218 Opts.DecisionForestScorer = [&](const SymbolQualitySignals &,
219 const SymbolRelevanceSignals &, float Base) {
220 DecisionForestScores Scores;
221 Scores.Total = MagicNumber;
222 Scores.ExcludingName = MagicNumber;
223 return Scores;
224 };
225 llvm::StringRef Code = "int func() { int xyz; xy^ }";
226 auto Results = completions(Text: Code,
227 /*IndexSymbols=*/{}, Opts);
228 ASSERT_EQ(Results.Completions.size(), 1u);
229 EXPECT_EQ(Results.Completions[0].Score.Total, MagicNumber);
230 EXPECT_EQ(Results.Completions[0].Score.ExcludingName, MagicNumber);
231
232 // Do not use DecisionForestScorer for heuristics model.
233 Opts.RankingModel = CodeCompleteOptions::Heuristics;
234 Results = completions(Text: Code,
235 /*IndexSymbols=*/{}, Opts);
236 ASSERT_EQ(Results.Completions.size(), 1u);
237 EXPECT_NE(Results.Completions[0].Score.Total, MagicNumber);
238 EXPECT_NE(Results.Completions[0].Score.ExcludingName, MagicNumber);
239}
240
241TEST(CompletionTest, Limit) {
242 clangd::CodeCompleteOptions Opts;
243 Opts.Limit = 2;
244 auto Results = completions(Text: R"cpp(
245struct ClassWithMembers {
246 int AAA();
247 int BBB();
248 int CCC();
249};
250
251int main() { ClassWithMembers().^ }
252 )cpp",
253 /*IndexSymbols=*/{}, Opts);
254
255 EXPECT_TRUE(Results.HasMore);
256 EXPECT_THAT(Results.Completions, ElementsAre(named("AAA"), named("BBB")));
257}
258
259TEST(CompletionTest, Filter) {
260 std::string Body = R"cpp(
261 #define MotorCar
262 int Car;
263 struct S {
264 int FooBar;
265 int FooBaz;
266 int Qux;
267 };
268 )cpp";
269
270 // Only items matching the fuzzy query are returned.
271 EXPECT_THAT(completions(Body + "int main() { S().Foba^ }").Completions,
272 AllOf(has("FooBar"), has("FooBaz"), Not(has("Qux"))));
273
274 // Macros require prefix match, either from index or AST.
275 Symbol Sym = var(Name: "MotorCarIndex");
276 Sym.SymInfo.Kind = index::SymbolKind::Macro;
277 EXPECT_THAT(
278 completions(Body + "int main() { C^ }", {Sym}).Completions,
279 AllOf(has("Car"), Not(has("MotorCar")), Not(has("MotorCarIndex"))));
280 EXPECT_THAT(completions(Body + "int main() { M^ }", {Sym}).Completions,
281 AllOf(has("MotorCar"), has("MotorCarIndex")));
282}
283
284void testAfterDotCompletion(clangd::CodeCompleteOptions Opts) {
285 auto Results = completions(
286 Text: R"cpp(
287 int global_var;
288
289 int global_func();
290
291 // Make sure this is not in preamble.
292 #define MACRO X
293
294 struct GlobalClass {};
295
296 struct ClassWithMembers {
297 /// doc for method.
298 int method();
299
300 int field;
301 private:
302 int private_field;
303 };
304
305 int test() {
306 struct LocalClass {};
307
308 /// doc for local_var.
309 int local_var;
310
311 ClassWithMembers().^
312 }
313 )cpp",
314 IndexSymbols: {cls(Name: "IndexClass"), var(Name: "index_var"), func(Name: "index_func")}, Opts);
315
316 EXPECT_TRUE(Results.RanParser);
317 // Class members. The only items that must be present in after-dot
318 // completion.
319 EXPECT_THAT(Results.Completions,
320 AllOf(has("method"), has("field"), Not(has("ClassWithMembers")),
321 Not(has("operator=")), Not(has("~ClassWithMembers"))));
322 EXPECT_IFF(Opts.IncludeIneligibleResults, Results.Completions,
323 has("private_field"));
324 // Global items.
325 EXPECT_THAT(
326 Results.Completions,
327 Not(AnyOf(has("global_var"), has("index_var"), has("global_func"),
328 has("global_func()"), has("index_func"), has("GlobalClass"),
329 has("IndexClass"), has("MACRO"), has("LocalClass"))));
330 // There should be no code patterns (aka snippets) in after-dot
331 // completion. At least there aren't any we're aware of.
332 EXPECT_THAT(Results.Completions,
333 Not(Contains(kind(CompletionItemKind::Snippet))));
334 // Check documentation.
335 EXPECT_THAT(Results.Completions, Contains(isDocumented()));
336}
337
338void testGlobalScopeCompletion(clangd::CodeCompleteOptions Opts) {
339 auto Results = completions(
340 Text: R"cpp(
341 int global_var;
342 int global_func();
343
344 // Make sure this is not in preamble.
345 #define MACRO X
346
347 struct GlobalClass {};
348
349 struct ClassWithMembers {
350 /// doc for method.
351 int method();
352 };
353
354 int test() {
355 struct LocalClass {};
356
357 /// doc for local_var.
358 int local_var;
359
360 ^
361 }
362 )cpp",
363 IndexSymbols: {cls(Name: "IndexClass"), var(Name: "index_var"), func(Name: "index_func")}, Opts);
364
365 EXPECT_TRUE(Results.RanParser);
366 // Class members. Should never be present in global completions.
367 EXPECT_THAT(Results.Completions,
368 Not(AnyOf(has("method"), has("method()"), has("field"))));
369 // Global items.
370 EXPECT_THAT(Results.Completions,
371 AllOf(has("global_var"), has("index_var"), has("global_func"),
372 has("index_func" /* our fake symbol doesn't include () */),
373 has("GlobalClass"), has("IndexClass")));
374 // A macro.
375 EXPECT_THAT(Results.Completions, has("MACRO"));
376 // Local items. Must be present always.
377 EXPECT_THAT(Results.Completions,
378 AllOf(has("local_var"), has("LocalClass"),
379 Contains(kind(CompletionItemKind::Snippet))));
380 // Check documentation.
381 EXPECT_THAT(Results.Completions, Contains(isDocumented()));
382}
383
384TEST(CompletionTest, CompletionOptions) {
385 auto Test = [&](const clangd::CodeCompleteOptions &Opts) {
386 testAfterDotCompletion(Opts);
387 testGlobalScopeCompletion(Opts);
388 };
389 // We used to test every combination of options, but that got too slow (2^N).
390 auto Flags = {
391 &clangd::CodeCompleteOptions::IncludeIneligibleResults,
392 };
393 // Test default options.
394 Test({});
395 // Test with one flag flipped.
396 for (auto &F : Flags) {
397 clangd::CodeCompleteOptions O;
398 O.*F ^= true;
399 Test(O);
400 }
401}
402
403TEST(CompletionTest, Accessible) {
404 auto Internal = completions(Text: R"cpp(
405 class Foo {
406 public: void pub();
407 protected: void prot();
408 private: void priv();
409 };
410 void Foo::pub() { this->^ }
411 )cpp");
412 EXPECT_THAT(Internal.Completions,
413 AllOf(has("priv"), has("prot"), has("pub")));
414
415 auto External = completions(Text: R"cpp(
416 class Foo {
417 public: void pub();
418 protected: void prot();
419 private: void priv();
420 };
421 void test() {
422 Foo F;
423 F.^
424 }
425 )cpp");
426 EXPECT_THAT(External.Completions,
427 AllOf(has("pub"), Not(has("prot")), Not(has("priv"))));
428
429 auto Results = completions(Text: R"cpp(
430 struct Foo {
431 public: void pub();
432 protected: void prot();
433 private: void priv();
434 };
435 struct Bar : public Foo {
436 private: using Foo::pub;
437 };
438 void test() {
439 Bar B;
440 B.^
441 }
442 )cpp");
443 EXPECT_THAT(Results.Completions,
444 AllOf(Not(has("priv")), Not(has("prot")), Not(has("pub"))));
445}
446
447TEST(CompletionTest, Qualifiers) {
448 auto Results = completions(Text: R"cpp(
449 class Foo {
450 public: int foo() const;
451 int bar() const;
452 };
453 class Bar : public Foo {
454 int foo() const;
455 };
456 void test() { Bar().^ }
457 )cpp");
458 EXPECT_THAT(Results.Completions,
459 Contains(AllOf(qualifier(""), named("bar"))));
460 // Hidden members are not shown.
461 EXPECT_THAT(Results.Completions,
462 Not(Contains(AllOf(qualifier("Foo::"), named("foo")))));
463 // Private members are not shown.
464 EXPECT_THAT(Results.Completions,
465 Not(Contains(AllOf(qualifier(""), named("foo")))));
466}
467
468// https://github.com/clangd/clangd/issues/1451
469TEST(CompletionTest, QualificationWithInlineNamespace) {
470 auto Results = completions(Text: R"cpp(
471 namespace a { inline namespace b {} }
472 using namespace a::b;
473 void f() { Foo^ }
474 )cpp",
475 IndexSymbols: {cls(Name: "a::Foo")});
476 EXPECT_THAT(Results.Completions,
477 UnorderedElementsAre(AllOf(qualifier("a::"), named("Foo"))));
478}
479
480TEST(CompletionTest, InjectedTypename) {
481 // These are suppressed when accessed as a member...
482 EXPECT_THAT(completions("struct X{}; void foo(){ X().^ }").Completions,
483 Not(has("X")));
484 EXPECT_THAT(completions("struct X{ void foo(){ this->^ } };").Completions,
485 Not(has("X")));
486 // ...but accessible in other, more useful cases.
487 EXPECT_THAT(completions("struct X{ void foo(){ ^ } };").Completions,
488 has("X"));
489 EXPECT_THAT(
490 completions("struct Y{}; struct X:Y{ void foo(){ ^ } };").Completions,
491 has("Y"));
492 EXPECT_THAT(
493 completions(
494 "template<class> struct Y{}; struct X:Y<int>{ void foo(){ ^ } };")
495 .Completions,
496 has("Y"));
497 // This case is marginal (`using X::X` is useful), we allow it for now.
498 EXPECT_THAT(completions("struct X{}; void foo(){ X::^ }").Completions,
499 has("X"));
500}
501
502TEST(CompletionTest, SkipInjectedWhenUnqualified) {
503 EXPECT_THAT(completions("struct X { void f() { X^ }};").Completions,
504 ElementsAre(named("X"), named("~X")));
505}
506
507TEST(CompletionTest, Snippets) {
508 clangd::CodeCompleteOptions Opts;
509 auto Results = completions(
510 Text: R"cpp(
511 struct fake {
512 int a;
513 int f(int i, const float f) const;
514 };
515 int main() {
516 fake f;
517 f.^
518 }
519 )cpp",
520 /*IndexSymbols=*/{}, Opts);
521 EXPECT_THAT(
522 Results.Completions,
523 HasSubsequence(named("a"),
524 snippetSuffix("(${1:int i}, ${2:const float f})")));
525}
526
527TEST(CompletionTest, HeuristicsForMemberFunctionCompletion) {
528 clangd::CodeCompleteOptions Opts;
529 Opts.EnableSnippets = true;
530
531 Annotations Code(R"cpp(
532 struct Foo {
533 static int staticMethod(int);
534 int method(int) const;
535 template <typename T, typename U, typename V = int>
536 T generic(U, V);
537 template <typename T, int U>
538 static T staticGeneric();
539 Foo() {
540 this->$canBeCall^
541 $canBeCall^
542 Foo::$canBeCall^
543 }
544 };
545
546 struct Derived : Foo {
547 using Foo::method;
548 using Foo::generic;
549 Derived() {
550 Foo::$canBeCall^
551 }
552 };
553
554 struct OtherClass {
555 OtherClass() {
556 Foo f;
557 Derived d;
558 f.$canBeCall^
559 ; // Prevent parsing as 'f.f'
560 f.Foo::$canBeCall^
561 &Foo::$canNotBeCall^
562 ;
563 d.Foo::$canBeCall^
564 ;
565 d.Derived::$canBeCall^
566 }
567 };
568
569 int main() {
570 Foo f;
571 Derived d;
572 f.$canBeCall^
573 ; // Prevent parsing as 'f.f'
574 f.Foo::$canBeCall^
575 &Foo::$canNotBeCall^
576 ;
577 d.Foo::$canBeCall^
578 ;
579 d.Derived::$canBeCall^
580 }
581 )cpp");
582 auto TU = TestTU::withCode(Code: Code.code());
583
584 for (const auto &P : Code.points(Name: "canNotBeCall")) {
585 auto Results = completions(TU, Point: P, /*IndexSymbols*/ {}, Opts);
586 EXPECT_THAT(Results.Completions,
587 Contains(AllOf(named("method"), signature("(int) const"),
588 snippetSuffix(""))));
589 // We don't have any arguments to deduce against if this isn't a call.
590 // Thus, we should emit these deducible template arguments explicitly.
591 EXPECT_THAT(
592 Results.Completions,
593 Contains(AllOf(named("generic"),
594 signature("<typename T, typename U>(U, V)"),
595 snippetSuffix("<${1:typename T}, ${2:typename U}>"))));
596 }
597
598 for (const auto &P : Code.points(Name: "canBeCall")) {
599 auto Results = completions(TU, Point: P, /*IndexSymbols*/ {}, Opts);
600 EXPECT_THAT(Results.Completions,
601 Contains(AllOf(named("method"), signature("(int) const"),
602 snippetSuffix("(${1:int})"))));
603 EXPECT_THAT(
604 Results.Completions,
605 Contains(AllOf(named("generic"), signature("<typename T>(U, V)"),
606 snippetSuffix("<${1:typename T}>(${2:U}, ${3:V})"))));
607 }
608
609 // static method will always keep the snippet
610 for (const auto &P : Code.points()) {
611 auto Results = completions(TU, Point: P, /*IndexSymbols*/ {}, Opts);
612 EXPECT_THAT(Results.Completions,
613 Contains(AllOf(named("staticMethod"), signature("(int)"),
614 snippetSuffix("(${1:int})"))));
615 EXPECT_THAT(Results.Completions,
616 Contains(AllOf(
617 named("staticGeneric"), signature("<typename T, int U>()"),
618 snippetSuffix("<${1:typename T}, ${2:int U}>()"))));
619 }
620}
621
622TEST(CompletionTest, NoSnippetsInUsings) {
623 clangd::CodeCompleteOptions Opts;
624 Opts.EnableSnippets = true;
625 auto Results = completions(
626 Text: R"cpp(
627 namespace ns {
628 int func(int a, int b);
629 }
630
631 using ns::^;
632 )cpp",
633 /*IndexSymbols=*/{}, Opts);
634 EXPECT_THAT(Results.Completions,
635 ElementsAre(AllOf(named("func"), labeled("func(int a, int b)"),
636 snippetSuffix(""))));
637
638 // Check index completions too.
639 auto Func = func(Name: "ns::func");
640 Func.CompletionSnippetSuffix = "(${1:int a}, ${2: int b})";
641 Func.Signature = "(int a, int b)";
642 Func.ReturnType = "void";
643
644 Results = completions(Text: R"cpp(
645 namespace ns {}
646 using ns::^;
647 )cpp",
648 /*IndexSymbols=*/{Func}, Opts);
649 EXPECT_THAT(Results.Completions,
650 ElementsAre(AllOf(named("func"), labeled("func(int a, int b)"),
651 snippetSuffix(""))));
652
653 // Check all-scopes completions too.
654 Opts.AllScopes = true;
655 Results = completions(Text: R"cpp(
656 using ^;
657 )cpp",
658 /*IndexSymbols=*/{Func}, Opts);
659 EXPECT_THAT(Results.Completions,
660 Contains(AllOf(named("func"), labeled("ns::func(int a, int b)"),
661 snippetSuffix(""))));
662}
663
664TEST(CompletionTest, Kinds) {
665 auto Results = completions(
666 Text: R"cpp(
667 int variable;
668 struct Struct {};
669 int function();
670 // make sure MACRO is not included in preamble.
671 #define MACRO 10
672 int X = ^
673 )cpp",
674 IndexSymbols: {func(Name: "indexFunction"), var(Name: "indexVariable"), cls(Name: "indexClass"),
675 macro(Name: "indexObjMacro"), macro(Name: "indexFuncMacro", ArgList: "(x, y)")});
676 EXPECT_THAT(Results.Completions,
677 AllOf(has("function", CompletionItemKind::Function),
678 has("variable", CompletionItemKind::Variable),
679 has("int", CompletionItemKind::Keyword),
680 has("Struct", CompletionItemKind::Struct),
681 has("MACRO", CompletionItemKind::Constant),
682 has("indexFunction", CompletionItemKind::Function),
683 has("indexVariable", CompletionItemKind::Variable),
684 has("indexClass", CompletionItemKind::Class),
685 has("indexObjMacro", CompletionItemKind::Constant),
686 has("indexFuncMacro", CompletionItemKind::Function)));
687
688 Results = completions(Text: "nam^");
689 EXPECT_THAT(Results.Completions,
690 has("namespace", CompletionItemKind::Snippet));
691
692 // Members of anonymous unions are of kind 'field'.
693 Results = completions(
694 Text: R"cpp(
695 struct X{
696 union {
697 void *a;
698 };
699 };
700 auto u = X().^
701 )cpp");
702 EXPECT_THAT(
703 Results.Completions,
704 UnorderedElementsAre(AllOf(named("a"), kind(CompletionItemKind::Field))));
705
706 // Completion kinds for templates should not be unknown.
707 Results = completions(
708 Text: R"cpp(
709 template <class T> struct complete_class {};
710 template <class T> void complete_function();
711 template <class T> using complete_type_alias = int;
712 template <class T> int complete_variable = 10;
713
714 struct X {
715 template <class T> static int complete_static_member = 10;
716
717 static auto x = complete_^
718 }
719 )cpp");
720 EXPECT_THAT(
721 Results.Completions,
722 UnorderedElementsAre(
723 AllOf(named("complete_class"), kind(CompletionItemKind::Class)),
724 AllOf(named("complete_function"), kind(CompletionItemKind::Function)),
725 AllOf(named("complete_type_alias"),
726 kind(CompletionItemKind::Interface)),
727 AllOf(named("complete_variable"), kind(CompletionItemKind::Variable)),
728 AllOf(named("complete_static_member"),
729 kind(CompletionItemKind::Property))));
730
731 Results = completions(
732 Text: R"cpp(
733 enum Color {
734 Red
735 };
736 Color u = ^
737 )cpp");
738 EXPECT_THAT(
739 Results.Completions,
740 Contains(AllOf(named("Red"), kind(CompletionItemKind::EnumMember))));
741}
742
743TEST(CompletionTest, NoDuplicates) {
744 auto Results = completions(
745 Text: R"cpp(
746 class Adapter {
747 };
748
749 void f() {
750 Adapter^
751 }
752 )cpp",
753 IndexSymbols: {cls(Name: "Adapter")});
754
755 // Make sure there are no duplicate entries of 'Adapter'.
756 EXPECT_THAT(Results.Completions, ElementsAre(named("Adapter")));
757}
758
759TEST(CompletionTest, ScopedNoIndex) {
760 auto Results = completions(
761 Text: R"cpp(
762 namespace fake { int BigBang, Babble, Box; };
763 int main() { fake::ba^ }
764 ")cpp");
765 // Babble is a better match than BigBang. Box doesn't match at all.
766 EXPECT_THAT(Results.Completions,
767 ElementsAre(named("Babble"), named("BigBang")));
768}
769
770TEST(CompletionTest, Scoped) {
771 auto Results = completions(
772 Text: R"cpp(
773 namespace fake { int Babble, Box; };
774 int main() { fake::ba^ }
775 ")cpp",
776 IndexSymbols: {var(Name: "fake::BigBang")});
777 EXPECT_THAT(Results.Completions,
778 ElementsAre(named("Babble"), named("BigBang")));
779}
780
781TEST(CompletionTest, ScopedWithFilter) {
782 auto Results = completions(
783 Text: R"cpp(
784 void f() { ns::x^ }
785 )cpp",
786 IndexSymbols: {cls(Name: "ns::XYZ"), func(Name: "ns::foo")});
787 EXPECT_THAT(Results.Completions, UnorderedElementsAre(named("XYZ")));
788}
789
790TEST(CompletionTest, ReferencesAffectRanking) {
791 EXPECT_THAT(completions("int main() { abs^ }", {func("absA"), func("absB")})
792 .Completions,
793 HasSubsequence(named("absA"), named("absB")));
794 EXPECT_THAT(completions("int main() { abs^ }",
795 {func("absA"), withReferences(1000, func("absB"))})
796 .Completions,
797 HasSubsequence(named("absB"), named("absA")));
798}
799
800TEST(CompletionTest, ContextWords) {
801 auto Results = completions(Text: R"cpp(
802 enum class Color { RED, YELLOW, BLUE };
803
804 // (blank lines so the definition above isn't "context")
805
806 // "It was a yellow car," he said. "Big yellow car, new."
807 auto Finish = Color::^
808 )cpp");
809 // Yellow would normally sort last (alphabetic).
810 // But the recent mention should bump it up.
811 ASSERT_THAT(Results.Completions,
812 HasSubsequence(named("YELLOW"), named("BLUE")));
813}
814
815TEST(CompletionTest, GlobalQualified) {
816 auto Results = completions(
817 Text: R"cpp(
818 void f() { ::^ }
819 )cpp",
820 IndexSymbols: {cls(Name: "XYZ")});
821 EXPECT_THAT(Results.Completions,
822 AllOf(has("XYZ", CompletionItemKind::Class),
823 has("f", CompletionItemKind::Function)));
824}
825
826TEST(CompletionTest, FullyQualified) {
827 auto Results = completions(
828 Text: R"cpp(
829 namespace ns { void bar(); }
830 void f() { ::ns::^ }
831 )cpp",
832 IndexSymbols: {cls(Name: "ns::XYZ")});
833 EXPECT_THAT(Results.Completions,
834 AllOf(has("XYZ", CompletionItemKind::Class),
835 has("bar", CompletionItemKind::Function)));
836}
837
838TEST(CompletionTest, SemaIndexMerge) {
839 auto Results = completions(
840 Text: R"cpp(
841 namespace ns { int local; void both(); }
842 void f() { ::ns::^ }
843 )cpp",
844 IndexSymbols: {func(Name: "ns::both"), cls(Name: "ns::Index")});
845 // We get results from both index and sema, with no duplicates.
846 EXPECT_THAT(Results.Completions,
847 UnorderedElementsAre(
848 AllOf(named("local"), origin(SymbolOrigin::AST)),
849 AllOf(named("Index"), origin(SymbolOrigin::Static)),
850 AllOf(named("both"),
851 origin(SymbolOrigin::AST | SymbolOrigin::Static))));
852}
853
854TEST(CompletionTest, SemaIndexMergeWithLimit) {
855 clangd::CodeCompleteOptions Opts;
856 Opts.Limit = 1;
857 auto Results = completions(
858 Text: R"cpp(
859 namespace ns { int local; void both(); }
860 void f() { ::ns::^ }
861 )cpp",
862 IndexSymbols: {func(Name: "ns::both"), cls(Name: "ns::Index")}, Opts);
863 EXPECT_EQ(Results.Completions.size(), Opts.Limit);
864 EXPECT_TRUE(Results.HasMore);
865}
866
867TEST(CompletionTest, IncludeInsertionPreprocessorIntegrationTests) {
868 TestTU TU;
869 TU.ExtraArgs.push_back(x: "-I" + testPath(File: "sub"));
870 TU.AdditionalFiles["sub/bar.h"] = "";
871 auto BarURI = URI::create(AbsolutePath: testPath(File: "sub/bar.h")).toString();
872
873 Symbol Sym = cls(Name: "ns::X");
874 Sym.CanonicalDeclaration.FileURI = BarURI.c_str();
875 Sym.IncludeHeaders.emplace_back(Args&: BarURI, Args: 1, Args: Symbol::Include);
876 // Shorten include path based on search directory and insert.
877 Annotations Test("int main() { ns::^ }");
878 TU.Code = Test.code().str();
879 auto Results = completions(TU, Point: Test.point(), IndexSymbols: {Sym});
880 EXPECT_THAT(Results.Completions,
881 ElementsAre(AllOf(named("X"), insertInclude("\"bar.h\""))));
882 // Can be disabled via option.
883 CodeCompleteOptions NoInsertion;
884 NoInsertion.InsertIncludes = CodeCompleteOptions::NeverInsert;
885 Results = completions(TU, Point: Test.point(), IndexSymbols: {Sym}, Opts: NoInsertion);
886 EXPECT_THAT(Results.Completions,
887 ElementsAre(AllOf(named("X"), Not(insertInclude()))));
888 // Duplicate based on inclusions in preamble.
889 Test = Annotations(R"cpp(
890 #include "sub/bar.h" // not shortest, so should only match resolved.
891 int main() { ns::^ }
892 )cpp");
893 TU.Code = Test.code().str();
894 Results = completions(TU, Point: Test.point(), IndexSymbols: {Sym});
895 EXPECT_THAT(Results.Completions, ElementsAre(AllOf(named("X"), labeled("X"),
896 Not(insertInclude()))));
897}
898
899TEST(CompletionTest, NoIncludeInsertionWhenDeclFoundInFile) {
900 Symbol SymX = cls(Name: "ns::X");
901 Symbol SymY = cls(Name: "ns::Y");
902 std::string BarHeader = testPath(File: "bar.h");
903 auto BarURI = URI::create(AbsolutePath: BarHeader).toString();
904 SymX.CanonicalDeclaration.FileURI = BarURI.c_str();
905 SymY.CanonicalDeclaration.FileURI = BarURI.c_str();
906 SymX.IncludeHeaders.emplace_back(Args: "<bar>", Args: 1, Args: Symbol::Include);
907 SymY.IncludeHeaders.emplace_back(Args: "<bar>", Args: 1, Args: Symbol::Include);
908 // Shorten include path based on search directory and insert.
909 auto Results = completions(Text: R"cpp(
910 namespace ns {
911 class X;
912 class Y {};
913 }
914 int main() { ns::^ }
915 )cpp",
916 IndexSymbols: {SymX, SymY});
917 EXPECT_THAT(Results.Completions,
918 ElementsAre(AllOf(named("X"), Not(insertInclude())),
919 AllOf(named("Y"), Not(insertInclude()))));
920}
921
922TEST(CompletionTest, IndexSuppressesPreambleCompletions) {
923 Annotations Test(R"cpp(
924 #include "bar.h"
925 namespace ns { int local; }
926 void f() { ns::^; }
927 void f2() { ns::preamble().$2^; }
928 )cpp");
929 auto TU = TestTU::withCode(Code: Test.code());
930 TU.AdditionalFiles["bar.h"] =
931 R"cpp(namespace ns { struct preamble { int member; }; })cpp";
932
933 clangd::CodeCompleteOptions Opts = {};
934 auto I = memIndex(Symbols: {var(Name: "ns::index")});
935 Opts.Index = I.get();
936 auto WithIndex = completions(TU, Point: Test.point(), IndexSymbols: {}, Opts);
937 EXPECT_THAT(WithIndex.Completions,
938 UnorderedElementsAre(named("local"), named("index")));
939 auto ClassFromPreamble = completions(TU, Point: Test.point(Name: "2"), IndexSymbols: {}, Opts);
940 EXPECT_THAT(ClassFromPreamble.Completions, Contains(named("member")));
941
942 Opts.Index = nullptr;
943 auto WithoutIndex = completions(TU, Point: Test.point(), IndexSymbols: {}, Opts);
944 EXPECT_THAT(WithoutIndex.Completions,
945 UnorderedElementsAre(named("local"), named("preamble")));
946}
947
948// This verifies that we get normal preprocessor completions in the preamble.
949// This is a regression test for an old bug: if we override the preamble and
950// try to complete inside it, clang kicks our completion point just outside the
951// preamble, resulting in always getting top-level completions.
952TEST(CompletionTest, CompletionInPreamble) {
953 auto Results = completions(Text: R"cpp(
954 #ifnd^ef FOO_H_
955 #define BAR_H_
956 #include <bar.h>
957 int foo() {}
958 #endif
959 )cpp")
960 .Completions;
961 EXPECT_THAT(Results, ElementsAre(named("ifndef")));
962}
963
964TEST(CompletionTest, CompletionRecoveryASTType) {
965 auto Results = completions(Text: R"cpp(
966 struct S { int member; };
967 S overloaded(int);
968 void foo() {
969 // No overload matches, but we have recovery-expr with the correct type.
970 overloaded().^
971 })cpp")
972 .Completions;
973 EXPECT_THAT(Results, ElementsAre(named("member")));
974}
975
976TEST(CompletionTest, DynamicIndexIncludeInsertion) {
977 MockFS FS;
978 MockCompilationDatabase CDB;
979 ClangdServer::Options Opts = ClangdServer::optsForTest();
980 Opts.BuildDynamicSymbolIndex = true;
981 ClangdServer Server(CDB, FS, Opts);
982
983 FS.Files[testPath(File: "foo_header.h")] = R"cpp(
984 #pragma once
985 struct Foo {
986 // Member doc
987 int foo();
988 };
989 )cpp";
990 const std::string FileContent(R"cpp(
991 #include "foo_header.h"
992 int Foo::foo() {
993 return 42;
994 }
995 )cpp");
996 Server.addDocument(File: testPath(File: "foo_impl.cpp"), Contents: FileContent);
997 // Wait for the dynamic index being built.
998 ASSERT_TRUE(Server.blockUntilIdleForTest());
999
1000 auto File = testPath(File: "foo.cpp");
1001 Annotations Test("Foo^ foo;");
1002 runAddDocument(Server, File, Contents: Test.code());
1003 auto CompletionList =
1004 llvm::cantFail(ValOrErr: runCodeComplete(Server, File, Pos: Test.point(), Opts: {}));
1005
1006 EXPECT_THAT(CompletionList.Completions,
1007 ElementsAre(AllOf(named("Foo"), hasInclude("\"foo_header.h\""),
1008 insertInclude())));
1009}
1010
1011TEST(CompletionTest, DynamicIndexMultiFile) {
1012 MockFS FS;
1013 MockCompilationDatabase CDB;
1014 auto Opts = ClangdServer::optsForTest();
1015 Opts.BuildDynamicSymbolIndex = true;
1016 ClangdServer Server(CDB, FS, Opts);
1017
1018 FS.Files[testPath(File: "foo.h")] = R"cpp(
1019 namespace ns { class XYZ {}; void foo(int x) {} }
1020 )cpp";
1021 runAddDocument(Server, File: testPath(File: "foo.cpp"), Contents: R"cpp(
1022 #include "foo.h"
1023 )cpp");
1024
1025 auto File = testPath(File: "bar.cpp");
1026 Annotations Test(R"cpp(
1027 namespace ns {
1028 class XXX {};
1029 /// Doooc
1030 void fooooo() {}
1031 }
1032 void f() { ns::^ }
1033 )cpp");
1034 runAddDocument(Server, File, Contents: Test.code());
1035
1036 auto Results = cantFail(ValOrErr: runCodeComplete(Server, File, Pos: Test.point(), Opts: {}));
1037 // "XYZ" and "foo" are not included in the file being completed but are still
1038 // visible through the index.
1039 EXPECT_THAT(Results.Completions, has("XYZ", CompletionItemKind::Class));
1040 EXPECT_THAT(Results.Completions, has("foo", CompletionItemKind::Function));
1041 EXPECT_THAT(Results.Completions, has("XXX", CompletionItemKind::Class));
1042 EXPECT_THAT(Results.Completions,
1043 Contains((named("fooooo"), kind(CompletionItemKind::Function),
1044 doc("Doooc"), returnType("void"))));
1045}
1046
1047TEST(CompletionTest, Documentation) {
1048 auto Results = completions(
1049 Text: R"cpp(
1050 // Non-doxygen comment.
1051 __attribute__((annotate("custom_annotation"))) int foo();
1052 /// Doxygen comment.
1053 /// \param int a
1054 int bar(int a);
1055 /* Multi-line
1056 block comment
1057 */
1058 int baz();
1059
1060 int x = ^
1061 )cpp");
1062 EXPECT_THAT(Results.Completions,
1063 Contains(AllOf(
1064 named("foo"),
1065 doc("Annotation: custom_annotation\nNon-doxygen comment."))));
1066 EXPECT_THAT(
1067 Results.Completions,
1068 Contains(AllOf(named("bar"), doc("Doxygen comment.\n\\param int a"))));
1069 EXPECT_THAT(Results.Completions,
1070 Contains(AllOf(named("baz"), doc("Multi-line block comment"))));
1071}
1072
1073TEST(CompletionTest, CommentsFromSystemHeaders) {
1074 MockFS FS;
1075 MockCompilationDatabase CDB;
1076
1077 auto Opts = ClangdServer::optsForTest();
1078 Opts.BuildDynamicSymbolIndex = true;
1079
1080 ClangdServer Server(CDB, FS, Opts);
1081
1082 FS.Files[testPath(File: "foo.h")] = R"cpp(
1083 #pragma GCC system_header
1084
1085 // This comment should be retained!
1086 int foo();
1087 )cpp";
1088
1089 auto File = testPath(File: "foo.cpp");
1090 Annotations Test(R"cpp(
1091#include "foo.h"
1092int x = foo^
1093 )cpp");
1094 runAddDocument(Server, File, Contents: Test.code());
1095 auto CompletionList =
1096 llvm::cantFail(ValOrErr: runCodeComplete(Server, File, Pos: Test.point(), Opts: {}));
1097
1098 EXPECT_THAT(
1099 CompletionList.Completions,
1100 Contains(AllOf(named("foo"), doc("This comment should be retained!"))));
1101}
1102
1103TEST(CompletionTest, GlobalCompletionFiltering) {
1104
1105 Symbol Class = cls(Name: "XYZ");
1106 Class.Flags = static_cast<Symbol::SymbolFlag>(
1107 Class.Flags & ~(Symbol::IndexedForCodeCompletion));
1108 Symbol Func = func(Name: "XYZ::foooo");
1109 Func.Flags = static_cast<Symbol::SymbolFlag>(
1110 Func.Flags & ~(Symbol::IndexedForCodeCompletion));
1111
1112 auto Results = completions(Text: R"(// void f() {
1113 XYZ::foooo^
1114 })",
1115 IndexSymbols: {Class, Func});
1116 EXPECT_THAT(Results.Completions, IsEmpty());
1117}
1118
1119TEST(CodeCompleteTest, DisableTypoCorrection) {
1120 auto Results = completions(Text: R"cpp(
1121 namespace clang { int v; }
1122 void f() { clangd::^
1123 )cpp");
1124 EXPECT_TRUE(Results.Completions.empty());
1125}
1126
1127TEST(CodeCompleteTest, NoColonColonAtTheEnd) {
1128 auto Results = completions(Text: R"cpp(
1129 namespace clang { }
1130 void f() {
1131 clan^
1132 }
1133 )cpp");
1134
1135 EXPECT_THAT(Results.Completions, Contains(labeled("clang")));
1136 EXPECT_THAT(Results.Completions, Not(Contains(labeled("clang::"))));
1137}
1138
1139TEST(CompletionTests, EmptySnippetDoesNotCrash) {
1140 // See https://github.com/clangd/clangd/issues/1216
1141 auto Results = completions(Text: R"cpp(
1142 int main() {
1143 auto w = [&](auto &&f) { return f(f); };
1144 auto f = w([&](auto &&f) {
1145 return [&](auto &&n) {
1146 if (n == 0) {
1147 return 1;
1148 }
1149 return n * ^(f)(n - 1);
1150 };
1151 })(10);
1152 }
1153 )cpp");
1154}
1155
1156TEST(CompletionTest, Issue1427Crash) {
1157 // Need to provide main file signals to ensure that the branch in
1158 // SymbolRelevanceSignals::computeASTSignals() that tries to
1159 // compute a symbol ID is taken.
1160 ASTSignals MainFileSignals;
1161 CodeCompleteOptions Opts;
1162 Opts.MainFileSignals = &MainFileSignals;
1163 completions(Text: R"cpp(
1164 auto f = []() {
1165 1.0_^
1166 };
1167 )cpp",
1168 IndexSymbols: {}, Opts);
1169}
1170
1171TEST(CompletionTest, BacktrackCrashes) {
1172 // Sema calls code completion callbacks twice in these cases.
1173 auto Results = completions(Text: R"cpp(
1174 namespace ns {
1175 struct FooBarBaz {};
1176 } // namespace ns
1177
1178 int foo(ns::FooBar^
1179 )cpp");
1180
1181 EXPECT_THAT(Results.Completions, ElementsAre(labeled("FooBarBaz")));
1182
1183 // Check we don't crash in that case too.
1184 completions(Text: R"cpp(
1185 struct FooBarBaz {};
1186 void test() {
1187 if (FooBarBaz * x^) {}
1188 }
1189)cpp");
1190}
1191
1192TEST(CompletionTest, CompleteInMacroWithStringification) {
1193 auto Results = completions(Text: R"cpp(
1194void f(const char *, int x);
1195#define F(x) f(#x, x)
1196
1197namespace ns {
1198int X;
1199int Y;
1200} // namespace ns
1201
1202int f(int input_num) {
1203 F(ns::^)
1204}
1205)cpp");
1206
1207 EXPECT_THAT(Results.Completions,
1208 UnorderedElementsAre(named("X"), named("Y")));
1209}
1210
1211TEST(CompletionTest, CompleteInMacroAndNamespaceWithStringification) {
1212 auto Results = completions(Text: R"cpp(
1213void f(const char *, int x);
1214#define F(x) f(#x, x)
1215
1216namespace ns {
1217int X;
1218
1219int f(int input_num) {
1220 F(^)
1221}
1222} // namespace ns
1223)cpp");
1224
1225 EXPECT_THAT(Results.Completions, Contains(named("X")));
1226}
1227
1228TEST(CompletionTest, IgnoreCompleteInExcludedPPBranchWithRecoveryContext) {
1229 auto Results = completions(Text: R"cpp(
1230 int bar(int param_in_bar) {
1231 }
1232
1233 int foo(int param_in_foo) {
1234#if 0
1235 // In recovery mode, "param_in_foo" will also be suggested among many other
1236 // unrelated symbols; however, this is really a special case where this works.
1237 // If the #if block is outside of the function, "param_in_foo" is still
1238 // suggested, but "bar" and "foo" are missing. So the recovery mode doesn't
1239 // really provide useful results in excluded branches.
1240 par^
1241#endif
1242 }
1243)cpp");
1244
1245 EXPECT_TRUE(Results.Completions.empty());
1246}
1247
1248TEST(CompletionTest, DefaultArgs) {
1249 clangd::CodeCompleteOptions Opts;
1250 std::string Context = R"cpp(
1251 int X(int A = 0);
1252 int Y(int A, int B = 0);
1253 int Z(int A, int B = 0, int C = 0, int D = 0);
1254 )cpp";
1255 EXPECT_THAT(completions(Context + "int y = X^", {}, Opts).Completions,
1256 UnorderedElementsAre(labeled("X(int A = 0)")));
1257 EXPECT_THAT(completions(Context + "int y = Y^", {}, Opts).Completions,
1258 UnorderedElementsAre(AllOf(labeled("Y(int A, int B = 0)"),
1259 snippetSuffix("(${1:int A})"))));
1260 EXPECT_THAT(completions(Context + "int y = Z^", {}, Opts).Completions,
1261 UnorderedElementsAre(
1262 AllOf(labeled("Z(int A, int B = 0, int C = 0, int D = 0)"),
1263 snippetSuffix("(${1:int A})"))));
1264}
1265
1266TEST(CompletionTest, NoCrashWithTemplateParamsAndPreferredTypes) {
1267 auto Completions = completions(Text: R"cpp(
1268template <template <class> class TT> int foo() {
1269 int a = ^
1270}
1271)cpp")
1272 .Completions;
1273 EXPECT_THAT(Completions, Contains(named("TT")));
1274}
1275
1276TEST(CompletionTest, NestedTemplateHeuristics) {
1277 auto Completions = completions(Text: R"cpp(
1278struct Plain { int xxx; };
1279template <typename T> class Templ { Plain ppp; };
1280template <typename T> void foo(Templ<T> &t) {
1281 // Formally ppp has DependentTy, because Templ may be specialized.
1282 // However we sholud be able to see into it using the primary template.
1283 t.ppp.^
1284}
1285)cpp")
1286 .Completions;
1287 EXPECT_THAT(Completions, Contains(named("xxx")));
1288}
1289
1290TEST(CompletionTest, RecordCCResultCallback) {
1291 std::vector<CodeCompletion> RecordedCompletions;
1292 CodeCompleteOptions Opts;
1293 Opts.RecordCCResult = [&RecordedCompletions](const CodeCompletion &CC,
1294 const SymbolQualitySignals &,
1295 const SymbolRelevanceSignals &,
1296 float Score) {
1297 RecordedCompletions.push_back(x: CC);
1298 };
1299
1300 completions(Text: "int xy1, xy2; int a = xy^", /*IndexSymbols=*/{}, Opts);
1301 EXPECT_THAT(RecordedCompletions,
1302 UnorderedElementsAre(named("xy1"), named("xy2")));
1303}
1304
1305TEST(CompletionTest, ASTSignals) {
1306 struct Completion {
1307 std::string Name;
1308 unsigned MainFileRefs;
1309 unsigned ScopeRefsInFile;
1310 };
1311 CodeCompleteOptions Opts;
1312 std::vector<Completion> RecordedCompletions;
1313 Opts.RecordCCResult = [&RecordedCompletions](const CodeCompletion &CC,
1314 const SymbolQualitySignals &,
1315 const SymbolRelevanceSignals &R,
1316 float Score) {
1317 RecordedCompletions.push_back(x: {.Name: CC.Name, .MainFileRefs: R.MainFileRefs, .ScopeRefsInFile: R.ScopeRefsInFile});
1318 };
1319 ASTSignals MainFileSignals;
1320 MainFileSignals.ReferencedSymbols[var(Name: "xy1").ID] = 3;
1321 MainFileSignals.ReferencedSymbols[var(Name: "xy2").ID] = 1;
1322 MainFileSignals.ReferencedSymbols[var(Name: "xyindex").ID] = 10;
1323 MainFileSignals.RelatedNamespaces["tar::"] = 5;
1324 MainFileSignals.RelatedNamespaces["bar::"] = 3;
1325 Opts.MainFileSignals = &MainFileSignals;
1326 Opts.AllScopes = true;
1327 completions(
1328 Text: R"cpp(
1329 int xy1;
1330 int xy2;
1331 namespace bar {
1332 int xybar = 1;
1333 int a = xy^
1334 }
1335 )cpp",
1336 /*IndexSymbols=*/{var(Name: "xyindex"), var(Name: "tar::xytar"), var(Name: "bar::xybar")},
1337 Opts);
1338 EXPECT_THAT(RecordedCompletions,
1339 UnorderedElementsAre(
1340 AllOf(named("xy1"), mainFileRefs(3u), scopeRefs(0u)),
1341 AllOf(named("xy2"), mainFileRefs(1u), scopeRefs(0u)),
1342 AllOf(named("xyindex"), mainFileRefs(10u), scopeRefs(0u)),
1343 AllOf(named("xytar"), mainFileRefs(0u), scopeRefs(5u)),
1344 AllOf(/*both from sema and index*/ named("xybar"),
1345 mainFileRefs(0u), scopeRefs(3u))));
1346}
1347
1348SignatureHelp
1349signatures(llvm::StringRef Text, Position Point,
1350 std::vector<Symbol> IndexSymbols = {},
1351 MarkupKind DocumentationFormat = MarkupKind::PlainText) {
1352 std::unique_ptr<SymbolIndex> Index;
1353 if (!IndexSymbols.empty())
1354 Index = memIndex(Symbols: IndexSymbols);
1355
1356 auto TU = TestTU::withCode(Code: Text);
1357 MockFS FS;
1358 auto Inputs = TU.inputs(FS);
1359 Inputs.Index = Index.get();
1360 IgnoreDiagnostics Diags;
1361 auto CI = buildCompilerInvocation(Inputs, D&: Diags);
1362 if (!CI) {
1363 ADD_FAILURE() << "Couldn't build CompilerInvocation";
1364 return {};
1365 }
1366 auto Preamble = buildPreamble(FileName: testPath(File: TU.Filename), CI: *CI, Inputs,
1367 /*InMemory=*/StoreInMemory: true, /*Callback=*/PreambleCallback: nullptr);
1368 if (!Preamble) {
1369 ADD_FAILURE() << "Couldn't build Preamble";
1370 return {};
1371 }
1372 return signatureHelp(FileName: testPath(File: TU.Filename), Pos: Point, Preamble: *Preamble, ParseInput: Inputs,
1373 DocumentationFormat);
1374}
1375
1376SignatureHelp
1377signatures(llvm::StringRef Text, std::vector<Symbol> IndexSymbols = {},
1378 MarkupKind DocumentationFormat = MarkupKind::PlainText) {
1379 Annotations Test(Text);
1380 return signatures(Text: Test.code(), Point: Test.point(), IndexSymbols: std::move(IndexSymbols),
1381 DocumentationFormat);
1382}
1383
1384struct ExpectedParameter {
1385 std::string Text;
1386 std::pair<unsigned, unsigned> Offsets;
1387};
1388llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
1389 const ExpectedParameter &P) {
1390 return OS << P.Text;
1391}
1392MATCHER_P(paramsAre, P, "") {
1393 if (P.size() != arg.parameters.size())
1394 return false;
1395 for (unsigned I = 0; I < P.size(); ++I) {
1396 if (P[I].Text != arg.parameters[I].labelString ||
1397 P[I].Offsets != arg.parameters[I].labelOffsets)
1398 return false;
1399 }
1400 return true;
1401}
1402MATCHER_P(sigDoc, doc, "") { return arg.documentation.value == doc; }
1403
1404/// \p AnnotatedLabel is a signature label with ranges marking parameters, e.g.
1405/// foo([[int p1]], [[double p2]]) -> void
1406Matcher<SignatureInformation> sig(llvm::StringRef AnnotatedLabel) {
1407 llvm::Annotations A(AnnotatedLabel);
1408 std::string Label = std::string(A.code());
1409 std::vector<ExpectedParameter> Parameters;
1410 for (auto Range : A.ranges()) {
1411 Parameters.emplace_back();
1412
1413 ExpectedParameter &P = Parameters.back();
1414 P.Text = Label.substr(pos: Range.Begin, n: Range.End - Range.Begin);
1415 P.Offsets.first = lspLength(Code: llvm::StringRef(Label).substr(Start: 0, N: Range.Begin));
1416 P.Offsets.second = lspLength(Code: llvm::StringRef(Label).substr(Start: 1, N: Range.End));
1417 }
1418 return AllOf(matchers: sigHelpLabeled(gmock_p0: Label), matchers: paramsAre(gmock_p0: Parameters));
1419}
1420
1421TEST(SignatureHelpTest, Overloads) {
1422 auto Results = signatures(Text: R"cpp(
1423 void foo(int x, int y);
1424 void foo(int x, float y);
1425 void foo(float x, int y);
1426 void foo(float x, float y);
1427 void bar(int x, int y = 0);
1428 int main() { foo(^); }
1429 )cpp");
1430 EXPECT_THAT(Results.signatures,
1431 UnorderedElementsAre(sig("foo([[float x]], [[float y]]) -> void"),
1432 sig("foo([[float x]], [[int y]]) -> void"),
1433 sig("foo([[int x]], [[float y]]) -> void"),
1434 sig("foo([[int x]], [[int y]]) -> void")));
1435 // We always prefer the first signature.
1436 EXPECT_EQ(0, Results.activeSignature);
1437 EXPECT_EQ(0, Results.activeParameter);
1438}
1439
1440TEST(SignatureHelpTest, FunctionPointers) {
1441 llvm::StringLiteral Tests[] = {
1442 // Variable of function pointer type
1443 R"cpp(
1444 void (*foo)(int x, int y);
1445 int main() { foo(^); }
1446 )cpp",
1447 // Wrapped in an AttributedType
1448 R"cpp(
1449 void (__stdcall *foo)(int x, int y);
1450 int main() { foo(^); }
1451 )cpp",
1452 // Another syntax for an AttributedType
1453 R"cpp(
1454 void (__attribute__(stdcall) *foo)(int x, int y);
1455 int main() { foo(^); },
1456 )cpp",
1457 // Wrapped in a typedef
1458 R"cpp(
1459 typedef void (*fn)(int x, int y);
1460 fn foo;
1461 int main() { foo(^); }
1462 )cpp",
1463 // Wrapped in both a typedef and an AttributedTyped
1464 R"cpp(
1465 typedef void (__stdcall *fn)(int x, int y);
1466 fn foo;
1467 int main() { foo(^); }
1468 )cpp",
1469 // Field of function pointer type
1470 R"cpp(
1471 struct S {
1472 void (*foo)(int x, int y);
1473 };
1474 S s;
1475 int main() { s.foo(^); }
1476 )cpp",
1477 // Field of function pointer typedef type
1478 R"cpp(
1479 typedef void (*fn)(int x, int y);
1480 struct S {
1481 fn foo;
1482 };
1483 S s;
1484 int main() { s.foo(^); }
1485 )cpp"};
1486 for (auto Test : Tests)
1487 EXPECT_THAT(signatures(Test).signatures,
1488 UnorderedElementsAre(sig("([[int x]], [[int y]]) -> void")));
1489}
1490
1491TEST(SignatureHelpTest, Constructors) {
1492 std::string Top = R"cpp(
1493 struct S {
1494 S(int);
1495 S(const S &) = delete;
1496 };
1497 )cpp";
1498
1499 auto CheckParenInit = [&](std::string Init) {
1500 EXPECT_THAT(signatures(Top + Init).signatures,
1501 UnorderedElementsAre(sig("S([[int]])")))
1502 << Init;
1503 };
1504 CheckParenInit("S s(^);");
1505 CheckParenInit("auto s = S(^);");
1506 CheckParenInit("auto s = new S(^);");
1507
1508 auto CheckBracedInit = [&](std::string Init) {
1509 EXPECT_THAT(signatures(Top + Init).signatures,
1510 UnorderedElementsAre(sig("S{[[int]]}")))
1511 << Init;
1512 };
1513 CheckBracedInit("S s{^};");
1514 CheckBracedInit("S s = {^};");
1515 CheckBracedInit("auto s = S{^};");
1516 // FIXME: doesn't work: no ExpectedType set in ParseCXXNewExpression.
1517 // CheckBracedInit("auto s = new S{^};");
1518 CheckBracedInit("int x(S); int i = x({^});");
1519}
1520
1521TEST(SignatureHelpTest, Aggregates) {
1522 std::string Top = R"cpp(
1523 struct S {
1524 int a, b, c, d;
1525 };
1526 )cpp";
1527 auto AggregateSig = sig(AnnotatedLabel: "S{[[int a]], [[int b]], [[int c]], [[int d]]}");
1528 EXPECT_THAT(signatures(Top + "S s{^}").signatures,
1529 UnorderedElementsAre(AggregateSig, sig("S{}"),
1530 sig("S{[[const S &]]}"),
1531 sig("S{[[S &&]]}")));
1532 EXPECT_THAT(signatures(Top + "S s{1,^}").signatures,
1533 ElementsAre(AggregateSig));
1534 EXPECT_EQ(signatures(Top + "S s{1,^}").activeParameter, 1);
1535 EXPECT_THAT(signatures(Top + "S s{.c=3,^}").signatures,
1536 ElementsAre(AggregateSig));
1537 EXPECT_EQ(signatures(Top + "S s{.c=3,^}").activeParameter, 3);
1538}
1539
1540TEST(SignatureHelpTest, OverloadInitListRegression) {
1541 auto Results = signatures(Text: R"cpp(
1542 struct A {int x;};
1543 struct B {B(A);};
1544 void f();
1545 int main() {
1546 B b({1});
1547 f(^);
1548 }
1549 )cpp");
1550 EXPECT_THAT(Results.signatures, UnorderedElementsAre(sig("f() -> void")));
1551}
1552
1553TEST(SignatureHelpTest, DefaultArgs) {
1554 auto Results = signatures(Text: R"cpp(
1555 void bar(int x, int y = 0);
1556 void bar(float x = 0, int y = 42);
1557 int main() { bar(^
1558 )cpp");
1559 EXPECT_THAT(Results.signatures,
1560 UnorderedElementsAre(
1561 sig("bar([[int x]], [[int y = 0]]) -> void"),
1562 sig("bar([[float x = 0]], [[int y = 42]]) -> void")));
1563 EXPECT_EQ(0, Results.activeSignature);
1564 EXPECT_EQ(0, Results.activeParameter);
1565}
1566
1567TEST(SignatureHelpTest, ActiveArg) {
1568 auto Results = signatures(Text: R"cpp(
1569 int baz(int a, int b, int c);
1570 int main() { baz(baz(1,2,3), ^); }
1571 )cpp");
1572 EXPECT_THAT(Results.signatures,
1573 ElementsAre(sig("baz([[int a]], [[int b]], [[int c]]) -> int")));
1574 EXPECT_EQ(0, Results.activeSignature);
1575 EXPECT_EQ(1, Results.activeParameter);
1576}
1577
1578TEST(SignatureHelpTest, OpeningParen) {
1579 llvm::StringLiteral Tests[] = {
1580 // Recursive function call.
1581 R"cpp(
1582 int foo(int a, int b, int c);
1583 int main() {
1584 foo(foo $p^( foo(10, 10, 10), ^ )));
1585 })cpp",
1586 // Functional type cast.
1587 R"cpp(
1588 struct Foo {
1589 Foo(int a, int b, int c);
1590 };
1591 int main() {
1592 Foo $p^( 10, ^ );
1593 })cpp",
1594 // New expression.
1595 R"cpp(
1596 struct Foo {
1597 Foo(int a, int b, int c);
1598 };
1599 int main() {
1600 new Foo $p^( 10, ^ );
1601 })cpp",
1602 // Macro expansion.
1603 R"cpp(
1604 int foo(int a, int b, int c);
1605 #define FOO foo(
1606
1607 int main() {
1608 // Macro expansions.
1609 $p^FOO 10, ^ );
1610 })cpp",
1611 // Macro arguments.
1612 R"cpp(
1613 int foo(int a, int b, int c);
1614 int main() {
1615 #define ID(X) X
1616 // FIXME: figure out why ID(foo (foo(10), )) doesn't work when preserving
1617 // the recovery expression.
1618 ID(foo $p^( 10, ^ ))
1619 })cpp",
1620 // Dependent args.
1621 R"cpp(
1622 int foo(int a, int b);
1623 template <typename T> void bar(T t) {
1624 foo$p^(t, ^t);
1625 })cpp",
1626 // Dependent args on templated func.
1627 R"cpp(
1628 template <typename T>
1629 int foo(T, T);
1630 template <typename T> void bar(T t) {
1631 foo$p^(t, ^t);
1632 })cpp",
1633 // Dependent args on member.
1634 R"cpp(
1635 struct Foo { int foo(int, int); };
1636 template <typename T> void bar(T t) {
1637 Foo f;
1638 f.foo$p^(t, ^t);
1639 })cpp",
1640 // Dependent args on templated member.
1641 R"cpp(
1642 struct Foo { template <typename T> int foo(T, T); };
1643 template <typename T> void bar(T t) {
1644 Foo f;
1645 f.foo$p^(t, ^t);
1646 })cpp",
1647 };
1648
1649 for (auto Test : Tests) {
1650 Annotations Code(Test);
1651 EXPECT_EQ(signatures(Code.code(), Code.point()).argListStart,
1652 Code.point("p"))
1653 << "Test source:" << Test;
1654 }
1655}
1656
1657TEST(SignatureHelpTest, StalePreamble) {
1658 TestTU TU;
1659 TU.Code = "";
1660 IgnoreDiagnostics Diags;
1661 MockFS FS;
1662 auto Inputs = TU.inputs(FS);
1663 auto CI = buildCompilerInvocation(Inputs, D&: Diags);
1664 ASSERT_TRUE(CI);
1665 auto EmptyPreamble = buildPreamble(FileName: testPath(File: TU.Filename), CI: *CI, Inputs,
1666 /*InMemory=*/StoreInMemory: true, /*Callback=*/PreambleCallback: nullptr);
1667 ASSERT_TRUE(EmptyPreamble);
1668
1669 TU.AdditionalFiles["a.h"] = "int foo(int x);";
1670 const Annotations Test(R"cpp(
1671 #include "a.h"
1672 void bar() { foo(^2); })cpp");
1673 TU.Code = Test.code().str();
1674 auto Results =
1675 signatureHelp(FileName: testPath(File: TU.Filename), Pos: Test.point(), Preamble: *EmptyPreamble,
1676 ParseInput: TU.inputs(FS), DocumentationFormat: MarkupKind::PlainText);
1677 EXPECT_THAT(Results.signatures, ElementsAre(sig("foo([[int x]]) -> int")));
1678 EXPECT_EQ(0, Results.activeSignature);
1679 EXPECT_EQ(0, Results.activeParameter);
1680}
1681
1682class IndexRequestCollector : public SymbolIndex {
1683public:
1684 IndexRequestCollector(std::vector<Symbol> Syms = {}) : Symbols(Syms) {}
1685
1686 bool
1687 fuzzyFind(const FuzzyFindRequest &Req,
1688 llvm::function_ref<void(const Symbol &)> Callback) const override {
1689 std::unique_lock<std::mutex> Lock(Mut);
1690 Requests.push_back(x: Req);
1691 ReceivedRequestCV.notify_one();
1692 for (const auto &Sym : Symbols)
1693 Callback(Sym);
1694 return true;
1695 }
1696
1697 void lookup(const LookupRequest &,
1698 llvm::function_ref<void(const Symbol &)>) const override {}
1699
1700 bool refs(const RefsRequest &,
1701 llvm::function_ref<void(const Ref &)>) const override {
1702 return false;
1703 }
1704
1705 void relations(const RelationsRequest &,
1706 llvm::function_ref<void(const SymbolID &, const Symbol &)>)
1707 const override {}
1708
1709 llvm::unique_function<IndexContents(llvm::StringRef) const>
1710 indexedFiles() const override {
1711 return [](llvm::StringRef) { return IndexContents::None; };
1712 }
1713
1714 // This is incorrect, but IndexRequestCollector is not an actual index and it
1715 // isn't used in production code.
1716 size_t estimateMemoryUsage() const override { return 0; }
1717
1718 const std::vector<FuzzyFindRequest> consumeRequests(size_t Num) const {
1719 std::unique_lock<std::mutex> Lock(Mut);
1720 EXPECT_TRUE(wait(Lock, ReceivedRequestCV, timeoutSeconds(30),
1721 [this, Num] { return Requests.size() == Num; }));
1722 auto Reqs = std::move(Requests);
1723 Requests = {};
1724 return Reqs;
1725 }
1726
1727private:
1728 std::vector<Symbol> Symbols;
1729 // We need a mutex to handle async fuzzy find requests.
1730 mutable std::condition_variable ReceivedRequestCV;
1731 mutable std::mutex Mut;
1732 mutable std::vector<FuzzyFindRequest> Requests;
1733};
1734
1735// Clients have to consume exactly Num requests.
1736std::vector<FuzzyFindRequest> captureIndexRequests(llvm::StringRef Code,
1737 size_t Num = 1) {
1738 clangd::CodeCompleteOptions Opts;
1739 IndexRequestCollector Requests;
1740 Opts.Index = &Requests;
1741 completions(Text: Code, IndexSymbols: {}, Opts);
1742 const auto Reqs = Requests.consumeRequests(Num);
1743 EXPECT_EQ(Reqs.size(), Num);
1744 return Reqs;
1745}
1746
1747TEST(CompletionTest, UnqualifiedIdQuery) {
1748 auto Requests = captureIndexRequests(Code: R"cpp(
1749 namespace std {}
1750 using namespace std;
1751 namespace ns {
1752 void f() {
1753 vec^
1754 }
1755 }
1756 )cpp");
1757
1758 EXPECT_THAT(Requests,
1759 ElementsAre(Field(&FuzzyFindRequest::Scopes,
1760 UnorderedElementsAre("", "ns::", "std::"))));
1761}
1762
1763TEST(CompletionTest, EnclosingScopeComesFirst) {
1764 auto Requests = captureIndexRequests(Code: R"cpp(
1765 namespace std {}
1766 using namespace std;
1767 namespace nx {
1768 namespace ns {
1769 namespace {
1770 void f() {
1771 vec^
1772 }
1773 }
1774 }
1775 }
1776 )cpp");
1777
1778 EXPECT_THAT(Requests,
1779 ElementsAre(Field(
1780 &FuzzyFindRequest::Scopes,
1781 UnorderedElementsAre("", "std::", "nx::ns::", "nx::"))));
1782 EXPECT_EQ(Requests[0].Scopes[0], "nx::ns::");
1783}
1784
1785TEST(CompletionTest, ResolvedQualifiedIdQuery) {
1786 auto Requests = captureIndexRequests(Code: R"cpp(
1787 namespace ns1 {}
1788 namespace ns2 {} // ignore
1789 namespace ns3 { namespace nns3 {} }
1790 namespace foo {
1791 using namespace ns1;
1792 using namespace ns3::nns3;
1793 }
1794 namespace ns {
1795 void f() {
1796 foo::^
1797 }
1798 }
1799 )cpp");
1800
1801 EXPECT_THAT(Requests,
1802 ElementsAre(Field(
1803 &FuzzyFindRequest::Scopes,
1804 UnorderedElementsAre("foo::", "ns1::", "ns3::nns3::"))));
1805}
1806
1807TEST(CompletionTest, UnresolvedQualifierIdQuery) {
1808 auto Requests = captureIndexRequests(Code: R"cpp(
1809 namespace a {}
1810 using namespace a;
1811 namespace ns {
1812 void f() {
1813 bar::^
1814 }
1815 } // namespace ns
1816 )cpp");
1817
1818 EXPECT_THAT(Requests,
1819 ElementsAre(Field(
1820 &FuzzyFindRequest::Scopes,
1821 UnorderedElementsAre("a::bar::", "ns::bar::", "bar::"))));
1822}
1823
1824TEST(CompletionTest, UnresolvedNestedQualifierIdQuery) {
1825 auto Requests = captureIndexRequests(Code: R"cpp(
1826 namespace a {}
1827 using namespace a;
1828 namespace ns {
1829 void f() {
1830 ::a::bar::^
1831 }
1832 } // namespace ns
1833 )cpp");
1834
1835 EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
1836 UnorderedElementsAre("a::bar::"))));
1837}
1838
1839TEST(CompletionTest, EmptyQualifiedQuery) {
1840 auto Requests = captureIndexRequests(Code: R"cpp(
1841 namespace ns {
1842 void f() {
1843 ^
1844 }
1845 } // namespace ns
1846 )cpp");
1847
1848 EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
1849 UnorderedElementsAre("", "ns::"))));
1850}
1851
1852TEST(CompletionTest, GlobalQualifiedQuery) {
1853 auto Requests = captureIndexRequests(Code: R"cpp(
1854 namespace ns {
1855 void f() {
1856 ::^
1857 }
1858 } // namespace ns
1859 )cpp");
1860
1861 EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
1862 UnorderedElementsAre(""))));
1863}
1864
1865TEST(CompletionTest, NoDuplicatedQueryScopes) {
1866 auto Requests = captureIndexRequests(Code: R"cpp(
1867 namespace {}
1868
1869 namespace na {
1870 namespace {}
1871 namespace nb {
1872 ^
1873 } // namespace nb
1874 } // namespace na
1875 )cpp");
1876
1877 EXPECT_THAT(Requests,
1878 ElementsAre(Field(&FuzzyFindRequest::Scopes,
1879 UnorderedElementsAre("na::", "na::nb::", ""))));
1880}
1881
1882TEST(CompletionTest, NoIndexCompletionsInsideClasses) {
1883 auto Completions = completions(
1884 Text: R"cpp(
1885 struct Foo {
1886 int SomeNameOfField;
1887 typedef int SomeNameOfTypedefField;
1888 };
1889
1890 Foo::^)cpp",
1891 IndexSymbols: {func(Name: "::SomeNameInTheIndex"), func(Name: "::Foo::SomeNameInTheIndex")});
1892
1893 EXPECT_THAT(Completions.Completions,
1894 AllOf(Contains(labeled("SomeNameOfField")),
1895 Contains(labeled("SomeNameOfTypedefField")),
1896 Not(Contains(labeled("SomeNameInTheIndex")))));
1897}
1898
1899TEST(CompletionTest, NoIndexCompletionsInsideDependentCode) {
1900 {
1901 auto Completions = completions(
1902 Text: R"cpp(
1903 template <class T>
1904 void foo() {
1905 T::^
1906 }
1907 )cpp",
1908 IndexSymbols: {func(Name: "::SomeNameInTheIndex")});
1909
1910 EXPECT_THAT(Completions.Completions,
1911 Not(Contains(labeled("SomeNameInTheIndex"))));
1912 }
1913
1914 {
1915 auto Completions = completions(
1916 Text: R"cpp(
1917 template <class T>
1918 void foo() {
1919 T::template Y<int>::^
1920 }
1921 )cpp",
1922 IndexSymbols: {func(Name: "::SomeNameInTheIndex")});
1923
1924 EXPECT_THAT(Completions.Completions,
1925 Not(Contains(labeled("SomeNameInTheIndex"))));
1926 }
1927
1928 {
1929 auto Completions = completions(
1930 Text: R"cpp(
1931 template <class T>
1932 void foo() {
1933 T::foo::^
1934 }
1935 )cpp",
1936 IndexSymbols: {func(Name: "::SomeNameInTheIndex")});
1937
1938 EXPECT_THAT(Completions.Completions,
1939 Not(Contains(labeled("SomeNameInTheIndex"))));
1940 }
1941}
1942
1943TEST(CompletionTest, OverloadBundling) {
1944 clangd::CodeCompleteOptions Opts;
1945 Opts.BundleOverloads = true;
1946
1947 std::string Context = R"cpp(
1948 struct X {
1949 // Overload with int
1950 int a(int) __attribute__((deprecated("", "")));
1951 // Overload with bool
1952 int a(bool);
1953 int b(float);
1954
1955 X(int);
1956 X(float);
1957 };
1958 int GFuncC(int);
1959 int GFuncD(int);
1960 )cpp";
1961
1962 // Member completions are bundled.
1963 EXPECT_THAT(completions(Context + "int y = X().^", {}, Opts).Completions,
1964 UnorderedElementsAre(labeled("a(…)"), labeled("b(float)")));
1965
1966 // Constructor completions are bundled.
1967 EXPECT_THAT(completions(Context + "X z = X^", {}, Opts).Completions,
1968 UnorderedElementsAre(labeled("X"), labeled("X(…)")));
1969
1970 // Non-member completions are bundled, including index+sema.
1971 Symbol NoArgsGFunc = func(Name: "GFuncC");
1972 EXPECT_THAT(
1973 completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions,
1974 UnorderedElementsAre(labeled("GFuncC(…)"), labeled("GFuncD(int)")));
1975
1976 // Differences in header-to-insert suppress bundling.
1977 std::string DeclFile = URI::create(AbsolutePath: testPath(File: "foo")).toString();
1978 NoArgsGFunc.CanonicalDeclaration.FileURI = DeclFile.c_str();
1979 NoArgsGFunc.IncludeHeaders.emplace_back(Args: "<foo>", Args: 1, Args: Symbol::Include);
1980 EXPECT_THAT(
1981 completions(Context + "int y = GFunc^", {NoArgsGFunc}, Opts).Completions,
1982 UnorderedElementsAre(AllOf(named("GFuncC"), insertInclude("<foo>")),
1983 labeled("GFuncC(int)"), labeled("GFuncD(int)")));
1984
1985 // Examine a bundled completion in detail.
1986 auto A =
1987 completions(Text: Context + "int y = X().a^", IndexSymbols: {}, Opts).Completions.front();
1988 EXPECT_EQ(A.Name, "a");
1989 EXPECT_EQ(A.Signature, "(…)");
1990 EXPECT_EQ(A.BundleSize, 2u);
1991 EXPECT_EQ(A.Kind, CompletionItemKind::Method);
1992 EXPECT_EQ(A.ReturnType, "int"); // All overloads return int.
1993 // For now we just return one of the doc strings arbitrarily.
1994 ASSERT_TRUE(A.Documentation);
1995 ASSERT_FALSE(A.Deprecated); // Not all overloads deprecated.
1996 EXPECT_THAT(
1997 A.Documentation->asPlainText(),
1998 AnyOf(HasSubstr("Overload with int"), HasSubstr("Overload with bool")));
1999 EXPECT_EQ(A.SnippetSuffix, "($0)");
2000}
2001
2002TEST(CompletionTest, OverloadBundlingSameFileDifferentURI) {
2003 clangd::CodeCompleteOptions Opts;
2004 Opts.BundleOverloads = true;
2005
2006 Symbol SymX = sym(QName: "ns::X", Kind: index::SymbolKind::Function, USRFormat: "@F@\\0#");
2007 Symbol SymY = sym(QName: "ns::X", Kind: index::SymbolKind::Function, USRFormat: "@F@\\0#I#");
2008 std::string BarHeader = testPath(File: "bar.h");
2009 auto BarURI = URI::create(AbsolutePath: BarHeader).toString();
2010 SymX.CanonicalDeclaration.FileURI = BarURI.c_str();
2011 SymY.CanonicalDeclaration.FileURI = BarURI.c_str();
2012 // The include header is different, but really it's the same file.
2013 SymX.IncludeHeaders.emplace_back(Args: "\"bar.h\"", Args: 1, Args: Symbol::Include);
2014 SymY.IncludeHeaders.emplace_back(Args: BarURI.c_str(), Args: 1, Args: Symbol::Include);
2015
2016 auto Results = completions(Text: "void f() { ::ns::^ }", IndexSymbols: {SymX, SymY}, Opts);
2017 // Expect both results are bundled, despite the different-but-same
2018 // IncludeHeader.
2019 ASSERT_EQ(1u, Results.Completions.size());
2020 const auto &R = Results.Completions.front();
2021 EXPECT_EQ("X", R.Name);
2022 EXPECT_EQ(2u, R.BundleSize);
2023}
2024
2025TEST(CompletionTest, DocumentationFromChangedFileCrash) {
2026 MockFS FS;
2027 auto FooH = testPath(File: "foo.h");
2028 auto FooCpp = testPath(File: "foo.cpp");
2029 FS.Files[FooH] = R"cpp(
2030 // this is my documentation comment.
2031 int func();
2032 )cpp";
2033 FS.Files[FooCpp] = "";
2034
2035 MockCompilationDatabase CDB;
2036 ClangdServer Server(CDB, FS, ClangdServer::optsForTest());
2037
2038 Annotations Source(R"cpp(
2039 #include "foo.h"
2040 int func() {
2041 // This makes sure we have func from header in the AST.
2042 }
2043 int a = fun^
2044 )cpp");
2045 Server.addDocument(File: FooCpp, Contents: Source.code(), Version: "null", WD: WantDiagnostics::Yes);
2046 // We need to wait for preamble to build.
2047 ASSERT_TRUE(Server.blockUntilIdleForTest());
2048
2049 // Change the header file. Completion will reuse the old preamble!
2050 FS.Files[FooH] = R"cpp(
2051 int func();
2052 )cpp";
2053
2054 clangd::CodeCompleteOptions Opts;
2055 CodeCompleteResult Completions =
2056 cantFail(ValOrErr: runCodeComplete(Server, File: FooCpp, Pos: Source.point(), Opts));
2057 // We shouldn't crash. Unfortunately, current workaround is to not produce
2058 // comments for symbols from headers.
2059 EXPECT_THAT(Completions.Completions,
2060 Contains(AllOf(Not(isDocumented()), named("func"))));
2061}
2062
2063TEST(CompletionTest, NonDocComments) {
2064 const char *Text = R"cpp(
2065 // We ignore namespace comments, for rationale see CodeCompletionStrings.h.
2066 namespace comments_ns {
2067 }
2068
2069 // ------------------
2070 int comments_foo();
2071
2072 // A comment and a decl are separated by newlines.
2073 // Therefore, the comment shouldn't show up as doc comment.
2074
2075 int comments_bar();
2076
2077 // this comment should be in the results.
2078 int comments_baz();
2079
2080
2081 template <class T>
2082 struct Struct {
2083 int comments_qux();
2084 int comments_quux();
2085 };
2086
2087
2088 // This comment should not be there.
2089
2090 template <class T>
2091 int Struct<T>::comments_qux() {
2092 }
2093
2094 // This comment **should** be in results.
2095 template <class T>
2096 int Struct<T>::comments_quux() {
2097 int a = comments^;
2098 }
2099 )cpp";
2100
2101 // We should not get any of those comments in completion.
2102 EXPECT_THAT(
2103 completions(Text).Completions,
2104 UnorderedElementsAre(AllOf(Not(isDocumented()), named("comments_foo")),
2105 AllOf(isDocumented(), named("comments_baz")),
2106 AllOf(isDocumented(), named("comments_quux")),
2107 AllOf(Not(isDocumented()), named("comments_ns")),
2108 // FIXME(ibiryukov): the following items should have
2109 // empty documentation, since they are separated from
2110 // a comment with an empty line. Unfortunately, I
2111 // couldn't make Sema tests pass if we ignore those.
2112 AllOf(isDocumented(), named("comments_bar")),
2113 AllOf(isDocumented(), named("comments_qux"))));
2114}
2115
2116TEST(CompletionTest, CompleteOnInvalidLine) {
2117 auto FooCpp = testPath(File: "foo.cpp");
2118
2119 MockCompilationDatabase CDB;
2120 MockFS FS;
2121 FS.Files[FooCpp] = "// empty file";
2122
2123 ClangdServer Server(CDB, FS, ClangdServer::optsForTest());
2124 // Run completion outside the file range.
2125 Position Pos;
2126 Pos.line = 100;
2127 Pos.character = 0;
2128 EXPECT_THAT_EXPECTED(
2129 runCodeComplete(Server, FooCpp, Pos, clangd::CodeCompleteOptions()),
2130 Failed());
2131}
2132
2133TEST(CompletionTest, QualifiedNames) {
2134 auto Results = completions(
2135 Text: R"cpp(
2136 namespace ns { int local; void both(); }
2137 void f() { ::ns::^ }
2138 )cpp",
2139 IndexSymbols: {func(Name: "ns::both"), cls(Name: "ns::Index")});
2140 // We get results from both index and sema, with no duplicates.
2141 EXPECT_THAT(
2142 Results.Completions,
2143 UnorderedElementsAre(scope("ns::"), scope("ns::"), scope("ns::")));
2144}
2145
2146TEST(CompletionTest, Render) {
2147 CodeCompletion C;
2148 C.Name = "x";
2149 C.FilterText = "x";
2150 C.Signature = "(bool) const";
2151 C.SnippetSuffix = "(${0:bool})";
2152 C.ReturnType = "int";
2153 C.RequiredQualifier = "Foo::";
2154 C.Scope = "ns::Foo::";
2155 C.Documentation.emplace();
2156 C.Documentation->addParagraph().appendText(Text: "This is ").appendCode(Code: "x()");
2157 C.Includes.emplace_back();
2158 auto &Include = C.Includes.back();
2159 Include.Header = "\"foo.h\"";
2160 C.Kind = CompletionItemKind::Method;
2161 C.Score.Total = 1.0;
2162 C.Score.ExcludingName = .5;
2163 C.Origin = SymbolOrigin::AST | SymbolOrigin::Static;
2164
2165 CodeCompleteOptions Opts;
2166 Opts.IncludeIndicator.Insert = "^";
2167 Opts.IncludeIndicator.NoInsert = "";
2168 Opts.EnableSnippets = false;
2169
2170 auto R = C.render(Opts);
2171 EXPECT_EQ(R.label, "Foo::x");
2172 EXPECT_EQ(R.labelDetails->detail, "(bool) const");
2173 EXPECT_EQ(R.insertText, "Foo::x");
2174 EXPECT_EQ(R.insertTextFormat, InsertTextFormat::PlainText);
2175 EXPECT_EQ(R.filterText, "x");
2176 EXPECT_EQ(R.detail, "int");
2177 EXPECT_EQ(R.documentation->value, "From \"foo.h\"\nThis is x()");
2178 EXPECT_THAT(R.additionalTextEdits, IsEmpty());
2179 EXPECT_EQ(R.sortText, sortText(1.0, "x"));
2180 EXPECT_FALSE(R.deprecated);
2181 EXPECT_EQ(R.score, .5f);
2182
2183 C.FilterText = "xtra";
2184 R = C.render(Opts);
2185 EXPECT_EQ(R.filterText, "xtra");
2186 EXPECT_EQ(R.sortText, sortText(1.0, "xtra"));
2187
2188 Opts.EnableSnippets = true;
2189 R = C.render(Opts);
2190 EXPECT_EQ(R.insertText, "Foo::x(${0:bool})");
2191 EXPECT_EQ(R.insertTextFormat, InsertTextFormat::Snippet);
2192
2193 C.SnippetSuffix = "";
2194 R = C.render(Opts);
2195 EXPECT_EQ(R.insertText, "Foo::x");
2196 EXPECT_EQ(R.insertTextFormat, InsertTextFormat::PlainText);
2197
2198 Include.Insertion.emplace();
2199 R = C.render(Opts);
2200 EXPECT_EQ(R.label, "^Foo::x");
2201 EXPECT_EQ(R.labelDetails->detail, "(bool) const");
2202 EXPECT_THAT(R.additionalTextEdits, Not(IsEmpty()));
2203
2204 Opts.ShowOrigins = true;
2205 R = C.render(Opts);
2206 EXPECT_EQ(R.label, "^[AS]Foo::x");
2207 EXPECT_EQ(R.labelDetails->detail, "(bool) const");
2208
2209 C.BundleSize = 2;
2210 R = C.render(Opts);
2211 EXPECT_EQ(R.detail, "[2 overloads]");
2212 EXPECT_EQ(R.documentation->value, "From \"foo.h\"\nThis is x()");
2213
2214 C.Deprecated = true;
2215 R = C.render(Opts);
2216 EXPECT_TRUE(R.deprecated);
2217
2218 Opts.DocumentationFormat = MarkupKind::Markdown;
2219 R = C.render(Opts);
2220 EXPECT_EQ(R.documentation->value, "From `\"foo.h\"` \nThis is `x()`");
2221}
2222
2223TEST(CompletionTest, IgnoreRecoveryResults) {
2224 auto Results = completions(
2225 Text: R"cpp(
2226 namespace ns { int NotRecovered() { return 0; } }
2227 void f() {
2228 // Sema enters recovery mode first and then normal mode.
2229 if (auto x = ns::NotRecover^)
2230 }
2231 )cpp");
2232 EXPECT_THAT(Results.Completions, UnorderedElementsAre(named("NotRecovered")));
2233}
2234
2235TEST(CompletionTest, ScopeOfClassFieldInConstructorInitializer) {
2236 auto Results = completions(
2237 Text: R"cpp(
2238 namespace ns {
2239 class X { public: X(); int x_; };
2240 X::X() : x_^(0) {}
2241 }
2242 )cpp");
2243 EXPECT_THAT(Results.Completions,
2244 UnorderedElementsAre(AllOf(scope("ns::X::"), named("x_"))));
2245}
2246
2247// Like other class members, constructor init lists have to parse what's below,
2248// after the completion point.
2249// But recovering from an incomplete constructor init list is particularly
2250// tricky because the bulk of the list is not surrounded by brackets.
2251TEST(CompletionTest, ConstructorInitListIncomplete) {
2252 auto Results = completions(
2253 Text: R"cpp(
2254 namespace ns {
2255 struct X {
2256 X() : x^
2257 int xyz_;
2258 };
2259 }
2260 )cpp");
2261 EXPECT_THAT(Results.Completions, ElementsAre(named("xyz_")));
2262
2263 Results = completions(
2264 Text: R"cpp(
2265 int foo();
2266
2267 namespace ns {
2268 struct X {
2269 X() : xyz_(fo^
2270 int xyz_;
2271 };
2272 }
2273 )cpp");
2274 EXPECT_THAT(Results.Completions, ElementsAre(named("foo")));
2275}
2276
2277TEST(CompletionTest, CodeCompletionContext) {
2278 auto Results = completions(
2279 Text: R"cpp(
2280 namespace ns {
2281 class X { public: X(); int x_; };
2282 void f() {
2283 X x;
2284 x.^;
2285 }
2286 }
2287 )cpp");
2288
2289 EXPECT_THAT(Results.Context, CodeCompletionContext::CCC_DotMemberAccess);
2290}
2291
2292TEST(CompletionTest, FixItForArrowToDot) {
2293 MockFS FS;
2294 MockCompilationDatabase CDB;
2295
2296 CodeCompleteOptions Opts;
2297 Opts.IncludeFixIts = true;
2298 const char *Code =
2299 R"cpp(
2300 class Auxilary {
2301 public:
2302 void AuxFunction();
2303 };
2304 class ClassWithPtr {
2305 public:
2306 void MemberFunction();
2307 Auxilary* operator->() const;
2308 Auxilary* Aux;
2309 };
2310 void f() {
2311 ClassWithPtr x;
2312 x[[->]]^;
2313 }
2314 )cpp";
2315 auto Results = completions(Text: Code, IndexSymbols: {}, Opts);
2316 EXPECT_EQ(Results.Completions.size(), 3u);
2317
2318 TextEdit ReplacementEdit;
2319 ReplacementEdit.range = Annotations(Code).range();
2320 ReplacementEdit.newText = ".";
2321 for (const auto &C : Results.Completions) {
2322 EXPECT_TRUE(C.FixIts.size() == 1u || C.Name == "AuxFunction");
2323 if (!C.FixIts.empty()) {
2324 EXPECT_THAT(C.FixIts, ElementsAre(ReplacementEdit));
2325 }
2326 }
2327}
2328
2329TEST(CompletionTest, FixItForDotToArrow) {
2330 CodeCompleteOptions Opts;
2331 Opts.IncludeFixIts = true;
2332 const char *Code =
2333 R"cpp(
2334 class Auxilary {
2335 public:
2336 void AuxFunction();
2337 };
2338 class ClassWithPtr {
2339 public:
2340 void MemberFunction();
2341 Auxilary* operator->() const;
2342 Auxilary* Aux;
2343 };
2344 void f() {
2345 ClassWithPtr x;
2346 x[[.]]^;
2347 }
2348 )cpp";
2349 auto Results = completions(Text: Code, IndexSymbols: {}, Opts);
2350 EXPECT_EQ(Results.Completions.size(), 3u);
2351
2352 TextEdit ReplacementEdit;
2353 ReplacementEdit.range = Annotations(Code).range();
2354 ReplacementEdit.newText = "->";
2355 for (const auto &C : Results.Completions) {
2356 EXPECT_TRUE(C.FixIts.empty() || C.Name == "AuxFunction");
2357 if (!C.FixIts.empty()) {
2358 EXPECT_THAT(C.FixIts, ElementsAre(ReplacementEdit));
2359 }
2360 }
2361}
2362
2363TEST(CompletionTest, RenderWithFixItMerged) {
2364 TextEdit FixIt;
2365 FixIt.range.end.character = 5;
2366 FixIt.newText = "->";
2367
2368 CodeCompletion C;
2369 C.Name = "x";
2370 C.RequiredQualifier = "Foo::";
2371 C.FixIts = {FixIt};
2372 C.CompletionTokenRange.start.character = 5;
2373
2374 CodeCompleteOptions Opts;
2375 Opts.IncludeFixIts = true;
2376
2377 auto R = C.render(Opts);
2378 EXPECT_TRUE(R.textEdit);
2379 EXPECT_EQ(R.textEdit->newText, "->Foo::x");
2380 EXPECT_TRUE(R.additionalTextEdits.empty());
2381}
2382
2383TEST(CompletionTest, RenderWithFixItNonMerged) {
2384 TextEdit FixIt;
2385 FixIt.range.end.character = 4;
2386 FixIt.newText = "->";
2387
2388 CodeCompletion C;
2389 C.Name = "x";
2390 C.RequiredQualifier = "Foo::";
2391 C.FixIts = {FixIt};
2392 C.CompletionTokenRange.start.character = 5;
2393
2394 CodeCompleteOptions Opts;
2395 Opts.IncludeFixIts = true;
2396
2397 auto R = C.render(Opts);
2398 EXPECT_TRUE(R.textEdit);
2399 EXPECT_EQ(R.textEdit->newText, "Foo::x");
2400 EXPECT_THAT(R.additionalTextEdits, UnorderedElementsAre(FixIt));
2401}
2402
2403TEST(CompletionTest, CompletionTokenRange) {
2404 MockFS FS;
2405 MockCompilationDatabase CDB;
2406 TestTU TU;
2407 TU.AdditionalFiles["foo/abc/foo.h"] = "";
2408
2409 constexpr const char *TestCodes[] = {
2410 R"cpp(
2411 class Auxilary {
2412 public:
2413 void AuxFunction();
2414 };
2415 void f() {
2416 Auxilary x;
2417 x.[[Aux]]^;
2418 }
2419 )cpp",
2420 R"cpp(
2421 class Auxilary {
2422 public:
2423 void AuxFunction();
2424 };
2425 void f() {
2426 Auxilary x;
2427 x.[[]]^;
2428 }
2429 )cpp",
2430 R"cpp(
2431 #include "foo/[[a^/]]foo.h"
2432 )cpp",
2433 R"cpp(
2434 #include "foo/abc/[[fo^o.h"]]
2435 )cpp",
2436 };
2437 for (const auto &Text : TestCodes) {
2438 Annotations TestCode(Text);
2439 TU.Code = TestCode.code().str();
2440 auto Results = completions(TU, Point: TestCode.point());
2441 if (Results.Completions.size() != 1) {
2442 ADD_FAILURE() << "Results.Completions.size() != 1" << Text;
2443 continue;
2444 }
2445 EXPECT_THAT(Results.Completions.front().CompletionTokenRange,
2446 TestCode.range());
2447 }
2448}
2449
2450TEST(SignatureHelpTest, OverloadsOrdering) {
2451 const auto Results = signatures(Text: R"cpp(
2452 void foo(int x);
2453 void foo(int x, float y);
2454 void foo(float x, int y);
2455 void foo(float x, float y);
2456 void foo(int x, int y = 0);
2457 int main() { foo(^); }
2458 )cpp");
2459 EXPECT_THAT(Results.signatures,
2460 ElementsAre(sig("foo([[int x]]) -> void"),
2461 sig("foo([[int x]], [[int y = 0]]) -> void"),
2462 sig("foo([[float x]], [[int y]]) -> void"),
2463 sig("foo([[int x]], [[float y]]) -> void"),
2464 sig("foo([[float x]], [[float y]]) -> void")));
2465 // We always prefer the first signature.
2466 EXPECT_EQ(0, Results.activeSignature);
2467 EXPECT_EQ(0, Results.activeParameter);
2468}
2469
2470TEST(SignatureHelpTest, InstantiatedSignatures) {
2471 StringRef Sig0 = R"cpp(
2472 template <class T>
2473 void foo(T, T, T);
2474
2475 int main() {
2476 foo<int>(^);
2477 }
2478 )cpp";
2479
2480 EXPECT_THAT(signatures(Sig0).signatures,
2481 ElementsAre(sig("foo([[T]], [[T]], [[T]]) -> void")));
2482
2483 StringRef Sig1 = R"cpp(
2484 template <class T>
2485 void foo(T, T, T);
2486
2487 int main() {
2488 foo(10, ^);
2489 })cpp";
2490
2491 EXPECT_THAT(signatures(Sig1).signatures,
2492 ElementsAre(sig("foo([[T]], [[T]], [[T]]) -> void")));
2493
2494 StringRef Sig2 = R"cpp(
2495 template <class ...T>
2496 void foo(T...);
2497
2498 int main() {
2499 foo<int>(^);
2500 }
2501 )cpp";
2502
2503 EXPECT_THAT(signatures(Sig2).signatures,
2504 ElementsAre(sig("foo([[T...]]) -> void")));
2505
2506 // It is debatable whether we should substitute the outer template parameter
2507 // ('T') in that case. Currently we don't substitute it in signature help, but
2508 // do substitute in code complete.
2509 // FIXME: make code complete and signature help consistent, figure out which
2510 // way is better.
2511 StringRef Sig3 = R"cpp(
2512 template <class T>
2513 struct X {
2514 template <class U>
2515 void foo(T, U);
2516 };
2517
2518 int main() {
2519 X<int>().foo<double>(^)
2520 }
2521 )cpp";
2522
2523 EXPECT_THAT(signatures(Sig3).signatures,
2524 ElementsAre(sig("foo([[T]], [[U]]) -> void")));
2525}
2526
2527TEST(SignatureHelpTest, IndexDocumentation) {
2528 Symbol Foo0 = sym(QName: "foo", Kind: index::SymbolKind::Function, USRFormat: "@F@\\0#");
2529 Foo0.Documentation = "doc from the index";
2530 Symbol Foo1 = sym(QName: "foo", Kind: index::SymbolKind::Function, USRFormat: "@F@\\0#I#");
2531 Foo1.Documentation = "doc from the index";
2532 Symbol Foo2 = sym(QName: "foo", Kind: index::SymbolKind::Function, USRFormat: "@F@\\0#I#I#");
2533
2534 StringRef Sig0 = R"cpp(
2535 int foo();
2536 int foo(double);
2537
2538 void test() {
2539 foo(^);
2540 }
2541 )cpp";
2542
2543 EXPECT_THAT(
2544 signatures(Sig0, {Foo0}).signatures,
2545 ElementsAre(AllOf(sig("foo() -> int"), sigDoc("doc from the index")),
2546 AllOf(sig("foo([[double]]) -> int"), sigDoc(""))));
2547
2548 StringRef Sig1 = R"cpp(
2549 int foo();
2550 // Overriden doc from sema
2551 int foo(int);
2552 // doc from sema
2553 int foo(int, int);
2554
2555 void test() {
2556 foo(^);
2557 }
2558 )cpp";
2559
2560 EXPECT_THAT(
2561 signatures(Sig1, {Foo0, Foo1, Foo2}).signatures,
2562 ElementsAre(
2563 AllOf(sig("foo() -> int"), sigDoc("doc from the index")),
2564 AllOf(sig("foo([[int]]) -> int"), sigDoc("Overriden doc from sema")),
2565 AllOf(sig("foo([[int]], [[int]]) -> int"), sigDoc("doc from sema"))));
2566}
2567
2568TEST(SignatureHelpTest, DynamicIndexDocumentation) {
2569 MockFS FS;
2570 MockCompilationDatabase CDB;
2571 ClangdServer::Options Opts = ClangdServer::optsForTest();
2572 Opts.BuildDynamicSymbolIndex = true;
2573 ClangdServer Server(CDB, FS, Opts);
2574
2575 FS.Files[testPath(File: "foo.h")] = R"cpp(
2576 struct Foo {
2577 // Member doc
2578 int foo();
2579 };
2580 )cpp";
2581 Annotations FileContent(R"cpp(
2582 #include "foo.h"
2583 void test() {
2584 Foo f;
2585 f.foo(^);
2586 }
2587 )cpp");
2588 auto File = testPath(File: "test.cpp");
2589 Server.addDocument(File, Contents: FileContent.code());
2590 // Wait for the dynamic index being built.
2591 ASSERT_TRUE(Server.blockUntilIdleForTest());
2592 EXPECT_THAT(llvm::cantFail(runSignatureHelp(Server, File, FileContent.point(),
2593 MarkupKind::PlainText))
2594 .signatures,
2595 ElementsAre(AllOf(sig("foo() -> int"), sigDoc("Member doc"))));
2596}
2597
2598TEST(CompletionTest, CompletionFunctionArgsDisabled) {
2599 CodeCompleteOptions Opts;
2600 Opts.EnableSnippets = true;
2601 Opts.EnableFunctionArgSnippets = false;
2602
2603 {
2604 auto Results = completions(
2605 Text: R"cpp(
2606 void xfoo();
2607 void xfoo(int x, int y);
2608 void f() { xfo^ })cpp",
2609 IndexSymbols: {}, Opts);
2610 EXPECT_THAT(
2611 Results.Completions,
2612 UnorderedElementsAre(AllOf(named("xfoo"), snippetSuffix("()")),
2613 AllOf(named("xfoo"), snippetSuffix("($0)"))));
2614 }
2615 {
2616 auto Results = completions(
2617 Text: R"cpp(
2618 void xbar();
2619 void f() { xba^ })cpp",
2620 IndexSymbols: {}, Opts);
2621 EXPECT_THAT(Results.Completions, UnorderedElementsAre(AllOf(
2622 named("xbar"), snippetSuffix("()"))));
2623 }
2624 {
2625 Opts.BundleOverloads = true;
2626 auto Results = completions(
2627 Text: R"cpp(
2628 void xfoo();
2629 void xfoo(int x, int y);
2630 void f() { xfo^ })cpp",
2631 IndexSymbols: {}, Opts);
2632 EXPECT_THAT(
2633 Results.Completions,
2634 UnorderedElementsAre(AllOf(named("xfoo"), snippetSuffix("($0)"))));
2635 }
2636 {
2637 auto Results = completions(
2638 Text: R"cpp(
2639 template <class T, class U>
2640 void xfoo(int a, U b);
2641 void f() { xfo^ })cpp",
2642 IndexSymbols: {}, Opts);
2643 EXPECT_THAT(
2644 Results.Completions,
2645 UnorderedElementsAre(AllOf(named("xfoo"), snippetSuffix("<$1>($0)"))));
2646 }
2647 {
2648 auto Results = completions(
2649 Text: R"cpp(
2650 template <class T>
2651 class foo_class{};
2652 template <class T>
2653 using foo_alias = T**;
2654 template <class T>
2655 T foo_var = T{};
2656 void f() { foo_^ })cpp",
2657 IndexSymbols: {}, Opts);
2658 EXPECT_THAT(
2659 Results.Completions,
2660 UnorderedElementsAre(AllOf(named("foo_class"), snippetSuffix("<$0>")),
2661 AllOf(named("foo_alias"), snippetSuffix("<$0>")),
2662 AllOf(named("foo_var"), snippetSuffix("<$0>"))));
2663 }
2664 {
2665 auto Results = completions(
2666 Text: R"cpp(
2667 #define FOO(x, y) x##f
2668 FO^ )cpp",
2669 IndexSymbols: {}, Opts);
2670 EXPECT_THAT(Results.Completions, UnorderedElementsAre(AllOf(
2671 named("FOO"), snippetSuffix("($0)"))));
2672 }
2673}
2674
2675TEST(CompletionTest, SuggestOverrides) {
2676 constexpr const char *const Text(R"cpp(
2677 class A {
2678 public:
2679 virtual void vfunc(bool param);
2680 virtual void vfunc(bool param, int p);
2681 void func(bool param);
2682 };
2683 class B : public A {
2684 virtual void ttt(bool param) const;
2685 void vfunc(bool param, int p) override;
2686 };
2687 class C : public B {
2688 public:
2689 void vfunc(bool param) override;
2690 ^
2691 };
2692 )cpp");
2693 const auto Results = completions(Text);
2694 EXPECT_THAT(
2695 Results.Completions,
2696 AllOf(Contains(AllOf(labeled("void vfunc(bool param, int p) override"),
2697 nameStartsWith("vfunc"))),
2698 Contains(AllOf(labeled("void ttt(bool param) const override"),
2699 nameStartsWith("ttt"))),
2700 Not(Contains(labeled("void vfunc(bool param) override")))));
2701}
2702
2703TEST(CompletionTest, OverridesNonIdentName) {
2704 // Check the completions call does not crash.
2705 completions(Text: R"cpp(
2706 struct Base {
2707 virtual ~Base() = 0;
2708 virtual operator int() = 0;
2709 virtual Base& operator+(Base&) = 0;
2710 };
2711
2712 struct Derived : Base {
2713 ^
2714 };
2715 )cpp");
2716}
2717
2718TEST(CompletionTest, NoCrashOnMissingNewLineAtEOF) {
2719 auto FooCpp = testPath(File: "foo.cpp");
2720
2721 MockCompilationDatabase CDB;
2722 MockFS FS;
2723 Annotations F("#pragma ^ // no new line");
2724 FS.Files[FooCpp] = F.code().str();
2725 ClangdServer Server(CDB, FS, ClangdServer::optsForTest());
2726 runAddDocument(Server, File: FooCpp, Contents: F.code());
2727 // Run completion outside the file range.
2728 EXPECT_THAT(cantFail(runCodeComplete(Server, FooCpp, F.point(),
2729 clangd::CodeCompleteOptions()))
2730 .Completions,
2731 IsEmpty());
2732 EXPECT_THAT(cantFail(runSignatureHelp(Server, FooCpp, F.point(),
2733 MarkupKind::PlainText))
2734 .signatures,
2735 IsEmpty());
2736}
2737
2738TEST(GuessCompletionPrefix, Filters) {
2739 for (llvm::StringRef Case : {
2740 "[[scope::]][[ident]]^",
2741 "[[]][[]]^",
2742 "\n[[]][[]]^",
2743 "[[]][[ab]]^",
2744 "x.[[]][[ab]]^",
2745 "x.[[]][[]]^",
2746 "[[x::]][[ab]]^",
2747 "[[x::]][[]]^",
2748 "[[::x::]][[ab]]^",
2749 "some text [[scope::more::]][[identif]]^ier",
2750 "some text [[scope::]][[mor]]^e::identifier",
2751 "weird case foo::[[::bar::]][[baz]]^",
2752 "/* [[]][[]]^ */",
2753 }) {
2754 Annotations F(Case);
2755 auto Offset = cantFail(ValOrErr: positionToOffset(Code: F.code(), P: F.point()));
2756 auto ToStringRef = [&](Range R) {
2757 return F.code().slice(Start: cantFail(ValOrErr: positionToOffset(Code: F.code(), P: R.start)),
2758 End: cantFail(ValOrErr: positionToOffset(Code: F.code(), P: R.end)));
2759 };
2760 auto WantQualifier = ToStringRef(F.ranges()[0]),
2761 WantName = ToStringRef(F.ranges()[1]);
2762
2763 auto Prefix = guessCompletionPrefix(Content: F.code(), Offset);
2764 // Even when components are empty, check their offsets are correct.
2765 EXPECT_EQ(WantQualifier, Prefix.Qualifier) << Case;
2766 EXPECT_EQ(WantQualifier.begin(), Prefix.Qualifier.begin()) << Case;
2767 EXPECT_EQ(WantName, Prefix.Name) << Case;
2768 EXPECT_EQ(WantName.begin(), Prefix.Name.begin()) << Case;
2769 }
2770}
2771
2772TEST(CompletionTest, EnableSpeculativeIndexRequest) {
2773 MockFS FS;
2774 MockCompilationDatabase CDB;
2775 ClangdServer Server(CDB, FS, ClangdServer::optsForTest());
2776
2777 auto File = testPath(File: "foo.cpp");
2778 Annotations Test(R"cpp(
2779 namespace ns1 { int abc; }
2780 namespace ns2 { int abc; }
2781 void f() { ns1::ab$1^; ns1::ab$2^; }
2782 void f2() { ns2::ab$3^; }
2783 )cpp");
2784 runAddDocument(Server, File, Contents: Test.code());
2785 clangd::CodeCompleteOptions Opts = {};
2786
2787 IndexRequestCollector Requests;
2788 Opts.Index = &Requests;
2789
2790 auto CompleteAtPoint = [&](StringRef P) {
2791 auto CCR = cantFail(ValOrErr: runCodeComplete(Server, File, Pos: Test.point(Name: P), Opts));
2792 EXPECT_TRUE(CCR.HasMore);
2793 };
2794
2795 CompleteAtPoint("1");
2796 auto Reqs1 = Requests.consumeRequests(Num: 1);
2797 ASSERT_EQ(Reqs1.size(), 1u);
2798 EXPECT_THAT(Reqs1[0].Scopes, UnorderedElementsAre("ns1::"));
2799
2800 CompleteAtPoint("2");
2801 auto Reqs2 = Requests.consumeRequests(Num: 1);
2802 // Speculation succeeded. Used speculative index result.
2803 ASSERT_EQ(Reqs2.size(), 1u);
2804 EXPECT_EQ(Reqs2[0], Reqs1[0]);
2805
2806 CompleteAtPoint("3");
2807 // Speculation failed. Sent speculative index request and the new index
2808 // request after sema.
2809 auto Reqs3 = Requests.consumeRequests(Num: 2);
2810 ASSERT_EQ(Reqs3.size(), 2u);
2811}
2812
2813TEST(CompletionTest, InsertTheMostPopularHeader) {
2814 std::string DeclFile = URI::create(AbsolutePath: testPath(File: "foo")).toString();
2815 Symbol Sym = func(Name: "Func");
2816 Sym.CanonicalDeclaration.FileURI = DeclFile.c_str();
2817 Sym.IncludeHeaders.emplace_back(Args: "\"foo.h\"", Args: 2, Args: Symbol::Include);
2818 Sym.IncludeHeaders.emplace_back(Args: "\"bar.h\"", Args: 1000, Args: Symbol::Include);
2819
2820 auto Results = completions(Text: "Fun^", IndexSymbols: {Sym}).Completions;
2821 assert(!Results.empty());
2822 EXPECT_THAT(Results[0], AllOf(named("Func"), insertInclude("\"bar.h\"")));
2823 EXPECT_EQ(Results[0].Includes.size(), 2u);
2824}
2825
2826TEST(CompletionTest, InsertIncludeOrImport) {
2827 std::string DeclFile = URI::create(AbsolutePath: testPath(File: "foo")).toString();
2828 Symbol Sym = func(Name: "Func");
2829 Sym.CanonicalDeclaration.FileURI = DeclFile.c_str();
2830 Sym.IncludeHeaders.emplace_back(Args: "\"bar.h\"", Args: 1000,
2831 Args: Symbol::Include | Symbol::Import);
2832 CodeCompleteOptions Opts;
2833 // Should only take effect in import contexts.
2834 Opts.ImportInsertions = true;
2835 auto Results = completions(Text: "Fun^", IndexSymbols: {Sym}, Opts).Completions;
2836 assert(!Results.empty());
2837 EXPECT_THAT(Results[0],
2838 AllOf(named("Func"), insertIncludeText("#include \"bar.h\"\n")));
2839
2840 ASTSignals Signals;
2841 Signals.InsertionDirective = Symbol::IncludeDirective::Import;
2842 Opts.MainFileSignals = &Signals;
2843 Results = completions(Text: "Fun^", IndexSymbols: {Sym}, Opts, FilePath: "Foo.m").Completions;
2844 assert(!Results.empty());
2845 EXPECT_THAT(Results[0],
2846 AllOf(named("Func"), insertIncludeText("#import \"bar.h\"\n")));
2847
2848 Sym.IncludeHeaders[0].SupportedDirectives = Symbol::Import;
2849 Results = completions(Text: "Fun^", IndexSymbols: {Sym}).Completions;
2850 assert(!Results.empty());
2851 EXPECT_THAT(Results[0], AllOf(named("Func"), Not(insertInclude())));
2852}
2853
2854TEST(CompletionTest, NoInsertIncludeIfOnePresent) {
2855 Annotations Test(R"cpp(
2856 #include "foo.h"
2857 Fun^
2858 )cpp");
2859 auto TU = TestTU::withCode(Code: Test.code());
2860 TU.AdditionalFiles["foo.h"] = "";
2861
2862 std::string DeclFile = URI::create(AbsolutePath: testPath(File: "foo")).toString();
2863 Symbol Sym = func(Name: "Func");
2864 Sym.CanonicalDeclaration.FileURI = DeclFile.c_str();
2865 Sym.IncludeHeaders.emplace_back(Args: "\"foo.h\"", Args: 2, Args: Symbol::Include);
2866 Sym.IncludeHeaders.emplace_back(Args: "\"bar.h\"", Args: 1000, Args: Symbol::Include);
2867
2868 EXPECT_THAT(completions(TU, Test.point(), {Sym}).Completions,
2869 UnorderedElementsAre(AllOf(named("Func"), hasInclude("\"foo.h\""),
2870 Not(insertInclude()))));
2871}
2872
2873TEST(CompletionTest, MergeMacrosFromIndexAndSema) {
2874 Symbol Sym;
2875 Sym.Name = "Clangd_Macro_Test";
2876 Sym.ID = SymbolID("c:foo.cpp@8@macro@Clangd_Macro_Test");
2877 Sym.SymInfo.Kind = index::SymbolKind::Macro;
2878 Sym.Flags |= Symbol::IndexedForCodeCompletion;
2879 EXPECT_THAT(completions("#define Clangd_Macro_Test\nClangd_Macro_T^", {Sym})
2880 .Completions,
2881 UnorderedElementsAre(named("Clangd_Macro_Test")));
2882}
2883
2884TEST(CompletionTest, MacroFromPreamble) {
2885 Annotations Test(R"cpp(#define CLANGD_PREAMBLE_MAIN x
2886
2887 int x = 0;
2888 #define CLANGD_MAIN x
2889 void f() { CLANGD_^ }
2890 )cpp");
2891 auto TU = TestTU::withCode(Code: Test.code());
2892 TU.HeaderCode = "#define CLANGD_PREAMBLE_HEADER x";
2893 auto Results = completions(TU, Point: Test.point(), IndexSymbols: {func(Name: "CLANGD_INDEX")});
2894 // We should get results from the main file, including the preamble section.
2895 // However no results from included files (the index should cover them).
2896 EXPECT_THAT(Results.Completions,
2897 UnorderedElementsAre(named("CLANGD_PREAMBLE_MAIN"),
2898 named("CLANGD_MAIN"),
2899 named("CLANGD_INDEX")));
2900}
2901
2902TEST(CompletionTest, DeprecatedResults) {
2903 std::string Body = R"cpp(
2904 void TestClangd();
2905 void TestClangc() __attribute__((deprecated("", "")));
2906 )cpp";
2907
2908 EXPECT_THAT(
2909 completions(Body + "int main() { TestClang^ }").Completions,
2910 UnorderedElementsAre(AllOf(named("TestClangd"), Not(deprecated())),
2911 AllOf(named("TestClangc"), deprecated())));
2912}
2913
2914TEST(SignatureHelpTest, PartialSpec) {
2915 const auto Results = signatures(Text: R"cpp(
2916 template <typename T> struct Foo {};
2917 template <typename T> struct Foo<T*> { Foo(T); };
2918 Foo<int*> F(^);)cpp");
2919 EXPECT_THAT(Results.signatures, Contains(sig("Foo([[T]])")));
2920 EXPECT_EQ(0, Results.activeParameter);
2921}
2922
2923TEST(SignatureHelpTest, InsideArgument) {
2924 {
2925 const auto Results = signatures(Text: R"cpp(
2926 void foo(int x);
2927 void foo(int x, int y);
2928 int main() { foo(1+^); }
2929 )cpp");
2930 EXPECT_THAT(Results.signatures,
2931 ElementsAre(sig("foo([[int x]]) -> void"),
2932 sig("foo([[int x]], [[int y]]) -> void")));
2933 EXPECT_EQ(0, Results.activeParameter);
2934 }
2935 {
2936 const auto Results = signatures(Text: R"cpp(
2937 void foo(int x);
2938 void foo(int x, int y);
2939 int main() { foo(1^); }
2940 )cpp");
2941 EXPECT_THAT(Results.signatures,
2942 ElementsAre(sig("foo([[int x]]) -> void"),
2943 sig("foo([[int x]], [[int y]]) -> void")));
2944 EXPECT_EQ(0, Results.activeParameter);
2945 }
2946 {
2947 const auto Results = signatures(Text: R"cpp(
2948 void foo(int x);
2949 void foo(int x, int y);
2950 int main() { foo(1^0); }
2951 )cpp");
2952 EXPECT_THAT(Results.signatures,
2953 ElementsAre(sig("foo([[int x]]) -> void"),
2954 sig("foo([[int x]], [[int y]]) -> void")));
2955 EXPECT_EQ(0, Results.activeParameter);
2956 }
2957 {
2958 const auto Results = signatures(Text: R"cpp(
2959 void foo(int x);
2960 void foo(int x, int y);
2961 int bar(int x, int y);
2962 int main() { bar(foo(2, 3^)); }
2963 )cpp");
2964 EXPECT_THAT(Results.signatures,
2965 ElementsAre(sig("foo([[int x]], [[int y]]) -> void")));
2966 EXPECT_EQ(1, Results.activeParameter);
2967 }
2968}
2969
2970TEST(SignatureHelpTest, ConstructorInitializeFields) {
2971 {
2972 const auto Results = signatures(Text: R"cpp(
2973 struct A { A(int); };
2974 struct B {
2975 B() : a_elem(^) {}
2976 A a_elem;
2977 };
2978 )cpp");
2979 EXPECT_THAT(Results.signatures,
2980 UnorderedElementsAre(sig("A([[int]])"), sig("A([[A &&]])"),
2981 sig("A([[const A &]])")));
2982 }
2983 {
2984 const auto Results = signatures(Text: R"cpp(
2985 struct A { A(int); };
2986 struct B {
2987 B() : a_elem(^
2988 A a_elem;
2989 };
2990 )cpp");
2991 // FIXME: currently the parser skips over the decl of a_elem as part of the
2992 // (broken) init list, so we don't get signatures for the first member.
2993 EXPECT_THAT(Results.signatures, IsEmpty());
2994 }
2995 {
2996 const auto Results = signatures(Text: R"cpp(
2997 struct A { A(int); };
2998 struct B {
2999 B() : a_elem(^
3000 int dummy_elem;
3001 A a_elem;
3002 };
3003 )cpp");
3004 EXPECT_THAT(Results.signatures,
3005 UnorderedElementsAre(sig("A([[int]])"), sig("A([[A &&]])"),
3006 sig("A([[const A &]])")));
3007 }
3008 {
3009 const auto Results = signatures(Text: R"cpp(
3010 struct A {
3011 A(int);
3012 };
3013 struct C {
3014 C(int);
3015 C(A);
3016 };
3017 struct B {
3018 B() : c_elem(A(1^)) {}
3019 C c_elem;
3020 };
3021 )cpp");
3022 EXPECT_THAT(Results.signatures,
3023 UnorderedElementsAre(sig("A([[int]])"), sig("A([[A &&]])"),
3024 sig("A([[const A &]])")));
3025 }
3026}
3027
3028TEST(SignatureHelpTest, Variadic) {
3029 const std::string Header = R"cpp(
3030 void fun(int x, ...) {}
3031 void test() {)cpp";
3032 const std::string ExpectedSig = "fun([[int x]], [[...]]) -> void";
3033
3034 {
3035 const auto Result = signatures(Text: Header + "fun(^);}");
3036 EXPECT_EQ(0, Result.activeParameter);
3037 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3038 }
3039 {
3040 const auto Result = signatures(Text: Header + "fun(1, ^);}");
3041 EXPECT_EQ(1, Result.activeParameter);
3042 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3043 }
3044 {
3045 const auto Result = signatures(Text: Header + "fun(1, 2, ^);}");
3046 EXPECT_EQ(1, Result.activeParameter);
3047 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3048 }
3049}
3050
3051TEST(SignatureHelpTest, VariadicTemplate) {
3052 const std::string Header = R"cpp(
3053 template<typename T, typename ...Args>
3054 void fun(T t, Args ...args) {}
3055 void test() {)cpp";
3056 const std::string ExpectedSig = "fun([[T t]], [[Args args...]]) -> void";
3057
3058 {
3059 const auto Result = signatures(Text: Header + "fun(^);}");
3060 EXPECT_EQ(0, Result.activeParameter);
3061 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3062 }
3063 {
3064 const auto Result = signatures(Text: Header + "fun(1, ^);}");
3065 EXPECT_EQ(1, Result.activeParameter);
3066 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3067 }
3068 {
3069 const auto Result = signatures(Text: Header + "fun(1, 2, ^);}");
3070 EXPECT_EQ(1, Result.activeParameter);
3071 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3072 }
3073}
3074
3075TEST(SignatureHelpTest, VariadicMethod) {
3076 const std::string Header = R"cpp(
3077 class C {
3078 template<typename T, typename ...Args>
3079 void fun(T t, Args ...args) {}
3080 };
3081 void test() {C c; )cpp";
3082 const std::string ExpectedSig = "fun([[T t]], [[Args args...]]) -> void";
3083
3084 {
3085 const auto Result = signatures(Text: Header + "c.fun(^);}");
3086 EXPECT_EQ(0, Result.activeParameter);
3087 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3088 }
3089 {
3090 const auto Result = signatures(Text: Header + "c.fun(1, ^);}");
3091 EXPECT_EQ(1, Result.activeParameter);
3092 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3093 }
3094 {
3095 const auto Result = signatures(Text: Header + "c.fun(1, 2, ^);}");
3096 EXPECT_EQ(1, Result.activeParameter);
3097 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3098 }
3099}
3100
3101TEST(SignatureHelpTest, VariadicType) {
3102 const std::string Header = R"cpp(
3103 void fun(int x, ...) {}
3104 auto get_fun() { return fun; }
3105 void test() {
3106 )cpp";
3107 const std::string ExpectedSig = "([[int]], [[...]]) -> void";
3108
3109 {
3110 const auto Result = signatures(Text: Header + "get_fun()(^);}");
3111 EXPECT_EQ(0, Result.activeParameter);
3112 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3113 }
3114 {
3115 const auto Result = signatures(Text: Header + "get_fun()(1, ^);}");
3116 EXPECT_EQ(1, Result.activeParameter);
3117 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3118 }
3119 {
3120 const auto Result = signatures(Text: Header + "get_fun()(1, 2, ^);}");
3121 EXPECT_EQ(1, Result.activeParameter);
3122 EXPECT_THAT(Result.signatures, UnorderedElementsAre(sig(ExpectedSig)));
3123 }
3124}
3125
3126TEST(CompletionTest, IncludedCompletionKinds) {
3127 Annotations Test(R"cpp(#include "^)cpp");
3128 auto TU = TestTU::withCode(Code: Test.code());
3129 TU.AdditionalFiles["sub/bar.h"] = "";
3130 TU.ExtraArgs.push_back(x: "-I" + testPath(File: "sub"));
3131
3132 auto Results = completions(TU, Point: Test.point());
3133 EXPECT_THAT(Results.Completions,
3134 AllOf(has("sub/", CompletionItemKind::Folder),
3135 has("bar.h\"", CompletionItemKind::File)));
3136}
3137
3138TEST(CompletionTest, NoCrashAtNonAlphaIncludeHeader) {
3139 completions(
3140 Text: R"cpp(
3141 #include "./^"
3142 )cpp");
3143}
3144
3145TEST(CompletionTest, NoAllScopesCompletionWhenQualified) {
3146 clangd::CodeCompleteOptions Opts = {};
3147 Opts.AllScopes = true;
3148
3149 auto Results = completions(
3150 Text: R"cpp(
3151 void f() { na::Clangd^ }
3152 )cpp",
3153 IndexSymbols: {cls(Name: "na::ClangdA"), cls(Name: "nx::ClangdX"), cls(Name: "Clangd3")}, Opts);
3154 EXPECT_THAT(Results.Completions,
3155 UnorderedElementsAre(
3156 AllOf(qualifier(""), scope("na::"), named("ClangdA"))));
3157}
3158
3159TEST(CompletionTest, AllScopesCompletion) {
3160 clangd::CodeCompleteOptions Opts = {};
3161 Opts.AllScopes = true;
3162
3163 auto Results = completions(
3164 Text: R"cpp(
3165 namespace na {
3166 void f() { Clangd^ }
3167 }
3168 )cpp",
3169 IndexSymbols: {cls(Name: "nx::Clangd1"), cls(Name: "ny::Clangd2"), cls(Name: "Clangd3"),
3170 cls(Name: "na::nb::Clangd4"), enmConstant(Name: "na::C::Clangd5")},
3171 Opts);
3172 EXPECT_THAT(
3173 Results.Completions,
3174 UnorderedElementsAre(AllOf(qualifier("nx::"), named("Clangd1"),
3175 kind(CompletionItemKind::Class)),
3176 AllOf(qualifier("ny::"), named("Clangd2"),
3177 kind(CompletionItemKind::Class)),
3178 AllOf(qualifier(""), scope(""), named("Clangd3"),
3179 kind(CompletionItemKind::Class)),
3180 AllOf(qualifier("nb::"), named("Clangd4"),
3181 kind(CompletionItemKind::Class)),
3182 AllOf(qualifier("C::"), named("Clangd5"),
3183 kind(CompletionItemKind::EnumMember))));
3184}
3185
3186TEST(CompletionTest, NoQualifierIfShadowed) {
3187 clangd::CodeCompleteOptions Opts = {};
3188 Opts.AllScopes = true;
3189
3190 auto Results = completions(Text: R"cpp(
3191 namespace nx { class Clangd1 {}; }
3192 using nx::Clangd1;
3193 void f() { Clangd^ }
3194 )cpp",
3195 IndexSymbols: {cls(Name: "nx::Clangd1"), cls(Name: "nx::Clangd2")}, Opts);
3196 // Although Clangd1 is from another namespace, Sema tells us it's in-scope and
3197 // needs no qualifier.
3198 EXPECT_THAT(Results.Completions,
3199 UnorderedElementsAre(AllOf(qualifier(""), named("Clangd1")),
3200 AllOf(qualifier("nx::"), named("Clangd2"))));
3201}
3202
3203TEST(CompletionTest, NoCompletionsForNewNames) {
3204 clangd::CodeCompleteOptions Opts;
3205 Opts.AllScopes = true;
3206 auto Results = completions(Text: R"cpp(
3207 void f() { int n^ }
3208 )cpp",
3209 IndexSymbols: {cls(Name: "naber"), cls(Name: "nx::naber")}, Opts);
3210 EXPECT_THAT(Results.Completions, UnorderedElementsAre());
3211}
3212
3213TEST(CompletionTest, Lambda) {
3214 clangd::CodeCompleteOptions Opts = {};
3215
3216 auto Results = completions(Text: R"cpp(
3217 void function() {
3218 auto Lambda = [](int a, const double &b) {return 1.f;};
3219 Lam^
3220 }
3221 )cpp",
3222 IndexSymbols: {}, Opts);
3223
3224 ASSERT_EQ(Results.Completions.size(), 1u);
3225 const auto &A = Results.Completions.front();
3226 EXPECT_EQ(A.Name, "Lambda");
3227 EXPECT_EQ(A.Signature, "(int a, const double &b) const");
3228 EXPECT_EQ(A.Kind, CompletionItemKind::Variable);
3229 EXPECT_EQ(A.ReturnType, "float");
3230 EXPECT_EQ(A.SnippetSuffix, "(${1:int a}, ${2:const double &b})");
3231}
3232
3233TEST(CompletionTest, StructuredBinding) {
3234 clangd::CodeCompleteOptions Opts = {};
3235
3236 auto Results = completions(Text: R"cpp(
3237 struct S {
3238 using Float = float;
3239 int x;
3240 Float y;
3241 };
3242 void function() {
3243 const auto &[xxx, yyy] = S{};
3244 yyy^
3245 }
3246 )cpp",
3247 IndexSymbols: {}, Opts);
3248
3249 ASSERT_EQ(Results.Completions.size(), 1u);
3250 const auto &A = Results.Completions.front();
3251 EXPECT_EQ(A.Name, "yyy");
3252 EXPECT_EQ(A.Kind, CompletionItemKind::Variable);
3253 EXPECT_EQ(A.ReturnType, "const Float");
3254}
3255
3256TEST(CompletionTest, ObjectiveCMethodNoArguments) {
3257 auto Results = completions(Text: R"objc(
3258 @interface Foo
3259 @property(nonatomic, setter=setXToIgnoreComplete:) int value;
3260 @end
3261 Foo *foo = [Foo new]; int y = [foo v^]
3262 )objc",
3263 /*IndexSymbols=*/{},
3264 /*Opts=*/{}, FilePath: "Foo.m");
3265
3266 auto C = Results.Completions;
3267 EXPECT_THAT(C, ElementsAre(named("value")));
3268 EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method)));
3269 EXPECT_THAT(C, ElementsAre(returnType("int")));
3270 EXPECT_THAT(C, ElementsAre(signature("")));
3271 EXPECT_THAT(C, ElementsAre(snippetSuffix("")));
3272}
3273
3274TEST(CompletionTest, ObjectiveCMethodOneArgument) {
3275 auto Results = completions(Text: R"objc(
3276 @interface Foo
3277 - (int)valueForCharacter:(char)c;
3278 @end
3279 Foo *foo = [Foo new]; int y = [foo v^]
3280 )objc",
3281 /*IndexSymbols=*/{},
3282 /*Opts=*/{}, FilePath: "Foo.m");
3283
3284 auto C = Results.Completions;
3285 EXPECT_THAT(C, ElementsAre(named("valueForCharacter:")));
3286 EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method)));
3287 EXPECT_THAT(C, ElementsAre(returnType("int")));
3288 EXPECT_THAT(C, ElementsAre(signature("(char)")));
3289 EXPECT_THAT(C, ElementsAre(snippetSuffix("${1:(char)}")));
3290}
3291
3292TEST(CompletionTest, ObjectiveCMethodTwoArgumentsFromBeginning) {
3293 auto Results = completions(Text: R"objc(
3294 @interface Foo
3295 + (id)fooWithValue:(int)value fooey:(unsigned int)fooey;
3296 @end
3297 id val = [Foo foo^]
3298 )objc",
3299 /*IndexSymbols=*/{},
3300 /*Opts=*/{}, FilePath: "Foo.m");
3301
3302 auto C = Results.Completions;
3303 EXPECT_THAT(C, ElementsAre(named("fooWithValue:")));
3304 EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method)));
3305 EXPECT_THAT(C, ElementsAre(returnType("id")));
3306 EXPECT_THAT(C, ElementsAre(signature("(int) fooey:(unsigned int)")));
3307 EXPECT_THAT(
3308 C, ElementsAre(snippetSuffix("${1:(int)} fooey:${2:(unsigned int)}")));
3309}
3310
3311TEST(CompletionTest, ObjectiveCMethodTwoArgumentsFromMiddle) {
3312 auto Results = completions(Text: R"objc(
3313 @interface Foo
3314 + (id)fooWithValue:(int)value fooey:(unsigned int)fooey;
3315 @end
3316 id val = [Foo fooWithValue:10 f^]
3317 )objc",
3318 /*IndexSymbols=*/{},
3319 /*Opts=*/{}, FilePath: "Foo.m");
3320
3321 auto C = Results.Completions;
3322 EXPECT_THAT(C, ElementsAre(named("fooey:")));
3323 EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method)));
3324 EXPECT_THAT(C, ElementsAre(returnType("id")));
3325 EXPECT_THAT(C, ElementsAre(signature("(unsigned int)")));
3326 EXPECT_THAT(C, ElementsAre(snippetSuffix("${1:(unsigned int)}")));
3327}
3328
3329TEST(CompletionTest, ObjectiveCMethodFilterOnEntireSelector) {
3330 auto Results = completions(Text: R"objc(
3331 @interface Foo
3332 + (id)player:(id)player willRun:(id)run;
3333 @end
3334 id val = [Foo wi^]
3335 )objc",
3336 /*IndexSymbols=*/{},
3337 /*Opts=*/{}, FilePath: "Foo.m");
3338
3339 auto C = Results.Completions;
3340 EXPECT_THAT(C, ElementsAre(named("player:")));
3341 EXPECT_THAT(C, ElementsAre(filterText("player:willRun:")));
3342 EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method)));
3343 EXPECT_THAT(C, ElementsAre(returnType("id")));
3344 EXPECT_THAT(C, ElementsAre(signature("(id) willRun:(id)")));
3345 EXPECT_THAT(C, ElementsAre(snippetSuffix("${1:(id)} willRun:${2:(id)}")));
3346}
3347
3348TEST(CompletionTest, ObjectiveCSimpleMethodDeclaration) {
3349 auto Results = completions(Text: R"objc(
3350 @interface Foo
3351 - (void)foo;
3352 @end
3353 @implementation Foo
3354 fo^
3355 @end
3356 )objc",
3357 /*IndexSymbols=*/{},
3358 /*Opts=*/{}, FilePath: "Foo.m");
3359
3360 auto C = Results.Completions;
3361 EXPECT_THAT(C, ElementsAre(named("foo")));
3362 EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method)));
3363 EXPECT_THAT(C, ElementsAre(qualifier("- (void)")));
3364}
3365
3366TEST(CompletionTest, ObjectiveCMethodDeclaration) {
3367 auto Results = completions(Text: R"objc(
3368 @interface Foo
3369 - (int)valueForCharacter:(char)c secondArgument:(id)object;
3370 @end
3371 @implementation Foo
3372 valueFor^
3373 @end
3374 )objc",
3375 /*IndexSymbols=*/{},
3376 /*Opts=*/{}, FilePath: "Foo.m");
3377
3378 auto C = Results.Completions;
3379 EXPECT_THAT(C, ElementsAre(named("valueForCharacter:")));
3380 EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method)));
3381 EXPECT_THAT(C, ElementsAre(qualifier("- (int)")));
3382 EXPECT_THAT(C, ElementsAre(signature("(char)c secondArgument:(id)object")));
3383}
3384
3385TEST(CompletionTest, ObjectiveCMethodDeclarationFilterOnEntireSelector) {
3386 auto Results = completions(Text: R"objc(
3387 @interface Foo
3388 - (int)valueForCharacter:(char)c secondArgument:(id)object;
3389 @end
3390 @implementation Foo
3391 secondArg^
3392 @end
3393 )objc",
3394 /*IndexSymbols=*/{},
3395 /*Opts=*/{}, FilePath: "Foo.m");
3396
3397 auto C = Results.Completions;
3398 EXPECT_THAT(C, ElementsAre(named("valueForCharacter:")));
3399 EXPECT_THAT(C, ElementsAre(filterText("valueForCharacter:secondArgument:")));
3400 EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method)));
3401 EXPECT_THAT(C, ElementsAre(qualifier("- (int)")));
3402 EXPECT_THAT(C, ElementsAre(signature("(char)c secondArgument:(id)object")));
3403}
3404
3405TEST(CompletionTest, ObjectiveCMethodDeclarationPrefixTyped) {
3406 auto Results = completions(Text: R"objc(
3407 @interface Foo
3408 - (int)valueForCharacter:(char)c;
3409 @end
3410 @implementation Foo
3411 - (int)valueFor^
3412 @end
3413 )objc",
3414 /*IndexSymbols=*/{},
3415 /*Opts=*/{}, FilePath: "Foo.m");
3416
3417 auto C = Results.Completions;
3418 EXPECT_THAT(C, ElementsAre(named("valueForCharacter:")));
3419 EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method)));
3420 EXPECT_THAT(C, ElementsAre(signature("(char)c")));
3421}
3422
3423TEST(CompletionTest, ObjectiveCMethodDeclarationFromMiddle) {
3424 auto Results = completions(Text: R"objc(
3425 @interface Foo
3426 - (int)valueForCharacter:(char)c secondArgument:(id)object;
3427 @end
3428 @implementation Foo
3429 - (int)valueForCharacter:(char)c second^
3430 @end
3431 )objc",
3432 /*IndexSymbols=*/{},
3433 /*Opts=*/{}, FilePath: "Foo.m");
3434
3435 auto C = Results.Completions;
3436 EXPECT_THAT(C, ElementsAre(named("secondArgument:")));
3437 EXPECT_THAT(C, ElementsAre(kind(CompletionItemKind::Method)));
3438 EXPECT_THAT(C, ElementsAre(signature("(id)object")));
3439}
3440
3441TEST(CompletionTest, ObjectiveCProtocolFromIndex) {
3442 Symbol FoodClass = objcClass(Name: "FoodClass");
3443 Symbol SymFood = objcProtocol(Name: "Food");
3444 Symbol SymFooey = objcProtocol(Name: "Fooey");
3445 auto Results = completions(Text: "id<Foo^>", IndexSymbols: {SymFood, FoodClass, SymFooey},
3446 /*Opts=*/{}, FilePath: "Foo.m");
3447
3448 // Should only give protocols for ObjC protocol completions.
3449 EXPECT_THAT(Results.Completions,
3450 UnorderedElementsAre(
3451 AllOf(named("Food"), kind(CompletionItemKind::Interface)),
3452 AllOf(named("Fooey"), kind(CompletionItemKind::Interface))));
3453
3454 Results = completions(Text: "Fo^", IndexSymbols: {SymFood, FoodClass, SymFooey},
3455 /*Opts=*/{}, FilePath: "Foo.m");
3456 // Shouldn't give protocols for non protocol completions.
3457 EXPECT_THAT(
3458 Results.Completions,
3459 ElementsAre(AllOf(named("FoodClass"), kind(CompletionItemKind::Class))));
3460}
3461
3462TEST(CompletionTest, ObjectiveCProtocolFromIndexSpeculation) {
3463 MockFS FS;
3464 MockCompilationDatabase CDB;
3465 ClangdServer Server(CDB, FS, ClangdServer::optsForTest());
3466
3467 auto File = testPath(File: "Foo.m");
3468 Annotations Test(R"cpp(
3469 @protocol Food
3470 @end
3471 id<Foo$1^> foo;
3472 Foo$2^ bar;
3473 )cpp");
3474 runAddDocument(Server, File, Contents: Test.code());
3475 clangd::CodeCompleteOptions Opts = {};
3476
3477 Symbol FoodClass = objcClass(Name: "FoodClass");
3478 IndexRequestCollector Requests({FoodClass});
3479 Opts.Index = &Requests;
3480
3481 auto CompleteAtPoint = [&](StringRef P) {
3482 return cantFail(ValOrErr: runCodeComplete(Server, File, Pos: Test.point(Name: P), Opts))
3483 .Completions;
3484 };
3485
3486 auto C = CompleteAtPoint("1");
3487 auto Reqs1 = Requests.consumeRequests(Num: 1);
3488 ASSERT_EQ(Reqs1.size(), 1u);
3489 EXPECT_THAT(C, ElementsAre(AllOf(named("Food"),
3490 kind(CompletionItemKind::Interface))));
3491
3492 C = CompleteAtPoint("2");
3493 auto Reqs2 = Requests.consumeRequests(Num: 1);
3494 // Speculation succeeded. Used speculative index result, but filtering now to
3495 // now include FoodClass.
3496 ASSERT_EQ(Reqs2.size(), 1u);
3497 EXPECT_EQ(Reqs2[0], Reqs1[0]);
3498 EXPECT_THAT(C, ElementsAre(AllOf(named("FoodClass"),
3499 kind(CompletionItemKind::Class))));
3500}
3501
3502TEST(CompletionTest, ObjectiveCCategoryFromIndexIgnored) {
3503 Symbol FoodCategory = objcCategory(Name: "FoodClass", CategoryName: "Extension");
3504 auto Results = completions(Text: R"objc(
3505 @interface Foo
3506 @end
3507 @interface Foo (^)
3508 @end
3509 )objc",
3510 IndexSymbols: {FoodCategory},
3511 /*Opts=*/{}, FilePath: "Foo.m");
3512 EXPECT_THAT(Results.Completions, IsEmpty());
3513}
3514
3515TEST(CompletionTest, ObjectiveCForwardDeclFromIndex) {
3516 Symbol FoodClass = objcClass(Name: "FoodClass");
3517 FoodClass.IncludeHeaders.emplace_back(Args: "\"Foo.h\"", Args: 2, Args: Symbol::Import);
3518 Symbol SymFood = objcProtocol(Name: "Food");
3519 auto Results = completions(Text: "@class Foo^", IndexSymbols: {SymFood, FoodClass},
3520 /*Opts=*/{}, FilePath: "Foo.m");
3521
3522 // Should only give class names without any include insertion.
3523 EXPECT_THAT(Results.Completions,
3524 UnorderedElementsAre(AllOf(named("FoodClass"),
3525 kind(CompletionItemKind::Class),
3526 Not(insertInclude()))));
3527}
3528
3529TEST(CompletionTest, CursorInSnippets) {
3530 clangd::CodeCompleteOptions Options;
3531 Options.EnableSnippets = true;
3532 auto Results = completions(
3533 Text: R"cpp(
3534 void while_foo(int a, int b);
3535 void test() {
3536 whil^
3537 })cpp",
3538 /*IndexSymbols=*/{}, Opts: Options);
3539
3540 // Last placeholder in code patterns should be $0 to put the cursor there.
3541 EXPECT_THAT(Results.Completions,
3542 Contains(AllOf(named("while"),
3543 snippetSuffix(" (${1:condition}) {\n$0\n}"))));
3544 // However, snippets for functions must *not* end with $0.
3545 EXPECT_THAT(Results.Completions,
3546 Contains(AllOf(named("while_foo"),
3547 snippetSuffix("(${1:int a}, ${2:int b})"))));
3548
3549 Results = completions(Text: R"cpp(
3550 struct Base {
3551 Base(int a, int b) {}
3552 };
3553
3554 struct Derived : Base {
3555 Derived() : Base^
3556 };
3557 )cpp",
3558 /*IndexSymbols=*/{}, Opts: Options);
3559 // Constructors from base classes are a kind of pattern that shouldn't end
3560 // with $0.
3561 EXPECT_THAT(Results.Completions,
3562 Contains(AllOf(named("Base"),
3563 snippetSuffix("(${1:int a}, ${2:int b})"))));
3564}
3565
3566TEST(CompletionTest, WorksWithNullType) {
3567 auto R = completions(Text: R"cpp(
3568 int main() {
3569 for (auto [loopVar] : y ) { // y has to be unresolved.
3570 int z = loopV^;
3571 }
3572 }
3573 )cpp");
3574 EXPECT_THAT(R.Completions, ElementsAre(named("loopVar")));
3575}
3576
3577TEST(CompletionTest, UsingDecl) {
3578 const char *Header(R"cpp(
3579 void foo(int);
3580 namespace std {
3581 using ::foo;
3582 })cpp");
3583 const char *Source(R"cpp(
3584 void bar() {
3585 std::^;
3586 })cpp");
3587 auto Index = TestTU::withHeaderCode(HeaderCode: Header).index();
3588 clangd::CodeCompleteOptions Opts;
3589 Opts.Index = Index.get();
3590 Opts.AllScopes = true;
3591 auto R = completions(Text: Source, IndexSymbols: {}, Opts);
3592 EXPECT_THAT(R.Completions,
3593 ElementsAre(AllOf(scope("std::"), named("foo"),
3594 kind(CompletionItemKind::Reference))));
3595}
3596
3597TEST(CompletionTest, Enums) {
3598 const char *Header(R"cpp(
3599 namespace ns {
3600 enum Unscoped { Clangd1 };
3601 class C {
3602 enum Unscoped { Clangd2 };
3603 };
3604 enum class Scoped { Clangd3 };
3605 })cpp");
3606 const char *Source(R"cpp(
3607 void bar() {
3608 Clangd^
3609 })cpp");
3610 auto Index = TestTU::withHeaderCode(HeaderCode: Header).index();
3611 clangd::CodeCompleteOptions Opts;
3612 Opts.Index = Index.get();
3613 Opts.AllScopes = true;
3614 auto R = completions(Text: Source, IndexSymbols: {}, Opts);
3615 EXPECT_THAT(R.Completions, UnorderedElementsAre(
3616 AllOf(scope("ns::"), named("Clangd1"),
3617 kind(CompletionItemKind::EnumMember)),
3618 AllOf(scope("ns::C::"), named("Clangd2"),
3619 kind(CompletionItemKind::EnumMember)),
3620 AllOf(scope("ns::Scoped::"), named("Clangd3"),
3621 kind(CompletionItemKind::EnumMember))));
3622}
3623
3624TEST(CompletionTest, ScopeIsUnresolved) {
3625 clangd::CodeCompleteOptions Opts = {};
3626 Opts.AllScopes = true;
3627
3628 auto Results = completions(Text: R"cpp(
3629 namespace a {
3630 void f() { b::X^ }
3631 }
3632 )cpp",
3633 IndexSymbols: {cls(Name: "a::b::XYZ")}, Opts);
3634 EXPECT_THAT(Results.Completions,
3635 UnorderedElementsAre(AllOf(qualifier(""), named("XYZ"))));
3636}
3637
3638TEST(CompletionTest, NestedScopeIsUnresolved) {
3639 clangd::CodeCompleteOptions Opts = {};
3640 Opts.AllScopes = true;
3641
3642 auto Results = completions(Text: R"cpp(
3643 namespace a {
3644 namespace b {}
3645 void f() { b::c::X^ }
3646 }
3647 )cpp",
3648 IndexSymbols: {cls(Name: "a::b::c::XYZ")}, Opts);
3649 EXPECT_THAT(Results.Completions,
3650 UnorderedElementsAre(AllOf(qualifier(""), named("XYZ"))));
3651}
3652
3653// Clang parser gets confused here and doesn't report the ns:: prefix.
3654// Naive behavior is to insert it again. We examine the source and recover.
3655TEST(CompletionTest, NamespaceDoubleInsertion) {
3656 clangd::CodeCompleteOptions Opts = {};
3657
3658 auto Results = completions(Text: R"cpp(
3659 namespace foo {
3660 namespace ns {}
3661 #define M(X) < X
3662 M(ns::ABC^
3663 }
3664 )cpp",
3665 IndexSymbols: {cls(Name: "foo::ns::ABCDE")}, Opts);
3666 EXPECT_THAT(Results.Completions,
3667 UnorderedElementsAre(AllOf(qualifier(""), named("ABCDE"))));
3668}
3669
3670TEST(CompletionTest, DerivedMethodsAreAlwaysVisible) {
3671 // Despite the fact that base method matches the ref-qualifier better,
3672 // completion results should only include the derived method.
3673 auto Completions = completions(Text: R"cpp(
3674 struct deque_base {
3675 float size();
3676 double size() const;
3677 };
3678 struct deque : deque_base {
3679 int size() const;
3680 };
3681
3682 auto x = deque().^
3683 )cpp")
3684 .Completions;
3685 EXPECT_THAT(Completions,
3686 ElementsAre(AllOf(returnType("int"), named("size"))));
3687}
3688
3689TEST(CompletionTest, NoCrashWithIncompleteLambda) {
3690 auto Completions = completions(Text: "auto&& x = []{^").Completions;
3691 // The completion of x itself can cause a problem: in the code completion
3692 // callback, its type is not known, which affects the linkage calculation.
3693 // A bad linkage value gets cached, and subsequently updated.
3694 EXPECT_THAT(Completions, Contains(named("x")));
3695
3696 auto Signatures = signatures(Text: "auto x() { x(^").signatures;
3697 EXPECT_THAT(Signatures, Contains(sig("x() -> auto")));
3698}
3699
3700TEST(CompletionTest, DelayedTemplateParsing) {
3701 Annotations Test(R"cpp(
3702 int xxx;
3703 template <typename T> int foo() { return xx^; }
3704 )cpp");
3705 auto TU = TestTU::withCode(Code: Test.code());
3706 // Even though delayed-template-parsing is on, we will disable it to provide
3707 // completion in templates.
3708 TU.ExtraArgs.push_back(x: "-fdelayed-template-parsing");
3709
3710 EXPECT_THAT(completions(TU, Test.point()).Completions,
3711 Contains(named("xxx")));
3712}
3713
3714TEST(CompletionTest, CompletionRange) {
3715 const char *WithRange = "auto x = [[abc]]^";
3716 auto Completions = completions(Text: WithRange);
3717 EXPECT_EQ(Completions.CompletionRange, Annotations(WithRange).range());
3718 Completions = completionsNoCompile(Text: WithRange);
3719 EXPECT_EQ(Completions.CompletionRange, Annotations(WithRange).range());
3720
3721 const char *EmptyRange = "auto x = [[]]^";
3722 Completions = completions(Text: EmptyRange);
3723 EXPECT_EQ(Completions.CompletionRange, Annotations(EmptyRange).range());
3724 Completions = completionsNoCompile(Text: EmptyRange);
3725 EXPECT_EQ(Completions.CompletionRange, Annotations(EmptyRange).range());
3726
3727 // Sema doesn't trigger at all here, while the no-sema completion runs
3728 // heuristics as normal and reports a range. It'd be nice to be consistent.
3729 const char *NoCompletion = "/* foo [[]]^ */";
3730 Completions = completions(Text: NoCompletion);
3731 EXPECT_EQ(Completions.CompletionRange, std::nullopt);
3732 Completions = completionsNoCompile(Text: NoCompletion);
3733 EXPECT_EQ(Completions.CompletionRange, Annotations(NoCompletion).range());
3734}
3735
3736TEST(NoCompileCompletionTest, Basic) {
3737 auto Results = completionsNoCompile(Text: R"cpp(
3738 void func() {
3739 int xyz;
3740 int abc;
3741 ^
3742 }
3743 )cpp");
3744 EXPECT_FALSE(Results.RanParser);
3745 EXPECT_THAT(Results.Completions,
3746 UnorderedElementsAre(named("void"), named("func"), named("int"),
3747 named("xyz"), named("abc")));
3748}
3749
3750TEST(NoCompileCompletionTest, WithFilter) {
3751 auto Results = completionsNoCompile(Text: R"cpp(
3752 void func() {
3753 int sym1;
3754 int sym2;
3755 int xyz1;
3756 int xyz2;
3757 sy^
3758 }
3759 )cpp");
3760 EXPECT_THAT(Results.Completions,
3761 UnorderedElementsAre(named("sym1"), named("sym2")));
3762}
3763
3764TEST(NoCompileCompletionTest, WithIndex) {
3765 std::vector<Symbol> Syms = {func(Name: "xxx"), func(Name: "a::xxx"), func(Name: "ns::b::xxx"),
3766 func(Name: "c::xxx"), func(Name: "ns::d::xxx")};
3767 auto Results = completionsNoCompile(
3768 Text: R"cpp(
3769 // Current-scopes, unqualified completion.
3770 using namespace a;
3771 namespace ns {
3772 using namespace b;
3773 void foo() {
3774 xx^
3775 }
3776 }
3777 )cpp",
3778 IndexSymbols: Syms);
3779 EXPECT_THAT(Results.Completions,
3780 UnorderedElementsAre(AllOf(qualifier(""), scope("")),
3781 AllOf(qualifier(""), scope("a::")),
3782 AllOf(qualifier(""), scope("ns::b::"))));
3783 CodeCompleteOptions Opts;
3784 Opts.AllScopes = true;
3785 Results = completionsNoCompile(
3786 Text: R"cpp(
3787 // All-scopes unqualified completion.
3788 using namespace a;
3789 namespace ns {
3790 using namespace b;
3791 void foo() {
3792 xx^
3793 }
3794 }
3795 )cpp",
3796 IndexSymbols: Syms, Opts);
3797 EXPECT_THAT(Results.Completions,
3798 UnorderedElementsAre(AllOf(qualifier(""), scope("")),
3799 AllOf(qualifier(""), scope("a::")),
3800 AllOf(qualifier(""), scope("ns::b::")),
3801 AllOf(qualifier("c::"), scope("c::")),
3802 AllOf(qualifier("d::"), scope("ns::d::"))));
3803 Results = completionsNoCompile(
3804 Text: R"cpp(
3805 // Qualified completion.
3806 using namespace a;
3807 namespace ns {
3808 using namespace b;
3809 void foo() {
3810 b::xx^
3811 }
3812 }
3813 )cpp",
3814 IndexSymbols: Syms, Opts);
3815 EXPECT_THAT(Results.Completions,
3816 ElementsAre(AllOf(qualifier(""), scope("ns::b::"))));
3817 Results = completionsNoCompile(
3818 Text: R"cpp(
3819 // Absolutely qualified completion.
3820 using namespace a;
3821 namespace ns {
3822 using namespace b;
3823 void foo() {
3824 ::a::xx^
3825 }
3826 }
3827 )cpp",
3828 IndexSymbols: Syms, Opts);
3829 EXPECT_THAT(Results.Completions,
3830 ElementsAre(AllOf(qualifier(""), scope("a::"))));
3831}
3832
3833TEST(AllowImplicitCompletion, All) {
3834 const char *Yes[] = {
3835 "foo.^bar",
3836 "foo->^bar",
3837 "foo::^bar",
3838 " # include <^foo.h>",
3839 "#import <foo/^bar.h>",
3840 "#include_next \"^",
3841 };
3842 const char *No[] = {
3843 "foo>^bar",
3844 "foo:^bar",
3845 "foo\n^bar",
3846 "#include <foo.h> //^",
3847 "#include \"foo.h\"^",
3848 "#error <^",
3849 "#<^",
3850 };
3851 for (const char *Test : Yes) {
3852 llvm::Annotations A(Test);
3853 EXPECT_TRUE(allowImplicitCompletion(A.code(), A.point())) << Test;
3854 }
3855 for (const char *Test : No) {
3856 llvm::Annotations A(Test);
3857 EXPECT_FALSE(allowImplicitCompletion(A.code(), A.point())) << Test;
3858 }
3859}
3860
3861TEST(CompletionTest, FunctionArgsExist) {
3862 clangd::CodeCompleteOptions Opts;
3863 Opts.EnableSnippets = true;
3864 std::string Context = R"cpp(
3865 #define MACRO(x)
3866 int foo(int A);
3867 int bar();
3868 struct Object {
3869 Object(int B) {}
3870 };
3871 template <typename T>
3872 struct Container {
3873 Container(int Size) {}
3874 };
3875 )cpp";
3876 EXPECT_THAT(completions(Context + "int y = fo^", {}, Opts).Completions,
3877 UnorderedElementsAre(
3878 AllOf(labeled("foo(int A)"), snippetSuffix("(${1:int A})"))));
3879 EXPECT_THAT(
3880 completions(Context + "int y = fo^(42)", {}, Opts).Completions,
3881 UnorderedElementsAre(AllOf(labeled("foo(int A)"), snippetSuffix(""))));
3882 // FIXME(kirillbobyrev): No snippet should be produced here.
3883 EXPECT_THAT(completions(Context + "int y = fo^o(42)", {}, Opts).Completions,
3884 UnorderedElementsAre(
3885 AllOf(labeled("foo(int A)"), snippetSuffix("(${1:int A})"))));
3886 EXPECT_THAT(
3887 completions(Context + "int y = ba^", {}, Opts).Completions,
3888 UnorderedElementsAre(AllOf(labeled("bar()"), snippetSuffix("()"))));
3889 EXPECT_THAT(completions(Context + "int y = ba^()", {}, Opts).Completions,
3890 UnorderedElementsAre(AllOf(labeled("bar()"), snippetSuffix(""))));
3891 EXPECT_THAT(
3892 completions(Context + "Object o = Obj^", {}, Opts).Completions,
3893 Contains(AllOf(labeled("Object(int B)"), snippetSuffix("(${1:int B})"),
3894 kind(CompletionItemKind::Constructor))));
3895 EXPECT_THAT(completions(Context + "Object o = Obj^()", {}, Opts).Completions,
3896 Contains(AllOf(labeled("Object(int B)"), snippetSuffix(""),
3897 kind(CompletionItemKind::Constructor))));
3898 EXPECT_THAT(
3899 completions(Context + "Container c = Cont^", {}, Opts).Completions,
3900 Contains(AllOf(labeled("Container<typename T>(int Size)"),
3901 snippetSuffix("<${1:typename T}>(${2:int Size})"),
3902 kind(CompletionItemKind::Constructor))));
3903 EXPECT_THAT(
3904 completions(Context + "Container c = Cont^()", {}, Opts).Completions,
3905 Contains(AllOf(labeled("Container<typename T>(int Size)"),
3906 snippetSuffix("<${1:typename T}>"),
3907 kind(CompletionItemKind::Constructor))));
3908 EXPECT_THAT(
3909 completions(Context + "Container c = Cont^<int>()", {}, Opts).Completions,
3910 Contains(AllOf(labeled("Container<typename T>(int Size)"),
3911 snippetSuffix(""),
3912 kind(CompletionItemKind::Constructor))));
3913 EXPECT_THAT(completions(Context + "MAC^(2)", {}, Opts).Completions,
3914 Contains(AllOf(labeled("MACRO(x)"), snippetSuffix(""),
3915 kind(CompletionItemKind::Function))));
3916}
3917
3918TEST(CompletionTest, FunctionArgsExist_Issue1785) {
3919 // This is a scenario where the implementation of our check for
3920 // "is there a function argument list right after the cursor"
3921 // gave a bogus result.
3922 clangd::CodeCompleteOptions Opts;
3923 Opts.EnableSnippets = true;
3924 // The whitespace in this testcase is important!
3925 std::string Code = R"cpp(
3926void waldo(int);
3927
3928int main()
3929{
3930 wal^
3931
3932
3933 // ( )
3934}
3935 )cpp";
3936 EXPECT_THAT(
3937 completions(Code, {}, Opts).Completions,
3938 Contains(AllOf(labeled("waldo(int)"), snippetSuffix("(${1:int})"))));
3939}
3940
3941TEST(CompletionTest, NoCrashDueToMacroOrdering) {
3942 EXPECT_THAT(completions(R"cpp(
3943 #define ECHO(X) X
3944 #define ECHO2(X) ECHO(X)
3945 int finish_preamble = EC^HO(2);)cpp")
3946 .Completions,
3947 UnorderedElementsAre(labeled("ECHO(X)"), labeled("ECHO2(X)")));
3948}
3949
3950TEST(CompletionTest, ObjCCategoryDecls) {
3951 TestTU TU;
3952 TU.ExtraArgs.push_back(x: "-xobjective-c");
3953 TU.HeaderCode = R"objc(
3954 @interface Foo
3955 @end
3956
3957 @interface Foo (FooExt1)
3958 @end
3959
3960 @interface Foo (FooExt2)
3961 @end
3962
3963 @interface Bar
3964 @end
3965
3966 @interface Bar (BarExt)
3967 @end)objc";
3968
3969 {
3970 Annotations Test(R"objc(
3971 @implementation Foo (^)
3972 @end
3973 )objc");
3974 TU.Code = Test.code().str();
3975 auto Results = completions(TU, Point: Test.point());
3976 EXPECT_THAT(Results.Completions,
3977 UnorderedElementsAre(labeled("FooExt1"), labeled("FooExt2")));
3978 }
3979 {
3980 Annotations Test(R"objc(
3981 @interface Foo (^)
3982 @end
3983 )objc");
3984 TU.Code = Test.code().str();
3985 auto Results = completions(TU, Point: Test.point());
3986 EXPECT_THAT(Results.Completions, UnorderedElementsAre(labeled("BarExt")));
3987 }
3988}
3989
3990TEST(CompletionTest, PreambleCodeComplete) {
3991 llvm::StringLiteral Baseline = "\n#define MACRO 12\nint num = MACRO;";
3992 llvm::StringLiteral ModifiedCC =
3993 "#include \"header.h\"\n#define MACRO 12\nint num = MACRO; int num2 = M^";
3994
3995 Annotations Test(ModifiedCC);
3996 auto BaselineTU = TestTU::withCode(Code: Baseline);
3997 auto ModifiedTU = TestTU::withCode(Code: Test.code());
3998
3999 MockFS FS;
4000 auto Inputs = ModifiedTU.inputs(FS);
4001 auto Result = codeComplete(FileName: testPath(File: ModifiedTU.Filename), Pos: Test.point(),
4002 Preamble: BaselineTU.preamble().get(), ParseInput: Inputs, Opts: {});
4003 EXPECT_THAT(Result.Completions, Not(testing::IsEmpty()));
4004}
4005
4006TEST(CompletionTest, CommentParamName) {
4007 const std::string Code = R"cpp(
4008 void fun(int foo, int bar);
4009 void overloaded(int param_int);
4010 void overloaded(int param_int, int param_other);
4011 void overloaded(char param_char);
4012 int main() {
4013 )cpp";
4014
4015 EXPECT_THAT(completions(Code + "fun(/*^").Completions,
4016 UnorderedElementsAre(labeled("foo=*/")));
4017 EXPECT_THAT(completions(Code + "fun(1, /*^").Completions,
4018 UnorderedElementsAre(labeled("bar=*/")));
4019 EXPECT_THAT(completions(Code + "/*^").Completions, IsEmpty());
4020 // Test de-duplication.
4021 EXPECT_THAT(
4022 completions(Code + "overloaded(/*^").Completions,
4023 UnorderedElementsAre(labeled("param_int=*/"), labeled("param_char=*/")));
4024 // Comment already has some text in it.
4025 EXPECT_THAT(completions(Code + "fun(/* ^").Completions,
4026 UnorderedElementsAre(labeled("foo=*/")));
4027 EXPECT_THAT(completions(Code + "fun(/* f^").Completions,
4028 UnorderedElementsAre(labeled("foo=*/")));
4029 EXPECT_THAT(completions(Code + "fun(/* x^").Completions, IsEmpty());
4030 EXPECT_THAT(completions(Code + "fun(/* f ^").Completions, IsEmpty());
4031
4032 // Test ranges
4033 {
4034 std::string CompletionRangeTest(Code + "fun(/*[[^]]");
4035 auto Results = completions(Text: CompletionRangeTest);
4036 EXPECT_THAT(Results.CompletionRange,
4037 llvm::ValueIs(Annotations(CompletionRangeTest).range()));
4038 EXPECT_THAT(
4039 Results.Completions,
4040 testing::Each(
4041 AllOf(replacesRange(Annotations(CompletionRangeTest).range()),
4042 origin(SymbolOrigin::AST), kind(CompletionItemKind::Text))));
4043 }
4044 {
4045 std::string CompletionRangeTest(Code + "fun(/*[[fo^]]");
4046 auto Results = completions(Text: CompletionRangeTest);
4047 EXPECT_THAT(Results.CompletionRange,
4048 llvm::ValueIs(Annotations(CompletionRangeTest).range()));
4049 EXPECT_THAT(
4050 Results.Completions,
4051 testing::Each(
4052 AllOf(replacesRange(Annotations(CompletionRangeTest).range()),
4053 origin(SymbolOrigin::AST), kind(CompletionItemKind::Text))));
4054 }
4055}
4056
4057TEST(CompletionTest, Concepts) {
4058 Annotations Code(R"cpp(
4059 template<class T>
4060 concept A = sizeof(T) <= 8;
4061
4062 template<$tparam^A U>
4063 int foo();
4064
4065 template<typename T>
4066 int bar(T t) requires $expr^A<int>;
4067
4068 template<class T>
4069 concept b = $expr^A && $expr^sizeof(T) % 2 == 0 || $expr^A && sizeof(T) == 1;
4070
4071 $toplevel^A auto i = 19;
4072
4073 template<$toplevel^A auto i> void constrainedNTTP();
4074
4075 // FIXME: The first parameter should be dropped in this case.
4076 void abbreviated($expr^A auto x) {}
4077 )cpp");
4078 TestTU TU;
4079 TU.Code = Code.code().str();
4080 TU.ExtraArgs = {"-std=c++20"};
4081
4082 auto Sym = conceptSym(Name: "same_as");
4083 Sym.Signature = "<typename Tp, typename Up>";
4084 Sym.CompletionSnippetSuffix = "<${1:typename Tp}, ${2:typename Up}>";
4085 std::vector<Symbol> Syms = {Sym};
4086 for (auto P : Code.points(Name: "tparam")) {
4087 ASSERT_THAT(
4088 completions(TU, P, Syms).Completions,
4089 AllOf(Contains(AllOf(named("A"), signature(""), snippetSuffix(""))),
4090 Contains(AllOf(named("same_as"), signature("<typename Up>"),
4091 snippetSuffix("<${2:typename Up}>"))),
4092 Contains(named("class")), Contains(named("typename"))))
4093 << "Completing template parameter at position " << P;
4094 }
4095
4096 for (auto P : Code.points(Name: "toplevel")) {
4097 EXPECT_THAT(
4098 completions(TU, P, Syms).Completions,
4099 AllOf(Contains(AllOf(named("A"), signature(""), snippetSuffix(""))),
4100 Contains(AllOf(named("same_as"), signature("<typename Up>"),
4101 snippetSuffix("<${2:typename Up}>")))))
4102 << "Completing 'requires' expression at position " << P;
4103 }
4104
4105 for (auto P : Code.points(Name: "expr")) {
4106 EXPECT_THAT(
4107 completions(TU, P, Syms).Completions,
4108 AllOf(Contains(AllOf(named("A"), signature("<class T>"),
4109 snippetSuffix("<${1:class T}>"))),
4110 Contains(AllOf(
4111 named("same_as"), signature("<typename Tp, typename Up>"),
4112 snippetSuffix("<${1:typename Tp}, ${2:typename Up}>")))))
4113 << "Completing 'requires' expression at position " << P;
4114 }
4115}
4116
4117TEST(SignatureHelp, DocFormat) {
4118 Annotations Code(R"cpp(
4119 // Comment `with` markup.
4120 void foo(int);
4121 void bar() { foo(^); }
4122 )cpp");
4123 for (auto DocumentationFormat :
4124 {MarkupKind::PlainText, MarkupKind::Markdown}) {
4125 auto Sigs = signatures(Text: Code.code(), Point: Code.point(), /*IndexSymbols=*/{},
4126 DocumentationFormat);
4127 ASSERT_EQ(Sigs.signatures.size(), 1U);
4128 EXPECT_EQ(Sigs.signatures[0].documentation.kind, DocumentationFormat);
4129 }
4130}
4131
4132TEST(SignatureHelp, TemplateArguments) {
4133 std::string Top = R"cpp(
4134 template <typename T, int> bool foo(char);
4135 template <int I, int> bool foo(float);
4136 )cpp";
4137
4138 auto First = signatures(Text: Top + "bool x = foo<^");
4139 EXPECT_THAT(
4140 First.signatures,
4141 UnorderedElementsAre(sig("foo<[[typename T]], [[int]]>() -> bool"),
4142 sig("foo<[[int I]], [[int]]>() -> bool")));
4143 EXPECT_EQ(First.activeParameter, 0);
4144
4145 auto Second = signatures(Text: Top + "bool x = foo<1, ^");
4146 EXPECT_THAT(Second.signatures,
4147 ElementsAre(sig("foo<[[int I]], [[int]]>() -> bool")));
4148 EXPECT_EQ(Second.activeParameter, 1);
4149}
4150
4151TEST(CompletionTest, DoNotCrash) {
4152 llvm::StringLiteral Cases[] = {
4153 R"cpp(
4154 template <typename = int> struct Foo {};
4155 auto a = [x(3)](Foo<^>){};
4156 )cpp",
4157 };
4158 for (auto Case : Cases) {
4159 SCOPED_TRACE(Case);
4160 auto Completions = completions(Text: Case);
4161 }
4162}
4163TEST(CompletionTest, PreambleFromDifferentTarget) {
4164 constexpr std::string_view PreambleTarget = "x86_64";
4165 constexpr std::string_view Contents =
4166 "int foo(int); int num; int num2 = foo(n^";
4167
4168 Annotations Test(Contents);
4169 auto TU = TestTU::withCode(Code: Test.code());
4170 TU.ExtraArgs.emplace_back(args: "-target");
4171 TU.ExtraArgs.emplace_back(args: PreambleTarget);
4172 auto Preamble = TU.preamble();
4173 ASSERT_TRUE(Preamble);
4174 // Switch target to wasm.
4175 TU.ExtraArgs.pop_back();
4176 TU.ExtraArgs.emplace_back(args: "wasm32");
4177
4178 MockFS FS;
4179 auto Inputs = TU.inputs(FS);
4180 auto Result = codeComplete(FileName: testPath(File: TU.Filename), Pos: Test.point(),
4181 Preamble: Preamble.get(), ParseInput: Inputs, Opts: {});
4182 auto Signatures =
4183 signatureHelp(FileName: testPath(File: TU.Filename), Pos: Test.point(), Preamble: *Preamble, ParseInput: Inputs, DocumentationFormat: {});
4184
4185 // Make sure we don't crash.
4186 EXPECT_THAT(Result.Completions, Not(testing::IsEmpty()));
4187 EXPECT_THAT(Signatures.signatures, Not(testing::IsEmpty()));
4188}
4189} // namespace
4190} // namespace clangd
4191} // namespace clang
4192

source code of clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp