blob: 999d6210fcd26cb1cf280a2278fc1340be912f05 [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
Manuel Klimekca547db2013-01-16 14:55:28 +000019#define DEBUG_TYPE "format-formatter"
20
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "UnwrappedLineParser.h"
Alexander Kornienko3048aea2013-01-10 15:05:09 +000022#include "clang/Basic/Diagnostic.h"
Daniel Jasper675d2e32012-12-21 10:20:02 +000023#include "clang/Basic/OperatorPrecedence.h"
Chandler Carruthb99083e2013-01-02 10:28:36 +000024#include "clang/Basic/SourceManager.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000025#include "clang/Format/Format.h"
Alexander Kornienko3048aea2013-01-10 15:05:09 +000026#include "clang/Frontend/TextDiagnosticPrinter.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000027#include "clang/Lex/Lexer.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000028#include "llvm/Support/Debug.h"
Daniel Jasper8822d3a2012-12-04 13:02:32 +000029#include <string>
30
Manuel Klimekca547db2013-01-16 14:55:28 +000031// Uncomment to get debug output from tests:
32// #define DEBUG_WITH_TYPE(T, X) do { X; } while(0)
33
Daniel Jasperbac016b2012-12-03 18:12:45 +000034namespace clang {
35namespace format {
36
Daniel Jasper71607512013-01-07 10:48:50 +000037enum TokenType {
Daniel Jasper71607512013-01-07 10:48:50 +000038 TT_BinaryOperator,
Daniel Jasper5cf7cf32013-01-10 11:14:08 +000039 TT_BlockComment,
40 TT_CastRParen,
Daniel Jasper71607512013-01-07 10:48:50 +000041 TT_ConditionalExpr,
42 TT_CtorInitializerColon,
Manuel Klimek407a31a2013-01-15 15:50:27 +000043 TT_ImplicitStringLiteral,
Daniel Jasper5cf7cf32013-01-10 11:14:08 +000044 TT_LineComment,
Daniel Jasper46ef8522013-01-10 13:08:12 +000045 TT_ObjCBlockLParen,
Nico Webered91bba2013-01-10 19:19:14 +000046 TT_ObjCDecl,
Daniel Jasper5cf7cf32013-01-10 11:14:08 +000047 TT_ObjCMethodSpecifier,
Nico Weberbcfdd262013-01-12 06:18:40 +000048 TT_ObjCMethodExpr,
Nico Weber70848232013-01-10 21:30:42 +000049 TT_ObjCProperty,
Daniel Jasper5cf7cf32013-01-10 11:14:08 +000050 TT_OverloadedOperator,
51 TT_PointerOrReference,
Daniel Jasper71607512013-01-07 10:48:50 +000052 TT_PureVirtualSpecifier,
Daniel Jasper5cf7cf32013-01-10 11:14:08 +000053 TT_TemplateCloser,
54 TT_TemplateOpener,
55 TT_TrailingUnaryOperator,
56 TT_UnaryOperator,
57 TT_Unknown
Daniel Jasper71607512013-01-07 10:48:50 +000058};
59
60enum LineType {
61 LT_Invalid,
62 LT_Other,
63 LT_PreprocessorDirective,
64 LT_VirtualFunctionDecl,
Nico Webered91bba2013-01-10 19:19:14 +000065 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
Nico Weber70848232013-01-10 21:30:42 +000066 LT_ObjCMethodDecl,
67 LT_ObjCProperty // An @property line.
Daniel Jasper71607512013-01-07 10:48:50 +000068};
69
Daniel Jasper26f7e782013-01-08 14:56:18 +000070class AnnotatedToken {
71public:
72 AnnotatedToken(const FormatToken &FormatTok)
Manuel Klimek94fc6f12013-01-10 19:17:33 +000073 : FormatTok(FormatTok), Type(TT_Unknown), SpaceRequiredBefore(false),
74 CanBreakBefore(false), MustBreakBefore(false),
Daniel Jasper0df6acd2013-01-16 14:59:02 +000075 ClosesTemplateDeclaration(false), MatchingParen(NULL), Parent(NULL) {}
Daniel Jasper26f7e782013-01-08 14:56:18 +000076
Daniel Jasperfeb18f52013-01-14 14:14:23 +000077 bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); }
78 bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); }
79
Daniel Jasper26f7e782013-01-08 14:56:18 +000080 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
81 return FormatTok.Tok.isObjCAtKeyword(Kind);
82 }
83
84 FormatToken FormatTok;
85
Daniel Jasperbac016b2012-12-03 18:12:45 +000086 TokenType Type;
87
Daniel Jasperbac016b2012-12-03 18:12:45 +000088 bool SpaceRequiredBefore;
89 bool CanBreakBefore;
90 bool MustBreakBefore;
Daniel Jasper9a64fb52013-01-02 15:08:56 +000091
92 bool ClosesTemplateDeclaration;
Daniel Jasper26f7e782013-01-08 14:56:18 +000093
Daniel Jasper0df6acd2013-01-16 14:59:02 +000094 AnnotatedToken *MatchingParen;
95
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +000096 /// \brief The total length of the line up to and including this token.
97 unsigned TotalLength;
98
Daniel Jasper26f7e782013-01-08 14:56:18 +000099 std::vector<AnnotatedToken> Children;
100 AnnotatedToken *Parent;
Daniel Jasper2c6cc482013-01-17 12:53:34 +0000101
102 const AnnotatedToken *getPreviousNoneComment() const {
103 AnnotatedToken *Tok = Parent;
104 while (Tok != NULL && Tok->is(tok::comment))
105 Tok = Tok->Parent;
106 return Tok;
107 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000108};
109
Daniel Jasper995e8202013-01-14 13:08:07 +0000110class AnnotatedLine {
111public:
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000112 AnnotatedLine(const UnwrappedLine &Line)
113 : First(Line.Tokens.front()), Level(Line.Level),
114 InPPDirective(Line.InPPDirective) {
115 assert(!Line.Tokens.empty());
116 AnnotatedToken *Current = &First;
117 for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(),
118 E = Line.Tokens.end();
119 I != E; ++I) {
120 Current->Children.push_back(*I);
121 Current->Children[0].Parent = Current;
122 Current = &Current->Children[0];
123 }
124 Last = Current;
125 }
126 AnnotatedLine(const AnnotatedLine &Other)
127 : First(Other.First), Type(Other.Type), Level(Other.Level),
128 InPPDirective(Other.InPPDirective) {
129 Last = &First;
130 while (!Last->Children.empty()) {
131 Last->Children[0].Parent = Last;
132 Last = &Last->Children[0];
133 }
134 }
135
Daniel Jasper995e8202013-01-14 13:08:07 +0000136 AnnotatedToken First;
137 AnnotatedToken *Last;
138
139 LineType Type;
140 unsigned Level;
141 bool InPPDirective;
142};
143
Daniel Jasper26f7e782013-01-08 14:56:18 +0000144static prec::Level getPrecedence(const AnnotatedToken &Tok) {
145 return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true);
Daniel Jaspercf225b62012-12-24 13:43:52 +0000146}
147
Daniel Jasperbac016b2012-12-03 18:12:45 +0000148FormatStyle getLLVMStyle() {
149 FormatStyle LLVMStyle;
150 LLVMStyle.ColumnLimit = 80;
151 LLVMStyle.MaxEmptyLinesToKeep = 1;
152 LLVMStyle.PointerAndReferenceBindToType = false;
153 LLVMStyle.AccessModifierOffset = -2;
154 LLVMStyle.SplitTemplateClosingGreater = true;
Alexander Kornienko15757312012-12-06 18:03:27 +0000155 LLVMStyle.IndentCaseLabels = false;
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000156 LLVMStyle.SpacesBeforeTrailingComments = 1;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000157 LLVMStyle.BinPackParameters = true;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000158 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000159 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Nico Weber5f500df2013-01-10 20:12:55 +0000160 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000161 return LLVMStyle;
162}
163
164FormatStyle getGoogleStyle() {
165 FormatStyle GoogleStyle;
166 GoogleStyle.ColumnLimit = 80;
167 GoogleStyle.MaxEmptyLinesToKeep = 1;
168 GoogleStyle.PointerAndReferenceBindToType = true;
169 GoogleStyle.AccessModifierOffset = -1;
170 GoogleStyle.SplitTemplateClosingGreater = false;
Alexander Kornienko15757312012-12-06 18:03:27 +0000171 GoogleStyle.IndentCaseLabels = true;
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000172 GoogleStyle.SpacesBeforeTrailingComments = 2;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000173 GoogleStyle.BinPackParameters = false;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000174 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
Daniel Jasperdf3736a2013-01-16 15:44:34 +0000175 GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
Nico Weber5f500df2013-01-10 20:12:55 +0000176 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000177 return GoogleStyle;
178}
179
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000180FormatStyle getChromiumStyle() {
181 FormatStyle ChromiumStyle = getGoogleStyle();
182 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
183 return ChromiumStyle;
184}
185
Daniel Jasperbac016b2012-12-03 18:12:45 +0000186struct OptimizationParameters {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000187 unsigned PenaltyIndentLevel;
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000188 unsigned PenaltyLevelDecrease;
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000189 unsigned PenaltyExcessCharacter;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000190};
191
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000192/// \brief Replaces the whitespace in front of \p Tok. Only call once for
193/// each \c FormatToken.
194static void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
195 unsigned Spaces, const FormatStyle &Style,
196 SourceManager &SourceMgr,
197 tooling::Replacements &Replaces) {
198 Replaces.insert(tooling::Replacement(
199 SourceMgr, Tok.FormatTok.WhiteSpaceStart, Tok.FormatTok.WhiteSpaceLength,
200 std::string(NewLines, '\n') + std::string(Spaces, ' ')));
201}
202
203/// \brief Like \c replaceWhitespace, but additionally adds right-aligned
204/// backslashes to escape newlines inside a preprocessor directive.
205///
206/// This function and \c replaceWhitespace have the same behavior if
207/// \c Newlines == 0.
208static void replacePPWhitespace(
209 const AnnotatedToken &Tok, unsigned NewLines, unsigned Spaces,
210 unsigned WhitespaceStartColumn, const FormatStyle &Style,
211 SourceManager &SourceMgr, tooling::Replacements &Replaces) {
212 std::string NewLineText;
213 if (NewLines > 0) {
214 unsigned Offset = std::min<int>(Style.ColumnLimit - 1,
215 WhitespaceStartColumn);
216 for (unsigned i = 0; i < NewLines; ++i) {
217 NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' ');
218 NewLineText += "\\\n";
219 Offset = 0;
220 }
221 }
222 Replaces.insert(tooling::Replacement(SourceMgr, Tok.FormatTok.WhiteSpaceStart,
223 Tok.FormatTok.WhiteSpaceLength,
224 NewLineText + std::string(Spaces, ' ')));
225}
226
Nico Webere8ccc812013-01-12 22:48:47 +0000227/// \brief Returns if a token is an Objective-C selector name.
228///
Nico Weberea865632013-01-12 22:51:13 +0000229/// For example, "bar" is a selector name in [foo bar:(4 + 5)].
Nico Webere8ccc812013-01-12 22:48:47 +0000230static bool isObjCSelectorName(const AnnotatedToken &Tok) {
231 return Tok.is(tok::identifier) && !Tok.Children.empty() &&
232 Tok.Children[0].is(tok::colon) &&
233 Tok.Children[0].Type == TT_ObjCMethodExpr;
234}
235
Daniel Jasperbac016b2012-12-03 18:12:45 +0000236class UnwrappedLineFormatter {
237public:
Manuel Klimek94fc6f12013-01-10 19:17:33 +0000238 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
Daniel Jasper995e8202013-01-14 13:08:07 +0000239 const AnnotatedLine &Line, unsigned FirstIndent,
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +0000240 const AnnotatedToken &RootToken,
Manuel Klimek94fc6f12013-01-10 19:17:33 +0000241 tooling::Replacements &Replaces, bool StructuralError)
Daniel Jasper1321eb52012-12-18 21:05:13 +0000242 : Style(Style), SourceMgr(SourceMgr), Line(Line),
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +0000243 FirstIndent(FirstIndent), RootToken(RootToken), Replaces(Replaces) {
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000244 Parameters.PenaltyIndentLevel = 15;
Daniel Jasper46a46a22013-01-07 07:13:20 +0000245 Parameters.PenaltyLevelDecrease = 30;
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000246 Parameters.PenaltyExcessCharacter = 1000000;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000247 }
248
Manuel Klimekd4397b92013-01-04 23:34:14 +0000249 /// \brief Formats an \c UnwrappedLine.
250 ///
251 /// \returns The column after the last token in the last line of the
252 /// \c UnwrappedLine.
253 unsigned format() {
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000254 // Initialize state dependent on indent.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000255 LineState State;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000256 State.Column = FirstIndent;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000257 State.NextToken = &RootToken;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000258 State.Stack.push_back(ParenState(FirstIndent + 4, FirstIndent));
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000259 State.ForLoopVariablePos = 0;
260 State.LineContainsContinuedForLoopSection = false;
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000261 State.StartOfLineLevel = 1;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000262
Manuel Klimekca547db2013-01-16 14:55:28 +0000263 DEBUG({
264 DebugTokenState(*State.NextToken);
265 });
266
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000267 // The first token has already been indented and thus consumed.
268 moveStateToNextToken(State);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000269
270 // Start iterating at 1 as we have correctly formatted of Token #0 above.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000271 while (State.NextToken != NULL) {
Manuel Klimek19132302013-01-15 16:41:02 +0000272 if (State.NextToken->Type == TT_ImplicitStringLiteral)
273 // We will not touch the rest of the white space in this
274 // \c UnwrappedLine. The returned value can also not matter, as we
275 // cannot continue an top-level implicit string literal on the next
276 // line.
277 return 0;
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +0000278 if (Line.Last->TotalLength <= getColumnLimit() - FirstIndent) {
Daniel Jasper1321eb52012-12-18 21:05:13 +0000279 addTokenToState(false, false, State);
280 } else {
281 unsigned NoBreak = calcPenalty(State, false, UINT_MAX);
282 unsigned Break = calcPenalty(State, true, NoBreak);
Manuel Klimekca547db2013-01-16 14:55:28 +0000283 DEBUG({
284 if (Break < NoBreak)
285 llvm::errs() << "\n";
286 else
287 llvm::errs() << " ";
288 llvm::errs() << "<";
289 DebugPenalty(Break, Break < NoBreak);
290 llvm::errs() << "/";
291 DebugPenalty(NoBreak, !(Break < NoBreak));
292 llvm::errs() << "> ";
293 DebugTokenState(*State.NextToken);
294 });
Daniel Jasper1321eb52012-12-18 21:05:13 +0000295 addTokenToState(Break < NoBreak, false, State);
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000296 if (State.NextToken != NULL &&
297 State.NextToken->Parent->Type == TT_CtorInitializerColon) {
298 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine &&
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +0000299 Line.Last->TotalLength > getColumnLimit() - State.Column - 1)
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000300 State.Stack.back().BreakAfterComma = true;
301 }
Daniel Jasper1321eb52012-12-18 21:05:13 +0000302 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000303 }
Manuel Klimekca547db2013-01-16 14:55:28 +0000304 DEBUG(llvm::errs() << "\n");
Manuel Klimekd4397b92013-01-04 23:34:14 +0000305 return State.Column;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000306 }
307
308private:
Manuel Klimekca547db2013-01-16 14:55:28 +0000309 void DebugTokenState(const AnnotatedToken &AnnotatedTok) {
310 const Token &Tok = AnnotatedTok.FormatTok.Tok;
311 llvm::errs()
312 << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
313 Tok.getLength());
314 llvm::errs();
315 }
316
317 void DebugPenalty(unsigned Penalty, bool Winner) {
318 llvm::errs().changeColor(Winner ? raw_ostream::GREEN : raw_ostream::RED);
319 if (Penalty == UINT_MAX)
320 llvm::errs() << "MAX";
321 else
322 llvm::errs() << Penalty;
323 llvm::errs().resetColor();
324 }
325
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000326 struct ParenState {
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000327 ParenState(unsigned Indent, unsigned LastSpace)
328 : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000329 BreakBeforeClosingBrace(false), BreakAfterComma(false),
330 HasMultiParameterLine(false) {}
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000331
Daniel Jasperbac016b2012-12-03 18:12:45 +0000332 /// \brief The position to which a specific parenthesis level needs to be
333 /// indented.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000334 unsigned Indent;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000335
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000336 /// \brief The position of the last space on each level.
337 ///
338 /// Used e.g. to break like:
339 /// functionCall(Parameter, otherCall(
340 /// OtherParameter));
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000341 unsigned LastSpace;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000342
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000343 /// \brief The position the first "<<" operator encountered on each level.
344 ///
345 /// Used to align "<<" operators. 0 if no such operator has been encountered
346 /// on a level.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000347 unsigned FirstLessLess;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000348
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000349 /// \brief Whether a newline needs to be inserted before the block's closing
350 /// brace.
351 ///
352 /// We only want to insert a newline before the closing brace if there also
353 /// was a newline after the beginning left brace.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000354 bool BreakBeforeClosingBrace;
355
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000356 bool BreakAfterComma;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000357 bool HasMultiParameterLine;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000358
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000359 bool operator<(const ParenState &Other) const {
360 if (Indent != Other.Indent)
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000361 return Indent < Other.Indent;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000362 if (LastSpace != Other.LastSpace)
363 return LastSpace < Other.LastSpace;
364 if (FirstLessLess != Other.FirstLessLess)
365 return FirstLessLess < Other.FirstLessLess;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000366 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
367 return BreakBeforeClosingBrace;
Daniel Jasperb3123142013-01-12 07:36:22 +0000368 if (BreakAfterComma != Other.BreakAfterComma)
369 return BreakAfterComma;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000370 if (HasMultiParameterLine != Other.HasMultiParameterLine)
371 return HasMultiParameterLine;
Daniel Jasperb3123142013-01-12 07:36:22 +0000372 return false;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000373 }
374 };
375
376 /// \brief The current state when indenting a unwrapped line.
377 ///
378 /// As the indenting tries different combinations this is copied by value.
379 struct LineState {
380 /// \brief The number of used columns in the current line.
381 unsigned Column;
382
383 /// \brief The token that needs to be next formatted.
384 const AnnotatedToken *NextToken;
385
386 /// \brief The parenthesis level of the first token on the current line.
387 unsigned StartOfLineLevel;
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000388
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000389 /// \brief The column of the first variable in a for-loop declaration.
390 ///
391 /// Used to align the second variable if necessary.
392 unsigned ForLoopVariablePos;
393
394 /// \brief \c true if this line contains a continued for-loop section.
395 bool LineContainsContinuedForLoopSection;
396
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000397 /// \brief A stack keeping track of properties applying to parenthesis
398 /// levels.
399 std::vector<ParenState> Stack;
400
401 /// \brief Comparison operator to be able to used \c LineState in \c map.
402 bool operator<(const LineState &Other) const {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000403 if (Other.NextToken != NextToken)
404 return Other.NextToken > NextToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000405 if (Other.Column != Column)
406 return Other.Column > Column;
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000407 if (Other.StartOfLineLevel != StartOfLineLevel)
408 return Other.StartOfLineLevel > StartOfLineLevel;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000409 if (Other.ForLoopVariablePos != ForLoopVariablePos)
410 return Other.ForLoopVariablePos < ForLoopVariablePos;
411 if (Other.LineContainsContinuedForLoopSection !=
412 LineContainsContinuedForLoopSection)
413 return LineContainsContinuedForLoopSection;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000414 return Other.Stack < Stack;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000415 }
416 };
417
Daniel Jasper20409152012-12-04 14:54:30 +0000418 /// \brief Appends the next token to \p State and updates information
419 /// necessary for indentation.
420 ///
421 /// Puts the token on the current line if \p Newline is \c true and adds a
422 /// line break and necessary indentation otherwise.
423 ///
424 /// If \p DryRun is \c false, also creates and stores the required
425 /// \c Replacement.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000426 void addTokenToState(bool Newline, bool DryRun, LineState &State) {
Daniel Jasper9c837d02013-01-09 07:06:56 +0000427 const AnnotatedToken &Current = *State.NextToken;
428 const AnnotatedToken &Previous = *State.NextToken->Parent;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000429 assert(State.Stack.size());
430 unsigned ParenLevel = State.Stack.size() - 1;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000431
432 if (Newline) {
Manuel Klimek060143e2013-01-02 18:33:23 +0000433 unsigned WhitespaceStartColumn = State.Column;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000434 if (Current.is(tok::r_brace)) {
435 State.Column = Line.Level * 2;
Daniel Jasper9c837d02013-01-09 07:06:56 +0000436 } else if (Current.is(tok::string_literal) &&
437 Previous.is(tok::string_literal)) {
438 State.Column = State.Column - Previous.FormatTok.TokenLength;
439 } else if (Current.is(tok::lessless) &&
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000440 State.Stack[ParenLevel].FirstLessLess != 0) {
441 State.Column = State.Stack[ParenLevel].FirstLessLess;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000442 } else if (ParenLevel != 0 &&
Daniel Jasper9c837d02013-01-09 07:06:56 +0000443 (Previous.is(tok::equal) || Current.is(tok::arrow) ||
444 Current.is(tok::period) || Previous.is(tok::question) ||
445 Previous.Type == TT_ConditionalExpr)) {
446 // Indent and extra 4 spaces after if we know the current expression is
447 // continued. Don't do that on the top level, as we already indent 4
448 // there.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000449 State.Column = State.Stack[ParenLevel].Indent + 4;
Daniel Jasper9c837d02013-01-09 07:06:56 +0000450 } else if (RootToken.is(tok::kw_for) && Previous.is(tok::comma)) {
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000451 State.Column = State.ForLoopVariablePos;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000452 } else if (State.NextToken->Parent->ClosesTemplateDeclaration) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000453 State.Column = State.Stack[ParenLevel].Indent - 4;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000454 } else {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000455 State.Column = State.Stack[ParenLevel].Indent;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000456 }
457
Manuel Klimek2851c162013-01-10 14:36:46 +0000458 // A line starting with a closing brace is assumed to be correct for the
459 // same level as before the opening brace.
460 State.StartOfLineLevel = ParenLevel + (Current.is(tok::r_brace) ? 0 : 1);
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000461
Daniel Jasper26f7e782013-01-08 14:56:18 +0000462 if (RootToken.is(tok::kw_for))
Daniel Jasper9c837d02013-01-09 07:06:56 +0000463 State.LineContainsContinuedForLoopSection = Previous.isNot(tok::semi);
Daniel Jasper20409152012-12-04 14:54:30 +0000464
Manuel Klimek060143e2013-01-02 18:33:23 +0000465 if (!DryRun) {
466 if (!Line.InPPDirective)
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000467 replaceWhitespace(Current.FormatTok, 1, State.Column, Style,
468 SourceMgr, Replaces);
Manuel Klimek060143e2013-01-02 18:33:23 +0000469 else
Daniel Jasper9c837d02013-01-09 07:06:56 +0000470 replacePPWhitespace(Current.FormatTok, 1, State.Column,
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000471 WhitespaceStartColumn, Style, SourceMgr,
472 Replaces);
Manuel Klimek060143e2013-01-02 18:33:23 +0000473 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000474
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000475 State.Stack[ParenLevel].LastSpace = State.Column;
Nico Weberf681fa82013-01-12 07:05:25 +0000476 if (Current.is(tok::colon) && State.NextToken->Type != TT_ConditionalExpr)
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000477 State.Stack[ParenLevel].Indent += 2;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000478 } else {
Daniel Jasper9c837d02013-01-09 07:06:56 +0000479 if (Current.is(tok::equal) && RootToken.is(tok::kw_for))
480 State.ForLoopVariablePos = State.Column -
481 Previous.FormatTok.TokenLength;
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000482
Daniel Jasper26f7e782013-01-08 14:56:18 +0000483 unsigned Spaces = State.NextToken->SpaceRequiredBefore ? 1 : 0;
484 if (State.NextToken->Type == TT_LineComment)
Daniel Jasper7ad4eff2013-01-07 11:09:06 +0000485 Spaces = Style.SpacesBeforeTrailingComments;
Daniel Jasper20409152012-12-04 14:54:30 +0000486
Daniel Jasperbac016b2012-12-03 18:12:45 +0000487 if (!DryRun)
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000488 replaceWhitespace(Current, 0, Spaces, Style, SourceMgr, Replaces);
Daniel Jasper20409152012-12-04 14:54:30 +0000489
Daniel Jasper3fc0bb72013-01-09 10:40:23 +0000490 // FIXME: Do we need to do this for assignments nested in other
491 // expressions?
492 if (RootToken.isNot(tok::kw_for) && ParenLevel == 0 &&
Daniel Jasper9cda8002013-01-07 13:08:40 +0000493 (getPrecedence(Previous) == prec::Assignment ||
Daniel Jasper9c837d02013-01-09 07:06:56 +0000494 Previous.is(tok::kw_return)))
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000495 State.Stack[ParenLevel].Indent = State.Column + Spaces;
Daniel Jasper7d19bc22013-01-11 14:23:32 +0000496 if (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
Daniel Jasper26f7e782013-01-08 14:56:18 +0000497 State.NextToken->Parent->Type == TT_TemplateOpener)
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000498 State.Stack[ParenLevel].Indent = State.Column + Spaces;
Daniel Jasper2c6cc482013-01-17 12:53:34 +0000499 if (Current.getPreviousNoneComment()->is(tok::comma) &&
500 Current.isNot(tok::comment))
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000501 State.Stack[ParenLevel].HasMultiParameterLine = true;
502
Daniel Jasper1321eb52012-12-18 21:05:13 +0000503
Daniel Jasper9cda8002013-01-07 13:08:40 +0000504 // Top-level spaces that are not part of assignments are exempt as that
505 // mostly leads to better results.
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000506 State.Column += Spaces;
Daniel Jasper9cda8002013-01-07 13:08:40 +0000507 if (Spaces > 0 &&
508 (ParenLevel != 0 || getPrecedence(Previous) == prec::Assignment))
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000509 State.Stack[ParenLevel].LastSpace = State.Column;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000510 }
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000511
512 // If we break after an {, we should also break before the corresponding }.
513 if (Newline && Previous.is(tok::l_brace))
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000514 State.Stack.back().BreakBeforeClosingBrace = true;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000515
516 // If we are breaking after '(', '{', '<' or ',', we need to break after
517 // future commas as well to avoid bin packing.
518 if (!Style.BinPackParameters && Newline &&
519 (Previous.is(tok::comma) || Previous.is(tok::l_paren) ||
520 Previous.is(tok::l_brace) || Previous.Type == TT_TemplateOpener))
521 State.Stack.back().BreakAfterComma = true;
522
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000523 moveStateToNextToken(State);
Daniel Jasper20409152012-12-04 14:54:30 +0000524 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000525
Daniel Jasper20409152012-12-04 14:54:30 +0000526 /// \brief Mark the next token as consumed in \p State and modify its stacks
527 /// accordingly.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000528 void moveStateToNextToken(LineState &State) {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000529 const AnnotatedToken &Current = *State.NextToken;
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000530 assert(State.Stack.size());
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000531
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000532 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
533 State.Stack.back().FirstLessLess = State.Column;
Daniel Jasper3b5943f2012-12-06 09:56:08 +0000534
Daniel Jaspercf225b62012-12-24 13:43:52 +0000535 // If we encounter an opening (, [, { or <, we add a level to our stacks to
Daniel Jasper20409152012-12-04 14:54:30 +0000536 // prepare for the following tokens.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000537 if (Current.is(tok::l_paren) || Current.is(tok::l_square) ||
538 Current.is(tok::l_brace) ||
539 State.NextToken->Type == TT_TemplateOpener) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000540 unsigned NewIndent;
Manuel Klimek2851c162013-01-10 14:36:46 +0000541 if (Current.is(tok::l_brace)) {
542 // FIXME: This does not work with nested static initializers.
543 // Implement a better handling for static initializers and similar
544 // constructs.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000545 NewIndent = Line.Level * 2 + 2;
Manuel Klimek2851c162013-01-10 14:36:46 +0000546 } else {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000547 NewIndent = 4 + State.Stack.back().LastSpace;
Manuel Klimek2851c162013-01-10 14:36:46 +0000548 }
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000549 State.Stack.push_back(
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000550 ParenState(NewIndent, State.Stack.back().LastSpace));
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000551
552 // If the entire set of parameters will not fit on the current line, we
553 // will need to break after commas on this level to avoid bin-packing.
554 if (!Style.BinPackParameters && Current.MatchingParen != NULL &&
555 !Current.Children.empty()) {
556 if (getColumnLimit() < State.Column + Current.FormatTok.TokenLength +
557 Current.MatchingParen->TotalLength -
558 Current.Children[0].TotalLength) {
559 State.Stack.back().BreakAfterComma = true;
560 }
561 }
Daniel Jasper20409152012-12-04 14:54:30 +0000562 }
563
Daniel Jaspercf225b62012-12-24 13:43:52 +0000564 // If we encounter a closing ), ], } or >, we can remove a level from our
Daniel Jasper20409152012-12-04 14:54:30 +0000565 // stacks.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000566 if (Current.is(tok::r_paren) || Current.is(tok::r_square) ||
567 (Current.is(tok::r_brace) && State.NextToken != &RootToken) ||
568 State.NextToken->Type == TT_TemplateCloser) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000569 State.Stack.pop_back();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000570 }
Manuel Klimek2851c162013-01-10 14:36:46 +0000571
Daniel Jasper26f7e782013-01-08 14:56:18 +0000572 if (State.NextToken->Children.empty())
573 State.NextToken = NULL;
574 else
575 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek2851c162013-01-10 14:36:46 +0000576
577 State.Column += Current.FormatTok.TokenLength;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000578 }
579
Nico Weberdecf7bc2013-01-07 15:15:29 +0000580 /// \brief Calculate the penalty for splitting after the token at \p Index.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000581 unsigned splitPenalty(const AnnotatedToken &Tok) {
582 const AnnotatedToken &Left = Tok;
583 const AnnotatedToken &Right = Tok.Children[0];
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000584
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000585 if (Left.is(tok::l_brace) && Right.isNot(tok::l_brace))
586 return 50;
587 if (Left.is(tok::equal) && Right.is(tok::l_brace))
588 return 150;
589
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000590 // In for-loops, prefer breaking at ',' and ';'.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000591 if (RootToken.is(tok::kw_for) &&
592 (Left.isNot(tok::comma) && Left.isNot(tok::semi)))
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000593 return 20;
594
Daniel Jasper26f7e782013-01-08 14:56:18 +0000595 if (Left.is(tok::semi) || Left.is(tok::comma) ||
596 Left.ClosesTemplateDeclaration)
Daniel Jasperbac016b2012-12-03 18:12:45 +0000597 return 0;
Nico Webere8ccc812013-01-12 22:48:47 +0000598
599 // In Objective-C method expressions, prefer breaking before "param:" over
600 // breaking after it.
601 if (isObjCSelectorName(Right))
602 return 0;
603 if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr)
604 return 20;
605
Daniel Jasper26f7e782013-01-08 14:56:18 +0000606 if (Left.is(tok::l_paren))
Daniel Jasper723f0302013-01-02 14:40:02 +0000607 return 20;
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000608
Daniel Jasper9c837d02013-01-09 07:06:56 +0000609 if (Left.is(tok::question) || Left.Type == TT_ConditionalExpr)
610 return prec::Assignment;
Daniel Jasper9cda8002013-01-07 13:08:40 +0000611 prec::Level Level = getPrecedence(Left);
612
613 // Breaking after an assignment leads to a bad result as the two sides of
614 // the assignment are visually very close together.
615 if (Level == prec::Assignment)
616 return 50;
617
Daniel Jaspere2c7acf2012-12-24 00:13:23 +0000618 if (Level != prec::Unknown)
619 return Level;
620
Daniel Jasper26f7e782013-01-08 14:56:18 +0000621 if (Right.is(tok::arrow) || Right.is(tok::period))
Daniel Jasper46a46a22013-01-07 07:13:20 +0000622 return 150;
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000623
Daniel Jasperbac016b2012-12-03 18:12:45 +0000624 return 3;
625 }
626
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000627 unsigned getColumnLimit() {
628 return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0);
629 }
630
Daniel Jasperbac016b2012-12-03 18:12:45 +0000631 /// \brief Calculate the number of lines needed to format the remaining part
632 /// of the unwrapped line.
633 ///
634 /// Assumes the formatting so far has led to
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000635 /// the \c LineSta \p State. If \p NewLine is set, a new line will be
Daniel Jasperbac016b2012-12-03 18:12:45 +0000636 /// added after the previous token.
637 ///
638 /// \param StopAt is used for optimization. If we can determine that we'll
639 /// definitely need at least \p StopAt additional lines, we already know of a
640 /// better solution.
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000641 unsigned calcPenalty(LineState State, bool NewLine, unsigned StopAt) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000642 // We are at the end of the unwrapped line, so we don't need any more lines.
Daniel Jasper26f7e782013-01-08 14:56:18 +0000643 if (State.NextToken == NULL)
Daniel Jasperbac016b2012-12-03 18:12:45 +0000644 return 0;
645
Daniel Jasper26f7e782013-01-08 14:56:18 +0000646 if (!NewLine && State.NextToken->MustBreakBefore)
Daniel Jasperbac016b2012-12-03 18:12:45 +0000647 return UINT_MAX;
Manuel Klimek2c7739e2013-01-14 16:41:43 +0000648 if (NewLine && !State.NextToken->CanBreakBefore &&
649 !(State.NextToken->is(tok::r_brace) &&
650 State.Stack.back().BreakBeforeClosingBrace))
Daniel Jasperbac016b2012-12-03 18:12:45 +0000651 return UINT_MAX;
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000652 if (!NewLine && State.NextToken->is(tok::r_brace) &&
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000653 State.Stack.back().BreakBeforeClosingBrace)
Manuel Klimekc8c8a472013-01-10 15:58:26 +0000654 return UINT_MAX;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000655 if (!NewLine && State.NextToken->Parent->is(tok::semi) &&
Daniel Jaspera324a0e2012-12-21 14:37:20 +0000656 State.LineContainsContinuedForLoopSection)
657 return UINT_MAX;
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000658 if (!NewLine && State.NextToken->Parent->is(tok::comma) &&
Daniel Jasper2c6cc482013-01-17 12:53:34 +0000659 State.NextToken->isNot(tok::comment) &&
Daniel Jasper7e9bf8c2013-01-11 11:37:55 +0000660 State.Stack.back().BreakAfterComma)
661 return UINT_MAX;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000662 // Trying to insert a parameter on a new line if there are already more than
663 // one parameter on the current line is bin packing.
664 if (NewLine && State.NextToken->Parent->is(tok::comma) &&
665 State.Stack.back().HasMultiParameterLine && !Style.BinPackParameters)
666 return UINT_MAX;
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +0000667 if (!NewLine && State.NextToken->Type == TT_CtorInitializerColon)
668 return UINT_MAX;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000669
Daniel Jasper33182dd2012-12-05 14:57:28 +0000670 unsigned CurrentPenalty = 0;
671 if (NewLine) {
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000672 CurrentPenalty += Parameters.PenaltyIndentLevel * State.Stack.size() +
Daniel Jasper26f7e782013-01-08 14:56:18 +0000673 splitPenalty(*State.NextToken->Parent);
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000674 } else {
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000675 if (State.Stack.size() < State.StartOfLineLevel &&
676 State.NextToken->is(tok::identifier))
Daniel Jaspera4974cf2012-12-24 16:43:00 +0000677 CurrentPenalty += Parameters.PenaltyLevelDecrease *
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000678 (State.StartOfLineLevel - State.Stack.size());
Daniel Jasper33182dd2012-12-05 14:57:28 +0000679 }
680
Daniel Jasper20409152012-12-04 14:54:30 +0000681 addTokenToState(NewLine, true, State);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000682
Daniel Jasperceb99ab2013-01-09 10:16:05 +0000683 // Exceeding column limit is bad, assign penalty.
684 if (State.Column > getColumnLimit()) {
685 unsigned ExcessCharacters = State.Column - getColumnLimit();
686 CurrentPenalty += Parameters.PenaltyExcessCharacter * ExcessCharacters;
687 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000688
Daniel Jasperbac016b2012-12-03 18:12:45 +0000689 if (StopAt <= CurrentPenalty)
690 return UINT_MAX;
691 StopAt -= CurrentPenalty;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000692 StateMap::iterator I = Memory.find(State);
Daniel Jasper33182dd2012-12-05 14:57:28 +0000693 if (I != Memory.end()) {
694 // If this state has already been examined, we can safely return the
695 // previous result if we
696 // - have not hit the optimatization (and thus returned UINT_MAX) OR
697 // - are now computing for a smaller or equal StopAt.
698 unsigned SavedResult = I->second.first;
699 unsigned SavedStopAt = I->second.second;
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000700 if (SavedResult != UINT_MAX)
701 return SavedResult + CurrentPenalty;
702 else if (StopAt <= SavedStopAt)
703 return UINT_MAX;
Daniel Jasper33182dd2012-12-05 14:57:28 +0000704 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000705
706 unsigned NoBreak = calcPenalty(State, false, StopAt);
707 unsigned WithBreak = calcPenalty(State, true, std::min(StopAt, NoBreak));
708 unsigned Result = std::min(NoBreak, WithBreak);
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000709
710 // We have to store 'Result' without adding 'CurrentPenalty' as the latter
711 // can depend on 'NewLine'.
Daniel Jasper33182dd2012-12-05 14:57:28 +0000712 Memory[State] = std::pair<unsigned, unsigned>(Result, StopAt);
Daniel Jasper9a0b4942012-12-17 14:34:14 +0000713
714 return Result == UINT_MAX ? UINT_MAX : Result + CurrentPenalty;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000715 }
716
Daniel Jasperbac016b2012-12-03 18:12:45 +0000717 FormatStyle Style;
718 SourceManager &SourceMgr;
Daniel Jasper995e8202013-01-14 13:08:07 +0000719 const AnnotatedLine &Line;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +0000720 const unsigned FirstIndent;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000721 const AnnotatedToken &RootToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000722 tooling::Replacements &Replaces;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000723
Daniel Jasper33182dd2012-12-05 14:57:28 +0000724 // A map from an indent state to a pair (Result, Used-StopAt).
Daniel Jasper604eb4c2013-01-11 10:22:12 +0000725 typedef std::map<LineState, std::pair<unsigned, unsigned> > StateMap;
Daniel Jasper33182dd2012-12-05 14:57:28 +0000726 StateMap Memory;
727
Daniel Jasperbac016b2012-12-03 18:12:45 +0000728 OptimizationParameters Parameters;
729};
730
731/// \brief Determines extra information about the tokens comprising an
732/// \c UnwrappedLine.
733class TokenAnnotator {
734public:
Daniel Jasper995e8202013-01-14 13:08:07 +0000735 TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex,
736 AnnotatedLine &Line)
737 : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Line(Line) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000738
739 /// \brief A parser that gathers additional information about tokens.
740 ///
741 /// The \c TokenAnnotator tries to matches parenthesis and square brakets and
742 /// store a parenthesis levels. It also tries to resolve matching "<" and ">"
743 /// into template parameter lists.
744 class AnnotatingParser {
745 public:
Daniel Jasper26f7e782013-01-08 14:56:18 +0000746 AnnotatingParser(AnnotatedToken &RootToken)
Nico Weberbcfdd262013-01-12 06:18:40 +0000747 : CurrentToken(&RootToken), KeywordVirtualFound(false),
748 ColonIsObjCMethodExpr(false) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000749
Nico Weber6a21a552013-01-18 02:43:57 +0000750 /// \brief A helper class to manage AnnotatingParser::ColonIsObjCMethodExpr.
751 struct ObjCSelectorRAII {
752 AnnotatingParser &P;
753 bool ColonWasObjCMethodExpr;
754
755 ObjCSelectorRAII(AnnotatingParser &P)
756 : P(P), ColonWasObjCMethodExpr(P.ColonIsObjCMethodExpr) {}
757
758 ~ObjCSelectorRAII() { P.ColonIsObjCMethodExpr = ColonWasObjCMethodExpr; }
759
760 void markStart(AnnotatedToken &Left) {
761 P.ColonIsObjCMethodExpr = true;
762 Left.Type = TT_ObjCMethodExpr;
763 }
764
765 void markEnd(AnnotatedToken &Right) { Right.Type = TT_ObjCMethodExpr; }
766 };
767
768
Daniel Jasper20409152012-12-04 14:54:30 +0000769 bool parseAngle() {
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000770 if (CurrentToken == NULL)
771 return false;
772 AnnotatedToken *Left = CurrentToken->Parent;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000773 while (CurrentToken != NULL) {
774 if (CurrentToken->is(tok::greater)) {
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000775 Left->MatchingParen = CurrentToken;
776 CurrentToken->MatchingParen = Left;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000777 CurrentToken->Type = TT_TemplateCloser;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000778 next();
779 return true;
780 }
Daniel Jasper26f7e782013-01-08 14:56:18 +0000781 if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square) ||
782 CurrentToken->is(tok::r_brace))
Daniel Jasperbac016b2012-12-03 18:12:45 +0000783 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000784 if (CurrentToken->is(tok::pipepipe) || CurrentToken->is(tok::ampamp) ||
785 CurrentToken->is(tok::question) || CurrentToken->is(tok::colon))
Daniel Jasperbac016b2012-12-03 18:12:45 +0000786 return false;
Daniel Jasper1f42f112013-01-04 18:52:56 +0000787 if (!consumeToken())
788 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000789 }
790 return false;
791 }
792
Nico Weber5096a442013-01-17 17:17:19 +0000793 bool parseParens(bool LookForDecls = false) {
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000794 if (CurrentToken == NULL)
795 return false;
Nico Weber6a21a552013-01-18 02:43:57 +0000796 bool StartsObjCMethodExpr = false;
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000797 AnnotatedToken *Left = CurrentToken->Parent;
Nico Weber6a21a552013-01-18 02:43:57 +0000798 if (CurrentToken->is(tok::caret)) {
799 // ^( starts a block.
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000800 Left->Type = TT_ObjCBlockLParen;
Nico Weber6a21a552013-01-18 02:43:57 +0000801 } else if (AnnotatedToken *MaybeSel = Left->Parent) {
802 // @selector( starts a selector.
803 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Parent &&
804 MaybeSel->Parent->is(tok::at)) {
805 StartsObjCMethodExpr = true;
806 }
807 }
808
809 ObjCSelectorRAII objCSelector(*this);
810 if (StartsObjCMethodExpr)
811 objCSelector.markStart(*Left);
812
Daniel Jasper26f7e782013-01-08 14:56:18 +0000813 while (CurrentToken != NULL) {
Nico Weber5096a442013-01-17 17:17:19 +0000814 // LookForDecls is set when "if (" has been seen. Check for
815 // 'identifier' '*' 'identifier' followed by not '=' -- this
816 // '*' has to be a binary operator but determineStarAmpUsage() will
817 // categorize it as an unary operator, so set the right type here.
818 if (LookForDecls && !CurrentToken->Children.empty()) {
819 AnnotatedToken &Prev = *CurrentToken->Parent;
820 AnnotatedToken &Next = CurrentToken->Children[0];
821 if (Prev.Parent->is(tok::identifier) &&
822 (Prev.is(tok::star) || Prev.is(tok::amp)) &&
823 CurrentToken->is(tok::identifier) && Next.isNot(tok::equal)) {
824 Prev.Type = TT_BinaryOperator;
825 LookForDecls = false;
826 }
827 }
828
Daniel Jasper26f7e782013-01-08 14:56:18 +0000829 if (CurrentToken->is(tok::r_paren)) {
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000830 Left->MatchingParen = CurrentToken;
831 CurrentToken->MatchingParen = Left;
Nico Weber6a21a552013-01-18 02:43:57 +0000832
833 if (StartsObjCMethodExpr)
834 objCSelector.markEnd(*CurrentToken);
835
Daniel Jasperbac016b2012-12-03 18:12:45 +0000836 next();
837 return true;
838 }
Daniel Jasper26f7e782013-01-08 14:56:18 +0000839 if (CurrentToken->is(tok::r_square) || CurrentToken->is(tok::r_brace))
Daniel Jasperbac016b2012-12-03 18:12:45 +0000840 return false;
Daniel Jasper1f42f112013-01-04 18:52:56 +0000841 if (!consumeToken())
842 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000843 }
844 return false;
845 }
846
Daniel Jasper20409152012-12-04 14:54:30 +0000847 bool parseSquare() {
Nico Weberbcfdd262013-01-12 06:18:40 +0000848 if (!CurrentToken)
849 return false;
850
851 // A '[' could be an index subscript (after an indentifier or after
852 // ')' or ']'), or it could be the start of an Objective-C method
853 // expression.
854 AnnotatedToken *LSquare = CurrentToken->Parent;
855 bool StartsObjCMethodExpr =
856 !LSquare->Parent || LSquare->Parent->is(tok::colon) ||
857 LSquare->Parent->is(tok::l_square) ||
858 LSquare->Parent->is(tok::l_paren) ||
859 LSquare->Parent->is(tok::kw_return) ||
860 LSquare->Parent->is(tok::kw_throw) ||
861 getBinOpPrecedence(LSquare->Parent->FormatTok.Tok.getKind(),
862 true, true) > prec::Unknown;
863
Nico Weber6a21a552013-01-18 02:43:57 +0000864 ObjCSelectorRAII objCSelector(*this);
865 if (StartsObjCMethodExpr)
866 objCSelector.markStart(*LSquare);
Nico Weberbcfdd262013-01-12 06:18:40 +0000867
Daniel Jasper26f7e782013-01-08 14:56:18 +0000868 while (CurrentToken != NULL) {
869 if (CurrentToken->is(tok::r_square)) {
Nico Weber6a21a552013-01-18 02:43:57 +0000870 if (StartsObjCMethodExpr)
871 objCSelector.markEnd(*CurrentToken);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000872 next();
873 return true;
874 }
Daniel Jasper26f7e782013-01-08 14:56:18 +0000875 if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_brace))
Daniel Jasperbac016b2012-12-03 18:12:45 +0000876 return false;
Daniel Jasper1f42f112013-01-04 18:52:56 +0000877 if (!consumeToken())
878 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000879 }
880 return false;
881 }
882
Daniel Jasper700e7102013-01-10 09:26:47 +0000883 bool parseBrace() {
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000884 // Lines are fine to end with '{'.
885 if (CurrentToken == NULL)
886 return true;
887 AnnotatedToken *Left = CurrentToken->Parent;
Daniel Jasper700e7102013-01-10 09:26:47 +0000888 while (CurrentToken != NULL) {
889 if (CurrentToken->is(tok::r_brace)) {
Daniel Jasper0df6acd2013-01-16 14:59:02 +0000890 Left->MatchingParen = CurrentToken;
891 CurrentToken->MatchingParen = Left;
Daniel Jasper700e7102013-01-10 09:26:47 +0000892 next();
893 return true;
894 }
895 if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square))
896 return false;
897 if (!consumeToken())
898 return false;
899 }
Daniel Jasper700e7102013-01-10 09:26:47 +0000900 return true;
901 }
902
Daniel Jasper20409152012-12-04 14:54:30 +0000903 bool parseConditional() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000904 while (CurrentToken != NULL) {
905 if (CurrentToken->is(tok::colon)) {
906 CurrentToken->Type = TT_ConditionalExpr;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000907 next();
908 return true;
909 }
Daniel Jasper1f42f112013-01-04 18:52:56 +0000910 if (!consumeToken())
911 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000912 }
913 return false;
914 }
915
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000916 bool parseTemplateDeclaration() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000917 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
918 CurrentToken->Type = TT_TemplateOpener;
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000919 next();
920 if (!parseAngle())
921 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +0000922 CurrentToken->Parent->ClosesTemplateDeclaration = true;
Daniel Jasper9a64fb52013-01-02 15:08:56 +0000923 parseLine();
924 return true;
925 }
926 return false;
927 }
928
Daniel Jasper1f42f112013-01-04 18:52:56 +0000929 bool consumeToken() {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000930 AnnotatedToken *Tok = CurrentToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000931 next();
Daniel Jasper26f7e782013-01-08 14:56:18 +0000932 switch (Tok->FormatTok.Tok.getKind()) {
Nico Webercd52bda2013-01-10 23:11:41 +0000933 case tok::plus:
934 case tok::minus:
935 // At the start of the line, +/- specific ObjectiveC method
936 // declarations.
937 if (Tok->Parent == NULL)
938 Tok->Type = TT_ObjCMethodSpecifier;
939 break;
Nico Weberbcfdd262013-01-12 06:18:40 +0000940 case tok::colon:
941 // Colons from ?: are handled in parseConditional().
Daniel Jasper1f2b0782013-01-16 16:23:19 +0000942 if (Tok->Parent->is(tok::r_paren))
943 Tok->Type = TT_CtorInitializerColon;
Nico Weberbcfdd262013-01-12 06:18:40 +0000944 if (ColonIsObjCMethodExpr)
945 Tok->Type = TT_ObjCMethodExpr;
946 break;
Nico Weber5096a442013-01-17 17:17:19 +0000947 case tok::kw_if:
948 case tok::kw_while:
949 if (CurrentToken->is(tok::l_paren)) {
950 next();
951 if (!parseParens(/*LookForDecls=*/true))
952 return false;
953 }
954 break;
Nico Weber94fb7292013-01-18 05:50:57 +0000955 case tok::l_paren:
Daniel Jasper1f42f112013-01-04 18:52:56 +0000956 if (!parseParens())
957 return false;
Nico Weber94fb7292013-01-18 05:50:57 +0000958 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000959 case tok::l_square:
Daniel Jasper1f42f112013-01-04 18:52:56 +0000960 if (!parseSquare())
961 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000962 break;
Daniel Jasper700e7102013-01-10 09:26:47 +0000963 case tok::l_brace:
964 if (!parseBrace())
965 return false;
966 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000967 case tok::less:
Daniel Jasper20409152012-12-04 14:54:30 +0000968 if (parseAngle())
Daniel Jasper26f7e782013-01-08 14:56:18 +0000969 Tok->Type = TT_TemplateOpener;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000970 else {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000971 Tok->Type = TT_BinaryOperator;
972 CurrentToken = Tok;
973 next();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000974 }
975 break;
Daniel Jasper1f42f112013-01-04 18:52:56 +0000976 case tok::r_paren:
977 case tok::r_square:
978 return false;
Daniel Jasper700e7102013-01-10 09:26:47 +0000979 case tok::r_brace:
980 // Lines can start with '}'.
981 if (Tok->Parent != NULL)
982 return false;
983 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000984 case tok::greater:
Daniel Jasper26f7e782013-01-08 14:56:18 +0000985 Tok->Type = TT_BinaryOperator;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000986 break;
987 case tok::kw_operator:
Daniel Jasper26f7e782013-01-08 14:56:18 +0000988 if (CurrentToken->is(tok::l_paren)) {
989 CurrentToken->Type = TT_OverloadedOperator;
Daniel Jasperf6aef6a2012-12-24 10:56:04 +0000990 next();
Daniel Jasper26f7e782013-01-08 14:56:18 +0000991 if (CurrentToken != NULL && CurrentToken->is(tok::r_paren)) {
992 CurrentToken->Type = TT_OverloadedOperator;
Daniel Jasperf6aef6a2012-12-24 10:56:04 +0000993 next();
994 }
995 } else {
Daniel Jasper26f7e782013-01-08 14:56:18 +0000996 while (CurrentToken != NULL && CurrentToken->isNot(tok::l_paren)) {
997 CurrentToken->Type = TT_OverloadedOperator;
Daniel Jasperf6aef6a2012-12-24 10:56:04 +0000998 next();
999 }
1000 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001001 break;
1002 case tok::question:
Daniel Jasper20409152012-12-04 14:54:30 +00001003 parseConditional();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001004 break;
Daniel Jasper9a64fb52013-01-02 15:08:56 +00001005 case tok::kw_template:
1006 parseTemplateDeclaration();
1007 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001008 default:
1009 break;
1010 }
Daniel Jasper1f42f112013-01-04 18:52:56 +00001011 return true;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001012 }
1013
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001014 void parseIncludeDirective() {
Manuel Klimek407a31a2013-01-15 15:50:27 +00001015 next();
1016 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
1017 next();
1018 while (CurrentToken != NULL) {
1019 CurrentToken->Type = TT_ImplicitStringLiteral;
1020 next();
1021 }
1022 } else {
1023 while (CurrentToken != NULL) {
1024 next();
1025 }
1026 }
1027 }
1028
1029 void parseWarningOrError() {
1030 next();
1031 // We still want to format the whitespace left of the first token of the
1032 // warning or error.
1033 next();
Daniel Jasper26f7e782013-01-08 14:56:18 +00001034 while (CurrentToken != NULL) {
Manuel Klimek407a31a2013-01-15 15:50:27 +00001035 CurrentToken->Type = TT_ImplicitStringLiteral;
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001036 next();
1037 }
1038 }
1039
1040 void parsePreprocessorDirective() {
1041 next();
Daniel Jasper26f7e782013-01-08 14:56:18 +00001042 if (CurrentToken == NULL)
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001043 return;
Manuel Klimekf6fd00b2013-01-05 22:56:06 +00001044 // Hashes in the middle of a line can lead to any strange token
1045 // sequence.
Daniel Jasper26f7e782013-01-08 14:56:18 +00001046 if (CurrentToken->FormatTok.Tok.getIdentifierInfo() == NULL)
Manuel Klimekf6fd00b2013-01-05 22:56:06 +00001047 return;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001048 switch (
1049 CurrentToken->FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001050 case tok::pp_include:
Nico Weberb23ae0c2012-12-21 18:21:56 +00001051 case tok::pp_import:
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001052 parseIncludeDirective();
1053 break;
Manuel Klimek407a31a2013-01-15 15:50:27 +00001054 case tok::pp_error:
1055 case tok::pp_warning:
1056 parseWarningOrError();
1057 break;
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001058 default:
1059 break;
1060 }
1061 }
1062
Daniel Jasper71607512013-01-07 10:48:50 +00001063 LineType parseLine() {
Daniel Jasper26f7e782013-01-08 14:56:18 +00001064 if (CurrentToken->is(tok::hash)) {
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001065 parsePreprocessorDirective();
Daniel Jasper71607512013-01-07 10:48:50 +00001066 return LT_PreprocessorDirective;
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001067 }
Daniel Jasper26f7e782013-01-08 14:56:18 +00001068 while (CurrentToken != NULL) {
1069 if (CurrentToken->is(tok::kw_virtual))
Daniel Jasper71607512013-01-07 10:48:50 +00001070 KeywordVirtualFound = true;
Daniel Jasper1f42f112013-01-04 18:52:56 +00001071 if (!consumeToken())
Daniel Jasper71607512013-01-07 10:48:50 +00001072 return LT_Invalid;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001073 }
Daniel Jasper71607512013-01-07 10:48:50 +00001074 if (KeywordVirtualFound)
1075 return LT_VirtualFunctionDecl;
1076 return LT_Other;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001077 }
1078
1079 void next() {
Daniel Jasper26f7e782013-01-08 14:56:18 +00001080 if (CurrentToken != NULL && !CurrentToken->Children.empty())
1081 CurrentToken = &CurrentToken->Children[0];
1082 else
1083 CurrentToken = NULL;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001084 }
1085
1086 private:
Daniel Jasper26f7e782013-01-08 14:56:18 +00001087 AnnotatedToken *CurrentToken;
Daniel Jasper71607512013-01-07 10:48:50 +00001088 bool KeywordVirtualFound;
Nico Weberbcfdd262013-01-12 06:18:40 +00001089 bool ColonIsObjCMethodExpr;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001090 };
1091
Daniel Jasper26f7e782013-01-08 14:56:18 +00001092 void calculateExtraInformation(AnnotatedToken &Current) {
1093 Current.SpaceRequiredBefore = spaceRequiredBefore(Current);
1094
Manuel Klimek526ed112013-01-09 15:25:02 +00001095 if (Current.FormatTok.MustBreakBefore) {
Daniel Jasper26f7e782013-01-08 14:56:18 +00001096 Current.MustBreakBefore = true;
1097 } else {
Daniel Jasper487f64b2013-01-13 16:10:20 +00001098 if (Current.Type == TT_LineComment) {
1099 Current.MustBreakBefore = Current.FormatTok.NewlinesBefore > 0;
Daniel Jasper2c6cc482013-01-17 12:53:34 +00001100 } else if ((Current.Parent->is(tok::comment) &&
1101 Current.FormatTok.NewlinesBefore > 0) ||
Daniel Jasper487f64b2013-01-13 16:10:20 +00001102 (Current.is(tok::string_literal) &&
1103 Current.Parent->is(tok::string_literal))) {
Manuel Klimek526ed112013-01-09 15:25:02 +00001104 Current.MustBreakBefore = true;
Manuel Klimek526ed112013-01-09 15:25:02 +00001105 } else {
1106 Current.MustBreakBefore = false;
1107 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001108 }
Daniel Jasper26f7e782013-01-08 14:56:18 +00001109 Current.CanBreakBefore = Current.MustBreakBefore || canBreakBefore(Current);
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001110 if (Current.MustBreakBefore)
1111 Current.TotalLength = Current.Parent->TotalLength + Style.ColumnLimit;
1112 else
1113 Current.TotalLength = Current.Parent->TotalLength +
1114 Current.FormatTok.TokenLength +
1115 (Current.SpaceRequiredBefore ? 1 : 0);
Daniel Jasper26f7e782013-01-08 14:56:18 +00001116 if (!Current.Children.empty())
1117 calculateExtraInformation(Current.Children[0]);
1118 }
1119
Daniel Jasper995e8202013-01-14 13:08:07 +00001120 void annotate() {
Daniel Jasper995e8202013-01-14 13:08:07 +00001121 AnnotatingParser Parser(Line.First);
1122 Line.Type = Parser.parseLine();
1123 if (Line.Type == LT_Invalid)
1124 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001125
Daniel Jasper995e8202013-01-14 13:08:07 +00001126 determineTokenTypes(Line.First, /*IsRHS=*/false);
Daniel Jasper71607512013-01-07 10:48:50 +00001127
Daniel Jasper995e8202013-01-14 13:08:07 +00001128 if (Line.First.Type == TT_ObjCMethodSpecifier)
1129 Line.Type = LT_ObjCMethodDecl;
1130 else if (Line.First.Type == TT_ObjCDecl)
1131 Line.Type = LT_ObjCDecl;
1132 else if (Line.First.Type == TT_ObjCProperty)
1133 Line.Type = LT_ObjCProperty;
Daniel Jasper71607512013-01-07 10:48:50 +00001134
Daniel Jasper995e8202013-01-14 13:08:07 +00001135 Line.First.SpaceRequiredBefore = true;
1136 Line.First.MustBreakBefore = Line.First.FormatTok.MustBreakBefore;
1137 Line.First.CanBreakBefore = Line.First.MustBreakBefore;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001138
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001139 Line.First.TotalLength = Line.First.FormatTok.TokenLength;
Daniel Jasper995e8202013-01-14 13:08:07 +00001140 if (!Line.First.Children.empty())
1141 calculateExtraInformation(Line.First.Children[0]);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001142 }
1143
1144private:
Daniel Jasper26f7e782013-01-08 14:56:18 +00001145 void determineTokenTypes(AnnotatedToken &Current, bool IsRHS) {
1146 if (getPrecedence(Current) == prec::Assignment ||
1147 Current.is(tok::kw_return) || Current.is(tok::kw_throw))
1148 IsRHS = true;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001149
Daniel Jasper26f7e782013-01-08 14:56:18 +00001150 if (Current.Type == TT_Unknown) {
1151 if (Current.is(tok::star) || Current.is(tok::amp)) {
1152 Current.Type = determineStarAmpUsage(Current, IsRHS);
Daniel Jasper886568d2013-01-09 08:36:49 +00001153 } else if (Current.is(tok::minus) || Current.is(tok::plus) ||
1154 Current.is(tok::caret)) {
1155 Current.Type = determinePlusMinusCaretUsage(Current);
Daniel Jasper26f7e782013-01-08 14:56:18 +00001156 } else if (Current.is(tok::minusminus) || Current.is(tok::plusplus)) {
1157 Current.Type = determineIncrementUsage(Current);
1158 } else if (Current.is(tok::exclaim)) {
1159 Current.Type = TT_UnaryOperator;
1160 } else if (isBinaryOperator(Current)) {
1161 Current.Type = TT_BinaryOperator;
1162 } else if (Current.is(tok::comment)) {
1163 std::string Data(Lexer::getSpelling(Current.FormatTok.Tok, SourceMgr,
1164 Lex.getLangOpts()));
Manuel Klimek6cf58142013-01-07 08:54:53 +00001165 if (StringRef(Data).startswith("//"))
Daniel Jasper26f7e782013-01-08 14:56:18 +00001166 Current.Type = TT_LineComment;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001167 else
Daniel Jasper26f7e782013-01-08 14:56:18 +00001168 Current.Type = TT_BlockComment;
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001169 } else if (Current.is(tok::r_paren) &&
1170 (Current.Parent->Type == TT_PointerOrReference ||
Daniel Jasper4981bd02013-01-13 08:01:36 +00001171 Current.Parent->Type == TT_TemplateCloser) &&
1172 (Current.Children.empty() ||
1173 (Current.Children[0].isNot(tok::equal) &&
1174 Current.Children[0].isNot(tok::semi) &&
1175 Current.Children[0].isNot(tok::l_brace)))) {
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001176 // FIXME: We need to get smarter and understand more cases of casts.
1177 Current.Type = TT_CastRParen;
Nico Webered91bba2013-01-10 19:19:14 +00001178 } else if (Current.is(tok::at) && Current.Children.size()) {
1179 switch (Current.Children[0].FormatTok.Tok.getObjCKeywordID()) {
1180 case tok::objc_interface:
1181 case tok::objc_implementation:
1182 case tok::objc_protocol:
1183 Current.Type = TT_ObjCDecl;
Nico Weber70848232013-01-10 21:30:42 +00001184 break;
1185 case tok::objc_property:
1186 Current.Type = TT_ObjCProperty;
1187 break;
Nico Webered91bba2013-01-10 19:19:14 +00001188 default:
1189 break;
1190 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001191 }
1192 }
Daniel Jasper26f7e782013-01-08 14:56:18 +00001193
1194 if (!Current.Children.empty())
1195 determineTokenTypes(Current.Children[0], IsRHS);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001196 }
1197
Daniel Jasper26f7e782013-01-08 14:56:18 +00001198 bool isBinaryOperator(const AnnotatedToken &Tok) {
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001199 // Comma is a binary operator, but does not behave as such wrt. formatting.
Daniel Jaspercf225b62012-12-24 13:43:52 +00001200 return getPrecedence(Tok) > prec::Comma;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001201 }
1202
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001203 /// \brief Returns the previous token ignoring comments.
1204 const AnnotatedToken *getPreviousToken(const AnnotatedToken &Tok) {
1205 const AnnotatedToken *PrevToken = Tok.Parent;
1206 while (PrevToken != NULL && PrevToken->is(tok::comment))
1207 PrevToken = PrevToken->Parent;
1208 return PrevToken;
1209 }
1210
1211 /// \brief Returns the next token ignoring comments.
1212 const AnnotatedToken *getNextToken(const AnnotatedToken &Tok) {
1213 if (Tok.Children.empty())
1214 return NULL;
1215 const AnnotatedToken *NextToken = &Tok.Children[0];
1216 while (NextToken->is(tok::comment)) {
1217 if (NextToken->Children.empty())
1218 return NULL;
1219 NextToken = &NextToken->Children[0];
1220 }
1221 return NextToken;
1222 }
1223
1224 /// \brief Return the type of the given token assuming it is * or &.
Daniel Jasper26f7e782013-01-08 14:56:18 +00001225 TokenType determineStarAmpUsage(const AnnotatedToken &Tok, bool IsRHS) {
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001226 const AnnotatedToken *PrevToken = getPreviousToken(Tok);
1227 if (PrevToken == NULL)
Daniel Jasper71607512013-01-07 10:48:50 +00001228 return TT_UnaryOperator;
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001229
1230 const AnnotatedToken *NextToken = getNextToken(Tok);
1231 if (NextToken == NULL)
Daniel Jasper71607512013-01-07 10:48:50 +00001232 return TT_Unknown;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001233
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001234 if (PrevToken->is(tok::l_paren) || PrevToken->is(tok::l_square) ||
1235 PrevToken->is(tok::l_brace) || PrevToken->is(tok::comma) ||
1236 PrevToken->is(tok::kw_return) || PrevToken->is(tok::colon) ||
1237 PrevToken->Type == TT_BinaryOperator ||
Daniel Jasper48bd7b72013-01-16 16:04:06 +00001238 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
Daniel Jasper71607512013-01-07 10:48:50 +00001239 return TT_UnaryOperator;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001240
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001241 if (PrevToken->FormatTok.Tok.isLiteral() || PrevToken->is(tok::r_paren) ||
1242 PrevToken->is(tok::r_square) || NextToken->FormatTok.Tok.isLiteral() ||
1243 NextToken->is(tok::plus) || NextToken->is(tok::minus) ||
1244 NextToken->is(tok::plusplus) || NextToken->is(tok::minusminus) ||
1245 NextToken->is(tok::tilde) || NextToken->is(tok::exclaim) ||
1246 NextToken->is(tok::l_paren) || NextToken->is(tok::l_square) ||
1247 NextToken->is(tok::kw_alignof) || NextToken->is(tok::kw_sizeof))
Daniel Jasper71607512013-01-07 10:48:50 +00001248 return TT_BinaryOperator;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001249
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001250 if (NextToken->is(tok::comma) || NextToken->is(tok::r_paren) ||
1251 NextToken->is(tok::greater))
Daniel Jasper71607512013-01-07 10:48:50 +00001252 return TT_PointerOrReference;
Daniel Jasperef5b9c32013-01-02 15:46:59 +00001253
Daniel Jasper112fb272012-12-05 07:51:39 +00001254 // It is very unlikely that we are going to find a pointer or reference type
1255 // definition on the RHS of an assignment.
Nico Weber00d5a042012-12-23 01:07:46 +00001256 if (IsRHS)
Daniel Jasper71607512013-01-07 10:48:50 +00001257 return TT_BinaryOperator;
Daniel Jasper112fb272012-12-05 07:51:39 +00001258
Daniel Jasper71607512013-01-07 10:48:50 +00001259 return TT_PointerOrReference;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001260 }
1261
Daniel Jasper886568d2013-01-09 08:36:49 +00001262 TokenType determinePlusMinusCaretUsage(const AnnotatedToken &Tok) {
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001263 const AnnotatedToken *PrevToken = getPreviousToken(Tok);
1264 if (PrevToken == NULL)
1265 return TT_UnaryOperator;
1266
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001267 // Use heuristics to recognize unary operators.
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001268 if (PrevToken->is(tok::equal) || PrevToken->is(tok::l_paren) ||
1269 PrevToken->is(tok::comma) || PrevToken->is(tok::l_square) ||
1270 PrevToken->is(tok::question) || PrevToken->is(tok::colon) ||
1271 PrevToken->is(tok::kw_return) || PrevToken->is(tok::kw_case) ||
1272 PrevToken->is(tok::at) || PrevToken->is(tok::l_brace))
Daniel Jasper71607512013-01-07 10:48:50 +00001273 return TT_UnaryOperator;
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001274
1275 // There can't be to consecutive binary operators.
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001276 if (PrevToken->Type == TT_BinaryOperator)
Daniel Jasper71607512013-01-07 10:48:50 +00001277 return TT_UnaryOperator;
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001278
1279 // Fall back to marking the token as binary operator.
Daniel Jasper71607512013-01-07 10:48:50 +00001280 return TT_BinaryOperator;
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001281 }
1282
1283 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Daniel Jasper26f7e782013-01-08 14:56:18 +00001284 TokenType determineIncrementUsage(const AnnotatedToken &Tok) {
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001285 const AnnotatedToken *PrevToken = getPreviousToken(Tok);
1286 if (PrevToken == NULL)
Daniel Jasper4abbb532013-01-14 12:18:19 +00001287 return TT_UnaryOperator;
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001288 if (PrevToken->is(tok::r_paren) || PrevToken->is(tok::r_square) ||
1289 PrevToken->is(tok::identifier))
Daniel Jasper71607512013-01-07 10:48:50 +00001290 return TT_TrailingUnaryOperator;
Daniel Jasper98e6b4a2012-12-21 09:41:31 +00001291
Daniel Jasper71607512013-01-07 10:48:50 +00001292 return TT_UnaryOperator;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001293 }
1294
Daniel Jasper26f7e782013-01-08 14:56:18 +00001295 bool spaceRequiredBetween(const AnnotatedToken &Left,
1296 const AnnotatedToken &Right) {
Daniel Jasper765561f2013-01-08 16:17:54 +00001297 if (Right.is(tok::hashhash))
1298 return Left.is(tok::hash);
1299 if (Left.is(tok::hashhash) || Left.is(tok::hash))
1300 return Right.is(tok::hash);
Daniel Jasper8b39c662012-12-10 18:59:13 +00001301 if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma))
1302 return false;
Nico Weber5f500df2013-01-10 20:12:55 +00001303 if (Right.is(tok::less) &&
1304 (Left.is(tok::kw_template) ||
Daniel Jasper995e8202013-01-14 13:08:07 +00001305 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
Daniel Jasperbac016b2012-12-03 18:12:45 +00001306 return true;
1307 if (Left.is(tok::arrow) || Right.is(tok::arrow))
1308 return false;
1309 if (Left.is(tok::exclaim) || Left.is(tok::tilde))
1310 return false;
Nico Webercb4d6902013-01-08 19:40:21 +00001311 if (Left.is(tok::at) &&
1312 (Right.is(tok::identifier) || Right.is(tok::string_literal) ||
1313 Right.is(tok::char_constant) || Right.is(tok::numeric_constant) ||
Daniel Jasper46ef8522013-01-10 13:08:12 +00001314 Right.is(tok::l_paren) || Right.is(tok::l_brace) ||
1315 Right.is(tok::kw_true) || Right.is(tok::kw_false)))
Fariborz Jahanian154120c2012-12-20 19:54:13 +00001316 return false;
Daniel Jasper6b825c22013-01-16 07:19:28 +00001317 if (Left.is(tok::coloncolon))
1318 return false;
1319 if (Right.is(tok::coloncolon))
1320 return Left.isNot(tok::identifier) && Left.isNot(tok::greater);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001321 if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less))
1322 return false;
Daniel Jasperc74e2792012-12-07 09:52:15 +00001323 if (Right.is(tok::amp) || Right.is(tok::star))
Daniel Jasper26f7e782013-01-08 14:56:18 +00001324 return Left.FormatTok.Tok.isLiteral() ||
Daniel Jasperd7610b82012-12-24 16:51:15 +00001325 (Left.isNot(tok::star) && Left.isNot(tok::amp) &&
1326 !Style.PointerAndReferenceBindToType);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001327 if (Left.is(tok::amp) || Left.is(tok::star))
Daniel Jasper26f7e782013-01-08 14:56:18 +00001328 return Right.FormatTok.Tok.isLiteral() ||
1329 Style.PointerAndReferenceBindToType;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001330 if (Right.is(tok::star) && Left.is(tok::l_paren))
1331 return false;
Nico Weberbcfdd262013-01-12 06:18:40 +00001332 if (Left.is(tok::l_square) || Right.is(tok::r_square))
1333 return false;
1334 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr)
Daniel Jasperbac016b2012-12-03 18:12:45 +00001335 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001336 if (Left.is(tok::period) || Right.is(tok::period))
1337 return false;
Nico Weberbcfdd262013-01-12 06:18:40 +00001338 if (Left.is(tok::colon))
1339 return Left.Type != TT_ObjCMethodExpr;
1340 if (Right.is(tok::colon))
1341 return Right.Type != TT_ObjCMethodExpr;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001342 if (Left.is(tok::l_paren))
1343 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001344 if (Right.is(tok::l_paren)) {
Daniel Jasper995e8202013-01-14 13:08:07 +00001345 return Line.Type == LT_ObjCDecl || Left.is(tok::kw_if) ||
Nico Webered91bba2013-01-10 19:19:14 +00001346 Left.is(tok::kw_for) || Left.is(tok::kw_while) ||
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001347 Left.is(tok::kw_switch) || Left.is(tok::kw_return) ||
Daniel Jasper088dab52013-01-11 16:09:04 +00001348 Left.is(tok::kw_catch) || Left.is(tok::kw_new) ||
1349 Left.is(tok::kw_delete);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001350 }
Daniel Jasper26f7e782013-01-08 14:56:18 +00001351 if (Left.is(tok::at) &&
1352 Right.FormatTok.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Nico Weberd0af4b42013-01-07 16:14:28 +00001353 return false;
Manuel Klimek36fab8d2013-01-10 13:24:24 +00001354 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
1355 return false;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001356 return true;
1357 }
1358
Daniel Jasper26f7e782013-01-08 14:56:18 +00001359 bool spaceRequiredBefore(const AnnotatedToken &Tok) {
Daniel Jasper995e8202013-01-14 13:08:07 +00001360 if (Line.Type == LT_ObjCMethodDecl) {
Daniel Jasper26f7e782013-01-08 14:56:18 +00001361 if (Tok.is(tok::identifier) && !Tok.Children.empty() &&
1362 Tok.Children[0].is(tok::colon) && Tok.Parent->is(tok::identifier))
Daniel Jasperda927712013-01-07 15:36:15 +00001363 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001364 if (Tok.is(tok::colon))
Daniel Jasperda927712013-01-07 15:36:15 +00001365 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001366 if (Tok.Parent->Type == TT_ObjCMethodSpecifier)
Nico Weberaab60052013-01-17 06:14:50 +00001367 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001368 if (Tok.Parent->is(tok::r_paren) && Tok.is(tok::identifier))
Daniel Jasperda927712013-01-07 15:36:15 +00001369 // Don't space between ')' and <id>
1370 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001371 if (Tok.Parent->is(tok::colon) && Tok.is(tok::l_paren))
Daniel Jasperda927712013-01-07 15:36:15 +00001372 // Don't space between ':' and '('
1373 return false;
1374 }
Daniel Jasper995e8202013-01-14 13:08:07 +00001375 if (Line.Type == LT_ObjCProperty &&
Nico Weber70848232013-01-10 21:30:42 +00001376 (Tok.is(tok::equal) || Tok.Parent->is(tok::equal)))
1377 return false;
Daniel Jasperda927712013-01-07 15:36:15 +00001378
Daniel Jasper4e9008a2013-01-13 08:19:51 +00001379 if (Tok.Parent->is(tok::comma))
1380 return true;
Daniel Jasper46ef8522013-01-10 13:08:12 +00001381 if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
Daniel Jasperda927712013-01-07 15:36:15 +00001382 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001383 if (Tok.Type == TT_OverloadedOperator)
1384 return Tok.is(tok::identifier) || Tok.is(tok::kw_new) ||
Daniel Jasper46ef8522013-01-10 13:08:12 +00001385 Tok.is(tok::kw_delete) || Tok.is(tok::kw_bool);
Daniel Jasper26f7e782013-01-08 14:56:18 +00001386 if (Tok.Parent->Type == TT_OverloadedOperator)
Daniel Jasperda927712013-01-07 15:36:15 +00001387 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001388 if (Tok.is(tok::colon))
Daniel Jasper995e8202013-01-14 13:08:07 +00001389 return Line.First.isNot(tok::kw_case) && !Tok.Children.empty() &&
Nico Weberbcfdd262013-01-12 06:18:40 +00001390 Tok.Type != TT_ObjCMethodExpr;
Daniel Jasper5cf7cf32013-01-10 11:14:08 +00001391 if (Tok.Parent->Type == TT_UnaryOperator ||
1392 Tok.Parent->Type == TT_CastRParen)
Daniel Jasperda927712013-01-07 15:36:15 +00001393 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001394 if (Tok.Type == TT_UnaryOperator)
1395 return Tok.Parent->isNot(tok::l_paren) &&
Nico Webercd458332013-01-12 23:48:49 +00001396 Tok.Parent->isNot(tok::l_square) && Tok.Parent->isNot(tok::at) &&
1397 (Tok.Parent->isNot(tok::colon) ||
1398 Tok.Parent->Type != TT_ObjCMethodExpr);
Daniel Jasper26f7e782013-01-08 14:56:18 +00001399 if (Tok.Parent->is(tok::greater) && Tok.is(tok::greater)) {
1400 return Tok.Type == TT_TemplateCloser && Tok.Parent->Type ==
Daniel Jasperda927712013-01-07 15:36:15 +00001401 TT_TemplateCloser && Style.SplitTemplateClosingGreater;
1402 }
Daniel Jasper26f7e782013-01-08 14:56:18 +00001403 if (Tok.Type == TT_BinaryOperator || Tok.Parent->Type == TT_BinaryOperator)
Daniel Jasperda927712013-01-07 15:36:15 +00001404 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001405 if (Tok.Parent->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
Daniel Jasperda927712013-01-07 15:36:15 +00001406 return false;
Daniel Jasper995e8202013-01-14 13:08:07 +00001407 if (Tok.is(tok::less) && Line.First.is(tok::hash))
Daniel Jasperda927712013-01-07 15:36:15 +00001408 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001409 if (Tok.Type == TT_TrailingUnaryOperator)
Daniel Jasperda927712013-01-07 15:36:15 +00001410 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001411 return spaceRequiredBetween(*Tok.Parent, Tok);
Daniel Jasperda927712013-01-07 15:36:15 +00001412 }
1413
Daniel Jasper26f7e782013-01-08 14:56:18 +00001414 bool canBreakBefore(const AnnotatedToken &Right) {
1415 const AnnotatedToken &Left = *Right.Parent;
Daniel Jasper995e8202013-01-14 13:08:07 +00001416 if (Line.Type == LT_ObjCMethodDecl) {
Daniel Jasper26f7e782013-01-08 14:56:18 +00001417 if (Right.is(tok::identifier) && !Right.Children.empty() &&
1418 Right.Children[0].is(tok::colon) && Left.is(tok::identifier))
Daniel Jasperda927712013-01-07 15:36:15 +00001419 return true;
Nico Weber774b9732013-01-12 07:00:16 +00001420 if (Right.is(tok::identifier) && Left.is(tok::l_paren) &&
1421 Left.Parent->is(tok::colon))
Daniel Jasperda927712013-01-07 15:36:15 +00001422 // Don't break this identifier as ':' or identifier
1423 // before it will break.
1424 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001425 if (Right.is(tok::colon) && Left.is(tok::identifier) &&
1426 Left.CanBreakBefore)
Daniel Jasperda927712013-01-07 15:36:15 +00001427 // Don't break at ':' if identifier before it can beak.
1428 return false;
1429 }
Nico Weberbcfdd262013-01-12 06:18:40 +00001430 if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr)
1431 return false;
1432 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
1433 return true;
Nico Webere8ccc812013-01-12 22:48:47 +00001434 if (isObjCSelectorName(Right))
1435 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001436 if (Left.ClosesTemplateDeclaration)
Daniel Jasper5eda31e2013-01-02 18:30:06 +00001437 return true;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001438 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
Daniel Jasper2db356d2013-01-08 20:03:18 +00001439 Left.Type == TT_UnaryOperator || Right.Type == TT_ConditionalExpr)
Daniel Jasper4dc41de2013-01-02 08:44:14 +00001440 return false;
Daniel Jasper995e8202013-01-14 13:08:07 +00001441 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
Daniel Jasper71607512013-01-07 10:48:50 +00001442 return false;
1443
Daniel Jasper2c6cc482013-01-17 12:53:34 +00001444 if (Right.Type == TT_LineComment)
Daniel Jasper487f64b2013-01-13 16:10:20 +00001445 // We rely on MustBreakBefore being set correctly here as we should not
1446 // change the "binding" behavior of a comment.
1447 return false;
1448
Daniel Jasper60ca75d2013-01-17 13:31:52 +00001449 // Allow breaking after a trailing 'const', e.g. after a method declaration,
1450 // unless it is follow by ';', '{' or '='.
1451 if (Left.is(tok::kw_const) && Left.Parent != NULL &&
1452 Left.Parent->is(tok::r_paren))
1453 return Right.isNot(tok::l_brace) && Right.isNot(tok::semi) &&
1454 Right.isNot(tok::equal);
1455
Manuel Klimek2c7739e2013-01-14 16:41:43 +00001456 // We only break before r_brace if there was a corresponding break before
1457 // the l_brace, which is tracked by BreakBeforeClosingBrace.
1458 if (Right.is(tok::r_brace))
1459 return false;
1460
Daniel Jasperb369c2c2013-01-15 14:27:39 +00001461 if (Right.is(tok::r_paren) || Right.is(tok::greater))
Daniel Jasperbac016b2012-12-03 18:12:45 +00001462 return false;
Daniel Jasper26f7e782013-01-08 14:56:18 +00001463 return (isBinaryOperator(Left) && Left.isNot(tok::lessless)) ||
1464 Left.is(tok::comma) || Right.is(tok::lessless) ||
1465 Right.is(tok::arrow) || Right.is(tok::period) ||
1466 Right.is(tok::colon) || Left.is(tok::semi) ||
Manuel Klimek2c7739e2013-01-14 16:41:43 +00001467 Left.is(tok::l_brace) || Left.is(tok::question) || Left.Type ==
1468 TT_ConditionalExpr || (Left.is(tok::r_paren) && Left.Type !=
1469 TT_CastRParen && Right.is(tok::identifier)) ||
Daniel Jasper26f7e782013-01-08 14:56:18 +00001470 (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001471 }
1472
Daniel Jasperbac016b2012-12-03 18:12:45 +00001473 FormatStyle Style;
1474 SourceManager &SourceMgr;
Manuel Klimek6cf58142013-01-07 08:54:53 +00001475 Lexer &Lex;
Daniel Jasper995e8202013-01-14 13:08:07 +00001476 AnnotatedLine &Line;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001477};
1478
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001479class LexerBasedFormatTokenSource : public FormatTokenSource {
1480public:
1481 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
Daniel Jasper1321eb52012-12-18 21:05:13 +00001482 : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001483 IdentTable(Lex.getLangOpts()) {
1484 Lex.SetKeepWhitespaceMode(true);
1485 }
1486
1487 virtual FormatToken getNextToken() {
1488 if (GreaterStashed) {
1489 FormatTok.NewlinesBefore = 0;
1490 FormatTok.WhiteSpaceStart =
1491 FormatTok.Tok.getLocation().getLocWithOffset(1);
1492 FormatTok.WhiteSpaceLength = 0;
1493 GreaterStashed = false;
1494 return FormatTok;
1495 }
1496
1497 FormatTok = FormatToken();
1498 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimek95419382013-01-07 07:56:50 +00001499 StringRef Text = rawTokenText(FormatTok.Tok);
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001500 FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
Manuel Klimekf6fd00b2013-01-05 22:56:06 +00001501 if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0)
1502 FormatTok.IsFirst = true;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001503
1504 // Consume and record whitespace until we find a significant token.
1505 while (FormatTok.Tok.is(tok::unknown)) {
Manuel Klimeka080a182013-01-02 16:30:12 +00001506 FormatTok.NewlinesBefore += Text.count('\n');
Daniel Jaspercd162382013-01-07 13:26:07 +00001507 FormatTok.HasUnescapedNewline = Text.count("\\\n") !=
1508 FormatTok.NewlinesBefore;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001509 FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
1510
1511 if (FormatTok.Tok.is(tok::eof))
1512 return FormatTok;
1513 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimek95419382013-01-07 07:56:50 +00001514 Text = rawTokenText(FormatTok.Tok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001515 }
Manuel Klimek95419382013-01-07 07:56:50 +00001516
1517 // Now FormatTok is the next non-whitespace token.
1518 FormatTok.TokenLength = Text.size();
1519
Manuel Klimekd4397b92013-01-04 23:34:14 +00001520 // In case the token starts with escaped newlines, we want to
1521 // take them into account as whitespace - this pattern is quite frequent
1522 // in macro definitions.
1523 // FIXME: What do we want to do with other escaped spaces, and escaped
1524 // spaces or newlines in the middle of tokens?
1525 // FIXME: Add a more explicit test.
1526 unsigned i = 0;
Daniel Jasper71607512013-01-07 10:48:50 +00001527 while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
Manuel Klimekd4397b92013-01-04 23:34:14 +00001528 FormatTok.WhiteSpaceLength += 2;
Manuel Klimek95419382013-01-07 07:56:50 +00001529 FormatTok.TokenLength -= 2;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001530 i += 2;
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001531 }
1532
1533 if (FormatTok.Tok.is(tok::raw_identifier)) {
Manuel Klimekd4397b92013-01-04 23:34:14 +00001534 IdentifierInfo &Info = IdentTable.get(Text);
Daniel Jaspercd1a32b2012-12-21 17:58:39 +00001535 FormatTok.Tok.setIdentifierInfo(&Info);
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001536 FormatTok.Tok.setKind(Info.getTokenID());
1537 }
1538
1539 if (FormatTok.Tok.is(tok::greatergreater)) {
1540 FormatTok.Tok.setKind(tok::greater);
1541 GreaterStashed = true;
1542 }
1543
1544 return FormatTok;
1545 }
1546
1547private:
1548 FormatToken FormatTok;
1549 bool GreaterStashed;
1550 Lexer &Lex;
1551 SourceManager &SourceMgr;
1552 IdentifierTable IdentTable;
1553
1554 /// Returns the text of \c FormatTok.
Manuel Klimek95419382013-01-07 07:56:50 +00001555 StringRef rawTokenText(Token &Tok) {
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001556 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
1557 Tok.getLength());
1558 }
1559};
1560
Daniel Jasperbac016b2012-12-03 18:12:45 +00001561class Formatter : public UnwrappedLineConsumer {
1562public:
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001563 Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex,
1564 SourceManager &SourceMgr,
Daniel Jasperbac016b2012-12-03 18:12:45 +00001565 const std::vector<CharSourceRange> &Ranges)
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001566 : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001567 Ranges(Ranges) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +00001568
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001569 virtual ~Formatter() {}
Daniel Jasperaccb0b02012-12-04 21:05:31 +00001570
Daniel Jasperbac016b2012-12-03 18:12:45 +00001571 tooling::Replacements format() {
Alexander Kornienko469a21b2012-12-07 16:15:44 +00001572 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001573 UnwrappedLineParser Parser(Diag, Style, Tokens, *this);
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001574 StructuralError = Parser.parse();
Manuel Klimekd4397b92013-01-04 23:34:14 +00001575 unsigned PreviousEndOfLineColumn = 0;
Daniel Jasper995e8202013-01-14 13:08:07 +00001576 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1577 TokenAnnotator Annotator(Style, SourceMgr, Lex, AnnotatedLines[i]);
1578 Annotator.annotate();
1579 }
1580 for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
1581 E = AnnotatedLines.end();
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001582 I != E; ++I) {
Daniel Jasper995e8202013-01-14 13:08:07 +00001583 const AnnotatedLine &TheLine = *I;
1584 if (touchesRanges(TheLine) && TheLine.Type != LT_Invalid) {
1585 unsigned Indent = formatFirstToken(TheLine.First, TheLine.Level,
1586 TheLine.InPPDirective,
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001587 PreviousEndOfLineColumn);
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001588 tryFitMultipleLinesInOne(Indent, I, E);
Daniel Jasper995e8202013-01-14 13:08:07 +00001589 UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001590 TheLine.First, Replaces,
Daniel Jasper995e8202013-01-14 13:08:07 +00001591 StructuralError);
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001592 PreviousEndOfLineColumn = Formatter.format();
1593 } else {
1594 // If we did not reformat this unwrapped line, the column at the end of
1595 // the last token is unchanged - thus, we can calculate the end of the
1596 // last token, and return the result.
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001597 PreviousEndOfLineColumn =
Daniel Jasper995e8202013-01-14 13:08:07 +00001598 SourceMgr.getSpellingColumnNumber(
1599 TheLine.Last->FormatTok.Tok.getLocation()) +
1600 Lex.MeasureTokenLength(TheLine.Last->FormatTok.Tok.getLocation(),
1601 SourceMgr, Lex.getLangOpts()) -
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001602 1;
1603 }
1604 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001605 return Replaces;
1606 }
1607
1608private:
Manuel Klimek517e8942013-01-11 17:54:10 +00001609 /// \brief Tries to merge lines into one.
1610 ///
1611 /// This will change \c Line and \c AnnotatedLine to contain the merged line,
1612 /// if possible; note that \c I will be incremented when lines are merged.
1613 ///
1614 /// Returns whether the resulting \c Line can fit in a single line.
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001615 void tryFitMultipleLinesInOne(unsigned Indent,
Daniel Jasper995e8202013-01-14 13:08:07 +00001616 std::vector<AnnotatedLine>::iterator &I,
1617 std::vector<AnnotatedLine>::iterator E) {
Manuel Klimek517e8942013-01-11 17:54:10 +00001618 unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent;
1619
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001620 // We can never merge stuff if there are trailing line comments.
1621 if (I->Last->Type == TT_LineComment)
1622 return;
1623
Manuel Klimek517e8942013-01-11 17:54:10 +00001624 // Check whether the UnwrappedLine can be put onto a single line. If
1625 // so, this is bound to be the optimal solution (by definition) and we
1626 // don't need to analyze the entire solution space.
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001627 if (I->Last->TotalLength >= Limit)
1628 return;
1629 Limit -= I->Last->TotalLength + 1; // One space.
Daniel Jasper55b08e72013-01-16 07:02:34 +00001630
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001631 if (I + 1 == E)
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001632 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001633
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001634 if (I->Last->is(tok::l_brace)) {
1635 tryMergeSimpleBlock(I, E, Limit);
1636 } else if (I->First.is(tok::kw_if)) {
1637 tryMergeSimpleIf(I, E, Limit);
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001638 } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline ||
1639 I->First.FormatTok.IsFirst)) {
1640 tryMergeSimplePPDirective(I, E, Limit);
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001641 }
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001642 return;
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001643 }
1644
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001645 void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
1646 std::vector<AnnotatedLine>::iterator E,
1647 unsigned Limit) {
1648 AnnotatedLine &Line = *I;
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001649 if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline)
1650 return;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001651 if (I + 2 != E && (I + 2)->InPPDirective &&
1652 !(I + 2)->First.FormatTok.HasUnescapedNewline)
1653 return;
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001654 if ((I + 1)->Last->TotalLength > Limit)
1655 return;
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001656 join(Line, *(++I));
1657 }
1658
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001659 void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I,
1660 std::vector<AnnotatedLine>::iterator E,
1661 unsigned Limit) {
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +00001662 if (!Style.AllowShortIfStatementsOnASingleLine)
1663 return;
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001664 AnnotatedLine &Line = *I;
Daniel Jasper55b08e72013-01-16 07:02:34 +00001665 if (Line.Last->isNot(tok::r_paren))
1666 return;
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001667 if ((I + 1)->Last->TotalLength > Limit)
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001668 return;
1669 if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment)
1670 return;
1671 // Only inline simple if's (no nested if or else).
1672 if (I + 2 != E && (I + 2)->First.is(tok::kw_else))
1673 return;
1674 join(Line, *(++I));
1675 }
1676
1677 void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
1678 std::vector<AnnotatedLine>::iterator E,
1679 unsigned Limit){
Daniel Jasper995e8202013-01-14 13:08:07 +00001680 // Check that we still have three lines and they fit into the limit.
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001681 if (I + 2 == E || !nextTwoLinesFitInto(I, Limit))
1682 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001683
1684 // First, check that the current line allows merging. This is the case if
1685 // we're not in a control flow statement and the last token is an opening
1686 // brace.
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001687 AnnotatedLine &Line = *I;
Manuel Klimek517e8942013-01-11 17:54:10 +00001688 bool AllowedTokens =
Daniel Jasper995e8202013-01-14 13:08:07 +00001689 Line.First.isNot(tok::kw_if) && Line.First.isNot(tok::kw_while) &&
1690 Line.First.isNot(tok::kw_do) && Line.First.isNot(tok::r_brace) &&
1691 Line.First.isNot(tok::kw_else) && Line.First.isNot(tok::kw_try) &&
1692 Line.First.isNot(tok::kw_catch) && Line.First.isNot(tok::kw_for) &&
Nico Weber67015ed2013-01-11 21:14:08 +00001693 // This gets rid of all ObjC @ keywords and methods.
Daniel Jasper995e8202013-01-14 13:08:07 +00001694 Line.First.isNot(tok::at) && Line.First.isNot(tok::minus) &&
1695 Line.First.isNot(tok::plus);
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001696 if (!AllowedTokens)
1697 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001698
1699 // Second, check that the next line does not contain any braces - if it
1700 // does, readability declines when putting it into a single line.
Daniel Jasper995e8202013-01-14 13:08:07 +00001701 const AnnotatedToken *Tok = &(I + 1)->First;
1702 if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001703 return;
Daniel Jasper995e8202013-01-14 13:08:07 +00001704 do {
1705 if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace))
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001706 return;
Daniel Jasper995e8202013-01-14 13:08:07 +00001707 Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
1708 } while (Tok != NULL);
Manuel Klimek517e8942013-01-11 17:54:10 +00001709
1710 // Last, check that the third line contains a single closing brace.
Daniel Jasper995e8202013-01-14 13:08:07 +00001711 Tok = &(I + 2)->First;
1712 if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
1713 Tok->MustBreakBefore)
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001714 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001715
Daniel Jasper995e8202013-01-14 13:08:07 +00001716 // If the merged line fits, we use that instead and skip the next two lines.
1717 Line.Last->Children.push_back((I + 1)->First);
1718 while (!Line.Last->Children.empty()) {
1719 Line.Last->Children[0].Parent = Line.Last;
1720 Line.Last = &Line.Last->Children[0];
Manuel Klimek517e8942013-01-11 17:54:10 +00001721 }
Daniel Jasper995e8202013-01-14 13:08:07 +00001722
1723 join(Line, *(I + 1));
1724 join(Line, *(I + 2));
1725 I += 2;
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001726 }
1727
1728 bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
1729 unsigned Limit) {
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001730 return (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <= Limit;
Manuel Klimek517e8942013-01-11 17:54:10 +00001731 }
1732
Daniel Jasper995e8202013-01-14 13:08:07 +00001733 void join(AnnotatedLine &A, const AnnotatedLine &B) {
1734 A.Last->Children.push_back(B.First);
1735 while (!A.Last->Children.empty()) {
1736 A.Last->Children[0].Parent = A.Last;
1737 A.Last = &A.Last->Children[0];
1738 }
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001739 }
1740
Daniel Jasper995e8202013-01-14 13:08:07 +00001741 bool touchesRanges(const AnnotatedLine &TheLine) {
1742 const FormatToken *First = &TheLine.First.FormatTok;
1743 const FormatToken *Last = &TheLine.Last->FormatTok;
Daniel Jaspercd162382013-01-07 13:26:07 +00001744 CharSourceRange LineRange = CharSourceRange::getTokenRange(
Daniel Jasper26f7e782013-01-08 14:56:18 +00001745 First->Tok.getLocation(),
1746 Last->Tok.getLocation());
Daniel Jasperbac016b2012-12-03 18:12:45 +00001747 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001748 if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
1749 Ranges[i].getBegin()) &&
1750 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1751 LineRange.getBegin()))
1752 return true;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001753 }
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001754 return false;
1755 }
1756
1757 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001758 AnnotatedLines.push_back(AnnotatedLine(TheLine));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001759 }
1760
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001761 /// \brief Add a new line and the required indent before the first Token
1762 /// of the \c UnwrappedLine if there was no structural parsing error.
1763 /// Returns the indent level of the \c UnwrappedLine.
1764 unsigned formatFirstToken(const AnnotatedToken &RootToken, unsigned Level,
1765 bool InPPDirective,
1766 unsigned PreviousEndOfLineColumn) {
Daniel Jasper7d19bc22013-01-11 14:23:32 +00001767 const FormatToken &Tok = RootToken.FormatTok;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001768 if (!Tok.WhiteSpaceStart.isValid() || StructuralError)
1769 return SourceMgr.getSpellingColumnNumber(Tok.Tok.getLocation()) - 1;
1770
1771 unsigned Newlines = std::min(Tok.NewlinesBefore,
1772 Style.MaxEmptyLinesToKeep + 1);
1773 if (Newlines == 0 && !Tok.IsFirst)
1774 Newlines = 1;
1775 unsigned Indent = Level * 2;
1776
1777 bool IsAccessModifier = false;
1778 if (RootToken.is(tok::kw_public) || RootToken.is(tok::kw_protected) ||
1779 RootToken.is(tok::kw_private))
1780 IsAccessModifier = true;
1781 else if (RootToken.is(tok::at) && !RootToken.Children.empty() &&
1782 (RootToken.Children[0].isObjCAtKeyword(tok::objc_public) ||
1783 RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) ||
1784 RootToken.Children[0].isObjCAtKeyword(tok::objc_package) ||
1785 RootToken.Children[0].isObjCAtKeyword(tok::objc_private)))
1786 IsAccessModifier = true;
1787
1788 if (IsAccessModifier &&
1789 static_cast<int>(Indent) + Style.AccessModifierOffset >= 0)
1790 Indent += Style.AccessModifierOffset;
1791 if (!InPPDirective || Tok.HasUnescapedNewline) {
1792 replaceWhitespace(Tok, Newlines, Indent, Style, SourceMgr, Replaces);
1793 } else {
1794 replacePPWhitespace(Tok, Newlines, Indent, PreviousEndOfLineColumn, Style,
1795 SourceMgr, Replaces);
1796 }
1797 return Indent;
1798 }
1799
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001800 DiagnosticsEngine &Diag;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001801 FormatStyle Style;
1802 Lexer &Lex;
1803 SourceManager &SourceMgr;
1804 tooling::Replacements Replaces;
1805 std::vector<CharSourceRange> Ranges;
Daniel Jasper995e8202013-01-14 13:08:07 +00001806 std::vector<AnnotatedLine> AnnotatedLines;
Alexander Kornienkocff563c2012-12-04 17:27:50 +00001807 bool StructuralError;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001808};
1809
1810tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
1811 SourceManager &SourceMgr,
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001812 std::vector<CharSourceRange> Ranges,
1813 DiagnosticConsumer *DiagClient) {
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001814 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001815 OwningPtr<DiagnosticConsumer> DiagPrinter;
1816 if (DiagClient == 0) {
1817 DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts));
1818 DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
1819 DiagClient = DiagPrinter.get();
1820 }
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001821 DiagnosticsEngine Diagnostics(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001822 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
Alexander Kornienkoa4ae9f32013-01-14 11:34:14 +00001823 DiagClient, false);
Alexander Kornienko3048aea2013-01-10 15:05:09 +00001824 Diagnostics.setSourceManager(&SourceMgr);
1825 Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001826 return formatter.format();
1827}
1828
Daniel Jasper46ef8522013-01-10 13:08:12 +00001829LangOptions getFormattingLangOpts() {
1830 LangOptions LangOpts;
1831 LangOpts.CPlusPlus = 1;
1832 LangOpts.CPlusPlus11 = 1;
1833 LangOpts.Bool = 1;
1834 LangOpts.ObjC1 = 1;
1835 LangOpts.ObjC2 = 1;
1836 return LangOpts;
1837}
1838
Daniel Jaspercd162382013-01-07 13:26:07 +00001839} // namespace format
1840} // namespace clang