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 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
|
//===- unittest/Format/QualifierFixerTest.cpp - Formatting unit tests -----===//
//
// 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 "../lib/Format/QualifierAlignmentFixer.h"
#include "FormatTestBase.h"
#include "TestLexer.h"
#define DEBUG_TYPE "format-qualifier-fixer-test"
namespace clang {
namespace format {
namespace test {
namespace {
#define CHECK_PARSE(TEXT, FIELD, VALUE) \
EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \
EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
#define FAIL_PARSE(TEXT, FIELD, VALUE) \
EXPECT_NE(0, parseConfiguration(TEXT, &Style).value()); \
EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
class QualifierFixerTest : public FormatTestBase {
protected:
TokenList annotate(StringRef Code,
const FormatStyle &Style = getLLVMStyle()) {
return TestLexer(Allocator, Buffers, Style).annotate(Code);
}
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
};
TEST_F(QualifierFixerTest, RotateTokens) {
// TODO add test
EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("const"),
tok::kw_const);
EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("volatile"),
tok::kw_volatile);
EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("inline"),
tok::kw_inline);
EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("static"),
tok::kw_static);
EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("restrict"),
tok::kw_restrict);
EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("friend"),
tok::kw_friend);
}
TEST_F(QualifierFixerTest, FailQualifierInvalidConfiguration) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
FAIL_PARSE("QualifierAlignment: Custom\n"
"QualifierOrder: [const, volatile, apples, type]",
QualifierOrder,
std::vector<std::string>({"const", "volatile", "apples", "type"}));
}
TEST_F(QualifierFixerTest, FailQualifierDuplicateConfiguration) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
FAIL_PARSE("QualifierAlignment: Custom\n"
"QualifierOrder: [const, volatile, const, type]",
QualifierOrder,
std::vector<std::string>({"const", "volatile", "const", "type"}));
}
TEST_F(QualifierFixerTest, FailQualifierMissingType) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
FAIL_PARSE("QualifierAlignment: Custom\n"
"QualifierOrder: [const, volatile ]",
QualifierOrder,
std::vector<std::string>({
"const",
"volatile",
}));
}
TEST_F(QualifierFixerTest, FailQualifierEmptyOrder) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
FAIL_PARSE("QualifierAlignment: Custom\nQualifierOrder: []", QualifierOrder,
std::vector<std::string>({}));
}
TEST_F(QualifierFixerTest, FailQualifierMissingOrder) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
FAIL_PARSE("QualifierAlignment: Custom", QualifierOrder,
std::vector<std::string>());
}
TEST_F(QualifierFixerTest, QualifierLeft) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
CHECK_PARSE("QualifierAlignment: Left", QualifierOrder,
std::vector<std::string>({"const", "volatile", "type"}));
}
TEST_F(QualifierFixerTest, QualifierRight) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
CHECK_PARSE("QualifierAlignment: Right", QualifierOrder,
std::vector<std::string>({"type", "const", "volatile"}));
}
TEST_F(QualifierFixerTest, QualifiersCustomOrder) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Left;
Style.QualifierOrder = {"friend", "inline", "constexpr", "static",
"const", "volatile", "type"};
verifyFormat("const volatile int a;", Style);
verifyFormat("const volatile int a;", "volatile const int a;", Style);
verifyFormat("const volatile int a;", "int const volatile a;", Style);
verifyFormat("const volatile int a;", "int volatile const a;", Style);
verifyFormat("const volatile int a;", "const int volatile a;", Style);
verifyFormat("static const volatile int a;", "const static int volatile a;",
Style);
verifyFormat("inline static const volatile int a;",
"const static inline int volatile a;", Style);
verifyFormat("constexpr static int a;", "static constexpr int a;", Style);
verifyFormat("constexpr static int A;", "static constexpr int A;", Style);
verifyFormat("constexpr static int Bar;", "static constexpr int Bar;", Style);
verifyFormat("constexpr static LPINT Bar;", "static constexpr LPINT Bar;",
Style);
verifyFormat("const const int a;", "const int const a;", Style);
verifyFormat(
"friend constexpr auto operator<=>(const foo &, const foo &) = default;",
"constexpr friend auto operator<=>(const foo &, const foo &) = default;",
Style);
verifyFormat(
"friend constexpr bool operator==(const foo &, const foo &) = default;",
"constexpr bool friend operator==(const foo &, const foo &) = default;",
Style);
}
TEST_F(QualifierFixerTest, LeftRightQualifier) {
FormatStyle Style = getLLVMStyle();
// keep the const style unaltered
verifyFormat("const int a;", Style);
verifyFormat("const int *a;", Style);
verifyFormat("const int &a;", Style);
verifyFormat("const int &&a;", Style);
verifyFormat("int const b;", Style);
verifyFormat("int const *b;", Style);
verifyFormat("int const &b;", Style);
verifyFormat("int const &&b;", Style);
verifyFormat("int const *const b;", Style);
verifyFormat("int *const c;", Style);
verifyFormat("const Foo a;", Style);
verifyFormat("const Foo *a;", Style);
verifyFormat("const Foo &a;", Style);
verifyFormat("const Foo &&a;", Style);
verifyFormat("Foo const b;", Style);
verifyFormat("Foo const *b;", Style);
verifyFormat("Foo const &b;", Style);
verifyFormat("Foo const &&b;", Style);
verifyFormat("Foo const *const b;", Style);
verifyFormat("LLVM_NODISCARD const int &Foo();", Style);
verifyFormat("LLVM_NODISCARD int const &Foo();", Style);
verifyFormat("volatile const int *restrict;", Style);
verifyFormat("const volatile int *restrict;", Style);
verifyFormat("const int volatile *restrict;", Style);
}
TEST_F(QualifierFixerTest, RightQualifier) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Right;
Style.QualifierOrder = {"type", "const", "volatile"};
verifyFormat("int const a;", Style);
verifyFormat("int const *a;", Style);
verifyFormat("int const &a;", Style);
verifyFormat("int const &&a;", Style);
verifyFormat("int const b;", Style);
verifyFormat("int const *b;", Style);
verifyFormat("int const &b;", Style);
verifyFormat("int const &&b;", Style);
verifyFormat("int const *const b;", Style);
verifyFormat("int *const c;", Style);
verifyFormat("Foo const a;", Style);
verifyFormat("Foo const *a;", Style);
verifyFormat("Foo const &a;", Style);
verifyFormat("Foo const &&a;", Style);
verifyFormat("Foo const b;", Style);
verifyFormat("Foo const *b;", Style);
verifyFormat("Foo const &b;", Style);
verifyFormat("Foo const &&b;", Style);
verifyFormat("Foo const *const b;", Style);
verifyFormat("Foo *const b;", Style);
verifyFormat("Foo const *const b;", Style);
verifyFormat("auto const v = get_value();", Style);
verifyFormat("long long const &a;", Style);
verifyFormat("unsigned char const *a;", Style);
verifyFormat("int main(int const argc, char const *const *const argv)",
Style);
verifyFormat("LLVM_NODISCARD int const &Foo();", Style);
verifyFormat("SourceRange getSourceRange() const override LLVM_READONLY",
Style);
verifyFormat("void foo() const override;", Style);
verifyFormat("void foo() const override LLVM_READONLY;", Style);
verifyFormat("void foo() const final;", Style);
verifyFormat("void foo() const final LLVM_READONLY;", Style);
verifyFormat("void foo() const LLVM_READONLY;", Style);
verifyFormat("void foo() const volatile override;", Style);
verifyFormat("void foo() const volatile override LLVM_READONLY;", Style);
verifyFormat("void foo() const volatile final;", Style);
verifyFormat("void foo() const volatile final LLVM_READONLY;", Style);
verifyFormat("void foo() const volatile LLVM_READONLY;", Style);
verifyFormat(
"template <typename Func> explicit Action(Action<Func> const &action);",
Style);
verifyFormat(
"template <typename Func> explicit Action(Action<Func> const &action);",
"template <typename Func> explicit Action(const Action<Func>& action);",
Style);
verifyFormat(
"template <typename Func> explicit Action(Action<Func> const &action);",
"template <typename Func>\nexplicit Action(const Action<Func>& action);",
Style);
verifyFormat("int const a;", "const int a;", Style);
verifyFormat("int const *a;", "const int *a;", Style);
verifyFormat("int const &a;", "const int &a;", Style);
verifyFormat("foo(int const &a)", "foo(const int &a)", Style);
verifyFormat("unsigned char *a;", Style);
verifyFormat("unsigned char const *a;", "const unsigned char *a;", Style);
verifyFormat("vector<int, int const, int &, int const &> args1",
"vector<int, const int, int &, const int &> args1", Style);
verifyFormat("unsigned int const &get_nu() const",
"const unsigned int &get_nu() const", Style);
verifyFormat("Foo<int> const &a", "const Foo<int> &a", Style);
verifyFormat("Foo<int>::iterator const &a", "const Foo<int>::iterator &a",
Style);
verifyFormat("::Foo<int>::iterator const &a", "const ::Foo<int>::iterator &a",
Style);
verifyFormat("Foo(int a, "
"unsigned b, // c-style args\n"
" Bar const &c);",
"Foo(int a, "
"unsigned b, // c-style args\n"
" const Bar &c);",
Style);
verifyFormat("int const volatile;", "volatile const int;", Style);
verifyFormat("int const volatile;", "const volatile int;", Style);
verifyFormat("int const volatile;", "const int volatile;", Style);
verifyFormat("int const volatile *restrict;", "volatile const int *restrict;",
Style);
verifyFormat("int const volatile *restrict;", "const volatile int *restrict;",
Style);
verifyFormat("int const volatile *restrict;", "const int volatile *restrict;",
Style);
verifyFormat("long long int const volatile;", "const long long int volatile;",
Style);
verifyFormat("long long int const volatile;", "long const long int volatile;",
Style);
verifyFormat("long long int const volatile;", "long long volatile int const;",
Style);
verifyFormat("long long int const volatile;", "long volatile long const int;",
Style);
verifyFormat("long long int const volatile;", "const long long volatile int;",
Style);
verifyFormat("static int const bat;", "static const int bat;", Style);
verifyFormat("static int const bat;", Style);
// static is not configured, unchanged on the left of the right hand
// qualifiers.
verifyFormat("int static const volatile;", "volatile const int static;",
Style);
verifyFormat("int static const volatile;", "const volatile int static;",
Style);
verifyFormat("int static const volatile;", "const int volatile static;",
Style);
verifyFormat("Foo static const volatile;", "volatile const Foo static;",
Style);
verifyFormat("Foo static const volatile;", "const volatile Foo static;",
Style);
verifyFormat("Foo static const volatile;", "const Foo volatile static;",
Style);
verifyFormat("Foo inline static const;", "const Foo inline static;", Style);
verifyFormat("Foo inline static const;", "Foo const inline static;", Style);
verifyFormat("Foo inline static const;", "Foo inline const static;", Style);
verifyFormat("Foo inline static const;", Style);
verifyFormat("Foo<T volatile>::Bar<Type const, 5> const volatile A::*;",
"volatile const Foo<volatile T>::Bar<const Type, 5> A::*;",
Style);
verifyFormat("int const Foo<int>::bat = 0;", "const int Foo<int>::bat = 0;",
Style);
verifyFormat("int const Foo<int>::bat = 0;", Style);
verifyFormat("void fn(Foo<T> const &i);", "void fn(const Foo<T> &i);", Style);
verifyFormat("int const Foo<int>::fn() {", Style);
verifyFormat("Foo<Foo<int>> const *p;", "const Foo<Foo<int>> *p;", Style);
verifyFormat(
"Foo<Foo<int>> const *p = const_cast<Foo<Foo<int>> const *>(&ffi);",
"const Foo<Foo<int>> *p = const_cast<const Foo<Foo<int>> *>(&ffi);",
Style);
verifyFormat("void fn(Foo<T> const &i);", "void fn(const Foo<T> &i);", Style);
verifyFormat("void fns(ns::S const &s);", "void fns(const ns::S &s);", Style);
verifyFormat("void fns(::ns::S const &s);", "void fns(const ::ns::S &s);",
Style);
verifyFormat("void fn(ns::Foo<T> const &i);", "void fn(const ns::Foo<T> &i);",
Style);
verifyFormat("void fns(ns::ns2::S const &s);",
"void fns(const ns::ns2::S &s);", Style);
verifyFormat("void fn(ns::Foo<Bar<T>> const &i);",
"void fn(const ns::Foo<Bar<T>> &i);", Style);
verifyFormat("void fn(ns::ns2::Foo<Bar<T>> const &i);",
"void fn(const ns::ns2::Foo<Bar<T>> &i);", Style);
verifyFormat("void fn(ns::ns2::Foo<Bar<T, U>> const &i);",
"void fn(const ns::ns2::Foo<Bar<T, U>> &i);", Style);
verifyFormat("LocalScope const *Scope = nullptr;",
"const LocalScope* Scope = nullptr;", Style);
verifyFormat("struct DOTGraphTraits<Stmt const *>",
"struct DOTGraphTraits<const Stmt *>", Style);
verifyFormat(
"bool tools::addXRayRuntime(ToolChain const &TC, ArgList const &Args) {",
"bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args) {",
Style);
verifyFormat("Foo<Foo<int> const> P;", "Foo<const Foo<int>> P;", Style);
verifyFormat("Foo<Foo<int> const> P;\n#if 0\n#else\n#endif",
"Foo<const Foo<int>> P;\n#if 0\n#else\n#endif", Style);
verifyFormat("auto const i = 0;", "const auto i = 0;", Style);
verifyFormat("auto const &ir = i;", "const auto &ir = i;", Style);
verifyFormat("auto const *ip = &i;", "const auto *ip = &i;", Style);
verifyFormat("void f(Concept auto const &x);",
"void f(const Concept auto &x);", Style);
verifyFormat("void f(std::integral auto const &x);",
"void f(const std::integral auto &x);", Style);
verifyFormat("auto lambda = [] { int const i = 0; };",
"auto lambda = [] { const int i = 0; };", Style);
verifyFormat("Foo<Foo<int> const> P;\n#if 0\n#else\n#endif",
"Foo<const Foo<int>> P;\n#if 0\n#else\n#endif", Style);
verifyFormat("Bar<Bar<int const> const> P;\n#if 0\n#else\n#endif",
"Bar<Bar<const int> const> P;\n#if 0\n#else\n#endif", Style);
verifyFormat("Baz<Baz<int const> const> P;\n#if 0\n#else\n#endif",
"Baz<const Baz<const int>> P;\n#if 0\n#else\n#endif", Style);
// verifyFormat("#if 0\nBoo<Boo<int const> const> P;\n#else\n#endif",
// "#if 0\nBoo<const Boo<const int>> P;\n#else\n#endif", Style);
verifyFormat("int const P;\n#if 0\n#else\n#endif",
"const int P;\n#if 0\n#else\n#endif", Style);
verifyFormat("unsigned long const a;", "const unsigned long a;", Style);
verifyFormat("unsigned long long const a;", "const unsigned long long a;",
Style);
// Multiple template parameters.
verifyFormat("Bar<std::Foo const, 32>", "Bar<const std::Foo, 32>", Style);
// Variable declaration based on template type.
verifyFormat("Bar<std::Foo const> bar", "Bar<const std::Foo> bar", Style);
// Using typename for a nested dependent type name.
verifyFormat("typename Foo::iterator const;", "const typename Foo::iterator;",
Style);
// Don't move past C-style struct/class.
verifyFormat("void foo(const struct A a);", Style);
verifyFormat("void foo(const class A a);", Style);
// Don't move past struct/class combined declaration and variable
// definition.
verifyFormat("const struct {\n} var;", Style);
verifyFormat("struct {\n} const var;", Style);
verifyFormat("const class {\n} var;", Style);
verifyFormat("class {\n} const var;", Style);
// Leave left qualifers unchanged for combined declaration and variable
// definition.
verifyFormat("volatile const class {\n} var;", Style);
verifyFormat("const volatile class {\n} var;", Style);
// Also do no sorting with respect to not-configured tokens.
verifyFormat("const static volatile class {\n} var;", Style);
// Sort right qualifiers for combined declaration and variable definition.
verifyFormat("class {\n} const volatile var;", Style);
verifyFormat("class {\n} const volatile var;",
"class {\n} volatile const var;", Style);
// Static keyword is not configured, should end up on the left of the right
// side.
verifyFormat("class {\n} static const volatile var;", Style);
verifyFormat("class {\n} static const volatile var;",
"class {\n} volatile static const var;", Style);
// ::template for dependent names
verifyFormat("::template Foo<T> const volatile var;",
"const volatile ::template Foo<T> var;", Style);
verifyFormat("typename ::template Foo<T> const volatile var;",
"const volatile typename ::template Foo<T> var;", Style);
verifyFormat("typename Bar::template Foo<T>::T const;",
"const typename Bar::template Foo<T>::T;", Style);
verifyFormat("typename Bar::template Foo<T>::T const volatile;",
"const volatile typename Bar::template Foo<T>::T;", Style);
// typename ::
verifyFormat("typename ::Bar<int> const;", "const typename ::Bar<int>;",
Style);
// typename ::template
verifyFormat("typename ::template Bar<int> const;",
"const typename ::template Bar<int>;", Style);
verifyFormat("foo<Bar<Baz> const>();", "foo<const Bar<Baz>>();", Style);
verifyFormat("foo<Bar<Baz> const>();", "foo<const Bar<Baz> >();", Style);
verifyFormat("Bar<32, Foo<25> const>;", "Bar<32, const Foo<25>>;", Style);
verifyFormat("A<B<C<D> const> const>;", "A<const B<const C<D>>>;", Style);
verifyFormat("A<B<C<D const> const> const>;", "A<const B<const C<const D>>>;",
Style);
// Don't move past decltype, typeof, or _Atomic.
verifyFormat("const decltype(foo)", Style);
verifyFormat("const typeof(foo)", Style);
verifyFormat("const _Atomic(foo)", Style);
// Comments
const int ColumnLimit = Style.ColumnLimit;
Style.ColumnLimit = 200;
verifyFormat("/*c*/ Foo const *foo;", "const /*c*/ Foo *foo;", Style);
verifyFormat("Foo const /*c*/ *foo;", "const Foo /*c*/ *foo;", Style);
verifyFormat("Foo const * /*c*/ foo;", "const Foo * /*c*/ foo;", Style);
verifyFormat("/*comment*/ std::vector<int> const v;",
"const /*comment*/ std::vector<int> v;", Style);
verifyFormat("std /*comment*/ ::vector<int> const v;",
"const std /*comment*/ ::vector<int> v;", Style);
verifyFormat("std::/*comment*/ vector<int> const v;",
"const std::/*comment*/ vector<int> v;", Style);
verifyFormat("std::vector /*comment*/<int> const v;",
"const std::vector /*comment*/ <int> v;", Style);
verifyFormat("std::vector</*comment*/ int> const v;",
"const std::vector</*comment*/ int> v;", Style);
verifyFormat("std::vector<int /*comment*/> const v;",
"const std::vector<int /*comment*/> v;", Style);
verifyFormat("std::vector<int> const /*comment*/ v;",
"const std::vector<int> /*comment*/ v;", Style);
verifyFormat("std::vector</*comment*/ int const> v;",
"std::vector</*comment*/ const int> v;", Style);
verifyFormat("std::vector</*comment*/ int const> v;",
"std::vector<const /*comment*/ int> v;", Style);
verifyFormat("std::vector<int const /*comment*/> v;",
"std::vector<const int /*comment*/> v;", Style);
verifyFormat("std::vector</*comment*/ Foo const> v;",
"std::vector</*comment*/ const Foo> v;", Style);
verifyFormat("std::vector</*comment*/ Foo const> v;",
"std::vector<const /*comment*/ Foo> v;", Style);
verifyFormat("std::vector<Foo const /*comment*/> v;",
"std::vector<const Foo /*comment*/> v;", Style);
verifyFormat("typename C<T>::template B<T> const;",
"const typename C<T>::template B<T>;", Style);
verifyFormat("/*c*/ typename C<T>::template B<T> const;",
"const /*c*/ typename C<T>::template B<T>;", Style);
verifyFormat("typename /*c*/ C<T>::template B<T> const;",
"const typename /*c*/ C<T>::template B<T>;", Style);
verifyFormat("typename C /*c*/<T>::template B<T> const;",
"const typename C /*c*/<T>::template B<T>;", Style);
verifyFormat("typename C<T> /*c*/ ::template B<T> const;",
"const typename C<T> /*c*/ ::template B<T>;", Style);
verifyFormat("typename C<T>::/*c*/ template B<T> const;",
"const typename C<T>::/*c*/ template B<T>;", Style);
verifyFormat("typename C<T>::template /*c*/ B<T> const;",
"const typename C<T>::template /*c*/B<T>;", Style);
verifyFormat("typename C<T>::template B<T> const /*c*/;",
"const typename C<T>::template B<T>/*c*/;", Style);
verifyFormat("/*c*/ /*c*/ typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template "
"/*c*/ B /*c*/<T> const /*c*/ v;",
"/*c*/ const /*c*/ typename /*c*/ C /*c*/<T> /*c*/ "
"::/*c*/template /*c*/ B /*c*/<T> /*c*/ v;",
Style);
verifyFormat("/*c*/ unsigned /*c*/ long const /*c*/ a;",
"const /*c*/ unsigned /*c*/ long /*c*/ a;", Style);
verifyFormat("unsigned /*c*/ long /*c*/ long const a;",
"const unsigned /*c*/ long /*c*/ long a;", Style);
// Not changed
verifyFormat("foo() /*c*/ const", Style);
verifyFormat("const /*c*/ struct a;", Style);
verifyFormat("const /*c*/ class a;", Style);
verifyFormat("const /*c*/ decltype(v) a;", Style);
verifyFormat("const /*c*/ typeof(v) a;", Style);
verifyFormat("const /*c*/ _Atomic(v) a;", Style);
verifyFormat("const decltype /*c*/ (v) a;", Style);
verifyFormat("const /*c*/ class {\n} volatile /*c*/ foo = {};", Style);
Style.ColumnLimit = ColumnLimit;
// Don't adjust macros
verifyFormat("const INTPTR a;", Style);
// Pointers to members
verifyFormat("int S::*a;", Style);
verifyFormat("int const S::*a;", "const int S::*a;", Style);
verifyFormat("int const S::*const a;", "const int S::* const a;", Style);
verifyFormat("int A::*const A::*p1;", Style);
verifyFormat("float (C::*p)(int);", Style);
verifyFormat("float (C::*const p)(int);", Style);
verifyFormat("float (C::*p)(int) const;", Style);
verifyFormat("float const (C::*p)(int);", "const float (C::*p)(int);", Style);
}
TEST_F(QualifierFixerTest, LeftQualifier) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Left;
Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
verifyFormat("const int a;", Style);
verifyFormat("const int *a;", Style);
verifyFormat("const int &a;", Style);
verifyFormat("const int &&a;", Style);
verifyFormat("const int b;", Style);
verifyFormat("const int *b;", Style);
verifyFormat("const int &b;", Style);
verifyFormat("const int &&b;", Style);
verifyFormat("const int *const b;", Style);
verifyFormat("int *const c;", Style);
verifyFormat("const Foo a;", Style);
verifyFormat("const Foo *a;", Style);
verifyFormat("const Foo &a;", Style);
verifyFormat("const Foo &&a;", Style);
verifyFormat("const Foo b;", Style);
verifyFormat("const Foo *b;", Style);
verifyFormat("const Foo &b;", Style);
verifyFormat("const Foo &&b;", Style);
verifyFormat("const Foo *const b;", Style);
verifyFormat("Foo *const b;", Style);
verifyFormat("const Foo *const b;", Style);
verifyFormat("LLVM_NODISCARD const int &Foo();", Style);
verifyFormat("const char a[];", Style);
verifyFormat("const auto v = get_value();", Style);
verifyFormat("const long long &a;", Style);
verifyFormat("const unsigned char *a;", Style);
verifyFormat("const unsigned char *a;", "unsigned char const *a;", Style);
verifyFormat("const Foo<int> &a", "Foo<int> const &a", Style);
verifyFormat("const Foo<int>::iterator &a", "Foo<int>::iterator const &a",
Style);
verifyFormat("const ::Foo<int>::iterator &a", "::Foo<int>::iterator const &a",
Style);
verifyFormat("const int a;", "int const a;", Style);
verifyFormat("const int *a;", "int const *a;", Style);
verifyFormat("const int &a;", "int const &a;", Style);
verifyFormat("foo(const int &a)", "foo(int const &a)", Style);
verifyFormat("unsigned char *a;", Style);
verifyFormat("const unsigned int &get_nu() const",
"unsigned int const &get_nu() const", Style);
verifyFormat("const volatile int;", "volatile const int;", Style);
verifyFormat("const volatile int;", Style);
verifyFormat("const volatile int;", "const int volatile;", Style);
verifyFormat("const volatile int *restrict;", "volatile const int *restrict;",
Style);
verifyFormat("const volatile int *restrict;", Style);
verifyFormat("const volatile int *restrict;", "const int volatile *restrict;",
Style);
verifyFormat("const volatile long long int;", "volatile long long int const;",
Style);
verifyFormat("const volatile long long int;", "volatile long long const int;",
Style);
verifyFormat("const volatile long long int;", "long long volatile int const;",
Style);
verifyFormat("const volatile long long int;", "long volatile long int const;",
Style);
verifyFormat("const volatile long long int;", "const long long volatile int;",
Style);
verifyFormat("SourceRange getSourceRange() const override LLVM_READONLY;",
Style);
verifyFormat("void foo() const override;", Style);
verifyFormat("void foo() const override LLVM_READONLY;", Style);
verifyFormat("void foo() const final;", Style);
verifyFormat("void foo() const final LLVM_READONLY;", Style);
verifyFormat("void foo() const LLVM_READONLY;", Style);
verifyFormat(
"template <typename Func> explicit Action(const Action<Func> &action);",
Style);
verifyFormat(
"template <typename Func> explicit Action(const Action<Func> &action);",
"template <typename Func> explicit Action(Action<Func> const &action);",
Style);
verifyFormat("static const int bat;", Style);
verifyFormat("static const int bat;", "static int const bat;", Style);
verifyFormat("static const int Foo<int>::bat = 0;", Style);
verifyFormat("static const int Foo<int>::bat = 0;",
"static int const Foo<int>::bat = 0;", Style);
verifyFormat("void fn(const Foo<T> &i);");
verifyFormat("const int Foo<int>::bat = 0;", Style);
verifyFormat("const int Foo<int>::bat = 0;", "int const Foo<int>::bat = 0;",
Style);
verifyFormat("void fn(const Foo<T> &i);", "void fn( Foo<T> const &i);",
Style);
verifyFormat("const int Foo<int>::fn() {", "int const Foo<int>::fn() {",
Style);
verifyFormat("const Foo<Foo<int>> *p;", "Foo<Foo<int>> const *p;", Style);
verifyFormat(
"const Foo<Foo<int>> *p = const_cast<const Foo<Foo<int>> *>(&ffi);",
"const Foo<Foo<int>> *p = const_cast<Foo<Foo<int>> const *>(&ffi);",
Style);
verifyFormat("void fn(const Foo<T> &i);", "void fn(Foo<T> const &i);", Style);
verifyFormat("void fns(const ns::S &s);", "void fns(ns::S const &s);", Style);
verifyFormat("void fns(const ::ns::S &s);", "void fns(::ns::S const &s);",
Style);
verifyFormat("void fn(const ns::Foo<T> &i);", "void fn(ns::Foo<T> const &i);",
Style);
verifyFormat("void fns(const ns::ns2::S &s);",
"void fns(ns::ns2::S const &s);", Style);
verifyFormat("void fn(const ns::Foo<Bar<T>> &i);",
"void fn(ns::Foo<Bar<T>> const &i);", Style);
verifyFormat("void fn(const ns::ns2::Foo<Bar<T>> &i);",
"void fn(ns::ns2::Foo<Bar<T>> const &i);", Style);
verifyFormat("void fn(const ns::ns2::Foo<Bar<T, U>> &i);",
"void fn(ns::ns2::Foo<Bar<T, U>> const &i);", Style);
verifyFormat("const auto i = 0;", "auto const i = 0;", Style);
verifyFormat("const auto &ir = i;", "auto const &ir = i;", Style);
verifyFormat("const auto *ip = &i;", "auto const *ip = &i;", Style);
verifyFormat("void f(const Concept auto &x);",
"void f(Concept auto const &x);", Style);
verifyFormat("void f(const std::integral auto &x);",
"void f(std::integral auto const &x);", Style);
verifyFormat("auto lambda = [] { const int i = 0; };",
"auto lambda = [] { int const i = 0; };", Style);
verifyFormat("Foo<const Foo<int>> P;\n#if 0\n#else\n#endif",
"Foo<Foo<int> const> P;\n#if 0\n#else\n#endif", Style);
verifyFormat("Foo<Foo<const int>> P;\n#if 0\n#else\n#endif",
"Foo<Foo<int const>> P;\n#if 0\n#else\n#endif", Style);
verifyFormat("const int P;\n#if 0\n#else\n#endif",
"int const P;\n#if 0\n#else\n#endif", Style);
verifyFormat("const unsigned long a;", "unsigned long const a;", Style);
verifyFormat("const unsigned long long a;", "unsigned long long const a;",
Style);
verifyFormat("const long long unsigned a;", "long const long unsigned a;",
Style);
verifyFormat("const std::Foo", Style);
verifyFormat("const std::Foo<>", Style);
verifyFormat("const std::Foo < int", "const std::Foo<int", Style);
verifyFormat("const std::Foo<int>", Style);
// Multiple template parameters.
verifyFormat("Bar<const std::Foo, 32>;", "Bar<std::Foo const, 32>;", Style);
// Variable declaration based on template type.
verifyFormat("Bar<const std::Foo> bar;", "Bar<std::Foo const> bar;", Style);
// Using typename for a dependent name.
verifyFormat("const typename Foo::iterator;", "typename Foo::iterator const;",
Style);
// Don't move past C-style struct/class.
verifyFormat("void foo(struct A const a);", Style);
verifyFormat("void foo(class A const a);", Style);
// Don't move past struct/class combined declaration and variable
// definition.
verifyFormat("const struct {\n} var;", Style);
verifyFormat("struct {\n} const var;", Style);
verifyFormat("const class {\n} var;", Style);
verifyFormat("class {\n} const var;", Style);
// Sort left qualifiers for struct/class combined declaration and variable
// definition.
verifyFormat("const volatile class {\n} var;", Style);
verifyFormat("const volatile class {\n} var;",
"volatile const class {\n} var;", Style);
// Leave right qualifers unchanged for struct/class combined declaration and
// variable definition.
verifyFormat("class {\n} const volatile var;", Style);
verifyFormat("class {\n} volatile const var;", Style);
verifyFormat("foo<const Bar<Baz<T>>>();", "foo<Bar<Baz<T>> const>();", Style);
verifyFormat("foo<const Bar<Baz<T>>>();", "foo<Bar<Baz<T> > const>();",
Style);
verifyFormat("Bar<32, const Foo<25>>;", "Bar<32, Foo<25> const>;", Style);
verifyFormat("A<const B<const C<D>>>;", "A<B<C<D> const> const>;", Style);
verifyFormat("A<const B<const C<const D>>>;", "A<B<C<D const> const> const>;",
Style);
// Don't move past decltype, typeof, or _Atomic.
verifyFormat("decltype(foo) const", Style);
verifyFormat("typeof(foo) const", Style);
verifyFormat("_Atomic(foo) const", Style);
// ::template for dependent names
verifyFormat("const volatile ::template Foo<T> var;",
"::template Foo<T> const volatile var;", Style);
verifyFormat("const volatile typename ::template Foo<T> var;",
"typename ::template Foo<T> const volatile var;", Style);
verifyFormat("const typename Bar::template Foo<T>::T;",
"typename Bar::template Foo<T>::T const;", Style);
verifyFormat("const volatile typename Bar::template Foo<T>::T;",
"typename Bar::template Foo<T>::T const volatile;", Style);
// typename ::
verifyFormat("const typename ::Bar<int>;", "typename ::Bar<int> const;",
Style);
// typename ::template
verifyFormat("const typename ::template Bar<int>;",
"typename ::template Bar<int> const;", Style);
// Comments
const int ColumnLimit = Style.ColumnLimit;
Style.ColumnLimit = 200;
verifyFormat("/*c*/ const Foo *foo;", "/*c*/ Foo const *foo;", Style);
verifyFormat("const Foo /*c*/ *foo;", "Foo const /*c*/ *foo;", Style);
verifyFormat("const Foo * /*c*/ foo;", "Foo const * /*c*/ foo;", Style);
verifyFormat("/*comment*/ const std::vector<int> v;",
"/*comment*/ std::vector<int> const v;", Style);
verifyFormat("const std /*comment*/ ::vector<int> v;",
"std /*comment*/ ::vector<int> const v;", Style);
verifyFormat("const std::/*comment*/ vector<int> v;",
"std::/*comment*/ vector<int> const v;", Style);
verifyFormat("const std::vector /*comment*/<int> v;",
"std::vector /*comment*/<int> const v;", Style);
verifyFormat("const std::vector</*comment*/ int> v;",
"std::vector</*comment*/ int> const v;", Style);
verifyFormat("const std::vector<int /*comment*/> v;",
"std::vector<int /*comment*/> const v;", Style);
verifyFormat("const std::vector<int> /*comment*/ v;",
"std::vector<int> /*comment*/ const v;", Style);
verifyFormat("std::vector</*comment*/ const int> v;",
"std::vector</*comment*/ int const> v;", Style);
verifyFormat("std::vector<const int /*comment*/> v;",
"std::vector<int /*comment*/ const> v;", Style);
verifyFormat("std::vector<const int /*comment*/> v;",
"std::vector<int const /*comment*/> v;", Style);
verifyFormat("std::vector</*comment*/ const Foo> v;",
"std::vector</*comment*/ Foo const> v;", Style);
verifyFormat("std::vector<const Foo /*comment*/> v;",
"std::vector<Foo /*comment*/ const> v;", Style);
verifyFormat("std::vector<const Foo /*comment*/> v;",
"std::vector<Foo const /*comment*/> v;", Style);
verifyFormat("const typename C<T>::template B<T>;",
"typename C<T>::template B<T> const;", Style);
verifyFormat("/*c*/ const typename C<T>::template B<T>;",
"/*c*/ typename C<T>::template B<T> const;", Style);
verifyFormat("const typename /*c*/ C<T>::template B<T>;",
"typename /*c*/ C<T>::template B<T> const;", Style);
verifyFormat("const typename C /*c*/<T>::template B<T>;",
"typename C /*c*/<T>::template B<T> const;", Style);
verifyFormat("const typename C<T> /*c*/ ::template B<T>;",
"typename C<T> /*c*/ ::template B<T> const;", Style);
verifyFormat("const typename C<T>::/*c*/ template B<T>;",
"typename C<T>::/*c*/ template B<T> const;", Style);
verifyFormat("const typename C<T>::template /*c*/ B<T>;",
"typename C<T>::template /*c*/ B<T> const;", Style);
verifyFormat("const typename C<T>::template B<T> /*c*/;",
"typename C<T>::template B<T> /*c*/ const;", Style);
verifyFormat("/*c*/ const typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template "
"/*c*/ B /*c*/<T> /*c*/ v;",
"/*c*/ typename /*c*/ C /*c*/<T> /*c*/ ::/*c*/ template /*c*/ B "
"/*c*/<T> /*c*/ const v;",
Style);
verifyFormat("const unsigned /*c*/ long /*c*/ a;",
"unsigned /*c*/ long /*c*/ const a;", Style);
verifyFormat("const unsigned /*c*/ long /*c*/ long a;",
"unsigned /*c*/ long /*c*/ long const a;", Style);
// Not changed
verifyFormat("foo() /*c*/ const", Style);
verifyFormat("struct /*c*/ const a;", Style);
verifyFormat("class /*c*/ const a;", Style);
verifyFormat("decltype(v) /*c*/ const a;", Style);
verifyFormat("typeof(v) /*c*/ const a;", Style);
verifyFormat("_Atomic(v) /*c*/ const a;", Style);
verifyFormat("decltype /*c*/ (v) const a;", Style);
verifyFormat("const /*c*/ class {\n} /*c*/ volatile /*c*/ foo = {};", Style);
Style.ColumnLimit = ColumnLimit;
// Don't adjust macros
verifyFormat("INTPTR const a;", Style);
// Pointers to members
verifyFormat("int S::*a;", Style);
verifyFormat("const int S::*a;", "int const S::*a;", Style);
verifyFormat("const int S::*const a;", "int const S::*const a;", Style);
verifyFormat("int A::*const A::*p1;", Style);
verifyFormat("float (C::*p)(int);", Style);
verifyFormat("float (C::*const p)(int);", Style);
verifyFormat("float (C::*p)(int) const;", Style);
verifyFormat("const float (C::*p)(int);", "float const (C::*p)(int);", Style);
}
TEST_F(QualifierFixerTest, ConstVolatileQualifiersOrder) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Left;
Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
// The Default
EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
verifyFormat("const volatile int a;", Style);
verifyFormat("const volatile int a;", "volatile const int a;", Style);
verifyFormat("const volatile int a;", "int const volatile a;", Style);
verifyFormat("const volatile int a;", "int volatile const a;", Style);
verifyFormat("const volatile int a;", "const int volatile a;", Style);
verifyFormat("const volatile Foo a;", Style);
verifyFormat("const volatile Foo a;", "volatile const Foo a;", Style);
verifyFormat("const volatile Foo a;", "Foo const volatile a;", Style);
verifyFormat("const volatile Foo a;", "Foo volatile const a;", Style);
verifyFormat("const volatile Foo a;", "const Foo volatile a;", Style);
Style.QualifierAlignment = FormatStyle::QAS_Right;
Style.QualifierOrder = {"type", "const", "volatile"};
verifyFormat("int const volatile a;", "const volatile int a;", Style);
verifyFormat("int const volatile a;", "volatile const int a;", Style);
verifyFormat("int const volatile a;", Style);
verifyFormat("int const volatile a;", "int volatile const a;", Style);
verifyFormat("int const volatile a;", "const int volatile a;", Style);
verifyFormat("Foo const volatile a;", "const volatile Foo a;", Style);
verifyFormat("Foo const volatile a;", "volatile const Foo a;", Style);
verifyFormat("Foo const volatile a;", Style);
verifyFormat("Foo const volatile a;", "Foo volatile const a;", Style);
verifyFormat("Foo const volatile a;", "const Foo volatile a;", Style);
Style.QualifierAlignment = FormatStyle::QAS_Left;
Style.QualifierOrder = {"volatile", "const", "type"};
verifyFormat("volatile const int a;", "const volatile int a;", Style);
verifyFormat("volatile const int a;", Style);
verifyFormat("volatile const int a;", "int const volatile a;", Style);
verifyFormat("volatile const int a;", "int volatile const a;", Style);
verifyFormat("volatile const int a;", "const int volatile a;", Style);
verifyFormat("volatile const Foo a;", "const volatile Foo a;", Style);
verifyFormat("volatile const Foo a;", Style);
verifyFormat("volatile const Foo a;", "Foo const volatile a;", Style);
verifyFormat("volatile const Foo a;", "Foo volatile const a;", Style);
verifyFormat("volatile const Foo a;", "const Foo volatile a;", Style);
Style.QualifierAlignment = FormatStyle::QAS_Right;
Style.QualifierOrder = {"type", "volatile", "const"};
verifyFormat("int volatile const a;", "const volatile int a;", Style);
verifyFormat("int volatile const a;", "volatile const int a;", Style);
verifyFormat("int volatile const a;", "int const volatile a;", Style);
verifyFormat("int volatile const a;", Style);
verifyFormat("int volatile const a;", "const int volatile a;", Style);
verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style);
verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style);
verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style);
verifyFormat("Foo volatile const a;", Style);
verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style);
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"type", "volatile", "const"};
verifyFormat("int volatile const a;", "const volatile int a;", Style);
verifyFormat("int volatile const a;", "volatile const int a;", Style);
verifyFormat("int volatile const a;", "int const volatile a;", Style);
verifyFormat("int volatile const a;", Style);
verifyFormat("int volatile const a;", "const int volatile a;", Style);
verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style);
verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style);
verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style);
verifyFormat("Foo volatile const a;", Style);
verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style);
}
TEST_F(QualifierFixerTest, InlineStatics) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Left;
Style.QualifierOrder = {"inline", "static", "const", "volatile", "type"};
EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
verifyFormat("inline static const volatile int a;",
"const inline static volatile int a;", Style);
verifyFormat("inline static const volatile int a;",
"volatile inline static const int a;", Style);
verifyFormat("inline static const volatile int a;",
"int const inline static volatile a;", Style);
verifyFormat("inline static const volatile int a;",
"int volatile inline static const a;", Style);
verifyFormat("inline static const volatile int a;",
"const int inline static volatile a;", Style);
}
TEST_F(QualifierFixerTest, AmpEqual) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"static", "type", "const"};
EXPECT_EQ(Style.QualifierOrder.size(), (size_t)3);
verifyFormat("foo(std::string const & = std::string()) const",
"foo(const std::string & = std::string()) const", Style);
verifyFormat("foo(std::string const & = std::string())",
"foo(const std::string & = std::string())", Style);
}
TEST_F(QualifierFixerTest, MoveConstBeyondTypeSmall) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"type", "const"};
EXPECT_EQ(Style.QualifierOrder.size(), (size_t)2);
verifyFormat("int const a;", "const int a;", Style);
verifyFormat("int const *a;", "const int*a;", Style);
verifyFormat("int const *a;", "const int *a;", Style);
verifyFormat("int const &a;", "const int &a;", Style);
verifyFormat("int const &&a;", "const int &&a;", Style);
}
TEST_F(QualifierFixerTest, MoveConstBeforeTypeSmall) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"const", "type"};
EXPECT_EQ(Style.QualifierOrder.size(), (size_t)2);
verifyFormat("const int a;", "int const a;", Style);
verifyFormat("const int *a;", "int const *a;", Style);
verifyFormat("const int *const a;", "int const *const a;", Style);
verifyFormat("const int a = foo();", "int const a = foo();", Style);
verifyFormat("const int *a = foo();", "int const *a = foo();", Style);
verifyFormat("const int *const a = foo();", "int const *const a = foo();",
Style);
verifyFormat("const auto a = foo();", "auto const a = foo();", Style);
verifyFormat("const auto *a = foo();", "auto const *a = foo();", Style);
verifyFormat("const auto *const a = foo();", "auto const *const a = foo();",
Style);
}
TEST_F(QualifierFixerTest, MoveConstBeyondType) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"static", "inline", "type", "const", "volatile"};
EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5);
verifyFormat("static inline int const volatile a;",
"const inline static volatile int a;", Style);
verifyFormat("static inline int const volatile a;",
"volatile inline static const int a;", Style);
verifyFormat("static inline int const volatile a;",
"int const inline static volatile a;", Style);
verifyFormat("static inline int const volatile a;",
"int volatile inline static const a;", Style);
verifyFormat("static inline int const volatile a;",
"const int inline static volatile a;", Style);
verifyFormat("static inline int const volatile *const a;",
"const int inline static volatile *const a;", Style);
verifyFormat("static inline Foo const volatile a;",
"const inline static volatile Foo a;", Style);
verifyFormat("static inline Foo const volatile a;",
"volatile inline static const Foo a;", Style);
verifyFormat("static inline Foo const volatile a;",
"Foo const inline static volatile a;", Style);
verifyFormat("static inline Foo const volatile a;",
"Foo volatile inline static const a;", Style);
verifyFormat("static inline Foo const volatile a;",
"const Foo inline static volatile a;", Style);
verifyFormat("static inline Foo const volatile *const a;",
"const Foo inline static volatile *const a;", Style);
}
TEST_F(QualifierFixerTest, PrepareLeftRightOrdering) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"static", "inline", "type", "const", "volatile"};
std::vector<std::string> Left;
std::vector<std::string> Right;
std::vector<tok::TokenKind> ConfiguredTokens;
prepareLeftRightOrderingForQualifierAlignmentFixer(Style.QualifierOrder, Left,
Right, ConfiguredTokens);
EXPECT_EQ(Left.size(), (size_t)2);
EXPECT_EQ(Right.size(), (size_t)2);
std::vector<std::string> LeftResult = {"inline", "static"};
std::vector<std::string> RightResult = {"const", "volatile"};
EXPECT_EQ(Left, LeftResult);
EXPECT_EQ(Right, RightResult);
}
TEST_F(QualifierFixerTest, IsQualifierType) {
std::vector<tok::TokenKind> ConfiguredTokens;
ConfiguredTokens.push_back(tok::kw_const);
ConfiguredTokens.push_back(tok::kw_static);
ConfiguredTokens.push_back(tok::kw_inline);
ConfiguredTokens.push_back(tok::kw_restrict);
ConfiguredTokens.push_back(tok::kw_constexpr);
ConfiguredTokens.push_back(tok::kw_friend);
TestLexer lexer{Allocator, Buffers};
const auto LangOpts = getFormattingLangOpts();
auto Tokens = lexer.lex(
"const static inline auto restrict int double long constexpr friend");
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_TRUE(
isConfiguredQualifierOrType(Tokens[0], ConfiguredTokens, LangOpts));
EXPECT_TRUE(
isConfiguredQualifierOrType(Tokens[1], ConfiguredTokens, LangOpts));
EXPECT_TRUE(
isConfiguredQualifierOrType(Tokens[2], ConfiguredTokens, LangOpts));
EXPECT_TRUE(
isConfiguredQualifierOrType(Tokens[3], ConfiguredTokens, LangOpts));
EXPECT_TRUE(
isConfiguredQualifierOrType(Tokens[4], ConfiguredTokens, LangOpts));
EXPECT_TRUE(
isConfiguredQualifierOrType(Tokens[5], ConfiguredTokens, LangOpts));
EXPECT_TRUE(
isConfiguredQualifierOrType(Tokens[6], ConfiguredTokens, LangOpts));
EXPECT_TRUE(
isConfiguredQualifierOrType(Tokens[7], ConfiguredTokens, LangOpts));
EXPECT_TRUE(
isConfiguredQualifierOrType(Tokens[8], ConfiguredTokens, LangOpts));
EXPECT_TRUE(
isConfiguredQualifierOrType(Tokens[9], ConfiguredTokens, LangOpts));
EXPECT_TRUE(isQualifierOrType(Tokens[0], LangOpts));
EXPECT_TRUE(isQualifierOrType(Tokens[1], LangOpts));
EXPECT_TRUE(isQualifierOrType(Tokens[2], LangOpts));
EXPECT_TRUE(isQualifierOrType(Tokens[3], LangOpts));
EXPECT_TRUE(isQualifierOrType(Tokens[4], LangOpts));
EXPECT_TRUE(isQualifierOrType(Tokens[5], LangOpts));
EXPECT_TRUE(isQualifierOrType(Tokens[6], LangOpts));
EXPECT_TRUE(isQualifierOrType(Tokens[7], LangOpts));
EXPECT_TRUE(isQualifierOrType(Tokens[8], LangOpts));
EXPECT_TRUE(isQualifierOrType(Tokens[9], LangOpts));
auto NotTokens = lexer.lex("for while do Foo Bar ");
ASSERT_EQ(NotTokens.size(), 6u) << Tokens;
EXPECT_FALSE(
isConfiguredQualifierOrType(NotTokens[0], ConfiguredTokens, LangOpts));
EXPECT_FALSE(
isConfiguredQualifierOrType(NotTokens[1], ConfiguredTokens, LangOpts));
EXPECT_FALSE(
isConfiguredQualifierOrType(NotTokens[2], ConfiguredTokens, LangOpts));
EXPECT_FALSE(
isConfiguredQualifierOrType(NotTokens[3], ConfiguredTokens, LangOpts));
EXPECT_FALSE(
isConfiguredQualifierOrType(NotTokens[4], ConfiguredTokens, LangOpts));
EXPECT_FALSE(
isConfiguredQualifierOrType(NotTokens[5], ConfiguredTokens, LangOpts));
EXPECT_FALSE(isQualifierOrType(NotTokens[0], LangOpts));
EXPECT_FALSE(isQualifierOrType(NotTokens[1], LangOpts));
EXPECT_FALSE(isQualifierOrType(NotTokens[2], LangOpts));
EXPECT_FALSE(isQualifierOrType(NotTokens[3], LangOpts));
EXPECT_FALSE(isQualifierOrType(NotTokens[4], LangOpts));
EXPECT_FALSE(isQualifierOrType(NotTokens[5], LangOpts));
}
TEST_F(QualifierFixerTest, IsMacro) {
auto Tokens = annotate("INT INTPR Foo int");
ASSERT_EQ(Tokens.size(), 5u) << Tokens;
EXPECT_TRUE(isPossibleMacro(Tokens[0]));
EXPECT_TRUE(isPossibleMacro(Tokens[1]));
EXPECT_FALSE(isPossibleMacro(Tokens[2]));
EXPECT_FALSE(isPossibleMacro(Tokens[3]));
}
TEST_F(QualifierFixerTest, OverlappingQualifier) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Left;
Style.QualifierOrder = {"const", "type"};
verifyFormat("Foo(const Bar &name);", "Foo(Bar const &name);", Style);
}
TEST_F(QualifierFixerTest, DontPushQualifierThroughNonSpecifiedTypes) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Left;
Style.QualifierOrder = {"const", "volatile", "type"};
verifyFormat("inline static const int a;", Style);
Style.QualifierOrder = {"static", "const", "type"};
verifyFormat("inline static const int a;", Style);
verifyFormat("static inline const int a;", Style);
verifyFormat("static const int a;", "const static int a;", Style);
Style.QualifierOrder = {"const", "volatile", "type"};
// static is not configured, unchanged at right hand qualifiers.
verifyFormat("const volatile int static;", "int volatile static const;",
Style);
verifyFormat("const volatile int static;", "int const static volatile;",
Style);
verifyFormat("const volatile int static;", "const int static volatile;",
Style);
verifyFormat("const volatile Foo static;", "Foo volatile static const;",
Style);
verifyFormat("const volatile Foo static;", "Foo const static volatile;",
Style);
verifyFormat("const volatile Foo static;", "const Foo static volatile;",
Style);
verifyFormat("inline static const Foo;", "inline static Foo const;", Style);
verifyFormat("inline static const Foo;", Style);
// Don't move qualifiers to the right for aestethics only.
verifyFormat("inline const static Foo;", Style);
verifyFormat("const inline static Foo;", Style);
}
TEST_F(QualifierFixerTest, QualifiersBrokenUpByPPDirectives) {
auto Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"constexpr", "inline", "type"};
verifyFormat("inline\n"
"#if FOO\n"
" constexpr\n"
"#endif\n"
" int i = 0;",
Style);
}
TEST_F(QualifierFixerTest, UnsignedQualifier) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Left;
Style.QualifierOrder = {"const", "type"};
verifyFormat("Foo(const unsigned char *bytes)",
"Foo(unsigned const char *bytes)", Style);
Style.QualifierAlignment = FormatStyle::QAS_Right;
Style.QualifierOrder = {"type", "const"};
verifyFormat("Foo(unsigned char const *bytes)",
"Foo(unsigned const char *bytes)", Style);
}
TEST_F(QualifierFixerTest, NoOpQualifierReplacements) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"static", "const", "type"};
verifyFormat("static const uint32 foo[] = {0, 31};", Style);
EXPECT_EQ(ReplacementCount, 0);
verifyFormat("#define MACRO static const", Style);
EXPECT_EQ(ReplacementCount, 0);
verifyFormat("using sc = static const", Style);
EXPECT_EQ(ReplacementCount, 0);
}
TEST_F(QualifierFixerTest, QualifierTemplates) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"static", "const", "type"};
ReplacementCount = 0;
EXPECT_EQ(ReplacementCount, 0);
verifyFormat("using A = B<>;", Style);
verifyFormat("using A = B /**/<>;", Style);
verifyFormat("template <class C> using A = B<Foo<C>, 1>;", Style);
verifyFormat("template <class C> using A = B /**/<Foo<C>, 1>;", Style);
verifyFormat("template <class C> using A = B /* */<Foo<C>, 1>;", Style);
verifyFormat("template <class C> using A = B /*foo*/<Foo<C>, 1>;", Style);
verifyFormat("template <class C> using A = B /**/ /**/<Foo<C>, 1>;", Style);
verifyFormat("template <class C> using A = B<Foo</**/ C>, 1>;", Style);
verifyFormat("template <class C> using A = /**/ B<Foo<C>, 1>;", Style);
EXPECT_EQ(ReplacementCount, 0);
verifyFormat("template <class C>\n"
"using A = B // foo\n"
" <Foo<C>, 1>;",
Style);
ReplacementCount = 0;
Style.QualifierOrder = {"type", "static", "const"};
verifyFormat("using A = B<>;", Style);
verifyFormat("using A = B /**/<>;", Style);
verifyFormat("template <class C> using A = B<Foo<C>, 1>;", Style);
verifyFormat("template <class C> using A = B /**/<Foo<C>, 1>;", Style);
verifyFormat("template <class C> using A = B /* */<Foo<C>, 1>;", Style);
verifyFormat("template <class C> using A = B /*foo*/<Foo<C>, 1>;", Style);
verifyFormat("template <class C> using A = B /**/ /**/<Foo<C>, 1>;", Style);
verifyFormat("template <class C> using A = B<Foo</**/ C>, 1>;", Style);
verifyFormat("template <class C> using A = /**/ B<Foo<C>, 1>;", Style);
EXPECT_EQ(ReplacementCount, 0);
verifyFormat("template <class C>\n"
"using A = B // foo\n"
" <Foo<C>, 1>;",
Style);
}
TEST_F(QualifierFixerTest, WithConstraints) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"constexpr", "type"};
verifyFormat("template <typename T>\n"
" requires Concept<F>\n"
"constexpr constructor();",
Style);
verifyFormat("template <typename T>\n"
" requires Concept1<F> && Concept2<F>\n"
"constexpr constructor();",
Style);
}
TEST_F(QualifierFixerTest, DisableRegions) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"inline", "static", "const", "type"};
ReplacementCount = 0;
verifyFormat("// clang-format off\n"
"int const inline static a = 0;\n"
"// clang-format on",
Style);
EXPECT_EQ(ReplacementCount, 0);
verifyFormat("// clang-format off\n"
"int const inline static a = 0;\n"
"// clang-format on\n"
"inline static const int a = 0;",
"// clang-format off\n"
"int const inline static a = 0;\n"
"// clang-format on\n"
"int const inline static a = 0;",
Style);
}
TEST_F(QualifierFixerTest, TemplatesRight) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"type", "const"};
verifyFormat("template <typename T> Foo const f();",
"template <typename T> const Foo f();", Style);
verifyFormat("template <typename T> int const f();",
"template <typename T> const int f();", Style);
verifyFormat("template <T const> t;", "template <const T> t;", Style);
verifyFormat("template <typename T>\n"
" requires Concept<T const>\n"
"Foo const f();",
"template <typename T>\n"
" requires Concept<const T>\n"
"const Foo f();",
Style);
verifyFormat("TemplateType<T const> t;", "TemplateType<const T> t;", Style);
verifyFormat("TemplateType<Container const> t;",
"TemplateType<const Container> t;", Style);
}
TEST_F(QualifierFixerTest, TemplatesLeft) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"const", "volatile", "type"};
verifyFormat("template <typename T> const Foo f();",
"template <typename T> Foo const f();", Style);
verifyFormat("template <typename T> const int f();",
"template <typename T> int const f();", Style);
verifyFormat("template <const T> t;", "template <T const> t;", Style);
verifyFormat("template <typename T>\n"
" requires Concept<const T>\n"
"const Foo f();",
"template <typename T>\n"
" requires Concept<T const>\n"
"Foo const f();",
Style);
verifyFormat("template <typename T>\n"
" requires Concept<const T>\n"
"const volatile Foo f();",
"template <typename T>\n"
" requires Concept<T const>\n"
"volatile const Foo f();",
Style);
verifyFormat("TemplateType<const T> t;", "TemplateType<T const> t;", Style);
verifyFormat("TemplateType<const Container> t;",
"TemplateType<Container const> t;", Style);
}
TEST_F(QualifierFixerTest, Ranges) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"const", "volatile", "type"};
// Only the first line should be formatted; the second should remain as is.
verifyFormat("template <typename T> const Foo f();\n"
"template <typename T> Foo const f();",
"template <typename T> Foo const f();\n"
"template <typename T> Foo const f();",
Style, {tooling::Range(0, 36)});
// Only the middle line should be formatted; the first and last should remain
// as is.
verifyFormat("template <typename T> Foo const f();\n"
"template <typename T> const Foo f();\n"
"template <typename T> Foo const f();",
"template <typename T> Foo const f();\n"
"template <typename T> Foo const f();\n"
"template <typename T> Foo const f();",
Style, {tooling::Range(37, 36)});
}
} // namespace
} // namespace test
} // namespace format
} // namespace clang
|