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 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
|
//===--- WhitespaceManager.cpp - Format C++ code --------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements WhitespaceManager class.
///
//===----------------------------------------------------------------------===//
#include "WhitespaceManager.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include <algorithm>
namespace clang {
namespace format {
bool WhitespaceManager::Change::IsBeforeInFile::operator()(
const Change &C1, const Change &C2) const {
return SourceMgr.isBeforeInTranslationUnit(
C1.OriginalWhitespaceRange.getBegin(),
C2.OriginalWhitespaceRange.getBegin()) ||
(C1.OriginalWhitespaceRange.getBegin() ==
C2.OriginalWhitespaceRange.getBegin() &&
SourceMgr.isBeforeInTranslationUnit(
C1.OriginalWhitespaceRange.getEnd(),
C2.OriginalWhitespaceRange.getEnd()));
}
WhitespaceManager::Change::Change(const FormatToken &Tok,
bool CreateReplacement,
SourceRange OriginalWhitespaceRange,
int Spaces, unsigned StartOfTokenColumn,
unsigned NewlinesBefore,
StringRef PreviousLinePostfix,
StringRef CurrentLinePrefix, bool IsAligned,
bool ContinuesPPDirective, bool IsInsideToken)
: Tok(&Tok), CreateReplacement(CreateReplacement),
OriginalWhitespaceRange(OriginalWhitespaceRange),
StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
PreviousLinePostfix(PreviousLinePostfix),
CurrentLinePrefix(CurrentLinePrefix), IsAligned(IsAligned),
ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces),
IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0),
PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
StartOfBlockComment(nullptr), IndentationOffset(0), ConditionalsLevel(0) {
}
void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
unsigned Spaces,
unsigned StartOfTokenColumn,
bool IsAligned, bool InPPDirective) {
if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
return;
Tok.setDecision((Newlines > 0) ? FD_Break : FD_Continue);
Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
Spaces, StartOfTokenColumn, Newlines, "", "",
IsAligned, InPPDirective && !Tok.IsFirst,
/*IsInsideToken=*/false));
}
void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
bool InPPDirective) {
if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
return;
Changes.push_back(Change(Tok, /*CreateReplacement=*/false,
Tok.WhitespaceRange, /*Spaces=*/0,
Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
/*IsAligned=*/false, InPPDirective && !Tok.IsFirst,
/*IsInsideToken=*/false));
}
llvm::Error
WhitespaceManager::addReplacement(const tooling::Replacement &Replacement) {
return Replaces.add(Replacement);
}
bool WhitespaceManager::inputUsesCRLF(StringRef Text, bool DefaultToCRLF) {
size_t LF = Text.count('\n');
size_t CR = Text.count('\r') * 2;
return LF == CR ? DefaultToCRLF : CR > LF;
}
void WhitespaceManager::replaceWhitespaceInToken(
const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
unsigned Newlines, int Spaces) {
if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
return;
SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
Changes.push_back(
Change(Tok, /*CreateReplacement=*/true,
SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
/*IsAligned=*/true, InPPDirective && !Tok.IsFirst,
/*IsInsideToken=*/true));
}
const tooling::Replacements &WhitespaceManager::generateReplacements() {
if (Changes.empty())
return Replaces;
llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr));
calculateLineBreakInformation();
alignConsecutiveMacros();
alignConsecutiveShortCaseStatements(/*IsExpr=*/true);
alignConsecutiveShortCaseStatements(/*IsExpr=*/false);
alignConsecutiveDeclarations();
alignConsecutiveBitFields();
alignConsecutiveAssignments();
if (Style.isTableGen()) {
alignConsecutiveTableGenBreakingDAGArgColons();
alignConsecutiveTableGenCondOperatorColons();
alignConsecutiveTableGenDefinitions();
}
alignChainedConditionals();
alignTrailingComments();
alignEscapedNewlines();
alignArrayInitializers();
generateChanges();
return Replaces;
}
void WhitespaceManager::calculateLineBreakInformation() {
Changes[0].PreviousEndOfTokenColumn = 0;
Change *LastOutsideTokenChange = &Changes[0];
for (unsigned I = 1, e = Changes.size(); I != e; ++I) {
auto &C = Changes[I];
auto &P = Changes[I - 1];
auto &PrevTokLength = P.TokenLength;
SourceLocation OriginalWhitespaceStart =
C.OriginalWhitespaceRange.getBegin();
SourceLocation PreviousOriginalWhitespaceEnd =
P.OriginalWhitespaceRange.getEnd();
unsigned OriginalWhitespaceStartOffset =
SourceMgr.getFileOffset(OriginalWhitespaceStart);
unsigned PreviousOriginalWhitespaceEndOffset =
SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
assert(PreviousOriginalWhitespaceEndOffset <=
OriginalWhitespaceStartOffset);
const char *const PreviousOriginalWhitespaceEndData =
SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
StringRef Text(PreviousOriginalWhitespaceEndData,
SourceMgr.getCharacterData(OriginalWhitespaceStart) -
PreviousOriginalWhitespaceEndData);
// Usually consecutive changes would occur in consecutive tokens. This is
// not the case however when analyzing some preprocessor runs of the
// annotated lines. For example, in this code:
//
// #if A // line 1
// int i = 1;
// #else B // line 2
// int i = 2;
// #endif // line 3
//
// one of the runs will produce the sequence of lines marked with line 1, 2
// and 3. So the two consecutive whitespace changes just before '// line 2'
// and before '#endif // line 3' span multiple lines and tokens:
//
// #else B{change X}[// line 2
// int i = 2;
// ]{change Y}#endif // line 3
//
// For this reason, if the text between consecutive changes spans multiple
// newlines, the token length must be adjusted to the end of the original
// line of the token.
auto NewlinePos = Text.find_first_of('\n');
if (NewlinePos == StringRef::npos) {
PrevTokLength = OriginalWhitespaceStartOffset -
PreviousOriginalWhitespaceEndOffset +
C.PreviousLinePostfix.size() + P.CurrentLinePrefix.size();
if (!P.IsInsideToken)
PrevTokLength = std::min(PrevTokLength, P.Tok->ColumnWidth);
} else {
PrevTokLength = NewlinePos + P.CurrentLinePrefix.size();
}
// If there are multiple changes in this token, sum up all the changes until
// the end of the line.
if (P.IsInsideToken && P.NewlinesBefore == 0)
LastOutsideTokenChange->TokenLength += PrevTokLength + P.Spaces;
else
LastOutsideTokenChange = &P;
C.PreviousEndOfTokenColumn = P.StartOfTokenColumn + PrevTokLength;
P.IsTrailingComment =
(C.NewlinesBefore > 0 || C.Tok->is(tok::eof) ||
(C.IsInsideToken && C.Tok->is(tok::comment))) &&
P.Tok->is(tok::comment) &&
// FIXME: This is a dirty hack. The problem is that
// BreakableLineCommentSection does comment reflow changes and here is
// the aligning of trailing comments. Consider the case where we reflow
// the second line up in this example:
//
// // line 1
// // line 2
//
// That amounts to 2 changes by BreakableLineCommentSection:
// - the first, delimited by (), for the whitespace between the tokens,
// - and second, delimited by [], for the whitespace at the beginning
// of the second token:
//
// // line 1(
// )[// ]line 2
//
// So in the end we have two changes like this:
//
// // line1()[ ]line 2
//
// Note that the OriginalWhitespaceStart of the second change is the
// same as the PreviousOriginalWhitespaceEnd of the first change.
// In this case, the below check ensures that the second change doesn't
// get treated as a trailing comment change here, since this might
// trigger additional whitespace to be wrongly inserted before "line 2"
// by the comment aligner here.
//
// For a proper solution we need a mechanism to say to WhitespaceManager
// that a particular change breaks the current sequence of trailing
// comments.
OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
}
// FIXME: The last token is currently not always an eof token; in those
// cases, setting TokenLength of the last token to 0 is wrong.
Changes.back().TokenLength = 0;
Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
const WhitespaceManager::Change *LastBlockComment = nullptr;
for (auto &Change : Changes) {
// Reset the IsTrailingComment flag for changes inside of trailing comments
// so they don't get realigned later. Comment line breaks however still need
// to be aligned.
if (Change.IsInsideToken && Change.NewlinesBefore == 0)
Change.IsTrailingComment = false;
Change.StartOfBlockComment = nullptr;
Change.IndentationOffset = 0;
if (Change.Tok->is(tok::comment)) {
if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken) {
LastBlockComment = &Change;
} else if ((Change.StartOfBlockComment = LastBlockComment)) {
Change.IndentationOffset =
Change.StartOfTokenColumn -
Change.StartOfBlockComment->StartOfTokenColumn;
}
} else {
LastBlockComment = nullptr;
}
}
// Compute conditional nesting level
// Level is increased for each conditional, unless this conditional continues
// a chain of conditional, i.e. starts immediately after the colon of another
// conditional.
SmallVector<bool, 16> ScopeStack;
int ConditionalsLevel = 0;
for (auto &Change : Changes) {
for (unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; ++i) {
bool isNestedConditional =
Change.Tok->FakeLParens[e - 1 - i] == prec::Conditional &&
!(i == 0 && Change.Tok->Previous &&
Change.Tok->Previous->is(TT_ConditionalExpr) &&
Change.Tok->Previous->is(tok::colon));
if (isNestedConditional)
++ConditionalsLevel;
ScopeStack.push_back(isNestedConditional);
}
Change.ConditionalsLevel = ConditionalsLevel;
for (unsigned i = Change.Tok->FakeRParens; i > 0 && ScopeStack.size(); --i)
if (ScopeStack.pop_back_val())
--ConditionalsLevel;
}
}
// Align a single sequence of tokens, see AlignTokens below.
// Column - The token for which Matches returns true is moved to this column.
// RightJustify - Whether it is the token's right end or left end that gets
// moved to that column.
template <typename F>
static void
AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
unsigned Column, bool RightJustify, F &&Matches,
SmallVector<WhitespaceManager::Change, 16> &Changes) {
bool FoundMatchOnLine = false;
int Shift = 0;
// ScopeStack keeps track of the current scope depth. It contains indices of
// the first token on each scope.
// We only run the "Matches" function on tokens from the outer-most scope.
// However, we do need to pay special attention to one class of tokens
// that are not in the outer-most scope, and that is function parameters
// which are split across multiple lines, as illustrated by this example:
// double a(int x);
// int b(int y,
// double z);
// In the above example, we need to take special care to ensure that
// 'double z' is indented along with it's owning function 'b'.
// The same holds for calling a function:
// double a = foo(x);
// int b = bar(foo(y),
// foor(z));
// Similar for broken string literals:
// double x = 3.14;
// auto s = "Hello"
// "World";
// Special handling is required for 'nested' ternary operators.
SmallVector<unsigned, 16> ScopeStack;
for (unsigned i = Start; i != End; ++i) {
auto &CurrentChange = Changes[i];
if (ScopeStack.size() != 0 &&
CurrentChange.indentAndNestingLevel() <
Changes[ScopeStack.back()].indentAndNestingLevel()) {
ScopeStack.pop_back();
}
// Compare current token to previous non-comment token to ensure whether
// it is in a deeper scope or not.
unsigned PreviousNonComment = i - 1;
while (PreviousNonComment > Start &&
Changes[PreviousNonComment].Tok->is(tok::comment)) {
--PreviousNonComment;
}
if (i != Start && CurrentChange.indentAndNestingLevel() >
Changes[PreviousNonComment].indentAndNestingLevel()) {
ScopeStack.push_back(i);
}
bool InsideNestedScope = ScopeStack.size() != 0;
bool ContinuedStringLiteral = i > Start &&
CurrentChange.Tok->is(tok::string_literal) &&
Changes[i - 1].Tok->is(tok::string_literal);
bool SkipMatchCheck = InsideNestedScope || ContinuedStringLiteral;
if (CurrentChange.NewlinesBefore > 0 && !SkipMatchCheck) {
Shift = 0;
FoundMatchOnLine = false;
}
// If this is the first matching token to be aligned, remember by how many
// spaces it has to be shifted, so the rest of the changes on the line are
// shifted by the same amount
if (!FoundMatchOnLine && !SkipMatchCheck && Matches(CurrentChange)) {
FoundMatchOnLine = true;
Shift = Column - (RightJustify ? CurrentChange.TokenLength : 0) -
CurrentChange.StartOfTokenColumn;
CurrentChange.Spaces += Shift;
// FIXME: This is a workaround that should be removed when we fix
// http://llvm.org/PR53699. An assertion later below verifies this.
if (CurrentChange.NewlinesBefore == 0) {
CurrentChange.Spaces =
std::max(CurrentChange.Spaces,
static_cast<int>(CurrentChange.Tok->SpacesRequiredBefore));
}
}
if (Shift == 0)
continue;
// This is for function parameters that are split across multiple lines,
// as mentioned in the ScopeStack comment.
if (InsideNestedScope && CurrentChange.NewlinesBefore > 0) {
unsigned ScopeStart = ScopeStack.back();
auto ShouldShiftBeAdded = [&] {
// Function declaration
if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
return true;
// Lambda.
if (Changes[ScopeStart - 1].Tok->is(TT_LambdaLBrace))
return false;
// Continued function declaration
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) {
return true;
}
// Continued (template) function call.
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->isOneOf(tok::identifier,
TT_TemplateCloser) &&
Changes[ScopeStart - 1].Tok->is(tok::l_paren) &&
Changes[ScopeStart].Tok->isNot(TT_LambdaLSquare)) {
if (CurrentChange.Tok->MatchingParen &&
CurrentChange.Tok->MatchingParen->is(TT_LambdaLBrace)) {
return false;
}
if (Changes[ScopeStart].NewlinesBefore > 0)
return false;
if (CurrentChange.Tok->is(tok::l_brace) &&
CurrentChange.Tok->is(BK_BracedInit)) {
return true;
}
return Style.BinPackArguments;
}
// Ternary operator
if (CurrentChange.Tok->is(TT_ConditionalExpr))
return true;
// Period Initializer .XXX = 1.
if (CurrentChange.Tok->is(TT_DesignatedInitializerPeriod))
return true;
// Continued ternary operator
if (CurrentChange.Tok->Previous &&
CurrentChange.Tok->Previous->is(TT_ConditionalExpr)) {
return true;
}
// Continued direct-list-initialization using braced list.
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
CurrentChange.Tok->is(tok::l_brace) &&
CurrentChange.Tok->is(BK_BracedInit)) {
return true;
}
// Continued braced list.
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
CurrentChange.Tok->isNot(tok::r_brace)) {
for (unsigned OuterScopeStart : llvm::reverse(ScopeStack)) {
// Lambda.
if (OuterScopeStart > Start &&
Changes[OuterScopeStart - 1].Tok->is(TT_LambdaLBrace)) {
return false;
}
}
if (Changes[ScopeStart].NewlinesBefore > 0)
return false;
return true;
}
// Continued template parameter.
if (Changes[ScopeStart - 1].Tok->is(TT_TemplateOpener))
return true;
return false;
};
if (ShouldShiftBeAdded())
CurrentChange.Spaces += Shift;
}
if (ContinuedStringLiteral)
CurrentChange.Spaces += Shift;
// We should not remove required spaces unless we break the line before.
assert(Shift > 0 || Changes[i].NewlinesBefore > 0 ||
CurrentChange.Spaces >=
static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) ||
CurrentChange.Tok->is(tok::eof));
CurrentChange.StartOfTokenColumn += Shift;
if (i + 1 != Changes.size())
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
// If PointerAlignment is PAS_Right, keep *s or &s next to the token,
// except if the token is equal, then a space is needed.
if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
CurrentChange.Spaces != 0 &&
!CurrentChange.Tok->isOneOf(tok::equal, tok::r_paren,
TT_TemplateCloser)) {
const bool ReferenceNotRightAligned =
Style.ReferenceAlignment != FormatStyle::RAS_Right &&
Style.ReferenceAlignment != FormatStyle::RAS_Pointer;
for (int Previous = i - 1;
Previous >= 0 && Changes[Previous].Tok->is(TT_PointerOrReference);
--Previous) {
assert(Changes[Previous].Tok->isPointerOrReference());
if (Changes[Previous].Tok->isNot(tok::star)) {
if (ReferenceNotRightAligned)
continue;
} else if (Style.PointerAlignment != FormatStyle::PAS_Right) {
continue;
}
Changes[Previous + 1].Spaces -= Shift;
Changes[Previous].Spaces += Shift;
Changes[Previous].StartOfTokenColumn += Shift;
}
}
}
}
// Walk through a subset of the changes, starting at StartAt, and find
// sequences of matching tokens to align. To do so, keep track of the lines and
// whether or not a matching token was found on a line. If a matching token is
// found, extend the current sequence. If the current line cannot be part of a
// sequence, e.g. because there is an empty line before it or it contains only
// non-matching tokens, finalize the previous sequence.
// The value returned is the token on which we stopped, either because we
// exhausted all items inside Changes, or because we hit a scope level higher
// than our initial scope.
// This function is recursive. Each invocation processes only the scope level
// equal to the initial level, which is the level of Changes[StartAt].
// If we encounter a scope level greater than the initial level, then we call
// ourselves recursively, thereby avoiding the pollution of the current state
// with the alignment requirements of the nested sub-level. This recursive
// behavior is necessary for aligning function prototypes that have one or more
// arguments.
// If this function encounters a scope level less than the initial level,
// it returns the current position.
// There is a non-obvious subtlety in the recursive behavior: Even though we
// defer processing of nested levels to recursive invocations of this
// function, when it comes time to align a sequence of tokens, we run the
// alignment on the entire sequence, including the nested levels.
// When doing so, most of the nested tokens are skipped, because their
// alignment was already handled by the recursive invocations of this function.
// However, the special exception is that we do NOT skip function parameters
// that are split across multiple lines. See the test case in FormatTest.cpp
// that mentions "split function parameter alignment" for an example of this.
// When the parameter RightJustify is true, the operator will be
// right-justified. It is used to align compound assignments like `+=` and `=`.
// When RightJustify and ACS.PadOperators are true, operators in each block to
// be aligned will be padded on the left to the same length before aligning.
template <typename F>
static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
SmallVector<WhitespaceManager::Change, 16> &Changes,
unsigned StartAt,
const FormatStyle::AlignConsecutiveStyle &ACS = {},
bool RightJustify = false) {
// We arrange each line in 3 parts. The operator to be aligned (the anchor),
// and text to its left and right. In the aligned text the width of each part
// will be the maximum of that over the block that has been aligned. Maximum
// widths of each part so far. When RightJustify is true and ACS.PadOperators
// is false, the part from start of line to the right end of the anchor.
// Otherwise, only the part to the left of the anchor. Including the space
// that exists on its left from the start. Not including the padding added on
// the left to right-justify the anchor.
unsigned WidthLeft = 0;
// The operator to be aligned when RightJustify is true and ACS.PadOperators
// is false. 0 otherwise.
unsigned WidthAnchor = 0;
// Width to the right of the anchor. Plus width of the anchor when
// RightJustify is false.
unsigned WidthRight = 0;
// Line number of the start and the end of the current token sequence.
unsigned StartOfSequence = 0;
unsigned EndOfSequence = 0;
// Measure the scope level (i.e. depth of (), [], {}) of the first token, and
// abort when we hit any token in a higher scope than the starting one.
auto IndentAndNestingLevel = StartAt < Changes.size()
? Changes[StartAt].indentAndNestingLevel()
: std::tuple<unsigned, unsigned, unsigned>();
// Keep track of the number of commas before the matching tokens, we will only
// align a sequence of matching tokens if they are preceded by the same number
// of commas.
unsigned CommasBeforeLastMatch = 0;
unsigned CommasBeforeMatch = 0;
// Whether a matching token has been found on the current line.
bool FoundMatchOnLine = false;
// Whether the current line consists purely of comments.
bool LineIsComment = true;
// Aligns a sequence of matching tokens, on the MinColumn column.
//
// Sequences start from the first matching token to align, and end at the
// first token of the first line that doesn't need to be aligned.
//
// We need to adjust the StartOfTokenColumn of each Change that is on a line
// containing any matching token to be aligned and located after such token.
auto AlignCurrentSequence = [&] {
if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
AlignTokenSequence(Style, StartOfSequence, EndOfSequence,
WidthLeft + WidthAnchor, RightJustify, Matches,
Changes);
}
WidthLeft = 0;
WidthAnchor = 0;
WidthRight = 0;
StartOfSequence = 0;
EndOfSequence = 0;
};
unsigned i = StartAt;
for (unsigned e = Changes.size(); i != e; ++i) {
auto &CurrentChange = Changes[i];
if (CurrentChange.indentAndNestingLevel() < IndentAndNestingLevel)
break;
if (CurrentChange.NewlinesBefore != 0) {
CommasBeforeMatch = 0;
EndOfSequence = i;
// Whether to break the alignment sequence because of an empty line.
bool EmptyLineBreak =
(CurrentChange.NewlinesBefore > 1) && !ACS.AcrossEmptyLines;
// Whether to break the alignment sequence because of a line without a
// match.
bool NoMatchBreak =
!FoundMatchOnLine && !(LineIsComment && ACS.AcrossComments);
if (EmptyLineBreak || NoMatchBreak)
AlignCurrentSequence();
// A new line starts, re-initialize line status tracking bools.
// Keep the match state if a string literal is continued on this line.
if (i == 0 || CurrentChange.Tok->isNot(tok::string_literal) ||
Changes[i - 1].Tok->isNot(tok::string_literal)) {
FoundMatchOnLine = false;
}
LineIsComment = true;
}
if (CurrentChange.Tok->isNot(tok::comment))
LineIsComment = false;
if (CurrentChange.Tok->is(tok::comma)) {
++CommasBeforeMatch;
} else if (CurrentChange.indentAndNestingLevel() > IndentAndNestingLevel) {
// Call AlignTokens recursively, skipping over this scope block.
unsigned StoppedAt =
AlignTokens(Style, Matches, Changes, i, ACS, RightJustify);
i = StoppedAt - 1;
continue;
}
if (!Matches(CurrentChange))
continue;
// If there is more than one matching token per line, or if the number of
// preceding commas, do not match anymore, end the sequence.
if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
AlignCurrentSequence();
CommasBeforeLastMatch = CommasBeforeMatch;
FoundMatchOnLine = true;
if (StartOfSequence == 0)
StartOfSequence = i;
unsigned ChangeWidthLeft = CurrentChange.StartOfTokenColumn;
unsigned ChangeWidthAnchor = 0;
unsigned ChangeWidthRight = 0;
if (RightJustify)
if (ACS.PadOperators)
ChangeWidthAnchor = CurrentChange.TokenLength;
else
ChangeWidthLeft += CurrentChange.TokenLength;
else
ChangeWidthRight = CurrentChange.TokenLength;
for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
ChangeWidthRight += Changes[j].Spaces;
// Changes are generally 1:1 with the tokens, but a change could also be
// inside of a token, in which case it's counted more than once: once for
// the whitespace surrounding the token (!IsInsideToken) and once for
// each whitespace change within it (IsInsideToken).
// Therefore, changes inside of a token should only count the space.
if (!Changes[j].IsInsideToken)
ChangeWidthRight += Changes[j].TokenLength;
}
// If we are restricted by the maximum column width, end the sequence.
unsigned NewLeft = std::max(ChangeWidthLeft, WidthLeft);
unsigned NewAnchor = std::max(ChangeWidthAnchor, WidthAnchor);
unsigned NewRight = std::max(ChangeWidthRight, WidthRight);
// `ColumnLimit == 0` means there is no column limit.
if (Style.ColumnLimit != 0 &&
Style.ColumnLimit < NewLeft + NewAnchor + NewRight) {
AlignCurrentSequence();
StartOfSequence = i;
WidthLeft = ChangeWidthLeft;
WidthAnchor = ChangeWidthAnchor;
WidthRight = ChangeWidthRight;
} else {
WidthLeft = NewLeft;
WidthAnchor = NewAnchor;
WidthRight = NewRight;
}
}
EndOfSequence = i;
AlignCurrentSequence();
return i;
}
// Aligns a sequence of matching tokens, on the MinColumn column.
//
// Sequences start from the first matching token to align, and end at the
// first token of the first line that doesn't need to be aligned.
//
// We need to adjust the StartOfTokenColumn of each Change that is on a line
// containing any matching token to be aligned and located after such token.
static void AlignMatchingTokenSequence(
unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn,
std::function<bool(const WhitespaceManager::Change &C)> Matches,
SmallVector<WhitespaceManager::Change, 16> &Changes) {
if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
bool FoundMatchOnLine = false;
int Shift = 0;
for (unsigned I = StartOfSequence; I != EndOfSequence; ++I) {
if (Changes[I].NewlinesBefore > 0) {
Shift = 0;
FoundMatchOnLine = false;
}
// If this is the first matching token to be aligned, remember by how many
// spaces it has to be shifted, so the rest of the changes on the line are
// shifted by the same amount.
if (!FoundMatchOnLine && Matches(Changes[I])) {
FoundMatchOnLine = true;
Shift = MinColumn - Changes[I].StartOfTokenColumn;
Changes[I].Spaces += Shift;
}
assert(Shift >= 0);
Changes[I].StartOfTokenColumn += Shift;
if (I + 1 != Changes.size())
Changes[I + 1].PreviousEndOfTokenColumn += Shift;
}
}
MinColumn = 0;
StartOfSequence = 0;
EndOfSequence = 0;
}
void WhitespaceManager::alignConsecutiveMacros() {
if (!Style.AlignConsecutiveMacros.Enabled)
return;
auto AlignMacrosMatches = [](const Change &C) {
const FormatToken *Current = C.Tok;
unsigned SpacesRequiredBefore = 1;
if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
return false;
Current = Current->Previous;
// If token is a ")", skip over the parameter list, to the
// token that precedes the "("
if (Current->is(tok::r_paren) && Current->MatchingParen) {
Current = Current->MatchingParen->Previous;
SpacesRequiredBefore = 0;
}
if (!Current || Current->isNot(tok::identifier))
return false;
if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
return false;
// For a macro function, 0 spaces are required between the
// identifier and the lparen that opens the parameter list.
// For a simple macro, 1 space is required between the
// identifier and the first token of the defined value.
return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
};
unsigned MinColumn = 0;
// Start and end of the token sequence we're processing.
unsigned StartOfSequence = 0;
unsigned EndOfSequence = 0;
// Whether a matching token has been found on the current line.
bool FoundMatchOnLine = false;
// Whether the current line consists only of comments
bool LineIsComment = true;
unsigned I = 0;
for (unsigned E = Changes.size(); I != E; ++I) {
if (Changes[I].NewlinesBefore != 0) {
EndOfSequence = I;
// Whether to break the alignment sequence because of an empty line.
bool EmptyLineBreak = (Changes[I].NewlinesBefore > 1) &&
!Style.AlignConsecutiveMacros.AcrossEmptyLines;
// Whether to break the alignment sequence because of a line without a
// match.
bool NoMatchBreak =
!FoundMatchOnLine &&
!(LineIsComment && Style.AlignConsecutiveMacros.AcrossComments);
if (EmptyLineBreak || NoMatchBreak) {
AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn,
AlignMacrosMatches, Changes);
}
// A new line starts, re-initialize line status tracking bools.
FoundMatchOnLine = false;
LineIsComment = true;
}
if (Changes[I].Tok->isNot(tok::comment))
LineIsComment = false;
if (!AlignMacrosMatches(Changes[I]))
continue;
FoundMatchOnLine = true;
if (StartOfSequence == 0)
StartOfSequence = I;
unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
MinColumn = std::max(MinColumn, ChangeMinColumn);
}
EndOfSequence = I;
AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn,
AlignMacrosMatches, Changes);
}
void WhitespaceManager::alignConsecutiveAssignments() {
if (!Style.AlignConsecutiveAssignments.Enabled)
return;
AlignTokens(
Style,
[&](const Change &C) {
// Do not align on equal signs that are first on a line.
if (C.NewlinesBefore > 0)
return false;
// Do not align on equal signs that are last on a line.
if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
return false;
// Do not align operator= overloads.
FormatToken *Previous = C.Tok->getPreviousNonComment();
if (Previous && Previous->is(tok::kw_operator))
return false;
return Style.AlignConsecutiveAssignments.AlignCompound
? C.Tok->getPrecedence() == prec::Assignment
: (C.Tok->is(tok::equal) ||
// In Verilog the '<=' is not a compound assignment, thus
// it is aligned even when the AlignCompound option is not
// set.
(Style.isVerilog() && C.Tok->is(tok::lessequal) &&
C.Tok->getPrecedence() == prec::Assignment));
},
Changes, /*StartAt=*/0, Style.AlignConsecutiveAssignments,
/*RightJustify=*/true);
}
void WhitespaceManager::alignConsecutiveBitFields() {
alignConsecutiveColons(Style.AlignConsecutiveBitFields, TT_BitFieldColon);
}
void WhitespaceManager::alignConsecutiveColons(
const FormatStyle::AlignConsecutiveStyle &AlignStyle, TokenType Type) {
if (!AlignStyle.Enabled)
return;
AlignTokens(
Style,
[&](Change const &C) {
// Do not align on ':' that is first on a line.
if (C.NewlinesBefore > 0)
return false;
// Do not align on ':' that is last on a line.
if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
return false;
return C.Tok->is(Type);
},
Changes, /*StartAt=*/0, AlignStyle);
}
void WhitespaceManager::alignConsecutiveShortCaseStatements(bool IsExpr) {
if (!Style.AlignConsecutiveShortCaseStatements.Enabled ||
!(IsExpr ? Style.AllowShortCaseExpressionOnASingleLine
: Style.AllowShortCaseLabelsOnASingleLine)) {
return;
}
const auto Type = IsExpr ? TT_CaseLabelArrow : TT_CaseLabelColon;
const auto &Option = Style.AlignConsecutiveShortCaseStatements;
const bool AlignArrowOrColon =
IsExpr ? Option.AlignCaseArrows : Option.AlignCaseColons;
auto Matches = [&](const Change &C) {
if (AlignArrowOrColon)
return C.Tok->is(Type);
// Ignore 'IsInsideToken' to allow matching trailing comments which
// need to be reflowed as that causes the token to appear in two
// different changes, which will cause incorrect alignment as we'll
// reflow early due to detecting multiple aligning tokens per line.
return !C.IsInsideToken && C.Tok->Previous && C.Tok->Previous->is(Type);
};
unsigned MinColumn = 0;
// Empty case statements don't break the alignment, but don't necessarily
// match our predicate, so we need to track their column so they can push out
// our alignment.
unsigned MinEmptyCaseColumn = 0;
// Start and end of the token sequence we're processing.
unsigned StartOfSequence = 0;
unsigned EndOfSequence = 0;
// Whether a matching token has been found on the current line.
bool FoundMatchOnLine = false;
bool LineIsComment = true;
bool LineIsEmptyCase = false;
unsigned I = 0;
for (unsigned E = Changes.size(); I != E; ++I) {
if (Changes[I].NewlinesBefore != 0) {
// Whether to break the alignment sequence because of an empty line.
bool EmptyLineBreak =
(Changes[I].NewlinesBefore > 1) &&
!Style.AlignConsecutiveShortCaseStatements.AcrossEmptyLines;
// Whether to break the alignment sequence because of a line without a
// match.
bool NoMatchBreak =
!FoundMatchOnLine &&
!(LineIsComment &&
Style.AlignConsecutiveShortCaseStatements.AcrossComments) &&
!LineIsEmptyCase;
if (EmptyLineBreak || NoMatchBreak) {
AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn,
Matches, Changes);
MinEmptyCaseColumn = 0;
}
// A new line starts, re-initialize line status tracking bools.
FoundMatchOnLine = false;
LineIsComment = true;
LineIsEmptyCase = false;
}
if (Changes[I].Tok->isNot(tok::comment))
LineIsComment = false;
if (Changes[I].Tok->is(Type)) {
LineIsEmptyCase =
!Changes[I].Tok->Next || Changes[I].Tok->Next->isTrailingComment();
if (LineIsEmptyCase) {
if (Style.AlignConsecutiveShortCaseStatements.AlignCaseColons) {
MinEmptyCaseColumn =
std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn);
} else {
MinEmptyCaseColumn =
std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn + 2);
}
}
}
if (!Matches(Changes[I]))
continue;
if (LineIsEmptyCase)
continue;
FoundMatchOnLine = true;
if (StartOfSequence == 0)
StartOfSequence = I;
EndOfSequence = I + 1;
MinColumn = std::max(MinColumn, Changes[I].StartOfTokenColumn);
// Allow empty case statements to push out our alignment.
MinColumn = std::max(MinColumn, MinEmptyCaseColumn);
}
AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
Changes);
}
void WhitespaceManager::alignConsecutiveTableGenBreakingDAGArgColons() {
alignConsecutiveColons(Style.AlignConsecutiveTableGenBreakingDAGArgColons,
TT_TableGenDAGArgListColonToAlign);
}
void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
TT_TableGenCondOperatorColon);
}
void WhitespaceManager::alignConsecutiveTableGenDefinitions() {
alignConsecutiveColons(Style.AlignConsecutiveTableGenDefinitionColons,
TT_InheritanceColon);
}
void WhitespaceManager::alignConsecutiveDeclarations() {
if (!Style.AlignConsecutiveDeclarations.Enabled)
return;
AlignTokens(
Style,
[&](Change const &C) {
if (Style.AlignConsecutiveDeclarations.AlignFunctionPointers) {
for (const auto *Prev = C.Tok->Previous; Prev; Prev = Prev->Previous)
if (Prev->is(tok::equal))
return false;
if (C.Tok->is(TT_FunctionTypeLParen))
return true;
}
if (C.Tok->is(TT_FunctionDeclarationName))
return true;
if (C.Tok->isNot(TT_StartOfName))
return false;
if (C.Tok->Previous &&
C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
return false;
// Check if there is a subsequent name that starts the same declaration.
for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
if (Next->is(tok::comment))
continue;
if (Next->is(TT_PointerOrReference))
return false;
if (!Next->Tok.getIdentifierInfo())
break;
if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
tok::kw_operator)) {
return false;
}
}
return true;
},
Changes, /*StartAt=*/0, Style.AlignConsecutiveDeclarations);
}
void WhitespaceManager::alignChainedConditionals() {
if (Style.BreakBeforeTernaryOperators) {
AlignTokens(
Style,
[](Change const &C) {
// Align question operators and last colon
return C.Tok->is(TT_ConditionalExpr) &&
((C.Tok->is(tok::question) && !C.NewlinesBefore) ||
(C.Tok->is(tok::colon) && C.Tok->Next &&
(C.Tok->Next->FakeLParens.size() == 0 ||
C.Tok->Next->FakeLParens.back() != prec::Conditional)));
},
Changes, /*StartAt=*/0);
} else {
static auto AlignWrappedOperand = [](Change const &C) {
FormatToken *Previous = C.Tok->getPreviousNonComment();
return C.NewlinesBefore && Previous && Previous->is(TT_ConditionalExpr) &&
(Previous->is(tok::colon) &&
(C.Tok->FakeLParens.size() == 0 ||
C.Tok->FakeLParens.back() != prec::Conditional));
};
// Ensure we keep alignment of wrapped operands with non-wrapped operands
// Since we actually align the operators, the wrapped operands need the
// extra offset to be properly aligned.
for (Change &C : Changes)
if (AlignWrappedOperand(C))
C.StartOfTokenColumn -= 2;
AlignTokens(
Style,
[this](Change const &C) {
// Align question operators if next operand is not wrapped, as
// well as wrapped operands after question operator or last
// colon in conditional sequence
return (C.Tok->is(TT_ConditionalExpr) && C.Tok->is(tok::question) &&
&C != &Changes.back() && (&C + 1)->NewlinesBefore == 0 &&
!(&C + 1)->IsTrailingComment) ||
AlignWrappedOperand(C);
},
Changes, /*StartAt=*/0);
}
}
void WhitespaceManager::alignTrailingComments() {
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Never)
return;
const int Size = Changes.size();
int MinColumn = 0;
int StartOfSequence = 0;
bool BreakBeforeNext = false;
int NewLineThreshold = 1;
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Always)
NewLineThreshold = Style.AlignTrailingComments.OverEmptyLines + 1;
for (int I = 0, MaxColumn = INT_MAX, Newlines = 0; I < Size; ++I) {
auto &C = Changes[I];
if (C.StartOfBlockComment)
continue;
Newlines += C.NewlinesBefore;
if (!C.IsTrailingComment)
continue;
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
const int OriginalSpaces =
C.OriginalWhitespaceRange.getEnd().getRawEncoding() -
C.OriginalWhitespaceRange.getBegin().getRawEncoding() -
C.Tok->LastNewlineOffset;
assert(OriginalSpaces >= 0);
const auto RestoredLineLength =
C.StartOfTokenColumn + C.TokenLength + OriginalSpaces;
// If leaving comments makes the line exceed the column limit, give up to
// leave the comments.
if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit > 0)
break;
C.Spaces = C.NewlinesBefore > 0 ? C.Tok->OriginalColumn : OriginalSpaces;
continue;
}
const int ChangeMinColumn = C.StartOfTokenColumn;
int ChangeMaxColumn;
// If we don't create a replacement for this change, we have to consider
// it to be immovable.
if (!C.CreateReplacement)
ChangeMaxColumn = ChangeMinColumn;
else if (Style.ColumnLimit == 0)
ChangeMaxColumn = INT_MAX;
else if (Style.ColumnLimit >= C.TokenLength)
ChangeMaxColumn = Style.ColumnLimit - C.TokenLength;
else
ChangeMaxColumn = ChangeMinColumn;
if (I + 1 < Size && Changes[I + 1].ContinuesPPDirective &&
ChangeMaxColumn >= 2) {
ChangeMaxColumn -= 2;
}
bool WasAlignedWithStartOfNextLine = false;
if (C.NewlinesBefore >= 1) { // A comment on its own line.
const auto CommentColumn =
SourceMgr.getSpellingColumnNumber(C.OriginalWhitespaceRange.getEnd());
for (int J = I + 1; J < Size; ++J) {
if (Changes[J].Tok->is(tok::comment))
continue;
const auto NextColumn = SourceMgr.getSpellingColumnNumber(
Changes[J].OriginalWhitespaceRange.getEnd());
// The start of the next token was previously aligned with the
// start of this comment.
WasAlignedWithStartOfNextLine =
CommentColumn == NextColumn ||
CommentColumn == NextColumn + Style.IndentWidth;
break;
}
}
// We don't want to align comments which end a scope, which are here
// identified by most closing braces.
auto DontAlignThisComment = [](const auto *Tok) {
if (Tok->is(tok::semi)) {
Tok = Tok->getPreviousNonComment();
if (!Tok)
return false;
}
if (Tok->is(tok::r_paren)) {
// Back up past the parentheses and a `TT_DoWhile` that may precede.
Tok = Tok->MatchingParen;
if (!Tok)
return false;
Tok = Tok->getPreviousNonComment();
if (!Tok)
return false;
if (Tok->is(TT_DoWhile)) {
const auto *Prev = Tok->getPreviousNonComment();
if (!Prev) {
// A do-while-loop without braces.
return true;
}
Tok = Prev;
}
}
if (Tok->isNot(tok::r_brace))
return false;
while (Tok->Previous && Tok->Previous->is(tok::r_brace))
Tok = Tok->Previous;
return Tok->NewlinesBefore > 0;
};
if (I > 0 && C.NewlinesBefore == 0 &&
DontAlignThisComment(Changes[I - 1].Tok)) {
alignTrailingComments(StartOfSequence, I, MinColumn);
// Reset to initial values, but skip this change for the next alignment
// pass.
MinColumn = 0;
MaxColumn = INT_MAX;
StartOfSequence = I + 1;
} else if (BreakBeforeNext || Newlines > NewLineThreshold ||
(ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
// Break the comment sequence if the previous line did not end
// in a trailing comment.
(C.NewlinesBefore == 1 && I > 0 &&
!Changes[I - 1].IsTrailingComment) ||
WasAlignedWithStartOfNextLine) {
alignTrailingComments(StartOfSequence, I, MinColumn);
MinColumn = ChangeMinColumn;
MaxColumn = ChangeMaxColumn;
StartOfSequence = I;
} else {
MinColumn = std::max(MinColumn, ChangeMinColumn);
MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
}
BreakBeforeNext = (I == 0) || (C.NewlinesBefore > 1) ||
// Never start a sequence with a comment at the beginning
// of the line.
(C.NewlinesBefore == 1 && StartOfSequence == I);
Newlines = 0;
}
alignTrailingComments(StartOfSequence, Size, MinColumn);
}
void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
unsigned Column) {
for (unsigned i = Start; i != End; ++i) {
int Shift = 0;
if (Changes[i].IsTrailingComment)
Shift = Column - Changes[i].StartOfTokenColumn;
if (Changes[i].StartOfBlockComment) {
Shift = Changes[i].IndentationOffset +
Changes[i].StartOfBlockComment->StartOfTokenColumn -
Changes[i].StartOfTokenColumn;
}
if (Shift <= 0)
continue;
Changes[i].Spaces += Shift;
if (i + 1 != Changes.size())
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
Changes[i].StartOfTokenColumn += Shift;
}
}
void WhitespaceManager::alignEscapedNewlines() {
const auto Align = Style.AlignEscapedNewlines;
if (Align == FormatStyle::ENAS_DontAlign)
return;
const bool WithLastLine = Align == FormatStyle::ENAS_LeftWithLastLine;
const bool AlignLeft = Align == FormatStyle::ENAS_Left || WithLastLine;
const auto MaxColumn = Style.ColumnLimit;
unsigned MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
unsigned StartOfMacro = 0;
for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
Change &C = Changes[i];
if (C.NewlinesBefore == 0 && (!WithLastLine || C.Tok->isNot(tok::eof)))
continue;
const bool InPPDirective = C.ContinuesPPDirective;
const auto BackslashColumn = C.PreviousEndOfTokenColumn + 2;
if (InPPDirective ||
(WithLastLine && (MaxColumn == 0 || BackslashColumn <= MaxColumn))) {
MaxEndOfLine = std::max(BackslashColumn, MaxEndOfLine);
}
if (!InPPDirective) {
alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
StartOfMacro = i;
}
}
alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
}
void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
unsigned Column) {
for (unsigned i = Start; i < End; ++i) {
Change &C = Changes[i];
if (C.NewlinesBefore > 0) {
assert(C.ContinuesPPDirective);
if (C.PreviousEndOfTokenColumn + 1 > Column)
C.EscapedNewlineColumn = 0;
else
C.EscapedNewlineColumn = Column;
}
}
}
void WhitespaceManager::alignArrayInitializers() {
if (Style.AlignArrayOfStructures == FormatStyle::AIAS_None)
return;
for (unsigned ChangeIndex = 1U, ChangeEnd = Changes.size();
ChangeIndex < ChangeEnd; ++ChangeIndex) {
auto &C = Changes[ChangeIndex];
if (C.Tok->IsArrayInitializer) {
bool FoundComplete = false;
for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
++InsideIndex) {
if (Changes[InsideIndex].Tok == C.Tok->MatchingParen) {
alignArrayInitializers(ChangeIndex, InsideIndex + 1);
ChangeIndex = InsideIndex + 1;
FoundComplete = true;
break;
}
}
if (!FoundComplete)
ChangeIndex = ChangeEnd;
}
}
}
void WhitespaceManager::alignArrayInitializers(unsigned Start, unsigned End) {
if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Right)
alignArrayInitializersRightJustified(getCells(Start, End));
else if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Left)
alignArrayInitializersLeftJustified(getCells(Start, End));
}
void WhitespaceManager::alignArrayInitializersRightJustified(
CellDescriptions &&CellDescs) {
if (!CellDescs.isRectangular())
return;
const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
auto &Cells = CellDescs.Cells;
// Now go through and fixup the spaces.
auto *CellIter = Cells.begin();
for (auto i = 0U; i < CellDescs.CellCounts[0]; ++i, ++CellIter) {
unsigned NetWidth = 0U;
if (isSplitCell(*CellIter))
NetWidth = getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
auto CellWidth = getMaximumCellWidth(CellIter, NetWidth);
if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
// So in here we want to see if there is a brace that falls
// on a line that was split. If so on that line we make sure that
// the spaces in front of the brace are enough.
const auto *Next = CellIter;
do {
const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
if (Previous && Previous->isNot(TT_LineComment)) {
Changes[Next->Index].Spaces = BracePadding;
Changes[Next->Index].NewlinesBefore = 0;
}
Next = Next->NextColumnElement;
} while (Next);
// Unless the array is empty, we need the position of all the
// immediately adjacent cells
if (CellIter != Cells.begin()) {
auto ThisNetWidth =
getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
auto MaxNetWidth = getMaximumNetWidth(
Cells.begin(), CellIter, CellDescs.InitialSpaces,
CellDescs.CellCounts[0], CellDescs.CellCounts.size());
if (ThisNetWidth < MaxNetWidth)
Changes[CellIter->Index].Spaces = (MaxNetWidth - ThisNetWidth);
auto RowCount = 1U;
auto Offset = std::distance(Cells.begin(), CellIter);
for (const auto *Next = CellIter->NextColumnElement; Next;
Next = Next->NextColumnElement) {
if (RowCount >= CellDescs.CellCounts.size())
break;
auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
auto *End = Start + Offset;
ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
if (ThisNetWidth < MaxNetWidth)
Changes[Next->Index].Spaces = (MaxNetWidth - ThisNetWidth);
++RowCount;
}
}
} else {
auto ThisWidth =
calculateCellWidth(CellIter->Index, CellIter->EndIndex, true) +
NetWidth;
if (Changes[CellIter->Index].NewlinesBefore == 0) {
Changes[CellIter->Index].Spaces = (CellWidth - (ThisWidth + NetWidth));
Changes[CellIter->Index].Spaces += (i > 0) ? 1 : BracePadding;
}
alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
for (const auto *Next = CellIter->NextColumnElement; Next;
Next = Next->NextColumnElement) {
ThisWidth =
calculateCellWidth(Next->Index, Next->EndIndex, true) + NetWidth;
if (Changes[Next->Index].NewlinesBefore == 0) {
Changes[Next->Index].Spaces = (CellWidth - ThisWidth);
Changes[Next->Index].Spaces += (i > 0) ? 1 : BracePadding;
}
alignToStartOfCell(Next->Index, Next->EndIndex);
}
}
}
}
void WhitespaceManager::alignArrayInitializersLeftJustified(
CellDescriptions &&CellDescs) {
if (!CellDescs.isRectangular())
return;
const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
auto &Cells = CellDescs.Cells;
// Now go through and fixup the spaces.
auto *CellIter = Cells.begin();
// The first cell of every row needs to be against the left brace.
for (const auto *Next = CellIter; Next; Next = Next->NextColumnElement) {
auto &Change = Changes[Next->Index];
Change.Spaces =
Change.NewlinesBefore == 0 ? BracePadding : CellDescs.InitialSpaces;
}
++CellIter;
for (auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
auto MaxNetWidth = getMaximumNetWidth(
Cells.begin(), CellIter, CellDescs.InitialSpaces,
CellDescs.CellCounts[0], CellDescs.CellCounts.size());
auto ThisNetWidth =
getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
if (Changes[CellIter->Index].NewlinesBefore == 0) {
Changes[CellIter->Index].Spaces =
MaxNetWidth - ThisNetWidth +
(Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1
: BracePadding);
}
auto RowCount = 1U;
auto Offset = std::distance(Cells.begin(), CellIter);
for (const auto *Next = CellIter->NextColumnElement; Next;
Next = Next->NextColumnElement) {
if (RowCount >= CellDescs.CellCounts.size())
break;
auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
auto *End = Start + Offset;
auto ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
if (Changes[Next->Index].NewlinesBefore == 0) {
Changes[Next->Index].Spaces =
MaxNetWidth - ThisNetWidth +
(Changes[Next->Index].Tok->isNot(tok::r_brace) ? 1 : BracePadding);
}
++RowCount;
}
}
}
bool WhitespaceManager::isSplitCell(const CellDescription &Cell) {
if (Cell.HasSplit)
return true;
for (const auto *Next = Cell.NextColumnElement; Next;
Next = Next->NextColumnElement) {
if (Next->HasSplit)
return true;
}
return false;
}
WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
unsigned End) {
unsigned Depth = 0;
unsigned Cell = 0;
SmallVector<unsigned> CellCounts;
unsigned InitialSpaces = 0;
unsigned InitialTokenLength = 0;
unsigned EndSpaces = 0;
SmallVector<CellDescription> Cells;
const FormatToken *MatchingParen = nullptr;
for (unsigned i = Start; i < End; ++i) {
auto &C = Changes[i];
if (C.Tok->is(tok::l_brace))
++Depth;
else if (C.Tok->is(tok::r_brace))
--Depth;
if (Depth == 2) {
if (C.Tok->is(tok::l_brace)) {
Cell = 0;
MatchingParen = C.Tok->MatchingParen;
if (InitialSpaces == 0) {
InitialSpaces = C.Spaces + C.TokenLength;
InitialTokenLength = C.TokenLength;
auto j = i - 1;
for (; Changes[j].NewlinesBefore == 0 && j > Start; --j) {
InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
InitialTokenLength += Changes[j].TokenLength;
}
if (C.NewlinesBefore == 0) {
InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
InitialTokenLength += Changes[j].TokenLength;
}
}
} else if (C.Tok->is(tok::comma)) {
if (!Cells.empty())
Cells.back().EndIndex = i;
if (const auto *Next = C.Tok->getNextNonComment();
Next && Next->isNot(tok::r_brace)) { // dangling comma
++Cell;
}
}
} else if (Depth == 1) {
if (C.Tok == MatchingParen) {
if (!Cells.empty())
Cells.back().EndIndex = i;
Cells.push_back(CellDescription{i, ++Cell, i + 1, false, nullptr});
CellCounts.push_back(C.Tok->Previous->isNot(tok::comma) ? Cell + 1
: Cell);
// Go to the next non-comment and ensure there is a break in front
const auto *NextNonComment = C.Tok->getNextNonComment();
while (NextNonComment && NextNonComment->is(tok::comma))
NextNonComment = NextNonComment->getNextNonComment();
auto j = i;
while (j < End && Changes[j].Tok != NextNonComment)
++j;
if (j < End && Changes[j].NewlinesBefore == 0 &&
Changes[j].Tok->isNot(tok::r_brace)) {
Changes[j].NewlinesBefore = 1;
// Account for the added token lengths
Changes[j].Spaces = InitialSpaces - InitialTokenLength;
}
} else if (C.Tok->is(tok::comment) && C.Tok->NewlinesBefore == 0) {
// Trailing comments stay at a space past the last token
C.Spaces = Changes[i - 1].Tok->is(tok::comma) ? 1 : 2;
} else if (C.Tok->is(tok::l_brace)) {
// We need to make sure that the ending braces is aligned to the
// start of our initializer
auto j = i - 1;
for (; j > 0 && !Changes[j].Tok->ArrayInitializerLineStart; --j)
; // Nothing the loop does the work
EndSpaces = Changes[j].Spaces;
}
} else if (Depth == 0 && C.Tok->is(tok::r_brace)) {
C.NewlinesBefore = 1;
C.Spaces = EndSpaces;
}
if (C.Tok->StartsColumn) {
// This gets us past tokens that have been split over multiple
// lines
bool HasSplit = false;
if (Changes[i].NewlinesBefore > 0) {
// So if we split a line previously and the tail line + this token is
// less then the column limit we remove the split here and just put
// the column start at a space past the comma
//
// FIXME This if branch covers the cases where the column is not
// the first column. This leads to weird pathologies like the formatting
// auto foo = Items{
// Section{
// 0, bar(),
// }
// };
// Well if it doesn't lead to that it's indicative that the line
// breaking should be revisited. Unfortunately alot of other options
// interact with this
auto j = i - 1;
if ((j - 1) > Start && Changes[j].Tok->is(tok::comma) &&
Changes[j - 1].NewlinesBefore > 0) {
--j;
auto LineLimit = Changes[j].Spaces + Changes[j].TokenLength;
if (LineLimit < Style.ColumnLimit) {
Changes[i].NewlinesBefore = 0;
Changes[i].Spaces = 1;
}
}
}
while (Changes[i].NewlinesBefore > 0 && Changes[i].Tok == C.Tok) {
Changes[i].Spaces = InitialSpaces;
++i;
HasSplit = true;
}
if (Changes[i].Tok != C.Tok)
--i;
Cells.push_back(CellDescription{i, Cell, i, HasSplit, nullptr});
}
}
return linkCells({Cells, CellCounts, InitialSpaces});
}
unsigned WhitespaceManager::calculateCellWidth(unsigned Start, unsigned End,
bool WithSpaces) const {
unsigned CellWidth = 0;
for (auto i = Start; i < End; i++) {
if (Changes[i].NewlinesBefore > 0)
CellWidth = 0;
CellWidth += Changes[i].TokenLength;
CellWidth += (WithSpaces ? Changes[i].Spaces : 0);
}
return CellWidth;
}
void WhitespaceManager::alignToStartOfCell(unsigned Start, unsigned End) {
if ((End - Start) <= 1)
return;
// If the line is broken anywhere in there make sure everything
// is aligned to the parent
for (auto i = Start + 1; i < End; i++)
if (Changes[i].NewlinesBefore > 0)
Changes[i].Spaces = Changes[Start].Spaces;
}
WhitespaceManager::CellDescriptions
WhitespaceManager::linkCells(CellDescriptions &&CellDesc) {
auto &Cells = CellDesc.Cells;
for (auto *CellIter = Cells.begin(); CellIter != Cells.end(); ++CellIter) {
if (!CellIter->NextColumnElement && (CellIter + 1) != Cells.end()) {
for (auto *NextIter = CellIter + 1; NextIter != Cells.end(); ++NextIter) {
if (NextIter->Cell == CellIter->Cell) {
CellIter->NextColumnElement = &(*NextIter);
break;
}
}
}
}
return std::move(CellDesc);
}
void WhitespaceManager::generateChanges() {
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
const Change &C = Changes[i];
if (i > 0) {
auto Last = Changes[i - 1].OriginalWhitespaceRange;
auto New = Changes[i].OriginalWhitespaceRange;
// Do not generate two replacements for the same location. As a special
// case, it is allowed if there is a replacement for the empty range
// between 2 tokens and another non-empty range at the start of the second
// token. We didn't implement logic to combine replacements for 2
// consecutive source ranges into a single replacement, because the
// program works fine without it.
//
// We can't eliminate empty original whitespace ranges. They appear when
// 2 tokens have no whitespace in between in the input. It does not
// matter whether whitespace is to be added. If no whitespace is to be
// added, the replacement will be empty, and it gets eliminated after this
// step in storeReplacement. For example, if the input is `foo();`,
// there will be a replacement for the range between every consecutive
// pair of tokens.
//
// A replacement at the start of a token can be added by
// BreakableStringLiteralUsingOperators::insertBreak when it adds braces
// around the string literal. Say Verilog code is being formatted and the
// first line is to become the next 2 lines.
// x("long string");
// x({"long ",
// "string"});
// There will be a replacement for the empty range between the parenthesis
// and the string and another replacement for the quote character. The
// replacement for the empty range between the parenthesis and the quote
// comes from ContinuationIndenter::addTokenOnCurrentLine when it changes
// the original empty range between the parenthesis and the string to
// another empty one. The replacement for the quote character comes from
// BreakableStringLiteralUsingOperators::insertBreak when it adds the
// brace. In the example, the replacement for the empty range is the same
// as the original text. However, eliminating replacements that are same
// as the original does not help in general. For example, a newline can
// be inserted, causing the first line to become the next 3 lines.
// xxxxxxxxxxx("long string");
// xxxxxxxxxxx(
// {"long ",
// "string"});
// In that case, the empty range between the parenthesis and the string
// will be replaced by a newline and 4 spaces. So we will still have to
// deal with a replacement for an empty source range followed by a
// replacement for a non-empty source range.
if (Last.getBegin() == New.getBegin() &&
(Last.getEnd() != Last.getBegin() ||
New.getEnd() == New.getBegin())) {
continue;
}
}
if (C.CreateReplacement) {
std::string ReplacementText = C.PreviousLinePostfix;
if (C.ContinuesPPDirective) {
appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
C.PreviousEndOfTokenColumn,
C.EscapedNewlineColumn);
} else {
appendNewlineText(ReplacementText, C.NewlinesBefore);
}
// FIXME: This assert should hold if we computed the column correctly.
// assert((int)C.StartOfTokenColumn >= C.Spaces);
appendIndentText(
ReplacementText, C.Tok->IndentLevel, std::max(0, C.Spaces),
std::max((int)C.StartOfTokenColumn, C.Spaces) - std::max(0, C.Spaces),
C.IsAligned);
ReplacementText.append(C.CurrentLinePrefix);
storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
}
}
}
void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
SourceMgr.getFileOffset(Range.getBegin());
// Don't create a replacement, if it does not change anything.
if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
WhitespaceLength) == Text) {
return;
}
auto Err = Replaces.add(tooling::Replacement(
SourceMgr, CharSourceRange::getCharRange(Range), Text));
// FIXME: better error handling. For now, just print an error message in the
// release version.
if (Err) {
llvm::errs() << llvm::toString(std::move(Err)) << "\n";
assert(false);
}
}
void WhitespaceManager::appendNewlineText(std::string &Text,
unsigned Newlines) {
if (UseCRLF) {
Text.reserve(Text.size() + 2 * Newlines);
for (unsigned i = 0; i < Newlines; ++i)
Text.append("\r\n");
} else {
Text.append(Newlines, '\n');
}
}
void WhitespaceManager::appendEscapedNewlineText(
std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
unsigned EscapedNewlineColumn) {
if (Newlines > 0) {
unsigned Spaces =
std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
for (unsigned i = 0; i < Newlines; ++i) {
Text.append(Spaces, ' ');
Text.append(UseCRLF ? "\\\r\n" : "\\\n");
Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
}
}
}
void WhitespaceManager::appendIndentText(std::string &Text,
unsigned IndentLevel, unsigned Spaces,
unsigned WhitespaceStartColumn,
bool IsAligned) {
switch (Style.UseTab) {
case FormatStyle::UT_Never:
Text.append(Spaces, ' ');
break;
case FormatStyle::UT_Always: {
if (Style.TabWidth) {
unsigned FirstTabWidth =
Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
// Insert only spaces when we want to end up before the next tab.
if (Spaces < FirstTabWidth || Spaces == 1) {
Text.append(Spaces, ' ');
break;
}
// Align to the next tab.
Spaces -= FirstTabWidth;
Text.append("\t");
Text.append(Spaces / Style.TabWidth, '\t');
Text.append(Spaces % Style.TabWidth, ' ');
} else if (Spaces == 1) {
Text.append(Spaces, ' ');
}
break;
}
case FormatStyle::UT_ForIndentation:
if (WhitespaceStartColumn == 0) {
unsigned Indentation = IndentLevel * Style.IndentWidth;
Spaces = appendTabIndent(Text, Spaces, Indentation);
}
Text.append(Spaces, ' ');
break;
case FormatStyle::UT_ForContinuationAndIndentation:
if (WhitespaceStartColumn == 0)
Spaces = appendTabIndent(Text, Spaces, Spaces);
Text.append(Spaces, ' ');
break;
case FormatStyle::UT_AlignWithSpaces:
if (WhitespaceStartColumn == 0) {
unsigned Indentation =
IsAligned ? IndentLevel * Style.IndentWidth : Spaces;
Spaces = appendTabIndent(Text, Spaces, Indentation);
}
Text.append(Spaces, ' ');
break;
}
}
unsigned WhitespaceManager::appendTabIndent(std::string &Text, unsigned Spaces,
unsigned Indentation) {
// This happens, e.g. when a line in a block comment is indented less than the
// first one.
if (Indentation > Spaces)
Indentation = Spaces;
if (Style.TabWidth) {
unsigned Tabs = Indentation / Style.TabWidth;
Text.append(Tabs, '\t');
Spaces -= Tabs * Style.TabWidth;
}
return Spaces;
}
} // namespace format
} // namespace clang
|