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 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
|
//===-- DefineInlineTests.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 "TweakTesting.h"
#include "TestFS.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using ::testing::ElementsAre;
namespace clang {
namespace clangd {
namespace {
TWEAK_TEST(DefineInline);
TEST_F(DefineInlineTest, TriggersOnFunctionDecl) {
// Basic check for function body and signature.
EXPECT_AVAILABLE(R"cpp(
class Bar {
void baz();
};
[[void [[Bar::[[b^a^z]]]]() [[{
return;
}]]]]
void foo();
[[void [[f^o^o]]() [[{
return;
}]]]]
)cpp");
EXPECT_UNAVAILABLE(R"cpp(
// Not a definition
vo^i[[d^ ^f]]^oo();
[[vo^id ]]foo[[()]] {[[
[[(void)(5+3);
return;]]
}]]
// Definition with no body.
class Bar { Bar() = def^ault; };
)cpp");
}
TEST_F(DefineInlineTest, NoForwardDecl) {
Header = "void bar();";
EXPECT_UNAVAILABLE(R"cpp(
void bar() {
return;
}
// FIXME: Generate a decl in the header.
void fo^o() {
return;
})cpp");
}
TEST_F(DefineInlineTest, ReferencedDecls) {
EXPECT_AVAILABLE(R"cpp(
void bar();
void foo(int test);
void fo^o(int baz) {
int x = 10;
bar();
})cpp");
// Internal symbol usage.
Header = "void foo(int test);";
EXPECT_UNAVAILABLE(R"cpp(
void bar();
void fo^o(int baz) {
int x = 10;
bar();
})cpp");
// Becomes available after making symbol visible.
Header = "void bar();" + Header;
EXPECT_AVAILABLE(R"cpp(
void fo^o(int baz) {
int x = 10;
bar();
})cpp");
// FIXME: Move declaration below bar to make it visible.
Header.clear();
EXPECT_UNAVAILABLE(R"cpp(
void foo();
void bar();
void fo^o() {
bar();
})cpp");
// Order doesn't matter within a class.
EXPECT_AVAILABLE(R"cpp(
class Bar {
void foo();
void bar();
};
void Bar::fo^o() {
bar();
})cpp");
// FIXME: Perform include insertion to make symbol visible.
ExtraFiles["a.h"] = "void bar();";
Header = "void foo(int test);";
EXPECT_UNAVAILABLE(R"cpp(
#include "a.h"
void fo^o(int baz) {
int x = 10;
bar();
})cpp");
}
TEST_F(DefineInlineTest, TemplateSpec) {
EXPECT_UNAVAILABLE(R"cpp(
template <typename T> void foo();
template<> void foo<char>();
template<> void f^oo<int>() {
})cpp");
EXPECT_UNAVAILABLE(R"cpp(
template <typename T> void foo();
template<> void f^oo<int>() {
})cpp");
EXPECT_UNAVAILABLE(R"cpp(
template <typename T> struct Foo { void foo(); };
template <typename T> void Foo<T>::f^oo() {
})cpp");
EXPECT_AVAILABLE(R"cpp(
template <typename T> void foo();
void bar();
template <> void foo<int>();
template<> void f^oo<int>() {
bar();
})cpp");
EXPECT_UNAVAILABLE(R"cpp(
namespace bar {
template <typename T> void f^oo() {}
template void foo<int>();
})cpp");
}
TEST_F(DefineInlineTest, CheckForCanonDecl) {
EXPECT_UNAVAILABLE(R"cpp(
void foo();
void bar() {}
void f^oo() {
// This bar normally refers to the definition just above, but it is not
// visible from the forward declaration of foo.
bar();
})cpp");
// Make it available with a forward decl.
EXPECT_AVAILABLE(R"cpp(
void bar();
void foo();
void bar() {}
void f^oo() {
bar();
})cpp");
}
TEST_F(DefineInlineTest, UsingShadowDecls) {
EXPECT_UNAVAILABLE(R"cpp(
namespace ns1 { void foo(int); }
namespace ns2 { void foo(int*); }
template <typename T>
void bar();
using ns1::foo;
using ns2::foo;
template <typename T>
void b^ar() {
foo(T());
})cpp");
}
TEST_F(DefineInlineTest, TransformNestedNamespaces) {
auto *Test = R"cpp(
namespace a {
void bar();
namespace b {
void baz();
namespace c {
void aux();
}
}
}
void foo();
using namespace a;
using namespace b;
using namespace c;
void f^oo() {
bar();
a::bar();
baz();
b::baz();
a::b::baz();
aux();
c::aux();
b::c::aux();
a::b::c::aux();
})cpp";
auto *Expected = R"cpp(
namespace a {
void bar();
namespace b {
void baz();
namespace c {
void aux();
}
}
}
void foo(){
a::bar();
a::bar();
a::b::baz();
a::b::baz();
a::b::baz();
a::b::c::aux();
a::b::c::aux();
a::b::c::aux();
a::b::c::aux();
}
using namespace a;
using namespace b;
using namespace c;
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformUsings) {
auto *Test = R"cpp(
namespace a { namespace b { namespace c { void aux(); } } }
void foo();
void f^oo() {
using namespace a;
using namespace b;
using namespace c;
using c::aux;
namespace d = c;
})cpp";
auto *Expected = R"cpp(
namespace a { namespace b { namespace c { void aux(); } } }
void foo(){
using namespace a;
using namespace a::b;
using namespace a::b::c;
using a::b::c::aux;
namespace d = a::b::c;
}
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformDecls) {
auto *Test = R"cpp(
void foo();
void f^oo() {
class Foo {
public:
void foo();
int x;
};
enum En { Zero, One };
En x = Zero;
enum class EnClass { Zero, One };
EnClass y = EnClass::Zero;
})cpp";
auto *Expected = R"cpp(
void foo(){
class Foo {
public:
void foo();
int x;
};
enum En { Zero, One };
En x = Zero;
enum class EnClass { Zero, One };
EnClass y = EnClass::Zero;
}
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformTemplDecls) {
auto *Test = R"cpp(
namespace a {
template <typename T> class Bar {
public:
void bar();
};
template <typename T> T bar;
template <typename T> void aux() {}
}
void foo();
using namespace a;
void f^oo() {
bar<Bar<int>>.bar();
aux<Bar<int>>();
})cpp";
auto *Expected = R"cpp(
namespace a {
template <typename T> class Bar {
public:
void bar();
};
template <typename T> T bar;
template <typename T> void aux() {}
}
void foo(){
a::bar<a::Bar<int>>.bar();
a::aux<a::Bar<int>>();
}
using namespace a;
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformMembers) {
auto *Test = R"cpp(
class Foo {
void foo();
};
void Foo::f^oo() {
return;
})cpp";
auto *Expected = R"cpp(
class Foo {
void foo(){
return;
}
};
)cpp";
EXPECT_EQ(apply(Test), Expected);
ExtraFiles["a.h"] = R"cpp(
class Foo {
void foo();
};)cpp";
llvm::StringMap<std::string> EditedFiles;
Test = R"cpp(
#include "a.h"
void Foo::f^oo() {
return;
})cpp";
Expected = R"cpp(
#include "a.h"
)cpp";
EXPECT_EQ(apply(Test, &EditedFiles), Expected);
Expected = R"cpp(
class Foo {
void foo(){
return;
}
};)cpp";
EXPECT_THAT(EditedFiles,
ElementsAre(FileWithContents(testPath("a.h"), Expected)));
}
TEST_F(DefineInlineTest, TransformDependentTypes) {
auto *Test = R"cpp(
namespace a {
template <typename T> class Bar {};
}
template <typename T>
void foo();
using namespace a;
template <typename T>
void f^oo() {
Bar<T> B;
Bar<Bar<T>> q;
})cpp";
auto *Expected = R"cpp(
namespace a {
template <typename T> class Bar {};
}
template <typename T>
void foo(){
a::Bar<T> B;
a::Bar<a::Bar<T>> q;
}
using namespace a;
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformFunctionTempls) {
// Check we select correct specialization decl.
std::pair<llvm::StringRef, llvm::StringRef> Cases[] = {
{R"cpp(
template <typename T>
void foo(T p);
template <>
void foo<int>(int p);
template <>
void foo<char>(char p);
template <>
void fo^o<int>(int p) {
return;
})cpp",
R"cpp(
template <typename T>
void foo(T p);
template <>
void foo<int>(int p){
return;
}
template <>
void foo<char>(char p);
)cpp"},
{// Make sure we are not selecting the first specialization all the time.
R"cpp(
template <typename T>
void foo(T p);
template <>
void foo<int>(int p);
template <>
void foo<char>(char p);
template <>
void fo^o<char>(char p) {
return;
})cpp",
R"cpp(
template <typename T>
void foo(T p);
template <>
void foo<int>(int p);
template <>
void foo<char>(char p){
return;
}
)cpp"},
{R"cpp(
template <typename T>
void foo(T p);
template <>
void foo<int>(int p);
template <typename T>
void fo^o(T p) {
return;
})cpp",
R"cpp(
template <typename T>
void foo(T p){
return;
}
template <>
void foo<int>(int p);
)cpp"},
};
for (const auto &Case : Cases)
EXPECT_EQ(apply(Case.first), Case.second) << Case.first;
}
TEST_F(DefineInlineTest, TransformTypeLocs) {
auto *Test = R"cpp(
namespace a {
template <typename T> class Bar {
public:
template <typename Q> class Baz {};
};
class Foo{};
}
void foo();
using namespace a;
void f^oo() {
Bar<int> B;
Foo foo;
a::Bar<Bar<int>>::Baz<Bar<int>> q;
})cpp";
auto *Expected = R"cpp(
namespace a {
template <typename T> class Bar {
public:
template <typename Q> class Baz {};
};
class Foo{};
}
void foo(){
a::Bar<int> B;
a::Foo foo;
a::Bar<a::Bar<int>>::Baz<a::Bar<int>> q;
}
using namespace a;
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformDeclRefs) {
auto *Test = R"cpp(
namespace a {
template <typename T> class Bar {
public:
void foo();
static void bar();
int x;
static int y;
};
void bar();
void test();
}
void foo();
using namespace a;
void f^oo() {
a::Bar<int> B;
B.foo();
a::bar();
Bar<Bar<int>>::bar();
a::Bar<int>::bar();
B.x = Bar<int>::y;
Bar<int>::y = 3;
bar();
a::test();
})cpp";
auto *Expected = R"cpp(
namespace a {
template <typename T> class Bar {
public:
void foo();
static void bar();
int x;
static int y;
};
void bar();
void test();
}
void foo(){
a::Bar<int> B;
B.foo();
a::bar();
a::Bar<a::Bar<int>>::bar();
a::Bar<int>::bar();
B.x = a::Bar<int>::y;
a::Bar<int>::y = 3;
a::bar();
a::test();
}
using namespace a;
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, StaticMembers) {
auto *Test = R"cpp(
namespace ns { class X { static void foo(); void bar(); }; }
void ns::X::b^ar() {
foo();
})cpp";
auto *Expected = R"cpp(
namespace ns { class X { static void foo(); void bar(){
foo();
} }; }
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformParamNames) {
std::pair<llvm::StringRef, llvm::StringRef> Cases[] = {
{R"cpp(
void foo(int, bool b, int T\
est);
void ^foo(int f, bool x, int z) {})cpp",
R"cpp(
void foo(int f, bool x, int z){}
)cpp"},
{R"cpp(
#define PARAM int Z
void foo(PARAM);
void ^foo(int X) {})cpp",
"fail: Cant rename parameter inside macro body."},
{R"cpp(
#define TYPE int
#define PARAM TYPE Z
#define BODY(x) 5 * (x) + 2
template <int P>
void foo(PARAM, TYPE Q, TYPE, TYPE W = BODY(P));
template <int x>
void ^foo(int Z, int b, int c, int d) {})cpp",
R"cpp(
#define TYPE int
#define PARAM TYPE Z
#define BODY(x) 5 * (x) + 2
template <int x>
void foo(PARAM, TYPE b, TYPE c, TYPE d = BODY(x)){}
)cpp"},
};
for (const auto &Case : Cases)
EXPECT_EQ(apply(Case.first), Case.second) << Case.first;
}
TEST_F(DefineInlineTest, TransformTemplParamNames) {
auto *Test = R"cpp(
struct Foo {
struct Bar {
template <class, class X,
template<typename> class, template<typename> class Y,
int, int Z>
void foo(X, Y<X>, int W = 5 * Z + 2);
};
};
template <class T, class U,
template<typename> class V, template<typename> class W,
int X, int Y>
void Foo::Bar::f^oo(U, W<U>, int Q) {})cpp";
auto *Expected = R"cpp(
struct Foo {
struct Bar {
template <class T, class U,
template<typename> class V, template<typename> class W,
int X, int Y>
void foo(U, W<U>, int Q = 5 * Y + 2){}
};
};
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TransformInlineNamespaces) {
auto *Test = R"cpp(
namespace a { inline namespace b { namespace { struct Foo{}; } } }
void foo();
using namespace a;
void ^foo() {Foo foo;})cpp";
auto *Expected = R"cpp(
namespace a { inline namespace b { namespace { struct Foo{}; } } }
void foo(){a::Foo foo;}
using namespace a;
)cpp";
EXPECT_EQ(apply(Test), Expected);
}
TEST_F(DefineInlineTest, TokensBeforeSemicolon) {
std::pair<llvm::StringRef, llvm::StringRef> Cases[] = {
{R"cpp(
void foo() /*Comment -_-*/ /*Com 2*/ ;
void fo^o() { return ; })cpp",
R"cpp(
void foo() /*Comment -_-*/ /*Com 2*/ { return ; }
)cpp"},
{R"cpp(
void foo();
void fo^o() { return ; })cpp",
R"cpp(
void foo(){ return ; }
)cpp"},
{R"cpp(
#define SEMI ;
void foo() SEMI
void fo^o() { return ; })cpp",
"fail: Couldn't find semicolon for target declaration."},
};
for (const auto &Case : Cases)
EXPECT_EQ(apply(Case.first), Case.second) << Case.first;
}
TEST_F(DefineInlineTest, HandleMacros) {
EXPECT_UNAVAILABLE(R"cpp(
#define BODY { return; }
void foo();
void f^oo()BODY)cpp");
EXPECT_UNAVAILABLE(R"cpp(
#define BODY void foo(){ return; }
void foo();
[[BODY]])cpp");
std::pair<llvm::StringRef, llvm::StringRef> Cases[] = {
// We don't qualify declarations coming from macros.
{R"cpp(
#define BODY Foo
namespace a { class Foo{}; }
void foo();
using namespace a;
void f^oo(){BODY();})cpp",
R"cpp(
#define BODY Foo
namespace a { class Foo{}; }
void foo(){BODY();}
using namespace a;
)cpp"},
// Macro is not visible at declaration location, but we proceed.
{R"cpp(
void foo();
#define BODY return;
void f^oo(){BODY})cpp",
R"cpp(
void foo(){BODY}
#define BODY return;
)cpp"},
{R"cpp(
#define TARGET void foo()
TARGET;
void f^oo(){ return; })cpp",
R"cpp(
#define TARGET void foo()
TARGET{ return; }
)cpp"},
{R"cpp(
#define TARGET foo
void TARGET();
void f^oo(){ return; })cpp",
R"cpp(
#define TARGET foo
void TARGET(){ return; }
)cpp"},
};
for (const auto &Case : Cases)
EXPECT_EQ(apply(Case.first), Case.second) << Case.first;
}
TEST_F(DefineInlineTest, DropCommonNameSpecifiers) {
struct {
llvm::StringRef Test;
llvm::StringRef Expected;
} Cases[] = {
{R"cpp(
namespace a { namespace b { void aux(); } }
namespace ns1 {
void foo();
namespace qq { void test(); }
namespace ns2 {
void bar();
namespace ns3 { void baz(); }
}
}
using namespace a;
using namespace a::b;
using namespace ns1::qq;
void ns1::ns2::ns3::b^az() {
foo();
bar();
baz();
ns1::ns2::ns3::baz();
aux();
test();
})cpp",
R"cpp(
namespace a { namespace b { void aux(); } }
namespace ns1 {
void foo();
namespace qq { void test(); }
namespace ns2 {
void bar();
namespace ns3 { void baz(){
foo();
bar();
baz();
ns1::ns2::ns3::baz();
a::b::aux();
qq::test();
} }
}
}
using namespace a;
using namespace a::b;
using namespace ns1::qq;
)cpp"},
{R"cpp(
namespace ns1 {
namespace qq { struct Foo { struct Bar {}; }; using B = Foo::Bar; }
namespace ns2 { void baz(); }
}
using namespace ns1::qq;
void ns1::ns2::b^az() { Foo f; B b; })cpp",
R"cpp(
namespace ns1 {
namespace qq { struct Foo { struct Bar {}; }; using B = Foo::Bar; }
namespace ns2 { void baz(){ qq::Foo f; qq::B b; } }
}
using namespace ns1::qq;
)cpp"},
{R"cpp(
namespace ns1 {
namespace qq {
template<class T> struct Foo { template <class U> struct Bar {}; };
template<class T, class U>
using B = typename Foo<T>::template Bar<U>;
}
namespace ns2 { void baz(); }
}
using namespace ns1::qq;
void ns1::ns2::b^az() { B<int, bool> b; })cpp",
R"cpp(
namespace ns1 {
namespace qq {
template<class T> struct Foo { template <class U> struct Bar {}; };
template<class T, class U>
using B = typename Foo<T>::template Bar<U>;
}
namespace ns2 { void baz(){ qq::B<int, bool> b; } }
}
using namespace ns1::qq;
)cpp"},
};
for (const auto &Case : Cases)
EXPECT_EQ(apply(Case.Test), Case.Expected) << Case.Test;
}
TEST_F(DefineInlineTest, QualifyWithUsingDirectives) {
llvm::StringRef Test = R"cpp(
namespace a {
void bar();
namespace b { struct Foo{}; void aux(); }
namespace c { void cux(); }
}
using namespace a;
using X = b::Foo;
void foo();
using namespace b;
using namespace c;
void ^foo() {
cux();
bar();
X x;
aux();
using namespace c;
// FIXME: The last reference to cux() in body of foo should not be
// qualified, since there is a using directive inside the function body.
cux();
})cpp";
llvm::StringRef Expected = R"cpp(
namespace a {
void bar();
namespace b { struct Foo{}; void aux(); }
namespace c { void cux(); }
}
using namespace a;
using X = b::Foo;
void foo(){
c::cux();
bar();
X x;
b::aux();
using namespace c;
// FIXME: The last reference to cux() in body of foo should not be
// qualified, since there is a using directive inside the function body.
c::cux();
}
using namespace b;
using namespace c;
)cpp";
EXPECT_EQ(apply(Test), Expected) << Test;
}
TEST_F(DefineInlineTest, AddInline) {
llvm::StringMap<std::string> EditedFiles;
ExtraFiles["a.h"] = "void foo();";
apply(R"cpp(#include "a.h"
void fo^o() {})cpp",
&EditedFiles);
EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
testPath("a.h"), "inline void foo(){}")));
// Check we put inline before cv-qualifiers.
ExtraFiles["a.h"] = "const int foo();";
apply(R"cpp(#include "a.h"
const int fo^o() {})cpp",
&EditedFiles);
EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
testPath("a.h"), "inline const int foo(){}")));
// No double inline.
ExtraFiles["a.h"] = "inline void foo();";
apply(R"cpp(#include "a.h"
inline void fo^o() {})cpp",
&EditedFiles);
EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
testPath("a.h"), "inline void foo(){}")));
// Constexprs don't need "inline".
ExtraFiles["a.h"] = "constexpr void foo();";
apply(R"cpp(#include "a.h"
constexpr void fo^o() {})cpp",
&EditedFiles);
EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
testPath("a.h"), "constexpr void foo(){}")));
// Class members don't need "inline".
ExtraFiles["a.h"] = "struct Foo { void foo(); };";
apply(R"cpp(#include "a.h"
void Foo::fo^o() {})cpp",
&EditedFiles);
EXPECT_THAT(EditedFiles,
testing::ElementsAre(FileWithContents(
testPath("a.h"), "struct Foo { void foo(){} };")));
// Function template doesn't need to be "inline"d.
ExtraFiles["a.h"] = "template <typename T> void foo();";
apply(R"cpp(#include "a.h"
template <typename T>
void fo^o() {})cpp",
&EditedFiles);
EXPECT_THAT(EditedFiles,
testing::ElementsAre(FileWithContents(
testPath("a.h"), "template <typename T> void foo(){}")));
// Specializations needs to be marked "inline".
ExtraFiles["a.h"] = R"cpp(
template <typename T> void foo();
template <> void foo<int>();)cpp";
apply(R"cpp(#include "a.h"
template <>
void fo^o<int>() {})cpp",
&EditedFiles);
EXPECT_THAT(EditedFiles,
testing::ElementsAre(FileWithContents(testPath("a.h"),
R"cpp(
template <typename T> void foo();
template <> inline void foo<int>(){})cpp")));
}
} // namespace
} // namespace clangd
} // namespace clang
|