blob: 347e31470d5ad2f9925f88321ba3adc7fb8bc405 [file] [log] [blame]
Daniel Jasperbac016b2012-12-03 18:12:45 +00001//===--- Format.cpp - Format C++ code -------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file implements functions declared in Format.h. This will be
12/// split into separate files as we go.
13///
Daniel Jasperbac016b2012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Manuel Klimekca547db2013-01-16 14:55:28 +000016#define DEBUG_TYPE "format-formatter"
17
Daniel Jasper32d28ee2013-01-29 21:01:14 +000018#include "TokenAnnotator.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "UnwrappedLineParser.h"
Alexander Kornienko3048aea2013-01-10 15:05:09 +000020#include "clang/Basic/Diagnostic.h"
Daniel Jasper675d2e32012-12-21 10:20:02 +000021#include "clang/Basic/OperatorPrecedence.h"
Chandler Carruthb99083e2013-01-02 10:28:36 +000022#include "clang/Basic/SourceManager.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000023#include "clang/Format/Format.h"
Alexander Kornienko3048aea2013-01-10 15:05:09 +000024#include "clang/Frontend/TextDiagnosticPrinter.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000025#include "clang/Lex/Lexer.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000026#include "llvm/Support/Debug.h"
Daniel Jasper8822d3a2012-12-04 13:02:32 +000027#include <string>
28
Daniel Jasperbac016b2012-12-03 18:12:45 +000029namespace clang {
30namespace format {
31
Daniel Jasperbac016b2012-12-03 18:12:45 +000032FormatStyle getLLVMStyle() {
33 FormatStyle LLVMStyle;
34 LLVMStyle.ColumnLimit = 80;
35 LLVMStyle.MaxEmptyLinesToKeep = 1;
Daniel Jasper8ff690a2013-02-06 14:22:40 +000036 LLVMStyle.PointerBindsToType = false;
37 LLVMStyle.DerivePointerBinding = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +000038 LLVMStyle.AccessModifierOffset = -2;
Daniel Jasper8ff690a2013-02-06 14:22:40 +000039 LLVMStyle.Standard = FormatStyle::LS_Cpp03;
Alexander Kornienko15757312012-12-06 18:03:27 +000040 LLVMStyle.IndentCaseLabels = false;
Daniel Jasper7ad4eff2013-01-07 11:09:06 +000041 LLVMStyle.SpacesBeforeTrailingComments = 1;
Daniel Jasper0df6acd2013-01-16 14:59:02 +000042 LLVMStyle.BinPackParameters = true;
Daniel Jasperf1579602013-01-29 16:03:49 +000043 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasperd75ff642013-01-28 15:40:20 +000044 LLVMStyle.AllowReturnTypeOnItsOwnLine = true;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +000045 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +000046 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Nico Weber5f500df2013-01-10 20:12:55 +000047 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Daniel Jasper01786732013-02-04 07:21:18 +000048 LLVMStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasperbac016b2012-12-03 18:12:45 +000049 return LLVMStyle;
50}
51
52FormatStyle getGoogleStyle() {
53 FormatStyle GoogleStyle;
54 GoogleStyle.ColumnLimit = 80;
55 GoogleStyle.MaxEmptyLinesToKeep = 1;
Daniel Jasper8ff690a2013-02-06 14:22:40 +000056 GoogleStyle.PointerBindsToType = true;
57 GoogleStyle.DerivePointerBinding = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +000058 GoogleStyle.AccessModifierOffset = -1;
Daniel Jasper8ff690a2013-02-06 14:22:40 +000059 GoogleStyle.Standard = FormatStyle::LS_Auto;
Alexander Kornienko15757312012-12-06 18:03:27 +000060 GoogleStyle.IndentCaseLabels = true;
Daniel Jasper7ad4eff2013-01-07 11:09:06 +000061 GoogleStyle.SpacesBeforeTrailingComments = 2;
Daniel Jasper0df6acd2013-01-16 14:59:02 +000062 GoogleStyle.BinPackParameters = false;
Daniel Jasperf1579602013-01-29 16:03:49 +000063 GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasperd75ff642013-01-28 15:40:20 +000064 GoogleStyle.AllowReturnTypeOnItsOwnLine = false;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +000065 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
Daniel Jasperdf3736a2013-01-16 15:44:34 +000066 GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
Nico Weber5f500df2013-01-10 20:12:55 +000067 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Daniel Jasper01786732013-02-04 07:21:18 +000068 GoogleStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasperbac016b2012-12-03 18:12:45 +000069 return GoogleStyle;
70}
71
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +000072FormatStyle getChromiumStyle() {
73 FormatStyle ChromiumStyle = getGoogleStyle();
Daniel Jasperf1579602013-01-29 16:03:49 +000074 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Daniel Jasper8ff690a2013-02-06 14:22:40 +000075 ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
76 ChromiumStyle.DerivePointerBinding = false;
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +000077 return ChromiumStyle;
78}
79
Daniel Jasper15417ef2013-02-06 20:07:35 +000080static bool isTrailingComment(const AnnotatedToken &Tok) {
81 return Tok.is(tok::comment) &&
82 (Tok.Children.empty() || Tok.Children[0].MustBreakBefore);
83}
84
Daniel Jasperce3d1a62013-02-08 08:22:00 +000085// Returns the length of everything up to the first possible line break after
86// the ), ], } or > matching \c Tok.
87static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) {
88 if (Tok.MatchingParen == NULL)
89 return 0;
90 AnnotatedToken *End = Tok.MatchingParen;
91 while (!End->Children.empty() && !End->Children[0].CanBreakBefore) {
92 End = &End->Children[0];
93 }
94 return End->TotalLength - Tok.TotalLength + 1;
95}
96
Daniel Jasperdcc2a622013-01-18 08:44:07 +000097/// \brief Manages the whitespaces around tokens and their replacements.
Manuel Klimek3f8c7f32013-01-10 18:45:26 +000098///
Daniel Jasperdcc2a622013-01-18 08:44:07 +000099/// This includes special handling for certain constructs, e.g. the alignment of
100/// trailing line comments.
101class WhitespaceManager {
102public:
103 WhitespaceManager(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
104
105 /// \brief Replaces the whitespace in front of \p Tok. Only call once for
106 /// each \c AnnotatedToken.
107 void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
108 unsigned Spaces, unsigned WhitespaceStartColumn,
109 const FormatStyle &Style) {
Daniel Jasper821627e2013-01-21 22:49:20 +0000110 // 2+ newlines mean an empty line separating logic scopes.
111 if (NewLines >= 2)
112 alignComments();
113
114 // Align line comments if they are trailing or if they continue other
115 // trailing comments.
Daniel Jasper15417ef2013-02-06 20:07:35 +0000116 if (isTrailingComment(Tok) && (Tok.Parent != NULL || !Comments.empty())) {
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000117 if (Style.ColumnLimit >=
118 Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
119 Comments.push_back(StoredComment());
120 Comments.back().Tok = Tok.FormatTok;
121 Comments.back().Spaces = Spaces;
122 Comments.back().NewLines = NewLines;
Daniel Jasper474e4622013-02-06 22:04:05 +0000123 if (NewLines == 0)
124 Comments.back().MinColumn = WhitespaceStartColumn + Spaces;
125 else
126 Comments.back().MinColumn = Spaces;
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000127 Comments.back().MaxColumn =
128 Style.ColumnLimit - Spaces - Tok.FormatTok.TokenLength;
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000129 return;
130 }
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000131 }
Daniel Jasper821627e2013-01-21 22:49:20 +0000132
133 // If this line does not have a trailing comment, align the stored comments.
Daniel Jasper15417ef2013-02-06 20:07:35 +0000134 if (Tok.Children.empty() && !isTrailingComment(Tok))
Daniel Jasper821627e2013-01-21 22:49:20 +0000135 alignComments();
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000136 storeReplacement(Tok.FormatTok,
137 std::string(NewLines, '\n') + std::string(Spaces, ' '));
138 }
139
140 /// \brief Like \c replaceWhitespace, but additionally adds right-aligned
141 /// backslashes to escape newlines inside a preprocessor directive.
142 ///
143 /// This function and \c replaceWhitespace have the same behavior if
144 /// \c Newlines == 0.
145 void replacePPWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
146 unsigned Spaces, unsigned WhitespaceStartColumn,
147 const FormatStyle &Style) {
148 std::string NewLineText;
149 if (NewLines > 0) {
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000150 unsigned Offset =
151 std::min<int>(Style.ColumnLimit - 1, WhitespaceStartColumn);
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000152 for (unsigned i = 0; i < NewLines; ++i) {
153 NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' ');
154 NewLineText += "\\\n";
155 Offset = 0;
156 }
157 }
158 storeReplacement(Tok.FormatTok, NewLineText + std::string(Spaces, ' '));
159 }
160
161 /// \brief Returns all the \c Replacements created during formatting.
162 const tooling::Replacements &generateReplacements() {
163 alignComments();
164 return Replaces;
165 }
166
167private:
168 /// \brief Structure to store a comment for later layout and alignment.
169 struct StoredComment {
170 FormatToken Tok;
171 unsigned MinColumn;
172 unsigned MaxColumn;
173 unsigned NewLines;
174 unsigned Spaces;
175 };
176 SmallVector<StoredComment, 16> Comments;
177 typedef SmallVector<StoredComment, 16>::iterator comment_iterator;
178
179 /// \brief Try to align all stashed comments.
180 void alignComments() {
181 unsigned MinColumn = 0;
182 unsigned MaxColumn = UINT_MAX;
183 comment_iterator Start = Comments.begin();
184 for (comment_iterator I = Comments.begin(), E = Comments.end(); I != E;
185 ++I) {
186 if (I->MinColumn > MaxColumn || I->MaxColumn < MinColumn) {
187 alignComments(Start, I, MinColumn);
188 MinColumn = I->MinColumn;
189 MaxColumn = I->MaxColumn;
190 Start = I;
191 } else {
192 MinColumn = std::max(MinColumn, I->MinColumn);
193 MaxColumn = std::min(MaxColumn, I->MaxColumn);
194 }
195 }
196 alignComments(Start, Comments.end(), MinColumn);
197 Comments.clear();
198 }
199
200 /// \brief Put all the comments between \p I and \p E into \p Column.
201 void alignComments(comment_iterator I, comment_iterator E, unsigned Column) {
202 while (I != E) {
203 unsigned Spaces = I->Spaces + Column - I->MinColumn;
204 storeReplacement(I->Tok, std::string(I->NewLines, '\n') +
Daniel Jasper29f123b2013-02-08 15:28:42 +0000205 std::string(Spaces, ' '));
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000206 ++I;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000207 }
208 }
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000209
210 /// \brief Stores \p Text as the replacement for the whitespace in front of
211 /// \p Tok.
212 void storeReplacement(const FormatToken &Tok, const std::string Text) {
Daniel Jasperafcbd852013-01-30 09:46:12 +0000213 // Don't create a replacement, if it does not change anything.
214 if (StringRef(SourceMgr.getCharacterData(Tok.WhiteSpaceStart),
215 Tok.WhiteSpaceLength) == Text)
216 return;
217
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000218 Replaces.insert(tooling::Replacement(SourceMgr, Tok.WhiteSpaceStart,
219 Tok.WhiteSpaceLength, Text));
220 }
221
222 SourceManager &SourceMgr;
223 tooling::Replacements Replaces;
224};
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000225
Daniel Jasperbac016b2012-12-03 18:12:45 +0000226class UnwrappedLineFormatter {
227public:
Manuel Klimek94fc6f12013-01-10 19:17:33 +0000228 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
Daniel Jasper995e8202013-01-14 13:08:07 +0000229 const AnnotatedLine &Line, unsigned FirstIndent,
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +0000230 const AnnotatedToken &RootToken,
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000231 WhitespaceManager &Whitespaces, bool StructuralError)
Daniel Jasper1321eb52012-12-18 21:05:13 +0000232 : Style(Style), SourceMgr(SourceMgr), Line(Line),
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000233 FirstIndent(FirstIndent), RootToken(RootToken),
234 Whitespaces(Whitespaces) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000235 }
236
Manuel Klimekd4397b92013-01-04 23:34:14 +0000237 /// \brief Formats an \c UnwrappedLine.
238 ///
239 /// \returns The column after the last token in the last line of the
240 /// \c UnwrappedLine.
241 unsigned format() {
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000242 // Initialize state dependent on indent.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000243 LineState State;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000244 State.Column = FirstIndent;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000245 State.NextToken = &RootToken;
Daniel Jasperd399bff2013-02-05 09:41:21 +0000246 State.Stack.push_back(
247 ParenState(FirstIndent + 4, FirstIndent, !Style.BinPackParameters,
248 /*HasMultiParameterLine=*/ false));
Daniel Jasper2e603772013-01-29 11:21:01 +0000249 State.VariablePos = 0;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000250 State.LineContainsContinuedForLoopSection = false;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000251 State.ParenLevel = 0;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000252
Manuel Klimekca547db2013-01-16 14:55:28 +0000253 DEBUG({
254 DebugTokenState(*State.NextToken);
255 });
256
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000257 // The first token has already been indented and thus consumed.
258 moveStateToNextToken(State);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000259
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000260 // If everything fits on a single line, just put it there.
261 if (Line.Last->TotalLength <= getColumnLimit() - FirstIndent) {
262 while (State.NextToken != NULL) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000263 addTokenToState(false, false, State);
Daniel Jasper1321eb52012-12-18 21:05:13 +0000264 }
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000265 return State.Column;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000266 }
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000267
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000268 // If the ObjC method declaration does not fit on a line, we should format
269 // it with one arg per line.
270 if (Line.Type == LT_ObjCMethodDecl)
271 State.Stack.back().BreakBeforeParameter = true;
272
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000273 // Find best solution in solution space.
274 return analyzeSolutionSpace(State);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000275 }
276
277private:
Manuel Klimekca547db2013-01-16 14:55:28 +0000278 void DebugTokenState(const AnnotatedToken &AnnotatedTok) {
279 const Token &Tok = AnnotatedTok.FormatTok.Tok;
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000280 llvm::errs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
281 Tok.getLength());
Manuel Klimekca547db2013-01-16 14:55:28 +0000282 llvm::errs();
283 }
284
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000285 struct ParenState {
Daniel Jasperd399bff2013-02-05 09:41:21 +0000286 ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking,
287 bool HasMultiParameterLine)
Daniel Jasper29f123b2013-02-08 15:28:42 +0000288 : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
289 BreakBeforeClosingBrace(false), QuestionColumn(0),
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000290 AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000291 HasMultiParameterLine(HasMultiParameterLine), ColonPos(0) {
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000292 }
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000293
Daniel Jasperbac016b2012-12-03 18:12:45 +0000294 /// \brief The position to which a specific parenthesis level needs to be
295 /// indented.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000296 unsigned Indent;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000297
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000298 /// \brief The position of the last space on each level.
299 ///
300 /// Used e.g. to break like:
301 /// functionCall(Parameter, otherCall(
302 /// OtherParameter));
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000303 unsigned LastSpace;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000304
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000305 /// \brief The position the first "<<" operator encountered on each level.
306 ///
307 /// Used to align "<<" operators. 0 if no such operator has been encountered
308 /// on a level.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000309 unsigned FirstLessLess;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000310
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000311 /// \brief Whether a newline needs to be inserted before the block's closing
312 /// brace.
313 ///
314 /// We only want to insert a newline before the closing brace if there also
315 /// was a newline after the beginning left brace.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000316 bool BreakBeforeClosingBrace;
317
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000318 /// \brief The column of a \c ? in a conditional expression;
319 unsigned QuestionColumn;
320
Daniel Jasperf343cab2013-01-31 14:59:26 +0000321 /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple
322 /// lines, in this context.
323 bool AvoidBinPacking;
324
325 /// \brief Break after the next comma (or all the commas in this context if
326 /// \c AvoidBinPacking is \c true).
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000327 bool BreakBeforeParameter;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000328
329 /// \brief This context already has a line with more than one parameter.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000330 bool HasMultiParameterLine;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000331
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000332 /// \brief The position of the colon in an ObjC method declaration/call.
333 unsigned ColonPos;
334
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000335 bool operator<(const ParenState &Other) const {
336 if (Indent != Other.Indent)
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000337 return Indent < Other.Indent;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000338 if (LastSpace != Other.LastSpace)
339 return LastSpace < Other.LastSpace;
340 if (FirstLessLess != Other.FirstLessLess)
341 return FirstLessLess < Other.FirstLessLess;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000342 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
343 return BreakBeforeClosingBrace;
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000344 if (QuestionColumn != Other.QuestionColumn)
345 return QuestionColumn < Other.QuestionColumn;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000346 if (AvoidBinPacking != Other.AvoidBinPacking)
347 return AvoidBinPacking;
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000348 if (BreakBeforeParameter != Other.BreakBeforeParameter)
349 return BreakBeforeParameter;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000350 if (HasMultiParameterLine != Other.HasMultiParameterLine)
351 return HasMultiParameterLine;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000352 if (ColonPos != Other.ColonPos)
353 return ColonPos < Other.ColonPos;
Daniel Jasperb3123142013-01-12 07:36:22 +0000354 return false;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000355 }
356 };
357
358 /// \brief The current state when indenting a unwrapped line.
359 ///
360 /// As the indenting tries different combinations this is copied by value.
361 struct LineState {
362 /// \brief The number of used columns in the current line.
363 unsigned Column;
364
365 /// \brief The token that needs to be next formatted.
366 const AnnotatedToken *NextToken;
367
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000368 /// \brief The column of the first variable name in a variable declaration.
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000369 ///
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000370 /// Used to align further variables if necessary.
Daniel Jasper2e603772013-01-29 11:21:01 +0000371 unsigned VariablePos;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000372
373 /// \brief \c true if this line contains a continued for-loop section.
374 bool LineContainsContinuedForLoopSection;
375
Daniel Jasper29f123b2013-02-08 15:28:42 +0000376 /// \brief The level of nesting inside (), [], <> and {}.
377 unsigned ParenLevel;
378
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000379 /// \brief A stack keeping track of properties applying to parenthesis
380 /// levels.
381 std::vector<ParenState> Stack;
382
383 /// \brief Comparison operator to be able to used \c LineState in \c map.
384 bool operator<(const LineState &Other) const {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000385 if (Other.NextToken != NextToken)
386 return Other.NextToken > NextToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000387 if (Other.Column != Column)
388 return Other.Column > Column;
Daniel Jasper2e603772013-01-29 11:21:01 +0000389 if (Other.VariablePos != VariablePos)
390 return Other.VariablePos < VariablePos;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000391 if (Other.LineContainsContinuedForLoopSection !=
392 LineContainsContinuedForLoopSection)
393 return LineContainsContinuedForLoopSection;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000394 if (Other.ParenLevel != ParenLevel)
395 return Other.ParenLevel < ParenLevel;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000396 return Other.Stack < Stack;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000397 }
398 };
399
Daniel Jasper20409152012-12-04 14:54:30 +0000400 /// \brief Appends the next token to \p State and updates information
401 /// necessary for indentation.
402 ///
403 /// Puts the token on the current line if \p Newline is \c true and adds a
404 /// line break and necessary indentation otherwise.
405 ///
406 /// If \p DryRun is \c false, also creates and stores the required
407 /// \c Replacement.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000408 void addTokenToState(bool Newline, bool DryRun, LineState &State) {
Daniel Jasper9c837d02013-01-09 07:06:56 +0000409 const AnnotatedToken &Current = *State.NextToken;
410 const AnnotatedToken &Previous = *State.NextToken->Parent;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000411 assert(State.Stack.size());
Daniel Jasperbac016b2012-12-03 18:12:45 +0000412
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000413 if (Current.Type == TT_ImplicitStringLiteral) {
414 State.Column += State.NextToken->FormatTok.WhiteSpaceLength +
415 State.NextToken->FormatTok.TokenLength;
416 if (State.NextToken->Children.empty())
417 State.NextToken = NULL;
418 else
419 State.NextToken = &State.NextToken->Children[0];
420 return;
421 }
422
Daniel Jasperbac016b2012-12-03 18:12:45 +0000423 if (Newline) {
Manuel Klimek060143e2013-01-02 18:33:23 +0000424 unsigned WhitespaceStartColumn = State.Column;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000425 if (Current.is(tok::r_brace)) {
426 State.Column = Line.Level * 2;
Daniel Jasper9c837d02013-01-09 07:06:56 +0000427 } else if (Current.is(tok::string_literal) &&
428 Previous.is(tok::string_literal)) {
429 State.Column = State.Column - Previous.FormatTok.TokenLength;
430 } else if (Current.is(tok::lessless) &&
Daniel Jasper29f123b2013-02-08 15:28:42 +0000431 State.Stack.back().FirstLessLess != 0) {
432 State.Column = State.Stack.back().FirstLessLess;
433 } else if (State.ParenLevel != 0 &&
Daniel Jasper5f2173e2013-01-28 07:43:15 +0000434 (Previous.is(tok::equal) || Previous.is(tok::coloncolon) ||
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000435 Current.is(tok::period) || Current.is(tok::arrow) ||
436 Current.is(tok::question))) {
Daniel Jasper9c837d02013-01-09 07:06:56 +0000437 // Indent and extra 4 spaces after if we know the current expression is
438 // continued. Don't do that on the top level, as we already indent 4
439 // there.
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000440 State.Column = std::max(State.Stack.back().LastSpace,
441 State.Stack.back().Indent) + 4;
442 } else if (Current.Type == TT_ConditionalExpr) {
443 State.Column = State.Stack.back().QuestionColumn;
Daniel Jasper2e603772013-01-29 11:21:01 +0000444 } else if (Previous.is(tok::comma) && State.VariablePos != 0 &&
Daniel Jasper29f123b2013-02-08 15:28:42 +0000445 ((RootToken.is(tok::kw_for) && State.ParenLevel == 1) ||
446 State.ParenLevel == 0)) {
Daniel Jasper2e603772013-01-29 11:21:01 +0000447 State.Column = State.VariablePos;
Daniel Jasper83f25ba2013-01-28 15:16:31 +0000448 } else if (State.NextToken->Parent->ClosesTemplateDeclaration ||
449 Current.Type == TT_StartOfName) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000450 State.Column = State.Stack.back().Indent - 4;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000451 } else if (Current.Type == TT_ObjCSelectorName) {
452 if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) {
453 State.Column =
454 State.Stack.back().ColonPos - Current.FormatTok.TokenLength;
455 } else {
456 State.Column = State.Stack.back().Indent;
457 State.Stack.back().ColonPos =
458 State.Column + Current.FormatTok.TokenLength;
459 }
460 } else if (Previous.Type == TT_ObjCMethodExpr) {
461 State.Column = State.Stack.back().Indent + 4;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000462 } else {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000463 State.Column = State.Stack.back().Indent;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000464 }
465
Daniel Jasperf343cab2013-01-31 14:59:26 +0000466 if (Previous.is(tok::comma) && !State.Stack.back().AvoidBinPacking)
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000467 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000468
Daniel Jasper26f7e782013-01-08 14:56:18 +0000469 if (RootToken.is(tok::kw_for))
Daniel Jasper9c837d02013-01-09 07:06:56 +0000470 State.LineContainsContinuedForLoopSection = Previous.isNot(tok::semi);
Daniel Jasper20409152012-12-04 14:54:30 +0000471
Manuel Klimek060143e2013-01-02 18:33:23 +0000472 if (!DryRun) {
473 if (!Line.InPPDirective)
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000474 Whitespaces.replaceWhitespace(Current, 1, State.Column,
475 WhitespaceStartColumn, Style);
Manuel Klimek060143e2013-01-02 18:33:23 +0000476 else
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000477 Whitespaces.replacePPWhitespace(Current, 1, State.Column,
478 WhitespaceStartColumn, Style);
Manuel Klimek060143e2013-01-02 18:33:23 +0000479 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000480
Daniel Jasper29f123b2013-02-08 15:28:42 +0000481 State.Stack.back().LastSpace = State.Column;
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000482 if (Current.is(tok::colon) && Current.Type != TT_ConditionalExpr)
Daniel Jasper29f123b2013-02-08 15:28:42 +0000483 State.Stack.back().Indent += 2;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000484 } else {
Daniel Jasper2e603772013-01-29 11:21:01 +0000485 if (Current.is(tok::equal) &&
Daniel Jasper29f123b2013-02-08 15:28:42 +0000486 (RootToken.is(tok::kw_for) || State.ParenLevel == 0))
Daniel Jasper2e603772013-01-29 11:21:01 +0000487 State.VariablePos = State.Column - Previous.FormatTok.TokenLength;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000488
Daniel Jasper729a7432013-02-11 12:36:37 +0000489 unsigned Spaces = State.NextToken->SpacesRequiredBefore;
Daniel Jasper20409152012-12-04 14:54:30 +0000490
Daniel Jasperbac016b2012-12-03 18:12:45 +0000491 if (!DryRun)
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000492 Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column, Style);
Daniel Jasper20409152012-12-04 14:54:30 +0000493
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000494 if (Current.Type == TT_ObjCSelectorName &&
495 State.Stack.back().ColonPos == 0) {
496 if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
497 State.Column + Spaces + Current.FormatTok.TokenLength)
498 State.Stack.back().ColonPos =
499 State.Stack.back().Indent + Current.LongestObjCSelectorName;
500 else
501 State.Stack.back().ColonPos =
Daniel Jasper9e9e6e02013-02-06 16:00:26 +0000502 State.Column + Spaces + Current.FormatTok.TokenLength;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000503 }
504
Daniel Jasperd4f2c2e2013-01-29 19:41:55 +0000505 if (Current.Type != TT_LineComment &&
506 (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
507 State.NextToken->Parent->Type == TT_TemplateOpener))
Daniel Jasper29f123b2013-02-08 15:28:42 +0000508 State.Stack.back().Indent = State.Column + Spaces;
Daniel Jaspercda16502013-02-04 08:34:57 +0000509 if (Previous.is(tok::comma) && !isTrailingComment(Current))
Daniel Jasper29f123b2013-02-08 15:28:42 +0000510 State.Stack.back().HasMultiParameterLine = true;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000511
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000512 State.Column += Spaces;
Daniel Jaspere438bac2013-01-23 20:41:06 +0000513 if (Current.is(tok::l_paren) && Previous.is(tok::kw_if))
514 // Treat the condition inside an if as if it was a second function
515 // parameter, i.e. let nested calls have an indent of 4.
516 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
Daniel Jasper29f123b2013-02-08 15:28:42 +0000517 else if (Previous.is(tok::comma) && State.ParenLevel != 0)
Daniel Jaspere438bac2013-01-23 20:41:06 +0000518 // Top-level spaces are exempt as that mostly leads to better results.
519 State.Stack.back().LastSpace = State.Column;
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000520 else if ((Previous.Type == TT_BinaryOperator ||
Daniel Jasper02b771e2013-01-28 13:31:35 +0000521 Previous.Type == TT_ConditionalExpr ||
522 Previous.Type == TT_CtorInitializerColon) &&
Daniel Jasperae8699b2013-01-28 09:35:24 +0000523 getPrecedence(Previous) != prec::Assignment)
524 State.Stack.back().LastSpace = State.Column;
Daniel Jasper986e17f2013-01-28 07:35:34 +0000525 else if (Previous.ParameterCount > 1 &&
526 (Previous.is(tok::l_paren) || Previous.is(tok::l_square) ||
Daniel Jasperf343cab2013-01-31 14:59:26 +0000527 Previous.is(tok::l_brace) ||
Daniel Jasper986e17f2013-01-28 07:35:34 +0000528 Previous.Type == TT_TemplateOpener))
529 // If this function has multiple parameters, indent nested calls from
530 // the start of the first parameter.
531 State.Stack.back().LastSpace = State.Column;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000532 }
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000533
534 // If we break after an {, we should also break before the corresponding }.
535 if (Newline && Previous.is(tok::l_brace))
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000536 State.Stack.back().BreakBeforeClosingBrace = true;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000537
Daniel Jasper8159d2f2013-02-04 07:30:30 +0000538 if (State.Stack.back().AvoidBinPacking && Newline &&
Daniel Jasper29f123b2013-02-08 15:28:42 +0000539 (Line.First.isNot(tok::kw_for) || State.ParenLevel != 1)) {
Daniel Jasper8f4bd7a2013-01-23 10:08:28 +0000540 // If we are breaking after '(', '{', '<', this is not bin packing unless
Daniel Jasperf1579602013-01-29 16:03:49 +0000541 // AllowAllParametersOfDeclarationOnNextLine is false.
Daniel Jasper8f4bd7a2013-01-23 10:08:28 +0000542 if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace) &&
543 Previous.Type != TT_TemplateOpener) ||
Daniel Jasperf1579602013-01-29 16:03:49 +0000544 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
545 Line.MustBeDeclaration))
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000546 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasper2e603772013-01-29 11:21:01 +0000547
Daniel Jasper8f4bd7a2013-01-23 10:08:28 +0000548 // Any break on this level means that the parent level has been broken
549 // and we need to avoid bin packing there.
550 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
Daniel Jasper8159d2f2013-02-04 07:30:30 +0000551 if (Line.First.isNot(tok::kw_for) || i != 1)
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000552 State.Stack[i].BreakBeforeParameter = true;
Daniel Jasper8f4bd7a2013-01-23 10:08:28 +0000553 }
554 }
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000555
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000556 moveStateToNextToken(State);
Daniel Jasper20409152012-12-04 14:54:30 +0000557 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000558
Daniel Jasper20409152012-12-04 14:54:30 +0000559 /// \brief Mark the next token as consumed in \p State and modify its stacks
560 /// accordingly.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000561 void moveStateToNextToken(LineState &State) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000562 const AnnotatedToken &Current = *State.NextToken;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000563 assert(State.Stack.size());
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000564
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000565 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
566 State.Stack.back().FirstLessLess = State.Column;
Daniel Jasperbfe6fd42013-01-28 12:45:14 +0000567 if (Current.is(tok::question))
568 State.Stack.back().QuestionColumn = State.Column;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000569 if (Current.is(tok::l_brace) && Current.MatchingParen != NULL &&
Daniel Jasperf343cab2013-01-31 14:59:26 +0000570 !Current.MatchingParen->MustBreakBefore) {
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000571 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
572 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000573 }
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000574
Daniel Jasper29f123b2013-02-08 15:28:42 +0000575 // Insert scopes created by fake parenthesis.
576 for (unsigned i = 0, e = Current.FakeLParens; i != e; ++i) {
577 ParenState NewParenState = State.Stack.back();
578 NewParenState.Indent = std::max(State.Column, State.Stack.back().Indent);
579 State.Stack.push_back(NewParenState);
580 }
581
Daniel Jaspercf225b62012-12-24 13:43:52 +0000582 // If we encounter an opening (, [, { or <, we add a level to our stacks to
Daniel Jasper20409152012-12-04 14:54:30 +0000583 // prepare for the following tokens.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000584 if (Current.is(tok::l_paren) || Current.is(tok::l_square) ||
585 Current.is(tok::l_brace) ||
586 State.NextToken->Type == TT_TemplateOpener) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000587 unsigned NewIndent;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000588 bool AvoidBinPacking;
Manuel Klimek2851c162013-01-10 14:36:46 +0000589 if (Current.is(tok::l_brace)) {
Daniel Jasperf343cab2013-01-31 14:59:26 +0000590 NewIndent = 2 + State.Stack.back().LastSpace;
591 AvoidBinPacking = false;
Manuel Klimek2851c162013-01-10 14:36:46 +0000592 } else {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000593 NewIndent = 4 + State.Stack.back().LastSpace;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000594 AvoidBinPacking = !Style.BinPackParameters;
Manuel Klimek2851c162013-01-10 14:36:46 +0000595 }
Daniel Jasperd399bff2013-02-05 09:41:21 +0000596 State.Stack.push_back(
597 ParenState(NewIndent, State.Stack.back().LastSpace, AvoidBinPacking,
598 State.Stack.back().HasMultiParameterLine));
Daniel Jasper29f123b2013-02-08 15:28:42 +0000599 ++State.ParenLevel;
Daniel Jasper20409152012-12-04 14:54:30 +0000600 }
601
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000602 // If this '[' opens an ObjC call, determine whether all parameters fit into
603 // one line and put one per line if they don't.
604 if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
605 Current.MatchingParen != NULL) {
606 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
607 State.Stack.back().BreakBeforeParameter = true;
608 }
609
Daniel Jaspercf225b62012-12-24 13:43:52 +0000610 // If we encounter a closing ), ], } or >, we can remove a level from our
Daniel Jasper20409152012-12-04 14:54:30 +0000611 // stacks.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000612 if (Current.is(tok::r_paren) || Current.is(tok::r_square) ||
613 (Current.is(tok::r_brace) && State.NextToken != &RootToken) ||
614 State.NextToken->Type == TT_TemplateCloser) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000615 State.Stack.pop_back();
Daniel Jasper29f123b2013-02-08 15:28:42 +0000616 --State.ParenLevel;
617 }
618
619 // Remove scopes created by fake parenthesis.
620 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
621 State.Stack.pop_back();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000622 }
Manuel Klimek2851c162013-01-10 14:36:46 +0000623
Daniel Jasper26f7e782013-01-08 14:56:18 +0000624 if (State.NextToken->Children.empty())
625 State.NextToken = NULL;
626 else
627 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek2851c162013-01-10 14:36:46 +0000628
629 State.Column += Current.FormatTok.TokenLength;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000630 }
631
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000632 unsigned getColumnLimit() {
633 return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0);
634 }
635
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000636 /// \brief An edge in the solution space starting from the \c LineState and
637 /// inserting a newline dependent on the \c bool.
638 typedef std::pair<bool, const LineState *> Edge;
639
640 /// \brief An item in the prioritized BFS search queue. The \c LineState was
641 /// reached using the \c Edge.
642 typedef std::pair<LineState, Edge> QueueItem;
643
644 /// \brief Analyze the entire solution space starting from \p InitialState.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000645 ///
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000646 /// This implements a variant of Dijkstra's algorithm on the graph that spans
647 /// the solution space (\c LineStates are the nodes). The algorithm tries to
648 /// find the shortest path (the one with lowest penalty) from \p InitialState
649 /// to a state where all tokens are placed.
650 unsigned analyzeSolutionSpace(const LineState &InitialState) {
651 // Insert start element into queue.
652 std::multimap<unsigned, QueueItem> Queue;
653 Queue.insert(std::pair<unsigned, QueueItem>(
Daniel Jasper01786732013-02-04 07:21:18 +0000654 0, QueueItem(InitialState, Edge(false, (const LineState *)0))));
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000655 std::map<LineState, Edge> Seen;
656
657 // While not empty, take first element and follow edges.
658 while (!Queue.empty()) {
659 unsigned Penalty = Queue.begin()->first;
660 QueueItem Item = Queue.begin()->second;
Daniel Jasper01786732013-02-04 07:21:18 +0000661 if (Item.first.NextToken == NULL) {
662 DEBUG(llvm::errs() << "\n---\nPenalty for line: " << Penalty << "\n");
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000663 break;
Daniel Jasper01786732013-02-04 07:21:18 +0000664 }
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000665 Queue.erase(Queue.begin());
666
667 if (Seen.find(Item.first) != Seen.end())
668 continue; // State already examined with lower penalty.
669
670 const LineState &SavedState = Seen.insert(std::pair<LineState, Edge>(
671 Item.first,
672 Edge(Item.second.first, Item.second.second))).first->first;
673
674 addNextStateToQueue(SavedState, Penalty, /*NewLine=*/ false, Queue);
675 addNextStateToQueue(SavedState, Penalty, /*NewLine=*/ true, Queue);
676 }
677
678 if (Queue.empty())
679 // We were unable to find a solution, do nothing.
680 // FIXME: Add diagnostic?
Daniel Jasperbac016b2012-12-03 18:12:45 +0000681 return 0;
682
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000683 // Reconstruct the solution.
684 // FIXME: Add debugging output.
685 Edge *CurrentEdge = &Queue.begin()->second.second;
686 while (CurrentEdge->second != NULL) {
687 LineState CurrentState = *CurrentEdge->second;
Daniel Jasper01786732013-02-04 07:21:18 +0000688 if (CurrentEdge->first) {
689 DEBUG(llvm::errs() << "Penalty for splitting before "
690 << CurrentState.NextToken->FormatTok.Tok.getName()
691 << ": " << CurrentState.NextToken->SplitPenalty
692 << "\n");
693 }
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000694 addTokenToState(CurrentEdge->first, false, CurrentState);
695 CurrentEdge = &Seen[*CurrentEdge->second];
696 }
Daniel Jasper01786732013-02-04 07:21:18 +0000697 DEBUG(llvm::errs() << "---\n");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000698
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000699 // Return the column after the last token of the solution.
700 return Queue.begin()->second.first.Column;
701 }
702
703 /// \brief Add the following state to the analysis queue \p Queue.
704 ///
705 /// Assume the current state is \p OldState and has been reached with a
706 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
707 void addNextStateToQueue(const LineState &OldState, unsigned Penalty,
708 bool NewLine,
709 std::multimap<unsigned, QueueItem> &Queue) {
710 if (NewLine && !canBreak(OldState))
711 return;
712 if (!NewLine && mustBreak(OldState))
713 return;
714 LineState State(OldState);
Daniel Jasperae8699b2013-01-28 09:35:24 +0000715 if (NewLine)
Daniel Jasper01786732013-02-04 07:21:18 +0000716 Penalty += State.NextToken->SplitPenalty;
Daniel Jasper20409152012-12-04 14:54:30 +0000717 addTokenToState(NewLine, true, State);
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000718 if (State.Column > getColumnLimit()) {
719 unsigned ExcessCharacters = State.Column - getColumnLimit();
Daniel Jasper01786732013-02-04 07:21:18 +0000720 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000721 }
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000722 Queue.insert(std::pair<unsigned, QueueItem>(
723 Penalty, QueueItem(State, Edge(NewLine, &OldState))));
724 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000725
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000726 /// \brief Returns \c true, if a line break after \p State is allowed.
727 bool canBreak(const LineState &State) {
728 if (!State.NextToken->CanBreakBefore &&
729 !(State.NextToken->is(tok::r_brace) &&
730 State.Stack.back().BreakBeforeClosingBrace))
731 return false;
732 // Trying to insert a parameter on a new line if there are already more than
733 // one parameter on the current line is bin packing.
Daniel Jasperd399bff2013-02-05 09:41:21 +0000734 if (State.Stack.back().HasMultiParameterLine &&
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000735 State.Stack.back().AvoidBinPacking)
736 return false;
737 return true;
738 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000739
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000740 /// \brief Returns \c true, if a line break after \p State is mandatory.
741 bool mustBreak(const LineState &State) {
742 if (State.NextToken->MustBreakBefore)
743 return true;
744 if (State.NextToken->is(tok::r_brace) &&
745 State.Stack.back().BreakBeforeClosingBrace)
746 return true;
747 if (State.NextToken->Parent->is(tok::semi) &&
748 State.LineContainsContinuedForLoopSection)
749 return true;
750 if (State.NextToken->Parent->is(tok::comma) &&
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000751 State.Stack.back().BreakBeforeParameter &&
Daniel Jaspercda16502013-02-04 08:34:57 +0000752 !isTrailingComment(*State.NextToken))
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000753 return true;
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000754 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
755 // out whether it is the first parameter. Clean this up.
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000756 if (State.NextToken->Type == TT_ObjCSelectorName &&
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000757 State.NextToken->LongestObjCSelectorName == 0 &&
758 State.Stack.back().BreakBeforeParameter)
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000759 return true;
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000760 if ((State.NextToken->Type == TT_CtorInitializerColon ||
761 (State.NextToken->Parent->ClosesTemplateDeclaration &&
Daniel Jasper29f123b2013-02-08 15:28:42 +0000762 State.ParenLevel == 0)))
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000763 return true;
764 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000765 }
766
Daniel Jasperbac016b2012-12-03 18:12:45 +0000767 FormatStyle Style;
768 SourceManager &SourceMgr;
Daniel Jasper995e8202013-01-14 13:08:07 +0000769 const AnnotatedLine &Line;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000770 const unsigned FirstIndent;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000771 const AnnotatedToken &RootToken;
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000772 WhitespaceManager &Whitespaces;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000773};
774
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000775class LexerBasedFormatTokenSource : public FormatTokenSource {
776public:
777 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
Daniel Jasper1321eb52012-12-18 21:05:13 +0000778 : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000779 IdentTable(Lex.getLangOpts()) {
780 Lex.SetKeepWhitespaceMode(true);
781 }
782
783 virtual FormatToken getNextToken() {
784 if (GreaterStashed) {
785 FormatTok.NewlinesBefore = 0;
786 FormatTok.WhiteSpaceStart =
787 FormatTok.Tok.getLocation().getLocWithOffset(1);
788 FormatTok.WhiteSpaceLength = 0;
789 GreaterStashed = false;
790 return FormatTok;
791 }
792
793 FormatTok = FormatToken();
794 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimek95419382013-01-07 07:56:50 +0000795 StringRef Text = rawTokenText(FormatTok.Tok);
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000796 FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000797 if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0)
798 FormatTok.IsFirst = true;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000799
800 // Consume and record whitespace until we find a significant token.
801 while (FormatTok.Tok.is(tok::unknown)) {
Manuel Klimeka28fc062013-02-11 12:33:24 +0000802 unsigned Newlines = Text.count('\n');
803 unsigned EscapedNewlines = Text.count("\\\n");
804 FormatTok.NewlinesBefore += Newlines;
805 FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000806 FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
807
808 if (FormatTok.Tok.is(tok::eof))
809 return FormatTok;
810 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimek95419382013-01-07 07:56:50 +0000811 Text = rawTokenText(FormatTok.Tok);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000812 }
Manuel Klimek95419382013-01-07 07:56:50 +0000813
814 // Now FormatTok is the next non-whitespace token.
815 FormatTok.TokenLength = Text.size();
816
Manuel Klimekd4397b92013-01-04 23:34:14 +0000817 // In case the token starts with escaped newlines, we want to
818 // take them into account as whitespace - this pattern is quite frequent
819 // in macro definitions.
820 // FIXME: What do we want to do with other escaped spaces, and escaped
821 // spaces or newlines in the middle of tokens?
822 // FIXME: Add a more explicit test.
823 unsigned i = 0;
Daniel Jasper71607512013-01-07 10:48:50 +0000824 while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
Manuel Klimek86721d22013-01-22 16:31:55 +0000825 // FIXME: ++FormatTok.NewlinesBefore is missing...
Manuel Klimekd4397b92013-01-04 23:34:14 +0000826 FormatTok.WhiteSpaceLength += 2;
Manuel Klimek95419382013-01-07 07:56:50 +0000827 FormatTok.TokenLength -= 2;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000828 i += 2;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000829 }
830
831 if (FormatTok.Tok.is(tok::raw_identifier)) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000832 IdentifierInfo &Info = IdentTable.get(Text);
Daniel Jaspercd1a32b2012-12-21 17:58:39 +0000833 FormatTok.Tok.setIdentifierInfo(&Info);
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000834 FormatTok.Tok.setKind(Info.getTokenID());
835 }
836
837 if (FormatTok.Tok.is(tok::greatergreater)) {
838 FormatTok.Tok.setKind(tok::greater);
839 GreaterStashed = true;
840 }
841
842 return FormatTok;
843 }
844
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000845 IdentifierTable &getIdentTable() { return IdentTable; }
846
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000847private:
848 FormatToken FormatTok;
849 bool GreaterStashed;
850 Lexer &Lex;
851 SourceManager &SourceMgr;
852 IdentifierTable IdentTable;
853
854 /// Returns the text of \c FormatTok.
Manuel Klimek95419382013-01-07 07:56:50 +0000855 StringRef rawTokenText(Token &Tok) {
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000856 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
857 Tok.getLength());
858 }
859};
860
Daniel Jasperbac016b2012-12-03 18:12:45 +0000861class Formatter : public UnwrappedLineConsumer {
862public:
Daniel Jasperfeb18f52013-01-14 14:14:23 +0000863 Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex,
864 SourceManager &SourceMgr,
Daniel Jasperbac016b2012-12-03 18:12:45 +0000865 const std::vector<CharSourceRange> &Ranges)
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000866 : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000867 Whitespaces(SourceMgr), Ranges(Ranges) {
868 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000869
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000870 virtual ~Formatter() {}
Daniel Jasperaccb0b02012-12-04 21:05:31 +0000871
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000872 void deriveLocalStyle() {
873 unsigned CountBoundToVariable = 0;
874 unsigned CountBoundToType = 0;
875 bool HasCpp03IncompatibleFormat = false;
876 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
877 if (AnnotatedLines[i].First.Children.empty())
878 continue;
879 AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0];
880 while (!Tok->Children.empty()) {
881 if (Tok->Type == TT_PointerOrReference) {
882 bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0;
883 bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0;
884 if (SpacesBefore && !SpacesAfter)
885 ++CountBoundToVariable;
886 else if (!SpacesBefore && SpacesAfter)
887 ++CountBoundToType;
888 }
889
Daniel Jasper29f123b2013-02-08 15:28:42 +0000890 if (Tok->Type == TT_TemplateCloser &&
891 Tok->Parent->Type == TT_TemplateCloser &&
892 Tok->FormatTok.WhiteSpaceLength == 0)
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000893 HasCpp03IncompatibleFormat = true;
894 Tok = &Tok->Children[0];
895 }
896 }
897 if (Style.DerivePointerBinding) {
898 if (CountBoundToType > CountBoundToVariable)
899 Style.PointerBindsToType = true;
900 else if (CountBoundToType < CountBoundToVariable)
901 Style.PointerBindsToType = false;
902 }
903 if (Style.Standard == FormatStyle::LS_Auto) {
904 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
905 : FormatStyle::LS_Cpp03;
906 }
907 }
908
Daniel Jasperbac016b2012-12-03 18:12:45 +0000909 tooling::Replacements format() {
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000910 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000911 UnwrappedLineParser Parser(Diag, Style, Tokens, *this);
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000912 StructuralError = Parser.parse();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000913 unsigned PreviousEndOfLineColumn = 0;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000914 TokenAnnotator Annotator(Style, SourceMgr, Lex,
915 Tokens.getIdentTable().get("in"));
Daniel Jasper995e8202013-01-14 13:08:07 +0000916 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper8ff690a2013-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 Jasper995e8202013-01-14 13:08:07 +0000922 }
Manuel Klimek547d5db2013-02-08 17:38:27 +0000923 std::vector<int> IndentForLevel;
Daniel Jasper995e8202013-01-14 13:08:07 +0000924 for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
925 E = AnnotatedLines.end();
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +0000926 I != E; ++I) {
Daniel Jasper995e8202013-01-14 13:08:07 +0000927 const AnnotatedLine &TheLine = *I;
Manuel Klimek547d5db2013-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 Jasper995e8202013-01-14 13:08:07 +0000932 if (touchesRanges(TheLine) && TheLine.Type != LT_Invalid) {
Manuel Klimek547d5db2013-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 Jasper3f8cdbf2013-01-16 10:41:46 +0000945 tryFitMultipleLinesInOne(Indent, I, E);
Daniel Jasper995e8202013-01-14 13:08:07 +0000946 UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000947 TheLine.First, Whitespaces,
Daniel Jasper995e8202013-01-14 13:08:07 +0000948 StructuralError);
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +0000949 PreviousEndOfLineColumn = Formatter.format();
Manuel Klimek547d5db2013-02-08 17:38:27 +0000950 IndentForLevel[TheLine.Level] = LevelIndent;
Manuel Klimekf9ea2ed2013-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 Klimek547d5db2013-02-08 17:38:27 +0000954 // last token.
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +0000955 PreviousEndOfLineColumn =
Daniel Jasper995e8202013-01-14 13:08:07 +0000956 SourceMgr.getSpellingColumnNumber(
957 TheLine.Last->FormatTok.Tok.getLocation()) +
958 Lex.MeasureTokenLength(TheLine.Last->FormatTok.Tok.getLocation(),
Daniel Jasper1a1ce832013-01-29 11:27:30 +0000959 SourceMgr, Lex.getLangOpts()) - 1;
Manuel Klimek547d5db2013-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 Klimekf9ea2ed2013-01-10 19:49:59 +0000966 }
967 }
Daniel Jasperdcc2a622013-01-18 08:44:07 +0000968 return Whitespaces.generateReplacements();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000969 }
970
971private:
Manuel Klimek547d5db2013-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 Klimek547d5db2013-02-08 17:38:27 +0000979 if (IndentForLevel[Level] != -1)
980 return IndentForLevel[Level];
Manuel Klimek52635ff2013-02-08 19:53:32 +0000981 if (Level == 0)
982 return 0;
Manuel Klimek547d5db2013-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 Klimek517e8942013-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 Jasper3f8cdbf2013-01-16 10:41:46 +00001013 void tryFitMultipleLinesInOne(unsigned Indent,
Daniel Jasper995e8202013-01-14 13:08:07 +00001014 std::vector<AnnotatedLine>::iterator &I,
1015 std::vector<AnnotatedLine>::iterator E) {
Manuel Klimek517e8942013-01-11 17:54:10 +00001016 unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent;
1017
Daniel Jasper3f8cdbf2013-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 Klimek517e8942013-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 Klimek2f1ac412013-01-21 16:42:44 +00001025 if (I->Last->TotalLength > Limit)
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001026 return;
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001027 Limit -= I->Last->TotalLength;
Daniel Jasper55b08e72013-01-16 07:02:34 +00001028
Daniel Jasper9c8c40e2013-01-21 14:18:28 +00001029 if (I + 1 == E || (I + 1)->Type == LT_Invalid)
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001030 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001031
Daniel Jasperfeb18f52013-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 Jaspere0b15ea2013-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 Jasperfeb18f52013-01-14 14:14:23 +00001039 }
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001040 return;
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001041 }
1042
Daniel Jaspere0b15ea2013-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 Jasper2b9c10b2013-01-14 15:52:06 +00001047 if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline)
1048 return;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001049 if (I + 2 != E && (I + 2)->InPPDirective &&
1050 !(I + 2)->First.FormatTok.HasUnescapedNewline)
1051 return;
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001052 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001053 return;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001054 join(Line, *(++I));
1055 }
1056
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001057 void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I,
1058 std::vector<AnnotatedLine>::iterator E,
1059 unsigned Limit) {
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001060 if (!Style.AllowShortIfStatementsOnASingleLine)
1061 return;
Manuel Klimek4c128122013-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 Jasperfeb18f52013-01-14 14:14:23 +00001066 AnnotatedLine &Line = *I;
Daniel Jasper55b08e72013-01-16 07:02:34 +00001067 if (Line.Last->isNot(tok::r_paren))
1068 return;
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001069 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jasperfeb18f52013-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 Jasper1a1ce832013-01-29 11:27:30 +00001080 std::vector<AnnotatedLine>::iterator E,
1081 unsigned Limit) {
Manuel Klimek517e8942013-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 Jasperfeb18f52013-01-14 14:14:23 +00001085 AnnotatedLine &Line = *I;
Manuel Klimek517e8942013-01-11 17:54:10 +00001086 bool AllowedTokens =
Daniel Jasper995e8202013-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 Weber67015ed2013-01-11 21:14:08 +00001091 // This gets rid of all ObjC @ keywords and methods.
Daniel Jasper995e8202013-01-14 13:08:07 +00001092 Line.First.isNot(tok::at) && Line.First.isNot(tok::minus) &&
1093 Line.First.isNot(tok::plus);
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001094 if (!AllowedTokens)
1095 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001096
Manuel Klimek2f1ac412013-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 Jasper729a7432013-02-11 12:36:37 +00001100 Tok->SpacesRequiredBefore = 0;
Manuel Klimek2f1ac412013-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 Jasperfeb18f52013-01-14 14:14:23 +00001107 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001108
Manuel Klimek2f1ac412013-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 Klimek517e8942013-01-11 17:54:10 +00001118
Manuel Klimek2f1ac412013-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 Klimek517e8942013-01-11 17:54:10 +00001128 }
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001129 }
1130
1131 bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
1132 unsigned Limit) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001133 return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
1134 Limit;
Manuel Klimek517e8942013-01-11 17:54:10 +00001135 }
1136
Daniel Jasper995e8202013-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 Klimekf9ea2ed2013-01-10 19:49:59 +00001143 }
1144
Daniel Jasper995e8202013-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 Jaspercd162382013-01-07 13:26:07 +00001148 CharSourceRange LineRange = CharSourceRange::getTokenRange(
Daniel Jasper1a1ce832013-01-29 11:27:30 +00001149 First->Tok.getLocation(), Last->Tok.getLocation());
Daniel Jasperbac016b2012-12-03 18:12:45 +00001150 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
Manuel Klimekf9ea2ed2013-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 Jasperbac016b2012-12-03 18:12:45 +00001156 }
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001157 return false;
1158 }
1159
1160 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001161 AnnotatedLines.push_back(AnnotatedLine(TheLine));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001162 }
1163
Manuel Klimek3f8c7f32013-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 Klimek547d5db2013-02-08 17:38:27 +00001167 void formatFirstToken(const AnnotatedToken &RootToken, unsigned Indent,
1168 bool InPPDirective, unsigned PreviousEndOfLineColumn) {
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001169 const FormatToken &Tok = RootToken.FormatTok;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001170
Daniel Jasper1a1ce832013-01-29 11:27:30 +00001171 unsigned Newlines =
1172 std::min(Tok.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001173 if (Newlines == 0 && !Tok.IsFirst)
1174 Newlines = 1;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001175
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001176 if (!InPPDirective || Tok.HasUnescapedNewline) {
Daniel Jasperdcc2a622013-01-18 08:44:07 +00001177 Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0, Style);
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001178 } else {
Daniel Jasperdcc2a622013-01-18 08:44:07 +00001179 Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent,
1180 PreviousEndOfLineColumn, Style);
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001181 }
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001182 }
1183
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001184 DiagnosticsEngine &Diag;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001185 FormatStyle Style;
1186 Lexer &Lex;
1187 SourceManager &SourceMgr;
Daniel Jasperdcc2a622013-01-18 08:44:07 +00001188 WhitespaceManager Whitespaces;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001189 std::vector<CharSourceRange> Ranges;
Daniel Jasper995e8202013-01-14 13:08:07 +00001190 std::vector<AnnotatedLine> AnnotatedLines;
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001191 bool StructuralError;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001192};
1193
Daniel Jasper1a1ce832013-01-29 11:27:30 +00001194tooling::Replacements
1195reformat(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
1196 std::vector<CharSourceRange> Ranges, DiagnosticConsumer *DiagClient) {
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001197 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Alexander Kornienkoa4ae9f32013-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 Kornienko3048aea2013-01-10 15:05:09 +00001204 DiagnosticsEngine Diagnostics(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001205 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001206 DiagClient, false);
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001207 Diagnostics.setSourceManager(&SourceMgr);
1208 Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001209 return formatter.format();
1210}
1211
Daniel Jasper46ef8522013-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 Jaspercd162382013-01-07 13:26:07 +00001222} // namespace format
1223} // namespace clang