Debian Codegraph @

llvm-project/clang-tools-extra/clangd/unittests/HoverTests.cpp

raw
1 //===-- HoverTests.cpp ----------------------------------------------------===//
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 "AST.h"
10 #include "Annotations.h"
11 #include "Config.h"
12 #include "Hover.h"
13 #include "TestIndex.h"
14 #include "TestTU.h"
15 #include "index/MemIndex.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/Index/IndexSymbol.h"
18 #include "llvm/ADT/StringRef.h"
19
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include <string>
23 #include <vector>
24
25 namespace clang {
26 namespace clangd {
27 namespace {
28
29 using PassMode = HoverInfo::PassType::PassMode;
30
31 TEST(Hover, Structured) {
32 struct {
33 const char *const Code;
34 const std::function<void(HoverInfo &)> ExpectedBuilder;
35 } Cases[] = {
36 // Global scope.
37 {R"cpp(
38 // Best foo ever.
39 void [[fo^o]]() {}
40 )cpp",
41 [](HoverInfo &HI) {
42 HI.NamespaceScope = "";
43 HI.Name = "foo";
44 HI.Kind = index::SymbolKind::Function;
45 HI.Documentation = "Best foo ever.";
46 HI.Definition = "void foo()";
47 HI.ReturnType = "void";
48 HI.Type = "void ()";
49 HI.Parameters.emplace();
50 }},
51 // Inside namespace
52 {R"cpp(
53 namespace ns1 { namespace ns2 {
54 /// Best foo ever.
55 void [[fo^o]]() {}
56 }}
57 )cpp",
58 [](HoverInfo &HI) {
59 HI.NamespaceScope = "ns1::ns2::";
60 HI.Name = "foo";
61 HI.Kind = index::SymbolKind::Function;
62 HI.Documentation = "Best foo ever.";
63 HI.Definition = "void foo()";
64 HI.ReturnType = "void";
65 HI.Type = "void ()";
66 HI.Parameters.emplace();
67 }},
68 // Field
69 {R"cpp(
70 namespace ns1 { namespace ns2 {
71 class Foo {
72 char [[b^ar]];
73 double y[2];
74 };
75 }}
76 )cpp",
77 [](HoverInfo &HI) {
78 HI.NamespaceScope = "ns1::ns2::";
79 HI.LocalScope = "Foo::";
80 HI.Name = "bar";
81 HI.Kind = index::SymbolKind::Field;
82 HI.Definition = "char bar";
83 HI.Type = "char";
84 HI.Offset = 0;
85 HI.Size = 1;
86 HI.Padding = 7;
87 HI.AccessSpecifier = "private";
88 }},
89 // Union field
90 {R"cpp(
91 union Foo {
92 char [[b^ar]];
93 double y[2];
94 };
95 )cpp",
96 [](HoverInfo &HI) {
97 HI.NamespaceScope = "";
98 HI.LocalScope = "Foo::";
99 HI.Name = "bar";
100 HI.Kind = index::SymbolKind::Field;
101 HI.Definition = "char bar";
102 HI.Type = "char";
103 HI.Size = 1;
104 HI.Padding = 15;
105 HI.AccessSpecifier = "public";
106 }},
107 // Bitfield
108 {R"cpp(
109 struct Foo {
110 int [[^x]] : 1;
111 int y : 1;
112 };
113 )cpp",
114 [](HoverInfo &HI) {
115 HI.NamespaceScope = "";
116 HI.LocalScope = "Foo::";
117 HI.Name = "x";
118 HI.Kind = index::SymbolKind::Field;
119 HI.Definition = "int x : 1";
120 HI.Type = "int";
121 HI.AccessSpecifier = "public";
122 }},
123 // Local to class method.
124 {R"cpp(
125 namespace ns1 { namespace ns2 {
126 struct Foo {
127 void foo() {
128 int [[b^ar]];
129 }
130 };
131 }}
132 )cpp",
133 [](HoverInfo &HI) {
134 HI.NamespaceScope = "ns1::ns2::";
135 HI.LocalScope = "Foo::foo::";
136 HI.Name = "bar";
137 HI.Kind = index::SymbolKind::Variable;
138 HI.Definition = "int bar";
139 HI.Type = "int";
140 }},
141 // Predefined variable
142 {R"cpp(
143 void foo() {
144 [[__f^unc__]];
145 }
146 )cpp",
147 [](HoverInfo &HI) {
148 HI.Name = "__func__";
149 HI.Kind = index::SymbolKind::Variable;
150 HI.Documentation =
151 "Name of the current function (predefined variable)";
152 HI.Value = "\"foo\"";
153 HI.Type = "const char[4]";
154 }},
155 // Predefined variable (dependent)
156 {R"cpp(
157 template<int> void foo() {
158 [[__f^unc__]];
159 }
160 )cpp",
161 [](HoverInfo &HI) {
162 HI.Name = "__func__";
163 HI.Kind = index::SymbolKind::Variable;
164 HI.Documentation =
165 "Name of the current function (predefined variable)";
166 HI.Type = "const char[]";
167 }},
168 // Anon namespace and local scope.
169 {R"cpp(
170 namespace ns1 { namespace {
171 struct {
172 char [[b^ar]];
173 } T;
174 }}
175 )cpp",
176 [](HoverInfo &HI) {
177 HI.NamespaceScope = "ns1::";
178 HI.LocalScope = "(anonymous struct)::";
179 HI.Name = "bar";
180 HI.Kind = index::SymbolKind::Field;
181 HI.Definition = "char bar";
182 HI.Type = "char";
183 HI.Offset = 0;
184 HI.Size = 1;
185 HI.AccessSpecifier = "public";
186 }},
187 // Struct definition shows size.
188 {R"cpp(
189 struct [[^X]]{};
190 )cpp",
191 [](HoverInfo &HI) {
192 HI.NamespaceScope = "";
193 HI.Name = "X";
194 HI.Kind = index::SymbolKind::Struct;
195 HI.Definition = "struct X {}";
196 HI.Size = 1;
197 }},
198 // Variable with template type
199 {R"cpp(
200 template <typename T, class... Ts> class Foo { public: Foo(int); };
201 Foo<int, char, bool> [[fo^o]] = Foo<int, char, bool>(5);
202 )cpp",
203 [](HoverInfo &HI) {
204 HI.NamespaceScope = "";
205 HI.Name = "foo";
206 HI.Kind = index::SymbolKind::Variable;
207 HI.Definition = "Foo<int, char, bool> foo = Foo<int, char, bool>(5)";
208 HI.Type = "Foo<int, char, bool>";
209 }},
210 // Implicit template instantiation
211 {R"cpp(
212 template <typename T> class vector{};
213 [[vec^tor]]<int> foo;
214 )cpp",
215 [](HoverInfo &HI) {
216 HI.NamespaceScope = "";
217 HI.Name = "vector<int>";
218 HI.Kind = index::SymbolKind::Class;
219 HI.Definition = "template <> class vector<int> {}";
220 }},
221 // Class template
222 {R"cpp(
223 template <template<typename, bool...> class C,
224 typename = char,
225 int = 0,
226 bool Q = false,
227 class... Ts> class Foo final {};
228 template <template<typename, bool...> class T>
229 [[F^oo]]<T> foo;
230 )cpp",
231 [](HoverInfo &HI) {
232 HI.NamespaceScope = "";
233 HI.Name = "Foo";
234 HI.Kind = index::SymbolKind::Class;
235 HI.Definition =
236 R"cpp(template <template <typename, bool...> class C, typename = char, int = 0,
237 bool Q = false, class... Ts>
238 class Foo final {})cpp";
239 HI.TemplateParameters = {
240 {{"template <typename, bool...> class"},
241 std::string("C"),
242 std::nullopt},
243 {{"typename"}, std::nullopt, std::string("char")},
244 {{"int"}, std::nullopt, std::string("0")},
245 {{"bool"}, std::string("Q"), std::string("false")},
246 {{"class..."}, std::string("Ts"), std::nullopt},
247 };
248 }},
249 // Function template
250 {R"cpp(
251 template <template<typename, bool...> class C,
252 typename = char,
253 int = 0,
254 bool Q = false,
255 class... Ts> void foo();
256 template<typename, bool...> class Foo;
257
258 void bar() {
259 [[fo^o]]<Foo>();
260 }
261 )cpp",
262 [](HoverInfo &HI) {
263 HI.NamespaceScope = "";
264 HI.Name = "foo";
265 HI.Kind = index::SymbolKind::Function;
266 HI.Definition = "template <> void foo<Foo, char, 0, false, <>>()";
267 HI.ReturnType = "void";
268 HI.Type = "void ()";
269 HI.Parameters.emplace();
270 }},
271 // Function decl
272 {R"cpp(
273 template<typename, bool...> class Foo {};
274 Foo<bool, true, false> foo(int, bool T = false);
275
276 void bar() {
277 [[fo^o]](3);
278 }
279 )cpp",
280 [](HoverInfo &HI) {
281 HI.NamespaceScope = "";
282 HI.Name = "foo";
283 HI.Kind = index::SymbolKind::Function;
284 HI.Definition = "Foo<bool, true, false> foo(int, bool T = false)";
285 HI.ReturnType = "Foo<bool, true, false>";
286 HI.Type = "Foo<bool, true, false> (int, bool)";
287 HI.Parameters = {
288 {{"int"}, std::nullopt, std::nullopt},
289 {{"bool"}, std::string("T"), std::string("false")},
290 };
291 }},
292 // Pointers to lambdas
293 {R"cpp(
294 void foo() {
295 auto lamb = [](int T, bool B) -> bool { return T && B; };
296 auto *b = &lamb;
297 auto *[[^c]] = &b;
298 }
299 )cpp",
300 [](HoverInfo &HI) {
301 HI.NamespaceScope = "";
302 HI.LocalScope = "foo::";
303 HI.Name = "c";
304 HI.Kind = index::SymbolKind::Variable;
305 HI.Definition = "auto *c = &b";
306 HI.Type = "(lambda) **";
307 HI.ReturnType = "bool";
308 HI.Parameters = {
309 {{"int"}, std::string("T"), std::nullopt},
310 {{"bool"}, std::string("B"), std::nullopt},
311 };
312 return HI;
313 }},
314 // Lambda parameter with decltype reference
315 {R"cpp(
316 auto lamb = [](int T, bool B) -> bool { return T && B; };
317 void foo(decltype(lamb)& bar) {
318 [[ba^r]](0, false);
319 }
320 )cpp",
321 [](HoverInfo &HI) {
322 HI.NamespaceScope = "";
323 HI.LocalScope = "foo::";
324 HI.Name = "bar";
325 HI.Kind = index::SymbolKind::Parameter;
326 HI.Definition = "decltype(lamb) &bar";
327 HI.Type = {"decltype(lamb) &", "(lambda) &"};
328 HI.ReturnType = "bool";
329 HI.Parameters = {
330 {{"int"}, std::string("T"), std::nullopt},
331 {{"bool"}, std::string("B"), std::nullopt},
332 };
333 return HI;
334 }},
335 // Lambda parameter with decltype
336 {R"cpp(
337 auto lamb = [](int T, bool B) -> bool { return T && B; };
338 void foo(decltype(lamb) bar) {
339 [[ba^r]](0, false);
340 }
341 )cpp",
342 [](HoverInfo &HI) {
343 HI.NamespaceScope = "";
344 HI.LocalScope = "foo::";
345 HI.Name = "bar";
346 HI.Kind = index::SymbolKind::Parameter;
347 HI.Definition = "decltype(lamb) bar";
348 HI.Type = "class (lambda)";
349 HI.ReturnType = "bool";
350 HI.Parameters = {
351 {{"int"}, std::string("T"), std::nullopt},
352 {{"bool"}, std::string("B"), std::nullopt},
353 };
354 HI.Value = "false";
355 return HI;
356 }},
357 // Lambda variable
358 {R"cpp(
359 void foo() {
360 int bar = 5;
361 auto lamb = [&bar](int T, bool B) -> bool { return T && B && bar; };
362 bool res = [[lam^b]](bar, false);
363 }
364 )cpp",
365 [](HoverInfo &HI) {
366 HI.NamespaceScope = "";
367 HI.LocalScope = "foo::";
368 HI.Name = "lamb";
369 HI.Kind = index::SymbolKind::Variable;
370 HI.Definition = "auto lamb = [&bar](int T, bool B) -> bool {}";
371 HI.Type = "class (lambda)";
372 HI.ReturnType = "bool";
373 HI.Parameters = {
374 {{"int"}, std::string("T"), std::nullopt},
375 {{"bool"}, std::string("B"), std::nullopt},
376 };
377 return HI;
378 }},
379 // Local variable in lambda
380 {R"cpp(
381 void foo() {
382 auto lamb = []{int [[te^st]];};
383 }
384 )cpp",
385 [](HoverInfo &HI) {
386 HI.NamespaceScope = "";
387 HI.LocalScope = "foo::(anonymous class)::operator()::";
388 HI.Name = "test";
389 HI.Kind = index::SymbolKind::Variable;
390 HI.Definition = "int test";
391 HI.Type = "int";
392 }},
393 // Partially-specialized class template. (formerly type-parameter-0-0)
394 {R"cpp(
395 template <typename T> class X;
396 template <typename T> class [[^X]]<T*> {};
397 )cpp",
398 [](HoverInfo &HI) {
399 HI.Name = "X<T *>";
400 HI.NamespaceScope = "";
401 HI.Kind = index::SymbolKind::Class;
402 HI.Definition = "template <typename T> class X<T *> {}";
403 }},
404 // Constructor of partially-specialized class template
405 {R"cpp(
406 template<typename, typename=void> struct X;
407 template<typename T> struct X<T*>{ [[^X]](); };
408 )cpp",
409 [](HoverInfo &HI) {
410 HI.NamespaceScope = "";
411 HI.Name = "X";
412 HI.LocalScope = "X<T *>::"; // FIXME: X<T *, void>::
413 HI.Kind = index::SymbolKind::Constructor;
414 HI.Definition = "X()";
415 HI.Parameters.emplace();
416 HI.AccessSpecifier = "public";
417 }},
418 {"class X { [[^~]]X(); };", // FIXME: Should be [[~X]]()
419 [](HoverInfo &HI) {
420 HI.NamespaceScope = "";
421 HI.Name = "~X";
422 HI.LocalScope = "X::";
423 HI.Kind = index::SymbolKind::Destructor;
424 HI.Definition = "~X()";
425 HI.Parameters.emplace();
426 HI.AccessSpecifier = "private";
427 }},
428 {"class X { [[op^erator]] int(); };",
429 [](HoverInfo &HI) {
430 HI.NamespaceScope = "";
431 HI.Name = "operator int";
432 HI.LocalScope = "X::";
433 HI.Kind = index::SymbolKind::ConversionFunction;
434 HI.Definition = "operator int()";
435 HI.Parameters.emplace();
436 HI.AccessSpecifier = "private";
437 }},
438 {"class X { operator [[^X]](); };",
439 [](HoverInfo &HI) {
440 HI.NamespaceScope = "";
441 HI.Name = "X";
442 HI.Kind = index::SymbolKind::Class;
443 HI.Definition = "class X {}";
444 }},
445
446 // auto on structured bindings
447 {R"cpp(
448 void foo() {
449 struct S { int x; float y; };
450 [[au^to]] [x, y] = S();
451 }
452 )cpp",
453 [](HoverInfo &HI) {
454 HI.Name = "auto";
455 HI.Kind = index::SymbolKind::TypeAlias;
456 HI.Definition = "S";
457 }},
458 // undeduced auto
459 {R"cpp(
460 template<typename T>
461 void foo() {
462 [[au^to]] x = T{};
463 }
464 )cpp",
465 [](HoverInfo &HI) {
466 HI.Name = "auto";
467 HI.Kind = index::SymbolKind::TypeAlias;
468 HI.Definition = "/* not deduced */";
469 }},
470 // auto on lambda
471 {R"cpp(
472 void foo() {
473 [[au^to]] lamb = []{};
474 }
475 )cpp",
476 [](HoverInfo &HI) {
477 HI.Name = "auto";
478 HI.Kind = index::SymbolKind::TypeAlias;
479 HI.Definition = "class(lambda)";
480 }},
481 // auto on template instantiation
482 {R"cpp(
483 template<typename T> class Foo{};
484 void foo() {
485 [[au^to]] x = Foo<int>();
486 }
487 )cpp",
488 [](HoverInfo &HI) {
489 HI.Name = "auto";
490 HI.Kind = index::SymbolKind::TypeAlias;
491 HI.Definition = "Foo<int>";
492 }},
493 // auto on specialized template
494 {R"cpp(
495 template<typename T> class Foo{};
496 template<> class Foo<int>{};
497 void foo() {
498 [[au^to]] x = Foo<int>();
499 }
500 )cpp",
501 [](HoverInfo &HI) {
502 HI.Name = "auto";
503 HI.Kind = index::SymbolKind::TypeAlias;
504 HI.Definition = "Foo<int>";
505 }},
506
507 // empty macro
508 {R"cpp(
509 #define MACRO
510 [[MAC^RO]]
511 )cpp",
512 [](HoverInfo &HI) {
513 HI.Name = "MACRO";
514 HI.Kind = index::SymbolKind::Macro;
515 HI.Definition = "#define MACRO";
516 }},
517
518 // object-like macro
519 {R"cpp(
520 #define MACRO 41
521 int x = [[MAC^RO]];
522 )cpp",
523 [](HoverInfo &HI) {
524 HI.Name = "MACRO";
525 HI.Kind = index::SymbolKind::Macro;
526 HI.Definition = "#define MACRO 41\n\n"
527 "// Expands to\n"
528 "41";
529 }},
530
531 // function-like macro
532 {R"cpp(
533 // Best MACRO ever.
534 #define MACRO(x,y,z) void foo(x, y, z)
535 [[MAC^RO]](int, double d, bool z = false);
536 )cpp",
537 [](HoverInfo &HI) {
538 HI.Name = "MACRO";
539 HI.Kind = index::SymbolKind::Macro;
540 HI.Definition = "#define MACRO(x, y, z) void foo(x, y, z)\n\n"
541 "// Expands to\n"
542 "void foo(int, double d, bool z = false)";
543 }},
544
545 // nested macro
546 {R"cpp(
547 #define STRINGIFY_AUX(s) #s
548 #define STRINGIFY(s) STRINGIFY_AUX(s)
549 #define DECL_STR(NAME, VALUE) const char *v_##NAME = STRINGIFY(VALUE)
550 #define FOO 41
551
552 [[DECL^_STR]](foo, FOO);
553 )cpp",
554 [](HoverInfo &HI) {
555 HI.Name = "DECL_STR";
556 HI.Kind = index::SymbolKind::Macro;
557 HI.Definition = "#define DECL_STR(NAME, VALUE) const char *v_##NAME = "
558 "STRINGIFY(VALUE)\n\n"
559 "// Expands to\n"
560 "const char *v_foo = \"41\"";
561 }},
562
563 // constexprs
564 {R"cpp(
565 constexpr int add(int a, int b) { return a + b; }
566 int [[b^ar]] = add(1, 2);
567 )cpp",
568 [](HoverInfo &HI) {
569 HI.Name = "bar";
570 HI.Definition = "int bar = add(1, 2)";
571 HI.Kind = index::SymbolKind::Variable;
572 HI.Type = "int";
573 HI.NamespaceScope = "";
574 HI.Value = "3";
575 }},
576 {R"cpp(
577 int [[b^ar]] = sizeof(char);
578 )cpp",
579 [](HoverInfo &HI) {
580 HI.Name = "bar";
581 HI.Definition = "int bar = sizeof(char)";
582 HI.Kind = index::SymbolKind::Variable;
583 HI.Type = "int";
584 HI.NamespaceScope = "";
585 HI.Value = "1";
586 }},
587 {R"cpp(
588 template<int a, int b> struct Add {
589 static constexpr int result = a + b;
590 };
591 int [[ba^r]] = Add<1, 2>::result;
592 )cpp",
593 [](HoverInfo &HI) {
594 HI.Name = "bar";
595 HI.Definition = "int bar = Add<1, 2>::result";
596 HI.Kind = index::SymbolKind::Variable;
597 HI.Type = "int";
598 HI.NamespaceScope = "";
599 HI.Value = "3";
600 }},
601 {R"cpp(
602 enum Color { RED = -123, GREEN = 5, };
603 Color x = [[GR^EEN]];
604 )cpp",
605 [](HoverInfo &HI) {
606 HI.Name = "GREEN";
607 HI.NamespaceScope = "";
608 HI.LocalScope = "Color::";
609 HI.Definition = "GREEN = 5";
610 HI.Kind = index::SymbolKind::EnumConstant;
611 HI.Type = "enum Color";
612 HI.Value = "5"; // Numeric on the enumerator name, no hex as small.
613 }},
614 {R"cpp(
615 enum Color { RED = -123, GREEN = 5, };
616 Color x = RED;
617 Color y = [[^x]];
618 )cpp",
619 [](HoverInfo &HI) {
620 HI.Name = "x";
621 HI.NamespaceScope = "";
622 HI.Definition = "Color x = RED";
623 HI.Kind = index::SymbolKind::Variable;
624 HI.Type = "Color";
625 HI.Value = "RED (0xffffff85)"; // Symbolic on an expression.
626 }},
627 {R"cpp(
628 template<int a, int b> struct Add {
629 static constexpr int result = a + b;
630 };
631 int bar = Add<1, 2>::[[resu^lt]];
632 )cpp",
633 [](HoverInfo &HI) {
634 HI.Name = "result";
635 HI.Definition = "static constexpr int result = a + b";
636 HI.Kind = index::SymbolKind::StaticProperty;
637 HI.Type = "const int";
638 HI.NamespaceScope = "";
639 HI.LocalScope = "Add<1, 2>::";
640 HI.Value = "3";
641 HI.AccessSpecifier = "public";
642 }},
643 {R"cpp(
644 using my_int = int;
645 constexpr my_int answer() { return 40 + 2; }
646 int x = [[ans^wer]]();
647 )cpp",
648 [](HoverInfo &HI) {
649 HI.Name = "answer";
650 HI.Definition = "constexpr my_int answer()";
651 HI.Kind = index::SymbolKind::Function;
652 HI.Type = {"my_int ()", "int ()"};
653 HI.ReturnType = {"my_int", "int"};
654 HI.Parameters.emplace();
655 HI.NamespaceScope = "";
656 HI.Value = "42 (0x2a)";
657 }},
658 {R"cpp(
659 const char *[[ba^r]] = "1234";
660 )cpp",
661 [](HoverInfo &HI) {
662 HI.Name = "bar";
663 HI.Definition = "const char *bar = \"1234\"";
664 HI.Kind = index::SymbolKind::Variable;
665 HI.Type = "const char *";
666 HI.NamespaceScope = "";
667 HI.Value = "&\"1234\"[0]";
668 }},
669 {R"cpp(// Should not crash
670 template <typename T>
671 struct Tmpl {
672 Tmpl(int name);
673 };
674
675 template <typename A>
676 void boom(int name) {
677 new Tmpl<A>([[na^me]]);
678 })cpp",
679 [](HoverInfo &HI) {
680 HI.Name = "name";
681 HI.Definition = "int name";
682 HI.Kind = index::SymbolKind::Parameter;
683 HI.Type = "int";
684 HI.NamespaceScope = "";
685 HI.LocalScope = "boom::";
686 }},
687 {
688 R"cpp(// Should not print inline or anon namespaces.
689 namespace ns {
690 inline namespace in_ns {
691 namespace a {
692 namespace {
693 namespace b {
694 inline namespace in_ns2 {
695 class Foo {};
696 } // in_ns2
697 } // b
698 } // anon
699 } // a
700 } // in_ns
701 } // ns
702 void foo() {
703 ns::a::b::[[F^oo]] x;
704 (void)x;
705 }
706 )cpp",
707 [](HoverInfo &HI) {
708 HI.Name = "Foo";
709 HI.Kind = index::SymbolKind::Class;
710 HI.NamespaceScope = "ns::a::b::";
711 HI.Definition = "class Foo {}";
712 }},
713 {
714 R"cpp(
715 template <typename T> class Foo {};
716 class X;
717 void foo() {
718 [[^auto]] x = Foo<X>();
719 }
720 )cpp",
721 [](HoverInfo &HI) {
722 HI.Name = "auto";
723 HI.Kind = index::SymbolKind::TypeAlias;
724 HI.Definition = "Foo<X>";
725 }},
726 {// Falls back to primary template, when the type is not instantiated.
727 R"cpp(
728 // comment from primary
729 template <typename T> class Foo {};
730 // comment from specialization
731 template <typename T> class Foo<T*> {};
732 void foo() {
733 [[Fo^o]]<int*> *x = nullptr;
734 }
735 )cpp",
736 [](HoverInfo &HI) {
737 HI.Name = "Foo<int *>";
738 HI.Kind = index::SymbolKind::Class;
739 HI.NamespaceScope = "";
740 HI.Definition = "template <> class Foo<int *>";
741 // FIXME: Maybe force instantiation to make use of real template
742 // pattern.
743 HI.Documentation = "comment from primary";
744 }},
745 {// Template Type Parameter
746 R"cpp(
747 template <typename [[^T]] = int> void foo();
748 )cpp",
749 [](HoverInfo &HI) {
750 HI.Name = "T";
751 HI.Kind = index::SymbolKind::TemplateTypeParm;
752 HI.NamespaceScope = "";
753 HI.Definition = "typename T = int";
754 HI.LocalScope = "foo::";
755 HI.Type = "typename";
756 HI.AccessSpecifier = "public";
757 }},
758 {// TemplateTemplate Type Parameter
759 R"cpp(
760 template <template<typename> class [[^T]]> void foo();
761 )cpp",
762 [](HoverInfo &HI) {
763 HI.Name = "T";
764 HI.Kind = index::SymbolKind::TemplateTemplateParm;
765 HI.NamespaceScope = "";
766 HI.Definition = "template <typename> class T";
767 HI.LocalScope = "foo::";
768 HI.Type = "template <typename> class";
769 HI.AccessSpecifier = "public";
770 }},
771 {// NonType Template Parameter
772 R"cpp(
773 template <int [[^T]] = 5> void foo();
774 )cpp",
775 [](HoverInfo &HI) {
776 HI.Name = "T";
777 HI.Kind = index::SymbolKind::NonTypeTemplateParm;
778 HI.NamespaceScope = "";
779 HI.Definition = "int T = 5";
780 HI.LocalScope = "foo::";
781 HI.Type = "int";
782 HI.AccessSpecifier = "public";
783 }},
784
785 {// Getter
786 R"cpp(
787 struct X { int Y; float [[^y]]() { return Y; } };
788 )cpp",
789 [](HoverInfo &HI) {
790 HI.Name = "y";
791 HI.Kind = index::SymbolKind::InstanceMethod;
792 HI.NamespaceScope = "";
793 HI.Definition = "float y()";
794 HI.LocalScope = "X::";
795 HI.Documentation = "Trivial accessor for `Y`.";
796 HI.Type = "float ()";
797 HI.ReturnType = "float";
798 HI.Parameters.emplace();
799 HI.AccessSpecifier = "public";
800 }},
801 {// Setter
802 R"cpp(
803 struct X { int Y; void [[^setY]](float v) { Y = v; } };
804 )cpp",
805 [](HoverInfo &HI) {
806 HI.Name = "setY";
807 HI.Kind = index::SymbolKind::InstanceMethod;
808 HI.NamespaceScope = "";
809 HI.Definition = "void setY(float v)";
810 HI.LocalScope = "X::";
811 HI.Documentation = "Trivial setter for `Y`.";
812 HI.Type = "void (float)";
813 HI.ReturnType = "void";
814 HI.Parameters.emplace();
815 HI.Parameters->emplace_back();
816 HI.Parameters->back().Type = "float";
817 HI.Parameters->back().Name = "v";
818 HI.AccessSpecifier = "public";
819 }},
820 {// Setter (builder)
821 R"cpp(
822 struct X { int Y; X& [[^setY]](float v) { Y = v; return *this; } };
823 )cpp",
824 [](HoverInfo &HI) {
825 HI.Name = "setY";
826 HI.Kind = index::SymbolKind::InstanceMethod;
827 HI.NamespaceScope = "";
828 HI.Definition = "X &setY(float v)";
829 HI.LocalScope = "X::";
830 HI.Documentation = "Trivial setter for `Y`.";
831 HI.Type = "X &(float)";
832 HI.ReturnType = "X &";
833 HI.Parameters.emplace();
834 HI.Parameters->emplace_back();
835 HI.Parameters->back().Type = "float";
836 HI.Parameters->back().Name = "v";
837 HI.AccessSpecifier = "public";
838 }},
839 {// Setter (move)
840 R"cpp(
841 namespace std { template<typename T> T&& move(T&& t); }
842 struct X { int Y; void [[^setY]](float v) { Y = std::move(v); } };
843 )cpp",
844 [](HoverInfo &HI) {
845 HI.Name = "setY";
846 HI.Kind = index::SymbolKind::InstanceMethod;
847 HI.NamespaceScope = "";
848 HI.Definition = "void setY(float v)";
849 HI.LocalScope = "X::";
850 HI.Documentation = "Trivial setter for `Y`.";
851 HI.Type = "void (float)";
852 HI.ReturnType = "void";
853 HI.Parameters.emplace();
854 HI.Parameters->emplace_back();
855 HI.Parameters->back().Type = "float";
856 HI.Parameters->back().Name = "v";
857 HI.AccessSpecifier = "public";
858 }},
859 {// Field type initializer.
860 R"cpp(
861 struct X { int x = 2; };
862 X ^[[x]];
863 )cpp",
864 [](HoverInfo &HI) {
865 HI.Name = "x";
866 HI.Kind = index::SymbolKind::Variable;
867 HI.NamespaceScope = "";
868 HI.Definition = "X x";
869 HI.Type = "X";
870 }},
871 {// Don't crash on null types.
872 R"cpp(auto [^[[x]]] = 1; /*error-ok*/)cpp",
873 [](HoverInfo &HI) {
874 HI.Name = "x";
875 HI.Kind = index::SymbolKind::Variable;
876 HI.NamespaceScope = "";
877 HI.Definition = "";
878 HI.Type = "NULL TYPE";
879 // Bindings are in theory public members of an anonymous struct.
880 HI.AccessSpecifier = "public";
881 }},
882 {// Extra info for function call.
883 R"cpp(
884 void fun(int arg_a, int &arg_b) {};
885 void code() {
886 int a = 1, b = 2;
887 fun(a, [[^b]]);
888 }
889 )cpp",
890 [](HoverInfo &HI) {
891 HI.Name = "b";
892 HI.Kind = index::SymbolKind::Variable;
893 HI.NamespaceScope = "";
894 HI.Definition = "int b = 2";
895 HI.LocalScope = "code::";
896 HI.Value = "2";
897 HI.Type = "int";
898 HI.CalleeArgInfo.emplace();
899 HI.CalleeArgInfo->Name = "arg_b";
900 HI.CalleeArgInfo->Type = "int &";
901 HI.CallPassType = HoverInfo::PassType{PassMode::Ref, false};
902 }},
903 {// Literal passed to function call
904 R"cpp(
905 void fun(int arg_a, const int &arg_b) {};
906 void code() {
907 int a = 1;
908 fun(a, [[^2]]);
909 }
910 )cpp",
911 [](HoverInfo &HI) {
912 HI.Name = "literal";
913 HI.Kind = index::SymbolKind::Unknown;
914 HI.CalleeArgInfo.emplace();
915 HI.CalleeArgInfo->Name = "arg_b";
916 HI.CalleeArgInfo->Type = "const int &";
917 HI.CallPassType = HoverInfo::PassType{PassMode::ConstRef, false};
918 }},
919 {// Expression passed to function call
920 R"cpp(
921 void fun(int arg_a, const int &arg_b) {};
922 void code() {
923 int a = 1;
924 fun(a, 1 [[^+]] 2);
925 }
926 )cpp",
927 [](HoverInfo &HI) {
928 HI.Name = "expression";
929 HI.Kind = index::SymbolKind::Unknown;
930 HI.Type = "int";
931 HI.Value = "3";
932 HI.CalleeArgInfo.emplace();
933 HI.CalleeArgInfo->Name = "arg_b";
934 HI.CalleeArgInfo->Type = "const int &";
935 HI.CallPassType = HoverInfo::PassType{PassMode::ConstRef, false};
936 }},
937 {// Extra info for method call.
938 R"cpp(
939 class C {
940 public:
941 void fun(int arg_a = 3, int arg_b = 4) {}
942 };
943 void code() {
944 int a = 1, b = 2;
945 C c;
946 c.fun([[^a]], b);
947 }
948 )cpp",
949 [](HoverInfo &HI) {
950 HI.Name = "a";
951 HI.Kind = index::SymbolKind::Variable;
952 HI.NamespaceScope = "";
953 HI.Definition = "int a = 1";
954 HI.LocalScope = "code::";
955 HI.Value = "1";
956 HI.Type = "int";
957 HI.CalleeArgInfo.emplace();
958 HI.CalleeArgInfo->Name = "arg_a";
959 HI.CalleeArgInfo->Type = "int";
960 HI.CalleeArgInfo->Default = "3";
961 HI.CallPassType = HoverInfo::PassType{PassMode::Value, false};
962 }},
963 {// Dont crash on invalid decl
964 R"cpp(
965 // error-ok
966 struct Foo {
967 Bar [[x^x]];
968 };)cpp",
969 [](HoverInfo &HI) {
970 HI.Name = "xx";
971 HI.Kind = index::SymbolKind::Field;
972 HI.NamespaceScope = "";
973 HI.Definition = "int xx";
974 HI.LocalScope = "Foo::";
975 HI.Type = "int";
976 HI.AccessSpecifier = "public";
977 }},
978 {R"cpp(
979 // error-ok
980 struct Foo {
981 Bar xx;
982 int [[y^y]];
983 };)cpp",
984 [](HoverInfo &HI) {
985 HI.Name = "yy";
986 HI.Kind = index::SymbolKind::Field;
987 HI.NamespaceScope = "";
988 HI.Definition = "int yy";
989 HI.LocalScope = "Foo::";
990 HI.Type = "int";
991 HI.AccessSpecifier = "public";
992 }},
993 {// No crash on InitListExpr.
994 R"cpp(
995 struct Foo {
996 int a[10];
997 };
998 constexpr Foo k2 = {
999 ^[[{]]1} // FIXME: why the hover range is 1 character?
1000 };
1001 )cpp",
1002 [](HoverInfo &HI) {
1003 HI.Name = "expression";
1004 HI.Kind = index::SymbolKind::Unknown;
1005 HI.Type = "int[10]";
1006 HI.Value = "{1}";
1007 }},
1008 {// Var template decl
1009 R"cpp(
1010 using m_int = int;
1011
1012 template <int Size> m_int ^[[arr]][Size];
1013 )cpp",
1014 [](HoverInfo &HI) {
1015 HI.Name = "arr";
1016 HI.Kind = index::SymbolKind::Variable;
1017 HI.Type = {"m_int[Size]", "int[Size]"};
1018 HI.NamespaceScope = "";
1019 HI.Definition = "template <int Size> m_int arr[Size]";
1020 HI.TemplateParameters = {{{"int"}, {"Size"}, std::nullopt}};
1021 }},
1022 {// Var template decl specialization
1023 R"cpp(
1024 using m_int = int;
1025
1026 template <int Size> m_int arr[Size];
1027
1028 template <> m_int ^[[arr]]<4>[4];
1029 )cpp",
1030 [](HoverInfo &HI) {
1031 HI.Name = "arr<4>";
1032 HI.Kind = index::SymbolKind::Variable;
1033 HI.Type = {"m_int[4]", "int[4]"};
1034 HI.NamespaceScope = "";
1035 HI.Definition = "m_int arr[4]";
1036 }},
1037 {// Canonical type
1038 R"cpp(
1039 template<typename T>
1040 struct TestHover {
1041 using Type = T;
1042 };
1043
1044 void code() {
1045 TestHover<int>::Type ^[[a]];
1046 }
1047 )cpp",
1048 [](HoverInfo &HI) {
1049 HI.Name = "a";
1050 HI.NamespaceScope = "";
1051 HI.LocalScope = "code::";
1052 HI.Definition = "TestHover<int>::Type a";
1053 HI.Kind = index::SymbolKind::Variable;
1054 HI.Type = {"TestHover<int>::Type", "int"};
1055 }},
1056 {// Canonical template type
1057 R"cpp(
1058 template<typename T>
1059 void ^[[foo]](T arg) {}
1060 )cpp",
1061 [](HoverInfo &HI) {
1062 HI.Name = "foo";
1063 HI.Kind = index::SymbolKind::Function;
1064 HI.NamespaceScope = "";
1065 HI.Definition = "template <typename T> void foo(T arg)";
1066 HI.Type = "void (T)";
1067 HI.ReturnType = "void";
1068 HI.Parameters = {{{"T"}, std::string("arg"), std::nullopt}};
1069 HI.TemplateParameters = {
1070 {{"typename"}, std::string("T"), std::nullopt}};
1071 }},
1072 {// TypeAlias Template
1073 R"cpp(
1074 template<typename T>
1075 using ^[[alias]] = T;
1076 )cpp",
1077 [](HoverInfo &HI) {
1078 HI.Name = "alias";
1079 HI.NamespaceScope = "";
1080 HI.LocalScope = "";
1081 HI.Kind = index::SymbolKind::TypeAlias;
1082 HI.Definition = "template <typename T> using alias = T";
1083 HI.Type = "T";
1084 HI.TemplateParameters = {
1085 {{"typename"}, std::string("T"), std::nullopt}};
1086 }},
1087 {// TypeAlias Template
1088 R"cpp(
1089 template<typename T>
1090 using A = T;
1091
1092 template<typename T>
1093 using ^[[AA]] = A<T>;
1094 )cpp",
1095 [](HoverInfo &HI) {
1096 HI.Name = "AA";
1097 HI.NamespaceScope = "";
1098 HI.LocalScope = "";
1099 HI.Kind = index::SymbolKind::TypeAlias;
1100 HI.Definition = "template <typename T> using AA = A<T>";
1101 HI.Type = {"A<T>", "type-parameter-0-0"}; // FIXME: should be 'T'
1102 HI.TemplateParameters = {
1103 {{"typename"}, std::string("T"), std::nullopt}};
1104 }},
1105 {// Constant array
1106 R"cpp(
1107 using m_int = int;
1108
1109 m_int ^[[arr]][10];
1110 )cpp",
1111 [](HoverInfo &HI) {
1112 HI.Name = "arr";
1113 HI.NamespaceScope = "";
1114 HI.LocalScope = "";
1115 HI.Kind = index::SymbolKind::Variable;
1116 HI.Definition = "m_int arr[10]";
1117 HI.Type = {"m_int[10]", "int[10]"};
1118 }},
1119 {// Incomplete array
1120 R"cpp(
1121 using m_int = int;
1122
1123 extern m_int ^[[arr]][];
1124 )cpp",
1125 [](HoverInfo &HI) {
1126 HI.Name = "arr";
1127 HI.NamespaceScope = "";
1128 HI.LocalScope = "";
1129 HI.Kind = index::SymbolKind::Variable;
1130 HI.Definition = "extern m_int arr[]";
1131 HI.Type = {"m_int[]", "int[]"};
1132 }},
1133 {// Dependent size array
1134 R"cpp(
1135 using m_int = int;
1136
1137 template<int Size>
1138 struct Test {
1139 m_int ^[[arr]][Size];
1140 };
1141 )cpp",
1142 [](HoverInfo &HI) {
1143 HI.Name = "arr";
1144 HI.NamespaceScope = "";
1145 HI.LocalScope = "Test<Size>::";
1146 HI.AccessSpecifier = "public";
1147 HI.Kind = index::SymbolKind::Field;
1148 HI.Definition = "m_int arr[Size]";
1149 HI.Type = {"m_int[Size]", "int[Size]"};
1150 }}};
1151 for (const auto &Case : Cases) {
1152 SCOPED_TRACE(Case.Code);
1153
1154 Annotations T(Case.Code);
1155 TestTU TU = TestTU::withCode(T.code());
1156 TU.ExtraArgs.push_back("-std=c++17");
1157 // Types might be different depending on the target triplet, we chose a
1158 // fixed one to make sure tests passes on different platform.
1159 TU.ExtraArgs.push_back("--target=x86_64-pc-linux-gnu");
1160 auto AST = TU.build();
1161 Config Cfg;
1162 Cfg.Hover.ShowAKA = true;
1163 WithContextValue WithCfg(Config::Key, std::move(Cfg));
1164
1165 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
1166 ASSERT_TRUE(H);
1167 HoverInfo Expected;
1168 Expected.SymRange = T.range();
1169 Case.ExpectedBuilder(Expected);
1170
1171 EXPECT_EQ(H->NamespaceScope, Expected.NamespaceScope);
1172 EXPECT_EQ(H->LocalScope, Expected.LocalScope);
1173 EXPECT_EQ(H->Name, Expected.Name);
1174 EXPECT_EQ(H->Kind, Expected.Kind);
1175 EXPECT_EQ(H->Documentation, Expected.Documentation);
1176 EXPECT_EQ(H->Definition, Expected.Definition);
1177 EXPECT_EQ(H->Type, Expected.Type);
1178 EXPECT_EQ(H->ReturnType, Expected.ReturnType);
1179 EXPECT_EQ(H->Parameters, Expected.Parameters);
1180 EXPECT_EQ(H->TemplateParameters, Expected.TemplateParameters);
1181 EXPECT_EQ(H->SymRange, Expected.SymRange);
1182 EXPECT_EQ(H->Value, Expected.Value);
1183 EXPECT_EQ(H->Size, Expected.Size);
1184 EXPECT_EQ(H->Offset, Expected.Offset);
1185 EXPECT_EQ(H->AccessSpecifier, Expected.AccessSpecifier);
1186 EXPECT_EQ(H->CalleeArgInfo, Expected.CalleeArgInfo);
1187 EXPECT_EQ(H->CallPassType, Expected.CallPassType);
1188 }
1189 }
1190
1191 TEST(Hover, DefinitionLanuage) {
1192 struct {
1193 const char *const Code;
1194 const std::string ClangLanguageFlag;
1195 const char *const ExpectedDefinitionLanguage;
1196 } Cases[] = {{R"cpp(
1197 void [[some^Global]]() {}
1198 )cpp",
1199 "", "cpp"},
1200 {R"cpp(
1201 void [[some^Global]]() {}
1202 )cpp",
1203 "-xobjective-c++", "objective-cpp"},
1204 {R"cpp(
1205 void [[some^Global]]() {}
1206 )cpp",
1207 "-xobjective-c", "objective-c"}};
1208 for (const auto &Case : Cases) {
1209 SCOPED_TRACE(Case.Code);
1210
1211 Annotations T(Case.Code);
1212 TestTU TU = TestTU::withCode(T.code());
1213 if (!Case.ClangLanguageFlag.empty())
1214 TU.ExtraArgs.push_back(Case.ClangLanguageFlag);
1215 auto AST = TU.build();
1216
1217 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
1218 ASSERT_TRUE(H);
1219
1220 EXPECT_STREQ(H->DefinitionLanguage, Case.ExpectedDefinitionLanguage);
1221 }
1222 }
1223
1224 TEST(Hover, CallPassType) {
1225 const llvm::StringRef CodePrefix = R"cpp(
1226 class Base {};
1227 class Derived : public Base {};
1228 class CustomClass {
1229 public:
1230 CustomClass() {}
1231 CustomClass(const Base &x) {}
1232 CustomClass(int &x) {}
1233 CustomClass(float x) {}
1234 };
1235
1236 void int_by_ref(int &x) {}
1237 void int_by_const_ref(const int &x) {}
1238 void int_by_value(int x) {}
1239 void base_by_ref(Base &x) {}
1240 void base_by_const_ref(const Base &x) {}
1241 void base_by_value(Base x) {}
1242 void float_by_value(float x) {}
1243 void custom_by_value(CustomClass x) {}
1244
1245 void fun() {
1246 int int_x;
1247 int &int_ref = int_x;
1248 const int &int_const_ref = int_x;
1249 Base base;
1250 const Base &base_const_ref = base;
1251 Derived derived;
1252 float float_x;
1253 )cpp";
1254 const llvm::StringRef CodeSuffix = "}";
1255
1256 struct {
1257 const char *const Code;
1258 HoverInfo::PassType::PassMode PassBy;
1259 bool Converted;
1260 } Tests[] = {
1261 // Integer tests
1262 {"int_by_value([[^int_x]]);", PassMode::Value, false},
1263 {"int_by_value([[^123]]);", PassMode::Value, false},
1264 {"int_by_ref([[^int_x]]);", PassMode::Ref, false},
1265 {"int_by_const_ref([[^int_x]]);", PassMode::ConstRef, false},
1266 {"int_by_const_ref([[^123]]);", PassMode::ConstRef, false},
1267 {"int_by_value([[^int_ref]]);", PassMode::Value, false},
1268 {"int_by_const_ref([[^int_ref]]);", PassMode::ConstRef, false},
1269 {"int_by_const_ref([[^int_ref]]);", PassMode::ConstRef, false},
1270 {"int_by_const_ref([[^int_const_ref]]);", PassMode::ConstRef, false},
1271 // Custom class tests
1272 {"base_by_ref([[^base]]);", PassMode::Ref, false},
1273 {"base_by_const_ref([[^base]]);", PassMode::ConstRef, false},
1274 {"base_by_const_ref([[^base_const_ref]]);", PassMode::ConstRef, false},
1275 {"base_by_value([[^base]]);", PassMode::Value, false},
1276 {"base_by_value([[^base_const_ref]]);", PassMode::Value, false},
1277 {"base_by_ref([[^derived]]);", PassMode::Ref, false},
1278 {"base_by_const_ref([[^derived]]);", PassMode::ConstRef, false},
1279 {"base_by_value([[^derived]]);", PassMode::Value, false},
1280 // Converted tests
1281 {"float_by_value([[^int_x]]);", PassMode::Value, true},
1282 {"float_by_value([[^int_ref]]);", PassMode::Value, true},
1283 {"float_by_value([[^int_const_ref]]);", PassMode::Value, true},
1284 {"float_by_value([[^123.0f]]);", PassMode::Value, false},
1285 {"float_by_value([[^123]]);", PassMode::Value, true},
1286 {"custom_by_value([[^int_x]]);", PassMode::Ref, true},
1287 {"custom_by_value([[^float_x]]);", PassMode::Value, true},
1288 {"custom_by_value([[^base]]);", PassMode::ConstRef, true},
1289 };
1290 for (const auto &Test : Tests) {
1291 SCOPED_TRACE(Test.Code);
1292
1293 const auto Code = (CodePrefix + Test.Code + CodeSuffix).str();
1294 Annotations T(Code);
1295 TestTU TU = TestTU::withCode(T.code());
1296 TU.ExtraArgs.push_back("-std=c++17");
1297 auto AST = TU.build();
1298 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
1299 ASSERT_TRUE(H);
1300 EXPECT_EQ(H->CallPassType->PassBy, Test.PassBy);
1301 EXPECT_EQ(H->CallPassType->Converted, Test.Converted);
1302 }
1303 }
1304
1305 TEST(Hover, NoHover) {
1306 llvm::StringRef Tests[] = {
1307 "^int main() {}",
1308 "void foo() {^}",
1309 // FIXME: "decltype(auto)" should be a single hover
1310 "decltype(au^to) x = 0;",
1311 // FIXME: not supported yet
1312 R"cpp(// Lambda auto parameter
1313 auto lamb = [](a^uto){};
1314 )cpp",
1315 R"cpp(// non-named decls don't get hover. Don't crash!
1316 ^static_assert(1, "");
1317 )cpp",
1318 R"cpp(// non-evaluatable expr
1319 template <typename T> void foo() {
1320 (void)[[size^of]](T);
1321 })cpp",
1322 R"cpp(// should not crash on invalid semantic form of init-list-expr.
1323 /*error-ok*/
1324 struct Foo {
1325 int xyz = 0;
1326 };
1327 class Bar {};
1328 constexpr Foo s = ^{
1329 .xyz = Bar(),
1330 };
1331 )cpp",
1332 // literals
1333 "auto x = t^rue;",
1334 "auto x = ^(int){42};",
1335 "auto x = ^42.;",
1336 "auto x = ^42.0i;",
1337 "auto x = ^42;",
1338 "auto x = ^nullptr;",
1339 };
1340
1341 for (const auto &Test : Tests) {
1342 SCOPED_TRACE(Test);
1343
1344 Annotations T(Test);
1345 TestTU TU = TestTU::withCode(T.code());
1346 TU.ExtraArgs.push_back("-std=c++17");
1347 auto AST = TU.build();
1348 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
1349 ASSERT_FALSE(H);
1350 }
1351 }
1352
1353 TEST(Hover, All) {
1354 struct {
1355 const char *const Code;
1356 const std::function<void(HoverInfo &)> ExpectedBuilder;
1357 } Cases[] = {
1358 {"auto x = [['^A']]; // character literal",
1359 [](HoverInfo &HI) {
1360 HI.Name = "expression";
1361 HI.Type = "char";
1362 HI.Value = "65 (0x41)";
1363 }},
1364 {"auto s = ^[[\"Hello, world!\"]]; // string literal",
1365 [](HoverInfo &HI) {
1366 HI.Name = "string-literal";
1367 HI.Size = 14;
1368 HI.Type = "const char[14]";
1369 }},
1370 {
1371 R"cpp(// Local variable
1372 int main() {
1373 int bonjour;
1374 ^[[bonjour]] = 2;
1375 int test1 = bonjour;
1376 }
1377 )cpp",
1378 [](HoverInfo &HI) {
1379 HI.Name = "bonjour";
1380 HI.Kind = index::SymbolKind::Variable;
1381 HI.NamespaceScope = "";
1382 HI.LocalScope = "main::";
1383 HI.Type = "int";
1384 HI.Definition = "int bonjour";
1385 }},
1386 {
1387 R"cpp(// Local variable in method
1388 struct s {
1389 void method() {
1390 int bonjour;
1391 ^[[bonjour]] = 2;
1392 }
1393 };
1394 )cpp",
1395 [](HoverInfo &HI) {
1396 HI.Name = "bonjour";
1397 HI.Kind = index::SymbolKind::Variable;
1398 HI.NamespaceScope = "";
1399 HI.LocalScope = "s::method::";
1400 HI.Type = "int";
1401 HI.Definition = "int bonjour";
1402 }},
1403 {
1404 R"cpp(// Struct
1405 namespace ns1 {
1406 struct MyClass {};
1407 } // namespace ns1
1408 int main() {
1409 ns1::[[My^Class]]* Params;
1410 }
1411 )cpp",
1412 [](HoverInfo &HI) {
1413 HI.Name = "MyClass";
1414 HI.Kind = index::SymbolKind::Struct;
1415 HI.NamespaceScope = "ns1::";
1416 HI.Definition = "struct MyClass {}";
1417 }},
1418 {
1419 R"cpp(// Class
1420 namespace ns1 {
1421 class MyClass {};
1422 } // namespace ns1
1423 int main() {
1424 ns1::[[My^Class]]* Params;
1425 }
1426 )cpp",
1427 [](HoverInfo &HI) {
1428 HI.Name = "MyClass";
1429 HI.Kind = index::SymbolKind::Class;
1430 HI.NamespaceScope = "ns1::";
1431 HI.Definition = "class MyClass {}";
1432 }},
1433 {
1434 R"cpp(// Union
1435 namespace ns1 {
1436 union MyUnion { int x; int y; };
1437 } // namespace ns1
1438 int main() {
1439 ns1::[[My^Union]] Params;
1440 }
1441 )cpp",
1442 [](HoverInfo &HI) {
1443 HI.Name = "MyUnion";
1444 HI.Kind = index::SymbolKind::Union;
1445 HI.NamespaceScope = "ns1::";
1446 HI.Definition = "union MyUnion {}";
1447 }},
1448 {
1449 R"cpp(// Function definition via pointer
1450 void foo(int) {}
1451 int main() {
1452 auto *X = &^[[foo]];
1453 }
1454 )cpp",
1455 [](HoverInfo &HI) {
1456 HI.Name = "foo";
1457 HI.Kind = index::SymbolKind::Function;
1458 HI.NamespaceScope = "";
1459 HI.Type = "void (int)";
1460 HI.Definition = "void foo(int)";
1461 HI.Documentation = "Function definition via pointer";
1462 HI.ReturnType = "void";
1463 HI.Parameters = {
1464 {{"int"}, std::nullopt, std::nullopt},
1465 };
1466 }},
1467 {
1468 R"cpp(// Function declaration via call
1469 int foo(int);
1470 int main() {
1471 return ^[[foo]](42);
1472 }
1473 )cpp",
1474 [](HoverInfo &HI) {
1475 HI.Name = "foo";
1476 HI.Kind = index::SymbolKind::Function;
1477 HI.NamespaceScope = "";
1478 HI.Type = "int (int)";
1479 HI.Definition = "int foo(int)";
1480 HI.Documentation = "Function declaration via call";
1481 HI.ReturnType = "int";
1482 HI.Parameters = {
1483 {{"int"}, std::nullopt, std::nullopt},
1484 };
1485 }},
1486 {
1487 R"cpp(// Field
1488 struct Foo { int x; };
1489 int main() {
1490 Foo bar;
1491 (void)bar.^[[x]];
1492 }
1493 )cpp",
1494 [](HoverInfo &HI) {
1495 HI.Name = "x";
1496 HI.Kind = index::SymbolKind::Field;
1497 HI.NamespaceScope = "";
1498 HI.LocalScope = "Foo::";
1499 HI.Type = "int";
1500 HI.Definition = "int x";
1501 }},
1502 {
1503 R"cpp(// Field with initialization
1504 struct Foo { int x = 5; };
1505 int main() {
1506 Foo bar;
1507 (void)bar.^[[x]];
1508 }
1509 )cpp",
1510 [](HoverInfo &HI) {
1511 HI.Name = "x";
1512 HI.Kind = index::SymbolKind::Field;
1513 HI.NamespaceScope = "";
1514 HI.LocalScope = "Foo::";
1515 HI.Type = "int";
1516 HI.Definition = "int x = 5";
1517 }},
1518 {
1519 R"cpp(// Static field
1520 struct Foo { static int x; };
1521 int main() {
1522 (void)Foo::^[[x]];
1523 }
1524 )cpp",
1525 [](HoverInfo &HI) {
1526 HI.Name = "x";
1527 HI.Kind = index::SymbolKind::StaticProperty;
1528 HI.NamespaceScope = "";
1529 HI.LocalScope = "Foo::";
1530 HI.Type = "int";
1531 HI.Definition = "static int x";
1532 }},
1533 {
1534 R"cpp(// Field, member initializer
1535 struct Foo {
1536 int x;
1537 Foo() : ^[[x]](0) {}
1538 };
1539 )cpp",
1540 [](HoverInfo &HI) {
1541 HI.Name = "x";
1542 HI.Kind = index::SymbolKind::Field;
1543 HI.NamespaceScope = "";
1544 HI.LocalScope = "Foo::";
1545 HI.Type = "int";
1546 HI.Definition = "int x";
1547 }},
1548 {
1549 R"cpp(// Field, GNU old-style field designator
1550 struct Foo { int x; };
1551 int main() {
1552 Foo bar = { ^[[x]] : 1 };
1553 }
1554 )cpp",
1555 [](HoverInfo &HI) {
1556 HI.Name = "x";
1557 HI.Kind = index::SymbolKind::Field;
1558 HI.NamespaceScope = "";
1559 HI.LocalScope = "Foo::";
1560 HI.Type = "int";
1561 HI.Definition = "int x";
1562 // FIXME: Initializer for x is a DesignatedInitListExpr, hence it is
1563 // of struct type and omitted.
1564 }},
1565 {
1566 R"cpp(// Field, field designator
1567 struct Foo { int x; int y; };
1568 int main() {
1569 Foo bar = { .^[[x]] = 2, .y = 2 };
1570 }
1571 )cpp",
1572 [](HoverInfo &HI) {
1573 HI.Name = "x";
1574 HI.Kind = index::SymbolKind::Field;
1575 HI.NamespaceScope = "";
1576 HI.LocalScope = "Foo::";
1577 HI.Type = "int";
1578 HI.Definition = "int x";
1579 }},
1580 {
1581 R"cpp(// Method call
1582 struct Foo { int x(); };
1583 int main() {
1584 Foo bar;
1585 bar.^[[x]]();
1586 }
1587 )cpp",
1588 [](HoverInfo &HI) {
1589 HI.Name = "x";
1590 HI.Kind = index::SymbolKind::InstanceMethod;
1591 HI.NamespaceScope = "";
1592 HI.LocalScope = "Foo::";
1593 HI.Type = "int ()";
1594 HI.Definition = "int x()";
1595 HI.ReturnType = "int";
1596 HI.Parameters = std::vector<HoverInfo::Param>{};
1597 }},
1598 {
1599 R"cpp(// Static method call
1600 struct Foo { static int x(); };
1601 int main() {
1602 Foo::^[[x]]();
1603 }
1604 )cpp",
1605 [](HoverInfo &HI) {
1606 HI.Name = "x";
1607 HI.Kind = index::SymbolKind::StaticMethod;
1608 HI.NamespaceScope = "";
1609 HI.LocalScope = "Foo::";
1610 HI.Type = "int ()";
1611 HI.Definition = "static int x()";
1612 HI.ReturnType = "int";
1613 HI.Parameters = std::vector<HoverInfo::Param>{};
1614 }},
1615 {
1616 R"cpp(// Typedef
1617 typedef int Foo;
1618 int main() {
1619 ^[[Foo]] bar;
1620 }
1621 )cpp",
1622 [](HoverInfo &HI) {
1623 HI.Name = "Foo";
1624 HI.Kind = index::SymbolKind::TypeAlias;
1625 HI.NamespaceScope = "";
1626 HI.Definition = "typedef int Foo";
1627 HI.Type = "int";
1628 HI.Documentation = "Typedef";
1629 }},
1630 {
1631 R"cpp(// Typedef with embedded definition
1632 typedef struct Bar {} Foo;
1633 int main() {
1634 ^[[Foo]] bar;
1635 }
1636 )cpp",
1637 [](HoverInfo &HI) {
1638 HI.Name = "Foo";
1639 HI.Kind = index::SymbolKind::TypeAlias;
1640 HI.NamespaceScope = "";
1641 HI.Definition = "typedef struct Bar Foo";
1642 HI.Type = "struct Bar";
1643 HI.Documentation = "Typedef with embedded definition";
1644 }},
1645 {
1646 R"cpp(// Namespace
1647 namespace ns {
1648 struct Foo { static void bar(); };
1649 } // namespace ns
1650 int main() { ^[[ns]]::Foo::bar(); }
1651 )cpp",
1652 [](HoverInfo &HI) {
1653 HI.Name = "ns";
1654 HI.Kind = index::SymbolKind::Namespace;
1655 HI.NamespaceScope = "";
1656 HI.Definition = "namespace ns {}";
1657 }},
1658 {
1659 R"cpp(// Anonymous namespace
1660 namespace ns {
1661 namespace {
1662 int foo;
1663 } // anonymous namespace
1664 } // namespace ns
1665 int main() { ns::[[f^oo]]++; }
1666 )cpp",
1667 [](HoverInfo &HI) {
1668 HI.Name = "foo";
1669 HI.Kind = index::SymbolKind::Variable;
1670 HI.NamespaceScope = "ns::";
1671 HI.Type = "int";
1672 HI.Definition = "int foo";
1673 }},
1674 {
1675 R"cpp(// Function definition via using declaration
1676 namespace ns {
1677 void foo();
1678 }
1679 int main() {
1680 using ns::foo;
1681 ^[[foo]]();
1682 }
1683 )cpp",
1684 [](HoverInfo &HI) {
1685 HI.Name = "foo";
1686 HI.Kind = index::SymbolKind::Function;
1687 HI.NamespaceScope = "ns::";
1688 HI.Type = "void ()";
1689 HI.Definition = "void foo()";
1690 HI.Documentation = "";
1691 HI.ReturnType = "void";
1692 HI.Parameters = std::vector<HoverInfo::Param>{};
1693 }},
1694 {
1695 R"cpp( // using declaration and two possible function declarations
1696 namespace ns { void foo(int); void foo(char); }
1697 using ns::foo;
1698 template <typename T> void bar() { [[f^oo]](T{}); }
1699 )cpp",
1700 [](HoverInfo &HI) {
1701 HI.Name = "foo";
1702 HI.Kind = index::SymbolKind::Using;
1703 HI.NamespaceScope = "";
1704 HI.Definition = "using ns::foo";
1705 }},
1706 {
1707 R"cpp(// Macro
1708 #define MACRO 0
1709 int main() { return ^[[MACRO]]; }
1710 )cpp",
1711 [](HoverInfo &HI) {
1712 HI.Name = "MACRO";
1713 HI.Kind = index::SymbolKind::Macro;
1714 HI.Definition = "#define MACRO 0\n\n"
1715 "// Expands to\n"
1716 "0";
1717 }},
1718 {
1719 R"cpp(// Macro
1720 #define MACRO 0
1721 #define MACRO2 ^[[MACRO]]
1722 )cpp",
1723 [](HoverInfo &HI) {
1724 HI.Name = "MACRO";
1725 HI.Kind = index::SymbolKind::Macro;
1726 HI.Definition = "#define MACRO 0";
1727 // NOTE MACRO doesn't have expansion since it technically isn't
1728 // expanded here
1729 }},
1730 {
1731 R"cpp(// Macro
1732 #define MACRO {\
1733 return 0;\
1734 }
1735 int main() ^[[MACRO]]
1736 )cpp",
1737 [](HoverInfo &HI) {
1738 HI.Name = "MACRO";
1739 HI.Kind = index::SymbolKind::Macro;
1740 HI.Definition =
1741 R"cpp(#define MACRO \
1742 { return 0; }
1743
1744 // Expands to
1745 { return 0; })cpp";
1746 }},
1747 {
1748 R"cpp(// Forward class declaration
1749 class Foo;
1750 class Foo {};
1751 [[F^oo]]* foo();
1752 )cpp",
1753 [](HoverInfo &HI) {
1754 HI.Name = "Foo";
1755 HI.Kind = index::SymbolKind::Class;
1756 HI.NamespaceScope = "";
1757 HI.Definition = "class Foo {}";
1758 HI.Documentation = "Forward class declaration";
1759 }},
1760 {
1761 R"cpp(// Function declaration
1762 void foo();
1763 void g() { [[f^oo]](); }
1764 void foo() {}
1765 )cpp",
1766 [](HoverInfo &HI) {
1767 HI.Name = "foo";
1768 HI.Kind = index::SymbolKind::Function;
1769 HI.NamespaceScope = "";
1770 HI.Type = "void ()";
1771 HI.Definition = "void foo()";
1772 HI.Documentation = "Function declaration";
1773 HI.ReturnType = "void";
1774 HI.Parameters = std::vector<HoverInfo::Param>{};
1775 }},
1776 {
1777 R"cpp(// Enum declaration
1778 enum Hello {
1779 ONE, TWO, THREE,
1780 };
1781 void foo() {
1782 [[Hel^lo]] hello = ONE;
1783 }
1784 )cpp",
1785 [](HoverInfo &HI) {
1786 HI.Name = "Hello";
1787 HI.Kind = index::SymbolKind::Enum;
1788 HI.NamespaceScope = "";
1789 HI.Definition = "enum Hello {}";
1790 HI.Documentation = "Enum declaration";
1791 }},
1792 {
1793 R"cpp(// Enumerator
1794 enum Hello {
1795 ONE, TWO, THREE,
1796 };
1797 void foo() {
1798 Hello hello = [[O^NE]];
1799 }
1800 )cpp",
1801 [](HoverInfo &HI) {
1802 HI.Name = "ONE";
1803 HI.Kind = index::SymbolKind::EnumConstant;
1804 HI.NamespaceScope = "";
1805 HI.LocalScope = "Hello::";
1806 HI.Type = "enum Hello";
1807 HI.Definition = "ONE";
1808 HI.Value = "0";
1809 }},
1810 {
1811 R"cpp(// C++20's using enum
1812 enum class Hello {
1813 ONE, TWO, THREE,
1814 };
1815 void foo() {
1816 using enum Hello;
1817 Hello hello = [[O^NE]];
1818 }
1819 )cpp",
1820 [](HoverInfo &HI) {
1821 HI.Name = "ONE";
1822 HI.Kind = index::SymbolKind::EnumConstant;
1823 HI.NamespaceScope = "";
1824 HI.LocalScope = "Hello::";
1825 HI.Type = "enum Hello";
1826 HI.Definition = "ONE";
1827 HI.Value = "0";
1828 }},
1829 {
1830 R"cpp(// Enumerator in anonymous enum
1831 enum {
1832 ONE, TWO, THREE,
1833 };
1834 void foo() {
1835 int hello = [[O^NE]];
1836 }
1837 )cpp",
1838 [](HoverInfo &HI) {
1839 HI.Name = "ONE";
1840 HI.Kind = index::SymbolKind::EnumConstant;
1841 HI.NamespaceScope = "";
1842 // FIXME: This should be `(anon enum)::`
1843 HI.LocalScope = "";
1844 HI.Type = "enum (unnamed)";
1845 HI.Definition = "ONE";
1846 HI.Value = "0";
1847 }},
1848 {
1849 R"cpp(// Global variable
1850 static int hey = 10;
1851 void foo() {
1852 [[he^y]]++;
1853 }
1854 )cpp",
1855 [](HoverInfo &HI) {
1856 HI.Name = "hey";
1857 HI.Kind = index::SymbolKind::Variable;
1858 HI.NamespaceScope = "";
1859 HI.Type = "int";
1860 HI.Definition = "static int hey = 10";
1861 HI.Documentation = "Global variable";
1862 // FIXME: Value shouldn't be set in this case
1863 HI.Value = "10 (0xa)";
1864 }},
1865 {
1866 R"cpp(// Global variable in namespace
1867 namespace ns1 {
1868 static long long hey = -36637162602497;
1869 }
1870 void foo() {
1871 ns1::[[he^y]]++;
1872 }
1873 )cpp",
1874 [](HoverInfo &HI) {
1875 HI.Name = "hey";
1876 HI.Kind = index::SymbolKind::Variable;
1877 HI.NamespaceScope = "ns1::";
1878 HI.Type = "long long";
1879 HI.Definition = "static long long hey = -36637162602497";
1880 HI.Value = "-36637162602497 (0xffffdeadbeefffff)"; // needs 64 bits
1881 }},
1882 {
1883 R"cpp(// Field in anonymous struct
1884 static struct {
1885 int hello;
1886 } s;
1887 void foo() {
1888 s.[[he^llo]]++;
1889 }
1890 )cpp",
1891 [](HoverInfo &HI) {
1892 HI.Name = "hello";
1893 HI.Kind = index::SymbolKind::Field;
1894 HI.NamespaceScope = "";
1895 HI.LocalScope = "(anonymous struct)::";
1896 HI.Type = "int";
1897 HI.Definition = "int hello";
1898 }},
1899 {
1900 R"cpp(// Templated function
1901 template <typename T>
1902 T foo() {
1903 return 17;
1904 }
1905 void g() { auto x = [[f^oo]]<int>(); }
1906 )cpp",
1907 [](HoverInfo &HI) {
1908 HI.Name = "foo";
1909 HI.Kind = index::SymbolKind::Function;
1910 HI.NamespaceScope = "";
1911 HI.Type = "int ()";
1912 HI.Definition = "template <> int foo<int>()";
1913 HI.Documentation = "Templated function";
1914 HI.ReturnType = "int";
1915 HI.Parameters = std::vector<HoverInfo::Param>{};
1916 // FIXME: We should populate template parameters with arguments in
1917 // case of instantiations.
1918 }},
1919 {
1920 R"cpp(// Anonymous union
1921 struct outer {
1922 union {
1923 int abc, def;
1924 } v;
1925 };
1926 void g() { struct outer o; o.v.[[d^ef]]++; }
1927 )cpp",
1928 [](HoverInfo &HI) {
1929 HI.Name = "def";
1930 HI.Kind = index::SymbolKind::Field;
1931 HI.NamespaceScope = "";
1932 HI.LocalScope = "outer::(anonymous union)::";
1933 HI.Type = "int";
1934 HI.Definition = "int def";
1935 }},
1936 {
1937 R"cpp(// documentation from index
1938 int nextSymbolIsAForwardDeclFromIndexWithNoLocalDocs;
1939 void indexSymbol();
1940 void g() { [[ind^exSymbol]](); }
1941 )cpp",
1942 [](HoverInfo &HI) {
1943 HI.Name = "indexSymbol";
1944 HI.Kind = index::SymbolKind::Function;
1945 HI.NamespaceScope = "";
1946 HI.Type = "void ()";
1947 HI.Definition = "void indexSymbol()";
1948 HI.ReturnType = "void";
1949 HI.Parameters = std::vector<HoverInfo::Param>{};
1950 HI.Documentation = "comment from index";
1951 }},
1952 {
1953 R"cpp(// Simple initialization with auto
1954 void foo() {
1955 ^[[auto]] i = 1;
1956 }
1957 )cpp",
1958 [](HoverInfo &HI) {
1959 HI.Name = "auto";
1960 HI.Kind = index::SymbolKind::TypeAlias;
1961 HI.Definition = "int";
1962 }},
1963 {
1964 R"cpp(// Simple initialization with const auto
1965 void foo() {
1966 const ^[[auto]] i = 1;
1967 }
1968 )cpp",
1969 [](HoverInfo &HI) {
1970 HI.Name = "auto";
1971 HI.Kind = index::SymbolKind::TypeAlias;
1972 HI.Definition = "int";
1973 }},
1974 {
1975 R"cpp(// Simple initialization with const auto&
1976 void foo() {
1977 const ^[[auto]]& i = 1;
1978 }
1979 )cpp",
1980 [](HoverInfo &HI) {
1981 HI.Name = "auto";
1982 HI.Kind = index::SymbolKind::TypeAlias;
1983 HI.Definition = "int";
1984 }},
1985 {
1986 R"cpp(// Simple initialization with auto&
1987 void foo() {
1988 int x;
1989 ^[[auto]]& i = x;
1990 }
1991 )cpp",
1992 [](HoverInfo &HI) {
1993 HI.Name = "auto";
1994 HI.Kind = index::SymbolKind::TypeAlias;
1995 HI.Definition = "int";
1996 }},
1997 {
1998 R"cpp(// Simple initialization with auto*
1999 void foo() {
2000 int a = 1;
2001 ^[[auto]]* i = &a;
2002 }
2003 )cpp",
2004 [](HoverInfo &HI) {
2005 HI.Name = "auto";
2006 HI.Kind = index::SymbolKind::TypeAlias;
2007 HI.Definition = "int";
2008 }},
2009 {
2010 R"cpp(// Simple initialization with auto from pointer
2011 void foo() {
2012 int a = 1;
2013 ^[[auto]] i = &a;
2014 }
2015 )cpp",
2016 [](HoverInfo &HI) {
2017 HI.Name = "auto";
2018 HI.Kind = index::SymbolKind::TypeAlias;
2019 HI.Definition = "int *";
2020 }},
2021 {
2022 R"cpp(// Auto with initializer list.
2023 namespace std
2024 {
2025 template<class _E>
2026 class initializer_list {};
2027 }
2028 void foo() {
2029 ^[[auto]] i = {1,2};
2030 }
2031 )cpp",
2032 [](HoverInfo &HI) {
2033 HI.Name = "auto";
2034 HI.Kind = index::SymbolKind::TypeAlias;
2035 HI.Definition = "std::initializer_list<int>";
2036 }},
2037 {
2038 R"cpp(// User defined conversion to auto
2039 struct Bar {
2040 operator ^[[auto]]() const { return 10; }
2041 };
2042 )cpp",
2043 [](HoverInfo &HI) {
2044 HI.Name = "auto";
2045 HI.Kind = index::SymbolKind::TypeAlias;
2046 HI.Definition = "int";
2047 }},
2048 {
2049 R"cpp(// Simple initialization with decltype(auto)
2050 void foo() {
2051 ^[[decltype]](auto) i = 1;
2052 }
2053 )cpp",
2054 [](HoverInfo &HI) {
2055 HI.Name = "decltype";
2056 HI.Kind = index::SymbolKind::TypeAlias;
2057 HI.Definition = "int";
2058 }},
2059 {
2060 R"cpp(// Simple initialization with const decltype(auto)
2061 void foo() {
2062 const int j = 0;
2063 ^[[decltype]](auto) i = j;
2064 }
2065 )cpp",
2066 [](HoverInfo &HI) {
2067 HI.Name = "decltype";
2068 HI.Kind = index::SymbolKind::TypeAlias;
2069 HI.Definition = "const int";
2070 }},
2071 {
2072 R"cpp(// Simple initialization with const& decltype(auto)
2073 void foo() {
2074 int k = 0;
2075 const int& j = k;
2076 ^[[decltype]](auto) i = j;
2077 }
2078 )cpp",
2079 [](HoverInfo &HI) {
2080 HI.Name = "decltype";
2081 HI.Kind = index::SymbolKind::TypeAlias;
2082 HI.Definition = "const int &";
2083 }},
2084 {
2085 R"cpp(// Simple initialization with & decltype(auto)
2086 void foo() {
2087 int k = 0;
2088 int& j = k;
2089 ^[[decltype]](auto) i = j;
2090 }
2091 )cpp",
2092 [](HoverInfo &HI) {
2093 HI.Name = "decltype";
2094 HI.Kind = index::SymbolKind::TypeAlias;
2095 HI.Definition = "int &";
2096 }},
2097 {
2098 R"cpp(// simple trailing return type
2099 ^[[auto]] main() -> int {
2100 return 0;
2101 }
2102 )cpp",
2103 [](HoverInfo &HI) {
2104 HI.Name = "auto";
2105 HI.Kind = index::SymbolKind::TypeAlias;
2106 HI.Definition = "int";
2107 }},
2108 {
2109 R"cpp(// auto function return with trailing type
2110 struct Bar {};
2111 ^[[auto]] test() -> decltype(Bar()) {
2112 return Bar();
2113 }
2114 )cpp",
2115 [](HoverInfo &HI) {
2116 HI.Name = "auto";
2117 HI.Kind = index::SymbolKind::TypeAlias;
2118 HI.Definition = "Bar";
2119 HI.Documentation = "auto function return with trailing type";
2120 }},
2121 {
2122 R"cpp(// trailing return type
2123 struct Bar {};
2124 auto test() -> ^[[decltype]](Bar()) {
2125 return Bar();
2126 }
2127 )cpp",
2128 [](HoverInfo &HI) {
2129 HI.Name = "decltype";
2130 HI.Kind = index::SymbolKind::TypeAlias;
2131 HI.Definition = "Bar";
2132 HI.Documentation = "trailing return type";
2133 }},
2134 {
2135 R"cpp(// auto in function return
2136 struct Bar {};
2137 ^[[auto]] test() {
2138 return Bar();
2139 }
2140 )cpp",
2141 [](HoverInfo &HI) {
2142 HI.Name = "auto";
2143 HI.Kind = index::SymbolKind::TypeAlias;
2144 HI.Definition = "Bar";
2145 HI.Documentation = "auto in function return";
2146 }},
2147 {
2148 R"cpp(// auto& in function return
2149 struct Bar {};
2150 ^[[auto]]& test() {
2151 static Bar x;
2152 return x;
2153 }
2154 )cpp",
2155 [](HoverInfo &HI) {
2156 HI.Name = "auto";
2157 HI.Kind = index::SymbolKind::TypeAlias;
2158 HI.Definition = "Bar";
2159 HI.Documentation = "auto& in function return";
2160 }},
2161 {
2162 R"cpp(// auto* in function return
2163 struct Bar {};
2164 ^[[auto]]* test() {
2165 Bar* bar;
2166 return bar;
2167 }
2168 )cpp",
2169 [](HoverInfo &HI) {
2170 HI.Name = "auto";
2171 HI.Kind = index::SymbolKind::TypeAlias;
2172 HI.Definition = "Bar";
2173 HI.Documentation = "auto* in function return";
2174 }},
2175 {
2176 R"cpp(// const auto& in function return
2177 struct Bar {};
2178 const ^[[auto]]& test() {
2179 static Bar x;
2180 return x;
2181 }
2182 )cpp",
2183 [](HoverInfo &HI) {
2184 HI.Name = "auto";
2185 HI.Kind = index::SymbolKind::TypeAlias;
2186 HI.Definition = "Bar";
2187 HI.Documentation = "const auto& in function return";
2188 }},
2189 {
2190 R"cpp(// decltype(auto) in function return
2191 struct Bar {};
2192 ^[[decltype]](auto) test() {
2193 return Bar();
2194 }
2195 )cpp",
2196 [](HoverInfo &HI) {
2197 HI.Name = "decltype";
2198 HI.Kind = index::SymbolKind::TypeAlias;
2199 HI.Definition = "Bar";
2200 HI.Documentation = "decltype(auto) in function return";
2201 }},
2202 {
2203 R"cpp(// decltype(auto) reference in function return
2204 ^[[decltype]](auto) test() {
2205 static int a;
2206 return (a);
2207 }
2208 )cpp",
2209 [](HoverInfo &HI) {
2210 HI.Name = "decltype";
2211 HI.Kind = index::SymbolKind::TypeAlias;
2212 HI.Definition = "int &";
2213 }},
2214 {
2215 R"cpp(// decltype lvalue reference
2216 void foo() {
2217 int I = 0;
2218 ^[[decltype]](I) J = I;
2219 }
2220 )cpp",
2221 [](HoverInfo &HI) {
2222 HI.Name = "decltype";
2223 HI.Kind = index::SymbolKind::TypeAlias;
2224 HI.Definition = "int";
2225 }},
2226 {
2227 R"cpp(// decltype lvalue reference
2228 void foo() {
2229 int I= 0;
2230 int &K = I;
2231 ^[[decltype]](K) J = I;
2232 }
2233 )cpp",
2234 [](HoverInfo &HI) {
2235 HI.Name = "decltype";
2236 HI.Kind = index::SymbolKind::TypeAlias;
2237 HI.Definition = "int &";
2238 }},
2239 {
2240 R"cpp(// decltype lvalue reference parenthesis
2241 void foo() {
2242 int I = 0;
2243 ^[[decltype]]((I)) J = I;
2244 }
2245 )cpp",
2246 [](HoverInfo &HI) {
2247 HI.Name = "decltype";
2248 HI.Kind = index::SymbolKind::TypeAlias;
2249 HI.Definition = "int &";
2250 }},
2251 {
2252 R"cpp(// decltype rvalue reference
2253 void foo() {
2254 int I = 0;
2255 ^[[decltype]](static_cast<int&&>(I)) J = static_cast<int&&>(I);
2256 }
2257 )cpp",
2258 [](HoverInfo &HI) {
2259 HI.Name = "decltype";
2260 HI.Kind = index::SymbolKind::TypeAlias;
2261 HI.Definition = "int &&";
2262 }},
2263 {
2264 R"cpp(// decltype rvalue reference function call
2265 int && bar();
2266 void foo() {
2267 int I = 0;
2268 ^[[decltype]](bar()) J = bar();
2269 }
2270 )cpp",
2271 [](HoverInfo &HI) {
2272 HI.Name = "decltype";
2273 HI.Kind = index::SymbolKind::TypeAlias;
2274 HI.Definition = "int &&";
2275 }},
2276 {
2277 R"cpp(// decltype of function with trailing return type.
2278 struct Bar {};
2279 auto test() -> decltype(Bar()) {
2280 return Bar();
2281 }
2282 void foo() {
2283 ^[[decltype]](test()) i = test();
2284 }
2285 )cpp",
2286 [](HoverInfo &HI) {
2287 HI.Name = "decltype";
2288 HI.Kind = index::SymbolKind::TypeAlias;
2289 HI.Definition = "Bar";
2290 HI.Documentation =
2291 "decltype of function with trailing return type.";
2292 }},
2293 {
2294 R"cpp(// decltype of var with decltype.
2295 void foo() {
2296 int I = 0;
2297 decltype(I) J = I;
2298 ^[[decltype]](J) K = J;
2299 }
2300 )cpp",
2301 [](HoverInfo &HI) {
2302 HI.Name = "decltype";
2303 HI.Kind = index::SymbolKind::TypeAlias;
2304 HI.Definition = "int";
2305 }},
2306 {
2307 R"cpp(// decltype of dependent type
2308 template <typename T>
2309 struct X {
2310 using Y = ^[[decltype]](T::Z);
2311 };
2312 )cpp",
2313 [](HoverInfo &HI) {
2314 HI.Name = "decltype";
2315 HI.Kind = index::SymbolKind::TypeAlias;
2316 HI.Definition = "<dependent type>";
2317 }},
2318 {
2319 R"cpp(// More complicated structured types.
2320 int bar();
2321 ^[[auto]] (*foo)() = bar;
2322 )cpp",
2323 [](HoverInfo &HI) {
2324 HI.Name = "auto";
2325 HI.Kind = index::SymbolKind::TypeAlias;
2326 HI.Definition = "int";
2327 }},
2328 {
2329 R"cpp(// Should not crash when evaluating the initializer.
2330 struct Test {};
2331 void test() { Test && [[te^st]] = {}; }
2332 )cpp",
2333 [](HoverInfo &HI) {
2334 HI.Name = "test";
2335 HI.Kind = index::SymbolKind::Variable;
2336 HI.NamespaceScope = "";
2337 HI.LocalScope = "test::";
2338 HI.Type = "Test &&";
2339 HI.Definition = "Test &&test = {}";
2340 }},
2341 {
2342 R"cpp(// auto on alias
2343 typedef int int_type;
2344 ^[[auto]] x = int_type();
2345 )cpp",
2346 [](HoverInfo &HI) {
2347 HI.Name = "auto";
2348 HI.Kind = index::SymbolKind::TypeAlias;
2349 HI.Definition = "int_type // aka: int";
2350 }},
2351 {
2352 R"cpp(// auto on alias
2353 struct cls {};
2354 typedef cls cls_type;
2355 ^[[auto]] y = cls_type();
2356 )cpp",
2357 [](HoverInfo &HI) {
2358 HI.Name = "auto";
2359 HI.Kind = index::SymbolKind::TypeAlias;
2360 HI.Definition = "cls_type // aka: cls";
2361 HI.Documentation = "auto on alias";
2362 }},
2363 {
2364 R"cpp(// auto on alias
2365 template <class>
2366 struct templ {};
2367 ^[[auto]] z = templ<int>();
2368 )cpp",
2369 [](HoverInfo &HI) {
2370 HI.Name = "auto";
2371 HI.Kind = index::SymbolKind::TypeAlias;
2372 HI.Definition = "templ<int>";
2373 HI.Documentation = "auto on alias";
2374 }},
2375 {
2376 R"cpp(// Undeduced auto declaration
2377 template<typename T>
2378 void foo() {
2379 ^[[auto]] x = T();
2380 }
2381 )cpp",
2382 [](HoverInfo &HI) {
2383 HI.Name = "auto";
2384 HI.Kind = index::SymbolKind::TypeAlias;
2385 HI.Definition = "/* not deduced */";
2386 }},
2387 {
2388 R"cpp(// Undeduced auto return type
2389 template<typename T>
2390 ^[[auto]] foo() {
2391 return T();
2392 }
2393 )cpp",
2394 [](HoverInfo &HI) {
2395 HI.Name = "auto";
2396 HI.Kind = index::SymbolKind::TypeAlias;
2397 HI.Definition = "/* not deduced */";
2398 }},
2399 {
2400 R"cpp(// Template auto parameter
2401 template<[[a^uto]] T>
2402 void func() {
2403 }
2404 )cpp",
2405 [](HoverInfo &HI) {
2406 // FIXME: not sure this is what we want, but this
2407 // is what we currently get with getDeducedType
2408 HI.Name = "auto";
2409 HI.Kind = index::SymbolKind::TypeAlias;
2410 HI.Definition = "/* not deduced */";
2411 }},
2412 {
2413 R"cpp(// Undeduced decltype(auto) return type
2414 template<typename T>
2415 ^[[decltype]](auto) foo() {
2416 return T();
2417 }
2418 )cpp",
2419 [](HoverInfo &HI) {
2420 HI.Name = "decltype";
2421 HI.Kind = index::SymbolKind::TypeAlias;
2422 HI.Definition = "/* not deduced */";
2423 }},
2424 {
2425 R"cpp(// should not crash.
2426 template <class T> struct cls {
2427 int method();
2428 };
2429
2430 auto test = cls<int>().[[m^ethod]]();
2431 )cpp",
2432 [](HoverInfo &HI) {
2433 HI.Definition = "int method()";
2434 HI.Kind = index::SymbolKind::InstanceMethod;
2435 HI.NamespaceScope = "";
2436 HI.LocalScope = "cls<int>::";
2437 HI.Name = "method";
2438 HI.Parameters.emplace();
2439 HI.ReturnType = "int";
2440 HI.Type = "int ()";
2441 }},
2442 {
2443 R"cpp(// type of nested templates.
2444 template <class T> struct cls {};
2445 cls<cls<cls<int>>> [[fo^o]];
2446 )cpp",
2447 [](HoverInfo &HI) {
2448 HI.Definition = "cls<cls<cls<int>>> foo";
2449 HI.Kind = index::SymbolKind::Variable;
2450 HI.NamespaceScope = "";
2451 HI.Name = "foo";
2452 HI.Type = "cls<cls<cls<int>>>";
2453 }},
2454 {
2455 R"cpp(// type of nested templates.
2456 template <class T> struct cls {};
2457 [[cl^s]]<cls<cls<int>>> foo;
2458 )cpp",
2459 [](HoverInfo &HI) {
2460 HI.Definition = "template <> struct cls<cls<cls<int>>> {}";
2461 HI.Kind = index::SymbolKind::Struct;
2462 HI.NamespaceScope = "";
2463 HI.Name = "cls<cls<cls<int>>>";
2464 HI.Documentation = "type of nested templates.";
2465 }},
2466 {
2467 R"cpp(// type with decltype
2468 int a;
2469 decltype(a) [[b^]] = a;)cpp",
2470 [](HoverInfo &HI) {
2471 HI.Definition = "decltype(a) b = a";
2472 HI.Kind = index::SymbolKind::Variable;
2473 HI.NamespaceScope = "";
2474 HI.Name = "b";
2475 HI.Type = "int";
2476 }},
2477 {
2478 R"cpp(// type with decltype
2479 int a;
2480 decltype(a) c;
2481 decltype(c) [[b^]] = a;)cpp",
2482 [](HoverInfo &HI) {
2483 HI.Definition = "decltype(c) b = a";
2484 HI.Kind = index::SymbolKind::Variable;
2485 HI.NamespaceScope = "";
2486 HI.Name = "b";
2487 HI.Type = "int";
2488 }},
2489 {
2490 R"cpp(// type with decltype
2491 int a;
2492 const decltype(a) [[b^]] = a;)cpp",
2493 [](HoverInfo &HI) {
2494 HI.Definition = "const decltype(a) b = a";
2495 HI.Kind = index::SymbolKind::Variable;
2496 HI.NamespaceScope = "";
2497 HI.Name = "b";
2498 HI.Type = "int";
2499 }},
2500 {
2501 R"cpp(// type with decltype
2502 int a;
2503 auto [[f^oo]](decltype(a) x) -> decltype(a) { return 0; })cpp",
2504 [](HoverInfo &HI) {
2505 HI.Definition = "auto foo(decltype(a) x) -> decltype(a)";
2506 HI.Kind = index::SymbolKind::Function;
2507 HI.NamespaceScope = "";
2508 HI.Name = "foo";
2509 // FIXME: Handle composite types with decltype with a printing
2510 // policy.
2511 HI.Type = {"auto (decltype(a)) -> decltype(a)",
2512 "auto (int) -> int"};
2513 HI.ReturnType = "int";
2514 HI.Parameters = {{{"int"}, std::string("x"), std::nullopt}};
2515 }},
2516 {
2517 R"cpp(// sizeof expr
2518 void foo() {
2519 (void)[[size^of]](char);
2520 })cpp",
2521 [](HoverInfo &HI) {
2522 HI.Name = "expression";
2523 HI.Type = "unsigned long";
2524 HI.Value = "1";
2525 }},
2526 {
2527 R"cpp(// alignof expr
2528 void foo() {
2529 (void)[[align^of]](char);
2530 })cpp",
2531 [](HoverInfo &HI) {
2532 HI.Name = "expression";
2533 HI.Type = "unsigned long";
2534 HI.Value = "1";
2535 }},
2536 {
2537 R"cpp(
2538 template <typename T = int>
2539 void foo(const T& = T()) {
2540 [[f^oo]]<>(3);
2541 })cpp",
2542 [](HoverInfo &HI) {
2543 HI.Name = "foo";
2544 HI.Kind = index::SymbolKind::Function;
2545 HI.Type = "void (const int &)";
2546 HI.ReturnType = "void";
2547 HI.Parameters = {
2548 {{"const int &"}, std::nullopt, std::string("T()")}};
2549 HI.Definition = "template <> void foo<int>(const int &)";
2550 HI.NamespaceScope = "";
2551 }},
2552 {
2553 R"cpp(// should not crash
2554 @interface ObjC {
2555 char [[da^ta]];
2556 }@end
2557 )cpp",
2558 [](HoverInfo &HI) {
2559 HI.Name = "data";
2560 HI.Type = "char";
2561 HI.Kind = index::SymbolKind::Field;
2562 HI.LocalScope = "ObjC::";
2563 HI.NamespaceScope = "";
2564 HI.Definition = "char data";
2565 }},
2566 {
2567 R"cpp(
2568 @interface MYObject
2569 @end
2570 @interface Interface
2571 @property(retain) [[MYOb^ject]] *x;
2572 @end
2573 )cpp",
2574 [](HoverInfo &HI) {
2575 HI.Name = "MYObject";
2576 HI.Kind = index::SymbolKind::Class;
2577 HI.NamespaceScope = "";
2578 HI.Definition = "@interface MYObject\n@end";
2579 }},
2580 {
2581 R"cpp(
2582 @interface MYObject
2583 @end
2584 @interface Interface
2585 - (void)doWith:([[MYOb^ject]] *)object;
2586 @end
2587 )cpp",
2588 [](HoverInfo &HI) {
2589 HI.Name = "MYObject";
2590 HI.Kind = index::SymbolKind::Class;
2591 HI.NamespaceScope = "";
2592 HI.Definition = "@interface MYObject\n@end";
2593 }},
2594 {
2595 R"cpp(// this expr
2596 // comment
2597 namespace ns {
2598 class Foo {
2599 Foo* bar() {
2600 return [[t^his]];
2601 }
2602 };
2603 }
2604 )cpp",
2605 [](HoverInfo &HI) {
2606 HI.Name = "this";
2607 HI.Definition = "ns::Foo *";
2608 }},
2609 {
2610 R"cpp(// this expr for template class
2611 namespace ns {
2612 template <typename T>
2613 class Foo {
2614 Foo* bar() const {
2615 return [[t^his]];
2616 }
2617 };
2618 }
2619 )cpp",
2620 [](HoverInfo &HI) {
2621 HI.Name = "this";
2622 HI.Definition = "const Foo<T> *";
2623 }},
2624 {
2625 R"cpp(// this expr for specialization class
2626 namespace ns {
2627 template <typename T> class Foo {};
2628 template <>
2629 struct Foo<int> {
2630 Foo* bar() {
2631 return [[thi^s]];
2632 }
2633 };
2634 }
2635 )cpp",
2636 [](HoverInfo &HI) {
2637 HI.Name = "this";
2638 HI.Definition = "Foo<int> *";
2639 }},
2640 {
2641 R"cpp(// this expr for partial specialization struct
2642 namespace ns {
2643 template <typename T, typename F> struct Foo {};
2644 template <typename F>
2645 struct Foo<int, F> {
2646 Foo* bar() const {
2647 return [[thi^s]];
2648 }
2649 };
2650 }
2651 )cpp",
2652 [](HoverInfo &HI) {
2653 HI.Name = "this";
2654 HI.Definition = "const Foo<int, F> *";
2655 }},
2656 {
2657 R"cpp(
2658 @interface MYObject
2659 @end
2660 @interface MYObject (Private)
2661 @property(nonatomic, assign) int privateField;
2662 @end
2663
2664 int someFunction() {
2665 MYObject *obj = [MYObject sharedInstance];
2666 return obj.[[private^Field]];
2667 }
2668 )cpp",
2669 [](HoverInfo &HI) {
2670 HI.Name = "privateField";
2671 HI.Kind = index::SymbolKind::InstanceProperty;
2672 HI.LocalScope = "MYObject(Private)::";
2673 HI.NamespaceScope = "";
2674 HI.Definition = "@property(nonatomic, assign, unsafe_unretained, "
2675 "readwrite) int privateField;";
2676 }},
2677 {
2678 R"cpp(
2679 @protocol MYProtocol
2680 @property(nonatomic, assign) int prop1;
2681 @end
2682
2683 int someFunction() {
2684 id<MYProtocol> obj = 0;
2685 return obj.[[pro^p1]];
2686 }
2687 )cpp",
2688 [](HoverInfo &HI) {
2689 HI.Name = "prop1";
2690 HI.Kind = index::SymbolKind::InstanceProperty;
2691 HI.LocalScope = "MYProtocol::";
2692 HI.NamespaceScope = "";
2693 HI.Definition = "@property(nonatomic, assign, unsafe_unretained, "
2694 "readwrite) int prop1;";
2695 }},
2696 {
2697 R"cpp(
2698 @protocol MYProtocol
2699 @end
2700 @interface MYObject
2701 @end
2702
2703 @interface MYObject (Ext) <[[MYProt^ocol]]>
2704 @end
2705 )cpp",
2706 [](HoverInfo &HI) {
2707 HI.Name = "MYProtocol";
2708 HI.Kind = index::SymbolKind::Protocol;
2709 HI.NamespaceScope = "";
2710 HI.Definition = "@protocol MYProtocol\n@end";
2711 }},
2712 {R"objc(
2713 @interface Foo
2714 @end
2715
2716 @implementation Foo(Private)
2717 + (int)somePrivateMethod {
2718 int [[res^ult]] = 2;
2719 return result;
2720 }
2721 @end
2722 )objc",
2723 [](HoverInfo &HI) {
2724 HI.Name = "result";
2725 HI.Definition = "int result = 2";
2726 HI.Kind = index::SymbolKind::Variable;
2727 HI.Type = "int";
2728 HI.LocalScope = "+[Foo(Private) somePrivateMethod]::";
2729 HI.NamespaceScope = "";
2730 HI.Value = "2";
2731 }},
2732 {R"objc(
2733 @interface Foo
2734 @end
2735
2736 @implementation Foo
2737 - (int)variadicArgMethod:(id)first, ... {
2738 int [[res^ult]] = 0;
2739 return result;
2740 }
2741 @end
2742 )objc",
2743 [](HoverInfo &HI) {
2744 HI.Name = "result";
2745 HI.Definition = "int result = 0";
2746 HI.Kind = index::SymbolKind::Variable;
2747 HI.Type = "int";
2748 HI.LocalScope = "-[Foo variadicArgMethod:, ...]::";
2749 HI.NamespaceScope = "";
2750 HI.Value = "0";
2751 }},
2752 {R"cpp(
2753 void foo(int * __attribute__(([[non^null]], noescape)) );
2754 )cpp",
2755 [](HoverInfo &HI) {
2756 HI.Name = "nonnull";
2757 HI.Kind = index::SymbolKind::Unknown; // FIXME: no suitable value
2758 HI.Definition = "__attribute__((nonnull))";
2759 HI.Documentation = Attr::getDocumentation(attr::NonNull).str();
2760 }},
2761 };
2762
2763 // Create a tiny index, so tests above can verify documentation is fetched.
2764 Symbol IndexSym = func("indexSymbol");
2765 IndexSym.Documentation = "comment from index";
2766 SymbolSlab::Builder Symbols;
2767 Symbols.insert(IndexSym);
2768 auto Index =
2769 MemIndex::build(std::move(Symbols).build(), RefSlab(), RelationSlab());
2770
2771 for (const auto &Case : Cases) {
2772 SCOPED_TRACE(Case.Code);
2773
2774 Annotations T(Case.Code);
2775 TestTU TU = TestTU::withCode(T.code());
2776 TU.ExtraArgs.push_back("-std=c++17");
2777 TU.ExtraArgs.push_back("-xobjective-c++");
2778
2779 TU.ExtraArgs.push_back("-Wno-gnu-designator");
2780 // Types might be different depending on the target triplet, we chose a
2781 // fixed one to make sure tests passes on different platform.
2782 TU.ExtraArgs.push_back("--target=x86_64-pc-linux-gnu");
2783 auto AST = TU.build();
2784 Config Cfg;
2785 Cfg.Hover.ShowAKA = true;
2786 WithContextValue WithCfg(Config::Key, std::move(Cfg));
2787 auto H = getHover(AST, T.point(), format::getLLVMStyle(), Index.get());
2788 ASSERT_TRUE(H);
2789 HoverInfo Expected;
2790 Expected.SymRange = T.range();
2791 Case.ExpectedBuilder(Expected);
2792
2793 SCOPED_TRACE(H->present().asPlainText());
2794 EXPECT_EQ(H->NamespaceScope, Expected.NamespaceScope);
2795 EXPECT_EQ(H->LocalScope, Expected.LocalScope);
2796 EXPECT_EQ(H->Name, Expected.Name);
2797 EXPECT_EQ(H->Kind, Expected.Kind);
2798 EXPECT_EQ(H->Documentation, Expected.Documentation);
2799 EXPECT_EQ(H->Definition, Expected.Definition);
2800 EXPECT_EQ(H->Type, Expected.Type);
2801 EXPECT_EQ(H->ReturnType, Expected.ReturnType);
2802 EXPECT_EQ(H->Parameters, Expected.Parameters);
2803 EXPECT_EQ(H->TemplateParameters, Expected.TemplateParameters);
2804 EXPECT_EQ(H->SymRange, Expected.SymRange);
2805 EXPECT_EQ(H->Value, Expected.Value);
2806 }
2807 }
2808
2809 TEST(Hover, DocsFromIndex) {
2810 Annotations T(R"cpp(
2811 template <typename T> class X {};
2812 void foo() {
2813 auto t = X<int>();
2814 X^<int> w;
2815 (void)w;
2816 })cpp");
2817
2818 TestTU TU = TestTU::withCode(T.code());
2819 auto AST = TU.build();
2820 Symbol IndexSym;
2821 IndexSym.ID = getSymbolID(&findDecl(AST, "X"));
2822 IndexSym.Documentation = "comment from index";
2823 SymbolSlab::Builder Symbols;
2824 Symbols.insert(IndexSym);
2825 auto Index =
2826 MemIndex::build(std::move(Symbols).build(), RefSlab(), RelationSlab());
2827
2828 for (const auto &P : T.points()) {
2829 auto H = getHover(AST, P, format::getLLVMStyle(), Index.get());
2830 ASSERT_TRUE(H);
2831 EXPECT_EQ(H->Documentation, IndexSym.Documentation);
2832 }
2833 }
2834
2835 TEST(Hover, DocsFromAST) {
2836 Annotations T(R"cpp(
2837 // doc
2838 template <typename T> class X {};
2839 // doc
2840 template <typename T> void bar() {}
2841 // doc
2842 template <typename T> T baz;
2843 void foo() {
2844 au^to t = X<int>();
2845 X^<int>();
2846 b^ar<int>();
2847 au^to T = ba^z<X<int>>;
2848 ba^z<int> = 0;
2849 })cpp");
2850
2851 TestTU TU = TestTU::withCode(T.code());
2852 auto AST = TU.build();
2853 for (const auto &P : T.points()) {
2854 auto H = getHover(AST, P, format::getLLVMStyle(), nullptr);
2855 ASSERT_TRUE(H);
2856 EXPECT_EQ(H->Documentation, "doc");
2857 }
2858 }
2859
2860 TEST(Hover, NoCrash) {
2861 Annotations T(R"cpp(
2862 /* error-ok */
2863 template<typename T> T foo(T);
2864
2865 // Setter variable heuristic might fail if the callexpr is broken.
2866 struct X { int Y; void [[^setY]](float) { Y = foo(undefined); } };)cpp");
2867
2868 TestTU TU = TestTU::withCode(T.code());
2869 auto AST = TU.build();
2870 for (const auto &P : T.points())
2871 getHover(AST, P, format::getLLVMStyle(), nullptr);
2872 }
2873
2874 TEST(Hover, DocsFromMostSpecial) {
2875 Annotations T(R"cpp(
2876 // doc1
2877 template <typename T> class $doc1^X {};
2878 // doc2
2879 template <> class $doc2^X<int> {};
2880 // doc3
2881 template <typename T> class $doc3^X<T*> {};
2882 void foo() {
2883 X$doc1^<char>();
2884 X$doc2^<int>();
2885 X$doc3^<int*>();
2886 })cpp");
2887
2888 TestTU TU = TestTU::withCode(T.code());
2889 auto AST = TU.build();
2890 for (const auto *Comment : {"doc1", "doc2", "doc3"}) {
2891 for (const auto &P : T.points(Comment)) {
2892 auto H = getHover(AST, P, format::getLLVMStyle(), nullptr);
2893 ASSERT_TRUE(H);
2894 EXPECT_EQ(H->Documentation, Comment);
2895 }
2896 }
2897 }
2898
2899 TEST(Hover, Present) {
2900 struct {
2901 const std::function<void(HoverInfo &)> Builder;
2902 llvm::StringRef ExpectedRender;
2903 } Cases[] = {
2904 {
2905 [](HoverInfo &HI) {
2906 HI.Kind = index::SymbolKind::Unknown;
2907 HI.Name = "X";
2908 },
2909 R"(X)",
2910 },
2911 {
2912 [](HoverInfo &HI) {
2913 HI.Kind = index::SymbolKind::NamespaceAlias;
2914 HI.Name = "foo";
2915 },
2916 R"(namespace-alias foo)",
2917 },
2918 {
2919 [](HoverInfo &HI) {
2920 HI.Kind = index::SymbolKind::Class;
2921 HI.Size = 10;
2922 HI.TemplateParameters = {
2923 {{"typename"}, std::string("T"), std::nullopt},
2924 {{"typename"}, std::string("C"), std::string("bool")},
2925 };
2926 HI.Documentation = "documentation";
2927 HI.Definition =
2928 "template <typename T, typename C = bool> class Foo {}";
2929 HI.Name = "foo";
2930 HI.NamespaceScope.emplace();
2931 },
2932 R"(class foo
2933
2934 Size: 10 bytes
2935 documentation
2936
2937 template <typename T, typename C = bool> class Foo {})",
2938 },
2939 {
2940 [](HoverInfo &HI) {
2941 HI.Kind = index::SymbolKind::Function;
2942 HI.Name = "foo";
2943 HI.Type = {"type", "c_type"};
2944 HI.ReturnType = {"ret_type", "can_ret_type"};
2945 HI.Parameters.emplace();
2946 HoverInfo::Param P;
2947 HI.Parameters->push_back(P);
2948 P.Type = {"type", "can_type"};
2949 HI.Parameters->push_back(P);
2950 P.Name = "foo";
2951 HI.Parameters->push_back(P);
2952 P.Default = "default";
2953 HI.Parameters->push_back(P);
2954 HI.NamespaceScope = "ns::";
2955 HI.Definition = "ret_type foo(params) {}";
2956 },
2957 "function foo\n"
2958 "\n"
2959 "→ ret_type (aka can_ret_type)\n"
2960 "Parameters:\n"
2961 "- \n"
2962 "- type (aka can_type)\n"
2963 "- type foo (aka can_type)\n"
2964 "- type foo = default (aka can_type)\n"
2965 "\n"
2966 "// In namespace ns\n"
2967 "ret_type foo(params) {}",
2968 },
2969 {
2970 [](HoverInfo &HI) {
2971 HI.Kind = index::SymbolKind::Field;
2972 HI.LocalScope = "test::Bar::";
2973 HI.Value = "value";
2974 HI.Name = "foo";
2975 HI.Type = {"type", "can_type"};
2976 HI.Definition = "def";
2977 HI.Size = 4;
2978 HI.Offset = 12;
2979 HI.Padding = 4;
2980 },
2981 R"(field foo
2982
2983 Type: type (aka can_type)
2984 Value = value
2985 Offset: 12 bytes
2986 Size: 4 bytes (+4 padding)
2987
2988 // In test::Bar
2989 def)",
2990 },
2991 {
2992 [](HoverInfo &HI) {
2993 HI.Kind = index::SymbolKind::Field;
2994 HI.AccessSpecifier = "public";
2995 HI.Name = "foo";
2996 HI.LocalScope = "test::Bar::";
2997 HI.Definition = "def";
2998 },
2999 R"(field foo
3000
3001 // In test::Bar
3002 public: def)",
3003 },
3004 {
3005 [](HoverInfo &HI) {
3006 HI.Definition = "size_t method()";
3007 HI.AccessSpecifier = "protected";
3008 HI.Kind = index::SymbolKind::InstanceMethod;
3009 HI.NamespaceScope = "";
3010 HI.LocalScope = "cls<int>::";
3011 HI.Name = "method";
3012 HI.Parameters.emplace();
3013 HI.ReturnType = {"size_t", "unsigned long"};
3014 HI.Type = {"size_t ()", "unsigned long ()"};
3015 },
3016 R"(instance-method method
3017
3018 → size_t (aka unsigned long)
3019
3020 // In cls<int>
3021 protected: size_t method())",
3022 },
3023 {
3024 [](HoverInfo &HI) {
3025 HI.Definition = "cls(int a, int b = 5)";
3026 HI.AccessSpecifier = "public";
3027 HI.Kind = index::SymbolKind::Constructor;
3028 HI.NamespaceScope = "";
3029 HI.LocalScope = "cls";
3030 HI.Name = "cls";
3031 HI.Parameters.emplace();
3032 HI.Parameters->emplace_back();
3033 HI.Parameters->back().Type = "int";
3034 HI.Parameters->back().Name = "a";
3035 HI.Parameters->emplace_back();
3036 HI.Parameters->back().Type = "int";
3037 HI.Parameters->back().Name = "b";
3038 HI.Parameters->back().Default = "5";
3039 },
3040 R"(constructor cls
3041
3042 Parameters:
3043 - int a
3044 - int b = 5
3045
3046 // In cls
3047 public: cls(int a, int b = 5))",
3048 },
3049 {
3050 [](HoverInfo &HI) {
3051 HI.Kind = index::SymbolKind::Union;
3052 HI.AccessSpecifier = "private";
3053 HI.Name = "foo";
3054 HI.NamespaceScope = "ns1::";
3055 HI.Definition = "union foo {}";
3056 },
3057 R"(union foo
3058
3059 // In namespace ns1
3060 private: union foo {})",
3061 },
3062 {
3063 [](HoverInfo &HI) {
3064 HI.Kind = index::SymbolKind::Variable;
3065 HI.Name = "foo";
3066 HI.Definition = "int foo = 3";
3067 HI.LocalScope = "test::Bar::";
3068 HI.Value = "3";
3069 HI.Type = "int";
3070 HI.CalleeArgInfo.emplace();
3071 HI.CalleeArgInfo->Name = "arg_a";
3072 HI.CalleeArgInfo->Type = "int";
3073 HI.CalleeArgInfo->Default = "7";
3074 HI.CallPassType = HoverInfo::PassType{PassMode::Value, false};
3075 },
3076 R"(variable foo
3077
3078 Type: int
3079 Value = 3
3080 Passed as arg_a
3081
3082 // In test::Bar
3083 int foo = 3)",
3084 },
3085 {
3086 [](HoverInfo &HI) {
3087 HI.Kind = index::SymbolKind::Variable;
3088 HI.Name = "foo";
3089 HI.CalleeArgInfo.emplace();
3090 HI.CalleeArgInfo->Type = "int";
3091 HI.CallPassType = HoverInfo::PassType{PassMode::Value, false};
3092 },
3093 R"(variable foo
3094
3095 Passed by value)",
3096 },
3097 {
3098 [](HoverInfo &HI) {
3099 HI.Kind = index::SymbolKind::Variable;
3100 HI.Name = "foo";
3101 HI.Definition = "int foo = 3";
3102 HI.LocalScope = "test::Bar::";
3103 HI.Value = "3";
3104 HI.Type = "int";
3105 HI.CalleeArgInfo.emplace();
3106 HI.CalleeArgInfo->Name = "arg_a";
3107 HI.CalleeArgInfo->Type = "int";
3108 HI.CalleeArgInfo->Default = "7";
3109 HI.CallPassType = HoverInfo::PassType{PassMode::Ref, false};
3110 },
3111 R"(variable foo
3112
3113 Type: int
3114 Value = 3
3115 Passed by reference as arg_a
3116
3117 // In test::Bar
3118 int foo = 3)",
3119 },
3120 {
3121 [](HoverInfo &HI) {
3122 HI.Kind = index::SymbolKind::Variable;
3123 HI.Name = "foo";
3124 HI.Definition = "int foo = 3";
3125 HI.LocalScope = "test::Bar::";
3126 HI.Value = "3";
3127 HI.Type = "int";
3128 HI.CalleeArgInfo.emplace();
3129 HI.CalleeArgInfo->Name = "arg_a";
3130 HI.CalleeArgInfo->Type = {"alias_int", "int"};
3131 HI.CalleeArgInfo->Default = "7";
3132 HI.CallPassType = HoverInfo::PassType{PassMode::Value, true};
3133 },
3134 R"(variable foo
3135
3136 Type: int
3137 Value = 3
3138 Passed as arg_a (converted to alias_int)
3139
3140 // In test::Bar
3141 int foo = 3)",
3142 },
3143 {
3144 [](HoverInfo &HI) {
3145 HI.Kind = index::SymbolKind::Macro;
3146 HI.Name = "PLUS_ONE";
3147 HI.Definition = "#define PLUS_ONE(X) (X+1)\n\n"
3148 "// Expands to\n"
3149 "(1 + 1)";
3150 },
3151 R"(macro PLUS_ONE
3152
3153 #define PLUS_ONE(X) (X+1)
3154
3155 // Expands to
3156 (1 + 1))",
3157 },
3158 {
3159 [](HoverInfo &HI) {
3160 HI.Kind = index::SymbolKind::Variable;
3161 HI.Name = "foo";
3162 HI.Definition = "int foo = 3";
3163 HI.LocalScope = "test::Bar::";
3164 HI.Value = "3";
3165 HI.Type = "int";
3166 HI.CalleeArgInfo.emplace();
3167 HI.CalleeArgInfo->Name = "arg_a";
3168 HI.CalleeArgInfo->Type = "int";
3169 HI.CalleeArgInfo->Default = "7";
3170 HI.CallPassType = HoverInfo::PassType{PassMode::ConstRef, true};
3171 },
3172 R"(variable foo
3173
3174 Type: int
3175 Value = 3
3176 Passed by const reference as arg_a (converted to int)
3177
3178 // In test::Bar
3179 int foo = 3)",
3180 },
3181 {
3182 [](HoverInfo &HI) {
3183 HI.Name = "stdio.h";
3184 HI.Definition = "/usr/include/stdio.h";
3185 },
3186 R"(stdio.h
3187
3188 /usr/include/stdio.h)",
3189 }};
3190
3191 for (const auto &C : Cases) {
3192 HoverInfo HI;
3193 C.Builder(HI);
3194 Config Cfg;
3195 Cfg.Hover.ShowAKA = true;
3196 WithContextValue WithCfg(Config::Key, std::move(Cfg));
3197 EXPECT_EQ(HI.present().asPlainText(), C.ExpectedRender);
3198 }
3199 }
3200
3201 TEST(Hover, ParseDocumentation) {
3202 struct Case {
3203 llvm::StringRef Documentation;
3204 llvm::StringRef ExpectedRenderMarkdown;
3205 llvm::StringRef ExpectedRenderPlainText;
3206 } Cases[] = {{
3207 " \n foo\nbar",
3208 "foo bar",
3209 "foo bar",
3210 },
3211 {
3212 "foo\nbar \n ",
3213 "foo bar",
3214 "foo bar",
3215 },
3216 {
3217 "foo \nbar",
3218 "foo bar",
3219 "foo bar",
3220 },
3221 {
3222 "foo \nbar",
3223 "foo bar",
3224 "foo bar",
3225 },
3226 {
3227 "foo\n\n\nbar",
3228 "foo \nbar",
3229 "foo\nbar",
3230 },
3231 {
3232 "foo\n\n\n\tbar",
3233 "foo \nbar",
3234 "foo\nbar",
3235 },
3236 {
3237 "foo\n\n\n bar",
3238 "foo \nbar",
3239 "foo\nbar",
3240 },
3241 {
3242 "foo.\nbar",
3243 "foo. \nbar",
3244 "foo.\nbar",
3245 },
3246 {
3247 "foo. \nbar",
3248 "foo. \nbar",
3249 "foo.\nbar",
3250 },
3251 {
3252 "foo\n*bar",
3253 "foo \n\\*bar",
3254 "foo\n*bar",
3255 },
3256 {
3257 "foo\nbar",
3258 "foo bar",
3259 "foo bar",
3260 },
3261 {
3262 "Tests primality of `p`.",
3263 "Tests primality of `p`.",
3264 "Tests primality of `p`.",
3265 },
3266 {
3267 "'`' should not occur in `Code`",
3268 "'\\`' should not occur in `Code`",
3269 "'`' should not occur in `Code`",
3270 },
3271 {
3272 "`not\nparsed`",
3273 "\\`not parsed\\`",
3274 "`not parsed`",
3275 }};
3276
3277 for (const auto &C : Cases) {
3278 markup::Document Output;
3279 parseDocumentation(C.Documentation, Output);
3280
3281 EXPECT_EQ(Output.asMarkdown(), C.ExpectedRenderMarkdown);
3282 EXPECT_EQ(Output.asPlainText(), C.ExpectedRenderPlainText);
3283 }
3284 }
3285
3286 // This is a separate test as headings don't create any differences in plaintext
3287 // mode.
3288 TEST(Hover, PresentHeadings) {
3289 HoverInfo HI;
3290 HI.Kind = index::SymbolKind::Variable;
3291 HI.Name = "foo";
3292
3293 EXPECT_EQ(HI.present().asMarkdown(), "### variable `foo`");
3294 }
3295
3296 // This is a separate test as rulers behave differently in markdown vs
3297 // plaintext.
3298 TEST(Hover, PresentRulers) {
3299 HoverInfo HI;
3300 HI.Kind = index::SymbolKind::Variable;
3301 HI.Name = "foo";
3302 HI.Value = "val";
3303 HI.Definition = "def";
3304
3305 llvm::StringRef ExpectedMarkdown = //
3306 "### variable `foo` \n"
3307 "\n"
3308 "---\n"
3309 "Value = `val` \n"
3310 "\n"
3311 "---\n"
3312 "```cpp\n"
3313 "def\n"
3314 "```";
3315 EXPECT_EQ(HI.present().asMarkdown(), ExpectedMarkdown);
3316
3317 llvm::StringRef ExpectedPlaintext = R"pt(variable foo
3318
3319 Value = val
3320
3321 def)pt";
3322 EXPECT_EQ(HI.present().asPlainText(), ExpectedPlaintext);
3323 }
3324
3325 TEST(Hover, SpaceshipTemplateNoCrash) {
3326 Annotations T(R"cpp(
3327 namespace std {
3328 struct strong_ordering {
3329 int n;
3330 constexpr operator int() const { return n; }
3331 static const strong_ordering equal, greater, less;
3332 };
3333 constexpr strong_ordering strong_ordering::equal = {0};
3334 constexpr strong_ordering strong_ordering::greater = {1};
3335 constexpr strong_ordering strong_ordering::less = {-1};
3336 }
3337
3338 template <typename T>
3339 struct S {
3340 // Foo bar baz
3341 friend auto operator<=>(S, S) = default;
3342 };
3343 static_assert(S<void>() =^= S<void>());
3344 )cpp");
3345
3346 TestTU TU = TestTU::withCode(T.code());
3347 TU.ExtraArgs.push_back("-std=c++20");
3348 auto AST = TU.build();
3349 auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
3350 EXPECT_EQ(HI->Documentation, "Foo bar baz");
3351 }
3352
3353 TEST(Hover, ForwardStructNoCrash) {
3354 Annotations T(R"cpp(
3355 struct Foo;
3356 int bar;
3357 auto baz = (Fo^o*)&bar;
3358 )cpp");
3359
3360 TestTU TU = TestTU::withCode(T.code());
3361 auto AST = TU.build();
3362 auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
3363 ASSERT_TRUE(HI);
3364 EXPECT_EQ(*HI->Value, "&bar");
3365 }
3366
3367 TEST(Hover, DisableShowAKA) {
3368 Annotations T(R"cpp(
3369 using m_int = int;
3370 m_int ^[[a]];
3371 )cpp");
3372
3373 Config Cfg;
3374 Cfg.Hover.ShowAKA = false;
3375 WithContextValue WithCfg(Config::Key, std::move(Cfg));
3376
3377 TestTU TU = TestTU::withCode(T.code());
3378 TU.ExtraArgs.push_back("-std=c++17");
3379 auto AST = TU.build();
3380 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
3381
3382 ASSERT_TRUE(H);
3383 EXPECT_EQ(H->Type, HoverInfo::PrintedType("m_int"));
3384 }
3385
3386 TEST(Hover, HideBigInitializers) {
3387 Annotations T(R"cpp(
3388 #define A(x) x, x, x, x
3389 #define B(x) A(A(A(A(x))))
3390 int a^rr[] = {B(0)};
3391 )cpp");
3392
3393 TestTU TU = TestTU::withCode(T.code());
3394 auto AST = TU.build();
3395 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
3396
3397 ASSERT_TRUE(H);
3398 EXPECT_EQ(H->Definition, "int arr[]");
3399 }
3400
3401 #if defined(__aarch64__)
3402 // FIXME: AARCH64 sanitizer buildbots are broken after 72142fbac4.
3403 #define PREDEFINEMACROS_TEST(x) DISABLED_##x
3404 #else
3405 #define PREDEFINEMACROS_TEST(x) x
3406 #endif
3407
3408 TEST(Hover, PREDEFINEMACROS_TEST(GlobalVarEnumeralCastNoCrash)) {
3409 Annotations T(R"cpp(
3410 using uintptr_t = __UINTPTR_TYPE__;
3411 enum Test : uintptr_t {};
3412 unsigned global_var;
3413 void foo() {
3414 Test v^al = static_cast<Test>(reinterpret_cast<uintptr_t>(&global_var));
3415 }
3416 )cpp");
3417
3418 TestTU TU = TestTU::withCode(T.code());
3419 TU.PredefineMacros = true;
3420 auto AST = TU.build();
3421 auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
3422 ASSERT_TRUE(HI);
3423 EXPECT_EQ(*HI->Value, "&global_var");
3424 }
3425
3426 TEST(Hover, PREDEFINEMACROS_TEST(GlobalVarIntCastNoCrash)) {
3427 Annotations T(R"cpp(
3428 using uintptr_t = __UINTPTR_TYPE__;
3429 unsigned global_var;
3430 void foo() {
3431 uintptr_t a^ddress = reinterpret_cast<uintptr_t>(&global_var);
3432 }
3433 )cpp");
3434
3435 TestTU TU = TestTU::withCode(T.code());
3436 TU.PredefineMacros = true;
3437 auto AST = TU.build();
3438 auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
3439 ASSERT_TRUE(HI);
3440 EXPECT_EQ(*HI->Value, "&global_var");
3441 }
3442
3443 TEST(Hover, Typedefs) {
3444 Annotations T(R"cpp(
3445 template <bool X, typename T, typename F>
3446 struct cond { using type = T; };
3447 template <typename T, typename F>
3448 struct cond<false, T, F> { using type = F; };
3449
3450 template <bool X, typename T, typename F>
3451 using type = typename cond<X, T, F>::type;
3452
3453 void foo() {
3454 using f^oo = type<true, int, double>;
3455 }
3456 )cpp");
3457
3458 TestTU TU = TestTU::withCode(T.code());
3459 auto AST = TU.build();
3460 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
3461
3462 ASSERT_TRUE(H && H->Type);
3463 EXPECT_EQ(H->Type->Type, "int");
3464 EXPECT_EQ(H->Definition, "using foo = type<true, int, double>");
3465 }
3466 } // namespace
3467 } // namespace clangd
3468 } // namespace clang