ContinuationIndenter.cpp 32 KB

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