WhitespaceManager.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. //===--- WhitespaceManager.cpp - Format C++ code --------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file implements WhitespaceManager class.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "WhitespaceManager.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include <algorithm>
  17. namespace clang {
  18. namespace format {
  19. bool WhitespaceManager::Change::IsBeforeInFile::operator()(
  20. const Change &C1, const Change &C2) const {
  21. return SourceMgr.isBeforeInTranslationUnit(
  22. C1.OriginalWhitespaceRange.getBegin(),
  23. C2.OriginalWhitespaceRange.getBegin());
  24. }
  25. WhitespaceManager::Change::Change(const FormatToken &Tok,
  26. bool CreateReplacement,
  27. SourceRange OriginalWhitespaceRange,
  28. int Spaces, unsigned StartOfTokenColumn,
  29. unsigned NewlinesBefore,
  30. StringRef PreviousLinePostfix,
  31. StringRef CurrentLinePrefix, bool IsAligned,
  32. bool ContinuesPPDirective, bool IsInsideToken)
  33. : Tok(&Tok), CreateReplacement(CreateReplacement),
  34. OriginalWhitespaceRange(OriginalWhitespaceRange),
  35. StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
  36. PreviousLinePostfix(PreviousLinePostfix),
  37. CurrentLinePrefix(CurrentLinePrefix), IsAligned(IsAligned),
  38. ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces),
  39. IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0),
  40. PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
  41. StartOfBlockComment(nullptr), IndentationOffset(0), ConditionalsLevel(0) {
  42. }
  43. void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
  44. unsigned Spaces,
  45. unsigned StartOfTokenColumn,
  46. bool IsAligned, bool InPPDirective) {
  47. if (Tok.Finalized)
  48. return;
  49. Tok.setDecision((Newlines > 0) ? FD_Break : FD_Continue);
  50. Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
  51. Spaces, StartOfTokenColumn, Newlines, "", "",
  52. IsAligned, InPPDirective && !Tok.IsFirst,
  53. /*IsInsideToken=*/false));
  54. }
  55. void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
  56. bool InPPDirective) {
  57. if (Tok.Finalized)
  58. return;
  59. Changes.push_back(Change(Tok, /*CreateReplacement=*/false,
  60. Tok.WhitespaceRange, /*Spaces=*/0,
  61. Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
  62. /*IsAligned=*/false, InPPDirective && !Tok.IsFirst,
  63. /*IsInsideToken=*/false));
  64. }
  65. llvm::Error
  66. WhitespaceManager::addReplacement(const tooling::Replacement &Replacement) {
  67. return Replaces.add(Replacement);
  68. }
  69. bool WhitespaceManager::inputUsesCRLF(StringRef Text, bool DefaultToCRLF) {
  70. size_t LF = Text.count('\n');
  71. size_t CR = Text.count('\r') * 2;
  72. return LF == CR ? DefaultToCRLF : CR > LF;
  73. }
  74. void WhitespaceManager::replaceWhitespaceInToken(
  75. const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
  76. StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
  77. unsigned Newlines, int Spaces) {
  78. if (Tok.Finalized)
  79. return;
  80. SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
  81. Changes.push_back(
  82. Change(Tok, /*CreateReplacement=*/true,
  83. SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
  84. std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
  85. /*IsAligned=*/true, InPPDirective && !Tok.IsFirst,
  86. /*IsInsideToken=*/true));
  87. }
  88. const tooling::Replacements &WhitespaceManager::generateReplacements() {
  89. if (Changes.empty())
  90. return Replaces;
  91. llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr));
  92. calculateLineBreakInformation();
  93. alignConsecutiveMacros();
  94. alignConsecutiveDeclarations();
  95. alignConsecutiveBitFields();
  96. alignConsecutiveAssignments();
  97. alignChainedConditionals();
  98. alignTrailingComments();
  99. alignEscapedNewlines();
  100. alignArrayInitializers();
  101. generateChanges();
  102. return Replaces;
  103. }
  104. void WhitespaceManager::calculateLineBreakInformation() {
  105. Changes[0].PreviousEndOfTokenColumn = 0;
  106. Change *LastOutsideTokenChange = &Changes[0];
  107. for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
  108. SourceLocation OriginalWhitespaceStart =
  109. Changes[i].OriginalWhitespaceRange.getBegin();
  110. SourceLocation PreviousOriginalWhitespaceEnd =
  111. Changes[i - 1].OriginalWhitespaceRange.getEnd();
  112. unsigned OriginalWhitespaceStartOffset =
  113. SourceMgr.getFileOffset(OriginalWhitespaceStart);
  114. unsigned PreviousOriginalWhitespaceEndOffset =
  115. SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
  116. assert(PreviousOriginalWhitespaceEndOffset <=
  117. OriginalWhitespaceStartOffset);
  118. const char *const PreviousOriginalWhitespaceEndData =
  119. SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
  120. StringRef Text(PreviousOriginalWhitespaceEndData,
  121. SourceMgr.getCharacterData(OriginalWhitespaceStart) -
  122. PreviousOriginalWhitespaceEndData);
  123. // Usually consecutive changes would occur in consecutive tokens. This is
  124. // not the case however when analyzing some preprocessor runs of the
  125. // annotated lines. For example, in this code:
  126. //
  127. // #if A // line 1
  128. // int i = 1;
  129. // #else B // line 2
  130. // int i = 2;
  131. // #endif // line 3
  132. //
  133. // one of the runs will produce the sequence of lines marked with line 1, 2
  134. // and 3. So the two consecutive whitespace changes just before '// line 2'
  135. // and before '#endif // line 3' span multiple lines and tokens:
  136. //
  137. // #else B{change X}[// line 2
  138. // int i = 2;
  139. // ]{change Y}#endif // line 3
  140. //
  141. // For this reason, if the text between consecutive changes spans multiple
  142. // newlines, the token length must be adjusted to the end of the original
  143. // line of the token.
  144. auto NewlinePos = Text.find_first_of('\n');
  145. if (NewlinePos == StringRef::npos) {
  146. Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
  147. PreviousOriginalWhitespaceEndOffset +
  148. Changes[i].PreviousLinePostfix.size() +
  149. Changes[i - 1].CurrentLinePrefix.size();
  150. } else {
  151. Changes[i - 1].TokenLength =
  152. NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
  153. }
  154. // If there are multiple changes in this token, sum up all the changes until
  155. // the end of the line.
  156. if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0)
  157. LastOutsideTokenChange->TokenLength +=
  158. Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
  159. else
  160. LastOutsideTokenChange = &Changes[i - 1];
  161. Changes[i].PreviousEndOfTokenColumn =
  162. Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
  163. Changes[i - 1].IsTrailingComment =
  164. (Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
  165. (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
  166. Changes[i - 1].Tok->is(tok::comment) &&
  167. // FIXME: This is a dirty hack. The problem is that
  168. // BreakableLineCommentSection does comment reflow changes and here is
  169. // the aligning of trailing comments. Consider the case where we reflow
  170. // the second line up in this example:
  171. //
  172. // // line 1
  173. // // line 2
  174. //
  175. // That amounts to 2 changes by BreakableLineCommentSection:
  176. // - the first, delimited by (), for the whitespace between the tokens,
  177. // - and second, delimited by [], for the whitespace at the beginning
  178. // of the second token:
  179. //
  180. // // line 1(
  181. // )[// ]line 2
  182. //
  183. // So in the end we have two changes like this:
  184. //
  185. // // line1()[ ]line 2
  186. //
  187. // Note that the OriginalWhitespaceStart of the second change is the
  188. // same as the PreviousOriginalWhitespaceEnd of the first change.
  189. // In this case, the below check ensures that the second change doesn't
  190. // get treated as a trailing comment change here, since this might
  191. // trigger additional whitespace to be wrongly inserted before "line 2"
  192. // by the comment aligner here.
  193. //
  194. // For a proper solution we need a mechanism to say to WhitespaceManager
  195. // that a particular change breaks the current sequence of trailing
  196. // comments.
  197. OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
  198. }
  199. // FIXME: The last token is currently not always an eof token; in those
  200. // cases, setting TokenLength of the last token to 0 is wrong.
  201. Changes.back().TokenLength = 0;
  202. Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
  203. const WhitespaceManager::Change *LastBlockComment = nullptr;
  204. for (auto &Change : Changes) {
  205. // Reset the IsTrailingComment flag for changes inside of trailing comments
  206. // so they don't get realigned later. Comment line breaks however still need
  207. // to be aligned.
  208. if (Change.IsInsideToken && Change.NewlinesBefore == 0)
  209. Change.IsTrailingComment = false;
  210. Change.StartOfBlockComment = nullptr;
  211. Change.IndentationOffset = 0;
  212. if (Change.Tok->is(tok::comment)) {
  213. if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken)
  214. LastBlockComment = &Change;
  215. else {
  216. if ((Change.StartOfBlockComment = LastBlockComment))
  217. Change.IndentationOffset =
  218. Change.StartOfTokenColumn -
  219. Change.StartOfBlockComment->StartOfTokenColumn;
  220. }
  221. } else {
  222. LastBlockComment = nullptr;
  223. }
  224. }
  225. // Compute conditional nesting level
  226. // Level is increased for each conditional, unless this conditional continues
  227. // a chain of conditional, i.e. starts immediately after the colon of another
  228. // conditional.
  229. SmallVector<bool, 16> ScopeStack;
  230. int ConditionalsLevel = 0;
  231. for (auto &Change : Changes) {
  232. for (unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; ++i) {
  233. bool isNestedConditional =
  234. Change.Tok->FakeLParens[e - 1 - i] == prec::Conditional &&
  235. !(i == 0 && Change.Tok->Previous &&
  236. Change.Tok->Previous->is(TT_ConditionalExpr) &&
  237. Change.Tok->Previous->is(tok::colon));
  238. if (isNestedConditional)
  239. ++ConditionalsLevel;
  240. ScopeStack.push_back(isNestedConditional);
  241. }
  242. Change.ConditionalsLevel = ConditionalsLevel;
  243. for (unsigned i = Change.Tok->FakeRParens; i > 0 && ScopeStack.size();
  244. --i) {
  245. if (ScopeStack.pop_back_val())
  246. --ConditionalsLevel;
  247. }
  248. }
  249. }
  250. // Align a single sequence of tokens, see AlignTokens below.
  251. template <typename F>
  252. static void
  253. AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
  254. unsigned Column, F &&Matches,
  255. SmallVector<WhitespaceManager::Change, 16> &Changes) {
  256. bool FoundMatchOnLine = false;
  257. int Shift = 0;
  258. // ScopeStack keeps track of the current scope depth. It contains indices of
  259. // the first token on each scope.
  260. // We only run the "Matches" function on tokens from the outer-most scope.
  261. // However, we do need to pay special attention to one class of tokens
  262. // that are not in the outer-most scope, and that is function parameters
  263. // which are split across multiple lines, as illustrated by this example:
  264. // double a(int x);
  265. // int b(int y,
  266. // double z);
  267. // In the above example, we need to take special care to ensure that
  268. // 'double z' is indented along with it's owning function 'b'.
  269. // The same holds for calling a function:
  270. // double a = foo(x);
  271. // int b = bar(foo(y),
  272. // foor(z));
  273. // Similar for broken string literals:
  274. // double x = 3.14;
  275. // auto s = "Hello"
  276. // "World";
  277. // Special handling is required for 'nested' ternary operators.
  278. SmallVector<unsigned, 16> ScopeStack;
  279. for (unsigned i = Start; i != End; ++i) {
  280. if (ScopeStack.size() != 0 &&
  281. Changes[i].indentAndNestingLevel() <
  282. Changes[ScopeStack.back()].indentAndNestingLevel())
  283. ScopeStack.pop_back();
  284. // Compare current token to previous non-comment token to ensure whether
  285. // it is in a deeper scope or not.
  286. unsigned PreviousNonComment = i - 1;
  287. while (PreviousNonComment > Start &&
  288. Changes[PreviousNonComment].Tok->is(tok::comment))
  289. --PreviousNonComment;
  290. if (i != Start && Changes[i].indentAndNestingLevel() >
  291. Changes[PreviousNonComment].indentAndNestingLevel())
  292. ScopeStack.push_back(i);
  293. bool InsideNestedScope = ScopeStack.size() != 0;
  294. bool ContinuedStringLiteral = i > Start &&
  295. Changes[i].Tok->is(tok::string_literal) &&
  296. Changes[i - 1].Tok->is(tok::string_literal);
  297. bool SkipMatchCheck = InsideNestedScope || ContinuedStringLiteral;
  298. if (Changes[i].NewlinesBefore > 0 && !SkipMatchCheck) {
  299. Shift = 0;
  300. FoundMatchOnLine = false;
  301. }
  302. // If this is the first matching token to be aligned, remember by how many
  303. // spaces it has to be shifted, so the rest of the changes on the line are
  304. // shifted by the same amount
  305. if (!FoundMatchOnLine && !SkipMatchCheck && Matches(Changes[i])) {
  306. FoundMatchOnLine = true;
  307. Shift = Column - Changes[i].StartOfTokenColumn;
  308. Changes[i].Spaces += Shift;
  309. }
  310. // This is for function parameters that are split across multiple lines,
  311. // as mentioned in the ScopeStack comment.
  312. if (InsideNestedScope && Changes[i].NewlinesBefore > 0) {
  313. unsigned ScopeStart = ScopeStack.back();
  314. auto ShouldShiftBeAdded = [&] {
  315. // Function declaration
  316. if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
  317. return true;
  318. // Lambda.
  319. if (Changes[ScopeStart - 1].Tok->is(TT_LambdaLBrace))
  320. return false;
  321. // Continued function declaration
  322. if (ScopeStart > Start + 1 &&
  323. Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName))
  324. return true;
  325. // Continued function call
  326. if (ScopeStart > Start + 1 &&
  327. Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
  328. Changes[ScopeStart - 1].Tok->is(tok::l_paren) &&
  329. Changes[ScopeStart].Tok->isNot(TT_LambdaLSquare)) {
  330. if (Changes[i].Tok->MatchingParen &&
  331. Changes[i].Tok->MatchingParen->is(TT_LambdaLBrace))
  332. return false;
  333. return Style.BinPackArguments;
  334. }
  335. // Ternary operator
  336. if (Changes[i].Tok->is(TT_ConditionalExpr))
  337. return true;
  338. // Period Initializer .XXX = 1.
  339. if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
  340. return true;
  341. // Continued ternary operator
  342. if (Changes[i].Tok->Previous &&
  343. Changes[i].Tok->Previous->is(TT_ConditionalExpr))
  344. return true;
  345. // Continued braced list.
  346. if (ScopeStart > Start + 1 &&
  347. Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
  348. Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
  349. Changes[i].Tok->isNot(tok::r_brace)) {
  350. for (unsigned OuterScopeStart : llvm::reverse(ScopeStack)) {
  351. // Lambda.
  352. if (OuterScopeStart > Start &&
  353. Changes[OuterScopeStart - 1].Tok->is(TT_LambdaLBrace))
  354. return false;
  355. }
  356. return true;
  357. }
  358. return false;
  359. };
  360. if (ShouldShiftBeAdded())
  361. Changes[i].Spaces += Shift;
  362. }
  363. if (ContinuedStringLiteral)
  364. Changes[i].Spaces += Shift;
  365. Changes[i].StartOfTokenColumn += Shift;
  366. if (i + 1 != Changes.size())
  367. Changes[i + 1].PreviousEndOfTokenColumn += Shift;
  368. // If PointerAlignment is PAS_Right, keep *s or &s next to the token
  369. if (Style.PointerAlignment == FormatStyle::PAS_Right &&
  370. Changes[i].Spaces != 0) {
  371. for (int Previous = i - 1;
  372. Previous >= 0 &&
  373. Changes[Previous].Tok->getType() == TT_PointerOrReference;
  374. --Previous) {
  375. Changes[Previous + 1].Spaces -= Shift;
  376. Changes[Previous].Spaces += Shift;
  377. Changes[Previous].StartOfTokenColumn += Shift;
  378. }
  379. }
  380. }
  381. }
  382. // Walk through a subset of the changes, starting at StartAt, and find
  383. // sequences of matching tokens to align. To do so, keep track of the lines and
  384. // whether or not a matching token was found on a line. If a matching token is
  385. // found, extend the current sequence. If the current line cannot be part of a
  386. // sequence, e.g. because there is an empty line before it or it contains only
  387. // non-matching tokens, finalize the previous sequence.
  388. // The value returned is the token on which we stopped, either because we
  389. // exhausted all items inside Changes, or because we hit a scope level higher
  390. // than our initial scope.
  391. // This function is recursive. Each invocation processes only the scope level
  392. // equal to the initial level, which is the level of Changes[StartAt].
  393. // If we encounter a scope level greater than the initial level, then we call
  394. // ourselves recursively, thereby avoiding the pollution of the current state
  395. // with the alignment requirements of the nested sub-level. This recursive
  396. // behavior is necessary for aligning function prototypes that have one or more
  397. // arguments.
  398. // If this function encounters a scope level less than the initial level,
  399. // it returns the current position.
  400. // There is a non-obvious subtlety in the recursive behavior: Even though we
  401. // defer processing of nested levels to recursive invocations of this
  402. // function, when it comes time to align a sequence of tokens, we run the
  403. // alignment on the entire sequence, including the nested levels.
  404. // When doing so, most of the nested tokens are skipped, because their
  405. // alignment was already handled by the recursive invocations of this function.
  406. // However, the special exception is that we do NOT skip function parameters
  407. // that are split across multiple lines. See the test case in FormatTest.cpp
  408. // that mentions "split function parameter alignment" for an example of this.
  409. template <typename F>
  410. static unsigned AlignTokens(
  411. const FormatStyle &Style, F &&Matches,
  412. SmallVector<WhitespaceManager::Change, 16> &Changes, unsigned StartAt,
  413. const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
  414. unsigned MinColumn = 0;
  415. unsigned MaxColumn = UINT_MAX;
  416. // Line number of the start and the end of the current token sequence.
  417. unsigned StartOfSequence = 0;
  418. unsigned EndOfSequence = 0;
  419. // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
  420. // abort when we hit any token in a higher scope than the starting one.
  421. auto IndentAndNestingLevel = StartAt < Changes.size()
  422. ? Changes[StartAt].indentAndNestingLevel()
  423. : std::tuple<unsigned, unsigned, unsigned>();
  424. // Keep track of the number of commas before the matching tokens, we will only
  425. // align a sequence of matching tokens if they are preceded by the same number
  426. // of commas.
  427. unsigned CommasBeforeLastMatch = 0;
  428. unsigned CommasBeforeMatch = 0;
  429. // Whether a matching token has been found on the current line.
  430. bool FoundMatchOnLine = false;
  431. // Whether the current line consists purely of comments.
  432. bool LineIsComment = true;
  433. // Aligns a sequence of matching tokens, on the MinColumn column.
  434. //
  435. // Sequences start from the first matching token to align, and end at the
  436. // first token of the first line that doesn't need to be aligned.
  437. //
  438. // We need to adjust the StartOfTokenColumn of each Change that is on a line
  439. // containing any matching token to be aligned and located after such token.
  440. auto AlignCurrentSequence = [&] {
  441. if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
  442. AlignTokenSequence(Style, StartOfSequence, EndOfSequence, MinColumn,
  443. Matches, Changes);
  444. MinColumn = 0;
  445. MaxColumn = UINT_MAX;
  446. StartOfSequence = 0;
  447. EndOfSequence = 0;
  448. };
  449. unsigned i = StartAt;
  450. for (unsigned e = Changes.size(); i != e; ++i) {
  451. if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
  452. break;
  453. if (Changes[i].NewlinesBefore != 0) {
  454. CommasBeforeMatch = 0;
  455. EndOfSequence = i;
  456. // Whether to break the alignment sequence because of an empty line.
  457. bool EmptyLineBreak =
  458. (Changes[i].NewlinesBefore > 1) &&
  459. (ACS != FormatStyle::ACS_AcrossEmptyLines) &&
  460. (ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments);
  461. // Whether to break the alignment sequence because of a line without a
  462. // match.
  463. bool NoMatchBreak =
  464. !FoundMatchOnLine &&
  465. !(LineIsComment &&
  466. ((ACS == FormatStyle::ACS_AcrossComments) ||
  467. (ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)));
  468. if (EmptyLineBreak || NoMatchBreak)
  469. AlignCurrentSequence();
  470. // A new line starts, re-initialize line status tracking bools.
  471. // Keep the match state if a string literal is continued on this line.
  472. if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
  473. !Changes[i - 1].Tok->is(tok::string_literal))
  474. FoundMatchOnLine = false;
  475. LineIsComment = true;
  476. }
  477. if (!Changes[i].Tok->is(tok::comment)) {
  478. LineIsComment = false;
  479. }
  480. if (Changes[i].Tok->is(tok::comma)) {
  481. ++CommasBeforeMatch;
  482. } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
  483. // Call AlignTokens recursively, skipping over this scope block.
  484. unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
  485. i = StoppedAt - 1;
  486. continue;
  487. }
  488. if (!Matches(Changes[i]))
  489. continue;
  490. // If there is more than one matching token per line, or if the number of
  491. // preceding commas, do not match anymore, end the sequence.
  492. if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
  493. AlignCurrentSequence();
  494. CommasBeforeLastMatch = CommasBeforeMatch;
  495. FoundMatchOnLine = true;
  496. if (StartOfSequence == 0)
  497. StartOfSequence = i;
  498. unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
  499. int LineLengthAfter = Changes[i].TokenLength;
  500. for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
  501. LineLengthAfter += Changes[j].Spaces;
  502. // Changes are generally 1:1 with the tokens, but a change could also be
  503. // inside of a token, in which case it's counted more than once: once for
  504. // the whitespace surrounding the token (!IsInsideToken) and once for
  505. // each whitespace change within it (IsInsideToken).
  506. // Therefore, changes inside of a token should only count the space.
  507. if (!Changes[j].IsInsideToken)
  508. LineLengthAfter += Changes[j].TokenLength;
  509. }
  510. unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
  511. // If we are restricted by the maximum column width, end the sequence.
  512. if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
  513. CommasBeforeLastMatch != CommasBeforeMatch) {
  514. AlignCurrentSequence();
  515. StartOfSequence = i;
  516. }
  517. MinColumn = std::max(MinColumn, ChangeMinColumn);
  518. MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
  519. }
  520. EndOfSequence = i;
  521. AlignCurrentSequence();
  522. return i;
  523. }
  524. // Aligns a sequence of matching tokens, on the MinColumn column.
  525. //
  526. // Sequences start from the first matching token to align, and end at the
  527. // first token of the first line that doesn't need to be aligned.
  528. //
  529. // We need to adjust the StartOfTokenColumn of each Change that is on a line
  530. // containing any matching token to be aligned and located after such token.
  531. static void AlignMacroSequence(
  532. unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn,
  533. unsigned &MaxColumn, bool &FoundMatchOnLine,
  534. std::function<bool(const WhitespaceManager::Change &C)> AlignMacrosMatches,
  535. SmallVector<WhitespaceManager::Change, 16> &Changes) {
  536. if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
  537. FoundMatchOnLine = false;
  538. int Shift = 0;
  539. for (unsigned I = StartOfSequence; I != EndOfSequence; ++I) {
  540. if (Changes[I].NewlinesBefore > 0) {
  541. Shift = 0;
  542. FoundMatchOnLine = false;
  543. }
  544. // If this is the first matching token to be aligned, remember by how many
  545. // spaces it has to be shifted, so the rest of the changes on the line are
  546. // shifted by the same amount
  547. if (!FoundMatchOnLine && AlignMacrosMatches(Changes[I])) {
  548. FoundMatchOnLine = true;
  549. Shift = MinColumn - Changes[I].StartOfTokenColumn;
  550. Changes[I].Spaces += Shift;
  551. }
  552. assert(Shift >= 0);
  553. Changes[I].StartOfTokenColumn += Shift;
  554. if (I + 1 != Changes.size())
  555. Changes[I + 1].PreviousEndOfTokenColumn += Shift;
  556. }
  557. }
  558. MinColumn = 0;
  559. MaxColumn = UINT_MAX;
  560. StartOfSequence = 0;
  561. EndOfSequence = 0;
  562. }
  563. void WhitespaceManager::alignConsecutiveMacros() {
  564. if (Style.AlignConsecutiveMacros == FormatStyle::ACS_None)
  565. return;
  566. auto AlignMacrosMatches = [](const Change &C) {
  567. const FormatToken *Current = C.Tok;
  568. unsigned SpacesRequiredBefore = 1;
  569. if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
  570. return false;
  571. Current = Current->Previous;
  572. // If token is a ")", skip over the parameter list, to the
  573. // token that precedes the "("
  574. if (Current->is(tok::r_paren) && Current->MatchingParen) {
  575. Current = Current->MatchingParen->Previous;
  576. SpacesRequiredBefore = 0;
  577. }
  578. if (!Current || !Current->is(tok::identifier))
  579. return false;
  580. if (!Current->Previous || !Current->Previous->is(tok::pp_define))
  581. return false;
  582. // For a macro function, 0 spaces are required between the
  583. // identifier and the lparen that opens the parameter list.
  584. // For a simple macro, 1 space is required between the
  585. // identifier and the first token of the defined value.
  586. return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
  587. };
  588. unsigned MinColumn = 0;
  589. unsigned MaxColumn = UINT_MAX;
  590. // Start and end of the token sequence we're processing.
  591. unsigned StartOfSequence = 0;
  592. unsigned EndOfSequence = 0;
  593. // Whether a matching token has been found on the current line.
  594. bool FoundMatchOnLine = false;
  595. // Whether the current line consists only of comments
  596. bool LineIsComment = true;
  597. unsigned I = 0;
  598. for (unsigned E = Changes.size(); I != E; ++I) {
  599. if (Changes[I].NewlinesBefore != 0) {
  600. EndOfSequence = I;
  601. // Whether to break the alignment sequence because of an empty line.
  602. bool EmptyLineBreak =
  603. (Changes[I].NewlinesBefore > 1) &&
  604. (Style.AlignConsecutiveMacros != FormatStyle::ACS_AcrossEmptyLines) &&
  605. (Style.AlignConsecutiveMacros !=
  606. FormatStyle::ACS_AcrossEmptyLinesAndComments);
  607. // Whether to break the alignment sequence because of a line without a
  608. // match.
  609. bool NoMatchBreak =
  610. !FoundMatchOnLine &&
  611. !(LineIsComment && ((Style.AlignConsecutiveMacros ==
  612. FormatStyle::ACS_AcrossComments) ||
  613. (Style.AlignConsecutiveMacros ==
  614. FormatStyle::ACS_AcrossEmptyLinesAndComments)));
  615. if (EmptyLineBreak || NoMatchBreak)
  616. AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
  617. FoundMatchOnLine, AlignMacrosMatches, Changes);
  618. // A new line starts, re-initialize line status tracking bools.
  619. FoundMatchOnLine = false;
  620. LineIsComment = true;
  621. }
  622. if (!Changes[I].Tok->is(tok::comment)) {
  623. LineIsComment = false;
  624. }
  625. if (!AlignMacrosMatches(Changes[I]))
  626. continue;
  627. FoundMatchOnLine = true;
  628. if (StartOfSequence == 0)
  629. StartOfSequence = I;
  630. unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
  631. int LineLengthAfter = -Changes[I].Spaces;
  632. for (unsigned j = I; j != E && Changes[j].NewlinesBefore == 0; ++j)
  633. LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
  634. unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
  635. MinColumn = std::max(MinColumn, ChangeMinColumn);
  636. MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
  637. }
  638. EndOfSequence = I;
  639. AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
  640. FoundMatchOnLine, AlignMacrosMatches, Changes);
  641. }
  642. void WhitespaceManager::alignConsecutiveAssignments() {
  643. if (Style.AlignConsecutiveAssignments == FormatStyle::ACS_None)
  644. return;
  645. AlignTokens(
  646. Style,
  647. [&](const Change &C) {
  648. // Do not align on equal signs that are first on a line.
  649. if (C.NewlinesBefore > 0)
  650. return false;
  651. // Do not align on equal signs that are last on a line.
  652. if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
  653. return false;
  654. // Do not align operator= overloads.
  655. FormatToken *Previous = C.Tok->getPreviousNonComment();
  656. if (Previous && Previous->is(tok::kw_operator))
  657. return false;
  658. return C.Tok->is(tok::equal);
  659. },
  660. Changes, /*StartAt=*/0, Style.AlignConsecutiveAssignments);
  661. }
  662. void WhitespaceManager::alignConsecutiveBitFields() {
  663. if (Style.AlignConsecutiveBitFields == FormatStyle::ACS_None)
  664. return;
  665. AlignTokens(
  666. Style,
  667. [&](Change const &C) {
  668. // Do not align on ':' that is first on a line.
  669. if (C.NewlinesBefore > 0)
  670. return false;
  671. // Do not align on ':' that is last on a line.
  672. if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
  673. return false;
  674. return C.Tok->is(TT_BitFieldColon);
  675. },
  676. Changes, /*StartAt=*/0, Style.AlignConsecutiveBitFields);
  677. }
  678. void WhitespaceManager::alignConsecutiveDeclarations() {
  679. if (Style.AlignConsecutiveDeclarations == FormatStyle::ACS_None)
  680. return;
  681. AlignTokens(
  682. Style,
  683. [](Change const &C) {
  684. // tok::kw_operator is necessary for aligning operator overload
  685. // definitions.
  686. if (C.Tok->isOneOf(TT_FunctionDeclarationName, tok::kw_operator))
  687. return true;
  688. if (C.Tok->isNot(TT_StartOfName))
  689. return false;
  690. if (C.Tok->Previous &&
  691. C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
  692. return false;
  693. // Check if there is a subsequent name that starts the same declaration.
  694. for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
  695. if (Next->is(tok::comment))
  696. continue;
  697. if (Next->is(TT_PointerOrReference))
  698. return false;
  699. if (!Next->Tok.getIdentifierInfo())
  700. break;
  701. if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
  702. tok::kw_operator))
  703. return false;
  704. }
  705. return true;
  706. },
  707. Changes, /*StartAt=*/0, Style.AlignConsecutiveDeclarations);
  708. }
  709. void WhitespaceManager::alignChainedConditionals() {
  710. if (Style.BreakBeforeTernaryOperators) {
  711. AlignTokens(
  712. Style,
  713. [](Change const &C) {
  714. // Align question operators and last colon
  715. return C.Tok->is(TT_ConditionalExpr) &&
  716. ((C.Tok->is(tok::question) && !C.NewlinesBefore) ||
  717. (C.Tok->is(tok::colon) && C.Tok->Next &&
  718. (C.Tok->Next->FakeLParens.size() == 0 ||
  719. C.Tok->Next->FakeLParens.back() != prec::Conditional)));
  720. },
  721. Changes, /*StartAt=*/0);
  722. } else {
  723. static auto AlignWrappedOperand = [](Change const &C) {
  724. FormatToken *Previous = C.Tok->getPreviousNonComment();
  725. return C.NewlinesBefore && Previous && Previous->is(TT_ConditionalExpr) &&
  726. (Previous->is(tok::colon) &&
  727. (C.Tok->FakeLParens.size() == 0 ||
  728. C.Tok->FakeLParens.back() != prec::Conditional));
  729. };
  730. // Ensure we keep alignment of wrapped operands with non-wrapped operands
  731. // Since we actually align the operators, the wrapped operands need the
  732. // extra offset to be properly aligned.
  733. for (Change &C : Changes) {
  734. if (AlignWrappedOperand(C))
  735. C.StartOfTokenColumn -= 2;
  736. }
  737. AlignTokens(
  738. Style,
  739. [this](Change const &C) {
  740. // Align question operators if next operand is not wrapped, as
  741. // well as wrapped operands after question operator or last
  742. // colon in conditional sequence
  743. return (C.Tok->is(TT_ConditionalExpr) && C.Tok->is(tok::question) &&
  744. &C != &Changes.back() && (&C + 1)->NewlinesBefore == 0 &&
  745. !(&C + 1)->IsTrailingComment) ||
  746. AlignWrappedOperand(C);
  747. },
  748. Changes, /*StartAt=*/0);
  749. }
  750. }
  751. void WhitespaceManager::alignTrailingComments() {
  752. unsigned MinColumn = 0;
  753. unsigned MaxColumn = UINT_MAX;
  754. unsigned StartOfSequence = 0;
  755. bool BreakBeforeNext = false;
  756. unsigned Newlines = 0;
  757. for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
  758. if (Changes[i].StartOfBlockComment)
  759. continue;
  760. Newlines += Changes[i].NewlinesBefore;
  761. if (!Changes[i].IsTrailingComment)
  762. continue;
  763. unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
  764. unsigned ChangeMaxColumn;
  765. if (Style.ColumnLimit == 0)
  766. ChangeMaxColumn = UINT_MAX;
  767. else if (Style.ColumnLimit >= Changes[i].TokenLength)
  768. ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
  769. else
  770. ChangeMaxColumn = ChangeMinColumn;
  771. // If we don't create a replacement for this change, we have to consider
  772. // it to be immovable.
  773. if (!Changes[i].CreateReplacement)
  774. ChangeMaxColumn = ChangeMinColumn;
  775. if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
  776. ChangeMaxColumn -= 2;
  777. // If this comment follows an } in column 0, it probably documents the
  778. // closing of a namespace and we don't want to align it.
  779. bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
  780. Changes[i - 1].Tok->is(tok::r_brace) &&
  781. Changes[i - 1].StartOfTokenColumn == 0;
  782. bool WasAlignedWithStartOfNextLine = false;
  783. if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
  784. unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
  785. Changes[i].OriginalWhitespaceRange.getEnd());
  786. for (unsigned j = i + 1; j != e; ++j) {
  787. if (Changes[j].Tok->is(tok::comment))
  788. continue;
  789. unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
  790. Changes[j].OriginalWhitespaceRange.getEnd());
  791. // The start of the next token was previously aligned with the
  792. // start of this comment.
  793. WasAlignedWithStartOfNextLine =
  794. CommentColumn == NextColumn ||
  795. CommentColumn == NextColumn + Style.IndentWidth;
  796. break;
  797. }
  798. }
  799. if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
  800. alignTrailingComments(StartOfSequence, i, MinColumn);
  801. MinColumn = ChangeMinColumn;
  802. MaxColumn = ChangeMinColumn;
  803. StartOfSequence = i;
  804. } else if (BreakBeforeNext || Newlines > 1 ||
  805. (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
  806. // Break the comment sequence if the previous line did not end
  807. // in a trailing comment.
  808. (Changes[i].NewlinesBefore == 1 && i > 0 &&
  809. !Changes[i - 1].IsTrailingComment) ||
  810. WasAlignedWithStartOfNextLine) {
  811. alignTrailingComments(StartOfSequence, i, MinColumn);
  812. MinColumn = ChangeMinColumn;
  813. MaxColumn = ChangeMaxColumn;
  814. StartOfSequence = i;
  815. } else {
  816. MinColumn = std::max(MinColumn, ChangeMinColumn);
  817. MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
  818. }
  819. BreakBeforeNext = (i == 0) || (Changes[i].NewlinesBefore > 1) ||
  820. // Never start a sequence with a comment at the beginning
  821. // of the line.
  822. (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
  823. Newlines = 0;
  824. }
  825. alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
  826. }
  827. void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
  828. unsigned Column) {
  829. for (unsigned i = Start; i != End; ++i) {
  830. int Shift = 0;
  831. if (Changes[i].IsTrailingComment) {
  832. Shift = Column - Changes[i].StartOfTokenColumn;
  833. }
  834. if (Changes[i].StartOfBlockComment) {
  835. Shift = Changes[i].IndentationOffset +
  836. Changes[i].StartOfBlockComment->StartOfTokenColumn -
  837. Changes[i].StartOfTokenColumn;
  838. }
  839. if (Shift < 0)
  840. continue;
  841. Changes[i].Spaces += Shift;
  842. if (i + 1 != Changes.size())
  843. Changes[i + 1].PreviousEndOfTokenColumn += Shift;
  844. Changes[i].StartOfTokenColumn += Shift;
  845. }
  846. }
  847. void WhitespaceManager::alignEscapedNewlines() {
  848. if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
  849. return;
  850. bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
  851. unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
  852. unsigned StartOfMacro = 0;
  853. for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
  854. Change &C = Changes[i];
  855. if (C.NewlinesBefore > 0) {
  856. if (C.ContinuesPPDirective) {
  857. MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
  858. } else {
  859. alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
  860. MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
  861. StartOfMacro = i;
  862. }
  863. }
  864. }
  865. alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
  866. }
  867. void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
  868. unsigned Column) {
  869. for (unsigned i = Start; i < End; ++i) {
  870. Change &C = Changes[i];
  871. if (C.NewlinesBefore > 0) {
  872. assert(C.ContinuesPPDirective);
  873. if (C.PreviousEndOfTokenColumn + 1 > Column)
  874. C.EscapedNewlineColumn = 0;
  875. else
  876. C.EscapedNewlineColumn = Column;
  877. }
  878. }
  879. }
  880. void WhitespaceManager::alignArrayInitializers() {
  881. if (Style.AlignArrayOfStructures == FormatStyle::AIAS_None)
  882. return;
  883. for (unsigned ChangeIndex = 1U, ChangeEnd = Changes.size();
  884. ChangeIndex < ChangeEnd; ++ChangeIndex) {
  885. auto &C = Changes[ChangeIndex];
  886. if (C.Tok->IsArrayInitializer) {
  887. bool FoundComplete = false;
  888. for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
  889. ++InsideIndex) {
  890. if (Changes[InsideIndex].Tok == C.Tok->MatchingParen) {
  891. alignArrayInitializers(ChangeIndex, InsideIndex + 1);
  892. ChangeIndex = InsideIndex + 1;
  893. FoundComplete = true;
  894. break;
  895. }
  896. }
  897. if (!FoundComplete)
  898. ChangeIndex = ChangeEnd;
  899. }
  900. }
  901. }
  902. void WhitespaceManager::alignArrayInitializers(unsigned Start, unsigned End) {
  903. if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Right)
  904. alignArrayInitializersRightJustified(getCells(Start, End));
  905. else if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Left)
  906. alignArrayInitializersLeftJustified(getCells(Start, End));
  907. }
  908. void WhitespaceManager::alignArrayInitializersRightJustified(
  909. CellDescriptions &&CellDescs) {
  910. auto &Cells = CellDescs.Cells;
  911. // Now go through and fixup the spaces.
  912. auto *CellIter = Cells.begin();
  913. for (auto i = 0U; i < CellDescs.CellCount; ++i, ++CellIter) {
  914. unsigned NetWidth = 0U;
  915. if (isSplitCell(*CellIter))
  916. NetWidth = getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
  917. auto CellWidth = getMaximumCellWidth(CellIter, NetWidth);
  918. if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
  919. // So in here we want to see if there is a brace that falls
  920. // on a line that was split. If so on that line we make sure that
  921. // the spaces in front of the brace are enough.
  922. Changes[CellIter->Index].NewlinesBefore = 0;
  923. Changes[CellIter->Index].Spaces = 0;
  924. for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
  925. Next = Next->NextColumnElement) {
  926. Changes[Next->Index].Spaces = 0;
  927. Changes[Next->Index].NewlinesBefore = 0;
  928. }
  929. // Unless the array is empty, we need the position of all the
  930. // immediately adjacent cells
  931. if (CellIter != Cells.begin()) {
  932. auto ThisNetWidth =
  933. getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
  934. auto MaxNetWidth =
  935. getMaximumNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces,
  936. CellDescs.CellCount);
  937. if (ThisNetWidth < MaxNetWidth)
  938. Changes[CellIter->Index].Spaces = (MaxNetWidth - ThisNetWidth);
  939. auto RowCount = 1U;
  940. auto Offset = std::distance(Cells.begin(), CellIter);
  941. for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
  942. Next = Next->NextColumnElement) {
  943. auto *Start = (Cells.begin() + RowCount * CellDescs.CellCount);
  944. auto *End = Start + Offset;
  945. ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
  946. if (ThisNetWidth < MaxNetWidth)
  947. Changes[Next->Index].Spaces = (MaxNetWidth - ThisNetWidth);
  948. ++RowCount;
  949. }
  950. }
  951. } else {
  952. auto ThisWidth =
  953. calculateCellWidth(CellIter->Index, CellIter->EndIndex, true) +
  954. NetWidth;
  955. if (Changes[CellIter->Index].NewlinesBefore == 0) {
  956. Changes[CellIter->Index].Spaces = (CellWidth - (ThisWidth + NetWidth));
  957. Changes[CellIter->Index].Spaces += (i > 0) ? 1 : 0;
  958. }
  959. alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
  960. for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
  961. Next = Next->NextColumnElement) {
  962. ThisWidth =
  963. calculateCellWidth(Next->Index, Next->EndIndex, true) + NetWidth;
  964. if (Changes[Next->Index].NewlinesBefore == 0) {
  965. Changes[Next->Index].Spaces = (CellWidth - ThisWidth);
  966. Changes[Next->Index].Spaces += (i > 0) ? 1 : 0;
  967. }
  968. alignToStartOfCell(Next->Index, Next->EndIndex);
  969. }
  970. }
  971. }
  972. }
  973. void WhitespaceManager::alignArrayInitializersLeftJustified(
  974. CellDescriptions &&CellDescs) {
  975. auto &Cells = CellDescs.Cells;
  976. // Now go through and fixup the spaces.
  977. auto *CellIter = Cells.begin();
  978. // The first cell needs to be against the left brace.
  979. if (Changes[CellIter->Index].NewlinesBefore == 0)
  980. Changes[CellIter->Index].Spaces = 0;
  981. else
  982. Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces;
  983. ++CellIter;
  984. for (auto i = 1U; i < CellDescs.CellCount; i++, ++CellIter) {
  985. auto MaxNetWidth = getMaximumNetWidth(
  986. Cells.begin(), CellIter, CellDescs.InitialSpaces, CellDescs.CellCount);
  987. auto ThisNetWidth =
  988. getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
  989. if (Changes[CellIter->Index].NewlinesBefore == 0) {
  990. Changes[CellIter->Index].Spaces =
  991. MaxNetWidth - ThisNetWidth +
  992. (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1 : 0);
  993. }
  994. auto RowCount = 1U;
  995. auto Offset = std::distance(Cells.begin(), CellIter);
  996. for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
  997. Next = Next->NextColumnElement) {
  998. auto *Start = (Cells.begin() + RowCount * CellDescs.CellCount);
  999. auto *End = Start + Offset;
  1000. auto ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
  1001. if (Changes[Next->Index].NewlinesBefore == 0) {
  1002. Changes[Next->Index].Spaces =
  1003. MaxNetWidth - ThisNetWidth +
  1004. (Changes[Next->Index].Tok->isNot(tok::r_brace) ? 1 : 0);
  1005. }
  1006. ++RowCount;
  1007. }
  1008. }
  1009. }
  1010. bool WhitespaceManager::isSplitCell(const CellDescription &Cell) {
  1011. if (Cell.HasSplit)
  1012. return true;
  1013. for (const auto *Next = Cell.NextColumnElement; Next != nullptr;
  1014. Next = Next->NextColumnElement) {
  1015. if (Next->HasSplit)
  1016. return true;
  1017. }
  1018. return false;
  1019. }
  1020. WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
  1021. unsigned End) {
  1022. unsigned Depth = 0;
  1023. unsigned Cell = 0;
  1024. unsigned CellCount = 0;
  1025. unsigned InitialSpaces = 0;
  1026. unsigned InitialTokenLength = 0;
  1027. unsigned EndSpaces = 0;
  1028. SmallVector<CellDescription> Cells;
  1029. const FormatToken *MatchingParen = nullptr;
  1030. for (unsigned i = Start; i < End; ++i) {
  1031. auto &C = Changes[i];
  1032. if (C.Tok->is(tok::l_brace))
  1033. ++Depth;
  1034. else if (C.Tok->is(tok::r_brace))
  1035. --Depth;
  1036. if (Depth == 2) {
  1037. if (C.Tok->is(tok::l_brace)) {
  1038. Cell = 0;
  1039. MatchingParen = C.Tok->MatchingParen;
  1040. if (InitialSpaces == 0) {
  1041. InitialSpaces = C.Spaces + C.TokenLength;
  1042. InitialTokenLength = C.TokenLength;
  1043. auto j = i - 1;
  1044. for (; Changes[j].NewlinesBefore == 0 && j > Start; --j) {
  1045. InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
  1046. InitialTokenLength += Changes[j].TokenLength;
  1047. }
  1048. if (C.NewlinesBefore == 0) {
  1049. InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
  1050. InitialTokenLength += Changes[j].TokenLength;
  1051. }
  1052. }
  1053. } else if (C.Tok->is(tok::comma)) {
  1054. if (!Cells.empty())
  1055. Cells.back().EndIndex = i;
  1056. if (C.Tok->getNextNonComment()->isNot(tok::r_brace)) // dangling comma
  1057. ++Cell;
  1058. }
  1059. } else if (Depth == 1) {
  1060. if (C.Tok == MatchingParen) {
  1061. if (!Cells.empty())
  1062. Cells.back().EndIndex = i;
  1063. Cells.push_back(CellDescription{i, ++Cell, i + 1, false, nullptr});
  1064. CellCount = C.Tok->Previous->isNot(tok::comma) ? Cell + 1 : Cell;
  1065. // Go to the next non-comment and ensure there is a break in front
  1066. const auto *NextNonComment = C.Tok->getNextNonComment();
  1067. while (NextNonComment->is(tok::comma))
  1068. NextNonComment = NextNonComment->getNextNonComment();
  1069. auto j = i;
  1070. while (Changes[j].Tok != NextNonComment && j < End)
  1071. ++j;
  1072. if (j < End && Changes[j].NewlinesBefore == 0 &&
  1073. Changes[j].Tok->isNot(tok::r_brace)) {
  1074. Changes[j].NewlinesBefore = 1;
  1075. // Account for the added token lengths
  1076. Changes[j].Spaces = InitialSpaces - InitialTokenLength;
  1077. }
  1078. } else if (C.Tok->is(tok::comment)) {
  1079. // Trailing comments stay at a space past the last token
  1080. C.Spaces = Changes[i - 1].Tok->is(tok::comma) ? 1 : 2;
  1081. } else if (C.Tok->is(tok::l_brace)) {
  1082. // We need to make sure that the ending braces is aligned to the
  1083. // start of our initializer
  1084. auto j = i - 1;
  1085. for (; j > 0 && !Changes[j].Tok->ArrayInitializerLineStart; --j)
  1086. ; // Nothing the loop does the work
  1087. EndSpaces = Changes[j].Spaces;
  1088. }
  1089. } else if (Depth == 0 && C.Tok->is(tok::r_brace)) {
  1090. C.NewlinesBefore = 1;
  1091. C.Spaces = EndSpaces;
  1092. }
  1093. if (C.Tok->StartsColumn) {
  1094. // This gets us past tokens that have been split over multiple
  1095. // lines
  1096. bool HasSplit = false;
  1097. if (Changes[i].NewlinesBefore > 0) {
  1098. // So if we split a line previously and the tail line + this token is
  1099. // less then the column limit we remove the split here and just put
  1100. // the column start at a space past the comma
  1101. //
  1102. // FIXME This if branch covers the cases where the column is not
  1103. // the first column. This leads to weird pathologies like the formatting
  1104. // auto foo = Items{
  1105. // Section{
  1106. // 0, bar(),
  1107. // }
  1108. // };
  1109. // Well if it doesn't lead to that it's indicative that the line
  1110. // breaking should be revisited. Unfortunately alot of other options
  1111. // interact with this
  1112. auto j = i - 1;
  1113. if ((j - 1) > Start && Changes[j].Tok->is(tok::comma) &&
  1114. Changes[j - 1].NewlinesBefore > 0) {
  1115. --j;
  1116. auto LineLimit = Changes[j].Spaces + Changes[j].TokenLength;
  1117. if (LineLimit < Style.ColumnLimit) {
  1118. Changes[i].NewlinesBefore = 0;
  1119. Changes[i].Spaces = 1;
  1120. }
  1121. }
  1122. }
  1123. while (Changes[i].NewlinesBefore > 0 && Changes[i].Tok == C.Tok) {
  1124. Changes[i].Spaces = InitialSpaces;
  1125. ++i;
  1126. HasSplit = true;
  1127. }
  1128. if (Changes[i].Tok != C.Tok)
  1129. --i;
  1130. Cells.push_back(CellDescription{i, Cell, i, HasSplit, nullptr});
  1131. }
  1132. }
  1133. return linkCells({Cells, CellCount, InitialSpaces});
  1134. }
  1135. unsigned WhitespaceManager::calculateCellWidth(unsigned Start, unsigned End,
  1136. bool WithSpaces) const {
  1137. unsigned CellWidth = 0;
  1138. for (auto i = Start; i < End; i++) {
  1139. if (Changes[i].NewlinesBefore > 0)
  1140. CellWidth = 0;
  1141. CellWidth += Changes[i].TokenLength;
  1142. CellWidth += (WithSpaces ? Changes[i].Spaces : 0);
  1143. }
  1144. return CellWidth;
  1145. }
  1146. void WhitespaceManager::alignToStartOfCell(unsigned Start, unsigned End) {
  1147. if ((End - Start) <= 1)
  1148. return;
  1149. // If the line is broken anywhere in there make sure everything
  1150. // is aligned to the parent
  1151. for (auto i = Start + 1; i < End; i++) {
  1152. if (Changes[i].NewlinesBefore > 0)
  1153. Changes[i].Spaces = Changes[Start].Spaces;
  1154. }
  1155. }
  1156. WhitespaceManager::CellDescriptions
  1157. WhitespaceManager::linkCells(CellDescriptions &&CellDesc) {
  1158. auto &Cells = CellDesc.Cells;
  1159. for (auto *CellIter = Cells.begin(); CellIter != Cells.end(); ++CellIter) {
  1160. if (CellIter->NextColumnElement == nullptr &&
  1161. ((CellIter + 1) != Cells.end())) {
  1162. for (auto *NextIter = CellIter + 1; NextIter != Cells.end(); ++NextIter) {
  1163. if (NextIter->Cell == CellIter->Cell) {
  1164. CellIter->NextColumnElement = &(*NextIter);
  1165. break;
  1166. }
  1167. }
  1168. }
  1169. }
  1170. return std::move(CellDesc);
  1171. }
  1172. void WhitespaceManager::generateChanges() {
  1173. for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
  1174. const Change &C = Changes[i];
  1175. if (i > 0 && Changes[i - 1].OriginalWhitespaceRange.getBegin() ==
  1176. C.OriginalWhitespaceRange.getBegin()) {
  1177. // Do not generate two replacements for the same location.
  1178. continue;
  1179. }
  1180. if (C.CreateReplacement) {
  1181. std::string ReplacementText = C.PreviousLinePostfix;
  1182. if (C.ContinuesPPDirective)
  1183. appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
  1184. C.PreviousEndOfTokenColumn,
  1185. C.EscapedNewlineColumn);
  1186. else
  1187. appendNewlineText(ReplacementText, C.NewlinesBefore);
  1188. // FIXME: This assert should hold if we computed the column correctly.
  1189. // assert((int)C.StartOfTokenColumn >= C.Spaces);
  1190. appendIndentText(
  1191. ReplacementText, C.Tok->IndentLevel, std::max(0, C.Spaces),
  1192. std::max((int)C.StartOfTokenColumn, C.Spaces) - std::max(0, C.Spaces),
  1193. C.IsAligned);
  1194. ReplacementText.append(C.CurrentLinePrefix);
  1195. storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
  1196. }
  1197. }
  1198. }
  1199. void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
  1200. unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
  1201. SourceMgr.getFileOffset(Range.getBegin());
  1202. // Don't create a replacement, if it does not change anything.
  1203. if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
  1204. WhitespaceLength) == Text)
  1205. return;
  1206. auto Err = Replaces.add(tooling::Replacement(
  1207. SourceMgr, CharSourceRange::getCharRange(Range), Text));
  1208. // FIXME: better error handling. For now, just print an error message in the
  1209. // release version.
  1210. if (Err) {
  1211. llvm::errs() << llvm::toString(std::move(Err)) << "\n";
  1212. assert(false);
  1213. }
  1214. }
  1215. void WhitespaceManager::appendNewlineText(std::string &Text,
  1216. unsigned Newlines) {
  1217. if (UseCRLF) {
  1218. Text.reserve(Text.size() + 2 * Newlines);
  1219. for (unsigned i = 0; i < Newlines; ++i)
  1220. Text.append("\r\n");
  1221. } else {
  1222. Text.append(Newlines, '\n');
  1223. }
  1224. }
  1225. void WhitespaceManager::appendEscapedNewlineText(
  1226. std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
  1227. unsigned EscapedNewlineColumn) {
  1228. if (Newlines > 0) {
  1229. unsigned Spaces =
  1230. std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
  1231. for (unsigned i = 0; i < Newlines; ++i) {
  1232. Text.append(Spaces, ' ');
  1233. Text.append(UseCRLF ? "\\\r\n" : "\\\n");
  1234. Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
  1235. }
  1236. }
  1237. }
  1238. void WhitespaceManager::appendIndentText(std::string &Text,
  1239. unsigned IndentLevel, unsigned Spaces,
  1240. unsigned WhitespaceStartColumn,
  1241. bool IsAligned) {
  1242. switch (Style.UseTab) {
  1243. case FormatStyle::UT_Never:
  1244. Text.append(Spaces, ' ');
  1245. break;
  1246. case FormatStyle::UT_Always: {
  1247. if (Style.TabWidth) {
  1248. unsigned FirstTabWidth =
  1249. Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
  1250. // Insert only spaces when we want to end up before the next tab.
  1251. if (Spaces < FirstTabWidth || Spaces == 1) {
  1252. Text.append(Spaces, ' ');
  1253. break;
  1254. }
  1255. // Align to the next tab.
  1256. Spaces -= FirstTabWidth;
  1257. Text.append("\t");
  1258. Text.append(Spaces / Style.TabWidth, '\t');
  1259. Text.append(Spaces % Style.TabWidth, ' ');
  1260. } else if (Spaces == 1) {
  1261. Text.append(Spaces, ' ');
  1262. }
  1263. break;
  1264. }
  1265. case FormatStyle::UT_ForIndentation:
  1266. if (WhitespaceStartColumn == 0) {
  1267. unsigned Indentation = IndentLevel * Style.IndentWidth;
  1268. Spaces = appendTabIndent(Text, Spaces, Indentation);
  1269. }
  1270. Text.append(Spaces, ' ');
  1271. break;
  1272. case FormatStyle::UT_ForContinuationAndIndentation:
  1273. if (WhitespaceStartColumn == 0)
  1274. Spaces = appendTabIndent(Text, Spaces, Spaces);
  1275. Text.append(Spaces, ' ');
  1276. break;
  1277. case FormatStyle::UT_AlignWithSpaces:
  1278. if (WhitespaceStartColumn == 0) {
  1279. unsigned Indentation =
  1280. IsAligned ? IndentLevel * Style.IndentWidth : Spaces;
  1281. Spaces = appendTabIndent(Text, Spaces, Indentation);
  1282. }
  1283. Text.append(Spaces, ' ');
  1284. break;
  1285. }
  1286. }
  1287. unsigned WhitespaceManager::appendTabIndent(std::string &Text, unsigned Spaces,
  1288. unsigned Indentation) {
  1289. // This happens, e.g. when a line in a block comment is indented less than the
  1290. // first one.
  1291. if (Indentation > Spaces)
  1292. Indentation = Spaces;
  1293. if (Style.TabWidth) {
  1294. unsigned Tabs = Indentation / Style.TabWidth;
  1295. Text.append(Tabs, '\t');
  1296. Spaces -= Tabs * Style.TabWidth;
  1297. }
  1298. return Spaces;
  1299. }
  1300. } // namespace format
  1301. } // namespace clang