blob: 83be79ea521ac89c5b05253d43e879fafdc57cc9 [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///
14/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,
15/// where it can be used to format real code.
16///
17//===----------------------------------------------------------------------===//
18
19#include "clang/Format/Format.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "UnwrappedLineParser.h"
Alexander Kornienko3048aea2013-01-10 15:05:09 +000021#include "clang/Basic/Diagnostic.h"
Daniel Jasper675d2e32012-12-21 10:20:02 +000022#include "clang/Basic/OperatorPrecedence.h"
Chandler Carruthb99083e2013-01-02 10:28:36 +000023#include "clang/Basic/SourceManager.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"
Daniel Jasper8822d3a2012-12-04 13:02:32 +000026#include <string>
27
Daniel Jasperbac016b2012-12-03 18:12:45 +000028namespace clang {
29namespace format {
30
Daniel Jasper71607512013-01-07 10:48:50 +000031enum TokenType {
Daniel Jasper71607512013-01-07 10:48:50 +000032 TT_BinaryOperator,
Daniel Jasper5cf7cf32013-01-10 11:14:08 +000033 TT_BlockComment,
34 TT_CastRParen,
Daniel Jasper71607512013-01-07 10:48:50 +000035 TT_ConditionalExpr,
36 TT_CtorInitializerColon,
Daniel Jasper71607512013-01-07 10:48:50 +000037 TT_DirectorySeparator,
Daniel Jasper5cf7cf32013-01-10 11:14:08 +000038 TT_LineComment,
Daniel Jasper46ef8522013-01-10 13:08:12 +000039 TT_ObjCBlockLParen,
Nico Webered91bba2013-01-10 19:19:14 +000040 TT_ObjCDecl,
Daniel Jasper5cf7cf32013-01-10 11:14:08 +000041 TT_ObjCMethodSpecifier,
Nico Weberbcfdd262013-01-12 06:18:40 +000042 TT_ObjCMethodExpr,
Nico Webercd52bda2013-01-10 23:11:41 +000043 TT_ObjCSelectorStart,
Nico Weber70848232013-01-10 21:30:42 +000044 TT_ObjCProperty,
Daniel Jasper5cf7cf32013-01-10 11:14:08 +000045 TT_OverloadedOperator,
46 TT_PointerOrReference,
Daniel Jasper71607512013-01-07 10:48:50 +000047 TT_PureVirtualSpecifier,
Daniel Jasper5cf7cf32013-01-10 11:14:08 +000048 TT_TemplateCloser,
49 TT_TemplateOpener,
50 TT_TrailingUnaryOperator,
51 TT_UnaryOperator,
52 TT_Unknown
Daniel Jasper71607512013-01-07 10:48:50 +000053};
54
55enum LineType {
56 LT_Invalid,
57 LT_Other,
58 LT_PreprocessorDirective,
59 LT_VirtualFunctionDecl,
Nico Webered91bba2013-01-10 19:19:14 +000060 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
Nico Weber70848232013-01-10 21:30:42 +000061 LT_ObjCMethodDecl,
62 LT_ObjCProperty // An @property line.
Daniel Jasper71607512013-01-07 10:48:50 +000063};
64
Daniel Jasper26f7e782013-01-08 14:56:18 +000065class AnnotatedToken {
66public:
67 AnnotatedToken(const FormatToken &FormatTok)
Manuel Klimek94fc6f12013-01-10 19:17:33 +000068 : FormatTok(FormatTok), Type(TT_Unknown), SpaceRequiredBefore(false),
69 CanBreakBefore(false), MustBreakBefore(false),
70 ClosesTemplateDeclaration(false), Parent(NULL) {}
Daniel Jasper26f7e782013-01-08 14:56:18 +000071
72 bool is(tok::TokenKind Kind) const {
73 return FormatTok.Tok.is(Kind);
74 }
75 bool isNot(tok::TokenKind Kind) const {
76 return FormatTok.Tok.isNot(Kind);
77 }
78 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
79 return FormatTok.Tok.isObjCAtKeyword(Kind);
80 }
81
82 FormatToken FormatTok;
83
Daniel Jasperbac016b2012-12-03 18:12:45 +000084 TokenType Type;
85
Daniel Jasperbac016b2012-12-03 18:12:45 +000086 bool SpaceRequiredBefore;
87 bool CanBreakBefore;
88 bool MustBreakBefore;
Daniel Jasper9a64fb52013-01-02 15:08:56 +000089
90 bool ClosesTemplateDeclaration;
Daniel Jasper26f7e782013-01-08 14:56:18 +000091
92 std::vector<AnnotatedToken> Children;
93 AnnotatedToken *Parent;
Daniel Jasperbac016b2012-12-03 18:12:45 +000094};
95
Daniel Jasper26f7e782013-01-08 14:56:18 +000096static prec::Level getPrecedence(const AnnotatedToken &Tok) {
97 return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true);
Daniel Jaspercf225b62012-12-24 13:43:52 +000098}
99
Daniel Jasperbac016b2012-12-03 18:12:45 +0000100FormatStyle getLLVMStyle() {
101 FormatStyle LLVMStyle;
102 LLVMStyle.ColumnLimit = 80;
103 LLVMStyle.MaxEmptyLinesToKeep = 1;
104 LLVMStyle.PointerAndReferenceBindToType = false;
105 LLVMStyle.AccessModifierOffset = -2;
106 LLVMStyle.SplitTemplateClosingGreater = true;
Alexander Kornienko15757312012-12-06 18:03:27 +0000107 LLVMStyle.IndentCaseLabels = false;
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000108 LLVMStyle.SpacesBeforeTrailingComments = 1;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000109 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Nico Weber5f500df2013-01-10 20:12:55 +0000110 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Nico Webercd52bda2013-01-10 23:11:41 +0000111 LLVMStyle.ObjCSpaceBeforeReturnType = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000112 return LLVMStyle;
113}
114
115FormatStyle getGoogleStyle() {
116 FormatStyle GoogleStyle;
117 GoogleStyle.ColumnLimit = 80;
118 GoogleStyle.MaxEmptyLinesToKeep = 1;
119 GoogleStyle.PointerAndReferenceBindToType = true;
120 GoogleStyle.AccessModifierOffset = -1;
121 GoogleStyle.SplitTemplateClosingGreater = false;
Alexander Kornienko15757312012-12-06 18:03:27 +0000122 GoogleStyle.IndentCaseLabels = true;
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000123 GoogleStyle.SpacesBeforeTrailingComments = 2;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000124 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
Nico Weber5f500df2013-01-10 20:12:55 +0000125 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Nico Webercd52bda2013-01-10 23:11:41 +0000126 GoogleStyle.ObjCSpaceBeforeReturnType = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000127 return GoogleStyle;
128}
129
130struct OptimizationParameters {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000131 unsigned PenaltyIndentLevel;
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000132 unsigned PenaltyLevelDecrease;
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000133 unsigned PenaltyExcessCharacter;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000134};
135
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000136/// \brief Replaces the whitespace in front of \p Tok. Only call once for
137/// each \c FormatToken.
138static void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
139 unsigned Spaces, const FormatStyle &Style,
140 SourceManager &SourceMgr,
141 tooling::Replacements &Replaces) {
142 Replaces.insert(tooling::Replacement(
143 SourceMgr, Tok.FormatTok.WhiteSpaceStart, Tok.FormatTok.WhiteSpaceLength,
144 std::string(NewLines, '\n') + std::string(Spaces, ' ')));
145}
146
147/// \brief Like \c replaceWhitespace, but additionally adds right-aligned
148/// backslashes to escape newlines inside a preprocessor directive.
149///
150/// This function and \c replaceWhitespace have the same behavior if
151/// \c Newlines == 0.
152static void replacePPWhitespace(
153 const AnnotatedToken &Tok, unsigned NewLines, unsigned Spaces,
154 unsigned WhitespaceStartColumn, const FormatStyle &Style,
155 SourceManager &SourceMgr, tooling::Replacements &Replaces) {
156 std::string NewLineText;
157 if (NewLines > 0) {
158 unsigned Offset = std::min<int>(Style.ColumnLimit - 1,
159 WhitespaceStartColumn);
160 for (unsigned i = 0; i < NewLines; ++i) {
161 NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' ');
162 NewLineText += "\\\n";
163 Offset = 0;
164 }
165 }
166 Replaces.insert(tooling::Replacement(SourceMgr, Tok.FormatTok.WhiteSpaceStart,
167 Tok.FormatTok.WhiteSpaceLength,
168 NewLineText + std::string(Spaces, ' ')));
169}
170
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000171/// \brief Checks whether the (remaining) \c UnwrappedLine starting with
172/// \p RootToken fits into \p Limit columns.
173bool fitsIntoLimit(const AnnotatedToken &RootToken, unsigned Limit) {
174 unsigned Columns = RootToken.FormatTok.TokenLength;
175 bool FitsOnALine = true;
176 const AnnotatedToken *Tok = &RootToken;
177 while (!Tok->Children.empty()) {
178 Tok = &Tok->Children[0];
179 Columns += (Tok->SpaceRequiredBefore ? 1 : 0) + Tok->FormatTok.TokenLength;
180 // A special case for the colon of a constructor initializer as this only
181 // needs to be put on a new line if the line needs to be split.
182 if (Columns > Limit ||
183 (Tok->MustBreakBefore && Tok->Type != TT_CtorInitializerColon)) {
184 FitsOnALine = false;
185 break;
186 }
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000187 }
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000188 return FitsOnALine;
189}
190
Daniel Jasperbac016b2012-12-03 18:12:45 +0000191class UnwrappedLineFormatter {
192public:
Manuel Klimek94fc6f12013-01-10 19:17:33 +0000193 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
194 const UnwrappedLine &Line, unsigned FirstIndent,
Rafael Espindolab14d1ca2013-01-12 14:22:42 +0000195 bool FitsOnALine, const AnnotatedToken &RootToken,
Manuel Klimek94fc6f12013-01-10 19:17:33 +0000196 tooling::Replacements &Replaces, bool StructuralError)
Daniel Jasper1321eb52012-12-18 21:05:13 +0000197 : Style(Style), SourceMgr(SourceMgr), Line(Line),
Manuel Klimek94fc6f12013-01-10 19:17:33 +0000198 FirstIndent(FirstIndent), FitsOnALine(FitsOnALine),
Rafael Espindolab14d1ca2013-01-12 14:22:42 +0000199 RootToken(RootToken), Replaces(Replaces) {
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000200 Parameters.PenaltyIndentLevel = 15;
Daniel Jasper46a46a22013-01-07 07:13:20 +0000201 Parameters.PenaltyLevelDecrease = 30;
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000202 Parameters.PenaltyExcessCharacter = 1000000;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000203 }
204
Manuel Klimekd4397b92013-01-04 23:34:14 +0000205 /// \brief Formats an \c UnwrappedLine.
206 ///
207 /// \returns The column after the last token in the last line of the
208 /// \c UnwrappedLine.
209 unsigned format() {
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000210 // Initialize state dependent on indent.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000211 LineState State;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000212 State.Column = FirstIndent;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000213 State.NextToken = &RootToken;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000214 State.Stack.push_back(ParenState(FirstIndent + 4, FirstIndent));
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000215 State.ForLoopVariablePos = 0;
216 State.LineContainsContinuedForLoopSection = false;
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000217 State.StartOfLineLevel = 1;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000218
219 // The first token has already been indented and thus consumed.
220 moveStateToNextToken(State);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000221
222 // Start iterating at 1 as we have correctly formatted of Token #0 above.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000223 while (State.NextToken != NULL) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000224 if (FitsOnALine) {
225 addTokenToState(false, false, State);
226 } else {
227 unsigned NoBreak = calcPenalty(State, false, UINT_MAX);
228 unsigned Break = calcPenalty(State, true, NoBreak);
229 addTokenToState(Break < NoBreak, false, State);
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000230 if (State.NextToken != NULL &&
231 State.NextToken->Parent->Type == TT_CtorInitializerColon) {
232 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine &&
233 !fitsIntoLimit(*State.NextToken,
234 getColumnLimit() - State.Column - 1))
235 State.Stack.back().BreakAfterComma = true;
236 }
Daniel Jasper1321eb52012-12-18 21:05:13 +0000237 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000238 }
Manuel Klimekd4397b92013-01-04 23:34:14 +0000239 return State.Column;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000240 }
241
242private:
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000243 struct ParenState {
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000244 ParenState(unsigned Indent, unsigned LastSpace)
245 : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
246 BreakBeforeClosingBrace(false), BreakAfterComma(false) {}
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000247
Daniel Jasperbac016b2012-12-03 18:12:45 +0000248 /// \brief The position to which a specific parenthesis level needs to be
249 /// indented.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000250 unsigned Indent;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000251
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000252 /// \brief The position of the last space on each level.
253 ///
254 /// Used e.g. to break like:
255 /// functionCall(Parameter, otherCall(
256 /// OtherParameter));
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000257 unsigned LastSpace;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000258
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000259 /// \brief The position the first "<<" operator encountered on each level.
260 ///
261 /// Used to align "<<" operators. 0 if no such operator has been encountered
262 /// on a level.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000263 unsigned FirstLessLess;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000264
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000265 /// \brief Whether a newline needs to be inserted before the block's closing
266 /// brace.
267 ///
268 /// We only want to insert a newline before the closing brace if there also
269 /// was a newline after the beginning left brace.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000270 bool BreakBeforeClosingBrace;
271
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000272 bool BreakAfterComma;
273
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000274 bool operator<(const ParenState &Other) const {
275 if (Indent != Other.Indent)
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000276 return Indent < Other.Indent;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000277 if (LastSpace != Other.LastSpace)
278 return LastSpace < Other.LastSpace;
279 if (FirstLessLess != Other.FirstLessLess)
280 return FirstLessLess < Other.FirstLessLess;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000281 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
282 return BreakBeforeClosingBrace;
Daniel Jasperb3123142013-01-12 07:36:22 +0000283 if (BreakAfterComma != Other.BreakAfterComma)
284 return BreakAfterComma;
285 return false;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000286 }
287 };
288
289 /// \brief The current state when indenting a unwrapped line.
290 ///
291 /// As the indenting tries different combinations this is copied by value.
292 struct LineState {
293 /// \brief The number of used columns in the current line.
294 unsigned Column;
295
296 /// \brief The token that needs to be next formatted.
297 const AnnotatedToken *NextToken;
298
299 /// \brief The parenthesis level of the first token on the current line.
300 unsigned StartOfLineLevel;
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000301
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000302 /// \brief The column of the first variable in a for-loop declaration.
303 ///
304 /// Used to align the second variable if necessary.
305 unsigned ForLoopVariablePos;
306
307 /// \brief \c true if this line contains a continued for-loop section.
308 bool LineContainsContinuedForLoopSection;
309
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000310 /// \brief A stack keeping track of properties applying to parenthesis
311 /// levels.
312 std::vector<ParenState> Stack;
313
314 /// \brief Comparison operator to be able to used \c LineState in \c map.
315 bool operator<(const LineState &Other) const {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000316 if (Other.NextToken != NextToken)
317 return Other.NextToken > NextToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000318 if (Other.Column != Column)
319 return Other.Column > Column;
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000320 if (Other.StartOfLineLevel != StartOfLineLevel)
321 return Other.StartOfLineLevel > StartOfLineLevel;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000322 if (Other.ForLoopVariablePos != ForLoopVariablePos)
323 return Other.ForLoopVariablePos < ForLoopVariablePos;
324 if (Other.LineContainsContinuedForLoopSection !=
325 LineContainsContinuedForLoopSection)
326 return LineContainsContinuedForLoopSection;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000327 return Other.Stack < Stack;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000328 }
329 };
330
Daniel Jasper20409152012-12-04 14:54:30 +0000331 /// \brief Appends the next token to \p State and updates information
332 /// necessary for indentation.
333 ///
334 /// Puts the token on the current line if \p Newline is \c true and adds a
335 /// line break and necessary indentation otherwise.
336 ///
337 /// If \p DryRun is \c false, also creates and stores the required
338 /// \c Replacement.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000339 void addTokenToState(bool Newline, bool DryRun, LineState &State) {
Daniel Jasper9c837d02013-01-09 07:06:56 +0000340 const AnnotatedToken &Current = *State.NextToken;
341 const AnnotatedToken &Previous = *State.NextToken->Parent;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000342 assert(State.Stack.size());
343 unsigned ParenLevel = State.Stack.size() - 1;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000344
345 if (Newline) {
Manuel Klimek060143e2013-01-02 18:33:23 +0000346 unsigned WhitespaceStartColumn = State.Column;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000347 if (Current.is(tok::r_brace)) {
348 State.Column = Line.Level * 2;
Daniel Jasper9c837d02013-01-09 07:06:56 +0000349 } else if (Current.is(tok::string_literal) &&
350 Previous.is(tok::string_literal)) {
351 State.Column = State.Column - Previous.FormatTok.TokenLength;
352 } else if (Current.is(tok::lessless) &&
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000353 State.Stack[ParenLevel].FirstLessLess != 0) {
354 State.Column = State.Stack[ParenLevel].FirstLessLess;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000355 } else if (ParenLevel != 0 &&
Daniel Jasper9c837d02013-01-09 07:06:56 +0000356 (Previous.is(tok::equal) || Current.is(tok::arrow) ||
357 Current.is(tok::period) || Previous.is(tok::question) ||
358 Previous.Type == TT_ConditionalExpr)) {
359 // Indent and extra 4 spaces after if we know the current expression is
360 // continued. Don't do that on the top level, as we already indent 4
361 // there.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000362 State.Column = State.Stack[ParenLevel].Indent + 4;
Daniel Jasper9c837d02013-01-09 07:06:56 +0000363 } else if (RootToken.is(tok::kw_for) && Previous.is(tok::comma)) {
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000364 State.Column = State.ForLoopVariablePos;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000365 } else if (State.NextToken->Parent->ClosesTemplateDeclaration) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000366 State.Column = State.Stack[ParenLevel].Indent - 4;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000367 } else {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000368 State.Column = State.Stack[ParenLevel].Indent;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000369 }
370
Manuel Klimek2851c162013-01-10 14:36:46 +0000371 // A line starting with a closing brace is assumed to be correct for the
372 // same level as before the opening brace.
373 State.StartOfLineLevel = ParenLevel + (Current.is(tok::r_brace) ? 0 : 1);
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000374
Daniel Jasper26f7e782013-01-08 14:56:18 +0000375 if (RootToken.is(tok::kw_for))
Daniel Jasper9c837d02013-01-09 07:06:56 +0000376 State.LineContainsContinuedForLoopSection = Previous.isNot(tok::semi);
Daniel Jasper20409152012-12-04 14:54:30 +0000377
Manuel Klimek060143e2013-01-02 18:33:23 +0000378 if (!DryRun) {
379 if (!Line.InPPDirective)
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000380 replaceWhitespace(Current.FormatTok, 1, State.Column, Style,
381 SourceMgr, Replaces);
Manuel Klimek060143e2013-01-02 18:33:23 +0000382 else
Daniel Jasper9c837d02013-01-09 07:06:56 +0000383 replacePPWhitespace(Current.FormatTok, 1, State.Column,
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000384 WhitespaceStartColumn, Style, SourceMgr,
385 Replaces);
Manuel Klimek060143e2013-01-02 18:33:23 +0000386 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000387
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000388 State.Stack[ParenLevel].LastSpace = State.Column;
Nico Weberf681fa82013-01-12 07:05:25 +0000389 if (Current.is(tok::colon) && State.NextToken->Type != TT_ConditionalExpr)
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000390 State.Stack[ParenLevel].Indent += 2;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000391 } else {
Daniel Jasper9c837d02013-01-09 07:06:56 +0000392 if (Current.is(tok::equal) && RootToken.is(tok::kw_for))
393 State.ForLoopVariablePos = State.Column -
394 Previous.FormatTok.TokenLength;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000395
Daniel Jasper26f7e782013-01-08 14:56:18 +0000396 unsigned Spaces = State.NextToken->SpaceRequiredBefore ? 1 : 0;
397 if (State.NextToken->Type == TT_LineComment)
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000398 Spaces = Style.SpacesBeforeTrailingComments;
Daniel Jasper20409152012-12-04 14:54:30 +0000399
Daniel Jasperbac016b2012-12-03 18:12:45 +0000400 if (!DryRun)
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000401 replaceWhitespace(Current, 0, Spaces, Style, SourceMgr, Replaces);
Daniel Jasper20409152012-12-04 14:54:30 +0000402
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000403 // FIXME: Do we need to do this for assignments nested in other
404 // expressions?
405 if (RootToken.isNot(tok::kw_for) && ParenLevel == 0 &&
Daniel Jasper9cda8002013-01-07 13:08:40 +0000406 (getPrecedence(Previous) == prec::Assignment ||
Daniel Jasper9c837d02013-01-09 07:06:56 +0000407 Previous.is(tok::kw_return)))
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000408 State.Stack[ParenLevel].Indent = State.Column + Spaces;
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000409 if (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
Daniel Jasper26f7e782013-01-08 14:56:18 +0000410 State.NextToken->Parent->Type == TT_TemplateOpener)
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000411 State.Stack[ParenLevel].Indent = State.Column + Spaces;
Daniel Jasper1321eb52012-12-18 21:05:13 +0000412
Daniel Jasper9cda8002013-01-07 13:08:40 +0000413 // Top-level spaces that are not part of assignments are exempt as that
414 // mostly leads to better results.
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000415 State.Column += Spaces;
Daniel Jasper9cda8002013-01-07 13:08:40 +0000416 if (Spaces > 0 &&
417 (ParenLevel != 0 || getPrecedence(Previous) == prec::Assignment))
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000418 State.Stack[ParenLevel].LastSpace = State.Column;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000419 }
Daniel Jasper20409152012-12-04 14:54:30 +0000420 moveStateToNextToken(State);
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000421 if (Newline && Previous.is(tok::l_brace)) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000422 State.Stack.back().BreakBeforeClosingBrace = true;
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000423 }
Daniel Jasper20409152012-12-04 14:54:30 +0000424 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000425
Daniel Jasper20409152012-12-04 14:54:30 +0000426 /// \brief Mark the next token as consumed in \p State and modify its stacks
427 /// accordingly.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000428 void moveStateToNextToken(LineState &State) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000429 const AnnotatedToken &Current = *State.NextToken;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000430 assert(State.Stack.size());
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000431
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000432 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
433 State.Stack.back().FirstLessLess = State.Column;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000434
Daniel Jaspercf225b62012-12-24 13:43:52 +0000435 // If we encounter an opening (, [, { or <, we add a level to our stacks to
Daniel Jasper20409152012-12-04 14:54:30 +0000436 // prepare for the following tokens.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000437 if (Current.is(tok::l_paren) || Current.is(tok::l_square) ||
438 Current.is(tok::l_brace) ||
439 State.NextToken->Type == TT_TemplateOpener) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000440 unsigned NewIndent;
Manuel Klimek2851c162013-01-10 14:36:46 +0000441 if (Current.is(tok::l_brace)) {
442 // FIXME: This does not work with nested static initializers.
443 // Implement a better handling for static initializers and similar
444 // constructs.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000445 NewIndent = Line.Level * 2 + 2;
Manuel Klimek2851c162013-01-10 14:36:46 +0000446 } else {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000447 NewIndent = 4 + State.Stack.back().LastSpace;
Manuel Klimek2851c162013-01-10 14:36:46 +0000448 }
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000449 State.Stack.push_back(
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000450 ParenState(NewIndent, State.Stack.back().LastSpace));
Daniel Jasper20409152012-12-04 14:54:30 +0000451 }
452
Daniel Jaspercf225b62012-12-24 13:43:52 +0000453 // If we encounter a closing ), ], } or >, we can remove a level from our
Daniel Jasper20409152012-12-04 14:54:30 +0000454 // stacks.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000455 if (Current.is(tok::r_paren) || Current.is(tok::r_square) ||
456 (Current.is(tok::r_brace) && State.NextToken != &RootToken) ||
457 State.NextToken->Type == TT_TemplateCloser) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000458 State.Stack.pop_back();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000459 }
Manuel Klimek2851c162013-01-10 14:36:46 +0000460
Daniel Jasper26f7e782013-01-08 14:56:18 +0000461 if (State.NextToken->Children.empty())
462 State.NextToken = NULL;
463 else
464 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek2851c162013-01-10 14:36:46 +0000465
466 State.Column += Current.FormatTok.TokenLength;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000467 }
468
Nico Weberdecf7bc2013-01-07 15:15:29 +0000469 /// \brief Calculate the penalty for splitting after the token at \p Index.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000470 unsigned splitPenalty(const AnnotatedToken &Tok) {
471 const AnnotatedToken &Left = Tok;
472 const AnnotatedToken &Right = Tok.Children[0];
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000473
474 // In for-loops, prefer breaking at ',' and ';'.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000475 if (RootToken.is(tok::kw_for) &&
476 (Left.isNot(tok::comma) && Left.isNot(tok::semi)))
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000477 return 20;
478
Daniel Jasper26f7e782013-01-08 14:56:18 +0000479 if (Left.is(tok::semi) || Left.is(tok::comma) ||
480 Left.ClosesTemplateDeclaration)
Daniel Jasperbac016b2012-12-03 18:12:45 +0000481 return 0;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000482 if (Left.is(tok::l_paren))
Daniel Jasper723f0302013-01-02 14:40:02 +0000483 return 20;
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000484
Daniel Jasper9c837d02013-01-09 07:06:56 +0000485 if (Left.is(tok::question) || Left.Type == TT_ConditionalExpr)
486 return prec::Assignment;
Daniel Jasper9cda8002013-01-07 13:08:40 +0000487 prec::Level Level = getPrecedence(Left);
488
489 // Breaking after an assignment leads to a bad result as the two sides of
490 // the assignment are visually very close together.
491 if (Level == prec::Assignment)
492 return 50;
493
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000494 if (Level != prec::Unknown)
495 return Level;
496
Daniel Jasper26f7e782013-01-08 14:56:18 +0000497 if (Right.is(tok::arrow) || Right.is(tok::period))
Daniel Jasper46a46a22013-01-07 07:13:20 +0000498 return 150;
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000499
Daniel Jasperbac016b2012-12-03 18:12:45 +0000500 return 3;
501 }
502
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000503 unsigned getColumnLimit() {
504 return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0);
505 }
506
Daniel Jasperbac016b2012-12-03 18:12:45 +0000507 /// \brief Calculate the number of lines needed to format the remaining part
508 /// of the unwrapped line.
509 ///
510 /// Assumes the formatting so far has led to
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000511 /// the \c LineSta \p State. If \p NewLine is set, a new line will be
Daniel Jasperbac016b2012-12-03 18:12:45 +0000512 /// added after the previous token.
513 ///
514 /// \param StopAt is used for optimization. If we can determine that we'll
515 /// definitely need at least \p StopAt additional lines, we already know of a
516 /// better solution.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000517 unsigned calcPenalty(LineState State, bool NewLine, unsigned StopAt) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000518 // We are at the end of the unwrapped line, so we don't need any more lines.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000519 if (State.NextToken == NULL)
Daniel Jasperbac016b2012-12-03 18:12:45 +0000520 return 0;
521
Daniel Jasper26f7e782013-01-08 14:56:18 +0000522 if (!NewLine && State.NextToken->MustBreakBefore)
Daniel Jasperbac016b2012-12-03 18:12:45 +0000523 return UINT_MAX;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000524 if (NewLine && !State.NextToken->CanBreakBefore)
Daniel Jasperbac016b2012-12-03 18:12:45 +0000525 return UINT_MAX;
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000526 if (!NewLine && State.NextToken->is(tok::r_brace) &&
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000527 State.Stack.back().BreakBeforeClosingBrace)
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000528 return UINT_MAX;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000529 if (!NewLine && State.NextToken->Parent->is(tok::semi) &&
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000530 State.LineContainsContinuedForLoopSection)
531 return UINT_MAX;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000532 if (!NewLine && State.NextToken->Parent->is(tok::comma) &&
533 State.NextToken->Type != TT_LineComment &&
534 State.Stack.back().BreakAfterComma)
535 return UINT_MAX;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000536
Daniel Jasper33182dd2012-12-05 14:57:28 +0000537 unsigned CurrentPenalty = 0;
538 if (NewLine) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000539 CurrentPenalty += Parameters.PenaltyIndentLevel * State.Stack.size() +
Daniel Jasper26f7e782013-01-08 14:56:18 +0000540 splitPenalty(*State.NextToken->Parent);
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000541 } else {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000542 if (State.Stack.size() < State.StartOfLineLevel)
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000543 CurrentPenalty += Parameters.PenaltyLevelDecrease *
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000544 (State.StartOfLineLevel - State.Stack.size());
Daniel Jasper33182dd2012-12-05 14:57:28 +0000545 }
546
Daniel Jasper20409152012-12-04 14:54:30 +0000547 addTokenToState(NewLine, true, State);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000548
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000549 // Exceeding column limit is bad, assign penalty.
550 if (State.Column > getColumnLimit()) {
551 unsigned ExcessCharacters = State.Column - getColumnLimit();
552 CurrentPenalty += Parameters.PenaltyExcessCharacter * ExcessCharacters;
553 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000554
Daniel Jasperbac016b2012-12-03 18:12:45 +0000555 if (StopAt <= CurrentPenalty)
556 return UINT_MAX;
557 StopAt -= CurrentPenalty;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000558 StateMap::iterator I = Memory.find(State);
Daniel Jasper33182dd2012-12-05 14:57:28 +0000559 if (I != Memory.end()) {
560 // If this state has already been examined, we can safely return the
561 // previous result if we
562 // - have not hit the optimatization (and thus returned UINT_MAX) OR
563 // - are now computing for a smaller or equal StopAt.
564 unsigned SavedResult = I->second.first;
565 unsigned SavedStopAt = I->second.second;
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000566 if (SavedResult != UINT_MAX)
567 return SavedResult + CurrentPenalty;
568 else if (StopAt <= SavedStopAt)
569 return UINT_MAX;
Daniel Jasper33182dd2012-12-05 14:57:28 +0000570 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000571
572 unsigned NoBreak = calcPenalty(State, false, StopAt);
573 unsigned WithBreak = calcPenalty(State, true, std::min(StopAt, NoBreak));
574 unsigned Result = std::min(NoBreak, WithBreak);
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000575
576 // We have to store 'Result' without adding 'CurrentPenalty' as the latter
577 // can depend on 'NewLine'.
Daniel Jasper33182dd2012-12-05 14:57:28 +0000578 Memory[State] = std::pair<unsigned, unsigned>(Result, StopAt);
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000579
580 return Result == UINT_MAX ? UINT_MAX : Result + CurrentPenalty;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000581 }
582
Daniel Jasperbac016b2012-12-03 18:12:45 +0000583 FormatStyle Style;
584 SourceManager &SourceMgr;
585 const UnwrappedLine &Line;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000586 const unsigned FirstIndent;
Manuel Klimek94fc6f12013-01-10 19:17:33 +0000587 const bool FitsOnALine;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000588 const AnnotatedToken &RootToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000589 tooling::Replacements &Replaces;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000590
Daniel Jasper33182dd2012-12-05 14:57:28 +0000591 // A map from an indent state to a pair (Result, Used-StopAt).
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000592 typedef std::map<LineState, std::pair<unsigned, unsigned> > StateMap;
Daniel Jasper33182dd2012-12-05 14:57:28 +0000593 StateMap Memory;
594
Daniel Jasperbac016b2012-12-03 18:12:45 +0000595 OptimizationParameters Parameters;
596};
597
598/// \brief Determines extra information about the tokens comprising an
599/// \c UnwrappedLine.
600class TokenAnnotator {
601public:
602 TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style,
Manuel Klimek6cf58142013-01-07 08:54:53 +0000603 SourceManager &SourceMgr, Lexer &Lex)
Daniel Jasper26f7e782013-01-08 14:56:18 +0000604 : Style(Style), SourceMgr(SourceMgr), Lex(Lex),
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000605 RootToken(Line.RootToken) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000606
607 /// \brief A parser that gathers additional information about tokens.
608 ///
609 /// The \c TokenAnnotator tries to matches parenthesis and square brakets and
610 /// store a parenthesis levels. It also tries to resolve matching "<" and ">"
611 /// into template parameter lists.
612 class AnnotatingParser {
613 public:
Daniel Jasper26f7e782013-01-08 14:56:18 +0000614 AnnotatingParser(AnnotatedToken &RootToken)
Nico Weberbcfdd262013-01-12 06:18:40 +0000615 : CurrentToken(&RootToken), KeywordVirtualFound(false),
616 ColonIsObjCMethodExpr(false) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000617
Daniel Jasper20409152012-12-04 14:54:30 +0000618 bool parseAngle() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000619 while (CurrentToken != NULL) {
620 if (CurrentToken->is(tok::greater)) {
621 CurrentToken->Type = TT_TemplateCloser;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000622 next();
623 return true;
624 }
Daniel Jasper26f7e782013-01-08 14:56:18 +0000625 if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square) ||
626 CurrentToken->is(tok::r_brace))
Daniel Jasperbac016b2012-12-03 18:12:45 +0000627 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000628 if (CurrentToken->is(tok::pipepipe) || CurrentToken->is(tok::ampamp) ||
629 CurrentToken->is(tok::question) || CurrentToken->is(tok::colon))
Daniel Jasperbac016b2012-12-03 18:12:45 +0000630 return false;
Daniel Jasper1f42f112013-01-04 18:52:56 +0000631 if (!consumeToken())
632 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000633 }
634 return false;
635 }
636
Daniel Jasper20409152012-12-04 14:54:30 +0000637 bool parseParens() {
Daniel Jasper46ef8522013-01-10 13:08:12 +0000638 if (CurrentToken != NULL && CurrentToken->is(tok::caret))
639 CurrentToken->Parent->Type = TT_ObjCBlockLParen;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000640 while (CurrentToken != NULL) {
641 if (CurrentToken->is(tok::r_paren)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000642 next();
643 return true;
644 }
Daniel Jasper26f7e782013-01-08 14:56:18 +0000645 if (CurrentToken->is(tok::r_square) || CurrentToken->is(tok::r_brace))
Daniel Jasperbac016b2012-12-03 18:12:45 +0000646 return false;
Daniel Jasper1f42f112013-01-04 18:52:56 +0000647 if (!consumeToken())
648 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000649 }
650 return false;
651 }
652
Daniel Jasper20409152012-12-04 14:54:30 +0000653 bool parseSquare() {
Nico Weberbcfdd262013-01-12 06:18:40 +0000654 if (!CurrentToken)
655 return false;
656
657 // A '[' could be an index subscript (after an indentifier or after
658 // ')' or ']'), or it could be the start of an Objective-C method
659 // expression.
660 AnnotatedToken *LSquare = CurrentToken->Parent;
661 bool StartsObjCMethodExpr =
662 !LSquare->Parent || LSquare->Parent->is(tok::colon) ||
663 LSquare->Parent->is(tok::l_square) ||
664 LSquare->Parent->is(tok::l_paren) ||
665 LSquare->Parent->is(tok::kw_return) ||
666 LSquare->Parent->is(tok::kw_throw) ||
667 getBinOpPrecedence(LSquare->Parent->FormatTok.Tok.getKind(),
668 true, true) > prec::Unknown;
669
670 bool ColonWasObjCMethodExpr = ColonIsObjCMethodExpr;
671 if (StartsObjCMethodExpr) {
672 ColonIsObjCMethodExpr = true;
673 LSquare->Type = TT_ObjCMethodExpr;
674 }
675
Daniel Jasper26f7e782013-01-08 14:56:18 +0000676 while (CurrentToken != NULL) {
677 if (CurrentToken->is(tok::r_square)) {
Nico Weberbcfdd262013-01-12 06:18:40 +0000678 if (StartsObjCMethodExpr) {
679 ColonIsObjCMethodExpr = ColonWasObjCMethodExpr;
680 CurrentToken->Type = TT_ObjCMethodExpr;
681 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000682 next();
683 return true;
684 }
Daniel Jasper26f7e782013-01-08 14:56:18 +0000685 if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_brace))
Daniel Jasperbac016b2012-12-03 18:12:45 +0000686 return false;
Daniel Jasper1f42f112013-01-04 18:52:56 +0000687 if (!consumeToken())
688 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000689 }
690 return false;
691 }
692
Daniel Jasper700e7102013-01-10 09:26:47 +0000693 bool parseBrace() {
694 while (CurrentToken != NULL) {
695 if (CurrentToken->is(tok::r_brace)) {
696 next();
697 return true;
698 }
699 if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square))
700 return false;
701 if (!consumeToken())
702 return false;
703 }
704 // Lines can currently end with '{'.
705 return true;
706 }
707
Daniel Jasper20409152012-12-04 14:54:30 +0000708 bool parseConditional() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000709 while (CurrentToken != NULL) {
710 if (CurrentToken->is(tok::colon)) {
711 CurrentToken->Type = TT_ConditionalExpr;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000712 next();
713 return true;
714 }
Daniel Jasper1f42f112013-01-04 18:52:56 +0000715 if (!consumeToken())
716 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000717 }
718 return false;
719 }
720
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000721 bool parseTemplateDeclaration() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000722 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
723 CurrentToken->Type = TT_TemplateOpener;
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000724 next();
725 if (!parseAngle())
726 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000727 CurrentToken->Parent->ClosesTemplateDeclaration = true;
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000728 parseLine();
729 return true;
730 }
731 return false;
732 }
733
Daniel Jasper1f42f112013-01-04 18:52:56 +0000734 bool consumeToken() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000735 AnnotatedToken *Tok = CurrentToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000736 next();
Daniel Jasper26f7e782013-01-08 14:56:18 +0000737 switch (Tok->FormatTok.Tok.getKind()) {
Nico Webercd52bda2013-01-10 23:11:41 +0000738 case tok::plus:
739 case tok::minus:
740 // At the start of the line, +/- specific ObjectiveC method
741 // declarations.
742 if (Tok->Parent == NULL)
743 Tok->Type = TT_ObjCMethodSpecifier;
744 break;
Nico Weberbcfdd262013-01-12 06:18:40 +0000745 case tok::colon:
746 // Colons from ?: are handled in parseConditional().
747 if (ColonIsObjCMethodExpr)
748 Tok->Type = TT_ObjCMethodExpr;
749 break;
Nico Webercd52bda2013-01-10 23:11:41 +0000750 case tok::l_paren: {
751 bool ParensWereObjCReturnType =
752 Tok->Parent && Tok->Parent->Type == TT_ObjCMethodSpecifier;
Daniel Jasper1f42f112013-01-04 18:52:56 +0000753 if (!parseParens())
754 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000755 if (CurrentToken != NULL && CurrentToken->is(tok::colon)) {
756 CurrentToken->Type = TT_CtorInitializerColon;
Daniel Jasper1321eb52012-12-18 21:05:13 +0000757 next();
Nico Webercd52bda2013-01-10 23:11:41 +0000758 } else if (CurrentToken != NULL && ParensWereObjCReturnType) {
759 CurrentToken->Type = TT_ObjCSelectorStart;
760 next();
Daniel Jasper1321eb52012-12-18 21:05:13 +0000761 }
Nico Webercd52bda2013-01-10 23:11:41 +0000762 } break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000763 case tok::l_square:
Daniel Jasper1f42f112013-01-04 18:52:56 +0000764 if (!parseSquare())
765 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000766 break;
Daniel Jasper700e7102013-01-10 09:26:47 +0000767 case tok::l_brace:
768 if (!parseBrace())
769 return false;
770 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000771 case tok::less:
Daniel Jasper20409152012-12-04 14:54:30 +0000772 if (parseAngle())
Daniel Jasper26f7e782013-01-08 14:56:18 +0000773 Tok->Type = TT_TemplateOpener;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000774 else {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000775 Tok->Type = TT_BinaryOperator;
776 CurrentToken = Tok;
777 next();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000778 }
779 break;
Daniel Jasper1f42f112013-01-04 18:52:56 +0000780 case tok::r_paren:
781 case tok::r_square:
782 return false;
Daniel Jasper700e7102013-01-10 09:26:47 +0000783 case tok::r_brace:
784 // Lines can start with '}'.
785 if (Tok->Parent != NULL)
786 return false;
787 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000788 case tok::greater:
Daniel Jasper26f7e782013-01-08 14:56:18 +0000789 Tok->Type = TT_BinaryOperator;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000790 break;
791 case tok::kw_operator:
Daniel Jasper26f7e782013-01-08 14:56:18 +0000792 if (CurrentToken->is(tok::l_paren)) {
793 CurrentToken->Type = TT_OverloadedOperator;
Daniel Jasperf6aef6a2012-12-24 10:56:04 +0000794 next();
Daniel Jasper26f7e782013-01-08 14:56:18 +0000795 if (CurrentToken != NULL && CurrentToken->is(tok::r_paren)) {
796 CurrentToken->Type = TT_OverloadedOperator;
Daniel Jasperf6aef6a2012-12-24 10:56:04 +0000797 next();
798 }
799 } else {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000800 while (CurrentToken != NULL && CurrentToken->isNot(tok::l_paren)) {
801 CurrentToken->Type = TT_OverloadedOperator;
Daniel Jasperf6aef6a2012-12-24 10:56:04 +0000802 next();
803 }
804 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000805 break;
806 case tok::question:
Daniel Jasper20409152012-12-04 14:54:30 +0000807 parseConditional();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000808 break;
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000809 case tok::kw_template:
810 parseTemplateDeclaration();
811 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000812 default:
813 break;
814 }
Daniel Jasper1f42f112013-01-04 18:52:56 +0000815 return true;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000816 }
817
Daniel Jaspercd1a32b2012-12-21 17:58:39 +0000818 void parseIncludeDirective() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000819 while (CurrentToken != NULL) {
820 if (CurrentToken->is(tok::slash))
821 CurrentToken->Type = TT_DirectorySeparator;
822 else if (CurrentToken->is(tok::less))
823 CurrentToken->Type = TT_TemplateOpener;
824 else if (CurrentToken->is(tok::greater))
825 CurrentToken->Type = TT_TemplateCloser;
Daniel Jaspercd1a32b2012-12-21 17:58:39 +0000826 next();
827 }
828 }
829
830 void parsePreprocessorDirective() {
831 next();
Daniel Jasper26f7e782013-01-08 14:56:18 +0000832 if (CurrentToken == NULL)
Daniel Jaspercd1a32b2012-12-21 17:58:39 +0000833 return;
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000834 // Hashes in the middle of a line can lead to any strange token
835 // sequence.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000836 if (CurrentToken->FormatTok.Tok.getIdentifierInfo() == NULL)
Manuel Klimekf6fd00b2013-01-05 22:56:06 +0000837 return;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000838 switch (
839 CurrentToken->FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jaspercd1a32b2012-12-21 17:58:39 +0000840 case tok::pp_include:
Nico Weberb23ae0c2012-12-21 18:21:56 +0000841 case tok::pp_import:
Daniel Jaspercd1a32b2012-12-21 17:58:39 +0000842 parseIncludeDirective();
843 break;
844 default:
845 break;
846 }
847 }
848
Daniel Jasper71607512013-01-07 10:48:50 +0000849 LineType parseLine() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000850 if (CurrentToken->is(tok::hash)) {
Daniel Jaspercd1a32b2012-12-21 17:58:39 +0000851 parsePreprocessorDirective();
Daniel Jasper71607512013-01-07 10:48:50 +0000852 return LT_PreprocessorDirective;
Daniel Jaspercd1a32b2012-12-21 17:58:39 +0000853 }
Daniel Jasper26f7e782013-01-08 14:56:18 +0000854 while (CurrentToken != NULL) {
855 if (CurrentToken->is(tok::kw_virtual))
Daniel Jasper71607512013-01-07 10:48:50 +0000856 KeywordVirtualFound = true;
Daniel Jasper1f42f112013-01-04 18:52:56 +0000857 if (!consumeToken())
Daniel Jasper71607512013-01-07 10:48:50 +0000858 return LT_Invalid;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000859 }
Daniel Jasper71607512013-01-07 10:48:50 +0000860 if (KeywordVirtualFound)
861 return LT_VirtualFunctionDecl;
862 return LT_Other;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000863 }
864
865 void next() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000866 if (CurrentToken != NULL && !CurrentToken->Children.empty())
867 CurrentToken = &CurrentToken->Children[0];
868 else
869 CurrentToken = NULL;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000870 }
871
872 private:
Daniel Jasper26f7e782013-01-08 14:56:18 +0000873 AnnotatedToken *CurrentToken;
Daniel Jasper71607512013-01-07 10:48:50 +0000874 bool KeywordVirtualFound;
Nico Weberbcfdd262013-01-12 06:18:40 +0000875 bool ColonIsObjCMethodExpr;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000876 };
877
Daniel Jasper26f7e782013-01-08 14:56:18 +0000878 void createAnnotatedTokens(AnnotatedToken &Current) {
879 if (!Current.FormatTok.Children.empty()) {
880 Current.Children.push_back(AnnotatedToken(Current.FormatTok.Children[0]));
881 Current.Children.back().Parent = &Current;
882 createAnnotatedTokens(Current.Children.back());
883 }
884 }
Daniel Jasper71607512013-01-07 10:48:50 +0000885
Daniel Jasper26f7e782013-01-08 14:56:18 +0000886 void calculateExtraInformation(AnnotatedToken &Current) {
887 Current.SpaceRequiredBefore = spaceRequiredBefore(Current);
888
Manuel Klimek526ed112013-01-09 15:25:02 +0000889 if (Current.FormatTok.MustBreakBefore) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000890 Current.MustBreakBefore = true;
891 } else {
Manuel Klimek526ed112013-01-09 15:25:02 +0000892 if (Current.Type == TT_CtorInitializerColon || Current.Parent->Type ==
893 TT_LineComment || (Current.is(tok::string_literal) &&
894 Current.Parent->is(tok::string_literal))) {
895 Current.MustBreakBefore = true;
Manuel Klimek526ed112013-01-09 15:25:02 +0000896 } else {
897 Current.MustBreakBefore = false;
898 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000899 }
Daniel Jasper26f7e782013-01-08 14:56:18 +0000900 Current.CanBreakBefore = Current.MustBreakBefore || canBreakBefore(Current);
Daniel Jasper26f7e782013-01-08 14:56:18 +0000901 if (!Current.Children.empty())
902 calculateExtraInformation(Current.Children[0]);
903 }
904
905 bool annotate() {
906 createAnnotatedTokens(RootToken);
907
908 AnnotatingParser Parser(RootToken);
Daniel Jasper71607512013-01-07 10:48:50 +0000909 CurrentLineType = Parser.parseLine();
910 if (CurrentLineType == LT_Invalid)
Daniel Jasper1f42f112013-01-04 18:52:56 +0000911 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000912
Daniel Jasper26f7e782013-01-08 14:56:18 +0000913 determineTokenTypes(RootToken, /*IsRHS=*/false);
Daniel Jasper71607512013-01-07 10:48:50 +0000914
Daniel Jasper26f7e782013-01-08 14:56:18 +0000915 if (RootToken.Type == TT_ObjCMethodSpecifier)
Daniel Jasper71607512013-01-07 10:48:50 +0000916 CurrentLineType = LT_ObjCMethodDecl;
Nico Webered91bba2013-01-10 19:19:14 +0000917 else if (RootToken.Type == TT_ObjCDecl)
918 CurrentLineType = LT_ObjCDecl;
Nico Weber70848232013-01-10 21:30:42 +0000919 else if (RootToken.Type == TT_ObjCProperty)
920 CurrentLineType = LT_ObjCProperty;
Daniel Jasper71607512013-01-07 10:48:50 +0000921
Daniel Jasper26f7e782013-01-08 14:56:18 +0000922 if (!RootToken.Children.empty())
923 calculateExtraInformation(RootToken.Children[0]);
Daniel Jasper1f42f112013-01-04 18:52:56 +0000924 return true;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000925 }
926
Daniel Jasper71607512013-01-07 10:48:50 +0000927 LineType getLineType() {
928 return CurrentLineType;
929 }
930
Daniel Jasper26f7e782013-01-08 14:56:18 +0000931 const AnnotatedToken &getRootToken() {
932 return RootToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000933 }
934
935private:
Daniel Jasper26f7e782013-01-08 14:56:18 +0000936 void determineTokenTypes(AnnotatedToken &Current, bool IsRHS) {
937 if (getPrecedence(Current) == prec::Assignment ||
938 Current.is(tok::kw_return) || Current.is(tok::kw_throw))
939 IsRHS = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000940
Daniel Jasper26f7e782013-01-08 14:56:18 +0000941 if (Current.Type == TT_Unknown) {
942 if (Current.is(tok::star) || Current.is(tok::amp)) {
943 Current.Type = determineStarAmpUsage(Current, IsRHS);
Daniel Jasper886568d2013-01-09 08:36:49 +0000944 } else if (Current.is(tok::minus) || Current.is(tok::plus) ||
945 Current.is(tok::caret)) {
946 Current.Type = determinePlusMinusCaretUsage(Current);
Daniel Jasper26f7e782013-01-08 14:56:18 +0000947 } else if (Current.is(tok::minusminus) || Current.is(tok::plusplus)) {
948 Current.Type = determineIncrementUsage(Current);
949 } else if (Current.is(tok::exclaim)) {
950 Current.Type = TT_UnaryOperator;
951 } else if (isBinaryOperator(Current)) {
952 Current.Type = TT_BinaryOperator;
953 } else if (Current.is(tok::comment)) {
954 std::string Data(Lexer::getSpelling(Current.FormatTok.Tok, SourceMgr,
955 Lex.getLangOpts()));
Manuel Klimek6cf58142013-01-07 08:54:53 +0000956 if (StringRef(Data).startswith("//"))
Daniel Jasper26f7e782013-01-08 14:56:18 +0000957 Current.Type = TT_LineComment;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000958 else
Daniel Jasper26f7e782013-01-08 14:56:18 +0000959 Current.Type = TT_BlockComment;
Daniel Jasper5cf7cf32013-01-10 11:14:08 +0000960 } else if (Current.is(tok::r_paren) &&
961 (Current.Parent->Type == TT_PointerOrReference ||
962 Current.Parent->Type == TT_TemplateCloser)) {
963 // FIXME: We need to get smarter and understand more cases of casts.
964 Current.Type = TT_CastRParen;
Nico Webered91bba2013-01-10 19:19:14 +0000965 } else if (Current.is(tok::at) && Current.Children.size()) {
966 switch (Current.Children[0].FormatTok.Tok.getObjCKeywordID()) {
967 case tok::objc_interface:
968 case tok::objc_implementation:
969 case tok::objc_protocol:
970 Current.Type = TT_ObjCDecl;
Nico Weber70848232013-01-10 21:30:42 +0000971 break;
972 case tok::objc_property:
973 Current.Type = TT_ObjCProperty;
974 break;
Nico Webered91bba2013-01-10 19:19:14 +0000975 default:
976 break;
977 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000978 }
979 }
Daniel Jasper26f7e782013-01-08 14:56:18 +0000980
981 if (!Current.Children.empty())
982 determineTokenTypes(Current.Children[0], IsRHS);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000983 }
984
Daniel Jasper26f7e782013-01-08 14:56:18 +0000985 bool isBinaryOperator(const AnnotatedToken &Tok) {
Daniel Jaspercd1a32b2012-12-21 17:58:39 +0000986 // Comma is a binary operator, but does not behave as such wrt. formatting.
Daniel Jaspercf225b62012-12-24 13:43:52 +0000987 return getPrecedence(Tok) > prec::Comma;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000988 }
989
Daniel Jasper26f7e782013-01-08 14:56:18 +0000990 TokenType determineStarAmpUsage(const AnnotatedToken &Tok, bool IsRHS) {
991 if (Tok.Parent == NULL)
Daniel Jasper71607512013-01-07 10:48:50 +0000992 return TT_UnaryOperator;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000993 if (Tok.Children.size() == 0)
Daniel Jasper71607512013-01-07 10:48:50 +0000994 return TT_Unknown;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000995 const FormatToken &PrevToken = Tok.Parent->FormatTok;
996 const FormatToken &NextToken = Tok.Children[0].FormatTok;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000997
Daniel Jasper9bb0d282013-01-04 20:46:38 +0000998 if (PrevToken.Tok.is(tok::l_paren) || PrevToken.Tok.is(tok::l_square) ||
999 PrevToken.Tok.is(tok::comma) || PrevToken.Tok.is(tok::kw_return) ||
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001000 PrevToken.Tok.is(tok::colon) || Tok.Parent->Type == TT_BinaryOperator ||
1001 Tok.Parent->Type == TT_CastRParen)
Daniel Jasper71607512013-01-07 10:48:50 +00001002 return TT_UnaryOperator;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001003
Nico Weber2355ceb2013-01-12 05:47:16 +00001004 if (PrevToken.Tok.isLiteral() || PrevToken.Tok.is(tok::r_paren) ||
1005 PrevToken.Tok.is(tok::r_square) || NextToken.Tok.isLiteral() ||
Daniel Jasperba3d3072013-01-02 17:21:36 +00001006 NextToken.Tok.is(tok::plus) || NextToken.Tok.is(tok::minus) ||
1007 NextToken.Tok.is(tok::plusplus) || NextToken.Tok.is(tok::minusminus) ||
1008 NextToken.Tok.is(tok::tilde) || NextToken.Tok.is(tok::exclaim) ||
Nico Weber5e9f91c2013-01-12 05:50:48 +00001009 NextToken.Tok.is(tok::l_paren) || NextToken.Tok.is(tok::l_square) ||
Daniel Jasperba3d3072013-01-02 17:21:36 +00001010 NextToken.Tok.is(tok::kw_alignof) || NextToken.Tok.is(tok::kw_sizeof))
Daniel Jasper71607512013-01-07 10:48:50 +00001011 return TT_BinaryOperator;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001012
Daniel Jasperef5b9c32013-01-02 15:46:59 +00001013 if (NextToken.Tok.is(tok::comma) || NextToken.Tok.is(tok::r_paren) ||
1014 NextToken.Tok.is(tok::greater))
Daniel Jasper71607512013-01-07 10:48:50 +00001015 return TT_PointerOrReference;
Daniel Jasperef5b9c32013-01-02 15:46:59 +00001016
Daniel Jasper112fb272012-12-05 07:51:39 +00001017 // It is very unlikely that we are going to find a pointer or reference type
1018 // definition on the RHS of an assignment.
Nico Weber00d5a042012-12-23 01:07:46 +00001019 if (IsRHS)
Daniel Jasper71607512013-01-07 10:48:50 +00001020 return TT_BinaryOperator;
Daniel Jasper112fb272012-12-05 07:51:39 +00001021
Daniel Jasper71607512013-01-07 10:48:50 +00001022 return TT_PointerOrReference;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001023 }
1024
Daniel Jasper886568d2013-01-09 08:36:49 +00001025 TokenType determinePlusMinusCaretUsage(const AnnotatedToken &Tok) {
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001026 // Use heuristics to recognize unary operators.
Daniel Jasper26f7e782013-01-08 14:56:18 +00001027 if (Tok.Parent->is(tok::equal) || Tok.Parent->is(tok::l_paren) ||
1028 Tok.Parent->is(tok::comma) || Tok.Parent->is(tok::l_square) ||
1029 Tok.Parent->is(tok::question) || Tok.Parent->is(tok::colon) ||
Nico Weber81ed2f12013-01-10 19:36:35 +00001030 Tok.Parent->is(tok::kw_return) || Tok.Parent->is(tok::kw_case) ||
Nico Webercc191d12013-01-12 05:41:23 +00001031 Tok.Parent->is(tok::at) || Tok.Parent->is(tok::l_brace))
Daniel Jasper71607512013-01-07 10:48:50 +00001032 return TT_UnaryOperator;
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001033
1034 // There can't be to consecutive binary operators.
Daniel Jasper26f7e782013-01-08 14:56:18 +00001035 if (Tok.Parent->Type == TT_BinaryOperator)
Daniel Jasper71607512013-01-07 10:48:50 +00001036 return TT_UnaryOperator;
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001037
1038 // Fall back to marking the token as binary operator.
Daniel Jasper71607512013-01-07 10:48:50 +00001039 return TT_BinaryOperator;
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001040 }
1041
1042 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Daniel Jasper26f7e782013-01-08 14:56:18 +00001043 TokenType determineIncrementUsage(const AnnotatedToken &Tok) {
1044 if (Tok.Parent != NULL && Tok.Parent->is(tok::identifier))
Daniel Jasper71607512013-01-07 10:48:50 +00001045 return TT_TrailingUnaryOperator;
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001046
Daniel Jasper71607512013-01-07 10:48:50 +00001047 return TT_UnaryOperator;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001048 }
1049
Daniel Jasper26f7e782013-01-08 14:56:18 +00001050 bool spaceRequiredBetween(const AnnotatedToken &Left,
1051 const AnnotatedToken &Right) {
Daniel Jasper765561f2013-01-08 16:17:54 +00001052 if (Right.is(tok::hashhash))
1053 return Left.is(tok::hash);
1054 if (Left.is(tok::hashhash) || Left.is(tok::hash))
1055 return Right.is(tok::hash);
Daniel Jasper8b39c662012-12-10 18:59:13 +00001056 if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma))
1057 return false;
Nico Weber5f500df2013-01-10 20:12:55 +00001058 if (Right.is(tok::less) &&
1059 (Left.is(tok::kw_template) ||
1060 (CurrentLineType == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
Daniel Jasperbac016b2012-12-03 18:12:45 +00001061 return true;
1062 if (Left.is(tok::arrow) || Right.is(tok::arrow))
1063 return false;
1064 if (Left.is(tok::exclaim) || Left.is(tok::tilde))
1065 return false;
Nico Webercb4d6902013-01-08 19:40:21 +00001066 if (Left.is(tok::at) &&
1067 (Right.is(tok::identifier) || Right.is(tok::string_literal) ||
1068 Right.is(tok::char_constant) || Right.is(tok::numeric_constant) ||
Daniel Jasper46ef8522013-01-10 13:08:12 +00001069 Right.is(tok::l_paren) || Right.is(tok::l_brace) ||
1070 Right.is(tok::kw_true) || Right.is(tok::kw_false)))
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001071 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001072 if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less))
1073 return false;
Daniel Jasperc74e2792012-12-07 09:52:15 +00001074 if (Right.is(tok::amp) || Right.is(tok::star))
Daniel Jasper26f7e782013-01-08 14:56:18 +00001075 return Left.FormatTok.Tok.isLiteral() ||
Daniel Jasperd7610b82012-12-24 16:51:15 +00001076 (Left.isNot(tok::star) && Left.isNot(tok::amp) &&
1077 !Style.PointerAndReferenceBindToType);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001078 if (Left.is(tok::amp) || Left.is(tok::star))
Daniel Jasper26f7e782013-01-08 14:56:18 +00001079 return Right.FormatTok.Tok.isLiteral() ||
1080 Style.PointerAndReferenceBindToType;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001081 if (Right.is(tok::star) && Left.is(tok::l_paren))
1082 return false;
Nico Weberbcfdd262013-01-12 06:18:40 +00001083 if (Left.is(tok::l_square) || Right.is(tok::r_square))
1084 return false;
1085 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr)
Daniel Jasperbac016b2012-12-03 18:12:45 +00001086 return false;
Daniel Jasperc74e2792012-12-07 09:52:15 +00001087 if (Left.is(tok::coloncolon) ||
1088 (Right.is(tok::coloncolon) &&
1089 (Left.is(tok::identifier) || Left.is(tok::greater))))
Daniel Jasperbac016b2012-12-03 18:12:45 +00001090 return false;
1091 if (Left.is(tok::period) || Right.is(tok::period))
1092 return false;
Nico Weberbcfdd262013-01-12 06:18:40 +00001093 if (Left.is(tok::colon))
1094 return Left.Type != TT_ObjCMethodExpr;
1095 if (Right.is(tok::colon))
1096 return Right.Type != TT_ObjCMethodExpr;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001097 if (Left.is(tok::l_paren))
1098 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001099 if (Right.is(tok::l_paren)) {
Nico Webered91bba2013-01-10 19:19:14 +00001100 return CurrentLineType == LT_ObjCDecl || Left.is(tok::kw_if) ||
1101 Left.is(tok::kw_for) || Left.is(tok::kw_while) ||
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001102 Left.is(tok::kw_switch) || Left.is(tok::kw_return) ||
Daniel Jasper088dab52013-01-11 16:09:04 +00001103 Left.is(tok::kw_catch) || Left.is(tok::kw_new) ||
1104 Left.is(tok::kw_delete);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001105 }
Daniel Jasper26f7e782013-01-08 14:56:18 +00001106 if (Left.is(tok::at) &&
1107 Right.FormatTok.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Nico Weberd0af4b42013-01-07 16:14:28 +00001108 return false;
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001109 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
1110 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001111 return true;
1112 }
1113
Daniel Jasper26f7e782013-01-08 14:56:18 +00001114 bool spaceRequiredBefore(const AnnotatedToken &Tok) {
Daniel Jasperda927712013-01-07 15:36:15 +00001115 if (CurrentLineType == LT_ObjCMethodDecl) {
Daniel Jasper26f7e782013-01-08 14:56:18 +00001116 if (Tok.is(tok::identifier) && !Tok.Children.empty() &&
1117 Tok.Children[0].is(tok::colon) && Tok.Parent->is(tok::identifier))
Daniel Jasperda927712013-01-07 15:36:15 +00001118 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001119 if (Tok.is(tok::colon))
Daniel Jasperda927712013-01-07 15:36:15 +00001120 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001121 if (Tok.Parent->Type == TT_ObjCMethodSpecifier)
Nico Webercd52bda2013-01-10 23:11:41 +00001122 return Style.ObjCSpaceBeforeReturnType || Tok.isNot(tok::l_paren);
1123 if (Tok.Type == TT_ObjCSelectorStart)
1124 return !Style.ObjCSpaceBeforeReturnType;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001125 if (Tok.Parent->is(tok::r_paren) && Tok.is(tok::identifier))
Daniel Jasperda927712013-01-07 15:36:15 +00001126 // Don't space between ')' and <id>
1127 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001128 if (Tok.Parent->is(tok::colon) && Tok.is(tok::l_paren))
Daniel Jasperda927712013-01-07 15:36:15 +00001129 // Don't space between ':' and '('
1130 return false;
1131 }
Nico Weber70848232013-01-10 21:30:42 +00001132 if (CurrentLineType == LT_ObjCProperty &&
1133 (Tok.is(tok::equal) || Tok.Parent->is(tok::equal)))
1134 return false;
Daniel Jasperda927712013-01-07 15:36:15 +00001135
Daniel Jasper46ef8522013-01-10 13:08:12 +00001136 if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
Daniel Jasperda927712013-01-07 15:36:15 +00001137 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001138 if (Tok.Type == TT_OverloadedOperator)
1139 return Tok.is(tok::identifier) || Tok.is(tok::kw_new) ||
Daniel Jasper46ef8522013-01-10 13:08:12 +00001140 Tok.is(tok::kw_delete) || Tok.is(tok::kw_bool);
Daniel Jasper26f7e782013-01-08 14:56:18 +00001141 if (Tok.Parent->Type == TT_OverloadedOperator)
Daniel Jasperda927712013-01-07 15:36:15 +00001142 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001143 if (Tok.is(tok::colon))
Nico Weberbcfdd262013-01-12 06:18:40 +00001144 return RootToken.isNot(tok::kw_case) && !Tok.Children.empty() &&
1145 Tok.Type != TT_ObjCMethodExpr;
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001146 if (Tok.Parent->Type == TT_UnaryOperator ||
1147 Tok.Parent->Type == TT_CastRParen)
Daniel Jasperda927712013-01-07 15:36:15 +00001148 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001149 if (Tok.Type == TT_UnaryOperator)
1150 return Tok.Parent->isNot(tok::l_paren) &&
Nico Weber81ed2f12013-01-10 19:36:35 +00001151 Tok.Parent->isNot(tok::l_square) &&
1152 Tok.Parent->isNot(tok::at);
Daniel Jasper26f7e782013-01-08 14:56:18 +00001153 if (Tok.Parent->is(tok::greater) && Tok.is(tok::greater)) {
1154 return Tok.Type == TT_TemplateCloser && Tok.Parent->Type ==
Daniel Jasperda927712013-01-07 15:36:15 +00001155 TT_TemplateCloser && Style.SplitTemplateClosingGreater;
1156 }
Daniel Jasper26f7e782013-01-08 14:56:18 +00001157 if (Tok.Type == TT_DirectorySeparator ||
1158 Tok.Parent->Type == TT_DirectorySeparator)
Daniel Jasperda927712013-01-07 15:36:15 +00001159 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001160 if (Tok.Type == TT_BinaryOperator || Tok.Parent->Type == TT_BinaryOperator)
Daniel Jasperda927712013-01-07 15:36:15 +00001161 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001162 if (Tok.Parent->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
Daniel Jasperda927712013-01-07 15:36:15 +00001163 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001164 if (Tok.is(tok::less) && RootToken.is(tok::hash))
Daniel Jasperda927712013-01-07 15:36:15 +00001165 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001166 if (Tok.Type == TT_TrailingUnaryOperator)
Daniel Jasperda927712013-01-07 15:36:15 +00001167 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001168 return spaceRequiredBetween(*Tok.Parent, Tok);
Daniel Jasperda927712013-01-07 15:36:15 +00001169 }
1170
Daniel Jasper26f7e782013-01-08 14:56:18 +00001171 bool canBreakBefore(const AnnotatedToken &Right) {
1172 const AnnotatedToken &Left = *Right.Parent;
Daniel Jasperda927712013-01-07 15:36:15 +00001173 if (CurrentLineType == LT_ObjCMethodDecl) {
Daniel Jasper26f7e782013-01-08 14:56:18 +00001174 if (Right.is(tok::identifier) && !Right.Children.empty() &&
1175 Right.Children[0].is(tok::colon) && Left.is(tok::identifier))
Daniel Jasperda927712013-01-07 15:36:15 +00001176 return true;
Nico Weber774b9732013-01-12 07:00:16 +00001177 if (Right.is(tok::identifier) && Left.is(tok::l_paren) &&
1178 Left.Parent->is(tok::colon))
Daniel Jasperda927712013-01-07 15:36:15 +00001179 // Don't break this identifier as ':' or identifier
1180 // before it will break.
1181 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001182 if (Right.is(tok::colon) && Left.is(tok::identifier) &&
1183 Left.CanBreakBefore)
Daniel Jasperda927712013-01-07 15:36:15 +00001184 // Don't break at ':' if identifier before it can beak.
1185 return false;
1186 }
Nico Weberbcfdd262013-01-12 06:18:40 +00001187 if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr)
1188 return false;
1189 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
1190 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001191 if (Left.ClosesTemplateDeclaration)
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001192 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001193 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
Daniel Jasper2db356d2013-01-08 20:03:18 +00001194 Left.Type == TT_UnaryOperator || Right.Type == TT_ConditionalExpr)
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001195 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001196 if (Left.is(tok::equal) && CurrentLineType == LT_VirtualFunctionDecl)
Daniel Jasper71607512013-01-07 10:48:50 +00001197 return false;
1198
Daniel Jasper043835a2013-01-09 09:33:39 +00001199 if (Right.is(tok::comment))
1200 return !Right.Children.empty();
Daniel Jasper26f7e782013-01-08 14:56:18 +00001201 if (Right.is(tok::r_paren) || Right.is(tok::l_brace) ||
Daniel Jasper043835a2013-01-09 09:33:39 +00001202 Right.is(tok::greater))
Daniel Jasperbac016b2012-12-03 18:12:45 +00001203 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001204 return (isBinaryOperator(Left) && Left.isNot(tok::lessless)) ||
1205 Left.is(tok::comma) || Right.is(tok::lessless) ||
1206 Right.is(tok::arrow) || Right.is(tok::period) ||
1207 Right.is(tok::colon) || Left.is(tok::semi) ||
Daniel Jasper9c837d02013-01-09 07:06:56 +00001208 Left.is(tok::l_brace) || Left.is(tok::question) ||
Manuel Klimekc8c8a472013-01-10 15:58:26 +00001209 Right.is(tok::r_brace) || Left.Type == TT_ConditionalExpr ||
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001210 (Left.is(tok::r_paren) && Left.Type != TT_CastRParen &&
1211 Right.is(tok::identifier)) ||
Daniel Jasper26f7e782013-01-08 14:56:18 +00001212 (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001213 }
1214
Daniel Jasperbac016b2012-12-03 18:12:45 +00001215 FormatStyle Style;
1216 SourceManager &SourceMgr;
Manuel Klimek6cf58142013-01-07 08:54:53 +00001217 Lexer &Lex;
Daniel Jasper71607512013-01-07 10:48:50 +00001218 LineType CurrentLineType;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001219 AnnotatedToken RootToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001220};
1221
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001222class LexerBasedFormatTokenSource : public FormatTokenSource {
1223public:
1224 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
Daniel Jasper1321eb52012-12-18 21:05:13 +00001225 : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001226 IdentTable(Lex.getLangOpts()) {
1227 Lex.SetKeepWhitespaceMode(true);
1228 }
1229
1230 virtual FormatToken getNextToken() {
1231 if (GreaterStashed) {
1232 FormatTok.NewlinesBefore = 0;
1233 FormatTok.WhiteSpaceStart =
1234 FormatTok.Tok.getLocation().getLocWithOffset(1);
1235 FormatTok.WhiteSpaceLength = 0;
1236 GreaterStashed = false;
1237 return FormatTok;
1238 }
1239
1240 FormatTok = FormatToken();
1241 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimek95419382013-01-07 07:56:50 +00001242 StringRef Text = rawTokenText(FormatTok.Tok);
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001243 FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
Manuel Klimekf6fd00b2013-01-05 22:56:06 +00001244 if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0)
1245 FormatTok.IsFirst = true;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001246
1247 // Consume and record whitespace until we find a significant token.
1248 while (FormatTok.Tok.is(tok::unknown)) {
Manuel Klimeka080a182013-01-02 16:30:12 +00001249 FormatTok.NewlinesBefore += Text.count('\n');
Daniel Jaspercd162382013-01-07 13:26:07 +00001250 FormatTok.HasUnescapedNewline = Text.count("\\\n") !=
1251 FormatTok.NewlinesBefore;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001252 FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
1253
1254 if (FormatTok.Tok.is(tok::eof))
1255 return FormatTok;
1256 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimek95419382013-01-07 07:56:50 +00001257 Text = rawTokenText(FormatTok.Tok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001258 }
Manuel Klimek95419382013-01-07 07:56:50 +00001259
1260 // Now FormatTok is the next non-whitespace token.
1261 FormatTok.TokenLength = Text.size();
1262
Manuel Klimekd4397b92013-01-04 23:34:14 +00001263 // In case the token starts with escaped newlines, we want to
1264 // take them into account as whitespace - this pattern is quite frequent
1265 // in macro definitions.
1266 // FIXME: What do we want to do with other escaped spaces, and escaped
1267 // spaces or newlines in the middle of tokens?
1268 // FIXME: Add a more explicit test.
1269 unsigned i = 0;
Daniel Jasper71607512013-01-07 10:48:50 +00001270 while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
Manuel Klimekd4397b92013-01-04 23:34:14 +00001271 FormatTok.WhiteSpaceLength += 2;
Manuel Klimek95419382013-01-07 07:56:50 +00001272 FormatTok.TokenLength -= 2;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001273 i += 2;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001274 }
1275
1276 if (FormatTok.Tok.is(tok::raw_identifier)) {
Manuel Klimekd4397b92013-01-04 23:34:14 +00001277 IdentifierInfo &Info = IdentTable.get(Text);
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001278 FormatTok.Tok.setIdentifierInfo(&Info);
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001279 FormatTok.Tok.setKind(Info.getTokenID());
1280 }
1281
1282 if (FormatTok.Tok.is(tok::greatergreater)) {
1283 FormatTok.Tok.setKind(tok::greater);
1284 GreaterStashed = true;
1285 }
1286
1287 return FormatTok;
1288 }
1289
1290private:
1291 FormatToken FormatTok;
1292 bool GreaterStashed;
1293 Lexer &Lex;
1294 SourceManager &SourceMgr;
1295 IdentifierTable IdentTable;
1296
1297 /// Returns the text of \c FormatTok.
Manuel Klimek95419382013-01-07 07:56:50 +00001298 StringRef rawTokenText(Token &Tok) {
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001299 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
1300 Tok.getLength());
1301 }
1302};
1303
Daniel Jasperbac016b2012-12-03 18:12:45 +00001304class Formatter : public UnwrappedLineConsumer {
1305public:
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001306 Formatter(clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
1307 Lexer &Lex, SourceManager &SourceMgr,
Daniel Jasperbac016b2012-12-03 18:12:45 +00001308 const std::vector<CharSourceRange> &Ranges)
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001309 : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001310 Ranges(Ranges) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001311
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001312 virtual ~Formatter() {}
Daniel Jasperaccb0b02012-12-04 21:05:31 +00001313
Daniel Jasperbac016b2012-12-03 18:12:45 +00001314 tooling::Replacements format() {
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001315 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001316 UnwrappedLineParser Parser(Diag, Style, Tokens, *this);
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001317 StructuralError = Parser.parse();
Manuel Klimekd4397b92013-01-04 23:34:14 +00001318 unsigned PreviousEndOfLineColumn = 0;
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001319 for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),
1320 E = UnwrappedLines.end();
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001321 I != E; ++I) {
1322 const UnwrappedLine &TheLine = *I;
1323 if (touchesRanges(TheLine)) {
Manuel Klimek517e8942013-01-11 17:54:10 +00001324 llvm::OwningPtr<TokenAnnotator> AnnotatedLine(
1325 new TokenAnnotator(TheLine, Style, SourceMgr, Lex));
1326 if (!AnnotatedLine->annotate())
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001327 break;
Manuel Klimek517e8942013-01-11 17:54:10 +00001328 unsigned Indent = formatFirstToken(AnnotatedLine->getRootToken(),
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001329 TheLine.Level, TheLine.InPPDirective,
1330 PreviousEndOfLineColumn);
Manuel Klimek517e8942013-01-11 17:54:10 +00001331
1332 UnwrappedLine Line(TheLine);
1333 bool FitsOnALine = tryFitMultipleLinesInOne(Indent, Line, AnnotatedLine,
1334 I, E);
1335 UnwrappedLineFormatter Formatter(
1336 Style, SourceMgr, Line, Indent, FitsOnALine,
Rafael Espindolab14d1ca2013-01-12 14:22:42 +00001337 AnnotatedLine->getRootToken(), Replaces, StructuralError);
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001338 PreviousEndOfLineColumn = Formatter.format();
1339 } else {
1340 // If we did not reformat this unwrapped line, the column at the end of
1341 // the last token is unchanged - thus, we can calculate the end of the
1342 // last token, and return the result.
Manuel Klimek517e8942013-01-11 17:54:10 +00001343 const FormatToken *Last = getLastInLine(TheLine);
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001344 PreviousEndOfLineColumn =
1345 SourceMgr.getSpellingColumnNumber(Last->Tok.getLocation()) +
1346 Lex.MeasureTokenLength(Last->Tok.getLocation(), SourceMgr,
1347 Lex.getLangOpts()) -
1348 1;
1349 }
1350 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001351 return Replaces;
1352 }
1353
1354private:
Manuel Klimek517e8942013-01-11 17:54:10 +00001355 /// \brief Tries to merge lines into one.
1356 ///
1357 /// This will change \c Line and \c AnnotatedLine to contain the merged line,
1358 /// if possible; note that \c I will be incremented when lines are merged.
1359 ///
1360 /// Returns whether the resulting \c Line can fit in a single line.
1361 bool tryFitMultipleLinesInOne(unsigned Indent, UnwrappedLine &Line,
1362 llvm::OwningPtr<TokenAnnotator> &AnnotatedLine,
1363 std::vector<UnwrappedLine>::iterator &I,
1364 std::vector<UnwrappedLine>::iterator E) {
1365 unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent;
1366
1367 // Check whether the UnwrappedLine can be put onto a single line. If
1368 // so, this is bound to be the optimal solution (by definition) and we
1369 // don't need to analyze the entire solution space.
1370 bool FitsOnALine = fitsIntoLimit(AnnotatedLine->getRootToken(), Limit);
1371 if (!FitsOnALine || I + 1 == E || I + 2 == E)
1372 return FitsOnALine;
1373
1374 // Try to merge the next two lines if possible.
1375 UnwrappedLine Combined(Line);
1376
1377 // First, check that the current line allows merging. This is the case if
1378 // we're not in a control flow statement and the last token is an opening
1379 // brace.
1380 FormatToken *Last = &Combined.RootToken;
1381 bool AllowedTokens =
Manuel Klimekd5688cf2013-01-11 19:17:44 +00001382 Last->Tok.isNot(tok::kw_if) && Last->Tok.isNot(tok::kw_while) &&
1383 Last->Tok.isNot(tok::kw_do) && Last->Tok.isNot(tok::r_brace) &&
1384 Last->Tok.isNot(tok::kw_else) && Last->Tok.isNot(tok::kw_try) &&
1385 Last->Tok.isNot(tok::kw_catch) && Last->Tok.isNot(tok::kw_for) &&
Nico Weber67015ed2013-01-11 21:14:08 +00001386 // This gets rid of all ObjC @ keywords and methods.
1387 Last->Tok.isNot(tok::at) && Last->Tok.isNot(tok::minus) &&
1388 Last->Tok.isNot(tok::plus);
Manuel Klimek517e8942013-01-11 17:54:10 +00001389 while (!Last->Children.empty())
1390 Last = &Last->Children.back();
1391 if (!Last->Tok.is(tok::l_brace))
1392 return FitsOnALine;
1393
1394 // Second, check that the next line does not contain any braces - if it
1395 // does, readability declines when putting it into a single line.
1396 const FormatToken *Next = &(I + 1)->RootToken;
1397 while (Next) {
1398 AllowedTokens = AllowedTokens && !Next->Tok.is(tok::l_brace) &&
1399 !Next->Tok.is(tok::r_brace);
1400 Last->Children.push_back(*Next);
1401 Last = &Last->Children[0];
1402 Last->Children.clear();
1403 Next = Next->Children.empty() ? NULL : &Next->Children.back();
1404 }
1405
1406 // Last, check that the third line contains a single closing brace.
1407 Next = &(I + 2)->RootToken;
1408 AllowedTokens = AllowedTokens && Next->Tok.is(tok::r_brace);
1409 if (!Next->Children.empty() || !AllowedTokens)
1410 return FitsOnALine;
1411 Last->Children.push_back(*Next);
1412
1413 llvm::OwningPtr<TokenAnnotator> CombinedAnnotator(
1414 new TokenAnnotator(Combined, Style, SourceMgr, Lex));
1415 if (CombinedAnnotator->annotate() &&
1416 fitsIntoLimit(CombinedAnnotator->getRootToken(), Limit)) {
1417 // If the merged line fits, we use that instead and skip the next two
1418 // lines.
1419 AnnotatedLine.reset(CombinedAnnotator.take());
1420 Line = Combined;
1421 I += 2;
1422 }
1423 return FitsOnALine;
1424 }
1425
1426 const FormatToken *getLastInLine(const UnwrappedLine &TheLine) {
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001427 const FormatToken *Last = &TheLine.RootToken;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001428 while (!Last->Children.empty())
1429 Last = &Last->Children.back();
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001430 return Last;
1431 }
1432
1433 bool touchesRanges(const UnwrappedLine &TheLine) {
1434 const FormatToken *First = &TheLine.RootToken;
Manuel Klimek517e8942013-01-11 17:54:10 +00001435 const FormatToken *Last = getLastInLine(TheLine);
Daniel Jaspercd162382013-01-07 13:26:07 +00001436 CharSourceRange LineRange = CharSourceRange::getTokenRange(
Daniel Jasper26f7e782013-01-08 14:56:18 +00001437 First->Tok.getLocation(),
1438 Last->Tok.getLocation());
Daniel Jasperbac016b2012-12-03 18:12:45 +00001439 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001440 if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
1441 Ranges[i].getBegin()) &&
1442 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1443 LineRange.getBegin()))
1444 return true;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001445 }
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001446 return false;
1447 }
1448
1449 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
1450 UnwrappedLines.push_back(TheLine);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001451 }
1452
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001453 /// \brief Add a new line and the required indent before the first Token
1454 /// of the \c UnwrappedLine if there was no structural parsing error.
1455 /// Returns the indent level of the \c UnwrappedLine.
1456 unsigned formatFirstToken(const AnnotatedToken &RootToken, unsigned Level,
1457 bool InPPDirective,
1458 unsigned PreviousEndOfLineColumn) {
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001459 const FormatToken &Tok = RootToken.FormatTok;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001460 if (!Tok.WhiteSpaceStart.isValid() || StructuralError)
1461 return SourceMgr.getSpellingColumnNumber(Tok.Tok.getLocation()) - 1;
1462
1463 unsigned Newlines = std::min(Tok.NewlinesBefore,
1464 Style.MaxEmptyLinesToKeep + 1);
1465 if (Newlines == 0 && !Tok.IsFirst)
1466 Newlines = 1;
1467 unsigned Indent = Level * 2;
1468
1469 bool IsAccessModifier = false;
1470 if (RootToken.is(tok::kw_public) || RootToken.is(tok::kw_protected) ||
1471 RootToken.is(tok::kw_private))
1472 IsAccessModifier = true;
1473 else if (RootToken.is(tok::at) && !RootToken.Children.empty() &&
1474 (RootToken.Children[0].isObjCAtKeyword(tok::objc_public) ||
1475 RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) ||
1476 RootToken.Children[0].isObjCAtKeyword(tok::objc_package) ||
1477 RootToken.Children[0].isObjCAtKeyword(tok::objc_private)))
1478 IsAccessModifier = true;
1479
1480 if (IsAccessModifier &&
1481 static_cast<int>(Indent) + Style.AccessModifierOffset >= 0)
1482 Indent += Style.AccessModifierOffset;
1483 if (!InPPDirective || Tok.HasUnescapedNewline) {
1484 replaceWhitespace(Tok, Newlines, Indent, Style, SourceMgr, Replaces);
1485 } else {
1486 replacePPWhitespace(Tok, Newlines, Indent, PreviousEndOfLineColumn, Style,
1487 SourceMgr, Replaces);
1488 }
1489 return Indent;
1490 }
1491
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001492 clang::DiagnosticsEngine &Diag;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001493 FormatStyle Style;
1494 Lexer &Lex;
1495 SourceManager &SourceMgr;
1496 tooling::Replacements Replaces;
1497 std::vector<CharSourceRange> Ranges;
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001498 std::vector<UnwrappedLine> UnwrappedLines;
1499 bool StructuralError;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001500};
1501
1502tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
1503 SourceManager &SourceMgr,
1504 std::vector<CharSourceRange> Ranges) {
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001505 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
1506 TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
1507 DiagnosticPrinter.BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
1508 DiagnosticsEngine Diagnostics(
1509 llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
1510 &DiagnosticPrinter, false);
1511 Diagnostics.setSourceManager(&SourceMgr);
1512 Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001513 return formatter.format();
1514}
1515
Daniel Jasper46ef8522013-01-10 13:08:12 +00001516LangOptions getFormattingLangOpts() {
1517 LangOptions LangOpts;
1518 LangOpts.CPlusPlus = 1;
1519 LangOpts.CPlusPlus11 = 1;
1520 LangOpts.Bool = 1;
1521 LangOpts.ObjC1 = 1;
1522 LangOpts.ObjC2 = 1;
1523 return LangOpts;
1524}
1525
Daniel Jaspercd162382013-01-07 13:26:07 +00001526} // namespace format
1527} // namespace clang