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"
24#include "gtest/gtest.h"
36std::string guard(llvm::StringRef Code) {
37 return "#pragma once\n" + Code.str();
42 const char *
const Code;
43 const std::function<void(
HoverInfo &)> ExpectedBuilder;
51 HI.NamespaceScope = "";
53 HI.Kind = index::SymbolKind::Function;
54 HI.Documentation =
"Best foo ever.";
55 HI.Definition =
"void foo()";
56 HI.ReturnType =
"void";
58 HI.Parameters.emplace();
62 void [[fo^o]](auto x) {}
65 HI.NamespaceScope = "";
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},
76 {{
"auto"}, std::string(
"x"), std::nullopt},
85 HI.NamespaceScope = "";
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";
92 HI.TemplateParameters = {
93 {{
"class"}, std::string(
"T"), std::nullopt},
96 {{
"T"}, std::string(
"x"), std::nullopt},
102 void [[fo^o]](T x, auto y) {}
105 HI.NamespaceScope = "";
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},
117 {{
"T"}, std::string(
"x"), std::nullopt},
118 {{
"auto"}, std::string(
"y"), std::nullopt},
122 template<typename T1, typename T2>
123 concept C = requires () { true; };
127 void [[fo^o]](T x) {}
130 HI.NamespaceScope = "";
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},
141 {{
"T"}, std::string(
"x"), std::nullopt},
145 template<typename T1, typename T2>
146 concept C = requires () { true; };
149 void [[fo^o]](C<int> auto x) {}
152 HI.NamespaceScope = "";
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},
163 {{
"C<int> auto"}, std::string(
"x"), std::nullopt},
167 template<typename T1, typename T2>
168 concept C = requires () { true; };
172 void [[fo^o]](T x, C<int> auto y) {}
175 HI.NamespaceScope = "";
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},
187 {{
"T"}, std::string(
"x"), std::nullopt},
188 {{
"C<int> auto"}, std::string(
"y"), std::nullopt},
193 namespace ns1 { namespace ns2 {
199 HI.NamespaceScope =
"ns1::ns2::";
201 HI.Kind = index::SymbolKind::Function;
202 HI.Documentation =
"Best foo ever.";
203 HI.Definition =
"void foo()";
204 HI.ReturnType =
"void";
206 HI.Parameters.emplace();
210 namespace ns1 { namespace ns2 {
218 HI.NamespaceScope = "ns1::ns2::";
219 HI.LocalScope =
"Foo::";
221 HI.Kind = index::SymbolKind::Field;
222 HI.Definition =
"char bar";
228 HI.AccessSpecifier =
"private";
238 HI.NamespaceScope = "";
239 HI.LocalScope =
"Foo::";
241 HI.Kind = index::SymbolKind::Field;
242 HI.Definition =
"char bar";
247 HI.AccessSpecifier =
"public";
257 HI.NamespaceScope = "";
258 HI.LocalScope =
"Foo::";
260 HI.Kind = index::SymbolKind::Field;
261 HI.Definition =
"int x : 1";
266 HI.AccessSpecifier =
"public";
270 namespace ns1 { namespace ns2 {
279 HI.NamespaceScope = "ns1::ns2::";
280 HI.LocalScope =
"Foo::foo::";
282 HI.Kind = index::SymbolKind::Variable;
283 HI.Definition =
"int bar";
293 HI.Name = "__func__";
294 HI.Kind = index::SymbolKind::Variable;
296 "Name of the current function (predefined variable)";
297 HI.Value =
"\"foo\"";
298 HI.Type =
"const char[4]";
302 template<int> void foo() {
307 HI.Name = "__func__";
308 HI.Kind = index::SymbolKind::Variable;
310 "Name of the current function (predefined variable)";
311 HI.Type =
"const char[]";
315 namespace ns1 { namespace {
322 HI.NamespaceScope = "ns1::";
323 HI.LocalScope =
"(anonymous struct)::";
325 HI.Kind = index::SymbolKind::Field;
326 HI.Definition =
"char bar";
331 HI.AccessSpecifier =
"public";
338 HI.NamespaceScope = "";
340 HI.Kind = index::SymbolKind::Struct;
341 HI.Definition =
"struct X {}";
347 template <typename T, class... Ts> class Foo { public: Foo(int); };
348 Foo<int, char, bool> [[fo^o]] = Foo<int, char, bool>(5);
351 HI.NamespaceScope = "";
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>";
359 template <typename T> class vector{};
360 [[vec^tor]]<int> foo;
363 HI.NamespaceScope = "";
364 HI.Name =
"vector<int>";
365 HI.Kind = index::SymbolKind::Class;
366 HI.Definition =
"template <> class vector<int> {}";
370 template <template<typename, bool...> class C,
374 class... Ts> class Foo final {};
375 template <template<typename, bool...> class T>
379 HI.NamespaceScope = "";
381 HI.Kind = index::SymbolKind::Class;
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"},
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},
398 template <template<typename, bool...> class C,
402 class... Ts> void foo();
403 template<typename, bool...> class Foo;
410 HI.NamespaceScope = "";
412 HI.Kind = index::SymbolKind::Function;
413 HI.Definition =
"template <> void foo<Foo, char, 0, false, <>>()";
414 HI.ReturnType =
"void";
416 HI.Parameters.emplace();
420 template<typename, bool...> class Foo {};
421 Foo<bool, true, false> foo(int, bool T = false);
428 HI.NamespaceScope = "";
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)";
435 {{
"int"}, std::nullopt, std::nullopt},
436 {{
"bool"}, std::string(
"T"), std::string(
"false")},
442 auto lamb = [](int T, bool B) -> bool { return T && B; };
448 HI.NamespaceScope = "";
449 HI.LocalScope =
"foo::";
451 HI.Kind = index::SymbolKind::Variable;
452 HI.Definition =
"auto *c = &b";
453 HI.Type =
"(lambda) **";
454 HI.ReturnType =
"bool";
456 {{
"int"}, std::string(
"T"), std::nullopt},
457 {{
"bool"}, std::string(
"B"), std::nullopt},
463 auto lamb = [](int T, bool B) -> bool { return T && B; };
464 void foo(decltype(lamb)& bar) {
469 HI.NamespaceScope = "";
470 HI.LocalScope =
"foo::";
472 HI.Kind = index::SymbolKind::Parameter;
473 HI.Definition =
"decltype(lamb) &bar";
474 HI.Type =
"(lambda) &";
475 HI.ReturnType =
"bool";
477 {{
"int"}, std::string(
"T"), std::nullopt},
478 {{
"bool"}, std::string(
"B"), std::nullopt},
484 auto lamb = [](int T, bool B) -> bool { return T && B; };
485 void foo(decltype(lamb) bar) {
490 HI.NamespaceScope = "";
491 HI.LocalScope =
"foo::";
493 HI.Kind = index::SymbolKind::Parameter;
494 HI.Definition =
"decltype(lamb) bar";
495 HI.Type =
"class (lambda)";
496 HI.ReturnType =
"bool";
498 {{
"int"}, std::string(
"T"), std::nullopt},
499 {{
"bool"}, std::string(
"B"), std::nullopt},
508 auto lamb = [&bar](int T, bool B) -> bool { return T && B && bar; };
509 bool res = [[lam^b]](bar, false);
513 HI.NamespaceScope = "";
514 HI.LocalScope =
"foo::";
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";
521 {{
"int"}, std::string(
"T"), std::nullopt},
522 {{
"bool"}, std::string(
"B"), std::nullopt},
529 auto lamb = []{int [[te^st]];};
533 HI.NamespaceScope = "";
534 HI.LocalScope =
"foo::(anonymous class)::operator()::";
536 HI.Kind = index::SymbolKind::Variable;
537 HI.Definition =
"int test";
542 template <typename T> class X;
543 template <typename T> class [[^X]]<T*> {};
547 HI.NamespaceScope =
"";
548 HI.Kind = index::SymbolKind::Class;
549 HI.Definition =
"template <typename T> class X<T *> {}";
553 template<typename, typename=void> struct X;
554 template<typename T> struct X<T*>{ [[^X]](); };
557 HI.NamespaceScope = "";
559 HI.LocalScope =
"X<T *>::";
560 HI.Kind = index::SymbolKind::Constructor;
561 HI.Definition =
"X()";
562 HI.Parameters.emplace();
563 HI.AccessSpecifier =
"public";
565 {
"class X { [[^~]]X(); };",
567 HI.NamespaceScope =
"";
569 HI.LocalScope =
"X::";
570 HI.Kind = index::SymbolKind::Destructor;
571 HI.Definition =
"~X()";
572 HI.Parameters.emplace();
573 HI.AccessSpecifier =
"private";
575 {
"class X { [[op^erator]] int(); };",
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";
585 {
"class X { operator [[^X]](); };",
587 HI.NamespaceScope =
"";
589 HI.Kind = index::SymbolKind::Class;
590 HI.Definition =
"class X {}";
596 struct S { int x; float y; };
597 [[au^to]] [x, y] = S();
602 HI.Kind = index::SymbolKind::TypeAlias;
614 HI.Kind = index::SymbolKind::TypeAlias;
619 template <class T> concept F = true;
624 HI.Kind = index::SymbolKind::TypeAlias;
625 HI.Definition =
"int";
628 template <class T> concept F = true;
632 HI.NamespaceScope = "";
634 HI.Kind = index::SymbolKind::Concept;
635 HI.Definition =
"template <class T>\nconcept F = true";
640 [[au^to]] lamb = []{};
645 HI.Kind = index::SymbolKind::TypeAlias;
646 HI.Definition =
"class(lambda)";
650 template<typename T> class Foo{};
652 [[au^to]] x = Foo<int>();
657 HI.Kind = index::SymbolKind::TypeAlias;
658 HI.Definition =
"Foo<int>";
662 template<typename T> class Foo{};
663 template<> class Foo<int>{};
665 [[au^to]] x = Foo<int>();
670 HI.Kind = index::SymbolKind::TypeAlias;
671 HI.Definition =
"Foo<int>";
675 template<class T> concept Fooable = true;
676 template<[[Foo^able]] T>
680 HI.NamespaceScope = "";
682 HI.Kind = index::SymbolKind::Concept;
683 HI.Definition =
"template <class T>\nconcept Fooable = true";
686 template<class T> concept Fooable = true;
687 template<Fooable [[T^T]]>
693 HI.AccessSpecifier =
"public";
694 HI.NamespaceScope =
"";
695 HI.LocalScope =
"bar::";
696 HI.Kind = index::SymbolKind::TemplateTypeParm;
697 HI.Definition =
"Fooable TT";
700 template<class T> concept Fooable = true;
701 void bar([[Foo^able]] auto t) {}
704 HI.NamespaceScope = "";
706 HI.Kind = index::SymbolKind::Concept;
707 HI.Definition =
"template <class T>\nconcept Fooable = true";
711 template<class T> concept Fooable = true;
712 auto X = [[Fooa^ble]]<int>;
715 HI.NamespaceScope = "";
717 HI.Kind = index::SymbolKind::Concept;
718 HI.Definition =
"template <class T>\nconcept Fooable = true";
729 HI.Kind = index::SymbolKind::Macro;
730 HI.Definition =
"#define MACRO";
740 HI.Kind = index::SymbolKind::Macro;
741 HI.Value =
"41 (0x29)";
743 HI.Definition =
"#define MACRO 41\n\n"
751 #define MACRO(x,y,z) void foo(x, y, z)
752 [[MAC^RO]](int, double d, bool z = false);
756 HI.Kind = index::SymbolKind::Macro;
757 HI.Definition =
"#define MACRO(x, y, z) void foo(x, y, z)\n\n"
759 "void foo(int, double d, bool z = false)";
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)
769 [[DECL^_STR]](foo, FOO);
772 HI.Name = "DECL_STR";
773 HI.Kind = index::SymbolKind::Macro;
775 HI.Definition =
"#define DECL_STR(NAME, VALUE) const char *v_##NAME = "
776 "STRINGIFY(VALUE)\n\n"
778 "const char *v_foo = \"41\"";
783 constexpr int add(int a, int b) { return a + b; }
784 int [[b^ar]] = add(1, 2);
788 HI.Definition =
"int bar = add(1, 2)";
789 HI.Kind = index::SymbolKind::Variable;
791 HI.NamespaceScope =
"";
795 int [[b^ar]] = sizeof(char);
799 HI.Definition =
"int bar = sizeof(char)";
800 HI.Kind = index::SymbolKind::Variable;
802 HI.NamespaceScope =
"";
806 template<int a, int b> struct Add {
807 static constexpr int result = a + b;
809 int [[ba^r]] = Add<1, 2>::result;
813 HI.Definition =
"int bar = Add<1, 2>::result";
814 HI.Kind = index::SymbolKind::Variable;
816 HI.NamespaceScope =
"";
820 enum Color { RED = -123, GREEN = 5, };
821 Color x = [[GR^EEN]];
825 HI.NamespaceScope =
"";
826 HI.LocalScope =
"Color::";
827 HI.Definition =
"GREEN = 5";
828 HI.Kind = index::SymbolKind::EnumConstant;
829 HI.Type =
"enum Color";
833 enum Color { RED = -123, GREEN = 5, };
839 HI.NamespaceScope =
"";
840 HI.Definition =
"Color x = RED";
841 HI.Kind = index::SymbolKind::Variable;
843 HI.Value =
"RED (0xffffff85)";
846 template<int a, int b> struct Add {
847 static constexpr int result = a + b;
849 int bar = Add<1, 2>::[[resu^lt]];
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>::";
859 HI.AccessSpecifier =
"public";
863 constexpr my_int answer() { return 40 + 2; }
864 int x = [[ans^wer]]();
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)";
877 const char *[[ba^r]] = "1234";
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]";
887 {R
"cpp(// Should not crash
888 template <typename T>
893 template <typename A>
894 void boom(int name) {
895 new Tmpl<A>([[na^me]]);
899 HI.Definition =
"int name";
900 HI.Kind = index::SymbolKind::Parameter;
902 HI.NamespaceScope =
"";
903 HI.LocalScope =
"boom::";
906 R
"cpp(// Should not print inline or anon namespaces.
908 inline namespace in_ns {
912 inline namespace in_ns2 {
921 ns::a::b::[[F^oo]] x;
927 HI.Kind = index::SymbolKind::Class;
928 HI.NamespaceScope =
"ns::a::b::";
929 HI.Definition =
"class Foo {}";
933 template <typename T> class Foo {};
936 [[^auto]] x = Foo<X>();
941 HI.Kind = index::SymbolKind::TypeAlias;
942 HI.Definition =
"Foo<X>";
946 // comment from primary
947 template <typename T> class Foo {};
948 // comment from specialization
949 template <typename T> class Foo<T*> {};
951 [[Fo^o]]<int*> *x = nullptr;
955 HI.Name = "Foo<int *>";
956 HI.Kind = index::SymbolKind::Class;
957 HI.NamespaceScope =
"";
958 HI.Definition =
"template <> class Foo<int *>";
961 HI.Documentation =
"comment from primary";
965 template <typename [[^T]] = int> void foo();
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";
978 template <template<typename> class [[^T]]> void foo();
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";
991 template <int [[^T]] = 5> void foo();
995 HI.Kind = index::SymbolKind::NonTypeTemplateParm;
996 HI.NamespaceScope =
"";
997 HI.Definition =
"int T = 5";
998 HI.LocalScope =
"foo::";
1000 HI.AccessSpecifier =
"public";
1005 struct X { int Y; float [[^y]]() { return 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";
1024 float [[^y]]() { return 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";
1041 struct X { int Y; void [[^setY]](float v) { Y = v; } };
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";
1060 struct X { int Y; X& [[^setY]](float v) { Y = v; return *this; } };
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";
1079 namespace std { template<typename T> T&& move(T&& t); }
1080 struct X { int Y; void [[^setY]](float v) { Y = std::move(v); } };
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";
1102 void [[^setY]](float v) { Y = v; }
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";
1122 struct X { int x = 2; };
1127 HI.Kind = index::SymbolKind::Variable;
1128 HI.NamespaceScope =
"";
1129 HI.Definition =
"X x";
1133 R
"cpp(auto [^[[x]]] = 1; /*error-ok*/)cpp",
1136 HI.Kind = index::SymbolKind::Variable;
1137 HI.NamespaceScope =
"";
1139 HI.Type =
"NULL TYPE";
1141 HI.AccessSpecifier =
"public";
1145 Unknown [[^abc]] = invalid;
1150 HI.Kind = index::SymbolKind::Variable;
1151 HI.NamespaceScope =
"";
1152 HI.Definition =
"int abc";
1154 HI.AccessSpecifier =
"public";
1158 void fun(int arg_a, int &arg_b) {};
1166 HI.Kind = index::SymbolKind::Variable;
1167 HI.NamespaceScope =
"";
1168 HI.Definition =
"int b = 2";
1169 HI.LocalScope =
"code::";
1172 HI.CalleeArgInfo.emplace();
1173 HI.CalleeArgInfo->Name =
"arg_b";
1174 HI.CalleeArgInfo->Type =
"int &";
1180 explicit Foo(int arg_a) {}
1182 template<class T, class... Args>
1183 T make(Args&&... args)
1190 auto foo = make<Foo>([[^a]]);
1195 HI.Kind = index::SymbolKind::Variable;
1196 HI.NamespaceScope =
"";
1197 HI.Definition =
"int a = 1";
1198 HI.LocalScope =
"code::";
1201 HI.CalleeArgInfo.emplace();
1202 HI.CalleeArgInfo->Name =
"arg_a";
1203 HI.CalleeArgInfo->Type =
"int";
1208 void foobar(const float &arg);
1216 HI.Kind = index::SymbolKind::Variable;
1217 HI.NamespaceScope =
"";
1218 HI.Definition =
"int a = 0";
1219 HI.LocalScope =
"main::";
1222 HI.CalleeArgInfo.emplace();
1223 HI.CalleeArgInfo->Name =
"arg";
1224 HI.CalleeArgInfo->Type =
"const float &";
1230 explicit Foo(const float& arg) {}
1239 HI.Kind = index::SymbolKind::Variable;
1240 HI.NamespaceScope =
"";
1241 HI.Definition =
"int a = 0";
1242 HI.LocalScope =
"main::";
1245 HI.CalleeArgInfo.emplace();
1246 HI.CalleeArgInfo->Name =
"arg";
1247 HI.CalleeArgInfo->Type =
"const float &";
1252 void fun(int arg_a, const int &arg_b) {};
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 &";
1268 void fun(int arg_a, const int &arg_b) {};
1275 HI.Name = "expression";
1276 HI.Kind = index::SymbolKind::Unknown;
1279 HI.CalleeArgInfo.emplace();
1280 HI.CalleeArgInfo->Name =
"arg_b";
1281 HI.CalleeArgInfo->Type =
"const int &";
1286 int add(int lhs, int rhs);
1292 HI.Name = "expression";
1293 HI.Kind = index::SymbolKind::Unknown;
1296 HI.CalleeArgInfo.emplace();
1297 HI.CalleeArgInfo->Name =
"lhs";
1298 HI.CalleeArgInfo->Type =
"int";
1303 void foobar(const float &arg);
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 &";
1320 void fun(int arg_a = 3, int arg_b = 4) {}
1330 HI.Kind = index::SymbolKind::Variable;
1331 HI.NamespaceScope =
"";
1332 HI.Definition =
"int a = 1";
1333 HI.LocalScope =
"code::";
1336 HI.CalleeArgInfo.emplace();
1337 HI.CalleeArgInfo->Name =
"arg_a";
1338 HI.CalleeArgInfo->Type =
"int";
1339 HI.CalleeArgInfo->Default =
"3";
1355 HI.Kind = index::SymbolKind::Variable;
1356 HI.NamespaceScope =
"";
1357 HI.Definition =
"const int x = 0";
1358 HI.LocalScope =
"bar::";
1360 HI.Type =
"const int";
1361 HI.CalleeArgInfo.emplace();
1362 HI.CalleeArgInfo->Type =
"Foo";
1373 HI.Kind = index::SymbolKind::Field;
1374 HI.NamespaceScope =
"";
1375 HI.Definition =
"int xx";
1376 HI.LocalScope =
"Foo::";
1378 HI.AccessSpecifier =
"public";
1388 HI.Kind = index::SymbolKind::Field;
1389 HI.NamespaceScope =
"";
1390 HI.Definition =
"int yy";
1391 HI.LocalScope =
"Foo::";
1393 HI.AccessSpecifier =
"public";
1400 constexpr Foo k2 = {
1401 ^[[{]]1} // FIXME: why the hover range is 1 character?
1405 HI.Name = "expression";
1406 HI.Kind = index::SymbolKind::Unknown;
1407 HI.Type =
"int[10]";
1414 template <int Size> m_int ^[[arr]][Size];
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}};
1428 template <int Size> m_int arr[Size];
1430 template <> m_int ^[[arr]]<4>[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]";
1441 template<typename T>
1447 TestHover<int>::Type ^[[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"};
1460 template<typename T>
1461 void ^[[foo]](T arg) {}
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}};
1476 template<typename T>
1477 using ^[[alias]] = T;
1481 HI.NamespaceScope =
"";
1483 HI.Kind = index::SymbolKind::TypeAlias;
1484 HI.Definition =
"template <typename T> using alias = T";
1486 HI.TemplateParameters = {
1487 {{
"typename"}, std::string(
"T"), std::nullopt}};
1491 template<typename T>
1494 template<typename T>
1495 using ^[[AA]] = A<T>;
1499 HI.NamespaceScope =
"";
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}};
1515 HI.NamespaceScope =
"";
1517 HI.Kind = index::SymbolKind::Variable;
1518 HI.Definition =
"m_int arr[10]";
1519 HI.Type = {
"m_int[10]",
"int[10]"};
1525 extern m_int ^[[arr]][];
1529 HI.NamespaceScope =
"";
1531 HI.Kind = index::SymbolKind::Variable;
1532 HI.Definition =
"extern m_int arr[]";
1533 HI.Type = {
"m_int[]",
"int[]"};
1541 m_int ^[[arr]][Size];
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]"};
1562 HI.NamespaceScope = "";
1563 HI.LocalScope =
"Foo::";
1565 HI.Kind = index::SymbolKind::Field;
1566 HI.Definition =
"char y : 1";
1572 HI.AccessSpecifier =
"public";
1574 for (
const auto &Case : Cases) {
1575 SCOPED_TRACE(Case.Code);
1579 TU.ExtraArgs.push_back(
"-std=c++20");
1582 TU.ExtraArgs.push_back(
"--target=x86_64-pc-linux-gnu");
1583 auto AST = TU.build();
1588 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
1592 Case.ExpectedBuilder(Expected);
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);
1617 const char *
const Code;
1618 const std::string ClangLanguageFlag;
1619 const char *
const ExpectedDefinitionLanguage;
1620 } Cases[] = {{R
"cpp(
1621 void [[some^Global]]() {}
1625 void [[some^Global]]() {}
1627 "-xobjective-c++",
"objective-cpp"},
1629 void [[some^Global]]() {}
1631 "-xobjective-c",
"objective-c"}};
1632 for (
const auto &Case : Cases) {
1633 SCOPED_TRACE(Case.Code);
1637 if (!Case.ClangLanguageFlag.empty())
1638 TU.ExtraArgs.push_back(Case.ClangLanguageFlag);
1639 auto AST = TU.build();
1641 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
1644 EXPECT_STREQ(H->DefinitionLanguage, Case.ExpectedDefinitionLanguage);
1650 const char *
const Code;
1651 const llvm::StringRef ClangLanguageFlag;
1652 const char *
const ExpectedAccessSpecifier;
1671 "-xobjective-c",
""},
1677 "-xobjective-c++",
"public"},
1687 "-xobjective-c",
""},
1695 "-xobjective-c++",
""},
1697 for (
const auto &Case : Cases) {
1698 SCOPED_TRACE(Case.Code);
1699 SCOPED_TRACE(Case.ClangLanguageFlag);
1703 if (!Case.ClangLanguageFlag.empty())
1704 TU.ExtraArgs.push_back(Case.ClangLanguageFlag.str());
1705 auto AST = TU.build();
1707 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
1710 EXPECT_EQ(H->AccessSpecifier, Case.ExpectedAccessSpecifier);
1715 const llvm::StringRef CodePrefix = R
"cpp(
1717class Derived : public Base {};
1721 CustomClass(const Base &x) {}
1722 CustomClass(int &x) {}
1723 CustomClass(float x) {}
1724 CustomClass(int x, int y) {}
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) {}
1738 int &int_ref = int_x;
1739 const int &int_const_ref = int_x;
1741 const Base &base_const_ref = base;
1745 const llvm::StringRef CodeSuffix =
"}";
1748 const char *
const Code;
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},
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},
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},
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},
1786 for (
const auto &Test : Tests) {
1787 SCOPED_TRACE(Test.Code);
1789 const auto Code = (CodePrefix + Test.Code + CodeSuffix).str();
1792 TU.ExtraArgs.push_back(
"-std=c++17");
1793 auto AST = TU.build();
1794 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
1796 EXPECT_EQ(H->CallPassType->PassBy, Test.PassBy);
1797 EXPECT_EQ(H->CallPassType->Converted, Test.Converted);
1802 llvm::StringRef Tests[] = {
1806 "decltype(au^to) x = 0;",
1808 R
"cpp(// Lambda auto parameter
1809 auto lamb = [](a^uto){};
1811 R"cpp(// non-named decls don't get hover. Don't crash!
1812 ^static_assert(1, "");
1814 R"cpp(// non-evaluatable expr
1815 template <typename T> void foo() {
1816 (void)[[size^of]](T);
1818 R"cpp(// should not crash on invalid semantic form of init-list-expr.
1824 constexpr Foo s = ^{
1830 "auto x = ^(int){42};",
1834 "auto x = ^nullptr;",
1837 for (
const auto &Test : Tests) {
1842 TU.ExtraArgs.push_back(
"-std=c++17");
1843 auto AST = TU.build();
1844 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
1851 struct Foo { int x; };
1852 int y = __builtin_o^ffsetof(Foo, x);
1855 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
1857 EXPECT_EQ(H->Name,
"expression");
1858 EXPECT_EQ(H->Kind, index::SymbolKind::Unknown);
1863 const char *
const Code;
1864 const std::function<void(
HoverInfo &)> ExpectedBuilder;
1866 {
"auto x = [['^A']]; // character literal",
1868 HI.Name =
"expression";
1870 HI.Value =
"65 (0x41)";
1872 {
"auto s = ^[[\"Hello, world!\"]]; // string literal",
1874 HI.Name =
"string-literal";
1876 HI.Type =
"const char[14]";
1879 R
"cpp(// Local variable
1883 int test1 = bonjour;
1887 HI.Name = "bonjour";
1888 HI.Kind = index::SymbolKind::Variable;
1889 HI.NamespaceScope =
"";
1890 HI.LocalScope =
"main::";
1892 HI.Definition =
"int bonjour";
1895 R
"cpp(// Local variable in method
1904 HI.Name = "bonjour";
1905 HI.Kind = index::SymbolKind::Variable;
1906 HI.NamespaceScope =
"";
1907 HI.LocalScope =
"s::method::";
1909 HI.Definition =
"int bonjour";
1917 ns1::[[My^Class]]* Params;
1921 HI.Name = "MyClass";
1922 HI.Kind = index::SymbolKind::Struct;
1923 HI.NamespaceScope =
"ns1::";
1924 HI.Definition =
"struct MyClass {}";
1932 ns1::[[My^Class]]* Params;
1936 HI.Name = "MyClass";
1937 HI.Kind = index::SymbolKind::Class;
1938 HI.NamespaceScope =
"ns1::";
1939 HI.Definition =
"class MyClass {}";
1944 union MyUnion { int x; int y; };
1947 ns1::[[My^Union]] Params;
1951 HI.Name = "MyUnion";
1952 HI.Kind = index::SymbolKind::Union;
1953 HI.NamespaceScope =
"ns1::";
1954 HI.Definition =
"union MyUnion {}";
1957 R
"cpp(// Function definition via pointer
1960 auto *X = &^[[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";
1972 {{
"int"}, std::nullopt, std::nullopt},
1976 R
"cpp(// Function declaration via call
1979 return ^[[foo]](42);
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";
1991 {{
"int"}, std::nullopt, std::nullopt},
1996 struct Foo { int x; };
2004 HI.Kind = index::SymbolKind::Field;
2005 HI.NamespaceScope =
"";
2006 HI.LocalScope =
"Foo::";
2008 HI.Definition =
"int x";
2011 R
"cpp(// Field with initialization
2012 struct Foo { int x = 5; };
2020 HI.Kind = index::SymbolKind::Field;
2021 HI.NamespaceScope =
"";
2022 HI.LocalScope =
"Foo::";
2024 HI.Definition =
"int x = 5";
2027 R
"cpp(// Static field
2028 struct Foo { static int x; };
2035 HI.Kind = index::SymbolKind::StaticProperty;
2036 HI.NamespaceScope =
"";
2037 HI.LocalScope =
"Foo::";
2039 HI.Definition =
"static int x";
2042 R
"cpp(// Field, member initializer
2045 Foo() : ^[[x]](0) {}
2050 HI.Kind = index::SymbolKind::Field;
2051 HI.NamespaceScope =
"";
2052 HI.LocalScope =
"Foo::";
2054 HI.Definition =
"int x";
2057 R
"cpp(// Field, GNU old-style field designator
2058 struct Foo { int x; };
2060 Foo bar = { ^[[x]] : 1 };
2065 HI.Kind = index::SymbolKind::Field;
2066 HI.NamespaceScope =
"";
2067 HI.LocalScope =
"Foo::";
2069 HI.Definition =
"int x";
2074 R
"cpp(// Field, field designator
2075 struct Foo { int x; int y; };
2077 Foo bar = { .^[[x]] = 2, .y = 2 };
2082 HI.Kind = index::SymbolKind::Field;
2083 HI.NamespaceScope =
"";
2084 HI.LocalScope =
"Foo::";
2086 HI.Definition =
"int x";
2089 R
"cpp(// Field, offsetof
2090 struct Foo { int x; int y; };
2091 int z = __builtin_offsetof(Foo, ^[[x]]);
2095 HI.Kind = index::SymbolKind::Field;
2096 HI.NamespaceScope =
"";
2097 HI.LocalScope =
"Foo::";
2099 HI.Definition =
"int x";
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);
2110 HI.Kind = index::SymbolKind::Field;
2111 HI.NamespaceScope =
"";
2112 HI.LocalScope =
"A::";
2114 HI.Definition =
"Inner B";
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]]);
2125 HI.Kind = index::SymbolKind::Field;
2126 HI.NamespaceScope =
"";
2127 HI.LocalScope =
"Inner::";
2129 HI.Definition =
"int c";
2133 R
"cpp(// Method call
2134 struct Foo { int x(); };
2142 HI.Kind = index::SymbolKind::InstanceMethod;
2143 HI.NamespaceScope =
"";
2144 HI.LocalScope =
"Foo::";
2146 HI.Definition =
"int x()";
2147 HI.ReturnType =
"int";
2148 HI.Parameters = std::vector<HoverInfo::Param>{};
2151 R
"cpp(// Static method call
2152 struct Foo { static int x(); };
2159 HI.Kind = index::SymbolKind::StaticMethod;
2160 HI.NamespaceScope =
"";
2161 HI.LocalScope =
"Foo::";
2163 HI.Definition =
"static int x()";
2164 HI.ReturnType =
"int";
2165 HI.Parameters = std::vector<HoverInfo::Param>{};
2176 HI.Kind = index::SymbolKind::TypeAlias;
2177 HI.NamespaceScope =
"";
2178 HI.Definition =
"typedef int Foo";
2180 HI.Documentation =
"Typedef";
2183 R
"cpp(// Typedef with embedded definition
2184 typedef struct Bar {} 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";
2200 struct Foo { static void bar(); };
2202 int main() { ^[[ns]]::Foo::bar(); }
2206 HI.Kind = index::SymbolKind::Namespace;
2207 HI.NamespaceScope =
"";
2208 HI.Definition =
"namespace ns {}";
2211 R
"cpp(// Anonymous namespace
2215 } // anonymous namespace
2217 int main() { ns::[[f^oo]]++; }
2221 HI.Kind = index::SymbolKind::Variable;
2222 HI.NamespaceScope =
"ns::";
2224 HI.Definition =
"int foo";
2227 R
"cpp(// Function definition via using declaration
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>{};
2247 R
"cpp( // using declaration and two possible function declarations
2248 namespace ns { void foo(int); void foo(char); }
2250 template <typename T> void bar() { [[f^oo]](T{}); }
2254 HI.Kind = index::SymbolKind::Using;
2255 HI.NamespaceScope =
"";
2256 HI.Definition =
"using ns::foo";
2261 int main() { return ^[[MACRO]]; }
2267 HI.Kind = index::SymbolKind::Macro;
2268 HI.Definition =
"#define MACRO 0\n\n"
2275 #define MACRO2 ^[[MACRO]]
2279 HI.Kind = index::SymbolKind::Macro;
2280 HI.Definition =
"#define MACRO 0";
2289 int main() ^[[MACRO]]
2293 HI.Kind = index::SymbolKind::Macro;
2295 R
"cpp(#define MACRO \
2306 R"cpp(// Forward class declaration
2313 HI.Kind = index::SymbolKind::Class;
2314 HI.NamespaceScope =
"";
2315 HI.Definition =
"class Foo {}";
2316 HI.Documentation =
"Forward class declaration";
2319 R
"cpp(// Function declaration
2321 void g() { [[f^oo]](); }
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>{};
2335 R
"cpp(// Enum declaration
2340 [[Hel^lo]] hello = ONE;
2345 HI.Kind = index::SymbolKind::Enum;
2346 HI.NamespaceScope =
"";
2347 HI.Definition =
"enum Hello {}";
2348 HI.Documentation =
"Enum declaration";
2356 Hello hello = [[O^NE]];
2361 HI.Kind = index::SymbolKind::EnumConstant;
2362 HI.NamespaceScope =
"";
2363 HI.LocalScope =
"Hello::";
2364 HI.Type =
"enum Hello";
2365 HI.Definition =
"ONE";
2369 R
"cpp(// C++20's using enum
2375 Hello hello = [[O^NE]];
2380 HI.Kind = index::SymbolKind::EnumConstant;
2381 HI.NamespaceScope =
"";
2382 HI.LocalScope =
"Hello::";
2383 HI.Type =
"enum Hello";
2384 HI.Definition =
"ONE";
2388 R
"cpp(// Enumerator in anonymous enum
2393 int hello = [[O^NE]];
2398 HI.Kind = index::SymbolKind::EnumConstant;
2399 HI.NamespaceScope =
"";
2402 HI.Type =
"enum (unnamed)";
2403 HI.Definition =
"ONE";
2407 R
"cpp(// Global variable
2408 static int hey = 10;
2415 HI.Kind = index::SymbolKind::Variable;
2416 HI.NamespaceScope =
"";
2418 HI.Definition =
"static int hey = 10";
2419 HI.Documentation =
"Global variable";
2421 HI.Value =
"10 (0xa)";
2424 R
"cpp(// Global variable in namespace
2426 static long long hey = -36637162602497;
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)";
2441 R
"cpp(// Field in anonymous struct
2451 HI.Kind = index::SymbolKind::Field;
2452 HI.NamespaceScope =
"";
2453 HI.LocalScope =
"(anonymous struct)::";
2455 HI.Definition =
"int hello";
2458 R
"cpp(// Templated function
2459 template <typename T>
2463 void g() { auto x = [[f^oo]]<int>(); }
2467 HI.Kind = index::SymbolKind::Function;
2468 HI.NamespaceScope =
"";
2470 HI.Definition =
"template <> int foo<int>()";
2471 HI.Documentation =
"Templated function";
2472 HI.ReturnType =
"int";
2473 HI.Parameters = std::vector<HoverInfo::Param>{};
2478 R
"cpp(// Anonymous union
2484 void g() { struct outer o; o.v.[[d^ef]]++; }
2488 HI.Kind = index::SymbolKind::Field;
2489 HI.NamespaceScope =
"";
2490 HI.LocalScope =
"outer::(anonymous union)::";
2492 HI.Definition =
"int def";
2495 R
"cpp(// documentation from index
2496 int nextSymbolIsAForwardDeclFromIndexWithNoLocalDocs;
2498 void g() { [[ind^exSymbol]](); }
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";
2511 R
"cpp(// Simple initialization with auto
2518 HI.Kind = index::SymbolKind::TypeAlias;
2519 HI.Definition =
"int";
2522 R
"cpp(// Simple initialization with const auto
2524 const ^[[auto]] i = 1;
2529 HI.Kind = index::SymbolKind::TypeAlias;
2530 HI.Definition =
"int";
2533 R
"cpp(// Simple initialization with const auto&
2535 const ^[[auto]]& i = 1;
2540 HI.Kind = index::SymbolKind::TypeAlias;
2541 HI.Definition =
"int";
2544 R
"cpp(// Simple initialization with auto&
2552 HI.Kind = index::SymbolKind::TypeAlias;
2553 HI.Definition =
"int";
2556 R
"cpp(// Simple initialization with auto*
2564 HI.Kind = index::SymbolKind::TypeAlias;
2565 HI.Definition =
"int";
2568 R
"cpp(// Simple initialization with auto from pointer
2576 HI.Kind = index::SymbolKind::TypeAlias;
2577 HI.Definition =
"int *";
2580 R
"cpp(// Auto with initializer list.
2584 class initializer_list { const _E *a, *b; };
2587 ^[[auto]] i = {1,2};
2592 HI.Kind = index::SymbolKind::TypeAlias;
2593 HI.Definition =
"std::initializer_list<int>";
2596 R
"cpp(// User defined conversion to auto
2598 operator ^[[auto]]() const { return 10; }
2603 HI.Kind = index::SymbolKind::TypeAlias;
2604 HI.Definition =
"int";
2607 R
"cpp(// Simple initialization with decltype(auto)
2609 ^[[decltype]](auto) i = 1;
2613 HI.Name = "decltype";
2614 HI.Kind = index::SymbolKind::TypeAlias;
2615 HI.Definition =
"int";
2618 R
"cpp(// Simple initialization with const decltype(auto)
2621 ^[[decltype]](auto) i = j;
2625 HI.Name = "decltype";
2626 HI.Kind = index::SymbolKind::TypeAlias;
2627 HI.Definition =
"const int";
2630 R
"cpp(// Simple initialization with const& decltype(auto)
2634 ^[[decltype]](auto) i = j;
2638 HI.Name = "decltype";
2639 HI.Kind = index::SymbolKind::TypeAlias;
2640 HI.Definition =
"const int &";
2643 R
"cpp(// Simple initialization with & decltype(auto)
2647 ^[[decltype]](auto) i = j;
2651 HI.Name = "decltype";
2652 HI.Kind = index::SymbolKind::TypeAlias;
2653 HI.Definition =
"int &";
2656 R
"cpp(// simple trailing return type
2657 ^[[auto]] main() -> int {
2663 HI.Kind = index::SymbolKind::TypeAlias;
2664 HI.Definition =
"int";
2667 R
"cpp(// auto function return with trailing type
2669 ^[[auto]] test() -> decltype(Bar()) {
2675 HI.Kind = index::SymbolKind::TypeAlias;
2676 HI.Definition =
"Bar";
2677 HI.Documentation =
"auto function return with trailing type";
2680 R
"cpp(// trailing return type
2682 auto test() -> ^[[decltype]](Bar()) {
2687 HI.Name = "decltype";
2688 HI.Kind = index::SymbolKind::TypeAlias;
2689 HI.Definition =
"Bar";
2690 HI.Documentation =
"trailing return type";
2693 R
"cpp(// auto in function return
2701 HI.Kind = index::SymbolKind::TypeAlias;
2702 HI.Definition =
"Bar";
2703 HI.Documentation =
"auto in function return";
2706 R
"cpp(// auto& in function return
2715 HI.Kind = index::SymbolKind::TypeAlias;
2716 HI.Definition =
"Bar";
2717 HI.Documentation =
"auto& in function return";
2720 R
"cpp(// auto* in function return
2729 HI.Kind = index::SymbolKind::TypeAlias;
2730 HI.Definition =
"Bar";
2731 HI.Documentation =
"auto* in function return";
2734 R
"cpp(// const auto& in function return
2736 const ^[[auto]]& test() {
2743 HI.Kind = index::SymbolKind::TypeAlias;
2744 HI.Definition =
"Bar";
2745 HI.Documentation =
"const auto& in function return";
2748 R
"cpp(// decltype(auto) in function return
2750 ^[[decltype]](auto) test() {
2755 HI.Name = "decltype";
2756 HI.Kind = index::SymbolKind::TypeAlias;
2757 HI.Definition =
"Bar";
2758 HI.Documentation =
"decltype(auto) in function return";
2761 R
"cpp(// decltype(auto) reference in function return
2762 ^[[decltype]](auto) test() {
2768 HI.Name = "decltype";
2769 HI.Kind = index::SymbolKind::TypeAlias;
2770 HI.Definition =
"int &";
2773 R
"cpp(// decltype lvalue reference
2776 ^[[decltype]](I) J = I;
2780 HI.Name = "decltype";
2781 HI.Kind = index::SymbolKind::TypeAlias;
2782 HI.Definition =
"int";
2785 R
"cpp(// decltype lvalue reference
2789 ^[[decltype]](K) J = I;
2793 HI.Name = "decltype";
2794 HI.Kind = index::SymbolKind::TypeAlias;
2795 HI.Definition =
"int &";
2798 R
"cpp(// decltype lvalue reference parenthesis
2801 ^[[decltype]]((I)) J = I;
2805 HI.Name = "decltype";
2806 HI.Kind = index::SymbolKind::TypeAlias;
2807 HI.Definition =
"int &";
2810 R
"cpp(// decltype rvalue reference
2813 ^[[decltype]](static_cast<int&&>(I)) J = static_cast<int&&>(I);
2817 HI.Name = "decltype";
2818 HI.Kind = index::SymbolKind::TypeAlias;
2819 HI.Definition =
"int &&";
2822 R
"cpp(// decltype rvalue reference function call
2826 ^[[decltype]](bar()) J = bar();
2830 HI.Name = "decltype";
2831 HI.Kind = index::SymbolKind::TypeAlias;
2832 HI.Definition =
"int &&";
2835 R
"cpp(// decltype of function with trailing return type.
2837 auto test() -> decltype(Bar()) {
2841 ^[[decltype]](test()) i = test();
2845 HI.Name = "decltype";
2846 HI.Kind = index::SymbolKind::TypeAlias;
2847 HI.Definition =
"Bar";
2849 "decltype of function with trailing return type.";
2852 R
"cpp(// decltype of var with decltype.
2856 ^[[decltype]](J) K = J;
2860 HI.Name = "decltype";
2861 HI.Kind = index::SymbolKind::TypeAlias;
2862 HI.Definition =
"int";
2865 R
"cpp(// decltype of dependent type
2866 template <typename T>
2868 using Y = ^[[decltype]](T::Z);
2872 HI.Name = "decltype";
2873 HI.Kind = index::SymbolKind::TypeAlias;
2874 HI.Definition =
"<dependent type>";
2877 R
"cpp(// More complicated structured types.
2879 ^[[auto]] (*foo)() = bar;
2883 HI.Kind = index::SymbolKind::TypeAlias;
2884 HI.Definition =
"int";
2887 R
"cpp(// Should not crash when evaluating the initializer.
2889 void test() { Test && [[te^st]] = {}; }
2893 HI.Kind = index::SymbolKind::Variable;
2894 HI.NamespaceScope =
"";
2895 HI.LocalScope =
"test::";
2896 HI.Type =
"Test &&";
2897 HI.Definition =
"Test &&test = {}";
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",
2906 HI.Kind = index::SymbolKind::Parameter;
2907 HI.NamespaceScope =
"";
2908 HI.LocalScope =
"Foo::foo::";
2910 HI.Definition =
"Bar x = <recovery - expr>()";
2913 R
"cpp(// auto on alias
2914 typedef int int_type;
2915 ^[[auto]] x = int_type();
2919 HI.Kind = index::SymbolKind::TypeAlias;
2920 HI.Definition =
"int_type // aka: int";
2923 R
"cpp(// auto on alias
2925 typedef cls cls_type;
2926 ^[[auto]] y = cls_type();
2930 HI.Kind = index::SymbolKind::TypeAlias;
2931 HI.Definition =
"cls_type // aka: cls";
2932 HI.Documentation =
"auto on alias";
2935 R
"cpp(// auto on alias
2938 ^[[auto]] z = templ<int>();
2942 HI.Kind = index::SymbolKind::TypeAlias;
2943 HI.Definition =
"templ<int>";
2944 HI.Documentation =
"auto on alias";
2947 R
"cpp(// Undeduced auto declaration
2948 template<typename T>
2955 HI.Kind = index::SymbolKind::TypeAlias;
2956 HI.Definition =
"T";
2959 R
"cpp(// Undeduced auto return type
2960 template<typename T>
2967 HI.Kind = index::SymbolKind::TypeAlias;
2968 HI.Definition =
"/* not deduced */";
2971 R
"cpp(// Template auto parameter
2972 template<[[a^uto]] T>
2980 HI.Kind = index::SymbolKind::TypeAlias;
2981 HI.Definition =
"/* not deduced */";
2984 R
"cpp(// Undeduced decltype(auto) return type
2985 template<typename T>
2986 ^[[decltype]](auto) foo() {
2991 HI.Name = "decltype";
2992 HI.Kind = index::SymbolKind::TypeAlias;
2993 HI.Definition =
"/* not deduced */";
2996 R
"cpp(// should not crash.
2997 template <class T> struct cls {
3001 auto test = cls<int>().[[m^ethod]]();
3004 HI.Definition = "int method()";
3005 HI.Kind = index::SymbolKind::InstanceMethod;
3006 HI.NamespaceScope =
"";
3007 HI.LocalScope =
"cls<int>::";
3009 HI.Parameters.emplace();
3010 HI.ReturnType =
"int";
3014 R
"cpp(// type of nested templates.
3015 template <class T> struct cls {};
3016 cls<cls<cls<int>>> [[fo^o]];
3019 HI.Definition = "cls<cls<cls<int>>> foo";
3020 HI.Kind = index::SymbolKind::Variable;
3021 HI.NamespaceScope =
"";
3023 HI.Type =
"cls<cls<cls<int>>>";
3026 R
"cpp(// type of nested templates.
3027 template <class T> struct cls {};
3028 [[cl^s]]<cls<cls<int>>> foo;
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.";
3038 R
"cpp(// type with decltype
3040 decltype(a) [[b^]] = a;)cpp",
3042 HI.Definition = "decltype(a) b = a";
3043 HI.Kind = index::SymbolKind::Variable;
3044 HI.NamespaceScope =
"";
3049 R
"cpp(// type with decltype
3052 decltype(c) [[b^]] = a;)cpp",
3054 HI.Definition = "decltype(c) b = a";
3055 HI.Kind = index::SymbolKind::Variable;
3056 HI.NamespaceScope =
"";
3061 R
"cpp(// type with decltype
3063 const decltype(a) [[b^]] = a;)cpp",
3065 HI.Definition = "const decltype(a) b = a";
3066 HI.Kind = index::SymbolKind::Variable;
3067 HI.NamespaceScope =
"";
3069 HI.Type =
"const int";
3072 R
"cpp(// type with decltype
3074 auto [[f^oo]](decltype(a) x) -> decltype(a) { return 0; })cpp",
3076 HI.Definition = "auto foo(decltype(a) x) -> decltype(a)";
3077 HI.Kind = index::SymbolKind::Function;
3078 HI.NamespaceScope =
"";
3080 HI.Type =
"auto (int) -> int";
3081 HI.ReturnType =
"int";
3082 HI.Parameters = {{{
"int"}, std::string(
"x"), std::nullopt}};
3085 R
"cpp(// sizeof expr
3087 (void)[[size^of]](char);
3090 HI.Name = "expression";
3091 HI.Type = {
"__size_t",
"unsigned long"};
3095 R
"cpp(// alignof expr
3097 (void)[[align^of]](char);
3100 HI.Name = "expression";
3101 HI.Type = {
"__size_t",
"unsigned long"};
3106 template <typename T = int>
3107 void foo(const T& = T()) {
3112 HI.Kind = index::SymbolKind::Function;
3113 HI.Type =
"void (const int &)";
3114 HI.ReturnType =
"void";
3116 {{
"const int &"}, std::nullopt, std::string(
"T()")}};
3117 HI.Definition =
"template <> void foo<int>(const int &)";
3118 HI.NamespaceScope =
"";
3121 R
"cpp(// should not crash
3129 HI.Kind = index::SymbolKind::Field;
3130 HI.LocalScope =
"ObjC::";
3131 HI.NamespaceScope =
"";
3132 HI.Definition =
"char data";
3138 @interface Interface
3139 @property(retain) [[MYOb^ject]] *x;
3143 HI.Name = "MYObject";
3144 HI.Kind = index::SymbolKind::Class;
3145 HI.NamespaceScope =
"";
3146 HI.Definition =
"@interface MYObject\n@end";
3152 @interface Interface
3153 - (void)doWith:([[MYOb^ject]] *)object;
3157 HI.Name = "MYObject";
3158 HI.Kind = index::SymbolKind::Class;
3159 HI.NamespaceScope =
"";
3160 HI.Definition =
"@interface MYObject\n@end";
3175 HI.Definition =
"ns::Foo *";
3178 R
"cpp(// this expr for template class
3180 template <typename T>
3190 HI.Definition =
"const ns::Foo<T> *";
3193 R
"cpp(// this expr for specialization class
3195 template <typename T> class Foo {};
3206 HI.Definition =
"ns::Foo<int> *";
3209 R
"cpp(// this expr for partial specialization struct
3211 template <typename T, typename F> struct Foo {};
3212 template <typename F>
3213 struct Foo<int, F> {
3222 HI.Definition =
"const ns::Foo<int, F> *";
3228 @interface MYObject (Private)
3229 @property(nonatomic, assign) int privateField;
3232 int someFunction() {
3233 MYObject *obj = [MYObject sharedInstance];
3234 return obj.[[private^Field]];
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;";
3247 @protocol MYProtocol
3248 @property(nonatomic, assign) int prop1;
3251 int someFunction() {
3252 id<MYProtocol> obj = 0;
3253 return obj.[[pro^p1]];
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;";
3266 @protocol MYProtocol
3271 @interface MYObject (Ext) <[[MYProt^ocol]]>
3275 HI.Name = "MYProtocol";
3276 HI.Kind = index::SymbolKind::Protocol;
3277 HI.NamespaceScope =
"";
3278 HI.Definition =
"@protocol MYProtocol\n@end";
3284 @implementation Foo(Private)
3285 + (int)somePrivateMethod {
3286 int [[res^ult]] = 2;
3293 HI.Definition =
"int result = 2";
3294 HI.Kind = index::SymbolKind::Variable;
3296 HI.LocalScope =
"+[Foo(Private) somePrivateMethod]::";
3297 HI.NamespaceScope =
"";
3305 - (int)variadicArgMethod:(id)first, ... {
3306 int [[res^ult]] = 0;
3313 HI.Definition =
"int result = 0";
3314 HI.Kind = index::SymbolKind::Variable;
3316 HI.LocalScope =
"-[Foo variadicArgMethod:, ...]::";
3317 HI.NamespaceScope =
"";
3322 typedef struct MyRect {} MyRect;
3325 @property(nonatomic) MyRect frame;
3334 v.frame = [[foo^bar]]();
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();
3347 void foo(int * __attribute__(([[non^null]], noescape)) );
3350 HI.Name = "nonnull";
3351 HI.Kind = index::SymbolKind::Unknown;
3352 HI.Definition =
"__attribute__((nonnull))";
3353 HI.Documentation = Attr::getDocumentation(attr::NonNull).str();
3358 struct strong_ordering {
3360 constexpr operator int() const { return n; }
3361 static const strong_ordering equal, greater, less;
3363 constexpr strong_ordering strong_ordering::equal = {0};
3364 constexpr strong_ordering strong_ordering::greater = {1};
3365 constexpr strong_ordering strong_ordering::less = {-1};
3372 auto operator<=>(const Foo&) const = default;
3375 bool x = Foo(1) [[!^=]] Foo(2);
3378 HI.Type = "bool (const Foo &) const noexcept";
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 =
"";
3387 "bool operator==(const Foo &) const noexcept = default";
3388 HI.Documentation =
"Foo spaceship";
3394 IndexSym.Documentation =
"comment from index";
3400 for (
const auto &Case : Cases) {
3401 SCOPED_TRACE(Case.Code);
3405 TU.ExtraArgs.push_back(
"-std=c++20");
3406 TU.ExtraArgs.push_back(
"-xobjective-c++");
3408 TU.ExtraArgs.push_back(
"-Wno-gnu-designator");
3411 TU.ExtraArgs.push_back(
"--target=x86_64-pc-linux-gnu");
3412 auto AST = TU.build();
3416 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(), Index.get());
3420 Case.ExpectedBuilder(Expected);
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);
3441 const std::function<void(
HoverInfo &)> ExpectedBuilder;
3442 } Cases[] = {{R
"cpp(
3446 [](HoverInfo &HI) { HI.Provider = ""; }},
3451 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3456 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3461 [](HoverInfo &HI) { HI.Provider = ""; }},
3466 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3471 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3478 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3487 [](HoverInfo &HI) { HI.Provider = "\"foo.h\""; }},
3490 using namespace fo^o;
3492 [](HoverInfo &HI) { HI.Provider = ""; }},
3495 for (
const auto &Case : Cases) {
3497 SCOPED_TRACE(Code.code());
3501 TU.Code = Code.code();
3502 TU.AdditionalFiles[
"foo.h"] = guard(R
"cpp(
3505 Foo& operator+(const Foo, const Foo);
3507 TU.AdditionalFiles["all.h"] = guard(
"#include \"foo.h\"");
3509 auto AST = TU.build();
3510 auto H =
getHover(
AST, Code.point(), format::getLLVMStyle(),
nullptr);
3513 Case.ExpectedBuilder(Expected);
3515 EXPECT_EQ(H->Provider, Expected.Provider);
3522 HIFoo.Provider =
"\"foo.h\"";
3525 HIFooBar.
Name =
"foo";
3526 HIFooBar.Provider =
"<bar.h>";
3529 llvm::StringRef ExpectedMarkdown;
3530 } Cases[] = {{HIFoo,
"### `foo`\n\nprovided by `\"foo.h\"`"},
3531 {HIFooBar,
"### `foo`\n\nprovided by `<bar.h>`"}};
3533 for (
const auto &Case : Cases)
3540 const std::function<void(
HoverInfo &)> ExpectedBuilder;
3541 } Cases[] = {{R
"cpp(
3543 int fstBar = bar1();
3544 int another= bar1(0);
3545 int sndBar = bar2();
3550 HI.UsedSymbolNames = {"BAR",
"Bar",
"bar1",
"bar2"};
3554 std::vector<int> vec;
3556 [](HoverInfo &HI) { HI.UsedSymbolNames = {"vector"}; }}};
3557 for (
const auto &Case : Cases) {
3559 SCOPED_TRACE(Code.code());
3563 TU.Code = Code.code();
3564 TU.AdditionalFiles[
"bar.h"] = guard(R
"cpp(
3571 TU.AdditionalFiles["system/vector"] = guard(R
"cpp(
3577 TU.ExtraArgs.push_back("-isystem" +
testPath(
"system"));
3579 auto AST = TU.build();
3580 auto H =
getHover(
AST, Code.point(), format::getLLVMStyle(),
nullptr);
3583 Case.ExpectedBuilder(Expected);
3585 EXPECT_EQ(H->UsedSymbolNames, Expected.UsedSymbolNames);
3591 template <typename T> class X {};
3599 auto AST = TU.build();
3602 IndexSym.Documentation =
"comment from index";
3608 for (
const auto &P :
T.points()) {
3609 auto H =
getHover(
AST, P, format::getLLVMStyle(), Index.get());
3611 EXPECT_EQ(H->Documentation, IndexSym.Documentation);
3618 template <typename T> class X {};
3620 template <typename T> void bar() {}
3622 template <typename T> T baz;
3627 au^to T = ba^z<X<int>>;
3632 auto AST = TU.build();
3633 for (
const auto &P :
T.points()) {
3634 auto H =
getHover(
AST, P, format::getLLVMStyle(),
nullptr);
3636 EXPECT_EQ(H->Documentation,
"doc");
3643 template<typename T> T foo(T);
3645 // Setter variable heuristic might fail if the callexpr is broken.
3646 struct X { int Y; void [[^setY]](float) { Y = foo(undefined); } };)cpp");
3649 auto AST = TU.build();
3650 for (
const auto &P :
T.points())
3651 getHover(
AST, P, format::getLLVMStyle(),
nullptr);
3656 constexpr unsigned long value = -1; // wrap around
3657 void foo() { va^lue; }
3660 getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
3665 constexpr __int128_t value = -4;
3666 void foo() { va^lue; }
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);
3674 EXPECT_EQ(H->Value,
"-4 (0xfffffffc)");
3680 template <typename T> class $doc1^X {};
3682 template <> class $doc2^X<int> {};
3684 template <typename T> class $doc3^X<T*> {};
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);
3697 EXPECT_EQ(H->Documentation, Comment);
3704 const std::function<void(
HoverInfo &)> Builder;
3705 llvm::StringRef ExpectedMarkdownRender;
3706 llvm::StringRef ExpectedDoxygenRender;
3710 HI.Kind = index::SymbolKind::Unknown;
3718 HI.Kind = index::SymbolKind::NamespaceAlias;
3721 R
"(namespace-alias foo)",
3722 R"(### namespace-alias `foo`)",
3726 HI.Kind = index::SymbolKind::Class;
3728 HI.TemplateParameters = {
3729 {{"typename"}, std::string(
"T"), std::nullopt},
3730 {{
"typename"}, std::string(
"C"), std::string(
"bool")},
3732 HI.Documentation =
"documentation";
3734 "template <typename T, typename C = bool> class Foo {}";
3736 HI.NamespaceScope.emplace();
3744template <typename T, typename C = bool> class Foo {})",
3749template <typename T, typename C = bool> class Foo {}
3758### Template Parameters
3761- `typename C = bool`
3768 HI.Kind = index::SymbolKind::Function;
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);
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) {}";
3786 "→ ret_type (aka can_ret_type)\n\n"
3789 "- type (aka can_type)\n"
3790 "- type foo (aka can_type)\n"
3791 "- type foo = default (aka can_type)\n"
3793 "// In namespace ns\n"
3794 "ret_type foo(params) {}",
3800ret_type foo(params) {}
3807- `type (aka can_type)`
3808- `type foo (aka can_type)`
3809- `type foo = default (aka can_type)`
3814`ret_type (aka can_ret_type)`)",
3818 HI.Kind = index::SymbolKind::Field;
3819 HI.LocalScope = "test::Bar::";
3822 HI.Type = {
"type",
"can_type"};
3823 HI.Definition =
"def";
3831Type: type (aka can_type)
3837Size: 4 bytes (+4 bytes padding), alignment 4 bytes
3850Type: `type (aka can_type)`
3856Size: 4 bytes (+4 bytes padding), alignment 4 bytes)",
3860 HI.Kind = index::SymbolKind::Field;
3861 HI.LocalScope = "test::Bar::";
3864 HI.Type = {
"type",
"can_type"};
3865 HI.Definition =
"def";
3873Type: type (aka can_type)
3877Offset: 4 bytes and 3 bits
3879Size: 25 bits (+4 bits padding), alignment 8 bytes
3892Type: `type (aka can_type)`
3896Offset: 4 bytes and 3 bits
3898Size: 25 bits (+4 bits padding), alignment 8 bytes)",
3902 HI.Kind = index::SymbolKind::Field;
3903 HI.AccessSpecifier = "public";
3905 HI.LocalScope =
"test::Bar::";
3906 HI.Definition =
"def";
3922 HI.Definition = "size_t method()";
3923 HI.AccessSpecifier =
"protected";
3924 HI.Kind = index::SymbolKind::InstanceMethod;
3925 HI.NamespaceScope =
"";
3926 HI.LocalScope =
"cls<int>::";
3928 HI.Parameters.emplace();
3929 HI.ReturnType = {
"size_t",
"unsigned long"};
3930 HI.Type = {
"size_t ()",
"unsigned long ()"};
3932 R
"(instance-method method
3934→ size_t (aka unsigned long)
3937protected: size_t method())",
3938 R"(### instance-method
3943protected: size_t method()
3949`size_t (aka unsigned long)`)",
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";
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";
3976public: cls(int a, int b = 5))",
3982public: cls(int a, int b = 5)
3993 HI.Kind = index::SymbolKind::Union;
3994 HI.AccessSpecifier = "private";
3996 HI.NamespaceScope =
"ns1::";
3997 HI.Definition =
"union foo {}";
4002private: union foo {})",
4008private: union foo {}
4013 HI.Kind = index::SymbolKind::Variable;
4015 HI.Definition =
"int foo = 3";
4016 HI.LocalScope =
"test::Bar::";
4019 HI.CalleeArgInfo.emplace();
4020 HI.CalleeArgInfo->Name =
"arg_a";
4021 HI.CalleeArgInfo->Type =
"int";
4022 HI.CalleeArgInfo->Default =
"7";
4052 HI.Kind = index::SymbolKind::Variable;
4054 HI.CalleeArgInfo.emplace();
4055 HI.CalleeArgInfo->Type =
"int";
4061 R"(### variable `foo`
4068 HI.Kind = index::SymbolKind::Variable;
4070 HI.Definition =
"int foo = 3";
4071 HI.LocalScope =
"test::Bar::";
4074 HI.CalleeArgInfo.emplace();
4075 HI.CalleeArgInfo->Name =
"arg_a";
4076 HI.CalleeArgInfo->Type =
"int";
4077 HI.CalleeArgInfo->Default =
"7";
4086Passed by reference as arg_a
4103Passed by reference as arg_a)",
4107 HI.Kind = index::SymbolKind::Variable;
4109 HI.Definition =
"int foo = 3";
4110 HI.LocalScope =
"test::Bar::";
4113 HI.CalleeArgInfo.emplace();
4114 HI.CalleeArgInfo->Name =
"arg_a";
4115 HI.CalleeArgInfo->Type = {
"alias_int",
"int"};
4116 HI.CalleeArgInfo->Default =
"7";
4125Passed as arg_a (converted to alias_int)
4142Passed as arg_a (converted to alias_int))",
4146 HI.Kind = index::SymbolKind::Macro;
4147 HI.Name = "PLUS_ONE";
4148 HI.Definition =
"#define PLUS_ONE(X) (X+1)\n\n"
4154#define PLUS_ONE(X) (X+1)
4162#define PLUS_ONE(X) (X+1)
4170 HI.Kind = index::SymbolKind::Variable;
4172 HI.Definition =
"int foo = 3";
4173 HI.LocalScope =
"test::Bar::";
4176 HI.CalleeArgInfo.emplace();
4177 HI.CalleeArgInfo->Name =
"arg_a";
4178 HI.CalleeArgInfo->Type =
"int";
4179 HI.CalleeArgInfo->Default =
"7";
4188Passed by const reference as arg_a (converted to int)
4205Passed by const reference as arg_a (converted to int))",
4209 HI.Name = "stdio.h";
4210 HI.Definition =
"/usr/include/stdio.h";
4211 HI.Kind = index::SymbolKind::IncludeDirective;
4215/usr/include/stdio.h)",
4218`/usr/include/stdio.h`)",
4223 HI.UsedSymbolNames = {
"Foo",
"Bar",
"Bar"};
4224 HI.Kind = index::SymbolKind::IncludeDirective;
4228provides Foo, Bar, Bar)",
4232provides `Foo`, `Bar`, `Bar`)",
4236 HI.UsedSymbolNames = {
"Foo",
"Bar",
"Baz",
"Foobar",
"Qux",
"Quux"};
4237 HI.Kind = index::SymbolKind::IncludeDirective;
4241provides Foo, Bar, Baz, Foobar, Qux and 1 more)",
4245provides `Foo`, `Bar`, `Baz`, `Foobar`, `Qux` and 1 more)"}};
4247 for (
const auto &C : Cases) {
4256 for (
const auto &C : Cases) {
4269 const std::function<void(
HoverInfo &)> Builder;
4270 llvm::StringRef ExpectedMarkdownRender;
4271 llvm::StringRef ExpectedDoxygenRender;
4274 HI.Kind = index::SymbolKind::Function;
4275 HI.Documentation =
"@brief brief doc\n\n"
4277 HI.Definition =
"void foo()";
4280 R
"(### function `foo`
4308 HI.Kind = index::SymbolKind::Function;
4309 HI.Documentation = "@brief brief doc\n\n"
4311 HI.Definition =
"int foo()";
4312 HI.ReturnType =
"int";
4315 R
"(### function `foo`
4350 HI.Kind = index::SymbolKind::Function;
4351 HI.Documentation = R"(@brief brief doc
4356As you see, notes are "inlined".
4357@warning this is a warning
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";
4367 HI.Parameters.emplace();
4368 HI.Parameters->emplace_back();
4369 HI.Parameters->back().Type =
"int";
4370 HI.Parameters->back().Name =
"a";
4372 R
"(### function `foo`
4386As you see, notes are "inlined".
4387@warning this is a warning
4390@param a this is a param
4391@return it returns something
4392@retval 0 if successful
4414- `int a` - this is a param
4419`int` - it returns something
4421- `0` - if successful
4432As you see, notes are "inlined".
4437As well as warnings)"},
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";
4446 HI.Parameters.emplace();
4447 HI.Parameters->emplace_back();
4448 HI.Parameters->back().Type =
"int";
4449 HI.Parameters->back().Name =
"a";
4451 R
"(### function `foo`
4463@param a this is a param
4464@param b does not exist
4465@return it returns something
4486- `int a` - this is a param
4491`int` - it returns something
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";
4504 HI.Parameters.emplace();
4505 HI.Parameters->emplace_back();
4506 HI.Parameters->back().Type =
"int";
4507 HI.Parameters->back().Name =
"a";
4509 R
"(### function `foo`
4519@unknown command is treated as an inline command
4536**@unknown** command is treated as an inline command
4549 for (
const auto &C : Cases) {
4558 for (
const auto &C : Cases) {
4571 llvm::StringRef Documentation;
4572 llvm::StringRef ExpectedRenderEscapedMarkdown;
4573 llvm::StringRef ExpectedRenderMarkdown;
4574 llvm::StringRef ExpectedRenderPlainText;
4684 "Tests primality of `p`.",
4685 "Tests primality of `p`.",
4686 "Tests primality of `p`.",
4687 "Tests primality of `p`.",
4690 "'`' should not occur in `Code`",
4691 "'\\`' should not occur in `Code`",
4692 "'`' should not occur in `Code`",
4693 "'`' should not occur in `Code`",
4702 R
"(@brief this is a typical use case
4706 R"(@brief this is a typical use case
4710 R"(@brief this is a typical use case
4714 R"(@brief this is a typical use case
4720 for (
const auto &C : Cases) {
4721 markup::Document Output;
4724 EXPECT_EQ(Output.asEscapedMarkdown(),
C.ExpectedRenderEscapedMarkdown);
4725 EXPECT_EQ(Output.asMarkdown(),
C.ExpectedRenderMarkdown);
4726 EXPECT_EQ(Output.asPlainText(),
C.ExpectedRenderPlainText);
4734 HI.
Kind = index::SymbolKind::Variable;
4744 HI.
Kind = index::SymbolKind::Variable;
4747 HI.Definition =
"def";
4749 llvm::StringRef ExpectedMarkdown =
4750 "### variable `foo`\n"
4761 llvm::StringRef ExpectedDoxygenMarkdown =
4776 llvm::StringRef ExpectedPlaintext = R
"pt(variable foo
4787 struct strong_ordering {
4789 constexpr operator int() const { return n; }
4790 static const strong_ordering equal, greater, less;
4792 constexpr strong_ordering strong_ordering::equal = {0};
4793 constexpr strong_ordering strong_ordering::greater = {1};
4794 constexpr strong_ordering strong_ordering::less = {-1};
4797 template <typename T>
4800 friend auto operator<=>(S, S) = default;
4802 static_assert(S<void>() =^= S<void>());
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");
4816 auto baz = (Fo^o*)&bar;
4820 auto AST = TU.build();
4821 auto HI =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
4823 EXPECT_EQ(*HI->Value,
"&bar");
4826TEST(
Hover, FunctionParameterDefaulValueNotEvaluatedOnInvalidDecls) {
4828 const char *
const Code;
4829 const std::optional<std::string> HoverValue;
4832 // error-ok testing behavior on invalid decl
4834 void foo(Foo p^aram = nullptr);
4839 void foo(Foo *p^aram = nullptr);
4844 for (
const auto &C : Cases) {
4847 auto AST = TU.build();
4848 auto HI =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
4850 ASSERT_EQ(HI->Value,
C.HoverValue);
4865 TU.ExtraArgs.push_back(
"-std=c++17");
4866 auto AST = TU.build();
4867 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
4875 #define A(x) x, x, x, x
4876 #define B(x) A(A(A(A(x))))
4877 int a^rr[] = {B(0)};
4881 auto AST = TU.build();
4882 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
4885 EXPECT_EQ(H->Definition,
"int arr[]");
4888#if defined(__aarch64__)
4890#define PREDEFINEMACROS_TEST(x) DISABLED_##x
4892#define PREDEFINEMACROS_TEST(x) x
4897 using uintptr_t = __UINTPTR_TYPE__;
4898 enum Test : uintptr_t {};
4899 unsigned global_var;
4901 Test v^al = static_cast<Test>(reinterpret_cast<uintptr_t>(&global_var));
4906 TU.PredefineMacros = true;
4907 auto AST = TU.build();
4908 auto HI =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
4910 EXPECT_EQ(*HI->Value,
"&global_var");
4915 using uintptr_t = __UINTPTR_TYPE__;
4916 unsigned global_var;
4918 uintptr_t a^ddress = reinterpret_cast<uintptr_t>(&global_var);
4923 TU.PredefineMacros = true;
4924 auto AST = TU.build();
4925 auto HI =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
4927 EXPECT_EQ(*HI->Value,
"&global_var");
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; };
4937 template <bool X, typename T, typename F>
4938 using type = typename cond<X, T, F>::type;
4941 using f^oo = type<true, int, double>;
4946 auto AST = TU.build();
4947 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
4949 ASSERT_TRUE(H && H->Type);
4950 EXPECT_EQ(H->Type->Type,
"int");
4951 EXPECT_EQ(H->Definition,
"using foo = type<true, int, double>");
4955 llvm::StringRef PredefinedCXX = R
"cpp(
4957#define SizeOf sizeof
4958#define AlignOf alignof
4962using u64 = unsigned long long;
4963// calculate (a ** b) % p
4964constexpr u64 pow_with_mod(u64 a, u64 b, u64 p) {
4968 ret = (ret * a) % p;
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) \
4978 constexpr auto name() { return value; } \
4980#define gnu_statement_expression(value) \
4982 declare_struct(Widget, getter, value); \
4983 Widget().getter(); \
4985#define define_lambda_begin(lambda, ...) \
4987#define define_lambda_end() }
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
4997 llvm::StringRef Code;
4998 const std::function<void(std::optional<HoverInfo>,
size_t )>
5006 [](std::optional<HoverInfo> HI, size_t) {
5007 EXPECT_EQ(HI->Value,
"42 (0x2a)");
5016 [](std::optional<HoverInfo> HI, size_t) {
5017 EXPECT_TRUE(HI->Value);
5018 EXPECT_TRUE(HI->Type);
5033 [](std::optional<HoverInfo> HI, size_t) {
5034 EXPECT_TRUE(HI->Value);
5035 EXPECT_TRUE(HI->Type);
5040 // 2**32 == 4294967296
5041 last_n_di^git(2, 32, 6);
5044 [](std::optional<HoverInfo> HI, size_t) {
5045 EXPECT_EQ(HI->Value,
"967296 (0xec280)");
5046 EXPECT_EQ(HI->Type,
"u64");
5051 gnu_statement_exp^ression(42);
5054 [](std::optional<HoverInfo> HI, size_t) {
5055 EXPECT_EQ(HI->Value,
"42 (0x2a)");
5056 EXPECT_EQ(HI->Type,
"int");
5064 [](std::optional<HoverInfo> HI, size_t) {
5065 EXPECT_EQ(HI->Value,
"2");
5066 EXPECT_EQ(HI->Type,
"int");
5074 [](std::optional<HoverInfo> HI, size_t) {
5075 EXPECT_FALSE(HI->Value) << HI->Value;
5076 EXPECT_FALSE(HI->Type) << HI->Type;
5084 [](std::optional<HoverInfo> HI, size_t) {
5085 EXPECT_EQ(HI->Value,
"2");
5086 EXPECT_EQ(HI->Type,
"int");
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;
5096 [](std::optional<HoverInfo> HI,
size_t Id) {
5105 EXPECT_FALSE(HI->Type) << HI->Type;
5106 EXPECT_FALSE(HI->Value) << HI->Value;
5109 ASSERT_TRUE(
false) <<
"Unhandled id: " << Id;
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();
5121 [](std::optional<HoverInfo> HI,
size_t Id) {
5124 EXPECT_FALSE(HI->Value);
5128 EXPECT_EQ(HI->Value,
"0");
5135 EXPECT_FALSE(HI->Type) << HI->Type;
5136 EXPECT_FALSE(HI->Value) << HI->Value;
5139 ASSERT_TRUE(
false) <<
"Unhandled id: " << Id;
5148 for (
const auto &C : Cases) {
5150 (PredefinedCXX +
"void function() {\n" +
C.Code +
"}\n").str());
5152 TU.ExtraArgs.push_back(
"-std=c++17");
5153 auto AST = TU.build();
5154 for (
auto [Index,
Position] : llvm::enumerate(Code.points())) {
5161 #define alignof _Alignof
5163 al^ignof(struct { int x; char y[10]; });
5168 TU.Filename =
"TestTU.c";
5172 auto AST = TU.build();
5173 auto H =
getHover(
AST,
C.point(), format::getLLVMStyle(),
nullptr);
5176 EXPECT_TRUE(H->Value);
5177 EXPECT_TRUE(H->Type);
5181 const char *
const Code =
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]]; }
5197 uint32_t MacroContentsLimit;
5198 const std::string ExpectedDefinition;
5201 {2048,
"#define Z4096 E(Z2048)"},
5203 {8192, std::string(
"#define Z4096 E(Z2048)\n\n") +
5204 std::string(
"// Expands to\n") + std::string(4096,
'0')},
5206 for (
const auto &Case : Cases) {
5211 auto AST = TU.build();
5215 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
5218 EXPECT_EQ(H->Definition, Case.ExpectedDefinition);
5224 const char *
const Code;
5225 const std::function<void(
HoverInfo &)> ExpectedBuilder;
5226 std::string ExpectedRender;
5228 {R
"cpp(/// Function doc
5229 void foo(int [[^a]]);
5233 HI.Kind = index::SymbolKind::Parameter;
5234 HI.NamespaceScope =
"";
5235 HI.LocalScope =
"foo::";
5237 HI.Definition =
"int a";
5238 HI.Documentation =
"";
5240 "### param\n\n---\n```cpp\n// In foo\nint a\n```\n\n---\nType: `int`"},
5241 {R
"cpp(/// Function doc
5243 void foo(int [[^a]]);
5247 HI.Kind = index::SymbolKind::Parameter;
5248 HI.NamespaceScope =
"";
5249 HI.LocalScope =
"foo::";
5251 HI.Definition =
"int a";
5252 HI.Documentation =
"this is doc for a";
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
5258 void foo(int [[^a]], int b);
5262 HI.Kind = index::SymbolKind::Parameter;
5263 HI.NamespaceScope =
"";
5264 HI.LocalScope =
"foo::";
5266 HI.Definition =
"int a";
5267 HI.Documentation =
"";
5269 "### param\n\n---\n```cpp\n// In foo\nint a\n```\n\n---\nType: `int`"},
5270 {R
"cpp(/// Function doc
5272 void foo(int a, int [[^b]]);
5276 HI.Kind = index::SymbolKind::Parameter;
5277 HI.NamespaceScope =
"";
5278 HI.LocalScope =
"foo::";
5280 HI.Definition =
"int b";
5281 HI.Documentation =
"this is doc for \\p b";
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
5287 template <typename T>
5288 void foo(T a, T [[^b]]);
5292 HI.Kind = index::SymbolKind::Parameter;
5293 HI.NamespaceScope =
"";
5294 HI.LocalScope =
"foo::";
5296 HI.Definition =
"T b";
5297 HI.Documentation =
"this is doc for \\p b";
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
5303 void foo(int a, int [[^b]]);
5307 HI.Kind = index::SymbolKind::Parameter;
5308 HI.NamespaceScope =
"";
5309 HI.LocalScope =
"foo::";
5311 HI.Definition =
"int b";
5313 "this is <b>doc</b> <html-tag attribute/> <another-html-tag "
5314 "attribute=\"value\">for</another-html-tag> \\p b";
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
5322 void foo(int [[^a]]);
5326 HI.Kind = index::SymbolKind::Parameter;
5327 HI.NamespaceScope =
"";
5328 HI.LocalScope =
"foo::";
5330 HI.Definition =
"int a";
5331 HI.Documentation =
"the next command is an\n @unknown command.";
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`"},
5339 IndexSym.Documentation =
"comment from index";
5345 for (
const auto &Case : Cases) {
5346 SCOPED_TRACE(Case.Code);
5350 auto AST = TU.build();
5355 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(), Index.get());
5359 Case.ExpectedBuilder(Expected);
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);
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");
5382 TU.ExtraArgs.push_back(
"-fenable-matrix");
5383 TU.ExtraArgs.push_back(
"--target=dxil-pc-shadermodel6.3-library");
5386TEST(
Hover, HLSLVectorAndMatrixSwizzle) {
5388 const char *
const Code;
5389 const std::function<void(
HoverInfo &)> ExpectedBuilder;
5393 typedef float float3 __attribute__((ext_vector_type(3)));
5396 float3 s = v.^[[xyz]];
5405 typedef float float3 __attribute__((ext_vector_type(3)));
5406 typedef float float2 __attribute__((ext_vector_type(2)));
5409 float2 s = v.^[[xy]];
5418 typedef float float4x4 __attribute__((matrix_type(4, 4)));
5421 float e = m.^[[_m00]];
5430 for (
const auto &Case : Cases) {
5431 SCOPED_TRACE(Case.Code);
5434 configureHLSL(TU,
true);
5435 auto AST = TU.build();
5436 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
5440 Case.ExpectedBuilder(Expected);
5442 EXPECT_EQ(H->Name, Expected.Name);
5443 EXPECT_EQ(H->Type, Expected.Type);
5444 EXPECT_EQ(H->SymRange, Expected.SymRange);
5448TEST(
Hover, HLSLInvalidMatrixSwizzleNoCrash) {
5450 typedef float float2x2 __attribute__((matrix_type(2, 2)));
5453 float bad = m.^[[_m22]]; // out of bounds for a 2x2 matrix /*error-ok*/
5457 configureHLSL(TU, true);
5458 auto AST = TU.build();
5459 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
5463TEST(
Hover, HLSLInvalidVectorSwizzleNoCrash) {
5465 typedef float float3 __attribute__((ext_vector_type(3)));
5468 float bad = v.^[[w]]; // 'w' is not a valid component for a 3-component vector /*error-ok*/
5473 auto AST = TU.build();
5474 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
5480 const char *
const Code;
5481 const char *
const ExpectedName;
5483 bool ExpectDocumentation;
5486 [numthreads(1, 1, 1)]
5489 for (int i = 0; i < 4; i++) {}
5492 "unroll",
true,
true},
5494 [numthreads(1, 1, 1)]
5497 for (int i = 0; i < 4; i++) {}
5500 "loop",
true,
true},
5502 [numthreads(1, 1, 1)]
5508 "branch",
true,
false},
5510 [numthreads(1, 1, 1)]
5516 "flatten",
true,
false},
5519 [[^likely]] if (true) {}
5522 "likely",
false,
false},
5525 [[^unlikely]] if (true) {}
5528 "unlikely",
false,
false},
5539 "fallthrough",
false,
false},
5541 for (
const auto &Case : Cases) {
5542 SCOPED_TRACE(Case.Code);
5544 Annotations::Markers().setRangeBegin(
"{{").setRangeEnd(
"}}"));
5549 TU.ExtraArgs.push_back(
"-std=c++20");
5550 auto AST = TU.build();
5551 auto H =
getHover(
AST,
T.point(), format::getLLVMStyle(),
nullptr);
5553 EXPECT_EQ(H->Name, Case.ExpectedName);
5554 if (Case.ExpectDocumentation) {
5555 EXPECT_FALSE(H->Documentation.empty());
5559TEST(
Hover, HLSLRegisterAttributeRange) {
5561 Texture2D tex : [[^register]]([[^t1]]);
5567 auto AST = TU.build();
5569 for (
const auto &P :
T.points()) {
5570 auto H =
getHover(
AST, P, format::getLLVMStyle(),
nullptr);
5573 EXPECT_EQ(H->Name,
"register");
5574 EXPECT_FALSE(H->Documentation.empty());
#define PREDEFINEMACROS_TEST(x)
Same as llvm::Annotations, but adjusts functions to LSP-specific types for positions and ranges.
static std::unique_ptr< SymbolIndex > build(SymbolSlab Symbols, RefSlab Refs, RelationSlab Relations)
Builds an index from slabs. The index takes ownership of the data.
An efficient structure of storing large set of symbol references in memory.
SymbolSlab::Builder is a mutable container that can 'freeze' to SymbolSlab.
WithContextValue extends Context::current() with a single value.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
SymbolID getSymbolID(const Decl *D)
Gets the symbol ID for a declaration. Returned SymbolID might be null.
Symbol func(llvm::StringRef Name)
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
std::string testPath(PathRef File, llvm::sys::path::Style Style)
std::optional< HoverInfo > getHover(ParsedAST &AST, Position Pos, const format::FormatStyle &Style, const SymbolIndex *Index)
Get the hover information when hovering at Pos.
TEST(BackgroundQueueTest, Priority)
void parseDocumentation(llvm::StringRef Input, markup::Document &Output)
cppcoreguidelines::ProBoundsAvoidUncheckedContainerAccessCheck P
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Settings that express user/project preferences and control clangd behavior.
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
@ Markdown
Treat comments as Markdown.
@ Doxygen
Treat comments as doxygen.
struct clang::clangd::Config::@124253042262101241326257264241307221112313102042 Hover
Configures hover feature.
bool ShowAKA
Whether hover show a.k.a type.
uint32_t MacroContentsLimit
Limit the number of characters returned when hovering a macro; 0 is no limit.
Represents parameters of a function, a template or a macro.
Contains pretty-printed type and desugared type.
Contains detailed information about a Symbol.
std::optional< Range > SymRange
std::string Name
Name of the symbol, does not contain any "::".
Ensure we have enough bits to represent all SymbolTag values.
static TestTU withCode(llvm::StringRef Code)