WhitespaceManager.cpp 13 KB

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