blob: 4e2298071c8f6c504a3a4bea22475e42eaafa969 [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 Jasperf9955d32013-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 Jasperce3d1a62013-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 Jasperdcc2a622013-01-18 08:44:07 +0000106/// \brief Manages the whitespaces around tokens and their replacements.
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000107///
Daniel Jasperdcc2a622013-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 Kornienko052685c2013-03-19 17:41:36 +0000112 WhitespaceManager(SourceManager &SourceMgr, const FormatStyle &Style)
113 : SourceMgr(SourceMgr), Style(Style) {}
Daniel Jasperdcc2a622013-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 Kornienko052685c2013-03-19 17:41:36 +0000118 unsigned Spaces, unsigned WhitespaceStartColumn) {
Daniel Jasper821627e2013-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 Jasper812c0452013-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 Jasperf9955d32013-03-20 12:37:50 +0000136 Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
Alexander Kornienkof7536152013-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;
Daniel Jasperc363dbb2013-03-22 16:25:51 +0000144 Comment.Untouchable = false;
Alexander Kornienkof7536152013-03-14 16:10:54 +0000145 Comments.push_back(Comment);
Daniel Jasper812c0452013-03-01 16:45:59 +0000146 return;
147 }
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000148 }
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000149 }
Daniel Jasper821627e2013-01-21 22:49:20 +0000150
151 // If this line does not have a trailing comment, align the stored comments.
Daniel Jasper15417ef2013-02-06 20:07:35 +0000152 if (Tok.Children.empty() && !isTrailingComment(Tok))
Daniel Jasper821627e2013-01-21 22:49:20 +0000153 alignComments();
Alexander Kornienkof7536152013-03-14 16:10:54 +0000154
155 if (Tok.Type == TT_BlockComment)
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000156 indentBlockComment(Tok, Spaces, WhitespaceStartColumn, NewLines, false);
Alexander Kornienkof7536152013-03-14 16:10:54 +0000157
Manuel Klimek8092a942013-02-20 10:15:13 +0000158 storeReplacement(Tok.FormatTok, getNewLineText(NewLines, Spaces));
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000159 }
160
161 /// \brief Like \c replaceWhitespace, but additionally adds right-aligned
162 /// backslashes to escape newlines inside a preprocessor directive.
163 ///
164 /// This function and \c replaceWhitespace have the same behavior if
165 /// \c Newlines == 0.
166 void replacePPWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
Alexander Kornienko052685c2013-03-19 17:41:36 +0000167 unsigned Spaces, unsigned WhitespaceStartColumn) {
168 if (Tok.Type == TT_BlockComment)
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000169 indentBlockComment(Tok, Spaces, WhitespaceStartColumn, NewLines, true);
Alexander Kornienko052685c2013-03-19 17:41:36 +0000170
171 storeReplacement(Tok.FormatTok,
172 getNewLineText(NewLines, Spaces, WhitespaceStartColumn));
Manuel Klimek8092a942013-02-20 10:15:13 +0000173 }
174
175 /// \brief Inserts a line break into the middle of a token.
176 ///
177 /// Will break at \p Offset inside \p Tok, putting \p Prefix before the line
178 /// break and \p Postfix before the rest of the token starts in the next line.
179 ///
180 /// \p InPPDirective, \p Spaces, \p WhitespaceStartColumn and \p Style are
181 /// used to generate the correct line break.
Alexander Kornienko052685c2013-03-19 17:41:36 +0000182 void breakToken(const FormatToken &Tok, unsigned Offset,
183 unsigned ReplaceChars, StringRef Prefix, StringRef Postfix,
184 bool InPPDirective, unsigned Spaces,
185 unsigned WhitespaceStartColumn) {
Manuel Klimek8092a942013-02-20 10:15:13 +0000186 std::string NewLineText;
187 if (!InPPDirective)
188 NewLineText = getNewLineText(1, Spaces);
189 else
Alexander Kornienko052685c2013-03-19 17:41:36 +0000190 NewLineText = getNewLineText(1, Spaces, WhitespaceStartColumn);
Manuel Klimek8092a942013-02-20 10:15:13 +0000191 std::string ReplacementText = (Prefix + NewLineText + Postfix).str();
Alexander Kornienko052685c2013-03-19 17:41:36 +0000192 SourceLocation Location = Tok.Tok.getLocation().getLocWithOffset(Offset);
193 Replaces.insert(tooling::Replacement(SourceMgr, Location, ReplaceChars,
194 ReplacementText));
Manuel Klimek8092a942013-02-20 10:15:13 +0000195 }
196
197 /// \brief Returns all the \c Replacements created during formatting.
198 const tooling::Replacements &generateReplacements() {
199 alignComments();
200 return Replaces;
201 }
202
Daniel Jasperc363dbb2013-03-22 16:25:51 +0000203 void addUntouchableComment(unsigned Column) {
204 StoredComment Comment;
205 Comment.MinColumn = Column;
206 Comment.MaxColumn = Column;
207 Comment.Untouchable = true;
208 Comments.push_back(Comment);
209 }
210
Manuel Klimek8092a942013-02-20 10:15:13 +0000211private:
Alexander Kornienko052685c2013-03-19 17:41:36 +0000212 /// \brief Finds a common prefix of lines of a block comment to properly
213 /// indent (and possibly decorate with '*'s) added lines.
214 ///
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000215 /// The first line is ignored (it's special and starts with /*). The number of
216 /// lines should be more than one.
Alexander Kornienko052685c2013-03-19 17:41:36 +0000217 static StringRef findCommentLinesPrefix(ArrayRef<StringRef> Lines,
218 const char *PrefixChars = " *") {
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000219 assert(Lines.size() > 1);
Alexander Kornienko052685c2013-03-19 17:41:36 +0000220 StringRef Prefix(Lines[1].data(), Lines[1].find_first_not_of(PrefixChars));
221 for (size_t i = 2; i < Lines.size(); ++i) {
222 for (size_t j = 0; j < Prefix.size() && j < Lines[i].size(); ++j) {
223 if (Prefix[j] != Lines[i][j]) {
224 Prefix = Prefix.substr(0, j);
225 break;
226 }
227 }
228 }
229 return Prefix;
230 }
231
232 void splitLineInComment(const FormatToken &Tok, StringRef Line,
233 size_t StartColumn, StringRef LinePrefix,
234 bool InPPDirective, bool CommentHasMoreLines,
235 const char *WhiteSpaceChars = " ") {
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000236 size_t ColumnLimit = Style.ColumnLimit - (InPPDirective ? 2 : 0);
Alexander Kornienko052685c2013-03-19 17:41:36 +0000237 const char *TokenStart = SourceMgr.getCharacterData(Tok.Tok.getLocation());
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000238 while (Line.rtrim().size() + StartColumn > ColumnLimit) {
Alexander Kornienko052685c2013-03-19 17:41:36 +0000239 // Try to break at the last whitespace before the column limit.
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000240 size_t SpacePos =
241 Line.find_last_of(WhiteSpaceChars, ColumnLimit - StartColumn + 1);
Alexander Kornienko052685c2013-03-19 17:41:36 +0000242 if (SpacePos == StringRef::npos) {
243 // Try to find any whitespace in the line.
244 SpacePos = Line.find_first_of(WhiteSpaceChars);
245 if (SpacePos == StringRef::npos) // No whitespace found, give up.
246 break;
247 }
248
249 StringRef NextCut = Line.substr(0, SpacePos).rtrim();
250 StringRef RemainingLine = Line.substr(SpacePos).ltrim();
251 if (RemainingLine.empty())
252 break;
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000253
254 if (RemainingLine == "*/" && LinePrefix.endswith("* "))
255 LinePrefix = LinePrefix.substr(0, LinePrefix.size() - 2);
256
Alexander Kornienko052685c2013-03-19 17:41:36 +0000257 Line = RemainingLine;
258
259 size_t ReplaceChars = Line.begin() - NextCut.end();
260 breakToken(Tok, NextCut.end() - TokenStart, ReplaceChars, "", LinePrefix,
261 InPPDirective, 0,
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000262 NextCut.size() + StartColumn);
263 StartColumn = LinePrefix.size();
Alexander Kornienko052685c2013-03-19 17:41:36 +0000264 }
265
266 StringRef TrimmedLine = Line.rtrim();
267 if (TrimmedLine != Line || (InPPDirective && CommentHasMoreLines)) {
268 // Remove trailing whitespace/insert backslash.
269 breakToken(Tok, TrimmedLine.end() - TokenStart,
270 Line.size() - TrimmedLine.size() + 1, "", "", InPPDirective, 0,
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000271 TrimmedLine.size() + StartColumn);
Alexander Kornienko052685c2013-03-19 17:41:36 +0000272 }
273 }
274
275 void indentBlockComment(const AnnotatedToken &Tok, int Indent,
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000276 int WhitespaceStartColumn, int NewLines,
Alexander Kornienko052685c2013-03-19 17:41:36 +0000277 bool InPPDirective) {
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000278 int StartColumn = NewLines > 0 ? Indent : WhitespaceStartColumn + Indent;
Alexander Kornienko052685c2013-03-19 17:41:36 +0000279 const SourceLocation TokenLoc = Tok.FormatTok.Tok.getLocation();
280 const int CurrentIndent = SourceMgr.getSpellingColumnNumber(TokenLoc) - 1;
281 const int IndentDelta = Indent - CurrentIndent;
282 const StringRef Text(SourceMgr.getCharacterData(TokenLoc),
283 Tok.FormatTok.TokenLength);
284 assert(Text.startswith("/*") && Text.endswith("*/"));
285
286 SmallVector<StringRef, 16> Lines;
287 Text.split(Lines, "\n");
288
289 if (IndentDelta > 0) {
290 std::string WhiteSpace(IndentDelta, ' ');
291 for (size_t i = 1; i < Lines.size(); ++i) {
292 Replaces.insert(tooling::Replacement(
293 SourceMgr, TokenLoc.getLocWithOffset(Lines[i].data() - Text.data()),
294 0, WhiteSpace));
295 }
296 } else if (IndentDelta < 0) {
297 std::string WhiteSpace(-IndentDelta, ' ');
298 // Check that the line is indented enough.
299 for (size_t i = 1; i < Lines.size(); ++i) {
300 if (!Lines[i].startswith(WhiteSpace))
301 return;
302 }
303 for (size_t i = 1; i < Lines.size(); ++i) {
304 Replaces.insert(tooling::Replacement(
305 SourceMgr, TokenLoc.getLocWithOffset(Lines[i].data() - Text.data()),
306 -IndentDelta, ""));
Alexander Kornienkof7536152013-03-14 16:10:54 +0000307 }
308 }
Alexander Kornienko1fdd8b32013-03-15 13:42:02 +0000309
Alexander Kornienko052685c2013-03-19 17:41:36 +0000310 // Split long lines in comments.
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000311 size_t PrefixSize = 0;
312 std::string NewPrefix;
313 if (Lines.size() > 1) {
314 StringRef CurrentPrefix = findCommentLinesPrefix(Lines);
315 PrefixSize = CurrentPrefix.size();
316 NewPrefix = (IndentDelta < 0)
317 ? CurrentPrefix.substr(-IndentDelta).str()
318 : std::string(IndentDelta, ' ') + CurrentPrefix.str();
319 if (CurrentPrefix.endswith("*")) {
320 NewPrefix += " ";
321 ++PrefixSize;
322 }
323 } else if (Tok.Parent == 0) {
324 NewPrefix = std::string(StartColumn, ' ') + " * ";
Alexander Kornienko052685c2013-03-19 17:41:36 +0000325 }
326
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000327 StartColumn += 2;
Alexander Kornienko052685c2013-03-19 17:41:36 +0000328 for (size_t i = 0; i < Lines.size(); ++i) {
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000329 StringRef Line = Lines[i].substr(i == 0 ? 2 : PrefixSize);
Alexander Kornienko052685c2013-03-19 17:41:36 +0000330 splitLineInComment(Tok.FormatTok, Line, StartColumn, NewPrefix,
331 InPPDirective, i != Lines.size() - 1);
Alexander Kornienko7c22cf32013-03-21 12:28:10 +0000332 StartColumn = NewPrefix.size();
Alexander Kornienko1fdd8b32013-03-15 13:42:02 +0000333 }
Alexander Kornienkof7536152013-03-14 16:10:54 +0000334 }
335
Manuel Klimek8092a942013-02-20 10:15:13 +0000336 std::string getNewLineText(unsigned NewLines, unsigned Spaces) {
337 return std::string(NewLines, '\n') + std::string(Spaces, ' ');
338 }
339
Alexander Kornienko052685c2013-03-19 17:41:36 +0000340 std::string getNewLineText(unsigned NewLines, unsigned Spaces,
341 unsigned WhitespaceStartColumn) {
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000342 std::string NewLineText;
343 if (NewLines > 0) {
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000344 unsigned Offset =
345 std::min<int>(Style.ColumnLimit - 1, WhitespaceStartColumn);
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000346 for (unsigned i = 0; i < NewLines; ++i) {
347 NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' ');
348 NewLineText += "\\\n";
349 Offset = 0;
350 }
351 }
Manuel Klimek8092a942013-02-20 10:15:13 +0000352 return NewLineText + std::string(Spaces, ' ');
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000353 }
354
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000355 /// \brief Structure to store a comment for later layout and alignment.
356 struct StoredComment {
357 FormatToken Tok;
358 unsigned MinColumn;
359 unsigned MaxColumn;
360 unsigned NewLines;
361 unsigned Spaces;
Daniel Jasperc363dbb2013-03-22 16:25:51 +0000362 bool Untouchable;
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000363 };
364 SmallVector<StoredComment, 16> Comments;
365 typedef SmallVector<StoredComment, 16>::iterator comment_iterator;
366
367 /// \brief Try to align all stashed comments.
368 void alignComments() {
369 unsigned MinColumn = 0;
370 unsigned MaxColumn = UINT_MAX;
371 comment_iterator Start = Comments.begin();
Alexander Kornienkof7536152013-03-14 16:10:54 +0000372 for (comment_iterator I = Start, E = Comments.end(); I != E; ++I) {
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000373 if (I->MinColumn > MaxColumn || I->MaxColumn < MinColumn) {
374 alignComments(Start, I, MinColumn);
375 MinColumn = I->MinColumn;
376 MaxColumn = I->MaxColumn;
377 Start = I;
378 } else {
379 MinColumn = std::max(MinColumn, I->MinColumn);
380 MaxColumn = std::min(MaxColumn, I->MaxColumn);
381 }
382 }
383 alignComments(Start, Comments.end(), MinColumn);
384 Comments.clear();
385 }
386
387 /// \brief Put all the comments between \p I and \p E into \p Column.
388 void alignComments(comment_iterator I, comment_iterator E, unsigned Column) {
389 while (I != E) {
Daniel Jasperc363dbb2013-03-22 16:25:51 +0000390 if (!I->Untouchable) {
391 unsigned Spaces = I->Spaces + Column - I->MinColumn;
392 storeReplacement(
393 I->Tok, std::string(I->NewLines, '\n') + std::string(Spaces, ' '));
394 }
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000395 ++I;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000396 }
397 }
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000398
399 /// \brief Stores \p Text as the replacement for the whitespace in front of
400 /// \p Tok.
401 void storeReplacement(const FormatToken &Tok, const std::string Text) {
Daniel Jasperafcbd852013-01-30 09:46:12 +0000402 // Don't create a replacement, if it does not change anything.
403 if (StringRef(SourceMgr.getCharacterData(Tok.WhiteSpaceStart),
404 Tok.WhiteSpaceLength) == Text)
405 return;
406
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000407 Replaces.insert(tooling::Replacement(SourceMgr, Tok.WhiteSpaceStart,
408 Tok.WhiteSpaceLength, Text));
409 }
410
411 SourceManager &SourceMgr;
412 tooling::Replacements Replaces;
Alexander Kornienko052685c2013-03-19 17:41:36 +0000413 const FormatStyle &Style;
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000414};
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000415
Daniel Jasperbac016b2012-12-03 18:12:45 +0000416class UnwrappedLineFormatter {
417public:
Manuel Klimek94fc6f12013-01-10 19:17:33 +0000418 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
Daniel Jasper995e8202013-01-14 13:08:07 +0000419 const AnnotatedLine &Line, unsigned FirstIndent,
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +0000420 const AnnotatedToken &RootToken,
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000421 WhitespaceManager &Whitespaces, bool StructuralError)
Daniel Jasper1321eb52012-12-18 21:05:13 +0000422 : Style(Style), SourceMgr(SourceMgr), Line(Line),
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000423 FirstIndent(FirstIndent), RootToken(RootToken),
Daniel Jasperf11a7052013-02-21 21:33:55 +0000424 Whitespaces(Whitespaces), Count(0) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000425
Manuel Klimekd4397b92013-01-04 23:34:14 +0000426 /// \brief Formats an \c UnwrappedLine.
427 ///
428 /// \returns The column after the last token in the last line of the
429 /// \c UnwrappedLine.
Daniel Jaspera4d46212013-02-28 11:05:57 +0000430 unsigned format(const AnnotatedLine *NextLine) {
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000431 // Initialize state dependent on indent.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000432 LineState State;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000433 State.Column = FirstIndent;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000434 State.NextToken = &RootToken;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000435 State.Stack.push_back(
436 ParenState(FirstIndent + 4, FirstIndent, !Style.BinPackParameters,
437 /*HasMultiParameterLine=*/ false));
Daniel Jasper2e603772013-01-29 11:21:01 +0000438 State.VariablePos = 0;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000439 State.LineContainsContinuedForLoopSection = false;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000440 State.ParenLevel = 0;
Manuel Klimekb56b6d12013-02-20 15:25:48 +0000441 State.StartOfStringLiteral = 0;
Daniel Jaspercf5767d2013-02-18 11:05:07 +0000442 State.StartOfLineLevel = State.ParenLevel;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000443
Manuel Klimekca547db2013-01-16 14:55:28 +0000444 DEBUG({
445 DebugTokenState(*State.NextToken);
446 });
447
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000448 // The first token has already been indented and thus consumed.
Manuel Klimek8092a942013-02-20 10:15:13 +0000449 moveStateToNextToken(State, /*DryRun=*/ false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000450
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000451 // If everything fits on a single line, just put it there.
Daniel Jaspera4d46212013-02-28 11:05:57 +0000452 unsigned ColumnLimit = Style.ColumnLimit;
453 if (NextLine && NextLine->InPPDirective &&
454 !NextLine->First.FormatTok.HasUnescapedNewline)
455 ColumnLimit = getColumnLimit();
456 if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) {
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000457 while (State.NextToken != NULL) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000458 addTokenToState(false, false, State);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000459 }
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000460 return State.Column;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000461 }
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000462
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000463 // If the ObjC method declaration does not fit on a line, we should format
464 // it with one arg per line.
465 if (Line.Type == LT_ObjCMethodDecl)
466 State.Stack.back().BreakBeforeParameter = true;
467
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000468 // Find best solution in solution space.
469 return analyzeSolutionSpace(State);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000470 }
471
472private:
Manuel Klimekca547db2013-01-16 14:55:28 +0000473 void DebugTokenState(const AnnotatedToken &AnnotatedTok) {
474 const Token &Tok = AnnotatedTok.FormatTok.Tok;
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000475 llvm::errs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
476 Tok.getLength());
Manuel Klimekca547db2013-01-16 14:55:28 +0000477 llvm::errs();
478 }
479
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000480 struct ParenState {
Daniel Jasperd399bff2013-02-05 09:41:21 +0000481 ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking,
482 bool HasMultiParameterLine)
Daniel Jasper29f123b2013-02-08 15:28:42 +0000483 : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
484 BreakBeforeClosingBrace(false), QuestionColumn(0),
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000485 AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
Daniel Jasper24849712013-03-01 16:48:32 +0000486 HasMultiParameterLine(HasMultiParameterLine), ColonPos(0),
487 StartOfFunctionCall(0) {}
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000488
Daniel Jasperbac016b2012-12-03 18:12:45 +0000489 /// \brief The position to which a specific parenthesis level needs to be
490 /// indented.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000491 unsigned Indent;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000492
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000493 /// \brief The position of the last space on each level.
494 ///
495 /// Used e.g. to break like:
496 /// functionCall(Parameter, otherCall(
497 /// OtherParameter));
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000498 unsigned LastSpace;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000499
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000500 /// \brief The position the first "<<" operator encountered on each level.
501 ///
502 /// Used to align "<<" operators. 0 if no such operator has been encountered
503 /// on a level.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000504 unsigned FirstLessLess;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000505
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000506 /// \brief Whether a newline needs to be inserted before the block's closing
507 /// brace.
508 ///
509 /// We only want to insert a newline before the closing brace if there also
510 /// was a newline after the beginning left brace.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000511 bool BreakBeforeClosingBrace;
512
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000513 /// \brief The column of a \c ? in a conditional expression;
514 unsigned QuestionColumn;
515
Daniel Jasperf343cab2013-01-31 14:59:26 +0000516 /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple
517 /// lines, in this context.
518 bool AvoidBinPacking;
519
520 /// \brief Break after the next comma (or all the commas in this context if
521 /// \c AvoidBinPacking is \c true).
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000522 bool BreakBeforeParameter;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000523
524 /// \brief This context already has a line with more than one parameter.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000525 bool HasMultiParameterLine;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000526
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000527 /// \brief The position of the colon in an ObjC method declaration/call.
528 unsigned ColonPos;
Daniel Jasperc4615b72013-02-20 12:56:39 +0000529
Daniel Jasper24849712013-03-01 16:48:32 +0000530 /// \brief The start of the most recent function in a builder-type call.
531 unsigned StartOfFunctionCall;
532
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000533 bool operator<(const ParenState &Other) const {
534 if (Indent != Other.Indent)
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000535 return Indent < Other.Indent;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000536 if (LastSpace != Other.LastSpace)
537 return LastSpace < Other.LastSpace;
538 if (FirstLessLess != Other.FirstLessLess)
539 return FirstLessLess < Other.FirstLessLess;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000540 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
541 return BreakBeforeClosingBrace;
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000542 if (QuestionColumn != Other.QuestionColumn)
543 return QuestionColumn < Other.QuestionColumn;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000544 if (AvoidBinPacking != Other.AvoidBinPacking)
545 return AvoidBinPacking;
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000546 if (BreakBeforeParameter != Other.BreakBeforeParameter)
547 return BreakBeforeParameter;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000548 if (HasMultiParameterLine != Other.HasMultiParameterLine)
549 return HasMultiParameterLine;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000550 if (ColonPos != Other.ColonPos)
551 return ColonPos < Other.ColonPos;
Daniel Jasper24849712013-03-01 16:48:32 +0000552 if (StartOfFunctionCall != Other.StartOfFunctionCall)
553 return StartOfFunctionCall < Other.StartOfFunctionCall;
Daniel Jasperb3123142013-01-12 07:36:22 +0000554 return false;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000555 }
556 };
557
558 /// \brief The current state when indenting a unwrapped line.
559 ///
560 /// As the indenting tries different combinations this is copied by value.
561 struct LineState {
562 /// \brief The number of used columns in the current line.
563 unsigned Column;
564
565 /// \brief The token that needs to be next formatted.
566 const AnnotatedToken *NextToken;
567
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000568 /// \brief The column of the first variable name in a variable declaration.
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000569 ///
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000570 /// Used to align further variables if necessary.
Daniel Jasper2e603772013-01-29 11:21:01 +0000571 unsigned VariablePos;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000572
573 /// \brief \c true if this line contains a continued for-loop section.
574 bool LineContainsContinuedForLoopSection;
575
Daniel Jasper29f123b2013-02-08 15:28:42 +0000576 /// \brief The level of nesting inside (), [], <> and {}.
577 unsigned ParenLevel;
578
Daniel Jaspercf5767d2013-02-18 11:05:07 +0000579 /// \brief The \c ParenLevel at the start of this line.
580 unsigned StartOfLineLevel;
581
Manuel Klimekb56b6d12013-02-20 15:25:48 +0000582 /// \brief The start column of the string literal, if we're in a string
583 /// literal sequence, 0 otherwise.
584 unsigned StartOfStringLiteral;
585
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000586 /// \brief A stack keeping track of properties applying to parenthesis
587 /// levels.
588 std::vector<ParenState> Stack;
589
590 /// \brief Comparison operator to be able to used \c LineState in \c map.
591 bool operator<(const LineState &Other) const {
Daniel Jasperd7896702013-02-19 09:28:55 +0000592 if (NextToken != Other.NextToken)
593 return NextToken < Other.NextToken;
594 if (Column != Other.Column)
595 return Column < Other.Column;
596 if (VariablePos != Other.VariablePos)
597 return VariablePos < Other.VariablePos;
598 if (LineContainsContinuedForLoopSection !=
Daniel Jasperf9955d32013-03-20 12:37:50 +0000599 Other.LineContainsContinuedForLoopSection)
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000600 return LineContainsContinuedForLoopSection;
Daniel Jasperd7896702013-02-19 09:28:55 +0000601 if (ParenLevel != Other.ParenLevel)
602 return ParenLevel < Other.ParenLevel;
603 if (StartOfLineLevel != Other.StartOfLineLevel)
604 return StartOfLineLevel < Other.StartOfLineLevel;
Manuel Klimekb56b6d12013-02-20 15:25:48 +0000605 if (StartOfStringLiteral != Other.StartOfStringLiteral)
606 return StartOfStringLiteral < Other.StartOfStringLiteral;
Daniel Jasperd7896702013-02-19 09:28:55 +0000607 return Stack < Other.Stack;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000608 }
609 };
610
Daniel Jasper20409152012-12-04 14:54:30 +0000611 /// \brief Appends the next token to \p State and updates information
612 /// necessary for indentation.
613 ///
614 /// Puts the token on the current line if \p Newline is \c true and adds a
615 /// line break and necessary indentation otherwise.
616 ///
617 /// If \p DryRun is \c false, also creates and stores the required
618 /// \c Replacement.
Manuel Klimek8092a942013-02-20 10:15:13 +0000619 unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) {
Daniel Jasper9c837d02013-01-09 07:06:56 +0000620 const AnnotatedToken &Current = *State.NextToken;
621 const AnnotatedToken &Previous = *State.NextToken->Parent;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000622
Daniel Jasper92f9faf2013-03-20 15:58:10 +0000623 if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000624 State.Column += State.NextToken->FormatTok.WhiteSpaceLength +
625 State.NextToken->FormatTok.TokenLength;
626 if (State.NextToken->Children.empty())
627 State.NextToken = NULL;
628 else
629 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek8092a942013-02-20 10:15:13 +0000630 return 0;
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000631 }
632
Daniel Jasperbac016b2012-12-03 18:12:45 +0000633 if (Newline) {
Manuel Klimek060143e2013-01-02 18:33:23 +0000634 unsigned WhitespaceStartColumn = State.Column;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000635 if (Current.is(tok::r_brace)) {
636 State.Column = Line.Level * 2;
Daniel Jasper9c837d02013-01-09 07:06:56 +0000637 } else if (Current.is(tok::string_literal) &&
Manuel Klimekb56b6d12013-02-20 15:25:48 +0000638 State.StartOfStringLiteral != 0) {
639 State.Column = State.StartOfStringLiteral;
Daniel Jasper66d19bd2013-02-18 11:59:17 +0000640 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasper9c837d02013-01-09 07:06:56 +0000641 } else if (Current.is(tok::lessless) &&
Daniel Jasper29f123b2013-02-08 15:28:42 +0000642 State.Stack.back().FirstLessLess != 0) {
643 State.Column = State.Stack.back().FirstLessLess;
644 } else if (State.ParenLevel != 0 &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000645 (Previous.isOneOf(tok::equal, tok::coloncolon) ||
Daniel Jasperf9955d32013-03-20 12:37:50 +0000646 Current.isOneOf(tok::period, tok::arrow, tok::question) ||
647 isComparison(Previous))) {
Daniel Jasper9c837d02013-01-09 07:06:56 +0000648 // Indent and extra 4 spaces after if we know the current expression is
649 // continued. Don't do that on the top level, as we already indent 4
650 // there.
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000651 State.Column = std::max(State.Stack.back().LastSpace,
652 State.Stack.back().Indent) + 4;
653 } else if (Current.Type == TT_ConditionalExpr) {
654 State.Column = State.Stack.back().QuestionColumn;
Daniel Jasper2e603772013-01-29 11:21:01 +0000655 } else if (Previous.is(tok::comma) && State.VariablePos != 0 &&
Daniel Jasper29f123b2013-02-08 15:28:42 +0000656 ((RootToken.is(tok::kw_for) && State.ParenLevel == 1) ||
657 State.ParenLevel == 0)) {
Daniel Jasper2e603772013-01-29 11:21:01 +0000658 State.Column = State.VariablePos;
Daniel Jasper3c08a812013-02-24 18:54:32 +0000659 } else if (Previous.ClosesTemplateDeclaration ||
660 (Current.Type == TT_StartOfName && State.ParenLevel == 0)) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000661 State.Column = State.Stack.back().Indent - 4;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000662 } else if (Current.Type == TT_ObjCSelectorName) {
663 if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) {
664 State.Column =
665 State.Stack.back().ColonPos - Current.FormatTok.TokenLength;
666 } else {
667 State.Column = State.Stack.back().Indent;
668 State.Stack.back().ColonPos =
669 State.Column + Current.FormatTok.TokenLength;
670 }
Daniel Jasper3c08a812013-02-24 18:54:32 +0000671 } else if (Previous.Type == TT_ObjCMethodExpr ||
672 Current.Type == TT_StartOfName) {
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000673 State.Column = State.Stack.back().Indent + 4;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000674 } else {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000675 State.Column = State.Stack.back().Indent;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000676 }
677
Daniel Jasper7878a7b2013-02-15 11:07:25 +0000678 if (Current.is(tok::question))
Daniel Jasper237d4c12013-02-23 21:01:55 +0000679 State.Stack.back().BreakBeforeParameter = true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000680 if (Previous.isOneOf(tok::comma, tok::semi) &&
Daniel Jasper237d4c12013-02-23 21:01:55 +0000681 !State.Stack.back().AvoidBinPacking)
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000682 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000683
Manuel Klimek060143e2013-01-02 18:33:23 +0000684 if (!DryRun) {
Daniel Jasper1ef81d52013-02-26 13:10:34 +0000685 unsigned NewLines = 1;
686 if (Current.Type == TT_LineComment)
687 NewLines =
688 std::max(NewLines, std::min(Current.FormatTok.NewlinesBefore,
689 Style.MaxEmptyLinesToKeep + 1));
Manuel Klimek060143e2013-01-02 18:33:23 +0000690 if (!Line.InPPDirective)
Daniel Jasperc4615b72013-02-20 12:56:39 +0000691 Whitespaces.replaceWhitespace(Current, NewLines, State.Column,
Alexander Kornienko052685c2013-03-19 17:41:36 +0000692 WhitespaceStartColumn);
Manuel Klimek060143e2013-01-02 18:33:23 +0000693 else
Daniel Jasperc4615b72013-02-20 12:56:39 +0000694 Whitespaces.replacePPWhitespace(Current, NewLines, State.Column,
Alexander Kornienko052685c2013-03-19 17:41:36 +0000695 WhitespaceStartColumn);
Manuel Klimek060143e2013-01-02 18:33:23 +0000696 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000697
Daniel Jasper29f123b2013-02-08 15:28:42 +0000698 State.Stack.back().LastSpace = State.Column;
Daniel Jaspercf5767d2013-02-18 11:05:07 +0000699 State.StartOfLineLevel = State.ParenLevel;
Daniel Jasper237d4c12013-02-23 21:01:55 +0000700
701 // Any break on this level means that the parent level has been broken
702 // and we need to avoid bin packing there.
703 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
704 State.Stack[i].BreakBeforeParameter = true;
705 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000706 if (Current.isOneOf(tok::period, tok::arrow))
Daniel Jasperfaab0d32013-02-27 09:47:53 +0000707 State.Stack.back().BreakBeforeParameter = true;
708
Daniel Jasper237d4c12013-02-23 21:01:55 +0000709 // If we break after {, we should also break before the corresponding }.
710 if (Previous.is(tok::l_brace))
711 State.Stack.back().BreakBeforeClosingBrace = true;
712
713 if (State.Stack.back().AvoidBinPacking) {
714 // If we are breaking after '(', '{', '<', this is not bin packing
715 // unless AllowAllParametersOfDeclarationOnNextLine is false.
Daniel Jasper3c08a812013-02-24 18:54:32 +0000716 if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace)) ||
Daniel Jasper237d4c12013-02-23 21:01:55 +0000717 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
718 Line.MustBeDeclaration))
719 State.Stack.back().BreakBeforeParameter = true;
720 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000721 } else {
Daniel Jasper9c3e71a2013-02-25 15:59:54 +0000722 // FIXME: Put VariablePos into ParenState and remove second part of if().
723 if (Current.is(tok::equal) &&
724 (RootToken.is(tok::kw_for) || State.ParenLevel == 0))
Daniel Jasper2e603772013-01-29 11:21:01 +0000725 State.VariablePos = State.Column - Previous.FormatTok.TokenLength;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000726
Daniel Jasper729a7432013-02-11 12:36:37 +0000727 unsigned Spaces = State.NextToken->SpacesRequiredBefore;
Daniel Jasper20409152012-12-04 14:54:30 +0000728
Daniel Jasperbac016b2012-12-03 18:12:45 +0000729 if (!DryRun)
Alexander Kornienko052685c2013-03-19 17:41:36 +0000730 Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column);
Daniel Jasper20409152012-12-04 14:54:30 +0000731
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000732 if (Current.Type == TT_ObjCSelectorName &&
733 State.Stack.back().ColonPos == 0) {
734 if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
Daniel Jasperf9955d32013-03-20 12:37:50 +0000735 State.Column + Spaces + Current.FormatTok.TokenLength)
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000736 State.Stack.back().ColonPos =
737 State.Stack.back().Indent + Current.LongestObjCSelectorName;
738 else
739 State.Stack.back().ColonPos =
Daniel Jasper9e9e6e02013-02-06 16:00:26 +0000740 State.Column + Spaces + Current.FormatTok.TokenLength;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000741 }
742
Daniel Jasperd4f2c2e2013-01-29 19:41:55 +0000743 if (Current.Type != TT_LineComment &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000744 (Previous.isOneOf(tok::l_paren, tok::l_brace) ||
Daniel Jasperd4f2c2e2013-01-29 19:41:55 +0000745 State.NextToken->Parent->Type == TT_TemplateOpener))
Daniel Jasper29f123b2013-02-08 15:28:42 +0000746 State.Stack.back().Indent = State.Column + Spaces;
Daniel Jaspercda16502013-02-04 08:34:57 +0000747 if (Previous.is(tok::comma) && !isTrailingComment(Current))
Daniel Jasper29f123b2013-02-08 15:28:42 +0000748 State.Stack.back().HasMultiParameterLine = true;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000749
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000750 State.Column += Spaces;
Daniel Jaspere438bac2013-01-23 20:41:06 +0000751 if (Current.is(tok::l_paren) && Previous.is(tok::kw_if))
752 // Treat the condition inside an if as if it was a second function
753 // parameter, i.e. let nested calls have an indent of 4.
754 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
Daniel Jasperf9955d32013-03-20 12:37:50 +0000755 else if (Previous.is(tok::comma))
Daniel Jaspere438bac2013-01-23 20:41:06 +0000756 // Top-level spaces are exempt as that mostly leads to better results.
757 State.Stack.back().LastSpace = State.Column;
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000758 else if ((Previous.Type == TT_BinaryOperator ||
Daniel Jasper02b771e2013-01-28 13:31:35 +0000759 Previous.Type == TT_ConditionalExpr ||
760 Previous.Type == TT_CtorInitializerColon) &&
Daniel Jasperae8699b2013-01-28 09:35:24 +0000761 getPrecedence(Previous) != prec::Assignment)
762 State.Stack.back().LastSpace = State.Column;
Daniel Jasper6cabab42013-02-14 08:42:54 +0000763 else if (Previous.Type == TT_InheritanceColon)
764 State.Stack.back().Indent = State.Column;
Daniel Jasper986e17f2013-01-28 07:35:34 +0000765 else if (Previous.ParameterCount > 1 &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000766 (Previous.isOneOf(tok::l_paren, tok::l_square, tok::l_brace) ||
Daniel Jasper986e17f2013-01-28 07:35:34 +0000767 Previous.Type == TT_TemplateOpener))
768 // If this function has multiple parameters, indent nested calls from
769 // the start of the first parameter.
770 State.Stack.back().LastSpace = State.Column;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000771 }
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000772
Manuel Klimek8092a942013-02-20 10:15:13 +0000773 return moveStateToNextToken(State, DryRun);
Daniel Jasper20409152012-12-04 14:54:30 +0000774 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000775
Daniel Jasper20409152012-12-04 14:54:30 +0000776 /// \brief Mark the next token as consumed in \p State and modify its stacks
777 /// accordingly.
Manuel Klimek8092a942013-02-20 10:15:13 +0000778 unsigned moveStateToNextToken(LineState &State, bool DryRun) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000779 const AnnotatedToken &Current = *State.NextToken;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000780 assert(State.Stack.size());
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000781
Daniel Jasper6cabab42013-02-14 08:42:54 +0000782 if (Current.Type == TT_InheritanceColon)
783 State.Stack.back().AvoidBinPacking = true;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000784 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
785 State.Stack.back().FirstLessLess = State.Column;
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000786 if (Current.is(tok::question))
787 State.Stack.back().QuestionColumn = State.Column;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000788 if (Current.isOneOf(tok::period, tok::arrow) &&
Daniel Jasper24849712013-03-01 16:48:32 +0000789 Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
790 State.Stack.back().StartOfFunctionCall =
791 Current.LastInChainOfCalls ? 0 : State.Column;
Daniel Jasper7d812812013-02-21 15:00:29 +0000792 if (Current.Type == TT_CtorInitializerColon) {
793 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
794 State.Stack.back().AvoidBinPacking = true;
795 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000796 }
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000797
Daniel Jasper29f123b2013-02-08 15:28:42 +0000798 // Insert scopes created by fake parenthesis.
799 for (unsigned i = 0, e = Current.FakeLParens; i != e; ++i) {
800 ParenState NewParenState = State.Stack.back();
801 NewParenState.Indent = std::max(State.Column, State.Stack.back().Indent);
Daniel Jasper237d4c12013-02-23 21:01:55 +0000802 NewParenState.BreakBeforeParameter = false;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000803 State.Stack.push_back(NewParenState);
804 }
805
Daniel Jaspercf225b62012-12-24 13:43:52 +0000806 // If we encounter an opening (, [, { or <, we add a level to our stacks to
Daniel Jasper20409152012-12-04 14:54:30 +0000807 // prepare for the following tokens.
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000808 if (Current.isOneOf(tok::l_paren, tok::l_square, tok::l_brace) ||
Daniel Jasper26f7e782013-01-08 14:56:18 +0000809 State.NextToken->Type == TT_TemplateOpener) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000810 unsigned NewIndent;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000811 bool AvoidBinPacking;
Manuel Klimek2851c162013-01-10 14:36:46 +0000812 if (Current.is(tok::l_brace)) {
Daniel Jasperf343cab2013-01-31 14:59:26 +0000813 NewIndent = 2 + State.Stack.back().LastSpace;
814 AvoidBinPacking = false;
Manuel Klimek2851c162013-01-10 14:36:46 +0000815 } else {
Daniel Jasper24849712013-03-01 16:48:32 +0000816 NewIndent = 4 + std::max(State.Stack.back().LastSpace,
817 State.Stack.back().StartOfFunctionCall);
Daniel Jasper3a39ac72013-02-28 09:39:12 +0000818 AvoidBinPacking =
819 !Style.BinPackParameters || State.Stack.back().AvoidBinPacking;
Manuel Klimek2851c162013-01-10 14:36:46 +0000820 }
Daniel Jasperd399bff2013-02-05 09:41:21 +0000821 State.Stack.push_back(
822 ParenState(NewIndent, State.Stack.back().LastSpace, AvoidBinPacking,
823 State.Stack.back().HasMultiParameterLine));
Daniel Jasper29f123b2013-02-08 15:28:42 +0000824 ++State.ParenLevel;
Daniel Jasper20409152012-12-04 14:54:30 +0000825 }
826
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000827 // If this '[' opens an ObjC call, determine whether all parameters fit into
828 // one line and put one per line if they don't.
829 if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
830 Current.MatchingParen != NULL) {
831 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
832 State.Stack.back().BreakBeforeParameter = true;
833 }
834
Daniel Jaspercf225b62012-12-24 13:43:52 +0000835 // If we encounter a closing ), ], } or >, we can remove a level from our
Daniel Jasper20409152012-12-04 14:54:30 +0000836 // stacks.
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000837 if (Current.isOneOf(tok::r_paren, tok::r_square) ||
Daniel Jasper26f7e782013-01-08 14:56:18 +0000838 (Current.is(tok::r_brace) && State.NextToken != &RootToken) ||
839 State.NextToken->Type == TT_TemplateCloser) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000840 State.Stack.pop_back();
Daniel Jasper29f123b2013-02-08 15:28:42 +0000841 --State.ParenLevel;
842 }
843
844 // Remove scopes created by fake parenthesis.
845 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
846 State.Stack.pop_back();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000847 }
Manuel Klimek2851c162013-01-10 14:36:46 +0000848
Manuel Klimeke9a62262013-02-20 15:32:58 +0000849 if (Current.is(tok::string_literal)) {
Manuel Klimekb56b6d12013-02-20 15:25:48 +0000850 State.StartOfStringLiteral = State.Column;
851 } else if (Current.isNot(tok::comment)) {
852 State.StartOfStringLiteral = 0;
853 }
854
Manuel Klimek8092a942013-02-20 10:15:13 +0000855 State.Column += Current.FormatTok.TokenLength;
856
Daniel Jasper26f7e782013-01-08 14:56:18 +0000857 if (State.NextToken->Children.empty())
858 State.NextToken = NULL;
859 else
860 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek2851c162013-01-10 14:36:46 +0000861
Manuel Klimek8092a942013-02-20 10:15:13 +0000862 return breakProtrudingToken(Current, State, DryRun);
863 }
864
865 /// \brief If the current token sticks out over the end of the line, break
866 /// it if possible.
867 unsigned breakProtrudingToken(const AnnotatedToken &Current, LineState &State,
868 bool DryRun) {
869 if (Current.isNot(tok::string_literal))
870 return 0;
Manuel Klimekaa62d0c2013-03-08 18:59:48 +0000871 // Only break up default narrow strings.
Alexander Kornienko052685c2013-03-19 17:41:36 +0000872 const char *LiteralData = Current.FormatTok.Tok.getLiteralData();
873 if (!LiteralData || *LiteralData != '"')
Manuel Klimekaa62d0c2013-03-08 18:59:48 +0000874 return 0;
Manuel Klimek8092a942013-02-20 10:15:13 +0000875
876 unsigned Penalty = 0;
877 unsigned TailOffset = 0;
878 unsigned TailLength = Current.FormatTok.TokenLength;
879 unsigned StartColumn = State.Column - Current.FormatTok.TokenLength;
880 unsigned OffsetFromStart = 0;
881 while (StartColumn + TailLength > getColumnLimit()) {
Alexander Kornienko052685c2013-03-19 17:41:36 +0000882 StringRef Text = StringRef(LiteralData + TailOffset, TailLength);
Manuel Klimekbc30c712013-03-01 13:29:19 +0000883 if (StartColumn + OffsetFromStart + 1 > getColumnLimit())
Manuel Klimekaf31fd72013-03-01 13:14:08 +0000884 break;
Manuel Klimekbc30c712013-03-01 13:29:19 +0000885 StringRef::size_type SplitPoint = getSplitPoint(
886 Text, getColumnLimit() - StartColumn - OffsetFromStart - 1);
Manuel Klimek8092a942013-02-20 10:15:13 +0000887 if (SplitPoint == StringRef::npos)
888 break;
889 assert(SplitPoint != 0);
890 // +2, because 'Text' starts after the opening quotes, and does not
891 // include the closing quote we need to insert.
892 unsigned WhitespaceStartColumn =
893 StartColumn + OffsetFromStart + SplitPoint + 2;
894 State.Stack.back().LastSpace = StartColumn;
895 if (!DryRun) {
Alexander Kornienko052685c2013-03-19 17:41:36 +0000896 Whitespaces.breakToken(Current.FormatTok, TailOffset + SplitPoint + 1,
897 0, "\"", "\"", Line.InPPDirective, StartColumn,
898 WhitespaceStartColumn);
Manuel Klimek8092a942013-02-20 10:15:13 +0000899 }
900 TailOffset += SplitPoint + 1;
901 TailLength -= SplitPoint + 1;
902 OffsetFromStart = 1;
Daniel Jasper0fb382b2013-02-26 12:52:34 +0000903 Penalty += Style.PenaltyExcessCharacter;
Daniel Jasperfaab0d32013-02-27 09:47:53 +0000904 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
905 State.Stack[i].BreakBeforeParameter = true;
Manuel Klimek8092a942013-02-20 10:15:13 +0000906 }
907 State.Column = StartColumn + TailLength;
908 return Penalty;
909 }
910
911 StringRef::size_type
912 getSplitPoint(StringRef Text, StringRef::size_type Offset) {
Manuel Klimekaf31fd72013-03-01 13:14:08 +0000913 StringRef::size_type SpaceOffset = Text.rfind(' ', Offset);
Manuel Klimek00905912013-03-04 20:03:38 +0000914 if (SpaceOffset != StringRef::npos && SpaceOffset != 0)
Manuel Klimekbc30c712013-03-01 13:29:19 +0000915 return SpaceOffset;
916 StringRef::size_type SlashOffset = Text.rfind('/', Offset);
Manuel Klimek00905912013-03-04 20:03:38 +0000917 if (SlashOffset != StringRef::npos && SlashOffset != 0)
Manuel Klimekbc30c712013-03-01 13:29:19 +0000918 return SlashOffset;
Manuel Klimekaa62d0c2013-03-08 18:59:48 +0000919 StringRef::size_type Split = getStartOfCharacter(Text, Offset);
920 if (Split != StringRef::npos && Split > 1)
Manuel Klimekbc30c712013-03-01 13:29:19 +0000921 // Do not split at 0.
Manuel Klimekaa62d0c2013-03-08 18:59:48 +0000922 return Split - 1;
Manuel Klimekbc30c712013-03-01 13:29:19 +0000923 return StringRef::npos;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000924 }
925
Manuel Klimekaa62d0c2013-03-08 18:59:48 +0000926 StringRef::size_type
927 getStartOfCharacter(StringRef Text, StringRef::size_type Offset) {
928 StringRef::size_type NextEscape = Text.find('\\');
929 while (NextEscape != StringRef::npos && NextEscape < Offset) {
930 StringRef::size_type SequenceLength =
931 getEscapeSequenceLength(Text.substr(NextEscape));
932 if (Offset < NextEscape + SequenceLength)
933 return NextEscape;
934 NextEscape = Text.find('\\', NextEscape + SequenceLength);
935 }
936 return Offset;
937 }
938
939 unsigned getEscapeSequenceLength(StringRef Text) {
940 assert(Text[0] == '\\');
941 if (Text.size() < 2)
942 return 1;
943
944 switch (Text[1]) {
945 case 'u':
946 return 6;
947 case 'U':
948 return 10;
949 case 'x':
950 return getHexLength(Text);
951 default:
952 if (Text[1] >= '0' && Text[1] <= '7')
953 return getOctalLength(Text);
954 return 2;
955 }
956 }
957
958 unsigned getHexLength(StringRef Text) {
959 unsigned I = 2; // Point after '\x'.
960 while (I < Text.size() && ((Text[I] >= '0' && Text[I] <= '9') ||
961 (Text[I] >= 'a' && Text[I] <= 'f') ||
962 (Text[I] >= 'A' && Text[I] <= 'F'))) {
963 ++I;
964 }
965 return I;
966 }
967
968 unsigned getOctalLength(StringRef Text) {
969 unsigned I = 1;
970 while (I < Text.size() && I < 4 && (Text[I] >= '0' && Text[I] <= '7')) {
971 ++I;
972 }
973 return I;
974 }
975
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000976 unsigned getColumnLimit() {
Daniel Jaspera4d46212013-02-28 11:05:57 +0000977 return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0);
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000978 }
979
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000980 /// \brief An edge in the solution space from \c Previous->State to \c State,
981 /// inserting a newline dependent on the \c NewLine.
982 struct StateNode {
983 StateNode(const LineState &State, bool NewLine, StateNode *Previous)
Daniel Jasperf11a7052013-02-21 21:33:55 +0000984 : State(State), NewLine(NewLine), Previous(Previous) {}
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000985 LineState State;
986 bool NewLine;
987 StateNode *Previous;
988 };
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000989
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000990 /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
991 ///
992 /// In case of equal penalties, we want to prefer states that were inserted
993 /// first. During state generation we make sure that we insert states first
994 /// that break the line as late as possible.
995 typedef std::pair<unsigned, unsigned> OrderedPenalty;
996
997 /// \brief An item in the prioritized BFS search queue. The \c StateNode's
998 /// \c State has the given \c OrderedPenalty.
999 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
1000
1001 /// \brief The BFS queue type.
1002 typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
1003 std::greater<QueueItem> > QueueType;
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001004
1005 /// \brief Analyze the entire solution space starting from \p InitialState.
Daniel Jasperbac016b2012-12-03 18:12:45 +00001006 ///
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001007 /// This implements a variant of Dijkstra's algorithm on the graph that spans
1008 /// the solution space (\c LineStates are the nodes). The algorithm tries to
1009 /// find the shortest path (the one with lowest penalty) from \p InitialState
1010 /// to a state where all tokens are placed.
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001011 unsigned analyzeSolutionSpace(LineState &InitialState) {
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001012 std::set<LineState> Seen;
1013
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001014 // Insert start element into queue.
Daniel Jasperfc759082013-02-14 14:26:07 +00001015 StateNode *Node =
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001016 new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
1017 Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
1018 ++Count;
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001019
1020 // While not empty, take first element and follow edges.
1021 while (!Queue.empty()) {
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001022 unsigned Penalty = Queue.top().first.first;
Daniel Jasperfc759082013-02-14 14:26:07 +00001023 StateNode *Node = Queue.top().second;
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001024 if (Node->State.NextToken == NULL) {
Daniel Jasper01786732013-02-04 07:21:18 +00001025 DEBUG(llvm::errs() << "\n---\nPenalty for line: " << Penalty << "\n");
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001026 break;
Daniel Jasper01786732013-02-04 07:21:18 +00001027 }
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001028 Queue.pop();
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001029
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001030 if (!Seen.insert(Node->State).second)
1031 // State already examined with lower penalty.
1032 continue;
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001033
Manuel Klimek62a48fb2013-02-13 10:54:19 +00001034 addNextStateToQueue(Penalty, Node, /*NewLine=*/ false);
1035 addNextStateToQueue(Penalty, Node, /*NewLine=*/ true);
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001036 }
1037
1038 if (Queue.empty())
1039 // We were unable to find a solution, do nothing.
1040 // FIXME: Add diagnostic?
Daniel Jasperbac016b2012-12-03 18:12:45 +00001041 return 0;
1042
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001043 // Reconstruct the solution.
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001044 reconstructPath(InitialState, Queue.top().second);
Daniel Jasper01786732013-02-04 07:21:18 +00001045 DEBUG(llvm::errs() << "---\n");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001046
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001047 // Return the column after the last token of the solution.
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001048 return Queue.top().second->State.Column;
1049 }
1050
1051 void reconstructPath(LineState &State, StateNode *Current) {
1052 // FIXME: This recursive implementation limits the possible number
1053 // of tokens per line if compiled into a binary with small stack space.
1054 // To become more independent of stack frame limitations we would need
1055 // to also change the TokenAnnotator.
1056 if (Current->Previous == NULL)
1057 return;
1058 reconstructPath(State, Current->Previous);
1059 DEBUG({
1060 if (Current->NewLine) {
Daniel Jaspera03ab102013-02-13 20:33:44 +00001061 llvm::errs()
1062 << "Penalty for splitting before "
1063 << Current->Previous->State.NextToken->FormatTok.Tok.getName()
1064 << ": " << Current->Previous->State.NextToken->SplitPenalty << "\n";
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001065 }
1066 });
1067 addTokenToState(Current->NewLine, false, State);
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001068 }
1069
Manuel Klimek62a48fb2013-02-13 10:54:19 +00001070 /// \brief Add the following state to the analysis queue \c Queue.
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001071 ///
Manuel Klimek62a48fb2013-02-13 10:54:19 +00001072 /// Assume the current state is \p PreviousNode and has been reached with a
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001073 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
Manuel Klimek62a48fb2013-02-13 10:54:19 +00001074 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
1075 bool NewLine) {
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001076 if (NewLine && !canBreak(PreviousNode->State))
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001077 return;
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001078 if (!NewLine && mustBreak(PreviousNode->State))
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001079 return;
Daniel Jasperae8699b2013-01-28 09:35:24 +00001080 if (NewLine)
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001081 Penalty += PreviousNode->State.NextToken->SplitPenalty;
1082
1083 StateNode *Node = new (Allocator.Allocate())
1084 StateNode(PreviousNode->State, NewLine, PreviousNode);
Manuel Klimek8092a942013-02-20 10:15:13 +00001085 Penalty += addTokenToState(NewLine, true, Node->State);
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001086 if (Node->State.Column > getColumnLimit()) {
1087 unsigned ExcessCharacters = Node->State.Column - getColumnLimit();
Daniel Jasper01786732013-02-04 07:21:18 +00001088 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
Daniel Jasperceb99ab2013-01-09 10:16:05 +00001089 }
Manuel Klimek32a2fd72013-02-13 10:46:36 +00001090
1091 Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
1092 ++Count;
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001093 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001094
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001095 /// \brief Returns \c true, if a line break after \p State is allowed.
1096 bool canBreak(const LineState &State) {
1097 if (!State.NextToken->CanBreakBefore &&
1098 !(State.NextToken->is(tok::r_brace) &&
1099 State.Stack.back().BreakBeforeClosingBrace))
1100 return false;
1101 // Trying to insert a parameter on a new line if there are already more than
1102 // one parameter on the current line is bin packing.
Daniel Jasperd399bff2013-02-05 09:41:21 +00001103 if (State.Stack.back().HasMultiParameterLine &&
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001104 State.Stack.back().AvoidBinPacking)
1105 return false;
1106 return true;
1107 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001108
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001109 /// \brief Returns \c true, if a line break after \p State is mandatory.
1110 bool mustBreak(const LineState &State) {
1111 if (State.NextToken->MustBreakBefore)
1112 return true;
1113 if (State.NextToken->is(tok::r_brace) &&
1114 State.Stack.back().BreakBeforeClosingBrace)
1115 return true;
1116 if (State.NextToken->Parent->is(tok::semi) &&
1117 State.LineContainsContinuedForLoopSection)
1118 return true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001119 if ((State.NextToken->Parent->isOneOf(tok::comma, tok::semi) ||
Daniel Jasper237d4c12013-02-23 21:01:55 +00001120 State.NextToken->is(tok::question) ||
1121 State.NextToken->Type == TT_ConditionalExpr) &&
Daniel Jasperce3d1a62013-02-08 08:22:00 +00001122 State.Stack.back().BreakBeforeParameter &&
Daniel Jasperc5cfa492013-02-14 09:19:04 +00001123 !isTrailingComment(*State.NextToken) &&
Daniel Jasper7d812812013-02-21 15:00:29 +00001124 State.NextToken->isNot(tok::r_paren) &&
1125 State.NextToken->isNot(tok::r_brace))
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001126 return true;
Daniel Jasperce3d1a62013-02-08 08:22:00 +00001127 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
1128 // out whether it is the first parameter. Clean this up.
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001129 if (State.NextToken->Type == TT_ObjCSelectorName &&
Daniel Jasperce3d1a62013-02-08 08:22:00 +00001130 State.NextToken->LongestObjCSelectorName == 0 &&
1131 State.Stack.back().BreakBeforeParameter)
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001132 return true;
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001133 if ((State.NextToken->Type == TT_CtorInitializerColon ||
1134 (State.NextToken->Parent->ClosesTemplateDeclaration &&
Daniel Jasper29f123b2013-02-08 15:28:42 +00001135 State.ParenLevel == 0)))
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001136 return true;
Daniel Jasper923ebef2013-03-14 13:45:21 +00001137 if (State.NextToken->Type == TT_InlineASMColon)
1138 return true;
Daniel Jasper3af59ce2013-03-15 14:57:30 +00001139 // This prevents breaks like:
1140 // ...
1141 // SomeParameter, OtherParameter).DoSomething(
1142 // ...
1143 // As they hide "DoSomething" and generally bad for readability.
1144 if (State.NextToken->isOneOf(tok::period, tok::arrow) &&
1145 getRemainingLength(State) + State.Column > getColumnLimit() &&
1146 State.ParenLevel < State.StartOfLineLevel)
1147 return true;
Daniel Jasper68ef0df2013-02-01 11:00:45 +00001148 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001149 }
1150
Daniel Jasper3af59ce2013-03-15 14:57:30 +00001151 // Returns the total number of columns required for the remaining tokens.
1152 unsigned getRemainingLength(const LineState &State) {
1153 if (State.NextToken && State.NextToken->Parent)
1154 return Line.Last->TotalLength - State.NextToken->Parent->TotalLength;
1155 return 0;
1156 }
1157
Daniel Jasperbac016b2012-12-03 18:12:45 +00001158 FormatStyle Style;
1159 SourceManager &SourceMgr;
Daniel Jasper995e8202013-01-14 13:08:07 +00001160 const AnnotatedLine &Line;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001161 const unsigned FirstIndent;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001162 const AnnotatedToken &RootToken;
Daniel Jasperdcc2a622013-01-18 08:44:07 +00001163 WhitespaceManager &Whitespaces;
Manuel Klimek62a48fb2013-02-13 10:54:19 +00001164
1165 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1166 QueueType Queue;
1167 // Increasing count of \c StateNode items we have created. This is used
1168 // to create a deterministic order independent of the container.
1169 unsigned Count;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001170};
1171
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001172class LexerBasedFormatTokenSource : public FormatTokenSource {
1173public:
1174 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
Daniel Jasper1321eb52012-12-18 21:05:13 +00001175 : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001176 IdentTable(Lex.getLangOpts()) {
1177 Lex.SetKeepWhitespaceMode(true);
1178 }
1179
1180 virtual FormatToken getNextToken() {
1181 if (GreaterStashed) {
1182 FormatTok.NewlinesBefore = 0;
1183 FormatTok.WhiteSpaceStart =
1184 FormatTok.Tok.getLocation().getLocWithOffset(1);
1185 FormatTok.WhiteSpaceLength = 0;
1186 GreaterStashed = false;
1187 return FormatTok;
1188 }
1189
1190 FormatTok = FormatToken();
1191 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimek95419382013-01-07 07:56:50 +00001192 StringRef Text = rawTokenText(FormatTok.Tok);
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001193 FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
Manuel Klimekf6fd00b2013-01-05 22:56:06 +00001194 if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0)
1195 FormatTok.IsFirst = true;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001196
1197 // Consume and record whitespace until we find a significant token.
1198 while (FormatTok.Tok.is(tok::unknown)) {
Manuel Klimeka28fc062013-02-11 12:33:24 +00001199 unsigned Newlines = Text.count('\n');
Daniel Jasper1eee6c42013-03-04 13:43:19 +00001200 if (Newlines > 0)
1201 FormatTok.LastNewlineOffset =
1202 FormatTok.WhiteSpaceLength + Text.rfind('\n') + 1;
Manuel Klimeka28fc062013-02-11 12:33:24 +00001203 unsigned EscapedNewlines = Text.count("\\\n");
1204 FormatTok.NewlinesBefore += Newlines;
1205 FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001206 FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
1207
1208 if (FormatTok.Tok.is(tok::eof))
1209 return FormatTok;
1210 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimek95419382013-01-07 07:56:50 +00001211 Text = rawTokenText(FormatTok.Tok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001212 }
Manuel Klimek95419382013-01-07 07:56:50 +00001213
1214 // Now FormatTok is the next non-whitespace token.
1215 FormatTok.TokenLength = Text.size();
1216
Manuel Klimekd4397b92013-01-04 23:34:14 +00001217 // In case the token starts with escaped newlines, we want to
1218 // take them into account as whitespace - this pattern is quite frequent
1219 // in macro definitions.
1220 // FIXME: What do we want to do with other escaped spaces, and escaped
1221 // spaces or newlines in the middle of tokens?
1222 // FIXME: Add a more explicit test.
1223 unsigned i = 0;
Daniel Jasper71607512013-01-07 10:48:50 +00001224 while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
Manuel Klimek86721d22013-01-22 16:31:55 +00001225 // FIXME: ++FormatTok.NewlinesBefore is missing...
Manuel Klimekd4397b92013-01-04 23:34:14 +00001226 FormatTok.WhiteSpaceLength += 2;
Manuel Klimek95419382013-01-07 07:56:50 +00001227 FormatTok.TokenLength -= 2;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001228 i += 2;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001229 }
1230
1231 if (FormatTok.Tok.is(tok::raw_identifier)) {
Manuel Klimekd4397b92013-01-04 23:34:14 +00001232 IdentifierInfo &Info = IdentTable.get(Text);
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001233 FormatTok.Tok.setIdentifierInfo(&Info);
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001234 FormatTok.Tok.setKind(Info.getTokenID());
1235 }
1236
1237 if (FormatTok.Tok.is(tok::greatergreater)) {
1238 FormatTok.Tok.setKind(tok::greater);
Daniel Jasperb6f02f32013-02-28 10:06:05 +00001239 FormatTok.TokenLength = 1;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001240 GreaterStashed = true;
1241 }
1242
Daniel Jasper812c0452013-03-01 16:45:59 +00001243 // If we reformat comments, we remove trailing whitespace. Update the length
1244 // accordingly.
1245 if (FormatTok.Tok.is(tok::comment))
1246 FormatTok.TokenLength = Text.rtrim().size();
1247
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001248 return FormatTok;
1249 }
1250
Nico Weberc2e6d2a2013-02-11 15:32:15 +00001251 IdentifierTable &getIdentTable() { return IdentTable; }
1252
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001253private:
1254 FormatToken FormatTok;
1255 bool GreaterStashed;
1256 Lexer &Lex;
1257 SourceManager &SourceMgr;
1258 IdentifierTable IdentTable;
1259
1260 /// Returns the text of \c FormatTok.
Manuel Klimek95419382013-01-07 07:56:50 +00001261 StringRef rawTokenText(Token &Tok) {
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001262 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
1263 Tok.getLength());
1264 }
1265};
1266
Daniel Jasperbac016b2012-12-03 18:12:45 +00001267class Formatter : public UnwrappedLineConsumer {
1268public:
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001269 Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex,
1270 SourceManager &SourceMgr,
Daniel Jasperbac016b2012-12-03 18:12:45 +00001271 const std::vector<CharSourceRange> &Ranges)
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001272 : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienko052685c2013-03-19 17:41:36 +00001273 Whitespaces(SourceMgr, Style), Ranges(Ranges) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001274
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001275 virtual ~Formatter() {}
Daniel Jasperaccb0b02012-12-04 21:05:31 +00001276
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001277 tooling::Replacements format() {
1278 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
1279 UnwrappedLineParser Parser(Diag, Style, Tokens, *this);
1280 StructuralError = Parser.parse();
1281 unsigned PreviousEndOfLineColumn = 0;
1282 TokenAnnotator Annotator(Style, SourceMgr, Lex,
1283 Tokens.getIdentTable().get("in"));
1284 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1285 Annotator.annotate(AnnotatedLines[i]);
1286 }
1287 deriveLocalStyle();
1288 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1289 Annotator.calculateFormattingInformation(AnnotatedLines[i]);
Daniel Jasper6050a1e2013-03-13 15:53:12 +00001290
1291 // Adapt level to the next line if this is a comment.
1292 // FIXME: Can/should this be done in the UnwrappedLineParser?
1293 if (i + 1 != e && AnnotatedLines[i].First.is(tok::comment) &&
1294 AnnotatedLines[i].First.Children.empty() &&
1295 AnnotatedLines[i + 1].First.isNot(tok::r_brace))
1296 AnnotatedLines[i].Level = AnnotatedLines[i + 1].Level;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001297 }
1298 std::vector<int> IndentForLevel;
1299 bool PreviousLineWasTouched = false;
1300 for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
1301 E = AnnotatedLines.end();
1302 I != E; ++I) {
1303 const AnnotatedLine &TheLine = *I;
1304 const FormatToken &FirstTok = TheLine.First.FormatTok;
1305 int Offset = getIndentOffset(TheLine.First);
1306 while (IndentForLevel.size() <= TheLine.Level)
1307 IndentForLevel.push_back(-1);
1308 IndentForLevel.resize(TheLine.Level + 1);
Daniel Jasperf9955d32013-03-20 12:37:50 +00001309 bool WasMoved = PreviousLineWasTouched && FirstTok.NewlinesBefore == 0;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001310 if (TheLine.First.is(tok::eof)) {
1311 if (PreviousLineWasTouched) {
1312 unsigned NewLines = std::min(FirstTok.NewlinesBefore, 1u);
1313 Whitespaces.replaceWhitespace(TheLine.First, NewLines, /*Indent*/ 0,
Alexander Kornienko052685c2013-03-19 17:41:36 +00001314 /*WhitespaceStartColumn*/ 0);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001315 }
1316 } else if (TheLine.Type != LT_Invalid &&
1317 (WasMoved || touchesLine(TheLine))) {
1318 unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
1319 unsigned Indent = LevelIndent;
1320 if (static_cast<int>(Indent) + Offset >= 0)
1321 Indent += Offset;
1322 if (!FirstTok.WhiteSpaceStart.isValid() || StructuralError) {
Daniel Jasperf9955d32013-03-20 12:37:50 +00001323 Indent = LevelIndent =
1324 SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001325 } else {
1326 formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
1327 PreviousEndOfLineColumn);
1328 }
1329 tryFitMultipleLinesInOne(Indent, I, E);
1330 UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
1331 TheLine.First, Whitespaces,
1332 StructuralError);
1333 PreviousEndOfLineColumn =
1334 Formatter.format(I + 1 != E ? &*(I + 1) : NULL);
1335 IndentForLevel[TheLine.Level] = LevelIndent;
1336 PreviousLineWasTouched = true;
1337 } else {
1338 if (FirstTok.NewlinesBefore > 0 || FirstTok.IsFirst) {
1339 unsigned Indent =
1340 SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
1341 unsigned LevelIndent = Indent;
1342 if (static_cast<int>(LevelIndent) - Offset >= 0)
1343 LevelIndent -= Offset;
Daniel Jasper83a90e52013-03-20 14:31:47 +00001344 if (TheLine.First.isNot(tok::comment))
1345 IndentForLevel[TheLine.Level] = LevelIndent;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001346
1347 // Remove trailing whitespace of the previous line if it was touched.
1348 if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine))
1349 formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
1350 PreviousEndOfLineColumn);
1351 }
1352 // If we did not reformat this unwrapped line, the column at the end of
1353 // the last token is unchanged - thus, we can calculate the end of the
1354 // last token.
1355 SourceLocation LastLoc = TheLine.Last->FormatTok.Tok.getLocation();
1356 PreviousEndOfLineColumn =
1357 SourceMgr.getSpellingColumnNumber(LastLoc) +
1358 Lex.MeasureTokenLength(LastLoc, SourceMgr, Lex.getLangOpts()) - 1;
1359 PreviousLineWasTouched = false;
Daniel Jasperc363dbb2013-03-22 16:25:51 +00001360 if (TheLine.Last->is(tok::comment))
1361 Whitespaces.addUntouchableComment(SourceMgr.getSpellingColumnNumber(
1362 TheLine.Last->FormatTok.Tok.getLocation()) - 1);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001363 }
1364 }
1365 return Whitespaces.generateReplacements();
1366 }
1367
1368private:
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001369 void deriveLocalStyle() {
1370 unsigned CountBoundToVariable = 0;
1371 unsigned CountBoundToType = 0;
1372 bool HasCpp03IncompatibleFormat = false;
1373 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1374 if (AnnotatedLines[i].First.Children.empty())
1375 continue;
1376 AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0];
1377 while (!Tok->Children.empty()) {
1378 if (Tok->Type == TT_PointerOrReference) {
1379 bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0;
1380 bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0;
1381 if (SpacesBefore && !SpacesAfter)
1382 ++CountBoundToVariable;
1383 else if (!SpacesBefore && SpacesAfter)
1384 ++CountBoundToType;
1385 }
1386
Daniel Jasper29f123b2013-02-08 15:28:42 +00001387 if (Tok->Type == TT_TemplateCloser &&
1388 Tok->Parent->Type == TT_TemplateCloser &&
1389 Tok->FormatTok.WhiteSpaceLength == 0)
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001390 HasCpp03IncompatibleFormat = true;
1391 Tok = &Tok->Children[0];
1392 }
1393 }
1394 if (Style.DerivePointerBinding) {
1395 if (CountBoundToType > CountBoundToVariable)
1396 Style.PointerBindsToType = true;
1397 else if (CountBoundToType < CountBoundToVariable)
1398 Style.PointerBindsToType = false;
1399 }
1400 if (Style.Standard == FormatStyle::LS_Auto) {
1401 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
1402 : FormatStyle::LS_Cpp03;
1403 }
1404 }
1405
Manuel Klimek547d5db2013-02-08 17:38:27 +00001406 /// \brief Get the indent of \p Level from \p IndentForLevel.
1407 ///
1408 /// \p IndentForLevel must contain the indent for the level \c l
1409 /// at \p IndentForLevel[l], or a value < 0 if the indent for
1410 /// that level is unknown.
Daniel Jasperfc759082013-02-14 14:26:07 +00001411 unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
Manuel Klimek547d5db2013-02-08 17:38:27 +00001412 if (IndentForLevel[Level] != -1)
1413 return IndentForLevel[Level];
Manuel Klimek52635ff2013-02-08 19:53:32 +00001414 if (Level == 0)
1415 return 0;
Daniel Jasperc78c6b32013-02-14 09:58:41 +00001416 return getIndent(IndentForLevel, Level - 1) + 2;
Manuel Klimek547d5db2013-02-08 17:38:27 +00001417 }
1418
1419 /// \brief Get the offset of the line relatively to the level.
1420 ///
1421 /// For example, 'public:' labels in classes are offset by 1 or 2
1422 /// characters to the left from their level.
Daniel Jasperc78c6b32013-02-14 09:58:41 +00001423 int getIndentOffset(const AnnotatedToken &RootToken) {
Manuel Klimek547d5db2013-02-08 17:38:27 +00001424 bool IsAccessModifier = false;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001425 if (RootToken.isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
Manuel Klimek547d5db2013-02-08 17:38:27 +00001426 IsAccessModifier = true;
1427 else if (RootToken.is(tok::at) && !RootToken.Children.empty() &&
1428 (RootToken.Children[0].isObjCAtKeyword(tok::objc_public) ||
1429 RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) ||
1430 RootToken.Children[0].isObjCAtKeyword(tok::objc_package) ||
1431 RootToken.Children[0].isObjCAtKeyword(tok::objc_private)))
1432 IsAccessModifier = true;
1433
1434 if (IsAccessModifier)
1435 return Style.AccessModifierOffset;
1436 return 0;
1437 }
1438
Manuel Klimek517e8942013-01-11 17:54:10 +00001439 /// \brief Tries to merge lines into one.
1440 ///
1441 /// This will change \c Line and \c AnnotatedLine to contain the merged line,
1442 /// if possible; note that \c I will be incremented when lines are merged.
1443 ///
1444 /// Returns whether the resulting \c Line can fit in a single line.
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001445 void tryFitMultipleLinesInOne(unsigned Indent,
Daniel Jasper995e8202013-01-14 13:08:07 +00001446 std::vector<AnnotatedLine>::iterator &I,
1447 std::vector<AnnotatedLine>::iterator E) {
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001448 // We can never merge stuff if there are trailing line comments.
1449 if (I->Last->Type == TT_LineComment)
1450 return;
1451
Daniel Jaspera4d46212013-02-28 11:05:57 +00001452 unsigned Limit = Style.ColumnLimit - Indent;
Daniel Jasperf11a7052013-02-21 21:33:55 +00001453 // If we already exceed the column limit, we set 'Limit' to 0. The different
1454 // tryMerge..() functions can then decide whether to still do merging.
1455 Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
Daniel Jasper55b08e72013-01-16 07:02:34 +00001456
Daniel Jasper9c8c40e2013-01-21 14:18:28 +00001457 if (I + 1 == E || (I + 1)->Type == LT_Invalid)
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001458 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001459
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001460 if (I->Last->is(tok::l_brace)) {
1461 tryMergeSimpleBlock(I, E, Limit);
1462 } else if (I->First.is(tok::kw_if)) {
1463 tryMergeSimpleIf(I, E, Limit);
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001464 } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline ||
1465 I->First.FormatTok.IsFirst)) {
1466 tryMergeSimplePPDirective(I, E, Limit);
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001467 }
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001468 return;
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001469 }
1470
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001471 void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
1472 std::vector<AnnotatedLine>::iterator E,
1473 unsigned Limit) {
Daniel Jasperf11a7052013-02-21 21:33:55 +00001474 if (Limit == 0)
1475 return;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001476 AnnotatedLine &Line = *I;
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001477 if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline)
1478 return;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001479 if (I + 2 != E && (I + 2)->InPPDirective &&
1480 !(I + 2)->First.FormatTok.HasUnescapedNewline)
1481 return;
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001482 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001483 return;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001484 join(Line, *(++I));
1485 }
1486
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001487 void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I,
1488 std::vector<AnnotatedLine>::iterator E,
1489 unsigned Limit) {
Daniel Jasperf11a7052013-02-21 21:33:55 +00001490 if (Limit == 0)
1491 return;
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001492 if (!Style.AllowShortIfStatementsOnASingleLine)
1493 return;
Manuel Klimek4c128122013-01-18 14:46:43 +00001494 if ((I + 1)->InPPDirective != I->InPPDirective ||
1495 ((I + 1)->InPPDirective &&
1496 (I + 1)->First.FormatTok.HasUnescapedNewline))
1497 return;
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001498 AnnotatedLine &Line = *I;
Daniel Jasper55b08e72013-01-16 07:02:34 +00001499 if (Line.Last->isNot(tok::r_paren))
1500 return;
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001501 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001502 return;
1503 if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment)
1504 return;
1505 // Only inline simple if's (no nested if or else).
1506 if (I + 2 != E && (I + 2)->First.is(tok::kw_else))
1507 return;
1508 join(Line, *(++I));
1509 }
1510
1511 void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
Daniel Jasper1a1ce832013-01-29 11:27:30 +00001512 std::vector<AnnotatedLine>::iterator E,
1513 unsigned Limit) {
Manuel Klimek517e8942013-01-11 17:54:10 +00001514 // First, check that the current line allows merging. This is the case if
1515 // we're not in a control flow statement and the last token is an opening
1516 // brace.
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001517 AnnotatedLine &Line = *I;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001518 if (Line.First.isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
1519 tok::kw_else, tok::kw_try, tok::kw_catch,
1520 tok::kw_for,
1521 // This gets rid of all ObjC @ keywords and methods.
1522 tok::at, tok::minus, tok::plus))
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001523 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001524
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001525 AnnotatedToken *Tok = &(I + 1)->First;
1526 if (Tok->Children.empty() && Tok->is(tok::r_brace) &&
Daniel Jasperf11a7052013-02-21 21:33:55 +00001527 !Tok->MustBreakBefore) {
1528 // We merge empty blocks even if the line exceeds the column limit.
Daniel Jasper729a7432013-02-11 12:36:37 +00001529 Tok->SpacesRequiredBefore = 0;
Daniel Jasperf11a7052013-02-21 21:33:55 +00001530 Tok->CanBreakBefore = true;
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001531 join(Line, *(I + 1));
1532 I += 1;
Daniel Jasperf11a7052013-02-21 21:33:55 +00001533 } else if (Limit != 0) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001534 // Check that we still have three lines and they fit into the limit.
1535 if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
1536 !nextTwoLinesFitInto(I, Limit))
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001537 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001538
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001539 // Second, check that the next line does not contain any braces - if it
1540 // does, readability declines when putting it into a single line.
1541 if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
1542 return;
1543 do {
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001544 if (Tok->isOneOf(tok::l_brace, tok::r_brace))
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001545 return;
1546 Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
1547 } while (Tok != NULL);
Manuel Klimek517e8942013-01-11 17:54:10 +00001548
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001549 // Last, check that the third line contains a single closing brace.
1550 Tok = &(I + 2)->First;
1551 if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
1552 Tok->MustBreakBefore)
1553 return;
1554
1555 join(Line, *(I + 1));
1556 join(Line, *(I + 2));
1557 I += 2;
Manuel Klimek517e8942013-01-11 17:54:10 +00001558 }
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001559 }
1560
1561 bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
1562 unsigned Limit) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001563 return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
1564 Limit;
Manuel Klimek517e8942013-01-11 17:54:10 +00001565 }
1566
Daniel Jasper995e8202013-01-14 13:08:07 +00001567 void join(AnnotatedLine &A, const AnnotatedLine &B) {
Daniel Jasperf11a7052013-02-21 21:33:55 +00001568 unsigned LengthA = A.Last->TotalLength + B.First.SpacesRequiredBefore;
Daniel Jasper995e8202013-01-14 13:08:07 +00001569 A.Last->Children.push_back(B.First);
1570 while (!A.Last->Children.empty()) {
1571 A.Last->Children[0].Parent = A.Last;
Daniel Jasperf11a7052013-02-21 21:33:55 +00001572 A.Last->Children[0].TotalLength += LengthA;
Daniel Jasper995e8202013-01-14 13:08:07 +00001573 A.Last = &A.Last->Children[0];
1574 }
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001575 }
1576
Daniel Jasper6f21a982013-03-13 07:49:51 +00001577 bool touchesRanges(const CharSourceRange &Range) {
Daniel Jasperf3023542013-03-07 20:50:00 +00001578 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1579 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
1580 Ranges[i].getBegin()) &&
1581 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1582 Range.getBegin()))
1583 return true;
1584 }
1585 return false;
1586 }
1587
1588 bool touchesLine(const AnnotatedLine &TheLine) {
Daniel Jasper995e8202013-01-14 13:08:07 +00001589 const FormatToken *First = &TheLine.First.FormatTok;
1590 const FormatToken *Last = &TheLine.Last->FormatTok;
Daniel Jaspercd162382013-01-07 13:26:07 +00001591 CharSourceRange LineRange = CharSourceRange::getTokenRange(
Daniel Jasper1eee6c42013-03-04 13:43:19 +00001592 First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset),
1593 Last->Tok.getLocation());
Daniel Jasperf3023542013-03-07 20:50:00 +00001594 return touchesRanges(LineRange);
1595 }
1596
1597 bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
1598 const FormatToken *First = &TheLine.First.FormatTok;
1599 CharSourceRange LineRange = CharSourceRange::getCharRange(
1600 First->WhiteSpaceStart,
1601 First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset));
1602 return touchesRanges(LineRange);
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001603 }
1604
1605 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001606 AnnotatedLines.push_back(AnnotatedLine(TheLine));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001607 }
1608
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001609 /// \brief Add a new line and the required indent before the first Token
1610 /// of the \c UnwrappedLine if there was no structural parsing error.
1611 /// Returns the indent level of the \c UnwrappedLine.
Manuel Klimek547d5db2013-02-08 17:38:27 +00001612 void formatFirstToken(const AnnotatedToken &RootToken, unsigned Indent,
1613 bool InPPDirective, unsigned PreviousEndOfLineColumn) {
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001614 const FormatToken &Tok = RootToken.FormatTok;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001615
Daniel Jasper1a1ce832013-01-29 11:27:30 +00001616 unsigned Newlines =
1617 std::min(Tok.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001618 if (Newlines == 0 && !Tok.IsFirst)
1619 Newlines = 1;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001620
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001621 if (!InPPDirective || Tok.HasUnescapedNewline) {
Alexander Kornienko052685c2013-03-19 17:41:36 +00001622 Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0);
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001623 } else {
Daniel Jasperdcc2a622013-01-18 08:44:07 +00001624 Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent,
Alexander Kornienko052685c2013-03-19 17:41:36 +00001625 PreviousEndOfLineColumn);
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001626 }
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001627 }
1628
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001629 DiagnosticsEngine &Diag;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001630 FormatStyle Style;
1631 Lexer &Lex;
1632 SourceManager &SourceMgr;
Daniel Jasperdcc2a622013-01-18 08:44:07 +00001633 WhitespaceManager Whitespaces;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001634 std::vector<CharSourceRange> Ranges;
Daniel Jasper995e8202013-01-14 13:08:07 +00001635 std::vector<AnnotatedLine> AnnotatedLines;
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001636 bool StructuralError;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001637};
1638
Daniel Jasper1a1ce832013-01-29 11:27:30 +00001639tooling::Replacements
1640reformat(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
1641 std::vector<CharSourceRange> Ranges, DiagnosticConsumer *DiagClient) {
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001642 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001643 OwningPtr<DiagnosticConsumer> DiagPrinter;
1644 if (DiagClient == 0) {
1645 DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts));
1646 DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
1647 DiagClient = DiagPrinter.get();
1648 }
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001649 DiagnosticsEngine Diagnostics(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001650 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001651 DiagClient, false);
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001652 Diagnostics.setSourceManager(&SourceMgr);
1653 Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001654 return formatter.format();
1655}
1656
Daniel Jasper46ef8522013-01-10 13:08:12 +00001657LangOptions getFormattingLangOpts() {
1658 LangOptions LangOpts;
1659 LangOpts.CPlusPlus = 1;
1660 LangOpts.CPlusPlus11 = 1;
Daniel Jasperb64eca02013-03-22 10:01:29 +00001661 LangOpts.LineComment = 1;
Daniel Jasper46ef8522013-01-10 13:08:12 +00001662 LangOpts.Bool = 1;
1663 LangOpts.ObjC1 = 1;
1664 LangOpts.ObjC2 = 1;
1665 return LangOpts;
1666}
1667
Daniel Jaspercd162382013-01-07 13:26:07 +00001668} // namespace format
1669} // namespace clang