WhitespaceManager.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. namespace clang {
  16. namespace format {
  17. bool WhitespaceManager::Change::IsBeforeInFile::operator()(
  18. const Change &C1, const Change &C2) const {
  19. return SourceMgr.isBeforeInTranslationUnit(
  20. C1.OriginalWhitespaceRange.getBegin(),
  21. C2.OriginalWhitespaceRange.getBegin());
  22. }
  23. WhitespaceManager::Change::Change(const FormatToken &Tok,
  24. bool CreateReplacement,
  25. SourceRange OriginalWhitespaceRange,
  26. int Spaces, unsigned StartOfTokenColumn,
  27. unsigned NewlinesBefore,
  28. StringRef PreviousLinePostfix,
  29. StringRef CurrentLinePrefix,
  30. bool ContinuesPPDirective, bool IsInsideToken)
  31. : Tok(&Tok), CreateReplacement(CreateReplacement),
  32. OriginalWhitespaceRange(OriginalWhitespaceRange),
  33. StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
  34. PreviousLinePostfix(PreviousLinePostfix),
  35. CurrentLinePrefix(CurrentLinePrefix),
  36. ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces),
  37. IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0),
  38. PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
  39. StartOfBlockComment(nullptr), IndentationOffset(0) {}
  40. void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
  41. unsigned Spaces,
  42. unsigned StartOfTokenColumn,
  43. bool InPPDirective) {
  44. if (Tok.Finalized)
  45. return;
  46. Tok.Decision = (Newlines > 0) ? FD_Break : FD_Continue;
  47. Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
  48. Spaces, StartOfTokenColumn, Newlines, "", "",
  49. InPPDirective && !Tok.IsFirst,
  50. /*IsInsideToken=*/false));
  51. }
  52. void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
  53. bool InPPDirective) {
  54. if (Tok.Finalized)
  55. return;
  56. Changes.push_back(Change(Tok, /*CreateReplacement=*/false,
  57. Tok.WhitespaceRange, /*Spaces=*/0,
  58. Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
  59. InPPDirective && !Tok.IsFirst,
  60. /*IsInsideToken=*/false));
  61. }
  62. llvm::Error
  63. WhitespaceManager::addReplacement(const tooling::Replacement &Replacement) {
  64. return Replaces.add(Replacement);
  65. }
  66. void WhitespaceManager::replaceWhitespaceInToken(
  67. const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
  68. StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
  69. unsigned Newlines, int Spaces) {
  70. if (Tok.Finalized)
  71. return;
  72. SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
  73. Changes.push_back(
  74. Change(Tok, /*CreateReplacement=*/true,
  75. SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
  76. std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
  77. InPPDirective && !Tok.IsFirst, /*IsInsideToken=*/true));
  78. }
  79. const tooling::Replacements &WhitespaceManager::generateReplacements() {
  80. if (Changes.empty())
  81. return Replaces;
  82. llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr));
  83. calculateLineBreakInformation();
  84. alignConsecutiveMacros();
  85. alignConsecutiveDeclarations();
  86. alignConsecutiveAssignments();
  87. alignTrailingComments();
  88. alignEscapedNewlines();
  89. generateChanges();
  90. return Replaces;
  91. }
  92. void WhitespaceManager::calculateLineBreakInformation() {
  93. Changes[0].PreviousEndOfTokenColumn = 0;
  94. Change *LastOutsideTokenChange = &Changes[0];
  95. for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
  96. SourceLocation OriginalWhitespaceStart =
  97. Changes[i].OriginalWhitespaceRange.getBegin();
  98. SourceLocation PreviousOriginalWhitespaceEnd =
  99. Changes[i - 1].OriginalWhitespaceRange.getEnd();
  100. unsigned OriginalWhitespaceStartOffset =
  101. SourceMgr.getFileOffset(OriginalWhitespaceStart);
  102. unsigned PreviousOriginalWhitespaceEndOffset =
  103. SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
  104. assert(PreviousOriginalWhitespaceEndOffset <=
  105. OriginalWhitespaceStartOffset);
  106. const char *const PreviousOriginalWhitespaceEndData =
  107. SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
  108. StringRef Text(PreviousOriginalWhitespaceEndData,
  109. SourceMgr.getCharacterData(OriginalWhitespaceStart) -
  110. PreviousOriginalWhitespaceEndData);
  111. // Usually consecutive changes would occur in consecutive tokens. This is
  112. // not the case however when analyzing some preprocessor runs of the
  113. // annotated lines. For example, in this code:
  114. //
  115. // #if A // line 1
  116. // int i = 1;
  117. // #else B // line 2
  118. // int i = 2;
  119. // #endif // line 3
  120. //
  121. // one of the runs will produce the sequence of lines marked with line 1, 2
  122. // and 3. So the two consecutive whitespace changes just before '// line 2'
  123. // and before '#endif // line 3' span multiple lines and tokens:
  124. //
  125. // #else B{change X}[// line 2
  126. // int i = 2;
  127. // ]{change Y}#endif // line 3
  128. //
  129. // For this reason, if the text between consecutive changes spans multiple
  130. // newlines, the token length must be adjusted to the end of the original
  131. // line of the token.
  132. auto NewlinePos = Text.find_first_of('\n');
  133. if (NewlinePos == StringRef::npos) {
  134. Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
  135. PreviousOriginalWhitespaceEndOffset +
  136. Changes[i].PreviousLinePostfix.size() +
  137. Changes[i - 1].CurrentLinePrefix.size();
  138. } else {
  139. Changes[i - 1].TokenLength =
  140. NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
  141. }
  142. // If there are multiple changes in this token, sum up all the changes until
  143. // the end of the line.
  144. if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0)
  145. LastOutsideTokenChange->TokenLength +=
  146. Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
  147. else
  148. LastOutsideTokenChange = &Changes[i - 1];
  149. Changes[i].PreviousEndOfTokenColumn =
  150. Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
  151. Changes[i - 1].IsTrailingComment =
  152. (Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
  153. (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
  154. Changes[i - 1].Tok->is(tok::comment) &&
  155. // FIXME: This is a dirty hack. The problem is that
  156. // BreakableLineCommentSection does comment reflow changes and here is
  157. // the aligning of trailing comments. Consider the case where we reflow
  158. // the second line up in this example:
  159. //
  160. // // line 1
  161. // // line 2
  162. //
  163. // That amounts to 2 changes by BreakableLineCommentSection:
  164. // - the first, delimited by (), for the whitespace between the tokens,
  165. // - and second, delimited by [], for the whitespace at the beginning
  166. // of the second token:
  167. //
  168. // // line 1(
  169. // )[// ]line 2
  170. //
  171. // So in the end we have two changes like this:
  172. //
  173. // // line1()[ ]line 2
  174. //
  175. // Note that the OriginalWhitespaceStart of the second change is the
  176. // same as the PreviousOriginalWhitespaceEnd of the first change.
  177. // In this case, the below check ensures that the second change doesn't
  178. // get treated as a trailing comment change here, since this might
  179. // trigger additional whitespace to be wrongly inserted before "line 2"
  180. // by the comment aligner here.
  181. //
  182. // For a proper solution we need a mechanism to say to WhitespaceManager
  183. // that a particular change breaks the current sequence of trailing
  184. // comments.
  185. OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
  186. }
  187. // FIXME: The last token is currently not always an eof token; in those
  188. // cases, setting TokenLength of the last token to 0 is wrong.
  189. Changes.back().TokenLength = 0;
  190. Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
  191. const WhitespaceManager::Change *LastBlockComment = nullptr;
  192. for (auto &Change : Changes) {
  193. // Reset the IsTrailingComment flag for changes inside of trailing comments
  194. // so they don't get realigned later. Comment line breaks however still need
  195. // to be aligned.
  196. if (Change.IsInsideToken && Change.NewlinesBefore == 0)
  197. Change.IsTrailingComment = false;
  198. Change.StartOfBlockComment = nullptr;
  199. Change.IndentationOffset = 0;
  200. if (Change.Tok->is(tok::comment)) {
  201. if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken)
  202. LastBlockComment = &Change;
  203. else {
  204. if ((Change.StartOfBlockComment = LastBlockComment))
  205. Change.IndentationOffset =
  206. Change.StartOfTokenColumn -
  207. Change.StartOfBlockComment->StartOfTokenColumn;
  208. }
  209. } else {
  210. LastBlockComment = nullptr;
  211. }
  212. }
  213. }
  214. // Align a single sequence of tokens, see AlignTokens below.
  215. template <typename F>
  216. static void
  217. AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches,
  218. SmallVector<WhitespaceManager::Change, 16> &Changes) {
  219. bool FoundMatchOnLine = false;
  220. int Shift = 0;
  221. // ScopeStack keeps track of the current scope depth. It contains indices of
  222. // the first token on each scope.
  223. // We only run the "Matches" function on tokens from the outer-most scope.
  224. // However, we do need to pay special attention to one class of tokens
  225. // that are not in the outer-most scope, and that is function parameters
  226. // which are split across multiple lines, as illustrated by this example:
  227. // double a(int x);
  228. // int b(int y,
  229. // double z);
  230. // In the above example, we need to take special care to ensure that
  231. // 'double z' is indented along with it's owning function 'b'.
  232. SmallVector<unsigned, 16> ScopeStack;
  233. for (unsigned i = Start; i != End; ++i) {
  234. if (ScopeStack.size() != 0 &&
  235. Changes[i].indentAndNestingLevel() <
  236. Changes[ScopeStack.back()].indentAndNestingLevel())
  237. ScopeStack.pop_back();
  238. // Compare current token to previous non-comment token to ensure whether
  239. // it is in a deeper scope or not.
  240. unsigned PreviousNonComment = i - 1;
  241. while (PreviousNonComment > Start &&
  242. Changes[PreviousNonComment].Tok->is(tok::comment))
  243. PreviousNonComment--;
  244. if (i != Start && Changes[i].indentAndNestingLevel() >
  245. Changes[PreviousNonComment].indentAndNestingLevel())
  246. ScopeStack.push_back(i);
  247. bool InsideNestedScope = ScopeStack.size() != 0;
  248. if (Changes[i].NewlinesBefore > 0 && !InsideNestedScope) {
  249. Shift = 0;
  250. FoundMatchOnLine = false;
  251. }
  252. // If this is the first matching token to be aligned, remember by how many
  253. // spaces it has to be shifted, so the rest of the changes on the line are
  254. // shifted by the same amount
  255. if (!FoundMatchOnLine && !InsideNestedScope && Matches(Changes[i])) {
  256. FoundMatchOnLine = true;
  257. Shift = Column - Changes[i].StartOfTokenColumn;
  258. Changes[i].Spaces += Shift;
  259. }
  260. // This is for function parameters that are split across multiple lines,
  261. // as mentioned in the ScopeStack comment.
  262. if (InsideNestedScope && Changes[i].NewlinesBefore > 0) {
  263. unsigned ScopeStart = ScopeStack.back();
  264. if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
  265. (ScopeStart > Start + 1 &&
  266. Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)))
  267. Changes[i].Spaces += Shift;
  268. }
  269. assert(Shift >= 0);
  270. Changes[i].StartOfTokenColumn += Shift;
  271. if (i + 1 != Changes.size())
  272. Changes[i + 1].PreviousEndOfTokenColumn += Shift;
  273. }
  274. }
  275. // Walk through a subset of the changes, starting at StartAt, and find
  276. // sequences of matching tokens to align. To do so, keep track of the lines and
  277. // whether or not a matching token was found on a line. If a matching token is
  278. // found, extend the current sequence. If the current line cannot be part of a
  279. // sequence, e.g. because there is an empty line before it or it contains only
  280. // non-matching tokens, finalize the previous sequence.
  281. // The value returned is the token on which we stopped, either because we
  282. // exhausted all items inside Changes, or because we hit a scope level higher
  283. // than our initial scope.
  284. // This function is recursive. Each invocation processes only the scope level
  285. // equal to the initial level, which is the level of Changes[StartAt].
  286. // If we encounter a scope level greater than the initial level, then we call
  287. // ourselves recursively, thereby avoiding the pollution of the current state
  288. // with the alignment requirements of the nested sub-level. This recursive
  289. // behavior is necessary for aligning function prototypes that have one or more
  290. // arguments.
  291. // If this function encounters a scope level less than the initial level,
  292. // it returns the current position.
  293. // There is a non-obvious subtlety in the recursive behavior: Even though we
  294. // defer processing of nested levels to recursive invocations of this
  295. // function, when it comes time to align a sequence of tokens, we run the
  296. // alignment on the entire sequence, including the nested levels.
  297. // When doing so, most of the nested tokens are skipped, because their
  298. // alignment was already handled by the recursive invocations of this function.
  299. // However, the special exception is that we do NOT skip function parameters
  300. // that are split across multiple lines. See the test case in FormatTest.cpp
  301. // that mentions "split function parameter alignment" for an example of this.
  302. template <typename F>
  303. static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
  304. SmallVector<WhitespaceManager::Change, 16> &Changes,
  305. unsigned StartAt) {
  306. unsigned MinColumn = 0;
  307. unsigned MaxColumn = UINT_MAX;
  308. // Line number of the start and the end of the current token sequence.
  309. unsigned StartOfSequence = 0;
  310. unsigned EndOfSequence = 0;
  311. // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
  312. // abort when we hit any token in a higher scope than the starting one.
  313. auto IndentAndNestingLevel = StartAt < Changes.size()
  314. ? Changes[StartAt].indentAndNestingLevel()
  315. : std::pair<unsigned, unsigned>(0, 0);
  316. // Keep track of the number of commas before the matching tokens, we will only
  317. // align a sequence of matching tokens if they are preceded by the same number
  318. // of commas.
  319. unsigned CommasBeforeLastMatch = 0;
  320. unsigned CommasBeforeMatch = 0;
  321. // Whether a matching token has been found on the current line.
  322. bool FoundMatchOnLine = false;
  323. // Aligns a sequence of matching tokens, on the MinColumn column.
  324. //
  325. // Sequences start from the first matching token to align, and end at the
  326. // first token of the first line that doesn't need to be aligned.
  327. //
  328. // We need to adjust the StartOfTokenColumn of each Change that is on a line
  329. // containing any matching token to be aligned and located after such token.
  330. auto AlignCurrentSequence = [&] {
  331. if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
  332. AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
  333. Changes);
  334. MinColumn = 0;
  335. MaxColumn = UINT_MAX;
  336. StartOfSequence = 0;
  337. EndOfSequence = 0;
  338. };
  339. unsigned i = StartAt;
  340. for (unsigned e = Changes.size(); i != e; ++i) {
  341. if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
  342. break;
  343. if (Changes[i].NewlinesBefore != 0) {
  344. CommasBeforeMatch = 0;
  345. EndOfSequence = i;
  346. // If there is a blank line, or if the last line didn't contain any
  347. // matching token, the sequence ends here.
  348. if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine)
  349. AlignCurrentSequence();
  350. FoundMatchOnLine = false;
  351. }
  352. if (Changes[i].Tok->is(tok::comma)) {
  353. ++CommasBeforeMatch;
  354. } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
  355. // Call AlignTokens recursively, skipping over this scope block.
  356. unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
  357. i = StoppedAt - 1;
  358. continue;
  359. }
  360. if (!Matches(Changes[i]))
  361. continue;
  362. // If there is more than one matching token per line, or if the number of
  363. // preceding commas, do not match anymore, end the sequence.
  364. if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
  365. AlignCurrentSequence();
  366. CommasBeforeLastMatch = CommasBeforeMatch;
  367. FoundMatchOnLine = true;
  368. if (StartOfSequence == 0)
  369. StartOfSequence = i;
  370. unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
  371. int LineLengthAfter = -Changes[i].Spaces;
  372. for (unsigned j = i; j != e && Changes[j].NewlinesBefore == 0; ++j)
  373. LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
  374. unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
  375. // If we are restricted by the maximum column width, end the sequence.
  376. if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
  377. CommasBeforeLastMatch != CommasBeforeMatch) {
  378. AlignCurrentSequence();
  379. StartOfSequence = i;
  380. }
  381. MinColumn = std::max(MinColumn, ChangeMinColumn);
  382. MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
  383. }
  384. EndOfSequence = i;
  385. AlignCurrentSequence();
  386. return i;
  387. }
  388. // Aligns a sequence of matching tokens, on the MinColumn column.
  389. //
  390. // Sequences start from the first matching token to align, and end at the
  391. // first token of the first line that doesn't need to be aligned.
  392. //
  393. // We need to adjust the StartOfTokenColumn of each Change that is on a line
  394. // containing any matching token to be aligned and located after such token.
  395. static void AlignMacroSequence(
  396. unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn,
  397. unsigned &MaxColumn, bool &FoundMatchOnLine,
  398. std::function<bool(const WhitespaceManager::Change &C)> AlignMacrosMatches,
  399. SmallVector<WhitespaceManager::Change, 16> &Changes) {
  400. if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
  401. FoundMatchOnLine = false;
  402. int Shift = 0;
  403. for (unsigned I = StartOfSequence; I != EndOfSequence; ++I) {
  404. if (Changes[I].NewlinesBefore > 0) {
  405. Shift = 0;
  406. FoundMatchOnLine = false;
  407. }
  408. // If this is the first matching token to be aligned, remember by how many
  409. // spaces it has to be shifted, so the rest of the changes on the line are
  410. // shifted by the same amount
  411. if (!FoundMatchOnLine && AlignMacrosMatches(Changes[I])) {
  412. FoundMatchOnLine = true;
  413. Shift = MinColumn - Changes[I].StartOfTokenColumn;
  414. Changes[I].Spaces += Shift;
  415. }
  416. assert(Shift >= 0);
  417. Changes[I].StartOfTokenColumn += Shift;
  418. if (I + 1 != Changes.size())
  419. Changes[I + 1].PreviousEndOfTokenColumn += Shift;
  420. }
  421. }
  422. MinColumn = 0;
  423. MaxColumn = UINT_MAX;
  424. StartOfSequence = 0;
  425. EndOfSequence = 0;
  426. }
  427. void WhitespaceManager::alignConsecutiveMacros() {
  428. if (!Style.AlignConsecutiveMacros)
  429. return;
  430. auto AlignMacrosMatches = [](const Change &C) {
  431. const FormatToken *Current = C.Tok;
  432. unsigned SpacesRequiredBefore = 1;
  433. if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
  434. return false;
  435. Current = Current->Previous;
  436. // If token is a ")", skip over the parameter list, to the
  437. // token that precedes the "("
  438. if (Current->is(tok::r_paren) && Current->MatchingParen) {
  439. Current = Current->MatchingParen->Previous;
  440. SpacesRequiredBefore = 0;
  441. }
  442. if (!Current || !Current->is(tok::identifier))
  443. return false;
  444. if (!Current->Previous || !Current->Previous->is(tok::pp_define))
  445. return false;
  446. // For a macro function, 0 spaces are required between the
  447. // identifier and the lparen that opens the parameter list.
  448. // For a simple macro, 1 space is required between the
  449. // identifier and the first token of the defined value.
  450. return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
  451. };
  452. unsigned MinColumn = 0;
  453. unsigned MaxColumn = UINT_MAX;
  454. // Start and end of the token sequence we're processing.
  455. unsigned StartOfSequence = 0;
  456. unsigned EndOfSequence = 0;
  457. // Whether a matching token has been found on the current line.
  458. bool FoundMatchOnLine = false;
  459. unsigned I = 0;
  460. for (unsigned E = Changes.size(); I != E; ++I) {
  461. if (Changes[I].NewlinesBefore != 0) {
  462. EndOfSequence = I;
  463. // If there is a blank line, or if the last line didn't contain any
  464. // matching token, the sequence ends here.
  465. if (Changes[I].NewlinesBefore > 1 || !FoundMatchOnLine)
  466. AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
  467. FoundMatchOnLine, AlignMacrosMatches, Changes);
  468. FoundMatchOnLine = false;
  469. }
  470. if (!AlignMacrosMatches(Changes[I]))
  471. continue;
  472. FoundMatchOnLine = true;
  473. if (StartOfSequence == 0)
  474. StartOfSequence = I;
  475. unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
  476. int LineLengthAfter = -Changes[I].Spaces;
  477. for (unsigned j = I; j != E && Changes[j].NewlinesBefore == 0; ++j)
  478. LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
  479. unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
  480. MinColumn = std::max(MinColumn, ChangeMinColumn);
  481. MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
  482. }
  483. EndOfSequence = I;
  484. AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
  485. FoundMatchOnLine, AlignMacrosMatches, Changes);
  486. }
  487. void WhitespaceManager::alignConsecutiveAssignments() {
  488. if (!Style.AlignConsecutiveAssignments)
  489. return;
  490. AlignTokens(
  491. Style,
  492. [&](const Change &C) {
  493. // Do not align on equal signs that are first on a line.
  494. if (C.NewlinesBefore > 0)
  495. return false;
  496. // Do not align on equal signs that are last on a line.
  497. if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
  498. return false;
  499. return C.Tok->is(tok::equal);
  500. },
  501. Changes, /*StartAt=*/0);
  502. }
  503. void WhitespaceManager::alignConsecutiveDeclarations() {
  504. if (!Style.AlignConsecutiveDeclarations)
  505. return;
  506. // FIXME: Currently we don't handle properly the PointerAlignment: Right
  507. // The * and & are not aligned and are left dangling. Something has to be done
  508. // about it, but it raises the question of alignment of code like:
  509. // const char* const* v1;
  510. // float const* v2;
  511. // SomeVeryLongType const& v3;
  512. AlignTokens(
  513. Style,
  514. [](Change const &C) {
  515. // tok::kw_operator is necessary for aligning operator overload
  516. // definitions.
  517. if (C.Tok->isOneOf(TT_FunctionDeclarationName, tok::kw_operator))
  518. return true;
  519. if (C.Tok->isNot(TT_StartOfName))
  520. return false;
  521. // Check if there is a subsequent name that starts the same declaration.
  522. for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
  523. if (Next->is(tok::comment))
  524. continue;
  525. if (!Next->Tok.getIdentifierInfo())
  526. break;
  527. if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
  528. tok::kw_operator))
  529. return false;
  530. }
  531. return true;
  532. },
  533. Changes, /*StartAt=*/0);
  534. }
  535. void WhitespaceManager::alignTrailingComments() {
  536. unsigned MinColumn = 0;
  537. unsigned MaxColumn = UINT_MAX;
  538. unsigned StartOfSequence = 0;
  539. bool BreakBeforeNext = false;
  540. unsigned Newlines = 0;
  541. for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
  542. if (Changes[i].StartOfBlockComment)
  543. continue;
  544. Newlines += Changes[i].NewlinesBefore;
  545. if (!Changes[i].IsTrailingComment)
  546. continue;
  547. unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
  548. unsigned ChangeMaxColumn;
  549. if (Style.ColumnLimit == 0)
  550. ChangeMaxColumn = UINT_MAX;
  551. else if (Style.ColumnLimit >= Changes[i].TokenLength)
  552. ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
  553. else
  554. ChangeMaxColumn = ChangeMinColumn;
  555. // If we don't create a replacement for this change, we have to consider
  556. // it to be immovable.
  557. if (!Changes[i].CreateReplacement)
  558. ChangeMaxColumn = ChangeMinColumn;
  559. if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
  560. ChangeMaxColumn -= 2;
  561. // If this comment follows an } in column 0, it probably documents the
  562. // closing of a namespace and we don't want to align it.
  563. bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
  564. Changes[i - 1].Tok->is(tok::r_brace) &&
  565. Changes[i - 1].StartOfTokenColumn == 0;
  566. bool WasAlignedWithStartOfNextLine = false;
  567. if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
  568. unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
  569. Changes[i].OriginalWhitespaceRange.getEnd());
  570. for (unsigned j = i + 1; j != e; ++j) {
  571. if (Changes[j].Tok->is(tok::comment))
  572. continue;
  573. unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
  574. Changes[j].OriginalWhitespaceRange.getEnd());
  575. // The start of the next token was previously aligned with the
  576. // start of this comment.
  577. WasAlignedWithStartOfNextLine =
  578. CommentColumn == NextColumn ||
  579. CommentColumn == NextColumn + Style.IndentWidth;
  580. break;
  581. }
  582. }
  583. if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
  584. alignTrailingComments(StartOfSequence, i, MinColumn);
  585. MinColumn = ChangeMinColumn;
  586. MaxColumn = ChangeMinColumn;
  587. StartOfSequence = i;
  588. } else if (BreakBeforeNext || Newlines > 1 ||
  589. (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
  590. // Break the comment sequence if the previous line did not end
  591. // in a trailing comment.
  592. (Changes[i].NewlinesBefore == 1 && i > 0 &&
  593. !Changes[i - 1].IsTrailingComment) ||
  594. WasAlignedWithStartOfNextLine) {
  595. alignTrailingComments(StartOfSequence, i, MinColumn);
  596. MinColumn = ChangeMinColumn;
  597. MaxColumn = ChangeMaxColumn;
  598. StartOfSequence = i;
  599. } else {
  600. MinColumn = std::max(MinColumn, ChangeMinColumn);
  601. MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
  602. }
  603. BreakBeforeNext = (i == 0) || (Changes[i].NewlinesBefore > 1) ||
  604. // Never start a sequence with a comment at the beginning
  605. // of the line.
  606. (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
  607. Newlines = 0;
  608. }
  609. alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
  610. }
  611. void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
  612. unsigned Column) {
  613. for (unsigned i = Start; i != End; ++i) {
  614. int Shift = 0;
  615. if (Changes[i].IsTrailingComment) {
  616. Shift = Column - Changes[i].StartOfTokenColumn;
  617. }
  618. if (Changes[i].StartOfBlockComment) {
  619. Shift = Changes[i].IndentationOffset +
  620. Changes[i].StartOfBlockComment->StartOfTokenColumn -
  621. Changes[i].StartOfTokenColumn;
  622. }
  623. assert(Shift >= 0);
  624. Changes[i].Spaces += Shift;
  625. if (i + 1 != Changes.size())
  626. Changes[i + 1].PreviousEndOfTokenColumn += Shift;
  627. Changes[i].StartOfTokenColumn += Shift;
  628. }
  629. }
  630. void WhitespaceManager::alignEscapedNewlines() {
  631. if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
  632. return;
  633. bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
  634. unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
  635. unsigned StartOfMacro = 0;
  636. for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
  637. Change &C = Changes[i];
  638. if (C.NewlinesBefore > 0) {
  639. if (C.ContinuesPPDirective) {
  640. MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
  641. } else {
  642. alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
  643. MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
  644. StartOfMacro = i;
  645. }
  646. }
  647. }
  648. alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
  649. }
  650. void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
  651. unsigned Column) {
  652. for (unsigned i = Start; i < End; ++i) {
  653. Change &C = Changes[i];
  654. if (C.NewlinesBefore > 0) {
  655. assert(C.ContinuesPPDirective);
  656. if (C.PreviousEndOfTokenColumn + 1 > Column)
  657. C.EscapedNewlineColumn = 0;
  658. else
  659. C.EscapedNewlineColumn = Column;
  660. }
  661. }
  662. }
  663. void WhitespaceManager::generateChanges() {
  664. for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
  665. const Change &C = Changes[i];
  666. if (i > 0) {
  667. assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() !=
  668. C.OriginalWhitespaceRange.getBegin() &&
  669. "Generating two replacements for the same location");
  670. }
  671. if (C.CreateReplacement) {
  672. std::string ReplacementText = C.PreviousLinePostfix;
  673. if (C.ContinuesPPDirective)
  674. appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
  675. C.PreviousEndOfTokenColumn,
  676. C.EscapedNewlineColumn);
  677. else
  678. appendNewlineText(ReplacementText, C.NewlinesBefore);
  679. appendIndentText(ReplacementText, C.Tok->IndentLevel,
  680. std::max(0, C.Spaces),
  681. C.StartOfTokenColumn - std::max(0, C.Spaces));
  682. ReplacementText.append(C.CurrentLinePrefix);
  683. storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
  684. }
  685. }
  686. }
  687. void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
  688. unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
  689. SourceMgr.getFileOffset(Range.getBegin());
  690. // Don't create a replacement, if it does not change anything.
  691. if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
  692. WhitespaceLength) == Text)
  693. return;
  694. auto Err = Replaces.add(tooling::Replacement(
  695. SourceMgr, CharSourceRange::getCharRange(Range), Text));
  696. // FIXME: better error handling. For now, just print an error message in the
  697. // release version.
  698. if (Err) {
  699. llvm::errs() << llvm::toString(std::move(Err)) << "\n";
  700. assert(false);
  701. }
  702. }
  703. void WhitespaceManager::appendNewlineText(std::string &Text,
  704. unsigned Newlines) {
  705. for (unsigned i = 0; i < Newlines; ++i)
  706. Text.append(UseCRLF ? "\r\n" : "\n");
  707. }
  708. void WhitespaceManager::appendEscapedNewlineText(
  709. std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
  710. unsigned EscapedNewlineColumn) {
  711. if (Newlines > 0) {
  712. unsigned Spaces =
  713. std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
  714. for (unsigned i = 0; i < Newlines; ++i) {
  715. Text.append(Spaces, ' ');
  716. Text.append(UseCRLF ? "\\\r\n" : "\\\n");
  717. Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
  718. }
  719. }
  720. }
  721. void WhitespaceManager::appendIndentText(std::string &Text,
  722. unsigned IndentLevel, unsigned Spaces,
  723. unsigned WhitespaceStartColumn) {
  724. switch (Style.UseTab) {
  725. case FormatStyle::UT_Never:
  726. Text.append(Spaces, ' ');
  727. break;
  728. case FormatStyle::UT_Always: {
  729. unsigned FirstTabWidth =
  730. Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
  731. // Insert only spaces when we want to end up before the next tab.
  732. if (Spaces < FirstTabWidth || Spaces == 1) {
  733. Text.append(Spaces, ' ');
  734. break;
  735. }
  736. // Align to the next tab.
  737. Spaces -= FirstTabWidth;
  738. Text.append("\t");
  739. Text.append(Spaces / Style.TabWidth, '\t');
  740. Text.append(Spaces % Style.TabWidth, ' ');
  741. break;
  742. }
  743. case FormatStyle::UT_ForIndentation:
  744. if (WhitespaceStartColumn == 0) {
  745. unsigned Indentation = IndentLevel * Style.IndentWidth;
  746. // This happens, e.g. when a line in a block comment is indented less than
  747. // the first one.
  748. if (Indentation > Spaces)
  749. Indentation = Spaces;
  750. unsigned Tabs = Indentation / Style.TabWidth;
  751. Text.append(Tabs, '\t');
  752. Spaces -= Tabs * Style.TabWidth;
  753. }
  754. Text.append(Spaces, ' ');
  755. break;
  756. case FormatStyle::UT_ForContinuationAndIndentation:
  757. if (WhitespaceStartColumn == 0) {
  758. unsigned Tabs = Spaces / Style.TabWidth;
  759. Text.append(Tabs, '\t');
  760. Spaces -= Tabs * Style.TabWidth;
  761. }
  762. Text.append(Spaces, ' ');
  763. break;
  764. }
  765. }
  766. } // namespace format
  767. } // namespace clang