16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/ScopedPrinter.h"
18#include "llvm/Support/raw_ostream.h"
19#include "gmock/gmock.h"
20#include "gtest/gtest.h"
36using ::testing::ElementsAre;
37using ::testing::IsEmpty;
39constexpr InlayHintOptions DefaultOptsForTests{2};
41std::vector<InlayHint> hintsOfKind(ParsedAST &
AST, InlayHintKind Kind,
42 InlayHintOptions Opts) {
43 std::vector<InlayHint> Result;
45 if (Hint.kind == Kind)
46 Result.push_back(Hint);
55 std::string RangeName;
58 friend llvm::raw_ostream &
operator<<(llvm::raw_ostream &Stream,
59 const ExpectedHint &Hint) {
60 return Stream << Hint.Label <<
"@$" << Hint.RangeName;
64MATCHER_P2(HintMatcher, Expected, Code, llvm::to_string(Expected)) {
65 llvm::StringRef ExpectedView(Expected.Label);
66 std::string ResultLabel = arg.joinLabels();
67 if (ResultLabel != ExpectedView.trim(
" ") ||
68 arg.paddingLeft != ExpectedView.starts_with(
" ") ||
69 arg.paddingRight != ExpectedView.ends_with(
" ")) {
70 *result_listener <<
"label is '" << ResultLabel <<
"'";
73 if (arg.range != Code.range(Expected.RangeName)) {
74 *result_listener <<
"range is " << llvm::to_string(arg.range) <<
" but $"
75 << Expected.RangeName <<
" is "
76 << llvm::to_string(Code.range(Expected.RangeName));
86 C.InlayHints.Parameters =
false;
87 C.InlayHints.DeducedTypes =
false;
88 C.InlayHints.Designators =
false;
89 C.InlayHints.BlockEnd =
false;
90 C.InlayHints.DefaultArguments =
false;
94template <
typename... ExpectedHints>
95void assertHintsWithHeader(
InlayHintKind Kind, llvm::StringRef AnnotatedSource,
97 ExpectedHints... Expected) {
100 TU.ExtraArgs.push_back(
"-std=c++23");
101 TU.HeaderCode = HeaderContent;
102 auto AST = TU.build();
104 EXPECT_THAT(hintsOfKind(
AST, Kind, Opts),
105 ElementsAre(HintMatcher(Expected, Source)...));
112template <
typename... ExpectedHints>
113void assertHints(
InlayHintKind Kind, llvm::StringRef AnnotatedSource,
115 return assertHintsWithHeader(Kind, AnnotatedSource,
"", Opts,
116 std::move(Expected)...);
120template <
typename...
T>
void ignore(T &&...) {}
122template <
typename... ExpectedHints>
123void assertParameterHints(llvm::StringRef AnnotatedSource,
124 ExpectedHints... Expected) {
125 ignore(Expected.Side =
Left...);
130template <
typename... ExpectedHints>
131void assertTypeHints(llvm::StringRef AnnotatedSource,
132 ExpectedHints... Expected) {
133 ignore(Expected.Side =
Right...);
138template <
typename... ExpectedHints>
139void assertDesignatorHints(llvm::StringRef AnnotatedSource,
140 ExpectedHints... Expected) {
148template <
typename... ExpectedHints>
149void assertBlockEndHintsWithOpts(llvm::StringRef AnnotatedSource,
151 ExpectedHints... Expected) {
158template <
typename... ExpectedHints>
159void assertBlockEndHints(llvm::StringRef AnnotatedSource,
160 ExpectedHints... Expected) {
161 assertBlockEndHintsWithOpts(AnnotatedSource, DefaultOptsForTests,
165TEST(ParameterHints, Smoke) {
166 assertParameterHints(R
"cpp(
172 ExpectedHint{"param: ",
"param"});
175TEST(ParameterHints, NoName) {
177 assertParameterHints(R
"cpp(
185TEST(ParameterHints, NoNameConstReference) {
187 assertParameterHints(R
"cpp(
188 void foo(const int&);
195TEST(ParameterHints, NoNameReference) {
197 assertParameterHints(R
"cpp(
204 ExpectedHint{"&: ",
"param"});
207TEST(ParameterHints, NoNameRValueReference) {
209 assertParameterHints(R
"cpp(
217TEST(ParameterHints, NoNameVariadicDeclaration) {
219 assertParameterHints(R
"cpp(
220 template <typename... Args>
221 void foo(Args&& ...);
228TEST(ParameterHints, NoNameVariadicForwarded) {
231 assertParameterHints(R
"cpp(
232 namespace std { template <typename T> T&& forward(T&); }
234 template <typename... Args>
235 void bar(Args&&... args) { return foo(std::forward<Args>(args)...); }
242TEST(ParameterHints, NoNameVariadicPlain) {
244 assertParameterHints(R
"cpp(
246 template <typename... Args>
247 void bar(Args&&... args) { return foo(args...); }
254TEST(ParameterHints, NameInDefinition) {
256 assertParameterHints(R
"cpp(
261 void foo(int param) {};
263 ExpectedHint{"param: ",
"param"});
266TEST(ParameterHints, NamePartiallyInDefinition) {
268 assertParameterHints(R
"cpp(
269 void foo(int, int b);
271 foo($param1[[42]], $param2[[42]]);
273 void foo(int a, int) {};
275 ExpectedHint{"a: ",
"param1"},
276 ExpectedHint{
"b: ",
"param2"});
279TEST(ParameterHints, NameInDefinitionVariadic) {
281 assertParameterHints(R
"cpp(
283 template <typename... Args>
284 void bar(Args... args) {
288 bar($param1[[42]], $param2[[42]]);
290 void foo(int a, int b) {};
292 ExpectedHint{"a: ",
"param1"},
293 ExpectedHint{
"b: ",
"param2"});
296TEST(ParameterHints, NameMismatch) {
298 assertParameterHints(R
"cpp(
303 void foo(int bad) {};
305 ExpectedHint{"good: ",
"good"});
308TEST(ParameterHints, NameConstReference) {
310 assertParameterHints(R
"cpp(
311 void foo(const int& param);
316 ExpectedHint{"param: ",
"param"});
319TEST(ParameterHints, NameTypeAliasConstReference) {
321 assertParameterHints(R
"cpp(
322 using alias = const int&;
323 void foo(alias param);
329 ExpectedHint{"param: ",
"param"});
332TEST(ParameterHints, NameReference) {
334 assertParameterHints(R
"cpp(
335 void foo(int& param);
341 ExpectedHint{"¶m: ",
"param"});
344TEST(ParameterHints, NameTypeAliasReference) {
346 assertParameterHints(R
"cpp(
348 void foo(alias param);
354 ExpectedHint{"¶m: ",
"param"});
357TEST(ParameterHints, NameRValueReference) {
359 assertParameterHints(R
"cpp(
360 void foo(int&& param);
365 ExpectedHint{"param: ",
"param"});
368TEST(ParameterHints, VariadicForwardedConstructor) {
371 assertParameterHints(R
"cpp(
372 namespace std { template <typename T> T&& forward(T&); }
373 struct S { S(int a); };
374 template <typename T, typename... Args>
375 T bar(Args&&... args) { return T{std::forward<Args>(args)...}; }
381 ExpectedHint{"a: ",
"param"});
384TEST(ParameterHints, VariadicPlainConstructor) {
386 assertParameterHints(R
"cpp(
387 struct S { S(int a); };
388 template <typename T, typename... Args>
389 T bar(Args&&... args) { return T{args...}; }
395 ExpectedHint{"a: ",
"param"});
398TEST(ParameterHints, VariadicForwardedNewConstructor) {
401 assertParameterHints(R
"cpp(
402 namespace std { template <typename T> T&& forward(T&); }
403 struct S { S(int a); };
404 template <typename T, typename... Args>
405 T* bar(Args&&... args) { return new T{std::forward<Args>(args)...}; }
411 ExpectedHint{"a: ",
"param"});
414TEST(ParameterHints, VariadicPlainNewConstructor) {
416 assertParameterHints(R
"cpp(
417 struct S { S(int a); };
418 template <typename T, typename... Args>
419 T* bar(Args&&... args) { return new T{args...}; }
425 ExpectedHint{"a: ",
"param"});
428TEST(ParameterHints, VariadicForwarded) {
431 assertParameterHints(R
"cpp(
432 namespace std { template <typename T> T&& forward(T&); }
434 template <typename... Args>
435 void bar(Args&&... args) { return foo(std::forward<Args>(args)...); }
441 ExpectedHint{"a: ",
"param"});
444TEST(ParameterHints, VariadicPlain) {
446 assertParameterHints(R
"cpp(
448 template <typename... Args>
449 void bar(Args&&... args) { return foo(args...); }
454 ExpectedHint{"a: ",
"param"});
457TEST(ParameterHints, VariadicPlainWithPackFirst) {
460 assertParameterHints(R
"cpp(
462 template <typename... Args, typename Arg>
463 void bar(Arg, Args&&... args) { return foo(args...); }
465 bar(1, $param[[42]]);
468 ExpectedHint{"a: ",
"param"});
471TEST(ParameterHints, VariadicSplitTwolevel) {
475 assertParameterHints(R
"cpp(
476 namespace std { template <typename T> T&& forward(T&); }
477 void baz(int, int b, double);
478 template <typename... Args>
479 void foo(int a, Args&&... args) {
480 return baz(1, std::forward<Args>(args)..., 1.0);
482 template <typename... Args>
483 void bar(Args&&... args) { return foo(std::forward<Args>(args)...); }
485 bar($param1[[32]], $param2[[42]]);
488 ExpectedHint{"a: ",
"param1"},
489 ExpectedHint{
"b: ",
"param2"});
492TEST(ParameterHints, VariadicNameFromSpecialization) {
495 assertParameterHints(R
"cpp(
497 template <typename... Args>
498 void bar(Args... args) {
502 void bar<int>(int b);
507 ExpectedHint{"b: ",
"param"});
510TEST(ParameterHints, VariadicNameFromSpecializationRecursive) {
513 assertParameterHints(R
"cpp(
515 template <typename... Args>
516 void foo(Args... args) {
519 template <typename... Args>
520 void bar(Args... args) {
524 void foo<int>(int b);
529 ExpectedHint{"b: ",
"param"});
532TEST(ParameterHints, VariadicOverloaded) {
536 assertParameterHints(
538 namespace std { template <typename T> T&& forward(T&); }
539 void baz(int b, int c);
540 void baz(int bb, int cc, int dd);
541 template <typename... Args>
542 void foo(int a, Args&&... args) {
543 return baz(std::forward<Args>(args)...);
545 template <typename... Args>
546 void bar(Args&&... args) { return foo(std::forward<Args>(args)...); }
548 bar($param1[[32]], $param2[[42]], $param3[[52]]);
549 bar($param4[[1]], $param5[[2]], $param6[[3]], $param7[[4]]);
552 ExpectedHint{"a: ",
"param1"}, ExpectedHint{
"b: ",
"param2"},
553 ExpectedHint{
"c: ",
"param3"}, ExpectedHint{
"a: ",
"param4"},
554 ExpectedHint{
"bb: ",
"param5"}, ExpectedHint{
"cc: ",
"param6"},
555 ExpectedHint{
"dd: ",
"param7"});
558TEST(ParameterHints, VariadicRecursive) {
560 assertParameterHints(
564 template <typename Head, typename... Tail>
565 void foo(Head head, Tail... tail) {
569 template <typename... Args>
570 void bar(Args... args) {
580TEST(ParameterHints, VariadicVarargs) {
582 assertParameterHints(R
"cpp(
583 void foo(int fixed, ...);
584 template <typename... Args>
585 void bar(Args&&... args) {
590 bar($fixed[[41]], 42, 43);
595TEST(ParameterHints, VariadicTwolevelUnresolved) {
597 assertParameterHints(R
"cpp(
598 template <typename... Args>
599 void foo(int fixed, Args&& ... args);
600 template <typename... Args>
601 void bar(Args&&... args) {
606 bar($fixed[[41]], 42, 43);
609 ExpectedHint{"fixed: ",
"fixed"});
612TEST(ParameterHints, VariadicTwoCalls) {
614 assertParameterHints(
616 void f1(int a, int b);
617 void f2(int c, int d);
621 template <typename... Args>
622 void foo(Args... args) {
631 foo($param1[[1]], $param2[[2]]);
634 ExpectedHint{"a: ",
"param1"}, ExpectedHint{
"b: ",
"param2"});
637TEST(ParameterHints, VariadicInfinite) {
639 assertParameterHints(
641 template <typename... Args>
644 template <typename... Args>
645 void bar(Args... args) {
649 template <typename... Args>
650 void foo(Args... args) {
660TEST(ParameterHints, VariadicDuplicatePack) {
662 assertParameterHints(
664 void foo(int a, int b, int c, int);
666 template <typename... Args>
667 void bar(int, Args... args, int d) {
671 template <typename... Args>
672 void baz(Args... args, Args... args2) {
673 bar<Args..., int>(1, args..., args2...);
677 baz<int, int>($p1[[1]], $p2[[2]], $p3[[3]], $p4[[4]]);
680 ExpectedHint{"a: ",
"p1"}, ExpectedHint{
"b: ",
"p2"},
681 ExpectedHint{
"c: ",
"p3"}, ExpectedHint{
"d: ",
"p4"});
684TEST(ParameterHints, VariadicEmplace) {
687 assertParameterHints(
689 namespace std { template <typename T> T&& forward(T&); }
690 using size_t = decltype(sizeof(0));
691 void *operator new(size_t, void *);
697 template <typename T>
699 template <typename T, typename... Args>
700 void construct(T* ptr, Args&&... args) {
701 ::new ((void*)ptr) T{std::forward<Args>(args)...};
704 template <typename T>
706 template <typename... Args>
707 void emplace(Args&&... args) {
709 auto ptr = a.template allocate<T>();
710 a.construct(ptr, std::forward<Args>(args)...);
715 c.emplace($param1[[1]]);
716 c.emplace($param2[[2]], $param3[[3]]);
719 ExpectedHint{"A: ",
"param1"}, ExpectedHint{
"B: ",
"param2"},
720 ExpectedHint{
"C: ",
"param3"});
723TEST(ParameterHints, VariadicReferenceHint) {
724 assertParameterHints(R
"cpp(
726 template <typename... Args>
727 void bar(Args... args) { return foo(args...); }
736TEST(ParameterHints, VariadicReferenceHintForwardingRef) {
737 assertParameterHints(R"cpp(
739 template <typename... Args>
740 void bar(Args&&... args) { return foo(args...); }
747 ExpectedHint{"&: ",
"param"});
750TEST(ParameterHints, VariadicReferenceHintForwardingRefStdForward) {
751 assertParameterHints(R
"cpp(
752 namespace std { template <typename T> T&& forward(T&); }
754 template <typename... Args>
755 void bar(Args&&... args) { return foo(std::forward<Args>(args)...); }
761 ExpectedHint{"&: ",
"param"});
764TEST(ParameterHints, VariadicNoReferenceHintForwardingRefStdForward) {
765 assertParameterHints(R
"cpp(
766 namespace std { template <typename T> T&& forward(T&); }
768 template <typename... Args>
769 void bar(Args&&... args) { return foo(std::forward<Args>(args)...); }
778TEST(ParameterHints, VariadicNoReferenceHintUnresolvedForward) {
779 assertParameterHints(R"cpp(
780 template <typename... Args>
781 void foo(Args&&... args);
789TEST(ParameterHints, MatchingNameVariadicForwarded) {
792 assertParameterHints(R
"cpp(
793 namespace std { template <typename T> T&& forward(T&); }
795 template <typename... Args>
796 void bar(Args&&... args) { return foo(std::forward<Args>(args)...); }
804TEST(ParameterHints, MatchingNameVariadicPlain) {
806 assertParameterHints(R
"cpp(
808 template <typename... Args>
809 void bar(Args&&... args) { return foo(args...); }
819 assertParameterHints(R
"cpp(
821 void operator+(S lhs, S rhs);
829TEST(ParameterHints, FunctionCallOperator) {
830 assertParameterHints(R"cpp(
832 void operator()(int x);
836 static void operator()(int x, int y);
839 auto l1 = [](int x) {};
840 auto l2 = [](int x) static {};
844 s.operator()($2[[1]]);
845 s.operator()($3[[1]], $4[[2]]);
846 S::operator()($5[[1]], $6[[2]]);
849 l1.operator()($8[[1]]);
851 l2.operator()($10[[1]]);
853 void (*ptr)(int a, int b) = &S::operator();
854 ptr($11[[1]], $12[[2]]);
857 ExpectedHint{"x: ",
"1"}, ExpectedHint{
"x: ",
"2"},
858 ExpectedHint{
"x: ",
"3"}, ExpectedHint{
"y: ",
"4"},
859 ExpectedHint{
"x: ",
"5"}, ExpectedHint{
"y: ",
"6"},
860 ExpectedHint{
"x: ",
"7"}, ExpectedHint{
"x: ",
"8"},
861 ExpectedHint{
"x: ",
"9"}, ExpectedHint{
"x: ",
"10"},
862 ExpectedHint{
"a: ",
"11"}, ExpectedHint{
"b: ",
"12"});
865TEST(ParameterHints, DeducingThis) {
866 assertParameterHints(R
"cpp(
868 template <typename This>
869 auto operator()(this This &&Self, int Param) {
873 auto function(this auto &Self, int Param) {
880 s.function($2[[42]]);
882 auto lambda = [](this auto &Self, char C) -> void {
888 ExpectedHint{"Param: ",
"1"},
889 ExpectedHint{
"Param: ",
"2"},
890 ExpectedHint{
"Param: ",
"3"}, ExpectedHint{
"C: ",
"4"});
893TEST(ParameterHints, DependentDeducingThis) {
894 assertParameterHints(R
"cpp(
895 template <typename T>
897 void f1(this S& obj);
898 void f2(this S& obj, int x, int y);
901 s.f2($x[[42]], $y[[43]]);
905 ExpectedHint{"x: ",
"x"}, ExpectedHint{
"y: ",
"y"});
908TEST(ParameterHints, Macros) {
913 assertParameterHints(R
"cpp(
915 #define ExpandsToCall() foo(42)
923 assertParameterHints(R
"cpp(
925 void foo(double param);
930 ExpectedHint{"param: ",
"param"});
933 assertParameterHints(R
"cpp(
935 #define ASSERT(expr) if (!expr) abort()
938 ASSERT(foo($param[[42]]) == 0);
941 ExpectedHint{"param: ",
"param"});
944 assertParameterHints(R
"cpp(
945 void foo(double x, double y);
946 #define CONSTANTS 3.14, 2.72
953TEST(ParameterHints, ConstructorParens) {
954 assertParameterHints(R"cpp(
962 ExpectedHint{"param: ",
"param"});
965TEST(ParameterHints, ConstructorBraces) {
966 assertParameterHints(R
"cpp(
974 ExpectedHint{"param: ",
"param"});
977TEST(ParameterHints, ConstructorStdInitList) {
979 assertParameterHints(R
"cpp(
981 template <typename E> class initializer_list { const E *a, *b; };
984 S(std::initializer_list<int> param);
992TEST(ParameterHints, MemberInit) {
993 assertParameterHints(R"cpp(
999 T() : member($param[[42]]) {}
1002 ExpectedHint{"param: ",
"param"});
1005TEST(ParameterHints, ImplicitConstructor) {
1006 assertParameterHints(R
"cpp(
1012 // Do not show hint for implicit constructor call in argument.
1014 // Do not show hint for implicit constructor call in return.
1020TEST(ParameterHints, FunctionPointer) {
1021 assertParameterHints(
1023 void (*f1)(int param);
1024 void (__stdcall *f2)(int param);
1025 using f3_t = void(*)(int param);
1027 using f4_t = void(__stdcall *)(int param);
1029 __attribute__((noreturn)) f4_t f5;
1035 // This one runs into an edge case in clang's type model
1036 // and we can't extract the parameter name. But at least
1037 // we shouldn't crash.
1041 ExpectedHint{"param: ",
"f1"}, ExpectedHint{
"param: ",
"f2"},
1042 ExpectedHint{
"param: ",
"f3"}, ExpectedHint{
"param: ",
"f4"});
1045TEST(ParameterHints, ArgMatchesParam) {
1046 assertParameterHints(R
"cpp(
1047 void foo(int param);
1049 static const int param = 42;
1053 // Do not show redundant "param: param".
1055 // But show it if the argument is qualified.
1056 foo($param[[S::param]]);
1061 // Do not show "param: param" for member-expr.
1066 ExpectedHint{"param: ",
"param"});
1069TEST(ParameterHints, ArgMatchesParamReference) {
1070 assertParameterHints(R
"cpp(
1071 void foo(int& param);
1072 void foo2(const int& param);
1075 // show reference hint on mutable reference
1076 foo($param[[param]]);
1077 // but not on const reference
1081 ExpectedHint{"&: ",
"param"});
1084TEST(ParameterHints, LeadingUnderscore) {
1085 assertParameterHints(R
"cpp(
1086 void foo(int p1, int _p2, int __p3);
1088 foo($p1[[41]], $p2[[42]], $p3[[43]]);
1091 ExpectedHint{"p1: ",
"p1"}, ExpectedHint{
"p2: ",
"p2"},
1092 ExpectedHint{
"p3: ",
"p3"});
1095TEST(ParameterHints, DependentCalls) {
1096 assertParameterHints(R
"cpp(
1097 template <typename T>
1098 void nonmember(T par1);
1100 template <typename T>
1102 void member(T par2);
1103 static void static_member(T par3);
1106 void overload(int anInt);
1107 void overload(double aDouble);
1109 template <typename T>
1111 void bar(A<T> a, T t) {
1112 nonmember($par1[[t]]);
1113 a.member($par2[[t]]);
1114 A<T>::static_member($par3[[t]]);
1115 // We don't want to arbitrarily pick between
1116 // "anInt" or "aDouble", so just show no hint.
1121 ExpectedHint{"par1: ",
"par1"},
1122 ExpectedHint{
"par2: ",
"par2"},
1123 ExpectedHint{
"par3: ",
"par3"});
1126TEST(ParameterHints, VariadicFunction) {
1127 assertParameterHints(R
"cpp(
1128 template <typename... T>
1129 void foo(int fixed, T... variadic);
1132 foo($fixed[[41]], 42, 43);
1135 ExpectedHint{"fixed: ",
"fixed"});
1138TEST(ParameterHints, VarargsFunction) {
1139 assertParameterHints(R
"cpp(
1140 void foo(int fixed, ...);
1143 foo($fixed[[41]], 42, 43);
1146 ExpectedHint{"fixed: ",
"fixed"});
1149TEST(ParameterHints, CopyOrMoveConstructor) {
1151 assertParameterHints(R
"cpp(
1165TEST(ParameterHints, UserDefinedLiteral) {
1167 assertParameterHints(R
"cpp(
1168 long double operator"" _w(long double param);
1175TEST(ParameterHints, ParamNameComment) {
1178 assertParameterHints(R
"cpp(
1179 void foo(int param);
1182 foo( /* param = */ 42);
1186 foo(/*param=*/Z(a));
1187 foo($macro[[Z(a)]]);
1188 foo(/* the answer */$param[[42]]);
1191 ExpectedHint{"param: ",
"macro"},
1192 ExpectedHint{
"param: ",
"param"});
1195TEST(ParameterHints, SetterFunctions) {
1196 assertParameterHints(R
"cpp(
1198 void setParent(S* parent);
1199 void set_parent(S* parent);
1200 void setTimeout(int timeoutMillis);
1201 void setTimeoutMillis(int timeout_millis);
1205 // Parameter name matches setter name - omit hint.
1206 s.setParent(nullptr);
1207 // Support snake_case
1208 s.set_parent(nullptr);
1209 // Parameter name may contain extra info - show hint.
1210 s.setTimeout($timeoutMillis[[120]]);
1211 // FIXME: Ideally we'd want to omit this.
1212 s.setTimeoutMillis($timeout_millis[[120]]);
1215 ExpectedHint{"timeoutMillis: ",
"timeoutMillis"},
1216 ExpectedHint{
"timeout_millis: ",
"timeout_millis"});
1219TEST(ParameterHints, BuiltinFunctions) {
1221 assertParameterHints(R
"cpp(
1222 namespace std { template <typename T> T&& forward(T&); }
1230TEST(ParameterHints, IncludeAtNonGlobalScope) {
1232 void bar() { foo(42); }
1236 void foo(int param);
1242 Workspace.addSource("foo.inc", FooInc.code());
1243 Workspace.addMainFile(
"foo.cc", FooCC.code());
1245 auto AST = Workspace.openFile(
"foo.cc");
1246 ASSERT_TRUE(
bool(
AST));
1254TEST(ParameterHints, Issue220359_NoCrash) {
1255 assertParameterHints(R
"cpp(
1259 template <typename... Args>
1260 void f(Args... args) {
1269TEST(TypeHints, Smoke) {
1270 assertTypeHints(R"cpp(
1271 auto $waldo[[waldo]] = 42;
1273 ExpectedHint{": int",
"waldo"});
1276TEST(TypeHints, Decorations) {
1277 assertTypeHints(R
"cpp(
1279 auto* $var1[[var1]] = &x;
1280 auto&& $var2[[var2]] = x;
1281 const auto& $var3[[var3]] = x;
1283 ExpectedHint{": int *",
"var1"},
1284 ExpectedHint{
": int &",
"var2"},
1285 ExpectedHint{
": const int &",
"var3"});
1288TEST(TypeHints, DecltypeAuto) {
1289 assertTypeHints(R
"cpp(
1292 decltype(auto) $z[[z]] = y;
1294 ExpectedHint{": int &",
"z"});
1297TEST(TypeHints, NoQualifiers) {
1298 assertTypeHints(R
"cpp(
1303 auto $x[[x]] = foo();
1306 template <typename T>
1309 S2::Inner<int> bar();
1310 auto $y[[y]] = bar();
1314 ExpectedHint{": S1",
"x"}, ExpectedHint{
": Inner<int>",
"y"});
1317TEST(TypeHints, Lambda) {
1320 assertTypeHints(R
"cpp(
1323 auto $L[[L]] = [cap, $init[[init]] = 1 + 1](int a$ret[[)]] {
1324 return a + cap + init;
1328 ExpectedHint{": (lambda)",
"L"},
1329 ExpectedHint{
": int",
"init"}, ExpectedHint{
"-> int",
"ret"});
1333 assertTypeHints(
"auto $L[[x]] = <:$ret[[:>]]{return 42;};",
1334 ExpectedHint{
": (lambda)",
"L"},
1335 ExpectedHint{
"-> int",
"ret"});
1341TEST(TypeHints, StructuredBindings_PublicStruct) {
1342 assertTypeHints(R
"cpp(
1343 // Struct with public fields.
1349 auto [$x[[x]], $y[[y]]] = foo();
1351 ExpectedHint{": int",
"x"}, ExpectedHint{
": int",
"y"});
1354TEST(TypeHints, StructuredBindings_Array) {
1355 assertTypeHints(R
"cpp(
1357 auto [$x[[x]], $y[[y]]] = arr;
1359 ExpectedHint{": int",
"x"}, ExpectedHint{
": int",
"y"});
1362TEST(TypeHints, StructuredBindings_TupleLike) {
1363 assertTypeHints(R
"cpp(
1370 template <typename T>
1371 struct tuple_size {};
1373 struct tuple_size<IntPair> {
1374 constexpr static unsigned value = 2;
1376 template <unsigned I, typename T>
1377 struct tuple_element {};
1378 template <unsigned I>
1379 struct tuple_element<I, IntPair> {
1383 template <unsigned I>
1384 int get(const IntPair& p) {
1385 if constexpr (I == 0) {
1387 } else if constexpr (I == 1) {
1392 auto [$x[[x]], $y[[y]]] = bar();
1394 ExpectedHint{": int",
"x"}, ExpectedHint{
": int",
"y"});
1397TEST(TypeHints, StructuredBindings_NoInitializer) {
1398 assertTypeHints(R
"cpp(
1399 // No initializer (ill-formed).
1400 // Do not show useless "NULL TYPE" hint.
1401 auto [x, y]; /*error-ok*/
1405TEST(TypeHints, InvalidType) {
1406 assertTypeHints(R"cpp(
1407 auto x = (unknown_type)42; /*error-ok*/
1408 auto *y = (unknown_ptr)nullptr;
1412TEST(TypeHints, ReturnTypeDeduction) {
1415 auto f1(int x$ret1a[[)]]; // Hint forward declaration too
1416 auto f1(int x$ret1b[[)]] { return x + 1; }
1418 // Include pointer operators in hint
1420 auto& f2($ret2[[)]] { return s; }
1422 // Do not hint `auto` for trailing return type.
1425 // Do not hint when a trailing return type is specified.
1426 auto f4() -> auto* { return "foo"; }
1428 auto f5($noreturn[[)]] {}
1430 // `auto` conversion operator
1432 operator auto($retConv[[)]] { return 42; }
1435 // FIXME: Dependent types do not work yet.
1436 template <typename T>
1438 auto method() { return T(); }
1441 ExpectedHint{"-> int",
"ret1a"}, ExpectedHint{
"-> int",
"ret1b"},
1442 ExpectedHint{
"-> int &",
"ret2"}, ExpectedHint{
"-> void",
"noreturn"},
1443 ExpectedHint{
"-> int",
"retConv"});
1446TEST(TypeHints, DependentType) {
1447 assertTypeHints(R
"cpp(
1448 template <typename T>
1450 // The hint would just be "auto" and we can't do any better.
1451 auto var1 = arg.method();
1452 // FIXME: It would be nice to show "T" as the hint.
1453 auto $var2[[var2]] = arg;
1456 template <typename T>
1461 ExpectedHint{": T",
"var2"});
1464TEST(TypeHints, LongTypeName) {
1465 assertTypeHints(R
"cpp(
1466 template <typename, typename, typename>
1468 struct MultipleWords {};
1469 A<MultipleWords, MultipleWords, MultipleWords> foo();
1470 // Omit type hint past a certain length (currently 32)
1480 template <typename, typename, typename>
1482 struct MultipleWords {};
1483 A<MultipleWords, MultipleWords, MultipleWords> foo();
1484 // Should have type hint with TypeNameLimit = 0
1485 auto $var[[var]] = foo();
1487 ExpectedHint{": A<MultipleWords, MultipleWords, MultipleWords>",
"var"});
1490TEST(TypeHints, DefaultTemplateArgs) {
1491 assertTypeHints(R
"cpp(
1492 template <typename, typename = int>
1495 auto $var[[var]] = foo();
1497 auto [$binding[[value]]] = bar;
1499 ExpectedHint{": A<float>",
"var"},
1500 ExpectedHint{
": A<float>",
"binding"});
1503TEST(DefaultArguments, Smoke) {
1507 Cfg.InlayHints.DeducedTypes =
false;
1508 Cfg.InlayHints.Designators =
false;
1509 Cfg.InlayHints.BlockEnd =
false;
1511 Cfg.InlayHints.DefaultArguments =
true;
1514 const auto *Code = R
"cpp(
1515 int foo(int A = 4) { return A; }
1516 int bar(int A, int B = 1, bool C = foo($default1[[)]]) { return A; }
1517 int A = bar($explicit[[2]]$default2[[)]];
1519 void baz(int = 5) { if (false) baz($unnamed[[)]]; };
1523 ExpectedHint{"A: 4",
"default1",
Left},
1524 ExpectedHint{
", B: 1, C: foo()",
"default2",
Left},
1525 ExpectedHint{
"5",
"unnamed",
Left});
1528 ExpectedHint{
"A: ",
"explicit",
Left});
1531TEST(DefaultArguments, WithoutParameterNames) {
1534 Cfg.InlayHints.DeducedTypes =
false;
1535 Cfg.InlayHints.Designators =
false;
1536 Cfg.InlayHints.BlockEnd =
false;
1538 Cfg.InlayHints.DefaultArguments =
true;
1541 const auto *Code = R
"cpp(
1547 Foo(int, Baz baz = //
1548 Baz{$abbreviated[[}]]
1555 Foo foo1(1$paren[[)]];
1556 Foo foo2{2$brace1[[}]];
1557 Foo foo3 = {3$brace2[[}]];
1558 auto foo4 = Foo{4$brace3[[}]];
1563 ExpectedHint{"...",
"abbreviated",
Left},
1564 ExpectedHint{
", Baz{}",
"paren",
Left},
1565 ExpectedHint{
", Baz{}",
"brace1",
Left},
1566 ExpectedHint{
", Baz{}",
"brace2",
Left},
1567 ExpectedHint{
", Baz{}",
"brace3",
Left});
1572TEST(TypeHints, Deduplication) {
1573 assertTypeHints(R
"cpp(
1574 template <typename T>
1576 auto $var[[var]] = 42;
1578 template void foo<int>();
1579 template void foo<float>();
1581 ExpectedHint{": int",
"var"});
1584TEST(TypeHints, SinglyInstantiatedTemplate) {
1585 assertTypeHints(R
"cpp(
1586 auto $lambda[[x]] = [](auto *$param[[y]], auto) { return 42; };
1587 int m = x("foo", 3);
1589 ExpectedHint{": (lambda)",
"lambda"},
1590 ExpectedHint{
": const char *",
"param"});
1593 assertTypeHints(R
"cpp(
1594 int x(auto $a[[a]], auto... b, auto c) { return 42; }
1595 int m = x<void*, char, float>(nullptr, 'c', 2.0, 2);
1597 ExpectedHint{": void *",
"a"});
1600TEST(TypeHints, Aliased) {
1604 TU.ExtraArgs.push_back(
"-xc");
1605 auto AST = TU.build();
1611TEST(TypeHints, CallingConvention) {
1616 []($lambda[[)]]__cdecl {};
1620 TU.ExtraArgs.push_back("--target=x86_64-w64-mingw32");
1621 TU.PredefineMacros =
true;
1622 auto AST = TU.build();
1626 ElementsAre(HintMatcher(ExpectedHint{
"-> void",
"lambda"}, Source)));
1629TEST(TypeHints, Decltype) {
1630 assertTypeHints(R
"cpp(
1631 $a[[decltype(0)]] a;
1632 $b[[decltype(a)]] b;
1633 const $c[[decltype(0)]] &c = b;
1635 // Don't show for dependent type
1637 constexpr decltype(T{}) d;
1639 $e[[decltype(0)]] e();
1640 auto f() -> $f[[decltype(0)]];
1642 template <class, class> struct Foo;
1643 using G = Foo<$g[[decltype(0)]], float>;
1645 auto $h[[h]] = $i[[decltype(0)]]{};
1651 ExpectedHint{": int",
"a"}, ExpectedHint{
": int",
"b"},
1652 ExpectedHint{
": int",
"c"}, ExpectedHint{
": int",
"e"},
1653 ExpectedHint{
": int",
"f"}, ExpectedHint{
": int",
"g"},
1654 ExpectedHint{
": int",
"h"}, ExpectedHint{
": int",
"i"});
1657TEST(TypeHints, SubstTemplateParameterAliases) {
1658 llvm::StringRef Header = R
"cpp(
1659 template <class T> struct allocator {};
1661 template <class T, class A>
1662 struct vector_base {
1666 template <class T, class A>
1667 struct internal_iterator_type_template_we_dont_expect {};
1669 struct my_iterator {};
1671 template <class T, class A = allocator<T>>
1672 struct vector : vector_base<T, A> {
1673 using base = vector_base<T, A>;
1674 typedef T value_type;
1675 typedef base::pointer pointer;
1676 using allocator_type = A;
1677 using size_type = int;
1678 using iterator = internal_iterator_type_template_we_dont_expect<T, A>;
1679 using non_template_iterator = my_iterator;
1681 value_type& operator[](int index) { return elements[index]; }
1682 const value_type& at(int index) const { return elements[index]; }
1683 pointer data() { return &elements[0]; }
1684 allocator_type get_allocator() { return A(); }
1685 size_type size() const { return 10; }
1686 iterator begin() { return iterator(); }
1687 non_template_iterator end() { return non_template_iterator(); }
1693 llvm::StringRef VectorIntPtr = R"cpp(
1694 vector<int *> array;
1695 auto $no_modifier[[x]] = array[3];
1696 auto* $ptr_modifier[[ptr]] = &array[3];
1697 auto& $ref_modifier[[ref]] = array[3];
1698 auto& $at[[immutable]] = array.at(3);
1700 auto $data[[data]] = array.data();
1701 auto $allocator[[alloc]] = array.get_allocator();
1702 auto $size[[size]] = array.size();
1703 auto $begin[[begin]] = array.begin();
1704 auto $end[[end]] = array.end();
1707 assertHintsWithHeader(
1709 ExpectedHint{": int *",
"no_modifier"},
1710 ExpectedHint{
": int **",
"ptr_modifier"},
1711 ExpectedHint{
": int *&",
"ref_modifier"},
1712 ExpectedHint{
": int *const &",
"at"}, ExpectedHint{
": int **",
"data"},
1713 ExpectedHint{
": allocator<int *>",
"allocator"},
1714 ExpectedHint{
": size_type",
"size"}, ExpectedHint{
": iterator",
"begin"},
1715 ExpectedHint{
": non_template_iterator",
"end"});
1717 llvm::StringRef VectorInt = R
"cpp(
1719 auto $no_modifier[[by_value]] = array[3];
1720 auto* $ptr_modifier[[ptr]] = &array[3];
1721 auto& $ref_modifier[[ref]] = array[3];
1722 auto& $at[[immutable]] = array.at(3);
1724 auto $data[[data]] = array.data();
1725 auto $allocator[[alloc]] = array.get_allocator();
1726 auto $size[[size]] = array.size();
1727 auto $begin[[begin]] = array.begin();
1728 auto $end[[end]] = array.end();
1731 assertHintsWithHeader(
1733 ExpectedHint{": int",
"no_modifier"},
1734 ExpectedHint{
": int *",
"ptr_modifier"},
1735 ExpectedHint{
": int &",
"ref_modifier"},
1736 ExpectedHint{
": const int &",
"at"}, ExpectedHint{
": int *",
"data"},
1737 ExpectedHint{
": allocator<int>",
"allocator"},
1738 ExpectedHint{
": size_type",
"size"}, ExpectedHint{
": iterator",
"begin"},
1739 ExpectedHint{
": non_template_iterator",
"end"});
1741 llvm::StringRef TypeAlias = R
"cpp(
1742 // If the type alias is not of substituted template parameter type,
1743 // do not show desugared type.
1744 using VeryLongLongTypeName = my_iterator;
1745 using Short = VeryLongLongTypeName;
1747 auto $short_name[[my_value]] = Short();
1749 // Same applies with templates.
1750 template <typename T, typename A>
1751 using basic_static_vector = vector<T, A>;
1752 template <typename T>
1753 using static_vector = basic_static_vector<T, allocator<T>>;
1755 auto $vector_name[[vec]] = static_vector<int>();
1759 DefaultOptsForTests,
1760 ExpectedHint{": Short",
"short_name"},
1761 ExpectedHint{
": static_vector<int>",
"vector_name"});
1764TEST(DesignatorHints, Basic) {
1765 assertDesignatorHints(R
"cpp(
1766 struct S { int x, y, z; };
1767 S s {$x[[1]], $y[[2+2]]};
1769 int x[] = {$0[[0]], $1[[1]]};
1771 ExpectedHint{".x=",
"x"}, ExpectedHint{
".y=",
"y"},
1772 ExpectedHint{
"[0]=",
"0"}, ExpectedHint{
"[1]=",
"1"});
1775TEST(DesignatorHints, Nested) {
1776 assertDesignatorHints(R
"cpp(
1777 struct Inner { int x, y; };
1778 struct Outer { Inner a, b; };
1779 Outer o{ $a[[{ $x[[1]], $y[[2]] }]], $bx[[3]] };
1781 ExpectedHint{".a=",
"a"}, ExpectedHint{
".x=",
"x"},
1782 ExpectedHint{
".y=",
"y"}, ExpectedHint{
".b.x=",
"bx"});
1785TEST(DesignatorHints, AnonymousRecord) {
1786 assertDesignatorHints(R
"cpp(
1798 ExpectedHint{".x.y=",
"xy"});
1801TEST(DesignatorHints, Suppression) {
1802 assertDesignatorHints(R
"cpp(
1803 struct Point { int a, b, c, d, e, f, g, h; };
1804 Point p{/*a=*/1, .c=2, /* .d = */3, $e[[4]]};
1806 ExpectedHint{".e=",
"e"});
1809TEST(DesignatorHints, StdArray) {
1812 assertDesignatorHints(R
"cpp(
1813 template <typename T, int N> struct Array { T __elements[N]; };
1814 Array<int, 2> x = {$0[[0]], $1[[1]]};
1816 ExpectedHint{"[0]=",
"0"}, ExpectedHint{
"[1]=",
"1"});
1819TEST(DesignatorHints, OnlyAggregateInit) {
1820 assertDesignatorHints(R
"cpp(
1821 struct Copyable { int x; } c;
1824 struct Constructible { Constructible(int x); };
1825 Constructible x{42};
1829TEST(DesignatorHints, NoCrash) {
1830 assertDesignatorHints(R
"cpp(
1833 struct Foo {int a; int b;};
1835 Foo f{A(), $b[[1]]};
1838 ExpectedHint{".b=",
"b"});
1841TEST(DesignatorHints, ParenInit) {
1842 assertDesignatorHints(R
"cpp(
1848 S s ($x[[1]], $y[[2+2]], $z[[4]]);
1850 ExpectedHint{".x=",
"x"}, ExpectedHint{
".y=",
"y"},
1851 ExpectedHint{
".z=",
"z"});
1854TEST(DesignatorHints, ParenInitDerived) {
1855 assertDesignatorHints(R
"cpp(
1865 S2 s2 ({$a[[0]], $b[[0]]}, $c[[0]], $d[[0]]);
1868 ExpectedHint{
".a=",
"a"}, ExpectedHint{
".b=",
"b"},
1869 ExpectedHint{
".c=",
"c"}, ExpectedHint{
".d=",
"d"});
1872TEST(DesignatorHints, ParenInitTemplate) {
1873 assertDesignatorHints(R
"cpp(
1874 template <typename T>
1881 struct S2 : S1<S2> {
1888 S2 sa ({$a1[[0]], $b1[[0]]}, $c[[0]], $d[[0]], $mem[[S1<int>($a2[[1]], $b2[[2]], $ptr[[nullptr]])]]);
1891 ExpectedHint{".a=",
"a1"}, ExpectedHint{
".b=",
"b1"},
1892 ExpectedHint{
".c=",
"c"}, ExpectedHint{
".d=",
"d"},
1893 ExpectedHint{
".mem=",
"mem"}, ExpectedHint{
".a=",
"a2"},
1894 ExpectedHint{
".b=",
"b2"},
1895 ExpectedHint{
".ptr=",
"ptr"});
1898TEST(InlayHints, RestrictRange) {
1907 ElementsAre(labelIs(
": int"), labelIs(
": char")));
1910TEST(ParameterHints, PseudoObjectExpr) {
1913 __declspec(property(get=GetX, put=PutX)) int x[];
1914 int GetX(int y, int z) { return 42 + y; }
1917 // This is a PseudoObjectExpression whose syntactic form is a binary
1919 void Work(int y) { x = y; } // Not `x = y: y`.
1922 int printf(const char *Format, ...);
1926 __builtin_dump_struct(&s, printf); // Not `Format: __builtin_dump_struct()`
1927 printf($Param[["Hello, %d"]], 42); // Normal calls are not affected.
1928 // This builds a PseudoObjectExpr, but here it's useful for showing the
1929 // arguments from the semantic form.
1930 return s.x[ $one[[1]] ][ $two[[2]] ]; // `x[y: 1][z: 2]`
1934 TU.ExtraArgs.push_back(
"-fms-extensions");
1935 auto AST = TU.build();
1937 ElementsAre(HintMatcher(ExpectedHint{
"Format: ",
"Param"}, Code),
1938 HintMatcher(ExpectedHint{
"y: ",
"one"}, Code),
1939 HintMatcher(ExpectedHint{
"z: ",
"two"}, Code)));
1942TEST(ParameterHints, ArgPacksAndConstructors) {
1943 assertParameterHints(
1945 struct Foo{ Foo(); Foo(int x); };
1946 void foo(Foo a, int b);
1947 template <typename... Args>
1948 void bar(Args... args) {
1951 template <typename... Args>
1952 void baz(Args... args) { foo($param1[[Foo{args...}]], $param2[[1]]); }
1954 template <typename... Args>
1955 void bax(Args... args) { foo($param3[[{args...}]], args...); }
1958 bar($param4[[Foo{}]], $param5[[42]]);
1959 bar($param6[[42]], $param7[[42]]);
1964 ExpectedHint{"a: ",
"param1"}, ExpectedHint{
"b: ",
"param2"},
1965 ExpectedHint{
"a: ",
"param3"}, ExpectedHint{
"a: ",
"param4"},
1966 ExpectedHint{
"b: ",
"param5"}, ExpectedHint{
"a: ",
"param6"},
1967 ExpectedHint{
"b: ",
"param7"}, ExpectedHint{
"x: ",
"param8"},
1968 ExpectedHint{
"b: ",
"param9"});
1971TEST(ParameterHints, DoesntExpandAllArgs) {
1972 assertParameterHints(
1974 void foo(int x, int y);
1975 int id(int a, int b, int c);
1976 template <typename... Args>
1977 void bar(Args... args) {
1978 foo(id($param1[[args]], $param2[[1]], $param3[[args]])...);
1981 bar(1, 2); // FIXME: We could have `bar(a: 1, a: 2)` here.
1984 ExpectedHint{"a: ",
"param1"}, ExpectedHint{
"b: ",
"param2"},
1985 ExpectedHint{
"c: ",
"param3"});
1988TEST(BlockEndHints, Functions) {
1989 assertBlockEndHints(R
"cpp(
1996 // No hint for lambda for now
2003 // No hint because this isn't a definition
2007 bool operator==(S, S) {
2011 ExpectedHint{" // foo",
"foo"},
2012 ExpectedHint{
" // bar",
"bar"},
2013 ExpectedHint{
" // operator==",
"opEqual"});
2016TEST(BlockEndHints, Methods) {
2017 assertBlockEndHints(R
"cpp(
2019 // No hint because there's no function body
2028 // No hint because this isn't a definition
2031 template <typename T>
2035 // No hint because this isn't a definition
2036 template <typename T>
2039 Test operator+(int) const {
2043 operator bool() const {
2047 // No hint because there's no function body
2048 operator int() const = delete;
2051 void Test::method2() {
2054 template <typename T>
2055 void Test::method4() {
2058 ExpectedHint{" // ~Test",
"dtor"},
2059 ExpectedHint{
" // method1",
"method1"},
2060 ExpectedHint{
" // method3",
"method3"},
2061 ExpectedHint{
" // operator+",
"opIdentity"},
2062 ExpectedHint{
" // operator bool",
"opBool"},
2063 ExpectedHint{
" // Test::method2",
"method2"},
2064 ExpectedHint{
" // Test::method4",
"method4"});
2067TEST(BlockEndHints, Namespaces) {
2068 assertBlockEndHints(
2078 ExpectedHint{" // namespace",
"anon"},
2079 ExpectedHint{
" // namespace ns",
"ns"});
2082TEST(BlockEndHints, Types) {
2083 assertBlockEndHints(
2100 ExpectedHint{" // struct S",
"S"}, ExpectedHint{
" // class C",
"C"},
2101 ExpectedHint{
" // union U",
"U"}, ExpectedHint{
" // enum E1",
"E1"},
2102 ExpectedHint{
" // enum class E2",
"E2"});
2105TEST(BlockEndHints, If) {
2106 assertBlockEndHints(
2108 void foo(bool cond) {
2130 if (auto X = cond) {
2133 if (int i = 0; i > 10) {
2136 if (ptr != nullptr) {
2140 ExpectedHint{" // if cond",
"simple"},
2141 ExpectedHint{
" // if cond",
"ifelse"}, ExpectedHint{
" // if",
"elseif"},
2142 ExpectedHint{
" // if !cond",
"inner"},
2143 ExpectedHint{
" // if cond",
"outer"}, ExpectedHint{
" // if X",
"init"},
2144 ExpectedHint{
" // if i > 10",
"init_cond"},
2145 ExpectedHint{
" // if ptr != nullptr",
"null_check"});
2148TEST(BlockEndHints, Loops) {
2149 assertBlockEndHints(
2164 for (int I = 0; I < 10; ++I) {
2172 ExpectedHint{" // while true",
"while"},
2173 ExpectedHint{
" // for true",
"forcond"},
2174 ExpectedHint{
" // for I",
"forvar"},
2175 ExpectedHint{
" // for V",
"foreach"});
2178TEST(BlockEndHints, Switch) {
2179 assertBlockEndHints(
2187 ExpectedHint{" // switch I",
"switch"});
2190TEST(BlockEndHints, PrintLiterals) {
2191 assertBlockEndHints(
2197 while ("foo but this time it is very long") {
2210 ExpectedHint{" // while \"foo\"",
"string"},
2211 ExpectedHint{
" // while \"foo but...\"",
"string_long"},
2212 ExpectedHint{
" // while true",
"boolean"},
2213 ExpectedHint{
" // while 1",
"integer"},
2214 ExpectedHint{
" // while 1.5",
"float"});
2217TEST(BlockEndHints, PrintRefs) {
2218 assertBlockEndHints(
2223 int func2(int, int);
2226 int method1() const;
2227 int method2(int, int) const;
2235 while (ns::func1()) {
2238 while (ns::func2(int_a, int_a)) {
2241 while (ns::S{}.Field) {
2244 while (ns::S{}.method1()) {
2247 while (ns::S{}.method2(int_a, int_a)) {
2251 ExpectedHint{" // while Var",
"var"},
2252 ExpectedHint{
" // while func1()",
"func1"},
2253 ExpectedHint{
" // while func2(...)",
"func2"},
2254 ExpectedHint{
" // while Field",
"field"},
2255 ExpectedHint{
" // while method1()",
"method1"},
2256 ExpectedHint{
" // while method2(...)",
"method2"});
2259TEST(BlockEndHints, PrintConversions) {
2260 assertBlockEndHints(
2265 explicit operator bool();
2269 $convert_primitive[[}]]
2275 $construct_class[[}]]
2278 ExpectedHint{" // while float",
"convert_primitive"},
2279 ExpectedHint{
" // while S",
"convert_class"},
2280 ExpectedHint{
" // while S",
"construct_class"});
2283TEST(BlockEndHints, PrintOperators) {
2284 std::string AnnotatedCode = R
"cpp(
2285 void foo(Integer I) {
2304 while((I + I) < (I + I)){
2305 $binary_complex[[}]]
2310 auto AssertExpectedHints = [&](llvm::StringRef Code) {
2311 assertBlockEndHints(Code, ExpectedHint{
" // while ++I",
"preinc"},
2312 ExpectedHint{
" // while I++",
"postinc"},
2313 ExpectedHint{
" // while",
"unary_complex"},
2314 ExpectedHint{
" // while I < 0",
"compare"},
2315 ExpectedHint{
" // while ... < I",
"lhs_complex"},
2316 ExpectedHint{
" // while I < ...",
"rhs_complex"},
2317 ExpectedHint{
" // while",
"binary_complex"});
2321 AssertExpectedHints(
"using Integer = int;" + AnnotatedCode);
2323 AssertExpectedHints(R
"cpp(
2325 explicit operator bool();
2326 Integer operator++();
2327 Integer operator++(int);
2328 Integer operator+(Integer);
2329 Integer operator+();
2330 bool operator<(Integer);
2331 bool operator<(int);
2333 )cpp" + AnnotatedCode);
2336TEST(BlockEndHints, TrailingSemicolon) {
2337 assertBlockEndHints(R"cpp(
2338 // The hint is placed after the trailing ';'
2342 // The hint is always placed in the same line with the closing '}'.
2343 // So in this case where ';' is missing, it is attached to '}'.
2349 // No hint because only one trailing ';' is allowed
2353 // No hint because trailing ';' is only allowed for class/struct/union/enum
2357 // Rare case, but yes we'll have a hint here.
2364 ExpectedHint{" // struct S1",
"S1"},
2365 ExpectedHint{
" // struct S2",
"S2"},
2366 ExpectedHint{
" // struct",
"anon"});
2369TEST(BlockEndHints, TrailingText) {
2370 assertBlockEndHints(R
"cpp(
2374 // No hint for S2 because of the trailing comment
2376 }; /* Put anything here */
2379 // No hint for S4 because of the trailing source code
2383 // No hint for ns because of the trailing comment
2387 ExpectedHint{" // struct S1",
"S1"},
2388 ExpectedHint{
" // struct S3",
"S3"});
2392 assertBlockEndHints(R
"cpp(
2393 #define DECL_STRUCT(NAME) struct NAME {
2399 // No hint because we require a '}'
2403 ExpectedHint{" // struct S1",
"S1"});
2406TEST(BlockEndHints, PointerToMemberFunction) {
2408 assertBlockEndHints(R
"cpp(
2410 using Predicate = bool(A::*)();
2411 void foo(A* a, Predicate p) {
2416 ExpectedHint{" // if ()",
"ptrmem"});
2419TEST(BlockEndHints, MinLineLimit) {
2424 assertBlockEndHintsWithOpts(
2429 int func2(int, int);
2432 int method1() const;
2433 int method2(int, int) const;
2441 while (ns::func1()) {
2444 while (ns::func2(int_a, int_a)) {
2447 while (ns::S{}.Field) {
2450 while (ns::S{}.method1()) {
2453 while (ns::S{}.method2(int_a, int_a)) {
2457 Opts, ExpectedHint{" // namespace ns",
"namespace"},
2458 ExpectedHint{
" // foo",
"foo"});
Same as llvm::Annotations, but adjusts functions to LSP-specific types for positions and ranges.
void addSource(llvm::StringRef Filename, llvm::StringRef Code)
WithContextValue extends Context::current() with a single value.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
MATCHER_P2(hasFlag, Flag, Path, "")
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
TEST(BackgroundQueueTest, Priority)
InlayHintKind
Inlay hint kinds.
@ BlockEnd
A hint after function, type or namespace definition, indicating the defined symbol name of the defini...
@ DefaultArgument
An inlay hint that is for a default argument.
@ Parameter
An inlay hint that is for a parameter.
@ Type
An inlay hint that for a type annotation.
@ Designator
A hint before an element of an aggregate braced initializer list, indicating what it is initializing.
std::vector< InlayHint > inlayHints(ParsedAST &AST, std::optional< Range > RestrictRange, InlayHintOptions HintOptions)
Compute and return inlay hints for a file.
===– 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.
struct clang::clangd::Config::@041344304366110202143331236314370324353035136032 InlayHints
std::string joinLabels() const
Join the label[].value together.
Range range
The range of source code to which the hint applies.
static TestTU withCode(llvm::StringRef Code)