WhitespaceManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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(
  25. bool CreateReplacement, const SourceRange &OriginalWhitespaceRange,
  26. unsigned IndentLevel, int Spaces, unsigned StartOfTokenColumn,
  27. unsigned NewlinesBefore, StringRef PreviousLinePostfix,
  28. StringRef CurrentLinePrefix, tok::TokenKind Kind, bool ContinuesPPDirective)
  29. : CreateReplacement(CreateReplacement),
  30. OriginalWhitespaceRange(OriginalWhitespaceRange),
  31. StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
  32. PreviousLinePostfix(PreviousLinePostfix),
  33. CurrentLinePrefix(CurrentLinePrefix), Kind(Kind),
  34. ContinuesPPDirective(ContinuesPPDirective), IndentLevel(IndentLevel),
  35. Spaces(Spaces) {}
  36. void WhitespaceManager::reset() {
  37. Changes.clear();
  38. Replaces.clear();
  39. }
  40. void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
  41. unsigned IndentLevel, 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(true, Tok.WhitespaceRange, IndentLevel, Spaces,
  48. StartOfTokenColumn, Newlines, "", "",
  49. Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst));
  50. }
  51. void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
  52. bool InPPDirective) {
  53. if (Tok.Finalized)
  54. return;
  55. Changes.push_back(Change(false, Tok.WhitespaceRange, /*IndentLevel=*/0,
  56. /*Spaces=*/0, Tok.OriginalColumn, Tok.NewlinesBefore,
  57. "", "", Tok.Tok.getKind(),
  58. InPPDirective && !Tok.IsFirst));
  59. }
  60. void WhitespaceManager::replaceWhitespaceInToken(
  61. const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
  62. StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
  63. unsigned Newlines, unsigned IndentLevel, int Spaces) {
  64. if (Tok.Finalized)
  65. return;
  66. SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
  67. Changes.push_back(Change(
  68. true, SourceRange(Start, Start.getLocWithOffset(ReplaceChars)),
  69. IndentLevel, Spaces, std::max(0, Spaces), Newlines, PreviousPostfix,
  70. CurrentPrefix,
  71. // If we don't add a newline this change doesn't start a comment. Thus,
  72. // when we align line comments, we don't need to treat this change as one.
  73. // FIXME: We still need to take this change in account to properly
  74. // calculate the new length of the comment and to calculate the changes
  75. // for which to do the alignment when aligning comments.
  76. Tok.is(TT_LineComment) && Newlines > 0 ? tok::comment : tok::unknown,
  77. InPPDirective && !Tok.IsFirst));
  78. }
  79. const tooling::Replacements &WhitespaceManager::generateReplacements() {
  80. if (Changes.empty())
  81. return Replaces;
  82. std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr));
  83. calculateLineBreakInformation();
  84. alignTrailingComments();
  85. alignEscapedNewlines();
  86. generateChanges();
  87. return Replaces;
  88. }
  89. void WhitespaceManager::calculateLineBreakInformation() {
  90. Changes[0].PreviousEndOfTokenColumn = 0;
  91. for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
  92. unsigned OriginalWhitespaceStart =
  93. SourceMgr.getFileOffset(Changes[i].OriginalWhitespaceRange.getBegin());
  94. unsigned PreviousOriginalWhitespaceEnd = SourceMgr.getFileOffset(
  95. Changes[i - 1].OriginalWhitespaceRange.getEnd());
  96. Changes[i - 1].TokenLength = OriginalWhitespaceStart -
  97. PreviousOriginalWhitespaceEnd +
  98. Changes[i].PreviousLinePostfix.size() +
  99. Changes[i - 1].CurrentLinePrefix.size();
  100. Changes[i].PreviousEndOfTokenColumn =
  101. Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
  102. Changes[i - 1].IsTrailingComment =
  103. (Changes[i].NewlinesBefore > 0 || Changes[i].Kind == tok::eof) &&
  104. Changes[i - 1].Kind == tok::comment;
  105. }
  106. // FIXME: The last token is currently not always an eof token; in those
  107. // cases, setting TokenLength of the last token to 0 is wrong.
  108. Changes.back().TokenLength = 0;
  109. Changes.back().IsTrailingComment = Changes.back().Kind == tok::comment;
  110. const WhitespaceManager::Change *LastBlockComment = nullptr;
  111. for (auto &Change : Changes) {
  112. Change.StartOfBlockComment = nullptr;
  113. Change.IndentationOffset = 0;
  114. if (Change.Kind == tok::comment) {
  115. LastBlockComment = &Change;
  116. } else if (Change.Kind == tok::unknown) {
  117. if ((Change.StartOfBlockComment = LastBlockComment))
  118. Change.IndentationOffset =
  119. Change.StartOfTokenColumn -
  120. Change.StartOfBlockComment->StartOfTokenColumn;
  121. } else {
  122. LastBlockComment = nullptr;
  123. }
  124. }
  125. }
  126. void WhitespaceManager::alignTrailingComments() {
  127. unsigned MinColumn = 0;
  128. unsigned MaxColumn = UINT_MAX;
  129. unsigned StartOfSequence = 0;
  130. bool BreakBeforeNext = false;
  131. unsigned Newlines = 0;
  132. for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
  133. if (Changes[i].StartOfBlockComment)
  134. continue;
  135. Newlines += Changes[i].NewlinesBefore;
  136. if (!Changes[i].IsTrailingComment)
  137. continue;
  138. unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
  139. unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
  140. if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
  141. ChangeMaxColumn -= 2;
  142. // If this comment follows an } in column 0, it probably documents the
  143. // closing of a namespace and we don't want to align it.
  144. bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
  145. Changes[i - 1].Kind == tok::r_brace &&
  146. Changes[i - 1].StartOfTokenColumn == 0;
  147. bool WasAlignedWithStartOfNextLine = false;
  148. if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
  149. unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
  150. Changes[i].OriginalWhitespaceRange.getEnd());
  151. for (unsigned j = i + 1; j != e; ++j) {
  152. if (Changes[j].Kind != tok::comment) { // Skip over comments.
  153. unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
  154. Changes[j].OriginalWhitespaceRange.getEnd());
  155. // The start of the next token was previously aligned with the
  156. // start of this comment.
  157. WasAlignedWithStartOfNextLine =
  158. CommentColumn == NextColumn ||
  159. CommentColumn == NextColumn + Style.IndentWidth;
  160. break;
  161. }
  162. }
  163. }
  164. if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
  165. alignTrailingComments(StartOfSequence, i, MinColumn);
  166. MinColumn = ChangeMinColumn;
  167. MaxColumn = ChangeMinColumn;
  168. StartOfSequence = i;
  169. } else if (BreakBeforeNext || Newlines > 1 ||
  170. (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
  171. // Break the comment sequence if the previous line did not end
  172. // in a trailing comment.
  173. (Changes[i].NewlinesBefore == 1 && i > 0 &&
  174. !Changes[i - 1].IsTrailingComment) ||
  175. WasAlignedWithStartOfNextLine) {
  176. alignTrailingComments(StartOfSequence, i, MinColumn);
  177. MinColumn = ChangeMinColumn;
  178. MaxColumn = ChangeMaxColumn;
  179. StartOfSequence = i;
  180. } else {
  181. MinColumn = std::max(MinColumn, ChangeMinColumn);
  182. MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
  183. }
  184. BreakBeforeNext =
  185. (i == 0) || (Changes[i].NewlinesBefore > 1) ||
  186. // Never start a sequence with a comment at the beginning of
  187. // the line.
  188. (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
  189. Newlines = 0;
  190. }
  191. alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
  192. }
  193. void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
  194. unsigned Column) {
  195. for (unsigned i = Start; i != End; ++i) {
  196. int Shift = 0;
  197. if (Changes[i].IsTrailingComment) {
  198. Shift = Column - Changes[i].StartOfTokenColumn;
  199. }
  200. if (Changes[i].StartOfBlockComment) {
  201. Shift = Changes[i].IndentationOffset +
  202. Changes[i].StartOfBlockComment->StartOfTokenColumn -
  203. Changes[i].StartOfTokenColumn;
  204. }
  205. assert(Shift >= 0);
  206. Changes[i].Spaces += Shift;
  207. if (i + 1 != End)
  208. Changes[i + 1].PreviousEndOfTokenColumn += Shift;
  209. Changes[i].StartOfTokenColumn += Shift;
  210. }
  211. }
  212. void WhitespaceManager::alignEscapedNewlines() {
  213. unsigned MaxEndOfLine =
  214. Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
  215. unsigned StartOfMacro = 0;
  216. Changes[0].EscapedNewlineColumn = 0;
  217. for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
  218. Change &C = Changes[i];
  219. if (C.NewlinesBefore > 0) {
  220. if (C.ContinuesPPDirective) {
  221. MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
  222. } else {
  223. alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
  224. MaxEndOfLine = Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
  225. StartOfMacro = i;
  226. }
  227. }
  228. }
  229. alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
  230. }
  231. void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
  232. unsigned Column) {
  233. for (unsigned i = Start; i < End; ++i) {
  234. Change &C = Changes[i];
  235. if (C.NewlinesBefore > 0) {
  236. assert(C.ContinuesPPDirective);
  237. if (C.PreviousEndOfTokenColumn + 1 > Column)
  238. C.EscapedNewlineColumn = 0;
  239. else
  240. C.EscapedNewlineColumn = Column;
  241. }
  242. }
  243. }
  244. void WhitespaceManager::generateChanges() {
  245. for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
  246. const Change &C = Changes[i];
  247. if (i > 0) {
  248. assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() !=
  249. C.OriginalWhitespaceRange.getBegin() &&
  250. "Generating two replacements for the same location");
  251. }
  252. if (C.CreateReplacement) {
  253. std::string ReplacementText = C.PreviousLinePostfix;
  254. if (C.ContinuesPPDirective)
  255. appendNewlineText(ReplacementText, C.NewlinesBefore,
  256. C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn);
  257. else
  258. appendNewlineText(ReplacementText, C.NewlinesBefore);
  259. appendIndentText(ReplacementText, C.IndentLevel, std::max(0, C.Spaces),
  260. C.StartOfTokenColumn - std::max(0, C.Spaces));
  261. ReplacementText.append(C.CurrentLinePrefix);
  262. storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
  263. }
  264. }
  265. }
  266. void WhitespaceManager::storeReplacement(const SourceRange &Range,
  267. StringRef Text) {
  268. unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
  269. SourceMgr.getFileOffset(Range.getBegin());
  270. // Don't create a replacement, if it does not change anything.
  271. if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
  272. WhitespaceLength) == Text)
  273. return;
  274. Replaces.insert(tooling::Replacement(
  275. SourceMgr, CharSourceRange::getCharRange(Range), Text));
  276. }
  277. void WhitespaceManager::appendNewlineText(std::string &Text,
  278. unsigned Newlines) {
  279. for (unsigned i = 0; i < Newlines; ++i)
  280. Text.append(UseCRLF ? "\r\n" : "\n");
  281. }
  282. void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines,
  283. unsigned PreviousEndOfTokenColumn,
  284. unsigned EscapedNewlineColumn) {
  285. if (Newlines > 0) {
  286. unsigned Offset =
  287. std::min<int>(EscapedNewlineColumn - 1, PreviousEndOfTokenColumn);
  288. for (unsigned i = 0; i < Newlines; ++i) {
  289. Text.append(std::string(EscapedNewlineColumn - Offset - 1, ' '));
  290. Text.append(UseCRLF ? "\\\r\n" : "\\\n");
  291. Offset = 0;
  292. }
  293. }
  294. }
  295. void WhitespaceManager::appendIndentText(std::string &Text,
  296. unsigned IndentLevel, unsigned Spaces,
  297. unsigned WhitespaceStartColumn) {
  298. switch (Style.UseTab) {
  299. case FormatStyle::UT_Never:
  300. Text.append(std::string(Spaces, ' '));
  301. break;
  302. case FormatStyle::UT_Always: {
  303. unsigned FirstTabWidth =
  304. Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
  305. // Indent with tabs only when there's at least one full tab.
  306. if (FirstTabWidth + Style.TabWidth <= Spaces) {
  307. Spaces -= FirstTabWidth;
  308. Text.append("\t");
  309. }
  310. Text.append(std::string(Spaces / Style.TabWidth, '\t'));
  311. Text.append(std::string(Spaces % Style.TabWidth, ' '));
  312. break;
  313. }
  314. case FormatStyle::UT_ForIndentation:
  315. if (WhitespaceStartColumn == 0) {
  316. unsigned Indentation = IndentLevel * Style.IndentWidth;
  317. // This happens, e.g. when a line in a block comment is indented less than
  318. // the first one.
  319. if (Indentation > Spaces)
  320. Indentation = Spaces;
  321. unsigned Tabs = Indentation / Style.TabWidth;
  322. Text.append(std::string(Tabs, '\t'));
  323. Spaces -= Tabs * Style.TabWidth;
  324. }
  325. Text.append(std::string(Spaces, ' '));
  326. break;
  327. }
  328. }
  329. } // namespace format
  330. } // namespace clang