blob: 1f36f9ab64c0212301da9c10bbeb3b98da30eddc [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- Format.cpp - Format C++ code -------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file implements functions declared in Format.h. This will be
12/// split into separate files as we go.
13///
Daniel Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Manuel Klimek24998102013-01-16 14:55:28 +000016#define DEBUG_TYPE "format-formatter"
17
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000018#include "BreakableToken.h"
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000019#include "TokenAnnotator.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "UnwrappedLineParser.h"
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000021#include "WhitespaceManager.h"
Daniel Jasperec04c0d2013-05-16 10:40:07 +000022#include "clang/Basic/Diagnostic.h"
Daniel Jasperab7654e2012-12-21 10:20:02 +000023#include "clang/Basic/OperatorPrecedence.h"
Chandler Carruth44eb4f62013-01-02 10:28:36 +000024#include "clang/Basic/SourceManager.h"
Manuel Klimek24998102013-01-16 14:55:28 +000025#include "clang/Format/Format.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000026#include "clang/Lex/Lexer.h"
Alexander Kornienkoffd6d042013-03-27 11:52:18 +000027#include "llvm/ADT/STLExtras.h"
Manuel Klimek2ef908e2013-02-13 10:46:36 +000028#include "llvm/Support/Allocator.h"
Manuel Klimek24998102013-01-16 14:55:28 +000029#include "llvm/Support/Debug.h"
Alexander Kornienkod6538332013-05-07 15:32:14 +000030#include "llvm/Support/YAMLTraits.h"
Manuel Klimek2ef908e2013-02-13 10:46:36 +000031#include <queue>
Daniel Jasper8b529712012-12-04 13:02:32 +000032#include <string>
33
Alexander Kornienkod6538332013-05-07 15:32:14 +000034namespace llvm {
35namespace yaml {
36template <>
37struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> {
Manuel Klimeka8eb9142013-05-13 12:51:40 +000038 static void enumeration(IO &IO,
39 clang::format::FormatStyle::LanguageStandard &Value) {
40 IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03);
41 IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11);
42 IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto);
43 }
44};
45
Daniel Jasper12f9d8e2013-05-14 09:30:02 +000046template <>
Manuel Klimeka8eb9142013-05-13 12:51:40 +000047struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> {
48 static void
49 enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) {
50 IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach);
51 IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux);
52 IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup);
Alexander Kornienkod6538332013-05-07 15:32:14 +000053 }
54};
55
56template <> struct MappingTraits<clang::format::FormatStyle> {
57 static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) {
Alexander Kornienko49149672013-05-10 11:56:10 +000058 if (IO.outputting()) {
59 StringRef StylesArray[] = { "LLVM", "Google", "Chromium", "Mozilla" };
60 ArrayRef<StringRef> Styles(StylesArray);
61 for (size_t i = 0, e = Styles.size(); i < e; ++i) {
62 StringRef StyleName(Styles[i]);
Alexander Kornienko006b5c82013-05-19 00:53:30 +000063 clang::format::FormatStyle PredefinedStyle;
64 if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) &&
65 Style == PredefinedStyle) {
Alexander Kornienko49149672013-05-10 11:56:10 +000066 IO.mapOptional("# BasedOnStyle", StyleName);
67 break;
68 }
69 }
70 } else {
Alexander Kornienkod6538332013-05-07 15:32:14 +000071 StringRef BasedOnStyle;
72 IO.mapOptional("BasedOnStyle", BasedOnStyle);
Alexander Kornienkod6538332013-05-07 15:32:14 +000073 if (!BasedOnStyle.empty())
Alexander Kornienko006b5c82013-05-19 00:53:30 +000074 if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) {
75 IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
76 return;
77 }
Alexander Kornienkod6538332013-05-07 15:32:14 +000078 }
79
80 IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
81 IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
82 IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
83 Style.AllowAllParametersOfDeclarationOnNextLine);
84 IO.mapOptional("AllowShortIfStatementsOnASingleLine",
85 Style.AllowShortIfStatementsOnASingleLine);
Daniel Jasper3a685df2013-05-16 12:12:21 +000086 IO.mapOptional("AllowShortLoopsOnASingleLine",
87 Style.AllowShortLoopsOnASingleLine);
Alexander Kornienkod6538332013-05-07 15:32:14 +000088 IO.mapOptional("BinPackParameters", Style.BinPackParameters);
89 IO.mapOptional("ColumnLimit", Style.ColumnLimit);
90 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
91 Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
92 IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding);
93 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
94 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
95 IO.mapOptional("ObjCSpaceBeforeProtocolList",
96 Style.ObjCSpaceBeforeProtocolList);
97 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
98 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
99 Style.PenaltyReturnTypeOnItsOwnLine);
100 IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
101 IO.mapOptional("SpacesBeforeTrailingComments",
102 Style.SpacesBeforeTrailingComments);
103 IO.mapOptional("Standard", Style.Standard);
Manuel Klimek13b97d82013-05-13 08:42:42 +0000104 IO.mapOptional("IndentWidth", Style.IndentWidth);
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000105 IO.mapOptional("UseTab", Style.UseTab);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000106 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000107 }
108};
109}
110}
111
Daniel Jasperf7935112012-12-03 18:12:45 +0000112namespace clang {
113namespace format {
114
Daniel Jasperf7935112012-12-03 18:12:45 +0000115FormatStyle getLLVMStyle() {
116 FormatStyle LLVMStyle;
Daniel Jasperf7935112012-12-03 18:12:45 +0000117 LLVMStyle.AccessModifierOffset = -2;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000118 LLVMStyle.AlignEscapedNewlinesLeft = false;
Daniel Jasperf7db4332013-01-29 16:03:49 +0000119 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000120 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000121 LLVMStyle.AllowShortLoopsOnASingleLine = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000122 LLVMStyle.BinPackParameters = true;
123 LLVMStyle.ColumnLimit = 80;
124 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
125 LLVMStyle.DerivePointerBinding = false;
126 LLVMStyle.IndentCaseLabels = false;
127 LLVMStyle.MaxEmptyLinesToKeep = 1;
Nico Webera6087752013-01-10 20:12:55 +0000128 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000129 LLVMStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasper6728fc12013-04-11 14:29:13 +0000130 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 75;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000131 LLVMStyle.PointerBindsToType = false;
132 LLVMStyle.SpacesBeforeTrailingComments = 1;
133 LLVMStyle.Standard = FormatStyle::LS_Cpp03;
Manuel Klimek13b97d82013-05-13 08:42:42 +0000134 LLVMStyle.IndentWidth = 2;
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000135 LLVMStyle.UseTab = false;
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000136 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
Daniel Jasperf7935112012-12-03 18:12:45 +0000137 return LLVMStyle;
138}
139
140FormatStyle getGoogleStyle() {
141 FormatStyle GoogleStyle;
Daniel Jasperf7935112012-12-03 18:12:45 +0000142 GoogleStyle.AccessModifierOffset = -1;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000143 GoogleStyle.AlignEscapedNewlinesLeft = true;
Daniel Jasperf7db4332013-01-29 16:03:49 +0000144 GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper085a2ed2013-04-24 13:46:00 +0000145 GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000146 GoogleStyle.AllowShortLoopsOnASingleLine= true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000147 GoogleStyle.BinPackParameters = true;
148 GoogleStyle.ColumnLimit = 80;
149 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
150 GoogleStyle.DerivePointerBinding = true;
151 GoogleStyle.IndentCaseLabels = true;
152 GoogleStyle.MaxEmptyLinesToKeep = 1;
Nico Webera6087752013-01-10 20:12:55 +0000153 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000154 GoogleStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasper6728fc12013-04-11 14:29:13 +0000155 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000156 GoogleStyle.PointerBindsToType = true;
157 GoogleStyle.SpacesBeforeTrailingComments = 2;
158 GoogleStyle.Standard = FormatStyle::LS_Auto;
Manuel Klimek13b97d82013-05-13 08:42:42 +0000159 GoogleStyle.IndentWidth = 2;
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000160 GoogleStyle.UseTab = false;
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000161 GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
Daniel Jasperf7935112012-12-03 18:12:45 +0000162 return GoogleStyle;
163}
164
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000165FormatStyle getChromiumStyle() {
166 FormatStyle ChromiumStyle = getGoogleStyle();
Daniel Jasperf7db4332013-01-29 16:03:49 +0000167 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Daniel Jasper085a2ed2013-04-24 13:46:00 +0000168 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000169 ChromiumStyle.AllowShortLoopsOnASingleLine = false;
Daniel Jasper2cf17bf2013-02-27 09:47:53 +0000170 ChromiumStyle.BinPackParameters = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000171 ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
172 ChromiumStyle.DerivePointerBinding = false;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000173 return ChromiumStyle;
174}
175
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000176FormatStyle getMozillaStyle() {
177 FormatStyle MozillaStyle = getLLVMStyle();
178 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
179 MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
180 MozillaStyle.DerivePointerBinding = true;
181 MozillaStyle.IndentCaseLabels = true;
182 MozillaStyle.ObjCSpaceBeforeProtocolList = false;
183 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
184 MozillaStyle.PointerBindsToType = true;
185 return MozillaStyle;
186}
187
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000188bool getPredefinedStyle(StringRef Name, FormatStyle *Style) {
Alexander Kornienkod6538332013-05-07 15:32:14 +0000189 if (Name.equals_lower("llvm"))
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000190 *Style = getLLVMStyle();
191 else if (Name.equals_lower("chromium"))
192 *Style = getChromiumStyle();
193 else if (Name.equals_lower("mozilla"))
194 *Style = getMozillaStyle();
195 else if (Name.equals_lower("google"))
196 *Style = getGoogleStyle();
197 else
198 return false;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000199
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000200 return true;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000201}
202
203llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
Alexander Kornienko06e00332013-05-20 15:18:01 +0000204 if (Text.trim().empty())
205 return llvm::make_error_code(llvm::errc::invalid_argument);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000206 llvm::yaml::Input Input(Text);
207 Input >> *Style;
208 return Input.error();
209}
210
211std::string configurationAsText(const FormatStyle &Style) {
212 std::string Text;
213 llvm::raw_string_ostream Stream(Text);
214 llvm::yaml::Output Output(Stream);
215 // We use the same mapping method for input and output, so we need a non-const
216 // reference here.
217 FormatStyle NonConstStyle = Style;
218 Output << NonConstStyle;
Alexander Kornienko9a38ec22013-05-13 12:56:35 +0000219 return Stream.str();
Alexander Kornienkod6538332013-05-07 15:32:14 +0000220}
221
Daniel Jasperacc33662013-02-08 08:22:00 +0000222// Returns the length of everything up to the first possible line break after
223// the ), ], } or > matching \c Tok.
224static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) {
225 if (Tok.MatchingParen == NULL)
226 return 0;
227 AnnotatedToken *End = Tok.MatchingParen;
228 while (!End->Children.empty() && !End->Children[0].CanBreakBefore) {
229 End = &End->Children[0];
230 }
231 return End->TotalLength - Tok.TotalLength + 1;
232}
233
Daniel Jasperf7935112012-12-03 18:12:45 +0000234class UnwrappedLineFormatter {
235public:
Manuel Klimekb2c6dbe2013-01-10 19:17:33 +0000236 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000237 const AnnotatedLine &Line, unsigned FirstIndent,
Daniel Jaspera67a8f02013-01-16 10:41:46 +0000238 const AnnotatedToken &RootToken,
Manuel Klimek1a18c402013-04-12 14:13:36 +0000239 WhitespaceManager &Whitespaces)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000240 : Style(Style), SourceMgr(SourceMgr), Line(Line),
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000241 FirstIndent(FirstIndent), RootToken(RootToken),
Daniel Jasper12ef4e52013-02-21 21:33:55 +0000242 Whitespaces(Whitespaces), Count(0) {}
Daniel Jasperf7935112012-12-03 18:12:45 +0000243
Manuel Klimek1abf7892013-01-04 23:34:14 +0000244 /// \brief Formats an \c UnwrappedLine.
245 ///
246 /// \returns The column after the last token in the last line of the
247 /// \c UnwrappedLine.
Daniel Jasperc22f5b42013-02-28 11:05:57 +0000248 unsigned format(const AnnotatedLine *NextLine) {
Daniel Jaspere9de2602012-12-06 09:56:08 +0000249 // Initialize state dependent on indent.
Daniel Jasper337816e2013-01-11 10:22:12 +0000250 LineState State;
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000251 State.Column = FirstIndent;
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000252 State.NextToken = &RootToken;
Daniel Jasper97b89482013-03-13 07:49:51 +0000253 State.Stack.push_back(
Daniel Jasper53e8d852013-05-22 08:55:55 +0000254 ParenState(FirstIndent, FirstIndent, /*AvoidBinPacking=*/ false,
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000255 /*NoLineBreak=*/ false));
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000256 State.LineContainsContinuedForLoopSection = false;
Daniel Jasper400adc62013-02-08 15:28:42 +0000257 State.ParenLevel = 0;
Manuel Klimek02f640a2013-02-20 15:25:48 +0000258 State.StartOfStringLiteral = 0;
Daniel Jasper40c36c52013-02-18 11:05:07 +0000259 State.StartOfLineLevel = State.ParenLevel;
Daniel Jasperf8114cf2013-05-22 05:27:42 +0000260 State.IgnoreStackForComparison = false;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000261
262 // The first token has already been indented and thus consumed.
Manuel Klimek1998ea22013-02-20 10:15:13 +0000263 moveStateToNextToken(State, /*DryRun=*/ false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000264
Daniel Jasper4b866272013-02-01 11:00:45 +0000265 // If everything fits on a single line, just put it there.
Daniel Jasperc22f5b42013-02-28 11:05:57 +0000266 unsigned ColumnLimit = Style.ColumnLimit;
267 if (NextLine && NextLine->InPPDirective &&
268 !NextLine->First.FormatTok.HasUnescapedNewline)
269 ColumnLimit = getColumnLimit();
270 if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) {
Daniel Jasper4b866272013-02-01 11:00:45 +0000271 while (State.NextToken != NULL) {
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000272 addTokenToState(false, false, State);
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000273 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000274 return State.Column;
Daniel Jasperf7935112012-12-03 18:12:45 +0000275 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000276
Daniel Jasperacc33662013-02-08 08:22:00 +0000277 // If the ObjC method declaration does not fit on a line, we should format
278 // it with one arg per line.
279 if (Line.Type == LT_ObjCMethodDecl)
280 State.Stack.back().BreakBeforeParameter = true;
281
Daniel Jasper4b866272013-02-01 11:00:45 +0000282 // Find best solution in solution space.
283 return analyzeSolutionSpace(State);
Daniel Jasperf7935112012-12-03 18:12:45 +0000284 }
285
286private:
Manuel Klimek24998102013-01-16 14:55:28 +0000287 void DebugTokenState(const AnnotatedToken &AnnotatedTok) {
288 const Token &Tok = AnnotatedTok.FormatTok.Tok;
Alexander Kornienko49149672013-05-10 11:56:10 +0000289 llvm::dbgs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
Daniel Jasperbbc84152013-01-29 11:27:30 +0000290 Tok.getLength());
Alexander Kornienko49149672013-05-10 11:56:10 +0000291 llvm::dbgs();
Manuel Klimek24998102013-01-16 14:55:28 +0000292 }
293
Daniel Jasper337816e2013-01-11 10:22:12 +0000294 struct ParenState {
Daniel Jasperb9ebd5d2013-02-05 09:41:21 +0000295 ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking,
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000296 bool NoLineBreak)
Daniel Jasper400adc62013-02-08 15:28:42 +0000297 : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
298 BreakBeforeClosingBrace(false), QuestionColumn(0),
Daniel Jasperacc33662013-02-08 08:22:00 +0000299 AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000300 NoLineBreak(NoLineBreak), ColonPos(0), StartOfFunctionCall(0),
301 NestedNameSpecifierContinuation(0), CallContinuation(0),
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000302 VariablePos(0), ForFakeParenthesis(false) {}
Daniel Jasper6d822722012-12-24 16:43:00 +0000303
Daniel Jasperf7935112012-12-03 18:12:45 +0000304 /// \brief The position to which a specific parenthesis level needs to be
305 /// indented.
Daniel Jasper337816e2013-01-11 10:22:12 +0000306 unsigned Indent;
Daniel Jasperf7935112012-12-03 18:12:45 +0000307
Daniel Jaspere9de2602012-12-06 09:56:08 +0000308 /// \brief The position of the last space on each level.
309 ///
310 /// Used e.g. to break like:
311 /// functionCall(Parameter, otherCall(
312 /// OtherParameter));
Daniel Jasper337816e2013-01-11 10:22:12 +0000313 unsigned LastSpace;
Daniel Jasperf7935112012-12-03 18:12:45 +0000314
Daniel Jaspere9de2602012-12-06 09:56:08 +0000315 /// \brief The position the first "<<" operator encountered on each level.
316 ///
317 /// Used to align "<<" operators. 0 if no such operator has been encountered
318 /// on a level.
Daniel Jasper337816e2013-01-11 10:22:12 +0000319 unsigned FirstLessLess;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000320
Manuel Klimek0ddd57a2013-01-10 15:58:26 +0000321 /// \brief Whether a newline needs to be inserted before the block's closing
322 /// brace.
323 ///
324 /// We only want to insert a newline before the closing brace if there also
325 /// was a newline after the beginning left brace.
Daniel Jasper337816e2013-01-11 10:22:12 +0000326 bool BreakBeforeClosingBrace;
327
Daniel Jasperca6623b2013-01-28 12:45:14 +0000328 /// \brief The column of a \c ? in a conditional expression;
329 unsigned QuestionColumn;
330
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000331 /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple
332 /// lines, in this context.
333 bool AvoidBinPacking;
334
335 /// \brief Break after the next comma (or all the commas in this context if
336 /// \c AvoidBinPacking is \c true).
Daniel Jasperacc33662013-02-08 08:22:00 +0000337 bool BreakBeforeParameter;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000338
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000339 /// \brief Line breaking in this context would break a formatting rule.
340 bool NoLineBreak;
Daniel Jasper2408a8c2013-01-11 11:37:55 +0000341
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000342 /// \brief The position of the colon in an ObjC method declaration/call.
343 unsigned ColonPos;
Daniel Jasperdc7d5812013-02-20 12:56:39 +0000344
Daniel Jasperf9a84b52013-03-01 16:48:32 +0000345 /// \brief The start of the most recent function in a builder-type call.
346 unsigned StartOfFunctionCall;
347
Daniel Jasperc238c872013-04-02 14:33:13 +0000348 /// \brief If a nested name specifier was broken over multiple lines, this
349 /// contains the start column of the second line. Otherwise 0.
350 unsigned NestedNameSpecifierContinuation;
351
352 /// \brief If a call expression was broken over multiple lines, this
353 /// contains the start column of the second line. Otherwise 0.
354 unsigned CallContinuation;
355
Daniel Jaspera628c982013-04-03 13:36:17 +0000356 /// \brief The column of the first variable name in a variable declaration.
357 ///
358 /// Used to align further variables if necessary.
359 unsigned VariablePos;
360
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000361 /// \brief \c true if this \c ParenState was created for a fake parenthesis.
362 ///
363 /// Does not need to be considered for memoization / the comparison function
364 /// as otherwise identical states will have the same fake/non-fake
365 /// \c ParenStates.
366 bool ForFakeParenthesis;
367
Daniel Jasper337816e2013-01-11 10:22:12 +0000368 bool operator<(const ParenState &Other) const {
369 if (Indent != Other.Indent)
Daniel Jasperfd8c4b12013-01-11 14:23:32 +0000370 return Indent < Other.Indent;
Daniel Jasper337816e2013-01-11 10:22:12 +0000371 if (LastSpace != Other.LastSpace)
372 return LastSpace < Other.LastSpace;
373 if (FirstLessLess != Other.FirstLessLess)
374 return FirstLessLess < Other.FirstLessLess;
Daniel Jasper2408a8c2013-01-11 11:37:55 +0000375 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
376 return BreakBeforeClosingBrace;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000377 if (QuestionColumn != Other.QuestionColumn)
378 return QuestionColumn < Other.QuestionColumn;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000379 if (AvoidBinPacking != Other.AvoidBinPacking)
380 return AvoidBinPacking;
Daniel Jasperacc33662013-02-08 08:22:00 +0000381 if (BreakBeforeParameter != Other.BreakBeforeParameter)
382 return BreakBeforeParameter;
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000383 if (NoLineBreak != Other.NoLineBreak)
384 return NoLineBreak;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000385 if (ColonPos != Other.ColonPos)
386 return ColonPos < Other.ColonPos;
Daniel Jasperf9a84b52013-03-01 16:48:32 +0000387 if (StartOfFunctionCall != Other.StartOfFunctionCall)
388 return StartOfFunctionCall < Other.StartOfFunctionCall;
Daniel Jasperc238c872013-04-02 14:33:13 +0000389 if (CallContinuation != Other.CallContinuation)
390 return CallContinuation < Other.CallContinuation;
Daniel Jaspera628c982013-04-03 13:36:17 +0000391 if (VariablePos != Other.VariablePos)
392 return VariablePos < Other.VariablePos;
Daniel Jasper7b7877a2013-01-12 07:36:22 +0000393 return false;
Daniel Jasper337816e2013-01-11 10:22:12 +0000394 }
395 };
396
397 /// \brief The current state when indenting a unwrapped line.
398 ///
399 /// As the indenting tries different combinations this is copied by value.
400 struct LineState {
401 /// \brief The number of used columns in the current line.
402 unsigned Column;
403
404 /// \brief The token that needs to be next formatted.
405 const AnnotatedToken *NextToken;
406
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000407 /// \brief \c true if this line contains a continued for-loop section.
408 bool LineContainsContinuedForLoopSection;
409
Daniel Jasper400adc62013-02-08 15:28:42 +0000410 /// \brief The level of nesting inside (), [], <> and {}.
411 unsigned ParenLevel;
412
Daniel Jasper40c36c52013-02-18 11:05:07 +0000413 /// \brief The \c ParenLevel at the start of this line.
414 unsigned StartOfLineLevel;
415
Manuel Klimek02f640a2013-02-20 15:25:48 +0000416 /// \brief The start column of the string literal, if we're in a string
417 /// literal sequence, 0 otherwise.
418 unsigned StartOfStringLiteral;
419
Daniel Jasper337816e2013-01-11 10:22:12 +0000420 /// \brief A stack keeping track of properties applying to parenthesis
421 /// levels.
422 std::vector<ParenState> Stack;
423
Daniel Jasperf8114cf2013-05-22 05:27:42 +0000424 /// \brief Ignore the stack of \c ParenStates for state comparison.
425 ///
426 /// In long and deeply nested unwrapped lines, the current algorithm can
427 /// be insufficient for finding the best formatting with a reasonable amount
428 /// of time and memory. Setting this flag will effectively lead to the
429 /// algorithm not analyzing some combinations. However, these combinations
430 /// rarely contain the optimal solution: In short, accepting a higher
431 /// penalty early would need to lead to different values in the \c
432 /// ParenState stack (in an otherwise identical state) and these different
433 /// values would need to lead to a significant amount of avoided penalty
434 /// later.
435 ///
436 /// FIXME: Come up with a better algorithm instead.
437 bool IgnoreStackForComparison;
438
Daniel Jasper337816e2013-01-11 10:22:12 +0000439 /// \brief Comparison operator to be able to used \c LineState in \c map.
440 bool operator<(const LineState &Other) const {
Daniel Jasper58f427e2013-02-19 09:28:55 +0000441 if (NextToken != Other.NextToken)
442 return NextToken < Other.NextToken;
443 if (Column != Other.Column)
444 return Column < Other.Column;
Daniel Jasper58f427e2013-02-19 09:28:55 +0000445 if (LineContainsContinuedForLoopSection !=
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000446 Other.LineContainsContinuedForLoopSection)
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000447 return LineContainsContinuedForLoopSection;
Daniel Jasper58f427e2013-02-19 09:28:55 +0000448 if (ParenLevel != Other.ParenLevel)
449 return ParenLevel < Other.ParenLevel;
450 if (StartOfLineLevel != Other.StartOfLineLevel)
451 return StartOfLineLevel < Other.StartOfLineLevel;
Manuel Klimek02f640a2013-02-20 15:25:48 +0000452 if (StartOfStringLiteral != Other.StartOfStringLiteral)
453 return StartOfStringLiteral < Other.StartOfStringLiteral;
Daniel Jasperf8114cf2013-05-22 05:27:42 +0000454 if (IgnoreStackForComparison || Other.IgnoreStackForComparison)
455 return false;
Daniel Jasper58f427e2013-02-19 09:28:55 +0000456 return Stack < Other.Stack;
Daniel Jasperf7935112012-12-03 18:12:45 +0000457 }
458 };
459
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000460 /// \brief Appends the next token to \p State and updates information
461 /// necessary for indentation.
462 ///
463 /// Puts the token on the current line if \p Newline is \c true and adds a
464 /// line break and necessary indentation otherwise.
465 ///
466 /// If \p DryRun is \c false, also creates and stores the required
467 /// \c Replacement.
Manuel Klimek1998ea22013-02-20 10:15:13 +0000468 unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) {
Daniel Jasper399d24b2013-01-09 07:06:56 +0000469 const AnnotatedToken &Current = *State.NextToken;
470 const AnnotatedToken &Previous = *State.NextToken->Parent;
Daniel Jasperf7935112012-12-03 18:12:45 +0000471
Daniel Jasper291f9362013-03-20 15:58:10 +0000472 if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
Daniel Jasper4b866272013-02-01 11:00:45 +0000473 State.Column += State.NextToken->FormatTok.WhiteSpaceLength +
474 State.NextToken->FormatTok.TokenLength;
475 if (State.NextToken->Children.empty())
476 State.NextToken = NULL;
477 else
478 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek1998ea22013-02-20 10:15:13 +0000479 return 0;
Daniel Jasper4b866272013-02-01 11:00:45 +0000480 }
481
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000482 // If we are continuing an expression, we want to indent an extra 4 spaces.
483 unsigned ContinuationIndent =
Daniel Jasperc238c872013-04-02 14:33:13 +0000484 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4;
Daniel Jasperf7935112012-12-03 18:12:45 +0000485 if (Newline) {
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000486 unsigned WhitespaceStartColumn = State.Column;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000487 if (Current.is(tok::r_brace)) {
Manuel Klimek13b97d82013-05-13 08:42:42 +0000488 State.Column = Line.Level * Style.IndentWidth;
Daniel Jasper399d24b2013-01-09 07:06:56 +0000489 } else if (Current.is(tok::string_literal) &&
Manuel Klimek02f640a2013-02-20 15:25:48 +0000490 State.StartOfStringLiteral != 0) {
491 State.Column = State.StartOfStringLiteral;
Daniel Jasper2ec3ffb82013-02-18 11:59:17 +0000492 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasper399d24b2013-01-09 07:06:56 +0000493 } else if (Current.is(tok::lessless) &&
Daniel Jasper400adc62013-02-08 15:28:42 +0000494 State.Stack.back().FirstLessLess != 0) {
495 State.Column = State.Stack.back().FirstLessLess;
Daniel Jasperc238c872013-04-02 14:33:13 +0000496 } else if (Current.isOneOf(tok::period, tok::arrow)) {
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000497 if (State.Stack.back().CallContinuation == 0) {
498 State.Column = ContinuationIndent;
Daniel Jasperc238c872013-04-02 14:33:13 +0000499 State.Stack.back().CallContinuation = State.Column;
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000500 } else {
501 State.Column = State.Stack.back().CallContinuation;
502 }
Daniel Jasperca6623b2013-01-28 12:45:14 +0000503 } else if (Current.Type == TT_ConditionalExpr) {
504 State.Column = State.Stack.back().QuestionColumn;
Daniel Jaspera628c982013-04-03 13:36:17 +0000505 } else if (Previous.is(tok::comma) &&
506 State.Stack.back().VariablePos != 0) {
507 State.Column = State.Stack.back().VariablePos;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000508 } else if (Previous.ClosesTemplateDeclaration ||
Daniel Jasper8e357692013-05-06 08:27:33 +0000509 (Current.Type == TT_StartOfName && State.ParenLevel == 0 &&
510 Line.StartsDefinition)) {
Daniel Jasperc238c872013-04-02 14:33:13 +0000511 State.Column = State.Stack.back().Indent;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000512 } else if (Current.Type == TT_ObjCSelectorName) {
513 if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) {
514 State.Column =
515 State.Stack.back().ColonPos - Current.FormatTok.TokenLength;
516 } else {
517 State.Column = State.Stack.back().Indent;
518 State.Stack.back().ColonPos =
519 State.Column + Current.FormatTok.TokenLength;
520 }
Daniel Jasper0f0234e2013-05-08 10:00:18 +0000521 } else if (Current.Type == TT_StartOfName ||
522 Previous.isOneOf(tok::coloncolon, tok::equal) ||
Daniel Jasperc238c872013-04-02 14:33:13 +0000523 Previous.Type == TT_ObjCMethodExpr) {
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000524 State.Column = ContinuationIndent;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000525 } else {
Daniel Jasper400adc62013-02-08 15:28:42 +0000526 State.Column = State.Stack.back().Indent;
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000527 // Ensure that we fall back to indenting 4 spaces instead of just
528 // flushing continuations left.
Daniel Jasperc238c872013-04-02 14:33:13 +0000529 if (State.Column == FirstIndent)
530 State.Column += 4;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000531 }
532
Daniel Jasper54a86022013-02-15 11:07:25 +0000533 if (Current.is(tok::question))
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000534 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasperd69fc772013-05-08 14:12:04 +0000535 if ((Previous.isOneOf(tok::comma, tok::semi) &&
536 !State.Stack.back().AvoidBinPacking) ||
537 Previous.Type == TT_BinaryOperator)
Daniel Jasperacc33662013-02-08 08:22:00 +0000538 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasperc6fbc212013-05-15 09:35:08 +0000539 if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
540 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000541
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000542 if (!DryRun) {
Daniel Jasperfb5e2412013-02-26 13:10:34 +0000543 unsigned NewLines = 1;
544 if (Current.Type == TT_LineComment)
545 NewLines =
546 std::max(NewLines, std::min(Current.FormatTok.NewlinesBefore,
547 Style.MaxEmptyLinesToKeep + 1));
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000548 if (!Line.InPPDirective)
Daniel Jasperdc7d5812013-02-20 12:56:39 +0000549 Whitespaces.replaceWhitespace(Current, NewLines, State.Column,
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000550 WhitespaceStartColumn);
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000551 else
Daniel Jasperdc7d5812013-02-20 12:56:39 +0000552 Whitespaces.replacePPWhitespace(Current, NewLines, State.Column,
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000553 WhitespaceStartColumn);
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000554 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000555
Daniel Jasper400adc62013-02-08 15:28:42 +0000556 State.Stack.back().LastSpace = State.Column;
Daniel Jasper66e4f832013-05-10 13:37:16 +0000557 if (Current.isOneOf(tok::arrow, tok::period))
558 State.Stack.back().LastSpace += Current.FormatTok.TokenLength;
Daniel Jasper40c36c52013-02-18 11:05:07 +0000559 State.StartOfLineLevel = State.ParenLevel;
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000560
561 // Any break on this level means that the parent level has been broken
562 // and we need to avoid bin packing there.
563 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
564 State.Stack[i].BreakBeforeParameter = true;
565 }
Daniel Jasper1b8e76f2013-04-15 22:36:37 +0000566 const AnnotatedToken *TokenBefore = Current.getPreviousNoneComment();
567 if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) &&
Daniel Jasperc6fbc212013-05-15 09:35:08 +0000568 TokenBefore->Type != TT_TemplateCloser &&
Daniel Jasperd69fc772013-05-08 14:12:04 +0000569 TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope())
Daniel Jasper2cf17bf2013-02-27 09:47:53 +0000570 State.Stack.back().BreakBeforeParameter = true;
571
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000572 // If we break after {, we should also break before the corresponding }.
573 if (Previous.is(tok::l_brace))
574 State.Stack.back().BreakBeforeClosingBrace = true;
575
576 if (State.Stack.back().AvoidBinPacking) {
577 // If we are breaking after '(', '{', '<', this is not bin packing
578 // unless AllowAllParametersOfDeclarationOnNextLine is false.
Daniel Jasper571f1af2013-05-14 20:39:56 +0000579 if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
580 Previous.Type == TT_BinaryOperator) ||
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000581 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
582 Line.MustBeDeclaration))
583 State.Stack.back().BreakBeforeParameter = true;
584 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000585 } else {
Daniel Jasper62e68172013-02-25 15:59:54 +0000586 if (Current.is(tok::equal) &&
Daniel Jasper31c96b92013-04-05 09:38:50 +0000587 (RootToken.is(tok::kw_for) || State.ParenLevel == 0) &&
588 State.Stack.back().VariablePos == 0) {
589 State.Stack.back().VariablePos = State.Column;
590 // Move over * and & if they are bound to the variable name.
591 const AnnotatedToken *Tok = &Previous;
592 while (Tok &&
593 State.Stack.back().VariablePos >= Tok->FormatTok.TokenLength) {
594 State.Stack.back().VariablePos -= Tok->FormatTok.TokenLength;
595 if (Tok->SpacesRequiredBefore != 0)
596 break;
597 Tok = Tok->Parent;
598 }
Daniel Jaspera628c982013-04-03 13:36:17 +0000599 if (Previous.PartOfMultiVariableDeclStmt)
600 State.Stack.back().LastSpace = State.Stack.back().VariablePos;
601 }
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000602
Daniel Jaspereef30492013-02-11 12:36:37 +0000603 unsigned Spaces = State.NextToken->SpacesRequiredBefore;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000604
Daniel Jasperf7935112012-12-03 18:12:45 +0000605 if (!DryRun)
Alexander Kornienkoafcef332013-03-19 17:41:36 +0000606 Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000607
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000608 if (Current.Type == TT_ObjCSelectorName &&
609 State.Stack.back().ColonPos == 0) {
610 if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000611 State.Column + Spaces + Current.FormatTok.TokenLength)
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000612 State.Stack.back().ColonPos =
613 State.Stack.back().Indent + Current.LongestObjCSelectorName;
614 else
615 State.Stack.back().ColonPos =
Daniel Jasperc485b4e2013-02-06 16:00:26 +0000616 State.Column + Spaces + Current.FormatTok.TokenLength;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000617 }
618
Daniel Jasperc04baae2013-04-10 09:49:49 +0000619 if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
Daniel Jasper6bee6822013-04-08 20:33:42 +0000620 Current.Type != TT_LineComment)
Daniel Jasper400adc62013-02-08 15:28:42 +0000621 State.Stack.back().Indent = State.Column + Spaces;
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000622 if (Previous.is(tok::comma) && !Current.isTrailingComment() &&
623 State.Stack.back().AvoidBinPacking)
624 State.Stack.back().NoLineBreak = true;
Daniel Jasper9278eb92013-01-16 14:59:02 +0000625
Daniel Jaspere9de2602012-12-06 09:56:08 +0000626 State.Column += Spaces;
Daniel Jaspera628c982013-04-03 13:36:17 +0000627 if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
Daniel Jasper39e27382013-01-23 20:41:06 +0000628 // Treat the condition inside an if as if it was a second function
629 // parameter, i.e. let nested calls have an indent of 4.
630 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000631 else if (Previous.is(tok::comma))
Daniel Jasper39e27382013-01-23 20:41:06 +0000632 State.Stack.back().LastSpace = State.Column;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000633 else if ((Previous.Type == TT_BinaryOperator ||
Daniel Jasper65585ed2013-01-28 13:31:35 +0000634 Previous.Type == TT_ConditionalExpr ||
635 Previous.Type == TT_CtorInitializerColon) &&
Daniel Jasper20b09ef2013-01-28 09:35:24 +0000636 getPrecedence(Previous) != prec::Assignment)
637 State.Stack.back().LastSpace = State.Column;
Daniel Jaspereead02b2013-02-14 08:42:54 +0000638 else if (Previous.Type == TT_InheritanceColon)
639 State.Stack.back().Indent = State.Column;
Daniel Jasperd69fc772013-05-08 14:12:04 +0000640 else if (Previous.opensScope() && !Current.FakeLParens.empty())
641 // If this function has multiple parameters or a binary expression
642 // parameter, indent nested calls from the start of the first parameter.
Daniel Jasper7b5773e92013-01-28 07:35:34 +0000643 State.Stack.back().LastSpace = State.Column;
Daniel Jasperf7935112012-12-03 18:12:45 +0000644 }
Daniel Jasper9278eb92013-01-16 14:59:02 +0000645
Manuel Klimek1998ea22013-02-20 10:15:13 +0000646 return moveStateToNextToken(State, DryRun);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000647 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000648
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000649 /// \brief Mark the next token as consumed in \p State and modify its stacks
650 /// accordingly.
Manuel Klimek1998ea22013-02-20 10:15:13 +0000651 unsigned moveStateToNextToken(LineState &State, bool DryRun) {
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000652 const AnnotatedToken &Current = *State.NextToken;
Daniel Jasper337816e2013-01-11 10:22:12 +0000653 assert(State.Stack.size());
Daniel Jaspere9de2602012-12-06 09:56:08 +0000654
Daniel Jaspereead02b2013-02-14 08:42:54 +0000655 if (Current.Type == TT_InheritanceColon)
656 State.Stack.back().AvoidBinPacking = true;
Daniel Jasper337816e2013-01-11 10:22:12 +0000657 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
658 State.Stack.back().FirstLessLess = State.Column;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000659 if (Current.is(tok::question))
660 State.Stack.back().QuestionColumn = State.Column;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000661 if (Current.isOneOf(tok::period, tok::arrow) &&
Daniel Jasperf9a84b52013-03-01 16:48:32 +0000662 Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
663 State.Stack.back().StartOfFunctionCall =
Daniel Jasper66e4f832013-05-10 13:37:16 +0000664 Current.LastInChainOfCalls ? 0 : State.Column +
665 Current.FormatTok.TokenLength;
Daniel Jasper37905f72013-02-21 15:00:29 +0000666 if (Current.Type == TT_CtorInitializerColon) {
Manuel Klimek13b97d82013-05-13 08:42:42 +0000667 // Indent 2 from the column, so:
668 // SomeClass::SomeClass()
669 // : First(...), ...
670 // Next(...)
671 // ^ line up here.
Daniel Jasper6bee6822013-04-08 20:33:42 +0000672 State.Stack.back().Indent = State.Column + 2;
Daniel Jasper37905f72013-02-21 15:00:29 +0000673 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
674 State.Stack.back().AvoidBinPacking = true;
675 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000676 }
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000677
Daniel Jasper6bee6822013-04-08 20:33:42 +0000678 // If return returns a binary expression, align after it.
679 if (Current.is(tok::kw_return) && !Current.FakeLParens.empty())
680 State.Stack.back().LastSpace = State.Column + 7;
681
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000682 // In ObjC method declaration we align on the ":" of parameters, but we need
683 // to ensure that we indent parameters on subsequent lines by at least 4.
Daniel Jasperc238c872013-04-02 14:33:13 +0000684 if (Current.Type == TT_ObjCMethodSpecifier)
685 State.Stack.back().Indent += 4;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000686
Daniel Jasper400adc62013-02-08 15:28:42 +0000687 // Insert scopes created by fake parenthesis.
Daniel Jasper6bee6822013-04-08 20:33:42 +0000688 const AnnotatedToken *Previous = Current.getPreviousNoneComment();
689 // Don't add extra indentation for the first fake parenthesis after
690 // 'return', assignements or opening <({[. The indentation for these cases
691 // is special cased.
692 bool SkipFirstExtraIndent =
693 Current.is(tok::kw_return) ||
Daniel Jasperc04baae2013-04-10 09:49:49 +0000694 (Previous && (Previous->opensScope() ||
Daniel Jasper6bee6822013-04-08 20:33:42 +0000695 getPrecedence(*Previous) == prec::Assignment));
696 for (SmallVector<prec::Level, 4>::const_reverse_iterator
697 I = Current.FakeLParens.rbegin(),
698 E = Current.FakeLParens.rend();
699 I != E; ++I) {
Daniel Jasper400adc62013-02-08 15:28:42 +0000700 ParenState NewParenState = State.Stack.back();
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000701 NewParenState.ForFakeParenthesis = true;
Daniel Jasper6bee6822013-04-08 20:33:42 +0000702 NewParenState.Indent =
703 std::max(std::max(State.Column, NewParenState.Indent),
704 State.Stack.back().LastSpace);
705
706 // Always indent conditional expressions. Never indent expression where
707 // the 'operator' is ',', ';' or an assignment (i.e. *I <=
708 // prec::Assignment) as those have different indentation rules. Indent
709 // other expression, unless the indentation needs to be skipped.
710 if (*I == prec::Conditional ||
711 (!SkipFirstExtraIndent && *I > prec::Assignment))
712 NewParenState.Indent += 4;
Daniel Jasperc04baae2013-04-10 09:49:49 +0000713 if (Previous && !Previous->opensScope())
Daniel Jasper6bee6822013-04-08 20:33:42 +0000714 NewParenState.BreakBeforeParameter = false;
Daniel Jasper400adc62013-02-08 15:28:42 +0000715 State.Stack.push_back(NewParenState);
Daniel Jasper6bee6822013-04-08 20:33:42 +0000716 SkipFirstExtraIndent = false;
Daniel Jasper400adc62013-02-08 15:28:42 +0000717 }
718
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000719 // If we encounter an opening (, [, { or <, we add a level to our stacks to
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000720 // prepare for the following tokens.
Daniel Jasperc04baae2013-04-10 09:49:49 +0000721 if (Current.opensScope()) {
Daniel Jasper337816e2013-01-11 10:22:12 +0000722 unsigned NewIndent;
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000723 unsigned LastSpace = State.Stack.back().LastSpace;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000724 bool AvoidBinPacking;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000725 if (Current.is(tok::l_brace)) {
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000726 NewIndent = Style.IndentWidth + LastSpace;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000727 AvoidBinPacking = false;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000728 } else {
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000729 NewIndent =
730 4 + std::max(LastSpace, State.Stack.back().StartOfFunctionCall);
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000731 AvoidBinPacking = !Style.BinPackParameters;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000732 }
Daniel Jaspere3c0e012013-04-25 13:31:51 +0000733
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000734 State.Stack.push_back(ParenState(NewIndent, LastSpace, AvoidBinPacking,
735 State.Stack.back().NoLineBreak));
Daniel Jasper400adc62013-02-08 15:28:42 +0000736 ++State.ParenLevel;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000737 }
738
Daniel Jasperacc33662013-02-08 08:22:00 +0000739 // If this '[' opens an ObjC call, determine whether all parameters fit into
740 // one line and put one per line if they don't.
741 if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
742 Current.MatchingParen != NULL) {
743 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
744 State.Stack.back().BreakBeforeParameter = true;
745 }
746
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000747 // If we encounter a closing ), ], } or >, we can remove a level from our
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000748 // stacks.
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000749 if (Current.isOneOf(tok::r_paren, tok::r_square) ||
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000750 (Current.is(tok::r_brace) && State.NextToken != &RootToken) ||
751 State.NextToken->Type == TT_TemplateCloser) {
Daniel Jasper337816e2013-01-11 10:22:12 +0000752 State.Stack.pop_back();
Daniel Jasper400adc62013-02-08 15:28:42 +0000753 --State.ParenLevel;
754 }
755
756 // Remove scopes created by fake parenthesis.
757 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
Daniel Jasper6daabe32013-04-04 19:31:00 +0000758 unsigned VariablePos = State.Stack.back().VariablePos;
Daniel Jasper400adc62013-02-08 15:28:42 +0000759 State.Stack.pop_back();
Daniel Jasper6daabe32013-04-04 19:31:00 +0000760 State.Stack.back().VariablePos = VariablePos;
Daniel Jasperf7935112012-12-03 18:12:45 +0000761 }
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000762
Daniel Jasper47a04442013-05-13 20:50:15 +0000763 if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
Manuel Klimek02f640a2013-02-20 15:25:48 +0000764 State.StartOfStringLiteral = State.Column;
Daniel Jasper47a04442013-05-13 20:50:15 +0000765 } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
766 tok::string_literal)) {
Daniel Jasper7dd22c51b2013-05-16 04:26:02 +0000767 State.StartOfStringLiteral = 0;
Manuel Klimek02f640a2013-02-20 15:25:48 +0000768 }
769
Manuel Klimek1998ea22013-02-20 10:15:13 +0000770 State.Column += Current.FormatTok.TokenLength;
771
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000772 if (State.NextToken->Children.empty())
773 State.NextToken = NULL;
774 else
775 State.NextToken = &State.NextToken->Children[0];
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000776
Manuel Klimek1998ea22013-02-20 10:15:13 +0000777 return breakProtrudingToken(Current, State, DryRun);
778 }
779
780 /// \brief If the current token sticks out over the end of the line, break
781 /// it if possible.
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000782 ///
783 /// \returns An extra penalty if a token was broken, otherwise 0.
784 ///
785 /// Note that the penalty of the token protruding the allowed line length is
786 /// already handled in \c addNextStateToQueue; the returned penalty will only
787 /// cover the cost of the additional line breaks.
Manuel Klimek1998ea22013-02-20 10:15:13 +0000788 unsigned breakProtrudingToken(const AnnotatedToken &Current, LineState &State,
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000789 bool DryRun,
790 unsigned UnbreakableTailLength = 0) {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000791 llvm::OwningPtr<BreakableToken> Token;
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000792 unsigned StartColumn = State.Column - Current.FormatTok.TokenLength -
793 UnbreakableTailLength;
Daniel Jasper8bb99e82013-05-16 12:59:13 +0000794 if (Current.is(tok::string_literal) &&
795 Current.Type != TT_ImplicitStringLiteral) {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000796 // Only break up default narrow strings.
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000797 const char *LiteralData = SourceMgr.getCharacterData(
798 Current.FormatTok.getStartOfNonWhitespace());
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000799 if (!LiteralData || *LiteralData != '"')
800 return 0;
801
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000802 Token.reset(new BreakableStringLiteral(SourceMgr, Current.FormatTok,
803 StartColumn));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000804 } else if (Current.Type == TT_BlockComment) {
805 BreakableBlockComment *BBC =
806 new BreakableBlockComment(SourceMgr, Current, StartColumn);
807 if (!DryRun)
808 BBC->alignLines(Whitespaces);
809 Token.reset(BBC);
Daniel Jasper4a4be012013-05-06 10:24:51 +0000810 } else if (Current.Type == TT_LineComment &&
811 (Current.Parent == NULL ||
812 Current.Parent->Type != TT_ImplicitStringLiteral)) {
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000813 Token.reset(new BreakableLineComment(SourceMgr, Current, StartColumn));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000814 } else {
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000815 // If a token that we cannot breaks protrudes, it means we were unable to
816 // break a sequence of tokens due to disallowed breaks between the tokens.
817 // Thus, we recursively search backwards to try to find a breakable token.
818 if (State.Column <= getColumnLimit() ||
819 Current.CanBreakBefore || !Current.Parent)
820 return 0;
821 return breakProtrudingToken(
822 *Current.Parent, State, DryRun,
823 UnbreakableTailLength + Current.FormatTok.TokenLength);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000824 }
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000825 if (UnbreakableTailLength >= getColumnLimit())
826 return 0;
827 unsigned RemainingSpace = getColumnLimit() - UnbreakableTailLength;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000828
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000829 bool BreakInserted = false;
830 unsigned Penalty = 0;
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000831 unsigned PositionAfterLastLineInToken = 0;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000832 for (unsigned LineIndex = 0; LineIndex < Token->getLineCount();
833 ++LineIndex) {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000834 unsigned TailOffset = 0;
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000835 unsigned RemainingTokenLength =
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000836 Token->getLineLengthAfterSplit(LineIndex, TailOffset);
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000837 while (RemainingTokenLength > RemainingSpace) {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000838 BreakableToken::Split Split =
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000839 Token->getSplit(LineIndex, TailOffset, RemainingSpace);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000840 if (Split.first == StringRef::npos)
841 break;
842 assert(Split.first != 0);
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000843 unsigned NewRemainingTokenLength = Token->getLineLengthAfterSplit(
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000844 LineIndex, TailOffset + Split.first + Split.second);
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000845 if (NewRemainingTokenLength >= RemainingTokenLength)
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000846 break;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000847 if (!DryRun) {
848 Token->insertBreak(LineIndex, TailOffset, Split, Line.InPPDirective,
849 Whitespaces);
850 }
851 TailOffset += Split.first + Split.second;
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000852 RemainingTokenLength = NewRemainingTokenLength;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000853 Penalty += Style.PenaltyExcessCharacter;
854 BreakInserted = true;
Manuel Klimek1998ea22013-02-20 10:15:13 +0000855 }
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000856 PositionAfterLastLineInToken = RemainingTokenLength;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000857 if (!DryRun) {
858 Token->trimLine(LineIndex, TailOffset, Line.InPPDirective, Whitespaces);
859 }
860 }
861
862 if (BreakInserted) {
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000863 State.Column = PositionAfterLastLineInToken + UnbreakableTailLength;
Daniel Jasper2cf17bf2013-02-27 09:47:53 +0000864 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
865 State.Stack[i].BreakBeforeParameter = true;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000866 State.Stack.back().LastSpace = StartColumn;
Manuel Klimek1998ea22013-02-20 10:15:13 +0000867 }
Manuel Klimek1998ea22013-02-20 10:15:13 +0000868 return Penalty;
869 }
870
Daniel Jasper2df93312013-01-09 10:16:05 +0000871 unsigned getColumnLimit() {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000872 // In preprocessor directives reserve two chars for trailing " \"
873 return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0);
Daniel Jasper2df93312013-01-09 10:16:05 +0000874 }
875
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000876 /// \brief An edge in the solution space from \c Previous->State to \c State,
877 /// inserting a newline dependent on the \c NewLine.
878 struct StateNode {
879 StateNode(const LineState &State, bool NewLine, StateNode *Previous)
Daniel Jasper12ef4e52013-02-21 21:33:55 +0000880 : State(State), NewLine(NewLine), Previous(Previous) {}
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000881 LineState State;
882 bool NewLine;
883 StateNode *Previous;
884 };
Daniel Jasper4b866272013-02-01 11:00:45 +0000885
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000886 /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
887 ///
888 /// In case of equal penalties, we want to prefer states that were inserted
889 /// first. During state generation we make sure that we insert states first
890 /// that break the line as late as possible.
891 typedef std::pair<unsigned, unsigned> OrderedPenalty;
892
893 /// \brief An item in the prioritized BFS search queue. The \c StateNode's
894 /// \c State has the given \c OrderedPenalty.
895 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
896
897 /// \brief The BFS queue type.
898 typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
899 std::greater<QueueItem> > QueueType;
Daniel Jasper4b866272013-02-01 11:00:45 +0000900
901 /// \brief Analyze the entire solution space starting from \p InitialState.
Daniel Jasperf7935112012-12-03 18:12:45 +0000902 ///
Daniel Jasper4b866272013-02-01 11:00:45 +0000903 /// This implements a variant of Dijkstra's algorithm on the graph that spans
904 /// the solution space (\c LineStates are the nodes). The algorithm tries to
905 /// find the shortest path (the one with lowest penalty) from \p InitialState
906 /// to a state where all tokens are placed.
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000907 unsigned analyzeSolutionSpace(LineState &InitialState) {
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000908 std::set<LineState> Seen;
909
Daniel Jasper4b866272013-02-01 11:00:45 +0000910 // Insert start element into queue.
Daniel Jasper687af3b2013-02-14 14:26:07 +0000911 StateNode *Node =
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000912 new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
913 Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
914 ++Count;
Daniel Jasper4b866272013-02-01 11:00:45 +0000915
916 // While not empty, take first element and follow edges.
917 while (!Queue.empty()) {
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000918 unsigned Penalty = Queue.top().first.first;
Daniel Jasper687af3b2013-02-14 14:26:07 +0000919 StateNode *Node = Queue.top().second;
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000920 if (Node->State.NextToken == NULL) {
Alexander Kornienko49149672013-05-10 11:56:10 +0000921 DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
Daniel Jasper4b866272013-02-01 11:00:45 +0000922 break;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000923 }
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000924 Queue.pop();
Daniel Jasper4b866272013-02-01 11:00:45 +0000925
Daniel Jasperf8114cf2013-05-22 05:27:42 +0000926 // Cut off the analysis of certain solutions if the analysis gets too
927 // complex. See description of IgnoreStackForComparison.
928 if (Count > 10000)
929 Node->State.IgnoreStackForComparison = true;
930
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000931 if (!Seen.insert(Node->State).second)
932 // State already examined with lower penalty.
933 continue;
Daniel Jasper4b866272013-02-01 11:00:45 +0000934
Manuel Klimekaf491072013-02-13 10:54:19 +0000935 addNextStateToQueue(Penalty, Node, /*NewLine=*/ false);
936 addNextStateToQueue(Penalty, Node, /*NewLine=*/ true);
Daniel Jasper4b866272013-02-01 11:00:45 +0000937 }
938
939 if (Queue.empty())
940 // We were unable to find a solution, do nothing.
941 // FIXME: Add diagnostic?
Daniel Jasperf7935112012-12-03 18:12:45 +0000942 return 0;
943
Daniel Jasper4b866272013-02-01 11:00:45 +0000944 // Reconstruct the solution.
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000945 reconstructPath(InitialState, Queue.top().second);
Alexander Kornienko49149672013-05-10 11:56:10 +0000946 DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
947 DEBUG(llvm::dbgs() << "---\n");
Daniel Jasperf7935112012-12-03 18:12:45 +0000948
Daniel Jasper4b866272013-02-01 11:00:45 +0000949 // Return the column after the last token of the solution.
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000950 return Queue.top().second->State.Column;
951 }
952
953 void reconstructPath(LineState &State, StateNode *Current) {
954 // FIXME: This recursive implementation limits the possible number
955 // of tokens per line if compiled into a binary with small stack space.
956 // To become more independent of stack frame limitations we would need
957 // to also change the TokenAnnotator.
958 if (Current->Previous == NULL)
959 return;
960 reconstructPath(State, Current->Previous);
961 DEBUG({
962 if (Current->NewLine) {
Alexander Kornienko49149672013-05-10 11:56:10 +0000963 llvm::dbgs()
Daniel Jasperb9caeac2013-02-13 20:33:44 +0000964 << "Penalty for splitting before "
965 << Current->Previous->State.NextToken->FormatTok.Tok.getName()
966 << ": " << Current->Previous->State.NextToken->SplitPenalty << "\n";
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000967 }
968 });
969 addTokenToState(Current->NewLine, false, State);
Daniel Jasper4b866272013-02-01 11:00:45 +0000970 }
971
Manuel Klimekaf491072013-02-13 10:54:19 +0000972 /// \brief Add the following state to the analysis queue \c Queue.
Daniel Jasper4b866272013-02-01 11:00:45 +0000973 ///
Manuel Klimekaf491072013-02-13 10:54:19 +0000974 /// Assume the current state is \p PreviousNode and has been reached with a
Daniel Jasper4b866272013-02-01 11:00:45 +0000975 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
Manuel Klimekaf491072013-02-13 10:54:19 +0000976 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
977 bool NewLine) {
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000978 if (NewLine && !canBreak(PreviousNode->State))
Daniel Jasper4b866272013-02-01 11:00:45 +0000979 return;
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000980 if (!NewLine && mustBreak(PreviousNode->State))
Daniel Jasper4b866272013-02-01 11:00:45 +0000981 return;
Daniel Jasper20b09ef2013-01-28 09:35:24 +0000982 if (NewLine)
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000983 Penalty += PreviousNode->State.NextToken->SplitPenalty;
984
985 StateNode *Node = new (Allocator.Allocate())
986 StateNode(PreviousNode->State, NewLine, PreviousNode);
Manuel Klimek1998ea22013-02-20 10:15:13 +0000987 Penalty += addTokenToState(NewLine, true, Node->State);
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000988 if (Node->State.Column > getColumnLimit()) {
989 unsigned ExcessCharacters = Node->State.Column - getColumnLimit();
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000990 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
Daniel Jasper2df93312013-01-09 10:16:05 +0000991 }
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000992
993 Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
994 ++Count;
Daniel Jasper4b866272013-02-01 11:00:45 +0000995 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000996
Daniel Jasper4b866272013-02-01 11:00:45 +0000997 /// \brief Returns \c true, if a line break after \p State is allowed.
998 bool canBreak(const LineState &State) {
Daniel Jasper473c62c2013-05-17 09:35:01 +0000999 const AnnotatedToken &Current = *State.NextToken;
1000 const AnnotatedToken &Previous = *Current.Parent;
1001 if (!Current.CanBreakBefore &&
1002 !(Current.is(tok::r_brace) &&
Daniel Jasper4b866272013-02-01 11:00:45 +00001003 State.Stack.back().BreakBeforeClosingBrace))
1004 return false;
Daniel Jasper473c62c2013-05-17 09:35:01 +00001005 // The opening "{" of a braced list has to be on the same line as the first
1006 // element if it is nested in another braced init list or function call.
1007 if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
1008 Previous.Parent &&
1009 Previous.Parent->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
1010 return false;
Daniel Jaspercc960fa2013-04-22 07:59:53 +00001011 return !State.Stack.back().NoLineBreak;
Daniel Jasper4b866272013-02-01 11:00:45 +00001012 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001013
Daniel Jasper4b866272013-02-01 11:00:45 +00001014 /// \brief Returns \c true, if a line break after \p State is mandatory.
1015 bool mustBreak(const LineState &State) {
Daniel Jasperd69fc772013-05-08 14:12:04 +00001016 const AnnotatedToken &Current = *State.NextToken;
1017 const AnnotatedToken &Previous = *Current.Parent;
1018 if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
Daniel Jasper4b866272013-02-01 11:00:45 +00001019 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001020 if (Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)
Daniel Jasper4b866272013-02-01 11:00:45 +00001021 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001022 if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
Daniel Jasper4b866272013-02-01 11:00:45 +00001023 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001024 if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) ||
1025 Current.Type == TT_ConditionalExpr) &&
Daniel Jasperacc33662013-02-08 08:22:00 +00001026 State.Stack.back().BreakBeforeParameter &&
Daniel Jasperd69fc772013-05-08 14:12:04 +00001027 !Current.isTrailingComment() &&
1028 !Current.isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper4b866272013-02-01 11:00:45 +00001029 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001030
1031 // If we need to break somewhere inside the LHS of a binary expression, we
1032 // should also break after the operator.
1033 if (Previous.Type == TT_BinaryOperator &&
1034 !Previous.isOneOf(tok::lessless, tok::question) &&
1035 getPrecedence(Previous) != prec::Assignment &&
Daniel Jasperacc33662013-02-08 08:22:00 +00001036 State.Stack.back().BreakBeforeParameter)
Daniel Jasper1ac3e052013-02-05 10:07:47 +00001037 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001038
1039 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
1040 // out whether it is the first parameter. Clean this up.
1041 if (Current.Type == TT_ObjCSelectorName &&
1042 Current.LongestObjCSelectorName == 0 &&
1043 State.Stack.back().BreakBeforeParameter)
Daniel Jasper4b866272013-02-01 11:00:45 +00001044 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001045 if ((Current.Type == TT_CtorInitializerColon ||
1046 (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0)))
Daniel Jasper40aacf42013-03-14 13:45:21 +00001047 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001048
Daniel Jasper9b334242013-03-15 14:57:30 +00001049 // This prevents breaks like:
1050 // ...
1051 // SomeParameter, OtherParameter).DoSomething(
1052 // ...
1053 // As they hide "DoSomething" and generally bad for readability.
Daniel Jasperd69fc772013-05-08 14:12:04 +00001054 if (Current.isOneOf(tok::period, tok::arrow) &&
Daniel Jasper9b334242013-03-15 14:57:30 +00001055 getRemainingLength(State) + State.Column > getColumnLimit() &&
1056 State.ParenLevel < State.StartOfLineLevel)
1057 return true;
Daniel Jasperc6fbc212013-05-15 09:35:08 +00001058
1059 if (Current.Type == TT_StartOfName && Line.MightBeFunctionDecl &&
1060 State.Stack.back().BreakBeforeParameter && State.ParenLevel == 0)
1061 return true;
Daniel Jasper4b866272013-02-01 11:00:45 +00001062 return false;
Daniel Jasperf7935112012-12-03 18:12:45 +00001063 }
1064
Daniel Jasper9b334242013-03-15 14:57:30 +00001065 // Returns the total number of columns required for the remaining tokens.
1066 unsigned getRemainingLength(const LineState &State) {
1067 if (State.NextToken && State.NextToken->Parent)
1068 return Line.Last->TotalLength - State.NextToken->Parent->TotalLength;
1069 return 0;
1070 }
1071
Daniel Jasperf7935112012-12-03 18:12:45 +00001072 FormatStyle Style;
1073 SourceManager &SourceMgr;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001074 const AnnotatedLine &Line;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001075 const unsigned FirstIndent;
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001076 const AnnotatedToken &RootToken;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001077 WhitespaceManager &Whitespaces;
Manuel Klimekaf491072013-02-13 10:54:19 +00001078
1079 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1080 QueueType Queue;
1081 // Increasing count of \c StateNode items we have created. This is used
1082 // to create a deterministic order independent of the container.
1083 unsigned Count;
Daniel Jasperf7935112012-12-03 18:12:45 +00001084};
1085
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001086class LexerBasedFormatTokenSource : public FormatTokenSource {
1087public:
1088 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +00001089 : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001090 IdentTable(Lex.getLangOpts()) {
1091 Lex.SetKeepWhitespaceMode(true);
1092 }
1093
1094 virtual FormatToken getNextToken() {
1095 if (GreaterStashed) {
1096 FormatTok.NewlinesBefore = 0;
1097 FormatTok.WhiteSpaceStart =
1098 FormatTok.Tok.getLocation().getLocWithOffset(1);
1099 FormatTok.WhiteSpaceLength = 0;
1100 GreaterStashed = false;
1101 return FormatTok;
1102 }
1103
1104 FormatTok = FormatToken();
1105 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimekef920692013-01-07 07:56:50 +00001106 StringRef Text = rawTokenText(FormatTok.Tok);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001107 FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
Manuel Klimek52d0fd82013-01-05 22:56:06 +00001108 if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0)
1109 FormatTok.IsFirst = true;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001110
1111 // Consume and record whitespace until we find a significant token.
1112 while (FormatTok.Tok.is(tok::unknown)) {
Manuel Klimek0c137952013-02-11 12:33:24 +00001113 unsigned Newlines = Text.count('\n');
Daniel Jasper973c9422013-03-04 13:43:19 +00001114 if (Newlines > 0)
1115 FormatTok.LastNewlineOffset =
1116 FormatTok.WhiteSpaceLength + Text.rfind('\n') + 1;
Manuel Klimek0c137952013-02-11 12:33:24 +00001117 unsigned EscapedNewlines = Text.count("\\\n");
1118 FormatTok.NewlinesBefore += Newlines;
1119 FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001120 FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
1121
1122 if (FormatTok.Tok.is(tok::eof))
1123 return FormatTok;
1124 Lex.LexFromRawLexer(FormatTok.Tok);
Manuel Klimekef920692013-01-07 07:56:50 +00001125 Text = rawTokenText(FormatTok.Tok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001126 }
Manuel Klimekef920692013-01-07 07:56:50 +00001127
1128 // Now FormatTok is the next non-whitespace token.
1129 FormatTok.TokenLength = Text.size();
1130
Alexander Kornienko9e90b622013-04-17 17:34:05 +00001131 if (FormatTok.Tok.is(tok::comment)) {
1132 FormatTok.TrailingWhiteSpaceLength = Text.size() - Text.rtrim().size();
1133 FormatTok.TokenLength -= FormatTok.TrailingWhiteSpaceLength;
1134 }
1135
Manuel Klimek1abf7892013-01-04 23:34:14 +00001136 // In case the token starts with escaped newlines, we want to
1137 // take them into account as whitespace - this pattern is quite frequent
1138 // in macro definitions.
1139 // FIXME: What do we want to do with other escaped spaces, and escaped
1140 // spaces or newlines in the middle of tokens?
1141 // FIXME: Add a more explicit test.
1142 unsigned i = 0;
Daniel Jasperda16db32013-01-07 10:48:50 +00001143 while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001144 // FIXME: ++FormatTok.NewlinesBefore is missing...
Manuel Klimek1abf7892013-01-04 23:34:14 +00001145 FormatTok.WhiteSpaceLength += 2;
Manuel Klimekef920692013-01-07 07:56:50 +00001146 FormatTok.TokenLength -= 2;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001147 i += 2;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001148 }
1149
1150 if (FormatTok.Tok.is(tok::raw_identifier)) {
Manuel Klimek1abf7892013-01-04 23:34:14 +00001151 IdentifierInfo &Info = IdentTable.get(Text);
Daniel Jasper050948a52012-12-21 17:58:39 +00001152 FormatTok.Tok.setIdentifierInfo(&Info);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001153 FormatTok.Tok.setKind(Info.getTokenID());
1154 }
1155
1156 if (FormatTok.Tok.is(tok::greatergreater)) {
1157 FormatTok.Tok.setKind(tok::greater);
Daniel Jasper57d4a582013-02-28 10:06:05 +00001158 FormatTok.TokenLength = 1;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001159 GreaterStashed = true;
1160 }
1161
1162 return FormatTok;
1163 }
1164
Nico Weber29f9dea2013-02-11 15:32:15 +00001165 IdentifierTable &getIdentTable() { return IdentTable; }
1166
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001167private:
1168 FormatToken FormatTok;
1169 bool GreaterStashed;
1170 Lexer &Lex;
1171 SourceManager &SourceMgr;
1172 IdentifierTable IdentTable;
1173
1174 /// Returns the text of \c FormatTok.
Manuel Klimekef920692013-01-07 07:56:50 +00001175 StringRef rawTokenText(Token &Tok) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001176 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
1177 Tok.getLength());
1178 }
1179};
1180
Daniel Jasperf7935112012-12-03 18:12:45 +00001181class Formatter : public UnwrappedLineConsumer {
1182public:
Daniel Jasperd2ae41a2013-05-15 08:14:19 +00001183 Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
Daniel Jasperf7935112012-12-03 18:12:45 +00001184 const std::vector<CharSourceRange> &Ranges)
Daniel Jasperd2ae41a2013-05-15 08:14:19 +00001185 : Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienkoafcef332013-03-19 17:41:36 +00001186 Whitespaces(SourceMgr, Style), Ranges(Ranges) {}
Daniel Jasperf7935112012-12-03 18:12:45 +00001187
Daniel Jasperfd8c4b12013-01-11 14:23:32 +00001188 virtual ~Formatter() {}
Daniel Jasper61bd3a12012-12-04 21:05:31 +00001189
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001190 tooling::Replacements format() {
1191 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
Daniel Jasperd2ae41a2013-05-15 08:14:19 +00001192 UnwrappedLineParser Parser(Style, Tokens, *this);
Manuel Klimek1a18c402013-04-12 14:13:36 +00001193 bool StructuralError = Parser.parse();
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001194 unsigned PreviousEndOfLineColumn = 0;
1195 TokenAnnotator Annotator(Style, SourceMgr, Lex,
1196 Tokens.getIdentTable().get("in"));
1197 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1198 Annotator.annotate(AnnotatedLines[i]);
1199 }
1200 deriveLocalStyle();
1201 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1202 Annotator.calculateFormattingInformation(AnnotatedLines[i]);
1203 }
Daniel Jasperb67cc422013-04-09 17:46:55 +00001204
1205 // Adapt level to the next line if this is a comment.
1206 // FIXME: Can/should this be done in the UnwrappedLineParser?
Daniel Jasper6728fc12013-04-11 14:29:13 +00001207 const AnnotatedLine *NextNoneCommentLine = NULL;
Daniel Jasperb67cc422013-04-09 17:46:55 +00001208 for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) {
1209 if (NextNoneCommentLine && AnnotatedLines[i].First.is(tok::comment) &&
1210 AnnotatedLines[i].First.Children.empty())
1211 AnnotatedLines[i].Level = NextNoneCommentLine->Level;
1212 else
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00001213 NextNoneCommentLine =
1214 AnnotatedLines[i].First.isNot(tok::r_brace) ? &AnnotatedLines[i]
1215 : NULL;
Daniel Jasperb67cc422013-04-09 17:46:55 +00001216 }
1217
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001218 std::vector<int> IndentForLevel;
1219 bool PreviousLineWasTouched = false;
Alexander Kornienkofd433362013-03-27 17:08:02 +00001220 const AnnotatedToken *PreviousLineLastToken = 0;
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001221 bool FormatPPDirective = false;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001222 for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
1223 E = AnnotatedLines.end();
1224 I != E; ++I) {
1225 const AnnotatedLine &TheLine = *I;
1226 const FormatToken &FirstTok = TheLine.First.FormatTok;
1227 int Offset = getIndentOffset(TheLine.First);
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001228
1229 // Check whether this line is part of a formatted preprocessor directive.
1230 if (FirstTok.HasUnescapedNewline)
1231 FormatPPDirective = false;
1232 if (!FormatPPDirective && TheLine.InPPDirective &&
1233 (touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
1234 FormatPPDirective = true;
1235
Daniel Jasper12f9d8e2013-05-14 09:30:02 +00001236 // Determine indent and try to merge multiple unwrapped lines.
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001237 while (IndentForLevel.size() <= TheLine.Level)
1238 IndentForLevel.push_back(-1);
1239 IndentForLevel.resize(TheLine.Level + 1);
Daniel Jasper12f9d8e2013-05-14 09:30:02 +00001240 unsigned Indent = getIndent(IndentForLevel, TheLine.Level);
1241 if (static_cast<int>(Indent) + Offset >= 0)
1242 Indent += Offset;
1243 tryFitMultipleLinesInOne(Indent, I, E);
1244
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001245 bool WasMoved = PreviousLineWasTouched && FirstTok.NewlinesBefore == 0;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001246 if (TheLine.First.is(tok::eof)) {
1247 if (PreviousLineWasTouched) {
1248 unsigned NewLines = std::min(FirstTok.NewlinesBefore, 1u);
1249 Whitespaces.replaceWhitespace(TheLine.First, NewLines, /*Indent*/ 0,
Alexander Kornienkoafcef332013-03-19 17:41:36 +00001250 /*WhitespaceStartColumn*/ 0);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001251 }
1252 } else if (TheLine.Type != LT_Invalid &&
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001253 (WasMoved || FormatPPDirective || touchesLine(TheLine))) {
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001254 unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
Manuel Klimek1a18c402013-04-12 14:13:36 +00001255 if (FirstTok.WhiteSpaceStart.isValid() &&
1256 // Insert a break even if there is a structural error in case where
1257 // we break apart a line consisting of multiple unwrapped lines.
1258 (FirstTok.NewlinesBefore == 0 || !StructuralError)) {
Alexander Kornienkofd433362013-03-27 17:08:02 +00001259 formatFirstToken(TheLine.First, PreviousLineLastToken, Indent,
1260 TheLine.InPPDirective, PreviousEndOfLineColumn);
Manuel Klimek1a18c402013-04-12 14:13:36 +00001261 } else {
1262 Indent = LevelIndent =
1263 SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001264 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001265 UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
Manuel Klimek1a18c402013-04-12 14:13:36 +00001266 TheLine.First, Whitespaces);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001267 PreviousEndOfLineColumn =
1268 Formatter.format(I + 1 != E ? &*(I + 1) : NULL);
1269 IndentForLevel[TheLine.Level] = LevelIndent;
1270 PreviousLineWasTouched = true;
1271 } else {
1272 if (FirstTok.NewlinesBefore > 0 || FirstTok.IsFirst) {
Daniel Jasper12f9d8e2013-05-14 09:30:02 +00001273 unsigned LevelIndent =
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001274 SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
Daniel Jasper12f9d8e2013-05-14 09:30:02 +00001275 // Remove trailing whitespace of the previous line if it was touched.
1276 if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine))
1277 formatFirstToken(TheLine.First, PreviousLineLastToken, LevelIndent,
1278 TheLine.InPPDirective, PreviousEndOfLineColumn);
1279
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001280 if (static_cast<int>(LevelIndent) - Offset >= 0)
1281 LevelIndent -= Offset;
Daniel Jasper66dc2ec2013-03-20 14:31:47 +00001282 if (TheLine.First.isNot(tok::comment))
1283 IndentForLevel[TheLine.Level] = LevelIndent;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001284 }
1285 // If we did not reformat this unwrapped line, the column at the end of
1286 // the last token is unchanged - thus, we can calculate the end of the
1287 // last token.
1288 SourceLocation LastLoc = TheLine.Last->FormatTok.Tok.getLocation();
1289 PreviousEndOfLineColumn =
1290 SourceMgr.getSpellingColumnNumber(LastLoc) +
1291 Lex.MeasureTokenLength(LastLoc, SourceMgr, Lex.getLangOpts()) - 1;
1292 PreviousLineWasTouched = false;
Daniel Jasperbc0fa392013-03-22 16:25:51 +00001293 if (TheLine.Last->is(tok::comment))
Daniel Jasperd69fc772013-05-08 14:12:04 +00001294 Whitespaces.addUntouchableComment(
1295 SourceMgr.getSpellingColumnNumber(
1296 TheLine.Last->FormatTok.Tok.getLocation()) -
1297 1);
Daniel Jasper770eb7c2013-04-24 06:33:59 +00001298 else
1299 Whitespaces.alignComments();
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001300 }
Alexander Kornienkofd433362013-03-27 17:08:02 +00001301 PreviousLineLastToken = I->Last;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001302 }
1303 return Whitespaces.generateReplacements();
1304 }
1305
1306private:
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001307 void deriveLocalStyle() {
1308 unsigned CountBoundToVariable = 0;
1309 unsigned CountBoundToType = 0;
1310 bool HasCpp03IncompatibleFormat = false;
1311 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1312 if (AnnotatedLines[i].First.Children.empty())
1313 continue;
1314 AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0];
1315 while (!Tok->Children.empty()) {
1316 if (Tok->Type == TT_PointerOrReference) {
1317 bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0;
1318 bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0;
1319 if (SpacesBefore && !SpacesAfter)
1320 ++CountBoundToVariable;
1321 else if (!SpacesBefore && SpacesAfter)
1322 ++CountBoundToType;
1323 }
1324
Daniel Jasper400adc62013-02-08 15:28:42 +00001325 if (Tok->Type == TT_TemplateCloser &&
1326 Tok->Parent->Type == TT_TemplateCloser &&
1327 Tok->FormatTok.WhiteSpaceLength == 0)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001328 HasCpp03IncompatibleFormat = true;
1329 Tok = &Tok->Children[0];
1330 }
1331 }
1332 if (Style.DerivePointerBinding) {
1333 if (CountBoundToType > CountBoundToVariable)
1334 Style.PointerBindsToType = true;
1335 else if (CountBoundToType < CountBoundToVariable)
1336 Style.PointerBindsToType = false;
1337 }
1338 if (Style.Standard == FormatStyle::LS_Auto) {
1339 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
1340 : FormatStyle::LS_Cpp03;
1341 }
1342 }
1343
Manuel Klimekb95f5452013-02-08 17:38:27 +00001344 /// \brief Get the indent of \p Level from \p IndentForLevel.
1345 ///
1346 /// \p IndentForLevel must contain the indent for the level \c l
1347 /// at \p IndentForLevel[l], or a value < 0 if the indent for
1348 /// that level is unknown.
Daniel Jasper687af3b2013-02-14 14:26:07 +00001349 unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
Manuel Klimekb95f5452013-02-08 17:38:27 +00001350 if (IndentForLevel[Level] != -1)
1351 return IndentForLevel[Level];
Manuel Klimekd076dcd2013-02-08 19:53:32 +00001352 if (Level == 0)
1353 return 0;
Manuel Klimek13b97d82013-05-13 08:42:42 +00001354 return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
Manuel Klimekb95f5452013-02-08 17:38:27 +00001355 }
1356
1357 /// \brief Get the offset of the line relatively to the level.
1358 ///
1359 /// For example, 'public:' labels in classes are offset by 1 or 2
1360 /// characters to the left from their level.
Daniel Jasper24570102013-02-14 09:58:41 +00001361 int getIndentOffset(const AnnotatedToken &RootToken) {
Alexander Kornienkofd433362013-03-27 17:08:02 +00001362 if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
Manuel Klimekb95f5452013-02-08 17:38:27 +00001363 return Style.AccessModifierOffset;
1364 return 0;
1365 }
1366
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001367 /// \brief Tries to merge lines into one.
1368 ///
1369 /// This will change \c Line and \c AnnotatedLine to contain the merged line,
1370 /// if possible; note that \c I will be incremented when lines are merged.
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001371 void tryFitMultipleLinesInOne(unsigned Indent,
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001372 std::vector<AnnotatedLine>::iterator &I,
1373 std::vector<AnnotatedLine>::iterator E) {
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001374 // We can never merge stuff if there are trailing line comments.
1375 if (I->Last->Type == TT_LineComment)
1376 return;
1377
Daniel Jasperc22f5b42013-02-28 11:05:57 +00001378 unsigned Limit = Style.ColumnLimit - Indent;
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001379 // If we already exceed the column limit, we set 'Limit' to 0. The different
1380 // tryMerge..() functions can then decide whether to still do merging.
1381 Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
Daniel Jasperc36492b2013-01-16 07:02:34 +00001382
Daniel Jasperd41ee2d2013-01-21 14:18:28 +00001383 if (I + 1 == E || (I + 1)->Type == LT_Invalid)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001384 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001385
Daniel Jasperabca58c2013-05-15 14:09:55 +00001386 if (I->Last->is(tok::l_brace)) {
Daniel Jasper25837aa2013-01-14 14:14:23 +00001387 tryMergeSimpleBlock(I, E, Limit);
Daniel Jasper3a685df2013-05-16 12:12:21 +00001388 } else if (Style.AllowShortIfStatementsOnASingleLine &&
1389 I->First.is(tok::kw_if)) {
1390 tryMergeSimpleControlStatement(I, E, Limit);
1391 } else if (Style.AllowShortLoopsOnASingleLine &&
1392 I->First.isOneOf(tok::kw_for, tok::kw_while)) {
1393 tryMergeSimpleControlStatement(I, E, Limit);
Daniel Jasper39825ea2013-01-14 15:40:57 +00001394 } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline ||
1395 I->First.FormatTok.IsFirst)) {
1396 tryMergeSimplePPDirective(I, E, Limit);
Daniel Jasper25837aa2013-01-14 14:14:23 +00001397 }
Daniel Jasper25837aa2013-01-14 14:14:23 +00001398 }
1399
Daniel Jasper39825ea2013-01-14 15:40:57 +00001400 void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
1401 std::vector<AnnotatedLine>::iterator E,
1402 unsigned Limit) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001403 if (Limit == 0)
1404 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001405 AnnotatedLine &Line = *I;
Daniel Jasper2ab0d012013-01-14 15:52:06 +00001406 if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline)
1407 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001408 if (I + 2 != E && (I + 2)->InPPDirective &&
1409 !(I + 2)->First.FormatTok.HasUnescapedNewline)
1410 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001411 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001412 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001413 join(Line, *(++I));
1414 }
1415
Daniel Jasper3a685df2013-05-16 12:12:21 +00001416 void tryMergeSimpleControlStatement(std::vector<AnnotatedLine>::iterator &I,
1417 std::vector<AnnotatedLine>::iterator E,
1418 unsigned Limit) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001419 if (Limit == 0)
1420 return;
Manuel Klimekda087612013-01-18 14:46:43 +00001421 if ((I + 1)->InPPDirective != I->InPPDirective ||
1422 ((I + 1)->InPPDirective &&
1423 (I + 1)->First.FormatTok.HasUnescapedNewline))
1424 return;
Daniel Jasper25837aa2013-01-14 14:14:23 +00001425 AnnotatedLine &Line = *I;
Daniel Jasperc36492b2013-01-16 07:02:34 +00001426 if (Line.Last->isNot(tok::r_paren))
1427 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001428 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jasper25837aa2013-01-14 14:14:23 +00001429 return;
Daniel Jasper3a685df2013-05-16 12:12:21 +00001430 if ((I + 1)->First.isOneOf(tok::semi, tok::kw_if, tok::kw_for,
1431 tok::kw_while) ||
1432 (I + 1)->First.Type == TT_LineComment)
Daniel Jasper25837aa2013-01-14 14:14:23 +00001433 return;
1434 // Only inline simple if's (no nested if or else).
Daniel Jasper3a685df2013-05-16 12:12:21 +00001435 if (I + 2 != E && Line.First.is(tok::kw_if) &&
1436 (I + 2)->First.is(tok::kw_else))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001437 return;
1438 join(Line, *(++I));
1439 }
1440
1441 void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
Daniel Jasperbbc84152013-01-29 11:27:30 +00001442 std::vector<AnnotatedLine>::iterator E,
1443 unsigned Limit) {
Daniel Jasperabca58c2013-05-15 14:09:55 +00001444 // No merging if the brace already is on the next line.
1445 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
1446 return;
1447
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001448 // First, check that the current line allows merging. This is the case if
1449 // we're not in a control flow statement and the last token is an opening
1450 // brace.
Daniel Jasper25837aa2013-01-14 14:14:23 +00001451 AnnotatedLine &Line = *I;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001452 if (Line.First.isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
1453 tok::kw_else, tok::kw_try, tok::kw_catch,
Daniel Jasperabca58c2013-05-15 14:09:55 +00001454 tok::kw_for, tok::kw_namespace,
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001455 // This gets rid of all ObjC @ keywords and methods.
1456 tok::at, tok::minus, tok::plus))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001457 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001458
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001459 AnnotatedToken *Tok = &(I + 1)->First;
Daniel Jasperf9eb9b12013-05-16 10:17:39 +00001460 if (Tok->getNextNoneComment() == NULL && Tok->is(tok::r_brace) &&
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001461 !Tok->MustBreakBefore) {
1462 // We merge empty blocks even if the line exceeds the column limit.
Daniel Jaspereef30492013-02-11 12:36:37 +00001463 Tok->SpacesRequiredBefore = 0;
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001464 Tok->CanBreakBefore = true;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001465 join(Line, *(I + 1));
1466 I += 1;
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001467 } else if (Limit != 0) {
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001468 // Check that we still have three lines and they fit into the limit.
1469 if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
1470 !nextTwoLinesFitInto(I, Limit))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001471 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001472
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001473 // Second, check that the next line does not contain any braces - if it
1474 // does, readability declines when putting it into a single line.
1475 if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
1476 return;
1477 do {
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001478 if (Tok->isOneOf(tok::l_brace, tok::r_brace))
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001479 return;
1480 Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
1481 } while (Tok != NULL);
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001482
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001483 // Last, check that the third line contains a single closing brace.
1484 Tok = &(I + 2)->First;
Daniel Jasperf9eb9b12013-05-16 10:17:39 +00001485 if (Tok->getNextNoneComment() != NULL || Tok->isNot(tok::r_brace) ||
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001486 Tok->MustBreakBefore)
1487 return;
1488
1489 join(Line, *(I + 1));
1490 join(Line, *(I + 2));
1491 I += 2;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001492 }
Daniel Jasper25837aa2013-01-14 14:14:23 +00001493 }
1494
1495 bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
1496 unsigned Limit) {
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001497 return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
1498 Limit;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001499 }
1500
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001501 void join(AnnotatedLine &A, const AnnotatedLine &B) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001502 unsigned LengthA = A.Last->TotalLength + B.First.SpacesRequiredBefore;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001503 A.Last->Children.push_back(B.First);
1504 while (!A.Last->Children.empty()) {
1505 A.Last->Children[0].Parent = A.Last;
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001506 A.Last->Children[0].TotalLength += LengthA;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001507 A.Last = &A.Last->Children[0];
1508 }
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001509 }
1510
Daniel Jasper97b89482013-03-13 07:49:51 +00001511 bool touchesRanges(const CharSourceRange &Range) {
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001512 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1513 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
1514 Ranges[i].getBegin()) &&
1515 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1516 Range.getBegin()))
1517 return true;
1518 }
1519 return false;
1520 }
1521
1522 bool touchesLine(const AnnotatedLine &TheLine) {
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001523 const FormatToken *First = &TheLine.First.FormatTok;
1524 const FormatToken *Last = &TheLine.Last->FormatTok;
Daniel Jaspercdd06622013-05-14 10:31:09 +00001525 CharSourceRange LineRange = CharSourceRange::getCharRange(
Daniel Jasper973c9422013-03-04 13:43:19 +00001526 First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset),
Daniel Jaspercdd06622013-05-14 10:31:09 +00001527 Last->Tok.getLocation().getLocWithOffset(Last->TokenLength - 1));
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001528 return touchesRanges(LineRange);
1529 }
1530
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001531 bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I,
1532 std::vector<AnnotatedLine>::iterator E) {
1533 for (; I != E; ++I) {
1534 if (I->First.FormatTok.HasUnescapedNewline)
1535 return false;
1536 if (touchesLine(*I))
1537 return true;
1538 }
1539 return false;
1540 }
1541
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001542 bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
1543 const FormatToken *First = &TheLine.First.FormatTok;
1544 CharSourceRange LineRange = CharSourceRange::getCharRange(
1545 First->WhiteSpaceStart,
1546 First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset));
1547 return touchesRanges(LineRange);
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001548 }
1549
1550 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001551 AnnotatedLines.push_back(AnnotatedLine(TheLine));
Daniel Jasperf7935112012-12-03 18:12:45 +00001552 }
1553
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001554 /// \brief Add a new line and the required indent before the first Token
1555 /// of the \c UnwrappedLine if there was no structural parsing error.
1556 /// Returns the indent level of the \c UnwrappedLine.
Alexander Kornienkofd433362013-03-27 17:08:02 +00001557 void formatFirstToken(const AnnotatedToken &RootToken,
1558 const AnnotatedToken *PreviousToken, unsigned Indent,
Manuel Klimekb95f5452013-02-08 17:38:27 +00001559 bool InPPDirective, unsigned PreviousEndOfLineColumn) {
Daniel Jasperfd8c4b12013-01-11 14:23:32 +00001560 const FormatToken &Tok = RootToken.FormatTok;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001561
Daniel Jasperbbc84152013-01-29 11:27:30 +00001562 unsigned Newlines =
1563 std::min(Tok.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001564 if (Newlines == 0 && !Tok.IsFirst)
1565 Newlines = 1;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001566
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001567 if (!InPPDirective || Tok.HasUnescapedNewline) {
Alexander Kornienkofd433362013-03-27 17:08:02 +00001568 // Insert extra new line before access specifiers.
1569 if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) &&
1570 RootToken.isAccessSpecifier() && Tok.NewlinesBefore == 1)
1571 ++Newlines;
1572
Alexander Kornienkoafcef332013-03-19 17:41:36 +00001573 Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001574 } else {
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001575 Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent,
Alexander Kornienkoafcef332013-03-19 17:41:36 +00001576 PreviousEndOfLineColumn);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001577 }
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001578 }
1579
Daniel Jasperf7935112012-12-03 18:12:45 +00001580 FormatStyle Style;
1581 Lexer &Lex;
1582 SourceManager &SourceMgr;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001583 WhitespaceManager Whitespaces;
Daniel Jasperf7935112012-12-03 18:12:45 +00001584 std::vector<CharSourceRange> Ranges;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001585 std::vector<AnnotatedLine> AnnotatedLines;
Daniel Jasperf7935112012-12-03 18:12:45 +00001586};
1587
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00001588tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
1589 SourceManager &SourceMgr,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +00001590 std::vector<CharSourceRange> Ranges) {
1591 Formatter formatter(Style, Lex, SourceMgr, Ranges);
Daniel Jasperf7935112012-12-03 18:12:45 +00001592 return formatter.format();
1593}
1594
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001595tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
1596 std::vector<tooling::Range> Ranges,
1597 StringRef FileName) {
1598 FileManager Files((FileSystemOptions()));
1599 DiagnosticsEngine Diagnostics(
1600 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1601 new DiagnosticOptions);
1602 SourceManager SourceMgr(Diagnostics, Files);
1603 llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName);
1604 const clang::FileEntry *Entry =
1605 Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
1606 SourceMgr.overrideFileContents(Entry, Buf);
1607 FileID ID =
1608 SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
1609 Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr, getFormattingLangOpts());
1610 SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
1611 std::vector<CharSourceRange> CharRanges;
1612 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1613 SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset());
1614 SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength());
1615 CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1616 }
1617 return reformat(Style, Lex, SourceMgr, CharRanges);
1618}
1619
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001620LangOptions getFormattingLangOpts() {
1621 LangOptions LangOpts;
1622 LangOpts.CPlusPlus = 1;
1623 LangOpts.CPlusPlus11 = 1;
Daniel Jasper55213652013-03-22 10:01:29 +00001624 LangOpts.LineComment = 1;
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001625 LangOpts.Bool = 1;
1626 LangOpts.ObjC1 = 1;
1627 LangOpts.ObjC2 = 1;
1628 return LangOpts;
1629}
1630
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001631} // namespace format
1632} // namespace clang