blob: 1a7dbe0936511686704e6d5f6313e51c5edd7382 [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- Format.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 functions declared in Format.h. This will be
12/// split into separate files as we go.
13///
Daniel Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Manuel Klimek24998102013-01-16 14:55:28 +000016#define DEBUG_TYPE "format-formatter"
17
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000018#include "TokenAnnotator.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "UnwrappedLineParser.h"
Alexander Kornienko5b7157a2013-01-10 15:05:09 +000020#include "clang/Basic/Diagnostic.h"
Daniel Jasperab7654e2012-12-21 10:20:02 +000021#include "clang/Basic/OperatorPrecedence.h"
Chandler Carruth44eb4f62013-01-02 10:28:36 +000022#include "clang/Basic/SourceManager.h"
Manuel Klimek24998102013-01-16 14:55:28 +000023#include "clang/Format/Format.h"
Alexander Kornienko5b7157a2013-01-10 15:05:09 +000024#include "clang/Frontend/TextDiagnosticPrinter.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000025#include "clang/Lex/Lexer.h"
Manuel Klimek2ef908e2013-02-13 10:46:36 +000026#include "llvm/Support/Allocator.h"
Manuel Klimek24998102013-01-16 14:55:28 +000027#include "llvm/Support/Debug.h"
Manuel Klimek2ef908e2013-02-13 10:46:36 +000028#include <queue>
Daniel Jasper8b529712012-12-04 13:02:32 +000029#include <string>
30
Daniel Jasperf7935112012-12-03 18:12:45 +000031namespace clang {
32namespace format {
33
Daniel Jasperf7935112012-12-03 18:12:45 +000034FormatStyle getLLVMStyle() {
35 FormatStyle LLVMStyle;
36 LLVMStyle.ColumnLimit = 80;
37 LLVMStyle.MaxEmptyLinesToKeep = 1;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +000038 LLVMStyle.PointerBindsToType = false;
39 LLVMStyle.DerivePointerBinding = false;
Daniel Jasperf7935112012-12-03 18:12:45 +000040 LLVMStyle.AccessModifierOffset = -2;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +000041 LLVMStyle.Standard = FormatStyle::LS_Cpp03;
Alexander Kornienko578fdd82012-12-06 18:03:27 +000042 LLVMStyle.IndentCaseLabels = false;
Daniel Jasper5ad1e192013-01-07 11:09:06 +000043 LLVMStyle.SpacesBeforeTrailingComments = 1;
Daniel Jasper9278eb92013-01-16 14:59:02 +000044 LLVMStyle.BinPackParameters = true;
Daniel Jasperf7db4332013-01-29 16:03:49 +000045 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper2408a8c2013-01-11 11:37:55 +000046 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Daniel Jasper1b750ed2013-01-14 16:24:39 +000047 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Nico Webera6087752013-01-10 20:12:55 +000048 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Daniel Jasper3a9370c2013-02-04 07:21:18 +000049 LLVMStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasperb9caeac2013-02-13 20:33:44 +000050 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 5;
Daniel Jasperf7935112012-12-03 18:12:45 +000051 return LLVMStyle;
52}
53
54FormatStyle getGoogleStyle() {
55 FormatStyle GoogleStyle;
56 GoogleStyle.ColumnLimit = 80;
57 GoogleStyle.MaxEmptyLinesToKeep = 1;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +000058 GoogleStyle.PointerBindsToType = true;
59 GoogleStyle.DerivePointerBinding = true;
Daniel Jasperf7935112012-12-03 18:12:45 +000060 GoogleStyle.AccessModifierOffset = -1;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +000061 GoogleStyle.Standard = FormatStyle::LS_Auto;
Alexander Kornienko578fdd82012-12-06 18:03:27 +000062 GoogleStyle.IndentCaseLabels = true;
Daniel Jasper5ad1e192013-01-07 11:09:06 +000063 GoogleStyle.SpacesBeforeTrailingComments = 2;
Daniel Jasper2cf17bf2013-02-27 09:47:53 +000064 GoogleStyle.BinPackParameters = true;
Daniel Jasperf7db4332013-01-29 16:03:49 +000065 GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper2408a8c2013-01-11 11:37:55 +000066 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
Daniel Jasperced17f82013-01-16 15:44:34 +000067 GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
Nico Webera6087752013-01-10 20:12:55 +000068 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Daniel Jasper3a9370c2013-02-04 07:21:18 +000069 GoogleStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasperb9caeac2013-02-13 20:33:44 +000070 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 100;
Daniel Jasperf7935112012-12-03 18:12:45 +000071 return GoogleStyle;
72}
73
Daniel Jasper1b750ed2013-01-14 16:24:39 +000074FormatStyle getChromiumStyle() {
75 FormatStyle ChromiumStyle = getGoogleStyle();
Daniel Jasperf7db4332013-01-29 16:03:49 +000076 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Daniel Jasper2cf17bf2013-02-27 09:47:53 +000077 ChromiumStyle.BinPackParameters = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +000078 ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
79 ChromiumStyle.DerivePointerBinding = false;
Daniel Jasper1b750ed2013-01-14 16:24:39 +000080 return ChromiumStyle;
81}
82
Daniel Jasper94f0e132013-02-06 20:07:35 +000083static bool isTrailingComment(const AnnotatedToken &Tok) {
84 return Tok.is(tok::comment) &&
85 (Tok.Children.empty() || Tok.Children[0].MustBreakBefore);
86}
87
Daniel Jasperd1ae3582013-03-20 12:37:50 +000088static bool isComparison(const AnnotatedToken &Tok) {
89 prec::Level Precedence = getPrecedence(Tok);
90 return Tok.Type == TT_BinaryOperator &&
91 (Precedence == prec::Equality || Precedence == prec::Relational);
92}
93
Daniel Jasperacc33662013-02-08 08:22:00 +000094// Returns the length of everything up to the first possible line break after
95// the ), ], } or > matching \c Tok.
96static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) {
97 if (Tok.MatchingParen == NULL)
98 return 0;
99 AnnotatedToken *End = Tok.MatchingParen;
100 while (!End->Children.empty() && !End->Children[0].CanBreakBefore) {
101 End = &End->Children[0];
102 }
103 return End->TotalLength - Tok.TotalLength + 1;
104}
105
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000106/// \brief Manages the whitespaces around tokens and their replacements.
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000107///
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000108/// This includes special handling for certain constructs, e.g. the alignment of
109/// trailing line comments.
110class WhitespaceManager {
111public:
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000112 WhitespaceManager(SourceManager &SourceMgr, const FormatStyle &Style)
113 : SourceMgr(SourceMgr), Style(Style) {}
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000114
115 /// \brief Replaces the whitespace in front of \p Tok. Only call once for
116 /// each \c AnnotatedToken.
117 void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000118 unsigned Spaces, unsigned WhitespaceStartColumn) {
Daniel Jasper304a9862013-01-21 22:49:20 +0000119 // 2+ newlines mean an empty line separating logic scopes.
120 if (NewLines >= 2)
121 alignComments();
122
123 // Align line comments if they are trailing or if they continue other
124 // trailing comments.
Daniel Jasper3324cbe2013-03-01 16:45:59 +0000125 if (isTrailingComment(Tok)) {
126 // Remove the comment's trailing whitespace.
127 if (Tok.FormatTok.Tok.getLength() != Tok.FormatTok.TokenLength)
128 Replaces.insert(tooling::Replacement(
129 SourceMgr, Tok.FormatTok.Tok.getLocation().getLocWithOffset(
130 Tok.FormatTok.TokenLength),
131 Tok.FormatTok.Tok.getLength() - Tok.FormatTok.TokenLength, ""));
132
133 // Align comment with other comments.
134 if (Tok.Parent != NULL || !Comments.empty()) {
135 if (Style.ColumnLimit >=
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000136 Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
Alexander Kornienkodd8ed852013-03-14 16:10:54 +0000137 StoredComment Comment;
138 Comment.Tok = Tok.FormatTok;
139 Comment.Spaces = Spaces;
140 Comment.NewLines = NewLines;
141 Comment.MinColumn =
142 NewLines > 0 ? Spaces : WhitespaceStartColumn + Spaces;
143 Comment.MaxColumn = Style.ColumnLimit - Tok.FormatTok.TokenLength;
144 Comments.push_back(Comment);
Daniel Jasper3324cbe2013-03-01 16:45:59 +0000145 return;
146 }
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000147 }
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000148 }
Daniel Jasper304a9862013-01-21 22:49:20 +0000149
150 // If this line does not have a trailing comment, align the stored comments.
Daniel Jasper94f0e132013-02-06 20:07:35 +0000151 if (Tok.Children.empty() && !isTrailingComment(Tok))
Daniel Jasper304a9862013-01-21 22:49:20 +0000152 alignComments();
Alexander Kornienkodd8ed852013-03-14 16:10:54 +0000153
154 if (Tok.Type == TT_BlockComment)
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000155 indentBlockComment(Tok, Spaces, false);
Alexander Kornienkodd8ed852013-03-14 16:10:54 +0000156
Manuel Klimek1998ea22013-02-20 10:15:13 +0000157 storeReplacement(Tok.FormatTok, getNewLineText(NewLines, Spaces));
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000158 }
159
160 /// \brief Like \c replaceWhitespace, but additionally adds right-aligned
161 /// backslashes to escape newlines inside a preprocessor directive.
162 ///
163 /// This function and \c replaceWhitespace have the same behavior if
164 /// \c Newlines == 0.
165 void replacePPWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000166 unsigned Spaces, unsigned WhitespaceStartColumn) {
167 if (Tok.Type == TT_BlockComment)
168 indentBlockComment(Tok, Spaces, true);
169
170 storeReplacement(Tok.FormatTok,
171 getNewLineText(NewLines, Spaces, WhitespaceStartColumn));
Manuel Klimek1998ea22013-02-20 10:15:13 +0000172 }
173
174 /// \brief Inserts a line break into the middle of a token.
175 ///
176 /// Will break at \p Offset inside \p Tok, putting \p Prefix before the line
177 /// break and \p Postfix before the rest of the token starts in the next line.
178 ///
179 /// \p InPPDirective, \p Spaces, \p WhitespaceStartColumn and \p Style are
180 /// used to generate the correct line break.
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000181 void breakToken(const FormatToken &Tok, unsigned Offset,
182 unsigned ReplaceChars, StringRef Prefix, StringRef Postfix,
183 bool InPPDirective, unsigned Spaces,
184 unsigned WhitespaceStartColumn) {
Manuel Klimek1998ea22013-02-20 10:15:13 +0000185 std::string NewLineText;
186 if (!InPPDirective)
187 NewLineText = getNewLineText(1, Spaces);
188 else
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000189 NewLineText = getNewLineText(1, Spaces, WhitespaceStartColumn);
Manuel Klimek1998ea22013-02-20 10:15:13 +0000190 std::string ReplacementText = (Prefix + NewLineText + Postfix).str();
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000191 SourceLocation Location = Tok.Tok.getLocation().getLocWithOffset(Offset);
192 Replaces.insert(tooling::Replacement(SourceMgr, Location, ReplaceChars,
193 ReplacementText));
Manuel Klimek1998ea22013-02-20 10:15:13 +0000194 }
195
196 /// \brief Returns all the \c Replacements created during formatting.
197 const tooling::Replacements &generateReplacements() {
198 alignComments();
199 return Replaces;
200 }
201
202private:
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000203 /// \brief Finds a common prefix of lines of a block comment to properly
204 /// indent (and possibly decorate with '*'s) added lines.
205 ///
206 /// The first line is ignored (it's special and starts with /*).
207 /// When there are less than three lines, we don't have enough information, so
208 /// better use no prefix.
209 static StringRef findCommentLinesPrefix(ArrayRef<StringRef> Lines,
210 const char *PrefixChars = " *") {
211 if (Lines.size() < 3)
212 return "";
213 StringRef Prefix(Lines[1].data(), Lines[1].find_first_not_of(PrefixChars));
214 for (size_t i = 2; i < Lines.size(); ++i) {
215 for (size_t j = 0; j < Prefix.size() && j < Lines[i].size(); ++j) {
216 if (Prefix[j] != Lines[i][j]) {
217 Prefix = Prefix.substr(0, j);
218 break;
219 }
220 }
221 }
222 return Prefix;
223 }
224
225 void splitLineInComment(const FormatToken &Tok, StringRef Line,
226 size_t StartColumn, StringRef LinePrefix,
227 bool InPPDirective, bool CommentHasMoreLines,
228 const char *WhiteSpaceChars = " ") {
229 size_t ColumnLimit =
230 Style.ColumnLimit - LinePrefix.size() - (InPPDirective ? 2 : 0);
231
232 if (Line.size() <= ColumnLimit)
233 return;
234
235 const char *TokenStart = SourceMgr.getCharacterData(Tok.Tok.getLocation());
236 while (Line.rtrim().size() > ColumnLimit) {
237 // Try to break at the last whitespace before the column limit.
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000238 size_t SpacePos = Line.find_last_of(WhiteSpaceChars, ColumnLimit + 1);
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000239 if (SpacePos == StringRef::npos) {
240 // Try to find any whitespace in the line.
241 SpacePos = Line.find_first_of(WhiteSpaceChars);
242 if (SpacePos == StringRef::npos) // No whitespace found, give up.
243 break;
244 }
245
246 StringRef NextCut = Line.substr(0, SpacePos).rtrim();
247 StringRef RemainingLine = Line.substr(SpacePos).ltrim();
248 if (RemainingLine.empty())
249 break;
250 Line = RemainingLine;
251
252 size_t ReplaceChars = Line.begin() - NextCut.end();
253 breakToken(Tok, NextCut.end() - TokenStart, ReplaceChars, "", LinePrefix,
254 InPPDirective, 0,
255 NextCut.size() + LinePrefix.size() + StartColumn);
256 StartColumn = 0;
257 }
258
259 StringRef TrimmedLine = Line.rtrim();
260 if (TrimmedLine != Line || (InPPDirective && CommentHasMoreLines)) {
261 // Remove trailing whitespace/insert backslash.
262 breakToken(Tok, TrimmedLine.end() - TokenStart,
263 Line.size() - TrimmedLine.size() + 1, "", "", InPPDirective, 0,
264 TrimmedLine.size() + LinePrefix.size());
265 }
266 }
267
268 void indentBlockComment(const AnnotatedToken &Tok, int Indent,
269 bool InPPDirective) {
270 const SourceLocation TokenLoc = Tok.FormatTok.Tok.getLocation();
271 const int CurrentIndent = SourceMgr.getSpellingColumnNumber(TokenLoc) - 1;
272 const int IndentDelta = Indent - CurrentIndent;
273 const StringRef Text(SourceMgr.getCharacterData(TokenLoc),
274 Tok.FormatTok.TokenLength);
275 assert(Text.startswith("/*") && Text.endswith("*/"));
276
277 SmallVector<StringRef, 16> Lines;
278 Text.split(Lines, "\n");
279
280 if (IndentDelta > 0) {
281 std::string WhiteSpace(IndentDelta, ' ');
282 for (size_t i = 1; i < Lines.size(); ++i) {
283 Replaces.insert(tooling::Replacement(
284 SourceMgr, TokenLoc.getLocWithOffset(Lines[i].data() - Text.data()),
285 0, WhiteSpace));
286 }
287 } else if (IndentDelta < 0) {
288 std::string WhiteSpace(-IndentDelta, ' ');
289 // Check that the line is indented enough.
290 for (size_t i = 1; i < Lines.size(); ++i) {
291 if (!Lines[i].startswith(WhiteSpace))
292 return;
293 }
294 for (size_t i = 1; i < Lines.size(); ++i) {
295 Replaces.insert(tooling::Replacement(
296 SourceMgr, TokenLoc.getLocWithOffset(Lines[i].data() - Text.data()),
297 -IndentDelta, ""));
Alexander Kornienkodd8ed852013-03-14 16:10:54 +0000298 }
299 }
Alexander Kornienko79d6c722013-03-15 13:42:02 +0000300
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000301 // Split long lines in comments.
302 const StringRef CurrentPrefix = findCommentLinesPrefix(Lines);
303 size_t PrefixSize = CurrentPrefix.size();
304 std::string NewPrefix =
305 (IndentDelta < 0) ? CurrentPrefix.substr(-IndentDelta).str()
306 : std::string(IndentDelta, ' ') + CurrentPrefix.str();
307
308 if (CurrentPrefix.endswith("*")) {
309 NewPrefix += " ";
310 ++PrefixSize;
311 }
312
313 for (size_t i = 0; i < Lines.size(); ++i) {
314 StringRef Line = (i == 0) ? Lines[i] : Lines[i].substr(PrefixSize);
315 size_t StartColumn = (i == 0) ? CurrentIndent : 0;
316 splitLineInComment(Tok.FormatTok, Line, StartColumn, NewPrefix,
317 InPPDirective, i != Lines.size() - 1);
Alexander Kornienko79d6c722013-03-15 13:42:02 +0000318 }
Alexander Kornienkodd8ed852013-03-14 16:10:54 +0000319 }
320
Manuel Klimek1998ea22013-02-20 10:15:13 +0000321 std::string getNewLineText(unsigned NewLines, unsigned Spaces) {
322 return std::string(NewLines, '\n') + std::string(Spaces, ' ');
323 }
324
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000325 std::string getNewLineText(unsigned NewLines, unsigned Spaces,
326 unsigned WhitespaceStartColumn) {
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000327 std::string NewLineText;
328 if (NewLines > 0) {
Daniel Jasperbbc84152013-01-29 11:27:30 +0000329 unsigned Offset =
330 std::min<int>(Style.ColumnLimit - 1, WhitespaceStartColumn);
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000331 for (unsigned i = 0; i < NewLines; ++i) {
332 NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' ');
333 NewLineText += "\\\n";
334 Offset = 0;
335 }
336 }
Manuel Klimek1998ea22013-02-20 10:15:13 +0000337 return NewLineText + std::string(Spaces, ' ');
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000338 }
339
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000340 /// \brief Structure to store a comment for later layout and alignment.
341 struct StoredComment {
342 FormatToken Tok;
343 unsigned MinColumn;
344 unsigned MaxColumn;
345 unsigned NewLines;
346 unsigned Spaces;
347 };
348 SmallVector<StoredComment, 16> Comments;
349 typedef SmallVector<StoredComment, 16>::iterator comment_iterator;
350
351 /// \brief Try to align all stashed comments.
352 void alignComments() {
353 unsigned MinColumn = 0;
354 unsigned MaxColumn = UINT_MAX;
355 comment_iterator Start = Comments.begin();
Alexander Kornienkodd8ed852013-03-14 16:10:54 +0000356 for (comment_iterator I = Start, E = Comments.end(); I != E; ++I) {
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000357 if (I->MinColumn > MaxColumn || I->MaxColumn < MinColumn) {
358 alignComments(Start, I, MinColumn);
359 MinColumn = I->MinColumn;
360 MaxColumn = I->MaxColumn;
361 Start = I;
362 } else {
363 MinColumn = std::max(MinColumn, I->MinColumn);
364 MaxColumn = std::min(MaxColumn, I->MaxColumn);
365 }
366 }
367 alignComments(Start, Comments.end(), MinColumn);
368 Comments.clear();
369 }
370
371 /// \brief Put all the comments between \p I and \p E into \p Column.
372 void alignComments(comment_iterator I, comment_iterator E, unsigned Column) {
373 while (I != E) {
374 unsigned Spaces = I->Spaces + Column - I->MinColumn;
Daniel Jasperaab220f2013-03-20 13:53:11 +0000375 storeReplacement(
376 I->Tok, std::string(I->NewLines, '\n') + std::string(Spaces, ' '));
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000377 ++I;
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000378 }
379 }
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000380
381 /// \brief Stores \p Text as the replacement for the whitespace in front of
382 /// \p Tok.
383 void storeReplacement(const FormatToken &Tok, const std::string Text) {
Daniel Jasper7b038a22013-01-30 09:46:12 +0000384 // Don't create a replacement, if it does not change anything.
385 if (StringRef(SourceMgr.getCharacterData(Tok.WhiteSpaceStart),
386 Tok.WhiteSpaceLength) == Text)
387 return;
388
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000389 Replaces.insert(tooling::Replacement(SourceMgr, Tok.WhiteSpaceStart,
390 Tok.WhiteSpaceLength, Text));
391 }
392
393 SourceManager &SourceMgr;
394 tooling::Replacements Replaces;
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000395 const FormatStyle &Style;
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000396};
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000397
Daniel Jasperf7935112012-12-03 18:12:45 +0000398class UnwrappedLineFormatter {
399public:
Manuel Klimekb2c6dbe2013-01-10 19:17:33 +0000400 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000401 const AnnotatedLine &Line, unsigned FirstIndent,
Daniel Jaspera67a8f02013-01-16 10:41:46 +0000402 const AnnotatedToken &RootToken,
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000403 WhitespaceManager &Whitespaces, bool StructuralError)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000404 : Style(Style), SourceMgr(SourceMgr), Line(Line),
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000405 FirstIndent(FirstIndent), RootToken(RootToken),
Daniel Jasper12ef4e52013-02-21 21:33:55 +0000406 Whitespaces(Whitespaces), Count(0) {}
Daniel Jasperf7935112012-12-03 18:12:45 +0000407
Manuel Klimek1abf7892013-01-04 23:34:14 +0000408 /// \brief Formats an \c UnwrappedLine.
409 ///
410 /// \returns The column after the last token in the last line of the
411 /// \c UnwrappedLine.
Daniel Jasperc22f5b42013-02-28 11:05:57 +0000412 unsigned format(const AnnotatedLine *NextLine) {
Daniel Jaspere9de2602012-12-06 09:56:08 +0000413 // Initialize state dependent on indent.
Daniel Jasper337816e2013-01-11 10:22:12 +0000414 LineState State;
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000415 State.Column = FirstIndent;
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000416 State.NextToken = &RootToken;
Daniel Jasper97b89482013-03-13 07:49:51 +0000417 State.Stack.push_back(
418 ParenState(FirstIndent + 4, FirstIndent, !Style.BinPackParameters,
419 /*HasMultiParameterLine=*/ false));
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000420 State.VariablePos = 0;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000421 State.LineContainsContinuedForLoopSection = false;
Daniel Jasper400adc62013-02-08 15:28:42 +0000422 State.ParenLevel = 0;
Manuel Klimek02f640a2013-02-20 15:25:48 +0000423 State.StartOfStringLiteral = 0;
Daniel Jasper40c36c52013-02-18 11:05:07 +0000424 State.StartOfLineLevel = State.ParenLevel;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000425
Manuel Klimek24998102013-01-16 14:55:28 +0000426 DEBUG({
427 DebugTokenState(*State.NextToken);
428 });
429
Daniel Jaspere9de2602012-12-06 09:56:08 +0000430 // The first token has already been indented and thus consumed.
Manuel Klimek1998ea22013-02-20 10:15:13 +0000431 moveStateToNextToken(State, /*DryRun=*/ false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000432
Daniel Jasper4b866272013-02-01 11:00:45 +0000433 // If everything fits on a single line, just put it there.
Daniel Jasperc22f5b42013-02-28 11:05:57 +0000434 unsigned ColumnLimit = Style.ColumnLimit;
435 if (NextLine && NextLine->InPPDirective &&
436 !NextLine->First.FormatTok.HasUnescapedNewline)
437 ColumnLimit = getColumnLimit();
438 if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) {
Daniel Jasper4b866272013-02-01 11:00:45 +0000439 while (State.NextToken != NULL) {
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000440 addTokenToState(false, false, State);
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000441 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000442 return State.Column;
Daniel Jasperf7935112012-12-03 18:12:45 +0000443 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000444
Daniel Jasperacc33662013-02-08 08:22:00 +0000445 // If the ObjC method declaration does not fit on a line, we should format
446 // it with one arg per line.
447 if (Line.Type == LT_ObjCMethodDecl)
448 State.Stack.back().BreakBeforeParameter = true;
449
Daniel Jasper4b866272013-02-01 11:00:45 +0000450 // Find best solution in solution space.
451 return analyzeSolutionSpace(State);
Daniel Jasperf7935112012-12-03 18:12:45 +0000452 }
453
454private:
Manuel Klimek24998102013-01-16 14:55:28 +0000455 void DebugTokenState(const AnnotatedToken &AnnotatedTok) {
456 const Token &Tok = AnnotatedTok.FormatTok.Tok;
Daniel Jasperbbc84152013-01-29 11:27:30 +0000457 llvm::errs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
458 Tok.getLength());
Manuel Klimek24998102013-01-16 14:55:28 +0000459 llvm::errs();
460 }
461
Daniel Jasper337816e2013-01-11 10:22:12 +0000462 struct ParenState {
Daniel Jasperb9ebd5d2013-02-05 09:41:21 +0000463 ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking,
464 bool HasMultiParameterLine)
Daniel Jasper400adc62013-02-08 15:28:42 +0000465 : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
466 BreakBeforeClosingBrace(false), QuestionColumn(0),
Daniel Jasperacc33662013-02-08 08:22:00 +0000467 AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
Daniel Jasperf9a84b52013-03-01 16:48:32 +0000468 HasMultiParameterLine(HasMultiParameterLine), ColonPos(0),
469 StartOfFunctionCall(0) {}
Daniel Jasper6d822722012-12-24 16:43:00 +0000470
Daniel Jasperf7935112012-12-03 18:12:45 +0000471 /// \brief The position to which a specific parenthesis level needs to be
472 /// indented.
Daniel Jasper337816e2013-01-11 10:22:12 +0000473 unsigned Indent;
Daniel Jasperf7935112012-12-03 18:12:45 +0000474
Daniel Jaspere9de2602012-12-06 09:56:08 +0000475 /// \brief The position of the last space on each level.
476 ///
477 /// Used e.g. to break like:
478 /// functionCall(Parameter, otherCall(
479 /// OtherParameter));
Daniel Jasper337816e2013-01-11 10:22:12 +0000480 unsigned LastSpace;
Daniel Jasperf7935112012-12-03 18:12:45 +0000481
Daniel Jaspere9de2602012-12-06 09:56:08 +0000482 /// \brief The position the first "<<" operator encountered on each level.
483 ///
484 /// Used to align "<<" operators. 0 if no such operator has been encountered
485 /// on a level.
Daniel Jasper337816e2013-01-11 10:22:12 +0000486 unsigned FirstLessLess;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000487
Manuel Klimek0ddd57a2013-01-10 15:58:26 +0000488 /// \brief Whether a newline needs to be inserted before the block's closing
489 /// brace.
490 ///
491 /// We only want to insert a newline before the closing brace if there also
492 /// was a newline after the beginning left brace.
Daniel Jasper337816e2013-01-11 10:22:12 +0000493 bool BreakBeforeClosingBrace;
494
Daniel Jasperca6623b2013-01-28 12:45:14 +0000495 /// \brief The column of a \c ? in a conditional expression;
496 unsigned QuestionColumn;
497
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000498 /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple
499 /// lines, in this context.
500 bool AvoidBinPacking;
501
502 /// \brief Break after the next comma (or all the commas in this context if
503 /// \c AvoidBinPacking is \c true).
Daniel Jasperacc33662013-02-08 08:22:00 +0000504 bool BreakBeforeParameter;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000505
506 /// \brief This context already has a line with more than one parameter.
Daniel Jasper9278eb92013-01-16 14:59:02 +0000507 bool HasMultiParameterLine;
Daniel Jasper2408a8c2013-01-11 11:37:55 +0000508
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000509 /// \brief The position of the colon in an ObjC method declaration/call.
510 unsigned ColonPos;
Daniel Jasperdc7d5812013-02-20 12:56:39 +0000511
Daniel Jasperf9a84b52013-03-01 16:48:32 +0000512 /// \brief The start of the most recent function in a builder-type call.
513 unsigned StartOfFunctionCall;
514
Daniel Jasper337816e2013-01-11 10:22:12 +0000515 bool operator<(const ParenState &Other) const {
516 if (Indent != Other.Indent)
Daniel Jasperfd8c4b12013-01-11 14:23:32 +0000517 return Indent < Other.Indent;
Daniel Jasper337816e2013-01-11 10:22:12 +0000518 if (LastSpace != Other.LastSpace)
519 return LastSpace < Other.LastSpace;
520 if (FirstLessLess != Other.FirstLessLess)
521 return FirstLessLess < Other.FirstLessLess;
Daniel Jasper2408a8c2013-01-11 11:37:55 +0000522 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
523 return BreakBeforeClosingBrace;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000524 if (QuestionColumn != Other.QuestionColumn)
525 return QuestionColumn < Other.QuestionColumn;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000526 if (AvoidBinPacking != Other.AvoidBinPacking)
527 return AvoidBinPacking;
Daniel Jasperacc33662013-02-08 08:22:00 +0000528 if (BreakBeforeParameter != Other.BreakBeforeParameter)
529 return BreakBeforeParameter;
Daniel Jasper9278eb92013-01-16 14:59:02 +0000530 if (HasMultiParameterLine != Other.HasMultiParameterLine)
531 return HasMultiParameterLine;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000532 if (ColonPos != Other.ColonPos)
533 return ColonPos < Other.ColonPos;
Daniel Jasperf9a84b52013-03-01 16:48:32 +0000534 if (StartOfFunctionCall != Other.StartOfFunctionCall)
535 return StartOfFunctionCall < Other.StartOfFunctionCall;
Daniel Jasper7b7877a2013-01-12 07:36:22 +0000536 return false;
Daniel Jasper337816e2013-01-11 10:22:12 +0000537 }
538 };
539
540 /// \brief The current state when indenting a unwrapped line.
541 ///
542 /// As the indenting tries different combinations this is copied by value.
543 struct LineState {
544 /// \brief The number of used columns in the current line.
545 unsigned Column;
546
547 /// \brief The token that needs to be next formatted.
548 const AnnotatedToken *NextToken;
549
Daniel Jasperbbc84152013-01-29 11:27:30 +0000550 /// \brief The column of the first variable name in a variable declaration.
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000551 ///
Daniel Jasperbbc84152013-01-29 11:27:30 +0000552 /// Used to align further variables if necessary.
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000553 unsigned VariablePos;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000554
555 /// \brief \c true if this line contains a continued for-loop section.
556 bool LineContainsContinuedForLoopSection;
557
Daniel Jasper400adc62013-02-08 15:28:42 +0000558 /// \brief The level of nesting inside (), [], <> and {}.
559 unsigned ParenLevel;
560
Daniel Jasper40c36c52013-02-18 11:05:07 +0000561 /// \brief The \c ParenLevel at the start of this line.
562 unsigned StartOfLineLevel;
563
Manuel Klimek02f640a2013-02-20 15:25:48 +0000564 /// \brief The start column of the string literal, if we're in a string
565 /// literal sequence, 0 otherwise.
566 unsigned StartOfStringLiteral;
567
Daniel Jasper337816e2013-01-11 10:22:12 +0000568 /// \brief A stack keeping track of properties applying to parenthesis
569 /// levels.
570 std::vector<ParenState> Stack;
571
572 /// \brief Comparison operator to be able to used \c LineState in \c map.
573 bool operator<(const LineState &Other) const {
Daniel Jasper58f427e2013-02-19 09:28:55 +0000574 if (NextToken != Other.NextToken)
575 return NextToken < Other.NextToken;
576 if (Column != Other.Column)
577 return Column < Other.Column;
578 if (VariablePos != Other.VariablePos)
579 return VariablePos < Other.VariablePos;
580 if (LineContainsContinuedForLoopSection !=
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000581 Other.LineContainsContinuedForLoopSection)
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000582 return LineContainsContinuedForLoopSection;
Daniel Jasper58f427e2013-02-19 09:28:55 +0000583 if (ParenLevel != Other.ParenLevel)
584 return ParenLevel < Other.ParenLevel;
585 if (StartOfLineLevel != Other.StartOfLineLevel)
586 return StartOfLineLevel < Other.StartOfLineLevel;
Manuel Klimek02f640a2013-02-20 15:25:48 +0000587 if (StartOfStringLiteral != Other.StartOfStringLiteral)
588 return StartOfStringLiteral < Other.StartOfStringLiteral;
Daniel Jasper58f427e2013-02-19 09:28:55 +0000589 return Stack < Other.Stack;
Daniel Jasperf7935112012-12-03 18:12:45 +0000590 }
591 };
592
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000593 /// \brief Appends the next token to \p State and updates information
594 /// necessary for indentation.
595 ///
596 /// Puts the token on the current line if \p Newline is \c true and adds a
597 /// line break and necessary indentation otherwise.
598 ///
599 /// If \p DryRun is \c false, also creates and stores the required
600 /// \c Replacement.
Manuel Klimek1998ea22013-02-20 10:15:13 +0000601 unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) {
Daniel Jasper399d24b2013-01-09 07:06:56 +0000602 const AnnotatedToken &Current = *State.NextToken;
603 const AnnotatedToken &Previous = *State.NextToken->Parent;
Daniel Jasper337816e2013-01-11 10:22:12 +0000604 assert(State.Stack.size());
Daniel Jasperf7935112012-12-03 18:12:45 +0000605
Daniel Jasper4b866272013-02-01 11:00:45 +0000606 if (Current.Type == TT_ImplicitStringLiteral) {
607 State.Column += State.NextToken->FormatTok.WhiteSpaceLength +
608 State.NextToken->FormatTok.TokenLength;
609 if (State.NextToken->Children.empty())
610 State.NextToken = NULL;
611 else
612 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek1998ea22013-02-20 10:15:13 +0000613 return 0;
Daniel Jasper4b866272013-02-01 11:00:45 +0000614 }
615
Daniel Jasperf7935112012-12-03 18:12:45 +0000616 if (Newline) {
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000617 unsigned WhitespaceStartColumn = State.Column;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000618 if (Current.is(tok::r_brace)) {
619 State.Column = Line.Level * 2;
Daniel Jasper399d24b2013-01-09 07:06:56 +0000620 } else if (Current.is(tok::string_literal) &&
Manuel Klimek02f640a2013-02-20 15:25:48 +0000621 State.StartOfStringLiteral != 0) {
622 State.Column = State.StartOfStringLiteral;
Daniel Jasper2ec3ffb82013-02-18 11:59:17 +0000623 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasper399d24b2013-01-09 07:06:56 +0000624 } else if (Current.is(tok::lessless) &&
Daniel Jasper400adc62013-02-08 15:28:42 +0000625 State.Stack.back().FirstLessLess != 0) {
626 State.Column = State.Stack.back().FirstLessLess;
627 } else if (State.ParenLevel != 0 &&
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000628 (Previous.isOneOf(tok::equal, tok::coloncolon) ||
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000629 Current.isOneOf(tok::period, tok::arrow, tok::question) ||
630 isComparison(Previous))) {
Daniel Jasper399d24b2013-01-09 07:06:56 +0000631 // Indent and extra 4 spaces after if we know the current expression is
632 // continued. Don't do that on the top level, as we already indent 4
633 // there.
Daniel Jasperca6623b2013-01-28 12:45:14 +0000634 State.Column = std::max(State.Stack.back().LastSpace,
635 State.Stack.back().Indent) + 4;
636 } else if (Current.Type == TT_ConditionalExpr) {
637 State.Column = State.Stack.back().QuestionColumn;
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000638 } else if (Previous.is(tok::comma) && State.VariablePos != 0 &&
Daniel Jasper400adc62013-02-08 15:28:42 +0000639 ((RootToken.is(tok::kw_for) && State.ParenLevel == 1) ||
640 State.ParenLevel == 0)) {
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000641 State.Column = State.VariablePos;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000642 } else if (Previous.ClosesTemplateDeclaration ||
643 (Current.Type == TT_StartOfName && State.ParenLevel == 0)) {
Daniel Jasper400adc62013-02-08 15:28:42 +0000644 State.Column = State.Stack.back().Indent - 4;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000645 } else if (Current.Type == TT_ObjCSelectorName) {
646 if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) {
647 State.Column =
648 State.Stack.back().ColonPos - Current.FormatTok.TokenLength;
649 } else {
650 State.Column = State.Stack.back().Indent;
651 State.Stack.back().ColonPos =
652 State.Column + Current.FormatTok.TokenLength;
653 }
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000654 } else if (Previous.Type == TT_ObjCMethodExpr ||
655 Current.Type == TT_StartOfName) {
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000656 State.Column = State.Stack.back().Indent + 4;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000657 } else {
Daniel Jasper400adc62013-02-08 15:28:42 +0000658 State.Column = State.Stack.back().Indent;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000659 }
660
Daniel Jasper54a86022013-02-15 11:07:25 +0000661 if (Current.is(tok::question))
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000662 State.Stack.back().BreakBeforeParameter = true;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000663 if (Previous.isOneOf(tok::comma, tok::semi) &&
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000664 !State.Stack.back().AvoidBinPacking)
Daniel Jasperacc33662013-02-08 08:22:00 +0000665 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000666
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000667 if (!DryRun) {
Daniel Jasperfb5e2412013-02-26 13:10:34 +0000668 unsigned NewLines = 1;
669 if (Current.Type == TT_LineComment)
670 NewLines =
671 std::max(NewLines, std::min(Current.FormatTok.NewlinesBefore,
672 Style.MaxEmptyLinesToKeep + 1));
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000673 if (!Line.InPPDirective)
Daniel Jasperdc7d5812013-02-20 12:56:39 +0000674 Whitespaces.replaceWhitespace(Current, NewLines, State.Column,
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000675 WhitespaceStartColumn);
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000676 else
Daniel Jasperdc7d5812013-02-20 12:56:39 +0000677 Whitespaces.replacePPWhitespace(Current, NewLines, State.Column,
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000678 WhitespaceStartColumn);
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000679 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000680
Daniel Jasper400adc62013-02-08 15:28:42 +0000681 State.Stack.back().LastSpace = State.Column;
Daniel Jasper40c36c52013-02-18 11:05:07 +0000682 State.StartOfLineLevel = State.ParenLevel;
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000683
684 // Any break on this level means that the parent level has been broken
685 // and we need to avoid bin packing there.
686 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
687 State.Stack[i].BreakBeforeParameter = true;
688 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000689 if (Current.isOneOf(tok::period, tok::arrow))
Daniel Jasper2cf17bf2013-02-27 09:47:53 +0000690 State.Stack.back().BreakBeforeParameter = true;
691
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000692 // If we break after {, we should also break before the corresponding }.
693 if (Previous.is(tok::l_brace))
694 State.Stack.back().BreakBeforeClosingBrace = true;
695
696 if (State.Stack.back().AvoidBinPacking) {
697 // If we are breaking after '(', '{', '<', this is not bin packing
698 // unless AllowAllParametersOfDeclarationOnNextLine is false.
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000699 if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace)) ||
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000700 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
701 Line.MustBeDeclaration))
702 State.Stack.back().BreakBeforeParameter = true;
703 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000704 } else {
Daniel Jasper62e68172013-02-25 15:59:54 +0000705 // FIXME: Put VariablePos into ParenState and remove second part of if().
706 if (Current.is(tok::equal) &&
707 (RootToken.is(tok::kw_for) || State.ParenLevel == 0))
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000708 State.VariablePos = State.Column - Previous.FormatTok.TokenLength;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000709
Daniel Jaspereef30492013-02-11 12:36:37 +0000710 unsigned Spaces = State.NextToken->SpacesRequiredBefore;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000711
Daniel Jasperf7935112012-12-03 18:12:45 +0000712 if (!DryRun)
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000713 Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000714
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000715 if (Current.Type == TT_ObjCSelectorName &&
716 State.Stack.back().ColonPos == 0) {
717 if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000718 State.Column + Spaces + Current.FormatTok.TokenLength)
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000719 State.Stack.back().ColonPos =
720 State.Stack.back().Indent + Current.LongestObjCSelectorName;
721 else
722 State.Stack.back().ColonPos =
Daniel Jasperc485b4e2013-02-06 16:00:26 +0000723 State.Column + Spaces + Current.FormatTok.TokenLength;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000724 }
725
Daniel Jasperddaa9be2013-01-29 19:41:55 +0000726 if (Current.Type != TT_LineComment &&
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000727 (Previous.isOneOf(tok::l_paren, tok::l_brace) ||
Daniel Jasperddaa9be2013-01-29 19:41:55 +0000728 State.NextToken->Parent->Type == TT_TemplateOpener))
Daniel Jasper400adc62013-02-08 15:28:42 +0000729 State.Stack.back().Indent = State.Column + Spaces;
Daniel Jasper14e40ec2013-02-04 08:34:57 +0000730 if (Previous.is(tok::comma) && !isTrailingComment(Current))
Daniel Jasper400adc62013-02-08 15:28:42 +0000731 State.Stack.back().HasMultiParameterLine = true;
Daniel Jasper9278eb92013-01-16 14:59:02 +0000732
Daniel Jaspere9de2602012-12-06 09:56:08 +0000733 State.Column += Spaces;
Daniel Jasper39e27382013-01-23 20:41:06 +0000734 if (Current.is(tok::l_paren) && Previous.is(tok::kw_if))
735 // Treat the condition inside an if as if it was a second function
736 // parameter, i.e. let nested calls have an indent of 4.
737 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000738 else if (Previous.is(tok::comma))
Daniel Jasper39e27382013-01-23 20:41:06 +0000739 // Top-level spaces are exempt as that mostly leads to better results.
740 State.Stack.back().LastSpace = State.Column;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000741 else if ((Previous.Type == TT_BinaryOperator ||
Daniel Jasper65585ed2013-01-28 13:31:35 +0000742 Previous.Type == TT_ConditionalExpr ||
743 Previous.Type == TT_CtorInitializerColon) &&
Daniel Jasper20b09ef2013-01-28 09:35:24 +0000744 getPrecedence(Previous) != prec::Assignment)
745 State.Stack.back().LastSpace = State.Column;
Daniel Jaspereead02b2013-02-14 08:42:54 +0000746 else if (Previous.Type == TT_InheritanceColon)
747 State.Stack.back().Indent = State.Column;
Daniel Jasper7b5773e92013-01-28 07:35:34 +0000748 else if (Previous.ParameterCount > 1 &&
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000749 (Previous.isOneOf(tok::l_paren, tok::l_square, tok::l_brace) ||
Daniel Jasper7b5773e92013-01-28 07:35:34 +0000750 Previous.Type == TT_TemplateOpener))
751 // If this function has multiple parameters, indent nested calls from
752 // the start of the first parameter.
753 State.Stack.back().LastSpace = State.Column;
Daniel Jasperf7935112012-12-03 18:12:45 +0000754 }
Daniel Jasper9278eb92013-01-16 14:59:02 +0000755
Manuel Klimek1998ea22013-02-20 10:15:13 +0000756 return moveStateToNextToken(State, DryRun);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000757 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000758
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000759 /// \brief Mark the next token as consumed in \p State and modify its stacks
760 /// accordingly.
Manuel Klimek1998ea22013-02-20 10:15:13 +0000761 unsigned moveStateToNextToken(LineState &State, bool DryRun) {
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000762 const AnnotatedToken &Current = *State.NextToken;
Daniel Jasper337816e2013-01-11 10:22:12 +0000763 assert(State.Stack.size());
Daniel Jaspere9de2602012-12-06 09:56:08 +0000764
Daniel Jaspereead02b2013-02-14 08:42:54 +0000765 if (Current.Type == TT_InheritanceColon)
766 State.Stack.back().AvoidBinPacking = true;
Daniel Jasper337816e2013-01-11 10:22:12 +0000767 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
768 State.Stack.back().FirstLessLess = State.Column;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000769 if (Current.is(tok::question))
770 State.Stack.back().QuestionColumn = State.Column;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000771 if (Current.isOneOf(tok::period, tok::arrow) &&
Daniel Jasperf9a84b52013-03-01 16:48:32 +0000772 Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
773 State.Stack.back().StartOfFunctionCall =
774 Current.LastInChainOfCalls ? 0 : State.Column;
Daniel Jasper37905f72013-02-21 15:00:29 +0000775 if (Current.Type == TT_CtorInitializerColon) {
776 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
777 State.Stack.back().AvoidBinPacking = true;
778 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000779 }
Daniel Jaspere9de2602012-12-06 09:56:08 +0000780
Daniel Jasper400adc62013-02-08 15:28:42 +0000781 // Insert scopes created by fake parenthesis.
782 for (unsigned i = 0, e = Current.FakeLParens; i != e; ++i) {
783 ParenState NewParenState = State.Stack.back();
784 NewParenState.Indent = std::max(State.Column, State.Stack.back().Indent);
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000785 NewParenState.BreakBeforeParameter = false;
Daniel Jasper400adc62013-02-08 15:28:42 +0000786 State.Stack.push_back(NewParenState);
787 }
788
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000789 // If we encounter an opening (, [, { or <, we add a level to our stacks to
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000790 // prepare for the following tokens.
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000791 if (Current.isOneOf(tok::l_paren, tok::l_square, tok::l_brace) ||
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000792 State.NextToken->Type == TT_TemplateOpener) {
Daniel Jasper337816e2013-01-11 10:22:12 +0000793 unsigned NewIndent;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000794 bool AvoidBinPacking;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000795 if (Current.is(tok::l_brace)) {
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000796 NewIndent = 2 + State.Stack.back().LastSpace;
797 AvoidBinPacking = false;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000798 } else {
Daniel Jasperf9a84b52013-03-01 16:48:32 +0000799 NewIndent = 4 + std::max(State.Stack.back().LastSpace,
800 State.Stack.back().StartOfFunctionCall);
Daniel Jasperead41b62013-02-28 09:39:12 +0000801 AvoidBinPacking =
802 !Style.BinPackParameters || State.Stack.back().AvoidBinPacking;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000803 }
Daniel Jasperb9ebd5d2013-02-05 09:41:21 +0000804 State.Stack.push_back(
805 ParenState(NewIndent, State.Stack.back().LastSpace, AvoidBinPacking,
806 State.Stack.back().HasMultiParameterLine));
Daniel Jasper400adc62013-02-08 15:28:42 +0000807 ++State.ParenLevel;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000808 }
809
Daniel Jasperacc33662013-02-08 08:22:00 +0000810 // If this '[' opens an ObjC call, determine whether all parameters fit into
811 // one line and put one per line if they don't.
812 if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
813 Current.MatchingParen != NULL) {
814 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
815 State.Stack.back().BreakBeforeParameter = true;
816 }
817
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000818 // If we encounter a closing ), ], } or >, we can remove a level from our
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000819 // stacks.
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000820 if (Current.isOneOf(tok::r_paren, tok::r_square) ||
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000821 (Current.is(tok::r_brace) && State.NextToken != &RootToken) ||
822 State.NextToken->Type == TT_TemplateCloser) {
Daniel Jasper337816e2013-01-11 10:22:12 +0000823 State.Stack.pop_back();
Daniel Jasper400adc62013-02-08 15:28:42 +0000824 --State.ParenLevel;
825 }
826
827 // Remove scopes created by fake parenthesis.
828 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
829 State.Stack.pop_back();
Daniel Jasperf7935112012-12-03 18:12:45 +0000830 }
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000831
Manuel Klimek0c915712013-02-20 15:32:58 +0000832 if (Current.is(tok::string_literal)) {
Manuel Klimek02f640a2013-02-20 15:25:48 +0000833 State.StartOfStringLiteral = State.Column;
834 } else if (Current.isNot(tok::comment)) {
835 State.StartOfStringLiteral = 0;
836 }
837
Manuel Klimek1998ea22013-02-20 10:15:13 +0000838 State.Column += Current.FormatTok.TokenLength;
839
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000840 if (State.NextToken->Children.empty())
841 State.NextToken = NULL;
842 else
843 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000844
Manuel Klimek1998ea22013-02-20 10:15:13 +0000845 return breakProtrudingToken(Current, State, DryRun);
846 }
847
848 /// \brief If the current token sticks out over the end of the line, break
849 /// it if possible.
850 unsigned breakProtrudingToken(const AnnotatedToken &Current, LineState &State,
851 bool DryRun) {
852 if (Current.isNot(tok::string_literal))
853 return 0;
Manuel Klimek5085d9b2013-03-08 18:59:48 +0000854 // Only break up default narrow strings.
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000855 const char *LiteralData = Current.FormatTok.Tok.getLiteralData();
856 if (!LiteralData || *LiteralData != '"')
Manuel Klimek5085d9b2013-03-08 18:59:48 +0000857 return 0;
Manuel Klimek1998ea22013-02-20 10:15:13 +0000858
859 unsigned Penalty = 0;
860 unsigned TailOffset = 0;
861 unsigned TailLength = Current.FormatTok.TokenLength;
862 unsigned StartColumn = State.Column - Current.FormatTok.TokenLength;
863 unsigned OffsetFromStart = 0;
864 while (StartColumn + TailLength > getColumnLimit()) {
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000865 StringRef Text = StringRef(LiteralData + TailOffset, TailLength);
Manuel Klimeke317d1b2013-03-01 13:29:19 +0000866 if (StartColumn + OffsetFromStart + 1 > getColumnLimit())
Manuel Klimekb176cff2013-03-01 13:14:08 +0000867 break;
Manuel Klimeke317d1b2013-03-01 13:29:19 +0000868 StringRef::size_type SplitPoint = getSplitPoint(
869 Text, getColumnLimit() - StartColumn - OffsetFromStart - 1);
Manuel Klimek1998ea22013-02-20 10:15:13 +0000870 if (SplitPoint == StringRef::npos)
871 break;
872 assert(SplitPoint != 0);
873 // +2, because 'Text' starts after the opening quotes, and does not
874 // include the closing quote we need to insert.
875 unsigned WhitespaceStartColumn =
876 StartColumn + OffsetFromStart + SplitPoint + 2;
877 State.Stack.back().LastSpace = StartColumn;
878 if (!DryRun) {
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000879 Whitespaces.breakToken(Current.FormatTok, TailOffset + SplitPoint + 1,
880 0, "\"", "\"", Line.InPPDirective, StartColumn,
881 WhitespaceStartColumn);
Manuel Klimek1998ea22013-02-20 10:15:13 +0000882 }
883 TailOffset += SplitPoint + 1;
884 TailLength -= SplitPoint + 1;
885 OffsetFromStart = 1;
Daniel Jasper5497fce2013-02-26 12:52:34 +0000886 Penalty += Style.PenaltyExcessCharacter;
Daniel Jasper2cf17bf2013-02-27 09:47:53 +0000887 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
888 State.Stack[i].BreakBeforeParameter = true;
Manuel Klimek1998ea22013-02-20 10:15:13 +0000889 }
890 State.Column = StartColumn + TailLength;
891 return Penalty;
892 }
893
894 StringRef::size_type
895 getSplitPoint(StringRef Text, StringRef::size_type Offset) {
Manuel Klimekb176cff2013-03-01 13:14:08 +0000896 StringRef::size_type SpaceOffset = Text.rfind(' ', Offset);
Manuel Klimekabf6e032013-03-04 20:03:38 +0000897 if (SpaceOffset != StringRef::npos && SpaceOffset != 0)
Manuel Klimeke317d1b2013-03-01 13:29:19 +0000898 return SpaceOffset;
899 StringRef::size_type SlashOffset = Text.rfind('/', Offset);
Manuel Klimekabf6e032013-03-04 20:03:38 +0000900 if (SlashOffset != StringRef::npos && SlashOffset != 0)
Manuel Klimeke317d1b2013-03-01 13:29:19 +0000901 return SlashOffset;
Manuel Klimek5085d9b2013-03-08 18:59:48 +0000902 StringRef::size_type Split = getStartOfCharacter(Text, Offset);
903 if (Split != StringRef::npos && Split > 1)
Manuel Klimeke317d1b2013-03-01 13:29:19 +0000904 // Do not split at 0.
Manuel Klimek5085d9b2013-03-08 18:59:48 +0000905 return Split - 1;
Manuel Klimeke317d1b2013-03-01 13:29:19 +0000906 return StringRef::npos;
Daniel Jasperf7935112012-12-03 18:12:45 +0000907 }
908
Manuel Klimek5085d9b2013-03-08 18:59:48 +0000909 StringRef::size_type
910 getStartOfCharacter(StringRef Text, StringRef::size_type Offset) {
911 StringRef::size_type NextEscape = Text.find('\\');
912 while (NextEscape != StringRef::npos && NextEscape < Offset) {
913 StringRef::size_type SequenceLength =
914 getEscapeSequenceLength(Text.substr(NextEscape));
915 if (Offset < NextEscape + SequenceLength)
916 return NextEscape;
917 NextEscape = Text.find('\\', NextEscape + SequenceLength);
918 }
919 return Offset;
920 }
921
922 unsigned getEscapeSequenceLength(StringRef Text) {
923 assert(Text[0] == '\\');
924 if (Text.size() < 2)
925 return 1;
926
927 switch (Text[1]) {
928 case 'u':
929 return 6;
930 case 'U':
931 return 10;
932 case 'x':
933 return getHexLength(Text);
934 default:
935 if (Text[1] >= '0' && Text[1] <= '7')
936 return getOctalLength(Text);
937 return 2;
938 }
939 }
940
941 unsigned getHexLength(StringRef Text) {
942 unsigned I = 2; // Point after '\x'.
943 while (I < Text.size() && ((Text[I] >= '0' && Text[I] <= '9') ||
944 (Text[I] >= 'a' && Text[I] <= 'f') ||
945 (Text[I] >= 'A' && Text[I] <= 'F'))) {
946 ++I;
947 }
948 return I;
949 }
950
951 unsigned getOctalLength(StringRef Text) {
952 unsigned I = 1;
953 while (I < Text.size() && I < 4 && (Text[I] >= '0' && Text[I] <= '7')) {
954 ++I;
955 }
956 return I;
957 }
958
Daniel Jasper2df93312013-01-09 10:16:05 +0000959 unsigned getColumnLimit() {
Daniel Jasperc22f5b42013-02-28 11:05:57 +0000960 return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0);
Daniel Jasper2df93312013-01-09 10:16:05 +0000961 }
962
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000963 /// \brief An edge in the solution space from \c Previous->State to \c State,
964 /// inserting a newline dependent on the \c NewLine.
965 struct StateNode {
966 StateNode(const LineState &State, bool NewLine, StateNode *Previous)
Daniel Jasper12ef4e52013-02-21 21:33:55 +0000967 : State(State), NewLine(NewLine), Previous(Previous) {}
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000968 LineState State;
969 bool NewLine;
970 StateNode *Previous;
971 };
Daniel Jasper4b866272013-02-01 11:00:45 +0000972
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000973 /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
974 ///
975 /// In case of equal penalties, we want to prefer states that were inserted
976 /// first. During state generation we make sure that we insert states first
977 /// that break the line as late as possible.
978 typedef std::pair<unsigned, unsigned> OrderedPenalty;
979
980 /// \brief An item in the prioritized BFS search queue. The \c StateNode's
981 /// \c State has the given \c OrderedPenalty.
982 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
983
984 /// \brief The BFS queue type.
985 typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
986 std::greater<QueueItem> > QueueType;
Daniel Jasper4b866272013-02-01 11:00:45 +0000987
988 /// \brief Analyze the entire solution space starting from \p InitialState.
Daniel Jasperf7935112012-12-03 18:12:45 +0000989 ///
Daniel Jasper4b866272013-02-01 11:00:45 +0000990 /// This implements a variant of Dijkstra's algorithm on the graph that spans
991 /// the solution space (\c LineStates are the nodes). The algorithm tries to
992 /// find the shortest path (the one with lowest penalty) from \p InitialState
993 /// to a state where all tokens are placed.
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000994 unsigned analyzeSolutionSpace(LineState &InitialState) {
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000995 std::set<LineState> Seen;
996
Daniel Jasper4b866272013-02-01 11:00:45 +0000997 // Insert start element into queue.
Daniel Jasper687af3b2013-02-14 14:26:07 +0000998 StateNode *Node =
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000999 new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
1000 Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
1001 ++Count;
Daniel Jasper4b866272013-02-01 11:00:45 +00001002
1003 // While not empty, take first element and follow edges.
1004 while (!Queue.empty()) {
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001005 unsigned Penalty = Queue.top().first.first;
Daniel Jasper687af3b2013-02-14 14:26:07 +00001006 StateNode *Node = Queue.top().second;
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001007 if (Node->State.NextToken == NULL) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001008 DEBUG(llvm::errs() << "\n---\nPenalty for line: " << Penalty << "\n");
Daniel Jasper4b866272013-02-01 11:00:45 +00001009 break;
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001010 }
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001011 Queue.pop();
Daniel Jasper4b866272013-02-01 11:00:45 +00001012
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001013 if (!Seen.insert(Node->State).second)
1014 // State already examined with lower penalty.
1015 continue;
Daniel Jasper4b866272013-02-01 11:00:45 +00001016
Manuel Klimekaf491072013-02-13 10:54:19 +00001017 addNextStateToQueue(Penalty, Node, /*NewLine=*/ false);
1018 addNextStateToQueue(Penalty, Node, /*NewLine=*/ true);
Daniel Jasper4b866272013-02-01 11:00:45 +00001019 }
1020
1021 if (Queue.empty())
1022 // We were unable to find a solution, do nothing.
1023 // FIXME: Add diagnostic?
Daniel Jasperf7935112012-12-03 18:12:45 +00001024 return 0;
1025
Daniel Jasper4b866272013-02-01 11:00:45 +00001026 // Reconstruct the solution.
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001027 reconstructPath(InitialState, Queue.top().second);
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001028 DEBUG(llvm::errs() << "---\n");
Daniel Jasperf7935112012-12-03 18:12:45 +00001029
Daniel Jasper4b866272013-02-01 11:00:45 +00001030 // Return the column after the last token of the solution.
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001031 return Queue.top().second->State.Column;
1032 }
1033
1034 void reconstructPath(LineState &State, StateNode *Current) {
1035 // FIXME: This recursive implementation limits the possible number
1036 // of tokens per line if compiled into a binary with small stack space.
1037 // To become more independent of stack frame limitations we would need
1038 // to also change the TokenAnnotator.
1039 if (Current->Previous == NULL)
1040 return;
1041 reconstructPath(State, Current->Previous);
1042 DEBUG({
1043 if (Current->NewLine) {
Daniel Jasperb9caeac2013-02-13 20:33:44 +00001044 llvm::errs()
1045 << "Penalty for splitting before "
1046 << Current->Previous->State.NextToken->FormatTok.Tok.getName()
1047 << ": " << Current->Previous->State.NextToken->SplitPenalty << "\n";
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001048 }
1049 });
1050 addTokenToState(Current->NewLine, false, State);
Daniel Jasper4b866272013-02-01 11:00:45 +00001051 }
1052
Manuel Klimekaf491072013-02-13 10:54:19 +00001053 /// \brief Add the following state to the analysis queue \c Queue.
Daniel Jasper4b866272013-02-01 11:00:45 +00001054 ///
Manuel Klimekaf491072013-02-13 10:54:19 +00001055 /// Assume the current state is \p PreviousNode and has been reached with a
Daniel Jasper4b866272013-02-01 11:00:45 +00001056 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
Manuel Klimekaf491072013-02-13 10:54:19 +00001057 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
1058 bool NewLine) {
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001059 if (NewLine && !canBreak(PreviousNode->State))
Daniel Jasper4b866272013-02-01 11:00:45 +00001060 return;
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001061 if (!NewLine && mustBreak(PreviousNode->State))
Daniel Jasper4b866272013-02-01 11:00:45 +00001062 return;
Daniel Jasper20b09ef2013-01-28 09:35:24 +00001063 if (NewLine)
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001064 Penalty += PreviousNode->State.NextToken->SplitPenalty;
1065
1066 StateNode *Node = new (Allocator.Allocate())
1067 StateNode(PreviousNode->State, NewLine, PreviousNode);
Manuel Klimek1998ea22013-02-20 10:15:13 +00001068 Penalty += addTokenToState(NewLine, true, Node->State);
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001069 if (Node->State.Column > getColumnLimit()) {
1070 unsigned ExcessCharacters = Node->State.Column - getColumnLimit();
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001071 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
Daniel Jasper2df93312013-01-09 10:16:05 +00001072 }
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001073
1074 Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
1075 ++Count;
Daniel Jasper4b866272013-02-01 11:00:45 +00001076 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001077
Daniel Jasper4b866272013-02-01 11:00:45 +00001078 /// \brief Returns \c true, if a line break after \p State is allowed.
1079 bool canBreak(const LineState &State) {
1080 if (!State.NextToken->CanBreakBefore &&
1081 !(State.NextToken->is(tok::r_brace) &&
1082 State.Stack.back().BreakBeforeClosingBrace))
1083 return false;
1084 // Trying to insert a parameter on a new line if there are already more than
1085 // one parameter on the current line is bin packing.
Daniel Jasperb9ebd5d2013-02-05 09:41:21 +00001086 if (State.Stack.back().HasMultiParameterLine &&
Daniel Jasper4b866272013-02-01 11:00:45 +00001087 State.Stack.back().AvoidBinPacking)
1088 return false;
1089 return true;
1090 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001091
Daniel Jasper4b866272013-02-01 11:00:45 +00001092 /// \brief Returns \c true, if a line break after \p State is mandatory.
1093 bool mustBreak(const LineState &State) {
1094 if (State.NextToken->MustBreakBefore)
1095 return true;
1096 if (State.NextToken->is(tok::r_brace) &&
1097 State.Stack.back().BreakBeforeClosingBrace)
1098 return true;
1099 if (State.NextToken->Parent->is(tok::semi) &&
1100 State.LineContainsContinuedForLoopSection)
1101 return true;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001102 if ((State.NextToken->Parent->isOneOf(tok::comma, tok::semi) ||
Daniel Jaspercd8599e2013-02-23 21:01:55 +00001103 State.NextToken->is(tok::question) ||
1104 State.NextToken->Type == TT_ConditionalExpr) &&
Daniel Jasperacc33662013-02-08 08:22:00 +00001105 State.Stack.back().BreakBeforeParameter &&
Daniel Jasper66e9dee2013-02-14 09:19:04 +00001106 !isTrailingComment(*State.NextToken) &&
Daniel Jasper37905f72013-02-21 15:00:29 +00001107 State.NextToken->isNot(tok::r_paren) &&
1108 State.NextToken->isNot(tok::r_brace))
Daniel Jasper4b866272013-02-01 11:00:45 +00001109 return true;
Daniel Jasperacc33662013-02-08 08:22:00 +00001110 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
1111 // out whether it is the first parameter. Clean this up.
Daniel Jasper1ac3e052013-02-05 10:07:47 +00001112 if (State.NextToken->Type == TT_ObjCSelectorName &&
Daniel Jasperacc33662013-02-08 08:22:00 +00001113 State.NextToken->LongestObjCSelectorName == 0 &&
1114 State.Stack.back().BreakBeforeParameter)
Daniel Jasper1ac3e052013-02-05 10:07:47 +00001115 return true;
Daniel Jasper4b866272013-02-01 11:00:45 +00001116 if ((State.NextToken->Type == TT_CtorInitializerColon ||
1117 (State.NextToken->Parent->ClosesTemplateDeclaration &&
Daniel Jasper400adc62013-02-08 15:28:42 +00001118 State.ParenLevel == 0)))
Daniel Jasper4b866272013-02-01 11:00:45 +00001119 return true;
Daniel Jasper40aacf42013-03-14 13:45:21 +00001120 if (State.NextToken->Type == TT_InlineASMColon)
1121 return true;
Daniel Jasper9b334242013-03-15 14:57:30 +00001122 // This prevents breaks like:
1123 // ...
1124 // SomeParameter, OtherParameter).DoSomething(
1125 // ...
1126 // As they hide "DoSomething" and generally bad for readability.
1127 if (State.NextToken->isOneOf(tok::period, tok::arrow) &&
1128 getRemainingLength(State) + State.Column > getColumnLimit() &&
1129 State.ParenLevel < State.StartOfLineLevel)
1130 return true;
Daniel Jasper4b866272013-02-01 11:00:45 +00001131 return false;
Daniel Jasperf7935112012-12-03 18:12:45 +00001132 }
1133
Daniel Jasper9b334242013-03-15 14:57:30 +00001134 // Returns the total number of columns required for the remaining tokens.
1135 unsigned getRemainingLength(const LineState &State) {
1136 if (State.NextToken && State.NextToken->Parent)
1137 return Line.Last->TotalLength - State.NextToken->Parent->TotalLength;
1138 return 0;
1139 }
1140
Daniel Jasperf7935112012-12-03 18:12:45 +00001141 FormatStyle Style;
1142 SourceManager &SourceMgr;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001143 const AnnotatedLine &Line;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001144 const unsigned FirstIndent;
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001145 const AnnotatedToken &RootToken;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001146 WhitespaceManager &Whitespaces;
Manuel Klimekaf491072013-02-13 10:54:19 +00001147
1148 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1149 QueueType Queue;
1150 // Increasing count of \c StateNode items we have created. This is used
1151 // to create a deterministic order independent of the container.
1152 unsigned Count;
Daniel Jasperf7935112012-12-03 18:12:45 +00001153};
1154
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001155class LexerBasedFormatTokenSource : public FormatTokenSource {
1156public:
1157 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +00001158 : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001159 IdentTable(Lex.getLangOpts()) {
1160 Lex.SetKeepWhitespaceMode(true);
1161 }
1162
1163 virtual FormatToken getNextToken() {
1164 if (GreaterStashed) {
1165 FormatTok.NewlinesBefore = 0;
1166 FormatTok.WhiteSpaceStart =
1167 FormatTok.Tok.getLocation().getLocWithOffset(1);
1168 FormatTok.WhiteSpaceLength = 0;
1169 GreaterStashed = false;
1170 return FormatTok;
1171 }
1172
1173 FormatTok = FormatToken();
1174 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimekef920692013-01-07 07:56:50 +00001175 StringRef Text = rawTokenText(FormatTok.Tok);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001176 FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
Manuel Klimek52d0fd82013-01-05 22:56:06 +00001177 if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0)
1178 FormatTok.IsFirst = true;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001179
1180 // Consume and record whitespace until we find a significant token.
1181 while (FormatTok.Tok.is(tok::unknown)) {
Manuel Klimek0c137952013-02-11 12:33:24 +00001182 unsigned Newlines = Text.count('\n');
Daniel Jasper973c9422013-03-04 13:43:19 +00001183 if (Newlines > 0)
1184 FormatTok.LastNewlineOffset =
1185 FormatTok.WhiteSpaceLength + Text.rfind('\n') + 1;
Manuel Klimek0c137952013-02-11 12:33:24 +00001186 unsigned EscapedNewlines = Text.count("\\\n");
1187 FormatTok.NewlinesBefore += Newlines;
1188 FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001189 FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
1190
1191 if (FormatTok.Tok.is(tok::eof))
1192 return FormatTok;
1193 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimekef920692013-01-07 07:56:50 +00001194 Text = rawTokenText(FormatTok.Tok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001195 }
Manuel Klimekef920692013-01-07 07:56:50 +00001196
1197 // Now FormatTok is the next non-whitespace token.
1198 FormatTok.TokenLength = Text.size();
1199
Manuel Klimek1abf7892013-01-04 23:34:14 +00001200 // In case the token starts with escaped newlines, we want to
1201 // take them into account as whitespace - this pattern is quite frequent
1202 // in macro definitions.
1203 // FIXME: What do we want to do with other escaped spaces, and escaped
1204 // spaces or newlines in the middle of tokens?
1205 // FIXME: Add a more explicit test.
1206 unsigned i = 0;
Daniel Jasperda16db32013-01-07 10:48:50 +00001207 while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001208 // FIXME: ++FormatTok.NewlinesBefore is missing...
Manuel Klimek1abf7892013-01-04 23:34:14 +00001209 FormatTok.WhiteSpaceLength += 2;
Manuel Klimekef920692013-01-07 07:56:50 +00001210 FormatTok.TokenLength -= 2;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001211 i += 2;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001212 }
1213
1214 if (FormatTok.Tok.is(tok::raw_identifier)) {
Manuel Klimek1abf7892013-01-04 23:34:14 +00001215 IdentifierInfo &Info = IdentTable.get(Text);
Daniel Jasper050948a52012-12-21 17:58:39 +00001216 FormatTok.Tok.setIdentifierInfo(&Info);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001217 FormatTok.Tok.setKind(Info.getTokenID());
1218 }
1219
1220 if (FormatTok.Tok.is(tok::greatergreater)) {
1221 FormatTok.Tok.setKind(tok::greater);
Daniel Jasper57d4a582013-02-28 10:06:05 +00001222 FormatTok.TokenLength = 1;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001223 GreaterStashed = true;
1224 }
1225
Daniel Jasper3324cbe2013-03-01 16:45:59 +00001226 // If we reformat comments, we remove trailing whitespace. Update the length
1227 // accordingly.
1228 if (FormatTok.Tok.is(tok::comment))
1229 FormatTok.TokenLength = Text.rtrim().size();
1230
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001231 return FormatTok;
1232 }
1233
Nico Weber29f9dea2013-02-11 15:32:15 +00001234 IdentifierTable &getIdentTable() { return IdentTable; }
1235
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001236private:
1237 FormatToken FormatTok;
1238 bool GreaterStashed;
1239 Lexer &Lex;
1240 SourceManager &SourceMgr;
1241 IdentifierTable IdentTable;
1242
1243 /// Returns the text of \c FormatTok.
Manuel Klimekef920692013-01-07 07:56:50 +00001244 StringRef rawTokenText(Token &Tok) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001245 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
1246 Tok.getLength());
1247 }
1248};
1249
Daniel Jasperf7935112012-12-03 18:12:45 +00001250class Formatter : public UnwrappedLineConsumer {
1251public:
Daniel Jasper25837aa2013-01-14 14:14:23 +00001252 Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex,
1253 SourceManager &SourceMgr,
Daniel Jasperf7935112012-12-03 18:12:45 +00001254 const std::vector<CharSourceRange> &Ranges)
Alexander Kornienko5b7157a2013-01-10 15:05:09 +00001255 : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienkoafcef332013-03-19 17:41:36 +00001256 Whitespaces(SourceMgr, Style), Ranges(Ranges) {}
Daniel Jasperf7935112012-12-03 18:12:45 +00001257
Daniel Jasperfd8c4b12013-01-11 14:23:32 +00001258 virtual ~Formatter() {}
Daniel Jasper61bd3a12012-12-04 21:05:31 +00001259
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001260 tooling::Replacements format() {
1261 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
1262 UnwrappedLineParser Parser(Diag, Style, Tokens, *this);
1263 StructuralError = Parser.parse();
1264 unsigned PreviousEndOfLineColumn = 0;
1265 TokenAnnotator Annotator(Style, SourceMgr, Lex,
1266 Tokens.getIdentTable().get("in"));
1267 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1268 Annotator.annotate(AnnotatedLines[i]);
1269 }
1270 deriveLocalStyle();
1271 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1272 Annotator.calculateFormattingInformation(AnnotatedLines[i]);
Daniel Jasper0f8ed9e2013-03-13 15:53:12 +00001273
1274 // Adapt level to the next line if this is a comment.
1275 // FIXME: Can/should this be done in the UnwrappedLineParser?
1276 if (i + 1 != e && AnnotatedLines[i].First.is(tok::comment) &&
1277 AnnotatedLines[i].First.Children.empty() &&
1278 AnnotatedLines[i + 1].First.isNot(tok::r_brace))
1279 AnnotatedLines[i].Level = AnnotatedLines[i + 1].Level;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001280 }
1281 std::vector<int> IndentForLevel;
1282 bool PreviousLineWasTouched = false;
1283 for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
1284 E = AnnotatedLines.end();
1285 I != E; ++I) {
1286 const AnnotatedLine &TheLine = *I;
1287 const FormatToken &FirstTok = TheLine.First.FormatTok;
1288 int Offset = getIndentOffset(TheLine.First);
1289 while (IndentForLevel.size() <= TheLine.Level)
1290 IndentForLevel.push_back(-1);
1291 IndentForLevel.resize(TheLine.Level + 1);
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001292 bool WasMoved = PreviousLineWasTouched && FirstTok.NewlinesBefore == 0;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001293 if (TheLine.First.is(tok::eof)) {
1294 if (PreviousLineWasTouched) {
1295 unsigned NewLines = std::min(FirstTok.NewlinesBefore, 1u);
1296 Whitespaces.replaceWhitespace(TheLine.First, NewLines, /*Indent*/ 0,
Alexander Kornienkoafcef332013-03-19 17:41:36 +00001297 /*WhitespaceStartColumn*/ 0);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001298 }
1299 } else if (TheLine.Type != LT_Invalid &&
1300 (WasMoved || touchesLine(TheLine))) {
1301 unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
1302 unsigned Indent = LevelIndent;
1303 if (static_cast<int>(Indent) + Offset >= 0)
1304 Indent += Offset;
1305 if (!FirstTok.WhiteSpaceStart.isValid() || StructuralError) {
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001306 Indent = LevelIndent =
1307 SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001308 } else {
1309 formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
1310 PreviousEndOfLineColumn);
1311 }
1312 tryFitMultipleLinesInOne(Indent, I, E);
1313 UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
1314 TheLine.First, Whitespaces,
1315 StructuralError);
1316 PreviousEndOfLineColumn =
1317 Formatter.format(I + 1 != E ? &*(I + 1) : NULL);
1318 IndentForLevel[TheLine.Level] = LevelIndent;
1319 PreviousLineWasTouched = true;
1320 } else {
1321 if (FirstTok.NewlinesBefore > 0 || FirstTok.IsFirst) {
1322 unsigned Indent =
1323 SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
1324 unsigned LevelIndent = Indent;
1325 if (static_cast<int>(LevelIndent) - Offset >= 0)
1326 LevelIndent -= Offset;
1327 IndentForLevel[TheLine.Level] = LevelIndent;
1328
1329 // Remove trailing whitespace of the previous line if it was touched.
1330 if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine))
1331 formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
1332 PreviousEndOfLineColumn);
1333 }
1334 // If we did not reformat this unwrapped line, the column at the end of
1335 // the last token is unchanged - thus, we can calculate the end of the
1336 // last token.
1337 SourceLocation LastLoc = TheLine.Last->FormatTok.Tok.getLocation();
1338 PreviousEndOfLineColumn =
1339 SourceMgr.getSpellingColumnNumber(LastLoc) +
1340 Lex.MeasureTokenLength(LastLoc, SourceMgr, Lex.getLangOpts()) - 1;
1341 PreviousLineWasTouched = false;
1342 }
1343 }
1344 return Whitespaces.generateReplacements();
1345 }
1346
1347private:
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001348 void deriveLocalStyle() {
1349 unsigned CountBoundToVariable = 0;
1350 unsigned CountBoundToType = 0;
1351 bool HasCpp03IncompatibleFormat = false;
1352 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1353 if (AnnotatedLines[i].First.Children.empty())
1354 continue;
1355 AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0];
1356 while (!Tok->Children.empty()) {
1357 if (Tok->Type == TT_PointerOrReference) {
1358 bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0;
1359 bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0;
1360 if (SpacesBefore && !SpacesAfter)
1361 ++CountBoundToVariable;
1362 else if (!SpacesBefore && SpacesAfter)
1363 ++CountBoundToType;
1364 }
1365
Daniel Jasper400adc62013-02-08 15:28:42 +00001366 if (Tok->Type == TT_TemplateCloser &&
1367 Tok->Parent->Type == TT_TemplateCloser &&
1368 Tok->FormatTok.WhiteSpaceLength == 0)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001369 HasCpp03IncompatibleFormat = true;
1370 Tok = &Tok->Children[0];
1371 }
1372 }
1373 if (Style.DerivePointerBinding) {
1374 if (CountBoundToType > CountBoundToVariable)
1375 Style.PointerBindsToType = true;
1376 else if (CountBoundToType < CountBoundToVariable)
1377 Style.PointerBindsToType = false;
1378 }
1379 if (Style.Standard == FormatStyle::LS_Auto) {
1380 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
1381 : FormatStyle::LS_Cpp03;
1382 }
1383 }
1384
Manuel Klimekb95f5452013-02-08 17:38:27 +00001385 /// \brief Get the indent of \p Level from \p IndentForLevel.
1386 ///
1387 /// \p IndentForLevel must contain the indent for the level \c l
1388 /// at \p IndentForLevel[l], or a value < 0 if the indent for
1389 /// that level is unknown.
Daniel Jasper687af3b2013-02-14 14:26:07 +00001390 unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
Manuel Klimekb95f5452013-02-08 17:38:27 +00001391 if (IndentForLevel[Level] != -1)
1392 return IndentForLevel[Level];
Manuel Klimekd076dcd2013-02-08 19:53:32 +00001393 if (Level == 0)
1394 return 0;
Daniel Jasper24570102013-02-14 09:58:41 +00001395 return getIndent(IndentForLevel, Level - 1) + 2;
Manuel Klimekb95f5452013-02-08 17:38:27 +00001396 }
1397
1398 /// \brief Get the offset of the line relatively to the level.
1399 ///
1400 /// For example, 'public:' labels in classes are offset by 1 or 2
1401 /// characters to the left from their level.
Daniel Jasper24570102013-02-14 09:58:41 +00001402 int getIndentOffset(const AnnotatedToken &RootToken) {
Manuel Klimekb95f5452013-02-08 17:38:27 +00001403 bool IsAccessModifier = false;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001404 if (RootToken.isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
Manuel Klimekb95f5452013-02-08 17:38:27 +00001405 IsAccessModifier = true;
1406 else if (RootToken.is(tok::at) && !RootToken.Children.empty() &&
1407 (RootToken.Children[0].isObjCAtKeyword(tok::objc_public) ||
1408 RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) ||
1409 RootToken.Children[0].isObjCAtKeyword(tok::objc_package) ||
1410 RootToken.Children[0].isObjCAtKeyword(tok::objc_private)))
1411 IsAccessModifier = true;
1412
1413 if (IsAccessModifier)
1414 return Style.AccessModifierOffset;
1415 return 0;
1416 }
1417
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001418 /// \brief Tries to merge lines into one.
1419 ///
1420 /// This will change \c Line and \c AnnotatedLine to contain the merged line,
1421 /// if possible; note that \c I will be incremented when lines are merged.
1422 ///
1423 /// Returns whether the resulting \c Line can fit in a single line.
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001424 void tryFitMultipleLinesInOne(unsigned Indent,
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001425 std::vector<AnnotatedLine>::iterator &I,
1426 std::vector<AnnotatedLine>::iterator E) {
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001427 // We can never merge stuff if there are trailing line comments.
1428 if (I->Last->Type == TT_LineComment)
1429 return;
1430
Daniel Jasperc22f5b42013-02-28 11:05:57 +00001431 unsigned Limit = Style.ColumnLimit - Indent;
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001432 // If we already exceed the column limit, we set 'Limit' to 0. The different
1433 // tryMerge..() functions can then decide whether to still do merging.
1434 Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
Daniel Jasperc36492b2013-01-16 07:02:34 +00001435
Daniel Jasperd41ee2d2013-01-21 14:18:28 +00001436 if (I + 1 == E || (I + 1)->Type == LT_Invalid)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001437 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001438
Daniel Jasper25837aa2013-01-14 14:14:23 +00001439 if (I->Last->is(tok::l_brace)) {
1440 tryMergeSimpleBlock(I, E, Limit);
1441 } else if (I->First.is(tok::kw_if)) {
1442 tryMergeSimpleIf(I, E, Limit);
Daniel Jasper39825ea2013-01-14 15:40:57 +00001443 } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline ||
1444 I->First.FormatTok.IsFirst)) {
1445 tryMergeSimplePPDirective(I, E, Limit);
Daniel Jasper25837aa2013-01-14 14:14:23 +00001446 }
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001447 return;
Daniel Jasper25837aa2013-01-14 14:14:23 +00001448 }
1449
Daniel Jasper39825ea2013-01-14 15:40:57 +00001450 void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
1451 std::vector<AnnotatedLine>::iterator E,
1452 unsigned Limit) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001453 if (Limit == 0)
1454 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001455 AnnotatedLine &Line = *I;
Daniel Jasper2ab0d012013-01-14 15:52:06 +00001456 if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline)
1457 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001458 if (I + 2 != E && (I + 2)->InPPDirective &&
1459 !(I + 2)->First.FormatTok.HasUnescapedNewline)
1460 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001461 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001462 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001463 join(Line, *(++I));
1464 }
1465
Daniel Jasper25837aa2013-01-14 14:14:23 +00001466 void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I,
1467 std::vector<AnnotatedLine>::iterator E,
1468 unsigned Limit) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001469 if (Limit == 0)
1470 return;
Daniel Jasper1b750ed2013-01-14 16:24:39 +00001471 if (!Style.AllowShortIfStatementsOnASingleLine)
1472 return;
Manuel Klimekda087612013-01-18 14:46:43 +00001473 if ((I + 1)->InPPDirective != I->InPPDirective ||
1474 ((I + 1)->InPPDirective &&
1475 (I + 1)->First.FormatTok.HasUnescapedNewline))
1476 return;
Daniel Jasper25837aa2013-01-14 14:14:23 +00001477 AnnotatedLine &Line = *I;
Daniel Jasperc36492b2013-01-16 07:02:34 +00001478 if (Line.Last->isNot(tok::r_paren))
1479 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001480 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jasper25837aa2013-01-14 14:14:23 +00001481 return;
1482 if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment)
1483 return;
1484 // Only inline simple if's (no nested if or else).
1485 if (I + 2 != E && (I + 2)->First.is(tok::kw_else))
1486 return;
1487 join(Line, *(++I));
1488 }
1489
1490 void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
Daniel Jasperbbc84152013-01-29 11:27:30 +00001491 std::vector<AnnotatedLine>::iterator E,
1492 unsigned Limit) {
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001493 // First, check that the current line allows merging. This is the case if
1494 // we're not in a control flow statement and the last token is an opening
1495 // brace.
Daniel Jasper25837aa2013-01-14 14:14:23 +00001496 AnnotatedLine &Line = *I;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001497 if (Line.First.isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
1498 tok::kw_else, tok::kw_try, tok::kw_catch,
1499 tok::kw_for,
1500 // This gets rid of all ObjC @ keywords and methods.
1501 tok::at, tok::minus, tok::plus))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001502 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001503
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001504 AnnotatedToken *Tok = &(I + 1)->First;
1505 if (Tok->Children.empty() && Tok->is(tok::r_brace) &&
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001506 !Tok->MustBreakBefore) {
1507 // We merge empty blocks even if the line exceeds the column limit.
Daniel Jaspereef30492013-02-11 12:36:37 +00001508 Tok->SpacesRequiredBefore = 0;
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001509 Tok->CanBreakBefore = true;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001510 join(Line, *(I + 1));
1511 I += 1;
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001512 } else if (Limit != 0) {
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001513 // Check that we still have three lines and they fit into the limit.
1514 if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
1515 !nextTwoLinesFitInto(I, Limit))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001516 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001517
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001518 // Second, check that the next line does not contain any braces - if it
1519 // does, readability declines when putting it into a single line.
1520 if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
1521 return;
1522 do {
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001523 if (Tok->isOneOf(tok::l_brace, tok::r_brace))
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001524 return;
1525 Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
1526 } while (Tok != NULL);
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001527
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001528 // Last, check that the third line contains a single closing brace.
1529 Tok = &(I + 2)->First;
1530 if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
1531 Tok->MustBreakBefore)
1532 return;
1533
1534 join(Line, *(I + 1));
1535 join(Line, *(I + 2));
1536 I += 2;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001537 }
Daniel Jasper25837aa2013-01-14 14:14:23 +00001538 }
1539
1540 bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
1541 unsigned Limit) {
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001542 return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
1543 Limit;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001544 }
1545
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001546 void join(AnnotatedLine &A, const AnnotatedLine &B) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001547 unsigned LengthA = A.Last->TotalLength + B.First.SpacesRequiredBefore;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001548 A.Last->Children.push_back(B.First);
1549 while (!A.Last->Children.empty()) {
1550 A.Last->Children[0].Parent = A.Last;
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001551 A.Last->Children[0].TotalLength += LengthA;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001552 A.Last = &A.Last->Children[0];
1553 }
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001554 }
1555
Daniel Jasper97b89482013-03-13 07:49:51 +00001556 bool touchesRanges(const CharSourceRange &Range) {
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001557 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1558 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
1559 Ranges[i].getBegin()) &&
1560 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1561 Range.getBegin()))
1562 return true;
1563 }
1564 return false;
1565 }
1566
1567 bool touchesLine(const AnnotatedLine &TheLine) {
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001568 const FormatToken *First = &TheLine.First.FormatTok;
1569 const FormatToken *Last = &TheLine.Last->FormatTok;
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001570 CharSourceRange LineRange = CharSourceRange::getTokenRange(
Daniel Jasper973c9422013-03-04 13:43:19 +00001571 First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset),
1572 Last->Tok.getLocation());
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001573 return touchesRanges(LineRange);
1574 }
1575
1576 bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
1577 const FormatToken *First = &TheLine.First.FormatTok;
1578 CharSourceRange LineRange = CharSourceRange::getCharRange(
1579 First->WhiteSpaceStart,
1580 First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset));
1581 return touchesRanges(LineRange);
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001582 }
1583
1584 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001585 AnnotatedLines.push_back(AnnotatedLine(TheLine));
Daniel Jasperf7935112012-12-03 18:12:45 +00001586 }
1587
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001588 /// \brief Add a new line and the required indent before the first Token
1589 /// of the \c UnwrappedLine if there was no structural parsing error.
1590 /// Returns the indent level of the \c UnwrappedLine.
Manuel Klimekb95f5452013-02-08 17:38:27 +00001591 void formatFirstToken(const AnnotatedToken &RootToken, unsigned Indent,
1592 bool InPPDirective, unsigned PreviousEndOfLineColumn) {
Daniel Jasperfd8c4b12013-01-11 14:23:32 +00001593 const FormatToken &Tok = RootToken.FormatTok;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001594
Daniel Jasperbbc84152013-01-29 11:27:30 +00001595 unsigned Newlines =
1596 std::min(Tok.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001597 if (Newlines == 0 && !Tok.IsFirst)
1598 Newlines = 1;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001599
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001600 if (!InPPDirective || Tok.HasUnescapedNewline) {
Alexander Kornienkoafcef332013-03-19 17:41:36 +00001601 Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001602 } else {
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001603 Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent,
Alexander Kornienkoafcef332013-03-19 17:41:36 +00001604 PreviousEndOfLineColumn);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001605 }
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001606 }
1607
Alexander Kornienko116ba682013-01-14 11:34:14 +00001608 DiagnosticsEngine &Diag;
Daniel Jasperf7935112012-12-03 18:12:45 +00001609 FormatStyle Style;
1610 Lexer &Lex;
1611 SourceManager &SourceMgr;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001612 WhitespaceManager Whitespaces;
Daniel Jasperf7935112012-12-03 18:12:45 +00001613 std::vector<CharSourceRange> Ranges;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001614 std::vector<AnnotatedLine> AnnotatedLines;
Alexander Kornienko870f9eb2012-12-04 17:27:50 +00001615 bool StructuralError;
Daniel Jasperf7935112012-12-03 18:12:45 +00001616};
1617
Daniel Jasperbbc84152013-01-29 11:27:30 +00001618tooling::Replacements
1619reformat(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
1620 std::vector<CharSourceRange> Ranges, DiagnosticConsumer *DiagClient) {
Alexander Kornienko5b7157a2013-01-10 15:05:09 +00001621 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Alexander Kornienko116ba682013-01-14 11:34:14 +00001622 OwningPtr<DiagnosticConsumer> DiagPrinter;
1623 if (DiagClient == 0) {
1624 DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts));
1625 DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
1626 DiagClient = DiagPrinter.get();
1627 }
Alexander Kornienko5b7157a2013-01-10 15:05:09 +00001628 DiagnosticsEngine Diagnostics(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001629 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
Alexander Kornienko116ba682013-01-14 11:34:14 +00001630 DiagClient, false);
Alexander Kornienko5b7157a2013-01-10 15:05:09 +00001631 Diagnostics.setSourceManager(&SourceMgr);
1632 Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
Daniel Jasperf7935112012-12-03 18:12:45 +00001633 return formatter.format();
1634}
1635
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001636LangOptions getFormattingLangOpts() {
1637 LangOptions LangOpts;
1638 LangOpts.CPlusPlus = 1;
1639 LangOpts.CPlusPlus11 = 1;
1640 LangOpts.Bool = 1;
1641 LangOpts.ObjC1 = 1;
1642 LangOpts.ObjC2 = 1;
1643 return LangOpts;
1644}
1645
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001646} // namespace format
1647} // namespace clang