blob: a78b650dfad08401537863ba8e7cf4fd2396eb32 [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- Format.cpp - Format C++ code -------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file implements functions declared in Format.h. This will be
12/// split into separate files as we go.
13///
Daniel Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Manuel Klimek24998102013-01-16 14:55:28 +000016#define DEBUG_TYPE "format-formatter"
17
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000018#include "TokenAnnotator.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "UnwrappedLineParser.h"
Alexander Kornienko5b7157a2013-01-10 15:05:09 +000020#include "clang/Basic/Diagnostic.h"
Daniel Jasperab7654e2012-12-21 10:20:02 +000021#include "clang/Basic/OperatorPrecedence.h"
Chandler Carruth44eb4f62013-01-02 10:28:36 +000022#include "clang/Basic/SourceManager.h"
Manuel Klimek24998102013-01-16 14:55:28 +000023#include "clang/Format/Format.h"
Alexander Kornienko5b7157a2013-01-10 15:05:09 +000024#include "clang/Frontend/TextDiagnosticPrinter.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000025#include "clang/Lex/Lexer.h"
Manuel Klimek24998102013-01-16 14:55:28 +000026#include "llvm/Support/Debug.h"
Daniel Jasper8b529712012-12-04 13:02:32 +000027#include <string>
28
Manuel Klimek24998102013-01-16 14:55:28 +000029// Uncomment to get debug output from tests:
30// #define DEBUG_WITH_TYPE(T, X) do { X; } while(0)
31
Daniel Jasperf7935112012-12-03 18:12:45 +000032namespace clang {
33namespace format {
34
Daniel Jasperf7935112012-12-03 18:12:45 +000035FormatStyle getLLVMStyle() {
36 FormatStyle LLVMStyle;
37 LLVMStyle.ColumnLimit = 80;
38 LLVMStyle.MaxEmptyLinesToKeep = 1;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +000039 LLVMStyle.PointerBindsToType = false;
40 LLVMStyle.DerivePointerBinding = false;
Daniel Jasperf7935112012-12-03 18:12:45 +000041 LLVMStyle.AccessModifierOffset = -2;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +000042 LLVMStyle.Standard = FormatStyle::LS_Cpp03;
Alexander Kornienko578fdd82012-12-06 18:03:27 +000043 LLVMStyle.IndentCaseLabels = false;
Daniel Jasper5ad1e192013-01-07 11:09:06 +000044 LLVMStyle.SpacesBeforeTrailingComments = 1;
Daniel Jasper9278eb92013-01-16 14:59:02 +000045 LLVMStyle.BinPackParameters = true;
Daniel Jasperf7db4332013-01-29 16:03:49 +000046 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasperd36ef5e2013-01-28 15:40:20 +000047 LLVMStyle.AllowReturnTypeOnItsOwnLine = true;
Daniel Jasper2408a8c2013-01-11 11:37:55 +000048 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Daniel Jasper1b750ed2013-01-14 16:24:39 +000049 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Nico Webera6087752013-01-10 20:12:55 +000050 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Daniel Jasper3a9370c2013-02-04 07:21:18 +000051 LLVMStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasperf7935112012-12-03 18:12:45 +000052 return LLVMStyle;
53}
54
55FormatStyle getGoogleStyle() {
56 FormatStyle GoogleStyle;
57 GoogleStyle.ColumnLimit = 80;
58 GoogleStyle.MaxEmptyLinesToKeep = 1;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +000059 GoogleStyle.PointerBindsToType = true;
60 GoogleStyle.DerivePointerBinding = true;
Daniel Jasperf7935112012-12-03 18:12:45 +000061 GoogleStyle.AccessModifierOffset = -1;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +000062 GoogleStyle.Standard = FormatStyle::LS_Auto;
Alexander Kornienko578fdd82012-12-06 18:03:27 +000063 GoogleStyle.IndentCaseLabels = true;
Daniel Jasper5ad1e192013-01-07 11:09:06 +000064 GoogleStyle.SpacesBeforeTrailingComments = 2;
Daniel Jasper9278eb92013-01-16 14:59:02 +000065 GoogleStyle.BinPackParameters = false;
Daniel Jasperf7db4332013-01-29 16:03:49 +000066 GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasperd36ef5e2013-01-28 15:40:20 +000067 GoogleStyle.AllowReturnTypeOnItsOwnLine = false;
Daniel Jasper2408a8c2013-01-11 11:37:55 +000068 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
Daniel Jasperced17f82013-01-16 15:44:34 +000069 GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
Nico Webera6087752013-01-10 20:12:55 +000070 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Daniel Jasper3a9370c2013-02-04 07:21:18 +000071 GoogleStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasperf7935112012-12-03 18:12:45 +000072 return GoogleStyle;
73}
74
Daniel Jasper1b750ed2013-01-14 16:24:39 +000075FormatStyle getChromiumStyle() {
76 FormatStyle ChromiumStyle = getGoogleStyle();
Daniel Jasperf7db4332013-01-29 16:03:49 +000077 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +000078 ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
79 ChromiumStyle.DerivePointerBinding = false;
Daniel Jasper1b750ed2013-01-14 16:24:39 +000080 return ChromiumStyle;
81}
82
Daniel Jasper94f0e132013-02-06 20:07:35 +000083static bool isTrailingComment(const AnnotatedToken &Tok) {
84 return Tok.is(tok::comment) &&
85 (Tok.Children.empty() || Tok.Children[0].MustBreakBefore);
86}
87
Daniel Jasperacc33662013-02-08 08:22:00 +000088// Returns the length of everything up to the first possible line break after
89// the ), ], } or > matching \c Tok.
90static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) {
91 if (Tok.MatchingParen == NULL)
92 return 0;
93 AnnotatedToken *End = Tok.MatchingParen;
94 while (!End->Children.empty() && !End->Children[0].CanBreakBefore) {
95 End = &End->Children[0];
96 }
97 return End->TotalLength - Tok.TotalLength + 1;
98}
99
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000100/// \brief Manages the whitespaces around tokens and their replacements.
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000101///
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000102/// This includes special handling for certain constructs, e.g. the alignment of
103/// trailing line comments.
104class WhitespaceManager {
105public:
106 WhitespaceManager(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
107
108 /// \brief Replaces the whitespace in front of \p Tok. Only call once for
109 /// each \c AnnotatedToken.
110 void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
111 unsigned Spaces, unsigned WhitespaceStartColumn,
112 const FormatStyle &Style) {
Daniel Jasper304a9862013-01-21 22:49:20 +0000113 // 2+ newlines mean an empty line separating logic scopes.
114 if (NewLines >= 2)
115 alignComments();
116
117 // Align line comments if they are trailing or if they continue other
118 // trailing comments.
Daniel Jasper94f0e132013-02-06 20:07:35 +0000119 if (isTrailingComment(Tok) && (Tok.Parent != NULL || !Comments.empty())) {
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000120 if (Style.ColumnLimit >=
121 Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
122 Comments.push_back(StoredComment());
123 Comments.back().Tok = Tok.FormatTok;
124 Comments.back().Spaces = Spaces;
125 Comments.back().NewLines = NewLines;
Daniel Jasperf79f9352013-02-06 22:04:05 +0000126 if (NewLines == 0)
127 Comments.back().MinColumn = WhitespaceStartColumn + Spaces;
128 else
129 Comments.back().MinColumn = Spaces;
Daniel Jasperbbc84152013-01-29 11:27:30 +0000130 Comments.back().MaxColumn =
131 Style.ColumnLimit - Spaces - Tok.FormatTok.TokenLength;
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000132 return;
133 }
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000134 }
Daniel Jasper304a9862013-01-21 22:49:20 +0000135
136 // If this line does not have a trailing comment, align the stored comments.
Daniel Jasper94f0e132013-02-06 20:07:35 +0000137 if (Tok.Children.empty() && !isTrailingComment(Tok))
Daniel Jasper304a9862013-01-21 22:49:20 +0000138 alignComments();
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000139 storeReplacement(Tok.FormatTok,
140 std::string(NewLines, '\n') + std::string(Spaces, ' '));
141 }
142
143 /// \brief Like \c replaceWhitespace, but additionally adds right-aligned
144 /// backslashes to escape newlines inside a preprocessor directive.
145 ///
146 /// This function and \c replaceWhitespace have the same behavior if
147 /// \c Newlines == 0.
148 void replacePPWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
149 unsigned Spaces, unsigned WhitespaceStartColumn,
150 const FormatStyle &Style) {
151 std::string NewLineText;
152 if (NewLines > 0) {
Daniel Jasperbbc84152013-01-29 11:27:30 +0000153 unsigned Offset =
154 std::min<int>(Style.ColumnLimit - 1, WhitespaceStartColumn);
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000155 for (unsigned i = 0; i < NewLines; ++i) {
156 NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' ');
157 NewLineText += "\\\n";
158 Offset = 0;
159 }
160 }
161 storeReplacement(Tok.FormatTok, NewLineText + std::string(Spaces, ' '));
162 }
163
164 /// \brief Returns all the \c Replacements created during formatting.
165 const tooling::Replacements &generateReplacements() {
166 alignComments();
167 return Replaces;
168 }
169
170private:
171 /// \brief Structure to store a comment for later layout and alignment.
172 struct StoredComment {
173 FormatToken Tok;
174 unsigned MinColumn;
175 unsigned MaxColumn;
176 unsigned NewLines;
177 unsigned Spaces;
178 };
179 SmallVector<StoredComment, 16> Comments;
180 typedef SmallVector<StoredComment, 16>::iterator comment_iterator;
181
182 /// \brief Try to align all stashed comments.
183 void alignComments() {
184 unsigned MinColumn = 0;
185 unsigned MaxColumn = UINT_MAX;
186 comment_iterator Start = Comments.begin();
187 for (comment_iterator I = Comments.begin(), E = Comments.end(); I != E;
188 ++I) {
189 if (I->MinColumn > MaxColumn || I->MaxColumn < MinColumn) {
190 alignComments(Start, I, MinColumn);
191 MinColumn = I->MinColumn;
192 MaxColumn = I->MaxColumn;
193 Start = I;
194 } else {
195 MinColumn = std::max(MinColumn, I->MinColumn);
196 MaxColumn = std::min(MaxColumn, I->MaxColumn);
197 }
198 }
199 alignComments(Start, Comments.end(), MinColumn);
200 Comments.clear();
201 }
202
203 /// \brief Put all the comments between \p I and \p E into \p Column.
204 void alignComments(comment_iterator I, comment_iterator E, unsigned Column) {
205 while (I != E) {
206 unsigned Spaces = I->Spaces + Column - I->MinColumn;
207 storeReplacement(I->Tok, std::string(I->NewLines, '\n') +
Daniel Jasper400adc62013-02-08 15:28:42 +0000208 std::string(Spaces, ' '));
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000209 ++I;
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000210 }
211 }
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000212
213 /// \brief Stores \p Text as the replacement for the whitespace in front of
214 /// \p Tok.
215 void storeReplacement(const FormatToken &Tok, const std::string Text) {
Daniel Jasper7b038a22013-01-30 09:46:12 +0000216 // Don't create a replacement, if it does not change anything.
217 if (StringRef(SourceMgr.getCharacterData(Tok.WhiteSpaceStart),
218 Tok.WhiteSpaceLength) == Text)
219 return;
220
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000221 Replaces.insert(tooling::Replacement(SourceMgr, Tok.WhiteSpaceStart,
222 Tok.WhiteSpaceLength, Text));
223 }
224
225 SourceManager &SourceMgr;
226 tooling::Replacements Replaces;
227};
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000228
Daniel Jasperf7935112012-12-03 18:12:45 +0000229class UnwrappedLineFormatter {
230public:
Manuel Klimekb2c6dbe2013-01-10 19:17:33 +0000231 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000232 const AnnotatedLine &Line, unsigned FirstIndent,
Daniel Jaspera67a8f02013-01-16 10:41:46 +0000233 const AnnotatedToken &RootToken,
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000234 WhitespaceManager &Whitespaces, bool StructuralError)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000235 : Style(Style), SourceMgr(SourceMgr), Line(Line),
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000236 FirstIndent(FirstIndent), RootToken(RootToken),
237 Whitespaces(Whitespaces) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000238 }
239
Manuel Klimek1abf7892013-01-04 23:34:14 +0000240 /// \brief Formats an \c UnwrappedLine.
241 ///
242 /// \returns The column after the last token in the last line of the
243 /// \c UnwrappedLine.
244 unsigned format() {
Daniel Jaspere9de2602012-12-06 09:56:08 +0000245 // Initialize state dependent on indent.
Daniel Jasper337816e2013-01-11 10:22:12 +0000246 LineState State;
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000247 State.Column = FirstIndent;
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000248 State.NextToken = &RootToken;
Daniel Jasperb9ebd5d2013-02-05 09:41:21 +0000249 State.Stack.push_back(
250 ParenState(FirstIndent + 4, FirstIndent, !Style.BinPackParameters,
251 /*HasMultiParameterLine=*/ false));
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000252 State.VariablePos = 0;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000253 State.LineContainsContinuedForLoopSection = false;
Daniel Jasper400adc62013-02-08 15:28:42 +0000254 State.ParenLevel = 0;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000255
Manuel Klimek24998102013-01-16 14:55:28 +0000256 DEBUG({
257 DebugTokenState(*State.NextToken);
258 });
259
Daniel Jaspere9de2602012-12-06 09:56:08 +0000260 // The first token has already been indented and thus consumed.
261 moveStateToNextToken(State);
Daniel Jasperf7935112012-12-03 18:12:45 +0000262
Daniel Jasper4b866272013-02-01 11:00:45 +0000263 // If everything fits on a single line, just put it there.
264 if (Line.Last->TotalLength <= getColumnLimit() - FirstIndent) {
265 while (State.NextToken != NULL) {
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000266 addTokenToState(false, false, State);
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000267 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000268 return State.Column;
Daniel Jasperf7935112012-12-03 18:12:45 +0000269 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000270
Daniel Jasperacc33662013-02-08 08:22:00 +0000271 // If the ObjC method declaration does not fit on a line, we should format
272 // it with one arg per line.
273 if (Line.Type == LT_ObjCMethodDecl)
274 State.Stack.back().BreakBeforeParameter = true;
275
Daniel Jasper4b866272013-02-01 11:00:45 +0000276 // Find best solution in solution space.
277 return analyzeSolutionSpace(State);
Daniel Jasperf7935112012-12-03 18:12:45 +0000278 }
279
280private:
Manuel Klimek24998102013-01-16 14:55:28 +0000281 void DebugTokenState(const AnnotatedToken &AnnotatedTok) {
282 const Token &Tok = AnnotatedTok.FormatTok.Tok;
Daniel Jasperbbc84152013-01-29 11:27:30 +0000283 llvm::errs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
284 Tok.getLength());
Manuel Klimek24998102013-01-16 14:55:28 +0000285 llvm::errs();
286 }
287
Daniel Jasper337816e2013-01-11 10:22:12 +0000288 struct ParenState {
Daniel Jasperb9ebd5d2013-02-05 09:41:21 +0000289 ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking,
290 bool HasMultiParameterLine)
Daniel Jasper400adc62013-02-08 15:28:42 +0000291 : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
292 BreakBeforeClosingBrace(false), QuestionColumn(0),
Daniel Jasperacc33662013-02-08 08:22:00 +0000293 AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000294 HasMultiParameterLine(HasMultiParameterLine), ColonPos(0) {
Daniel Jasperbbc84152013-01-29 11:27:30 +0000295 }
Daniel Jasper6d822722012-12-24 16:43:00 +0000296
Daniel Jasperf7935112012-12-03 18:12:45 +0000297 /// \brief The position to which a specific parenthesis level needs to be
298 /// indented.
Daniel Jasper337816e2013-01-11 10:22:12 +0000299 unsigned Indent;
Daniel Jasperf7935112012-12-03 18:12:45 +0000300
Daniel Jaspere9de2602012-12-06 09:56:08 +0000301 /// \brief The position of the last space on each level.
302 ///
303 /// Used e.g. to break like:
304 /// functionCall(Parameter, otherCall(
305 /// OtherParameter));
Daniel Jasper337816e2013-01-11 10:22:12 +0000306 unsigned LastSpace;
Daniel Jasperf7935112012-12-03 18:12:45 +0000307
Daniel Jaspere9de2602012-12-06 09:56:08 +0000308 /// \brief The position the first "<<" operator encountered on each level.
309 ///
310 /// Used to align "<<" operators. 0 if no such operator has been encountered
311 /// on a level.
Daniel Jasper337816e2013-01-11 10:22:12 +0000312 unsigned FirstLessLess;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000313
Manuel Klimek0ddd57a2013-01-10 15:58:26 +0000314 /// \brief Whether a newline needs to be inserted before the block's closing
315 /// brace.
316 ///
317 /// We only want to insert a newline before the closing brace if there also
318 /// was a newline after the beginning left brace.
Daniel Jasper337816e2013-01-11 10:22:12 +0000319 bool BreakBeforeClosingBrace;
320
Daniel Jasperca6623b2013-01-28 12:45:14 +0000321 /// \brief The column of a \c ? in a conditional expression;
322 unsigned QuestionColumn;
323
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000324 /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple
325 /// lines, in this context.
326 bool AvoidBinPacking;
327
328 /// \brief Break after the next comma (or all the commas in this context if
329 /// \c AvoidBinPacking is \c true).
Daniel Jasperacc33662013-02-08 08:22:00 +0000330 bool BreakBeforeParameter;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000331
332 /// \brief This context already has a line with more than one parameter.
Daniel Jasper9278eb92013-01-16 14:59:02 +0000333 bool HasMultiParameterLine;
Daniel Jasper2408a8c2013-01-11 11:37:55 +0000334
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000335 /// \brief The position of the colon in an ObjC method declaration/call.
336 unsigned ColonPos;
337
Daniel Jasper337816e2013-01-11 10:22:12 +0000338 bool operator<(const ParenState &Other) const {
339 if (Indent != Other.Indent)
Daniel Jasperfd8c4b12013-01-11 14:23:32 +0000340 return Indent < Other.Indent;
Daniel Jasper337816e2013-01-11 10:22:12 +0000341 if (LastSpace != Other.LastSpace)
342 return LastSpace < Other.LastSpace;
343 if (FirstLessLess != Other.FirstLessLess)
344 return FirstLessLess < Other.FirstLessLess;
Daniel Jasper2408a8c2013-01-11 11:37:55 +0000345 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
346 return BreakBeforeClosingBrace;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000347 if (QuestionColumn != Other.QuestionColumn)
348 return QuestionColumn < Other.QuestionColumn;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000349 if (AvoidBinPacking != Other.AvoidBinPacking)
350 return AvoidBinPacking;
Daniel Jasperacc33662013-02-08 08:22:00 +0000351 if (BreakBeforeParameter != Other.BreakBeforeParameter)
352 return BreakBeforeParameter;
Daniel Jasper9278eb92013-01-16 14:59:02 +0000353 if (HasMultiParameterLine != Other.HasMultiParameterLine)
354 return HasMultiParameterLine;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000355 if (ColonPos != Other.ColonPos)
356 return ColonPos < Other.ColonPos;
Daniel Jasper7b7877a2013-01-12 07:36:22 +0000357 return false;
Daniel Jasper337816e2013-01-11 10:22:12 +0000358 }
359 };
360
361 /// \brief The current state when indenting a unwrapped line.
362 ///
363 /// As the indenting tries different combinations this is copied by value.
364 struct LineState {
365 /// \brief The number of used columns in the current line.
366 unsigned Column;
367
368 /// \brief The token that needs to be next formatted.
369 const AnnotatedToken *NextToken;
370
Daniel Jasperbbc84152013-01-29 11:27:30 +0000371 /// \brief The column of the first variable name in a variable declaration.
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000372 ///
Daniel Jasperbbc84152013-01-29 11:27:30 +0000373 /// Used to align further variables if necessary.
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000374 unsigned VariablePos;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000375
376 /// \brief \c true if this line contains a continued for-loop section.
377 bool LineContainsContinuedForLoopSection;
378
Daniel Jasper400adc62013-02-08 15:28:42 +0000379 /// \brief The level of nesting inside (), [], <> and {}.
380 unsigned ParenLevel;
381
Daniel Jasper337816e2013-01-11 10:22:12 +0000382 /// \brief A stack keeping track of properties applying to parenthesis
383 /// levels.
384 std::vector<ParenState> Stack;
385
386 /// \brief Comparison operator to be able to used \c LineState in \c map.
387 bool operator<(const LineState &Other) const {
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000388 if (Other.NextToken != NextToken)
389 return Other.NextToken > NextToken;
Daniel Jasperf7935112012-12-03 18:12:45 +0000390 if (Other.Column != Column)
391 return Other.Column > Column;
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000392 if (Other.VariablePos != VariablePos)
393 return Other.VariablePos < VariablePos;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000394 if (Other.LineContainsContinuedForLoopSection !=
395 LineContainsContinuedForLoopSection)
396 return LineContainsContinuedForLoopSection;
Daniel Jasper400adc62013-02-08 15:28:42 +0000397 if (Other.ParenLevel != ParenLevel)
398 return Other.ParenLevel < ParenLevel;
Daniel Jasper337816e2013-01-11 10:22:12 +0000399 return Other.Stack < Stack;
Daniel Jasperf7935112012-12-03 18:12:45 +0000400 }
401 };
402
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000403 /// \brief Appends the next token to \p State and updates information
404 /// necessary for indentation.
405 ///
406 /// Puts the token on the current line if \p Newline is \c true and adds a
407 /// line break and necessary indentation otherwise.
408 ///
409 /// If \p DryRun is \c false, also creates and stores the required
410 /// \c Replacement.
Daniel Jasper337816e2013-01-11 10:22:12 +0000411 void addTokenToState(bool Newline, bool DryRun, LineState &State) {
Daniel Jasper399d24b2013-01-09 07:06:56 +0000412 const AnnotatedToken &Current = *State.NextToken;
413 const AnnotatedToken &Previous = *State.NextToken->Parent;
Daniel Jasper337816e2013-01-11 10:22:12 +0000414 assert(State.Stack.size());
Daniel Jasperf7935112012-12-03 18:12:45 +0000415
Daniel Jasper4b866272013-02-01 11:00:45 +0000416 if (Current.Type == TT_ImplicitStringLiteral) {
417 State.Column += State.NextToken->FormatTok.WhiteSpaceLength +
418 State.NextToken->FormatTok.TokenLength;
419 if (State.NextToken->Children.empty())
420 State.NextToken = NULL;
421 else
422 State.NextToken = &State.NextToken->Children[0];
423 return;
424 }
425
Daniel Jasperf7935112012-12-03 18:12:45 +0000426 if (Newline) {
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000427 unsigned WhitespaceStartColumn = State.Column;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000428 if (Current.is(tok::r_brace)) {
429 State.Column = Line.Level * 2;
Daniel Jasper399d24b2013-01-09 07:06:56 +0000430 } else if (Current.is(tok::string_literal) &&
431 Previous.is(tok::string_literal)) {
432 State.Column = State.Column - Previous.FormatTok.TokenLength;
433 } else if (Current.is(tok::lessless) &&
Daniel Jasper400adc62013-02-08 15:28:42 +0000434 State.Stack.back().FirstLessLess != 0) {
435 State.Column = State.Stack.back().FirstLessLess;
436 } else if (State.ParenLevel != 0 &&
Daniel Jasper4ad42352013-01-28 07:43:15 +0000437 (Previous.is(tok::equal) || Previous.is(tok::coloncolon) ||
Daniel Jasperca6623b2013-01-28 12:45:14 +0000438 Current.is(tok::period) || Current.is(tok::arrow) ||
439 Current.is(tok::question))) {
Daniel Jasper399d24b2013-01-09 07:06:56 +0000440 // Indent and extra 4 spaces after if we know the current expression is
441 // continued. Don't do that on the top level, as we already indent 4
442 // there.
Daniel Jasperca6623b2013-01-28 12:45:14 +0000443 State.Column = std::max(State.Stack.back().LastSpace,
444 State.Stack.back().Indent) + 4;
445 } else if (Current.Type == TT_ConditionalExpr) {
446 State.Column = State.Stack.back().QuestionColumn;
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000447 } else if (Previous.is(tok::comma) && State.VariablePos != 0 &&
Daniel Jasper400adc62013-02-08 15:28:42 +0000448 ((RootToken.is(tok::kw_for) && State.ParenLevel == 1) ||
449 State.ParenLevel == 0)) {
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000450 State.Column = State.VariablePos;
Daniel Jasperd2639ef2013-01-28 15:16:31 +0000451 } else if (State.NextToken->Parent->ClosesTemplateDeclaration ||
452 Current.Type == TT_StartOfName) {
Daniel Jasper400adc62013-02-08 15:28:42 +0000453 State.Column = State.Stack.back().Indent - 4;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000454 } else if (Current.Type == TT_ObjCSelectorName) {
455 if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) {
456 State.Column =
457 State.Stack.back().ColonPos - Current.FormatTok.TokenLength;
458 } else {
459 State.Column = State.Stack.back().Indent;
460 State.Stack.back().ColonPos =
461 State.Column + Current.FormatTok.TokenLength;
462 }
463 } else if (Previous.Type == TT_ObjCMethodExpr) {
464 State.Column = State.Stack.back().Indent + 4;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000465 } else {
Daniel Jasper400adc62013-02-08 15:28:42 +0000466 State.Column = State.Stack.back().Indent;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000467 }
468
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000469 if (Previous.is(tok::comma) && !State.Stack.back().AvoidBinPacking)
Daniel Jasperacc33662013-02-08 08:22:00 +0000470 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000471
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000472 if (RootToken.is(tok::kw_for))
Daniel Jasper399d24b2013-01-09 07:06:56 +0000473 State.LineContainsContinuedForLoopSection = Previous.isNot(tok::semi);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000474
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000475 if (!DryRun) {
476 if (!Line.InPPDirective)
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000477 Whitespaces.replaceWhitespace(Current, 1, State.Column,
478 WhitespaceStartColumn, Style);
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000479 else
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000480 Whitespaces.replacePPWhitespace(Current, 1, State.Column,
481 WhitespaceStartColumn, Style);
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000482 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000483
Daniel Jasper400adc62013-02-08 15:28:42 +0000484 State.Stack.back().LastSpace = State.Column;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000485 if (Current.is(tok::colon) && Current.Type != TT_ConditionalExpr)
Daniel Jasper400adc62013-02-08 15:28:42 +0000486 State.Stack.back().Indent += 2;
Daniel Jasperf7935112012-12-03 18:12:45 +0000487 } else {
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000488 if (Current.is(tok::equal) &&
Daniel Jasper400adc62013-02-08 15:28:42 +0000489 (RootToken.is(tok::kw_for) || State.ParenLevel == 0))
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000490 State.VariablePos = State.Column - Previous.FormatTok.TokenLength;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000491
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000492 unsigned Spaces = State.NextToken->SpaceRequiredBefore ? 1 : 0;
493 if (State.NextToken->Type == TT_LineComment)
Daniel Jasper5ad1e192013-01-07 11:09:06 +0000494 Spaces = Style.SpacesBeforeTrailingComments;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000495
Daniel Jasperf7935112012-12-03 18:12:45 +0000496 if (!DryRun)
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000497 Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column, Style);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000498
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000499 if (Current.Type == TT_ObjCSelectorName &&
500 State.Stack.back().ColonPos == 0) {
501 if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
502 State.Column + Spaces + Current.FormatTok.TokenLength)
503 State.Stack.back().ColonPos =
504 State.Stack.back().Indent + Current.LongestObjCSelectorName;
505 else
506 State.Stack.back().ColonPos =
Daniel Jasperc485b4e2013-02-06 16:00:26 +0000507 State.Column + Spaces + Current.FormatTok.TokenLength;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000508 }
509
Daniel Jasperddaa9be2013-01-29 19:41:55 +0000510 if (Current.Type != TT_LineComment &&
511 (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
512 State.NextToken->Parent->Type == TT_TemplateOpener))
Daniel Jasper400adc62013-02-08 15:28:42 +0000513 State.Stack.back().Indent = State.Column + Spaces;
Daniel Jasper14e40ec2013-02-04 08:34:57 +0000514 if (Previous.is(tok::comma) && !isTrailingComment(Current))
Daniel Jasper400adc62013-02-08 15:28:42 +0000515 State.Stack.back().HasMultiParameterLine = true;
Daniel Jasper9278eb92013-01-16 14:59:02 +0000516
Daniel Jaspere9de2602012-12-06 09:56:08 +0000517 State.Column += Spaces;
Daniel Jasper39e27382013-01-23 20:41:06 +0000518 if (Current.is(tok::l_paren) && Previous.is(tok::kw_if))
519 // Treat the condition inside an if as if it was a second function
520 // parameter, i.e. let nested calls have an indent of 4.
521 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
Daniel Jasper400adc62013-02-08 15:28:42 +0000522 else if (Previous.is(tok::comma) && State.ParenLevel != 0)
Daniel Jasper39e27382013-01-23 20:41:06 +0000523 // Top-level spaces are exempt as that mostly leads to better results.
524 State.Stack.back().LastSpace = State.Column;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000525 else if ((Previous.Type == TT_BinaryOperator ||
Daniel Jasper65585ed2013-01-28 13:31:35 +0000526 Previous.Type == TT_ConditionalExpr ||
527 Previous.Type == TT_CtorInitializerColon) &&
Daniel Jasper20b09ef2013-01-28 09:35:24 +0000528 getPrecedence(Previous) != prec::Assignment)
529 State.Stack.back().LastSpace = State.Column;
Daniel Jasper7b5773e92013-01-28 07:35:34 +0000530 else if (Previous.ParameterCount > 1 &&
531 (Previous.is(tok::l_paren) || Previous.is(tok::l_square) ||
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000532 Previous.is(tok::l_brace) ||
Daniel Jasper7b5773e92013-01-28 07:35:34 +0000533 Previous.Type == TT_TemplateOpener))
534 // If this function has multiple parameters, indent nested calls from
535 // the start of the first parameter.
536 State.Stack.back().LastSpace = State.Column;
Daniel Jasperf7935112012-12-03 18:12:45 +0000537 }
Daniel Jasper9278eb92013-01-16 14:59:02 +0000538
539 // If we break after an {, we should also break before the corresponding }.
540 if (Newline && Previous.is(tok::l_brace))
Daniel Jasper337816e2013-01-11 10:22:12 +0000541 State.Stack.back().BreakBeforeClosingBrace = true;
Daniel Jasper9278eb92013-01-16 14:59:02 +0000542
Daniel Jasperf7f13c02013-02-04 07:30:30 +0000543 if (State.Stack.back().AvoidBinPacking && Newline &&
Daniel Jasper400adc62013-02-08 15:28:42 +0000544 (Line.First.isNot(tok::kw_for) || State.ParenLevel != 1)) {
Daniel Jaspere941b162013-01-23 10:08:28 +0000545 // If we are breaking after '(', '{', '<', this is not bin packing unless
Daniel Jasperf7db4332013-01-29 16:03:49 +0000546 // AllowAllParametersOfDeclarationOnNextLine is false.
Daniel Jaspere941b162013-01-23 10:08:28 +0000547 if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace) &&
548 Previous.Type != TT_TemplateOpener) ||
Daniel Jasperf7db4332013-01-29 16:03:49 +0000549 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
550 Line.MustBeDeclaration))
Daniel Jasperacc33662013-02-08 08:22:00 +0000551 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000552
Daniel Jaspere941b162013-01-23 10:08:28 +0000553 // Any break on this level means that the parent level has been broken
554 // and we need to avoid bin packing there.
555 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
Daniel Jasperf7f13c02013-02-04 07:30:30 +0000556 if (Line.First.isNot(tok::kw_for) || i != 1)
Daniel Jasperacc33662013-02-08 08:22:00 +0000557 State.Stack[i].BreakBeforeParameter = true;
Daniel Jaspere941b162013-01-23 10:08:28 +0000558 }
559 }
Daniel Jasper9278eb92013-01-16 14:59:02 +0000560
Manuel Klimeka54d1a92013-01-14 16:41:43 +0000561 moveStateToNextToken(State);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000562 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000563
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000564 /// \brief Mark the next token as consumed in \p State and modify its stacks
565 /// accordingly.
Daniel Jasper337816e2013-01-11 10:22:12 +0000566 void moveStateToNextToken(LineState &State) {
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000567 const AnnotatedToken &Current = *State.NextToken;
Daniel Jasper337816e2013-01-11 10:22:12 +0000568 assert(State.Stack.size());
Daniel Jaspere9de2602012-12-06 09:56:08 +0000569
Daniel Jasper337816e2013-01-11 10:22:12 +0000570 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
571 State.Stack.back().FirstLessLess = State.Column;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000572 if (Current.is(tok::question))
573 State.Stack.back().QuestionColumn = State.Column;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000574 if (Current.is(tok::l_brace) && Current.MatchingParen != NULL &&
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000575 !Current.MatchingParen->MustBreakBefore) {
Daniel Jasperacc33662013-02-08 08:22:00 +0000576 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
577 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000578 }
Daniel Jaspere9de2602012-12-06 09:56:08 +0000579
Daniel Jasper400adc62013-02-08 15:28:42 +0000580 // Insert scopes created by fake parenthesis.
581 for (unsigned i = 0, e = Current.FakeLParens; i != e; ++i) {
582 ParenState NewParenState = State.Stack.back();
583 NewParenState.Indent = std::max(State.Column, State.Stack.back().Indent);
584 State.Stack.push_back(NewParenState);
585 }
586
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000587 // If we encounter an opening (, [, { or <, we add a level to our stacks to
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000588 // prepare for the following tokens.
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000589 if (Current.is(tok::l_paren) || Current.is(tok::l_square) ||
590 Current.is(tok::l_brace) ||
591 State.NextToken->Type == TT_TemplateOpener) {
Daniel Jasper337816e2013-01-11 10:22:12 +0000592 unsigned NewIndent;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000593 bool AvoidBinPacking;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000594 if (Current.is(tok::l_brace)) {
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000595 NewIndent = 2 + State.Stack.back().LastSpace;
596 AvoidBinPacking = false;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000597 } else {
Daniel Jasper337816e2013-01-11 10:22:12 +0000598 NewIndent = 4 + State.Stack.back().LastSpace;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000599 AvoidBinPacking = !Style.BinPackParameters;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000600 }
Daniel Jasperb9ebd5d2013-02-05 09:41:21 +0000601 State.Stack.push_back(
602 ParenState(NewIndent, State.Stack.back().LastSpace, AvoidBinPacking,
603 State.Stack.back().HasMultiParameterLine));
Daniel Jasper400adc62013-02-08 15:28:42 +0000604 ++State.ParenLevel;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000605 }
606
Daniel Jasperacc33662013-02-08 08:22:00 +0000607 // If this '[' opens an ObjC call, determine whether all parameters fit into
608 // one line and put one per line if they don't.
609 if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
610 Current.MatchingParen != NULL) {
611 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
612 State.Stack.back().BreakBeforeParameter = true;
613 }
614
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000615 // If we encounter a closing ), ], } or >, we can remove a level from our
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000616 // stacks.
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000617 if (Current.is(tok::r_paren) || Current.is(tok::r_square) ||
618 (Current.is(tok::r_brace) && State.NextToken != &RootToken) ||
619 State.NextToken->Type == TT_TemplateCloser) {
Daniel Jasper337816e2013-01-11 10:22:12 +0000620 State.Stack.pop_back();
Daniel Jasper400adc62013-02-08 15:28:42 +0000621 --State.ParenLevel;
622 }
623
624 // Remove scopes created by fake parenthesis.
625 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
626 State.Stack.pop_back();
Daniel Jasperf7935112012-12-03 18:12:45 +0000627 }
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000628
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000629 if (State.NextToken->Children.empty())
630 State.NextToken = NULL;
631 else
632 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000633
634 State.Column += Current.FormatTok.TokenLength;
Daniel Jasperf7935112012-12-03 18:12:45 +0000635 }
636
Daniel Jasper2df93312013-01-09 10:16:05 +0000637 unsigned getColumnLimit() {
638 return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0);
639 }
640
Daniel Jasper4b866272013-02-01 11:00:45 +0000641 /// \brief An edge in the solution space starting from the \c LineState and
642 /// inserting a newline dependent on the \c bool.
643 typedef std::pair<bool, const LineState *> Edge;
644
645 /// \brief An item in the prioritized BFS search queue. The \c LineState was
646 /// reached using the \c Edge.
647 typedef std::pair<LineState, Edge> QueueItem;
648
649 /// \brief Analyze the entire solution space starting from \p InitialState.
Daniel Jasperf7935112012-12-03 18:12:45 +0000650 ///
Daniel Jasper4b866272013-02-01 11:00:45 +0000651 /// This implements a variant of Dijkstra's algorithm on the graph that spans
652 /// the solution space (\c LineStates are the nodes). The algorithm tries to
653 /// find the shortest path (the one with lowest penalty) from \p InitialState
654 /// to a state where all tokens are placed.
655 unsigned analyzeSolutionSpace(const LineState &InitialState) {
656 // Insert start element into queue.
657 std::multimap<unsigned, QueueItem> Queue;
658 Queue.insert(std::pair<unsigned, QueueItem>(
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000659 0, QueueItem(InitialState, Edge(false, (const LineState *)0))));
Daniel Jasper4b866272013-02-01 11:00:45 +0000660 std::map<LineState, Edge> Seen;
661
662 // While not empty, take first element and follow edges.
663 while (!Queue.empty()) {
664 unsigned Penalty = Queue.begin()->first;
665 QueueItem Item = Queue.begin()->second;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000666 if (Item.first.NextToken == NULL) {
667 DEBUG(llvm::errs() << "\n---\nPenalty for line: " << Penalty << "\n");
Daniel Jasper4b866272013-02-01 11:00:45 +0000668 break;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000669 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000670 Queue.erase(Queue.begin());
671
672 if (Seen.find(Item.first) != Seen.end())
673 continue; // State already examined with lower penalty.
674
675 const LineState &SavedState = Seen.insert(std::pair<LineState, Edge>(
676 Item.first,
677 Edge(Item.second.first, Item.second.second))).first->first;
678
679 addNextStateToQueue(SavedState, Penalty, /*NewLine=*/ false, Queue);
680 addNextStateToQueue(SavedState, Penalty, /*NewLine=*/ true, Queue);
681 }
682
683 if (Queue.empty())
684 // We were unable to find a solution, do nothing.
685 // FIXME: Add diagnostic?
Daniel Jasperf7935112012-12-03 18:12:45 +0000686 return 0;
687
Daniel Jasper4b866272013-02-01 11:00:45 +0000688 // Reconstruct the solution.
689 // FIXME: Add debugging output.
690 Edge *CurrentEdge = &Queue.begin()->second.second;
691 while (CurrentEdge->second != NULL) {
692 LineState CurrentState = *CurrentEdge->second;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000693 if (CurrentEdge->first) {
694 DEBUG(llvm::errs() << "Penalty for splitting before "
695 << CurrentState.NextToken->FormatTok.Tok.getName()
696 << ": " << CurrentState.NextToken->SplitPenalty
697 << "\n");
698 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000699 addTokenToState(CurrentEdge->first, false, CurrentState);
700 CurrentEdge = &Seen[*CurrentEdge->second];
701 }
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000702 DEBUG(llvm::errs() << "---\n");
Daniel Jasperf7935112012-12-03 18:12:45 +0000703
Daniel Jasper4b866272013-02-01 11:00:45 +0000704 // Return the column after the last token of the solution.
705 return Queue.begin()->second.first.Column;
706 }
707
708 /// \brief Add the following state to the analysis queue \p Queue.
709 ///
710 /// Assume the current state is \p OldState and has been reached with a
711 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
712 void addNextStateToQueue(const LineState &OldState, unsigned Penalty,
713 bool NewLine,
714 std::multimap<unsigned, QueueItem> &Queue) {
715 if (NewLine && !canBreak(OldState))
716 return;
717 if (!NewLine && mustBreak(OldState))
718 return;
719 LineState State(OldState);
Daniel Jasper20b09ef2013-01-28 09:35:24 +0000720 if (NewLine)
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000721 Penalty += State.NextToken->SplitPenalty;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000722 addTokenToState(NewLine, true, State);
Daniel Jasper2df93312013-01-09 10:16:05 +0000723 if (State.Column > getColumnLimit()) {
724 unsigned ExcessCharacters = State.Column - getColumnLimit();
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000725 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
Daniel Jasper2df93312013-01-09 10:16:05 +0000726 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000727 Queue.insert(std::pair<unsigned, QueueItem>(
728 Penalty, QueueItem(State, Edge(NewLine, &OldState))));
729 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000730
Daniel Jasper4b866272013-02-01 11:00:45 +0000731 /// \brief Returns \c true, if a line break after \p State is allowed.
732 bool canBreak(const LineState &State) {
733 if (!State.NextToken->CanBreakBefore &&
734 !(State.NextToken->is(tok::r_brace) &&
735 State.Stack.back().BreakBeforeClosingBrace))
736 return false;
737 // Trying to insert a parameter on a new line if there are already more than
738 // one parameter on the current line is bin packing.
Daniel Jasperb9ebd5d2013-02-05 09:41:21 +0000739 if (State.Stack.back().HasMultiParameterLine &&
Daniel Jasper4b866272013-02-01 11:00:45 +0000740 State.Stack.back().AvoidBinPacking)
741 return false;
742 return true;
743 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000744
Daniel Jasper4b866272013-02-01 11:00:45 +0000745 /// \brief Returns \c true, if a line break after \p State is mandatory.
746 bool mustBreak(const LineState &State) {
747 if (State.NextToken->MustBreakBefore)
748 return true;
749 if (State.NextToken->is(tok::r_brace) &&
750 State.Stack.back().BreakBeforeClosingBrace)
751 return true;
752 if (State.NextToken->Parent->is(tok::semi) &&
753 State.LineContainsContinuedForLoopSection)
754 return true;
755 if (State.NextToken->Parent->is(tok::comma) &&
Daniel Jasperacc33662013-02-08 08:22:00 +0000756 State.Stack.back().BreakBeforeParameter &&
Daniel Jasper14e40ec2013-02-04 08:34:57 +0000757 !isTrailingComment(*State.NextToken))
Daniel Jasper4b866272013-02-01 11:00:45 +0000758 return true;
Daniel Jasperacc33662013-02-08 08:22:00 +0000759 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
760 // out whether it is the first parameter. Clean this up.
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000761 if (State.NextToken->Type == TT_ObjCSelectorName &&
Daniel Jasperacc33662013-02-08 08:22:00 +0000762 State.NextToken->LongestObjCSelectorName == 0 &&
763 State.Stack.back().BreakBeforeParameter)
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000764 return true;
Daniel Jasper4b866272013-02-01 11:00:45 +0000765 if ((State.NextToken->Type == TT_CtorInitializerColon ||
766 (State.NextToken->Parent->ClosesTemplateDeclaration &&
Daniel Jasper400adc62013-02-08 15:28:42 +0000767 State.ParenLevel == 0)))
Daniel Jasper4b866272013-02-01 11:00:45 +0000768 return true;
769 return false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000770 }
771
Daniel Jasperf7935112012-12-03 18:12:45 +0000772 FormatStyle Style;
773 SourceManager &SourceMgr;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000774 const AnnotatedLine &Line;
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000775 const unsigned FirstIndent;
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000776 const AnnotatedToken &RootToken;
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000777 WhitespaceManager &Whitespaces;
Daniel Jasperf7935112012-12-03 18:12:45 +0000778};
779
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000780class LexerBasedFormatTokenSource : public FormatTokenSource {
781public:
782 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000783 : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000784 IdentTable(Lex.getLangOpts()) {
785 Lex.SetKeepWhitespaceMode(true);
786 }
787
788 virtual FormatToken getNextToken() {
789 if (GreaterStashed) {
790 FormatTok.NewlinesBefore = 0;
791 FormatTok.WhiteSpaceStart =
792 FormatTok.Tok.getLocation().getLocWithOffset(1);
793 FormatTok.WhiteSpaceLength = 0;
794 GreaterStashed = false;
795 return FormatTok;
796 }
797
798 FormatTok = FormatToken();
799 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimekef920692013-01-07 07:56:50 +0000800 StringRef Text = rawTokenText(FormatTok.Tok);
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000801 FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
Manuel Klimek52d0fd82013-01-05 22:56:06 +0000802 if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0)
803 FormatTok.IsFirst = true;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000804
805 // Consume and record whitespace until we find a significant token.
806 while (FormatTok.Tok.is(tok::unknown)) {
Manuel Klimek0c137952013-02-11 12:33:24 +0000807 unsigned Newlines = Text.count('\n');
808 unsigned EscapedNewlines = Text.count("\\\n");
809 FormatTok.NewlinesBefore += Newlines;
810 FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000811 FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
812
813 if (FormatTok.Tok.is(tok::eof))
814 return FormatTok;
815 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimekef920692013-01-07 07:56:50 +0000816 Text = rawTokenText(FormatTok.Tok);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000817 }
Manuel Klimekef920692013-01-07 07:56:50 +0000818
819 // Now FormatTok is the next non-whitespace token.
820 FormatTok.TokenLength = Text.size();
821
Manuel Klimek1abf7892013-01-04 23:34:14 +0000822 // In case the token starts with escaped newlines, we want to
823 // take them into account as whitespace - this pattern is quite frequent
824 // in macro definitions.
825 // FIXME: What do we want to do with other escaped spaces, and escaped
826 // spaces or newlines in the middle of tokens?
827 // FIXME: Add a more explicit test.
828 unsigned i = 0;
Daniel Jasperda16db32013-01-07 10:48:50 +0000829 while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000830 // FIXME: ++FormatTok.NewlinesBefore is missing...
Manuel Klimek1abf7892013-01-04 23:34:14 +0000831 FormatTok.WhiteSpaceLength += 2;
Manuel Klimekef920692013-01-07 07:56:50 +0000832 FormatTok.TokenLength -= 2;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000833 i += 2;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000834 }
835
836 if (FormatTok.Tok.is(tok::raw_identifier)) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000837 IdentifierInfo &Info = IdentTable.get(Text);
Daniel Jasper050948a52012-12-21 17:58:39 +0000838 FormatTok.Tok.setIdentifierInfo(&Info);
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000839 FormatTok.Tok.setKind(Info.getTokenID());
840 }
841
842 if (FormatTok.Tok.is(tok::greatergreater)) {
843 FormatTok.Tok.setKind(tok::greater);
844 GreaterStashed = true;
845 }
846
847 return FormatTok;
848 }
849
850private:
851 FormatToken FormatTok;
852 bool GreaterStashed;
853 Lexer &Lex;
854 SourceManager &SourceMgr;
855 IdentifierTable IdentTable;
856
857 /// Returns the text of \c FormatTok.
Manuel Klimekef920692013-01-07 07:56:50 +0000858 StringRef rawTokenText(Token &Tok) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000859 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
860 Tok.getLength());
861 }
862};
863
Daniel Jasperf7935112012-12-03 18:12:45 +0000864class Formatter : public UnwrappedLineConsumer {
865public:
Daniel Jasper25837aa2013-01-14 14:14:23 +0000866 Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex,
867 SourceManager &SourceMgr,
Daniel Jasperf7935112012-12-03 18:12:45 +0000868 const std::vector<CharSourceRange> &Ranges)
Alexander Kornienko5b7157a2013-01-10 15:05:09 +0000869 : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Daniel Jasperbbc84152013-01-29 11:27:30 +0000870 Whitespaces(SourceMgr), Ranges(Ranges) {
871 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000872
Daniel Jasperfd8c4b12013-01-11 14:23:32 +0000873 virtual ~Formatter() {}
Daniel Jasper61bd3a12012-12-04 21:05:31 +0000874
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000875 void deriveLocalStyle() {
876 unsigned CountBoundToVariable = 0;
877 unsigned CountBoundToType = 0;
878 bool HasCpp03IncompatibleFormat = false;
879 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
880 if (AnnotatedLines[i].First.Children.empty())
881 continue;
882 AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0];
883 while (!Tok->Children.empty()) {
884 if (Tok->Type == TT_PointerOrReference) {
885 bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0;
886 bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0;
887 if (SpacesBefore && !SpacesAfter)
888 ++CountBoundToVariable;
889 else if (!SpacesBefore && SpacesAfter)
890 ++CountBoundToType;
891 }
892
Daniel Jasper400adc62013-02-08 15:28:42 +0000893 if (Tok->Type == TT_TemplateCloser &&
894 Tok->Parent->Type == TT_TemplateCloser &&
895 Tok->FormatTok.WhiteSpaceLength == 0)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000896 HasCpp03IncompatibleFormat = true;
897 Tok = &Tok->Children[0];
898 }
899 }
900 if (Style.DerivePointerBinding) {
901 if (CountBoundToType > CountBoundToVariable)
902 Style.PointerBindsToType = true;
903 else if (CountBoundToType < CountBoundToVariable)
904 Style.PointerBindsToType = false;
905 }
906 if (Style.Standard == FormatStyle::LS_Auto) {
907 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
908 : FormatStyle::LS_Cpp03;
909 }
910 }
911
Daniel Jasperf7935112012-12-03 18:12:45 +0000912 tooling::Replacements format() {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000913 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
Alexander Kornienko5b7157a2013-01-10 15:05:09 +0000914 UnwrappedLineParser Parser(Diag, Style, Tokens, *this);
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000915 StructuralError = Parser.parse();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000916 unsigned PreviousEndOfLineColumn = 0;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000917 TokenAnnotator Annotator(Style, SourceMgr, Lex);
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000918 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000919 Annotator.annotate(AnnotatedLines[i]);
920 }
921 deriveLocalStyle();
922 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
923 Annotator.calculateFormattingInformation(AnnotatedLines[i]);
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000924 }
Manuel Klimekb95f5452013-02-08 17:38:27 +0000925 std::vector<int> IndentForLevel;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000926 for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
927 E = AnnotatedLines.end();
Manuel Klimek51bd6ec2013-01-10 19:49:59 +0000928 I != E; ++I) {
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000929 const AnnotatedLine &TheLine = *I;
Manuel Klimekb95f5452013-02-08 17:38:27 +0000930 int Offset = GetIndentOffset(TheLine.First);
931 while (IndentForLevel.size() <= TheLine.Level)
932 IndentForLevel.push_back(-1);
933 IndentForLevel.resize(TheLine.Level + 1);
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000934 if (touchesRanges(TheLine) && TheLine.Type != LT_Invalid) {
Manuel Klimekb95f5452013-02-08 17:38:27 +0000935 unsigned LevelIndent = GetIndent(IndentForLevel, TheLine.Level);
936 unsigned Indent = LevelIndent;
937 if (static_cast<int>(Indent) + Offset >= 0)
938 Indent += Offset;
939 if (!TheLine.First.FormatTok.WhiteSpaceStart.isValid() ||
940 StructuralError) {
941 Indent = LevelIndent = SourceMgr.getSpellingColumnNumber(
942 TheLine.First.FormatTok.Tok.getLocation()) - 1;
943 } else {
944 formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
945 PreviousEndOfLineColumn);
946 }
Daniel Jaspera67a8f02013-01-16 10:41:46 +0000947 tryFitMultipleLinesInOne(Indent, I, E);
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000948 UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000949 TheLine.First, Whitespaces,
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000950 StructuralError);
Manuel Klimek51bd6ec2013-01-10 19:49:59 +0000951 PreviousEndOfLineColumn = Formatter.format();
Manuel Klimekb95f5452013-02-08 17:38:27 +0000952 IndentForLevel[TheLine.Level] = LevelIndent;
Manuel Klimek51bd6ec2013-01-10 19:49:59 +0000953 } else {
954 // If we did not reformat this unwrapped line, the column at the end of
955 // the last token is unchanged - thus, we can calculate the end of the
Manuel Klimekb95f5452013-02-08 17:38:27 +0000956 // last token.
Manuel Klimek51bd6ec2013-01-10 19:49:59 +0000957 PreviousEndOfLineColumn =
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000958 SourceMgr.getSpellingColumnNumber(
959 TheLine.Last->FormatTok.Tok.getLocation()) +
960 Lex.MeasureTokenLength(TheLine.Last->FormatTok.Tok.getLocation(),
Daniel Jasperbbc84152013-01-29 11:27:30 +0000961 SourceMgr, Lex.getLangOpts()) - 1;
Manuel Klimekb95f5452013-02-08 17:38:27 +0000962 unsigned Indent = SourceMgr.getSpellingColumnNumber(
963 TheLine.First.FormatTok.Tok.getLocation()) - 1;
964 unsigned LevelIndent = Indent;
965 if (static_cast<int>(LevelIndent) - Offset >= 0)
966 LevelIndent -= Offset;
967 IndentForLevel[TheLine.Level] = LevelIndent;
Manuel Klimek51bd6ec2013-01-10 19:49:59 +0000968 }
969 }
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000970 return Whitespaces.generateReplacements();
Daniel Jasperf7935112012-12-03 18:12:45 +0000971 }
972
973private:
Manuel Klimekb95f5452013-02-08 17:38:27 +0000974 /// \brief Get the indent of \p Level from \p IndentForLevel.
975 ///
976 /// \p IndentForLevel must contain the indent for the level \c l
977 /// at \p IndentForLevel[l], or a value < 0 if the indent for
978 /// that level is unknown.
979 unsigned GetIndent(const std::vector<int> IndentForLevel,
980 unsigned Level) {
Manuel Klimekb95f5452013-02-08 17:38:27 +0000981 if (IndentForLevel[Level] != -1)
982 return IndentForLevel[Level];
Manuel Klimekd076dcd2013-02-08 19:53:32 +0000983 if (Level == 0)
984 return 0;
Manuel Klimekb95f5452013-02-08 17:38:27 +0000985 return GetIndent(IndentForLevel, Level - 1) + 2;
986 }
987
988 /// \brief Get the offset of the line relatively to the level.
989 ///
990 /// For example, 'public:' labels in classes are offset by 1 or 2
991 /// characters to the left from their level.
992 int GetIndentOffset(const AnnotatedToken &RootToken) {
993 bool IsAccessModifier = false;
994 if (RootToken.is(tok::kw_public) || RootToken.is(tok::kw_protected) ||
995 RootToken.is(tok::kw_private))
996 IsAccessModifier = true;
997 else if (RootToken.is(tok::at) && !RootToken.Children.empty() &&
998 (RootToken.Children[0].isObjCAtKeyword(tok::objc_public) ||
999 RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) ||
1000 RootToken.Children[0].isObjCAtKeyword(tok::objc_package) ||
1001 RootToken.Children[0].isObjCAtKeyword(tok::objc_private)))
1002 IsAccessModifier = true;
1003
1004 if (IsAccessModifier)
1005 return Style.AccessModifierOffset;
1006 return 0;
1007 }
1008
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001009 /// \brief Tries to merge lines into one.
1010 ///
1011 /// This will change \c Line and \c AnnotatedLine to contain the merged line,
1012 /// if possible; note that \c I will be incremented when lines are merged.
1013 ///
1014 /// Returns whether the resulting \c Line can fit in a single line.
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001015 void tryFitMultipleLinesInOne(unsigned Indent,
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001016 std::vector<AnnotatedLine>::iterator &I,
1017 std::vector<AnnotatedLine>::iterator E) {
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001018 unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent;
1019
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001020 // We can never merge stuff if there are trailing line comments.
1021 if (I->Last->Type == TT_LineComment)
1022 return;
1023
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001024 // Check whether the UnwrappedLine can be put onto a single line. If
1025 // so, this is bound to be the optimal solution (by definition) and we
1026 // don't need to analyze the entire solution space.
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001027 if (I->Last->TotalLength > Limit)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001028 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001029 Limit -= I->Last->TotalLength;
Daniel Jasperc36492b2013-01-16 07:02:34 +00001030
Daniel Jasperd41ee2d2013-01-21 14:18:28 +00001031 if (I + 1 == E || (I + 1)->Type == LT_Invalid)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001032 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001033
Daniel Jasper25837aa2013-01-14 14:14:23 +00001034 if (I->Last->is(tok::l_brace)) {
1035 tryMergeSimpleBlock(I, E, Limit);
1036 } else if (I->First.is(tok::kw_if)) {
1037 tryMergeSimpleIf(I, E, Limit);
Daniel Jasper39825ea2013-01-14 15:40:57 +00001038 } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline ||
1039 I->First.FormatTok.IsFirst)) {
1040 tryMergeSimplePPDirective(I, E, Limit);
Daniel Jasper25837aa2013-01-14 14:14:23 +00001041 }
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001042 return;
Daniel Jasper25837aa2013-01-14 14:14:23 +00001043 }
1044
Daniel Jasper39825ea2013-01-14 15:40:57 +00001045 void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
1046 std::vector<AnnotatedLine>::iterator E,
1047 unsigned Limit) {
1048 AnnotatedLine &Line = *I;
Daniel Jasper2ab0d012013-01-14 15:52:06 +00001049 if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline)
1050 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001051 if (I + 2 != E && (I + 2)->InPPDirective &&
1052 !(I + 2)->First.FormatTok.HasUnescapedNewline)
1053 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001054 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001055 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001056 join(Line, *(++I));
1057 }
1058
Daniel Jasper25837aa2013-01-14 14:14:23 +00001059 void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I,
1060 std::vector<AnnotatedLine>::iterator E,
1061 unsigned Limit) {
Daniel Jasper1b750ed2013-01-14 16:24:39 +00001062 if (!Style.AllowShortIfStatementsOnASingleLine)
1063 return;
Manuel Klimekda087612013-01-18 14:46:43 +00001064 if ((I + 1)->InPPDirective != I->InPPDirective ||
1065 ((I + 1)->InPPDirective &&
1066 (I + 1)->First.FormatTok.HasUnescapedNewline))
1067 return;
Daniel Jasper25837aa2013-01-14 14:14:23 +00001068 AnnotatedLine &Line = *I;
Daniel Jasperc36492b2013-01-16 07:02:34 +00001069 if (Line.Last->isNot(tok::r_paren))
1070 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001071 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jasper25837aa2013-01-14 14:14:23 +00001072 return;
1073 if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment)
1074 return;
1075 // Only inline simple if's (no nested if or else).
1076 if (I + 2 != E && (I + 2)->First.is(tok::kw_else))
1077 return;
1078 join(Line, *(++I));
1079 }
1080
1081 void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
Daniel Jasperbbc84152013-01-29 11:27:30 +00001082 std::vector<AnnotatedLine>::iterator E,
1083 unsigned Limit) {
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001084 // First, check that the current line allows merging. This is the case if
1085 // we're not in a control flow statement and the last token is an opening
1086 // brace.
Daniel Jasper25837aa2013-01-14 14:14:23 +00001087 AnnotatedLine &Line = *I;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001088 bool AllowedTokens =
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001089 Line.First.isNot(tok::kw_if) && Line.First.isNot(tok::kw_while) &&
1090 Line.First.isNot(tok::kw_do) && Line.First.isNot(tok::r_brace) &&
1091 Line.First.isNot(tok::kw_else) && Line.First.isNot(tok::kw_try) &&
1092 Line.First.isNot(tok::kw_catch) && Line.First.isNot(tok::kw_for) &&
Nico Webera21aaae2013-01-11 21:14:08 +00001093 // This gets rid of all ObjC @ keywords and methods.
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001094 Line.First.isNot(tok::at) && Line.First.isNot(tok::minus) &&
1095 Line.First.isNot(tok::plus);
Daniel Jasper25837aa2013-01-14 14:14:23 +00001096 if (!AllowedTokens)
1097 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001098
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001099 AnnotatedToken *Tok = &(I + 1)->First;
1100 if (Tok->Children.empty() && Tok->is(tok::r_brace) &&
1101 !Tok->MustBreakBefore && Tok->TotalLength <= Limit) {
1102 Tok->SpaceRequiredBefore = false;
1103 join(Line, *(I + 1));
1104 I += 1;
1105 } else {
1106 // Check that we still have three lines and they fit into the limit.
1107 if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
1108 !nextTwoLinesFitInto(I, Limit))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001109 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001110
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001111 // Second, check that the next line does not contain any braces - if it
1112 // does, readability declines when putting it into a single line.
1113 if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
1114 return;
1115 do {
1116 if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace))
1117 return;
1118 Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
1119 } while (Tok != NULL);
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001120
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001121 // Last, check that the third line contains a single closing brace.
1122 Tok = &(I + 2)->First;
1123 if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
1124 Tok->MustBreakBefore)
1125 return;
1126
1127 join(Line, *(I + 1));
1128 join(Line, *(I + 2));
1129 I += 2;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001130 }
Daniel Jasper25837aa2013-01-14 14:14:23 +00001131 }
1132
1133 bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
1134 unsigned Limit) {
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001135 return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
1136 Limit;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001137 }
1138
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001139 void join(AnnotatedLine &A, const AnnotatedLine &B) {
1140 A.Last->Children.push_back(B.First);
1141 while (!A.Last->Children.empty()) {
1142 A.Last->Children[0].Parent = A.Last;
1143 A.Last = &A.Last->Children[0];
1144 }
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001145 }
1146
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001147 bool touchesRanges(const AnnotatedLine &TheLine) {
1148 const FormatToken *First = &TheLine.First.FormatTok;
1149 const FormatToken *Last = &TheLine.Last->FormatTok;
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001150 CharSourceRange LineRange = CharSourceRange::getTokenRange(
Daniel Jasperbbc84152013-01-29 11:27:30 +00001151 First->Tok.getLocation(), Last->Tok.getLocation());
Daniel Jasperf7935112012-12-03 18:12:45 +00001152 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001153 if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
1154 Ranges[i].getBegin()) &&
1155 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1156 LineRange.getBegin()))
1157 return true;
Daniel Jasperf7935112012-12-03 18:12:45 +00001158 }
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001159 return false;
1160 }
1161
1162 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001163 AnnotatedLines.push_back(AnnotatedLine(TheLine));
Daniel Jasperf7935112012-12-03 18:12:45 +00001164 }
1165
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001166 /// \brief Add a new line and the required indent before the first Token
1167 /// of the \c UnwrappedLine if there was no structural parsing error.
1168 /// Returns the indent level of the \c UnwrappedLine.
Manuel Klimekb95f5452013-02-08 17:38:27 +00001169 void formatFirstToken(const AnnotatedToken &RootToken, unsigned Indent,
1170 bool InPPDirective, unsigned PreviousEndOfLineColumn) {
Daniel Jasperfd8c4b12013-01-11 14:23:32 +00001171 const FormatToken &Tok = RootToken.FormatTok;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001172
Daniel Jasperbbc84152013-01-29 11:27:30 +00001173 unsigned Newlines =
1174 std::min(Tok.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001175 if (Newlines == 0 && !Tok.IsFirst)
1176 Newlines = 1;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001177
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001178 if (!InPPDirective || Tok.HasUnescapedNewline) {
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001179 Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0, Style);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001180 } else {
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001181 Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent,
1182 PreviousEndOfLineColumn, Style);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001183 }
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001184 }
1185
Alexander Kornienko116ba682013-01-14 11:34:14 +00001186 DiagnosticsEngine &Diag;
Daniel Jasperf7935112012-12-03 18:12:45 +00001187 FormatStyle Style;
1188 Lexer &Lex;
1189 SourceManager &SourceMgr;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001190 WhitespaceManager Whitespaces;
Daniel Jasperf7935112012-12-03 18:12:45 +00001191 std::vector<CharSourceRange> Ranges;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001192 std::vector<AnnotatedLine> AnnotatedLines;
Alexander Kornienko870f9eb2012-12-04 17:27:50 +00001193 bool StructuralError;
Daniel Jasperf7935112012-12-03 18:12:45 +00001194};
1195
Daniel Jasperbbc84152013-01-29 11:27:30 +00001196tooling::Replacements
1197reformat(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
1198 std::vector<CharSourceRange> Ranges, DiagnosticConsumer *DiagClient) {
Alexander Kornienko5b7157a2013-01-10 15:05:09 +00001199 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Alexander Kornienko116ba682013-01-14 11:34:14 +00001200 OwningPtr<DiagnosticConsumer> DiagPrinter;
1201 if (DiagClient == 0) {
1202 DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts));
1203 DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
1204 DiagClient = DiagPrinter.get();
1205 }
Alexander Kornienko5b7157a2013-01-10 15:05:09 +00001206 DiagnosticsEngine Diagnostics(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001207 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
Alexander Kornienko116ba682013-01-14 11:34:14 +00001208 DiagClient, false);
Alexander Kornienko5b7157a2013-01-10 15:05:09 +00001209 Diagnostics.setSourceManager(&SourceMgr);
1210 Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
Daniel Jasperf7935112012-12-03 18:12:45 +00001211 return formatter.format();
1212}
1213
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001214LangOptions getFormattingLangOpts() {
1215 LangOptions LangOpts;
1216 LangOpts.CPlusPlus = 1;
1217 LangOpts.CPlusPlus11 = 1;
1218 LangOpts.Bool = 1;
1219 LangOpts.ObjC1 = 1;
1220 LangOpts.ObjC2 = 1;
1221 return LangOpts;
1222}
1223
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001224} // namespace format
1225} // namespace clang