clang-tools 24.0.0git
HoverTests.cpp
Go to the documentation of this file.
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 "Protocol.h"
14#include "TestFS.h"
15#include "TestIndex.h"
16#include "TestTU.h"
17#include "index/MemIndex.h"
18#include "clang/AST/Attr.h"
19#include "clang/Format/Format.h"
20#include "clang/Index/IndexSymbol.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/Twine.h"
23
24#include "gtest/gtest.h"
25#include <functional>
26#include <optional>
27#include <string>
28#include <vector>
29
30namespace clang {
31namespace clangd {
32namespace {
33
34using PassMode = HoverInfo::PassType::PassMode;
35
36std::string guard(llvm::StringRef Code) {
37 return "#pragma once\n" + Code.str();
38}
39
40TEST(Hover, Structured) {
41 struct {
42 const char *const Code;
43 const std::function<void(HoverInfo &)> ExpectedBuilder;
44 } Cases[] = {
45 // Global scope.
46 {R"cpp(
47 // Best foo ever.
48 void [[fo^o]]() {}
49 )cpp",
50 [](HoverInfo &HI) {
51 HI.NamespaceScope = "";
52 HI.Name = "foo";
53 HI.Kind = index::SymbolKind::Function;
54 HI.Documentation = "Best foo ever.";
55 HI.Definition = "void foo()";
56 HI.ReturnType = "void";
57 HI.Type = "void ()";
58 HI.Parameters.emplace();
59 }},
60 {R"cpp(
61 // Best foo ever.
62 void [[fo^o]](auto x) {}
63 )cpp",
64 [](HoverInfo &HI) {
65 HI.NamespaceScope = "";
66 HI.Name = "foo";
67 HI.Kind = index::SymbolKind::Function;
68 HI.Documentation = "Best foo ever.";
69 HI.Definition = "void foo(auto x)";
70 HI.ReturnType = "void";
71 HI.Type = "void (auto)";
72 HI.TemplateParameters = {
73 {{"class"}, std::string("x:auto"), std::nullopt},
74 };
75 HI.Parameters = {
76 {{"auto"}, std::string("x"), std::nullopt},
77 };
78 }},
79 {R"cpp(
80 // Best foo ever.
81 template <class T>
82 void [[fo^o]](T x) {}
83 )cpp",
84 [](HoverInfo &HI) {
85 HI.NamespaceScope = "";
86 HI.Name = "foo";
87 HI.Kind = index::SymbolKind::Function;
88 HI.Documentation = "Best foo ever.";
89 HI.Definition = "template <class T> void foo(T x)";
90 HI.ReturnType = "void";
91 HI.Type = "void (T)";
92 HI.TemplateParameters = {
93 {{"class"}, std::string("T"), std::nullopt},
94 };
95 HI.Parameters = {
96 {{"T"}, std::string("x"), std::nullopt},
97 };
98 }},
99 {R"cpp(
100 // Best foo ever.
101 template <class T>
102 void [[fo^o]](T x, auto y) {}
103 )cpp",
104 [](HoverInfo &HI) {
105 HI.NamespaceScope = "";
106 HI.Name = "foo";
107 HI.Kind = index::SymbolKind::Function;
108 HI.Documentation = "Best foo ever.";
109 HI.Definition = "template <class T> void foo(T x, auto y)";
110 HI.ReturnType = "void";
111 HI.Type = "void (T, auto)";
112 HI.TemplateParameters = {
113 {{"class"}, std::string("T"), std::nullopt},
114 {{"class"}, std::string("y:auto"), std::nullopt},
115 };
116 HI.Parameters = {
117 {{"T"}, std::string("x"), std::nullopt},
118 {{"auto"}, std::string("y"), std::nullopt},
119 };
120 }},
121 {R"cpp(
122 template<typename T1, typename T2>
123 concept C = requires () { true; };
124
125 // Best foo ever.
126 template<C<int> T>
127 void [[fo^o]](T x) {}
128 )cpp",
129 [](HoverInfo &HI) {
130 HI.NamespaceScope = "";
131 HI.Name = "foo";
132 HI.Kind = index::SymbolKind::Function;
133 HI.Documentation = "Best foo ever.";
134 HI.Definition = "template <C<int> T> void foo(T x)";
135 HI.ReturnType = "void";
136 HI.Type = "void (T)";
137 HI.TemplateParameters = {
138 {{"class"}, std::string("T"), std::nullopt},
139 };
140 HI.Parameters = {
141 {{"T"}, std::string("x"), std::nullopt},
142 };
143 }},
144 {R"cpp(
145 template<typename T1, typename T2>
146 concept C = requires () { true; };
147
148 // Best foo ever.
149 void [[fo^o]](C<int> auto x) {}
150 )cpp",
151 [](HoverInfo &HI) {
152 HI.NamespaceScope = "";
153 HI.Name = "foo";
154 HI.Kind = index::SymbolKind::Function;
155 HI.Documentation = "Best foo ever.";
156 HI.Definition = "void foo(C<int> auto x)";
157 HI.ReturnType = "void";
158 HI.Type = "void (C<int> auto)";
159 HI.TemplateParameters = {
160 {{"class"}, std::string("x:auto"), std::nullopt},
161 };
162 HI.Parameters = {
163 {{"C<int> auto"}, std::string("x"), std::nullopt},
164 };
165 }},
166 {R"cpp(
167 template<typename T1, typename T2>
168 concept C = requires () { true; };
169
170 // Best foo ever.
171 template<C<int> T>
172 void [[fo^o]](T x, C<int> auto y) {}
173 )cpp",
174 [](HoverInfo &HI) {
175 HI.NamespaceScope = "";
176 HI.Name = "foo";
177 HI.Kind = index::SymbolKind::Function;
178 HI.Documentation = "Best foo ever.";
179 HI.Definition = "template <C<int> T> void foo(T x, C<int> auto y)";
180 HI.ReturnType = "void";
181 HI.Type = "void (T, C<int> auto)";
182 HI.TemplateParameters = {
183 {{"class"}, std::string("T"), std::nullopt},
184 {{"class"}, std::string("y:auto"), std::nullopt},
185 };
186 HI.Parameters = {
187 {{"T"}, std::string("x"), std::nullopt},
188 {{"C<int> auto"}, std::string("y"), std::nullopt},
189 };
190 }},
191 // Inside namespace
192 {R"cpp(
193 namespace ns1 { namespace ns2 {
194 /// Best foo ever.
195 void [[fo^o]]() {}
196 }}
197 )cpp",
198 [](HoverInfo &HI) {
199 HI.NamespaceScope = "ns1::ns2::";
200 HI.Name = "foo";
201 HI.Kind = index::SymbolKind::Function;
202 HI.Documentation = "Best foo ever.";
203 HI.Definition = "void foo()";
204 HI.ReturnType = "void";
205 HI.Type = "void ()";
206 HI.Parameters.emplace();
207 }},
208 // Field
209 {R"cpp(
210 namespace ns1 { namespace ns2 {
211 class Foo {
212 char [[b^ar]];
213 double y[2];
214 };
215 }}
216 )cpp",
217 [](HoverInfo &HI) {
218 HI.NamespaceScope = "ns1::ns2::";
219 HI.LocalScope = "Foo::";
220 HI.Name = "bar";
221 HI.Kind = index::SymbolKind::Field;
222 HI.Definition = "char bar";
223 HI.Type = "char";
224 HI.Offset = 0;
225 HI.Size = 8;
226 HI.Padding = 56;
227 HI.Align = 8;
228 HI.AccessSpecifier = "private";
229 }},
230 // Union field
231 {R"cpp(
232 union Foo {
233 char [[b^ar]];
234 double y[2];
235 };
236 )cpp",
237 [](HoverInfo &HI) {
238 HI.NamespaceScope = "";
239 HI.LocalScope = "Foo::";
240 HI.Name = "bar";
241 HI.Kind = index::SymbolKind::Field;
242 HI.Definition = "char bar";
243 HI.Type = "char";
244 HI.Size = 8;
245 HI.Padding = 120;
246 HI.Align = 8;
247 HI.AccessSpecifier = "public";
248 }},
249 // Bitfield
250 {R"cpp(
251 struct Foo {
252 int [[^x]] : 1;
253 int y : 1;
254 };
255 )cpp",
256 [](HoverInfo &HI) {
257 HI.NamespaceScope = "";
258 HI.LocalScope = "Foo::";
259 HI.Name = "x";
260 HI.Kind = index::SymbolKind::Field;
261 HI.Definition = "int x : 1";
262 HI.Type = "int";
263 HI.Offset = 0;
264 HI.Size = 1;
265 HI.Align = 32;
266 HI.AccessSpecifier = "public";
267 }},
268 // Local to class method.
269 {R"cpp(
270 namespace ns1 { namespace ns2 {
271 struct Foo {
272 void foo() {
273 int [[b^ar]];
274 }
275 };
276 }}
277 )cpp",
278 [](HoverInfo &HI) {
279 HI.NamespaceScope = "ns1::ns2::";
280 HI.LocalScope = "Foo::foo::";
281 HI.Name = "bar";
282 HI.Kind = index::SymbolKind::Variable;
283 HI.Definition = "int bar";
284 HI.Type = "int";
285 }},
286 // Predefined variable
287 {R"cpp(
288 void foo() {
289 [[__f^unc__]];
290 }
291 )cpp",
292 [](HoverInfo &HI) {
293 HI.Name = "__func__";
294 HI.Kind = index::SymbolKind::Variable;
295 HI.Documentation =
296 "Name of the current function (predefined variable)";
297 HI.Value = "\"foo\"";
298 HI.Type = "const char[4]";
299 }},
300 // Predefined variable (dependent)
301 {R"cpp(
302 template<int> void foo() {
303 [[__f^unc__]];
304 }
305 )cpp",
306 [](HoverInfo &HI) {
307 HI.Name = "__func__";
308 HI.Kind = index::SymbolKind::Variable;
309 HI.Documentation =
310 "Name of the current function (predefined variable)";
311 HI.Type = "const char[]";
312 }},
313 // Anon namespace and local scope.
314 {R"cpp(
315 namespace ns1 { namespace {
316 struct {
317 char [[b^ar]];
318 } T;
319 }}
320 )cpp",
321 [](HoverInfo &HI) {
322 HI.NamespaceScope = "ns1::";
323 HI.LocalScope = "(anonymous struct)::";
324 HI.Name = "bar";
325 HI.Kind = index::SymbolKind::Field;
326 HI.Definition = "char bar";
327 HI.Type = "char";
328 HI.Offset = 0;
329 HI.Size = 8;
330 HI.Align = 8;
331 HI.AccessSpecifier = "public";
332 }},
333 // Struct definition shows size.
334 {R"cpp(
335 struct [[^X]]{};
336 )cpp",
337 [](HoverInfo &HI) {
338 HI.NamespaceScope = "";
339 HI.Name = "X";
340 HI.Kind = index::SymbolKind::Struct;
341 HI.Definition = "struct X {}";
342 HI.Size = 8;
343 HI.Align = 8;
344 }},
345 // Variable with template type
346 {R"cpp(
347 template <typename T, class... Ts> class Foo { public: Foo(int); };
348 Foo<int, char, bool> [[fo^o]] = Foo<int, char, bool>(5);
349 )cpp",
350 [](HoverInfo &HI) {
351 HI.NamespaceScope = "";
352 HI.Name = "foo";
353 HI.Kind = index::SymbolKind::Variable;
354 HI.Definition = "Foo<int, char, bool> foo = Foo<int, char, bool>(5)";
355 HI.Type = "Foo<int, char, bool>";
356 }},
357 // Implicit template instantiation
358 {R"cpp(
359 template <typename T> class vector{};
360 [[vec^tor]]<int> foo;
361 )cpp",
362 [](HoverInfo &HI) {
363 HI.NamespaceScope = "";
364 HI.Name = "vector<int>";
365 HI.Kind = index::SymbolKind::Class;
366 HI.Definition = "template <> class vector<int> {}";
367 }},
368 // Class template
369 {R"cpp(
370 template <template<typename, bool...> class C,
371 typename = char,
372 int = 0,
373 bool Q = false,
374 class... Ts> class Foo final {};
375 template <template<typename, bool...> class T>
376 [[F^oo]]<T> foo;
377 )cpp",
378 [](HoverInfo &HI) {
379 HI.NamespaceScope = "";
380 HI.Name = "Foo";
381 HI.Kind = index::SymbolKind::Class;
382 HI.Definition =
383 R"cpp(template <template <typename, bool...> class C, typename = char, int = 0,
384 bool Q = false, class... Ts>
385class Foo final {})cpp";
386 HI.TemplateParameters = {
387 {{"template <typename, bool...> class"},
388 std::string("C"),
389 std::nullopt},
390 {{"typename"}, std::nullopt, std::string("char")},
391 {{"int"}, std::nullopt, std::string("0")},
392 {{"bool"}, std::string("Q"), std::string("false")},
393 {{"class..."}, std::string("Ts"), std::nullopt},
394 };
395 }},
396 // Function template
397 {R"cpp(
398 template <template<typename, bool...> class C,
399 typename = char,
400 int = 0,
401 bool Q = false,
402 class... Ts> void foo();
403 template<typename, bool...> class Foo;
404
405 void bar() {
406 [[fo^o]]<Foo>();
407 }
408 )cpp",
409 [](HoverInfo &HI) {
410 HI.NamespaceScope = "";
411 HI.Name = "foo";
412 HI.Kind = index::SymbolKind::Function;
413 HI.Definition = "template <> void foo<Foo, char, 0, false, <>>()";
414 HI.ReturnType = "void";
415 HI.Type = "void ()";
416 HI.Parameters.emplace();
417 }},
418 // Function decl
419 {R"cpp(
420 template<typename, bool...> class Foo {};
421 Foo<bool, true, false> foo(int, bool T = false);
422
423 void bar() {
424 [[fo^o]](3);
425 }
426 )cpp",
427 [](HoverInfo &HI) {
428 HI.NamespaceScope = "";
429 HI.Name = "foo";
430 HI.Kind = index::SymbolKind::Function;
431 HI.Definition = "Foo<bool, true, false> foo(int, bool T = false)";
432 HI.ReturnType = "Foo<bool, true, false>";
433 HI.Type = "Foo<bool, true, false> (int, bool)";
434 HI.Parameters = {
435 {{"int"}, std::nullopt, std::nullopt},
436 {{"bool"}, std::string("T"), std::string("false")},
437 };
438 }},
439 // Pointers to lambdas
440 {R"cpp(
441 void foo() {
442 auto lamb = [](int T, bool B) -> bool { return T && B; };
443 auto *b = &lamb;
444 auto *[[^c]] = &b;
445 }
446 )cpp",
447 [](HoverInfo &HI) {
448 HI.NamespaceScope = "";
449 HI.LocalScope = "foo::";
450 HI.Name = "c";
451 HI.Kind = index::SymbolKind::Variable;
452 HI.Definition = "auto *c = &b";
453 HI.Type = "(lambda) **";
454 HI.ReturnType = "bool";
455 HI.Parameters = {
456 {{"int"}, std::string("T"), std::nullopt},
457 {{"bool"}, std::string("B"), std::nullopt},
458 };
459 return HI;
460 }},
461 // Lambda parameter with decltype reference
462 {R"cpp(
463 auto lamb = [](int T, bool B) -> bool { return T && B; };
464 void foo(decltype(lamb)& bar) {
465 [[ba^r]](0, false);
466 }
467 )cpp",
468 [](HoverInfo &HI) {
469 HI.NamespaceScope = "";
470 HI.LocalScope = "foo::";
471 HI.Name = "bar";
472 HI.Kind = index::SymbolKind::Parameter;
473 HI.Definition = "decltype(lamb) &bar";
474 HI.Type = "(lambda) &";
475 HI.ReturnType = "bool";
476 HI.Parameters = {
477 {{"int"}, std::string("T"), std::nullopt},
478 {{"bool"}, std::string("B"), std::nullopt},
479 };
480 return HI;
481 }},
482 // Lambda parameter with decltype
483 {R"cpp(
484 auto lamb = [](int T, bool B) -> bool { return T && B; };
485 void foo(decltype(lamb) bar) {
486 [[ba^r]](0, false);
487 }
488 )cpp",
489 [](HoverInfo &HI) {
490 HI.NamespaceScope = "";
491 HI.LocalScope = "foo::";
492 HI.Name = "bar";
493 HI.Kind = index::SymbolKind::Parameter;
494 HI.Definition = "decltype(lamb) bar";
495 HI.Type = "class (lambda)";
496 HI.ReturnType = "bool";
497 HI.Parameters = {
498 {{"int"}, std::string("T"), std::nullopt},
499 {{"bool"}, std::string("B"), std::nullopt},
500 };
501 HI.Value = "false";
502 return HI;
503 }},
504 // Lambda variable
505 {R"cpp(
506 void foo() {
507 int bar = 5;
508 auto lamb = [&bar](int T, bool B) -> bool { return T && B && bar; };
509 bool res = [[lam^b]](bar, false);
510 }
511 )cpp",
512 [](HoverInfo &HI) {
513 HI.NamespaceScope = "";
514 HI.LocalScope = "foo::";
515 HI.Name = "lamb";
516 HI.Kind = index::SymbolKind::Variable;
517 HI.Definition = "auto lamb = [&bar](int T, bool B) -> bool {}";
518 HI.Type = "class (lambda)";
519 HI.ReturnType = "bool";
520 HI.Parameters = {
521 {{"int"}, std::string("T"), std::nullopt},
522 {{"bool"}, std::string("B"), std::nullopt},
523 };
524 return HI;
525 }},
526 // Local variable in lambda
527 {R"cpp(
528 void foo() {
529 auto lamb = []{int [[te^st]];};
530 }
531 )cpp",
532 [](HoverInfo &HI) {
533 HI.NamespaceScope = "";
534 HI.LocalScope = "foo::(anonymous class)::operator()::";
535 HI.Name = "test";
536 HI.Kind = index::SymbolKind::Variable;
537 HI.Definition = "int test";
538 HI.Type = "int";
539 }},
540 // Partially-specialized class template. (formerly type-parameter-0-0)
541 {R"cpp(
542 template <typename T> class X;
543 template <typename T> class [[^X]]<T*> {};
544 )cpp",
545 [](HoverInfo &HI) {
546 HI.Name = "X<T *>";
547 HI.NamespaceScope = "";
548 HI.Kind = index::SymbolKind::Class;
549 HI.Definition = "template <typename T> class X<T *> {}";
550 }},
551 // Constructor of partially-specialized class template
552 {R"cpp(
553 template<typename, typename=void> struct X;
554 template<typename T> struct X<T*>{ [[^X]](); };
555 )cpp",
556 [](HoverInfo &HI) {
557 HI.NamespaceScope = "";
558 HI.Name = "X";
559 HI.LocalScope = "X<T *>::"; // FIXME: X<T *, void>::
560 HI.Kind = index::SymbolKind::Constructor;
561 HI.Definition = "X()";
562 HI.Parameters.emplace();
563 HI.AccessSpecifier = "public";
564 }},
565 {"class X { [[^~]]X(); };", // FIXME: Should be [[~X]]()
566 [](HoverInfo &HI) {
567 HI.NamespaceScope = "";
568 HI.Name = "~X";
569 HI.LocalScope = "X::";
570 HI.Kind = index::SymbolKind::Destructor;
571 HI.Definition = "~X()";
572 HI.Parameters.emplace();
573 HI.AccessSpecifier = "private";
574 }},
575 {"class X { [[op^erator]] int(); };",
576 [](HoverInfo &HI) {
577 HI.NamespaceScope = "";
578 HI.Name = "operator int";
579 HI.LocalScope = "X::";
580 HI.Kind = index::SymbolKind::ConversionFunction;
581 HI.Definition = "operator int()";
582 HI.Parameters.emplace();
583 HI.AccessSpecifier = "private";
584 }},
585 {"class X { operator [[^X]](); };",
586 [](HoverInfo &HI) {
587 HI.NamespaceScope = "";
588 HI.Name = "X";
589 HI.Kind = index::SymbolKind::Class;
590 HI.Definition = "class X {}";
591 }},
592
593 // auto on structured bindings
594 {R"cpp(
595 void foo() {
596 struct S { int x; float y; };
597 [[au^to]] [x, y] = S();
598 }
599 )cpp",
600 [](HoverInfo &HI) {
601 HI.Name = "auto";
602 HI.Kind = index::SymbolKind::TypeAlias;
603 HI.Definition = "S";
604 }},
605 // undeduced auto
606 {R"cpp(
607 template<typename T>
608 void foo() {
609 [[au^to]] x = T{};
610 }
611 )cpp",
612 [](HoverInfo &HI) {
613 HI.Name = "auto";
614 HI.Kind = index::SymbolKind::TypeAlias;
615 HI.Definition = "T";
616 }},
617 // constrained auto
618 {R"cpp(
619 template <class T> concept F = true;
620 F [[au^to]] x = 1;
621 )cpp",
622 [](HoverInfo &HI) {
623 HI.Name = "auto";
624 HI.Kind = index::SymbolKind::TypeAlias;
625 HI.Definition = "int";
626 }},
627 {R"cpp(
628 template <class T> concept F = true;
629 [[^F]] auto x = 1;
630 )cpp",
631 [](HoverInfo &HI) {
632 HI.NamespaceScope = "";
633 HI.Name = "F";
634 HI.Kind = index::SymbolKind::Concept;
635 HI.Definition = "template <class T>\nconcept F = true";
636 }},
637 // auto on lambda
638 {R"cpp(
639 void foo() {
640 [[au^to]] lamb = []{};
641 }
642 )cpp",
643 [](HoverInfo &HI) {
644 HI.Name = "auto";
645 HI.Kind = index::SymbolKind::TypeAlias;
646 HI.Definition = "class(lambda)";
647 }},
648 // auto on template instantiation
649 {R"cpp(
650 template<typename T> class Foo{};
651 void foo() {
652 [[au^to]] x = Foo<int>();
653 }
654 )cpp",
655 [](HoverInfo &HI) {
656 HI.Name = "auto";
657 HI.Kind = index::SymbolKind::TypeAlias;
658 HI.Definition = "Foo<int>";
659 }},
660 // auto on specialized template
661 {R"cpp(
662 template<typename T> class Foo{};
663 template<> class Foo<int>{};
664 void foo() {
665 [[au^to]] x = Foo<int>();
666 }
667 )cpp",
668 [](HoverInfo &HI) {
669 HI.Name = "auto";
670 HI.Kind = index::SymbolKind::TypeAlias;
671 HI.Definition = "Foo<int>";
672 }},
673 // constrained template parameter
674 {R"cpp(
675 template<class T> concept Fooable = true;
676 template<[[Foo^able]] T>
677 void bar(T t) {}
678 )cpp",
679 [](HoverInfo &HI) {
680 HI.NamespaceScope = "";
681 HI.Name = "Fooable";
682 HI.Kind = index::SymbolKind::Concept;
683 HI.Definition = "template <class T>\nconcept Fooable = true";
684 }},
685 {R"cpp(
686 template<class T> concept Fooable = true;
687 template<Fooable [[T^T]]>
688 void bar(TT t) {}
689 )cpp",
690 [](HoverInfo &HI) {
691 HI.Name = "TT";
692 HI.Type = "class";
693 HI.AccessSpecifier = "public";
694 HI.NamespaceScope = "";
695 HI.LocalScope = "bar::";
696 HI.Kind = index::SymbolKind::TemplateTypeParm;
697 HI.Definition = "Fooable TT";
698 }},
699 {R"cpp(
700 template<class T> concept Fooable = true;
701 void bar([[Foo^able]] auto t) {}
702 )cpp",
703 [](HoverInfo &HI) {
704 HI.NamespaceScope = "";
705 HI.Name = "Fooable";
706 HI.Kind = index::SymbolKind::Concept;
707 HI.Definition = "template <class T>\nconcept Fooable = true";
708 }},
709 // concept reference
710 {R"cpp(
711 template<class T> concept Fooable = true;
712 auto X = [[Fooa^ble]]<int>;
713 )cpp",
714 [](HoverInfo &HI) {
715 HI.NamespaceScope = "";
716 HI.Name = "Fooable";
717 HI.Kind = index::SymbolKind::Concept;
718 HI.Definition = "template <class T>\nconcept Fooable = true";
719 HI.Value = "true";
720 }},
721
722 // empty macro
723 {R"cpp(
724 #define MACRO
725 [[MAC^RO]]
726 )cpp",
727 [](HoverInfo &HI) {
728 HI.Name = "MACRO";
729 HI.Kind = index::SymbolKind::Macro;
730 HI.Definition = "#define MACRO";
731 }},
732
733 // object-like macro
734 {R"cpp(
735 #define MACRO 41
736 int x = [[MAC^RO]];
737 )cpp",
738 [](HoverInfo &HI) {
739 HI.Name = "MACRO";
740 HI.Kind = index::SymbolKind::Macro;
741 HI.Value = "41 (0x29)";
742 HI.Type = "int";
743 HI.Definition = "#define MACRO 41\n\n"
744 "// Expands to\n"
745 "41";
746 }},
747
748 // function-like macro
749 {R"cpp(
750 // Best MACRO ever.
751 #define MACRO(x,y,z) void foo(x, y, z)
752 [[MAC^RO]](int, double d, bool z = false);
753 )cpp",
754 [](HoverInfo &HI) {
755 HI.Name = "MACRO";
756 HI.Kind = index::SymbolKind::Macro;
757 HI.Definition = "#define MACRO(x, y, z) void foo(x, y, z)\n\n"
758 "// Expands to\n"
759 "void foo(int, double d, bool z = false)";
760 }},
761
762 // nested macro
763 {R"cpp(
764 #define STRINGIFY_AUX(s) #s
765 #define STRINGIFY(s) STRINGIFY_AUX(s)
766 #define DECL_STR(NAME, VALUE) const char *v_##NAME = STRINGIFY(VALUE)
767 #define FOO 41
768
769 [[DECL^_STR]](foo, FOO);
770 )cpp",
771 [](HoverInfo &HI) {
772 HI.Name = "DECL_STR";
773 HI.Kind = index::SymbolKind::Macro;
774 HI.Type = HoverInfo::PrintedType("const char *");
775 HI.Definition = "#define DECL_STR(NAME, VALUE) const char *v_##NAME = "
776 "STRINGIFY(VALUE)\n\n"
777 "// Expands to\n"
778 "const char *v_foo = \"41\"";
779 }},
780
781 // constexprs
782 {R"cpp(
783 constexpr int add(int a, int b) { return a + b; }
784 int [[b^ar]] = add(1, 2);
785 )cpp",
786 [](HoverInfo &HI) {
787 HI.Name = "bar";
788 HI.Definition = "int bar = add(1, 2)";
789 HI.Kind = index::SymbolKind::Variable;
790 HI.Type = "int";
791 HI.NamespaceScope = "";
792 HI.Value = "3";
793 }},
794 {R"cpp(
795 int [[b^ar]] = sizeof(char);
796 )cpp",
797 [](HoverInfo &HI) {
798 HI.Name = "bar";
799 HI.Definition = "int bar = sizeof(char)";
800 HI.Kind = index::SymbolKind::Variable;
801 HI.Type = "int";
802 HI.NamespaceScope = "";
803 HI.Value = "1";
804 }},
805 {R"cpp(
806 template<int a, int b> struct Add {
807 static constexpr int result = a + b;
808 };
809 int [[ba^r]] = Add<1, 2>::result;
810 )cpp",
811 [](HoverInfo &HI) {
812 HI.Name = "bar";
813 HI.Definition = "int bar = Add<1, 2>::result";
814 HI.Kind = index::SymbolKind::Variable;
815 HI.Type = "int";
816 HI.NamespaceScope = "";
817 HI.Value = "3";
818 }},
819 {R"cpp(
820 enum Color { RED = -123, GREEN = 5, };
821 Color x = [[GR^EEN]];
822 )cpp",
823 [](HoverInfo &HI) {
824 HI.Name = "GREEN";
825 HI.NamespaceScope = "";
826 HI.LocalScope = "Color::";
827 HI.Definition = "GREEN = 5";
828 HI.Kind = index::SymbolKind::EnumConstant;
829 HI.Type = "enum Color";
830 HI.Value = "5"; // Numeric on the enumerator name, no hex as small.
831 }},
832 {R"cpp(
833 enum Color { RED = -123, GREEN = 5, };
834 Color x = RED;
835 Color y = [[^x]];
836 )cpp",
837 [](HoverInfo &HI) {
838 HI.Name = "x";
839 HI.NamespaceScope = "";
840 HI.Definition = "Color x = RED";
841 HI.Kind = index::SymbolKind::Variable;
842 HI.Type = "Color";
843 HI.Value = "RED (0xffffff85)"; // Symbolic on an expression.
844 }},
845 {R"cpp(
846 template<int a, int b> struct Add {
847 static constexpr int result = a + b;
848 };
849 int bar = Add<1, 2>::[[resu^lt]];
850 )cpp",
851 [](HoverInfo &HI) {
852 HI.Name = "result";
853 HI.Definition = "static constexpr int result = a + b";
854 HI.Kind = index::SymbolKind::StaticProperty;
855 HI.Type = "const int";
856 HI.NamespaceScope = "";
857 HI.LocalScope = "Add<1, 2>::";
858 HI.Value = "3";
859 HI.AccessSpecifier = "public";
860 }},
861 {R"cpp(
862 using my_int = int;
863 constexpr my_int answer() { return 40 + 2; }
864 int x = [[ans^wer]]();
865 )cpp",
866 [](HoverInfo &HI) {
867 HI.Name = "answer";
868 HI.Definition = "constexpr my_int answer()";
869 HI.Kind = index::SymbolKind::Function;
870 HI.Type = {"my_int ()", "int ()"};
871 HI.ReturnType = {"my_int", "int"};
872 HI.Parameters.emplace();
873 HI.NamespaceScope = "";
874 HI.Value = "42 (0x2a)";
875 }},
876 {R"cpp(
877 const char *[[ba^r]] = "1234";
878 )cpp",
879 [](HoverInfo &HI) {
880 HI.Name = "bar";
881 HI.Definition = "const char *bar = \"1234\"";
882 HI.Kind = index::SymbolKind::Variable;
883 HI.Type = "const char *";
884 HI.NamespaceScope = "";
885 HI.Value = "&\"1234\"[0]";
886 }},
887 {R"cpp(// Should not crash
888 template <typename T>
889 struct Tmpl {
890 Tmpl(int name);
891 };
892
893 template <typename A>
894 void boom(int name) {
895 new Tmpl<A>([[na^me]]);
896 })cpp",
897 [](HoverInfo &HI) {
898 HI.Name = "name";
899 HI.Definition = "int name";
900 HI.Kind = index::SymbolKind::Parameter;
901 HI.Type = "int";
902 HI.NamespaceScope = "";
903 HI.LocalScope = "boom::";
904 }},
905 {
906 R"cpp(// Should not print inline or anon namespaces.
907 namespace ns {
908 inline namespace in_ns {
909 namespace a {
910 namespace {
911 namespace b {
912 inline namespace in_ns2 {
913 class Foo {};
914 } // in_ns2
915 } // b
916 } // anon
917 } // a
918 } // in_ns
919 } // ns
920 void foo() {
921 ns::a::b::[[F^oo]] x;
922 (void)x;
923 }
924 )cpp",
925 [](HoverInfo &HI) {
926 HI.Name = "Foo";
927 HI.Kind = index::SymbolKind::Class;
928 HI.NamespaceScope = "ns::a::b::";
929 HI.Definition = "class Foo {}";
930 }},
931 {
932 R"cpp(
933 template <typename T> class Foo {};
934 class X;
935 void foo() {
936 [[^auto]] x = Foo<X>();
937 }
938 )cpp",
939 [](HoverInfo &HI) {
940 HI.Name = "auto";
941 HI.Kind = index::SymbolKind::TypeAlias;
942 HI.Definition = "Foo<X>";
943 }},
944 {// Falls back to primary template, when the type is not instantiated.
945 R"cpp(
946 // comment from primary
947 template <typename T> class Foo {};
948 // comment from specialization
949 template <typename T> class Foo<T*> {};
950 void foo() {
951 [[Fo^o]]<int*> *x = nullptr;
952 }
953 )cpp",
954 [](HoverInfo &HI) {
955 HI.Name = "Foo<int *>";
956 HI.Kind = index::SymbolKind::Class;
957 HI.NamespaceScope = "";
958 HI.Definition = "template <> class Foo<int *>";
959 // FIXME: Maybe force instantiation to make use of real template
960 // pattern.
961 HI.Documentation = "comment from primary";
962 }},
963 {// Template Type Parameter
964 R"cpp(
965 template <typename [[^T]] = int> void foo();
966 )cpp",
967 [](HoverInfo &HI) {
968 HI.Name = "T";
969 HI.Kind = index::SymbolKind::TemplateTypeParm;
970 HI.NamespaceScope = "";
971 HI.Definition = "typename T = int";
972 HI.LocalScope = "foo::";
973 HI.Type = "typename";
974 HI.AccessSpecifier = "public";
975 }},
976 {// TemplateTemplate Type Parameter
977 R"cpp(
978 template <template<typename> class [[^T]]> void foo();
979 )cpp",
980 [](HoverInfo &HI) {
981 HI.Name = "T";
982 HI.Kind = index::SymbolKind::TemplateTemplateParm;
983 HI.NamespaceScope = "";
984 HI.Definition = "template <typename> class T";
985 HI.LocalScope = "foo::";
986 HI.Type = "template <typename> class";
987 HI.AccessSpecifier = "public";
988 }},
989 {// NonType Template Parameter
990 R"cpp(
991 template <int [[^T]] = 5> void foo();
992 )cpp",
993 [](HoverInfo &HI) {
994 HI.Name = "T";
995 HI.Kind = index::SymbolKind::NonTypeTemplateParm;
996 HI.NamespaceScope = "";
997 HI.Definition = "int T = 5";
998 HI.LocalScope = "foo::";
999 HI.Type = "int";
1000 HI.AccessSpecifier = "public";
1001 }},
1002
1003 {// Getter
1004 R"cpp(
1005 struct X { int Y; float [[^y]]() { return Y; } };
1006 )cpp",
1007 [](HoverInfo &HI) {
1008 HI.Name = "y";
1009 HI.Kind = index::SymbolKind::InstanceMethod;
1010 HI.NamespaceScope = "";
1011 HI.Definition = "float y()";
1012 HI.LocalScope = "X::";
1013 HI.Documentation = "Trivial accessor for `Y`.";
1014 HI.Type = "float ()";
1015 HI.ReturnType = "float";
1016 HI.Parameters.emplace();
1017 HI.AccessSpecifier = "public";
1018 }},
1019 {// Getter with comment
1020 R"cpp(
1021 struct X {
1022 // An int named Y
1023 int Y;
1024 float [[^y]]() { return Y; }
1025 };
1026 )cpp",
1027 [](HoverInfo &HI) {
1028 HI.Name = "y";
1029 HI.Kind = index::SymbolKind::InstanceMethod;
1030 HI.NamespaceScope = "";
1031 HI.Definition = "float y()";
1032 HI.LocalScope = "X::";
1033 HI.Documentation = "Trivial accessor for `Y`.\n\nAn int named Y";
1034 HI.Type = "float ()";
1035 HI.ReturnType = "float";
1036 HI.Parameters.emplace();
1037 HI.AccessSpecifier = "public";
1038 }},
1039 {// Setter
1040 R"cpp(
1041 struct X { int Y; void [[^setY]](float v) { Y = v; } };
1042 )cpp",
1043 [](HoverInfo &HI) {
1044 HI.Name = "setY";
1045 HI.Kind = index::SymbolKind::InstanceMethod;
1046 HI.NamespaceScope = "";
1047 HI.Definition = "void setY(float v)";
1048 HI.LocalScope = "X::";
1049 HI.Documentation = "Trivial setter for `Y`.";
1050 HI.Type = "void (float)";
1051 HI.ReturnType = "void";
1052 HI.Parameters.emplace();
1053 HI.Parameters->emplace_back();
1054 HI.Parameters->back().Type = "float";
1055 HI.Parameters->back().Name = "v";
1056 HI.AccessSpecifier = "public";
1057 }},
1058 {// Setter (builder)
1059 R"cpp(
1060 struct X { int Y; X& [[^setY]](float v) { Y = v; return *this; } };
1061 )cpp",
1062 [](HoverInfo &HI) {
1063 HI.Name = "setY";
1064 HI.Kind = index::SymbolKind::InstanceMethod;
1065 HI.NamespaceScope = "";
1066 HI.Definition = "X &setY(float v)";
1067 HI.LocalScope = "X::";
1068 HI.Documentation = "Trivial setter for `Y`.";
1069 HI.Type = "X &(float)";
1070 HI.ReturnType = "X &";
1071 HI.Parameters.emplace();
1072 HI.Parameters->emplace_back();
1073 HI.Parameters->back().Type = "float";
1074 HI.Parameters->back().Name = "v";
1075 HI.AccessSpecifier = "public";
1076 }},
1077 {// Setter (move)
1078 R"cpp(
1079 namespace std { template<typename T> T&& move(T&& t); }
1080 struct X { int Y; void [[^setY]](float v) { Y = std::move(v); } };
1081 )cpp",
1082 [](HoverInfo &HI) {
1083 HI.Name = "setY";
1084 HI.Kind = index::SymbolKind::InstanceMethod;
1085 HI.NamespaceScope = "";
1086 HI.Definition = "void setY(float v)";
1087 HI.LocalScope = "X::";
1088 HI.Documentation = "Trivial setter for `Y`.";
1089 HI.Type = "void (float)";
1090 HI.ReturnType = "void";
1091 HI.Parameters.emplace();
1092 HI.Parameters->emplace_back();
1093 HI.Parameters->back().Type = "float";
1094 HI.Parameters->back().Name = "v";
1095 HI.AccessSpecifier = "public";
1096 }},
1097 {// Setter with comment
1098 R"cpp(
1099 struct X {
1100 // An int named Y
1101 int Y;
1102 void [[^setY]](float v) { Y = v; }
1103 };
1104 )cpp",
1105 [](HoverInfo &HI) {
1106 HI.Name = "setY";
1107 HI.Kind = index::SymbolKind::InstanceMethod;
1108 HI.NamespaceScope = "";
1109 HI.Definition = "void setY(float v)";
1110 HI.LocalScope = "X::";
1111 HI.Documentation = "Trivial setter for `Y`.\n\nAn int named Y";
1112 HI.Type = "void (float)";
1113 HI.ReturnType = "void";
1114 HI.Parameters.emplace();
1115 HI.Parameters->emplace_back();
1116 HI.Parameters->back().Type = "float";
1117 HI.Parameters->back().Name = "v";
1118 HI.AccessSpecifier = "public";
1119 }},
1120 {// Field type initializer.
1121 R"cpp(
1122 struct X { int x = 2; };
1123 X ^[[x]];
1124 )cpp",
1125 [](HoverInfo &HI) {
1126 HI.Name = "x";
1127 HI.Kind = index::SymbolKind::Variable;
1128 HI.NamespaceScope = "";
1129 HI.Definition = "X x";
1130 HI.Type = "X";
1131 }},
1132 {// Don't crash on null types.
1133 R"cpp(auto [^[[x]]] = 1; /*error-ok*/)cpp",
1134 [](HoverInfo &HI) {
1135 HI.Name = "x";
1136 HI.Kind = index::SymbolKind::Variable;
1137 HI.NamespaceScope = "";
1138 HI.Definition = "";
1139 HI.Type = "NULL TYPE";
1140 // Bindings are in theory public members of an anonymous struct.
1141 HI.AccessSpecifier = "public";
1142 }},
1143 {// Don't crash on invalid decl with invalid init expr.
1144 R"cpp(
1145 Unknown [[^abc]] = invalid;
1146 // error-ok
1147 )cpp",
1148 [](HoverInfo &HI) {
1149 HI.Name = "abc";
1150 HI.Kind = index::SymbolKind::Variable;
1151 HI.NamespaceScope = "";
1152 HI.Definition = "int abc";
1153 HI.Type = "int";
1154 HI.AccessSpecifier = "public";
1155 }},
1156 {// Extra info for function call.
1157 R"cpp(
1158 void fun(int arg_a, int &arg_b) {};
1159 void code() {
1160 int a = 1, b = 2;
1161 fun(a, [[^b]]);
1162 }
1163 )cpp",
1164 [](HoverInfo &HI) {
1165 HI.Name = "b";
1166 HI.Kind = index::SymbolKind::Variable;
1167 HI.NamespaceScope = "";
1168 HI.Definition = "int b = 2";
1169 HI.LocalScope = "code::";
1170 HI.Value = "2";
1171 HI.Type = "int";
1172 HI.CalleeArgInfo.emplace();
1173 HI.CalleeArgInfo->Name = "arg_b";
1174 HI.CalleeArgInfo->Type = "int &";
1175 HI.CallPassType = HoverInfo::PassType{PassMode::Ref, false};
1176 }},
1177 {// make_unique-like function call
1178 R"cpp(
1179 struct Foo {
1180 explicit Foo(int arg_a) {}
1181 };
1182 template<class T, class... Args>
1183 T make(Args&&... args)
1184 {
1185 return T(args...);
1186 }
1187
1188 void code() {
1189 int a = 1;
1190 auto foo = make<Foo>([[^a]]);
1191 }
1192 )cpp",
1193 [](HoverInfo &HI) {
1194 HI.Name = "a";
1195 HI.Kind = index::SymbolKind::Variable;
1196 HI.NamespaceScope = "";
1197 HI.Definition = "int a = 1";
1198 HI.LocalScope = "code::";
1199 HI.Value = "1";
1200 HI.Type = "int";
1201 HI.CalleeArgInfo.emplace();
1202 HI.CalleeArgInfo->Name = "arg_a";
1203 HI.CalleeArgInfo->Type = "int";
1204 HI.CallPassType = HoverInfo::PassType{PassMode::Value, false};
1205 }},
1206 {
1207 R"cpp(
1208 void foobar(const float &arg);
1209 int main() {
1210 int a = 0;
1211 foobar([[^a]]);
1212 }
1213 )cpp",
1214 [](HoverInfo &HI) {
1215 HI.Name = "a";
1216 HI.Kind = index::SymbolKind::Variable;
1217 HI.NamespaceScope = "";
1218 HI.Definition = "int a = 0";
1219 HI.LocalScope = "main::";
1220 HI.Value = "0";
1221 HI.Type = "int";
1222 HI.CalleeArgInfo.emplace();
1223 HI.CalleeArgInfo->Name = "arg";
1224 HI.CalleeArgInfo->Type = "const float &";
1225 HI.CallPassType = HoverInfo::PassType{PassMode::Value, true};
1226 }},
1227 {
1228 R"cpp(
1229 struct Foo {
1230 explicit Foo(const float& arg) {}
1231 };
1232 int main() {
1233 int a = 0;
1234 Foo foo([[^a]]);
1235 }
1236 )cpp",
1237 [](HoverInfo &HI) {
1238 HI.Name = "a";
1239 HI.Kind = index::SymbolKind::Variable;
1240 HI.NamespaceScope = "";
1241 HI.Definition = "int a = 0";
1242 HI.LocalScope = "main::";
1243 HI.Value = "0";
1244 HI.Type = "int";
1245 HI.CalleeArgInfo.emplace();
1246 HI.CalleeArgInfo->Name = "arg";
1247 HI.CalleeArgInfo->Type = "const float &";
1248 HI.CallPassType = HoverInfo::PassType{PassMode::Value, true};
1249 }},
1250 {// Literal passed to function call
1251 R"cpp(
1252 void fun(int arg_a, const int &arg_b) {};
1253 void code() {
1254 int a = 1;
1255 fun(a, [[^2]]);
1256 }
1257 )cpp",
1258 [](HoverInfo &HI) {
1259 HI.Name = "literal";
1260 HI.Kind = index::SymbolKind::Unknown;
1261 HI.CalleeArgInfo.emplace();
1262 HI.CalleeArgInfo->Name = "arg_b";
1263 HI.CalleeArgInfo->Type = "const int &";
1264 HI.CallPassType = HoverInfo::PassType{PassMode::ConstRef, false};
1265 }},
1266 {// Expression passed to function call
1267 R"cpp(
1268 void fun(int arg_a, const int &arg_b) {};
1269 void code() {
1270 int a = 1;
1271 fun(a, 1 [[^+]] 2);
1272 }
1273 )cpp",
1274 [](HoverInfo &HI) {
1275 HI.Name = "expression";
1276 HI.Kind = index::SymbolKind::Unknown;
1277 HI.Type = "int";
1278 HI.Value = "3";
1279 HI.CalleeArgInfo.emplace();
1280 HI.CalleeArgInfo->Name = "arg_b";
1281 HI.CalleeArgInfo->Type = "const int &";
1282 HI.CallPassType = HoverInfo::PassType{PassMode::ConstRef, false};
1283 }},
1284 {
1285 R"cpp(
1286 int add(int lhs, int rhs);
1287 int main() {
1288 add(1 [[^+]] 2, 3);
1289 }
1290 )cpp",
1291 [](HoverInfo &HI) {
1292 HI.Name = "expression";
1293 HI.Kind = index::SymbolKind::Unknown;
1294 HI.Type = "int";
1295 HI.Value = "3";
1296 HI.CalleeArgInfo.emplace();
1297 HI.CalleeArgInfo->Name = "lhs";
1298 HI.CalleeArgInfo->Type = "int";
1299 HI.CallPassType = HoverInfo::PassType{PassMode::Value, false};
1300 }},
1301 {
1302 R"cpp(
1303 void foobar(const float &arg);
1304 int main() {
1305 foobar([[^0]]);
1306 }
1307 )cpp",
1308 [](HoverInfo &HI) {
1309 HI.Name = "literal";
1310 HI.Kind = index::SymbolKind::Unknown;
1311 HI.CalleeArgInfo.emplace();
1312 HI.CalleeArgInfo->Name = "arg";
1313 HI.CalleeArgInfo->Type = "const float &";
1314 HI.CallPassType = HoverInfo::PassType{PassMode::Value, true};
1315 }},
1316 {// Extra info for method call.
1317 R"cpp(
1318 class C {
1319 public:
1320 void fun(int arg_a = 3, int arg_b = 4) {}
1321 };
1322 void code() {
1323 int a = 1, b = 2;
1324 C c;
1325 c.fun([[^a]], b);
1326 }
1327 )cpp",
1328 [](HoverInfo &HI) {
1329 HI.Name = "a";
1330 HI.Kind = index::SymbolKind::Variable;
1331 HI.NamespaceScope = "";
1332 HI.Definition = "int a = 1";
1333 HI.LocalScope = "code::";
1334 HI.Value = "1";
1335 HI.Type = "int";
1336 HI.CalleeArgInfo.emplace();
1337 HI.CalleeArgInfo->Name = "arg_a";
1338 HI.CalleeArgInfo->Type = "int";
1339 HI.CalleeArgInfo->Default = "3";
1340 HI.CallPassType = HoverInfo::PassType{PassMode::Value, false};
1341 }},
1342 {
1343 R"cpp(
1344 struct Foo {
1345 Foo(const int &);
1346 };
1347 void foo(Foo);
1348 void bar() {
1349 const int x = 0;
1350 foo([[^x]]);
1351 }
1352 )cpp",
1353 [](HoverInfo &HI) {
1354 HI.Name = "x";
1355 HI.Kind = index::SymbolKind::Variable;
1356 HI.NamespaceScope = "";
1357 HI.Definition = "const int x = 0";
1358 HI.LocalScope = "bar::";
1359 HI.Value = "0";
1360 HI.Type = "const int";
1361 HI.CalleeArgInfo.emplace();
1362 HI.CalleeArgInfo->Type = "Foo";
1363 HI.CallPassType = HoverInfo::PassType{PassMode::ConstRef, true};
1364 }},
1365 {// Dont crash on invalid decl
1366 R"cpp(
1367 // error-ok
1368 struct Foo {
1369 Bar [[x^x]];
1370 };)cpp",
1371 [](HoverInfo &HI) {
1372 HI.Name = "xx";
1373 HI.Kind = index::SymbolKind::Field;
1374 HI.NamespaceScope = "";
1375 HI.Definition = "int xx";
1376 HI.LocalScope = "Foo::";
1377 HI.Type = "int";
1378 HI.AccessSpecifier = "public";
1379 }},
1380 {R"cpp(
1381 // error-ok
1382 struct Foo {
1383 Bar xx;
1384 int [[y^y]];
1385 };)cpp",
1386 [](HoverInfo &HI) {
1387 HI.Name = "yy";
1388 HI.Kind = index::SymbolKind::Field;
1389 HI.NamespaceScope = "";
1390 HI.Definition = "int yy";
1391 HI.LocalScope = "Foo::";
1392 HI.Type = "int";
1393 HI.AccessSpecifier = "public";
1394 }},
1395 {// No crash on InitListExpr.
1396 R"cpp(
1397 struct Foo {
1398 int a[10];
1399 };
1400 constexpr Foo k2 = {
1401 ^[[{]]1} // FIXME: why the hover range is 1 character?
1402 };
1403 )cpp",
1404 [](HoverInfo &HI) {
1405 HI.Name = "expression";
1406 HI.Kind = index::SymbolKind::Unknown;
1407 HI.Type = "int[10]";
1408 HI.Value = "{1}";
1409 }},
1410 {// Var template decl
1411 R"cpp(
1412 using m_int = int;
1413
1414 template <int Size> m_int ^[[arr]][Size];
1415 )cpp",
1416 [](HoverInfo &HI) {
1417 HI.Name = "arr";
1418 HI.Kind = index::SymbolKind::Variable;
1419 HI.Type = {"m_int[Size]", "int[Size]"};
1420 HI.NamespaceScope = "";
1421 HI.Definition = "template <int Size> m_int arr[Size]";
1422 HI.TemplateParameters = {{{"int"}, {"Size"}, std::nullopt}};
1423 }},
1424 {// Var template decl specialization
1425 R"cpp(
1426 using m_int = int;
1427
1428 template <int Size> m_int arr[Size];
1429
1430 template <> m_int ^[[arr]]<4>[4];
1431 )cpp",
1432 [](HoverInfo &HI) {
1433 HI.Name = "arr<4>";
1434 HI.Kind = index::SymbolKind::Variable;
1435 HI.Type = {"m_int[4]", "int[4]"};
1436 HI.NamespaceScope = "";
1437 HI.Definition = "m_int arr[4]";
1438 }},
1439 {// Canonical type
1440 R"cpp(
1441 template<typename T>
1442 struct TestHover {
1443 using Type = T;
1444 };
1445
1446 void code() {
1447 TestHover<int>::Type ^[[a]];
1448 }
1449 )cpp",
1450 [](HoverInfo &HI) {
1451 HI.Name = "a";
1452 HI.NamespaceScope = "";
1453 HI.LocalScope = "code::";
1454 HI.Definition = "TestHover<int>::Type a";
1455 HI.Kind = index::SymbolKind::Variable;
1456 HI.Type = {"TestHover<int>::Type", "int"};
1457 }},
1458 {// Canonical template type
1459 R"cpp(
1460 template<typename T>
1461 void ^[[foo]](T arg) {}
1462 )cpp",
1463 [](HoverInfo &HI) {
1464 HI.Name = "foo";
1465 HI.Kind = index::SymbolKind::Function;
1466 HI.NamespaceScope = "";
1467 HI.Definition = "template <typename T> void foo(T arg)";
1468 HI.Type = "void (T)";
1469 HI.ReturnType = "void";
1470 HI.Parameters = {{{"T"}, std::string("arg"), std::nullopt}};
1471 HI.TemplateParameters = {
1472 {{"typename"}, std::string("T"), std::nullopt}};
1473 }},
1474 {// TypeAlias Template
1475 R"cpp(
1476 template<typename T>
1477 using ^[[alias]] = T;
1478 )cpp",
1479 [](HoverInfo &HI) {
1480 HI.Name = "alias";
1481 HI.NamespaceScope = "";
1482 HI.LocalScope = "";
1483 HI.Kind = index::SymbolKind::TypeAlias;
1484 HI.Definition = "template <typename T> using alias = T";
1485 HI.Type = "T";
1486 HI.TemplateParameters = {
1487 {{"typename"}, std::string("T"), std::nullopt}};
1488 }},
1489 {// TypeAlias Template
1490 R"cpp(
1491 template<typename T>
1492 using A = T;
1493
1494 template<typename T>
1495 using ^[[AA]] = A<T>;
1496 )cpp",
1497 [](HoverInfo &HI) {
1498 HI.Name = "AA";
1499 HI.NamespaceScope = "";
1500 HI.LocalScope = "";
1501 HI.Kind = index::SymbolKind::TypeAlias;
1502 HI.Definition = "template <typename T> using AA = A<T>";
1503 HI.Type = {"A<T>", "T"};
1504 HI.TemplateParameters = {
1505 {{"typename"}, std::string("T"), std::nullopt}};
1506 }},
1507 {// Constant array
1508 R"cpp(
1509 using m_int = int;
1510
1511 m_int ^[[arr]][10];
1512 )cpp",
1513 [](HoverInfo &HI) {
1514 HI.Name = "arr";
1515 HI.NamespaceScope = "";
1516 HI.LocalScope = "";
1517 HI.Kind = index::SymbolKind::Variable;
1518 HI.Definition = "m_int arr[10]";
1519 HI.Type = {"m_int[10]", "int[10]"};
1520 }},
1521 {// Incomplete array
1522 R"cpp(
1523 using m_int = int;
1524
1525 extern m_int ^[[arr]][];
1526 )cpp",
1527 [](HoverInfo &HI) {
1528 HI.Name = "arr";
1529 HI.NamespaceScope = "";
1530 HI.LocalScope = "";
1531 HI.Kind = index::SymbolKind::Variable;
1532 HI.Definition = "extern m_int arr[]";
1533 HI.Type = {"m_int[]", "int[]"};
1534 }},
1535 {// Dependent size array
1536 R"cpp(
1537 using m_int = int;
1538
1539 template<int Size>
1540 struct Test {
1541 m_int ^[[arr]][Size];
1542 };
1543 )cpp",
1544 [](HoverInfo &HI) {
1545 HI.Name = "arr";
1546 HI.NamespaceScope = "";
1547 HI.LocalScope = "Test<Size>::";
1548 HI.AccessSpecifier = "public";
1549 HI.Kind = index::SymbolKind::Field;
1550 HI.Definition = "m_int arr[Size]";
1551 HI.Type = {"m_int[Size]", "int[Size]"};
1552 }},
1553 {// Bitfield offset, size and padding
1554 R"cpp(
1555 struct Foo {
1556 char x;
1557 char [[^y]] : 1;
1558 int z;
1559 };
1560 )cpp",
1561 [](HoverInfo &HI) {
1562 HI.NamespaceScope = "";
1563 HI.LocalScope = "Foo::";
1564 HI.Name = "y";
1565 HI.Kind = index::SymbolKind::Field;
1566 HI.Definition = "char y : 1";
1567 HI.Type = "char";
1568 HI.Offset = 8;
1569 HI.Size = 1;
1570 HI.Padding = 23;
1571 HI.Align = 8;
1572 HI.AccessSpecifier = "public";
1573 }}};
1574 for (const auto &Case : Cases) {
1575 SCOPED_TRACE(Case.Code);
1576
1577 Annotations T(Case.Code);
1578 TestTU TU = TestTU::withCode(T.code());
1579 TU.ExtraArgs.push_back("-std=c++20");
1580 // Types might be different depending on the target triplet, we chose a
1581 // fixed one to make sure tests passes on different platform.
1582 TU.ExtraArgs.push_back("--target=x86_64-pc-linux-gnu");
1583 auto AST = TU.build();
1584 Config Cfg;
1585 Cfg.Hover.ShowAKA = true;
1586 WithContextValue WithCfg(Config::Key, std::move(Cfg));
1587
1588 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
1589 ASSERT_TRUE(H);
1590 HoverInfo Expected;
1591 Expected.SymRange = T.range();
1592 Case.ExpectedBuilder(Expected);
1593
1594 EXPECT_EQ(H->NamespaceScope, Expected.NamespaceScope);
1595 EXPECT_EQ(H->LocalScope, Expected.LocalScope);
1596 EXPECT_EQ(H->Name, Expected.Name);
1597 EXPECT_EQ(H->Kind, Expected.Kind);
1598 EXPECT_EQ(H->Documentation, Expected.Documentation);
1599 EXPECT_EQ(H->Definition, Expected.Definition);
1600 EXPECT_EQ(H->Type, Expected.Type);
1601 EXPECT_EQ(H->ReturnType, Expected.ReturnType);
1602 EXPECT_EQ(H->Parameters, Expected.Parameters);
1603 EXPECT_EQ(H->TemplateParameters, Expected.TemplateParameters);
1604 EXPECT_EQ(H->SymRange, Expected.SymRange);
1605 EXPECT_EQ(H->Value, Expected.Value);
1606 EXPECT_EQ(H->Size, Expected.Size);
1607 EXPECT_EQ(H->Offset, Expected.Offset);
1608 EXPECT_EQ(H->Align, Expected.Align);
1609 EXPECT_EQ(H->AccessSpecifier, Expected.AccessSpecifier);
1610 EXPECT_EQ(H->CalleeArgInfo, Expected.CalleeArgInfo);
1611 EXPECT_EQ(H->CallPassType, Expected.CallPassType);
1612 }
1613}
1614
1615TEST(Hover, DefinitionLanuage) {
1616 struct {
1617 const char *const Code;
1618 const std::string ClangLanguageFlag;
1619 const char *const ExpectedDefinitionLanguage;
1620 } Cases[] = {{R"cpp(
1621 void [[some^Global]]() {}
1622 )cpp",
1623 "", "cpp"},
1624 {R"cpp(
1625 void [[some^Global]]() {}
1626 )cpp",
1627 "-xobjective-c++", "objective-cpp"},
1628 {R"cpp(
1629 void [[some^Global]]() {}
1630 )cpp",
1631 "-xobjective-c", "objective-c"}};
1632 for (const auto &Case : Cases) {
1633 SCOPED_TRACE(Case.Code);
1634
1635 Annotations T(Case.Code);
1636 TestTU TU = TestTU::withCode(T.code());
1637 if (!Case.ClangLanguageFlag.empty())
1638 TU.ExtraArgs.push_back(Case.ClangLanguageFlag);
1639 auto AST = TU.build();
1640
1641 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
1642 ASSERT_TRUE(H);
1643
1644 EXPECT_STREQ(H->DefinitionLanguage, Case.ExpectedDefinitionLanguage);
1645 }
1646}
1647
1648TEST(Hover, AccessSpecifierByLanguage) {
1649 struct {
1650 const char *const Code;
1651 const llvm::StringRef ClangLanguageFlag;
1652 const char *const ExpectedAccessSpecifier;
1653 } Cases[] = {
1654 {R"c(
1655 struct Waldo {
1656 int loca^tion;
1657 };
1658 )c",
1659 "-xc", ""},
1660 {R"cpp(
1661 struct Waldo {
1662 int loca^tion;
1663 };
1664 )cpp",
1665 "-xc++", "public"},
1666 {R"objc(
1667 struct Waldo {
1668 int loca^tion;
1669 };
1670 )objc",
1671 "-xobjective-c", ""},
1672 {R"objcpp(
1673 struct Waldo {
1674 int loca^tion;
1675 };
1676 )objcpp",
1677 "-xobjective-c++", "public"},
1678 // Hover currently does not retrieve ivar access specifiers in either
1679 // Objective-C or Objective-C++, so no label is expected.
1680 {R"objc(
1681 @interface Waldo {
1682 @public
1683 int loca^tion;
1684 }
1685 @end
1686 )objc",
1687 "-xobjective-c", ""},
1688 {R"objcpp(
1689 @interface Waldo {
1690 @public
1691 int loca^tion;
1692 }
1693 @end
1694 )objcpp",
1695 "-xobjective-c++", ""},
1696 };
1697 for (const auto &Case : Cases) {
1698 SCOPED_TRACE(Case.Code);
1699 SCOPED_TRACE(Case.ClangLanguageFlag);
1700
1701 Annotations T(Case.Code);
1702 TestTU TU = TestTU::withCode(T.code());
1703 if (!Case.ClangLanguageFlag.empty())
1704 TU.ExtraArgs.push_back(Case.ClangLanguageFlag.str());
1705 auto AST = TU.build();
1706
1707 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
1708 ASSERT_TRUE(H);
1709
1710 EXPECT_EQ(H->AccessSpecifier, Case.ExpectedAccessSpecifier);
1711 }
1712}
1713
1714TEST(Hover, CallPassType) {
1715 const llvm::StringRef CodePrefix = R"cpp(
1716class Base {};
1717class Derived : public Base {};
1718class CustomClass {
1719 public:
1720 CustomClass() {}
1721 CustomClass(const Base &x) {}
1722 CustomClass(int &x) {}
1723 CustomClass(float x) {}
1724 CustomClass(int x, int y) {}
1725};
1726
1727void int_by_ref(int &x) {}
1728void int_by_const_ref(const int &x) {}
1729void int_by_value(int x) {}
1730void base_by_ref(Base &x) {}
1731void base_by_const_ref(const Base &x) {}
1732void base_by_value(Base x) {}
1733void float_by_value(float x) {}
1734void custom_by_value(CustomClass x) {}
1735
1736void fun() {
1737 int int_x;
1738 int &int_ref = int_x;
1739 const int &int_const_ref = int_x;
1740 Base base;
1741 const Base &base_const_ref = base;
1742 Derived derived;
1743 float float_x;
1744)cpp";
1745 const llvm::StringRef CodeSuffix = "}";
1746
1747 struct {
1748 const char *const Code;
1750 bool Converted;
1751 } Tests[] = {
1752 // Integer tests
1753 {"int_by_value([[^int_x]]);", PassMode::Value, false},
1754 {"int_by_value([[^123]]);", PassMode::Value, false},
1755 {"int_by_ref([[^int_x]]);", PassMode::Ref, false},
1756 {"int_by_const_ref([[^int_x]]);", PassMode::ConstRef, false},
1757 {"int_by_const_ref([[^123]]);", PassMode::ConstRef, false},
1758 {"int_by_value([[^int_ref]]);", PassMode::Value, false},
1759 {"int_by_const_ref([[^int_ref]]);", PassMode::ConstRef, false},
1760 {"int_by_const_ref([[^int_ref]]);", PassMode::ConstRef, false},
1761 {"int_by_const_ref([[^int_const_ref]]);", PassMode::ConstRef, false},
1762 // Custom class tests
1763 {"base_by_ref([[^base]]);", PassMode::Ref, false},
1764 {"base_by_const_ref([[^base]]);", PassMode::ConstRef, false},
1765 {"base_by_const_ref([[^base_const_ref]]);", PassMode::ConstRef, false},
1766 {"base_by_value([[^base]]);", PassMode::Value, false},
1767 {"base_by_value([[^base_const_ref]]);", PassMode::Value, false},
1768 {"base_by_ref([[^derived]]);", PassMode::Ref, false},
1769 {"base_by_const_ref([[^derived]]);", PassMode::ConstRef, false},
1770 {"base_by_value([[^derived]]);", PassMode::Value, false},
1771 // Custom class constructor tests
1772 {"CustomClass c1([[^base]]);", PassMode::ConstRef, false},
1773 {"auto c2 = new CustomClass([[^base]]);", PassMode::ConstRef, false},
1774 {"CustomClass c3([[^int_x]]);", PassMode::Ref, false},
1775 {"CustomClass c3(int_x, [[^int_x]]);", PassMode::Value, false},
1776 // Converted tests
1777 {"float_by_value([[^int_x]]);", PassMode::Value, true},
1778 {"float_by_value([[^int_ref]]);", PassMode::Value, true},
1779 {"float_by_value([[^int_const_ref]]);", PassMode::Value, true},
1780 {"float_by_value([[^123.0f]]);", PassMode::Value, false},
1781 {"float_by_value([[^123]]);", PassMode::Value, true},
1782 {"custom_by_value([[^int_x]]);", PassMode::Ref, true},
1783 {"custom_by_value([[^float_x]]);", PassMode::Value, true},
1784 {"custom_by_value([[^base]]);", PassMode::ConstRef, true},
1785 };
1786 for (const auto &Test : Tests) {
1787 SCOPED_TRACE(Test.Code);
1788
1789 const auto Code = (CodePrefix + Test.Code + CodeSuffix).str();
1790 Annotations T(Code);
1791 TestTU TU = TestTU::withCode(T.code());
1792 TU.ExtraArgs.push_back("-std=c++17");
1793 auto AST = TU.build();
1794 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
1795 ASSERT_TRUE(H);
1796 EXPECT_EQ(H->CallPassType->PassBy, Test.PassBy);
1797 EXPECT_EQ(H->CallPassType->Converted, Test.Converted);
1798 }
1799}
1800
1801TEST(Hover, NoHover) {
1802 llvm::StringRef Tests[] = {
1803 "^int main() {}",
1804 "void foo() {^}",
1805 // FIXME: "decltype(auto)" should be a single hover
1806 "decltype(au^to) x = 0;",
1807 // FIXME: not supported yet
1808 R"cpp(// Lambda auto parameter
1809 auto lamb = [](a^uto){};
1810 )cpp",
1811 R"cpp(// non-named decls don't get hover. Don't crash!
1812 ^static_assert(1, "");
1813 )cpp",
1814 R"cpp(// non-evaluatable expr
1815 template <typename T> void foo() {
1816 (void)[[size^of]](T);
1817 })cpp",
1818 R"cpp(// should not crash on invalid semantic form of init-list-expr.
1819 /*error-ok*/
1820 struct Foo {
1821 int xyz = 0;
1822 };
1823 class Bar {};
1824 constexpr Foo s = ^{
1825 .xyz = Bar(),
1826 };
1827 )cpp",
1828 // literals
1829 "auto x = t^rue;",
1830 "auto x = ^(int){42};",
1831 "auto x = ^42.;",
1832 "auto x = ^42.0i;",
1833 "auto x = ^42;",
1834 "auto x = ^nullptr;",
1835 };
1836
1837 for (const auto &Test : Tests) {
1838 SCOPED_TRACE(Test);
1839
1840 Annotations T(Test);
1841 TestTU TU = TestTU::withCode(T.code());
1842 TU.ExtraArgs.push_back("-std=c++17");
1843 auto AST = TU.build();
1844 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
1845 ASSERT_FALSE(H);
1846 }
1847}
1848
1849TEST(Hover, OffsetOfBuiltin) {
1850 Annotations T(R"cpp(
1851 struct Foo { int x; };
1852 int y = __builtin_o^ffsetof(Foo, x);
1853 )cpp");
1854 auto AST = TestTU::withCode(T.code()).build();
1855 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
1856 ASSERT_TRUE(H);
1857 EXPECT_EQ(H->Name, "expression");
1858 EXPECT_EQ(H->Kind, index::SymbolKind::Unknown);
1859}
1860
1861TEST(Hover, All) {
1862 struct {
1863 const char *const Code;
1864 const std::function<void(HoverInfo &)> ExpectedBuilder;
1865 } Cases[] = {
1866 {"auto x = [['^A']]; // character literal",
1867 [](HoverInfo &HI) {
1868 HI.Name = "expression";
1869 HI.Type = "char";
1870 HI.Value = "65 (0x41)";
1871 }},
1872 {"auto s = ^[[\"Hello, world!\"]]; // string literal",
1873 [](HoverInfo &HI) {
1874 HI.Name = "string-literal";
1875 HI.Size = 112;
1876 HI.Type = "const char[14]";
1877 }},
1878 {
1879 R"cpp(// Local variable
1880 int main() {
1881 int bonjour;
1882 ^[[bonjour]] = 2;
1883 int test1 = bonjour;
1884 }
1885 )cpp",
1886 [](HoverInfo &HI) {
1887 HI.Name = "bonjour";
1888 HI.Kind = index::SymbolKind::Variable;
1889 HI.NamespaceScope = "";
1890 HI.LocalScope = "main::";
1891 HI.Type = "int";
1892 HI.Definition = "int bonjour";
1893 }},
1894 {
1895 R"cpp(// Local variable in method
1896 struct s {
1897 void method() {
1898 int bonjour;
1899 ^[[bonjour]] = 2;
1900 }
1901 };
1902 )cpp",
1903 [](HoverInfo &HI) {
1904 HI.Name = "bonjour";
1905 HI.Kind = index::SymbolKind::Variable;
1906 HI.NamespaceScope = "";
1907 HI.LocalScope = "s::method::";
1908 HI.Type = "int";
1909 HI.Definition = "int bonjour";
1910 }},
1911 {
1912 R"cpp(// Struct
1913 namespace ns1 {
1914 struct MyClass {};
1915 } // namespace ns1
1916 int main() {
1917 ns1::[[My^Class]]* Params;
1918 }
1919 )cpp",
1920 [](HoverInfo &HI) {
1921 HI.Name = "MyClass";
1922 HI.Kind = index::SymbolKind::Struct;
1923 HI.NamespaceScope = "ns1::";
1924 HI.Definition = "struct MyClass {}";
1925 }},
1926 {
1927 R"cpp(// Class
1928 namespace ns1 {
1929 class MyClass {};
1930 } // namespace ns1
1931 int main() {
1932 ns1::[[My^Class]]* Params;
1933 }
1934 )cpp",
1935 [](HoverInfo &HI) {
1936 HI.Name = "MyClass";
1937 HI.Kind = index::SymbolKind::Class;
1938 HI.NamespaceScope = "ns1::";
1939 HI.Definition = "class MyClass {}";
1940 }},
1941 {
1942 R"cpp(// Union
1943 namespace ns1 {
1944 union MyUnion { int x; int y; };
1945 } // namespace ns1
1946 int main() {
1947 ns1::[[My^Union]] Params;
1948 }
1949 )cpp",
1950 [](HoverInfo &HI) {
1951 HI.Name = "MyUnion";
1952 HI.Kind = index::SymbolKind::Union;
1953 HI.NamespaceScope = "ns1::";
1954 HI.Definition = "union MyUnion {}";
1955 }},
1956 {
1957 R"cpp(// Function definition via pointer
1958 void foo(int) {}
1959 int main() {
1960 auto *X = &^[[foo]];
1961 }
1962 )cpp",
1963 [](HoverInfo &HI) {
1964 HI.Name = "foo";
1965 HI.Kind = index::SymbolKind::Function;
1966 HI.NamespaceScope = "";
1967 HI.Type = "void (int)";
1968 HI.Definition = "void foo(int)";
1969 HI.Documentation = "Function definition via pointer";
1970 HI.ReturnType = "void";
1971 HI.Parameters = {
1972 {{"int"}, std::nullopt, std::nullopt},
1973 };
1974 }},
1975 {
1976 R"cpp(// Function declaration via call
1977 int foo(int);
1978 int main() {
1979 return ^[[foo]](42);
1980 }
1981 )cpp",
1982 [](HoverInfo &HI) {
1983 HI.Name = "foo";
1984 HI.Kind = index::SymbolKind::Function;
1985 HI.NamespaceScope = "";
1986 HI.Type = "int (int)";
1987 HI.Definition = "int foo(int)";
1988 HI.Documentation = "Function declaration via call";
1989 HI.ReturnType = "int";
1990 HI.Parameters = {
1991 {{"int"}, std::nullopt, std::nullopt},
1992 };
1993 }},
1994 {
1995 R"cpp(// Field
1996 struct Foo { int x; };
1997 int main() {
1998 Foo bar;
1999 (void)bar.^[[x]];
2000 }
2001 )cpp",
2002 [](HoverInfo &HI) {
2003 HI.Name = "x";
2004 HI.Kind = index::SymbolKind::Field;
2005 HI.NamespaceScope = "";
2006 HI.LocalScope = "Foo::";
2007 HI.Type = "int";
2008 HI.Definition = "int x";
2009 }},
2010 {
2011 R"cpp(// Field with initialization
2012 struct Foo { int x = 5; };
2013 int main() {
2014 Foo bar;
2015 (void)bar.^[[x]];
2016 }
2017 )cpp",
2018 [](HoverInfo &HI) {
2019 HI.Name = "x";
2020 HI.Kind = index::SymbolKind::Field;
2021 HI.NamespaceScope = "";
2022 HI.LocalScope = "Foo::";
2023 HI.Type = "int";
2024 HI.Definition = "int x = 5";
2025 }},
2026 {
2027 R"cpp(// Static field
2028 struct Foo { static int x; };
2029 int main() {
2030 (void)Foo::^[[x]];
2031 }
2032 )cpp",
2033 [](HoverInfo &HI) {
2034 HI.Name = "x";
2035 HI.Kind = index::SymbolKind::StaticProperty;
2036 HI.NamespaceScope = "";
2037 HI.LocalScope = "Foo::";
2038 HI.Type = "int";
2039 HI.Definition = "static int x";
2040 }},
2041 {
2042 R"cpp(// Field, member initializer
2043 struct Foo {
2044 int x;
2045 Foo() : ^[[x]](0) {}
2046 };
2047 )cpp",
2048 [](HoverInfo &HI) {
2049 HI.Name = "x";
2050 HI.Kind = index::SymbolKind::Field;
2051 HI.NamespaceScope = "";
2052 HI.LocalScope = "Foo::";
2053 HI.Type = "int";
2054 HI.Definition = "int x";
2055 }},
2056 {
2057 R"cpp(// Field, GNU old-style field designator
2058 struct Foo { int x; };
2059 int main() {
2060 Foo bar = { ^[[x]] : 1 };
2061 }
2062 )cpp",
2063 [](HoverInfo &HI) {
2064 HI.Name = "x";
2065 HI.Kind = index::SymbolKind::Field;
2066 HI.NamespaceScope = "";
2067 HI.LocalScope = "Foo::";
2068 HI.Type = "int";
2069 HI.Definition = "int x";
2070 // FIXME: Initializer for x is a DesignatedInitListExpr, hence it is
2071 // of struct type and omitted.
2072 }},
2073 {
2074 R"cpp(// Field, field designator
2075 struct Foo { int x; int y; };
2076 int main() {
2077 Foo bar = { .^[[x]] = 2, .y = 2 };
2078 }
2079 )cpp",
2080 [](HoverInfo &HI) {
2081 HI.Name = "x";
2082 HI.Kind = index::SymbolKind::Field;
2083 HI.NamespaceScope = "";
2084 HI.LocalScope = "Foo::";
2085 HI.Type = "int";
2086 HI.Definition = "int x";
2087 }},
2088 {
2089 R"cpp(// Field, offsetof
2090 struct Foo { int x; int y; };
2091 int z = __builtin_offsetof(Foo, ^[[x]]);
2092 )cpp",
2093 [](HoverInfo &HI) {
2094 HI.Name = "x";
2095 HI.Kind = index::SymbolKind::Field;
2096 HI.NamespaceScope = "";
2097 HI.LocalScope = "Foo::";
2098 HI.Type = "int";
2099 HI.Definition = "int x";
2100 HI.Value = "0";
2101 }},
2102 {
2103 R"cpp(// Outer field, nested offsetof designator
2104 struct Inner { int c; };
2105 struct A { Inner B; };
2106 int z = __builtin_offsetof(A, ^[[B]].c);
2107 )cpp",
2108 [](HoverInfo &HI) {
2109 HI.Name = "B";
2110 HI.Kind = index::SymbolKind::Field;
2111 HI.NamespaceScope = "";
2112 HI.LocalScope = "A::";
2113 HI.Type = "Inner";
2114 HI.Definition = "Inner B";
2115 HI.Value = "0";
2116 }},
2117 {
2118 R"cpp(// Inner field, nested offsetof designator
2119 struct Inner { int c; };
2120 struct A { Inner B; };
2121 int z = __builtin_offsetof(A, B.^[[c]]);
2122 )cpp",
2123 [](HoverInfo &HI) {
2124 HI.Name = "c";
2125 HI.Kind = index::SymbolKind::Field;
2126 HI.NamespaceScope = "";
2127 HI.LocalScope = "Inner::";
2128 HI.Type = "int";
2129 HI.Definition = "int c";
2130 HI.Value = "0";
2131 }},
2132 {
2133 R"cpp(// Method call
2134 struct Foo { int x(); };
2135 int main() {
2136 Foo bar;
2137 bar.^[[x]]();
2138 }
2139 )cpp",
2140 [](HoverInfo &HI) {
2141 HI.Name = "x";
2142 HI.Kind = index::SymbolKind::InstanceMethod;
2143 HI.NamespaceScope = "";
2144 HI.LocalScope = "Foo::";
2145 HI.Type = "int ()";
2146 HI.Definition = "int x()";
2147 HI.ReturnType = "int";
2148 HI.Parameters = std::vector<HoverInfo::Param>{};
2149 }},
2150 {
2151 R"cpp(// Static method call
2152 struct Foo { static int x(); };
2153 int main() {
2154 Foo::^[[x]]();
2155 }
2156 )cpp",
2157 [](HoverInfo &HI) {
2158 HI.Name = "x";
2159 HI.Kind = index::SymbolKind::StaticMethod;
2160 HI.NamespaceScope = "";
2161 HI.LocalScope = "Foo::";
2162 HI.Type = "int ()";
2163 HI.Definition = "static int x()";
2164 HI.ReturnType = "int";
2165 HI.Parameters = std::vector<HoverInfo::Param>{};
2166 }},
2167 {
2168 R"cpp(// Typedef
2169 typedef int Foo;
2170 int main() {
2171 ^[[Foo]] bar;
2172 }
2173 )cpp",
2174 [](HoverInfo &HI) {
2175 HI.Name = "Foo";
2176 HI.Kind = index::SymbolKind::TypeAlias;
2177 HI.NamespaceScope = "";
2178 HI.Definition = "typedef int Foo";
2179 HI.Type = "int";
2180 HI.Documentation = "Typedef";
2181 }},
2182 {
2183 R"cpp(// Typedef with embedded definition
2184 typedef struct Bar {} Foo;
2185 int main() {
2186 ^[[Foo]] bar;
2187 }
2188 )cpp",
2189 [](HoverInfo &HI) {
2190 HI.Name = "Foo";
2191 HI.Kind = index::SymbolKind::TypeAlias;
2192 HI.NamespaceScope = "";
2193 HI.Definition = "typedef struct Bar Foo";
2194 HI.Type = "struct Bar";
2195 HI.Documentation = "Typedef with embedded definition";
2196 }},
2197 {
2198 R"cpp(// Namespace
2199 namespace ns {
2200 struct Foo { static void bar(); };
2201 } // namespace ns
2202 int main() { ^[[ns]]::Foo::bar(); }
2203 )cpp",
2204 [](HoverInfo &HI) {
2205 HI.Name = "ns";
2206 HI.Kind = index::SymbolKind::Namespace;
2207 HI.NamespaceScope = "";
2208 HI.Definition = "namespace ns {}";
2209 }},
2210 {
2211 R"cpp(// Anonymous namespace
2212 namespace ns {
2213 namespace {
2214 int foo;
2215 } // anonymous namespace
2216 } // namespace ns
2217 int main() { ns::[[f^oo]]++; }
2218 )cpp",
2219 [](HoverInfo &HI) {
2220 HI.Name = "foo";
2221 HI.Kind = index::SymbolKind::Variable;
2222 HI.NamespaceScope = "ns::";
2223 HI.Type = "int";
2224 HI.Definition = "int foo";
2225 }},
2226 {
2227 R"cpp(// Function definition via using declaration
2228 namespace ns {
2229 void foo();
2230 }
2231 int main() {
2232 using ns::foo;
2233 ^[[foo]]();
2234 }
2235 )cpp",
2236 [](HoverInfo &HI) {
2237 HI.Name = "foo";
2238 HI.Kind = index::SymbolKind::Function;
2239 HI.NamespaceScope = "ns::";
2240 HI.Type = "void ()";
2241 HI.Definition = "void foo()";
2242 HI.Documentation = "";
2243 HI.ReturnType = "void";
2244 HI.Parameters = std::vector<HoverInfo::Param>{};
2245 }},
2246 {
2247 R"cpp( // using declaration and two possible function declarations
2248 namespace ns { void foo(int); void foo(char); }
2249 using ns::foo;
2250 template <typename T> void bar() { [[f^oo]](T{}); }
2251 )cpp",
2252 [](HoverInfo &HI) {
2253 HI.Name = "foo";
2254 HI.Kind = index::SymbolKind::Using;
2255 HI.NamespaceScope = "";
2256 HI.Definition = "using ns::foo";
2257 }},
2258 {
2259 R"cpp(// Macro
2260 #define MACRO 0
2261 int main() { return ^[[MACRO]]; }
2262 )cpp",
2263 [](HoverInfo &HI) {
2264 HI.Name = "MACRO";
2265 HI.Value = "0";
2266 HI.Type = "int";
2267 HI.Kind = index::SymbolKind::Macro;
2268 HI.Definition = "#define MACRO 0\n\n"
2269 "// Expands to\n"
2270 "0";
2271 }},
2272 {
2273 R"cpp(// Macro
2274 #define MACRO 0
2275 #define MACRO2 ^[[MACRO]]
2276 )cpp",
2277 [](HoverInfo &HI) {
2278 HI.Name = "MACRO";
2279 HI.Kind = index::SymbolKind::Macro;
2280 HI.Definition = "#define MACRO 0";
2281 // NOTE MACRO doesn't have expansion since it technically isn't
2282 // expanded here
2283 }},
2284 {
2285 R"cpp(// Macro
2286 #define MACRO {\
2287 return 0;\
2288 }
2289 int main() ^[[MACRO]]
2290 )cpp",
2291 [](HoverInfo &HI) {
2292 HI.Name = "MACRO";
2293 HI.Kind = index::SymbolKind::Macro;
2294 HI.Definition =
2295 R"cpp(#define MACRO \
2296 { \
2297 return 0; \
2298 }
2299
2300// Expands to
2301{
2302 return 0;
2303})cpp";
2304 }},
2305 {
2306 R"cpp(// Forward class declaration
2307 class Foo;
2308 class Foo {};
2309 [[F^oo]]* foo();
2310 )cpp",
2311 [](HoverInfo &HI) {
2312 HI.Name = "Foo";
2313 HI.Kind = index::SymbolKind::Class;
2314 HI.NamespaceScope = "";
2315 HI.Definition = "class Foo {}";
2316 HI.Documentation = "Forward class declaration";
2317 }},
2318 {
2319 R"cpp(// Function declaration
2320 void foo();
2321 void g() { [[f^oo]](); }
2322 void foo() {}
2323 )cpp",
2324 [](HoverInfo &HI) {
2325 HI.Name = "foo";
2326 HI.Kind = index::SymbolKind::Function;
2327 HI.NamespaceScope = "";
2328 HI.Type = "void ()";
2329 HI.Definition = "void foo()";
2330 HI.Documentation = "Function declaration";
2331 HI.ReturnType = "void";
2332 HI.Parameters = std::vector<HoverInfo::Param>{};
2333 }},
2334 {
2335 R"cpp(// Enum declaration
2336 enum Hello {
2337 ONE, TWO, THREE,
2338 };
2339 void foo() {
2340 [[Hel^lo]] hello = ONE;
2341 }
2342 )cpp",
2343 [](HoverInfo &HI) {
2344 HI.Name = "Hello";
2345 HI.Kind = index::SymbolKind::Enum;
2346 HI.NamespaceScope = "";
2347 HI.Definition = "enum Hello {}";
2348 HI.Documentation = "Enum declaration";
2349 }},
2350 {
2351 R"cpp(// Enumerator
2352 enum Hello {
2353 ONE, TWO, THREE,
2354 };
2355 void foo() {
2356 Hello hello = [[O^NE]];
2357 }
2358 )cpp",
2359 [](HoverInfo &HI) {
2360 HI.Name = "ONE";
2361 HI.Kind = index::SymbolKind::EnumConstant;
2362 HI.NamespaceScope = "";
2363 HI.LocalScope = "Hello::";
2364 HI.Type = "enum Hello";
2365 HI.Definition = "ONE";
2366 HI.Value = "0";
2367 }},
2368 {
2369 R"cpp(// C++20's using enum
2370 enum class Hello {
2371 ONE, TWO, THREE,
2372 };
2373 void foo() {
2374 using enum Hello;
2375 Hello hello = [[O^NE]];
2376 }
2377 )cpp",
2378 [](HoverInfo &HI) {
2379 HI.Name = "ONE";
2380 HI.Kind = index::SymbolKind::EnumConstant;
2381 HI.NamespaceScope = "";
2382 HI.LocalScope = "Hello::";
2383 HI.Type = "enum Hello";
2384 HI.Definition = "ONE";
2385 HI.Value = "0";
2386 }},
2387 {
2388 R"cpp(// Enumerator in anonymous enum
2389 enum {
2390 ONE, TWO, THREE,
2391 };
2392 void foo() {
2393 int hello = [[O^NE]];
2394 }
2395 )cpp",
2396 [](HoverInfo &HI) {
2397 HI.Name = "ONE";
2398 HI.Kind = index::SymbolKind::EnumConstant;
2399 HI.NamespaceScope = "";
2400 // FIXME: This should be `(anon enum)::`
2401 HI.LocalScope = "";
2402 HI.Type = "enum (unnamed)";
2403 HI.Definition = "ONE";
2404 HI.Value = "0";
2405 }},
2406 {
2407 R"cpp(// Global variable
2408 static int hey = 10;
2409 void foo() {
2410 [[he^y]]++;
2411 }
2412 )cpp",
2413 [](HoverInfo &HI) {
2414 HI.Name = "hey";
2415 HI.Kind = index::SymbolKind::Variable;
2416 HI.NamespaceScope = "";
2417 HI.Type = "int";
2418 HI.Definition = "static int hey = 10";
2419 HI.Documentation = "Global variable";
2420 // FIXME: Value shouldn't be set in this case
2421 HI.Value = "10 (0xa)";
2422 }},
2423 {
2424 R"cpp(// Global variable in namespace
2425 namespace ns1 {
2426 static long long hey = -36637162602497;
2427 }
2428 void foo() {
2429 ns1::[[he^y]]++;
2430 }
2431 )cpp",
2432 [](HoverInfo &HI) {
2433 HI.Name = "hey";
2434 HI.Kind = index::SymbolKind::Variable;
2435 HI.NamespaceScope = "ns1::";
2436 HI.Type = "long long";
2437 HI.Definition = "static long long hey = -36637162602497";
2438 HI.Value = "-36637162602497 (0xffffdeadbeefffff)"; // needs 64 bits
2439 }},
2440 {
2441 R"cpp(// Field in anonymous struct
2442 static struct {
2443 int hello;
2444 } s;
2445 void foo() {
2446 s.[[he^llo]]++;
2447 }
2448 )cpp",
2449 [](HoverInfo &HI) {
2450 HI.Name = "hello";
2451 HI.Kind = index::SymbolKind::Field;
2452 HI.NamespaceScope = "";
2453 HI.LocalScope = "(anonymous struct)::";
2454 HI.Type = "int";
2455 HI.Definition = "int hello";
2456 }},
2457 {
2458 R"cpp(// Templated function
2459 template <typename T>
2460 T foo() {
2461 return 17;
2462 }
2463 void g() { auto x = [[f^oo]]<int>(); }
2464 )cpp",
2465 [](HoverInfo &HI) {
2466 HI.Name = "foo";
2467 HI.Kind = index::SymbolKind::Function;
2468 HI.NamespaceScope = "";
2469 HI.Type = "int ()";
2470 HI.Definition = "template <> int foo<int>()";
2471 HI.Documentation = "Templated function";
2472 HI.ReturnType = "int";
2473 HI.Parameters = std::vector<HoverInfo::Param>{};
2474 // FIXME: We should populate template parameters with arguments in
2475 // case of instantiations.
2476 }},
2477 {
2478 R"cpp(// Anonymous union
2479 struct outer {
2480 union {
2481 int abc, def;
2482 } v;
2483 };
2484 void g() { struct outer o; o.v.[[d^ef]]++; }
2485 )cpp",
2486 [](HoverInfo &HI) {
2487 HI.Name = "def";
2488 HI.Kind = index::SymbolKind::Field;
2489 HI.NamespaceScope = "";
2490 HI.LocalScope = "outer::(anonymous union)::";
2491 HI.Type = "int";
2492 HI.Definition = "int def";
2493 }},
2494 {
2495 R"cpp(// documentation from index
2496 int nextSymbolIsAForwardDeclFromIndexWithNoLocalDocs;
2497 void indexSymbol();
2498 void g() { [[ind^exSymbol]](); }
2499 )cpp",
2500 [](HoverInfo &HI) {
2501 HI.Name = "indexSymbol";
2502 HI.Kind = index::SymbolKind::Function;
2503 HI.NamespaceScope = "";
2504 HI.Type = "void ()";
2505 HI.Definition = "void indexSymbol()";
2506 HI.ReturnType = "void";
2507 HI.Parameters = std::vector<HoverInfo::Param>{};
2508 HI.Documentation = "comment from index";
2509 }},
2510 {
2511 R"cpp(// Simple initialization with auto
2512 void foo() {
2513 ^[[auto]] i = 1;
2514 }
2515 )cpp",
2516 [](HoverInfo &HI) {
2517 HI.Name = "auto";
2518 HI.Kind = index::SymbolKind::TypeAlias;
2519 HI.Definition = "int";
2520 }},
2521 {
2522 R"cpp(// Simple initialization with const auto
2523 void foo() {
2524 const ^[[auto]] i = 1;
2525 }
2526 )cpp",
2527 [](HoverInfo &HI) {
2528 HI.Name = "auto";
2529 HI.Kind = index::SymbolKind::TypeAlias;
2530 HI.Definition = "int";
2531 }},
2532 {
2533 R"cpp(// Simple initialization with const auto&
2534 void foo() {
2535 const ^[[auto]]& i = 1;
2536 }
2537 )cpp",
2538 [](HoverInfo &HI) {
2539 HI.Name = "auto";
2540 HI.Kind = index::SymbolKind::TypeAlias;
2541 HI.Definition = "int";
2542 }},
2543 {
2544 R"cpp(// Simple initialization with auto&
2545 void foo() {
2546 int x;
2547 ^[[auto]]& i = x;
2548 }
2549 )cpp",
2550 [](HoverInfo &HI) {
2551 HI.Name = "auto";
2552 HI.Kind = index::SymbolKind::TypeAlias;
2553 HI.Definition = "int";
2554 }},
2555 {
2556 R"cpp(// Simple initialization with auto*
2557 void foo() {
2558 int a = 1;
2559 ^[[auto]]* i = &a;
2560 }
2561 )cpp",
2562 [](HoverInfo &HI) {
2563 HI.Name = "auto";
2564 HI.Kind = index::SymbolKind::TypeAlias;
2565 HI.Definition = "int";
2566 }},
2567 {
2568 R"cpp(// Simple initialization with auto from pointer
2569 void foo() {
2570 int a = 1;
2571 ^[[auto]] i = &a;
2572 }
2573 )cpp",
2574 [](HoverInfo &HI) {
2575 HI.Name = "auto";
2576 HI.Kind = index::SymbolKind::TypeAlias;
2577 HI.Definition = "int *";
2578 }},
2579 {
2580 R"cpp(// Auto with initializer list.
2581 namespace std
2582 {
2583 template<class _E>
2584 class initializer_list { const _E *a, *b; };
2585 }
2586 void foo() {
2587 ^[[auto]] i = {1,2};
2588 }
2589 )cpp",
2590 [](HoverInfo &HI) {
2591 HI.Name = "auto";
2592 HI.Kind = index::SymbolKind::TypeAlias;
2593 HI.Definition = "std::initializer_list<int>";
2594 }},
2595 {
2596 R"cpp(// User defined conversion to auto
2597 struct Bar {
2598 operator ^[[auto]]() const { return 10; }
2599 };
2600 )cpp",
2601 [](HoverInfo &HI) {
2602 HI.Name = "auto";
2603 HI.Kind = index::SymbolKind::TypeAlias;
2604 HI.Definition = "int";
2605 }},
2606 {
2607 R"cpp(// Simple initialization with decltype(auto)
2608 void foo() {
2609 ^[[decltype]](auto) i = 1;
2610 }
2611 )cpp",
2612 [](HoverInfo &HI) {
2613 HI.Name = "decltype";
2614 HI.Kind = index::SymbolKind::TypeAlias;
2615 HI.Definition = "int";
2616 }},
2617 {
2618 R"cpp(// Simple initialization with const decltype(auto)
2619 void foo() {
2620 const int j = 0;
2621 ^[[decltype]](auto) i = j;
2622 }
2623 )cpp",
2624 [](HoverInfo &HI) {
2625 HI.Name = "decltype";
2626 HI.Kind = index::SymbolKind::TypeAlias;
2627 HI.Definition = "const int";
2628 }},
2629 {
2630 R"cpp(// Simple initialization with const& decltype(auto)
2631 void foo() {
2632 int k = 0;
2633 const int& j = k;
2634 ^[[decltype]](auto) i = j;
2635 }
2636 )cpp",
2637 [](HoverInfo &HI) {
2638 HI.Name = "decltype";
2639 HI.Kind = index::SymbolKind::TypeAlias;
2640 HI.Definition = "const int &";
2641 }},
2642 {
2643 R"cpp(// Simple initialization with & decltype(auto)
2644 void foo() {
2645 int k = 0;
2646 int& j = k;
2647 ^[[decltype]](auto) i = j;
2648 }
2649 )cpp",
2650 [](HoverInfo &HI) {
2651 HI.Name = "decltype";
2652 HI.Kind = index::SymbolKind::TypeAlias;
2653 HI.Definition = "int &";
2654 }},
2655 {
2656 R"cpp(// simple trailing return type
2657 ^[[auto]] main() -> int {
2658 return 0;
2659 }
2660 )cpp",
2661 [](HoverInfo &HI) {
2662 HI.Name = "auto";
2663 HI.Kind = index::SymbolKind::TypeAlias;
2664 HI.Definition = "int";
2665 }},
2666 {
2667 R"cpp(// auto function return with trailing type
2668 struct Bar {};
2669 ^[[auto]] test() -> decltype(Bar()) {
2670 return Bar();
2671 }
2672 )cpp",
2673 [](HoverInfo &HI) {
2674 HI.Name = "auto";
2675 HI.Kind = index::SymbolKind::TypeAlias;
2676 HI.Definition = "Bar";
2677 HI.Documentation = "auto function return with trailing type";
2678 }},
2679 {
2680 R"cpp(// trailing return type
2681 struct Bar {};
2682 auto test() -> ^[[decltype]](Bar()) {
2683 return Bar();
2684 }
2685 )cpp",
2686 [](HoverInfo &HI) {
2687 HI.Name = "decltype";
2688 HI.Kind = index::SymbolKind::TypeAlias;
2689 HI.Definition = "Bar";
2690 HI.Documentation = "trailing return type";
2691 }},
2692 {
2693 R"cpp(// auto in function return
2694 struct Bar {};
2695 ^[[auto]] test() {
2696 return Bar();
2697 }
2698 )cpp",
2699 [](HoverInfo &HI) {
2700 HI.Name = "auto";
2701 HI.Kind = index::SymbolKind::TypeAlias;
2702 HI.Definition = "Bar";
2703 HI.Documentation = "auto in function return";
2704 }},
2705 {
2706 R"cpp(// auto& in function return
2707 struct Bar {};
2708 ^[[auto]]& test() {
2709 static Bar x;
2710 return x;
2711 }
2712 )cpp",
2713 [](HoverInfo &HI) {
2714 HI.Name = "auto";
2715 HI.Kind = index::SymbolKind::TypeAlias;
2716 HI.Definition = "Bar";
2717 HI.Documentation = "auto& in function return";
2718 }},
2719 {
2720 R"cpp(// auto* in function return
2721 struct Bar {};
2722 ^[[auto]]* test() {
2723 Bar* bar;
2724 return bar;
2725 }
2726 )cpp",
2727 [](HoverInfo &HI) {
2728 HI.Name = "auto";
2729 HI.Kind = index::SymbolKind::TypeAlias;
2730 HI.Definition = "Bar";
2731 HI.Documentation = "auto* in function return";
2732 }},
2733 {
2734 R"cpp(// const auto& in function return
2735 struct Bar {};
2736 const ^[[auto]]& test() {
2737 static Bar x;
2738 return x;
2739 }
2740 )cpp",
2741 [](HoverInfo &HI) {
2742 HI.Name = "auto";
2743 HI.Kind = index::SymbolKind::TypeAlias;
2744 HI.Definition = "Bar";
2745 HI.Documentation = "const auto& in function return";
2746 }},
2747 {
2748 R"cpp(// decltype(auto) in function return
2749 struct Bar {};
2750 ^[[decltype]](auto) test() {
2751 return Bar();
2752 }
2753 )cpp",
2754 [](HoverInfo &HI) {
2755 HI.Name = "decltype";
2756 HI.Kind = index::SymbolKind::TypeAlias;
2757 HI.Definition = "Bar";
2758 HI.Documentation = "decltype(auto) in function return";
2759 }},
2760 {
2761 R"cpp(// decltype(auto) reference in function return
2762 ^[[decltype]](auto) test() {
2763 static int a;
2764 return (a);
2765 }
2766 )cpp",
2767 [](HoverInfo &HI) {
2768 HI.Name = "decltype";
2769 HI.Kind = index::SymbolKind::TypeAlias;
2770 HI.Definition = "int &";
2771 }},
2772 {
2773 R"cpp(// decltype lvalue reference
2774 void foo() {
2775 int I = 0;
2776 ^[[decltype]](I) J = I;
2777 }
2778 )cpp",
2779 [](HoverInfo &HI) {
2780 HI.Name = "decltype";
2781 HI.Kind = index::SymbolKind::TypeAlias;
2782 HI.Definition = "int";
2783 }},
2784 {
2785 R"cpp(// decltype lvalue reference
2786 void foo() {
2787 int I= 0;
2788 int &K = I;
2789 ^[[decltype]](K) J = I;
2790 }
2791 )cpp",
2792 [](HoverInfo &HI) {
2793 HI.Name = "decltype";
2794 HI.Kind = index::SymbolKind::TypeAlias;
2795 HI.Definition = "int &";
2796 }},
2797 {
2798 R"cpp(// decltype lvalue reference parenthesis
2799 void foo() {
2800 int I = 0;
2801 ^[[decltype]]((I)) J = I;
2802 }
2803 )cpp",
2804 [](HoverInfo &HI) {
2805 HI.Name = "decltype";
2806 HI.Kind = index::SymbolKind::TypeAlias;
2807 HI.Definition = "int &";
2808 }},
2809 {
2810 R"cpp(// decltype rvalue reference
2811 void foo() {
2812 int I = 0;
2813 ^[[decltype]](static_cast<int&&>(I)) J = static_cast<int&&>(I);
2814 }
2815 )cpp",
2816 [](HoverInfo &HI) {
2817 HI.Name = "decltype";
2818 HI.Kind = index::SymbolKind::TypeAlias;
2819 HI.Definition = "int &&";
2820 }},
2821 {
2822 R"cpp(// decltype rvalue reference function call
2823 int && bar();
2824 void foo() {
2825 int I = 0;
2826 ^[[decltype]](bar()) J = bar();
2827 }
2828 )cpp",
2829 [](HoverInfo &HI) {
2830 HI.Name = "decltype";
2831 HI.Kind = index::SymbolKind::TypeAlias;
2832 HI.Definition = "int &&";
2833 }},
2834 {
2835 R"cpp(// decltype of function with trailing return type.
2836 struct Bar {};
2837 auto test() -> decltype(Bar()) {
2838 return Bar();
2839 }
2840 void foo() {
2841 ^[[decltype]](test()) i = test();
2842 }
2843 )cpp",
2844 [](HoverInfo &HI) {
2845 HI.Name = "decltype";
2846 HI.Kind = index::SymbolKind::TypeAlias;
2847 HI.Definition = "Bar";
2848 HI.Documentation =
2849 "decltype of function with trailing return type.";
2850 }},
2851 {
2852 R"cpp(// decltype of var with decltype.
2853 void foo() {
2854 int I = 0;
2855 decltype(I) J = I;
2856 ^[[decltype]](J) K = J;
2857 }
2858 )cpp",
2859 [](HoverInfo &HI) {
2860 HI.Name = "decltype";
2861 HI.Kind = index::SymbolKind::TypeAlias;
2862 HI.Definition = "int";
2863 }},
2864 {
2865 R"cpp(// decltype of dependent type
2866 template <typename T>
2867 struct X {
2868 using Y = ^[[decltype]](T::Z);
2869 };
2870 )cpp",
2871 [](HoverInfo &HI) {
2872 HI.Name = "decltype";
2873 HI.Kind = index::SymbolKind::TypeAlias;
2874 HI.Definition = "<dependent type>";
2875 }},
2876 {
2877 R"cpp(// More complicated structured types.
2878 int bar();
2879 ^[[auto]] (*foo)() = bar;
2880 )cpp",
2881 [](HoverInfo &HI) {
2882 HI.Name = "auto";
2883 HI.Kind = index::SymbolKind::TypeAlias;
2884 HI.Definition = "int";
2885 }},
2886 {
2887 R"cpp(// Should not crash when evaluating the initializer.
2888 struct Test {};
2889 void test() { Test && [[te^st]] = {}; }
2890 )cpp",
2891 [](HoverInfo &HI) {
2892 HI.Name = "test";
2893 HI.Kind = index::SymbolKind::Variable;
2894 HI.NamespaceScope = "";
2895 HI.LocalScope = "test::";
2896 HI.Type = "Test &&";
2897 HI.Definition = "Test &&test = {}";
2898 }},
2899 {
2900 R"cpp(// Shouldn't crash when evaluating the initializer.
2901 struct Bar {}; // error-ok
2902 struct Foo { void foo(Bar x = y); }
2903 void Foo::foo(Bar [[^x]]) {})cpp",
2904 [](HoverInfo &HI) {
2905 HI.Name = "x";
2906 HI.Kind = index::SymbolKind::Parameter;
2907 HI.NamespaceScope = "";
2908 HI.LocalScope = "Foo::foo::";
2909 HI.Type = "Bar";
2910 HI.Definition = "Bar x = <recovery - expr>()";
2911 }},
2912 {
2913 R"cpp(// auto on alias
2914 typedef int int_type;
2915 ^[[auto]] x = int_type();
2916 )cpp",
2917 [](HoverInfo &HI) {
2918 HI.Name = "auto";
2919 HI.Kind = index::SymbolKind::TypeAlias;
2920 HI.Definition = "int_type // aka: int";
2921 }},
2922 {
2923 R"cpp(// auto on alias
2924 struct cls {};
2925 typedef cls cls_type;
2926 ^[[auto]] y = cls_type();
2927 )cpp",
2928 [](HoverInfo &HI) {
2929 HI.Name = "auto";
2930 HI.Kind = index::SymbolKind::TypeAlias;
2931 HI.Definition = "cls_type // aka: cls";
2932 HI.Documentation = "auto on alias";
2933 }},
2934 {
2935 R"cpp(// auto on alias
2936 template <class>
2937 struct templ {};
2938 ^[[auto]] z = templ<int>();
2939 )cpp",
2940 [](HoverInfo &HI) {
2941 HI.Name = "auto";
2942 HI.Kind = index::SymbolKind::TypeAlias;
2943 HI.Definition = "templ<int>";
2944 HI.Documentation = "auto on alias";
2945 }},
2946 {
2947 R"cpp(// Undeduced auto declaration
2948 template<typename T>
2949 void foo() {
2950 ^[[auto]] x = T();
2951 }
2952 )cpp",
2953 [](HoverInfo &HI) {
2954 HI.Name = "auto";
2955 HI.Kind = index::SymbolKind::TypeAlias;
2956 HI.Definition = "T";
2957 }},
2958 {
2959 R"cpp(// Undeduced auto return type
2960 template<typename T>
2961 ^[[auto]] foo() {
2962 return T();
2963 }
2964 )cpp",
2965 [](HoverInfo &HI) {
2966 HI.Name = "auto";
2967 HI.Kind = index::SymbolKind::TypeAlias;
2968 HI.Definition = "/* not deduced */";
2969 }},
2970 {
2971 R"cpp(// Template auto parameter
2972 template<[[a^uto]] T>
2973 void func() {
2974 }
2975 )cpp",
2976 [](HoverInfo &HI) {
2977 // FIXME: not sure this is what we want, but this
2978 // is what we currently get with getDeducedType
2979 HI.Name = "auto";
2980 HI.Kind = index::SymbolKind::TypeAlias;
2981 HI.Definition = "/* not deduced */";
2982 }},
2983 {
2984 R"cpp(// Undeduced decltype(auto) return type
2985 template<typename T>
2986 ^[[decltype]](auto) foo() {
2987 return T();
2988 }
2989 )cpp",
2990 [](HoverInfo &HI) {
2991 HI.Name = "decltype";
2992 HI.Kind = index::SymbolKind::TypeAlias;
2993 HI.Definition = "/* not deduced */";
2994 }},
2995 {
2996 R"cpp(// should not crash.
2997 template <class T> struct cls {
2998 int method();
2999 };
3000
3001 auto test = cls<int>().[[m^ethod]]();
3002 )cpp",
3003 [](HoverInfo &HI) {
3004 HI.Definition = "int method()";
3005 HI.Kind = index::SymbolKind::InstanceMethod;
3006 HI.NamespaceScope = "";
3007 HI.LocalScope = "cls<int>::";
3008 HI.Name = "method";
3009 HI.Parameters.emplace();
3010 HI.ReturnType = "int";
3011 HI.Type = "int ()";
3012 }},
3013 {
3014 R"cpp(// type of nested templates.
3015 template <class T> struct cls {};
3016 cls<cls<cls<int>>> [[fo^o]];
3017 )cpp",
3018 [](HoverInfo &HI) {
3019 HI.Definition = "cls<cls<cls<int>>> foo";
3020 HI.Kind = index::SymbolKind::Variable;
3021 HI.NamespaceScope = "";
3022 HI.Name = "foo";
3023 HI.Type = "cls<cls<cls<int>>>";
3024 }},
3025 {
3026 R"cpp(// type of nested templates.
3027 template <class T> struct cls {};
3028 [[cl^s]]<cls<cls<int>>> foo;
3029 )cpp",
3030 [](HoverInfo &HI) {
3031 HI.Definition = "template <> struct cls<cls<cls<int>>> {}";
3032 HI.Kind = index::SymbolKind::Struct;
3033 HI.NamespaceScope = "";
3034 HI.Name = "cls<cls<cls<int>>>";
3035 HI.Documentation = "type of nested templates.";
3036 }},
3037 {
3038 R"cpp(// type with decltype
3039 int a;
3040 decltype(a) [[b^]] = a;)cpp",
3041 [](HoverInfo &HI) {
3042 HI.Definition = "decltype(a) b = a";
3043 HI.Kind = index::SymbolKind::Variable;
3044 HI.NamespaceScope = "";
3045 HI.Name = "b";
3046 HI.Type = "int";
3047 }},
3048 {
3049 R"cpp(// type with decltype
3050 int a;
3051 decltype(a) c;
3052 decltype(c) [[b^]] = a;)cpp",
3053 [](HoverInfo &HI) {
3054 HI.Definition = "decltype(c) b = a";
3055 HI.Kind = index::SymbolKind::Variable;
3056 HI.NamespaceScope = "";
3057 HI.Name = "b";
3058 HI.Type = "int";
3059 }},
3060 {
3061 R"cpp(// type with decltype
3062 int a;
3063 const decltype(a) [[b^]] = a;)cpp",
3064 [](HoverInfo &HI) {
3065 HI.Definition = "const decltype(a) b = a";
3066 HI.Kind = index::SymbolKind::Variable;
3067 HI.NamespaceScope = "";
3068 HI.Name = "b";
3069 HI.Type = "const int";
3070 }},
3071 {
3072 R"cpp(// type with decltype
3073 int a;
3074 auto [[f^oo]](decltype(a) x) -> decltype(a) { return 0; })cpp",
3075 [](HoverInfo &HI) {
3076 HI.Definition = "auto foo(decltype(a) x) -> decltype(a)";
3077 HI.Kind = index::SymbolKind::Function;
3078 HI.NamespaceScope = "";
3079 HI.Name = "foo";
3080 HI.Type = "auto (int) -> int";
3081 HI.ReturnType = "int";
3082 HI.Parameters = {{{"int"}, std::string("x"), std::nullopt}};
3083 }},
3084 {
3085 R"cpp(// sizeof expr
3086 void foo() {
3087 (void)[[size^of]](char);
3088 })cpp",
3089 [](HoverInfo &HI) {
3090 HI.Name = "expression";
3091 HI.Type = {"__size_t", "unsigned long"};
3092 HI.Value = "1";
3093 }},
3094 {
3095 R"cpp(// alignof expr
3096 void foo() {
3097 (void)[[align^of]](char);
3098 })cpp",
3099 [](HoverInfo &HI) {
3100 HI.Name = "expression";
3101 HI.Type = {"__size_t", "unsigned long"};
3102 HI.Value = "1";
3103 }},
3104 {
3105 R"cpp(
3106 template <typename T = int>
3107 void foo(const T& = T()) {
3108 [[f^oo]]<>(3);
3109 })cpp",
3110 [](HoverInfo &HI) {
3111 HI.Name = "foo";
3112 HI.Kind = index::SymbolKind::Function;
3113 HI.Type = "void (const int &)";
3114 HI.ReturnType = "void";
3115 HI.Parameters = {
3116 {{"const int &"}, std::nullopt, std::string("T()")}};
3117 HI.Definition = "template <> void foo<int>(const int &)";
3118 HI.NamespaceScope = "";
3119 }},
3120 {
3121 R"cpp(// should not crash
3122 @interface ObjC {
3123 char [[da^ta]];
3124 }@end
3125 )cpp",
3126 [](HoverInfo &HI) {
3127 HI.Name = "data";
3128 HI.Type = "char";
3129 HI.Kind = index::SymbolKind::Field;
3130 HI.LocalScope = "ObjC::";
3131 HI.NamespaceScope = "";
3132 HI.Definition = "char data";
3133 }},
3134 {
3135 R"cpp(
3136 @interface MYObject
3137 @end
3138 @interface Interface
3139 @property(retain) [[MYOb^ject]] *x;
3140 @end
3141 )cpp",
3142 [](HoverInfo &HI) {
3143 HI.Name = "MYObject";
3144 HI.Kind = index::SymbolKind::Class;
3145 HI.NamespaceScope = "";
3146 HI.Definition = "@interface MYObject\n@end";
3147 }},
3148 {
3149 R"cpp(
3150 @interface MYObject
3151 @end
3152 @interface Interface
3153 - (void)doWith:([[MYOb^ject]] *)object;
3154 @end
3155 )cpp",
3156 [](HoverInfo &HI) {
3157 HI.Name = "MYObject";
3158 HI.Kind = index::SymbolKind::Class;
3159 HI.NamespaceScope = "";
3160 HI.Definition = "@interface MYObject\n@end";
3161 }},
3162 {
3163 R"cpp(// this expr
3164 // comment
3165 namespace ns {
3166 class Foo {
3167 Foo* bar() {
3168 return [[t^his]];
3169 }
3170 };
3171 }
3172 )cpp",
3173 [](HoverInfo &HI) {
3174 HI.Name = "this";
3175 HI.Definition = "ns::Foo *";
3176 }},
3177 {
3178 R"cpp(// this expr for template class
3179 namespace ns {
3180 template <typename T>
3181 class Foo {
3182 Foo* bar() const {
3183 return [[t^his]];
3184 }
3185 };
3186 }
3187 )cpp",
3188 [](HoverInfo &HI) {
3189 HI.Name = "this";
3190 HI.Definition = "const ns::Foo<T> *";
3191 }},
3192 {
3193 R"cpp(// this expr for specialization class
3194 namespace ns {
3195 template <typename T> class Foo {};
3196 template <>
3197 struct Foo<int> {
3198 Foo* bar() {
3199 return [[thi^s]];
3200 }
3201 };
3202 }
3203 )cpp",
3204 [](HoverInfo &HI) {
3205 HI.Name = "this";
3206 HI.Definition = "ns::Foo<int> *";
3207 }},
3208 {
3209 R"cpp(// this expr for partial specialization struct
3210 namespace ns {
3211 template <typename T, typename F> struct Foo {};
3212 template <typename F>
3213 struct Foo<int, F> {
3214 Foo* bar() const {
3215 return [[thi^s]];
3216 }
3217 };
3218 }
3219 )cpp",
3220 [](HoverInfo &HI) {
3221 HI.Name = "this";
3222 HI.Definition = "const ns::Foo<int, F> *";
3223 }},
3224 {
3225 R"cpp(
3226 @interface MYObject
3227 @end
3228 @interface MYObject (Private)
3229 @property(nonatomic, assign) int privateField;
3230 @end
3231
3232 int someFunction() {
3233 MYObject *obj = [MYObject sharedInstance];
3234 return obj.[[private^Field]];
3235 }
3236 )cpp",
3237 [](HoverInfo &HI) {
3238 HI.Name = "privateField";
3239 HI.Kind = index::SymbolKind::InstanceProperty;
3240 HI.LocalScope = "MYObject(Private)::";
3241 HI.NamespaceScope = "";
3242 HI.Definition = "@property(nonatomic, assign, unsafe_unretained, "
3243 "readwrite) int privateField;";
3244 }},
3245 {
3246 R"cpp(
3247 @protocol MYProtocol
3248 @property(nonatomic, assign) int prop1;
3249 @end
3250
3251 int someFunction() {
3252 id<MYProtocol> obj = 0;
3253 return obj.[[pro^p1]];
3254 }
3255 )cpp",
3256 [](HoverInfo &HI) {
3257 HI.Name = "prop1";
3258 HI.Kind = index::SymbolKind::InstanceProperty;
3259 HI.LocalScope = "MYProtocol::";
3260 HI.NamespaceScope = "";
3261 HI.Definition = "@property(nonatomic, assign, unsafe_unretained, "
3262 "readwrite) int prop1;";
3263 }},
3264 {
3265 R"cpp(
3266 @protocol MYProtocol
3267 @end
3268 @interface MYObject
3269 @end
3270
3271 @interface MYObject (Ext) <[[MYProt^ocol]]>
3272 @end
3273 )cpp",
3274 [](HoverInfo &HI) {
3275 HI.Name = "MYProtocol";
3276 HI.Kind = index::SymbolKind::Protocol;
3277 HI.NamespaceScope = "";
3278 HI.Definition = "@protocol MYProtocol\n@end";
3279 }},
3280 {R"objc(
3281 @interface Foo
3282 @end
3283
3284 @implementation Foo(Private)
3285 + (int)somePrivateMethod {
3286 int [[res^ult]] = 2;
3287 return result;
3288 }
3289 @end
3290 )objc",
3291 [](HoverInfo &HI) {
3292 HI.Name = "result";
3293 HI.Definition = "int result = 2";
3294 HI.Kind = index::SymbolKind::Variable;
3295 HI.Type = "int";
3296 HI.LocalScope = "+[Foo(Private) somePrivateMethod]::";
3297 HI.NamespaceScope = "";
3298 HI.Value = "2";
3299 }},
3300 {R"objc(
3301 @interface Foo
3302 @end
3303
3304 @implementation Foo
3305 - (int)variadicArgMethod:(id)first, ... {
3306 int [[res^ult]] = 0;
3307 return result;
3308 }
3309 @end
3310 )objc",
3311 [](HoverInfo &HI) {
3312 HI.Name = "result";
3313 HI.Definition = "int result = 0";
3314 HI.Kind = index::SymbolKind::Variable;
3315 HI.Type = "int";
3316 HI.LocalScope = "-[Foo variadicArgMethod:, ...]::";
3317 HI.NamespaceScope = "";
3318 HI.Value = "0";
3319 }},
3320 // Should not crash.
3321 {R"objc(
3322 typedef struct MyRect {} MyRect;
3323
3324 @interface IFace
3325 @property(nonatomic) MyRect frame;
3326 @end
3327
3328 MyRect foobar() {
3329 MyRect mr;
3330 return mr;
3331 }
3332 void test() {
3333 IFace *v;
3334 v.frame = [[foo^bar]]();
3335 }
3336 )objc",
3337 [](HoverInfo &HI) {
3338 HI.Name = "foobar";
3339 HI.Kind = index::SymbolKind::Function;
3340 HI.NamespaceScope = "";
3341 HI.Definition = "MyRect foobar()";
3342 HI.Type = {"MyRect ()", "struct MyRect ()"};
3343 HI.ReturnType = {"MyRect", "struct MyRect"};
3344 HI.Parameters.emplace();
3345 }},
3346 {R"cpp(
3347 void foo(int * __attribute__(([[non^null]], noescape)) );
3348 )cpp",
3349 [](HoverInfo &HI) {
3350 HI.Name = "nonnull";
3351 HI.Kind = index::SymbolKind::Unknown; // FIXME: no suitable value
3352 HI.Definition = "__attribute__((nonnull))";
3353 HI.Documentation = Attr::getDocumentation(attr::NonNull).str();
3354 }},
3355 {
3356 R"cpp(
3357 namespace std {
3358 struct strong_ordering {
3359 int n;
3360 constexpr operator int() const { return n; }
3361 static const strong_ordering equal, greater, less;
3362 };
3363 constexpr strong_ordering strong_ordering::equal = {0};
3364 constexpr strong_ordering strong_ordering::greater = {1};
3365 constexpr strong_ordering strong_ordering::less = {-1};
3366 }
3367
3368 struct Foo
3369 {
3370 int x;
3371 // Foo spaceship
3372 auto operator<=>(const Foo&) const = default;
3373 };
3374
3375 bool x = Foo(1) [[!^=]] Foo(2);
3376 )cpp",
3377 [](HoverInfo &HI) {
3378 HI.Type = "bool (const Foo &) const noexcept";
3379 HI.Value = "true";
3380 HI.Name = "operator==";
3381 HI.Parameters = {{{"const Foo &"}, std::nullopt, std::nullopt}};
3382 HI.ReturnType = "bool";
3383 HI.Kind = index::SymbolKind::InstanceMethod;
3384 HI.LocalScope = "Foo::";
3385 HI.NamespaceScope = "";
3386 HI.Definition =
3387 "bool operator==(const Foo &) const noexcept = default";
3388 HI.Documentation = "Foo spaceship";
3389 }},
3390 };
3391
3392 // Create a tiny index, so tests above can verify documentation is fetched.
3393 Symbol IndexSym = func("indexSymbol");
3394 IndexSym.Documentation = "comment from index";
3396 Symbols.insert(IndexSym);
3397 auto Index =
3398 MemIndex::build(std::move(Symbols).build(), RefSlab(), RelationSlab());
3399
3400 for (const auto &Case : Cases) {
3401 SCOPED_TRACE(Case.Code);
3402
3403 Annotations T(Case.Code);
3404 TestTU TU = TestTU::withCode(T.code());
3405 TU.ExtraArgs.push_back("-std=c++20");
3406 TU.ExtraArgs.push_back("-xobjective-c++");
3407
3408 TU.ExtraArgs.push_back("-Wno-gnu-designator");
3409 // Types might be different depending on the target triplet, we chose a
3410 // fixed one to make sure tests passes on different platform.
3411 TU.ExtraArgs.push_back("--target=x86_64-pc-linux-gnu");
3412 auto AST = TU.build();
3413 Config Cfg;
3414 Cfg.Hover.ShowAKA = true;
3415 WithContextValue WithCfg(Config::Key, std::move(Cfg));
3416 auto H = getHover(AST, T.point(), format::getLLVMStyle(), Index.get());
3417 ASSERT_TRUE(H);
3418 HoverInfo Expected;
3419 Expected.SymRange = T.range();
3420 Case.ExpectedBuilder(Expected);
3421
3422 SCOPED_TRACE(H->present(MarkupKind::PlainText));
3423 EXPECT_EQ(H->NamespaceScope, Expected.NamespaceScope);
3424 EXPECT_EQ(H->LocalScope, Expected.LocalScope);
3425 EXPECT_EQ(H->Name, Expected.Name);
3426 EXPECT_EQ(H->Kind, Expected.Kind);
3427 EXPECT_EQ(H->Documentation, Expected.Documentation);
3428 EXPECT_EQ(H->Definition, Expected.Definition);
3429 EXPECT_EQ(H->Type, Expected.Type);
3430 EXPECT_EQ(H->ReturnType, Expected.ReturnType);
3431 EXPECT_EQ(H->Parameters, Expected.Parameters);
3432 EXPECT_EQ(H->TemplateParameters, Expected.TemplateParameters);
3433 EXPECT_EQ(H->SymRange, Expected.SymRange);
3434 EXPECT_EQ(H->Value, Expected.Value);
3435 }
3436}
3437
3438TEST(Hover, Providers) {
3439 struct {
3440 const char *Code;
3441 const std::function<void(HoverInfo &)> ExpectedBuilder;
3442 } Cases[] = {{R"cpp(
3443 struct Foo {};
3444 Foo F = Fo^o{};
3445 )cpp",
3446 [](HoverInfo &HI) { HI.Provider = ""; }},
3447 {R"cpp(
3448 #include "foo.h"
3449 Foo F = Fo^o{};
3450 )cpp",
3451 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3452 {R"cpp(
3453 #include "all.h"
3454 Foo F = Fo^o{};
3455 )cpp",
3456 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3457 {R"cpp(
3458 #define FOO 5
3459 int F = ^FOO;
3460 )cpp",
3461 [](HoverInfo &HI) { HI.Provider = ""; }},
3462 {R"cpp(
3463 #include "foo.h"
3464 int F = ^FOO;
3465 )cpp",
3466 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3467 {R"cpp(
3468 #include "all.h"
3469 int F = ^FOO;
3470 )cpp",
3471 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3472 {R"cpp(
3473 #include "foo.h"
3474 Foo A;
3475 Foo B;
3476 Foo C = A ^+ B;
3477 )cpp",
3478 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3479 // Hover selects the underlying decl of the using decl
3480 {R"cpp(
3481 #include "foo.h"
3482 namespace ns {
3483 using ::Foo;
3484 }
3485 ns::F^oo d;
3486 )cpp",
3487 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3488 {R"cpp(
3489 namespace foo {};
3490 using namespace fo^o;
3491 )cpp",
3492 [](HoverInfo &HI) { HI.Provider = ""; }},
3493 };
3494
3495 for (const auto &Case : Cases) {
3496 Annotations Code{Case.Code};
3497 SCOPED_TRACE(Code.code());
3498
3499 TestTU TU;
3500 TU.Filename = "foo.cpp";
3501 TU.Code = Code.code();
3502 TU.AdditionalFiles["foo.h"] = guard(R"cpp(
3503 #define FOO 1
3504 class Foo {};
3505 Foo& operator+(const Foo, const Foo);
3506 )cpp");
3507 TU.AdditionalFiles["all.h"] = guard("#include \"foo.h\"");
3508
3509 auto AST = TU.build();
3510 auto H = getHover(AST, Code.point(), format::getLLVMStyle(), nullptr);
3511 ASSERT_TRUE(H);
3512 HoverInfo Expected;
3513 Case.ExpectedBuilder(Expected);
3514 SCOPED_TRACE(H->present(MarkupKind::Markdown));
3515 EXPECT_EQ(H->Provider, Expected.Provider);
3516 }
3517}
3518
3519TEST(Hover, ParseProviderInfo) {
3520 HoverInfo HIFoo;
3521 HIFoo.Name = "foo";
3522 HIFoo.Provider = "\"foo.h\"";
3523
3524 HoverInfo HIFooBar;
3525 HIFooBar.Name = "foo";
3526 HIFooBar.Provider = "<bar.h>";
3527 struct Case {
3528 HoverInfo HI;
3529 llvm::StringRef ExpectedMarkdown;
3530 } Cases[] = {{HIFoo, "### `foo`\n\nprovided by `\"foo.h\"`"},
3531 {HIFooBar, "### `foo`\n\nprovided by `<bar.h>`"}};
3532
3533 for (const auto &Case : Cases)
3534 EXPECT_EQ(Case.HI.present(MarkupKind::Markdown), Case.ExpectedMarkdown);
3535}
3536
3537TEST(Hover, UsedSymbols) {
3538 struct {
3539 const char *Code;
3540 const std::function<void(HoverInfo &)> ExpectedBuilder;
3541 } Cases[] = {{R"cpp(
3542 #include ^"bar.h"
3543 int fstBar = bar1();
3544 int another= bar1(0);
3545 int sndBar = bar2();
3546 Bar bar;
3547 int macroBar = BAR;
3548 )cpp",
3549 [](HoverInfo &HI) {
3550 HI.UsedSymbolNames = {"BAR", "Bar", "bar1", "bar2"};
3551 }},
3552 {R"cpp(
3553 #in^clude <vector>
3554 std::vector<int> vec;
3555 )cpp",
3556 [](HoverInfo &HI) { HI.UsedSymbolNames = {"vector"}; }}};
3557 for (const auto &Case : Cases) {
3558 Annotations Code{Case.Code};
3559 SCOPED_TRACE(Code.code());
3560
3561 TestTU TU;
3562 TU.Filename = "foo.cpp";
3563 TU.Code = Code.code();
3564 TU.AdditionalFiles["bar.h"] = guard(R"cpp(
3565 #define BAR 5
3566 int bar1();
3567 int bar2();
3568 int bar1(double);
3569 class Bar {};
3570 )cpp");
3571 TU.AdditionalFiles["system/vector"] = guard(R"cpp(
3572 namespace std {
3573 template<typename>
3574 class vector{};
3575 }
3576 )cpp");
3577 TU.ExtraArgs.push_back("-isystem" + testPath("system"));
3578
3579 auto AST = TU.build();
3580 auto H = getHover(AST, Code.point(), format::getLLVMStyle(), nullptr);
3581 ASSERT_TRUE(H);
3582 HoverInfo Expected;
3583 Case.ExpectedBuilder(Expected);
3584 SCOPED_TRACE(H->present(MarkupKind::Markdown));
3585 EXPECT_EQ(H->UsedSymbolNames, Expected.UsedSymbolNames);
3586 }
3587}
3588
3589TEST(Hover, DocsFromIndex) {
3590 Annotations T(R"cpp(
3591 template <typename T> class X {};
3592 void foo() {
3593 auto t = X<int>();
3594 X^<int> w;
3595 (void)w;
3596 })cpp");
3597
3598 TestTU TU = TestTU::withCode(T.code());
3599 auto AST = TU.build();
3600 Symbol IndexSym;
3601 IndexSym.ID = getSymbolID(&findDecl(AST, "X"));
3602 IndexSym.Documentation = "comment from index";
3604 Symbols.insert(IndexSym);
3605 auto Index =
3606 MemIndex::build(std::move(Symbols).build(), RefSlab(), RelationSlab());
3607
3608 for (const auto &P : T.points()) {
3609 auto H = getHover(AST, P, format::getLLVMStyle(), Index.get());
3610 ASSERT_TRUE(H);
3611 EXPECT_EQ(H->Documentation, IndexSym.Documentation);
3612 }
3613}
3614
3615TEST(Hover, DocsFromAST) {
3616 Annotations T(R"cpp(
3617 // doc
3618 template <typename T> class X {};
3619 // doc
3620 template <typename T> void bar() {}
3621 // doc
3622 template <typename T> T baz;
3623 void foo() {
3624 au^to t = X<int>();
3625 X^<int>();
3626 b^ar<int>();
3627 au^to T = ba^z<X<int>>;
3628 ba^z<int> = 0;
3629 })cpp");
3630
3631 TestTU TU = TestTU::withCode(T.code());
3632 auto AST = TU.build();
3633 for (const auto &P : T.points()) {
3634 auto H = getHover(AST, P, format::getLLVMStyle(), nullptr);
3635 ASSERT_TRUE(H);
3636 EXPECT_EQ(H->Documentation, "doc");
3637 }
3638}
3639
3640TEST(Hover, NoCrash) {
3641 Annotations T(R"cpp(
3642 /* error-ok */
3643 template<typename T> T foo(T);
3644
3645 // Setter variable heuristic might fail if the callexpr is broken.
3646 struct X { int Y; void [[^setY]](float) { Y = foo(undefined); } };)cpp");
3647
3648 TestTU TU = TestTU::withCode(T.code());
3649 auto AST = TU.build();
3650 for (const auto &P : T.points())
3651 getHover(AST, P, format::getLLVMStyle(), nullptr);
3652}
3653
3654TEST(Hover, NoCrashAPInt64) {
3655 Annotations T(R"cpp(
3656 constexpr unsigned long value = -1; // wrap around
3657 void foo() { va^lue; }
3658 )cpp");
3659 auto AST = TestTU::withCode(T.code()).build();
3660 getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
3661}
3662
3663TEST(Hover, NoCrashInt128) {
3664 Annotations T(R"cpp(
3665 constexpr __int128_t value = -4;
3666 void foo() { va^lue; }
3667 )cpp");
3668 auto TU = TestTU::withCode(T.code());
3669 // Need a triple that support __int128_t.
3670 TU.ExtraArgs.push_back("--target=x86_64-pc-linux-gnu");
3671 auto AST = TU.build();
3672 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
3673 ASSERT_TRUE(H);
3674 EXPECT_EQ(H->Value, "-4 (0xfffffffc)");
3675}
3676
3677TEST(Hover, DocsFromMostSpecial) {
3678 Annotations T(R"cpp(
3679 // doc1
3680 template <typename T> class $doc1^X {};
3681 // doc2
3682 template <> class $doc2^X<int> {};
3683 // doc3
3684 template <typename T> class $doc3^X<T*> {};
3685 void foo() {
3686 X$doc1^<char>();
3687 X$doc2^<int>();
3688 X$doc3^<int*>();
3689 })cpp");
3690
3691 TestTU TU = TestTU::withCode(T.code());
3692 auto AST = TU.build();
3693 for (const auto *Comment : {"doc1", "doc2", "doc3"}) {
3694 for (const auto &P : T.points(Comment)) {
3695 auto H = getHover(AST, P, format::getLLVMStyle(), nullptr);
3696 ASSERT_TRUE(H);
3697 EXPECT_EQ(H->Documentation, Comment);
3698 }
3699 }
3700}
3701
3702TEST(Hover, Present) {
3703 struct {
3704 const std::function<void(HoverInfo &)> Builder;
3705 llvm::StringRef ExpectedMarkdownRender;
3706 llvm::StringRef ExpectedDoxygenRender;
3707 } Cases[] = {
3708 {
3709 [](HoverInfo &HI) {
3710 HI.Kind = index::SymbolKind::Unknown;
3711 HI.Name = "X";
3712 },
3713 R"(X)",
3714 R"(### `X`)",
3715 },
3716 {
3717 [](HoverInfo &HI) {
3718 HI.Kind = index::SymbolKind::NamespaceAlias;
3719 HI.Name = "foo";
3720 },
3721 R"(namespace-alias foo)",
3722 R"(### namespace-alias `foo`)",
3723 },
3724 {
3725 [](HoverInfo &HI) {
3726 HI.Kind = index::SymbolKind::Class;
3727 HI.Size = 80;
3728 HI.TemplateParameters = {
3729 {{"typename"}, std::string("T"), std::nullopt},
3730 {{"typename"}, std::string("C"), std::string("bool")},
3731 };
3732 HI.Documentation = "documentation";
3733 HI.Definition =
3734 "template <typename T, typename C = bool> class Foo {}";
3735 HI.Name = "foo";
3736 HI.NamespaceScope.emplace();
3737 },
3738 R"(class foo
3739
3740Size: 10 bytes
3741
3742documentation
3743
3744template <typename T, typename C = bool> class Foo {})",
3745 R"(### class
3746
3747---
3748```cpp
3749template <typename T, typename C = bool> class Foo {}
3750```
3751
3752---
3753### Brief
3754
3755documentation
3756
3757---
3758### Template Parameters
3759
3760- `typename T`
3761- `typename C = bool`
3762
3763---
3764Size: 10 bytes)",
3765 },
3766 {
3767 [](HoverInfo &HI) {
3768 HI.Kind = index::SymbolKind::Function;
3769 HI.Name = "foo";
3770 HI.Type = {"type", "c_type"};
3771 HI.ReturnType = {"ret_type", "can_ret_type"};
3772 HI.Parameters.emplace();
3774 HI.Parameters->push_back(P);
3775 P.Type = {"type", "can_type"};
3776 HI.Parameters->push_back(P);
3777 P.Name = "foo";
3778 HI.Parameters->push_back(P);
3779 P.Default = "default";
3780 HI.Parameters->push_back(P);
3781 HI.NamespaceScope = "ns::";
3782 HI.Definition = "ret_type foo(params) {}";
3783 },
3784 "function foo\n"
3785 "\n"
3786 "→ ret_type (aka can_ret_type)\n\n"
3787 "Parameters:\n\n"
3788 "- \n"
3789 "- type (aka can_type)\n"
3790 "- type foo (aka can_type)\n"
3791 "- type foo = default (aka can_type)\n"
3792 "\n"
3793 "// In namespace ns\n"
3794 "ret_type foo(params) {}",
3795 R"(### function
3796
3797---
3798```cpp
3799// In namespace ns
3800ret_type foo(params) {}
3801```
3802
3803---
3804### Parameters
3805
3806-
3807- `type (aka can_type)`
3808- `type foo (aka can_type)`
3809- `type foo = default (aka can_type)`
3810
3811---
3812### Returns
3813
3814`ret_type (aka can_ret_type)`)",
3815 },
3816 {
3817 [](HoverInfo &HI) {
3818 HI.Kind = index::SymbolKind::Field;
3819 HI.LocalScope = "test::Bar::";
3820 HI.Value = "value";
3821 HI.Name = "foo";
3822 HI.Type = {"type", "can_type"};
3823 HI.Definition = "def";
3824 HI.Size = 32;
3825 HI.Offset = 96;
3826 HI.Padding = 32;
3827 HI.Align = 32;
3828 },
3829 R"(field foo
3830
3831Type: type (aka can_type)
3832
3833Value = value
3834
3835Offset: 12 bytes
3836
3837Size: 4 bytes (+4 bytes padding), alignment 4 bytes
3838
3839// In test::Bar
3840def)",
3841 R"(### field
3842
3843---
3844```cpp
3845// In test::Bar
3846def
3847```
3848
3849---
3850Type: `type (aka can_type)`
3851
3852Value = `value`
3853
3854Offset: 12 bytes
3855
3856Size: 4 bytes (+4 bytes padding), alignment 4 bytes)",
3857 },
3858 {
3859 [](HoverInfo &HI) {
3860 HI.Kind = index::SymbolKind::Field;
3861 HI.LocalScope = "test::Bar::";
3862 HI.Value = "value";
3863 HI.Name = "foo";
3864 HI.Type = {"type", "can_type"};
3865 HI.Definition = "def";
3866 HI.Size = 25;
3867 HI.Offset = 35;
3868 HI.Padding = 4;
3869 HI.Align = 64;
3870 },
3871 R"(field foo
3872
3873Type: type (aka can_type)
3874
3875Value = value
3876
3877Offset: 4 bytes and 3 bits
3878
3879Size: 25 bits (+4 bits padding), alignment 8 bytes
3880
3881// In test::Bar
3882def)",
3883 R"(### field
3884
3885---
3886```cpp
3887// In test::Bar
3888def
3889```
3890
3891---
3892Type: `type (aka can_type)`
3893
3894Value = `value`
3895
3896Offset: 4 bytes and 3 bits
3897
3898Size: 25 bits (+4 bits padding), alignment 8 bytes)",
3899 },
3900 {
3901 [](HoverInfo &HI) {
3902 HI.Kind = index::SymbolKind::Field;
3903 HI.AccessSpecifier = "public";
3904 HI.Name = "foo";
3905 HI.LocalScope = "test::Bar::";
3906 HI.Definition = "def";
3907 },
3908 R"(field foo
3909
3910// In test::Bar
3911public: def)",
3912 R"(### field
3913
3914---
3915```cpp
3916// In test::Bar
3917public: def
3918```)",
3919 },
3920 {
3921 [](HoverInfo &HI) {
3922 HI.Definition = "size_t method()";
3923 HI.AccessSpecifier = "protected";
3924 HI.Kind = index::SymbolKind::InstanceMethod;
3925 HI.NamespaceScope = "";
3926 HI.LocalScope = "cls<int>::";
3927 HI.Name = "method";
3928 HI.Parameters.emplace();
3929 HI.ReturnType = {"size_t", "unsigned long"};
3930 HI.Type = {"size_t ()", "unsigned long ()"};
3931 },
3932 R"(instance-method method
3933
3934→ size_t (aka unsigned long)
3935
3936// In cls<int>
3937protected: size_t method())",
3938 R"(### instance-method
3939
3940---
3941```cpp
3942// In cls<int>
3943protected: size_t method()
3944```
3945
3946---
3947### Returns
3948
3949`size_t (aka unsigned long)`)",
3950 },
3951 {
3952 [](HoverInfo &HI) {
3953 HI.Definition = "cls(int a, int b = 5)";
3954 HI.AccessSpecifier = "public";
3955 HI.Kind = index::SymbolKind::Constructor;
3956 HI.NamespaceScope = "";
3957 HI.LocalScope = "cls";
3958 HI.Name = "cls";
3959 HI.Parameters.emplace();
3960 HI.Parameters->emplace_back();
3961 HI.Parameters->back().Type = "int";
3962 HI.Parameters->back().Name = "a";
3963 HI.Parameters->emplace_back();
3964 HI.Parameters->back().Type = "int";
3965 HI.Parameters->back().Name = "b";
3966 HI.Parameters->back().Default = "5";
3967 },
3968 R"(constructor cls
3969
3970Parameters:
3971
3972- int a
3973- int b = 5
3974
3975// In cls
3976public: cls(int a, int b = 5))",
3977 R"(### constructor
3978
3979---
3980```cpp
3981// In cls
3982public: cls(int a, int b = 5)
3983```
3984
3985---
3986### Parameters
3987
3988- `int a`
3989- `int b = 5`)",
3990 },
3991 {
3992 [](HoverInfo &HI) {
3993 HI.Kind = index::SymbolKind::Union;
3994 HI.AccessSpecifier = "private";
3995 HI.Name = "foo";
3996 HI.NamespaceScope = "ns1::";
3997 HI.Definition = "union foo {}";
3998 },
3999 R"(union foo
4000
4001// In namespace ns1
4002private: union foo {})",
4003 R"(### union
4004
4005---
4006```cpp
4007// In namespace ns1
4008private: union foo {}
4009```)",
4010 },
4011 {
4012 [](HoverInfo &HI) {
4013 HI.Kind = index::SymbolKind::Variable;
4014 HI.Name = "foo";
4015 HI.Definition = "int foo = 3";
4016 HI.LocalScope = "test::Bar::";
4017 HI.Value = "3";
4018 HI.Type = "int";
4019 HI.CalleeArgInfo.emplace();
4020 HI.CalleeArgInfo->Name = "arg_a";
4021 HI.CalleeArgInfo->Type = "int";
4022 HI.CalleeArgInfo->Default = "7";
4023 HI.CallPassType = HoverInfo::PassType{PassMode::Value, false};
4024 },
4025 R"(variable foo
4026
4027Type: int
4028
4029Value = 3
4030
4031Passed as arg_a
4032
4033// In test::Bar
4034int foo = 3)",
4035 R"(### variable
4036
4037---
4038```cpp
4039// In test::Bar
4040int foo = 3
4041```
4042
4043---
4044Type: `int`
4045
4046Value = `3`
4047
4048Passed as arg_a)",
4049 },
4050 {
4051 [](HoverInfo &HI) {
4052 HI.Kind = index::SymbolKind::Variable;
4053 HI.Name = "foo";
4054 HI.CalleeArgInfo.emplace();
4055 HI.CalleeArgInfo->Type = "int";
4056 HI.CallPassType = HoverInfo::PassType{PassMode::Value, false};
4057 },
4058 R"(variable foo
4059
4060Passed by value)",
4061 R"(### variable `foo`
4062
4063---
4064Passed by value)",
4065 },
4066 {
4067 [](HoverInfo &HI) {
4068 HI.Kind = index::SymbolKind::Variable;
4069 HI.Name = "foo";
4070 HI.Definition = "int foo = 3";
4071 HI.LocalScope = "test::Bar::";
4072 HI.Value = "3";
4073 HI.Type = "int";
4074 HI.CalleeArgInfo.emplace();
4075 HI.CalleeArgInfo->Name = "arg_a";
4076 HI.CalleeArgInfo->Type = "int";
4077 HI.CalleeArgInfo->Default = "7";
4078 HI.CallPassType = HoverInfo::PassType{PassMode::Ref, false};
4079 },
4080 R"(variable foo
4081
4082Type: int
4083
4084Value = 3
4085
4086Passed by reference as arg_a
4087
4088// In test::Bar
4089int foo = 3)",
4090 R"(### variable
4091
4092---
4093```cpp
4094// In test::Bar
4095int foo = 3
4096```
4097
4098---
4099Type: `int`
4100
4101Value = `3`
4102
4103Passed by reference as arg_a)",
4104 },
4105 {
4106 [](HoverInfo &HI) {
4107 HI.Kind = index::SymbolKind::Variable;
4108 HI.Name = "foo";
4109 HI.Definition = "int foo = 3";
4110 HI.LocalScope = "test::Bar::";
4111 HI.Value = "3";
4112 HI.Type = "int";
4113 HI.CalleeArgInfo.emplace();
4114 HI.CalleeArgInfo->Name = "arg_a";
4115 HI.CalleeArgInfo->Type = {"alias_int", "int"};
4116 HI.CalleeArgInfo->Default = "7";
4117 HI.CallPassType = HoverInfo::PassType{PassMode::Value, true};
4118 },
4119 R"(variable foo
4120
4121Type: int
4122
4123Value = 3
4124
4125Passed as arg_a (converted to alias_int)
4126
4127// In test::Bar
4128int foo = 3)",
4129 R"(### variable
4130
4131---
4132```cpp
4133// In test::Bar
4134int foo = 3
4135```
4136
4137---
4138Type: `int`
4139
4140Value = `3`
4141
4142Passed as arg_a (converted to alias_int))",
4143 },
4144 {
4145 [](HoverInfo &HI) {
4146 HI.Kind = index::SymbolKind::Macro;
4147 HI.Name = "PLUS_ONE";
4148 HI.Definition = "#define PLUS_ONE(X) (X+1)\n\n"
4149 "// Expands to\n"
4150 "(1 + 1)";
4151 },
4152 R"(macro PLUS_ONE
4153
4154#define PLUS_ONE(X) (X+1)
4155
4156// Expands to
4157(1 + 1))",
4158 R"(### macro
4159
4160---
4161```cpp
4162#define PLUS_ONE(X) (X+1)
4163
4164// Expands to
4165(1 + 1)
4166```)",
4167 },
4168 {
4169 [](HoverInfo &HI) {
4170 HI.Kind = index::SymbolKind::Variable;
4171 HI.Name = "foo";
4172 HI.Definition = "int foo = 3";
4173 HI.LocalScope = "test::Bar::";
4174 HI.Value = "3";
4175 HI.Type = "int";
4176 HI.CalleeArgInfo.emplace();
4177 HI.CalleeArgInfo->Name = "arg_a";
4178 HI.CalleeArgInfo->Type = "int";
4179 HI.CalleeArgInfo->Default = "7";
4180 HI.CallPassType = HoverInfo::PassType{PassMode::ConstRef, true};
4181 },
4182 R"(variable foo
4183
4184Type: int
4185
4186Value = 3
4187
4188Passed by const reference as arg_a (converted to int)
4189
4190// In test::Bar
4191int foo = 3)",
4192 R"(### variable
4193
4194---
4195```cpp
4196// In test::Bar
4197int foo = 3
4198```
4199
4200---
4201Type: `int`
4202
4203Value = `3`
4204
4205Passed by const reference as arg_a (converted to int))",
4206 },
4207 {
4208 [](HoverInfo &HI) {
4209 HI.Name = "stdio.h";
4210 HI.Definition = "/usr/include/stdio.h";
4211 HI.Kind = index::SymbolKind::IncludeDirective;
4212 },
4213 R"(stdio.h
4214
4215/usr/include/stdio.h)",
4216 R"(### `stdio.h`
4217
4218`/usr/include/stdio.h`)",
4219 },
4220 {
4221 [](HoverInfo &HI) {
4222 HI.Name = "foo.h";
4223 HI.UsedSymbolNames = {"Foo", "Bar", "Bar"};
4224 HI.Kind = index::SymbolKind::IncludeDirective;
4225 },
4226 R"(foo.h
4227
4228provides Foo, Bar, Bar)",
4229 R"(### `foo.h`
4230
4231---
4232provides `Foo`, `Bar`, `Bar`)",
4233 },
4234 {[](HoverInfo &HI) {
4235 HI.Name = "foo.h";
4236 HI.UsedSymbolNames = {"Foo", "Bar", "Baz", "Foobar", "Qux", "Quux"};
4237 HI.Kind = index::SymbolKind::IncludeDirective;
4238 },
4239 R"(foo.h
4240
4241provides Foo, Bar, Baz, Foobar, Qux and 1 more)",
4242 R"(### `foo.h`
4243
4244---
4245provides `Foo`, `Bar`, `Baz`, `Foobar`, `Qux` and 1 more)"}};
4246
4247 for (const auto &C : Cases) {
4248 HoverInfo HI;
4249 C.Builder(HI);
4250 Config Cfg;
4251 Cfg.Hover.ShowAKA = true;
4252 Cfg.Documentation.CommentFormat = Config::CommentFormatPolicy::Markdown;
4253 WithContextValue WithCfg(Config::Key, std::move(Cfg));
4254 EXPECT_EQ(HI.present(MarkupKind::PlainText), C.ExpectedMarkdownRender);
4255 }
4256 for (const auto &C : Cases) {
4257 HoverInfo HI;
4258 C.Builder(HI);
4259 Config Cfg;
4260 Cfg.Hover.ShowAKA = true;
4261 Cfg.Documentation.CommentFormat = Config::CommentFormatPolicy::Doxygen;
4262 WithContextValue WithCfg(Config::Key, std::move(Cfg));
4263 EXPECT_EQ(HI.present(MarkupKind::Markdown), C.ExpectedDoxygenRender);
4264 }
4265}
4266
4267TEST(Hover, PresentDocumentation) {
4268 struct {
4269 const std::function<void(HoverInfo &)> Builder;
4270 llvm::StringRef ExpectedMarkdownRender;
4271 llvm::StringRef ExpectedDoxygenRender;
4272 } Cases[] = {
4273 {[](HoverInfo &HI) {
4274 HI.Kind = index::SymbolKind::Function;
4275 HI.Documentation = "@brief brief doc\n\n"
4276 "longer doc";
4277 HI.Definition = "void foo()";
4278 HI.Name = "foo";
4279 },
4280 R"(### function `foo`
4281
4282---
4283@brief brief doc
4284
4285longer doc
4286
4287---
4288```cpp
4289void foo()
4290```)",
4291 R"(### function
4292
4293---
4294```cpp
4295void foo()
4296```
4297
4298---
4299### Brief
4300
4301brief doc
4302
4303---
4304### Details
4305
4306longer doc)"},
4307 {[](HoverInfo &HI) {
4308 HI.Kind = index::SymbolKind::Function;
4309 HI.Documentation = "@brief brief doc\n\n"
4310 "longer doc";
4311 HI.Definition = "int foo()";
4312 HI.ReturnType = "int";
4313 HI.Name = "foo";
4314 },
4315 R"(### function `foo`
4316
4317---
4318→ `int`
4319
4320@brief brief doc
4321
4322longer doc
4323
4324---
4325```cpp
4326int foo()
4327```)",
4328 R"(### function
4329
4330---
4331```cpp
4332int foo()
4333```
4334
4335---
4336### Brief
4337
4338brief doc
4339
4340---
4341### Returns
4342
4343`int`
4344
4345---
4346### Details
4347
4348longer doc)"},
4349 {[](HoverInfo &HI) {
4350 HI.Kind = index::SymbolKind::Function;
4351 HI.Documentation = R"(@brief brief doc
4352
4353longer doc
4354@note this is a note
4355
4356As you see, notes are "inlined".
4357@warning this is a warning
4358
4359As well as warnings
4360@param a this is a param
4361@return it returns something
4362@retval 0 if successful
4363@retval 1 if failed)";
4364 HI.Definition = "int foo(int a)";
4365 HI.ReturnType = "int";
4366 HI.Name = "foo";
4367 HI.Parameters.emplace();
4368 HI.Parameters->emplace_back();
4369 HI.Parameters->back().Type = "int";
4370 HI.Parameters->back().Name = "a";
4371 },
4372 R"(### function `foo`
4373
4374---
4375→ `int`
4376
4377Parameters:
4378
4379- `int a`
4380
4381@brief brief doc
4382
4383longer doc
4384@note this is a note
4385
4386As you see, notes are "inlined".
4387@warning this is a warning
4388
4389As well as warnings
4390@param a this is a param
4391@return it returns something
4392@retval 0 if successful
4393@retval 1 if failed
4394
4395---
4396```cpp
4397int foo(int a)
4398```)",
4399 R"(### function
4400
4401---
4402```cpp
4403int foo(int a)
4404```
4405
4406---
4407### Brief
4408
4409brief doc
4410
4411---
4412### Parameters
4413
4414- `int a` - this is a param
4415
4416---
4417### Returns
4418
4419`int` - it returns something
4420
4421- `0` - if successful
4422- `1` - if failed
4423
4424---
4425### Details
4426
4427longer doc
4428
4429**Note:**
4430this is a note
4431
4432As you see, notes are "inlined".
4433
4434**Warning:**
4435this is a warning
4436
4437As well as warnings)"},
4438 {[](HoverInfo &HI) {
4439 HI.Kind = index::SymbolKind::Function;
4440 HI.Documentation = "@brief brief doc\n\n"
4441 "longer doc\n@param a this is a param\n@param b "
4442 "does not exist\n@return it returns something";
4443 HI.Definition = "int foo(int a)";
4444 HI.ReturnType = "int";
4445 HI.Name = "foo";
4446 HI.Parameters.emplace();
4447 HI.Parameters->emplace_back();
4448 HI.Parameters->back().Type = "int";
4449 HI.Parameters->back().Name = "a";
4450 },
4451 R"(### function `foo`
4452
4453---
4454→ `int`
4455
4456Parameters:
4457
4458- `int a`
4459
4460@brief brief doc
4461
4462longer doc
4463@param a this is a param
4464@param b does not exist
4465@return it returns something
4466
4467---
4468```cpp
4469int foo(int a)
4470```)",
4471 R"(### function
4472
4473---
4474```cpp
4475int foo(int a)
4476```
4477
4478---
4479### Brief
4480
4481brief doc
4482
4483---
4484### Parameters
4485
4486- `int a` - this is a param
4487
4488---
4489### Returns
4490
4491`int` - it returns something
4492
4493---
4494### Details
4495
4496longer doc)"},
4497 {[](HoverInfo &HI) {
4498 HI.Kind = index::SymbolKind::Function;
4499 HI.Documentation = "@brief brief doc\n"
4500 "@unknown command is treated as an inline command";
4501 HI.Definition = "int foo(int a)";
4502 HI.ReturnType = "int";
4503 HI.Name = "foo";
4504 HI.Parameters.emplace();
4505 HI.Parameters->emplace_back();
4506 HI.Parameters->back().Type = "int";
4507 HI.Parameters->back().Name = "a";
4508 },
4509 R"(### function `foo`
4510
4511---
4512→ `int`
4513
4514Parameters:
4515
4516- `int a`
4517
4518@brief brief doc
4519@unknown command is treated as an inline command
4520
4521---
4522```cpp
4523int foo(int a)
4524```)",
4525 R"(### function
4526
4527---
4528```cpp
4529int foo(int a)
4530```
4531
4532---
4533### Brief
4534
4535brief doc
4536**@unknown** command is treated as an inline command
4537
4538---
4539### Parameters
4540
4541- `int a`
4542
4543---
4544### Returns
4545
4546`int`)"},
4547 };
4548
4549 for (const auto &C : Cases) {
4550 HoverInfo HI;
4551 C.Builder(HI);
4552 Config Cfg;
4553 Cfg.Hover.ShowAKA = true;
4554 Cfg.Documentation.CommentFormat = Config::CommentFormatPolicy::Markdown;
4555 WithContextValue WithCfg(Config::Key, std::move(Cfg));
4556 EXPECT_EQ(HI.present(MarkupKind::Markdown), C.ExpectedMarkdownRender);
4557 }
4558 for (const auto &C : Cases) {
4559 HoverInfo HI;
4560 C.Builder(HI);
4561 Config Cfg;
4562 Cfg.Hover.ShowAKA = true;
4563 Cfg.Documentation.CommentFormat = Config::CommentFormatPolicy::Doxygen;
4564 WithContextValue WithCfg(Config::Key, std::move(Cfg));
4565 EXPECT_EQ(HI.present(MarkupKind::Markdown), C.ExpectedDoxygenRender);
4566 }
4567}
4568
4569TEST(Hover, ParseDocumentation) {
4570 struct Case {
4571 llvm::StringRef Documentation;
4572 llvm::StringRef ExpectedRenderEscapedMarkdown;
4573 llvm::StringRef ExpectedRenderMarkdown;
4574 llvm::StringRef ExpectedRenderPlainText;
4575 } Cases[] = {{
4576 " \n foo\nbar",
4577 "foo bar",
4578 "foo\nbar",
4579 "foo bar",
4580 },
4581 {
4582 "foo\nbar \n ",
4583 "foo bar",
4584 "foo\nbar",
4585 "foo bar",
4586 },
4587 {
4588 "foo \nbar",
4589 "foo \nbar",
4590 "foo \nbar",
4591 "foo\nbar",
4592 },
4593 {
4594 "foo \nbar",
4595 "foo \nbar",
4596 "foo \nbar",
4597 "foo\nbar",
4598 },
4599 {
4600 "foo\n\n\nbar",
4601 "foo\n\nbar",
4602 "foo\n\nbar",
4603 "foo\n\nbar",
4604 },
4605 {
4606 "foo\n\n\n\tbar",
4607 "foo\n\nbar",
4608 "foo\n\n\tbar",
4609 "foo\n\nbar",
4610 },
4611 {
4612 "foo\n\n\n bar",
4613 "foo\n\nbar",
4614 "foo\n\n bar",
4615 "foo\n\nbar",
4616 },
4617 {
4618 "foo\n\n\n bar",
4619 "foo\n\nbar",
4620 "foo\n\n bar",
4621 "foo\n\nbar",
4622 },
4623 {
4624 "foo\n\n\n bar",
4625 "foo\n\nbar",
4626 "foo\n\n bar",
4627 "foo\n\nbar",
4628 },
4629 {
4630 "foo\n\n\n\nbar",
4631 "foo\n\nbar",
4632 "foo\n\nbar",
4633 "foo\n\nbar",
4634 },
4635 {
4636 "foo\n\n\n\n\tbar",
4637 "foo\n\nbar",
4638 "foo\n\n\tbar",
4639 "foo\n\nbar",
4640 },
4641 {
4642 "foo\n\n\n\n bar",
4643 "foo\n\nbar",
4644 "foo\n\n bar",
4645 "foo\n\nbar",
4646 },
4647 {
4648 "foo\n\n\n\n bar",
4649 "foo\n\nbar",
4650 "foo\n\n bar",
4651 "foo\n\nbar",
4652 },
4653 {
4654 "foo\n\n\n\n bar",
4655 "foo\n\nbar",
4656 "foo\n\n bar",
4657 "foo\n\nbar",
4658 },
4659 {
4660 "foo.\nbar",
4661 "foo. \nbar",
4662 "foo. \nbar",
4663 "foo.\nbar",
4664 },
4665 {
4666 "foo. \nbar",
4667 "foo. \nbar",
4668 "foo. \nbar",
4669 "foo.\nbar",
4670 },
4671 {
4672 "foo\n*bar",
4673 "foo \n\\*bar",
4674 "foo\n*bar",
4675 "foo\n*bar",
4676 },
4677 {
4678 "foo\nbar",
4679 "foo bar",
4680 "foo\nbar",
4681 "foo bar",
4682 },
4683 {
4684 "Tests primality of `p`.",
4685 "Tests primality of `p`.",
4686 "Tests primality of `p`.",
4687 "Tests primality of `p`.",
4688 },
4689 {
4690 "'`' should not occur in `Code`",
4691 "'\\`' should not occur in `Code`",
4692 "'`' should not occur in `Code`",
4693 "'`' should not occur in `Code`",
4694 },
4695 {
4696 "`not\nparsed`",
4697 "\\`not parsed\\`",
4698 "`not\nparsed`",
4699 "`not parsed`",
4700 },
4701 {
4702 R"(@brief this is a typical use case
4703@param x this is x
4704\param y this is y
4705@return something)",
4706 R"(@brief this is a typical use case
4707@param x this is x
4708\\param y this is y
4709@return something)",
4710 R"(@brief this is a typical use case
4711@param x this is x
4712\param y this is y
4713@return something)",
4714 R"(@brief this is a typical use case
4715@param x this is x
4716\param y this is y
4717@return something)",
4718 }};
4719
4720 for (const auto &C : Cases) {
4721 markup::Document Output;
4722 parseDocumentation(C.Documentation, Output);
4723
4724 EXPECT_EQ(Output.asEscapedMarkdown(), C.ExpectedRenderEscapedMarkdown);
4725 EXPECT_EQ(Output.asMarkdown(), C.ExpectedRenderMarkdown);
4726 EXPECT_EQ(Output.asPlainText(), C.ExpectedRenderPlainText);
4727 }
4728}
4729
4730// This is a separate test as headings don't create any differences in
4731// plaintext mode.
4732TEST(Hover, PresentHeadings) {
4733 HoverInfo HI;
4734 HI.Kind = index::SymbolKind::Variable;
4735 HI.Name = "foo";
4736
4737 EXPECT_EQ(HI.present(MarkupKind::Markdown), "### variable `foo`");
4738}
4739
4740// This is a separate test as rulers behave differently in markdown vs
4741// plaintext.
4742TEST(Hover, PresentRulers) {
4743 HoverInfo HI;
4744 HI.Kind = index::SymbolKind::Variable;
4745 HI.Name = "foo";
4746 HI.Value = "val";
4747 HI.Definition = "def";
4748
4749 llvm::StringRef ExpectedMarkdown = //
4750 "### variable `foo`\n"
4751 "\n"
4752 "---\n"
4753 "Value = `val`\n"
4754 "\n"
4755 "---\n"
4756 "```cpp\n"
4757 "def\n"
4758 "```";
4759 EXPECT_EQ(HI.present(MarkupKind::Markdown), ExpectedMarkdown);
4760
4761 llvm::StringRef ExpectedDoxygenMarkdown = //
4762 "### variable\n"
4763 "\n"
4764 "---\n"
4765 "```cpp\n"
4766 "def\n"
4767 "```\n\n"
4768 "---\n"
4769 "Value = `val`";
4770 Config Cfg;
4771 Cfg.Hover.ShowAKA = true;
4772 Cfg.Documentation.CommentFormat = Config::CommentFormatPolicy::Doxygen;
4773 WithContextValue WithCfg(Config::Key, std::move(Cfg));
4774 EXPECT_EQ(HI.present(MarkupKind::Markdown), ExpectedDoxygenMarkdown);
4775
4776 llvm::StringRef ExpectedPlaintext = R"pt(variable foo
4777
4778Value = val
4779
4780def)pt";
4781 EXPECT_EQ(HI.present(MarkupKind::PlainText), ExpectedPlaintext);
4782}
4783
4784TEST(Hover, SpaceshipTemplateNoCrash) {
4785 Annotations T(R"cpp(
4786 namespace std {
4787 struct strong_ordering {
4788 int n;
4789 constexpr operator int() const { return n; }
4790 static const strong_ordering equal, greater, less;
4791 };
4792 constexpr strong_ordering strong_ordering::equal = {0};
4793 constexpr strong_ordering strong_ordering::greater = {1};
4794 constexpr strong_ordering strong_ordering::less = {-1};
4795 }
4796
4797 template <typename T>
4798 struct S {
4799 // Foo bar baz
4800 friend auto operator<=>(S, S) = default;
4801 };
4802 static_assert(S<void>() =^= S<void>());
4803 )cpp");
4804
4805 TestTU TU = TestTU::withCode(T.code());
4806 TU.ExtraArgs.push_back("-std=c++20");
4807 auto AST = TU.build();
4808 auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
4809 EXPECT_EQ(HI->Documentation, "Foo bar baz");
4810}
4811
4812TEST(Hover, ForwardStructNoCrash) {
4813 Annotations T(R"cpp(
4814 struct Foo;
4815 int bar;
4816 auto baz = (Fo^o*)&bar;
4817 )cpp");
4818
4819 TestTU TU = TestTU::withCode(T.code());
4820 auto AST = TU.build();
4821 auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
4822 ASSERT_TRUE(HI);
4823 EXPECT_EQ(*HI->Value, "&bar");
4824}
4825
4826TEST(Hover, FunctionParameterDefaulValueNotEvaluatedOnInvalidDecls) {
4827 struct {
4828 const char *const Code;
4829 const std::optional<std::string> HoverValue;
4830 } Cases[] = {
4831 {R"cpp(
4832 // error-ok testing behavior on invalid decl
4833 class Foo {};
4834 void foo(Foo p^aram = nullptr);
4835 )cpp",
4836 std::nullopt},
4837 {R"cpp(
4838 class Foo {};
4839 void foo(Foo *p^aram = nullptr);
4840 )cpp",
4841 "nullptr"},
4842 };
4843
4844 for (const auto &C : Cases) {
4845 Annotations T(C.Code);
4846 TestTU TU = TestTU::withCode(T.code());
4847 auto AST = TU.build();
4848 auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
4849 ASSERT_TRUE(HI);
4850 ASSERT_EQ(HI->Value, C.HoverValue);
4851 }
4852}
4853
4854TEST(Hover, DisableShowAKA) {
4855 Annotations T(R"cpp(
4856 using m_int = int;
4857 m_int ^[[a]];
4858 )cpp");
4859
4860 Config Cfg;
4861 Cfg.Hover.ShowAKA = false;
4862 WithContextValue WithCfg(Config::Key, std::move(Cfg));
4863
4864 TestTU TU = TestTU::withCode(T.code());
4865 TU.ExtraArgs.push_back("-std=c++17");
4866 auto AST = TU.build();
4867 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
4868
4869 ASSERT_TRUE(H);
4870 EXPECT_EQ(H->Type, HoverInfo::PrintedType("m_int"));
4871}
4872
4873TEST(Hover, HideBigInitializers) {
4874 Annotations T(R"cpp(
4875 #define A(x) x, x, x, x
4876 #define B(x) A(A(A(A(x))))
4877 int a^rr[] = {B(0)};
4878 )cpp");
4879
4880 TestTU TU = TestTU::withCode(T.code());
4881 auto AST = TU.build();
4882 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
4883
4884 ASSERT_TRUE(H);
4885 EXPECT_EQ(H->Definition, "int arr[]");
4886}
4887
4888#if defined(__aarch64__)
4889// FIXME: AARCH64 sanitizer buildbots are broken after 72142fbac4.
4890#define PREDEFINEMACROS_TEST(x) DISABLED_##x
4891#else
4892#define PREDEFINEMACROS_TEST(x) x
4893#endif
4894
4895TEST(Hover, PREDEFINEMACROS_TEST(GlobalVarEnumeralCastNoCrash)) {
4896 Annotations T(R"cpp(
4897 using uintptr_t = __UINTPTR_TYPE__;
4898 enum Test : uintptr_t {};
4899 unsigned global_var;
4900 void foo() {
4901 Test v^al = static_cast<Test>(reinterpret_cast<uintptr_t>(&global_var));
4902 }
4903 )cpp");
4904
4905 TestTU TU = TestTU::withCode(T.code());
4906 TU.PredefineMacros = true;
4907 auto AST = TU.build();
4908 auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
4909 ASSERT_TRUE(HI);
4910 EXPECT_EQ(*HI->Value, "&global_var");
4911}
4912
4913TEST(Hover, PREDEFINEMACROS_TEST(GlobalVarIntCastNoCrash)) {
4914 Annotations T(R"cpp(
4915 using uintptr_t = __UINTPTR_TYPE__;
4916 unsigned global_var;
4917 void foo() {
4918 uintptr_t a^ddress = reinterpret_cast<uintptr_t>(&global_var);
4919 }
4920 )cpp");
4921
4922 TestTU TU = TestTU::withCode(T.code());
4923 TU.PredefineMacros = true;
4924 auto AST = TU.build();
4925 auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
4926 ASSERT_TRUE(HI);
4927 EXPECT_EQ(*HI->Value, "&global_var");
4928}
4929
4930TEST(Hover, Typedefs) {
4931 Annotations T(R"cpp(
4932 template <bool X, typename T, typename F>
4933 struct cond { using type = T; };
4934 template <typename T, typename F>
4935 struct cond<false, T, F> { using type = F; };
4936
4937 template <bool X, typename T, typename F>
4938 using type = typename cond<X, T, F>::type;
4939
4940 void foo() {
4941 using f^oo = type<true, int, double>;
4942 }
4943 )cpp");
4944
4945 TestTU TU = TestTU::withCode(T.code());
4946 auto AST = TU.build();
4947 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
4948
4949 ASSERT_TRUE(H && H->Type);
4950 EXPECT_EQ(H->Type->Type, "int");
4951 EXPECT_EQ(H->Definition, "using foo = type<true, int, double>");
4952}
4953
4954TEST(Hover, EvaluateMacros) {
4955 llvm::StringRef PredefinedCXX = R"cpp(
4956#define X 42
4957#define SizeOf sizeof
4958#define AlignOf alignof
4959#define PLUS_TWO +2
4960#define TWO 2
4961
4962using u64 = unsigned long long;
4963// calculate (a ** b) % p
4964constexpr u64 pow_with_mod(u64 a, u64 b, u64 p) {
4965 u64 ret = 1;
4966 while (b) {
4967 if (b & 1)
4968 ret = (ret * a) % p;
4969 a = (a * a) % p;
4970 b >>= 1;
4971 }
4972 return ret;
4973}
4974#define last_n_digit(x, y, n) \
4975 pow_with_mod(x, y, pow_with_mod(10, n, 2147483647))
4976#define declare_struct(X, name, value) \
4977 struct X { \
4978 constexpr auto name() { return value; } \
4979 }
4980#define gnu_statement_expression(value) \
4981 ({ \
4982 declare_struct(Widget, getter, value); \
4983 Widget().getter(); \
4984 })
4985#define define_lambda_begin(lambda, ...) \
4986 [&](__VA_ARGS__) {
4987#define define_lambda_end() }
4988
4989#define left_bracket [
4990#define right_bracket ]
4991#define dg_left_bracket <:
4992#define dg_right_bracket :>
4993#define array_decl(type, name, size) type name left_bracket size right_bracket
4994 )cpp";
4995
4996 struct {
4997 llvm::StringRef Code;
4998 const std::function<void(std::optional<HoverInfo>, size_t /*Id*/)>
4999 Validator;
5000 } Cases[] = {
5001 {
5002 /*Code=*/R"cpp(
5003 X^;
5004 )cpp",
5005 /*Validator=*/
5006 [](std::optional<HoverInfo> HI, size_t) {
5007 EXPECT_EQ(HI->Value, "42 (0x2a)");
5008 EXPECT_EQ(HI->Type, HoverInfo::PrintedType("int"));
5009 },
5010 },
5011 {
5012 /*Code=*/R"cpp(
5013 Size^Of(int);
5014 )cpp",
5015 /*Validator=*/
5016 [](std::optional<HoverInfo> HI, size_t) {
5017 EXPECT_TRUE(HI->Value);
5018 EXPECT_TRUE(HI->Type);
5019 // Don't validate type or value of `sizeof` and `alignof` as we're
5020 // getting different values or desugared types on different
5021 // platforms. Same as below.
5022 },
5023 },
5024 {
5025 /*Code=*/R"cpp(
5026 struct Y {
5027 int y;
5028 double z;
5029 };
5030 Alig^nOf(Y);
5031 )cpp",
5032 /*Validator=*/
5033 [](std::optional<HoverInfo> HI, size_t) {
5034 EXPECT_TRUE(HI->Value);
5035 EXPECT_TRUE(HI->Type);
5036 },
5037 },
5038 {
5039 /*Code=*/R"cpp(
5040 // 2**32 == 4294967296
5041 last_n_di^git(2, 32, 6);
5042 )cpp",
5043 /*Validator=*/
5044 [](std::optional<HoverInfo> HI, size_t) {
5045 EXPECT_EQ(HI->Value, "967296 (0xec280)");
5046 EXPECT_EQ(HI->Type, "u64");
5047 },
5048 },
5049 {
5050 /*Code=*/R"cpp(
5051 gnu_statement_exp^ression(42);
5052 )cpp",
5053 /*Validator=*/
5054 [](std::optional<HoverInfo> HI, size_t) {
5055 EXPECT_EQ(HI->Value, "42 (0x2a)");
5056 EXPECT_EQ(HI->Type, "int");
5057 },
5058 },
5059 {
5060 /*Code=*/R"cpp(
5061 40 + PLU^S_TWO;
5062 )cpp",
5063 /*Validator=*/
5064 [](std::optional<HoverInfo> HI, size_t) {
5065 EXPECT_EQ(HI->Value, "2");
5066 EXPECT_EQ(HI->Type, "int");
5067 },
5068 },
5069 {
5070 /*Code=*/R"cpp(
5071 40 PLU^S_TWO;
5072 )cpp",
5073 /*Validator=*/
5074 [](std::optional<HoverInfo> HI, size_t) {
5075 EXPECT_FALSE(HI->Value) << HI->Value;
5076 EXPECT_FALSE(HI->Type) << HI->Type;
5077 },
5078 },
5079 {
5080 /*Code=*/R"cpp(
5081 40 + TW^O;
5082 )cpp",
5083 /*Validator=*/
5084 [](std::optional<HoverInfo> HI, size_t) {
5085 EXPECT_EQ(HI->Value, "2");
5086 EXPECT_EQ(HI->Type, "int");
5087 },
5088 },
5089 {
5090 /*Code=*/R"cpp(
5091 arra^y_decl(int, vector, 10);
5092 vector left_b^racket 3 right_b^racket;
5093 vector dg_le^ft_bracket 3 dg_righ^t_bracket;
5094 )cpp",
5095 /*Validator=*/
5096 [](std::optional<HoverInfo> HI, size_t Id) {
5097 switch (Id) {
5098 case 0:
5099 EXPECT_EQ(HI->Type, HoverInfo::PrintedType("int[10]"));
5100 break;
5101 case 1:
5102 case 2:
5103 case 3:
5104 case 4:
5105 EXPECT_FALSE(HI->Type) << HI->Type;
5106 EXPECT_FALSE(HI->Value) << HI->Value;
5107 break;
5108 default:
5109 ASSERT_TRUE(false) << "Unhandled id: " << Id;
5110 }
5111 },
5112 },
5113 {
5114 /*Code=*/R"cpp(
5115 constexpr auto value = define_lamb^da_begin(lambda, int, char)
5116 // Check if the expansion range is right.
5117 return ^last_n_digit(10, 3, 3)^;
5118 define_lam^bda_end();
5119 )cpp",
5120 /*Validator=*/
5121 [](std::optional<HoverInfo> HI, size_t Id) {
5122 switch (Id) {
5123 case 0:
5124 EXPECT_FALSE(HI->Value);
5125 EXPECT_EQ(HI->Type, HoverInfo::PrintedType("const (lambda)"));
5126 break;
5127 case 1:
5128 EXPECT_EQ(HI->Value, "0");
5129 EXPECT_EQ(HI->Type, HoverInfo::PrintedType("u64"));
5130 break;
5131 case 2:
5132 EXPECT_FALSE(HI);
5133 break;
5134 case 3:
5135 EXPECT_FALSE(HI->Type) << HI->Type;
5136 EXPECT_FALSE(HI->Value) << HI->Value;
5137 break;
5138 default:
5139 ASSERT_TRUE(false) << "Unhandled id: " << Id;
5140 }
5141 },
5142 },
5143 };
5144
5145 Config Cfg;
5146 Cfg.Hover.ShowAKA = false;
5147 WithContextValue WithCfg(Config::Key, std::move(Cfg));
5148 for (const auto &C : Cases) {
5149 Annotations Code(
5150 (PredefinedCXX + "void function() {\n" + C.Code + "}\n").str());
5151 auto TU = TestTU::withCode(Code.code());
5152 TU.ExtraArgs.push_back("-std=c++17");
5153 auto AST = TU.build();
5154 for (auto [Index, Position] : llvm::enumerate(Code.points())) {
5155 C.Validator(getHover(AST, Position, format::getLLVMStyle(), nullptr),
5156 Index);
5157 }
5158 }
5159
5160 Annotations C(R"c(
5161 #define alignof _Alignof
5162 void foo() {
5163 al^ignof(struct { int x; char y[10]; });
5164 }
5165 )c");
5166
5167 auto TU = TestTU::withCode(C.code());
5168 TU.Filename = "TestTU.c";
5169 TU.ExtraArgs = {
5170 "-std=c17",
5171 };
5172 auto AST = TU.build();
5173 auto H = getHover(AST, C.point(), format::getLLVMStyle(), nullptr);
5174
5175 ASSERT_TRUE(H);
5176 EXPECT_TRUE(H->Value);
5177 EXPECT_TRUE(H->Type);
5178}
5179
5180TEST(Hover, HoverMacroContentsLimit) {
5181 const char *const Code =
5182 R"cpp(
5183 #define C(A) A##A // Concatenate
5184 #define E(A) C(A) // Expand
5185 #define Z0032 00000000000000000000000000000000
5186 #define Z0064 E(Z0032)
5187 #define Z0128 E(Z0064)
5188 #define Z0256 E(Z0128)
5189 #define Z0512 E(Z0256)
5190 #define Z1024 E(Z0512)
5191 #define Z2048 E(Z1024)
5192 #define Z4096 E(Z2048) // 4096 zeroes
5193 int main() { return [[^Z4096]]; }
5194 )cpp";
5195
5196 struct {
5197 uint32_t MacroContentsLimit;
5198 const std::string ExpectedDefinition;
5199 } Cases[] = {
5200 // With a limit of 2048, the macro expansion should get dropped.
5201 {2048, "#define Z4096 E(Z2048)"},
5202 // With a limit of 8192, the macro expansion should be fully expanded.
5203 {8192, std::string("#define Z4096 E(Z2048)\n\n") +
5204 std::string("// Expands to\n") + std::string(4096, '0')},
5205 };
5206 for (const auto &Case : Cases) {
5207 SCOPED_TRACE(Code);
5208
5209 Annotations T(Code);
5210 TestTU TU = TestTU::withCode(T.code());
5211 auto AST = TU.build();
5212 Config Cfg;
5213 Cfg.Hover.MacroContentsLimit = Case.MacroContentsLimit;
5214 WithContextValue WithCfg(Config::Key, std::move(Cfg));
5215 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
5216 ASSERT_TRUE(H);
5217
5218 EXPECT_EQ(H->Definition, Case.ExpectedDefinition);
5219 }
5220}
5221
5222TEST(Hover, FunctionParameters) {
5223 struct {
5224 const char *const Code;
5225 const std::function<void(HoverInfo &)> ExpectedBuilder;
5226 std::string ExpectedRender;
5227 } Cases[] = {
5228 {R"cpp(/// Function doc
5229 void foo(int [[^a]]);
5230 )cpp",
5231 [](HoverInfo &HI) {
5232 HI.Name = "a";
5233 HI.Kind = index::SymbolKind::Parameter;
5234 HI.NamespaceScope = "";
5235 HI.LocalScope = "foo::";
5236 HI.Type = "int";
5237 HI.Definition = "int a";
5238 HI.Documentation = "";
5239 },
5240 "### param\n\n---\n```cpp\n// In foo\nint a\n```\n\n---\nType: `int`"},
5241 {R"cpp(/// Function doc
5242 /// @param a this is doc for a
5243 void foo(int [[^a]]);
5244 )cpp",
5245 [](HoverInfo &HI) {
5246 HI.Name = "a";
5247 HI.Kind = index::SymbolKind::Parameter;
5248 HI.NamespaceScope = "";
5249 HI.LocalScope = "foo::";
5250 HI.Type = "int";
5251 HI.Definition = "int a";
5252 HI.Documentation = "this is doc for a";
5253 },
5254 "### param\n\n---\n```cpp\n// In foo\nint a\n```\n\n---\nthis is doc "
5255 "for a\n\n---\nType: `int`"},
5256 {R"cpp(/// Function doc
5257 /// @param b this is doc for b
5258 void foo(int [[^a]], int b);
5259 )cpp",
5260 [](HoverInfo &HI) {
5261 HI.Name = "a";
5262 HI.Kind = index::SymbolKind::Parameter;
5263 HI.NamespaceScope = "";
5264 HI.LocalScope = "foo::";
5265 HI.Type = "int";
5266 HI.Definition = "int a";
5267 HI.Documentation = "";
5268 },
5269 "### param\n\n---\n```cpp\n// In foo\nint a\n```\n\n---\nType: `int`"},
5270 {R"cpp(/// Function doc
5271 /// @param b this is doc for \p b
5272 void foo(int a, int [[^b]]);
5273 )cpp",
5274 [](HoverInfo &HI) {
5275 HI.Name = "b";
5276 HI.Kind = index::SymbolKind::Parameter;
5277 HI.NamespaceScope = "";
5278 HI.LocalScope = "foo::";
5279 HI.Type = "int";
5280 HI.Definition = "int b";
5281 HI.Documentation = "this is doc for \\p b";
5282 },
5283 "### param\n\n---\n```cpp\n// In foo\nint b\n```\n\n---\nthis is doc "
5284 "for `b`\n\n---\nType: `int`"},
5285 {R"cpp(/// Function doc
5286 /// @param b this is doc for \p b
5287 template <typename T>
5288 void foo(T a, T [[^b]]);
5289 )cpp",
5290 [](HoverInfo &HI) {
5291 HI.Name = "b";
5292 HI.Kind = index::SymbolKind::Parameter;
5293 HI.NamespaceScope = "";
5294 HI.LocalScope = "foo::";
5295 HI.Type = "T";
5296 HI.Definition = "T b";
5297 HI.Documentation = "this is doc for \\p b";
5298 },
5299 "### param\n\n---\n```cpp\n// In foo\nT b\n```\n\n---\nthis is doc for "
5300 "`b`\n\n---\nType: `T`"},
5301 {R"cpp(/// Function doc
5302 /// @param b this is <b>doc</b> <html-tag attribute/> <another-html-tag attribute="value">for</another-html-tag> \p b
5303 void foo(int a, int [[^b]]);
5304 )cpp",
5305 [](HoverInfo &HI) {
5306 HI.Name = "b";
5307 HI.Kind = index::SymbolKind::Parameter;
5308 HI.NamespaceScope = "";
5309 HI.LocalScope = "foo::";
5310 HI.Type = "int";
5311 HI.Definition = "int b";
5312 HI.Documentation =
5313 "this is <b>doc</b> <html-tag attribute/> <another-html-tag "
5314 "attribute=\"value\">for</another-html-tag> \\p b";
5315 },
5316 "### param\n\n---\n```cpp\n// In foo\nint b\n```\n\n---\nthis is "
5317 "\\<b>doc\\</b> \\<html-tag attribute/> \\<another-html-tag "
5318 "attribute=\"value\">for\\</another-html-tag> `b`\n\n---\nType: `int`"},
5319 {R"cpp(/// Function doc
5320 /// @param a the next command is an
5321 /// @unknown command.
5322 void foo(int [[^a]]);
5323 )cpp",
5324 [](HoverInfo &HI) {
5325 HI.Name = "a";
5326 HI.Kind = index::SymbolKind::Parameter;
5327 HI.NamespaceScope = "";
5328 HI.LocalScope = "foo::";
5329 HI.Type = "int";
5330 HI.Definition = "int a";
5331 HI.Documentation = "the next command is an\n @unknown command.";
5332 },
5333 "### param\n\n---\n```cpp\n// In foo\nint a\n```\n\n---\nthe next "
5334 "command is an\n**@unknown** command.\n\n---\nType: `int`"},
5335 };
5336
5337 // Create a tiny index, so tests above can verify documentation is fetched.
5338 Symbol IndexSym = func("indexSymbol");
5339 IndexSym.Documentation = "comment from index";
5341 Symbols.insert(IndexSym);
5342 auto Index =
5343 MemIndex::build(std::move(Symbols).build(), RefSlab(), RelationSlab());
5344
5345 for (const auto &Case : Cases) {
5346 SCOPED_TRACE(Case.Code);
5347
5348 Annotations T(Case.Code);
5349 TestTU TU = TestTU::withCode(T.code());
5350 auto AST = TU.build();
5351 Config Cfg;
5352 Cfg.Hover.ShowAKA = true;
5353 Cfg.Documentation.CommentFormat = Config::CommentFormatPolicy::Doxygen;
5354 WithContextValue WithCfg(Config::Key, std::move(Cfg));
5355 auto H = getHover(AST, T.point(), format::getLLVMStyle(), Index.get());
5356 ASSERT_TRUE(H);
5357 HoverInfo Expected;
5358 Expected.SymRange = T.range();
5359 Case.ExpectedBuilder(Expected);
5360
5361 EXPECT_EQ(H->present(MarkupKind::Markdown), Case.ExpectedRender);
5362 EXPECT_EQ(H->NamespaceScope, Expected.NamespaceScope);
5363 EXPECT_EQ(H->LocalScope, Expected.LocalScope);
5364 EXPECT_EQ(H->Name, Expected.Name);
5365 EXPECT_EQ(H->Kind, Expected.Kind);
5366 EXPECT_EQ(H->Documentation, Expected.Documentation);
5367 EXPECT_EQ(H->Definition, Expected.Definition);
5368 EXPECT_EQ(H->Type, Expected.Type);
5369 EXPECT_EQ(H->ReturnType, Expected.ReturnType);
5370 EXPECT_EQ(H->Parameters, Expected.Parameters);
5371 EXPECT_EQ(H->TemplateParameters, Expected.TemplateParameters);
5372 EXPECT_EQ(H->SymRange, Expected.SymRange);
5373 EXPECT_EQ(H->Value, Expected.Value);
5374 }
5375}
5376
5377static void configureHLSL(TestTU &TU, bool EnableMatrix = false) {
5378 TU.Filename = "TestTU.hlsl";
5379 TU.ExtraArgs.push_back("-x");
5380 TU.ExtraArgs.push_back("hlsl");
5381 if (EnableMatrix)
5382 TU.ExtraArgs.push_back("-fenable-matrix");
5383 TU.ExtraArgs.push_back("--target=dxil-pc-shadermodel6.3-library");
5384}
5385
5386TEST(Hover, HLSLVectorAndMatrixSwizzle) {
5387 struct {
5388 const char *const Code;
5389 const std::function<void(HoverInfo &)> ExpectedBuilder;
5390 } Cases[] = {
5391 {
5392 R"hlsl(
5393 typedef float float3 __attribute__((ext_vector_type(3)));
5394 void main() {
5395 float3 v;
5396 float3 s = v.^[[xyz]];
5397 }
5398 )hlsl",
5399 [](HoverInfo &HI) {
5400 HI.Name = "xyz";
5401 HI.Type = "float3";
5402 }},
5403 {
5404 R"hlsl(
5405 typedef float float3 __attribute__((ext_vector_type(3)));
5406 typedef float float2 __attribute__((ext_vector_type(2)));
5407 void main() {
5408 float3 v;
5409 float2 s = v.^[[xy]];
5410 }
5411 )hlsl",
5412 [](HoverInfo &HI) {
5413 HI.Name = "xy";
5414 HI.Type = "float2";
5415 }},
5416 {
5417 R"hlsl(
5418 typedef float float4x4 __attribute__((matrix_type(4, 4)));
5419 void main() {
5420 float4x4 m;
5421 float e = m.^[[_m00]];
5422 }
5423 )hlsl",
5424 [](HoverInfo &HI) {
5425 HI.Name = "_m00";
5426 HI.Type = "float";
5427 }},
5428 };
5429
5430 for (const auto &Case : Cases) {
5431 SCOPED_TRACE(Case.Code);
5432 Annotations T(Case.Code);
5433 TestTU TU = TestTU::withCode(T.code());
5434 configureHLSL(TU, /*EnableMatrix=*/true);
5435 auto AST = TU.build();
5436 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
5437 ASSERT_TRUE(H);
5438 HoverInfo Expected;
5439 Expected.SymRange = T.range();
5440 Case.ExpectedBuilder(Expected);
5441 SCOPED_TRACE(H->present(MarkupKind::PlainText));
5442 EXPECT_EQ(H->Name, Expected.Name);
5443 EXPECT_EQ(H->Type, Expected.Type);
5444 EXPECT_EQ(H->SymRange, Expected.SymRange);
5445 }
5446}
5447
5448TEST(Hover, HLSLInvalidMatrixSwizzleNoCrash) {
5449 Annotations T(R"hlsl(
5450 typedef float float2x2 __attribute__((matrix_type(2, 2)));
5451 void main() {
5452 float2x2 m;
5453 float bad = m.^[[_m22]]; // out of bounds for a 2x2 matrix /*error-ok*/
5454 }
5455 )hlsl");
5456 TestTU TU = TestTU::withCode(T.code());
5457 configureHLSL(TU, /*EnableMatrix=*/true);
5458 auto AST = TU.build();
5459 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
5460 EXPECT_FALSE(H);
5461}
5462
5463TEST(Hover, HLSLInvalidVectorSwizzleNoCrash) {
5464 Annotations T(R"hlsl(
5465 typedef float float3 __attribute__((ext_vector_type(3)));
5466 void main() {
5467 float3 v;
5468 float bad = v.^[[w]]; // 'w' is not a valid component for a 3-component vector /*error-ok*/
5469 }
5470 )hlsl");
5471 TestTU TU = TestTU::withCode(T.code());
5472 configureHLSL(TU);
5473 auto AST = TU.build();
5474 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
5475 EXPECT_FALSE(H);
5476}
5477
5478TEST(Hover, AttributedStmt) {
5479 struct {
5480 const char *const Code;
5481 const char *const ExpectedName;
5482 bool IsHLSL;
5483 bool ExpectDocumentation;
5484 } Cases[] = {
5485 {R"hlsl(
5486 [numthreads(1, 1, 1)]
5487 void main() {
5488 [^unroll]
5489 for (int i = 0; i < 4; i++) {}
5490 }
5491 )hlsl",
5492 "unroll", /*IsHLSL=*/true, /*ExpectDocumentation=*/true},
5493 {R"hlsl(
5494 [numthreads(1, 1, 1)]
5495 void main() {
5496 [l^oop]
5497 for (int i = 0; i < 4; i++) {}
5498 }
5499 )hlsl",
5500 "loop", /*IsHLSL=*/true, /*ExpectDocumentation=*/true},
5501 {R"hlsl(
5502 [numthreads(1, 1, 1)]
5503 void main() {
5504 [b^ranch]
5505 if (true) {}
5506 }
5507 )hlsl",
5508 "branch", /*IsHLSL=*/true, /*ExpectDocumentation=*/false},
5509 {R"hlsl(
5510 [numthreads(1, 1, 1)]
5511 void main() {
5512 [f^latten]
5513 if (true) {}
5514 }
5515 )hlsl",
5516 "flatten", /*IsHLSL=*/true, /*ExpectDocumentation=*/false},
5517 {R"cpp(
5518 void foo() {
5519 [[^likely]] if (true) {}
5520 }
5521 )cpp",
5522 "likely", /*IsHLSL=*/false, /*ExpectDocumentation=*/false},
5523 {R"cpp(
5524 void foo() {
5525 [[^unlikely]] if (true) {}
5526 }
5527 )cpp",
5528 "unlikely", /*IsHLSL=*/false, /*ExpectDocumentation=*/false},
5529 {R"cpp(
5530 void foo() {
5531 switch (1) {
5532 case 1:
5533 [[^fallthrough]];
5534 case 2:
5535 break;
5536 }
5537 }
5538 )cpp",
5539 "fallthrough", /*IsHLSL=*/false, /*ExpectDocumentation=*/false},
5540 };
5541 for (const auto &Case : Cases) {
5542 SCOPED_TRACE(Case.Code);
5543 Annotations T(Case.Code,
5544 Annotations::Markers().setRangeBegin("{{").setRangeEnd("}}"));
5545 TestTU TU = TestTU::withCode(T.code());
5546 if (Case.IsHLSL)
5547 configureHLSL(TU);
5548 else
5549 TU.ExtraArgs.push_back("-std=c++20");
5550 auto AST = TU.build();
5551 auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
5552 ASSERT_TRUE(H);
5553 EXPECT_EQ(H->Name, Case.ExpectedName);
5554 if (Case.ExpectDocumentation) {
5555 EXPECT_FALSE(H->Documentation.empty());
5556 }
5557 }
5558}
5559TEST(Hover, HLSLRegisterAttributeRange) {
5560 Annotations T(R"hlsl(
5561 Texture2D tex : [[^register]]([[^t1]]);
5562 )hlsl");
5563
5564 TestTU TU = TestTU::withCode(T.code());
5565 configureHLSL(TU);
5566
5567 auto AST = TU.build();
5568
5569 for (const auto &P : T.points()) {
5570 auto H = getHover(AST, P, format::getLLVMStyle(), nullptr);
5571
5572 ASSERT_TRUE(H);
5573 EXPECT_EQ(H->Name, "register");
5574 EXPECT_FALSE(H->Documentation.empty());
5575 }
5576}
5577
5578} // namespace
5579} // namespace clangd
5580} // namespace clang
#define PREDEFINEMACROS_TEST(x)
Same as llvm::Annotations, but adjusts functions to LSP-specific types for positions and ranges.
Definition Annotations.h:23
static std::unique_ptr< SymbolIndex > build(SymbolSlab Symbols, RefSlab Refs, RelationSlab Relations)
Builds an index from slabs. The index takes ownership of the data.
Definition MemIndex.cpp:18
An efficient structure of storing large set of symbol references in memory.
Definition Ref.h:111
SymbolSlab::Builder is a mutable container that can 'freeze' to SymbolSlab.
Definition Symbol.h:238
WithContextValue extends Context::current() with a single value.
Definition Context.h:200
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
SymbolID getSymbolID(const Decl *D)
Gets the symbol ID for a declaration. Returned SymbolID might be null.
Definition AST.cpp:354
Symbol func(llvm::StringRef Name)
Definition TestIndex.cpp:62
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Definition TestTU.cpp:231
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition TestFS.cpp:94
std::optional< HoverInfo > getHover(ParsedAST &AST, Position Pos, const format::FormatStyle &Style, const SymbolIndex *Index)
Get the hover information when hovering at Pos.
Definition Hover.cpp:1314
TEST(BackgroundQueueTest, Priority)
void parseDocumentation(llvm::StringRef Input, markup::Document &Output)
Definition Hover.cpp:1853
cppcoreguidelines::ProBoundsAvoidUncheckedContainerAccessCheck P
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Settings that express user/project preferences and control clangd behavior.
Definition Config.h:45
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
Definition Config.h:49
@ Markdown
Treat comments as Markdown.
Definition Config.h:217
@ Doxygen
Treat comments as doxygen.
Definition Config.h:219
struct clang::clangd::Config::@124253042262101241326257264241307221112313102042 Hover
Configures hover feature.
bool ShowAKA
Whether hover show a.k.a type.
Definition Config.h:186
uint32_t MacroContentsLimit
Limit the number of characters returned when hovering a macro; 0 is no limit.
Definition Config.h:189
Represents parameters of a function, a template or a macro.
Definition Hover.h:46
Contains pretty-printed type and desugared type.
Definition Hover.h:29
Contains detailed information about a Symbol.
Definition Hover.h:27
std::optional< Range > SymRange
Definition Hover.h:74
std::string Name
Name of the symbol, does not contain any "::".
Definition Hover.h:71
index::SymbolKind Kind
Definition Hover.h:75
Ensure we have enough bits to represent all SymbolTag values.
Definition Symbol.h:49
ParsedAST build() const
Definition TestTU.cpp:114
std::string Filename
Definition TestTU.h:50
static TestTU withCode(llvm::StringRef Code)
Definition TestTU.h:36