WhitespaceManager.cpp 28 KB

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