1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
//===-- InlayHintTests.cpp -------------------------------*- C++ -*-------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Annotations.h"
#include "InlayHints.h"
#include "Protocol.h"
#include "TestTU.h"
#include "XRefs.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang {
namespace clangd {
std::ostream &operator<<(std::ostream &Stream, const InlayHint &Hint) {
return Stream << Hint.label;
}
namespace {
using ::testing::UnorderedElementsAre;
std::vector<InlayHint> hintsOfKind(ParsedAST &AST, InlayHintKind Kind) {
std::vector<InlayHint> Result;
for (auto &Hint : inlayHints(AST)) {
if (Hint.kind == Kind)
Result.push_back(Hint);
}
return Result;
}
struct ExpectedHint {
std::string Label;
std::string RangeName;
friend std::ostream &operator<<(std::ostream &Stream,
const ExpectedHint &Hint) {
return Stream << Hint.RangeName << ": " << Hint.Label;
}
};
MATCHER_P2(HintMatcher, Expected, Code, "") {
return arg.label == Expected.Label &&
arg.range == Code.range(Expected.RangeName);
}
template <typename... ExpectedHints>
void assertHints(InlayHintKind Kind, llvm::StringRef AnnotatedSource,
ExpectedHints... Expected) {
Annotations Source(AnnotatedSource);
TestTU TU = TestTU::withCode(Source.code());
TU.ExtraArgs.push_back("-std=c++14");
auto AST = TU.build();
EXPECT_THAT(hintsOfKind(AST, Kind),
UnorderedElementsAre(HintMatcher(Expected, Source)...));
}
template <typename... ExpectedHints>
void assertParameterHints(llvm::StringRef AnnotatedSource,
ExpectedHints... Expected) {
assertHints(InlayHintKind::ParameterHint, AnnotatedSource, Expected...);
}
template <typename... ExpectedHints>
void assertTypeHints(llvm::StringRef AnnotatedSource,
ExpectedHints... Expected) {
assertHints(InlayHintKind::TypeHint, AnnotatedSource, Expected...);
}
TEST(ParameterHints, Smoke) {
assertParameterHints(R"cpp(
void foo(int param);
void bar() {
foo($param[[42]]);
}
)cpp",
ExpectedHint{"param: ", "param"});
}
TEST(ParameterHints, NoName) {
// No hint for anonymous parameter.
assertParameterHints(R"cpp(
void foo(int);
void bar() {
foo(42);
}
)cpp");
}
TEST(ParameterHints, NameInDefinition) {
// Parameter name picked up from definition if necessary.
assertParameterHints(R"cpp(
void foo(int);
void bar() {
foo($param[[42]]);
}
void foo(int param) {};
)cpp",
ExpectedHint{"param: ", "param"});
}
TEST(ParameterHints, NameMismatch) {
// Prefer name from declaration.
assertParameterHints(R"cpp(
void foo(int good);
void bar() {
foo($good[[42]]);
}
void foo(int bad) {};
)cpp",
ExpectedHint{"good: ", "good"});
}
TEST(ParameterHints, Operator) {
// No hint for operator call with operator syntax.
assertParameterHints(R"cpp(
struct S {};
void operator+(S lhs, S rhs);
void bar() {
S a, b;
a + b;
}
)cpp");
}
TEST(ParameterHints, Macros) {
// Handling of macros depends on where the call's argument list comes from.
// If it comes from a macro definition, there's nothing to hint
// at the invocation site.
assertParameterHints(R"cpp(
void foo(int param);
#define ExpandsToCall() foo(42)
void bar() {
ExpandsToCall();
}
)cpp");
// The argument expression being a macro invocation shouldn't interfere
// with hinting.
assertParameterHints(R"cpp(
#define PI 3.14
void foo(double param);
void bar() {
foo($param[[PI]]);
}
)cpp",
ExpectedHint{"param: ", "param"});
// If the whole argument list comes from a macro parameter, hint it.
assertParameterHints(R"cpp(
void abort();
#define ASSERT(expr) if (!expr) abort()
int foo(int param);
void bar() {
ASSERT(foo($param[[42]]) == 0);
}
)cpp",
ExpectedHint{"param: ", "param"});
}
TEST(ParameterHints, ConstructorParens) {
assertParameterHints(R"cpp(
struct S {
S(int param);
};
void bar() {
S obj($param[[42]]);
}
)cpp",
ExpectedHint{"param: ", "param"});
}
TEST(ParameterHints, ConstructorBraces) {
assertParameterHints(R"cpp(
struct S {
S(int param);
};
void bar() {
S obj{$param[[42]]};
}
)cpp",
ExpectedHint{"param: ", "param"});
}
TEST(ParameterHints, ConstructorStdInitList) {
// Do not show hints for std::initializer_list constructors.
assertParameterHints(R"cpp(
namespace std {
template <typename> class initializer_list {};
}
struct S {
S(std::initializer_list<int> param);
};
void bar() {
S obj{42, 43};
}
)cpp");
}
TEST(ParameterHints, MemberInit) {
assertParameterHints(R"cpp(
struct S {
S(int param);
};
struct T {
S member;
T() : member($param[[42]]) {}
};
)cpp",
ExpectedHint{"param: ", "param"});
}
TEST(ParameterHints, ImplicitConstructor) {
assertParameterHints(R"cpp(
struct S {
S(int param);
};
void bar(S);
S foo() {
// Do not show hint for implicit constructor call in argument.
bar(42);
// Do not show hint for implicit constructor call in return.
return 42;
}
)cpp");
}
TEST(ParameterHints, ArgMatchesParam) {
assertParameterHints(R"cpp(
void foo(int param);
struct S {
static const int param = 42;
};
void bar() {
int param = 42;
// Do not show redundant "param: param".
foo(param);
// But show it if the argument is qualified.
foo($param[[S::param]]);
}
struct A {
int param;
void bar() {
// Do not show "param: param" for member-expr.
foo(param);
}
};
)cpp",
ExpectedHint{"param: ", "param"});
}
TEST(ParameterHints, LeadingUnderscore) {
assertParameterHints(R"cpp(
void foo(int p1, int _p2, int __p3);
void bar() {
foo($p1[[41]], $p2[[42]], $p3[[43]]);
}
)cpp",
ExpectedHint{"p1: ", "p1"}, ExpectedHint{"p2: ", "p2"},
ExpectedHint{"p3: ", "p3"});
}
TEST(ParameterHints, DependentCalls) {
assertParameterHints(R"cpp(
template <typename T>
void nonmember(T par1);
template <typename T>
struct A {
void member(T par2);
static void static_member(T par3);
};
void overload(int anInt);
void overload(double aDouble);
template <typename T>
struct S {
void bar(A<T> a, T t) {
nonmember($par1[[t]]);
a.member($par2[[t]]);
A<T>::static_member($par3[[t]]);
// We don't want to arbitrarily pick between
// "anInt" or "aDouble", so just show no hint.
overload(T{});
}
};
)cpp",
ExpectedHint{"par1: ", "par1"},
ExpectedHint{"par2: ", "par2"},
ExpectedHint{"par3: ", "par3"});
}
TEST(ParameterHints, VariadicFunction) {
assertParameterHints(R"cpp(
template <typename... T>
void foo(int fixed, T... variadic);
void bar() {
foo($fixed[[41]], 42, 43);
}
)cpp",
ExpectedHint{"fixed: ", "fixed"});
}
TEST(ParameterHints, VarargsFunction) {
assertParameterHints(R"cpp(
void foo(int fixed, ...);
void bar() {
foo($fixed[[41]], 42, 43);
}
)cpp",
ExpectedHint{"fixed: ", "fixed"});
}
TEST(ParameterHints, CopyOrMoveConstructor) {
// Do not show hint for parameter of copy or move constructor.
assertParameterHints(R"cpp(
struct S {
S();
S(const S& other);
S(S&& other);
};
void bar() {
S a;
S b(a); // copy
S c(S()); // move
}
)cpp");
}
TEST(ParameterHints, AggregateInit) {
// FIXME: This is not implemented yet, but it would be a natural
// extension to show member names as hints here.
assertParameterHints(R"cpp(
struct Point {
int x;
int y;
};
void bar() {
Point p{41, 42};
}
)cpp");
}
TEST(ParameterHints, UserDefinedLiteral) {
// Do not hint call to user-defined literal operator.
assertParameterHints(R"cpp(
long double operator"" _w(long double param);
void bar() {
1.2_w;
}
)cpp");
}
TEST(ParameterHints, ParamNameComment) {
// Do not hint an argument which already has a comment
// with the parameter name preceding it.
assertParameterHints(R"cpp(
void foo(int param);
void bar() {
foo(/*param*/42);
foo( /* param = */ 42);
foo(/* the answer */$param[[42]]);
}
)cpp",
ExpectedHint{"param: ", "param"});
}
TEST(ParameterHints, SetterFunctions) {
assertParameterHints(R"cpp(
struct S {
void setParent(S* parent);
void set_parent(S* parent);
void setTimeout(int timeoutMillis);
void setTimeoutMillis(int timeout_millis);
};
void bar() {
S s;
// Parameter name matches setter name - omit hint.
s.setParent(nullptr);
// Support snake_case
s.set_parent(nullptr);
// Parameter name may contain extra info - show hint.
s.setTimeout($timeoutMillis[[120]]);
// FIXME: Ideally we'd want to omit this.
s.setTimeoutMillis($timeout_millis[[120]]);
}
)cpp",
ExpectedHint{"timeoutMillis: ", "timeoutMillis"},
ExpectedHint{"timeout_millis: ", "timeout_millis"});
}
TEST(TypeHints, Smoke) {
assertTypeHints(R"cpp(
auto $waldo[[waldo]] = 42;
)cpp",
ExpectedHint{": int", "waldo"});
}
TEST(TypeHints, Decorations) {
assertTypeHints(R"cpp(
int x = 42;
auto* $var1[[var1]] = &x;
auto&& $var2[[var2]] = x;
const auto& $var3[[var3]] = x;
)cpp",
ExpectedHint{": int *", "var1"},
ExpectedHint{": int &", "var2"},
ExpectedHint{": const int &", "var3"});
}
TEST(TypeHints, DecltypeAuto) {
assertTypeHints(R"cpp(
int x = 42;
int& y = x;
decltype(auto) $z[[z]] = y;
)cpp",
ExpectedHint{": int &", "z"});
}
TEST(TypeHints, NoQualifiers) {
assertTypeHints(R"cpp(
namespace A {
namespace B {
struct S1 {};
S1 foo();
auto $x[[x]] = foo();
struct S2 {
template <typename T>
struct Inner {};
};
S2::Inner<int> bar();
auto $y[[y]] = bar();
}
}
)cpp",
ExpectedHint{": S1", "x"}, ExpectedHint{": Inner<int>", "y"});
}
TEST(TypeHints, Lambda) {
// Do not print something overly verbose like the lambda's location.
// Show hints for init-captures (but not regular captures).
assertTypeHints(R"cpp(
void f() {
int cap = 42;
auto $L[[L]] = [cap, $init[[init]] = 1 + 1](int a) {
return a + cap + init;
};
}
)cpp",
ExpectedHint{": (lambda)", "L"},
ExpectedHint{": int", "init"});
}
// Structured bindings tests.
// Note, we hint the individual bindings, not the aggregate.
TEST(TypeHints, StructuredBindings_PublicStruct) {
assertTypeHints(R"cpp(
// Struct with public fields.
struct Point {
int x;
int y;
};
Point foo();
auto [$x[[x]], $y[[y]]] = foo();
)cpp",
ExpectedHint{": int", "x"}, ExpectedHint{": int", "y"});
}
TEST(TypeHints, StructuredBindings_Array) {
assertTypeHints(R"cpp(
int arr[2];
auto [$x[[x]], $y[[y]]] = arr;
)cpp",
ExpectedHint{": int", "x"}, ExpectedHint{": int", "y"});
}
TEST(TypeHints, StructuredBindings_TupleLike) {
assertTypeHints(R"cpp(
// Tuple-like type.
struct IntPair {
int a;
int b;
};
namespace std {
template <typename T>
struct tuple_size {};
template <>
struct tuple_size<IntPair> {
constexpr static unsigned value = 2;
};
template <unsigned I, typename T>
struct tuple_element {};
template <unsigned I>
struct tuple_element<I, IntPair> {
using type = int;
};
}
template <unsigned I>
int get(const IntPair& p) {
if constexpr (I == 0) {
return p.a;
} else if constexpr (I == 1) {
return p.b;
}
}
IntPair bar();
auto [$x[[x]], $y[[y]]] = bar();
)cpp",
ExpectedHint{": int", "x"}, ExpectedHint{": int", "y"});
}
TEST(TypeHints, StructuredBindings_NoInitializer) {
assertTypeHints(R"cpp(
// No initializer (ill-formed).
// Do not show useless "NULL TYPE" hint.
auto [x, y]; /*error-ok*/
)cpp");
}
TEST(TypeHints, ReturnTypeDeduction) {
assertTypeHints(
R"cpp(
auto f1(int x$ret1a[[)]]; // Hint forward declaration too
auto f1(int x$ret1b[[)]] { return x + 1; }
// Include pointer operators in hint
int s;
auto& f2($ret2[[)]] { return s; }
// Do not hint `auto` for trailing return type.
auto f3() -> int;
// `auto` conversion operator
struct A {
operator auto($retConv[[)]] { return 42; }
};
// FIXME: Dependent types do not work yet.
template <typename T>
struct S {
auto method() { return T(); }
};
)cpp",
ExpectedHint{"-> int", "ret1a"}, ExpectedHint{"-> int", "ret1b"},
ExpectedHint{"-> int &", "ret2"}, ExpectedHint{"-> int", "retConv"});
}
TEST(TypeHints, DependentType) {
assertTypeHints(R"cpp(
template <typename T>
void foo(T arg) {
// The hint would just be "auto" and we can't do any better.
auto var1 = arg.method();
// FIXME: It would be nice to show "T" as the hint.
auto $var2[[var2]] = arg;
}
)cpp");
}
// FIXME: Low-hanging fruit where we could omit a type hint:
// - auto x = TypeName(...);
// - auto x = (TypeName) (...);
// - auto x = static_cast<TypeName>(...); // and other built-in casts
// Annoyances for which a heuristic is not obvious:
// - auto x = llvm::dyn_cast<LongTypeName>(y); // and similar
// - stdlib algos return unwieldy __normal_iterator<X*, ...> type
// (For this one, perhaps we should omit type hints that start
// with a double underscore.)
} // namespace
} // namespace clangd
} // namespace clang
|