blob: ab71d593599b03ab98bf497c1f8dc851799d595b [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 Jaspereef30492013-02-11 12:36:37 +0000492 unsigned Spaces = State.NextToken->SpacesRequiredBefore;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000493
Daniel Jasperf7935112012-12-03 18:12:45 +0000494 if (!DryRun)
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000495 Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column, Style);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000496
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000497 if (Current.Type == TT_ObjCSelectorName &&
498 State.Stack.back().ColonPos == 0) {
499 if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
500 State.Column + Spaces + Current.FormatTok.TokenLength)
501 State.Stack.back().ColonPos =
502 State.Stack.back().Indent + Current.LongestObjCSelectorName;
503 else
504 State.Stack.back().ColonPos =
Daniel Jasperc485b4e2013-02-06 16:00:26 +0000505 State.Column + Spaces + Current.FormatTok.TokenLength;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000506 }
507
Daniel Jasperddaa9be2013-01-29 19:41:55 +0000508 if (Current.Type != TT_LineComment &&
509 (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
510 State.NextToken->Parent->Type == TT_TemplateOpener))
Daniel Jasper400adc62013-02-08 15:28:42 +0000511 State.Stack.back().Indent = State.Column + Spaces;
Daniel Jasper14e40ec2013-02-04 08:34:57 +0000512 if (Previous.is(tok::comma) && !isTrailingComment(Current))
Daniel Jasper400adc62013-02-08 15:28:42 +0000513 State.Stack.back().HasMultiParameterLine = true;
Daniel Jasper9278eb92013-01-16 14:59:02 +0000514
Daniel Jaspere9de2602012-12-06 09:56:08 +0000515 State.Column += Spaces;
Daniel Jasper39e27382013-01-23 20:41:06 +0000516 if (Current.is(tok::l_paren) && Previous.is(tok::kw_if))
517 // Treat the condition inside an if as if it was a second function
518 // parameter, i.e. let nested calls have an indent of 4.
519 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
Daniel Jasper400adc62013-02-08 15:28:42 +0000520 else if (Previous.is(tok::comma) && State.ParenLevel != 0)
Daniel Jasper39e27382013-01-23 20:41:06 +0000521 // Top-level spaces are exempt as that mostly leads to better results.
522 State.Stack.back().LastSpace = State.Column;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000523 else if ((Previous.Type == TT_BinaryOperator ||
Daniel Jasper65585ed2013-01-28 13:31:35 +0000524 Previous.Type == TT_ConditionalExpr ||
525 Previous.Type == TT_CtorInitializerColon) &&
Daniel Jasper20b09ef2013-01-28 09:35:24 +0000526 getPrecedence(Previous) != prec::Assignment)
527 State.Stack.back().LastSpace = State.Column;
Daniel Jasper7b5773e92013-01-28 07:35:34 +0000528 else if (Previous.ParameterCount > 1 &&
529 (Previous.is(tok::l_paren) || Previous.is(tok::l_square) ||
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000530 Previous.is(tok::l_brace) ||
Daniel Jasper7b5773e92013-01-28 07:35:34 +0000531 Previous.Type == TT_TemplateOpener))
532 // If this function has multiple parameters, indent nested calls from
533 // the start of the first parameter.
534 State.Stack.back().LastSpace = State.Column;
Daniel Jasperf7935112012-12-03 18:12:45 +0000535 }
Daniel Jasper9278eb92013-01-16 14:59:02 +0000536
537 // If we break after an {, we should also break before the corresponding }.
538 if (Newline && Previous.is(tok::l_brace))
Daniel Jasper337816e2013-01-11 10:22:12 +0000539 State.Stack.back().BreakBeforeClosingBrace = true;
Daniel Jasper9278eb92013-01-16 14:59:02 +0000540
Daniel Jasperf7f13c02013-02-04 07:30:30 +0000541 if (State.Stack.back().AvoidBinPacking && Newline &&
Daniel Jasper400adc62013-02-08 15:28:42 +0000542 (Line.First.isNot(tok::kw_for) || State.ParenLevel != 1)) {
Daniel Jaspere941b162013-01-23 10:08:28 +0000543 // If we are breaking after '(', '{', '<', this is not bin packing unless
Daniel Jasperf7db4332013-01-29 16:03:49 +0000544 // AllowAllParametersOfDeclarationOnNextLine is false.
Daniel Jaspere941b162013-01-23 10:08:28 +0000545 if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace) &&
546 Previous.Type != TT_TemplateOpener) ||
Daniel Jasperf7db4332013-01-29 16:03:49 +0000547 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
548 Line.MustBeDeclaration))
Daniel Jasperacc33662013-02-08 08:22:00 +0000549 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasper38c11ce2013-01-29 11:21:01 +0000550
Daniel Jaspere941b162013-01-23 10:08:28 +0000551 // Any break on this level means that the parent level has been broken
552 // and we need to avoid bin packing there.
553 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
Daniel Jasperf7f13c02013-02-04 07:30:30 +0000554 if (Line.First.isNot(tok::kw_for) || i != 1)
Daniel Jasperacc33662013-02-08 08:22:00 +0000555 State.Stack[i].BreakBeforeParameter = true;
Daniel Jaspere941b162013-01-23 10:08:28 +0000556 }
557 }
Daniel Jasper9278eb92013-01-16 14:59:02 +0000558
Manuel Klimeka54d1a92013-01-14 16:41:43 +0000559 moveStateToNextToken(State);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000560 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000561
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000562 /// \brief Mark the next token as consumed in \p State and modify its stacks
563 /// accordingly.
Daniel Jasper337816e2013-01-11 10:22:12 +0000564 void moveStateToNextToken(LineState &State) {
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000565 const AnnotatedToken &Current = *State.NextToken;
Daniel Jasper337816e2013-01-11 10:22:12 +0000566 assert(State.Stack.size());
Daniel Jaspere9de2602012-12-06 09:56:08 +0000567
Daniel Jasper337816e2013-01-11 10:22:12 +0000568 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
569 State.Stack.back().FirstLessLess = State.Column;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000570 if (Current.is(tok::question))
571 State.Stack.back().QuestionColumn = State.Column;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000572 if (Current.is(tok::l_brace) && Current.MatchingParen != NULL &&
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000573 !Current.MatchingParen->MustBreakBefore) {
Daniel Jasperacc33662013-02-08 08:22:00 +0000574 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
575 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000576 }
Daniel Jaspere9de2602012-12-06 09:56:08 +0000577
Daniel Jasper400adc62013-02-08 15:28:42 +0000578 // Insert scopes created by fake parenthesis.
579 for (unsigned i = 0, e = Current.FakeLParens; i != e; ++i) {
580 ParenState NewParenState = State.Stack.back();
581 NewParenState.Indent = std::max(State.Column, State.Stack.back().Indent);
582 State.Stack.push_back(NewParenState);
583 }
584
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000585 // If we encounter an opening (, [, { or <, we add a level to our stacks to
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000586 // prepare for the following tokens.
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000587 if (Current.is(tok::l_paren) || Current.is(tok::l_square) ||
588 Current.is(tok::l_brace) ||
589 State.NextToken->Type == TT_TemplateOpener) {
Daniel Jasper337816e2013-01-11 10:22:12 +0000590 unsigned NewIndent;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000591 bool AvoidBinPacking;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000592 if (Current.is(tok::l_brace)) {
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000593 NewIndent = 2 + State.Stack.back().LastSpace;
594 AvoidBinPacking = false;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000595 } else {
Daniel Jasper337816e2013-01-11 10:22:12 +0000596 NewIndent = 4 + State.Stack.back().LastSpace;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000597 AvoidBinPacking = !Style.BinPackParameters;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000598 }
Daniel Jasperb9ebd5d2013-02-05 09:41:21 +0000599 State.Stack.push_back(
600 ParenState(NewIndent, State.Stack.back().LastSpace, AvoidBinPacking,
601 State.Stack.back().HasMultiParameterLine));
Daniel Jasper400adc62013-02-08 15:28:42 +0000602 ++State.ParenLevel;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000603 }
604
Daniel Jasperacc33662013-02-08 08:22:00 +0000605 // If this '[' opens an ObjC call, determine whether all parameters fit into
606 // one line and put one per line if they don't.
607 if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
608 Current.MatchingParen != NULL) {
609 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
610 State.Stack.back().BreakBeforeParameter = true;
611 }
612
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000613 // If we encounter a closing ), ], } or >, we can remove a level from our
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000614 // stacks.
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000615 if (Current.is(tok::r_paren) || Current.is(tok::r_square) ||
616 (Current.is(tok::r_brace) && State.NextToken != &RootToken) ||
617 State.NextToken->Type == TT_TemplateCloser) {
Daniel Jasper337816e2013-01-11 10:22:12 +0000618 State.Stack.pop_back();
Daniel Jasper400adc62013-02-08 15:28:42 +0000619 --State.ParenLevel;
620 }
621
622 // Remove scopes created by fake parenthesis.
623 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
624 State.Stack.pop_back();
Daniel Jasperf7935112012-12-03 18:12:45 +0000625 }
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000626
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000627 if (State.NextToken->Children.empty())
628 State.NextToken = NULL;
629 else
630 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000631
632 State.Column += Current.FormatTok.TokenLength;
Daniel Jasperf7935112012-12-03 18:12:45 +0000633 }
634
Daniel Jasper2df93312013-01-09 10:16:05 +0000635 unsigned getColumnLimit() {
636 return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0);
637 }
638
Daniel Jasper4b866272013-02-01 11:00:45 +0000639 /// \brief An edge in the solution space starting from the \c LineState and
640 /// inserting a newline dependent on the \c bool.
641 typedef std::pair<bool, const LineState *> Edge;
642
643 /// \brief An item in the prioritized BFS search queue. The \c LineState was
644 /// reached using the \c Edge.
645 typedef std::pair<LineState, Edge> QueueItem;
646
647 /// \brief Analyze the entire solution space starting from \p InitialState.
Daniel Jasperf7935112012-12-03 18:12:45 +0000648 ///
Daniel Jasper4b866272013-02-01 11:00:45 +0000649 /// This implements a variant of Dijkstra's algorithm on the graph that spans
650 /// the solution space (\c LineStates are the nodes). The algorithm tries to
651 /// find the shortest path (the one with lowest penalty) from \p InitialState
652 /// to a state where all tokens are placed.
653 unsigned analyzeSolutionSpace(const LineState &InitialState) {
654 // Insert start element into queue.
655 std::multimap<unsigned, QueueItem> Queue;
656 Queue.insert(std::pair<unsigned, QueueItem>(
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000657 0, QueueItem(InitialState, Edge(false, (const LineState *)0))));
Daniel Jasper4b866272013-02-01 11:00:45 +0000658 std::map<LineState, Edge> Seen;
659
660 // While not empty, take first element and follow edges.
661 while (!Queue.empty()) {
662 unsigned Penalty = Queue.begin()->first;
663 QueueItem Item = Queue.begin()->second;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000664 if (Item.first.NextToken == NULL) {
665 DEBUG(llvm::errs() << "\n---\nPenalty for line: " << Penalty << "\n");
Daniel Jasper4b866272013-02-01 11:00:45 +0000666 break;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000667 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000668 Queue.erase(Queue.begin());
669
670 if (Seen.find(Item.first) != Seen.end())
671 continue; // State already examined with lower penalty.
672
673 const LineState &SavedState = Seen.insert(std::pair<LineState, Edge>(
674 Item.first,
675 Edge(Item.second.first, Item.second.second))).first->first;
676
677 addNextStateToQueue(SavedState, Penalty, /*NewLine=*/ false, Queue);
678 addNextStateToQueue(SavedState, Penalty, /*NewLine=*/ true, Queue);
679 }
680
681 if (Queue.empty())
682 // We were unable to find a solution, do nothing.
683 // FIXME: Add diagnostic?
Daniel Jasperf7935112012-12-03 18:12:45 +0000684 return 0;
685
Daniel Jasper4b866272013-02-01 11:00:45 +0000686 // Reconstruct the solution.
687 // FIXME: Add debugging output.
688 Edge *CurrentEdge = &Queue.begin()->second.second;
689 while (CurrentEdge->second != NULL) {
690 LineState CurrentState = *CurrentEdge->second;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000691 if (CurrentEdge->first) {
692 DEBUG(llvm::errs() << "Penalty for splitting before "
693 << CurrentState.NextToken->FormatTok.Tok.getName()
694 << ": " << CurrentState.NextToken->SplitPenalty
695 << "\n");
696 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000697 addTokenToState(CurrentEdge->first, false, CurrentState);
698 CurrentEdge = &Seen[*CurrentEdge->second];
699 }
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000700 DEBUG(llvm::errs() << "---\n");
Daniel Jasperf7935112012-12-03 18:12:45 +0000701
Daniel Jasper4b866272013-02-01 11:00:45 +0000702 // Return the column after the last token of the solution.
703 return Queue.begin()->second.first.Column;
704 }
705
706 /// \brief Add the following state to the analysis queue \p Queue.
707 ///
708 /// Assume the current state is \p OldState and has been reached with a
709 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
710 void addNextStateToQueue(const LineState &OldState, unsigned Penalty,
711 bool NewLine,
712 std::multimap<unsigned, QueueItem> &Queue) {
713 if (NewLine && !canBreak(OldState))
714 return;
715 if (!NewLine && mustBreak(OldState))
716 return;
717 LineState State(OldState);
Daniel Jasper20b09ef2013-01-28 09:35:24 +0000718 if (NewLine)
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000719 Penalty += State.NextToken->SplitPenalty;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000720 addTokenToState(NewLine, true, State);
Daniel Jasper2df93312013-01-09 10:16:05 +0000721 if (State.Column > getColumnLimit()) {
722 unsigned ExcessCharacters = State.Column - getColumnLimit();
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000723 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
Daniel Jasper2df93312013-01-09 10:16:05 +0000724 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000725 Queue.insert(std::pair<unsigned, QueueItem>(
726 Penalty, QueueItem(State, Edge(NewLine, &OldState))));
727 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000728
Daniel Jasper4b866272013-02-01 11:00:45 +0000729 /// \brief Returns \c true, if a line break after \p State is allowed.
730 bool canBreak(const LineState &State) {
731 if (!State.NextToken->CanBreakBefore &&
732 !(State.NextToken->is(tok::r_brace) &&
733 State.Stack.back().BreakBeforeClosingBrace))
734 return false;
735 // Trying to insert a parameter on a new line if there are already more than
736 // one parameter on the current line is bin packing.
Daniel Jasperb9ebd5d2013-02-05 09:41:21 +0000737 if (State.Stack.back().HasMultiParameterLine &&
Daniel Jasper4b866272013-02-01 11:00:45 +0000738 State.Stack.back().AvoidBinPacking)
739 return false;
740 return true;
741 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000742
Daniel Jasper4b866272013-02-01 11:00:45 +0000743 /// \brief Returns \c true, if a line break after \p State is mandatory.
744 bool mustBreak(const LineState &State) {
745 if (State.NextToken->MustBreakBefore)
746 return true;
747 if (State.NextToken->is(tok::r_brace) &&
748 State.Stack.back().BreakBeforeClosingBrace)
749 return true;
750 if (State.NextToken->Parent->is(tok::semi) &&
751 State.LineContainsContinuedForLoopSection)
752 return true;
753 if (State.NextToken->Parent->is(tok::comma) &&
Daniel Jasperacc33662013-02-08 08:22:00 +0000754 State.Stack.back().BreakBeforeParameter &&
Daniel Jasper14e40ec2013-02-04 08:34:57 +0000755 !isTrailingComment(*State.NextToken))
Daniel Jasper4b866272013-02-01 11:00:45 +0000756 return true;
Daniel Jasperacc33662013-02-08 08:22:00 +0000757 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
758 // out whether it is the first parameter. Clean this up.
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000759 if (State.NextToken->Type == TT_ObjCSelectorName &&
Daniel Jasperacc33662013-02-08 08:22:00 +0000760 State.NextToken->LongestObjCSelectorName == 0 &&
761 State.Stack.back().BreakBeforeParameter)
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000762 return true;
Daniel Jasper4b866272013-02-01 11:00:45 +0000763 if ((State.NextToken->Type == TT_CtorInitializerColon ||
764 (State.NextToken->Parent->ClosesTemplateDeclaration &&
Daniel Jasper400adc62013-02-08 15:28:42 +0000765 State.ParenLevel == 0)))
Daniel Jasper4b866272013-02-01 11:00:45 +0000766 return true;
767 return false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000768 }
769
Daniel Jasperf7935112012-12-03 18:12:45 +0000770 FormatStyle Style;
771 SourceManager &SourceMgr;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000772 const AnnotatedLine &Line;
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000773 const unsigned FirstIndent;
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000774 const AnnotatedToken &RootToken;
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000775 WhitespaceManager &Whitespaces;
Daniel Jasperf7935112012-12-03 18:12:45 +0000776};
777
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000778class LexerBasedFormatTokenSource : public FormatTokenSource {
779public:
780 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000781 : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000782 IdentTable(Lex.getLangOpts()) {
783 Lex.SetKeepWhitespaceMode(true);
784 }
785
786 virtual FormatToken getNextToken() {
787 if (GreaterStashed) {
788 FormatTok.NewlinesBefore = 0;
789 FormatTok.WhiteSpaceStart =
790 FormatTok.Tok.getLocation().getLocWithOffset(1);
791 FormatTok.WhiteSpaceLength = 0;
792 GreaterStashed = false;
793 return FormatTok;
794 }
795
796 FormatTok = FormatToken();
797 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimekef920692013-01-07 07:56:50 +0000798 StringRef Text = rawTokenText(FormatTok.Tok);
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000799 FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
Manuel Klimek52d0fd82013-01-05 22:56:06 +0000800 if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0)
801 FormatTok.IsFirst = true;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000802
803 // Consume and record whitespace until we find a significant token.
804 while (FormatTok.Tok.is(tok::unknown)) {
Manuel Klimek0c137952013-02-11 12:33:24 +0000805 unsigned Newlines = Text.count('\n');
806 unsigned EscapedNewlines = Text.count("\\\n");
807 FormatTok.NewlinesBefore += Newlines;
808 FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000809 FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
810
811 if (FormatTok.Tok.is(tok::eof))
812 return FormatTok;
813 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimekef920692013-01-07 07:56:50 +0000814 Text = rawTokenText(FormatTok.Tok);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000815 }
Manuel Klimekef920692013-01-07 07:56:50 +0000816
817 // Now FormatTok is the next non-whitespace token.
818 FormatTok.TokenLength = Text.size();
819
Manuel Klimek1abf7892013-01-04 23:34:14 +0000820 // In case the token starts with escaped newlines, we want to
821 // take them into account as whitespace - this pattern is quite frequent
822 // in macro definitions.
823 // FIXME: What do we want to do with other escaped spaces, and escaped
824 // spaces or newlines in the middle of tokens?
825 // FIXME: Add a more explicit test.
826 unsigned i = 0;
Daniel Jasperda16db32013-01-07 10:48:50 +0000827 while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000828 // FIXME: ++FormatTok.NewlinesBefore is missing...
Manuel Klimek1abf7892013-01-04 23:34:14 +0000829 FormatTok.WhiteSpaceLength += 2;
Manuel Klimekef920692013-01-07 07:56:50 +0000830 FormatTok.TokenLength -= 2;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000831 i += 2;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000832 }
833
834 if (FormatTok.Tok.is(tok::raw_identifier)) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000835 IdentifierInfo &Info = IdentTable.get(Text);
Daniel Jasper050948a52012-12-21 17:58:39 +0000836 FormatTok.Tok.setIdentifierInfo(&Info);
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000837 FormatTok.Tok.setKind(Info.getTokenID());
838 }
839
840 if (FormatTok.Tok.is(tok::greatergreater)) {
841 FormatTok.Tok.setKind(tok::greater);
842 GreaterStashed = true;
843 }
844
845 return FormatTok;
846 }
847
848private:
849 FormatToken FormatTok;
850 bool GreaterStashed;
851 Lexer &Lex;
852 SourceManager &SourceMgr;
853 IdentifierTable IdentTable;
854
855 /// Returns the text of \c FormatTok.
Manuel Klimekef920692013-01-07 07:56:50 +0000856 StringRef rawTokenText(Token &Tok) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000857 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
858 Tok.getLength());
859 }
860};
861
Daniel Jasperf7935112012-12-03 18:12:45 +0000862class Formatter : public UnwrappedLineConsumer {
863public:
Daniel Jasper25837aa2013-01-14 14:14:23 +0000864 Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex,
865 SourceManager &SourceMgr,
Daniel Jasperf7935112012-12-03 18:12:45 +0000866 const std::vector<CharSourceRange> &Ranges)
Alexander Kornienko5b7157a2013-01-10 15:05:09 +0000867 : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Daniel Jasperbbc84152013-01-29 11:27:30 +0000868 Whitespaces(SourceMgr), Ranges(Ranges) {
869 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000870
Daniel Jasperfd8c4b12013-01-11 14:23:32 +0000871 virtual ~Formatter() {}
Daniel Jasper61bd3a12012-12-04 21:05:31 +0000872
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000873 void deriveLocalStyle() {
874 unsigned CountBoundToVariable = 0;
875 unsigned CountBoundToType = 0;
876 bool HasCpp03IncompatibleFormat = false;
877 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
878 if (AnnotatedLines[i].First.Children.empty())
879 continue;
880 AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0];
881 while (!Tok->Children.empty()) {
882 if (Tok->Type == TT_PointerOrReference) {
883 bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0;
884 bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0;
885 if (SpacesBefore && !SpacesAfter)
886 ++CountBoundToVariable;
887 else if (!SpacesBefore && SpacesAfter)
888 ++CountBoundToType;
889 }
890
Daniel Jasper400adc62013-02-08 15:28:42 +0000891 if (Tok->Type == TT_TemplateCloser &&
892 Tok->Parent->Type == TT_TemplateCloser &&
893 Tok->FormatTok.WhiteSpaceLength == 0)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000894 HasCpp03IncompatibleFormat = true;
895 Tok = &Tok->Children[0];
896 }
897 }
898 if (Style.DerivePointerBinding) {
899 if (CountBoundToType > CountBoundToVariable)
900 Style.PointerBindsToType = true;
901 else if (CountBoundToType < CountBoundToVariable)
902 Style.PointerBindsToType = false;
903 }
904 if (Style.Standard == FormatStyle::LS_Auto) {
905 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
906 : FormatStyle::LS_Cpp03;
907 }
908 }
909
Daniel Jasperf7935112012-12-03 18:12:45 +0000910 tooling::Replacements format() {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000911 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
Alexander Kornienko5b7157a2013-01-10 15:05:09 +0000912 UnwrappedLineParser Parser(Diag, Style, Tokens, *this);
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000913 StructuralError = Parser.parse();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000914 unsigned PreviousEndOfLineColumn = 0;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000915 TokenAnnotator Annotator(Style, SourceMgr, Lex);
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000916 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000917 Annotator.annotate(AnnotatedLines[i]);
918 }
919 deriveLocalStyle();
920 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
921 Annotator.calculateFormattingInformation(AnnotatedLines[i]);
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000922 }
Manuel Klimekb95f5452013-02-08 17:38:27 +0000923 std::vector<int> IndentForLevel;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000924 for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
925 E = AnnotatedLines.end();
Manuel Klimek51bd6ec2013-01-10 19:49:59 +0000926 I != E; ++I) {
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000927 const AnnotatedLine &TheLine = *I;
Manuel Klimekb95f5452013-02-08 17:38:27 +0000928 int Offset = GetIndentOffset(TheLine.First);
929 while (IndentForLevel.size() <= TheLine.Level)
930 IndentForLevel.push_back(-1);
931 IndentForLevel.resize(TheLine.Level + 1);
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000932 if (touchesRanges(TheLine) && TheLine.Type != LT_Invalid) {
Manuel Klimekb95f5452013-02-08 17:38:27 +0000933 unsigned LevelIndent = GetIndent(IndentForLevel, TheLine.Level);
934 unsigned Indent = LevelIndent;
935 if (static_cast<int>(Indent) + Offset >= 0)
936 Indent += Offset;
937 if (!TheLine.First.FormatTok.WhiteSpaceStart.isValid() ||
938 StructuralError) {
939 Indent = LevelIndent = SourceMgr.getSpellingColumnNumber(
940 TheLine.First.FormatTok.Tok.getLocation()) - 1;
941 } else {
942 formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
943 PreviousEndOfLineColumn);
944 }
Daniel Jaspera67a8f02013-01-16 10:41:46 +0000945 tryFitMultipleLinesInOne(Indent, I, E);
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000946 UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000947 TheLine.First, Whitespaces,
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000948 StructuralError);
Manuel Klimek51bd6ec2013-01-10 19:49:59 +0000949 PreviousEndOfLineColumn = Formatter.format();
Manuel Klimekb95f5452013-02-08 17:38:27 +0000950 IndentForLevel[TheLine.Level] = LevelIndent;
Manuel Klimek51bd6ec2013-01-10 19:49:59 +0000951 } else {
952 // If we did not reformat this unwrapped line, the column at the end of
953 // the last token is unchanged - thus, we can calculate the end of the
Manuel Klimekb95f5452013-02-08 17:38:27 +0000954 // last token.
Manuel Klimek51bd6ec2013-01-10 19:49:59 +0000955 PreviousEndOfLineColumn =
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000956 SourceMgr.getSpellingColumnNumber(
957 TheLine.Last->FormatTok.Tok.getLocation()) +
958 Lex.MeasureTokenLength(TheLine.Last->FormatTok.Tok.getLocation(),
Daniel Jasperbbc84152013-01-29 11:27:30 +0000959 SourceMgr, Lex.getLangOpts()) - 1;
Manuel Klimekb95f5452013-02-08 17:38:27 +0000960 unsigned Indent = SourceMgr.getSpellingColumnNumber(
961 TheLine.First.FormatTok.Tok.getLocation()) - 1;
962 unsigned LevelIndent = Indent;
963 if (static_cast<int>(LevelIndent) - Offset >= 0)
964 LevelIndent -= Offset;
965 IndentForLevel[TheLine.Level] = LevelIndent;
Manuel Klimek51bd6ec2013-01-10 19:49:59 +0000966 }
967 }
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000968 return Whitespaces.generateReplacements();
Daniel Jasperf7935112012-12-03 18:12:45 +0000969 }
970
971private:
Manuel Klimekb95f5452013-02-08 17:38:27 +0000972 /// \brief Get the indent of \p Level from \p IndentForLevel.
973 ///
974 /// \p IndentForLevel must contain the indent for the level \c l
975 /// at \p IndentForLevel[l], or a value < 0 if the indent for
976 /// that level is unknown.
977 unsigned GetIndent(const std::vector<int> IndentForLevel,
978 unsigned Level) {
Manuel Klimekb95f5452013-02-08 17:38:27 +0000979 if (IndentForLevel[Level] != -1)
980 return IndentForLevel[Level];
Manuel Klimekd076dcd2013-02-08 19:53:32 +0000981 if (Level == 0)
982 return 0;
Manuel Klimekb95f5452013-02-08 17:38:27 +0000983 return GetIndent(IndentForLevel, Level - 1) + 2;
984 }
985
986 /// \brief Get the offset of the line relatively to the level.
987 ///
988 /// For example, 'public:' labels in classes are offset by 1 or 2
989 /// characters to the left from their level.
990 int GetIndentOffset(const AnnotatedToken &RootToken) {
991 bool IsAccessModifier = false;
992 if (RootToken.is(tok::kw_public) || RootToken.is(tok::kw_protected) ||
993 RootToken.is(tok::kw_private))
994 IsAccessModifier = true;
995 else if (RootToken.is(tok::at) && !RootToken.Children.empty() &&
996 (RootToken.Children[0].isObjCAtKeyword(tok::objc_public) ||
997 RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) ||
998 RootToken.Children[0].isObjCAtKeyword(tok::objc_package) ||
999 RootToken.Children[0].isObjCAtKeyword(tok::objc_private)))
1000 IsAccessModifier = true;
1001
1002 if (IsAccessModifier)
1003 return Style.AccessModifierOffset;
1004 return 0;
1005 }
1006
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001007 /// \brief Tries to merge lines into one.
1008 ///
1009 /// This will change \c Line and \c AnnotatedLine to contain the merged line,
1010 /// if possible; note that \c I will be incremented when lines are merged.
1011 ///
1012 /// Returns whether the resulting \c Line can fit in a single line.
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001013 void tryFitMultipleLinesInOne(unsigned Indent,
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001014 std::vector<AnnotatedLine>::iterator &I,
1015 std::vector<AnnotatedLine>::iterator E) {
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001016 unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent;
1017
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001018 // We can never merge stuff if there are trailing line comments.
1019 if (I->Last->Type == TT_LineComment)
1020 return;
1021
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001022 // Check whether the UnwrappedLine can be put onto a single line. If
1023 // so, this is bound to be the optimal solution (by definition) and we
1024 // don't need to analyze the entire solution space.
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001025 if (I->Last->TotalLength > Limit)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001026 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001027 Limit -= I->Last->TotalLength;
Daniel Jasperc36492b2013-01-16 07:02:34 +00001028
Daniel Jasperd41ee2d2013-01-21 14:18:28 +00001029 if (I + 1 == E || (I + 1)->Type == LT_Invalid)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001030 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001031
Daniel Jasper25837aa2013-01-14 14:14:23 +00001032 if (I->Last->is(tok::l_brace)) {
1033 tryMergeSimpleBlock(I, E, Limit);
1034 } else if (I->First.is(tok::kw_if)) {
1035 tryMergeSimpleIf(I, E, Limit);
Daniel Jasper39825ea2013-01-14 15:40:57 +00001036 } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline ||
1037 I->First.FormatTok.IsFirst)) {
1038 tryMergeSimplePPDirective(I, E, Limit);
Daniel Jasper25837aa2013-01-14 14:14:23 +00001039 }
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001040 return;
Daniel Jasper25837aa2013-01-14 14:14:23 +00001041 }
1042
Daniel Jasper39825ea2013-01-14 15:40:57 +00001043 void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
1044 std::vector<AnnotatedLine>::iterator E,
1045 unsigned Limit) {
1046 AnnotatedLine &Line = *I;
Daniel Jasper2ab0d012013-01-14 15:52:06 +00001047 if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline)
1048 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001049 if (I + 2 != E && (I + 2)->InPPDirective &&
1050 !(I + 2)->First.FormatTok.HasUnescapedNewline)
1051 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001052 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001053 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001054 join(Line, *(++I));
1055 }
1056
Daniel Jasper25837aa2013-01-14 14:14:23 +00001057 void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I,
1058 std::vector<AnnotatedLine>::iterator E,
1059 unsigned Limit) {
Daniel Jasper1b750ed2013-01-14 16:24:39 +00001060 if (!Style.AllowShortIfStatementsOnASingleLine)
1061 return;
Manuel Klimekda087612013-01-18 14:46:43 +00001062 if ((I + 1)->InPPDirective != I->InPPDirective ||
1063 ((I + 1)->InPPDirective &&
1064 (I + 1)->First.FormatTok.HasUnescapedNewline))
1065 return;
Daniel Jasper25837aa2013-01-14 14:14:23 +00001066 AnnotatedLine &Line = *I;
Daniel Jasperc36492b2013-01-16 07:02:34 +00001067 if (Line.Last->isNot(tok::r_paren))
1068 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001069 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jasper25837aa2013-01-14 14:14:23 +00001070 return;
1071 if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment)
1072 return;
1073 // Only inline simple if's (no nested if or else).
1074 if (I + 2 != E && (I + 2)->First.is(tok::kw_else))
1075 return;
1076 join(Line, *(++I));
1077 }
1078
1079 void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
Daniel Jasperbbc84152013-01-29 11:27:30 +00001080 std::vector<AnnotatedLine>::iterator E,
1081 unsigned Limit) {
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001082 // First, check that the current line allows merging. This is the case if
1083 // we're not in a control flow statement and the last token is an opening
1084 // brace.
Daniel Jasper25837aa2013-01-14 14:14:23 +00001085 AnnotatedLine &Line = *I;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001086 bool AllowedTokens =
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001087 Line.First.isNot(tok::kw_if) && Line.First.isNot(tok::kw_while) &&
1088 Line.First.isNot(tok::kw_do) && Line.First.isNot(tok::r_brace) &&
1089 Line.First.isNot(tok::kw_else) && Line.First.isNot(tok::kw_try) &&
1090 Line.First.isNot(tok::kw_catch) && Line.First.isNot(tok::kw_for) &&
Nico Webera21aaae2013-01-11 21:14:08 +00001091 // This gets rid of all ObjC @ keywords and methods.
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001092 Line.First.isNot(tok::at) && Line.First.isNot(tok::minus) &&
1093 Line.First.isNot(tok::plus);
Daniel Jasper25837aa2013-01-14 14:14:23 +00001094 if (!AllowedTokens)
1095 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001096
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001097 AnnotatedToken *Tok = &(I + 1)->First;
1098 if (Tok->Children.empty() && Tok->is(tok::r_brace) &&
1099 !Tok->MustBreakBefore && Tok->TotalLength <= Limit) {
Daniel Jaspereef30492013-02-11 12:36:37 +00001100 Tok->SpacesRequiredBefore = 0;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001101 join(Line, *(I + 1));
1102 I += 1;
1103 } else {
1104 // Check that we still have three lines and they fit into the limit.
1105 if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
1106 !nextTwoLinesFitInto(I, Limit))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001107 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001108
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001109 // Second, check that the next line does not contain any braces - if it
1110 // does, readability declines when putting it into a single line.
1111 if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
1112 return;
1113 do {
1114 if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace))
1115 return;
1116 Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
1117 } while (Tok != NULL);
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001118
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001119 // Last, check that the third line contains a single closing brace.
1120 Tok = &(I + 2)->First;
1121 if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
1122 Tok->MustBreakBefore)
1123 return;
1124
1125 join(Line, *(I + 1));
1126 join(Line, *(I + 2));
1127 I += 2;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001128 }
Daniel Jasper25837aa2013-01-14 14:14:23 +00001129 }
1130
1131 bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
1132 unsigned Limit) {
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001133 return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
1134 Limit;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001135 }
1136
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001137 void join(AnnotatedLine &A, const AnnotatedLine &B) {
1138 A.Last->Children.push_back(B.First);
1139 while (!A.Last->Children.empty()) {
1140 A.Last->Children[0].Parent = A.Last;
1141 A.Last = &A.Last->Children[0];
1142 }
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001143 }
1144
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001145 bool touchesRanges(const AnnotatedLine &TheLine) {
1146 const FormatToken *First = &TheLine.First.FormatTok;
1147 const FormatToken *Last = &TheLine.Last->FormatTok;
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001148 CharSourceRange LineRange = CharSourceRange::getTokenRange(
Daniel Jasperbbc84152013-01-29 11:27:30 +00001149 First->Tok.getLocation(), Last->Tok.getLocation());
Daniel Jasperf7935112012-12-03 18:12:45 +00001150 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001151 if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
1152 Ranges[i].getBegin()) &&
1153 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1154 LineRange.getBegin()))
1155 return true;
Daniel Jasperf7935112012-12-03 18:12:45 +00001156 }
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001157 return false;
1158 }
1159
1160 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001161 AnnotatedLines.push_back(AnnotatedLine(TheLine));
Daniel Jasperf7935112012-12-03 18:12:45 +00001162 }
1163
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001164 /// \brief Add a new line and the required indent before the first Token
1165 /// of the \c UnwrappedLine if there was no structural parsing error.
1166 /// Returns the indent level of the \c UnwrappedLine.
Manuel Klimekb95f5452013-02-08 17:38:27 +00001167 void formatFirstToken(const AnnotatedToken &RootToken, unsigned Indent,
1168 bool InPPDirective, unsigned PreviousEndOfLineColumn) {
Daniel Jasperfd8c4b12013-01-11 14:23:32 +00001169 const FormatToken &Tok = RootToken.FormatTok;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001170
Daniel Jasperbbc84152013-01-29 11:27:30 +00001171 unsigned Newlines =
1172 std::min(Tok.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001173 if (Newlines == 0 && !Tok.IsFirst)
1174 Newlines = 1;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001175
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001176 if (!InPPDirective || Tok.HasUnescapedNewline) {
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001177 Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0, Style);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001178 } else {
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001179 Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent,
1180 PreviousEndOfLineColumn, Style);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001181 }
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001182 }
1183
Alexander Kornienko116ba682013-01-14 11:34:14 +00001184 DiagnosticsEngine &Diag;
Daniel Jasperf7935112012-12-03 18:12:45 +00001185 FormatStyle Style;
1186 Lexer &Lex;
1187 SourceManager &SourceMgr;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001188 WhitespaceManager Whitespaces;
Daniel Jasperf7935112012-12-03 18:12:45 +00001189 std::vector<CharSourceRange> Ranges;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001190 std::vector<AnnotatedLine> AnnotatedLines;
Alexander Kornienko870f9eb2012-12-04 17:27:50 +00001191 bool StructuralError;
Daniel Jasperf7935112012-12-03 18:12:45 +00001192};
1193
Daniel Jasperbbc84152013-01-29 11:27:30 +00001194tooling::Replacements
1195reformat(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
1196 std::vector<CharSourceRange> Ranges, DiagnosticConsumer *DiagClient) {
Alexander Kornienko5b7157a2013-01-10 15:05:09 +00001197 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Alexander Kornienko116ba682013-01-14 11:34:14 +00001198 OwningPtr<DiagnosticConsumer> DiagPrinter;
1199 if (DiagClient == 0) {
1200 DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts));
1201 DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
1202 DiagClient = DiagPrinter.get();
1203 }
Alexander Kornienko5b7157a2013-01-10 15:05:09 +00001204 DiagnosticsEngine Diagnostics(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001205 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
Alexander Kornienko116ba682013-01-14 11:34:14 +00001206 DiagClient, false);
Alexander Kornienko5b7157a2013-01-10 15:05:09 +00001207 Diagnostics.setSourceManager(&SourceMgr);
1208 Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
Daniel Jasperf7935112012-12-03 18:12:45 +00001209 return formatter.format();
1210}
1211
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001212LangOptions getFormattingLangOpts() {
1213 LangOptions LangOpts;
1214 LangOpts.CPlusPlus = 1;
1215 LangOpts.CPlusPlus11 = 1;
1216 LangOpts.Bool = 1;
1217 LangOpts.ObjC1 = 1;
1218 LangOpts.ObjC2 = 1;
1219 return LangOpts;
1220}
1221
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001222} // namespace format
1223} // namespace clang