UnwrappedLineFormatter.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. //===--- UnwrappedLineFormatter.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. #include "UnwrappedLineFormatter.h"
  9. #include "NamespaceEndCommentsFixer.h"
  10. #include "WhitespaceManager.h"
  11. #include "llvm/Support/Debug.h"
  12. #include <queue>
  13. #define DEBUG_TYPE "format-formatter"
  14. namespace clang {
  15. namespace format {
  16. namespace {
  17. bool startsExternCBlock(const AnnotatedLine &Line) {
  18. const FormatToken *Next = Line.First->getNextNonComment();
  19. const FormatToken *NextNext = Next ? Next->getNextNonComment() : nullptr;
  20. return Line.startsWith(tok::kw_extern) && Next && Next->isStringLiteral() &&
  21. NextNext && NextNext->is(tok::l_brace);
  22. }
  23. /// Tracks the indent level of \c AnnotatedLines across levels.
  24. ///
  25. /// \c nextLine must be called for each \c AnnotatedLine, after which \c
  26. /// getIndent() will return the indent for the last line \c nextLine was called
  27. /// with.
  28. /// If the line is not formatted (and thus the indent does not change), calling
  29. /// \c adjustToUnmodifiedLine after the call to \c nextLine will cause
  30. /// subsequent lines on the same level to be indented at the same level as the
  31. /// given line.
  32. class LevelIndentTracker {
  33. public:
  34. LevelIndentTracker(const FormatStyle &Style,
  35. const AdditionalKeywords &Keywords, unsigned StartLevel,
  36. int AdditionalIndent)
  37. : Style(Style), Keywords(Keywords), AdditionalIndent(AdditionalIndent) {
  38. for (unsigned i = 0; i != StartLevel; ++i)
  39. IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent);
  40. }
  41. /// Returns the indent for the current line.
  42. unsigned getIndent() const { return Indent; }
  43. /// Update the indent state given that \p Line is going to be formatted
  44. /// next.
  45. void nextLine(const AnnotatedLine &Line) {
  46. Offset = getIndentOffset(*Line.First);
  47. // Update the indent level cache size so that we can rely on it
  48. // having the right size in adjustToUnmodifiedline.
  49. while (IndentForLevel.size() <= Line.Level)
  50. IndentForLevel.push_back(-1);
  51. if (Line.InPPDirective) {
  52. Indent = Line.Level * Style.IndentWidth + AdditionalIndent;
  53. } else {
  54. IndentForLevel.resize(Line.Level + 1);
  55. Indent = getIndent(IndentForLevel, Line.Level);
  56. }
  57. if (static_cast<int>(Indent) + Offset >= 0)
  58. Indent += Offset;
  59. }
  60. /// Update the indent state given that \p Line indent should be
  61. /// skipped.
  62. void skipLine(const AnnotatedLine &Line) {
  63. while (IndentForLevel.size() <= Line.Level)
  64. IndentForLevel.push_back(Indent);
  65. }
  66. /// Update the level indent to adapt to the given \p Line.
  67. ///
  68. /// When a line is not formatted, we move the subsequent lines on the same
  69. /// level to the same indent.
  70. /// Note that \c nextLine must have been called before this method.
  71. void adjustToUnmodifiedLine(const AnnotatedLine &Line) {
  72. unsigned LevelIndent = Line.First->OriginalColumn;
  73. if (static_cast<int>(LevelIndent) - Offset >= 0)
  74. LevelIndent -= Offset;
  75. if ((!Line.First->is(tok::comment) || IndentForLevel[Line.Level] == -1) &&
  76. !Line.InPPDirective)
  77. IndentForLevel[Line.Level] = LevelIndent;
  78. }
  79. private:
  80. /// Get the offset of the line relatively to the level.
  81. ///
  82. /// For example, 'public:' labels in classes are offset by 1 or 2
  83. /// characters to the left from their level.
  84. int getIndentOffset(const FormatToken &RootToken) {
  85. if (Style.Language == FormatStyle::LK_Java ||
  86. Style.Language == FormatStyle::LK_JavaScript)
  87. return 0;
  88. if (RootToken.isAccessSpecifier(false) ||
  89. RootToken.isObjCAccessSpecifier() ||
  90. (RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&
  91. RootToken.Next && RootToken.Next->is(tok::colon)))
  92. return Style.AccessModifierOffset;
  93. return 0;
  94. }
  95. /// Get the indent of \p Level from \p IndentForLevel.
  96. ///
  97. /// \p IndentForLevel must contain the indent for the level \c l
  98. /// at \p IndentForLevel[l], or a value < 0 if the indent for
  99. /// that level is unknown.
  100. unsigned getIndent(ArrayRef<int> IndentForLevel, unsigned Level) {
  101. if (IndentForLevel[Level] != -1)
  102. return IndentForLevel[Level];
  103. if (Level == 0)
  104. return 0;
  105. return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
  106. }
  107. const FormatStyle &Style;
  108. const AdditionalKeywords &Keywords;
  109. const unsigned AdditionalIndent;
  110. /// The indent in characters for each level.
  111. std::vector<int> IndentForLevel;
  112. /// Offset of the current line relative to the indent level.
  113. ///
  114. /// For example, the 'public' keywords is often indented with a negative
  115. /// offset.
  116. int Offset = 0;
  117. /// The current line's indent.
  118. unsigned Indent = 0;
  119. };
  120. bool isNamespaceDeclaration(const AnnotatedLine *Line) {
  121. const FormatToken *NamespaceTok = Line->First;
  122. return NamespaceTok && NamespaceTok->getNamespaceToken();
  123. }
  124. bool isEndOfNamespace(const AnnotatedLine *Line,
  125. const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
  126. if (!Line->startsWith(tok::r_brace))
  127. return false;
  128. size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex;
  129. if (StartLineIndex == UnwrappedLine::kInvalidIndex)
  130. return false;
  131. assert(StartLineIndex < AnnotatedLines.size());
  132. return isNamespaceDeclaration(AnnotatedLines[StartLineIndex]);
  133. }
  134. class LineJoiner {
  135. public:
  136. LineJoiner(const FormatStyle &Style, const AdditionalKeywords &Keywords,
  137. const SmallVectorImpl<AnnotatedLine *> &Lines)
  138. : Style(Style), Keywords(Keywords), End(Lines.end()), Next(Lines.begin()),
  139. AnnotatedLines(Lines) {}
  140. /// Returns the next line, merging multiple lines into one if possible.
  141. const AnnotatedLine *getNextMergedLine(bool DryRun,
  142. LevelIndentTracker &IndentTracker) {
  143. if (Next == End)
  144. return nullptr;
  145. const AnnotatedLine *Current = *Next;
  146. IndentTracker.nextLine(*Current);
  147. unsigned MergedLines = tryFitMultipleLinesInOne(IndentTracker, Next, End);
  148. if (MergedLines > 0 && Style.ColumnLimit == 0)
  149. // Disallow line merging if there is a break at the start of one of the
  150. // input lines.
  151. for (unsigned i = 0; i < MergedLines; ++i)
  152. if (Next[i + 1]->First->NewlinesBefore > 0)
  153. MergedLines = 0;
  154. if (!DryRun)
  155. for (unsigned i = 0; i < MergedLines; ++i)
  156. join(*Next[0], *Next[i + 1]);
  157. Next = Next + MergedLines + 1;
  158. return Current;
  159. }
  160. private:
  161. /// Calculates how many lines can be merged into 1 starting at \p I.
  162. unsigned
  163. tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker,
  164. SmallVectorImpl<AnnotatedLine *>::const_iterator I,
  165. SmallVectorImpl<AnnotatedLine *>::const_iterator E) {
  166. const unsigned Indent = IndentTracker.getIndent();
  167. // Can't join the last line with anything.
  168. if (I + 1 == E)
  169. return 0;
  170. // We can never merge stuff if there are trailing line comments.
  171. const AnnotatedLine *TheLine = *I;
  172. if (TheLine->Last->is(TT_LineComment))
  173. return 0;
  174. if (I[1]->Type == LT_Invalid || I[1]->First->MustBreakBefore)
  175. return 0;
  176. if (TheLine->InPPDirective &&
  177. (!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline))
  178. return 0;
  179. if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
  180. return 0;
  181. unsigned Limit =
  182. Style.ColumnLimit == 0 ? UINT_MAX : Style.ColumnLimit - Indent;
  183. // If we already exceed the column limit, we set 'Limit' to 0. The different
  184. // tryMerge..() functions can then decide whether to still do merging.
  185. Limit = TheLine->Last->TotalLength > Limit
  186. ? 0
  187. : Limit - TheLine->Last->TotalLength;
  188. if (TheLine->Last->is(TT_FunctionLBrace) &&
  189. TheLine->First == TheLine->Last &&
  190. !Style.BraceWrapping.SplitEmptyFunction &&
  191. I[1]->First->is(tok::r_brace))
  192. return tryMergeSimpleBlock(I, E, Limit);
  193. // Handle empty record blocks where the brace has already been wrapped
  194. if (TheLine->Last->is(tok::l_brace) && TheLine->First == TheLine->Last &&
  195. I != AnnotatedLines.begin()) {
  196. bool EmptyBlock = I[1]->First->is(tok::r_brace);
  197. const FormatToken *Tok = I[-1]->First;
  198. if (Tok && Tok->is(tok::comment))
  199. Tok = Tok->getNextNonComment();
  200. if (Tok && Tok->getNamespaceToken())
  201. return !Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock
  202. ? tryMergeSimpleBlock(I, E, Limit)
  203. : 0;
  204. if (Tok && Tok->is(tok::kw_typedef))
  205. Tok = Tok->getNextNonComment();
  206. if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,
  207. tok::kw_extern, Keywords.kw_interface))
  208. return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
  209. ? tryMergeSimpleBlock(I, E, Limit)
  210. : 0;
  211. }
  212. // FIXME: TheLine->Level != 0 might or might not be the right check to do.
  213. // If necessary, change to something smarter.
  214. bool MergeShortFunctions =
  215. Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All ||
  216. (Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&
  217. I[1]->First->is(tok::r_brace)) ||
  218. (Style.AllowShortFunctionsOnASingleLine & FormatStyle::SFS_InlineOnly &&
  219. TheLine->Level != 0);
  220. if (Style.CompactNamespaces) {
  221. if (isNamespaceDeclaration(TheLine)) {
  222. int i = 0;
  223. unsigned closingLine = TheLine->MatchingClosingBlockLineIndex - 1;
  224. for (; I + 1 + i != E && isNamespaceDeclaration(I[i + 1]) &&
  225. closingLine == I[i + 1]->MatchingClosingBlockLineIndex &&
  226. I[i + 1]->Last->TotalLength < Limit;
  227. i++, closingLine--) {
  228. // No extra indent for compacted namespaces
  229. IndentTracker.skipLine(*I[i + 1]);
  230. Limit -= I[i + 1]->Last->TotalLength;
  231. }
  232. return i;
  233. }
  234. if (isEndOfNamespace(TheLine, AnnotatedLines)) {
  235. int i = 0;
  236. unsigned openingLine = TheLine->MatchingOpeningBlockLineIndex - 1;
  237. for (; I + 1 + i != E && isEndOfNamespace(I[i + 1], AnnotatedLines) &&
  238. openingLine == I[i + 1]->MatchingOpeningBlockLineIndex;
  239. i++, openingLine--) {
  240. // No space between consecutive braces
  241. I[i + 1]->First->SpacesRequiredBefore = !I[i]->Last->is(tok::r_brace);
  242. // Indent like the outer-most namespace
  243. IndentTracker.nextLine(*I[i + 1]);
  244. }
  245. return i;
  246. }
  247. }
  248. // Try to merge a function block with left brace unwrapped
  249. if (TheLine->Last->is(TT_FunctionLBrace) &&
  250. TheLine->First != TheLine->Last) {
  251. return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
  252. }
  253. // Try to merge a control statement block with left brace unwrapped
  254. if (TheLine->Last->is(tok::l_brace) && TheLine->First != TheLine->Last &&
  255. TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) {
  256. return Style.AllowShortBlocksOnASingleLine
  257. ? tryMergeSimpleBlock(I, E, Limit)
  258. : 0;
  259. }
  260. // Try to merge a control statement block with left brace wrapped
  261. if (I[1]->First->is(tok::l_brace) &&
  262. TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) {
  263. return Style.BraceWrapping.AfterControlStatement
  264. ? tryMergeSimpleBlock(I, E, Limit)
  265. : 0;
  266. }
  267. // Try to merge either empty or one-line block if is precedeed by control
  268. // statement token
  269. if (TheLine->First->is(tok::l_brace) && TheLine->First == TheLine->Last &&
  270. I != AnnotatedLines.begin() &&
  271. I[-1]->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) {
  272. unsigned MergedLines = 0;
  273. if (Style.AllowShortBlocksOnASingleLine) {
  274. MergedLines = tryMergeSimpleBlock(I - 1, E, Limit);
  275. // If we managed to merge the block, discard the first merged line
  276. // since we are merging starting from I.
  277. if (MergedLines > 0)
  278. --MergedLines;
  279. }
  280. return MergedLines;
  281. }
  282. // Don't merge block with left brace wrapped after ObjC special blocks
  283. if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() &&
  284. I[-1]->First->is(tok::at) && I[-1]->First->Next) {
  285. tok::ObjCKeywordKind kwId = I[-1]->First->Next->Tok.getObjCKeywordID();
  286. if (kwId == clang::tok::objc_autoreleasepool ||
  287. kwId == clang::tok::objc_synchronized)
  288. return 0;
  289. }
  290. // Don't merge block with left brace wrapped after case labels
  291. if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() &&
  292. I[-1]->First->isOneOf(tok::kw_case, tok::kw_default))
  293. return 0;
  294. // Try to merge a block with left brace wrapped that wasn't yet covered
  295. if (TheLine->Last->is(tok::l_brace)) {
  296. return !Style.BraceWrapping.AfterFunction ||
  297. (I[1]->First->is(tok::r_brace) &&
  298. !Style.BraceWrapping.SplitEmptyRecord)
  299. ? tryMergeSimpleBlock(I, E, Limit)
  300. : 0;
  301. }
  302. // Try to merge a function block with left brace wrapped
  303. if (I[1]->First->is(TT_FunctionLBrace) &&
  304. Style.BraceWrapping.AfterFunction) {
  305. if (I[1]->Last->is(TT_LineComment))
  306. return 0;
  307. // Check for Limit <= 2 to account for the " {".
  308. if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
  309. return 0;
  310. Limit -= 2;
  311. unsigned MergedLines = 0;
  312. if (MergeShortFunctions ||
  313. (Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&
  314. I[1]->First == I[1]->Last && I + 2 != E &&
  315. I[2]->First->is(tok::r_brace))) {
  316. MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
  317. // If we managed to merge the block, count the function header, which is
  318. // on a separate line.
  319. if (MergedLines > 0)
  320. ++MergedLines;
  321. }
  322. return MergedLines;
  323. }
  324. if (TheLine->First->is(tok::kw_if)) {
  325. return Style.AllowShortIfStatementsOnASingleLine
  326. ? tryMergeSimpleControlStatement(I, E, Limit)
  327. : 0;
  328. }
  329. if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) {
  330. return Style.AllowShortLoopsOnASingleLine
  331. ? tryMergeSimpleControlStatement(I, E, Limit)
  332. : 0;
  333. }
  334. if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) {
  335. return Style.AllowShortCaseLabelsOnASingleLine
  336. ? tryMergeShortCaseLabels(I, E, Limit)
  337. : 0;
  338. }
  339. if (TheLine->InPPDirective &&
  340. (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {
  341. return tryMergeSimplePPDirective(I, E, Limit);
  342. }
  343. return 0;
  344. }
  345. unsigned
  346. tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
  347. SmallVectorImpl<AnnotatedLine *>::const_iterator E,
  348. unsigned Limit) {
  349. if (Limit == 0)
  350. return 0;
  351. if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline)
  352. return 0;
  353. if (1 + I[1]->Last->TotalLength > Limit)
  354. return 0;
  355. return 1;
  356. }
  357. unsigned tryMergeSimpleControlStatement(
  358. SmallVectorImpl<AnnotatedLine *>::const_iterator I,
  359. SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) {
  360. if (Limit == 0)
  361. return 0;
  362. if (Style.BraceWrapping.AfterControlStatement &&
  363. (I[1]->First->is(tok::l_brace) && !Style.AllowShortBlocksOnASingleLine))
  364. return 0;
  365. if (I[1]->InPPDirective != (*I)->InPPDirective ||
  366. (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline))
  367. return 0;
  368. Limit = limitConsideringMacros(I + 1, E, Limit);
  369. AnnotatedLine &Line = **I;
  370. if (Line.Last->isNot(tok::r_paren))
  371. return 0;
  372. if (1 + I[1]->Last->TotalLength > Limit)
  373. return 0;
  374. if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while,
  375. TT_LineComment))
  376. return 0;
  377. // Only inline simple if's (no nested if or else), unless specified
  378. if (Style.AllowShortIfStatementsOnASingleLine != FormatStyle::SIS_Always) {
  379. if (I + 2 != E && Line.startsWith(tok::kw_if) &&
  380. I[2]->First->is(tok::kw_else))
  381. return 0;
  382. }
  383. return 1;
  384. }
  385. unsigned
  386. tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
  387. SmallVectorImpl<AnnotatedLine *>::const_iterator E,
  388. unsigned Limit) {
  389. if (Limit == 0 || I + 1 == E ||
  390. I[1]->First->isOneOf(tok::kw_case, tok::kw_default))
  391. return 0;
  392. if (I[0]->Last->is(tok::l_brace) || I[1]->First->is(tok::l_brace))
  393. return 0;
  394. unsigned NumStmts = 0;
  395. unsigned Length = 0;
  396. bool EndsWithComment = false;
  397. bool InPPDirective = I[0]->InPPDirective;
  398. const unsigned Level = I[0]->Level;
  399. for (; NumStmts < 3; ++NumStmts) {
  400. if (I + 1 + NumStmts == E)
  401. break;
  402. const AnnotatedLine *Line = I[1 + NumStmts];
  403. if (Line->InPPDirective != InPPDirective)
  404. break;
  405. if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
  406. break;
  407. if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,
  408. tok::kw_while) ||
  409. EndsWithComment)
  410. return 0;
  411. if (Line->First->is(tok::comment)) {
  412. if (Level != Line->Level)
  413. return 0;
  414. SmallVectorImpl<AnnotatedLine *>::const_iterator J = I + 2 + NumStmts;
  415. for (; J != E; ++J) {
  416. Line = *J;
  417. if (Line->InPPDirective != InPPDirective)
  418. break;
  419. if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
  420. break;
  421. if (Line->First->isNot(tok::comment) || Level != Line->Level)
  422. return 0;
  423. }
  424. break;
  425. }
  426. if (Line->Last->is(tok::comment))
  427. EndsWithComment = true;
  428. Length += I[1 + NumStmts]->Last->TotalLength + 1; // 1 for the space.
  429. }
  430. if (NumStmts == 0 || NumStmts == 3 || Length > Limit)
  431. return 0;
  432. return NumStmts;
  433. }
  434. unsigned
  435. tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
  436. SmallVectorImpl<AnnotatedLine *>::const_iterator E,
  437. unsigned Limit) {
  438. AnnotatedLine &Line = **I;
  439. // Don't merge ObjC @ keywords and methods.
  440. // FIXME: If an option to allow short exception handling clauses on a single
  441. // line is added, change this to not return for @try and friends.
  442. if (Style.Language != FormatStyle::LK_Java &&
  443. Line.First->isOneOf(tok::at, tok::minus, tok::plus))
  444. return 0;
  445. // Check that the current line allows merging. This depends on whether we
  446. // are in a control flow statements as well as several style flags.
  447. if (Line.First->isOneOf(tok::kw_else, tok::kw_case) ||
  448. (Line.First->Next && Line.First->Next->is(tok::kw_else)))
  449. return 0;
  450. // default: in switch statement
  451. if (Line.First->is(tok::kw_default)) {
  452. const FormatToken *Tok = Line.First->getNextNonComment();
  453. if (Tok && Tok->is(tok::colon))
  454. return 0;
  455. }
  456. if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
  457. tok::kw___try, tok::kw_catch, tok::kw___finally,
  458. tok::kw_for, tok::r_brace, Keywords.kw___except)) {
  459. if (!Style.AllowShortBlocksOnASingleLine)
  460. return 0;
  461. // Don't merge when we can't except the case when
  462. // the control statement block is empty
  463. if (!Style.AllowShortIfStatementsOnASingleLine &&
  464. Line.startsWith(tok::kw_if) &&
  465. !Style.BraceWrapping.AfterControlStatement &&
  466. !I[1]->First->is(tok::r_brace))
  467. return 0;
  468. if (!Style.AllowShortIfStatementsOnASingleLine &&
  469. Line.startsWith(tok::kw_if) &&
  470. Style.BraceWrapping.AfterControlStatement && I + 2 != E &&
  471. !I[2]->First->is(tok::r_brace))
  472. return 0;
  473. if (!Style.AllowShortLoopsOnASingleLine &&
  474. Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for) &&
  475. !Style.BraceWrapping.AfterControlStatement &&
  476. !I[1]->First->is(tok::r_brace))
  477. return 0;
  478. if (!Style.AllowShortLoopsOnASingleLine &&
  479. Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for) &&
  480. Style.BraceWrapping.AfterControlStatement && I + 2 != E &&
  481. !I[2]->First->is(tok::r_brace))
  482. return 0;
  483. // FIXME: Consider an option to allow short exception handling clauses on
  484. // a single line.
  485. // FIXME: This isn't covered by tests.
  486. // FIXME: For catch, __except, __finally the first token on the line
  487. // is '}', so this isn't correct here.
  488. if (Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
  489. Keywords.kw___except, tok::kw___finally))
  490. return 0;
  491. }
  492. if (Line.Last->is(tok::l_brace)) {
  493. FormatToken *Tok = I[1]->First;
  494. if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
  495. (Tok->getNextNonComment() == nullptr ||
  496. Tok->getNextNonComment()->is(tok::semi))) {
  497. // We merge empty blocks even if the line exceeds the column limit.
  498. Tok->SpacesRequiredBefore = 0;
  499. Tok->CanBreakBefore = true;
  500. return 1;
  501. } else if (Limit != 0 && !Line.startsWithNamespace() &&
  502. !startsExternCBlock(Line)) {
  503. // We don't merge short records.
  504. FormatToken *RecordTok = Line.First;
  505. // Skip record modifiers.
  506. while (RecordTok->Next &&
  507. RecordTok->isOneOf(tok::kw_typedef, tok::kw_export,
  508. Keywords.kw_declare, Keywords.kw_abstract,
  509. tok::kw_default))
  510. RecordTok = RecordTok->Next;
  511. if (RecordTok &&
  512. RecordTok->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct,
  513. Keywords.kw_interface))
  514. return 0;
  515. // Check that we still have three lines and they fit into the limit.
  516. if (I + 2 == E || I[2]->Type == LT_Invalid)
  517. return 0;
  518. Limit = limitConsideringMacros(I + 2, E, Limit);
  519. if (!nextTwoLinesFitInto(I, Limit))
  520. return 0;
  521. // Second, check that the next line does not contain any braces - if it
  522. // does, readability declines when putting it into a single line.
  523. if (I[1]->Last->is(TT_LineComment))
  524. return 0;
  525. do {
  526. if (Tok->is(tok::l_brace) && Tok->BlockKind != BK_BracedInit)
  527. return 0;
  528. Tok = Tok->Next;
  529. } while (Tok);
  530. // Last, check that the third line starts with a closing brace.
  531. Tok = I[2]->First;
  532. if (Tok->isNot(tok::r_brace))
  533. return 0;
  534. // Don't merge "if (a) { .. } else {".
  535. if (Tok->Next && Tok->Next->is(tok::kw_else))
  536. return 0;
  537. return 2;
  538. }
  539. } else if (I[1]->First->is(tok::l_brace)) {
  540. if (I[1]->Last->is(TT_LineComment))
  541. return 0;
  542. // Check for Limit <= 2 to account for the " {".
  543. if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(*I)))
  544. return 0;
  545. Limit -= 2;
  546. unsigned MergedLines = 0;
  547. if (Style.AllowShortBlocksOnASingleLine ||
  548. (I[1]->First == I[1]->Last && I + 2 != E &&
  549. I[2]->First->is(tok::r_brace))) {
  550. MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
  551. // If we managed to merge the block, count the statement header, which
  552. // is on a separate line.
  553. if (MergedLines > 0)
  554. ++MergedLines;
  555. }
  556. return MergedLines;
  557. }
  558. return 0;
  559. }
  560. /// Returns the modified column limit for \p I if it is inside a macro and
  561. /// needs a trailing '\'.
  562. unsigned
  563. limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
  564. SmallVectorImpl<AnnotatedLine *>::const_iterator E,
  565. unsigned Limit) {
  566. if (I[0]->InPPDirective && I + 1 != E &&
  567. !I[1]->First->HasUnescapedNewline && !I[1]->First->is(tok::eof)) {
  568. return Limit < 2 ? 0 : Limit - 2;
  569. }
  570. return Limit;
  571. }
  572. bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
  573. unsigned Limit) {
  574. if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore)
  575. return false;
  576. return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
  577. }
  578. bool containsMustBreak(const AnnotatedLine *Line) {
  579. for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
  580. if (Tok->MustBreakBefore)
  581. return true;
  582. }
  583. return false;
  584. }
  585. void join(AnnotatedLine &A, const AnnotatedLine &B) {
  586. assert(!A.Last->Next);
  587. assert(!B.First->Previous);
  588. if (B.Affected)
  589. A.Affected = true;
  590. A.Last->Next = B.First;
  591. B.First->Previous = A.Last;
  592. B.First->CanBreakBefore = true;
  593. unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
  594. for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
  595. Tok->TotalLength += LengthA;
  596. A.Last = Tok;
  597. }
  598. }
  599. const FormatStyle &Style;
  600. const AdditionalKeywords &Keywords;
  601. const SmallVectorImpl<AnnotatedLine *>::const_iterator End;
  602. SmallVectorImpl<AnnotatedLine *>::const_iterator Next;
  603. const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines;
  604. };
  605. static void markFinalized(FormatToken *Tok) {
  606. for (; Tok; Tok = Tok->Next) {
  607. Tok->Finalized = true;
  608. for (AnnotatedLine *Child : Tok->Children)
  609. markFinalized(Child->First);
  610. }
  611. }
  612. #ifndef NDEBUG
  613. static void printLineState(const LineState &State) {
  614. llvm::dbgs() << "State: ";
  615. for (const ParenState &P : State.Stack) {
  616. llvm::dbgs() << (P.Tok ? P.Tok->TokenText : "F") << "|" << P.Indent << "|"
  617. << P.LastSpace << "|" << P.NestedBlockIndent << " ";
  618. }
  619. llvm::dbgs() << State.NextToken->TokenText << "\n";
  620. }
  621. #endif
  622. /// Base class for classes that format one \c AnnotatedLine.
  623. class LineFormatter {
  624. public:
  625. LineFormatter(ContinuationIndenter *Indenter, WhitespaceManager *Whitespaces,
  626. const FormatStyle &Style,
  627. UnwrappedLineFormatter *BlockFormatter)
  628. : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
  629. BlockFormatter(BlockFormatter) {}
  630. virtual ~LineFormatter() {}
  631. /// Formats an \c AnnotatedLine and returns the penalty.
  632. ///
  633. /// If \p DryRun is \c false, directly applies the changes.
  634. virtual unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
  635. unsigned FirstStartColumn, bool DryRun) = 0;
  636. protected:
  637. /// If the \p State's next token is an r_brace closing a nested block,
  638. /// format the nested block before it.
  639. ///
  640. /// Returns \c true if all children could be placed successfully and adapts
  641. /// \p Penalty as well as \p State. If \p DryRun is false, also directly
  642. /// creates changes using \c Whitespaces.
  643. ///
  644. /// The crucial idea here is that children always get formatted upon
  645. /// encountering the closing brace right after the nested block. Now, if we
  646. /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is
  647. /// \c false), the entire block has to be kept on the same line (which is only
  648. /// possible if it fits on the line, only contains a single statement, etc.
  649. ///
  650. /// If \p NewLine is true, we format the nested block on separate lines, i.e.
  651. /// break after the "{", format all lines with correct indentation and the put
  652. /// the closing "}" on yet another new line.
  653. ///
  654. /// This enables us to keep the simple structure of the
  655. /// \c UnwrappedLineFormatter, where we only have two options for each token:
  656. /// break or don't break.
  657. bool formatChildren(LineState &State, bool NewLine, bool DryRun,
  658. unsigned &Penalty) {
  659. const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
  660. FormatToken &Previous = *State.NextToken->Previous;
  661. if (!LBrace || LBrace->isNot(tok::l_brace) ||
  662. LBrace->BlockKind != BK_Block || Previous.Children.size() == 0)
  663. // The previous token does not open a block. Nothing to do. We don't
  664. // assert so that we can simply call this function for all tokens.
  665. return true;
  666. if (NewLine) {
  667. int AdditionalIndent = State.Stack.back().Indent -
  668. Previous.Children[0]->Level * Style.IndentWidth;
  669. Penalty +=
  670. BlockFormatter->format(Previous.Children, DryRun, AdditionalIndent,
  671. /*FixBadIndentation=*/true);
  672. return true;
  673. }
  674. if (Previous.Children[0]->First->MustBreakBefore)
  675. return false;
  676. // Cannot merge into one line if this line ends on a comment.
  677. if (Previous.is(tok::comment))
  678. return false;
  679. // Cannot merge multiple statements into a single line.
  680. if (Previous.Children.size() > 1)
  681. return false;
  682. const AnnotatedLine *Child = Previous.Children[0];
  683. // We can't put the closing "}" on a line with a trailing comment.
  684. if (Child->Last->isTrailingComment())
  685. return false;
  686. // If the child line exceeds the column limit, we wouldn't want to merge it.
  687. // We add +2 for the trailing " }".
  688. if (Style.ColumnLimit > 0 &&
  689. Child->Last->TotalLength + State.Column + 2 > Style.ColumnLimit)
  690. return false;
  691. if (!DryRun) {
  692. Whitespaces->replaceWhitespace(
  693. *Child->First, /*Newlines=*/0, /*Spaces=*/1,
  694. /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective);
  695. }
  696. Penalty +=
  697. formatLine(*Child, State.Column + 1, /*FirstStartColumn=*/0, DryRun);
  698. State.Column += 1 + Child->Last->TotalLength;
  699. return true;
  700. }
  701. ContinuationIndenter *Indenter;
  702. private:
  703. WhitespaceManager *Whitespaces;
  704. const FormatStyle &Style;
  705. UnwrappedLineFormatter *BlockFormatter;
  706. };
  707. /// Formatter that keeps the existing line breaks.
  708. class NoColumnLimitLineFormatter : public LineFormatter {
  709. public:
  710. NoColumnLimitLineFormatter(ContinuationIndenter *Indenter,
  711. WhitespaceManager *Whitespaces,
  712. const FormatStyle &Style,
  713. UnwrappedLineFormatter *BlockFormatter)
  714. : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
  715. /// Formats the line, simply keeping all of the input's line breaking
  716. /// decisions.
  717. unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
  718. unsigned FirstStartColumn, bool DryRun) override {
  719. assert(!DryRun);
  720. LineState State = Indenter->getInitialState(FirstIndent, FirstStartColumn,
  721. &Line, /*DryRun=*/false);
  722. while (State.NextToken) {
  723. bool Newline =
  724. Indenter->mustBreak(State) ||
  725. (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
  726. unsigned Penalty = 0;
  727. formatChildren(State, Newline, /*DryRun=*/false, Penalty);
  728. Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
  729. }
  730. return 0;
  731. }
  732. };
  733. /// Formatter that puts all tokens into a single line without breaks.
  734. class NoLineBreakFormatter : public LineFormatter {
  735. public:
  736. NoLineBreakFormatter(ContinuationIndenter *Indenter,
  737. WhitespaceManager *Whitespaces, const FormatStyle &Style,
  738. UnwrappedLineFormatter *BlockFormatter)
  739. : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
  740. /// Puts all tokens into a single line.
  741. unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
  742. unsigned FirstStartColumn, bool DryRun) override {
  743. unsigned Penalty = 0;
  744. LineState State =
  745. Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);
  746. while (State.NextToken) {
  747. formatChildren(State, /*Newline=*/false, DryRun, Penalty);
  748. Indenter->addTokenToState(
  749. State, /*Newline=*/State.NextToken->MustBreakBefore, DryRun);
  750. }
  751. return Penalty;
  752. }
  753. };
  754. /// Finds the best way to break lines.
  755. class OptimizingLineFormatter : public LineFormatter {
  756. public:
  757. OptimizingLineFormatter(ContinuationIndenter *Indenter,
  758. WhitespaceManager *Whitespaces,
  759. const FormatStyle &Style,
  760. UnwrappedLineFormatter *BlockFormatter)
  761. : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
  762. /// Formats the line by finding the best line breaks with line lengths
  763. /// below the column limit.
  764. unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
  765. unsigned FirstStartColumn, bool DryRun) override {
  766. LineState State =
  767. Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);
  768. // If the ObjC method declaration does not fit on a line, we should format
  769. // it with one arg per line.
  770. if (State.Line->Type == LT_ObjCMethodDecl)
  771. State.Stack.back().BreakBeforeParameter = true;
  772. // Find best solution in solution space.
  773. return analyzeSolutionSpace(State, DryRun);
  774. }
  775. private:
  776. struct CompareLineStatePointers {
  777. bool operator()(LineState *obj1, LineState *obj2) const {
  778. return *obj1 < *obj2;
  779. }
  780. };
  781. /// A pair of <penalty, count> that is used to prioritize the BFS on.
  782. ///
  783. /// In case of equal penalties, we want to prefer states that were inserted
  784. /// first. During state generation we make sure that we insert states first
  785. /// that break the line as late as possible.
  786. typedef std::pair<unsigned, unsigned> OrderedPenalty;
  787. /// An edge in the solution space from \c Previous->State to \c State,
  788. /// inserting a newline dependent on the \c NewLine.
  789. struct StateNode {
  790. StateNode(const LineState &State, bool NewLine, StateNode *Previous)
  791. : State(State), NewLine(NewLine), Previous(Previous) {}
  792. LineState State;
  793. bool NewLine;
  794. StateNode *Previous;
  795. };
  796. /// An item in the prioritized BFS search queue. The \c StateNode's
  797. /// \c State has the given \c OrderedPenalty.
  798. typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
  799. /// The BFS queue type.
  800. typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
  801. std::greater<QueueItem>>
  802. QueueType;
  803. /// Analyze the entire solution space starting from \p InitialState.
  804. ///
  805. /// This implements a variant of Dijkstra's algorithm on the graph that spans
  806. /// the solution space (\c LineStates are the nodes). The algorithm tries to
  807. /// find the shortest path (the one with lowest penalty) from \p InitialState
  808. /// to a state where all tokens are placed. Returns the penalty.
  809. ///
  810. /// If \p DryRun is \c false, directly applies the changes.
  811. unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun) {
  812. std::set<LineState *, CompareLineStatePointers> Seen;
  813. // Increasing count of \c StateNode items we have created. This is used to
  814. // create a deterministic order independent of the container.
  815. unsigned Count = 0;
  816. QueueType Queue;
  817. // Insert start element into queue.
  818. StateNode *Node =
  819. new (Allocator.Allocate()) StateNode(InitialState, false, nullptr);
  820. Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
  821. ++Count;
  822. unsigned Penalty = 0;
  823. // While not empty, take first element and follow edges.
  824. while (!Queue.empty()) {
  825. Penalty = Queue.top().first.first;
  826. StateNode *Node = Queue.top().second;
  827. if (!Node->State.NextToken) {
  828. LLVM_DEBUG(llvm::dbgs()
  829. << "\n---\nPenalty for line: " << Penalty << "\n");
  830. break;
  831. }
  832. Queue.pop();
  833. // Cut off the analysis of certain solutions if the analysis gets too
  834. // complex. See description of IgnoreStackForComparison.
  835. if (Count > 50000)
  836. Node->State.IgnoreStackForComparison = true;
  837. if (!Seen.insert(&Node->State).second)
  838. // State already examined with lower penalty.
  839. continue;
  840. FormatDecision LastFormat = Node->State.NextToken->Decision;
  841. if (LastFormat == FD_Unformatted || LastFormat == FD_Continue)
  842. addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue);
  843. if (LastFormat == FD_Unformatted || LastFormat == FD_Break)
  844. addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue);
  845. }
  846. if (Queue.empty()) {
  847. // We were unable to find a solution, do nothing.
  848. // FIXME: Add diagnostic?
  849. LLVM_DEBUG(llvm::dbgs() << "Could not find a solution.\n");
  850. return 0;
  851. }
  852. // Reconstruct the solution.
  853. if (!DryRun)
  854. reconstructPath(InitialState, Queue.top().second);
  855. LLVM_DEBUG(llvm::dbgs()
  856. << "Total number of analyzed states: " << Count << "\n");
  857. LLVM_DEBUG(llvm::dbgs() << "---\n");
  858. return Penalty;
  859. }
  860. /// Add the following state to the analysis queue \c Queue.
  861. ///
  862. /// Assume the current state is \p PreviousNode and has been reached with a
  863. /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
  864. void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
  865. bool NewLine, unsigned *Count, QueueType *Queue) {
  866. if (NewLine && !Indenter->canBreak(PreviousNode->State))
  867. return;
  868. if (!NewLine && Indenter->mustBreak(PreviousNode->State))
  869. return;
  870. StateNode *Node = new (Allocator.Allocate())
  871. StateNode(PreviousNode->State, NewLine, PreviousNode);
  872. if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty))
  873. return;
  874. Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
  875. Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node));
  876. ++(*Count);
  877. }
  878. /// Applies the best formatting by reconstructing the path in the
  879. /// solution space that leads to \c Best.
  880. void reconstructPath(LineState &State, StateNode *Best) {
  881. std::deque<StateNode *> Path;
  882. // We do not need a break before the initial token.
  883. while (Best->Previous) {
  884. Path.push_front(Best);
  885. Best = Best->Previous;
  886. }
  887. for (auto I = Path.begin(), E = Path.end(); I != E; ++I) {
  888. unsigned Penalty = 0;
  889. formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty);
  890. Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false);
  891. LLVM_DEBUG({
  892. printLineState((*I)->Previous->State);
  893. if ((*I)->NewLine) {
  894. llvm::dbgs() << "Penalty for placing "
  895. << (*I)->Previous->State.NextToken->Tok.getName()
  896. << " on a new line: " << Penalty << "\n";
  897. }
  898. });
  899. }
  900. }
  901. llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
  902. };
  903. } // anonymous namespace
  904. unsigned UnwrappedLineFormatter::format(
  905. const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun,
  906. int AdditionalIndent, bool FixBadIndentation, unsigned FirstStartColumn,
  907. unsigned NextStartColumn, unsigned LastStartColumn) {
  908. LineJoiner Joiner(Style, Keywords, Lines);
  909. // Try to look up already computed penalty in DryRun-mode.
  910. std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned> CacheKey(
  911. &Lines, AdditionalIndent);
  912. auto CacheIt = PenaltyCache.find(CacheKey);
  913. if (DryRun && CacheIt != PenaltyCache.end())
  914. return CacheIt->second;
  915. assert(!Lines.empty());
  916. unsigned Penalty = 0;
  917. LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level,
  918. AdditionalIndent);
  919. const AnnotatedLine *PreviousLine = nullptr;
  920. const AnnotatedLine *NextLine = nullptr;
  921. // The minimum level of consecutive lines that have been formatted.
  922. unsigned RangeMinLevel = UINT_MAX;
  923. bool FirstLine = true;
  924. for (const AnnotatedLine *Line =
  925. Joiner.getNextMergedLine(DryRun, IndentTracker);
  926. Line; Line = NextLine, FirstLine = false) {
  927. const AnnotatedLine &TheLine = *Line;
  928. unsigned Indent = IndentTracker.getIndent();
  929. // We continue formatting unchanged lines to adjust their indent, e.g. if a
  930. // scope was added. However, we need to carefully stop doing this when we
  931. // exit the scope of affected lines to prevent indenting a the entire
  932. // remaining file if it currently missing a closing brace.
  933. bool PreviousRBrace =
  934. PreviousLine && PreviousLine->startsWith(tok::r_brace);
  935. bool ContinueFormatting =
  936. TheLine.Level > RangeMinLevel ||
  937. (TheLine.Level == RangeMinLevel && !PreviousRBrace &&
  938. !TheLine.startsWith(tok::r_brace));
  939. bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
  940. Indent != TheLine.First->OriginalColumn;
  941. bool ShouldFormat = TheLine.Affected || FixIndentation;
  942. // We cannot format this line; if the reason is that the line had a
  943. // parsing error, remember that.
  944. if (ShouldFormat && TheLine.Type == LT_Invalid && Status) {
  945. Status->FormatComplete = false;
  946. Status->Line =
  947. SourceMgr.getSpellingLineNumber(TheLine.First->Tok.getLocation());
  948. }
  949. if (ShouldFormat && TheLine.Type != LT_Invalid) {
  950. if (!DryRun) {
  951. bool LastLine = Line->First->is(tok::eof);
  952. formatFirstToken(TheLine, PreviousLine, Lines, Indent,
  953. LastLine ? LastStartColumn : NextStartColumn + Indent);
  954. }
  955. NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
  956. unsigned ColumnLimit = getColumnLimit(TheLine.InPPDirective, NextLine);
  957. bool FitsIntoOneLine =
  958. TheLine.Last->TotalLength + Indent <= ColumnLimit ||
  959. (TheLine.Type == LT_ImportStatement &&
  960. (Style.Language != FormatStyle::LK_JavaScript ||
  961. !Style.JavaScriptWrapImports));
  962. if (Style.ColumnLimit == 0)
  963. NoColumnLimitLineFormatter(Indenter, Whitespaces, Style, this)
  964. .formatLine(TheLine, NextStartColumn + Indent,
  965. FirstLine ? FirstStartColumn : 0, DryRun);
  966. else if (FitsIntoOneLine)
  967. Penalty += NoLineBreakFormatter(Indenter, Whitespaces, Style, this)
  968. .formatLine(TheLine, NextStartColumn + Indent,
  969. FirstLine ? FirstStartColumn : 0, DryRun);
  970. else
  971. Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this)
  972. .formatLine(TheLine, NextStartColumn + Indent,
  973. FirstLine ? FirstStartColumn : 0, DryRun);
  974. RangeMinLevel = std::min(RangeMinLevel, TheLine.Level);
  975. } else {
  976. // If no token in the current line is affected, we still need to format
  977. // affected children.
  978. if (TheLine.ChildrenAffected)
  979. for (const FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next)
  980. if (!Tok->Children.empty())
  981. format(Tok->Children, DryRun);
  982. // Adapt following lines on the current indent level to the same level
  983. // unless the current \c AnnotatedLine is not at the beginning of a line.
  984. bool StartsNewLine =
  985. TheLine.First->NewlinesBefore > 0 || TheLine.First->IsFirst;
  986. if (StartsNewLine)
  987. IndentTracker.adjustToUnmodifiedLine(TheLine);
  988. if (!DryRun) {
  989. bool ReformatLeadingWhitespace =
  990. StartsNewLine && ((PreviousLine && PreviousLine->Affected) ||
  991. TheLine.LeadingEmptyLinesAffected);
  992. // Format the first token.
  993. if (ReformatLeadingWhitespace)
  994. formatFirstToken(TheLine, PreviousLine, Lines,
  995. TheLine.First->OriginalColumn,
  996. TheLine.First->OriginalColumn);
  997. else
  998. Whitespaces->addUntouchableToken(*TheLine.First,
  999. TheLine.InPPDirective);
  1000. // Notify the WhitespaceManager about the unchanged whitespace.
  1001. for (FormatToken *Tok = TheLine.First->Next; Tok; Tok = Tok->Next)
  1002. Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
  1003. }
  1004. NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
  1005. RangeMinLevel = UINT_MAX;
  1006. }
  1007. if (!DryRun)
  1008. markFinalized(TheLine.First);
  1009. PreviousLine = &TheLine;
  1010. }
  1011. PenaltyCache[CacheKey] = Penalty;
  1012. return Penalty;
  1013. }
  1014. void UnwrappedLineFormatter::formatFirstToken(
  1015. const AnnotatedLine &Line, const AnnotatedLine *PreviousLine,
  1016. const SmallVectorImpl<AnnotatedLine *> &Lines, unsigned Indent,
  1017. unsigned NewlineIndent) {
  1018. FormatToken &RootToken = *Line.First;
  1019. if (RootToken.is(tok::eof)) {
  1020. unsigned Newlines = std::min(RootToken.NewlinesBefore, 1u);
  1021. unsigned TokenIndent = Newlines ? NewlineIndent : 0;
  1022. Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent,
  1023. TokenIndent);
  1024. return;
  1025. }
  1026. unsigned Newlines =
  1027. std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
  1028. // Remove empty lines before "}" where applicable.
  1029. if (RootToken.is(tok::r_brace) &&
  1030. (!RootToken.Next ||
  1031. (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)) &&
  1032. // Do not remove empty lines before namespace closing "}".
  1033. !getNamespaceToken(&Line, Lines))
  1034. Newlines = std::min(Newlines, 1u);
  1035. // Remove empty lines at the start of nested blocks (lambdas/arrow functions)
  1036. if (PreviousLine == nullptr && Line.Level > 0)
  1037. Newlines = std::min(Newlines, 1u);
  1038. if (Newlines == 0 && !RootToken.IsFirst)
  1039. Newlines = 1;
  1040. if (RootToken.IsFirst && !RootToken.HasUnescapedNewline)
  1041. Newlines = 0;
  1042. // Remove empty lines after "{".
  1043. if (!Style.KeepEmptyLinesAtTheStartOfBlocks && PreviousLine &&
  1044. PreviousLine->Last->is(tok::l_brace) &&
  1045. !PreviousLine->startsWithNamespace() &&
  1046. !startsExternCBlock(*PreviousLine))
  1047. Newlines = 1;
  1048. // Insert extra new line before access specifiers.
  1049. if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) &&
  1050. RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
  1051. ++Newlines;
  1052. // Remove empty lines after access specifiers.
  1053. if (PreviousLine && PreviousLine->First->isAccessSpecifier() &&
  1054. (!PreviousLine->InPPDirective || !RootToken.HasUnescapedNewline))
  1055. Newlines = std::min(1u, Newlines);
  1056. if (Newlines)
  1057. Indent = NewlineIndent;
  1058. // Preprocessor directives get indented after the hash, if indented.
  1059. if (Line.Type == LT_PreprocessorDirective || Line.Type == LT_ImportStatement)
  1060. Indent = 0;
  1061. Whitespaces->replaceWhitespace(RootToken, Newlines, Indent, Indent,
  1062. Line.InPPDirective &&
  1063. !RootToken.HasUnescapedNewline);
  1064. }
  1065. unsigned
  1066. UnwrappedLineFormatter::getColumnLimit(bool InPPDirective,
  1067. const AnnotatedLine *NextLine) const {
  1068. // In preprocessor directives reserve two chars for trailing " \" if the
  1069. // next line continues the preprocessor directive.
  1070. bool ContinuesPPDirective =
  1071. InPPDirective &&
  1072. // If there is no next line, this is likely a child line and the parent
  1073. // continues the preprocessor directive.
  1074. (!NextLine ||
  1075. (NextLine->InPPDirective &&
  1076. // If there is an unescaped newline between this line and the next, the
  1077. // next line starts a new preprocessor directive.
  1078. !NextLine->First->HasUnescapedNewline));
  1079. return Style.ColumnLimit - (ContinuesPPDirective ? 2 : 0);
  1080. }
  1081. } // namespace format
  1082. } // namespace clang