| 1 |
//===-- DefineOutline.cpp ---------------------------------------*- C++ -*-===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "TestFS.h" |
| 10 |
#include "TweakTesting.h" |
| 11 |
#include "gmock/gmock.h" |
| 12 |
#include "gtest/gtest.h" |
| 13 |
|
| 14 |
using ::testing::ElementsAre; |
| 15 |
|
| 16 |
namespace clang { |
| 17 |
namespace clangd { |
| 18 |
namespace { |
| 19 |
|
| 20 |
TWEAK_TEST(DefineOutline); |
| 21 |
|
| 22 |
TEST_F(DefineOutlineTest, TriggersOnFunctionDecl) { |
| 23 |
FileName = "Test.cpp"; |
| 24 |
// Not available unless in a header file. |
| 25 |
EXPECT_UNAVAILABLE(R"cpp( |
| 26 |
[[void [[f^o^o]]() [[{ |
| 27 |
return; |
| 28 |
}]]]])cpp"); |
| 29 |
|
| 30 |
FileName = "Test.hpp"; |
| 31 |
// Not available unless function name or fully body is selected. |
| 32 |
EXPECT_UNAVAILABLE(R"cpp( |
| 33 |
// Not a definition |
| 34 |
vo^i[[d^ ^f]]^oo(); |
| 35 |
|
| 36 |
[[vo^id ]]foo[[()]] {[[ |
| 37 |
[[(void)(5+3); |
| 38 |
return;]] |
| 39 |
}]])cpp"); |
| 40 |
|
| 41 |
// Available even if there are no implementation files. |
| 42 |
EXPECT_AVAILABLE(R"cpp( |
| 43 |
[[void [[f^o^o]]() [[{ |
| 44 |
return; |
| 45 |
}]]]])cpp"); |
| 46 |
|
| 47 |
// Not available for out-of-line methods. |
| 48 |
EXPECT_UNAVAILABLE(R"cpp( |
| 49 |
class Bar { |
| 50 |
void baz(); |
| 51 |
}; |
| 52 |
|
| 53 |
[[void [[Bar::[[b^a^z]]]]() [[{ |
| 54 |
return; |
| 55 |
}]]]])cpp"); |
| 56 |
|
| 57 |
// Basic check for function body and signature. |
| 58 |
EXPECT_AVAILABLE(R"cpp( |
| 59 |
class Bar { |
| 60 |
[[void [[f^o^o^]]() [[{ return; }]]]] |
| 61 |
}; |
| 62 |
|
| 63 |
void foo(); |
| 64 |
[[void [[f^o^o]]() [[{ |
| 65 |
return; |
| 66 |
}]]]])cpp"); |
| 67 |
|
| 68 |
// Not available on defaulted/deleted members. |
| 69 |
EXPECT_UNAVAILABLE(R"cpp( |
| 70 |
class Foo { |
| 71 |
Fo^o() = default; |
| 72 |
F^oo(const Foo&) = delete; |
| 73 |
};)cpp"); |
| 74 |
|
| 75 |
// Not available within templated classes, as it is hard to spell class name |
| 76 |
// out-of-line in such cases. |
| 77 |
EXPECT_UNAVAILABLE(R"cpp( |
| 78 |
template <typename> struct Foo { void fo^o(){} }; |
| 79 |
)cpp"); |
| 80 |
|
| 81 |
// Not available on function templates and specializations, as definition must |
| 82 |
// be visible to all translation units. |
| 83 |
EXPECT_UNAVAILABLE(R"cpp( |
| 84 |
template <typename> void fo^o() {}; |
| 85 |
template <> void fo^o<int>() {}; |
| 86 |
)cpp"); |
| 87 |
} |
| 88 |
|
| 89 |
TEST_F(DefineOutlineTest, FailsWithoutSource) { |
| 90 |
FileName = "Test.hpp"; |
| 91 |
llvm::StringRef Test = "void fo^o() { return; }"; |
| 92 |
llvm::StringRef Expected = |
| 93 |
"fail: Couldn't find a suitable implementation file."; |
| 94 |
EXPECT_EQ(apply(Test), Expected); |
| 95 |
} |
| 96 |
|
| 97 |
TEST_F(DefineOutlineTest, ApplyTest) { |
| 98 |
llvm::StringMap<std::string> EditedFiles; |
| 99 |
ExtraFiles["Test.cpp"] = ""; |
| 100 |
FileName = "Test.hpp"; |
| 101 |
|
| 102 |
struct { |
| 103 |
llvm::StringRef Test; |
| 104 |
llvm::StringRef ExpectedHeader; |
| 105 |
llvm::StringRef ExpectedSource; |
| 106 |
} Cases[] = { |
| 107 |
// Simple check |
| 108 |
{ |
| 109 |
"void fo^o() { return; }", |
| 110 |
"void foo() ;", |
| 111 |
"void foo() { return; }", |
| 112 |
}, |
| 113 |
// Default args. |
| 114 |
{ |
| 115 |
"void fo^o(int x, int y = 5, int = 2, int (*foo)(int) = nullptr) {}", |
| 116 |
"void foo(int x, int y = 5, int = 2, int (*foo)(int) = nullptr) ;", |
| 117 |
"void foo(int x, int y , int , int (*foo)(int) ) {}", |
| 118 |
}, |
| 119 |
{ |
| 120 |
"struct Bar{Bar();}; void fo^o(Bar x = {}) {}", |
| 121 |
"struct Bar{Bar();}; void foo(Bar x = {}) ;", |
| 122 |
"void foo(Bar x ) {}", |
| 123 |
}, |
| 124 |
// Constructors |
| 125 |
{ |
| 126 |
R"cpp( |
| 127 |
class Foo {public: Foo(); Foo(int);}; |
| 128 |
class Bar { |
| 129 |
Ba^r() {} |
| 130 |
Bar(int x) : f1(x) {} |
| 131 |
Foo f1; |
| 132 |
Foo f2 = 2; |
| 133 |
};)cpp", |
| 134 |
R"cpp( |
| 135 |
class Foo {public: Foo(); Foo(int);}; |
| 136 |
class Bar { |
| 137 |
Bar() ; |
| 138 |
Bar(int x) : f1(x) {} |
| 139 |
Foo f1; |
| 140 |
Foo f2 = 2; |
| 141 |
};)cpp", |
| 142 |
"Bar::Bar() {}\n", |
| 143 |
}, |
| 144 |
// Ctor with initializer. |
| 145 |
{ |
| 146 |
R"cpp( |
| 147 |
class Foo {public: Foo(); Foo(int);}; |
| 148 |
class Bar { |
| 149 |
Bar() {} |
| 150 |
B^ar(int x) : f1(x), f2(3) {} |
| 151 |
Foo f1; |
| 152 |
Foo f2 = 2; |
| 153 |
};)cpp", |
| 154 |
R"cpp( |
| 155 |
class Foo {public: Foo(); Foo(int);}; |
| 156 |
class Bar { |
| 157 |
Bar() {} |
| 158 |
Bar(int x) ; |
| 159 |
Foo f1; |
| 160 |
Foo f2 = 2; |
| 161 |
};)cpp", |
| 162 |
"Bar::Bar(int x) : f1(x), f2(3) {}\n", |
| 163 |
}, |
| 164 |
// Ctor initializer with attribute. |
| 165 |
{ |
| 166 |
R"cpp( |
| 167 |
class Foo { |
| 168 |
F^oo(int z) __attribute__((weak)) : bar(2){} |
| 169 |
int bar; |
| 170 |
};)cpp", |
| 171 |
R"cpp( |
| 172 |
class Foo { |
| 173 |
Foo(int z) __attribute__((weak)) ; |
| 174 |
int bar; |
| 175 |
};)cpp", |
| 176 |
"Foo::Foo(int z) __attribute__((weak)) : bar(2){}\n", |
| 177 |
}, |
| 178 |
// Virt specifiers. |
| 179 |
{ |
| 180 |
R"cpp( |
| 181 |
struct A { |
| 182 |
virtual void f^oo() {} |
| 183 |
};)cpp", |
| 184 |
R"cpp( |
| 185 |
struct A { |
| 186 |
virtual void foo() ; |
| 187 |
};)cpp", |
| 188 |
" void A::foo() {}\n", |
| 189 |
}, |
| 190 |
{ |
| 191 |
R"cpp( |
| 192 |
struct A { |
| 193 |
virtual virtual void virtual f^oo() {} |
| 194 |
};)cpp", |
| 195 |
R"cpp( |
| 196 |
struct A { |
| 197 |
virtual virtual void virtual foo() ; |
| 198 |
};)cpp", |
| 199 |
" void A::foo() {}\n", |
| 200 |
}, |
| 201 |
{ |
| 202 |
R"cpp( |
| 203 |
struct A { |
| 204 |
virtual void foo() = 0; |
| 205 |
}; |
| 206 |
struct B : A { |
| 207 |
void fo^o() override {} |
| 208 |
};)cpp", |
| 209 |
R"cpp( |
| 210 |
struct A { |
| 211 |
virtual void foo() = 0; |
| 212 |
}; |
| 213 |
struct B : A { |
| 214 |
void foo() override ; |
| 215 |
};)cpp", |
| 216 |
"void B::foo() {}\n", |
| 217 |
}, |
| 218 |
{ |
| 219 |
R"cpp( |
| 220 |
struct A { |
| 221 |
virtual void foo() = 0; |
| 222 |
}; |
| 223 |
struct B : A { |
| 224 |
void fo^o() final {} |
| 225 |
};)cpp", |
| 226 |
R"cpp( |
| 227 |
struct A { |
| 228 |
virtual void foo() = 0; |
| 229 |
}; |
| 230 |
struct B : A { |
| 231 |
void foo() final ; |
| 232 |
};)cpp", |
| 233 |
"void B::foo() {}\n", |
| 234 |
}, |
| 235 |
{ |
| 236 |
R"cpp( |
| 237 |
struct A { |
| 238 |
virtual void foo() = 0; |
| 239 |
}; |
| 240 |
struct B : A { |
| 241 |
void fo^o() final override {} |
| 242 |
};)cpp", |
| 243 |
R"cpp( |
| 244 |
struct A { |
| 245 |
virtual void foo() = 0; |
| 246 |
}; |
| 247 |
struct B : A { |
| 248 |
void foo() final override ; |
| 249 |
};)cpp", |
| 250 |
"void B::foo() {}\n", |
| 251 |
}, |
| 252 |
{ |
| 253 |
R"cpp( |
| 254 |
struct A { |
| 255 |
static void fo^o() {} |
| 256 |
};)cpp", |
| 257 |
R"cpp( |
| 258 |
struct A { |
| 259 |
static void foo() ; |
| 260 |
};)cpp", |
| 261 |
" void A::foo() {}\n", |
| 262 |
}, |
| 263 |
{ |
| 264 |
R"cpp( |
| 265 |
struct A { |
| 266 |
static static void fo^o() {} |
| 267 |
};)cpp", |
| 268 |
R"cpp( |
| 269 |
struct A { |
| 270 |
static static void foo() ; |
| 271 |
};)cpp", |
| 272 |
" void A::foo() {}\n", |
| 273 |
}, |
| 274 |
{ |
| 275 |
R"cpp( |
| 276 |
struct Foo { |
| 277 |
explicit Fo^o(int) {} |
| 278 |
};)cpp", |
| 279 |
R"cpp( |
| 280 |
struct Foo { |
| 281 |
explicit Foo(int) ; |
| 282 |
};)cpp", |
| 283 |
" Foo::Foo(int) {}\n", |
| 284 |
}, |
| 285 |
{ |
| 286 |
R"cpp( |
| 287 |
struct Foo { |
| 288 |
explicit explicit Fo^o(int) {} |
| 289 |
};)cpp", |
| 290 |
R"cpp( |
| 291 |
struct Foo { |
| 292 |
explicit explicit Foo(int) ; |
| 293 |
};)cpp", |
| 294 |
" Foo::Foo(int) {}\n", |
| 295 |
}, |
| 296 |
}; |
| 297 |
for (const auto &Case : Cases) { |
| 298 |
SCOPED_TRACE(Case.Test); |
| 299 |
EXPECT_EQ(apply(Case.Test, &EditedFiles), Case.ExpectedHeader); |
| 300 |
EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents( |
| 301 |
testPath("Test.cpp"), Case.ExpectedSource))); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
TEST_F(DefineOutlineTest, HandleMacros) { |
| 306 |
llvm::StringMap<std::string> EditedFiles; |
| 307 |
ExtraFiles["Test.cpp"] = ""; |
| 308 |
FileName = "Test.hpp"; |
| 309 |
ExtraArgs.push_back("-DVIRTUAL=virtual"); |
| 310 |
ExtraArgs.push_back("-DOVER=override"); |
| 311 |
|
| 312 |
struct { |
| 313 |
llvm::StringRef Test; |
| 314 |
llvm::StringRef ExpectedHeader; |
| 315 |
llvm::StringRef ExpectedSource; |
| 316 |
} Cases[] = { |
| 317 |
{R"cpp( |
| 318 |
#define BODY { return; } |
| 319 |
void f^oo()BODY)cpp", |
| 320 |
R"cpp( |
| 321 |
#define BODY { return; } |
| 322 |
void foo();)cpp", |
| 323 |
"void foo()BODY"}, |
| 324 |
|
| 325 |
{R"cpp( |
| 326 |
#define BODY return; |
| 327 |
void f^oo(){BODY})cpp", |
| 328 |
R"cpp( |
| 329 |
#define BODY return; |
| 330 |
void foo();)cpp", |
| 331 |
"void foo(){BODY}"}, |
| 332 |
|
| 333 |
{R"cpp( |
| 334 |
#define TARGET void foo() |
| 335 |
[[TARGET]]{ return; })cpp", |
| 336 |
R"cpp( |
| 337 |
#define TARGET void foo() |
| 338 |
TARGET;)cpp", |
| 339 |
"TARGET{ return; }"}, |
| 340 |
|
| 341 |
{R"cpp( |
| 342 |
#define TARGET foo |
| 343 |
void [[TARGET]](){ return; })cpp", |
| 344 |
R"cpp( |
| 345 |
#define TARGET foo |
| 346 |
void TARGET();)cpp", |
| 347 |
"void TARGET(){ return; }"}, |
| 348 |
{R"cpp(#define VIRT virtual |
| 349 |
struct A { |
| 350 |
VIRT void f^oo() {} |
| 351 |
};)cpp", |
| 352 |
R"cpp(#define VIRT virtual |
| 353 |
struct A { |
| 354 |
VIRT void foo() ; |
| 355 |
};)cpp", |
| 356 |
" void A::foo() {}\n"}, |
| 357 |
{R"cpp( |
| 358 |
struct A { |
| 359 |
VIRTUAL void f^oo() {} |
| 360 |
};)cpp", |
| 361 |
R"cpp( |
| 362 |
struct A { |
| 363 |
VIRTUAL void foo() ; |
| 364 |
};)cpp", |
| 365 |
" void A::foo() {}\n"}, |
| 366 |
{R"cpp( |
| 367 |
struct A { |
| 368 |
virtual void foo() = 0; |
| 369 |
}; |
| 370 |
struct B : A { |
| 371 |
void fo^o() OVER {} |
| 372 |
};)cpp", |
| 373 |
R"cpp( |
| 374 |
struct A { |
| 375 |
virtual void foo() = 0; |
| 376 |
}; |
| 377 |
struct B : A { |
| 378 |
void foo() OVER ; |
| 379 |
};)cpp", |
| 380 |
"void B::foo() {}\n"}, |
| 381 |
{R"cpp(#define STUPID_MACRO(X) virtual |
| 382 |
struct A { |
| 383 |
STUPID_MACRO(sizeof sizeof int) void f^oo() {} |
| 384 |
};)cpp", |
| 385 |
R"cpp(#define STUPID_MACRO(X) virtual |
| 386 |
struct A { |
| 387 |
STUPID_MACRO(sizeof sizeof int) void foo() ; |
| 388 |
};)cpp", |
| 389 |
" void A::foo() {}\n"}, |
| 390 |
{R"cpp(#define STAT static |
| 391 |
struct A { |
| 392 |
STAT void f^oo() {} |
| 393 |
};)cpp", |
| 394 |
R"cpp(#define STAT static |
| 395 |
struct A { |
| 396 |
STAT void foo() ; |
| 397 |
};)cpp", |
| 398 |
" void A::foo() {}\n"}, |
| 399 |
{R"cpp(#define STUPID_MACRO(X) static |
| 400 |
struct A { |
| 401 |
STUPID_MACRO(sizeof sizeof int) void f^oo() {} |
| 402 |
};)cpp", |
| 403 |
R"cpp(#define STUPID_MACRO(X) static |
| 404 |
struct A { |
| 405 |
STUPID_MACRO(sizeof sizeof int) void foo() ; |
| 406 |
};)cpp", |
| 407 |
" void A::foo() {}\n"}, |
| 408 |
}; |
| 409 |
for (const auto &Case : Cases) { |
| 410 |
SCOPED_TRACE(Case.Test); |
| 411 |
EXPECT_EQ(apply(Case.Test, &EditedFiles), Case.ExpectedHeader); |
| 412 |
EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents( |
| 413 |
testPath("Test.cpp"), Case.ExpectedSource))); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
TEST_F(DefineOutlineTest, QualifyReturnValue) { |
| 418 |
FileName = "Test.hpp"; |
| 419 |
ExtraFiles["Test.cpp"] = ""; |
| 420 |
|
| 421 |
struct { |
| 422 |
llvm::StringRef Test; |
| 423 |
llvm::StringRef ExpectedHeader; |
| 424 |
llvm::StringRef ExpectedSource; |
| 425 |
} Cases[] = { |
| 426 |
{R"cpp( |
| 427 |
namespace a { class Foo{}; } |
| 428 |
using namespace a; |
| 429 |
Foo fo^o() { return {}; })cpp", |
| 430 |
R"cpp( |
| 431 |
namespace a { class Foo{}; } |
| 432 |
using namespace a; |
| 433 |
Foo foo() ;)cpp", |
| 434 |
"a::Foo foo() { return {}; }"}, |
| 435 |
{R"cpp( |
| 436 |
namespace a { |
| 437 |
class Foo { |
| 438 |
class Bar {}; |
| 439 |
Bar fo^o() { return {}; } |
| 440 |
}; |
| 441 |
})cpp", |
| 442 |
R"cpp( |
| 443 |
namespace a { |
| 444 |
class Foo { |
| 445 |
class Bar {}; |
| 446 |
Bar foo() ; |
| 447 |
}; |
| 448 |
})cpp", |
| 449 |
"a::Foo::Bar a::Foo::foo() { return {}; }\n"}, |
| 450 |
{R"cpp( |
| 451 |
class Foo {}; |
| 452 |
Foo fo^o() { return {}; })cpp", |
| 453 |
R"cpp( |
| 454 |
class Foo {}; |
| 455 |
Foo foo() ;)cpp", |
| 456 |
"Foo foo() { return {}; }"}, |
| 457 |
}; |
| 458 |
llvm::StringMap<std::string> EditedFiles; |
| 459 |
for (auto &Case : Cases) { |
| 460 |
apply(Case.Test, &EditedFiles); |
| 461 |
EXPECT_EQ(apply(Case.Test, &EditedFiles), Case.ExpectedHeader); |
| 462 |
EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents( |
| 463 |
testPath("Test.cpp"), Case.ExpectedSource))); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
TEST_F(DefineOutlineTest, QualifyFunctionName) { |
| 468 |
FileName = "Test.hpp"; |
| 469 |
struct { |
| 470 |
llvm::StringRef TestHeader; |
| 471 |
llvm::StringRef TestSource; |
| 472 |
llvm::StringRef ExpectedHeader; |
| 473 |
llvm::StringRef ExpectedSource; |
| 474 |
} Cases[] = { |
| 475 |
{ |
| 476 |
R"cpp( |
| 477 |
namespace a { |
| 478 |
namespace b { |
| 479 |
class Foo { |
| 480 |
void fo^o() {} |
| 481 |
}; |
| 482 |
} |
| 483 |
})cpp", |
| 484 |
"", |
| 485 |
R"cpp( |
| 486 |
namespace a { |
| 487 |
namespace b { |
| 488 |
class Foo { |
| 489 |
void foo() ; |
| 490 |
}; |
| 491 |
} |
| 492 |
})cpp", |
| 493 |
"void a::b::Foo::foo() {}\n", |
| 494 |
}, |
| 495 |
{ |
| 496 |
"namespace a { namespace b { void f^oo() {} } }", |
| 497 |
"namespace a{}", |
| 498 |
"namespace a { namespace b { void foo() ; } }", |
| 499 |
"namespace a{void b::foo() {} }", |
| 500 |
}, |
| 501 |
{ |
| 502 |
"namespace a { namespace b { void f^oo() {} } }", |
| 503 |
"using namespace a;", |
| 504 |
"namespace a { namespace b { void foo() ; } }", |
| 505 |
// FIXME: Take using namespace directives in the source file into |
| 506 |
// account. This can be spelled as b::foo instead. |
| 507 |
"using namespace a;void a::b::foo() {} ", |
| 508 |
}, |
| 509 |
}; |
| 510 |
llvm::StringMap<std::string> EditedFiles; |
| 511 |
for (auto &Case : Cases) { |
| 512 |
ExtraFiles["Test.cpp"] = std::string(Case.TestSource); |
| 513 |
EXPECT_EQ(apply(Case.TestHeader, &EditedFiles), Case.ExpectedHeader); |
| 514 |
EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents( |
| 515 |
testPath("Test.cpp"), Case.ExpectedSource))) |
| 516 |
<< Case.TestHeader; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
TEST_F(DefineOutlineTest, FailsMacroSpecifier) { |
| 521 |
FileName = "Test.hpp"; |
| 522 |
ExtraFiles["Test.cpp"] = ""; |
| 523 |
ExtraArgs.push_back("-DFINALOVER=final override"); |
| 524 |
|
| 525 |
std::pair<StringRef, StringRef> Cases[] = { |
| 526 |
{ |
| 527 |
R"cpp( |
| 528 |
#define VIRT virtual void |
| 529 |
struct A { |
| 530 |
VIRT fo^o() {} |
| 531 |
};)cpp", |
| 532 |
"fail: define outline: couldn't remove `virtual` keyword."}, |
| 533 |
{ |
| 534 |
R"cpp( |
| 535 |
#define OVERFINAL final override |
| 536 |
struct A { |
| 537 |
virtual void foo() {} |
| 538 |
}; |
| 539 |
struct B : A { |
| 540 |
void fo^o() OVERFINAL {} |
| 541 |
};)cpp", |
| 542 |
"fail: define outline: Can't move out of line as function has a " |
| 543 |
"macro `override` specifier.\ndefine outline: Can't move out of line " |
| 544 |
"as function has a macro `final` specifier."}, |
| 545 |
{ |
| 546 |
R"cpp( |
| 547 |
struct A { |
| 548 |
virtual void foo() {} |
| 549 |
}; |
| 550 |
struct B : A { |
| 551 |
void fo^o() FINALOVER {} |
| 552 |
};)cpp", |
| 553 |
"fail: define outline: Can't move out of line as function has a " |
| 554 |
"macro `override` specifier.\ndefine outline: Can't move out of line " |
| 555 |
"as function has a macro `final` specifier."}, |
| 556 |
}; |
| 557 |
for (const auto &Case : Cases) { |
| 558 |
EXPECT_EQ(apply(Case.first), Case.second); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
} // namespace |
| 563 |
} // namespace clangd |
| 564 |
} // namespace clang |