ContinuationIndenter.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. //===--- ContinuationIndenter.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 the continuation indenter.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #define DEBUG_TYPE "format-formatter"
  15. #include "BreakableToken.h"
  16. #include "ContinuationIndenter.h"
  17. #include "WhitespaceManager.h"
  18. #include "clang/Basic/OperatorPrecedence.h"
  19. #include "clang/Basic/SourceManager.h"
  20. #include "clang/Format/Format.h"
  21. #include "llvm/Support/Debug.h"
  22. #include <string>
  23. namespace clang {
  24. namespace format {
  25. // Returns the length of everything up to the first possible line break after
  26. // the ), ], } or > matching \c Tok.
  27. static unsigned getLengthToMatchingParen(const FormatToken &Tok) {
  28. if (Tok.MatchingParen == NULL)
  29. return 0;
  30. FormatToken *End = Tok.MatchingParen;
  31. while (End->Next && !End->Next->CanBreakBefore) {
  32. End = End->Next;
  33. }
  34. return End->TotalLength - Tok.TotalLength + 1;
  35. }
  36. // Returns \c true if \c Tok is the "." or "->" of a call and starts the next
  37. // segment of a builder type call.
  38. static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
  39. return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
  40. }
  41. // Returns \c true if \c Current starts a new parameter.
  42. static bool startsNextParameter(const FormatToken &Current,
  43. const FormatStyle &Style) {
  44. const FormatToken &Previous = *Current.Previous;
  45. if (Current.Type == TT_CtorInitializerComma &&
  46. Style.BreakConstructorInitializersBeforeComma)
  47. return true;
  48. return Previous.is(tok::comma) && !Current.isTrailingComment() &&
  49. (Previous.Type != TT_CtorInitializerComma ||
  50. !Style.BreakConstructorInitializersBeforeComma);
  51. }
  52. ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
  53. SourceManager &SourceMgr,
  54. WhitespaceManager &Whitespaces,
  55. encoding::Encoding Encoding,
  56. bool BinPackInconclusiveFunctions)
  57. : Style(Style), SourceMgr(SourceMgr), Whitespaces(Whitespaces),
  58. Encoding(Encoding),
  59. BinPackInconclusiveFunctions(BinPackInconclusiveFunctions) {}
  60. LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
  61. const AnnotatedLine *Line,
  62. bool DryRun) {
  63. LineState State;
  64. State.FirstIndent = FirstIndent;
  65. State.Column = FirstIndent;
  66. State.Line = Line;
  67. State.NextToken = Line->First;
  68. State.Stack.push_back(ParenState(FirstIndent, FirstIndent,
  69. /*AvoidBinPacking=*/false,
  70. /*NoLineBreak=*/false));
  71. State.LineContainsContinuedForLoopSection = false;
  72. State.ParenLevel = 0;
  73. State.StartOfStringLiteral = 0;
  74. State.StartOfLineLevel = State.ParenLevel;
  75. State.LowestLevelOnLine = State.ParenLevel;
  76. State.IgnoreStackForComparison = false;
  77. // The first token has already been indented and thus consumed.
  78. moveStateToNextToken(State, DryRun, /*Newline=*/false);
  79. return State;
  80. }
  81. bool ContinuationIndenter::canBreak(const LineState &State) {
  82. const FormatToken &Current = *State.NextToken;
  83. const FormatToken &Previous = *Current.Previous;
  84. assert(&Previous == Current.Previous);
  85. if (!Current.CanBreakBefore &&
  86. !(Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace))
  87. return false;
  88. // The opening "{" of a braced list has to be on the same line as the first
  89. // element if it is nested in another braced init list or function call.
  90. if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
  91. Previous.BlockKind == BK_BracedInit && Previous.Previous &&
  92. Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
  93. return false;
  94. // This prevents breaks like:
  95. // ...
  96. // SomeParameter, OtherParameter).DoSomething(
  97. // ...
  98. // As they hide "DoSomething" and are generally bad for readability.
  99. if (Previous.opensScope() && State.LowestLevelOnLine < State.StartOfLineLevel)
  100. return false;
  101. if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder)
  102. return false;
  103. return !State.Stack.back().NoLineBreak;
  104. }
  105. bool ContinuationIndenter::mustBreak(const LineState &State) {
  106. const FormatToken &Current = *State.NextToken;
  107. const FormatToken &Previous = *Current.Previous;
  108. if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
  109. return true;
  110. if ((!Style.Cpp11BracedListStyle ||
  111. (Current.MatchingParen &&
  112. Current.MatchingParen->BlockKind == BK_Block)) &&
  113. Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)
  114. return true;
  115. if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
  116. return true;
  117. if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
  118. Current.is(tok::question) ||
  119. (Current.Type == TT_ConditionalExpr && Previous.isNot(tok::question))) &&
  120. State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
  121. !Current.isOneOf(tok::r_paren, tok::r_brace))
  122. return true;
  123. if (Style.AlwaysBreakBeforeMultilineStrings &&
  124. State.Column > State.Stack.back().Indent && // Breaking saves columns.
  125. Previous.isNot(tok::lessless) && Previous.Type != TT_InlineASMColon &&
  126. NextIsMultilineString(State))
  127. return true;
  128. if (!Style.BreakBeforeBinaryOperators) {
  129. // If we need to break somewhere inside the LHS of a binary expression, we
  130. // should also break after the operator. Otherwise, the formatting would
  131. // hide the operator precedence, e.g. in:
  132. // if (aaaaaaaaaaaaaa ==
  133. // bbbbbbbbbbbbbb && c) {..
  134. // For comparisons, we only apply this rule, if the LHS is a binary
  135. // expression itself as otherwise, the line breaks seem superfluous.
  136. // We need special cases for ">>" which we have split into two ">" while
  137. // lexing in order to make template parsing easier.
  138. //
  139. // FIXME: We'll need something similar for styles that break before binary
  140. // operators.
  141. bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
  142. Previous.getPrecedence() == prec::Equality) &&
  143. Previous.Previous &&
  144. Previous.Previous->Type != TT_BinaryOperator; // For >>.
  145. bool LHSIsBinaryExpr =
  146. Previous.Previous && Previous.Previous->EndsBinaryExpression;
  147. if (Previous.Type == TT_BinaryOperator &&
  148. (!IsComparison || LHSIsBinaryExpr) &&
  149. Current.Type != TT_BinaryOperator && // For >>.
  150. !Current.isTrailingComment() &&
  151. !Previous.isOneOf(tok::lessless, tok::question) &&
  152. Previous.getPrecedence() != prec::Assignment &&
  153. State.Stack.back().BreakBeforeParameter)
  154. return true;
  155. }
  156. // Same as above, but for the first "<<" operator.
  157. if (Current.is(tok::lessless) && State.Stack.back().BreakBeforeParameter &&
  158. State.Stack.back().FirstLessLess == 0)
  159. return true;
  160. // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
  161. // out whether it is the first parameter. Clean this up.
  162. if (Current.Type == TT_ObjCSelectorName &&
  163. Current.LongestObjCSelectorName == 0 &&
  164. State.Stack.back().BreakBeforeParameter)
  165. return true;
  166. if ((Current.Type == TT_CtorInitializerColon ||
  167. (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0 &&
  168. !Current.isTrailingComment())))
  169. return true;
  170. if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) &&
  171. State.Line->MightBeFunctionDecl &&
  172. State.Stack.back().BreakBeforeParameter && State.ParenLevel == 0)
  173. return true;
  174. if (startsSegmentOfBuilderTypeCall(Current) &&
  175. (State.Stack.back().CallContinuation != 0 ||
  176. (State.Stack.back().BreakBeforeParameter &&
  177. State.Stack.back().ContainsUnwrappedBuilder)))
  178. return true;
  179. return false;
  180. }
  181. unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
  182. bool DryRun,
  183. unsigned ExtraSpaces) {
  184. const FormatToken &Current = *State.NextToken;
  185. if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
  186. // FIXME: Is this correct?
  187. int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
  188. State.NextToken->WhitespaceRange.getEnd()) -
  189. SourceMgr.getSpellingColumnNumber(
  190. State.NextToken->WhitespaceRange.getBegin());
  191. State.Column += WhitespaceLength + State.NextToken->ColumnWidth;
  192. State.NextToken = State.NextToken->Next;
  193. return 0;
  194. }
  195. unsigned Penalty = 0;
  196. if (Newline)
  197. Penalty = addTokenOnNewLine(State, DryRun);
  198. else
  199. addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
  200. return moveStateToNextToken(State, DryRun, Newline) + Penalty;
  201. }
  202. void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
  203. unsigned ExtraSpaces) {
  204. FormatToken &Current = *State.NextToken;
  205. const FormatToken &Previous = *State.NextToken->Previous;
  206. if (Current.is(tok::equal) &&
  207. (State.Line->First->is(tok::kw_for) || State.ParenLevel == 0) &&
  208. State.Stack.back().VariablePos == 0) {
  209. State.Stack.back().VariablePos = State.Column;
  210. // Move over * and & if they are bound to the variable name.
  211. const FormatToken *Tok = &Previous;
  212. while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) {
  213. State.Stack.back().VariablePos -= Tok->ColumnWidth;
  214. if (Tok->SpacesRequiredBefore != 0)
  215. break;
  216. Tok = Tok->Previous;
  217. }
  218. if (Previous.PartOfMultiVariableDeclStmt)
  219. State.Stack.back().LastSpace = State.Stack.back().VariablePos;
  220. }
  221. unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
  222. if (!DryRun)
  223. Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, /*IndentLevel=*/0,
  224. Spaces, State.Column + Spaces);
  225. if (Current.Type == TT_ObjCSelectorName && State.Stack.back().ColonPos == 0) {
  226. if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
  227. State.Column + Spaces + Current.ColumnWidth)
  228. State.Stack.back().ColonPos =
  229. State.Stack.back().Indent + Current.LongestObjCSelectorName;
  230. else
  231. State.Stack.back().ColonPos = State.Column + Spaces + Current.ColumnWidth;
  232. }
  233. if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
  234. Current.Type != TT_LineComment)
  235. State.Stack.back().Indent = State.Column + Spaces;
  236. if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style))
  237. State.Stack.back().NoLineBreak = true;
  238. if (startsSegmentOfBuilderTypeCall(Current))
  239. State.Stack.back().ContainsUnwrappedBuilder = true;
  240. State.Column += Spaces;
  241. if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
  242. // Treat the condition inside an if as if it was a second function
  243. // parameter, i.e. let nested calls have an indent of 4.
  244. State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
  245. else if (Previous.is(tok::comma))
  246. State.Stack.back().LastSpace = State.Column;
  247. else if ((Previous.Type == TT_BinaryOperator ||
  248. Previous.Type == TT_ConditionalExpr ||
  249. Previous.Type == TT_UnaryOperator ||
  250. Previous.Type == TT_CtorInitializerColon) &&
  251. (Previous.getPrecedence() != prec::Assignment ||
  252. Current.StartsBinaryExpression))
  253. // Always indent relative to the RHS of the expression unless this is a
  254. // simple assignment without binary expression on the RHS. Also indent
  255. // relative to unary operators and the colons of constructor initializers.
  256. State.Stack.back().LastSpace = State.Column;
  257. else if (Previous.Type == TT_InheritanceColon) {
  258. State.Stack.back().Indent = State.Column;
  259. State.Stack.back().LastSpace = State.Column;
  260. } else if (Previous.opensScope()) {
  261. // If a function has a trailing call, indent all parameters from the
  262. // opening parenthesis. This avoids confusing indents like:
  263. // OuterFunction(InnerFunctionCall( // break
  264. // ParameterToInnerFunction)) // break
  265. // .SecondInnerFunctionCall();
  266. bool HasTrailingCall = false;
  267. if (Previous.MatchingParen) {
  268. const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
  269. HasTrailingCall = Next && Next->isMemberAccess();
  270. }
  271. if (HasTrailingCall &&
  272. State.Stack[State.Stack.size() - 2].CallContinuation == 0)
  273. State.Stack.back().LastSpace = State.Column;
  274. }
  275. }
  276. unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
  277. bool DryRun) {
  278. FormatToken &Current = *State.NextToken;
  279. const FormatToken &Previous = *State.NextToken->Previous;
  280. // If we are continuing an expression, we want to indent an extra 4 spaces.
  281. unsigned ContinuationIndent =
  282. std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4;
  283. // Extra penalty that needs to be added because of the way certain line
  284. // breaks are chosen.
  285. unsigned Penalty = 0;
  286. const FormatToken *PreviousNonComment =
  287. State.NextToken->getPreviousNonComment();
  288. // The first line break on any ParenLevel causes an extra penalty in order
  289. // prefer similar line breaks.
  290. if (!State.Stack.back().ContainsLineBreak)
  291. Penalty += 15;
  292. State.Stack.back().ContainsLineBreak = true;
  293. Penalty += State.NextToken->SplitPenalty;
  294. // Breaking before the first "<<" is generally not desirable if the LHS is
  295. // short.
  296. if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 &&
  297. State.Column <= Style.ColumnLimit / 2)
  298. Penalty += Style.PenaltyBreakFirstLessLess;
  299. if (Current.is(tok::l_brace) && Current.BlockKind == BK_Block) {
  300. State.Column = State.FirstIndent;
  301. } else if (Current.is(tok::r_brace)) {
  302. if (Current.MatchingParen &&
  303. (Current.MatchingParen->BlockKind == BK_BracedInit ||
  304. !Current.MatchingParen->Children.empty()))
  305. State.Column = State.Stack[State.Stack.size() - 2].LastSpace;
  306. else
  307. State.Column = State.FirstIndent;
  308. } else if (Current.is(tok::string_literal) &&
  309. State.StartOfStringLiteral != 0) {
  310. State.Column = State.StartOfStringLiteral;
  311. State.Stack.back().BreakBeforeParameter = true;
  312. } else if (Current.is(tok::lessless) &&
  313. State.Stack.back().FirstLessLess != 0) {
  314. State.Column = State.Stack.back().FirstLessLess;
  315. } else if (Current.isMemberAccess()) {
  316. if (State.Stack.back().CallContinuation == 0) {
  317. State.Column = ContinuationIndent;
  318. State.Stack.back().CallContinuation = State.Column;
  319. } else {
  320. State.Column = State.Stack.back().CallContinuation;
  321. }
  322. } else if (Current.Type == TT_ConditionalExpr) {
  323. State.Column = State.Stack.back().QuestionColumn;
  324. } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) {
  325. State.Column = State.Stack.back().VariablePos;
  326. } else if ((PreviousNonComment &&
  327. PreviousNonComment->ClosesTemplateDeclaration) ||
  328. ((Current.Type == TT_StartOfName ||
  329. Current.is(tok::kw_operator)) &&
  330. State.ParenLevel == 0 &&
  331. (!Style.IndentFunctionDeclarationAfterType ||
  332. State.Line->StartsDefinition))) {
  333. State.Column = State.Stack.back().Indent;
  334. } else if (Current.Type == TT_ObjCSelectorName) {
  335. if (State.Stack.back().ColonPos > Current.ColumnWidth) {
  336. State.Column = State.Stack.back().ColonPos - Current.ColumnWidth;
  337. } else {
  338. State.Column = State.Stack.back().Indent;
  339. State.Stack.back().ColonPos = State.Column + Current.ColumnWidth;
  340. }
  341. } else if (Current.is(tok::l_square) && Current.Type != TT_ObjCMethodExpr &&
  342. Current.Type != TT_LambdaLSquare) {
  343. if (State.Stack.back().StartOfArraySubscripts != 0)
  344. State.Column = State.Stack.back().StartOfArraySubscripts;
  345. else
  346. State.Column = ContinuationIndent;
  347. } else if (Current.Type == TT_StartOfName ||
  348. Previous.isOneOf(tok::coloncolon, tok::equal) ||
  349. Previous.Type == TT_ObjCMethodExpr) {
  350. State.Column = ContinuationIndent;
  351. } else if (Current.Type == TT_CtorInitializerColon) {
  352. State.Column = State.FirstIndent + Style.ConstructorInitializerIndentWidth;
  353. } else if (Current.Type == TT_CtorInitializerComma) {
  354. State.Column = State.Stack.back().Indent;
  355. } else {
  356. State.Column = State.Stack.back().Indent;
  357. // Ensure that we fall back to indenting 4 spaces instead of just
  358. // flushing continuations left.
  359. if (State.Column == State.FirstIndent)
  360. State.Column += 4;
  361. }
  362. if (Current.is(tok::question))
  363. State.Stack.back().BreakBeforeParameter = true;
  364. if ((Previous.isOneOf(tok::comma, tok::semi) &&
  365. !State.Stack.back().AvoidBinPacking) ||
  366. Previous.Type == TT_BinaryOperator)
  367. State.Stack.back().BreakBeforeParameter = false;
  368. if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
  369. State.Stack.back().BreakBeforeParameter = false;
  370. if (!DryRun) {
  371. unsigned Newlines = 1;
  372. if (Current.is(tok::comment))
  373. Newlines = std::max(Newlines, std::min(Current.NewlinesBefore,
  374. Style.MaxEmptyLinesToKeep + 1));
  375. Whitespaces.replaceWhitespace(Current, Newlines, State.Line->Level,
  376. State.Column, State.Column,
  377. State.Line->InPPDirective);
  378. }
  379. if (!Current.isTrailingComment())
  380. State.Stack.back().LastSpace = State.Column;
  381. if (Current.isMemberAccess())
  382. State.Stack.back().LastSpace += Current.ColumnWidth;
  383. State.StartOfLineLevel = State.ParenLevel;
  384. State.LowestLevelOnLine = State.ParenLevel;
  385. // Any break on this level means that the parent level has been broken
  386. // and we need to avoid bin packing there.
  387. for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
  388. State.Stack[i].BreakBeforeParameter = true;
  389. }
  390. const FormatToken *TokenBefore = Current.getPreviousNonComment();
  391. if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) &&
  392. TokenBefore->Type != TT_TemplateCloser &&
  393. TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope())
  394. State.Stack.back().BreakBeforeParameter = true;
  395. // If we break after {, we should also break before the corresponding }.
  396. if (Previous.is(tok::l_brace))
  397. State.Stack.back().BreakBeforeClosingBrace = true;
  398. if (State.Stack.back().AvoidBinPacking) {
  399. // If we are breaking after '(', '{', '<', this is not bin packing
  400. // unless AllowAllParametersOfDeclarationOnNextLine is false.
  401. if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
  402. Previous.Type == TT_BinaryOperator) ||
  403. (!Style.AllowAllParametersOfDeclarationOnNextLine &&
  404. State.Line->MustBeDeclaration))
  405. State.Stack.back().BreakBeforeParameter = true;
  406. }
  407. return Penalty;
  408. }
  409. unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
  410. bool DryRun, bool Newline) {
  411. const FormatToken &Current = *State.NextToken;
  412. assert(State.Stack.size());
  413. if (Current.Type == TT_InheritanceColon)
  414. State.Stack.back().AvoidBinPacking = true;
  415. if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
  416. State.Stack.back().FirstLessLess = State.Column;
  417. if (Current.is(tok::l_square) && Current.Type != TT_LambdaLSquare &&
  418. State.Stack.back().StartOfArraySubscripts == 0)
  419. State.Stack.back().StartOfArraySubscripts = State.Column;
  420. if (Current.is(tok::question))
  421. State.Stack.back().QuestionColumn = State.Column;
  422. if (!Current.opensScope() && !Current.closesScope())
  423. State.LowestLevelOnLine =
  424. std::min(State.LowestLevelOnLine, State.ParenLevel);
  425. if (Current.isMemberAccess())
  426. State.Stack.back().StartOfFunctionCall =
  427. Current.LastInChainOfCalls ? 0 : State.Column + Current.ColumnWidth;
  428. if (Current.Type == TT_CtorInitializerColon) {
  429. // Indent 2 from the column, so:
  430. // SomeClass::SomeClass()
  431. // : First(...), ...
  432. // Next(...)
  433. // ^ line up here.
  434. State.Stack.back().Indent =
  435. State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2);
  436. if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
  437. State.Stack.back().AvoidBinPacking = true;
  438. State.Stack.back().BreakBeforeParameter = false;
  439. }
  440. // In ObjC method declaration we align on the ":" of parameters, but we need
  441. // to ensure that we indent parameters on subsequent lines by at least 4.
  442. if (Current.Type == TT_ObjCMethodSpecifier)
  443. State.Stack.back().Indent += 4;
  444. // Insert scopes created by fake parenthesis.
  445. const FormatToken *Previous = Current.getPreviousNonComment();
  446. // Don't add extra indentation for the first fake parenthesis after
  447. // 'return', assignements or opening <({[. The indentation for these cases
  448. // is special cased.
  449. bool SkipFirstExtraIndent =
  450. (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) ||
  451. Previous->getPrecedence() == prec::Assignment));
  452. for (SmallVectorImpl<prec::Level>::const_reverse_iterator
  453. I = Current.FakeLParens.rbegin(),
  454. E = Current.FakeLParens.rend();
  455. I != E; ++I) {
  456. ParenState NewParenState = State.Stack.back();
  457. NewParenState.ContainsLineBreak = false;
  458. // Indent from 'LastSpace' unless this the fake parentheses encapsulating a
  459. // builder type call after 'return'. If such a call is line-wrapped, we
  460. // commonly just want to indent from the start of the line.
  461. if (!Previous || Previous->isNot(tok::kw_return) || *I > 0)
  462. NewParenState.Indent =
  463. std::max(std::max(State.Column, NewParenState.Indent),
  464. State.Stack.back().LastSpace);
  465. // Do not indent relative to the fake parentheses inserted for "." or "->".
  466. // This is a special case to make the following to statements consistent:
  467. // OuterFunction(InnerFunctionCall( // break
  468. // ParameterToInnerFunction));
  469. // OuterFunction(SomeObject.InnerFunctionCall( // break
  470. // ParameterToInnerFunction));
  471. if (*I > prec::Unknown)
  472. NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
  473. // Always indent conditional expressions. Never indent expression where
  474. // the 'operator' is ',', ';' or an assignment (i.e. *I <=
  475. // prec::Assignment) as those have different indentation rules. Indent
  476. // other expression, unless the indentation needs to be skipped.
  477. if (*I == prec::Conditional ||
  478. (!SkipFirstExtraIndent && *I > prec::Assignment &&
  479. !Style.BreakBeforeBinaryOperators))
  480. NewParenState.Indent += 4;
  481. if (Previous && !Previous->opensScope())
  482. NewParenState.BreakBeforeParameter = false;
  483. State.Stack.push_back(NewParenState);
  484. SkipFirstExtraIndent = false;
  485. }
  486. // If we encounter an opening (, [, { or <, we add a level to our stacks to
  487. // prepare for the following tokens.
  488. if (Current.opensScope()) {
  489. unsigned NewIndent;
  490. bool AvoidBinPacking;
  491. if (Current.is(tok::l_brace)) {
  492. if (Current.MatchingParen && Current.BlockKind == BK_Block) {
  493. // If this is an l_brace starting a nested block, we pretend (wrt. to
  494. // indentation) that we already consumed the corresponding r_brace.
  495. // Thus, we remove all ParenStates caused bake fake parentheses that end
  496. // at the r_brace. The net effect of this is that we don't indent
  497. // relative to the l_brace, if the nested block is the last parameter of
  498. // a function. For example, this formats:
  499. //
  500. // SomeFunction(a, [] {
  501. // f(); // break
  502. // });
  503. //
  504. // instead of:
  505. // SomeFunction(a, [] {
  506. // f(); // break
  507. // });
  508. for (unsigned i = 0; i != Current.MatchingParen->FakeRParens; ++i)
  509. State.Stack.pop_back();
  510. NewIndent = State.Stack.back().LastSpace + Style.IndentWidth;
  511. } else {
  512. NewIndent = State.Stack.back().LastSpace +
  513. (Style.Cpp11BracedListStyle ? 4 : Style.IndentWidth);
  514. }
  515. const FormatToken *NextNoComment = Current.getNextNonComment();
  516. AvoidBinPacking = Current.BlockKind == BK_Block ||
  517. (NextNoComment &&
  518. NextNoComment->Type == TT_DesignatedInitializerPeriod);
  519. } else {
  520. NewIndent = 4 + std::max(State.Stack.back().LastSpace,
  521. State.Stack.back().StartOfFunctionCall);
  522. AvoidBinPacking = !Style.BinPackParameters ||
  523. (Style.ExperimentalAutoDetectBinPacking &&
  524. (Current.PackingKind == PPK_OnePerLine ||
  525. (!BinPackInconclusiveFunctions &&
  526. Current.PackingKind == PPK_Inconclusive)));
  527. }
  528. State.Stack.push_back(ParenState(NewIndent, State.Stack.back().LastSpace,
  529. AvoidBinPacking,
  530. State.Stack.back().NoLineBreak));
  531. State.Stack.back().BreakBeforeParameter = Current.BlockKind == BK_Block;
  532. ++State.ParenLevel;
  533. }
  534. // If this '[' opens an ObjC call, determine whether all parameters fit into
  535. // one line and put one per line if they don't.
  536. if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
  537. Current.MatchingParen != NULL) {
  538. if (getLengthToMatchingParen(Current) + State.Column >
  539. getColumnLimit(State))
  540. State.Stack.back().BreakBeforeParameter = true;
  541. }
  542. // If we encounter a closing ), ], } or >, we can remove a level from our
  543. // stacks.
  544. if (State.Stack.size() > 1 &&
  545. (Current.isOneOf(tok::r_paren, tok::r_square) ||
  546. (Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
  547. State.NextToken->Type == TT_TemplateCloser)) {
  548. State.Stack.pop_back();
  549. --State.ParenLevel;
  550. }
  551. if (Current.is(tok::r_square)) {
  552. // If this ends the array subscript expr, reset the corresponding value.
  553. const FormatToken *NextNonComment = Current.getNextNonComment();
  554. if (NextNonComment && NextNonComment->isNot(tok::l_square))
  555. State.Stack.back().StartOfArraySubscripts = 0;
  556. }
  557. // Remove scopes created by fake parenthesis.
  558. if (Current.isNot(tok::r_brace) ||
  559. (Current.MatchingParen && Current.MatchingParen->BlockKind != BK_Block)) {
  560. // Don't remove FakeRParens attached to r_braces that surround nested blocks
  561. // as they will have been removed early (see above).
  562. for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
  563. unsigned VariablePos = State.Stack.back().VariablePos;
  564. State.Stack.pop_back();
  565. State.Stack.back().VariablePos = VariablePos;
  566. }
  567. }
  568. if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
  569. State.StartOfStringLiteral = State.Column;
  570. } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
  571. tok::string_literal)) {
  572. State.StartOfStringLiteral = 0;
  573. }
  574. State.Column += Current.ColumnWidth;
  575. State.NextToken = State.NextToken->Next;
  576. unsigned Penalty = breakProtrudingToken(Current, State, DryRun);
  577. if (State.Column > getColumnLimit(State)) {
  578. unsigned ExcessCharacters = State.Column - getColumnLimit(State);
  579. Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
  580. }
  581. // If the previous has a special role, let it consume tokens as appropriate.
  582. // It is necessary to start at the previous token for the only implemented
  583. // role (comma separated list). That way, the decision whether or not to break
  584. // after the "{" is already done and both options are tried and evaluated.
  585. // FIXME: This is ugly, find a better way.
  586. if (Previous && Previous->Role)
  587. Penalty += Previous->Role->format(State, this, DryRun);
  588. return Penalty;
  589. }
  590. unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
  591. LineState &State) {
  592. // Break before further function parameters on all levels.
  593. for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
  594. State.Stack[i].BreakBeforeParameter = true;
  595. unsigned ColumnsUsed = State.Column;
  596. // We can only affect layout of the first and the last line, so the penalty
  597. // for all other lines is constant, and we ignore it.
  598. State.Column = Current.LastLineColumnWidth;
  599. if (ColumnsUsed > getColumnLimit(State))
  600. return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
  601. return 0;
  602. }
  603. static bool getRawStringLiteralPrefixPostfix(StringRef Text,
  604. StringRef &Prefix,
  605. StringRef &Postfix) {
  606. if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") ||
  607. Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") ||
  608. Text.startswith(Prefix = "LR\"")) {
  609. size_t ParenPos = Text.find('(');
  610. if (ParenPos != StringRef::npos) {
  611. StringRef Delimiter =
  612. Text.substr(Prefix.size(), ParenPos - Prefix.size());
  613. Prefix = Text.substr(0, ParenPos + 1);
  614. Postfix = Text.substr(Text.size() - 2 - Delimiter.size());
  615. return Postfix.front() == ')' && Postfix.back() == '"' &&
  616. Postfix.substr(1).startswith(Delimiter);
  617. }
  618. }
  619. return false;
  620. }
  621. unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
  622. LineState &State,
  623. bool DryRun) {
  624. // Don't break multi-line tokens other than block comments. Instead, just
  625. // update the state.
  626. if (Current.Type != TT_BlockComment && Current.IsMultiline)
  627. return addMultilineToken(Current, State);
  628. if (!Current.isOneOf(tok::string_literal, tok::wide_string_literal,
  629. tok::utf8_string_literal, tok::utf16_string_literal,
  630. tok::utf32_string_literal, tok::comment))
  631. return 0;
  632. llvm::OwningPtr<BreakableToken> Token;
  633. unsigned StartColumn = State.Column - Current.ColumnWidth;
  634. if (Current.isOneOf(tok::string_literal, tok::wide_string_literal,
  635. tok::utf8_string_literal, tok::utf16_string_literal,
  636. tok::utf32_string_literal) &&
  637. Current.Type != TT_ImplicitStringLiteral) {
  638. // Exempts unterminated string literals from line breaking. The user will
  639. // likely want to terminate the string before any line breaking is done.
  640. if (Current.IsUnterminatedLiteral)
  641. return 0;
  642. StringRef Text = Current.TokenText;
  643. StringRef Prefix;
  644. StringRef Postfix;
  645. // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
  646. // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
  647. // reduce the overhead) for each FormatToken, which is a string, so that we
  648. // don't run multiple checks here on the hot path.
  649. if ((Text.endswith(Postfix = "\"") &&
  650. (Text.startswith(Prefix = "\"") || Text.startswith(Prefix = "u\"") ||
  651. Text.startswith(Prefix = "U\"") || Text.startswith(Prefix = "u8\"") ||
  652. Text.startswith(Prefix = "L\""))) ||
  653. (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) ||
  654. getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) {
  655. Token.reset(new BreakableStringLiteral(
  656. Current, State.Line->Level, StartColumn, Prefix, Postfix,
  657. State.Line->InPPDirective, Encoding, Style));
  658. } else {
  659. return 0;
  660. }
  661. } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) {
  662. Token.reset(new BreakableBlockComment(
  663. Current, State.Line->Level, StartColumn, Current.OriginalColumn,
  664. !Current.Previous, State.Line->InPPDirective, Encoding, Style));
  665. } else if (Current.Type == TT_LineComment &&
  666. (Current.Previous == NULL ||
  667. Current.Previous->Type != TT_ImplicitStringLiteral)) {
  668. Token.reset(new BreakableLineComment(Current, State.Line->Level,
  669. StartColumn, State.Line->InPPDirective,
  670. Encoding, Style));
  671. } else {
  672. return 0;
  673. }
  674. if (Current.UnbreakableTailLength >= getColumnLimit(State))
  675. return 0;
  676. unsigned RemainingSpace =
  677. getColumnLimit(State) - Current.UnbreakableTailLength;
  678. bool BreakInserted = false;
  679. unsigned Penalty = 0;
  680. unsigned RemainingTokenColumns = 0;
  681. for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
  682. LineIndex != EndIndex; ++LineIndex) {
  683. if (!DryRun)
  684. Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
  685. unsigned TailOffset = 0;
  686. RemainingTokenColumns =
  687. Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
  688. while (RemainingTokenColumns > RemainingSpace) {
  689. BreakableToken::Split Split =
  690. Token->getSplit(LineIndex, TailOffset, getColumnLimit(State));
  691. if (Split.first == StringRef::npos) {
  692. // The last line's penalty is handled in addNextStateToQueue().
  693. if (LineIndex < EndIndex - 1)
  694. Penalty += Style.PenaltyExcessCharacter *
  695. (RemainingTokenColumns - RemainingSpace);
  696. break;
  697. }
  698. assert(Split.first != 0);
  699. unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
  700. LineIndex, TailOffset + Split.first + Split.second, StringRef::npos);
  701. assert(NewRemainingTokenColumns < RemainingTokenColumns);
  702. if (!DryRun)
  703. Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
  704. Penalty += Current.SplitPenalty;
  705. unsigned ColumnsUsed =
  706. Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
  707. if (ColumnsUsed > getColumnLimit(State)) {
  708. Penalty += Style.PenaltyExcessCharacter *
  709. (ColumnsUsed - getColumnLimit(State));
  710. }
  711. TailOffset += Split.first + Split.second;
  712. RemainingTokenColumns = NewRemainingTokenColumns;
  713. BreakInserted = true;
  714. }
  715. }
  716. State.Column = RemainingTokenColumns;
  717. if (BreakInserted) {
  718. // If we break the token inside a parameter list, we need to break before
  719. // the next parameter on all levels, so that the next parameter is clearly
  720. // visible. Line comments already introduce a break.
  721. if (Current.Type != TT_LineComment) {
  722. for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
  723. State.Stack[i].BreakBeforeParameter = true;
  724. }
  725. Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString
  726. : Style.PenaltyBreakComment;
  727. State.Stack.back().LastSpace = StartColumn;
  728. }
  729. return Penalty;
  730. }
  731. unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
  732. // In preprocessor directives reserve two chars for trailing " \"
  733. return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
  734. }
  735. bool ContinuationIndenter::NextIsMultilineString(const LineState &State) {
  736. const FormatToken &Current = *State.NextToken;
  737. if (!Current.is(tok::string_literal))
  738. return false;
  739. // We never consider raw string literals "multiline" for the purpose of
  740. // AlwaysBreakBeforeMultilineStrings implementation.
  741. if (Current.TokenText.startswith("R\""))
  742. return false;
  743. if (Current.IsMultiline)
  744. return true;
  745. if (Current.getNextNonComment() &&
  746. Current.getNextNonComment()->is(tok::string_literal))
  747. return true; // Implicit concatenation.
  748. if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
  749. Style.ColumnLimit)
  750. return true; // String will be split.
  751. return false;
  752. }
  753. } // namespace format
  754. } // namespace clang