blob: dd8f6cde030310902840b33778dc612bc5aa18bf [file] [log] [blame]
Daniel Jasperbac016b2012-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 Jasperbac016b2012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Manuel Klimekca547db2013-01-16 14:55:28 +000016#define DEBUG_TYPE "format-formatter"
17
Daniel Jasper32d28ee2013-01-29 21:01:14 +000018#include "TokenAnnotator.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "UnwrappedLineParser.h"
Alexander Kornienko3048aea2013-01-10 15:05:09 +000020#include "clang/Basic/Diagnostic.h"
Daniel Jasper675d2e32012-12-21 10:20:02 +000021#include "clang/Basic/OperatorPrecedence.h"
Chandler Carruthb99083e2013-01-02 10:28:36 +000022#include "clang/Basic/SourceManager.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000023#include "clang/Format/Format.h"
Alexander Kornienko3048aea2013-01-10 15:05:09 +000024#include "clang/Frontend/TextDiagnosticPrinter.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000025#include "clang/Lex/Lexer.h"
Manuel Klimek32a2fd72013-02-13 10:46:36 +000026#include "llvm/Support/Allocator.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000027#include "llvm/Support/Debug.h"
Manuel Klimek32a2fd72013-02-13 10:46:36 +000028#include <queue>
Daniel Jasper8822d3a2012-12-04 13:02:32 +000029#include <string>
30
Daniel Jasperbac016b2012-12-03 18:12:45 +000031namespace clang {
32namespace format {
33
Daniel Jasperbac016b2012-12-03 18:12:45 +000034FormatStyle getLLVMStyle() {
35 FormatStyle LLVMStyle;
36 LLVMStyle.ColumnLimit = 80;
37 LLVMStyle.MaxEmptyLinesToKeep = 1;
Daniel Jasper8ff690a2013-02-06 14:22:40 +000038 LLVMStyle.PointerBindsToType = false;
39 LLVMStyle.DerivePointerBinding = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +000040 LLVMStyle.AccessModifierOffset = -2;
Daniel Jasper8ff690a2013-02-06 14:22:40 +000041 LLVMStyle.Standard = FormatStyle::LS_Cpp03;
Alexander Kornienko15757312012-12-06 18:03:27 +000042 LLVMStyle.IndentCaseLabels = false;
Daniel Jasper7ad4eff2013-01-07 11:09:06 +000043 LLVMStyle.SpacesBeforeTrailingComments = 1;
Daniel Jasper0df6acd2013-01-16 14:59:02 +000044 LLVMStyle.BinPackParameters = true;
Daniel Jasperf1579602013-01-29 16:03:49 +000045 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +000046 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +000047 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Nico Weber5f500df2013-01-10 20:12:55 +000048 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Daniel Jasper01786732013-02-04 07:21:18 +000049 LLVMStyle.PenaltyExcessCharacter = 1000000;
Daniel Jaspera03ab102013-02-13 20:33:44 +000050 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 5;
Daniel Jasperbac016b2012-12-03 18:12:45 +000051 return LLVMStyle;
52}
53
54FormatStyle getGoogleStyle() {
55 FormatStyle GoogleStyle;
56 GoogleStyle.ColumnLimit = 80;
57 GoogleStyle.MaxEmptyLinesToKeep = 1;
Daniel Jasper8ff690a2013-02-06 14:22:40 +000058 GoogleStyle.PointerBindsToType = true;
59 GoogleStyle.DerivePointerBinding = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +000060 GoogleStyle.AccessModifierOffset = -1;
Daniel Jasper8ff690a2013-02-06 14:22:40 +000061 GoogleStyle.Standard = FormatStyle::LS_Auto;
Alexander Kornienko15757312012-12-06 18:03:27 +000062 GoogleStyle.IndentCaseLabels = true;
Daniel Jasper7ad4eff2013-01-07 11:09:06 +000063 GoogleStyle.SpacesBeforeTrailingComments = 2;
Daniel Jasperfaab0d32013-02-27 09:47:53 +000064 GoogleStyle.BinPackParameters = true;
Daniel Jasperf1579602013-01-29 16:03:49 +000065 GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +000066 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
Daniel Jasperdf3736a2013-01-16 15:44:34 +000067 GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
Nico Weber5f500df2013-01-10 20:12:55 +000068 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Daniel Jasper01786732013-02-04 07:21:18 +000069 GoogleStyle.PenaltyExcessCharacter = 1000000;
Daniel Jaspera03ab102013-02-13 20:33:44 +000070 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 100;
Daniel Jasperbac016b2012-12-03 18:12:45 +000071 return GoogleStyle;
72}
73
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +000074FormatStyle getChromiumStyle() {
75 FormatStyle ChromiumStyle = getGoogleStyle();
Daniel Jasperf1579602013-01-29 16:03:49 +000076 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Daniel Jasperfaab0d32013-02-27 09:47:53 +000077 ChromiumStyle.BinPackParameters = false;
Daniel Jasper8ff690a2013-02-06 14:22:40 +000078 ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
79 ChromiumStyle.DerivePointerBinding = false;
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +000080 return ChromiumStyle;
81}
82
Daniel Jasper15417ef2013-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 Jasperce3d1a62013-02-08 08:22:00 +000088// Returns the length of everything up to the first possible line break after
89// the ), ], } or > matching \c Tok.
90static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) {
91 if (Tok.MatchingParen == NULL)
92 return 0;
93 AnnotatedToken *End = Tok.MatchingParen;
94 while (!End->Children.empty() && !End->Children[0].CanBreakBefore) {
95 End = &End->Children[0];
96 }
97 return End->TotalLength - Tok.TotalLength + 1;
98}
99
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000100/// \brief Manages the whitespaces around tokens and their replacements.
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000101///
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000102/// This includes special handling for certain constructs, e.g. the alignment of
103/// trailing line comments.
104class WhitespaceManager {
105public:
Alexander Kornienko052685c2013-03-19 17:41:36 +0000106 WhitespaceManager(SourceManager &SourceMgr, const FormatStyle &Style)
107 : SourceMgr(SourceMgr), Style(Style) {}
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000108
109 /// \brief Replaces the whitespace in front of \p Tok. Only call once for
110 /// each \c AnnotatedToken.
111 void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
Alexander Kornienko052685c2013-03-19 17:41:36 +0000112 unsigned Spaces, unsigned WhitespaceStartColumn) {
Daniel Jasper821627e2013-01-21 22:49:20 +0000113 // 2+ newlines mean an empty line separating logic scopes.
114 if (NewLines >= 2)
115 alignComments();
116
117 // Align line comments if they are trailing or if they continue other
118 // trailing comments.
Daniel Jasper812c0452013-03-01 16:45:59 +0000119 if (isTrailingComment(Tok)) {
120 // Remove the comment's trailing whitespace.
121 if (Tok.FormatTok.Tok.getLength() != Tok.FormatTok.TokenLength)
122 Replaces.insert(tooling::Replacement(
123 SourceMgr, Tok.FormatTok.Tok.getLocation().getLocWithOffset(
124 Tok.FormatTok.TokenLength),
125 Tok.FormatTok.Tok.getLength() - Tok.FormatTok.TokenLength, ""));
126
127 // Align comment with other comments.
128 if (Tok.Parent != NULL || !Comments.empty()) {
129 if (Style.ColumnLimit >=
130 Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
Alexander Kornienkof7536152013-03-14 16:10:54 +0000131 StoredComment Comment;
132 Comment.Tok = Tok.FormatTok;
133 Comment.Spaces = Spaces;
134 Comment.NewLines = NewLines;
135 Comment.MinColumn =
136 NewLines > 0 ? Spaces : WhitespaceStartColumn + Spaces;
137 Comment.MaxColumn = Style.ColumnLimit - Tok.FormatTok.TokenLength;
138 Comments.push_back(Comment);
Daniel Jasper812c0452013-03-01 16:45:59 +0000139 return;
140 }
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000141 }
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000142 }
Daniel Jasper821627e2013-01-21 22:49:20 +0000143
144 // If this line does not have a trailing comment, align the stored comments.
Daniel Jasper15417ef2013-02-06 20:07:35 +0000145 if (Tok.Children.empty() && !isTrailingComment(Tok))
Daniel Jasper821627e2013-01-21 22:49:20 +0000146 alignComments();
Alexander Kornienkof7536152013-03-14 16:10:54 +0000147
148 if (Tok.Type == TT_BlockComment)
Alexander Kornienko052685c2013-03-19 17:41:36 +0000149 indentBlockComment(Tok, Spaces, false);
Alexander Kornienkof7536152013-03-14 16:10:54 +0000150
Manuel Klimek8092a942013-02-20 10:15:13 +0000151 storeReplacement(Tok.FormatTok, getNewLineText(NewLines, Spaces));
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000152 }
153
154 /// \brief Like \c replaceWhitespace, but additionally adds right-aligned
155 /// backslashes to escape newlines inside a preprocessor directive.
156 ///
157 /// This function and \c replaceWhitespace have the same behavior if
158 /// \c Newlines == 0.
159 void replacePPWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
Alexander Kornienko052685c2013-03-19 17:41:36 +0000160 unsigned Spaces, unsigned WhitespaceStartColumn) {
161 if (Tok.Type == TT_BlockComment)
162 indentBlockComment(Tok, Spaces, true);
163
164 storeReplacement(Tok.FormatTok,
165 getNewLineText(NewLines, Spaces, WhitespaceStartColumn));
Manuel Klimek8092a942013-02-20 10:15:13 +0000166 }
167
168 /// \brief Inserts a line break into the middle of a token.
169 ///
170 /// Will break at \p Offset inside \p Tok, putting \p Prefix before the line
171 /// break and \p Postfix before the rest of the token starts in the next line.
172 ///
173 /// \p InPPDirective, \p Spaces, \p WhitespaceStartColumn and \p Style are
174 /// used to generate the correct line break.
Alexander Kornienko052685c2013-03-19 17:41:36 +0000175 void breakToken(const FormatToken &Tok, unsigned Offset,
176 unsigned ReplaceChars, StringRef Prefix, StringRef Postfix,
177 bool InPPDirective, unsigned Spaces,
178 unsigned WhitespaceStartColumn) {
Manuel Klimek8092a942013-02-20 10:15:13 +0000179 std::string NewLineText;
180 if (!InPPDirective)
181 NewLineText = getNewLineText(1, Spaces);
182 else
Alexander Kornienko052685c2013-03-19 17:41:36 +0000183 NewLineText = getNewLineText(1, Spaces, WhitespaceStartColumn);
Manuel Klimek8092a942013-02-20 10:15:13 +0000184 std::string ReplacementText = (Prefix + NewLineText + Postfix).str();
Alexander Kornienko052685c2013-03-19 17:41:36 +0000185 SourceLocation Location = Tok.Tok.getLocation().getLocWithOffset(Offset);
186 Replaces.insert(tooling::Replacement(SourceMgr, Location, ReplaceChars,
187 ReplacementText));
Manuel Klimek8092a942013-02-20 10:15:13 +0000188 }
189
190 /// \brief Returns all the \c Replacements created during formatting.
191 const tooling::Replacements &generateReplacements() {
192 alignComments();
193 return Replaces;
194 }
195
196private:
Alexander Kornienko052685c2013-03-19 17:41:36 +0000197 /// \brief Finds a common prefix of lines of a block comment to properly
198 /// indent (and possibly decorate with '*'s) added lines.
199 ///
200 /// The first line is ignored (it's special and starts with /*).
201 /// When there are less than three lines, we don't have enough information, so
202 /// better use no prefix.
203 static StringRef findCommentLinesPrefix(ArrayRef<StringRef> Lines,
204 const char *PrefixChars = " *") {
205 if (Lines.size() < 3)
206 return "";
207 StringRef Prefix(Lines[1].data(), Lines[1].find_first_not_of(PrefixChars));
208 for (size_t i = 2; i < Lines.size(); ++i) {
209 for (size_t j = 0; j < Prefix.size() && j < Lines[i].size(); ++j) {
210 if (Prefix[j] != Lines[i][j]) {
211 Prefix = Prefix.substr(0, j);
212 break;
213 }
214 }
215 }
216 return Prefix;
217 }
218
219 void splitLineInComment(const FormatToken &Tok, StringRef Line,
220 size_t StartColumn, StringRef LinePrefix,
221 bool InPPDirective, bool CommentHasMoreLines,
222 const char *WhiteSpaceChars = " ") {
223 size_t ColumnLimit =
224 Style.ColumnLimit - LinePrefix.size() - (InPPDirective ? 2 : 0);
225
226 if (Line.size() <= ColumnLimit)
227 return;
228
229 const char *TokenStart = SourceMgr.getCharacterData(Tok.Tok.getLocation());
230 while (Line.rtrim().size() > ColumnLimit) {
231 // Try to break at the last whitespace before the column limit.
232 size_t SpacePos =
233 Line.find_last_of(WhiteSpaceChars, ColumnLimit + 1);
234 if (SpacePos == StringRef::npos) {
235 // Try to find any whitespace in the line.
236 SpacePos = Line.find_first_of(WhiteSpaceChars);
237 if (SpacePos == StringRef::npos) // No whitespace found, give up.
238 break;
239 }
240
241 StringRef NextCut = Line.substr(0, SpacePos).rtrim();
242 StringRef RemainingLine = Line.substr(SpacePos).ltrim();
243 if (RemainingLine.empty())
244 break;
245 Line = RemainingLine;
246
247 size_t ReplaceChars = Line.begin() - NextCut.end();
248 breakToken(Tok, NextCut.end() - TokenStart, ReplaceChars, "", LinePrefix,
249 InPPDirective, 0,
250 NextCut.size() + LinePrefix.size() + StartColumn);
251 StartColumn = 0;
252 }
253
254 StringRef TrimmedLine = Line.rtrim();
255 if (TrimmedLine != Line || (InPPDirective && CommentHasMoreLines)) {
256 // Remove trailing whitespace/insert backslash.
257 breakToken(Tok, TrimmedLine.end() - TokenStart,
258 Line.size() - TrimmedLine.size() + 1, "", "", InPPDirective, 0,
259 TrimmedLine.size() + LinePrefix.size());
260 }
261 }
262
263 void indentBlockComment(const AnnotatedToken &Tok, int Indent,
264 bool InPPDirective) {
265 const SourceLocation TokenLoc = Tok.FormatTok.Tok.getLocation();
266 const int CurrentIndent = SourceMgr.getSpellingColumnNumber(TokenLoc) - 1;
267 const int IndentDelta = Indent - CurrentIndent;
268 const StringRef Text(SourceMgr.getCharacterData(TokenLoc),
269 Tok.FormatTok.TokenLength);
270 assert(Text.startswith("/*") && Text.endswith("*/"));
271
272 SmallVector<StringRef, 16> Lines;
273 Text.split(Lines, "\n");
274
275 if (IndentDelta > 0) {
276 std::string WhiteSpace(IndentDelta, ' ');
277 for (size_t i = 1; i < Lines.size(); ++i) {
278 Replaces.insert(tooling::Replacement(
279 SourceMgr, TokenLoc.getLocWithOffset(Lines[i].data() - Text.data()),
280 0, WhiteSpace));
281 }
282 } else if (IndentDelta < 0) {
283 std::string WhiteSpace(-IndentDelta, ' ');
284 // Check that the line is indented enough.
285 for (size_t i = 1; i < Lines.size(); ++i) {
286 if (!Lines[i].startswith(WhiteSpace))
287 return;
288 }
289 for (size_t i = 1; i < Lines.size(); ++i) {
290 Replaces.insert(tooling::Replacement(
291 SourceMgr, TokenLoc.getLocWithOffset(Lines[i].data() - Text.data()),
292 -IndentDelta, ""));
Alexander Kornienkof7536152013-03-14 16:10:54 +0000293 }
294 }
Alexander Kornienko1fdd8b32013-03-15 13:42:02 +0000295
Alexander Kornienko052685c2013-03-19 17:41:36 +0000296 // Split long lines in comments.
297 const StringRef CurrentPrefix = findCommentLinesPrefix(Lines);
298 size_t PrefixSize = CurrentPrefix.size();
299 std::string NewPrefix =
300 (IndentDelta < 0) ? CurrentPrefix.substr(-IndentDelta).str()
301 : std::string(IndentDelta, ' ') + CurrentPrefix.str();
302
303 if (CurrentPrefix.endswith("*")) {
304 NewPrefix += " ";
305 ++PrefixSize;
306 }
307
308 for (size_t i = 0; i < Lines.size(); ++i) {
309 StringRef Line = (i == 0) ? Lines[i] : Lines[i].substr(PrefixSize);
310 size_t StartColumn = (i == 0) ? CurrentIndent : 0;
311 splitLineInComment(Tok.FormatTok, Line, StartColumn, NewPrefix,
312 InPPDirective, i != Lines.size() - 1);
Alexander Kornienko1fdd8b32013-03-15 13:42:02 +0000313 }
Alexander Kornienkof7536152013-03-14 16:10:54 +0000314 }
315
Manuel Klimek8092a942013-02-20 10:15:13 +0000316 std::string getNewLineText(unsigned NewLines, unsigned Spaces) {
317 return std::string(NewLines, '\n') + std::string(Spaces, ' ');
318 }
319
Alexander Kornienko052685c2013-03-19 17:41:36 +0000320 std::string getNewLineText(unsigned NewLines, unsigned Spaces,
321 unsigned WhitespaceStartColumn) {
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000322 std::string NewLineText;
323 if (NewLines > 0) {
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000324 unsigned Offset =
325 std::min<int>(Style.ColumnLimit - 1, WhitespaceStartColumn);
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000326 for (unsigned i = 0; i < NewLines; ++i) {
327 NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' ');
328 NewLineText += "\\\n";
329 Offset = 0;
330 }
331 }
Manuel Klimek8092a942013-02-20 10:15:13 +0000332 return NewLineText + std::string(Spaces, ' ');
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000333 }
334
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000335 /// \brief Structure to store a comment for later layout and alignment.
336 struct StoredComment {
337 FormatToken Tok;
338 unsigned MinColumn;
339 unsigned MaxColumn;
340 unsigned NewLines;
341 unsigned Spaces;
342 };
343 SmallVector<StoredComment, 16> Comments;
344 typedef SmallVector<StoredComment, 16>::iterator comment_iterator;
345
346 /// \brief Try to align all stashed comments.
347 void alignComments() {
348 unsigned MinColumn = 0;
349 unsigned MaxColumn = UINT_MAX;
350 comment_iterator Start = Comments.begin();
Alexander Kornienkof7536152013-03-14 16:10:54 +0000351 for (comment_iterator I = Start, E = Comments.end(); I != E; ++I) {
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000352 if (I->MinColumn > MaxColumn || I->MaxColumn < MinColumn) {
353 alignComments(Start, I, MinColumn);
354 MinColumn = I->MinColumn;
355 MaxColumn = I->MaxColumn;
356 Start = I;
357 } else {
358 MinColumn = std::max(MinColumn, I->MinColumn);
359 MaxColumn = std::min(MaxColumn, I->MaxColumn);
360 }
361 }
362 alignComments(Start, Comments.end(), MinColumn);
363 Comments.clear();
364 }
365
366 /// \brief Put all the comments between \p I and \p E into \p Column.
367 void alignComments(comment_iterator I, comment_iterator E, unsigned Column) {
368 while (I != E) {
369 unsigned Spaces = I->Spaces + Column - I->MinColumn;
370 storeReplacement(I->Tok, std::string(I->NewLines, '\n') +
Daniel Jasper29f123b2013-02-08 15:28:42 +0000371 std::string(Spaces, ' '));
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000372 ++I;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000373 }
374 }
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000375
376 /// \brief Stores \p Text as the replacement for the whitespace in front of
377 /// \p Tok.
378 void storeReplacement(const FormatToken &Tok, const std::string Text) {
Daniel Jasperafcbd852013-01-30 09:46:12 +0000379 // Don't create a replacement, if it does not change anything.
380 if (StringRef(SourceMgr.getCharacterData(Tok.WhiteSpaceStart),
381 Tok.WhiteSpaceLength) == Text)
382 return;
383
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000384 Replaces.insert(tooling::Replacement(SourceMgr, Tok.WhiteSpaceStart,
385 Tok.WhiteSpaceLength, Text));
386 }
387
388 SourceManager &SourceMgr;
389 tooling::Replacements Replaces;
Alexander Kornienko052685c2013-03-19 17:41:36 +0000390 const FormatStyle &Style;
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000391};
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000392
Daniel Jasperbac016b2012-12-03 18:12:45 +0000393class UnwrappedLineFormatter {
394public:
Manuel Klimek94fc6f12013-01-10 19:17:33 +0000395 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
Daniel Jasper995e8202013-01-14 13:08:07 +0000396 const AnnotatedLine &Line, unsigned FirstIndent,
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +0000397 const AnnotatedToken &RootToken,
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000398 WhitespaceManager &Whitespaces, bool StructuralError)
Daniel Jasper1321eb52012-12-18 21:05:13 +0000399 : Style(Style), SourceMgr(SourceMgr), Line(Line),
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000400 FirstIndent(FirstIndent), RootToken(RootToken),
Daniel Jasperf11a7052013-02-21 21:33:55 +0000401 Whitespaces(Whitespaces), Count(0) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000402
Manuel Klimekd4397b92013-01-04 23:34:14 +0000403 /// \brief Formats an \c UnwrappedLine.
404 ///
405 /// \returns The column after the last token in the last line of the
406 /// \c UnwrappedLine.
Daniel Jaspera4d46212013-02-28 11:05:57 +0000407 unsigned format(const AnnotatedLine *NextLine) {
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000408 // Initialize state dependent on indent.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000409 LineState State;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000410 State.Column = FirstIndent;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000411 State.NextToken = &RootToken;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000412 State.Stack.push_back(
413 ParenState(FirstIndent + 4, FirstIndent, !Style.BinPackParameters,
414 /*HasMultiParameterLine=*/ false));
Daniel Jasper2e603772013-01-29 11:21:01 +0000415 State.VariablePos = 0;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000416 State.LineContainsContinuedForLoopSection = false;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000417 State.ParenLevel = 0;
Manuel Klimekb56b6d12013-02-20 15:25:48 +0000418 State.StartOfStringLiteral = 0;
Daniel Jaspercf5767d2013-02-18 11:05:07 +0000419 State.StartOfLineLevel = State.ParenLevel;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000420
Manuel Klimekca547db2013-01-16 14:55:28 +0000421 DEBUG({
422 DebugTokenState(*State.NextToken);
423 });
424
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000425 // The first token has already been indented and thus consumed.
Manuel Klimek8092a942013-02-20 10:15:13 +0000426 moveStateToNextToken(State, /*DryRun=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000427
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000428 // If everything fits on a single line, just put it there.
Daniel Jaspera4d46212013-02-28 11:05:57 +0000429 unsigned ColumnLimit = Style.ColumnLimit;
430 if (NextLine && NextLine->InPPDirective &&
431 !NextLine->First.FormatTok.HasUnescapedNewline)
432 ColumnLimit = getColumnLimit();
433 if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) {
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000434 while (State.NextToken != NULL) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000435 addTokenToState(false, false, State);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000436 }
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000437 return State.Column;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000438 }
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000439
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000440 // If the ObjC method declaration does not fit on a line, we should format
441 // it with one arg per line.
442 if (Line.Type == LT_ObjCMethodDecl)
443 State.Stack.back().BreakBeforeParameter = true;
444
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000445 // Find best solution in solution space.
446 return analyzeSolutionSpace(State);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000447 }
448
449private:
Manuel Klimekca547db2013-01-16 14:55:28 +0000450 void DebugTokenState(const AnnotatedToken &AnnotatedTok) {
451 const Token &Tok = AnnotatedTok.FormatTok.Tok;
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000452 llvm::errs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
453 Tok.getLength());
Manuel Klimekca547db2013-01-16 14:55:28 +0000454 llvm::errs();
455 }
456
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000457 struct ParenState {
Daniel Jasperd399bff2013-02-05 09:41:21 +0000458 ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking,
459 bool HasMultiParameterLine)
Daniel Jasper29f123b2013-02-08 15:28:42 +0000460 : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
461 BreakBeforeClosingBrace(false), QuestionColumn(0),
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000462 AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
Daniel Jasper24849712013-03-01 16:48:32 +0000463 HasMultiParameterLine(HasMultiParameterLine), ColonPos(0),
464 StartOfFunctionCall(0) {}
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000465
Daniel Jasperbac016b2012-12-03 18:12:45 +0000466 /// \brief The position to which a specific parenthesis level needs to be
467 /// indented.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000468 unsigned Indent;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000469
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000470 /// \brief The position of the last space on each level.
471 ///
472 /// Used e.g. to break like:
473 /// functionCall(Parameter, otherCall(
474 /// OtherParameter));
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000475 unsigned LastSpace;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000476
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000477 /// \brief The position the first "<<" operator encountered on each level.
478 ///
479 /// Used to align "<<" operators. 0 if no such operator has been encountered
480 /// on a level.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000481 unsigned FirstLessLess;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000482
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000483 /// \brief Whether a newline needs to be inserted before the block's closing
484 /// brace.
485 ///
486 /// We only want to insert a newline before the closing brace if there also
487 /// was a newline after the beginning left brace.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000488 bool BreakBeforeClosingBrace;
489
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000490 /// \brief The column of a \c ? in a conditional expression;
491 unsigned QuestionColumn;
492
Daniel Jasperf343cab2013-01-31 14:59:26 +0000493 /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple
494 /// lines, in this context.
495 bool AvoidBinPacking;
496
497 /// \brief Break after the next comma (or all the commas in this context if
498 /// \c AvoidBinPacking is \c true).
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000499 bool BreakBeforeParameter;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000500
501 /// \brief This context already has a line with more than one parameter.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000502 bool HasMultiParameterLine;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000503
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000504 /// \brief The position of the colon in an ObjC method declaration/call.
505 unsigned ColonPos;
Daniel Jasperc4615b72013-02-20 12:56:39 +0000506
Daniel Jasper24849712013-03-01 16:48:32 +0000507 /// \brief The start of the most recent function in a builder-type call.
508 unsigned StartOfFunctionCall;
509
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000510 bool operator<(const ParenState &Other) const {
511 if (Indent != Other.Indent)
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000512 return Indent < Other.Indent;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000513 if (LastSpace != Other.LastSpace)
514 return LastSpace < Other.LastSpace;
515 if (FirstLessLess != Other.FirstLessLess)
516 return FirstLessLess < Other.FirstLessLess;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000517 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
518 return BreakBeforeClosingBrace;
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000519 if (QuestionColumn != Other.QuestionColumn)
520 return QuestionColumn < Other.QuestionColumn;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000521 if (AvoidBinPacking != Other.AvoidBinPacking)
522 return AvoidBinPacking;
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000523 if (BreakBeforeParameter != Other.BreakBeforeParameter)
524 return BreakBeforeParameter;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000525 if (HasMultiParameterLine != Other.HasMultiParameterLine)
526 return HasMultiParameterLine;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000527 if (ColonPos != Other.ColonPos)
528 return ColonPos < Other.ColonPos;
Daniel Jasper24849712013-03-01 16:48:32 +0000529 if (StartOfFunctionCall != Other.StartOfFunctionCall)
530 return StartOfFunctionCall < Other.StartOfFunctionCall;
Daniel Jasperb3123142013-01-12 07:36:22 +0000531 return false;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000532 }
533 };
534
535 /// \brief The current state when indenting a unwrapped line.
536 ///
537 /// As the indenting tries different combinations this is copied by value.
538 struct LineState {
539 /// \brief The number of used columns in the current line.
540 unsigned Column;
541
542 /// \brief The token that needs to be next formatted.
543 const AnnotatedToken *NextToken;
544
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000545 /// \brief The column of the first variable name in a variable declaration.
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000546 ///
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000547 /// Used to align further variables if necessary.
Daniel Jasper2e603772013-01-29 11:21:01 +0000548 unsigned VariablePos;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000549
550 /// \brief \c true if this line contains a continued for-loop section.
551 bool LineContainsContinuedForLoopSection;
552
Daniel Jasper29f123b2013-02-08 15:28:42 +0000553 /// \brief The level of nesting inside (), [], <> and {}.
554 unsigned ParenLevel;
555
Daniel Jaspercf5767d2013-02-18 11:05:07 +0000556 /// \brief The \c ParenLevel at the start of this line.
557 unsigned StartOfLineLevel;
558
Manuel Klimekb56b6d12013-02-20 15:25:48 +0000559 /// \brief The start column of the string literal, if we're in a string
560 /// literal sequence, 0 otherwise.
561 unsigned StartOfStringLiteral;
562
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000563 /// \brief A stack keeping track of properties applying to parenthesis
564 /// levels.
565 std::vector<ParenState> Stack;
566
567 /// \brief Comparison operator to be able to used \c LineState in \c map.
568 bool operator<(const LineState &Other) const {
Daniel Jasperd7896702013-02-19 09:28:55 +0000569 if (NextToken != Other.NextToken)
570 return NextToken < Other.NextToken;
571 if (Column != Other.Column)
572 return Column < Other.Column;
573 if (VariablePos != Other.VariablePos)
574 return VariablePos < Other.VariablePos;
575 if (LineContainsContinuedForLoopSection !=
576 Other.LineContainsContinuedForLoopSection)
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000577 return LineContainsContinuedForLoopSection;
Daniel Jasperd7896702013-02-19 09:28:55 +0000578 if (ParenLevel != Other.ParenLevel)
579 return ParenLevel < Other.ParenLevel;
580 if (StartOfLineLevel != Other.StartOfLineLevel)
581 return StartOfLineLevel < Other.StartOfLineLevel;
Manuel Klimekb56b6d12013-02-20 15:25:48 +0000582 if (StartOfStringLiteral != Other.StartOfStringLiteral)
583 return StartOfStringLiteral < Other.StartOfStringLiteral;
Daniel Jasperd7896702013-02-19 09:28:55 +0000584 return Stack < Other.Stack;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000585 }
586 };
587
Daniel Jasper20409152012-12-04 14:54:30 +0000588 /// \brief Appends the next token to \p State and updates information
589 /// necessary for indentation.
590 ///
591 /// Puts the token on the current line if \p Newline is \c true and adds a
592 /// line break and necessary indentation otherwise.
593 ///
594 /// If \p DryRun is \c false, also creates and stores the required
595 /// \c Replacement.
Manuel Klimek8092a942013-02-20 10:15:13 +0000596 unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) {
Daniel Jasper9c837d02013-01-09 07:06:56 +0000597 const AnnotatedToken &Current = *State.NextToken;
598 const AnnotatedToken &Previous = *State.NextToken->Parent;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000599 assert(State.Stack.size());
Daniel Jasperbac016b2012-12-03 18:12:45 +0000600
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000601 if (Current.Type == TT_ImplicitStringLiteral) {
602 State.Column += State.NextToken->FormatTok.WhiteSpaceLength +
603 State.NextToken->FormatTok.TokenLength;
604 if (State.NextToken->Children.empty())
605 State.NextToken = NULL;
606 else
607 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek8092a942013-02-20 10:15:13 +0000608 return 0;
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000609 }
610
Daniel Jasperbac016b2012-12-03 18:12:45 +0000611 if (Newline) {
Manuel Klimek060143e2013-01-02 18:33:23 +0000612 unsigned WhitespaceStartColumn = State.Column;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000613 if (Current.is(tok::r_brace)) {
614 State.Column = Line.Level * 2;
Daniel Jasper9c837d02013-01-09 07:06:56 +0000615 } else if (Current.is(tok::string_literal) &&
Manuel Klimekb56b6d12013-02-20 15:25:48 +0000616 State.StartOfStringLiteral != 0) {
617 State.Column = State.StartOfStringLiteral;
Daniel Jasper66d19bd2013-02-18 11:59:17 +0000618 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasper9c837d02013-01-09 07:06:56 +0000619 } else if (Current.is(tok::lessless) &&
Daniel Jasper29f123b2013-02-08 15:28:42 +0000620 State.Stack.back().FirstLessLess != 0) {
621 State.Column = State.Stack.back().FirstLessLess;
622 } else if (State.ParenLevel != 0 &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000623 (Previous.isOneOf(tok::equal, tok::coloncolon) ||
624 Current.isOneOf(tok::period, tok::arrow, tok::question))) {
Daniel Jasper9c837d02013-01-09 07:06:56 +0000625 // Indent and extra 4 spaces after if we know the current expression is
626 // continued. Don't do that on the top level, as we already indent 4
627 // there.
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000628 State.Column = std::max(State.Stack.back().LastSpace,
629 State.Stack.back().Indent) + 4;
630 } else if (Current.Type == TT_ConditionalExpr) {
631 State.Column = State.Stack.back().QuestionColumn;
Daniel Jasper2e603772013-01-29 11:21:01 +0000632 } else if (Previous.is(tok::comma) && State.VariablePos != 0 &&
Daniel Jasper29f123b2013-02-08 15:28:42 +0000633 ((RootToken.is(tok::kw_for) && State.ParenLevel == 1) ||
634 State.ParenLevel == 0)) {
Daniel Jasper2e603772013-01-29 11:21:01 +0000635 State.Column = State.VariablePos;
Daniel Jasper3c08a812013-02-24 18:54:32 +0000636 } else if (Previous.ClosesTemplateDeclaration ||
637 (Current.Type == TT_StartOfName && State.ParenLevel == 0)) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000638 State.Column = State.Stack.back().Indent - 4;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000639 } else if (Current.Type == TT_ObjCSelectorName) {
640 if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) {
641 State.Column =
642 State.Stack.back().ColonPos - Current.FormatTok.TokenLength;
643 } else {
644 State.Column = State.Stack.back().Indent;
645 State.Stack.back().ColonPos =
646 State.Column + Current.FormatTok.TokenLength;
647 }
Daniel Jasper3c08a812013-02-24 18:54:32 +0000648 } else if (Previous.Type == TT_ObjCMethodExpr ||
649 Current.Type == TT_StartOfName) {
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000650 State.Column = State.Stack.back().Indent + 4;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000651 } else {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000652 State.Column = State.Stack.back().Indent;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000653 }
654
Daniel Jasper7878a7b2013-02-15 11:07:25 +0000655 if (Current.is(tok::question))
Daniel Jasper237d4c12013-02-23 21:01:55 +0000656 State.Stack.back().BreakBeforeParameter = true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000657 if (Previous.isOneOf(tok::comma, tok::semi) &&
Daniel Jasper237d4c12013-02-23 21:01:55 +0000658 !State.Stack.back().AvoidBinPacking)
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000659 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000660
Manuel Klimek060143e2013-01-02 18:33:23 +0000661 if (!DryRun) {
Daniel Jasper1ef81d52013-02-26 13:10:34 +0000662 unsigned NewLines = 1;
663 if (Current.Type == TT_LineComment)
664 NewLines =
665 std::max(NewLines, std::min(Current.FormatTok.NewlinesBefore,
666 Style.MaxEmptyLinesToKeep + 1));
Manuel Klimek060143e2013-01-02 18:33:23 +0000667 if (!Line.InPPDirective)
Daniel Jasperc4615b72013-02-20 12:56:39 +0000668 Whitespaces.replaceWhitespace(Current, NewLines, State.Column,
Alexander Kornienko052685c2013-03-19 17:41:36 +0000669 WhitespaceStartColumn);
Manuel Klimek060143e2013-01-02 18:33:23 +0000670 else
Daniel Jasperc4615b72013-02-20 12:56:39 +0000671 Whitespaces.replacePPWhitespace(Current, NewLines, State.Column,
Alexander Kornienko052685c2013-03-19 17:41:36 +0000672 WhitespaceStartColumn);
Manuel Klimek060143e2013-01-02 18:33:23 +0000673 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000674
Daniel Jasper29f123b2013-02-08 15:28:42 +0000675 State.Stack.back().LastSpace = State.Column;
Daniel Jaspercf5767d2013-02-18 11:05:07 +0000676 State.StartOfLineLevel = State.ParenLevel;
Daniel Jasper237d4c12013-02-23 21:01:55 +0000677
678 // Any break on this level means that the parent level has been broken
679 // and we need to avoid bin packing there.
680 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
681 State.Stack[i].BreakBeforeParameter = true;
682 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000683 if (Current.isOneOf(tok::period, tok::arrow))
Daniel Jasperfaab0d32013-02-27 09:47:53 +0000684 State.Stack.back().BreakBeforeParameter = true;
685
Daniel Jasper237d4c12013-02-23 21:01:55 +0000686 // If we break after {, we should also break before the corresponding }.
687 if (Previous.is(tok::l_brace))
688 State.Stack.back().BreakBeforeClosingBrace = true;
689
690 if (State.Stack.back().AvoidBinPacking) {
691 // If we are breaking after '(', '{', '<', this is not bin packing
692 // unless AllowAllParametersOfDeclarationOnNextLine is false.
Daniel Jasper3c08a812013-02-24 18:54:32 +0000693 if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace)) ||
Daniel Jasper237d4c12013-02-23 21:01:55 +0000694 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
695 Line.MustBeDeclaration))
696 State.Stack.back().BreakBeforeParameter = true;
697 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000698 } else {
Daniel Jasper9c3e71a2013-02-25 15:59:54 +0000699 // FIXME: Put VariablePos into ParenState and remove second part of if().
700 if (Current.is(tok::equal) &&
701 (RootToken.is(tok::kw_for) || State.ParenLevel == 0))
Daniel Jasper2e603772013-01-29 11:21:01 +0000702 State.VariablePos = State.Column - Previous.FormatTok.TokenLength;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000703
Daniel Jasper729a7432013-02-11 12:36:37 +0000704 unsigned Spaces = State.NextToken->SpacesRequiredBefore;
Daniel Jasper20409152012-12-04 14:54:30 +0000705
Daniel Jasperbac016b2012-12-03 18:12:45 +0000706 if (!DryRun)
Alexander Kornienko052685c2013-03-19 17:41:36 +0000707 Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column);
Daniel Jasper20409152012-12-04 14:54:30 +0000708
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000709 if (Current.Type == TT_ObjCSelectorName &&
710 State.Stack.back().ColonPos == 0) {
711 if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
712 State.Column + Spaces + Current.FormatTok.TokenLength)
713 State.Stack.back().ColonPos =
714 State.Stack.back().Indent + Current.LongestObjCSelectorName;
715 else
716 State.Stack.back().ColonPos =
Daniel Jasper9e9e6e02013-02-06 16:00:26 +0000717 State.Column + Spaces + Current.FormatTok.TokenLength;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000718 }
719
Daniel Jasperd4f2c2e2013-01-29 19:41:55 +0000720 if (Current.Type != TT_LineComment &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000721 (Previous.isOneOf(tok::l_paren, tok::l_brace) ||
Daniel Jasperd4f2c2e2013-01-29 19:41:55 +0000722 State.NextToken->Parent->Type == TT_TemplateOpener))
Daniel Jasper29f123b2013-02-08 15:28:42 +0000723 State.Stack.back().Indent = State.Column + Spaces;
Daniel Jaspercda16502013-02-04 08:34:57 +0000724 if (Previous.is(tok::comma) && !isTrailingComment(Current))
Daniel Jasper29f123b2013-02-08 15:28:42 +0000725 State.Stack.back().HasMultiParameterLine = true;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000726
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000727 State.Column += Spaces;
Daniel Jaspere438bac2013-01-23 20:41:06 +0000728 if (Current.is(tok::l_paren) && Previous.is(tok::kw_if))
729 // Treat the condition inside an if as if it was a second function
730 // parameter, i.e. let nested calls have an indent of 4.
731 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
Daniel Jasper29f123b2013-02-08 15:28:42 +0000732 else if (Previous.is(tok::comma) && State.ParenLevel != 0)
Daniel Jaspere438bac2013-01-23 20:41:06 +0000733 // Top-level spaces are exempt as that mostly leads to better results.
734 State.Stack.back().LastSpace = State.Column;
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000735 else if ((Previous.Type == TT_BinaryOperator ||
Daniel Jasper02b771e2013-01-28 13:31:35 +0000736 Previous.Type == TT_ConditionalExpr ||
737 Previous.Type == TT_CtorInitializerColon) &&
Daniel Jasperae8699b2013-01-28 09:35:24 +0000738 getPrecedence(Previous) != prec::Assignment)
739 State.Stack.back().LastSpace = State.Column;
Daniel Jasper6cabab42013-02-14 08:42:54 +0000740 else if (Previous.Type == TT_InheritanceColon)
741 State.Stack.back().Indent = State.Column;
Daniel Jasper986e17f2013-01-28 07:35:34 +0000742 else if (Previous.ParameterCount > 1 &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000743 (Previous.isOneOf(tok::l_paren, tok::l_square, tok::l_brace) ||
Daniel Jasper986e17f2013-01-28 07:35:34 +0000744 Previous.Type == TT_TemplateOpener))
745 // If this function has multiple parameters, indent nested calls from
746 // the start of the first parameter.
747 State.Stack.back().LastSpace = State.Column;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000748 }
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000749
Manuel Klimek8092a942013-02-20 10:15:13 +0000750 return moveStateToNextToken(State, DryRun);
Daniel Jasper20409152012-12-04 14:54:30 +0000751 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000752
Daniel Jasper20409152012-12-04 14:54:30 +0000753 /// \brief Mark the next token as consumed in \p State and modify its stacks
754 /// accordingly.
Manuel Klimek8092a942013-02-20 10:15:13 +0000755 unsigned moveStateToNextToken(LineState &State, bool DryRun) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000756 const AnnotatedToken &Current = *State.NextToken;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000757 assert(State.Stack.size());
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000758
Daniel Jasper6cabab42013-02-14 08:42:54 +0000759 if (Current.Type == TT_InheritanceColon)
760 State.Stack.back().AvoidBinPacking = true;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000761 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
762 State.Stack.back().FirstLessLess = State.Column;
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000763 if (Current.is(tok::question))
764 State.Stack.back().QuestionColumn = State.Column;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000765 if (Current.isOneOf(tok::period, tok::arrow) &&
Daniel Jasper24849712013-03-01 16:48:32 +0000766 Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
767 State.Stack.back().StartOfFunctionCall =
768 Current.LastInChainOfCalls ? 0 : State.Column;
Daniel Jasper7d812812013-02-21 15:00:29 +0000769 if (Current.Type == TT_CtorInitializerColon) {
770 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
771 State.Stack.back().AvoidBinPacking = true;
772 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000773 }
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000774
Daniel Jasper29f123b2013-02-08 15:28:42 +0000775 // Insert scopes created by fake parenthesis.
776 for (unsigned i = 0, e = Current.FakeLParens; i != e; ++i) {
777 ParenState NewParenState = State.Stack.back();
778 NewParenState.Indent = std::max(State.Column, State.Stack.back().Indent);
Daniel Jasper237d4c12013-02-23 21:01:55 +0000779 NewParenState.BreakBeforeParameter = false;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000780 State.Stack.push_back(NewParenState);
781 }
782
Daniel Jaspercf225b62012-12-24 13:43:52 +0000783 // If we encounter an opening (, [, { or <, we add a level to our stacks to
Daniel Jasper20409152012-12-04 14:54:30 +0000784 // prepare for the following tokens.
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000785 if (Current.isOneOf(tok::l_paren, tok::l_square, tok::l_brace) ||
Daniel Jasper26f7e782013-01-08 14:56:18 +0000786 State.NextToken->Type == TT_TemplateOpener) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000787 unsigned NewIndent;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000788 bool AvoidBinPacking;
Manuel Klimek2851c162013-01-10 14:36:46 +0000789 if (Current.is(tok::l_brace)) {
Daniel Jasperf343cab2013-01-31 14:59:26 +0000790 NewIndent = 2 + State.Stack.back().LastSpace;
791 AvoidBinPacking = false;
Manuel Klimek2851c162013-01-10 14:36:46 +0000792 } else {
Daniel Jasper24849712013-03-01 16:48:32 +0000793 NewIndent = 4 + std::max(State.Stack.back().LastSpace,
794 State.Stack.back().StartOfFunctionCall);
Daniel Jasper3a39ac72013-02-28 09:39:12 +0000795 AvoidBinPacking =
796 !Style.BinPackParameters || State.Stack.back().AvoidBinPacking;
Manuel Klimek2851c162013-01-10 14:36:46 +0000797 }
Daniel Jasperd399bff2013-02-05 09:41:21 +0000798 State.Stack.push_back(
799 ParenState(NewIndent, State.Stack.back().LastSpace, AvoidBinPacking,
800 State.Stack.back().HasMultiParameterLine));
Daniel Jasper29f123b2013-02-08 15:28:42 +0000801 ++State.ParenLevel;
Daniel Jasper20409152012-12-04 14:54:30 +0000802 }
803
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000804 // If this '[' opens an ObjC call, determine whether all parameters fit into
805 // one line and put one per line if they don't.
806 if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
807 Current.MatchingParen != NULL) {
808 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
809 State.Stack.back().BreakBeforeParameter = true;
810 }
811
Daniel Jaspercf225b62012-12-24 13:43:52 +0000812 // If we encounter a closing ), ], } or >, we can remove a level from our
Daniel Jasper20409152012-12-04 14:54:30 +0000813 // stacks.
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000814 if (Current.isOneOf(tok::r_paren, tok::r_square) ||
Daniel Jasper26f7e782013-01-08 14:56:18 +0000815 (Current.is(tok::r_brace) && State.NextToken != &RootToken) ||
816 State.NextToken->Type == TT_TemplateCloser) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000817 State.Stack.pop_back();
Daniel Jasper29f123b2013-02-08 15:28:42 +0000818 --State.ParenLevel;
819 }
820
821 // Remove scopes created by fake parenthesis.
822 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
823 State.Stack.pop_back();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000824 }
Manuel Klimek2851c162013-01-10 14:36:46 +0000825
Manuel Klimeke9a62262013-02-20 15:32:58 +0000826 if (Current.is(tok::string_literal)) {
Manuel Klimekb56b6d12013-02-20 15:25:48 +0000827 State.StartOfStringLiteral = State.Column;
828 } else if (Current.isNot(tok::comment)) {
829 State.StartOfStringLiteral = 0;
830 }
831
Manuel Klimek8092a942013-02-20 10:15:13 +0000832 State.Column += Current.FormatTok.TokenLength;
833
Daniel Jasper26f7e782013-01-08 14:56:18 +0000834 if (State.NextToken->Children.empty())
835 State.NextToken = NULL;
836 else
837 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek2851c162013-01-10 14:36:46 +0000838
Manuel Klimek8092a942013-02-20 10:15:13 +0000839 return breakProtrudingToken(Current, State, DryRun);
840 }
841
842 /// \brief If the current token sticks out over the end of the line, break
843 /// it if possible.
844 unsigned breakProtrudingToken(const AnnotatedToken &Current, LineState &State,
845 bool DryRun) {
846 if (Current.isNot(tok::string_literal))
847 return 0;
Manuel Klimekaa62d0c2013-03-08 18:59:48 +0000848 // Only break up default narrow strings.
Alexander Kornienko052685c2013-03-19 17:41:36 +0000849 const char *LiteralData = Current.FormatTok.Tok.getLiteralData();
850 if (!LiteralData || *LiteralData != '"')
Manuel Klimekaa62d0c2013-03-08 18:59:48 +0000851 return 0;
Manuel Klimek8092a942013-02-20 10:15:13 +0000852
853 unsigned Penalty = 0;
854 unsigned TailOffset = 0;
855 unsigned TailLength = Current.FormatTok.TokenLength;
856 unsigned StartColumn = State.Column - Current.FormatTok.TokenLength;
857 unsigned OffsetFromStart = 0;
858 while (StartColumn + TailLength > getColumnLimit()) {
Alexander Kornienko052685c2013-03-19 17:41:36 +0000859 StringRef Text = StringRef(LiteralData + TailOffset, TailLength);
Manuel Klimekbc30c712013-03-01 13:29:19 +0000860 if (StartColumn + OffsetFromStart + 1 > getColumnLimit())
Manuel Klimekaf31fd72013-03-01 13:14:08 +0000861 break;
Manuel Klimekbc30c712013-03-01 13:29:19 +0000862 StringRef::size_type SplitPoint = getSplitPoint(
863 Text, getColumnLimit() - StartColumn - OffsetFromStart - 1);
Manuel Klimek8092a942013-02-20 10:15:13 +0000864 if (SplitPoint == StringRef::npos)
865 break;
866 assert(SplitPoint != 0);
867 // +2, because 'Text' starts after the opening quotes, and does not
868 // include the closing quote we need to insert.
869 unsigned WhitespaceStartColumn =
870 StartColumn + OffsetFromStart + SplitPoint + 2;
871 State.Stack.back().LastSpace = StartColumn;
872 if (!DryRun) {
Alexander Kornienko052685c2013-03-19 17:41:36 +0000873 Whitespaces.breakToken(Current.FormatTok, TailOffset + SplitPoint + 1,
874 0, "\"", "\"", Line.InPPDirective, StartColumn,
875 WhitespaceStartColumn);
Manuel Klimek8092a942013-02-20 10:15:13 +0000876 }
877 TailOffset += SplitPoint + 1;
878 TailLength -= SplitPoint + 1;
879 OffsetFromStart = 1;
Daniel Jasper0fb382b2013-02-26 12:52:34 +0000880 Penalty += Style.PenaltyExcessCharacter;
Daniel Jasperfaab0d32013-02-27 09:47:53 +0000881 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
882 State.Stack[i].BreakBeforeParameter = true;
Manuel Klimek8092a942013-02-20 10:15:13 +0000883 }
884 State.Column = StartColumn + TailLength;
885 return Penalty;
886 }
887
888 StringRef::size_type
889 getSplitPoint(StringRef Text, StringRef::size_type Offset) {
Manuel Klimekaf31fd72013-03-01 13:14:08 +0000890 StringRef::size_type SpaceOffset = Text.rfind(' ', Offset);
Manuel Klimek00905912013-03-04 20:03:38 +0000891 if (SpaceOffset != StringRef::npos && SpaceOffset != 0)
Manuel Klimekbc30c712013-03-01 13:29:19 +0000892 return SpaceOffset;
893 StringRef::size_type SlashOffset = Text.rfind('/', Offset);
Manuel Klimek00905912013-03-04 20:03:38 +0000894 if (SlashOffset != StringRef::npos && SlashOffset != 0)
Manuel Klimekbc30c712013-03-01 13:29:19 +0000895 return SlashOffset;
Manuel Klimekaa62d0c2013-03-08 18:59:48 +0000896 StringRef::size_type Split = getStartOfCharacter(Text, Offset);
897 if (Split != StringRef::npos && Split > 1)
Manuel Klimekbc30c712013-03-01 13:29:19 +0000898 // Do not split at 0.
Manuel Klimekaa62d0c2013-03-08 18:59:48 +0000899 return Split - 1;
Manuel Klimekbc30c712013-03-01 13:29:19 +0000900 return StringRef::npos;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000901 }
902
Manuel Klimekaa62d0c2013-03-08 18:59:48 +0000903 StringRef::size_type
904 getStartOfCharacter(StringRef Text, StringRef::size_type Offset) {
905 StringRef::size_type NextEscape = Text.find('\\');
906 while (NextEscape != StringRef::npos && NextEscape < Offset) {
907 StringRef::size_type SequenceLength =
908 getEscapeSequenceLength(Text.substr(NextEscape));
909 if (Offset < NextEscape + SequenceLength)
910 return NextEscape;
911 NextEscape = Text.find('\\', NextEscape + SequenceLength);
912 }
913 return Offset;
914 }
915
916 unsigned getEscapeSequenceLength(StringRef Text) {
917 assert(Text[0] == '\\');
918 if (Text.size() < 2)
919 return 1;
920
921 switch (Text[1]) {
922 case 'u':
923 return 6;
924 case 'U':
925 return 10;
926 case 'x':
927 return getHexLength(Text);
928 default:
929 if (Text[1] >= '0' && Text[1] <= '7')
930 return getOctalLength(Text);
931 return 2;
932 }
933 }
934
935 unsigned getHexLength(StringRef Text) {
936 unsigned I = 2; // Point after '\x'.
937 while (I < Text.size() && ((Text[I] >= '0' && Text[I] <= '9') ||
938 (Text[I] >= 'a' && Text[I] <= 'f') ||
939 (Text[I] >= 'A' && Text[I] <= 'F'))) {
940 ++I;
941 }
942 return I;
943 }
944
945 unsigned getOctalLength(StringRef Text) {
946 unsigned I = 1;
947 while (I < Text.size() && I < 4 && (Text[I] >= '0' && Text[I] <= '7')) {
948 ++I;
949 }
950 return I;
951 }
952
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000953 unsigned getColumnLimit() {
Daniel Jaspera4d46212013-02-28 11:05:57 +0000954 return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0);
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000955 }
956
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000957 /// \brief An edge in the solution space from \c Previous->State to \c State,
958 /// inserting a newline dependent on the \c NewLine.
959 struct StateNode {
960 StateNode(const LineState &State, bool NewLine, StateNode *Previous)
Daniel Jasperf11a7052013-02-21 21:33:55 +0000961 : State(State), NewLine(NewLine), Previous(Previous) {}
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000962 LineState State;
963 bool NewLine;
964 StateNode *Previous;
965 };
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000966
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000967 /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
968 ///
969 /// In case of equal penalties, we want to prefer states that were inserted
970 /// first. During state generation we make sure that we insert states first
971 /// that break the line as late as possible.
972 typedef std::pair<unsigned, unsigned> OrderedPenalty;
973
974 /// \brief An item in the prioritized BFS search queue. The \c StateNode's
975 /// \c State has the given \c OrderedPenalty.
976 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
977
978 /// \brief The BFS queue type.
979 typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
980 std::greater<QueueItem> > QueueType;
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000981
982 /// \brief Analyze the entire solution space starting from \p InitialState.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000983 ///
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000984 /// This implements a variant of Dijkstra's algorithm on the graph that spans
985 /// the solution space (\c LineStates are the nodes). The algorithm tries to
986 /// find the shortest path (the one with lowest penalty) from \p InitialState
987 /// to a state where all tokens are placed.
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000988 unsigned analyzeSolutionSpace(LineState &InitialState) {
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000989 std::set<LineState> Seen;
990
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000991 // Insert start element into queue.
Daniel Jasperfc759082013-02-14 14:26:07 +0000992 StateNode *Node =
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000993 new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
994 Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
995 ++Count;
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000996
997 // While not empty, take first element and follow edges.
998 while (!Queue.empty()) {
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000999 unsigned Penalty = Queue.top().first.first;
Daniel Jasperfc759082013-02-14 14:26:07 +00001000 StateNode *Node = Queue.top().second;
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001001 if (Node->State.NextToken == NULL) {
Daniel Jasper01786732013-02-04 07:21:18 +00001002 DEBUG(llvm::errs() << "\n---\nPenalty for line: " << Penalty << "\n");
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001003 break;
Daniel Jasper01786732013-02-04 07:21:18 +00001004 }
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001005 Queue.pop();
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001006
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001007 if (!Seen.insert(Node->State).second)
1008 // State already examined with lower penalty.
1009 continue;
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001010
Manuel Klimek62a48fb2013-02-13 10:54:19 +00001011 addNextStateToQueue(Penalty, Node, /*NewLine=*/ false);
1012 addNextStateToQueue(Penalty, Node, /*NewLine=*/ true);
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001013 }
1014
1015 if (Queue.empty())
1016 // We were unable to find a solution, do nothing.
1017 // FIXME: Add diagnostic?
Daniel Jasperbac016b2012-12-03 18:12:45 +00001018 return 0;
1019
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001020 // Reconstruct the solution.
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001021 reconstructPath(InitialState, Queue.top().second);
Daniel Jasper01786732013-02-04 07:21:18 +00001022 DEBUG(llvm::errs() << "---\n");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001023
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001024 // Return the column after the last token of the solution.
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001025 return Queue.top().second->State.Column;
1026 }
1027
1028 void reconstructPath(LineState &State, StateNode *Current) {
1029 // FIXME: This recursive implementation limits the possible number
1030 // of tokens per line if compiled into a binary with small stack space.
1031 // To become more independent of stack frame limitations we would need
1032 // to also change the TokenAnnotator.
1033 if (Current->Previous == NULL)
1034 return;
1035 reconstructPath(State, Current->Previous);
1036 DEBUG({
1037 if (Current->NewLine) {
Daniel Jaspera03ab102013-02-13 20:33:44 +00001038 llvm::errs()
1039 << "Penalty for splitting before "
1040 << Current->Previous->State.NextToken->FormatTok.Tok.getName()
1041 << ": " << Current->Previous->State.NextToken->SplitPenalty << "\n";
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001042 }
1043 });
1044 addTokenToState(Current->NewLine, false, State);
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001045 }
1046
Manuel Klimek62a48fb2013-02-13 10:54:19 +00001047 /// \brief Add the following state to the analysis queue \c Queue.
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001048 ///
Manuel Klimek62a48fb2013-02-13 10:54:19 +00001049 /// Assume the current state is \p PreviousNode and has been reached with a
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001050 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
Manuel Klimek62a48fb2013-02-13 10:54:19 +00001051 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
1052 bool NewLine) {
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001053 if (NewLine && !canBreak(PreviousNode->State))
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001054 return;
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001055 if (!NewLine && mustBreak(PreviousNode->State))
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001056 return;
Daniel Jasperae8699b2013-01-28 09:35:24 +00001057 if (NewLine)
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001058 Penalty += PreviousNode->State.NextToken->SplitPenalty;
1059
1060 StateNode *Node = new (Allocator.Allocate())
1061 StateNode(PreviousNode->State, NewLine, PreviousNode);
Manuel Klimek8092a942013-02-20 10:15:13 +00001062 Penalty += addTokenToState(NewLine, true, Node->State);
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001063 if (Node->State.Column > getColumnLimit()) {
1064 unsigned ExcessCharacters = Node->State.Column - getColumnLimit();
Daniel Jasper01786732013-02-04 07:21:18 +00001065 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001066 }
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001067
1068 Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
1069 ++Count;
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001070 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001071
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001072 /// \brief Returns \c true, if a line break after \p State is allowed.
1073 bool canBreak(const LineState &State) {
1074 if (!State.NextToken->CanBreakBefore &&
1075 !(State.NextToken->is(tok::r_brace) &&
1076 State.Stack.back().BreakBeforeClosingBrace))
1077 return false;
1078 // Trying to insert a parameter on a new line if there are already more than
1079 // one parameter on the current line is bin packing.
Daniel Jasperd399bff2013-02-05 09:41:21 +00001080 if (State.Stack.back().HasMultiParameterLine &&
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001081 State.Stack.back().AvoidBinPacking)
1082 return false;
1083 return true;
1084 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001085
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001086 /// \brief Returns \c true, if a line break after \p State is mandatory.
1087 bool mustBreak(const LineState &State) {
1088 if (State.NextToken->MustBreakBefore)
1089 return true;
1090 if (State.NextToken->is(tok::r_brace) &&
1091 State.Stack.back().BreakBeforeClosingBrace)
1092 return true;
1093 if (State.NextToken->Parent->is(tok::semi) &&
1094 State.LineContainsContinuedForLoopSection)
1095 return true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001096 if ((State.NextToken->Parent->isOneOf(tok::comma, tok::semi) ||
Daniel Jasper237d4c12013-02-23 21:01:55 +00001097 State.NextToken->is(tok::question) ||
1098 State.NextToken->Type == TT_ConditionalExpr) &&
Daniel Jasperce3d1a62013-02-08 08:22:00 +00001099 State.Stack.back().BreakBeforeParameter &&
Daniel Jasperc5cfa492013-02-14 09:19:04 +00001100 !isTrailingComment(*State.NextToken) &&
Daniel Jasper7d812812013-02-21 15:00:29 +00001101 State.NextToken->isNot(tok::r_paren) &&
1102 State.NextToken->isNot(tok::r_brace))
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001103 return true;
Daniel Jasperce3d1a62013-02-08 08:22:00 +00001104 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
1105 // out whether it is the first parameter. Clean this up.
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001106 if (State.NextToken->Type == TT_ObjCSelectorName &&
Daniel Jasperce3d1a62013-02-08 08:22:00 +00001107 State.NextToken->LongestObjCSelectorName == 0 &&
1108 State.Stack.back().BreakBeforeParameter)
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001109 return true;
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001110 if ((State.NextToken->Type == TT_CtorInitializerColon ||
1111 (State.NextToken->Parent->ClosesTemplateDeclaration &&
Daniel Jasper29f123b2013-02-08 15:28:42 +00001112 State.ParenLevel == 0)))
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001113 return true;
Daniel Jasper923ebef2013-03-14 13:45:21 +00001114 if (State.NextToken->Type == TT_InlineASMColon)
1115 return true;
Daniel Jasper3af59ce2013-03-15 14:57:30 +00001116 // This prevents breaks like:
1117 // ...
1118 // SomeParameter, OtherParameter).DoSomething(
1119 // ...
1120 // As they hide "DoSomething" and generally bad for readability.
1121 if (State.NextToken->isOneOf(tok::period, tok::arrow) &&
1122 getRemainingLength(State) + State.Column > getColumnLimit() &&
1123 State.ParenLevel < State.StartOfLineLevel)
1124 return true;
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001125 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001126 }
1127
Daniel Jasper3af59ce2013-03-15 14:57:30 +00001128 // Returns the total number of columns required for the remaining tokens.
1129 unsigned getRemainingLength(const LineState &State) {
1130 if (State.NextToken && State.NextToken->Parent)
1131 return Line.Last->TotalLength - State.NextToken->Parent->TotalLength;
1132 return 0;
1133 }
1134
Daniel Jasperbac016b2012-12-03 18:12:45 +00001135 FormatStyle Style;
1136 SourceManager &SourceMgr;
Daniel Jasper995e8202013-01-14 13:08:07 +00001137 const AnnotatedLine &Line;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001138 const unsigned FirstIndent;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001139 const AnnotatedToken &RootToken;
Daniel Jasperdcc2a622013-01-18 08:44:07 +00001140 WhitespaceManager &Whitespaces;
Manuel Klimek62a48fb2013-02-13 10:54:19 +00001141
1142 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1143 QueueType Queue;
1144 // Increasing count of \c StateNode items we have created. This is used
1145 // to create a deterministic order independent of the container.
1146 unsigned Count;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001147};
1148
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001149class LexerBasedFormatTokenSource : public FormatTokenSource {
1150public:
1151 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
Daniel Jasper1321eb52012-12-18 21:05:13 +00001152 : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001153 IdentTable(Lex.getLangOpts()) {
1154 Lex.SetKeepWhitespaceMode(true);
1155 }
1156
1157 virtual FormatToken getNextToken() {
1158 if (GreaterStashed) {
1159 FormatTok.NewlinesBefore = 0;
1160 FormatTok.WhiteSpaceStart =
1161 FormatTok.Tok.getLocation().getLocWithOffset(1);
1162 FormatTok.WhiteSpaceLength = 0;
1163 GreaterStashed = false;
1164 return FormatTok;
1165 }
1166
1167 FormatTok = FormatToken();
1168 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimek95419382013-01-07 07:56:50 +00001169 StringRef Text = rawTokenText(FormatTok.Tok);
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001170 FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
Manuel Klimekf6fd00b2013-01-05 22:56:06 +00001171 if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0)
1172 FormatTok.IsFirst = true;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001173
1174 // Consume and record whitespace until we find a significant token.
1175 while (FormatTok.Tok.is(tok::unknown)) {
Manuel Klimeka28fc062013-02-11 12:33:24 +00001176 unsigned Newlines = Text.count('\n');
Daniel Jasper1eee6c42013-03-04 13:43:19 +00001177 if (Newlines > 0)
1178 FormatTok.LastNewlineOffset =
1179 FormatTok.WhiteSpaceLength + Text.rfind('\n') + 1;
Manuel Klimeka28fc062013-02-11 12:33:24 +00001180 unsigned EscapedNewlines = Text.count("\\\n");
1181 FormatTok.NewlinesBefore += Newlines;
1182 FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001183 FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
1184
1185 if (FormatTok.Tok.is(tok::eof))
1186 return FormatTok;
1187 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimek95419382013-01-07 07:56:50 +00001188 Text = rawTokenText(FormatTok.Tok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001189 }
Manuel Klimek95419382013-01-07 07:56:50 +00001190
1191 // Now FormatTok is the next non-whitespace token.
1192 FormatTok.TokenLength = Text.size();
1193
Manuel Klimekd4397b92013-01-04 23:34:14 +00001194 // In case the token starts with escaped newlines, we want to
1195 // take them into account as whitespace - this pattern is quite frequent
1196 // in macro definitions.
1197 // FIXME: What do we want to do with other escaped spaces, and escaped
1198 // spaces or newlines in the middle of tokens?
1199 // FIXME: Add a more explicit test.
1200 unsigned i = 0;
Daniel Jasper71607512013-01-07 10:48:50 +00001201 while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
Manuel Klimek86721d22013-01-22 16:31:55 +00001202 // FIXME: ++FormatTok.NewlinesBefore is missing...
Manuel Klimekd4397b92013-01-04 23:34:14 +00001203 FormatTok.WhiteSpaceLength += 2;
Manuel Klimek95419382013-01-07 07:56:50 +00001204 FormatTok.TokenLength -= 2;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001205 i += 2;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001206 }
1207
1208 if (FormatTok.Tok.is(tok::raw_identifier)) {
Manuel Klimekd4397b92013-01-04 23:34:14 +00001209 IdentifierInfo &Info = IdentTable.get(Text);
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001210 FormatTok.Tok.setIdentifierInfo(&Info);
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001211 FormatTok.Tok.setKind(Info.getTokenID());
1212 }
1213
1214 if (FormatTok.Tok.is(tok::greatergreater)) {
1215 FormatTok.Tok.setKind(tok::greater);
Daniel Jasperb6f02f32013-02-28 10:06:05 +00001216 FormatTok.TokenLength = 1;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001217 GreaterStashed = true;
1218 }
1219
Daniel Jasper812c0452013-03-01 16:45:59 +00001220 // If we reformat comments, we remove trailing whitespace. Update the length
1221 // accordingly.
1222 if (FormatTok.Tok.is(tok::comment))
1223 FormatTok.TokenLength = Text.rtrim().size();
1224
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001225 return FormatTok;
1226 }
1227
Nico Weberc2e6d2a2013-02-11 15:32:15 +00001228 IdentifierTable &getIdentTable() { return IdentTable; }
1229
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001230private:
1231 FormatToken FormatTok;
1232 bool GreaterStashed;
1233 Lexer &Lex;
1234 SourceManager &SourceMgr;
1235 IdentifierTable IdentTable;
1236
1237 /// Returns the text of \c FormatTok.
Manuel Klimek95419382013-01-07 07:56:50 +00001238 StringRef rawTokenText(Token &Tok) {
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001239 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
1240 Tok.getLength());
1241 }
1242};
1243
Daniel Jasperbac016b2012-12-03 18:12:45 +00001244class Formatter : public UnwrappedLineConsumer {
1245public:
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001246 Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex,
1247 SourceManager &SourceMgr,
Daniel Jasperbac016b2012-12-03 18:12:45 +00001248 const std::vector<CharSourceRange> &Ranges)
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001249 : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienko052685c2013-03-19 17:41:36 +00001250 Whitespaces(SourceMgr, Style), Ranges(Ranges) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001251
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001252 virtual ~Formatter() {}
Daniel Jasperaccb0b02012-12-04 21:05:31 +00001253
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001254 tooling::Replacements format() {
1255 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
1256 UnwrappedLineParser Parser(Diag, Style, Tokens, *this);
1257 StructuralError = Parser.parse();
1258 unsigned PreviousEndOfLineColumn = 0;
1259 TokenAnnotator Annotator(Style, SourceMgr, Lex,
1260 Tokens.getIdentTable().get("in"));
1261 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1262 Annotator.annotate(AnnotatedLines[i]);
1263 }
1264 deriveLocalStyle();
1265 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1266 Annotator.calculateFormattingInformation(AnnotatedLines[i]);
Daniel Jasper6050a1e2013-03-13 15:53:12 +00001267
1268 // Adapt level to the next line if this is a comment.
1269 // FIXME: Can/should this be done in the UnwrappedLineParser?
1270 if (i + 1 != e && AnnotatedLines[i].First.is(tok::comment) &&
1271 AnnotatedLines[i].First.Children.empty() &&
1272 AnnotatedLines[i + 1].First.isNot(tok::r_brace))
1273 AnnotatedLines[i].Level = AnnotatedLines[i + 1].Level;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001274 }
1275 std::vector<int> IndentForLevel;
1276 bool PreviousLineWasTouched = false;
1277 for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
1278 E = AnnotatedLines.end();
1279 I != E; ++I) {
1280 const AnnotatedLine &TheLine = *I;
1281 const FormatToken &FirstTok = TheLine.First.FormatTok;
1282 int Offset = getIndentOffset(TheLine.First);
1283 while (IndentForLevel.size() <= TheLine.Level)
1284 IndentForLevel.push_back(-1);
1285 IndentForLevel.resize(TheLine.Level + 1);
1286 bool WasMoved =
1287 PreviousLineWasTouched && FirstTok.NewlinesBefore == 0;
1288 if (TheLine.First.is(tok::eof)) {
1289 if (PreviousLineWasTouched) {
1290 unsigned NewLines = std::min(FirstTok.NewlinesBefore, 1u);
1291 Whitespaces.replaceWhitespace(TheLine.First, NewLines, /*Indent*/ 0,
Alexander Kornienko052685c2013-03-19 17:41:36 +00001292 /*WhitespaceStartColumn*/ 0);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001293 }
1294 } else if (TheLine.Type != LT_Invalid &&
1295 (WasMoved || touchesLine(TheLine))) {
1296 unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
1297 unsigned Indent = LevelIndent;
1298 if (static_cast<int>(Indent) + Offset >= 0)
1299 Indent += Offset;
1300 if (!FirstTok.WhiteSpaceStart.isValid() || StructuralError) {
1301 Indent = LevelIndent = SourceMgr.getSpellingColumnNumber(
1302 FirstTok.Tok.getLocation()) - 1;
1303 } else {
1304 formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
1305 PreviousEndOfLineColumn);
1306 }
1307 tryFitMultipleLinesInOne(Indent, I, E);
1308 UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
1309 TheLine.First, Whitespaces,
1310 StructuralError);
1311 PreviousEndOfLineColumn =
1312 Formatter.format(I + 1 != E ? &*(I + 1) : NULL);
1313 IndentForLevel[TheLine.Level] = LevelIndent;
1314 PreviousLineWasTouched = true;
1315 } else {
1316 if (FirstTok.NewlinesBefore > 0 || FirstTok.IsFirst) {
1317 unsigned Indent =
1318 SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
1319 unsigned LevelIndent = Indent;
1320 if (static_cast<int>(LevelIndent) - Offset >= 0)
1321 LevelIndent -= Offset;
1322 IndentForLevel[TheLine.Level] = LevelIndent;
1323
1324 // Remove trailing whitespace of the previous line if it was touched.
1325 if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine))
1326 formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
1327 PreviousEndOfLineColumn);
1328 }
1329 // If we did not reformat this unwrapped line, the column at the end of
1330 // the last token is unchanged - thus, we can calculate the end of the
1331 // last token.
1332 SourceLocation LastLoc = TheLine.Last->FormatTok.Tok.getLocation();
1333 PreviousEndOfLineColumn =
1334 SourceMgr.getSpellingColumnNumber(LastLoc) +
1335 Lex.MeasureTokenLength(LastLoc, SourceMgr, Lex.getLangOpts()) - 1;
1336 PreviousLineWasTouched = false;
1337 }
1338 }
1339 return Whitespaces.generateReplacements();
1340 }
1341
1342private:
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001343 void deriveLocalStyle() {
1344 unsigned CountBoundToVariable = 0;
1345 unsigned CountBoundToType = 0;
1346 bool HasCpp03IncompatibleFormat = false;
1347 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1348 if (AnnotatedLines[i].First.Children.empty())
1349 continue;
1350 AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0];
1351 while (!Tok->Children.empty()) {
1352 if (Tok->Type == TT_PointerOrReference) {
1353 bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0;
1354 bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0;
1355 if (SpacesBefore && !SpacesAfter)
1356 ++CountBoundToVariable;
1357 else if (!SpacesBefore && SpacesAfter)
1358 ++CountBoundToType;
1359 }
1360
Daniel Jasper29f123b2013-02-08 15:28:42 +00001361 if (Tok->Type == TT_TemplateCloser &&
1362 Tok->Parent->Type == TT_TemplateCloser &&
1363 Tok->FormatTok.WhiteSpaceLength == 0)
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001364 HasCpp03IncompatibleFormat = true;
1365 Tok = &Tok->Children[0];
1366 }
1367 }
1368 if (Style.DerivePointerBinding) {
1369 if (CountBoundToType > CountBoundToVariable)
1370 Style.PointerBindsToType = true;
1371 else if (CountBoundToType < CountBoundToVariable)
1372 Style.PointerBindsToType = false;
1373 }
1374 if (Style.Standard == FormatStyle::LS_Auto) {
1375 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
1376 : FormatStyle::LS_Cpp03;
1377 }
1378 }
1379
Manuel Klimek547d5db2013-02-08 17:38:27 +00001380 /// \brief Get the indent of \p Level from \p IndentForLevel.
1381 ///
1382 /// \p IndentForLevel must contain the indent for the level \c l
1383 /// at \p IndentForLevel[l], or a value < 0 if the indent for
1384 /// that level is unknown.
Daniel Jasperfc759082013-02-14 14:26:07 +00001385 unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
Manuel Klimek547d5db2013-02-08 17:38:27 +00001386 if (IndentForLevel[Level] != -1)
1387 return IndentForLevel[Level];
Manuel Klimek52635ff2013-02-08 19:53:32 +00001388 if (Level == 0)
1389 return 0;
Daniel Jasperc78c6b32013-02-14 09:58:41 +00001390 return getIndent(IndentForLevel, Level - 1) + 2;
Manuel Klimek547d5db2013-02-08 17:38:27 +00001391 }
1392
1393 /// \brief Get the offset of the line relatively to the level.
1394 ///
1395 /// For example, 'public:' labels in classes are offset by 1 or 2
1396 /// characters to the left from their level.
Daniel Jasperc78c6b32013-02-14 09:58:41 +00001397 int getIndentOffset(const AnnotatedToken &RootToken) {
Manuel Klimek547d5db2013-02-08 17:38:27 +00001398 bool IsAccessModifier = false;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001399 if (RootToken.isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
Manuel Klimek547d5db2013-02-08 17:38:27 +00001400 IsAccessModifier = true;
1401 else if (RootToken.is(tok::at) && !RootToken.Children.empty() &&
1402 (RootToken.Children[0].isObjCAtKeyword(tok::objc_public) ||
1403 RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) ||
1404 RootToken.Children[0].isObjCAtKeyword(tok::objc_package) ||
1405 RootToken.Children[0].isObjCAtKeyword(tok::objc_private)))
1406 IsAccessModifier = true;
1407
1408 if (IsAccessModifier)
1409 return Style.AccessModifierOffset;
1410 return 0;
1411 }
1412
Manuel Klimek517e8942013-01-11 17:54:10 +00001413 /// \brief Tries to merge lines into one.
1414 ///
1415 /// This will change \c Line and \c AnnotatedLine to contain the merged line,
1416 /// if possible; note that \c I will be incremented when lines are merged.
1417 ///
1418 /// Returns whether the resulting \c Line can fit in a single line.
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001419 void tryFitMultipleLinesInOne(unsigned Indent,
Daniel Jasper995e8202013-01-14 13:08:07 +00001420 std::vector<AnnotatedLine>::iterator &I,
1421 std::vector<AnnotatedLine>::iterator E) {
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001422 // We can never merge stuff if there are trailing line comments.
1423 if (I->Last->Type == TT_LineComment)
1424 return;
1425
Daniel Jaspera4d46212013-02-28 11:05:57 +00001426 unsigned Limit = Style.ColumnLimit - Indent;
Daniel Jasperf11a7052013-02-21 21:33:55 +00001427 // If we already exceed the column limit, we set 'Limit' to 0. The different
1428 // tryMerge..() functions can then decide whether to still do merging.
1429 Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
Daniel Jasper55b08e72013-01-16 07:02:34 +00001430
Daniel Jasper9c8c40e2013-01-21 14:18:28 +00001431 if (I + 1 == E || (I + 1)->Type == LT_Invalid)
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001432 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001433
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001434 if (I->Last->is(tok::l_brace)) {
1435 tryMergeSimpleBlock(I, E, Limit);
1436 } else if (I->First.is(tok::kw_if)) {
1437 tryMergeSimpleIf(I, E, Limit);
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001438 } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline ||
1439 I->First.FormatTok.IsFirst)) {
1440 tryMergeSimplePPDirective(I, E, Limit);
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001441 }
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001442 return;
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001443 }
1444
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001445 void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
1446 std::vector<AnnotatedLine>::iterator E,
1447 unsigned Limit) {
Daniel Jasperf11a7052013-02-21 21:33:55 +00001448 if (Limit == 0)
1449 return;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001450 AnnotatedLine &Line = *I;
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001451 if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline)
1452 return;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001453 if (I + 2 != E && (I + 2)->InPPDirective &&
1454 !(I + 2)->First.FormatTok.HasUnescapedNewline)
1455 return;
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001456 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001457 return;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001458 join(Line, *(++I));
1459 }
1460
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001461 void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I,
1462 std::vector<AnnotatedLine>::iterator E,
1463 unsigned Limit) {
Daniel Jasperf11a7052013-02-21 21:33:55 +00001464 if (Limit == 0)
1465 return;
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001466 if (!Style.AllowShortIfStatementsOnASingleLine)
1467 return;
Manuel Klimek4c128122013-01-18 14:46:43 +00001468 if ((I + 1)->InPPDirective != I->InPPDirective ||
1469 ((I + 1)->InPPDirective &&
1470 (I + 1)->First.FormatTok.HasUnescapedNewline))
1471 return;
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001472 AnnotatedLine &Line = *I;
Daniel Jasper55b08e72013-01-16 07:02:34 +00001473 if (Line.Last->isNot(tok::r_paren))
1474 return;
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001475 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001476 return;
1477 if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment)
1478 return;
1479 // Only inline simple if's (no nested if or else).
1480 if (I + 2 != E && (I + 2)->First.is(tok::kw_else))
1481 return;
1482 join(Line, *(++I));
1483 }
1484
1485 void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
Daniel Jasper1a1ce832013-01-29 11:27:30 +00001486 std::vector<AnnotatedLine>::iterator E,
1487 unsigned Limit) {
Manuel Klimek517e8942013-01-11 17:54:10 +00001488 // First, check that the current line allows merging. This is the case if
1489 // we're not in a control flow statement and the last token is an opening
1490 // brace.
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001491 AnnotatedLine &Line = *I;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001492 if (Line.First.isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
1493 tok::kw_else, tok::kw_try, tok::kw_catch,
1494 tok::kw_for,
1495 // This gets rid of all ObjC @ keywords and methods.
1496 tok::at, tok::minus, tok::plus))
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001497 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001498
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001499 AnnotatedToken *Tok = &(I + 1)->First;
1500 if (Tok->Children.empty() && Tok->is(tok::r_brace) &&
Daniel Jasperf11a7052013-02-21 21:33:55 +00001501 !Tok->MustBreakBefore) {
1502 // We merge empty blocks even if the line exceeds the column limit.
Daniel Jasper729a7432013-02-11 12:36:37 +00001503 Tok->SpacesRequiredBefore = 0;
Daniel Jasperf11a7052013-02-21 21:33:55 +00001504 Tok->CanBreakBefore = true;
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001505 join(Line, *(I + 1));
1506 I += 1;
Daniel Jasperf11a7052013-02-21 21:33:55 +00001507 } else if (Limit != 0) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001508 // Check that we still have three lines and they fit into the limit.
1509 if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
1510 !nextTwoLinesFitInto(I, Limit))
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001511 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001512
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001513 // Second, check that the next line does not contain any braces - if it
1514 // does, readability declines when putting it into a single line.
1515 if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
1516 return;
1517 do {
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001518 if (Tok->isOneOf(tok::l_brace, tok::r_brace))
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001519 return;
1520 Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
1521 } while (Tok != NULL);
Manuel Klimek517e8942013-01-11 17:54:10 +00001522
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001523 // Last, check that the third line contains a single closing brace.
1524 Tok = &(I + 2)->First;
1525 if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
1526 Tok->MustBreakBefore)
1527 return;
1528
1529 join(Line, *(I + 1));
1530 join(Line, *(I + 2));
1531 I += 2;
Manuel Klimek517e8942013-01-11 17:54:10 +00001532 }
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001533 }
1534
1535 bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
1536 unsigned Limit) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001537 return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
1538 Limit;
Manuel Klimek517e8942013-01-11 17:54:10 +00001539 }
1540
Daniel Jasper995e8202013-01-14 13:08:07 +00001541 void join(AnnotatedLine &A, const AnnotatedLine &B) {
Daniel Jasperf11a7052013-02-21 21:33:55 +00001542 unsigned LengthA = A.Last->TotalLength + B.First.SpacesRequiredBefore;
Daniel Jasper995e8202013-01-14 13:08:07 +00001543 A.Last->Children.push_back(B.First);
1544 while (!A.Last->Children.empty()) {
1545 A.Last->Children[0].Parent = A.Last;
Daniel Jasperf11a7052013-02-21 21:33:55 +00001546 A.Last->Children[0].TotalLength += LengthA;
Daniel Jasper995e8202013-01-14 13:08:07 +00001547 A.Last = &A.Last->Children[0];
1548 }
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001549 }
1550
Daniel Jasper6f21a982013-03-13 07:49:51 +00001551 bool touchesRanges(const CharSourceRange &Range) {
Daniel Jasperf3023542013-03-07 20:50:00 +00001552 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1553 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
1554 Ranges[i].getBegin()) &&
1555 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1556 Range.getBegin()))
1557 return true;
1558 }
1559 return false;
1560 }
1561
1562 bool touchesLine(const AnnotatedLine &TheLine) {
Daniel Jasper995e8202013-01-14 13:08:07 +00001563 const FormatToken *First = &TheLine.First.FormatTok;
1564 const FormatToken *Last = &TheLine.Last->FormatTok;
Daniel Jaspercd162382013-01-07 13:26:07 +00001565 CharSourceRange LineRange = CharSourceRange::getTokenRange(
Daniel Jasper1eee6c42013-03-04 13:43:19 +00001566 First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset),
1567 Last->Tok.getLocation());
Daniel Jasperf3023542013-03-07 20:50:00 +00001568 return touchesRanges(LineRange);
1569 }
1570
1571 bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
1572 const FormatToken *First = &TheLine.First.FormatTok;
1573 CharSourceRange LineRange = CharSourceRange::getCharRange(
1574 First->WhiteSpaceStart,
1575 First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset));
1576 return touchesRanges(LineRange);
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001577 }
1578
1579 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001580 AnnotatedLines.push_back(AnnotatedLine(TheLine));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001581 }
1582
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001583 /// \brief Add a new line and the required indent before the first Token
1584 /// of the \c UnwrappedLine if there was no structural parsing error.
1585 /// Returns the indent level of the \c UnwrappedLine.
Manuel Klimek547d5db2013-02-08 17:38:27 +00001586 void formatFirstToken(const AnnotatedToken &RootToken, unsigned Indent,
1587 bool InPPDirective, unsigned PreviousEndOfLineColumn) {
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001588 const FormatToken &Tok = RootToken.FormatTok;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001589
Daniel Jasper1a1ce832013-01-29 11:27:30 +00001590 unsigned Newlines =
1591 std::min(Tok.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001592 if (Newlines == 0 && !Tok.IsFirst)
1593 Newlines = 1;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001594
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001595 if (!InPPDirective || Tok.HasUnescapedNewline) {
Alexander Kornienko052685c2013-03-19 17:41:36 +00001596 Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0);
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001597 } else {
Daniel Jasperdcc2a622013-01-18 08:44:07 +00001598 Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent,
Alexander Kornienko052685c2013-03-19 17:41:36 +00001599 PreviousEndOfLineColumn);
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001600 }
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001601 }
1602
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001603 DiagnosticsEngine &Diag;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001604 FormatStyle Style;
1605 Lexer &Lex;
1606 SourceManager &SourceMgr;
Daniel Jasperdcc2a622013-01-18 08:44:07 +00001607 WhitespaceManager Whitespaces;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001608 std::vector<CharSourceRange> Ranges;
Daniel Jasper995e8202013-01-14 13:08:07 +00001609 std::vector<AnnotatedLine> AnnotatedLines;
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001610 bool StructuralError;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001611};
1612
Daniel Jasper1a1ce832013-01-29 11:27:30 +00001613tooling::Replacements
1614reformat(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
1615 std::vector<CharSourceRange> Ranges, DiagnosticConsumer *DiagClient) {
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001616 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001617 OwningPtr<DiagnosticConsumer> DiagPrinter;
1618 if (DiagClient == 0) {
1619 DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts));
1620 DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
1621 DiagClient = DiagPrinter.get();
1622 }
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001623 DiagnosticsEngine Diagnostics(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001624 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001625 DiagClient, false);
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001626 Diagnostics.setSourceManager(&SourceMgr);
1627 Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001628 return formatter.format();
1629}
1630
Daniel Jasper46ef8522013-01-10 13:08:12 +00001631LangOptions getFormattingLangOpts() {
1632 LangOptions LangOpts;
1633 LangOpts.CPlusPlus = 1;
1634 LangOpts.CPlusPlus11 = 1;
1635 LangOpts.Bool = 1;
1636 LangOpts.ObjC1 = 1;
1637 LangOpts.ObjC2 = 1;
1638 return LangOpts;
1639}
1640
Daniel Jaspercd162382013-01-07 13:26:07 +00001641} // namespace format
1642} // namespace clang