blob: e4004a649b08557465b1c8cf8086a4733702e7a4 [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);
Daniel Jasper61e6bbf2013-05-29 12:07:31 +000088 IO.mapOptional("AlwaysBreakTemplateDeclarations",
89 Style.AlwaysBreakTemplateDeclarations);
Alexander Kornienko58611712013-07-04 12:02:44 +000090 IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
91 Style.AlwaysBreakBeforeMultilineStrings);
Alexander Kornienkod6538332013-05-07 15:32:14 +000092 IO.mapOptional("BinPackParameters", Style.BinPackParameters);
93 IO.mapOptional("ColumnLimit", Style.ColumnLimit);
94 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
95 Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
96 IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding);
97 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
98 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
99 IO.mapOptional("ObjCSpaceBeforeProtocolList",
100 Style.ObjCSpaceBeforeProtocolList);
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000101 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
102 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000103 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
104 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
105 Style.PenaltyReturnTypeOnItsOwnLine);
106 IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
107 IO.mapOptional("SpacesBeforeTrailingComments",
108 Style.SpacesBeforeTrailingComments);
Daniel Jasper5bd0b9e2013-05-23 18:05:18 +0000109 IO.mapOptional("SpacesInBracedLists", Style.SpacesInBracedLists);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000110 IO.mapOptional("Standard", Style.Standard);
Manuel Klimek13b97d82013-05-13 08:42:42 +0000111 IO.mapOptional("IndentWidth", Style.IndentWidth);
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000112 IO.mapOptional("UseTab", Style.UseTab);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000113 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
Manuel Klimek836c2862013-06-21 17:25:42 +0000114 IO.mapOptional("IndentFunctionDeclarationAfterType",
115 Style.IndentFunctionDeclarationAfterType);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000116 }
117};
118}
119}
120
Daniel Jasperf7935112012-12-03 18:12:45 +0000121namespace clang {
122namespace format {
123
Daniel Jasperf7935112012-12-03 18:12:45 +0000124FormatStyle getLLVMStyle() {
125 FormatStyle LLVMStyle;
Daniel Jasperf7935112012-12-03 18:12:45 +0000126 LLVMStyle.AccessModifierOffset = -2;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000127 LLVMStyle.AlignEscapedNewlinesLeft = false;
Daniel Jasperf7db4332013-01-29 16:03:49 +0000128 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000129 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000130 LLVMStyle.AllowShortLoopsOnASingleLine = false;
Daniel Jasper61e6bbf2013-05-29 12:07:31 +0000131 LLVMStyle.AlwaysBreakTemplateDeclarations = false;
Alexander Kornienko58611712013-07-04 12:02:44 +0000132 LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000133 LLVMStyle.BinPackParameters = true;
134 LLVMStyle.ColumnLimit = 80;
135 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
136 LLVMStyle.DerivePointerBinding = false;
137 LLVMStyle.IndentCaseLabels = false;
138 LLVMStyle.MaxEmptyLinesToKeep = 1;
Nico Webera6087752013-01-10 20:12:55 +0000139 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000140 LLVMStyle.PenaltyBreakComment = 45;
141 LLVMStyle.PenaltyBreakString = 1000;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000142 LLVMStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasperee7539a2013-07-08 14:25:23 +0000143 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000144 LLVMStyle.PointerBindsToType = false;
145 LLVMStyle.SpacesBeforeTrailingComments = 1;
Daniel Jaspere5777d22013-05-23 10:15:45 +0000146 LLVMStyle.SpacesInBracedLists = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000147 LLVMStyle.Standard = FormatStyle::LS_Cpp03;
Manuel Klimek13b97d82013-05-13 08:42:42 +0000148 LLVMStyle.IndentWidth = 2;
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000149 LLVMStyle.UseTab = false;
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000150 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
Manuel Klimek836c2862013-06-21 17:25:42 +0000151 LLVMStyle.IndentFunctionDeclarationAfterType = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000152 return LLVMStyle;
153}
154
155FormatStyle getGoogleStyle() {
156 FormatStyle GoogleStyle;
Daniel Jasperf7935112012-12-03 18:12:45 +0000157 GoogleStyle.AccessModifierOffset = -1;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000158 GoogleStyle.AlignEscapedNewlinesLeft = true;
Daniel Jasperf7db4332013-01-29 16:03:49 +0000159 GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper085a2ed2013-04-24 13:46:00 +0000160 GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
Daniel Jasper5bd0b9e2013-05-23 18:05:18 +0000161 GoogleStyle.AllowShortLoopsOnASingleLine = true;
Daniel Jasper61e6bbf2013-05-29 12:07:31 +0000162 GoogleStyle.AlwaysBreakTemplateDeclarations = true;
Alexander Kornienko58611712013-07-04 12:02:44 +0000163 GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000164 GoogleStyle.BinPackParameters = true;
165 GoogleStyle.ColumnLimit = 80;
166 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
167 GoogleStyle.DerivePointerBinding = true;
168 GoogleStyle.IndentCaseLabels = true;
169 GoogleStyle.MaxEmptyLinesToKeep = 1;
Nico Webera6087752013-01-10 20:12:55 +0000170 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000171 GoogleStyle.PenaltyBreakComment = 45;
172 GoogleStyle.PenaltyBreakString = 1000;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000173 GoogleStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasper6728fc12013-04-11 14:29:13 +0000174 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000175 GoogleStyle.PointerBindsToType = true;
176 GoogleStyle.SpacesBeforeTrailingComments = 2;
Daniel Jaspere5777d22013-05-23 10:15:45 +0000177 GoogleStyle.SpacesInBracedLists = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000178 GoogleStyle.Standard = FormatStyle::LS_Auto;
Manuel Klimek13b97d82013-05-13 08:42:42 +0000179 GoogleStyle.IndentWidth = 2;
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000180 GoogleStyle.UseTab = false;
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000181 GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
Manuel Klimek836c2862013-06-21 17:25:42 +0000182 GoogleStyle.IndentFunctionDeclarationAfterType = true;
Daniel Jasperf7935112012-12-03 18:12:45 +0000183 return GoogleStyle;
184}
185
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000186FormatStyle getChromiumStyle() {
187 FormatStyle ChromiumStyle = getGoogleStyle();
Daniel Jasperf7db4332013-01-29 16:03:49 +0000188 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Daniel Jasper085a2ed2013-04-24 13:46:00 +0000189 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000190 ChromiumStyle.AllowShortLoopsOnASingleLine = false;
Daniel Jasper2cf17bf2013-02-27 09:47:53 +0000191 ChromiumStyle.BinPackParameters = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000192 ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
193 ChromiumStyle.DerivePointerBinding = false;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000194 return ChromiumStyle;
195}
196
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000197FormatStyle getMozillaStyle() {
198 FormatStyle MozillaStyle = getLLVMStyle();
199 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
200 MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
201 MozillaStyle.DerivePointerBinding = true;
202 MozillaStyle.IndentCaseLabels = true;
203 MozillaStyle.ObjCSpaceBeforeProtocolList = false;
204 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
205 MozillaStyle.PointerBindsToType = true;
206 return MozillaStyle;
207}
208
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000209bool getPredefinedStyle(StringRef Name, FormatStyle *Style) {
Alexander Kornienkod6538332013-05-07 15:32:14 +0000210 if (Name.equals_lower("llvm"))
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000211 *Style = getLLVMStyle();
212 else if (Name.equals_lower("chromium"))
213 *Style = getChromiumStyle();
214 else if (Name.equals_lower("mozilla"))
215 *Style = getMozillaStyle();
216 else if (Name.equals_lower("google"))
217 *Style = getGoogleStyle();
218 else
219 return false;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000220
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000221 return true;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000222}
223
224llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
Alexander Kornienko06e00332013-05-20 15:18:01 +0000225 if (Text.trim().empty())
226 return llvm::make_error_code(llvm::errc::invalid_argument);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000227 llvm::yaml::Input Input(Text);
228 Input >> *Style;
229 return Input.error();
230}
231
232std::string configurationAsText(const FormatStyle &Style) {
233 std::string Text;
234 llvm::raw_string_ostream Stream(Text);
235 llvm::yaml::Output Output(Stream);
236 // We use the same mapping method for input and output, so we need a non-const
237 // reference here.
238 FormatStyle NonConstStyle = Style;
239 Output << NonConstStyle;
Alexander Kornienko9a38ec22013-05-13 12:56:35 +0000240 return Stream.str();
Alexander Kornienkod6538332013-05-07 15:32:14 +0000241}
242
Daniel Jasperacc33662013-02-08 08:22:00 +0000243// Returns the length of everything up to the first possible line break after
244// the ), ], } or > matching \c Tok.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000245static unsigned getLengthToMatchingParen(const FormatToken &Tok) {
Daniel Jasperacc33662013-02-08 08:22:00 +0000246 if (Tok.MatchingParen == NULL)
247 return 0;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000248 FormatToken *End = Tok.MatchingParen;
249 while (End->Next && !End->Next->CanBreakBefore) {
250 End = End->Next;
Daniel Jasperacc33662013-02-08 08:22:00 +0000251 }
252 return End->TotalLength - Tok.TotalLength + 1;
253}
254
Craig Topperaf35e852013-06-30 22:29:28 +0000255namespace {
256
Daniel Jasperf7935112012-12-03 18:12:45 +0000257class UnwrappedLineFormatter {
258public:
Manuel Klimekb2c6dbe2013-01-10 19:17:33 +0000259 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000260 const AnnotatedLine &Line, unsigned FirstIndent,
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000261 const FormatToken *RootToken,
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000262 WhitespaceManager &Whitespaces,
263 encoding::Encoding Encoding)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000264 : Style(Style), SourceMgr(SourceMgr), Line(Line),
Daniel Jasperaa701fa2013-01-18 08:44:07 +0000265 FirstIndent(FirstIndent), RootToken(RootToken),
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000266 Whitespaces(Whitespaces), Count(0), Encoding(Encoding) {}
Daniel Jasperf7935112012-12-03 18:12:45 +0000267
Manuel Klimek1abf7892013-01-04 23:34:14 +0000268 /// \brief Formats an \c UnwrappedLine.
Manuel Klimek4fe43002013-05-22 12:51:29 +0000269 void format(const AnnotatedLine *NextLine) {
Daniel Jaspere9de2602012-12-06 09:56:08 +0000270 // Initialize state dependent on indent.
Daniel Jasper337816e2013-01-11 10:22:12 +0000271 LineState State;
Manuel Klimek0b689fd2013-01-10 18:45:26 +0000272 State.Column = FirstIndent;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000273 State.NextToken = RootToken;
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000274 State.Stack.push_back(ParenState(FirstIndent, FirstIndent,
275 /*AvoidBinPacking=*/false,
276 /*NoLineBreak=*/false));
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000277 State.LineContainsContinuedForLoopSection = false;
Daniel Jasper400adc62013-02-08 15:28:42 +0000278 State.ParenLevel = 0;
Manuel Klimek02f640a2013-02-20 15:25:48 +0000279 State.StartOfStringLiteral = 0;
Daniel Jasper40c36c52013-02-18 11:05:07 +0000280 State.StartOfLineLevel = State.ParenLevel;
Daniel Jasper0e90c3d2013-07-05 09:14:35 +0000281 State.LowestLevelOnLine = State.ParenLevel;
Daniel Jasperf8114cf2013-05-22 05:27:42 +0000282 State.IgnoreStackForComparison = false;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000283
284 // The first token has already been indented and thus consumed.
Nico Weber9096fc02013-06-26 00:30:14 +0000285 moveStateToNextToken(State, /*DryRun=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000286
Daniel Jasper4b866272013-02-01 11:00:45 +0000287 // If everything fits on a single line, just put it there.
Daniel Jasperc22f5b42013-02-28 11:05:57 +0000288 unsigned ColumnLimit = Style.ColumnLimit;
289 if (NextLine && NextLine->InPPDirective &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000290 !NextLine->First->HasUnescapedNewline)
Daniel Jasperc22f5b42013-02-28 11:05:57 +0000291 ColumnLimit = getColumnLimit();
292 if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) {
Daniel Jasper4b866272013-02-01 11:00:45 +0000293 while (State.NextToken != NULL) {
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000294 addTokenToState(false, false, State);
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000295 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000296 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000297
Daniel Jasperacc33662013-02-08 08:22:00 +0000298 // If the ObjC method declaration does not fit on a line, we should format
299 // it with one arg per line.
300 if (Line.Type == LT_ObjCMethodDecl)
301 State.Stack.back().BreakBeforeParameter = true;
302
Daniel Jasper4b866272013-02-01 11:00:45 +0000303 // Find best solution in solution space.
Manuel Klimek4fe43002013-05-22 12:51:29 +0000304 analyzeSolutionSpace(State);
Daniel Jasperf7935112012-12-03 18:12:45 +0000305 }
306
307private:
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000308 void DebugTokenState(const FormatToken &FormatTok) {
309 const Token &Tok = FormatTok.Tok;
Alexander Kornienko49149672013-05-10 11:56:10 +0000310 llvm::dbgs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
Daniel Jasperbbc84152013-01-29 11:27:30 +0000311 Tok.getLength());
Alexander Kornienko49149672013-05-10 11:56:10 +0000312 llvm::dbgs();
Manuel Klimek24998102013-01-16 14:55:28 +0000313 }
314
Daniel Jasper337816e2013-01-11 10:22:12 +0000315 struct ParenState {
Daniel Jasperb9ebd5d2013-02-05 09:41:21 +0000316 ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking,
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000317 bool NoLineBreak)
Daniel Jasper400adc62013-02-08 15:28:42 +0000318 : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
319 BreakBeforeClosingBrace(false), QuestionColumn(0),
Daniel Jasperacc33662013-02-08 08:22:00 +0000320 AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000321 NoLineBreak(NoLineBreak), ColonPos(0), StartOfFunctionCall(0),
322 NestedNameSpecifierContinuation(0), CallContinuation(0),
Daniel Jasperee7539a2013-07-08 14:25:23 +0000323 VariablePos(0), ContainsLineBreak(false) {}
Daniel Jasper6d822722012-12-24 16:43:00 +0000324
Daniel Jasperf7935112012-12-03 18:12:45 +0000325 /// \brief The position to which a specific parenthesis level needs to be
326 /// indented.
Daniel Jasper337816e2013-01-11 10:22:12 +0000327 unsigned Indent;
Daniel Jasperf7935112012-12-03 18:12:45 +0000328
Daniel Jaspere9de2602012-12-06 09:56:08 +0000329 /// \brief The position of the last space on each level.
330 ///
331 /// Used e.g. to break like:
332 /// functionCall(Parameter, otherCall(
333 /// OtherParameter));
Daniel Jasper337816e2013-01-11 10:22:12 +0000334 unsigned LastSpace;
Daniel Jasperf7935112012-12-03 18:12:45 +0000335
Daniel Jaspere9de2602012-12-06 09:56:08 +0000336 /// \brief The position the first "<<" operator encountered on each level.
337 ///
338 /// Used to align "<<" operators. 0 if no such operator has been encountered
339 /// on a level.
Daniel Jasper337816e2013-01-11 10:22:12 +0000340 unsigned FirstLessLess;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000341
Manuel Klimek0ddd57a2013-01-10 15:58:26 +0000342 /// \brief Whether a newline needs to be inserted before the block's closing
343 /// brace.
344 ///
345 /// We only want to insert a newline before the closing brace if there also
346 /// was a newline after the beginning left brace.
Daniel Jasper337816e2013-01-11 10:22:12 +0000347 bool BreakBeforeClosingBrace;
348
Daniel Jasperca6623b2013-01-28 12:45:14 +0000349 /// \brief The column of a \c ? in a conditional expression;
350 unsigned QuestionColumn;
351
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000352 /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple
353 /// lines, in this context.
354 bool AvoidBinPacking;
355
356 /// \brief Break after the next comma (or all the commas in this context if
357 /// \c AvoidBinPacking is \c true).
Daniel Jasperacc33662013-02-08 08:22:00 +0000358 bool BreakBeforeParameter;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000359
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000360 /// \brief Line breaking in this context would break a formatting rule.
361 bool NoLineBreak;
Daniel Jasper2408a8c2013-01-11 11:37:55 +0000362
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000363 /// \brief The position of the colon in an ObjC method declaration/call.
364 unsigned ColonPos;
Daniel Jasperdc7d5812013-02-20 12:56:39 +0000365
Daniel Jasperf9a84b52013-03-01 16:48:32 +0000366 /// \brief The start of the most recent function in a builder-type call.
367 unsigned StartOfFunctionCall;
368
Daniel Jasperc238c872013-04-02 14:33:13 +0000369 /// \brief If a nested name specifier was broken over multiple lines, this
370 /// contains the start column of the second line. Otherwise 0.
371 unsigned NestedNameSpecifierContinuation;
372
373 /// \brief If a call expression was broken over multiple lines, this
374 /// contains the start column of the second line. Otherwise 0.
375 unsigned CallContinuation;
376
Daniel Jaspera628c982013-04-03 13:36:17 +0000377 /// \brief The column of the first variable name in a variable declaration.
378 ///
379 /// Used to align further variables if necessary.
380 unsigned VariablePos;
381
Daniel Jasperee7539a2013-07-08 14:25:23 +0000382 /// \brief \c true if this \c ParenState already contains a line-break.
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000383 ///
Daniel Jasperee7539a2013-07-08 14:25:23 +0000384 /// The first line break in a certain \c ParenState causes extra penalty so
385 /// that clang-format prefers similar breaks, i.e. breaks in the same
386 /// parenthesis.
387 bool ContainsLineBreak;
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000388
Daniel Jasper337816e2013-01-11 10:22:12 +0000389 bool operator<(const ParenState &Other) const {
390 if (Indent != Other.Indent)
Daniel Jasperfd8c4b12013-01-11 14:23:32 +0000391 return Indent < Other.Indent;
Daniel Jasper337816e2013-01-11 10:22:12 +0000392 if (LastSpace != Other.LastSpace)
393 return LastSpace < Other.LastSpace;
394 if (FirstLessLess != Other.FirstLessLess)
395 return FirstLessLess < Other.FirstLessLess;
Daniel Jasper2408a8c2013-01-11 11:37:55 +0000396 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
397 return BreakBeforeClosingBrace;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000398 if (QuestionColumn != Other.QuestionColumn)
399 return QuestionColumn < Other.QuestionColumn;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000400 if (AvoidBinPacking != Other.AvoidBinPacking)
401 return AvoidBinPacking;
Daniel Jasperacc33662013-02-08 08:22:00 +0000402 if (BreakBeforeParameter != Other.BreakBeforeParameter)
403 return BreakBeforeParameter;
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000404 if (NoLineBreak != Other.NoLineBreak)
405 return NoLineBreak;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000406 if (ColonPos != Other.ColonPos)
407 return ColonPos < Other.ColonPos;
Daniel Jasperf9a84b52013-03-01 16:48:32 +0000408 if (StartOfFunctionCall != Other.StartOfFunctionCall)
409 return StartOfFunctionCall < Other.StartOfFunctionCall;
Daniel Jasperc238c872013-04-02 14:33:13 +0000410 if (CallContinuation != Other.CallContinuation)
411 return CallContinuation < Other.CallContinuation;
Daniel Jaspera628c982013-04-03 13:36:17 +0000412 if (VariablePos != Other.VariablePos)
413 return VariablePos < Other.VariablePos;
Daniel Jasperee7539a2013-07-08 14:25:23 +0000414 if (ContainsLineBreak != Other.ContainsLineBreak)
415 return ContainsLineBreak < Other.ContainsLineBreak;
Daniel Jasper7b7877a2013-01-12 07:36:22 +0000416 return false;
Daniel Jasper337816e2013-01-11 10:22:12 +0000417 }
418 };
419
420 /// \brief The current state when indenting a unwrapped line.
421 ///
422 /// As the indenting tries different combinations this is copied by value.
423 struct LineState {
424 /// \brief The number of used columns in the current line.
425 unsigned Column;
426
427 /// \brief The token that needs to be next formatted.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000428 const FormatToken *NextToken;
Daniel Jasper337816e2013-01-11 10:22:12 +0000429
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000430 /// \brief \c true if this line contains a continued for-loop section.
431 bool LineContainsContinuedForLoopSection;
432
Daniel Jasper400adc62013-02-08 15:28:42 +0000433 /// \brief The level of nesting inside (), [], <> and {}.
434 unsigned ParenLevel;
435
Daniel Jasper40c36c52013-02-18 11:05:07 +0000436 /// \brief The \c ParenLevel at the start of this line.
437 unsigned StartOfLineLevel;
438
Daniel Jasper0e90c3d2013-07-05 09:14:35 +0000439 /// \brief The lowest \c ParenLevel on the current line.
440 unsigned LowestLevelOnLine;
Daniel Jasper32a796b2013-05-27 11:50:16 +0000441
Manuel Klimek02f640a2013-02-20 15:25:48 +0000442 /// \brief The start column of the string literal, if we're in a string
443 /// literal sequence, 0 otherwise.
444 unsigned StartOfStringLiteral;
445
Daniel Jasper337816e2013-01-11 10:22:12 +0000446 /// \brief A stack keeping track of properties applying to parenthesis
447 /// levels.
448 std::vector<ParenState> Stack;
449
Daniel Jasperf8114cf2013-05-22 05:27:42 +0000450 /// \brief Ignore the stack of \c ParenStates for state comparison.
451 ///
452 /// In long and deeply nested unwrapped lines, the current algorithm can
453 /// be insufficient for finding the best formatting with a reasonable amount
454 /// of time and memory. Setting this flag will effectively lead to the
455 /// algorithm not analyzing some combinations. However, these combinations
456 /// rarely contain the optimal solution: In short, accepting a higher
457 /// penalty early would need to lead to different values in the \c
458 /// ParenState stack (in an otherwise identical state) and these different
459 /// values would need to lead to a significant amount of avoided penalty
460 /// later.
461 ///
462 /// FIXME: Come up with a better algorithm instead.
463 bool IgnoreStackForComparison;
464
Daniel Jasper337816e2013-01-11 10:22:12 +0000465 /// \brief Comparison operator to be able to used \c LineState in \c map.
466 bool operator<(const LineState &Other) const {
Daniel Jasper58f427e2013-02-19 09:28:55 +0000467 if (NextToken != Other.NextToken)
468 return NextToken < Other.NextToken;
469 if (Column != Other.Column)
470 return Column < Other.Column;
Daniel Jasper58f427e2013-02-19 09:28:55 +0000471 if (LineContainsContinuedForLoopSection !=
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000472 Other.LineContainsContinuedForLoopSection)
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000473 return LineContainsContinuedForLoopSection;
Daniel Jasper58f427e2013-02-19 09:28:55 +0000474 if (ParenLevel != Other.ParenLevel)
475 return ParenLevel < Other.ParenLevel;
476 if (StartOfLineLevel != Other.StartOfLineLevel)
477 return StartOfLineLevel < Other.StartOfLineLevel;
Daniel Jasper0e90c3d2013-07-05 09:14:35 +0000478 if (LowestLevelOnLine != Other.LowestLevelOnLine)
479 return LowestLevelOnLine < Other.LowestLevelOnLine;
Manuel Klimek02f640a2013-02-20 15:25:48 +0000480 if (StartOfStringLiteral != Other.StartOfStringLiteral)
481 return StartOfStringLiteral < Other.StartOfStringLiteral;
Daniel Jasperf8114cf2013-05-22 05:27:42 +0000482 if (IgnoreStackForComparison || Other.IgnoreStackForComparison)
483 return false;
Daniel Jasper58f427e2013-02-19 09:28:55 +0000484 return Stack < Other.Stack;
Daniel Jasperf7935112012-12-03 18:12:45 +0000485 }
486 };
487
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000488 /// \brief Appends the next token to \p State and updates information
489 /// necessary for indentation.
490 ///
Nico Weberf579ab32013-06-26 02:42:46 +0000491 /// Puts the token on the current line if \p Newline is \c false and adds a
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000492 /// line break and necessary indentation otherwise.
493 ///
494 /// If \p DryRun is \c false, also creates and stores the required
495 /// \c Replacement.
Manuel Klimek1998ea22013-02-20 10:15:13 +0000496 unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000497 const FormatToken &Current = *State.NextToken;
498 const FormatToken &Previous = *State.NextToken->Previous;
Daniel Jasperf7935112012-12-03 18:12:45 +0000499
Daniel Jasper291f9362013-03-20 15:58:10 +0000500 if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
Manuel Klimek5c24cca2013-05-23 10:56:37 +0000501 // FIXME: Is this correct?
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000502 int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
503 State.NextToken->WhitespaceRange.getEnd()) -
504 SourceMgr.getSpellingColumnNumber(
505 State.NextToken->WhitespaceRange.getBegin());
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000506 State.Column += WhitespaceLength + State.NextToken->CodePointCount;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000507 State.NextToken = State.NextToken->Next;
Manuel Klimek1998ea22013-02-20 10:15:13 +0000508 return 0;
Daniel Jasper4b866272013-02-01 11:00:45 +0000509 }
510
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000511 // If we are continuing an expression, we want to indent an extra 4 spaces.
512 unsigned ContinuationIndent =
Daniel Jasperc238c872013-04-02 14:33:13 +0000513 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4;
Daniel Jasperf7935112012-12-03 18:12:45 +0000514 if (Newline) {
Daniel Jasperee7539a2013-07-08 14:25:23 +0000515 State.Stack.back().ContainsLineBreak = true;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000516 if (Current.is(tok::r_brace)) {
Manuel Klimek13b97d82013-05-13 08:42:42 +0000517 State.Column = Line.Level * Style.IndentWidth;
Daniel Jasper399d24b2013-01-09 07:06:56 +0000518 } else if (Current.is(tok::string_literal) &&
Manuel Klimek02f640a2013-02-20 15:25:48 +0000519 State.StartOfStringLiteral != 0) {
520 State.Column = State.StartOfStringLiteral;
Daniel Jasper2ec3ffb82013-02-18 11:59:17 +0000521 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasper399d24b2013-01-09 07:06:56 +0000522 } else if (Current.is(tok::lessless) &&
Daniel Jasper400adc62013-02-08 15:28:42 +0000523 State.Stack.back().FirstLessLess != 0) {
524 State.Column = State.Stack.back().FirstLessLess;
Daniel Jasperbca4bbe2013-05-28 11:30:49 +0000525 } else if (Current.isOneOf(tok::period, tok::arrow) &&
526 Current.Type != TT_DesignatedInitializerPeriod) {
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000527 if (State.Stack.back().CallContinuation == 0) {
528 State.Column = ContinuationIndent;
Daniel Jasperc238c872013-04-02 14:33:13 +0000529 State.Stack.back().CallContinuation = State.Column;
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000530 } else {
531 State.Column = State.Stack.back().CallContinuation;
532 }
Daniel Jasperca6623b2013-01-28 12:45:14 +0000533 } else if (Current.Type == TT_ConditionalExpr) {
534 State.Column = State.Stack.back().QuestionColumn;
Daniel Jaspera628c982013-04-03 13:36:17 +0000535 } else if (Previous.is(tok::comma) &&
536 State.Stack.back().VariablePos != 0) {
537 State.Column = State.Stack.back().VariablePos;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000538 } else if (Previous.ClosesTemplateDeclaration ||
Daniel Jasper8e357692013-05-06 08:27:33 +0000539 (Current.Type == TT_StartOfName && State.ParenLevel == 0 &&
Manuel Klimek836c2862013-06-21 17:25:42 +0000540 (!Style.IndentFunctionDeclarationAfterType ||
541 Line.StartsDefinition))) {
Daniel Jasperc238c872013-04-02 14:33:13 +0000542 State.Column = State.Stack.back().Indent;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000543 } else if (Current.Type == TT_ObjCSelectorName) {
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000544 if (State.Stack.back().ColonPos > Current.CodePointCount) {
545 State.Column = State.Stack.back().ColonPos - Current.CodePointCount;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000546 } else {
547 State.Column = State.Stack.back().Indent;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000548 State.Stack.back().ColonPos = State.Column + Current.CodePointCount;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000549 }
Daniel Jasper0f0234e2013-05-08 10:00:18 +0000550 } else if (Current.Type == TT_StartOfName ||
551 Previous.isOneOf(tok::coloncolon, tok::equal) ||
Daniel Jasperc238c872013-04-02 14:33:13 +0000552 Previous.Type == TT_ObjCMethodExpr) {
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000553 State.Column = ContinuationIndent;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000554 } else {
Daniel Jasper400adc62013-02-08 15:28:42 +0000555 State.Column = State.Stack.back().Indent;
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000556 // Ensure that we fall back to indenting 4 spaces instead of just
557 // flushing continuations left.
Daniel Jasperc238c872013-04-02 14:33:13 +0000558 if (State.Column == FirstIndent)
559 State.Column += 4;
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000560 }
561
Daniel Jasper54a86022013-02-15 11:07:25 +0000562 if (Current.is(tok::question))
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000563 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasperd69fc772013-05-08 14:12:04 +0000564 if ((Previous.isOneOf(tok::comma, tok::semi) &&
565 !State.Stack.back().AvoidBinPacking) ||
566 Previous.Type == TT_BinaryOperator)
Daniel Jasperacc33662013-02-08 08:22:00 +0000567 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasperc6fbc212013-05-15 09:35:08 +0000568 if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
569 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000570
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000571 if (!DryRun) {
Daniel Jasperfb5e2412013-02-26 13:10:34 +0000572 unsigned NewLines = 1;
Alexander Kornienkof370ad92013-06-12 19:04:12 +0000573 if (Current.is(tok::comment))
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000574 NewLines = std::max(
575 NewLines,
576 std::min(Current.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1));
Manuel Klimek4fe43002013-05-22 12:51:29 +0000577 Whitespaces.replaceWhitespace(Current, NewLines, State.Column,
578 State.Column, Line.InPPDirective);
Manuel Klimekb69e3c62013-01-02 18:33:23 +0000579 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000580
Daniel Jasper400adc62013-02-08 15:28:42 +0000581 State.Stack.back().LastSpace = State.Column;
Daniel Jasperbca4bbe2013-05-28 11:30:49 +0000582 if (Current.isOneOf(tok::arrow, tok::period) &&
583 Current.Type != TT_DesignatedInitializerPeriod)
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000584 State.Stack.back().LastSpace += Current.CodePointCount;
Daniel Jasper40c36c52013-02-18 11:05:07 +0000585 State.StartOfLineLevel = State.ParenLevel;
Daniel Jasper0e90c3d2013-07-05 09:14:35 +0000586 State.LowestLevelOnLine = State.ParenLevel;
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000587
588 // Any break on this level means that the parent level has been broken
589 // and we need to avoid bin packing there.
590 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
591 State.Stack[i].BreakBeforeParameter = true;
592 }
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000593 const FormatToken *TokenBefore = Current.getPreviousNonComment();
Daniel Jasper1b8e76f2013-04-15 22:36:37 +0000594 if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) &&
Daniel Jasperc6fbc212013-05-15 09:35:08 +0000595 TokenBefore->Type != TT_TemplateCloser &&
Daniel Jasperd69fc772013-05-08 14:12:04 +0000596 TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope())
Daniel Jasper2cf17bf2013-02-27 09:47:53 +0000597 State.Stack.back().BreakBeforeParameter = true;
598
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000599 // If we break after {, we should also break before the corresponding }.
600 if (Previous.is(tok::l_brace))
601 State.Stack.back().BreakBeforeClosingBrace = true;
602
603 if (State.Stack.back().AvoidBinPacking) {
604 // If we are breaking after '(', '{', '<', this is not bin packing
605 // unless AllowAllParametersOfDeclarationOnNextLine is false.
Daniel Jasper571f1af2013-05-14 20:39:56 +0000606 if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
607 Previous.Type == TT_BinaryOperator) ||
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000608 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
609 Line.MustBeDeclaration))
610 State.Stack.back().BreakBeforeParameter = true;
611 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000612 } else {
Daniel Jasper62e68172013-02-25 15:59:54 +0000613 if (Current.is(tok::equal) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000614 (RootToken->is(tok::kw_for) || State.ParenLevel == 0) &&
Daniel Jasper31c96b92013-04-05 09:38:50 +0000615 State.Stack.back().VariablePos == 0) {
616 State.Stack.back().VariablePos = State.Column;
617 // Move over * and & if they are bound to the variable name.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000618 const FormatToken *Tok = &Previous;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000619 while (Tok && State.Stack.back().VariablePos >= Tok->CodePointCount) {
620 State.Stack.back().VariablePos -= Tok->CodePointCount;
Daniel Jasper31c96b92013-04-05 09:38:50 +0000621 if (Tok->SpacesRequiredBefore != 0)
622 break;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000623 Tok = Tok->Previous;
Daniel Jasper31c96b92013-04-05 09:38:50 +0000624 }
Daniel Jaspera628c982013-04-03 13:36:17 +0000625 if (Previous.PartOfMultiVariableDeclStmt)
626 State.Stack.back().LastSpace = State.Stack.back().VariablePos;
627 }
Daniel Jasperfbde69e2012-12-21 14:37:20 +0000628
Daniel Jaspereef30492013-02-11 12:36:37 +0000629 unsigned Spaces = State.NextToken->SpacesRequiredBefore;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000630
Daniel Jasperf7935112012-12-03 18:12:45 +0000631 if (!DryRun)
Manuel Klimek4fe43002013-05-22 12:51:29 +0000632 Whitespaces.replaceWhitespace(Current, 0, Spaces,
633 State.Column + Spaces);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000634
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000635 if (Current.Type == TT_ObjCSelectorName &&
636 State.Stack.back().ColonPos == 0) {
637 if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000638 State.Column + Spaces + Current.CodePointCount)
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000639 State.Stack.back().ColonPos =
640 State.Stack.back().Indent + Current.LongestObjCSelectorName;
641 else
642 State.Stack.back().ColonPos =
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000643 State.Column + Spaces + Current.CodePointCount;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000644 }
645
Daniel Jasperc04baae2013-04-10 09:49:49 +0000646 if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
Daniel Jasper6bee6822013-04-08 20:33:42 +0000647 Current.Type != TT_LineComment)
Daniel Jasper400adc62013-02-08 15:28:42 +0000648 State.Stack.back().Indent = State.Column + Spaces;
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000649 if (Previous.is(tok::comma) && !Current.isTrailingComment() &&
650 State.Stack.back().AvoidBinPacking)
651 State.Stack.back().NoLineBreak = true;
Daniel Jasper9278eb92013-01-16 14:59:02 +0000652
Daniel Jaspere9de2602012-12-06 09:56:08 +0000653 State.Column += Spaces;
Daniel Jaspera628c982013-04-03 13:36:17 +0000654 if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
Daniel Jasper39e27382013-01-23 20:41:06 +0000655 // Treat the condition inside an if as if it was a second function
656 // parameter, i.e. let nested calls have an indent of 4.
657 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000658 else if (Previous.is(tok::comma))
Daniel Jasper39e27382013-01-23 20:41:06 +0000659 State.Stack.back().LastSpace = State.Column;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000660 else if ((Previous.Type == TT_BinaryOperator ||
Daniel Jasper65585ed2013-01-28 13:31:35 +0000661 Previous.Type == TT_ConditionalExpr ||
662 Previous.Type == TT_CtorInitializerColon) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000663 !(Previous.getPrecedence() == prec::Assignment &&
Daniel Jasper7b27a102013-05-27 12:45:09 +0000664 Current.FakeLParens.empty()))
665 // Always indent relative to the RHS of the expression unless this is a
666 // simple assignment without binary expression on the RHS.
Daniel Jasper20b09ef2013-01-28 09:35:24 +0000667 State.Stack.back().LastSpace = State.Column;
Daniel Jaspereead02b2013-02-14 08:42:54 +0000668 else if (Previous.Type == TT_InheritanceColon)
669 State.Stack.back().Indent = State.Column;
Daniel Jasperd69fc772013-05-08 14:12:04 +0000670 else if (Previous.opensScope() && !Current.FakeLParens.empty())
671 // If this function has multiple parameters or a binary expression
672 // parameter, indent nested calls from the start of the first parameter.
Daniel Jasper7b5773e92013-01-28 07:35:34 +0000673 State.Stack.back().LastSpace = State.Column;
Daniel Jasperf7935112012-12-03 18:12:45 +0000674 }
Daniel Jasper9278eb92013-01-16 14:59:02 +0000675
Manuel Klimek1998ea22013-02-20 10:15:13 +0000676 return moveStateToNextToken(State, DryRun);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000677 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000678
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000679 /// \brief Mark the next token as consumed in \p State and modify its stacks
680 /// accordingly.
Manuel Klimek1998ea22013-02-20 10:15:13 +0000681 unsigned moveStateToNextToken(LineState &State, bool DryRun) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000682 const FormatToken &Current = *State.NextToken;
Daniel Jasper337816e2013-01-11 10:22:12 +0000683 assert(State.Stack.size());
Daniel Jaspere9de2602012-12-06 09:56:08 +0000684
Daniel Jaspereead02b2013-02-14 08:42:54 +0000685 if (Current.Type == TT_InheritanceColon)
686 State.Stack.back().AvoidBinPacking = true;
Daniel Jasper337816e2013-01-11 10:22:12 +0000687 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
688 State.Stack.back().FirstLessLess = State.Column;
Daniel Jasperca6623b2013-01-28 12:45:14 +0000689 if (Current.is(tok::question))
690 State.Stack.back().QuestionColumn = State.Column;
Daniel Jasper0e90c3d2013-07-05 09:14:35 +0000691 if (!Current.opensScope() && !Current.closesScope())
692 State.LowestLevelOnLine =
693 std::min(State.LowestLevelOnLine, State.ParenLevel);
694 if (Current.isOneOf(tok::period, tok::arrow) &&
695 Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
696 State.Stack.back().StartOfFunctionCall =
697 Current.LastInChainOfCalls ? 0
698 : State.Column + Current.CodePointCount;
Daniel Jasper37905f72013-02-21 15:00:29 +0000699 if (Current.Type == TT_CtorInitializerColon) {
Manuel Klimek13b97d82013-05-13 08:42:42 +0000700 // Indent 2 from the column, so:
701 // SomeClass::SomeClass()
702 // : First(...), ...
703 // Next(...)
704 // ^ line up here.
Daniel Jasper6bee6822013-04-08 20:33:42 +0000705 State.Stack.back().Indent = State.Column + 2;
Daniel Jasper37905f72013-02-21 15:00:29 +0000706 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
707 State.Stack.back().AvoidBinPacking = true;
708 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000709 }
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000710
Daniel Jasper6bee6822013-04-08 20:33:42 +0000711 // If return returns a binary expression, align after it.
712 if (Current.is(tok::kw_return) && !Current.FakeLParens.empty())
713 State.Stack.back().LastSpace = State.Column + 7;
714
Daniel Jasper5188e6b2013-04-03 07:21:51 +0000715 // In ObjC method declaration we align on the ":" of parameters, but we need
716 // to ensure that we indent parameters on subsequent lines by at least 4.
Daniel Jasperc238c872013-04-02 14:33:13 +0000717 if (Current.Type == TT_ObjCMethodSpecifier)
718 State.Stack.back().Indent += 4;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000719
Daniel Jasper400adc62013-02-08 15:28:42 +0000720 // Insert scopes created by fake parenthesis.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000721 const FormatToken *Previous = Current.getPreviousNonComment();
Daniel Jasper6bee6822013-04-08 20:33:42 +0000722 // Don't add extra indentation for the first fake parenthesis after
723 // 'return', assignements or opening <({[. The indentation for these cases
724 // is special cased.
725 bool SkipFirstExtraIndent =
726 Current.is(tok::kw_return) ||
Daniel Jasperc04baae2013-04-10 09:49:49 +0000727 (Previous && (Previous->opensScope() ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000728 Previous->getPrecedence() == prec::Assignment));
Craig Topper61ac9062013-07-08 03:55:09 +0000729 for (SmallVectorImpl<prec::Level>::const_reverse_iterator
Daniel Jasper6bee6822013-04-08 20:33:42 +0000730 I = Current.FakeLParens.rbegin(),
731 E = Current.FakeLParens.rend();
732 I != E; ++I) {
Daniel Jasper400adc62013-02-08 15:28:42 +0000733 ParenState NewParenState = State.Stack.back();
Daniel Jasperee7539a2013-07-08 14:25:23 +0000734 NewParenState.ContainsLineBreak = false;
Daniel Jasper6bee6822013-04-08 20:33:42 +0000735 NewParenState.Indent =
736 std::max(std::max(State.Column, NewParenState.Indent),
737 State.Stack.back().LastSpace);
738
739 // Always indent conditional expressions. Never indent expression where
740 // the 'operator' is ',', ';' or an assignment (i.e. *I <=
741 // prec::Assignment) as those have different indentation rules. Indent
742 // other expression, unless the indentation needs to be skipped.
743 if (*I == prec::Conditional ||
744 (!SkipFirstExtraIndent && *I > prec::Assignment))
745 NewParenState.Indent += 4;
Daniel Jasperc04baae2013-04-10 09:49:49 +0000746 if (Previous && !Previous->opensScope())
Daniel Jasper6bee6822013-04-08 20:33:42 +0000747 NewParenState.BreakBeforeParameter = false;
Daniel Jasper400adc62013-02-08 15:28:42 +0000748 State.Stack.push_back(NewParenState);
Daniel Jasper6bee6822013-04-08 20:33:42 +0000749 SkipFirstExtraIndent = false;
Daniel Jasper400adc62013-02-08 15:28:42 +0000750 }
751
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000752 // If we encounter an opening (, [, { or <, we add a level to our stacks to
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000753 // prepare for the following tokens.
Daniel Jasperc04baae2013-04-10 09:49:49 +0000754 if (Current.opensScope()) {
Daniel Jasper337816e2013-01-11 10:22:12 +0000755 unsigned NewIndent;
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000756 unsigned LastSpace = State.Stack.back().LastSpace;
Daniel Jasper8a8ce242013-01-31 14:59:26 +0000757 bool AvoidBinPacking;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000758 if (Current.is(tok::l_brace)) {
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000759 NewIndent = Style.IndentWidth + LastSpace;
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000760 const FormatToken *NextNoComment = Current.getNextNonComment();
Daniel Jasperbca4bbe2013-05-28 11:30:49 +0000761 AvoidBinPacking = NextNoComment &&
762 NextNoComment->Type == TT_DesignatedInitializerPeriod;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000763 } else {
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000764 NewIndent =
765 4 + std::max(LastSpace, State.Stack.back().StartOfFunctionCall);
Daniel Jaspercc960fa2013-04-22 07:59:53 +0000766 AvoidBinPacking = !Style.BinPackParameters;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000767 }
Daniel Jaspere3c0e012013-04-25 13:31:51 +0000768
Daniel Jaspercc3044c2013-05-13 09:19:24 +0000769 State.Stack.push_back(ParenState(NewIndent, LastSpace, AvoidBinPacking,
770 State.Stack.back().NoLineBreak));
Daniel Jasper400adc62013-02-08 15:28:42 +0000771 ++State.ParenLevel;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000772 }
773
Daniel Jasperacc33662013-02-08 08:22:00 +0000774 // If this '[' opens an ObjC call, determine whether all parameters fit into
775 // one line and put one per line if they don't.
776 if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
777 Current.MatchingParen != NULL) {
778 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
779 State.Stack.back().BreakBeforeParameter = true;
780 }
781
Daniel Jasper2eda23e2012-12-24 13:43:52 +0000782 // If we encounter a closing ), ], } or >, we can remove a level from our
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000783 // stacks.
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000784 if (Current.isOneOf(tok::r_paren, tok::r_square) ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000785 (Current.is(tok::r_brace) && State.NextToken != RootToken) ||
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000786 State.NextToken->Type == TT_TemplateCloser) {
Daniel Jasper337816e2013-01-11 10:22:12 +0000787 State.Stack.pop_back();
Daniel Jasper400adc62013-02-08 15:28:42 +0000788 --State.ParenLevel;
789 }
790
791 // Remove scopes created by fake parenthesis.
792 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
Daniel Jasper6daabe32013-04-04 19:31:00 +0000793 unsigned VariablePos = State.Stack.back().VariablePos;
Daniel Jasper400adc62013-02-08 15:28:42 +0000794 State.Stack.pop_back();
Daniel Jasper6daabe32013-04-04 19:31:00 +0000795 State.Stack.back().VariablePos = VariablePos;
Daniel Jasperf7935112012-12-03 18:12:45 +0000796 }
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000797
Daniel Jasper47a04442013-05-13 20:50:15 +0000798 if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
Manuel Klimek02f640a2013-02-20 15:25:48 +0000799 State.StartOfStringLiteral = State.Column;
Daniel Jasper47a04442013-05-13 20:50:15 +0000800 } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
801 tok::string_literal)) {
Daniel Jasper7dd22c51b2013-05-16 04:26:02 +0000802 State.StartOfStringLiteral = 0;
Manuel Klimek02f640a2013-02-20 15:25:48 +0000803 }
804
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000805 State.Column += Current.CodePointCount;
Manuel Klimek1998ea22013-02-20 10:15:13 +0000806
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000807 State.NextToken = State.NextToken->Next;
Manuel Klimek73a2fdf2013-01-10 14:36:46 +0000808
Manuel Klimek1998ea22013-02-20 10:15:13 +0000809 return breakProtrudingToken(Current, State, DryRun);
810 }
811
812 /// \brief If the current token sticks out over the end of the line, break
813 /// it if possible.
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000814 ///
815 /// \returns An extra penalty if a token was broken, otherwise 0.
816 ///
Alexander Kornienkoaa620e12013-07-01 13:42:42 +0000817 /// The returned penalty will cover the cost of the additional line breaks and
818 /// column limit violation in all lines except for the last one. The penalty
819 /// for the column limit violation in the last line (and in single line
820 /// tokens) is handled in \c addNextStateToQueue.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000821 unsigned breakProtrudingToken(const FormatToken &Current, LineState &State,
Manuel Klimek4fe43002013-05-22 12:51:29 +0000822 bool DryRun) {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000823 llvm::OwningPtr<BreakableToken> Token;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000824 unsigned StartColumn = State.Column - Current.CodePointCount;
Manuel Klimek591ab5a2013-05-28 13:42:28 +0000825 unsigned OriginalStartColumn =
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000826 SourceMgr.getSpellingColumnNumber(Current.getStartOfNonWhitespace()) -
Manuel Klimek591ab5a2013-05-28 13:42:28 +0000827 1;
Manuel Klimek9043c742013-05-27 15:23:34 +0000828
Daniel Jasper8bb99e82013-05-16 12:59:13 +0000829 if (Current.is(tok::string_literal) &&
830 Current.Type != TT_ImplicitStringLiteral) {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000831 // Only break up default narrow strings.
Alexander Kornienkobe633902013-06-14 11:46:10 +0000832 if (!Current.TokenText.startswith("\""))
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000833 return 0;
834
Alexander Kornienkobe633902013-06-14 11:46:10 +0000835 Token.reset(new BreakableStringLiteral(Current, StartColumn,
836 Line.InPPDirective, Encoding));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000837 } else if (Current.Type == TT_BlockComment) {
Alexander Kornienkobe633902013-06-14 11:46:10 +0000838 Token.reset(new BreakableBlockComment(
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000839 Style, Current, StartColumn, OriginalStartColumn, !Current.Previous,
Alexander Kornienkobe633902013-06-14 11:46:10 +0000840 Line.InPPDirective, Encoding));
Daniel Jasper4a4be012013-05-06 10:24:51 +0000841 } else if (Current.Type == TT_LineComment &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000842 (Current.Previous == NULL ||
843 Current.Previous->Type != TT_ImplicitStringLiteral)) {
Alexander Kornienkobe633902013-06-14 11:46:10 +0000844 Token.reset(new BreakableLineComment(Current, StartColumn,
845 Line.InPPDirective, Encoding));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000846 } else {
Manuel Klimek4fe43002013-05-22 12:51:29 +0000847 return 0;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000848 }
Alexander Kornienkobe633902013-06-14 11:46:10 +0000849 if (Current.UnbreakableTailLength >= getColumnLimit())
Manuel Klimek5ecb5fd2013-05-14 09:04:24 +0000850 return 0;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000851
Alexander Kornienkoa3555e22013-06-19 19:50:11 +0000852 unsigned RemainingSpace = getColumnLimit() - Current.UnbreakableTailLength;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000853 bool BreakInserted = false;
854 unsigned Penalty = 0;
Alexander Kornienkoa3555e22013-06-19 19:50:11 +0000855 unsigned RemainingTokenColumns = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +0000856 for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
857 LineIndex != EndIndex; ++LineIndex) {
Alexander Kornienkobe633902013-06-14 11:46:10 +0000858 if (!DryRun)
859 Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000860 unsigned TailOffset = 0;
Alexander Kornienkoa3555e22013-06-19 19:50:11 +0000861 RemainingTokenColumns = Token->getLineLengthAfterSplit(
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000862 LineIndex, TailOffset, StringRef::npos);
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000863 while (RemainingTokenColumns > RemainingSpace) {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000864 BreakableToken::Split Split =
Manuel Klimek4fe43002013-05-22 12:51:29 +0000865 Token->getSplit(LineIndex, TailOffset, getColumnLimit());
Alexander Kornienkoaa620e12013-07-01 13:42:42 +0000866 if (Split.first == StringRef::npos) {
867 // The last line's penalty is handled in addNextStateToQueue().
868 if (LineIndex < EndIndex - 1)
869 Penalty += Style.PenaltyExcessCharacter *
870 (RemainingTokenColumns - RemainingSpace);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000871 break;
Alexander Kornienkoaa620e12013-07-01 13:42:42 +0000872 }
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000873 assert(Split.first != 0);
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000874 unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000875 LineIndex, TailOffset + Split.first + Split.second,
876 StringRef::npos);
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000877 assert(NewRemainingTokenColumns < RemainingTokenColumns);
Alexander Kornienkobe633902013-06-14 11:46:10 +0000878 if (!DryRun)
879 Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000880 Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString
881 : Style.PenaltyBreakComment;
882 unsigned ColumnsUsed =
883 Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
884 if (ColumnsUsed > getColumnLimit()) {
885 Penalty +=
886 Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit());
887 }
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000888 TailOffset += Split.first + Split.second;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000889 RemainingTokenColumns = NewRemainingTokenColumns;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000890 BreakInserted = true;
Manuel Klimek1998ea22013-02-20 10:15:13 +0000891 }
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000892 }
893
Alexander Kornienkoa3555e22013-06-19 19:50:11 +0000894 State.Column = RemainingTokenColumns;
895
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000896 if (BreakInserted) {
Alexander Kornienko4d26b6e2013-06-17 12:59:44 +0000897 // If we break the token inside a parameter list, we need to break before
898 // the next parameter on all levels, so that the next parameter is clearly
899 // visible. Line comments already introduce a break.
900 if (Current.Type != TT_LineComment) {
901 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
902 State.Stack[i].BreakBeforeParameter = true;
903 }
904
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000905 State.Stack.back().LastSpace = StartColumn;
Manuel Klimek1998ea22013-02-20 10:15:13 +0000906 }
Manuel Klimek1998ea22013-02-20 10:15:13 +0000907 return Penalty;
908 }
909
Daniel Jasper2df93312013-01-09 10:16:05 +0000910 unsigned getColumnLimit() {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000911 // In preprocessor directives reserve two chars for trailing " \"
912 return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0);
Daniel Jasper2df93312013-01-09 10:16:05 +0000913 }
914
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000915 /// \brief An edge in the solution space from \c Previous->State to \c State,
916 /// inserting a newline dependent on the \c NewLine.
917 struct StateNode {
918 StateNode(const LineState &State, bool NewLine, StateNode *Previous)
Daniel Jasper12ef4e52013-02-21 21:33:55 +0000919 : State(State), NewLine(NewLine), Previous(Previous) {}
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000920 LineState State;
921 bool NewLine;
922 StateNode *Previous;
923 };
Daniel Jasper4b866272013-02-01 11:00:45 +0000924
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000925 /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
926 ///
927 /// In case of equal penalties, we want to prefer states that were inserted
928 /// first. During state generation we make sure that we insert states first
929 /// that break the line as late as possible.
930 typedef std::pair<unsigned, unsigned> OrderedPenalty;
931
932 /// \brief An item in the prioritized BFS search queue. The \c StateNode's
933 /// \c State has the given \c OrderedPenalty.
934 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
935
936 /// \brief The BFS queue type.
937 typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
938 std::greater<QueueItem> > QueueType;
Daniel Jasper4b866272013-02-01 11:00:45 +0000939
940 /// \brief Analyze the entire solution space starting from \p InitialState.
Daniel Jasperf7935112012-12-03 18:12:45 +0000941 ///
Daniel Jasper4b866272013-02-01 11:00:45 +0000942 /// This implements a variant of Dijkstra's algorithm on the graph that spans
943 /// the solution space (\c LineStates are the nodes). The algorithm tries to
944 /// find the shortest path (the one with lowest penalty) from \p InitialState
945 /// to a state where all tokens are placed.
Manuel Klimek4fe43002013-05-22 12:51:29 +0000946 void analyzeSolutionSpace(LineState &InitialState) {
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000947 std::set<LineState> Seen;
948
Daniel Jasper4b866272013-02-01 11:00:45 +0000949 // Insert start element into queue.
Daniel Jasper687af3b2013-02-14 14:26:07 +0000950 StateNode *Node =
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000951 new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
952 Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
953 ++Count;
Daniel Jasper4b866272013-02-01 11:00:45 +0000954
955 // While not empty, take first element and follow edges.
956 while (!Queue.empty()) {
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000957 unsigned Penalty = Queue.top().first.first;
Daniel Jasper687af3b2013-02-14 14:26:07 +0000958 StateNode *Node = Queue.top().second;
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000959 if (Node->State.NextToken == NULL) {
Alexander Kornienko49149672013-05-10 11:56:10 +0000960 DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
Daniel Jasper4b866272013-02-01 11:00:45 +0000961 break;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000962 }
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000963 Queue.pop();
Daniel Jasper4b866272013-02-01 11:00:45 +0000964
Daniel Jasperf8114cf2013-05-22 05:27:42 +0000965 // Cut off the analysis of certain solutions if the analysis gets too
966 // complex. See description of IgnoreStackForComparison.
967 if (Count > 10000)
968 Node->State.IgnoreStackForComparison = true;
969
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000970 if (!Seen.insert(Node->State).second)
971 // State already examined with lower penalty.
972 continue;
Daniel Jasper4b866272013-02-01 11:00:45 +0000973
Nico Weber9096fc02013-06-26 00:30:14 +0000974 addNextStateToQueue(Penalty, Node, /*NewLine=*/false);
975 addNextStateToQueue(Penalty, Node, /*NewLine=*/true);
Daniel Jasper4b866272013-02-01 11:00:45 +0000976 }
977
978 if (Queue.empty())
979 // We were unable to find a solution, do nothing.
980 // FIXME: Add diagnostic?
Manuel Klimek4fe43002013-05-22 12:51:29 +0000981 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000982
Daniel Jasper4b866272013-02-01 11:00:45 +0000983 // Reconstruct the solution.
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000984 reconstructPath(InitialState, Queue.top().second);
Alexander Kornienko49149672013-05-10 11:56:10 +0000985 DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
986 DEBUG(llvm::dbgs() << "---\n");
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000987 }
988
989 void reconstructPath(LineState &State, StateNode *Current) {
Manuel Klimek4c5c28b2013-05-29 15:10:11 +0000990 std::deque<StateNode *> Path;
991 // We do not need a break before the initial token.
992 while (Current->Previous) {
993 Path.push_front(Current);
994 Current = Current->Previous;
995 }
996 for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
997 I != E; ++I) {
998 DEBUG({
999 if ((*I)->NewLine) {
1000 llvm::dbgs() << "Penalty for splitting before "
1001 << (*I)->Previous->State.NextToken->Tok.getName() << ": "
1002 << (*I)->Previous->State.NextToken->SplitPenalty << "\n";
1003 }
1004 });
1005 addTokenToState((*I)->NewLine, false, State);
1006 }
Daniel Jasper4b866272013-02-01 11:00:45 +00001007 }
1008
Manuel Klimekaf491072013-02-13 10:54:19 +00001009 /// \brief Add the following state to the analysis queue \c Queue.
Daniel Jasper4b866272013-02-01 11:00:45 +00001010 ///
Manuel Klimekaf491072013-02-13 10:54:19 +00001011 /// Assume the current state is \p PreviousNode and has been reached with a
Daniel Jasper4b866272013-02-01 11:00:45 +00001012 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
Manuel Klimekaf491072013-02-13 10:54:19 +00001013 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
1014 bool NewLine) {
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001015 if (NewLine && !canBreak(PreviousNode->State))
Daniel Jasper4b866272013-02-01 11:00:45 +00001016 return;
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001017 if (!NewLine && mustBreak(PreviousNode->State))
Daniel Jasper4b866272013-02-01 11:00:45 +00001018 return;
Daniel Jasperee7539a2013-07-08 14:25:23 +00001019 if (NewLine) {
1020 if (!PreviousNode->State.Stack.back().ContainsLineBreak)
1021 Penalty += 15;
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001022 Penalty += PreviousNode->State.NextToken->SplitPenalty;
Daniel Jasperee7539a2013-07-08 14:25:23 +00001023 }
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001024
1025 StateNode *Node = new (Allocator.Allocate())
1026 StateNode(PreviousNode->State, NewLine, PreviousNode);
Manuel Klimek1998ea22013-02-20 10:15:13 +00001027 Penalty += addTokenToState(NewLine, true, Node->State);
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001028 if (Node->State.Column > getColumnLimit()) {
1029 unsigned ExcessCharacters = Node->State.Column - getColumnLimit();
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001030 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
Daniel Jasper2df93312013-01-09 10:16:05 +00001031 }
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001032
1033 Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
1034 ++Count;
Daniel Jasper4b866272013-02-01 11:00:45 +00001035 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001036
Daniel Jasper4b866272013-02-01 11:00:45 +00001037 /// \brief Returns \c true, if a line break after \p State is allowed.
1038 bool canBreak(const LineState &State) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001039 const FormatToken &Current = *State.NextToken;
1040 const FormatToken &Previous = *Current.Previous;
1041 assert(&Previous == Current.Previous);
Daniel Jasper473c62c2013-05-17 09:35:01 +00001042 if (!Current.CanBreakBefore &&
1043 !(Current.is(tok::r_brace) &&
Daniel Jasper4b866272013-02-01 11:00:45 +00001044 State.Stack.back().BreakBeforeClosingBrace))
1045 return false;
Daniel Jasper473c62c2013-05-17 09:35:01 +00001046 // The opening "{" of a braced list has to be on the same line as the first
1047 // element if it is nested in another braced init list or function call.
1048 if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001049 Previous.Previous &&
1050 Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
Daniel Jasper473c62c2013-05-17 09:35:01 +00001051 return false;
Daniel Jasper32a796b2013-05-27 11:50:16 +00001052 // This prevents breaks like:
1053 // ...
1054 // SomeParameter, OtherParameter).DoSomething(
1055 // ...
1056 // As they hide "DoSomething" and are generally bad for readability.
Daniel Jasper0e90c3d2013-07-05 09:14:35 +00001057 if (Previous.opensScope() &&
1058 State.LowestLevelOnLine < State.StartOfLineLevel)
Daniel Jasper32a796b2013-05-27 11:50:16 +00001059 return false;
Daniel Jaspercc960fa2013-04-22 07:59:53 +00001060 return !State.Stack.back().NoLineBreak;
Daniel Jasper4b866272013-02-01 11:00:45 +00001061 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001062
Daniel Jasper4b866272013-02-01 11:00:45 +00001063 /// \brief Returns \c true, if a line break after \p State is mandatory.
1064 bool mustBreak(const LineState &State) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001065 const FormatToken &Current = *State.NextToken;
1066 const FormatToken &Previous = *Current.Previous;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001067 if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
Daniel Jasper4b866272013-02-01 11:00:45 +00001068 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001069 if (Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)
Daniel Jasper4b866272013-02-01 11:00:45 +00001070 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001071 if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
Daniel Jasper4b866272013-02-01 11:00:45 +00001072 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001073 if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) ||
1074 Current.Type == TT_ConditionalExpr) &&
Daniel Jasperacc33662013-02-08 08:22:00 +00001075 State.Stack.back().BreakBeforeParameter &&
Daniel Jasperd69fc772013-05-08 14:12:04 +00001076 !Current.isTrailingComment() &&
1077 !Current.isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper4b866272013-02-01 11:00:45 +00001078 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001079
1080 // If we need to break somewhere inside the LHS of a binary expression, we
Daniel Jasper7ae41cd2013-07-03 10:34:47 +00001081 // should also break after the operator. Otherwise, the formatting would
1082 // hide the operator precedence, e.g. in:
1083 // if (aaaaaaaaaaaaaa ==
1084 // bbbbbbbbbbbbbb && c) {..
1085 // For comparisons, we only apply this rule, if the LHS is a binary
1086 // expression itself as otherwise, the line breaks seem superfluous.
1087 // We need special cases for ">>" which we have split into two ">" while
1088 // lexing in order to make template parsing easier.
1089 bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
1090 Previous.getPrecedence() == prec::Equality) &&
1091 Previous.Previous &&
1092 Previous.Previous->Type != TT_BinaryOperator; // For >>.
1093 bool LHSIsBinaryExpr =
1094 Previous.Previous && Previous.Previous->FakeRParens > 0;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001095 if (Previous.Type == TT_BinaryOperator &&
Daniel Jasper7ae41cd2013-07-03 10:34:47 +00001096 (!IsComparison || LHSIsBinaryExpr) &&
1097 Current.Type != TT_BinaryOperator && // For >>.
Daniel Jasper68d888c2013-06-03 08:42:05 +00001098 !Current.isTrailingComment() &&
Daniel Jasperd69fc772013-05-08 14:12:04 +00001099 !Previous.isOneOf(tok::lessless, tok::question) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001100 Previous.getPrecedence() != prec::Assignment &&
Daniel Jasperacc33662013-02-08 08:22:00 +00001101 State.Stack.back().BreakBeforeParameter)
Daniel Jasper1ac3e052013-02-05 10:07:47 +00001102 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001103
1104 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
1105 // out whether it is the first parameter. Clean this up.
1106 if (Current.Type == TT_ObjCSelectorName &&
1107 Current.LongestObjCSelectorName == 0 &&
1108 State.Stack.back().BreakBeforeParameter)
Daniel Jasper4b866272013-02-01 11:00:45 +00001109 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001110 if ((Current.Type == TT_CtorInitializerColon ||
1111 (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0)))
Daniel Jasper40aacf42013-03-14 13:45:21 +00001112 return true;
Daniel Jasperd69fc772013-05-08 14:12:04 +00001113
Daniel Jasperc6fbc212013-05-15 09:35:08 +00001114 if (Current.Type == TT_StartOfName && Line.MightBeFunctionDecl &&
1115 State.Stack.back().BreakBeforeParameter && State.ParenLevel == 0)
1116 return true;
Daniel Jasper4b866272013-02-01 11:00:45 +00001117 return false;
Daniel Jasperf7935112012-12-03 18:12:45 +00001118 }
1119
Daniel Jasper9b334242013-03-15 14:57:30 +00001120 // Returns the total number of columns required for the remaining tokens.
1121 unsigned getRemainingLength(const LineState &State) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001122 if (State.NextToken && State.NextToken->Previous)
1123 return Line.Last->TotalLength - State.NextToken->Previous->TotalLength;
Daniel Jasper9b334242013-03-15 14:57:30 +00001124 return 0;
1125 }
1126
Daniel Jasperf7935112012-12-03 18:12:45 +00001127 FormatStyle Style;
1128 SourceManager &SourceMgr;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001129 const AnnotatedLine &Line;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001130 const unsigned FirstIndent;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001131 const FormatToken *RootToken;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001132 WhitespaceManager &Whitespaces;
Manuel Klimekaf491072013-02-13 10:54:19 +00001133
1134 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1135 QueueType Queue;
1136 // Increasing count of \c StateNode items we have created. This is used
1137 // to create a deterministic order independent of the container.
1138 unsigned Count;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001139 encoding::Encoding Encoding;
Daniel Jasperf7935112012-12-03 18:12:45 +00001140};
1141
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001142class FormatTokenLexer {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001143public:
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001144 FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr,
1145 encoding::Encoding Encoding)
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001146 : FormatTok(NULL), GreaterStashed(false), TrailingWhitespace(0), Lex(Lex),
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001147 SourceMgr(SourceMgr), IdentTable(Lex.getLangOpts()),
1148 Encoding(Encoding) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001149 Lex.SetKeepWhitespaceMode(true);
1150 }
1151
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001152 ArrayRef<FormatToken *> lex() {
1153 assert(Tokens.empty());
1154 do {
1155 Tokens.push_back(getNextToken());
1156 } while (Tokens.back()->Tok.isNot(tok::eof));
1157 return Tokens;
1158 }
1159
1160 IdentifierTable &getIdentTable() { return IdentTable; }
1161
1162private:
1163 FormatToken *getNextToken() {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001164 if (GreaterStashed) {
Manuel Klimek591ab5a2013-05-28 13:42:28 +00001165 // Create a synthesized second '>' token.
1166 Token Greater = FormatTok->Tok;
1167 FormatTok = new (Allocator.Allocate()) FormatToken;
1168 FormatTok->Tok = Greater;
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001169 SourceLocation GreaterLocation =
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001170 FormatTok->Tok.getLocation().getLocWithOffset(1);
1171 FormatTok->WhitespaceRange =
1172 SourceRange(GreaterLocation, GreaterLocation);
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001173 FormatTok->TokenText = ">";
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001174 FormatTok->CodePointCount = 1;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001175 GreaterStashed = false;
1176 return FormatTok;
1177 }
1178
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001179 FormatTok = new (Allocator.Allocate()) FormatToken;
1180 Lex.LexFromRawLexer(FormatTok->Tok);
1181 StringRef Text = rawTokenText(FormatTok->Tok);
Manuel Klimek9043c742013-05-27 15:23:34 +00001182 SourceLocation WhitespaceStart =
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001183 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001184 if (SourceMgr.getFileOffset(WhitespaceStart) == 0)
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001185 FormatTok->IsFirst = true;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001186
1187 // Consume and record whitespace until we find a significant token.
Manuel Klimek9043c742013-05-27 15:23:34 +00001188 unsigned WhitespaceLength = TrailingWhitespace;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001189 while (FormatTok->Tok.is(tok::unknown)) {
Manuel Klimek0c137952013-02-11 12:33:24 +00001190 unsigned Newlines = Text.count('\n');
Daniel Jasper973c9422013-03-04 13:43:19 +00001191 if (Newlines > 0)
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001192 FormatTok->LastNewlineOffset = WhitespaceLength + Text.rfind('\n') + 1;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001193 FormatTok->NewlinesBefore += Newlines;
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001194 unsigned EscapedNewlines = Text.count("\\\n");
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001195 FormatTok->HasUnescapedNewline |= EscapedNewlines != Newlines;
1196 WhitespaceLength += FormatTok->Tok.getLength();
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001197
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001198 Lex.LexFromRawLexer(FormatTok->Tok);
1199 Text = rawTokenText(FormatTok->Tok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001200 }
Manuel Klimekef920692013-01-07 07:56:50 +00001201
Manuel Klimek1abf7892013-01-04 23:34:14 +00001202 // In case the token starts with escaped newlines, we want to
1203 // take them into account as whitespace - this pattern is quite frequent
1204 // in macro definitions.
1205 // FIXME: What do we want to do with other escaped spaces, and escaped
1206 // spaces or newlines in the middle of tokens?
1207 // FIXME: Add a more explicit test.
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001208 while (Text.size() > 1 && Text[0] == '\\' && Text[1] == '\n') {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001209 // FIXME: ++FormatTok->NewlinesBefore is missing...
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001210 WhitespaceLength += 2;
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001211 Text = Text.substr(2);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001212 }
1213
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001214 TrailingWhitespace = 0;
1215 if (FormatTok->Tok.is(tok::comment)) {
1216 StringRef UntrimmedText = Text;
1217 Text = Text.rtrim();
1218 TrailingWhitespace = UntrimmedText.size() - Text.size();
1219 } else if (FormatTok->Tok.is(tok::raw_identifier)) {
Manuel Klimek1abf7892013-01-04 23:34:14 +00001220 IdentifierInfo &Info = IdentTable.get(Text);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001221 FormatTok->Tok.setIdentifierInfo(&Info);
1222 FormatTok->Tok.setKind(Info.getTokenID());
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001223 } else if (FormatTok->Tok.is(tok::greatergreater)) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001224 FormatTok->Tok.setKind(tok::greater);
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001225 Text = Text.substr(0, 1);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001226 GreaterStashed = true;
1227 }
1228
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001229 // Now FormatTok is the next non-whitespace token.
1230 FormatTok->TokenText = Text;
1231 FormatTok->CodePointCount = encoding::getCodePointCount(Text, Encoding);
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001232
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001233 FormatTok->WhitespaceRange = SourceRange(
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001234 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001235 return FormatTok;
1236 }
1237
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001238 FormatToken *FormatTok;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001239 bool GreaterStashed;
Manuel Klimek9043c742013-05-27 15:23:34 +00001240 unsigned TrailingWhitespace;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001241 Lexer &Lex;
1242 SourceManager &SourceMgr;
1243 IdentifierTable IdentTable;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001244 encoding::Encoding Encoding;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001245 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
1246 SmallVector<FormatToken *, 16> Tokens;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001247
1248 /// Returns the text of \c FormatTok.
Manuel Klimekef920692013-01-07 07:56:50 +00001249 StringRef rawTokenText(Token &Tok) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001250 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
1251 Tok.getLength());
1252 }
1253};
1254
Daniel Jasperf7935112012-12-03 18:12:45 +00001255class Formatter : public UnwrappedLineConsumer {
1256public:
Daniel Jasperd2ae41a2013-05-15 08:14:19 +00001257 Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
Daniel Jasperf7935112012-12-03 18:12:45 +00001258 const std::vector<CharSourceRange> &Ranges)
Daniel Jasperd2ae41a2013-05-15 08:14:19 +00001259 : Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001260 Whitespaces(SourceMgr, Style), Ranges(Ranges),
1261 Encoding(encoding::detectEncoding(Lex.getBuffer())) {
1262 DEBUG(llvm::dbgs()
1263 << "File encoding: "
1264 << (Encoding == encoding::Encoding_UTF8 ? "UTF8" : "unknown")
1265 << "\n");
1266 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001267
Daniel Jasperfd8c4b12013-01-11 14:23:32 +00001268 virtual ~Formatter() {}
Daniel Jasper61bd3a12012-12-04 21:05:31 +00001269
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001270 tooling::Replacements format() {
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001271 FormatTokenLexer Tokens(Lex, SourceMgr, Encoding);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001272
1273 UnwrappedLineParser Parser(Style, Tokens.lex(), *this);
Manuel Klimek1a18c402013-04-12 14:13:36 +00001274 bool StructuralError = Parser.parse();
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001275 TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in"));
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001276 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1277 Annotator.annotate(AnnotatedLines[i]);
1278 }
1279 deriveLocalStyle();
1280 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1281 Annotator.calculateFormattingInformation(AnnotatedLines[i]);
1282 }
Daniel Jasperb67cc422013-04-09 17:46:55 +00001283
1284 // Adapt level to the next line if this is a comment.
1285 // FIXME: Can/should this be done in the UnwrappedLineParser?
Alexander Kornienko1efe0a02013-07-04 14:47:51 +00001286 const AnnotatedLine *NextNonCommentLine = NULL;
Daniel Jasperb67cc422013-04-09 17:46:55 +00001287 for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +00001288 if (NextNonCommentLine && AnnotatedLines[i].First->is(tok::comment) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001289 !AnnotatedLines[i].First->Next)
Alexander Kornienko1efe0a02013-07-04 14:47:51 +00001290 AnnotatedLines[i].Level = NextNonCommentLine->Level;
Daniel Jasperb67cc422013-04-09 17:46:55 +00001291 else
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +00001292 NextNonCommentLine = AnnotatedLines[i].First->isNot(tok::r_brace)
1293 ? &AnnotatedLines[i]
1294 : NULL;
Daniel Jasperb67cc422013-04-09 17:46:55 +00001295 }
1296
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001297 std::vector<int> IndentForLevel;
1298 bool PreviousLineWasTouched = false;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001299 const FormatToken *PreviousLineLastToken = 0;
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001300 bool FormatPPDirective = false;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001301 for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
1302 E = AnnotatedLines.end();
1303 I != E; ++I) {
1304 const AnnotatedLine &TheLine = *I;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001305 const FormatToken *FirstTok = TheLine.First;
1306 int Offset = getIndentOffset(*TheLine.First);
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001307
1308 // Check whether this line is part of a formatted preprocessor directive.
Manuel Klimek591ab5a2013-05-28 13:42:28 +00001309 if (FirstTok->HasUnescapedNewline)
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001310 FormatPPDirective = false;
1311 if (!FormatPPDirective && TheLine.InPPDirective &&
1312 (touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
1313 FormatPPDirective = true;
1314
Daniel Jasper12f9d8e2013-05-14 09:30:02 +00001315 // Determine indent and try to merge multiple unwrapped lines.
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001316 while (IndentForLevel.size() <= TheLine.Level)
1317 IndentForLevel.push_back(-1);
1318 IndentForLevel.resize(TheLine.Level + 1);
Daniel Jasper12f9d8e2013-05-14 09:30:02 +00001319 unsigned Indent = getIndent(IndentForLevel, TheLine.Level);
1320 if (static_cast<int>(Indent) + Offset >= 0)
1321 Indent += Offset;
1322 tryFitMultipleLinesInOne(Indent, I, E);
1323
Manuel Klimek591ab5a2013-05-28 13:42:28 +00001324 bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001325 if (TheLine.First->is(tok::eof)) {
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001326 if (PreviousLineWasTouched) {
Manuel Klimek591ab5a2013-05-28 13:42:28 +00001327 unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u);
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001328 Whitespaces.replaceWhitespace(*TheLine.First, NewLines, /*Indent*/ 0,
Manuel Klimek4fe43002013-05-22 12:51:29 +00001329 /*TargetColumn*/ 0);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001330 }
1331 } else if (TheLine.Type != LT_Invalid &&
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001332 (WasMoved || FormatPPDirective || touchesLine(TheLine))) {
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001333 unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
Manuel Klimek591ab5a2013-05-28 13:42:28 +00001334 if (FirstTok->WhitespaceRange.isValid() &&
Manuel Klimek1a18c402013-04-12 14:13:36 +00001335 // Insert a break even if there is a structural error in case where
1336 // we break apart a line consisting of multiple unwrapped lines.
Manuel Klimek591ab5a2013-05-28 13:42:28 +00001337 (FirstTok->NewlinesBefore == 0 || !StructuralError)) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001338 formatFirstToken(*TheLine.First, PreviousLineLastToken, Indent,
Manuel Klimek4fe43002013-05-22 12:51:29 +00001339 TheLine.InPPDirective);
Manuel Klimek1a18c402013-04-12 14:13:36 +00001340 } else {
1341 Indent = LevelIndent =
Manuel Klimek591ab5a2013-05-28 13:42:28 +00001342 SourceMgr.getSpellingColumnNumber(FirstTok->Tok.getLocation()) -
1343 1;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001344 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001345 UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001346 TheLine.First, Whitespaces, Encoding);
Manuel Klimek4fe43002013-05-22 12:51:29 +00001347 Formatter.format(I + 1 != E ? &*(I + 1) : NULL);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001348 IndentForLevel[TheLine.Level] = LevelIndent;
1349 PreviousLineWasTouched = true;
1350 } else {
Manuel Klimek4fe43002013-05-22 12:51:29 +00001351 // Format the first token if necessary, and notify the WhitespaceManager
1352 // about the unchanged whitespace.
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001353 for (const FormatToken *Tok = TheLine.First; Tok != NULL;
1354 Tok = Tok->Next) {
1355 if (Tok == TheLine.First &&
1356 (Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
1357 unsigned LevelIndent =
1358 SourceMgr.getSpellingColumnNumber(Tok->Tok.getLocation()) - 1;
Manuel Klimek4fe43002013-05-22 12:51:29 +00001359 // Remove trailing whitespace of the previous line if it was
1360 // touched.
1361 if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) {
1362 formatFirstToken(*Tok, PreviousLineLastToken, LevelIndent,
1363 TheLine.InPPDirective);
1364 } else {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001365 Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
Manuel Klimek4fe43002013-05-22 12:51:29 +00001366 }
Daniel Jasper12f9d8e2013-05-14 09:30:02 +00001367
Manuel Klimek4fe43002013-05-22 12:51:29 +00001368 if (static_cast<int>(LevelIndent) - Offset >= 0)
1369 LevelIndent -= Offset;
1370 if (Tok->isNot(tok::comment))
1371 IndentForLevel[TheLine.Level] = LevelIndent;
1372 } else {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001373 Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
Manuel Klimek4fe43002013-05-22 12:51:29 +00001374 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001375 }
1376 // If we did not reformat this unwrapped line, the column at the end of
1377 // the last token is unchanged - thus, we can calculate the end of the
1378 // last token.
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001379 PreviousLineWasTouched = false;
1380 }
Alexander Kornienkofd433362013-03-27 17:08:02 +00001381 PreviousLineLastToken = I->Last;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001382 }
1383 return Whitespaces.generateReplacements();
1384 }
1385
1386private:
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001387 void deriveLocalStyle() {
1388 unsigned CountBoundToVariable = 0;
1389 unsigned CountBoundToType = 0;
1390 bool HasCpp03IncompatibleFormat = false;
1391 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001392 if (!AnnotatedLines[i].First->Next)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001393 continue;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001394 FormatToken *Tok = AnnotatedLines[i].First->Next;
1395 while (Tok->Next) {
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001396 if (Tok->Type == TT_PointerOrReference) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001397 bool SpacesBefore =
1398 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
1399 bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
1400 Tok->Next->WhitespaceRange.getEnd();
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001401 if (SpacesBefore && !SpacesAfter)
1402 ++CountBoundToVariable;
1403 else if (!SpacesBefore && SpacesAfter)
1404 ++CountBoundToType;
1405 }
1406
Daniel Jasper400adc62013-02-08 15:28:42 +00001407 if (Tok->Type == TT_TemplateCloser &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001408 Tok->Previous->Type == TT_TemplateCloser &&
1409 Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd())
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001410 HasCpp03IncompatibleFormat = true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001411 Tok = Tok->Next;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001412 }
1413 }
1414 if (Style.DerivePointerBinding) {
1415 if (CountBoundToType > CountBoundToVariable)
1416 Style.PointerBindsToType = true;
1417 else if (CountBoundToType < CountBoundToVariable)
1418 Style.PointerBindsToType = false;
1419 }
1420 if (Style.Standard == FormatStyle::LS_Auto) {
1421 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
1422 : FormatStyle::LS_Cpp03;
1423 }
1424 }
1425
Manuel Klimekb95f5452013-02-08 17:38:27 +00001426 /// \brief Get the indent of \p Level from \p IndentForLevel.
1427 ///
1428 /// \p IndentForLevel must contain the indent for the level \c l
1429 /// at \p IndentForLevel[l], or a value < 0 if the indent for
1430 /// that level is unknown.
Daniel Jasper687af3b2013-02-14 14:26:07 +00001431 unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
Manuel Klimekb95f5452013-02-08 17:38:27 +00001432 if (IndentForLevel[Level] != -1)
1433 return IndentForLevel[Level];
Manuel Klimekd076dcd2013-02-08 19:53:32 +00001434 if (Level == 0)
1435 return 0;
Manuel Klimek13b97d82013-05-13 08:42:42 +00001436 return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
Manuel Klimekb95f5452013-02-08 17:38:27 +00001437 }
1438
1439 /// \brief Get the offset of the line relatively to the level.
1440 ///
1441 /// For example, 'public:' labels in classes are offset by 1 or 2
1442 /// characters to the left from their level.
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001443 int getIndentOffset(const FormatToken &RootToken) {
Alexander Kornienkofd433362013-03-27 17:08:02 +00001444 if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
Manuel Klimekb95f5452013-02-08 17:38:27 +00001445 return Style.AccessModifierOffset;
1446 return 0;
1447 }
1448
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001449 /// \brief Tries to merge lines into one.
1450 ///
1451 /// This will change \c Line and \c AnnotatedLine to contain the merged line,
1452 /// if possible; note that \c I will be incremented when lines are merged.
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001453 void tryFitMultipleLinesInOne(unsigned Indent,
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001454 std::vector<AnnotatedLine>::iterator &I,
1455 std::vector<AnnotatedLine>::iterator E) {
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001456 // We can never merge stuff if there are trailing line comments.
1457 if (I->Last->Type == TT_LineComment)
1458 return;
1459
Daniel Jasperc22f5b42013-02-28 11:05:57 +00001460 unsigned Limit = Style.ColumnLimit - Indent;
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001461 // If we already exceed the column limit, we set 'Limit' to 0. The different
1462 // tryMerge..() functions can then decide whether to still do merging.
1463 Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
Daniel Jasperc36492b2013-01-16 07:02:34 +00001464
Daniel Jasperd41ee2d2013-01-21 14:18:28 +00001465 if (I + 1 == E || (I + 1)->Type == LT_Invalid)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001466 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001467
Daniel Jasperabca58c2013-05-15 14:09:55 +00001468 if (I->Last->is(tok::l_brace)) {
Daniel Jasper25837aa2013-01-14 14:14:23 +00001469 tryMergeSimpleBlock(I, E, Limit);
Daniel Jasper3a685df2013-05-16 12:12:21 +00001470 } else if (Style.AllowShortIfStatementsOnASingleLine &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001471 I->First->is(tok::kw_if)) {
Daniel Jasper3a685df2013-05-16 12:12:21 +00001472 tryMergeSimpleControlStatement(I, E, Limit);
1473 } else if (Style.AllowShortLoopsOnASingleLine &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001474 I->First->isOneOf(tok::kw_for, tok::kw_while)) {
Daniel Jasper3a685df2013-05-16 12:12:21 +00001475 tryMergeSimpleControlStatement(I, E, Limit);
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001476 } else if (I->InPPDirective &&
1477 (I->First->HasUnescapedNewline || I->First->IsFirst)) {
Daniel Jasper39825ea2013-01-14 15:40:57 +00001478 tryMergeSimplePPDirective(I, E, Limit);
Daniel Jasper25837aa2013-01-14 14:14:23 +00001479 }
Daniel Jasper25837aa2013-01-14 14:14:23 +00001480 }
1481
Daniel Jasper39825ea2013-01-14 15:40:57 +00001482 void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
1483 std::vector<AnnotatedLine>::iterator E,
1484 unsigned Limit) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001485 if (Limit == 0)
1486 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001487 AnnotatedLine &Line = *I;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001488 if (!(I + 1)->InPPDirective || (I + 1)->First->HasUnescapedNewline)
Daniel Jasper2ab0d012013-01-14 15:52:06 +00001489 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001490 if (I + 2 != E && (I + 2)->InPPDirective &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001491 !(I + 2)->First->HasUnescapedNewline)
Daniel Jasper39825ea2013-01-14 15:40:57 +00001492 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001493 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jaspera67a8f02013-01-16 10:41:46 +00001494 return;
Daniel Jasper39825ea2013-01-14 15:40:57 +00001495 join(Line, *(++I));
1496 }
1497
Daniel Jasper3a685df2013-05-16 12:12:21 +00001498 void tryMergeSimpleControlStatement(std::vector<AnnotatedLine>::iterator &I,
1499 std::vector<AnnotatedLine>::iterator E,
1500 unsigned Limit) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001501 if (Limit == 0)
1502 return;
Manuel Klimekda087612013-01-18 14:46:43 +00001503 if ((I + 1)->InPPDirective != I->InPPDirective ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001504 ((I + 1)->InPPDirective && (I + 1)->First->HasUnescapedNewline))
Manuel Klimekda087612013-01-18 14:46:43 +00001505 return;
Daniel Jasper25837aa2013-01-14 14:14:23 +00001506 AnnotatedLine &Line = *I;
Daniel Jasperc36492b2013-01-16 07:02:34 +00001507 if (Line.Last->isNot(tok::r_paren))
1508 return;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001509 if (1 + (I + 1)->Last->TotalLength > Limit)
Daniel Jasper25837aa2013-01-14 14:14:23 +00001510 return;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001511 if ((I + 1)->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for,
1512 tok::kw_while) ||
1513 (I + 1)->First->Type == TT_LineComment)
Daniel Jasper25837aa2013-01-14 14:14:23 +00001514 return;
1515 // Only inline simple if's (no nested if or else).
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001516 if (I + 2 != E && Line.First->is(tok::kw_if) &&
1517 (I + 2)->First->is(tok::kw_else))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001518 return;
1519 join(Line, *(++I));
1520 }
1521
1522 void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
Daniel Jasperbbc84152013-01-29 11:27:30 +00001523 std::vector<AnnotatedLine>::iterator E,
1524 unsigned Limit) {
Daniel Jasperabca58c2013-05-15 14:09:55 +00001525 // No merging if the brace already is on the next line.
1526 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
1527 return;
1528
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001529 // First, check that the current line allows merging. This is the case if
1530 // we're not in a control flow statement and the last token is an opening
1531 // brace.
Daniel Jasper25837aa2013-01-14 14:14:23 +00001532 AnnotatedLine &Line = *I;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001533 if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
1534 tok::kw_else, tok::kw_try, tok::kw_catch,
Daniel Jaspera9eb2aa2013-05-31 14:56:20 +00001535 tok::kw_for,
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001536 // This gets rid of all ObjC @ keywords and methods.
1537 tok::at, tok::minus, tok::plus))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001538 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001539
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001540 FormatToken *Tok = (I + 1)->First;
Daniel Jaspera9eb2aa2013-05-31 14:56:20 +00001541 if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
Alexander Kornienko1efe0a02013-07-04 14:47:51 +00001542 (Tok->getNextNonComment() == NULL ||
1543 Tok->getNextNonComment()->is(tok::semi))) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001544 // We merge empty blocks even if the line exceeds the column limit.
Daniel Jaspereef30492013-02-11 12:36:37 +00001545 Tok->SpacesRequiredBefore = 0;
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001546 Tok->CanBreakBefore = true;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001547 join(Line, *(I + 1));
1548 I += 1;
Daniel Jaspera9eb2aa2013-05-31 14:56:20 +00001549 } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) {
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001550 // Check that we still have three lines and they fit into the limit.
1551 if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
1552 !nextTwoLinesFitInto(I, Limit))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001553 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001554
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001555 // Second, check that the next line does not contain any braces - if it
1556 // does, readability declines when putting it into a single line.
1557 if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
1558 return;
1559 do {
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001560 if (Tok->isOneOf(tok::l_brace, tok::r_brace))
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001561 return;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001562 Tok = Tok->Next;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001563 } while (Tok != NULL);
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001564
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001565 // Last, check that the third line contains a single closing brace.
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001566 Tok = (I + 2)->First;
Alexander Kornienko1efe0a02013-07-04 14:47:51 +00001567 if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) ||
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001568 Tok->MustBreakBefore)
1569 return;
1570
1571 join(Line, *(I + 1));
1572 join(Line, *(I + 2));
1573 I += 2;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001574 }
Daniel Jasper25837aa2013-01-14 14:14:23 +00001575 }
1576
1577 bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
1578 unsigned Limit) {
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001579 return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
1580 Limit;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001581 }
1582
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001583 void join(AnnotatedLine &A, const AnnotatedLine &B) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001584 assert(!A.Last->Next);
1585 assert(!B.First->Previous);
1586 A.Last->Next = B.First;
1587 B.First->Previous = A.Last;
1588 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
1589 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
1590 Tok->TotalLength += LengthA;
1591 A.Last = Tok;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001592 }
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001593 }
1594
Daniel Jasper97b89482013-03-13 07:49:51 +00001595 bool touchesRanges(const CharSourceRange &Range) {
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001596 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1597 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
1598 Ranges[i].getBegin()) &&
1599 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1600 Range.getBegin()))
1601 return true;
1602 }
1603 return false;
1604 }
1605
1606 bool touchesLine(const AnnotatedLine &TheLine) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001607 const FormatToken *First = TheLine.First;
1608 const FormatToken *Last = TheLine.Last;
Daniel Jaspercdd06622013-05-14 10:31:09 +00001609 CharSourceRange LineRange = CharSourceRange::getCharRange(
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001610 First->WhitespaceRange.getBegin().getLocWithOffset(
1611 First->LastNewlineOffset),
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001612 Last->Tok.getLocation().getLocWithOffset(Last->TokenText.size() - 1));
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001613 return touchesRanges(LineRange);
1614 }
1615
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001616 bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I,
1617 std::vector<AnnotatedLine>::iterator E) {
1618 for (; I != E; ++I) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001619 if (I->First->HasUnescapedNewline)
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001620 return false;
1621 if (touchesLine(*I))
1622 return true;
1623 }
1624 return false;
1625 }
1626
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001627 bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001628 const FormatToken *First = TheLine.First;
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001629 CharSourceRange LineRange = CharSourceRange::getCharRange(
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001630 First->WhitespaceRange.getBegin(),
1631 First->WhitespaceRange.getBegin().getLocWithOffset(
1632 First->LastNewlineOffset));
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001633 return touchesRanges(LineRange);
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001634 }
1635
1636 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001637 AnnotatedLines.push_back(AnnotatedLine(TheLine));
Daniel Jasperf7935112012-12-03 18:12:45 +00001638 }
1639
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001640 /// \brief Add a new line and the required indent before the first Token
1641 /// of the \c UnwrappedLine if there was no structural parsing error.
1642 /// Returns the indent level of the \c UnwrappedLine.
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001643 void formatFirstToken(const FormatToken &RootToken,
1644 const FormatToken *PreviousToken, unsigned Indent,
Manuel Klimek4fe43002013-05-22 12:51:29 +00001645 bool InPPDirective) {
Daniel Jasperbbc84152013-01-29 11:27:30 +00001646 unsigned Newlines =
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001647 std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
Daniel Jasper1027c6e2013-06-03 16:16:41 +00001648 // Remove empty lines before "}" where applicable.
1649 if (RootToken.is(tok::r_brace) &&
1650 (!RootToken.Next ||
1651 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
1652 Newlines = std::min(Newlines, 1u);
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001653 if (Newlines == 0 && !RootToken.IsFirst)
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001654 Newlines = 1;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001655
Manuel Klimek4fe43002013-05-22 12:51:29 +00001656 // Insert extra new line before access specifiers.
1657 if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001658 RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
Manuel Klimek4fe43002013-05-22 12:51:29 +00001659 ++Newlines;
Alexander Kornienkofd433362013-03-27 17:08:02 +00001660
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001661 Whitespaces.replaceWhitespace(
1662 RootToken, Newlines, Indent, Indent,
1663 InPPDirective && !RootToken.HasUnescapedNewline);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001664 }
1665
Daniel Jasperf7935112012-12-03 18:12:45 +00001666 FormatStyle Style;
1667 Lexer &Lex;
1668 SourceManager &SourceMgr;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001669 WhitespaceManager Whitespaces;
Daniel Jasperf7935112012-12-03 18:12:45 +00001670 std::vector<CharSourceRange> Ranges;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001671 std::vector<AnnotatedLine> AnnotatedLines;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001672
1673 encoding::Encoding Encoding;
Daniel Jasperf7935112012-12-03 18:12:45 +00001674};
1675
Craig Topperaf35e852013-06-30 22:29:28 +00001676} // end anonymous namespace
1677
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00001678tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
1679 SourceManager &SourceMgr,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +00001680 std::vector<CharSourceRange> Ranges) {
1681 Formatter formatter(Style, Lex, SourceMgr, Ranges);
Daniel Jasperf7935112012-12-03 18:12:45 +00001682 return formatter.format();
1683}
1684
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001685tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
1686 std::vector<tooling::Range> Ranges,
1687 StringRef FileName) {
1688 FileManager Files((FileSystemOptions()));
1689 DiagnosticsEngine Diagnostics(
1690 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1691 new DiagnosticOptions);
1692 SourceManager SourceMgr(Diagnostics, Files);
1693 llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName);
1694 const clang::FileEntry *Entry =
1695 Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
1696 SourceMgr.overrideFileContents(Entry, Buf);
1697 FileID ID =
1698 SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
Alexander Kornienko1e808872013-06-28 12:51:24 +00001699 Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr,
1700 getFormattingLangOpts(Style.Standard));
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001701 SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
1702 std::vector<CharSourceRange> CharRanges;
1703 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1704 SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset());
1705 SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength());
1706 CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1707 }
1708 return reformat(Style, Lex, SourceMgr, CharRanges);
1709}
1710
Alexander Kornienko1e808872013-06-28 12:51:24 +00001711LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) {
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001712 LangOptions LangOpts;
1713 LangOpts.CPlusPlus = 1;
Alexander Kornienko1e808872013-06-28 12:51:24 +00001714 LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
Daniel Jasper55213652013-03-22 10:01:29 +00001715 LangOpts.LineComment = 1;
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001716 LangOpts.Bool = 1;
1717 LangOpts.ObjC1 = 1;
1718 LangOpts.ObjC2 = 1;
1719 return LangOpts;
1720}
1721
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001722} // namespace format
1723} // namespace clang