blob: aac9fffb01e3edf906f1bcf4e70834f0af3ae468 [file] [log] [blame]
Daniel Jasperbac016b2012-12-03 18:12:45 +00001//===--- Format.cpp - Format C++ code -------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file implements functions declared in Format.h. This will be
12/// split into separate files as we go.
13///
Daniel Jasperbac016b2012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Manuel Klimekca547db2013-01-16 14:55:28 +000016#define DEBUG_TYPE "format-formatter"
17
Daniel Jasper6b2afe42013-08-16 11:20:30 +000018#include "ContinuationIndenter.h"
Daniel Jasper32d28ee2013-01-29 21:01:14 +000019#include "TokenAnnotator.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "UnwrappedLineParser.h"
Alexander Kornienko70ce7882013-04-15 14:28:00 +000021#include "WhitespaceManager.h"
Daniel Jasper8a999452013-05-16 10:40:07 +000022#include "clang/Basic/Diagnostic.h"
Chandler Carruthb99083e2013-01-02 10:28:36 +000023#include "clang/Basic/SourceManager.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000024#include "clang/Format/Format.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000025#include "clang/Lex/Lexer.h"
Alexander Kornienko5262dd92013-03-27 11:52:18 +000026#include "llvm/ADT/STLExtras.h"
Manuel Klimek32a2fd72013-02-13 10:46:36 +000027#include "llvm/Support/Allocator.h"
Manuel Klimekca547db2013-01-16 14:55:28 +000028#include "llvm/Support/Debug.h"
Alexander Kornienkod71ec162013-05-07 15:32:14 +000029#include "llvm/Support/YAMLTraits.h"
Edwin Vanef4e12c82013-09-30 13:31:48 +000030#include "llvm/Support/Path.h"
Manuel Klimek32a2fd72013-02-13 10:46:36 +000031#include <queue>
Daniel Jasper8822d3a2012-12-04 13:02:32 +000032#include <string>
33
Alexander Kornienkod71ec162013-05-07 15:32:14 +000034namespace llvm {
35namespace yaml {
36template <>
37struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> {
Manuel Klimek44135b82013-05-13 12:51:40 +000038 static void enumeration(IO &IO,
39 clang::format::FormatStyle::LanguageStandard &Value) {
Alexander Kornienko9321e872013-09-04 14:09:13 +000040 IO.enumCase(Value, "Cpp03", clang::format::FormatStyle::LS_Cpp03);
Manuel Klimek44135b82013-05-13 12:51:40 +000041 IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03);
Alexander Kornienko9321e872013-09-04 14:09:13 +000042 IO.enumCase(Value, "Cpp11", clang::format::FormatStyle::LS_Cpp11);
Manuel Klimek44135b82013-05-13 12:51:40 +000043 IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11);
44 IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto);
45 }
46};
47
Daniel Jasper1fb8d882013-05-14 09:30:02 +000048template <>
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +000049struct ScalarEnumerationTraits<clang::format::FormatStyle::UseTabStyle> {
50 static void
51 enumeration(IO &IO, clang::format::FormatStyle::UseTabStyle &Value) {
52 IO.enumCase(Value, "Never", clang::format::FormatStyle::UT_Never);
53 IO.enumCase(Value, "false", clang::format::FormatStyle::UT_Never);
54 IO.enumCase(Value, "Always", clang::format::FormatStyle::UT_Always);
55 IO.enumCase(Value, "true", clang::format::FormatStyle::UT_Always);
56 IO.enumCase(Value, "ForIndentation",
57 clang::format::FormatStyle::UT_ForIndentation);
58 }
59};
60
61template <>
Manuel Klimek44135b82013-05-13 12:51:40 +000062struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> {
63 static void
64 enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) {
65 IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach);
66 IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux);
67 IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup);
Manuel Klimeke4907052013-08-02 21:31:59 +000068 IO.enumCase(Value, "Allman", clang::format::FormatStyle::BS_Allman);
Alexander Kornienkod71ec162013-05-07 15:32:14 +000069 }
70};
71
Daniel Jaspereff18b92013-07-31 23:16:02 +000072template <>
73struct ScalarEnumerationTraits<
74 clang::format::FormatStyle::NamespaceIndentationKind> {
75 static void
76 enumeration(IO &IO,
77 clang::format::FormatStyle::NamespaceIndentationKind &Value) {
78 IO.enumCase(Value, "None", clang::format::FormatStyle::NI_None);
79 IO.enumCase(Value, "Inner", clang::format::FormatStyle::NI_Inner);
80 IO.enumCase(Value, "All", clang::format::FormatStyle::NI_All);
81 }
82};
83
Alexander Kornienkod71ec162013-05-07 15:32:14 +000084template <> struct MappingTraits<clang::format::FormatStyle> {
85 static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) {
Alexander Kornienkodd256312013-05-10 11:56:10 +000086 if (IO.outputting()) {
Alexander Kornienko4e65c982013-09-02 16:39:23 +000087 StringRef StylesArray[] = { "LLVM", "Google", "Chromium",
88 "Mozilla", "WebKit" };
Alexander Kornienkodd256312013-05-10 11:56:10 +000089 ArrayRef<StringRef> Styles(StylesArray);
90 for (size_t i = 0, e = Styles.size(); i < e; ++i) {
91 StringRef StyleName(Styles[i]);
Alexander Kornienko885f87b2013-05-19 00:53:30 +000092 clang::format::FormatStyle PredefinedStyle;
93 if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) &&
94 Style == PredefinedStyle) {
Alexander Kornienkodd256312013-05-10 11:56:10 +000095 IO.mapOptional("# BasedOnStyle", StyleName);
96 break;
97 }
98 }
99 } else {
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000100 StringRef BasedOnStyle;
101 IO.mapOptional("BasedOnStyle", BasedOnStyle);
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000102 if (!BasedOnStyle.empty())
Alexander Kornienko885f87b2013-05-19 00:53:30 +0000103 if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) {
104 IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
105 return;
106 }
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000107 }
108
109 IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
Daniel Jasper6315fec2013-08-13 10:58:30 +0000110 IO.mapOptional("ConstructorInitializerIndentWidth",
111 Style.ConstructorInitializerIndentWidth);
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000112 IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
Daniel Jasper893ea8d2013-07-31 23:55:15 +0000113 IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000114 IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
115 Style.AllowAllParametersOfDeclarationOnNextLine);
116 IO.mapOptional("AllowShortIfStatementsOnASingleLine",
117 Style.AllowShortIfStatementsOnASingleLine);
Daniel Jasperf11bbb92013-05-16 12:12:21 +0000118 IO.mapOptional("AllowShortLoopsOnASingleLine",
119 Style.AllowShortLoopsOnASingleLine);
Daniel Jasperbbc87762013-05-29 12:07:31 +0000120 IO.mapOptional("AlwaysBreakTemplateDeclarations",
121 Style.AlwaysBreakTemplateDeclarations);
Alexander Kornienko56312022013-07-04 12:02:44 +0000122 IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
123 Style.AlwaysBreakBeforeMultilineStrings);
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000124 IO.mapOptional("BreakBeforeBinaryOperators",
125 Style.BreakBeforeBinaryOperators);
126 IO.mapOptional("BreakConstructorInitializersBeforeComma",
127 Style.BreakConstructorInitializersBeforeComma);
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000128 IO.mapOptional("BinPackParameters", Style.BinPackParameters);
129 IO.mapOptional("ColumnLimit", Style.ColumnLimit);
130 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
131 Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
132 IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000133 IO.mapOptional("ExperimentalAutoDetectBinPacking",
134 Style.ExperimentalAutoDetectBinPacking);
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000135 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
136 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000137 IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000138 IO.mapOptional("ObjCSpaceBeforeProtocolList",
139 Style.ObjCSpaceBeforeProtocolList);
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000140 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
141 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
Daniel Jasperfaec47b2013-07-11 20:41:21 +0000142 IO.mapOptional("PenaltyBreakFirstLessLess",
143 Style.PenaltyBreakFirstLessLess);
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000144 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
145 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
146 Style.PenaltyReturnTypeOnItsOwnLine);
147 IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
148 IO.mapOptional("SpacesBeforeTrailingComments",
149 Style.SpacesBeforeTrailingComments);
Daniel Jasperb5dc3f42013-07-16 18:22:10 +0000150 IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000151 IO.mapOptional("Standard", Style.Standard);
Manuel Klimek07a64ec2013-05-13 08:42:42 +0000152 IO.mapOptional("IndentWidth", Style.IndentWidth);
Alexander Kornienko0b62cc32013-09-05 14:08:34 +0000153 IO.mapOptional("TabWidth", Style.TabWidth);
Manuel Klimek7c9a93e2013-05-13 09:22:11 +0000154 IO.mapOptional("UseTab", Style.UseTab);
Manuel Klimek44135b82013-05-13 12:51:40 +0000155 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
Manuel Klimeka9a7f102013-06-21 17:25:42 +0000156 IO.mapOptional("IndentFunctionDeclarationAfterType",
157 Style.IndentFunctionDeclarationAfterType);
Daniel Jasper7df56bf2013-08-20 12:36:34 +0000158 IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
Daniel Jasper34f3d052013-08-21 08:39:01 +0000159 IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
Daniel Jasper7df56bf2013-08-20 12:36:34 +0000160 IO.mapOptional("SpacesInCStyleCastParentheses",
161 Style.SpacesInCStyleCastParentheses);
162 IO.mapOptional("SpaceAfterControlStatementKeyword",
163 Style.SpaceAfterControlStatementKeyword);
Daniel Jasper9b4de852013-09-25 15:15:02 +0000164 IO.mapOptional("SpaceBeforeAssignmentOperators",
165 Style.SpaceBeforeAssignmentOperators);
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000166 }
167};
168}
169}
170
Daniel Jasperbac016b2012-12-03 18:12:45 +0000171namespace clang {
172namespace format {
173
Daniel Jasperfaec47b2013-07-11 20:41:21 +0000174void setDefaultPenalties(FormatStyle &Style) {
Daniel Jasperf5461782013-08-28 10:03:58 +0000175 Style.PenaltyBreakComment = 60;
Daniel Jasper9637dda2013-07-15 14:33:14 +0000176 Style.PenaltyBreakFirstLessLess = 120;
Daniel Jasperfaec47b2013-07-11 20:41:21 +0000177 Style.PenaltyBreakString = 1000;
178 Style.PenaltyExcessCharacter = 1000000;
179}
180
Daniel Jasperbac016b2012-12-03 18:12:45 +0000181FormatStyle getLLVMStyle() {
182 FormatStyle LLVMStyle;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000183 LLVMStyle.AccessModifierOffset = -2;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000184 LLVMStyle.AlignEscapedNewlinesLeft = false;
Daniel Jasper893ea8d2013-07-31 23:55:15 +0000185 LLVMStyle.AlignTrailingComments = true;
Daniel Jasperf1579602013-01-29 16:03:49 +0000186 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000187 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasperf11bbb92013-05-16 12:12:21 +0000188 LLVMStyle.AllowShortLoopsOnASingleLine = false;
Alexander Kornienko56312022013-07-04 12:02:44 +0000189 LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000190 LLVMStyle.AlwaysBreakTemplateDeclarations = false;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000191 LLVMStyle.BinPackParameters = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000192 LLVMStyle.BreakBeforeBinaryOperators = false;
193 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
194 LLVMStyle.BreakConstructorInitializersBeforeComma = false;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000195 LLVMStyle.ColumnLimit = 80;
196 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Daniel Jasper6315fec2013-08-13 10:58:30 +0000197 LLVMStyle.ConstructorInitializerIndentWidth = 4;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000198 LLVMStyle.Cpp11BracedListStyle = false;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000199 LLVMStyle.DerivePointerBinding = false;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000200 LLVMStyle.ExperimentalAutoDetectBinPacking = false;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000201 LLVMStyle.IndentCaseLabels = false;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000202 LLVMStyle.IndentFunctionDeclarationAfterType = false;
203 LLVMStyle.IndentWidth = 2;
Alexander Kornienko0b62cc32013-09-05 14:08:34 +0000204 LLVMStyle.TabWidth = 8;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000205 LLVMStyle.MaxEmptyLinesToKeep = 1;
Daniel Jaspereff18b92013-07-31 23:16:02 +0000206 LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
Nico Weber5f500df2013-01-10 20:12:55 +0000207 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000208 LLVMStyle.PointerBindsToType = false;
209 LLVMStyle.SpacesBeforeTrailingComments = 1;
210 LLVMStyle.Standard = FormatStyle::LS_Cpp03;
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000211 LLVMStyle.UseTab = FormatStyle::UT_Never;
Daniel Jasper7df56bf2013-08-20 12:36:34 +0000212 LLVMStyle.SpacesInParentheses = false;
213 LLVMStyle.SpaceInEmptyParentheses = false;
214 LLVMStyle.SpacesInCStyleCastParentheses = false;
215 LLVMStyle.SpaceAfterControlStatementKeyword = true;
Daniel Jasper9b4de852013-09-25 15:15:02 +0000216 LLVMStyle.SpaceBeforeAssignmentOperators = true;
Daniel Jasperfaec47b2013-07-11 20:41:21 +0000217
218 setDefaultPenalties(LLVMStyle);
219 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
220
Daniel Jasperbac016b2012-12-03 18:12:45 +0000221 return LLVMStyle;
222}
223
224FormatStyle getGoogleStyle() {
225 FormatStyle GoogleStyle;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000226 GoogleStyle.AccessModifierOffset = -1;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000227 GoogleStyle.AlignEscapedNewlinesLeft = true;
Daniel Jasper893ea8d2013-07-31 23:55:15 +0000228 GoogleStyle.AlignTrailingComments = true;
Daniel Jasperf1579602013-01-29 16:03:49 +0000229 GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper94d6ad72013-04-24 13:46:00 +0000230 GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
Daniel Jasper1bee0732013-05-23 18:05:18 +0000231 GoogleStyle.AllowShortLoopsOnASingleLine = true;
Alexander Kornienko56312022013-07-04 12:02:44 +0000232 GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000233 GoogleStyle.AlwaysBreakTemplateDeclarations = true;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000234 GoogleStyle.BinPackParameters = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000235 GoogleStyle.BreakBeforeBinaryOperators = false;
236 GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
237 GoogleStyle.BreakConstructorInitializersBeforeComma = false;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000238 GoogleStyle.ColumnLimit = 80;
239 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
Daniel Jasper6315fec2013-08-13 10:58:30 +0000240 GoogleStyle.ConstructorInitializerIndentWidth = 4;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000241 GoogleStyle.Cpp11BracedListStyle = true;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000242 GoogleStyle.DerivePointerBinding = true;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000243 GoogleStyle.ExperimentalAutoDetectBinPacking = false;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000244 GoogleStyle.IndentCaseLabels = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000245 GoogleStyle.IndentFunctionDeclarationAfterType = true;
246 GoogleStyle.IndentWidth = 2;
Alexander Kornienko0b62cc32013-09-05 14:08:34 +0000247 GoogleStyle.TabWidth = 8;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000248 GoogleStyle.MaxEmptyLinesToKeep = 1;
Daniel Jaspereff18b92013-07-31 23:16:02 +0000249 GoogleStyle.NamespaceIndentation = FormatStyle::NI_None;
Nico Weber5f500df2013-01-10 20:12:55 +0000250 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Alexander Kornienkofb594862013-05-06 14:11:27 +0000251 GoogleStyle.PointerBindsToType = true;
252 GoogleStyle.SpacesBeforeTrailingComments = 2;
253 GoogleStyle.Standard = FormatStyle::LS_Auto;
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000254 GoogleStyle.UseTab = FormatStyle::UT_Never;
Daniel Jasper7df56bf2013-08-20 12:36:34 +0000255 GoogleStyle.SpacesInParentheses = false;
256 GoogleStyle.SpaceInEmptyParentheses = false;
257 GoogleStyle.SpacesInCStyleCastParentheses = false;
258 GoogleStyle.SpaceAfterControlStatementKeyword = true;
Daniel Jasper9b4de852013-09-25 15:15:02 +0000259 GoogleStyle.SpaceBeforeAssignmentOperators = true;
Daniel Jasperfaec47b2013-07-11 20:41:21 +0000260
261 setDefaultPenalties(GoogleStyle);
262 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
263
Daniel Jasperbac016b2012-12-03 18:12:45 +0000264 return GoogleStyle;
265}
266
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000267FormatStyle getChromiumStyle() {
268 FormatStyle ChromiumStyle = getGoogleStyle();
Daniel Jasperf1579602013-01-29 16:03:49 +0000269 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Daniel Jasper94d6ad72013-04-24 13:46:00 +0000270 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasperf11bbb92013-05-16 12:12:21 +0000271 ChromiumStyle.AllowShortLoopsOnASingleLine = false;
Daniel Jasperfaab0d32013-02-27 09:47:53 +0000272 ChromiumStyle.BinPackParameters = false;
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000273 ChromiumStyle.DerivePointerBinding = false;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000274 ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
Daniel Jasper6f5bb2c2013-01-14 16:24:39 +0000275 return ChromiumStyle;
276}
277
Alexander Kornienkofb594862013-05-06 14:11:27 +0000278FormatStyle getMozillaStyle() {
279 FormatStyle MozillaStyle = getLLVMStyle();
280 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
281 MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
282 MozillaStyle.DerivePointerBinding = true;
283 MozillaStyle.IndentCaseLabels = true;
284 MozillaStyle.ObjCSpaceBeforeProtocolList = false;
285 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
286 MozillaStyle.PointerBindsToType = true;
287 return MozillaStyle;
288}
289
Daniel Jaspere05dc6d2013-07-24 13:10:59 +0000290FormatStyle getWebKitStyle() {
291 FormatStyle Style = getLLVMStyle();
Daniel Jaspereff18b92013-07-31 23:16:02 +0000292 Style.AccessModifierOffset = -4;
Daniel Jasper893ea8d2013-07-31 23:55:15 +0000293 Style.AlignTrailingComments = false;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000294 Style.BreakBeforeBinaryOperators = true;
Daniel Jaspereff18b92013-07-31 23:16:02 +0000295 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000296 Style.BreakConstructorInitializersBeforeComma = true;
Daniel Jaspereff18b92013-07-31 23:16:02 +0000297 Style.ColumnLimit = 0;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000298 Style.IndentWidth = 4;
Daniel Jaspereff18b92013-07-31 23:16:02 +0000299 Style.NamespaceIndentation = FormatStyle::NI_Inner;
Daniel Jaspere05dc6d2013-07-24 13:10:59 +0000300 Style.PointerBindsToType = true;
301 return Style;
302}
303
Alexander Kornienko885f87b2013-05-19 00:53:30 +0000304bool getPredefinedStyle(StringRef Name, FormatStyle *Style) {
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000305 if (Name.equals_lower("llvm"))
Alexander Kornienko885f87b2013-05-19 00:53:30 +0000306 *Style = getLLVMStyle();
307 else if (Name.equals_lower("chromium"))
308 *Style = getChromiumStyle();
309 else if (Name.equals_lower("mozilla"))
310 *Style = getMozillaStyle();
311 else if (Name.equals_lower("google"))
312 *Style = getGoogleStyle();
Daniel Jaspere05dc6d2013-07-24 13:10:59 +0000313 else if (Name.equals_lower("webkit"))
314 *Style = getWebKitStyle();
Alexander Kornienko885f87b2013-05-19 00:53:30 +0000315 else
316 return false;
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000317
Alexander Kornienko885f87b2013-05-19 00:53:30 +0000318 return true;
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000319}
320
321llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
Alexander Kornienko107db3c2013-05-20 15:18:01 +0000322 if (Text.trim().empty())
323 return llvm::make_error_code(llvm::errc::invalid_argument);
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000324 llvm::yaml::Input Input(Text);
325 Input >> *Style;
326 return Input.error();
327}
328
329std::string configurationAsText(const FormatStyle &Style) {
330 std::string Text;
331 llvm::raw_string_ostream Stream(Text);
332 llvm::yaml::Output Output(Stream);
333 // We use the same mapping method for input and output, so we need a non-const
334 // reference here.
335 FormatStyle NonConstStyle = Style;
336 Output << NonConstStyle;
Alexander Kornienko2b6acb62013-05-13 12:56:35 +0000337 return Stream.str();
Alexander Kornienkod71ec162013-05-07 15:32:14 +0000338}
339
Craig Topper83f81d72013-06-30 22:29:28 +0000340namespace {
341
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000342class NoColumnLimitFormatter {
343public:
Daniel Jasper34f3d052013-08-21 08:39:01 +0000344 NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {}
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000345
346 /// \brief Formats the line starting at \p State, simply keeping all of the
347 /// input's line breaking decisions.
Daniel Jasper567dcf92013-09-05 09:29:45 +0000348 void format(unsigned FirstIndent, const AnnotatedLine *Line) {
Daniel Jasperb77d7412013-09-06 07:54:20 +0000349 LineState State =
350 Indenter->getInitialState(FirstIndent, Line, /*DryRun=*/false);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000351 while (State.NextToken != NULL) {
352 bool Newline =
353 Indenter->mustBreak(State) ||
354 (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
355 Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
356 }
357 }
Daniel Jasper34f3d052013-08-21 08:39:01 +0000358
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000359private:
360 ContinuationIndenter *Indenter;
361};
362
Daniel Jasperbac016b2012-12-03 18:12:45 +0000363class UnwrappedLineFormatter {
364public:
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000365 UnwrappedLineFormatter(ContinuationIndenter *Indenter,
Daniel Jasper567dcf92013-09-05 09:29:45 +0000366 WhitespaceManager *Whitespaces,
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000367 const FormatStyle &Style, const AnnotatedLine &Line)
Daniel Jasper567dcf92013-09-05 09:29:45 +0000368 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), Line(Line),
369 Count(0) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000370
Daniel Jasper567dcf92013-09-05 09:29:45 +0000371 /// \brief Formats an \c UnwrappedLine and returns the penalty.
372 ///
373 /// If \p DryRun is \c false, directly applies the changes.
374 unsigned format(unsigned FirstIndent, bool DryRun = false) {
Daniel Jasperb77d7412013-09-06 07:54:20 +0000375 LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun);
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000376
Daniel Jasperce3d1a62013-02-08 08:22:00 +0000377 // If the ObjC method declaration does not fit on a line, we should format
378 // it with one arg per line.
379 if (Line.Type == LT_ObjCMethodDecl)
380 State.Stack.back().BreakBeforeParameter = true;
381
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000382 // Find best solution in solution space.
Daniel Jasper567dcf92013-09-05 09:29:45 +0000383 return analyzeSolutionSpace(State, DryRun);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000384 }
385
386private:
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000387 /// \brief An edge in the solution space from \c Previous->State to \c State,
388 /// inserting a newline dependent on the \c NewLine.
389 struct StateNode {
390 StateNode(const LineState &State, bool NewLine, StateNode *Previous)
Daniel Jasperf11a7052013-02-21 21:33:55 +0000391 : State(State), NewLine(NewLine), Previous(Previous) {}
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000392 LineState State;
393 bool NewLine;
394 StateNode *Previous;
395 };
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000396
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000397 /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
398 ///
399 /// In case of equal penalties, we want to prefer states that were inserted
400 /// first. During state generation we make sure that we insert states first
401 /// that break the line as late as possible.
402 typedef std::pair<unsigned, unsigned> OrderedPenalty;
403
404 /// \brief An item in the prioritized BFS search queue. The \c StateNode's
405 /// \c State has the given \c OrderedPenalty.
406 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
407
408 /// \brief The BFS queue type.
409 typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
410 std::greater<QueueItem> > QueueType;
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000411
412 /// \brief Analyze the entire solution space starting from \p InitialState.
Daniel Jasperbac016b2012-12-03 18:12:45 +0000413 ///
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000414 /// This implements a variant of Dijkstra's algorithm on the graph that spans
415 /// the solution space (\c LineStates are the nodes). The algorithm tries to
416 /// find the shortest path (the one with lowest penalty) from \p InitialState
Daniel Jasper567dcf92013-09-05 09:29:45 +0000417 /// to a state where all tokens are placed. Returns the penalty.
418 ///
419 /// If \p DryRun is \c false, directly applies the changes.
420 unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun = false) {
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000421 std::set<LineState> Seen;
422
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000423 // Insert start element into queue.
Daniel Jasperfc759082013-02-14 14:26:07 +0000424 StateNode *Node =
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000425 new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
426 Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
427 ++Count;
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000428
Daniel Jasper567dcf92013-09-05 09:29:45 +0000429 unsigned Penalty = 0;
430
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000431 // While not empty, take first element and follow edges.
432 while (!Queue.empty()) {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000433 Penalty = Queue.top().first.first;
Daniel Jasperfc759082013-02-14 14:26:07 +0000434 StateNode *Node = Queue.top().second;
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000435 if (Node->State.NextToken == NULL) {
Alexander Kornienkodd256312013-05-10 11:56:10 +0000436 DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000437 break;
Daniel Jasper01786732013-02-04 07:21:18 +0000438 }
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000439 Queue.pop();
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000440
Daniel Jasper54b4e442013-05-22 05:27:42 +0000441 // Cut off the analysis of certain solutions if the analysis gets too
442 // complex. See description of IgnoreStackForComparison.
443 if (Count > 10000)
444 Node->State.IgnoreStackForComparison = true;
445
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000446 if (!Seen.insert(Node->State).second)
447 // State already examined with lower penalty.
448 continue;
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000449
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000450 FormatDecision LastFormat = Node->State.NextToken->Decision;
451 if (LastFormat == FD_Unformatted || LastFormat == FD_Continue)
452 addNextStateToQueue(Penalty, Node, /*NewLine=*/false);
453 if (LastFormat == FD_Unformatted || LastFormat == FD_Break)
454 addNextStateToQueue(Penalty, Node, /*NewLine=*/true);
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000455 }
456
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000457 if (Queue.empty()) {
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000458 // We were unable to find a solution, do nothing.
459 // FIXME: Add diagnostic?
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000460 DEBUG(llvm::dbgs() << "Could not find a solution.\n");
Daniel Jasper567dcf92013-09-05 09:29:45 +0000461 return 0;
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000462 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000463
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000464 // Reconstruct the solution.
Daniel Jasper567dcf92013-09-05 09:29:45 +0000465 if (!DryRun)
466 reconstructPath(InitialState, Queue.top().second);
467
Alexander Kornienkodd256312013-05-10 11:56:10 +0000468 DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
469 DEBUG(llvm::dbgs() << "---\n");
Daniel Jasper567dcf92013-09-05 09:29:45 +0000470
471 return Penalty;
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000472 }
473
474 void reconstructPath(LineState &State, StateNode *Current) {
Manuel Klimek9c333b92013-05-29 15:10:11 +0000475 std::deque<StateNode *> Path;
476 // We do not need a break before the initial token.
477 while (Current->Previous) {
478 Path.push_front(Current);
479 Current = Current->Previous;
480 }
481 for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
482 I != E; ++I) {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000483 unsigned Penalty = 0;
484 formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty);
485 Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false);
486
Manuel Klimek9c333b92013-05-29 15:10:11 +0000487 DEBUG({
488 if ((*I)->NewLine) {
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000489 llvm::dbgs() << "Penalty for placing "
Manuel Klimek9c333b92013-05-29 15:10:11 +0000490 << (*I)->Previous->State.NextToken->Tok.getName() << ": "
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000491 << Penalty << "\n";
Manuel Klimek9c333b92013-05-29 15:10:11 +0000492 }
493 });
Manuel Klimek9c333b92013-05-29 15:10:11 +0000494 }
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000495 }
496
Manuel Klimek62a48fb2013-02-13 10:54:19 +0000497 /// \brief Add the following state to the analysis queue \c Queue.
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000498 ///
Manuel Klimek62a48fb2013-02-13 10:54:19 +0000499 /// Assume the current state is \p PreviousNode and has been reached with a
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000500 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
Manuel Klimek62a48fb2013-02-13 10:54:19 +0000501 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
502 bool NewLine) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000503 if (NewLine && !Indenter->canBreak(PreviousNode->State))
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000504 return;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000505 if (!NewLine && Indenter->mustBreak(PreviousNode->State))
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000506 return;
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000507
508 StateNode *Node = new (Allocator.Allocate())
509 StateNode(PreviousNode->State, NewLine, PreviousNode);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000510 if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty))
511 return;
512
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000513 Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
Manuel Klimek32a2fd72013-02-13 10:46:36 +0000514
515 Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
516 ++Count;
Daniel Jasper68ef0df2013-02-01 11:00:45 +0000517 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000518
Daniel Jasper1a925bc2013-09-05 10:48:50 +0000519 /// \brief If the \p State's next token is an r_brace closing a nested block,
520 /// format the nested block before it.
Daniel Jasper567dcf92013-09-05 09:29:45 +0000521 ///
522 /// Returns \c true if all children could be placed successfully and adapts
523 /// \p Penalty as well as \p State. If \p DryRun is false, also directly
524 /// creates changes using \c Whitespaces.
525 ///
526 /// The crucial idea here is that children always get formatted upon
527 /// encountering the closing brace right after the nested block. Now, if we
528 /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is
529 /// \c false), the entire block has to be kept on the same line (which is only
530 /// possible if it fits on the line, only contains a single statement, etc.
531 ///
532 /// If \p NewLine is true, we format the nested block on separate lines, i.e.
533 /// break after the "{", format all lines with correct indentation and the put
534 /// the closing "}" on yet another new line.
535 ///
536 /// This enables us to keep the simple structure of the
537 /// \c UnwrappedLineFormatter, where we only have two options for each token:
538 /// break or don't break.
539 bool formatChildren(LineState &State, bool NewLine, bool DryRun,
540 unsigned &Penalty) {
541 const FormatToken &LBrace = *State.NextToken->Previous;
542 if (LBrace.isNot(tok::l_brace) || LBrace.BlockKind != BK_Block ||
543 LBrace.Children.size() == 0)
Daniel Jasper1a925bc2013-09-05 10:48:50 +0000544 // The previous token does not open a block. Nothing to do. We don't
545 // assert so that we can simply call this function for all tokens.
Daniel Jasper2f0a0202013-09-06 08:54:24 +0000546 return true;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000547
548 if (NewLine) {
549 unsigned ParentIndent = State.Stack.back().Indent;
550 for (SmallVector<AnnotatedLine *, 1>::const_iterator
551 I = LBrace.Children.begin(),
552 E = LBrace.Children.end();
553 I != E; ++I) {
554 unsigned Indent =
Daniel Jasper57981202013-09-13 09:20:45 +0000555 ParentIndent + ((*I)->Level - Line.Level - 1) * Style.IndentWidth;
Daniel Jasper14e25c02013-09-08 14:07:57 +0000556 if (!DryRun) {
557 unsigned Newlines = std::min((*I)->First->NewlinesBefore,
558 Style.MaxEmptyLinesToKeep + 1);
559 Newlines = std::max(1u, Newlines);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000560 Whitespaces->replaceWhitespace(
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000561 *(*I)->First, Newlines, (*I)->Level, /*Spaces=*/Indent,
Daniel Jasper567dcf92013-09-05 09:29:45 +0000562 /*StartOfTokenColumn=*/Indent, Line.InPPDirective);
Daniel Jasper14e25c02013-09-08 14:07:57 +0000563 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000564 UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style, **I);
565 Penalty += Formatter.format(Indent, DryRun);
566 }
567 return true;
568 }
569
570 if (LBrace.Children.size() > 1)
571 return false; // Cannot merge multiple statements into a single line.
572
573 // We can't put the closing "}" on a line with a trailing comment.
574 if (LBrace.Children[0]->Last->isTrailingComment())
575 return false;
576
577 if (!DryRun) {
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000578 Whitespaces->replaceWhitespace(
579 *LBrace.Children[0]->First,
580 /*Newlines=*/0, /*IndentLevel=*/1, /*Spaces=*/1,
581 /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective);
Daniel Jasper14e25c02013-09-08 14:07:57 +0000582 UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style,
583 *LBrace.Children[0]);
584 Penalty += Formatter.format(State.Column + 1, DryRun);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000585 }
586
587 State.Column += 1 + LBrace.Children[0]->Last->TotalLength;
588 return true;
589 }
590
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000591 ContinuationIndenter *Indenter;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000592 WhitespaceManager *Whitespaces;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000593 FormatStyle Style;
Daniel Jasper995e8202013-01-14 13:08:07 +0000594 const AnnotatedLine &Line;
Manuel Klimek62a48fb2013-02-13 10:54:19 +0000595
596 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
597 QueueType Queue;
598 // Increasing count of \c StateNode items we have created. This is used
599 // to create a deterministic order independent of the container.
600 unsigned Count;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000601};
602
Manuel Klimek96e888b2013-05-28 11:55:06 +0000603class FormatTokenLexer {
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000604public:
Manuel Klimekc41e8192013-08-29 15:21:40 +0000605 FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr, FormatStyle &Style,
Alexander Kornienko00895102013-06-05 14:09:10 +0000606 encoding::Encoding Encoding)
Manuel Klimekc41e8192013-08-29 15:21:40 +0000607 : FormatTok(NULL), GreaterStashed(false), Column(0),
608 TrailingWhitespace(0), Lex(Lex), SourceMgr(SourceMgr), Style(Style),
609 IdentTable(getFormattingLangOpts()), Encoding(Encoding) {
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000610 Lex.SetKeepWhitespaceMode(true);
611 }
612
Manuel Klimek96e888b2013-05-28 11:55:06 +0000613 ArrayRef<FormatToken *> lex() {
614 assert(Tokens.empty());
615 do {
616 Tokens.push_back(getNextToken());
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000617 maybeJoinPreviousTokens();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000618 } while (Tokens.back()->Tok.isNot(tok::eof));
619 return Tokens;
620 }
621
622 IdentifierTable &getIdentTable() { return IdentTable; }
623
624private:
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000625 void maybeJoinPreviousTokens() {
626 if (Tokens.size() < 4)
627 return;
628 FormatToken *Last = Tokens.back();
629 if (!Last->is(tok::r_paren))
630 return;
631
632 FormatToken *String = Tokens[Tokens.size() - 2];
633 if (!String->is(tok::string_literal) || String->IsMultiline)
634 return;
635
636 if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
637 return;
638
639 FormatToken *Macro = Tokens[Tokens.size() - 4];
640 if (Macro->TokenText != "_T")
641 return;
642
643 const char *Start = Macro->TokenText.data();
644 const char *End = Last->TokenText.data() + Last->TokenText.size();
645 String->TokenText = StringRef(Start, End - Start);
646 String->IsFirst = Macro->IsFirst;
647 String->LastNewlineOffset = Macro->LastNewlineOffset;
648 String->WhitespaceRange = Macro->WhitespaceRange;
649 String->OriginalColumn = Macro->OriginalColumn;
650 String->ColumnWidth = encoding::columnWidthWithTabs(
651 String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding);
652
653 Tokens.pop_back();
654 Tokens.pop_back();
655 Tokens.pop_back();
656 Tokens.back() = String;
657 }
658
Manuel Klimek96e888b2013-05-28 11:55:06 +0000659 FormatToken *getNextToken() {
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000660 if (GreaterStashed) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +0000661 // Create a synthesized second '>' token.
Manuel Klimekc41e8192013-08-29 15:21:40 +0000662 // FIXME: Increment Column and set OriginalColumn.
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +0000663 Token Greater = FormatTok->Tok;
664 FormatTok = new (Allocator.Allocate()) FormatToken;
665 FormatTok->Tok = Greater;
Manuel Klimekad3094b2013-05-23 10:56:37 +0000666 SourceLocation GreaterLocation =
Manuel Klimek96e888b2013-05-28 11:55:06 +0000667 FormatTok->Tok.getLocation().getLocWithOffset(1);
668 FormatTok->WhitespaceRange =
669 SourceRange(GreaterLocation, GreaterLocation);
Alexander Kornienko54e6c9d2013-06-07 17:45:07 +0000670 FormatTok->TokenText = ">";
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000671 FormatTok->ColumnWidth = 1;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000672 GreaterStashed = false;
673 return FormatTok;
674 }
675
Manuel Klimek96e888b2013-05-28 11:55:06 +0000676 FormatTok = new (Allocator.Allocate()) FormatToken;
Daniel Jasper561211d2013-07-16 20:28:33 +0000677 readRawToken(*FormatTok);
Manuel Klimekde008c02013-05-27 15:23:34 +0000678 SourceLocation WhitespaceStart =
Manuel Klimek96e888b2013-05-28 11:55:06 +0000679 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
Manuel Klimekad3094b2013-05-23 10:56:37 +0000680 if (SourceMgr.getFileOffset(WhitespaceStart) == 0)
Manuel Klimek96e888b2013-05-28 11:55:06 +0000681 FormatTok->IsFirst = true;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000682
683 // Consume and record whitespace until we find a significant token.
Manuel Klimekde008c02013-05-27 15:23:34 +0000684 unsigned WhitespaceLength = TrailingWhitespace;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000685 while (FormatTok->Tok.is(tok::unknown)) {
Manuel Klimekc41e8192013-08-29 15:21:40 +0000686 for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) {
687 switch (FormatTok->TokenText[i]) {
688 case '\n':
689 ++FormatTok->NewlinesBefore;
690 // FIXME: This is technically incorrect, as it could also
691 // be a literal backslash at the end of the line.
Alexander Kornienko73d845c2013-09-11 12:25:57 +0000692 if (i == 0 || (FormatTok->TokenText[i - 1] != '\\' &&
693 (FormatTok->TokenText[i - 1] != '\r' || i == 1 ||
694 FormatTok->TokenText[i - 2] != '\\')))
Manuel Klimekc41e8192013-08-29 15:21:40 +0000695 FormatTok->HasUnescapedNewline = true;
696 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
697 Column = 0;
698 break;
Daniel Jasper1d82b1a2013-10-11 19:45:02 +0000699 case '\r':
700 case '\f':
701 case '\v':
702 Column = 0;
703 break;
Manuel Klimekc41e8192013-08-29 15:21:40 +0000704 case ' ':
705 ++Column;
706 break;
707 case '\t':
Alexander Kornienko0b62cc32013-09-05 14:08:34 +0000708 Column += Style.TabWidth - Column % Style.TabWidth;
Manuel Klimekc41e8192013-08-29 15:21:40 +0000709 break;
Daniel Jasper1d82b1a2013-10-11 19:45:02 +0000710 case '\\':
711 ++Column;
712 if (i + 1 == e || (FormatTok->TokenText[i + 1] != '\r' &&
713 FormatTok->TokenText[i + 1] != '\n'))
714 FormatTok->Type = TT_ImplicitStringLiteral;
715 break;
Manuel Klimekc41e8192013-08-29 15:21:40 +0000716 default:
Daniel Jasper1d82b1a2013-10-11 19:45:02 +0000717 FormatTok->Type = TT_ImplicitStringLiteral;
Manuel Klimekc41e8192013-08-29 15:21:40 +0000718 ++Column;
719 break;
720 }
721 }
722
Daniel Jasper1d82b1a2013-10-11 19:45:02 +0000723 if (FormatTok->Type == TT_ImplicitStringLiteral)
724 break;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000725 WhitespaceLength += FormatTok->Tok.getLength();
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000726
Daniel Jasper561211d2013-07-16 20:28:33 +0000727 readRawToken(*FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000728 }
Manuel Klimek95419382013-01-07 07:56:50 +0000729
Manuel Klimekd4397b92013-01-04 23:34:14 +0000730 // In case the token starts with escaped newlines, we want to
731 // take them into account as whitespace - this pattern is quite frequent
732 // in macro definitions.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000733 // FIXME: Add a more explicit test.
Daniel Jasper561211d2013-07-16 20:28:33 +0000734 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
735 FormatTok->TokenText[1] == '\n') {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000736 // FIXME: ++FormatTok->NewlinesBefore is missing...
Manuel Klimekad3094b2013-05-23 10:56:37 +0000737 WhitespaceLength += 2;
Manuel Klimekc41e8192013-08-29 15:21:40 +0000738 Column = 0;
Daniel Jasper561211d2013-07-16 20:28:33 +0000739 FormatTok->TokenText = FormatTok->TokenText.substr(2);
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000740 }
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000741
742 FormatTok->WhitespaceRange = SourceRange(
743 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
744
Manuel Klimekc41e8192013-08-29 15:21:40 +0000745 FormatTok->OriginalColumn = Column;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000746
Alexander Kornienko54e6c9d2013-06-07 17:45:07 +0000747 TrailingWhitespace = 0;
748 if (FormatTok->Tok.is(tok::comment)) {
Manuel Klimekc41e8192013-08-29 15:21:40 +0000749 // FIXME: Add the trimmed whitespace to Column.
Daniel Jasper561211d2013-07-16 20:28:33 +0000750 StringRef UntrimmedText = FormatTok->TokenText;
Alexander Kornienko51bb5d92013-09-06 17:24:54 +0000751 FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
Daniel Jasper561211d2013-07-16 20:28:33 +0000752 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
Alexander Kornienko54e6c9d2013-06-07 17:45:07 +0000753 } else if (FormatTok->Tok.is(tok::raw_identifier)) {
Daniel Jasper561211d2013-07-16 20:28:33 +0000754 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000755 FormatTok->Tok.setIdentifierInfo(&Info);
756 FormatTok->Tok.setKind(Info.getTokenID());
Alexander Kornienko54e6c9d2013-06-07 17:45:07 +0000757 } else if (FormatTok->Tok.is(tok::greatergreater)) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000758 FormatTok->Tok.setKind(tok::greater);
Daniel Jasper561211d2013-07-16 20:28:33 +0000759 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000760 GreaterStashed = true;
761 }
762
Alexander Kornienko54e6c9d2013-06-07 17:45:07 +0000763 // Now FormatTok is the next non-whitespace token.
Alexander Kornienko00895102013-06-05 14:09:10 +0000764
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000765 StringRef Text = FormatTok->TokenText;
766 size_t FirstNewlinePos = Text.find('\n');
Alexander Kornienko6f6154c2013-09-10 12:29:48 +0000767 if (FirstNewlinePos == StringRef::npos) {
768 // FIXME: ColumnWidth actually depends on the start column, we need to
769 // take this into account when the token is moved.
770 FormatTok->ColumnWidth =
771 encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding);
772 Column += FormatTok->ColumnWidth;
773 } else {
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000774 FormatTok->IsMultiline = true;
Alexander Kornienko6f6154c2013-09-10 12:29:48 +0000775 // FIXME: ColumnWidth actually depends on the start column, we need to
776 // take this into account when the token is moved.
777 FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
778 Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
779
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000780 // The last line of the token always starts in column 0.
781 // Thus, the length can be precomputed even in the presence of tabs.
782 FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
783 Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth,
784 Encoding);
Alexander Kornienko6f6154c2013-09-10 12:29:48 +0000785 Column = FormatTok->LastLineColumnWidth;
Alexander Kornienko4b762a92013-09-02 13:58:14 +0000786 }
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000787
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000788 return FormatTok;
789 }
790
Manuel Klimek96e888b2013-05-28 11:55:06 +0000791 FormatToken *FormatTok;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000792 bool GreaterStashed;
Manuel Klimekc41e8192013-08-29 15:21:40 +0000793 unsigned Column;
Manuel Klimekde008c02013-05-27 15:23:34 +0000794 unsigned TrailingWhitespace;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000795 Lexer &Lex;
796 SourceManager &SourceMgr;
Manuel Klimekc41e8192013-08-29 15:21:40 +0000797 FormatStyle &Style;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000798 IdentifierTable IdentTable;
Alexander Kornienko00895102013-06-05 14:09:10 +0000799 encoding::Encoding Encoding;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000800 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
801 SmallVector<FormatToken *, 16> Tokens;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000802
Daniel Jasper561211d2013-07-16 20:28:33 +0000803 void readRawToken(FormatToken &Tok) {
804 Lex.LexFromRawLexer(Tok.Tok);
805 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
806 Tok.Tok.getLength());
Daniel Jasper561211d2013-07-16 20:28:33 +0000807 // For formatting, treat unterminated string literals like normal string
808 // literals.
809 if (Tok.is(tok::unknown) && !Tok.TokenText.empty() &&
810 Tok.TokenText[0] == '"') {
811 Tok.Tok.setKind(tok::string_literal);
812 Tok.IsUnterminatedLiteral = true;
813 }
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000814 }
815};
816
Daniel Jasperbac016b2012-12-03 18:12:45 +0000817class Formatter : public UnwrappedLineConsumer {
818public:
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000819 Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
Daniel Jasperbac016b2012-12-03 18:12:45 +0000820 const std::vector<CharSourceRange> &Ranges)
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000821 : Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienko73d845c2013-09-11 12:25:57 +0000822 Whitespaces(SourceMgr, Style, inputUsesCRLF(Lex.getBuffer())),
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000823 Ranges(Ranges), UnwrappedLines(1),
824 Encoding(encoding::detectEncoding(Lex.getBuffer())) {
Daniel Jasper9637dda2013-07-15 14:33:14 +0000825 DEBUG(llvm::dbgs() << "File encoding: "
826 << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
827 : "unknown")
828 << "\n");
Alexander Kornienko00895102013-06-05 14:09:10 +0000829 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000830
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000831 tooling::Replacements format() {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000832 tooling::Replacements Result;
Manuel Klimekc41e8192013-08-29 15:21:40 +0000833 FormatTokenLexer Tokens(Lex, SourceMgr, Style, Encoding);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000834
835 UnwrappedLineParser Parser(Style, Tokens.lex(), *this);
Manuel Klimek67d080d2013-04-12 14:13:36 +0000836 bool StructuralError = Parser.parse();
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000837 assert(UnwrappedLines.rbegin()->empty());
838 for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE;
839 ++Run) {
840 DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
841 SmallVector<AnnotatedLine *, 16> AnnotatedLines;
842 for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) {
843 AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i]));
844 }
845 tooling::Replacements RunResult =
846 format(AnnotatedLines, StructuralError, Tokens);
847 DEBUG({
848 llvm::dbgs() << "Replacements for run " << Run << ":\n";
849 for (tooling::Replacements::iterator I = RunResult.begin(),
850 E = RunResult.end();
851 I != E; ++I) {
852 llvm::dbgs() << I->toString() << "\n";
853 }
854 });
855 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
856 delete AnnotatedLines[i];
857 }
858 Result.insert(RunResult.begin(), RunResult.end());
859 Whitespaces.reset();
860 }
861 return Result;
862 }
863
864 tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
865 bool StructuralError, FormatTokenLexer &Tokens) {
Alexander Kornienko00895102013-06-05 14:09:10 +0000866 TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in"));
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000867 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000868 Annotator.annotate(*AnnotatedLines[i]);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000869 }
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000870 deriveLocalStyle(AnnotatedLines);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000871 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000872 Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000873 }
Daniel Jasper5999f762013-04-09 17:46:55 +0000874
Daniel Jasperb77d7412013-09-06 07:54:20 +0000875 Annotator.setCommentLineLevels(AnnotatedLines);
Daniel Jasper5999f762013-04-09 17:46:55 +0000876
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000877 std::vector<int> IndentForLevel;
878 bool PreviousLineWasTouched = false;
Daniel Jasper63cfb892013-10-06 11:40:08 +0000879 const AnnotatedLine *PreviousLine = NULL;
Daniel Jasper89b3a7f2013-05-10 13:00:49 +0000880 bool FormatPPDirective = false;
Daniel Jasperb77d7412013-09-06 07:54:20 +0000881 for (SmallVectorImpl<AnnotatedLine *>::iterator I = AnnotatedLines.begin(),
882 E = AnnotatedLines.end();
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000883 I != E; ++I) {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000884 const AnnotatedLine &TheLine = **I;
Manuel Klimekb3987012013-05-29 14:47:47 +0000885 const FormatToken *FirstTok = TheLine.First;
886 int Offset = getIndentOffset(*TheLine.First);
Daniel Jasper89b3a7f2013-05-10 13:00:49 +0000887
888 // Check whether this line is part of a formatted preprocessor directive.
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +0000889 if (FirstTok->HasUnescapedNewline)
Daniel Jasper89b3a7f2013-05-10 13:00:49 +0000890 FormatPPDirective = false;
891 if (!FormatPPDirective && TheLine.InPPDirective &&
892 (touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
893 FormatPPDirective = true;
894
Daniel Jasper1fb8d882013-05-14 09:30:02 +0000895 // Determine indent and try to merge multiple unwrapped lines.
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000896 while (IndentForLevel.size() <= TheLine.Level)
897 IndentForLevel.push_back(-1);
898 IndentForLevel.resize(TheLine.Level + 1);
Daniel Jasper1fb8d882013-05-14 09:30:02 +0000899 unsigned Indent = getIndent(IndentForLevel, TheLine.Level);
900 if (static_cast<int>(Indent) + Offset >= 0)
901 Indent += Offset;
902 tryFitMultipleLinesInOne(Indent, I, E);
903
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +0000904 bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0;
Manuel Klimekb3987012013-05-29 14:47:47 +0000905 if (TheLine.First->is(tok::eof)) {
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000906 if (PreviousLineWasTouched) {
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +0000907 unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u);
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000908 Whitespaces.replaceWhitespace(*TheLine.First, NewLines,
909 /*IndentLevel=*/0, /*Spaces=*/0,
910 /*TargetColumn=*/0);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000911 }
912 } else if (TheLine.Type != LT_Invalid &&
Daniel Jasper89b3a7f2013-05-10 13:00:49 +0000913 (WasMoved || FormatPPDirective || touchesLine(TheLine))) {
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000914 unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +0000915 if (FirstTok->WhitespaceRange.isValid() &&
Manuel Klimek67d080d2013-04-12 14:13:36 +0000916 // Insert a break even if there is a structural error in case where
917 // we break apart a line consisting of multiple unwrapped lines.
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +0000918 (FirstTok->NewlinesBefore == 0 || !StructuralError)) {
Daniel Jasper63cfb892013-10-06 11:40:08 +0000919 formatFirstToken(*TheLine.First, PreviousLine, Indent,
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000920 TheLine.InPPDirective);
Manuel Klimek67d080d2013-04-12 14:13:36 +0000921 } else {
Manuel Klimekc41e8192013-08-29 15:21:40 +0000922 Indent = LevelIndent = FirstTok->OriginalColumn;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000923 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000924 ContinuationIndenter Indenter(Style, SourceMgr, Whitespaces, Encoding,
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000925 BinPackInconclusiveFunctions);
926
927 // If everything fits on a single line, just put it there.
928 unsigned ColumnLimit = Style.ColumnLimit;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000929 AnnotatedLine *NextLine = *(I + 1);
930 if ((I + 1) != E && NextLine->InPPDirective &&
931 !NextLine->First->HasUnescapedNewline)
932 ColumnLimit = getColumnLimit(TheLine.InPPDirective);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000933
Daniel Jasper567dcf92013-09-05 09:29:45 +0000934 if (TheLine.Last->TotalLength + Indent <= ColumnLimit) {
Daniel Jasperb77d7412013-09-06 07:54:20 +0000935 LineState State =
936 Indenter.getInitialState(Indent, &TheLine, /*DryRun=*/false);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000937 while (State.NextToken != NULL)
938 Indenter.addTokenToState(State, false, false);
939 } else if (Style.ColumnLimit == 0) {
940 NoColumnLimitFormatter Formatter(&Indenter);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000941 Formatter.format(Indent, &TheLine);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000942 } else {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000943 UnwrappedLineFormatter Formatter(&Indenter, &Whitespaces, Style,
944 TheLine);
945 Formatter.format(Indent);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000946 }
947
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000948 IndentForLevel[TheLine.Level] = LevelIndent;
949 PreviousLineWasTouched = true;
950 } else {
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000951 // Format the first token if necessary, and notify the WhitespaceManager
952 // about the unchanged whitespace.
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000953 for (FormatToken *Tok = TheLine.First; Tok != NULL; Tok = Tok->Next) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000954 if (Tok == TheLine.First &&
955 (Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
Manuel Klimekc41e8192013-08-29 15:21:40 +0000956 unsigned LevelIndent = Tok->OriginalColumn;
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000957 // Remove trailing whitespace of the previous line if it was
958 // touched.
959 if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) {
Daniel Jasper63cfb892013-10-06 11:40:08 +0000960 formatFirstToken(*Tok, PreviousLine, LevelIndent,
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000961 TheLine.InPPDirective);
962 } else {
Manuel Klimekb3987012013-05-29 14:47:47 +0000963 Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000964 }
Daniel Jasper1fb8d882013-05-14 09:30:02 +0000965
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000966 if (static_cast<int>(LevelIndent) - Offset >= 0)
967 LevelIndent -= Offset;
968 if (Tok->isNot(tok::comment))
969 IndentForLevel[TheLine.Level] = LevelIndent;
970 } else {
Manuel Klimekb3987012013-05-29 14:47:47 +0000971 Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000972 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000973 }
974 // If we did not reformat this unwrapped line, the column at the end of
975 // the last token is unchanged - thus, we can calculate the end of the
976 // last token.
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000977 PreviousLineWasTouched = false;
978 }
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000979 for (FormatToken *Tok = TheLine.First; Tok != NULL; Tok = Tok->Next) {
980 Tok->Finalized = true;
981 }
Daniel Jasper63cfb892013-10-06 11:40:08 +0000982 PreviousLine = *I;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000983 }
984 return Whitespaces.generateReplacements();
985 }
986
987private:
Alexander Kornienko73d845c2013-09-11 12:25:57 +0000988 static bool inputUsesCRLF(StringRef Text) {
989 return Text.count('\r') * 2 > Text.count('\n');
990 }
991
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000992 void
993 deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000994 unsigned CountBoundToVariable = 0;
995 unsigned CountBoundToType = 0;
996 bool HasCpp03IncompatibleFormat = false;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000997 bool HasBinPackedFunction = false;
998 bool HasOnePerLineFunction = false;
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000999 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001000 if (!AnnotatedLines[i]->First->Next)
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001001 continue;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001002 FormatToken *Tok = AnnotatedLines[i]->First->Next;
Manuel Klimekb3987012013-05-29 14:47:47 +00001003 while (Tok->Next) {
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001004 if (Tok->Type == TT_PointerOrReference) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001005 bool SpacesBefore =
1006 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
1007 bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
1008 Tok->Next->WhitespaceRange.getEnd();
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001009 if (SpacesBefore && !SpacesAfter)
1010 ++CountBoundToVariable;
1011 else if (!SpacesBefore && SpacesAfter)
1012 ++CountBoundToType;
1013 }
1014
Daniel Jasper29f123b2013-02-08 15:28:42 +00001015 if (Tok->Type == TT_TemplateCloser &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001016 Tok->Previous->Type == TT_TemplateCloser &&
1017 Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd())
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001018 HasCpp03IncompatibleFormat = true;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00001019
1020 if (Tok->PackingKind == PPK_BinPacked)
1021 HasBinPackedFunction = true;
1022 if (Tok->PackingKind == PPK_OnePerLine)
1023 HasOnePerLineFunction = true;
1024
Manuel Klimekb3987012013-05-29 14:47:47 +00001025 Tok = Tok->Next;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001026 }
1027 }
1028 if (Style.DerivePointerBinding) {
1029 if (CountBoundToType > CountBoundToVariable)
1030 Style.PointerBindsToType = true;
1031 else if (CountBoundToType < CountBoundToVariable)
1032 Style.PointerBindsToType = false;
1033 }
1034 if (Style.Standard == FormatStyle::LS_Auto) {
1035 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
1036 : FormatStyle::LS_Cpp03;
1037 }
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00001038 BinPackInconclusiveFunctions =
1039 HasBinPackedFunction || !HasOnePerLineFunction;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001040 }
1041
Manuel Klimek547d5db2013-02-08 17:38:27 +00001042 /// \brief Get the indent of \p Level from \p IndentForLevel.
1043 ///
1044 /// \p IndentForLevel must contain the indent for the level \c l
1045 /// at \p IndentForLevel[l], or a value < 0 if the indent for
1046 /// that level is unknown.
Daniel Jasperfc759082013-02-14 14:26:07 +00001047 unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
Manuel Klimek547d5db2013-02-08 17:38:27 +00001048 if (IndentForLevel[Level] != -1)
1049 return IndentForLevel[Level];
Manuel Klimek52635ff2013-02-08 19:53:32 +00001050 if (Level == 0)
1051 return 0;
Manuel Klimek07a64ec2013-05-13 08:42:42 +00001052 return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
Manuel Klimek547d5db2013-02-08 17:38:27 +00001053 }
1054
1055 /// \brief Get the offset of the line relatively to the level.
1056 ///
1057 /// For example, 'public:' labels in classes are offset by 1 or 2
1058 /// characters to the left from their level.
Manuel Klimekb3987012013-05-29 14:47:47 +00001059 int getIndentOffset(const FormatToken &RootToken) {
Alexander Kornienko94b748f2013-03-27 17:08:02 +00001060 if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
Manuel Klimek547d5db2013-02-08 17:38:27 +00001061 return Style.AccessModifierOffset;
1062 return 0;
1063 }
1064
Manuel Klimek517e8942013-01-11 17:54:10 +00001065 /// \brief Tries to merge lines into one.
1066 ///
1067 /// This will change \c Line and \c AnnotatedLine to contain the merged line,
1068 /// if possible; note that \c I will be incremented when lines are merged.
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001069 void tryFitMultipleLinesInOne(unsigned Indent,
Daniel Jasperb77d7412013-09-06 07:54:20 +00001070 SmallVectorImpl<AnnotatedLine *>::iterator &I,
1071 SmallVectorImpl<AnnotatedLine *>::iterator E) {
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001072 // We can never merge stuff if there are trailing line comments.
Daniel Jasper567dcf92013-09-05 09:29:45 +00001073 AnnotatedLine *TheLine = *I;
1074 if (TheLine->Last->Type == TT_LineComment)
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001075 return;
1076
Daniel Jaspere05dc6d2013-07-24 13:10:59 +00001077 if (Indent > Style.ColumnLimit)
1078 return;
1079
Daniel Jaspera4d46212013-02-28 11:05:57 +00001080 unsigned Limit = Style.ColumnLimit - Indent;
Daniel Jasperf11a7052013-02-21 21:33:55 +00001081 // If we already exceed the column limit, we set 'Limit' to 0. The different
1082 // tryMerge..() functions can then decide whether to still do merging.
Daniel Jasper567dcf92013-09-05 09:29:45 +00001083 Limit = TheLine->Last->TotalLength > Limit
1084 ? 0
1085 : Limit - TheLine->Last->TotalLength;
Daniel Jasper55b08e72013-01-16 07:02:34 +00001086
Daniel Jasper567dcf92013-09-05 09:29:45 +00001087 if (I + 1 == E || (*(I + 1))->Type == LT_Invalid)
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001088 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001089
Daniel Jasper567dcf92013-09-05 09:29:45 +00001090 if (TheLine->Last->is(tok::l_brace)) {
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001091 tryMergeSimpleBlock(I, E, Limit);
Daniel Jasperf11bbb92013-05-16 12:12:21 +00001092 } else if (Style.AllowShortIfStatementsOnASingleLine &&
Daniel Jasper567dcf92013-09-05 09:29:45 +00001093 TheLine->First->is(tok::kw_if)) {
Daniel Jasperf11bbb92013-05-16 12:12:21 +00001094 tryMergeSimpleControlStatement(I, E, Limit);
1095 } else if (Style.AllowShortLoopsOnASingleLine &&
Daniel Jasper567dcf92013-09-05 09:29:45 +00001096 TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) {
Daniel Jasperf11bbb92013-05-16 12:12:21 +00001097 tryMergeSimpleControlStatement(I, E, Limit);
Daniel Jasper567dcf92013-09-05 09:29:45 +00001098 } else if (TheLine->InPPDirective && (TheLine->First->HasUnescapedNewline ||
1099 TheLine->First->IsFirst)) {
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001100 tryMergeSimplePPDirective(I, E, Limit);
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001101 }
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001102 }
1103
Daniel Jasperb77d7412013-09-06 07:54:20 +00001104 void tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::iterator &I,
1105 SmallVectorImpl<AnnotatedLine *>::iterator E,
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001106 unsigned Limit) {
Daniel Jasperf11a7052013-02-21 21:33:55 +00001107 if (Limit == 0)
1108 return;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001109 AnnotatedLine &Line = **I;
1110 if (!(*(I + 1))->InPPDirective || (*(I + 1))->First->HasUnescapedNewline)
Daniel Jasper2b9c10b2013-01-14 15:52:06 +00001111 return;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001112 if (I + 2 != E && (*(I + 2))->InPPDirective &&
1113 !(*(I + 2))->First->HasUnescapedNewline)
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001114 return;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001115 if (1 + (*(I + 1))->Last->TotalLength > Limit)
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +00001116 return;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001117 join(Line, **(++I));
Daniel Jaspere0b15ea2013-01-14 15:40:57 +00001118 }
1119
Daniel Jasperb77d7412013-09-06 07:54:20 +00001120 void
1121 tryMergeSimpleControlStatement(SmallVectorImpl<AnnotatedLine *>::iterator &I,
1122 SmallVectorImpl<AnnotatedLine *>::iterator E,
1123 unsigned Limit) {
Daniel Jasperf11a7052013-02-21 21:33:55 +00001124 if (Limit == 0)
1125 return;
Manuel Klimekd3a247c2013-08-07 19:20:45 +00001126 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman &&
Daniel Jasper567dcf92013-09-05 09:29:45 +00001127 (*(I + 1))->First->is(tok::l_brace))
Manuel Klimekd3a247c2013-08-07 19:20:45 +00001128 return;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001129 if ((*(I + 1))->InPPDirective != (*I)->InPPDirective ||
1130 ((*(I + 1))->InPPDirective && (*(I + 1))->First->HasUnescapedNewline))
Manuel Klimek4c128122013-01-18 14:46:43 +00001131 return;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001132 AnnotatedLine &Line = **I;
Daniel Jasper55b08e72013-01-16 07:02:34 +00001133 if (Line.Last->isNot(tok::r_paren))
1134 return;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001135 if (1 + (*(I + 1))->Last->TotalLength > Limit)
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001136 return;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001137 if ((*(I + 1))->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for,
1138 tok::kw_while) ||
1139 (*(I + 1))->First->Type == TT_LineComment)
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001140 return;
1141 // Only inline simple if's (no nested if or else).
Manuel Klimekb3987012013-05-29 14:47:47 +00001142 if (I + 2 != E && Line.First->is(tok::kw_if) &&
Daniel Jasper567dcf92013-09-05 09:29:45 +00001143 (*(I + 2))->First->is(tok::kw_else))
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001144 return;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001145 join(Line, **(++I));
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001146 }
1147
Daniel Jasperb77d7412013-09-06 07:54:20 +00001148 void tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::iterator &I,
1149 SmallVectorImpl<AnnotatedLine *>::iterator E,
Daniel Jasper1a1ce832013-01-29 11:27:30 +00001150 unsigned Limit) {
Daniel Jasper5be59ba2013-05-15 14:09:55 +00001151 // No merging if the brace already is on the next line.
1152 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
1153 return;
1154
Manuel Klimek517e8942013-01-11 17:54:10 +00001155 // First, check that the current line allows merging. This is the case if
1156 // we're not in a control flow statement and the last token is an opening
1157 // brace.
Daniel Jasper567dcf92013-09-05 09:29:45 +00001158 AnnotatedLine &Line = **I;
Manuel Klimekb3987012013-05-29 14:47:47 +00001159 if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
1160 tok::kw_else, tok::kw_try, tok::kw_catch,
Daniel Jasper8893b8a2013-05-31 14:56:20 +00001161 tok::kw_for,
Manuel Klimekb3987012013-05-29 14:47:47 +00001162 // This gets rid of all ObjC @ keywords and methods.
1163 tok::at, tok::minus, tok::plus))
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001164 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001165
Daniel Jasper567dcf92013-09-05 09:29:45 +00001166 FormatToken *Tok = (*(I + 1))->First;
Daniel Jasper8893b8a2013-05-31 14:56:20 +00001167 if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
Alexander Kornienko0bdc6432013-07-04 14:47:51 +00001168 (Tok->getNextNonComment() == NULL ||
1169 Tok->getNextNonComment()->is(tok::semi))) {
Daniel Jasperf11a7052013-02-21 21:33:55 +00001170 // We merge empty blocks even if the line exceeds the column limit.
Daniel Jasper729a7432013-02-11 12:36:37 +00001171 Tok->SpacesRequiredBefore = 0;
Daniel Jasperf11a7052013-02-21 21:33:55 +00001172 Tok->CanBreakBefore = true;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001173 join(Line, **(I + 1));
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001174 I += 1;
Daniel Jasper8893b8a2013-05-31 14:56:20 +00001175 } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) {
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001176 // Check that we still have three lines and they fit into the limit.
Daniel Jasper567dcf92013-09-05 09:29:45 +00001177 if (I + 2 == E || (*(I + 2))->Type == LT_Invalid ||
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001178 !nextTwoLinesFitInto(I, Limit))
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001179 return;
Manuel Klimek517e8942013-01-11 17:54:10 +00001180
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001181 // Second, check that the next line does not contain any braces - if it
1182 // does, readability declines when putting it into a single line.
Daniel Jasper567dcf92013-09-05 09:29:45 +00001183 if ((*(I + 1))->Last->Type == TT_LineComment || Tok->MustBreakBefore)
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001184 return;
1185 do {
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001186 if (Tok->isOneOf(tok::l_brace, tok::r_brace))
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001187 return;
Manuel Klimekb3987012013-05-29 14:47:47 +00001188 Tok = Tok->Next;
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001189 } while (Tok != NULL);
Manuel Klimek517e8942013-01-11 17:54:10 +00001190
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001191 // Last, check that the third line contains a single closing brace.
Daniel Jasper567dcf92013-09-05 09:29:45 +00001192 Tok = (*(I + 2))->First;
Alexander Kornienko0bdc6432013-07-04 14:47:51 +00001193 if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) ||
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001194 Tok->MustBreakBefore)
1195 return;
1196
Daniel Jasper567dcf92013-09-05 09:29:45 +00001197 join(Line, **(I + 1));
1198 join(Line, **(I + 2));
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001199 I += 2;
Manuel Klimek517e8942013-01-11 17:54:10 +00001200 }
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001201 }
1202
Daniel Jasperb77d7412013-09-06 07:54:20 +00001203 bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::iterator I,
Daniel Jasperfeb18f52013-01-14 14:14:23 +00001204 unsigned Limit) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001205 return 1 + (*(I + 1))->Last->TotalLength + 1 +
1206 (*(I + 2))->Last->TotalLength <=
Manuel Klimek2f1ac412013-01-21 16:42:44 +00001207 Limit;
Manuel Klimek517e8942013-01-11 17:54:10 +00001208 }
1209
Daniel Jasper995e8202013-01-14 13:08:07 +00001210 void join(AnnotatedLine &A, const AnnotatedLine &B) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001211 assert(!A.Last->Next);
1212 assert(!B.First->Previous);
1213 A.Last->Next = B.First;
1214 B.First->Previous = A.Last;
1215 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
1216 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
1217 Tok->TotalLength += LengthA;
1218 A.Last = Tok;
Daniel Jasper995e8202013-01-14 13:08:07 +00001219 }
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001220 }
1221
Daniel Jasper6f21a982013-03-13 07:49:51 +00001222 bool touchesRanges(const CharSourceRange &Range) {
Daniel Jasperf3023542013-03-07 20:50:00 +00001223 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1224 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
1225 Ranges[i].getBegin()) &&
1226 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1227 Range.getBegin()))
1228 return true;
1229 }
1230 return false;
1231 }
1232
1233 bool touchesLine(const AnnotatedLine &TheLine) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001234 const FormatToken *First = TheLine.First;
1235 const FormatToken *Last = TheLine.Last;
Daniel Jasper84f5ddf2013-05-14 10:31:09 +00001236 CharSourceRange LineRange = CharSourceRange::getCharRange(
Manuel Klimekad3094b2013-05-23 10:56:37 +00001237 First->WhitespaceRange.getBegin().getLocWithOffset(
1238 First->LastNewlineOffset),
Alexander Kornienko2c2f7292013-09-16 20:20:49 +00001239 Last->getStartOfNonWhitespace().getLocWithOffset(
1240 Last->TokenText.size() - 1));
Daniel Jasperf3023542013-03-07 20:50:00 +00001241 return touchesRanges(LineRange);
1242 }
1243
Daniel Jasperb77d7412013-09-06 07:54:20 +00001244 bool touchesPPDirective(SmallVectorImpl<AnnotatedLine *>::iterator I,
1245 SmallVectorImpl<AnnotatedLine *>::iterator E) {
Daniel Jasper89b3a7f2013-05-10 13:00:49 +00001246 for (; I != E; ++I) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001247 if ((*I)->First->HasUnescapedNewline)
Daniel Jasper89b3a7f2013-05-10 13:00:49 +00001248 return false;
Daniel Jasper567dcf92013-09-05 09:29:45 +00001249 if (touchesLine(**I))
Daniel Jasper89b3a7f2013-05-10 13:00:49 +00001250 return true;
1251 }
1252 return false;
1253 }
1254
Daniel Jasperf3023542013-03-07 20:50:00 +00001255 bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001256 const FormatToken *First = TheLine.First;
Daniel Jasperf3023542013-03-07 20:50:00 +00001257 CharSourceRange LineRange = CharSourceRange::getCharRange(
Manuel Klimekad3094b2013-05-23 10:56:37 +00001258 First->WhitespaceRange.getBegin(),
1259 First->WhitespaceRange.getBegin().getLocWithOffset(
1260 First->LastNewlineOffset));
Daniel Jasperf3023542013-03-07 20:50:00 +00001261 return touchesRanges(LineRange);
Manuel Klimekf9ea2ed2013-01-10 19:49:59 +00001262 }
1263
1264 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +00001265 assert(!UnwrappedLines.empty());
1266 UnwrappedLines.back().push_back(TheLine);
1267 }
1268
1269 virtual void finishRun() {
1270 UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
Daniel Jasperbac016b2012-12-03 18:12:45 +00001271 }
1272
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001273 /// \brief Add a new line and the required indent before the first Token
1274 /// of the \c UnwrappedLine if there was no structural parsing error.
1275 /// Returns the indent level of the \c UnwrappedLine.
Manuel Klimekae76f7f2013-10-11 21:25:45 +00001276 void formatFirstToken(FormatToken &RootToken,
Daniel Jasper63cfb892013-10-06 11:40:08 +00001277 const AnnotatedLine *PreviousLine, unsigned Indent,
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001278 bool InPPDirective) {
Daniel Jasper1a1ce832013-01-29 11:27:30 +00001279 unsigned Newlines =
Manuel Klimekb3987012013-05-29 14:47:47 +00001280 std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
Daniel Jasper15f33f02013-06-03 16:16:41 +00001281 // Remove empty lines before "}" where applicable.
1282 if (RootToken.is(tok::r_brace) &&
1283 (!RootToken.Next ||
1284 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
1285 Newlines = std::min(Newlines, 1u);
Manuel Klimekb3987012013-05-29 14:47:47 +00001286 if (Newlines == 0 && !RootToken.IsFirst)
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001287 Newlines = 1;
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001288
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001289 // Insert extra new line before access specifiers.
Daniel Jasper63cfb892013-10-06 11:40:08 +00001290 if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001291 RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001292 ++Newlines;
Alexander Kornienko94b748f2013-03-27 17:08:02 +00001293
Daniel Jasper63cfb892013-10-06 11:40:08 +00001294 // Remove empty lines after access specifiers.
1295 if (PreviousLine && PreviousLine->First->isAccessSpecifier())
1296 Newlines = std::min(1u, Newlines);
1297
Manuel Klimekb3987012013-05-29 14:47:47 +00001298 Whitespaces.replaceWhitespace(
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +00001299 RootToken, Newlines, Indent / Style.IndentWidth, Indent, Indent,
Manuel Klimekb3987012013-05-29 14:47:47 +00001300 InPPDirective && !RootToken.HasUnescapedNewline);
Manuel Klimek3f8c7f32013-01-10 18:45:26 +00001301 }
1302
Daniel Jasper567dcf92013-09-05 09:29:45 +00001303 unsigned getColumnLimit(bool InPPDirective) const {
1304 // In preprocessor directives reserve two chars for trailing " \"
1305 return Style.ColumnLimit - (InPPDirective ? 2 : 0);
1306 }
1307
Daniel Jasperbac016b2012-12-03 18:12:45 +00001308 FormatStyle Style;
1309 Lexer &Lex;
1310 SourceManager &SourceMgr;
Daniel Jasperdcc2a622013-01-18 08:44:07 +00001311 WhitespaceManager Whitespaces;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001312 std::vector<CharSourceRange> Ranges;
Manuel Klimekae76f7f2013-10-11 21:25:45 +00001313 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
Alexander Kornienko00895102013-06-05 14:09:10 +00001314
1315 encoding::Encoding Encoding;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00001316 bool BinPackInconclusiveFunctions;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001317};
1318
Craig Topper83f81d72013-06-30 22:29:28 +00001319} // end anonymous namespace
1320
Alexander Kornienko70ce7882013-04-15 14:28:00 +00001321tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
1322 SourceManager &SourceMgr,
Daniel Jaspercaf42a32013-05-15 08:14:19 +00001323 std::vector<CharSourceRange> Ranges) {
1324 Formatter formatter(Style, Lex, SourceMgr, Ranges);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001325 return formatter.format();
1326}
1327
Daniel Jasper8a999452013-05-16 10:40:07 +00001328tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
1329 std::vector<tooling::Range> Ranges,
1330 StringRef FileName) {
1331 FileManager Files((FileSystemOptions()));
1332 DiagnosticsEngine Diagnostics(
1333 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1334 new DiagnosticOptions);
1335 SourceManager SourceMgr(Diagnostics, Files);
1336 llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName);
1337 const clang::FileEntry *Entry =
1338 Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
1339 SourceMgr.overrideFileContents(Entry, Buf);
1340 FileID ID =
1341 SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
Alexander Kornienkoa1753f42013-06-28 12:51:24 +00001342 Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr,
1343 getFormattingLangOpts(Style.Standard));
Daniel Jasper8a999452013-05-16 10:40:07 +00001344 SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
1345 std::vector<CharSourceRange> CharRanges;
1346 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1347 SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset());
1348 SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength());
1349 CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1350 }
1351 return reformat(Style, Lex, SourceMgr, CharRanges);
1352}
1353
Alexander Kornienkoa1753f42013-06-28 12:51:24 +00001354LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) {
Daniel Jasper46ef8522013-01-10 13:08:12 +00001355 LangOptions LangOpts;
1356 LangOpts.CPlusPlus = 1;
Alexander Kornienkoa1753f42013-06-28 12:51:24 +00001357 LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
Daniel Jasperb64eca02013-03-22 10:01:29 +00001358 LangOpts.LineComment = 1;
Daniel Jasper46ef8522013-01-10 13:08:12 +00001359 LangOpts.Bool = 1;
1360 LangOpts.ObjC1 = 1;
1361 LangOpts.ObjC2 = 1;
1362 return LangOpts;
1363}
1364
Edwin Vanef4e12c82013-09-30 13:31:48 +00001365const char *StyleOptionHelpDescription =
1366 "Coding style, currently supports:\n"
1367 " LLVM, Google, Chromium, Mozilla, WebKit.\n"
1368 "Use -style=file to load style configuration from\n"
1369 ".clang-format file located in one of the parent\n"
1370 "directories of the source file (or current\n"
1371 "directory for stdin).\n"
1372 "Use -style=\"{key: value, ...}\" to set specific\n"
1373 "parameters, e.g.:\n"
1374 " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
1375
1376FormatStyle getStyle(StringRef StyleName, StringRef FileName) {
1377 // Fallback style in case the rest of this function can't determine a style.
1378 StringRef FallbackStyle = "LLVM";
1379 FormatStyle Style;
1380 getPredefinedStyle(FallbackStyle, &Style);
1381
1382 if (StyleName.startswith("{")) {
1383 // Parse YAML/JSON style from the command line.
1384 if (llvm::error_code ec = parseConfiguration(StyleName, &Style)) {
1385 llvm::errs() << "Error parsing -style: " << ec.message()
1386 << ", using " << FallbackStyle << " style\n";
1387 }
1388 return Style;
1389 }
1390
1391 if (!StyleName.equals_lower("file")) {
1392 if (!getPredefinedStyle(StyleName, &Style))
1393 llvm::errs() << "Invalid value for -style, using " << FallbackStyle
1394 << " style\n";
1395 return Style;
1396 }
1397
1398 SmallString<128> Path(FileName);
1399 llvm::sys::fs::make_absolute(Path);
1400 for (StringRef Directory = Path;
1401 !Directory.empty();
1402 Directory = llvm::sys::path::parent_path(Directory)) {
1403 if (!llvm::sys::fs::is_directory(Directory))
1404 continue;
1405 SmallString<128> ConfigFile(Directory);
1406
1407 llvm::sys::path::append(ConfigFile, ".clang-format");
1408 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1409 bool IsFile = false;
1410 // Ignore errors from is_regular_file: we only need to know if we can read
1411 // the file or not.
1412 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1413
1414 if (!IsFile) {
1415 // Try _clang-format too, since dotfiles are not commonly used on Windows.
1416 ConfigFile = Directory;
1417 llvm::sys::path::append(ConfigFile, "_clang-format");
1418 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1419 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1420 }
1421
1422 if (IsFile) {
1423 OwningPtr<llvm::MemoryBuffer> Text;
1424 if (llvm::error_code ec = llvm::MemoryBuffer::getFile(ConfigFile, Text)) {
1425 llvm::errs() << ec.message() << "\n";
1426 continue;
1427 }
1428 if (llvm::error_code ec = parseConfiguration(Text->getBuffer(), &Style)) {
1429 llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()
1430 << "\n";
1431 continue;
1432 }
1433 DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
1434 return Style;
1435 }
1436 }
1437 llvm::errs() << "Can't find usable .clang-format, using " << FallbackStyle
1438 << " style\n";
1439 return Style;
1440}
1441
Daniel Jaspercd162382013-01-07 13:26:07 +00001442} // namespace format
1443} // namespace clang