blob: 512f99f5e7f246ec900a40dfda3a58240e5dcbb4 [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
Daniel Jasperde0328a2013-08-16 11:20:30 +000018#include "ContinuationIndenter.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"
Chandler Carruth44eb4f62013-01-02 10:28:36 +000023#include "clang/Basic/SourceManager.h"
Manuel Klimek24998102013-01-16 14:55:28 +000024#include "clang/Format/Format.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000025#include "clang/Lex/Lexer.h"
Alexander Kornienkoffd6d042013-03-27 11:52:18 +000026#include "llvm/ADT/STLExtras.h"
Manuel Klimek2ef908e2013-02-13 10:46:36 +000027#include "llvm/Support/Allocator.h"
Manuel Klimek24998102013-01-16 14:55:28 +000028#include "llvm/Support/Debug.h"
Alexander Kornienkod6538332013-05-07 15:32:14 +000029#include "llvm/Support/YAMLTraits.h"
Manuel Klimek2ef908e2013-02-13 10:46:36 +000030#include <queue>
Daniel Jasper8b529712012-12-04 13:02:32 +000031#include <string>
32
Alexander Kornienkod6538332013-05-07 15:32:14 +000033namespace llvm {
34namespace yaml {
35template <>
36struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> {
Manuel Klimeka8eb9142013-05-13 12:51:40 +000037 static void enumeration(IO &IO,
38 clang::format::FormatStyle::LanguageStandard &Value) {
Alexander Kornienkob40cfe42013-09-04 14:09:13 +000039 IO.enumCase(Value, "Cpp03", clang::format::FormatStyle::LS_Cpp03);
Manuel Klimeka8eb9142013-05-13 12:51:40 +000040 IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03);
Alexander Kornienkob40cfe42013-09-04 14:09:13 +000041 IO.enumCase(Value, "Cpp11", clang::format::FormatStyle::LS_Cpp11);
Manuel Klimeka8eb9142013-05-13 12:51:40 +000042 IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11);
43 IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto);
44 }
45};
46
Daniel Jasper12f9d8e2013-05-14 09:30:02 +000047template <>
Manuel Klimeka8eb9142013-05-13 12:51:40 +000048struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> {
49 static void
50 enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) {
51 IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach);
52 IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux);
53 IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +000054 IO.enumCase(Value, "Allman", clang::format::FormatStyle::BS_Allman);
Alexander Kornienkod6538332013-05-07 15:32:14 +000055 }
56};
57
Daniel Jasper65ee3472013-07-31 23:16:02 +000058template <>
59struct ScalarEnumerationTraits<
60 clang::format::FormatStyle::NamespaceIndentationKind> {
61 static void
62 enumeration(IO &IO,
63 clang::format::FormatStyle::NamespaceIndentationKind &Value) {
64 IO.enumCase(Value, "None", clang::format::FormatStyle::NI_None);
65 IO.enumCase(Value, "Inner", clang::format::FormatStyle::NI_Inner);
66 IO.enumCase(Value, "All", clang::format::FormatStyle::NI_All);
67 }
68};
69
Alexander Kornienkod6538332013-05-07 15:32:14 +000070template <> struct MappingTraits<clang::format::FormatStyle> {
71 static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) {
Alexander Kornienko49149672013-05-10 11:56:10 +000072 if (IO.outputting()) {
Alexander Kornienkoe3648fb2013-09-02 16:39:23 +000073 StringRef StylesArray[] = { "LLVM", "Google", "Chromium",
74 "Mozilla", "WebKit" };
Alexander Kornienko49149672013-05-10 11:56:10 +000075 ArrayRef<StringRef> Styles(StylesArray);
76 for (size_t i = 0, e = Styles.size(); i < e; ++i) {
77 StringRef StyleName(Styles[i]);
Alexander Kornienko006b5c82013-05-19 00:53:30 +000078 clang::format::FormatStyle PredefinedStyle;
79 if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) &&
80 Style == PredefinedStyle) {
Alexander Kornienko49149672013-05-10 11:56:10 +000081 IO.mapOptional("# BasedOnStyle", StyleName);
82 break;
83 }
84 }
85 } else {
Alexander Kornienkod6538332013-05-07 15:32:14 +000086 StringRef BasedOnStyle;
87 IO.mapOptional("BasedOnStyle", BasedOnStyle);
Alexander Kornienkod6538332013-05-07 15:32:14 +000088 if (!BasedOnStyle.empty())
Alexander Kornienko006b5c82013-05-19 00:53:30 +000089 if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) {
90 IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
91 return;
92 }
Alexander Kornienkod6538332013-05-07 15:32:14 +000093 }
94
95 IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
Daniel Jaspercdaffa42013-08-13 10:58:30 +000096 IO.mapOptional("ConstructorInitializerIndentWidth",
97 Style.ConstructorInitializerIndentWidth);
Alexander Kornienkod6538332013-05-07 15:32:14 +000098 IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
Daniel Jasper552f4a72013-07-31 23:55:15 +000099 IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000100 IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
101 Style.AllowAllParametersOfDeclarationOnNextLine);
102 IO.mapOptional("AllowShortIfStatementsOnASingleLine",
103 Style.AllowShortIfStatementsOnASingleLine);
Daniel Jasper3a685df2013-05-16 12:12:21 +0000104 IO.mapOptional("AllowShortLoopsOnASingleLine",
105 Style.AllowShortLoopsOnASingleLine);
Daniel Jasper61e6bbf2013-05-29 12:07:31 +0000106 IO.mapOptional("AlwaysBreakTemplateDeclarations",
107 Style.AlwaysBreakTemplateDeclarations);
Alexander Kornienko58611712013-07-04 12:02:44 +0000108 IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
109 Style.AlwaysBreakBeforeMultilineStrings);
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000110 IO.mapOptional("BreakBeforeBinaryOperators",
111 Style.BreakBeforeBinaryOperators);
112 IO.mapOptional("BreakConstructorInitializersBeforeComma",
113 Style.BreakConstructorInitializersBeforeComma);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000114 IO.mapOptional("BinPackParameters", Style.BinPackParameters);
115 IO.mapOptional("ColumnLimit", Style.ColumnLimit);
116 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
117 Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
118 IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000119 IO.mapOptional("ExperimentalAutoDetectBinPacking",
120 Style.ExperimentalAutoDetectBinPacking);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000121 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
122 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000123 IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000124 IO.mapOptional("ObjCSpaceBeforeProtocolList",
125 Style.ObjCSpaceBeforeProtocolList);
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000126 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
127 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000128 IO.mapOptional("PenaltyBreakFirstLessLess",
129 Style.PenaltyBreakFirstLessLess);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000130 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
131 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
132 Style.PenaltyReturnTypeOnItsOwnLine);
133 IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
134 IO.mapOptional("SpacesBeforeTrailingComments",
135 Style.SpacesBeforeTrailingComments);
Daniel Jasper6ab54682013-07-16 18:22:10 +0000136 IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000137 IO.mapOptional("Standard", Style.Standard);
Manuel Klimek13b97d82013-05-13 08:42:42 +0000138 IO.mapOptional("IndentWidth", Style.IndentWidth);
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000139 IO.mapOptional("UseTab", Style.UseTab);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000140 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
Manuel Klimek836c2862013-06-21 17:25:42 +0000141 IO.mapOptional("IndentFunctionDeclarationAfterType",
142 Style.IndentFunctionDeclarationAfterType);
Daniel Jasperb55acad2013-08-20 12:36:34 +0000143 IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
Daniel Jasperf110e202013-08-21 08:39:01 +0000144 IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
Daniel Jasperb55acad2013-08-20 12:36:34 +0000145 IO.mapOptional("SpacesInCStyleCastParentheses",
146 Style.SpacesInCStyleCastParentheses);
147 IO.mapOptional("SpaceAfterControlStatementKeyword",
148 Style.SpaceAfterControlStatementKeyword);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000149 }
150};
151}
152}
153
Daniel Jasperf7935112012-12-03 18:12:45 +0000154namespace clang {
155namespace format {
156
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000157void setDefaultPenalties(FormatStyle &Style) {
Daniel Jasper2739af32013-08-28 10:03:58 +0000158 Style.PenaltyBreakComment = 60;
Daniel Jasperfa21c072013-07-15 14:33:14 +0000159 Style.PenaltyBreakFirstLessLess = 120;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000160 Style.PenaltyBreakString = 1000;
161 Style.PenaltyExcessCharacter = 1000000;
162}
163
Daniel Jasperf7935112012-12-03 18:12:45 +0000164FormatStyle getLLVMStyle() {
165 FormatStyle LLVMStyle;
Daniel Jasperf7935112012-12-03 18:12:45 +0000166 LLVMStyle.AccessModifierOffset = -2;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000167 LLVMStyle.AlignEscapedNewlinesLeft = false;
Daniel Jasper552f4a72013-07-31 23:55:15 +0000168 LLVMStyle.AlignTrailingComments = true;
Daniel Jasperf7db4332013-01-29 16:03:49 +0000169 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000170 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000171 LLVMStyle.AllowShortLoopsOnASingleLine = false;
Alexander Kornienko58611712013-07-04 12:02:44 +0000172 LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000173 LLVMStyle.AlwaysBreakTemplateDeclarations = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000174 LLVMStyle.BinPackParameters = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000175 LLVMStyle.BreakBeforeBinaryOperators = false;
176 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
177 LLVMStyle.BreakConstructorInitializersBeforeComma = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000178 LLVMStyle.ColumnLimit = 80;
179 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Daniel Jaspercdaffa42013-08-13 10:58:30 +0000180 LLVMStyle.ConstructorInitializerIndentWidth = 4;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000181 LLVMStyle.Cpp11BracedListStyle = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000182 LLVMStyle.DerivePointerBinding = false;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000183 LLVMStyle.ExperimentalAutoDetectBinPacking = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000184 LLVMStyle.IndentCaseLabels = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000185 LLVMStyle.IndentFunctionDeclarationAfterType = false;
186 LLVMStyle.IndentWidth = 2;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000187 LLVMStyle.MaxEmptyLinesToKeep = 1;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000188 LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
Nico Webera6087752013-01-10 20:12:55 +0000189 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000190 LLVMStyle.PointerBindsToType = false;
191 LLVMStyle.SpacesBeforeTrailingComments = 1;
192 LLVMStyle.Standard = FormatStyle::LS_Cpp03;
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000193 LLVMStyle.UseTab = false;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000194 LLVMStyle.SpacesInParentheses = false;
195 LLVMStyle.SpaceInEmptyParentheses = false;
196 LLVMStyle.SpacesInCStyleCastParentheses = false;
197 LLVMStyle.SpaceAfterControlStatementKeyword = true;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000198
199 setDefaultPenalties(LLVMStyle);
200 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
201
Daniel Jasperf7935112012-12-03 18:12:45 +0000202 return LLVMStyle;
203}
204
205FormatStyle getGoogleStyle() {
206 FormatStyle GoogleStyle;
Daniel Jasperf7935112012-12-03 18:12:45 +0000207 GoogleStyle.AccessModifierOffset = -1;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000208 GoogleStyle.AlignEscapedNewlinesLeft = true;
Daniel Jasper552f4a72013-07-31 23:55:15 +0000209 GoogleStyle.AlignTrailingComments = true;
Daniel Jasperf7db4332013-01-29 16:03:49 +0000210 GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasper085a2ed2013-04-24 13:46:00 +0000211 GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
Daniel Jasper5bd0b9e2013-05-23 18:05:18 +0000212 GoogleStyle.AllowShortLoopsOnASingleLine = true;
Alexander Kornienko58611712013-07-04 12:02:44 +0000213 GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000214 GoogleStyle.AlwaysBreakTemplateDeclarations = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000215 GoogleStyle.BinPackParameters = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000216 GoogleStyle.BreakBeforeBinaryOperators = false;
217 GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
218 GoogleStyle.BreakConstructorInitializersBeforeComma = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000219 GoogleStyle.ColumnLimit = 80;
220 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
Daniel Jaspercdaffa42013-08-13 10:58:30 +0000221 GoogleStyle.ConstructorInitializerIndentWidth = 4;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000222 GoogleStyle.Cpp11BracedListStyle = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000223 GoogleStyle.DerivePointerBinding = true;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000224 GoogleStyle.ExperimentalAutoDetectBinPacking = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000225 GoogleStyle.IndentCaseLabels = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000226 GoogleStyle.IndentFunctionDeclarationAfterType = true;
227 GoogleStyle.IndentWidth = 2;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000228 GoogleStyle.MaxEmptyLinesToKeep = 1;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000229 GoogleStyle.NamespaceIndentation = FormatStyle::NI_None;
Nico Webera6087752013-01-10 20:12:55 +0000230 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000231 GoogleStyle.PointerBindsToType = true;
232 GoogleStyle.SpacesBeforeTrailingComments = 2;
233 GoogleStyle.Standard = FormatStyle::LS_Auto;
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000234 GoogleStyle.UseTab = false;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000235 GoogleStyle.SpacesInParentheses = false;
236 GoogleStyle.SpaceInEmptyParentheses = false;
237 GoogleStyle.SpacesInCStyleCastParentheses = false;
238 GoogleStyle.SpaceAfterControlStatementKeyword = true;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000239
240 setDefaultPenalties(GoogleStyle);
241 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
242
Daniel Jasperf7935112012-12-03 18:12:45 +0000243 return GoogleStyle;
244}
245
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000246FormatStyle getChromiumStyle() {
247 FormatStyle ChromiumStyle = getGoogleStyle();
Daniel Jasperf7db4332013-01-29 16:03:49 +0000248 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Daniel Jasper085a2ed2013-04-24 13:46:00 +0000249 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000250 ChromiumStyle.AllowShortLoopsOnASingleLine = false;
Daniel Jasper2cf17bf2013-02-27 09:47:53 +0000251 ChromiumStyle.BinPackParameters = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000252 ChromiumStyle.DerivePointerBinding = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000253 ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000254 return ChromiumStyle;
255}
256
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000257FormatStyle getMozillaStyle() {
258 FormatStyle MozillaStyle = getLLVMStyle();
259 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
260 MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
261 MozillaStyle.DerivePointerBinding = true;
262 MozillaStyle.IndentCaseLabels = true;
263 MozillaStyle.ObjCSpaceBeforeProtocolList = false;
264 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
265 MozillaStyle.PointerBindsToType = true;
266 return MozillaStyle;
267}
268
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000269FormatStyle getWebKitStyle() {
270 FormatStyle Style = getLLVMStyle();
Daniel Jasper65ee3472013-07-31 23:16:02 +0000271 Style.AccessModifierOffset = -4;
Daniel Jasper552f4a72013-07-31 23:55:15 +0000272 Style.AlignTrailingComments = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000273 Style.BreakBeforeBinaryOperators = true;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000274 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000275 Style.BreakConstructorInitializersBeforeComma = true;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000276 Style.ColumnLimit = 0;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000277 Style.IndentWidth = 4;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000278 Style.NamespaceIndentation = FormatStyle::NI_Inner;
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000279 Style.PointerBindsToType = true;
280 return Style;
281}
282
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000283bool getPredefinedStyle(StringRef Name, FormatStyle *Style) {
Alexander Kornienkod6538332013-05-07 15:32:14 +0000284 if (Name.equals_lower("llvm"))
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000285 *Style = getLLVMStyle();
286 else if (Name.equals_lower("chromium"))
287 *Style = getChromiumStyle();
288 else if (Name.equals_lower("mozilla"))
289 *Style = getMozillaStyle();
290 else if (Name.equals_lower("google"))
291 *Style = getGoogleStyle();
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000292 else if (Name.equals_lower("webkit"))
293 *Style = getWebKitStyle();
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000294 else
295 return false;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000296
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000297 return true;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000298}
299
300llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
Alexander Kornienko06e00332013-05-20 15:18:01 +0000301 if (Text.trim().empty())
302 return llvm::make_error_code(llvm::errc::invalid_argument);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000303 llvm::yaml::Input Input(Text);
304 Input >> *Style;
305 return Input.error();
306}
307
308std::string configurationAsText(const FormatStyle &Style) {
309 std::string Text;
310 llvm::raw_string_ostream Stream(Text);
311 llvm::yaml::Output Output(Stream);
312 // We use the same mapping method for input and output, so we need a non-const
313 // reference here.
314 FormatStyle NonConstStyle = Style;
315 Output << NonConstStyle;
Alexander Kornienko9a38ec22013-05-13 12:56:35 +0000316 return Stream.str();
Alexander Kornienkod6538332013-05-07 15:32:14 +0000317}
318
Craig Topperaf35e852013-06-30 22:29:28 +0000319namespace {
320
Daniel Jasperde0328a2013-08-16 11:20:30 +0000321class NoColumnLimitFormatter {
322public:
Daniel Jasperf110e202013-08-21 08:39:01 +0000323 NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {}
Daniel Jasperde0328a2013-08-16 11:20:30 +0000324
325 /// \brief Formats the line starting at \p State, simply keeping all of the
326 /// input's line breaking decisions.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000327 void format(unsigned FirstIndent, const AnnotatedLine *Line) {
328 LineState State = Indenter->getInitialState(FirstIndent, Line);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000329 while (State.NextToken != NULL) {
330 bool Newline =
331 Indenter->mustBreak(State) ||
332 (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
333 Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
334 }
335 }
Daniel Jasperf110e202013-08-21 08:39:01 +0000336
Daniel Jasperde0328a2013-08-16 11:20:30 +0000337private:
338 ContinuationIndenter *Indenter;
339};
340
Daniel Jasperf7935112012-12-03 18:12:45 +0000341class UnwrappedLineFormatter {
342public:
Daniel Jasperde0328a2013-08-16 11:20:30 +0000343 UnwrappedLineFormatter(ContinuationIndenter *Indenter,
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000344 WhitespaceManager *Whitespaces,
Daniel Jasperde0328a2013-08-16 11:20:30 +0000345 const FormatStyle &Style, const AnnotatedLine &Line)
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000346 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), Line(Line),
347 Count(0) {}
Daniel Jasperf7935112012-12-03 18:12:45 +0000348
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000349 /// \brief Formats an \c UnwrappedLine and returns the penalty.
350 ///
351 /// If \p DryRun is \c false, directly applies the changes.
352 unsigned format(unsigned FirstIndent, bool DryRun = false) {
353 LineState State = Indenter->getInitialState(FirstIndent, &Line);
Daniel Jasper4b866272013-02-01 11:00:45 +0000354
Daniel Jasperacc33662013-02-08 08:22:00 +0000355 // If the ObjC method declaration does not fit on a line, we should format
356 // it with one arg per line.
357 if (Line.Type == LT_ObjCMethodDecl)
358 State.Stack.back().BreakBeforeParameter = true;
359
Daniel Jasper4b866272013-02-01 11:00:45 +0000360 // Find best solution in solution space.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000361 return analyzeSolutionSpace(State, DryRun);
Daniel Jasperf7935112012-12-03 18:12:45 +0000362 }
363
364private:
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000365 /// \brief An edge in the solution space from \c Previous->State to \c State,
366 /// inserting a newline dependent on the \c NewLine.
367 struct StateNode {
368 StateNode(const LineState &State, bool NewLine, StateNode *Previous)
Daniel Jasper12ef4e52013-02-21 21:33:55 +0000369 : State(State), NewLine(NewLine), Previous(Previous) {}
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000370 LineState State;
371 bool NewLine;
372 StateNode *Previous;
373 };
Daniel Jasper4b866272013-02-01 11:00:45 +0000374
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000375 /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
376 ///
377 /// In case of equal penalties, we want to prefer states that were inserted
378 /// first. During state generation we make sure that we insert states first
379 /// that break the line as late as possible.
380 typedef std::pair<unsigned, unsigned> OrderedPenalty;
381
382 /// \brief An item in the prioritized BFS search queue. The \c StateNode's
383 /// \c State has the given \c OrderedPenalty.
384 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
385
386 /// \brief The BFS queue type.
387 typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
388 std::greater<QueueItem> > QueueType;
Daniel Jasper4b866272013-02-01 11:00:45 +0000389
390 /// \brief Analyze the entire solution space starting from \p InitialState.
Daniel Jasperf7935112012-12-03 18:12:45 +0000391 ///
Daniel Jasper4b866272013-02-01 11:00:45 +0000392 /// This implements a variant of Dijkstra's algorithm on the graph that spans
393 /// the solution space (\c LineStates are the nodes). The algorithm tries to
394 /// find the shortest path (the one with lowest penalty) from \p InitialState
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000395 /// to a state where all tokens are placed. Returns the penalty.
396 ///
397 /// If \p DryRun is \c false, directly applies the changes.
398 unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun = false) {
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000399 std::set<LineState> Seen;
400
Daniel Jasper4b866272013-02-01 11:00:45 +0000401 // Insert start element into queue.
Daniel Jasper687af3b2013-02-14 14:26:07 +0000402 StateNode *Node =
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000403 new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
404 Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
405 ++Count;
Daniel Jasper4b866272013-02-01 11:00:45 +0000406
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000407 unsigned Penalty = 0;
408
Daniel Jasper4b866272013-02-01 11:00:45 +0000409 // While not empty, take first element and follow edges.
410 while (!Queue.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000411 Penalty = Queue.top().first.first;
Daniel Jasper687af3b2013-02-14 14:26:07 +0000412 StateNode *Node = Queue.top().second;
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000413 if (Node->State.NextToken == NULL) {
Alexander Kornienko49149672013-05-10 11:56:10 +0000414 DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
Daniel Jasper4b866272013-02-01 11:00:45 +0000415 break;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000416 }
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000417 Queue.pop();
Daniel Jasper4b866272013-02-01 11:00:45 +0000418
Daniel Jasperf8114cf2013-05-22 05:27:42 +0000419 // Cut off the analysis of certain solutions if the analysis gets too
420 // complex. See description of IgnoreStackForComparison.
421 if (Count > 10000)
422 Node->State.IgnoreStackForComparison = true;
423
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000424 if (!Seen.insert(Node->State).second)
425 // State already examined with lower penalty.
426 continue;
Daniel Jasper4b866272013-02-01 11:00:45 +0000427
Nico Weber9096fc02013-06-26 00:30:14 +0000428 addNextStateToQueue(Penalty, Node, /*NewLine=*/false);
429 addNextStateToQueue(Penalty, Node, /*NewLine=*/true);
Daniel Jasper4b866272013-02-01 11:00:45 +0000430 }
431
432 if (Queue.empty())
433 // We were unable to find a solution, do nothing.
434 // FIXME: Add diagnostic?
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000435 return 0;
Daniel Jasperf7935112012-12-03 18:12:45 +0000436
Daniel Jasper4b866272013-02-01 11:00:45 +0000437 // Reconstruct the solution.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000438 if (!DryRun)
439 reconstructPath(InitialState, Queue.top().second);
440
Alexander Kornienko49149672013-05-10 11:56:10 +0000441 DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
442 DEBUG(llvm::dbgs() << "---\n");
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000443
444 return Penalty;
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000445 }
446
447 void reconstructPath(LineState &State, StateNode *Current) {
Manuel Klimek4c5c28b2013-05-29 15:10:11 +0000448 std::deque<StateNode *> Path;
449 // We do not need a break before the initial token.
450 while (Current->Previous) {
451 Path.push_front(Current);
452 Current = Current->Previous;
453 }
454 for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
455 I != E; ++I) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000456 unsigned Penalty = 0;
457 formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty);
458 Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false);
459
Manuel Klimek4c5c28b2013-05-29 15:10:11 +0000460 DEBUG({
461 if ((*I)->NewLine) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000462 llvm::dbgs() << "Penalty for placing "
Manuel Klimek4c5c28b2013-05-29 15:10:11 +0000463 << (*I)->Previous->State.NextToken->Tok.getName() << ": "
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000464 << Penalty << "\n";
Manuel Klimek4c5c28b2013-05-29 15:10:11 +0000465 }
466 });
Manuel Klimek4c5c28b2013-05-29 15:10:11 +0000467 }
Daniel Jasper4b866272013-02-01 11:00:45 +0000468 }
469
Manuel Klimekaf491072013-02-13 10:54:19 +0000470 /// \brief Add the following state to the analysis queue \c Queue.
Daniel Jasper4b866272013-02-01 11:00:45 +0000471 ///
Manuel Klimekaf491072013-02-13 10:54:19 +0000472 /// Assume the current state is \p PreviousNode and has been reached with a
Daniel Jasper4b866272013-02-01 11:00:45 +0000473 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
Manuel Klimekaf491072013-02-13 10:54:19 +0000474 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
475 bool NewLine) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000476 if (NewLine && !Indenter->canBreak(PreviousNode->State))
Daniel Jasper4b866272013-02-01 11:00:45 +0000477 return;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000478 if (!NewLine && Indenter->mustBreak(PreviousNode->State))
Daniel Jasper4b866272013-02-01 11:00:45 +0000479 return;
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000480
481 StateNode *Node = new (Allocator.Allocate())
482 StateNode(PreviousNode->State, NewLine, PreviousNode);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000483 if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty))
484 return;
485
Daniel Jasperde0328a2013-08-16 11:20:30 +0000486 Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000487
488 Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
489 ++Count;
Daniel Jasper4b866272013-02-01 11:00:45 +0000490 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000491
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000492 /// \brief Format all children of \p Tok assuming the parent is indented to
493 /// \p ParentIndent.
494 ///
495 /// Returns \c true if all children could be placed successfully and adapts
496 /// \p Penalty as well as \p State. If \p DryRun is false, also directly
497 /// creates changes using \c Whitespaces.
498 ///
499 /// The crucial idea here is that children always get formatted upon
500 /// encountering the closing brace right after the nested block. Now, if we
501 /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is
502 /// \c false), the entire block has to be kept on the same line (which is only
503 /// possible if it fits on the line, only contains a single statement, etc.
504 ///
505 /// If \p NewLine is true, we format the nested block on separate lines, i.e.
506 /// break after the "{", format all lines with correct indentation and the put
507 /// the closing "}" on yet another new line.
508 ///
509 /// This enables us to keep the simple structure of the
510 /// \c UnwrappedLineFormatter, where we only have two options for each token:
511 /// break or don't break.
512 bool formatChildren(LineState &State, bool NewLine, bool DryRun,
513 unsigned &Penalty) {
514 const FormatToken &LBrace = *State.NextToken->Previous;
515 if (LBrace.isNot(tok::l_brace) || LBrace.BlockKind != BK_Block ||
516 LBrace.Children.size() == 0)
517 return true; // The previous token does not open a block. Nothing to do.
518
519 if (NewLine) {
520 unsigned ParentIndent = State.Stack.back().Indent;
521 for (SmallVector<AnnotatedLine *, 1>::const_iterator
522 I = LBrace.Children.begin(),
523 E = LBrace.Children.end();
524 I != E; ++I) {
525 unsigned Indent =
526 ParentIndent + ((*I)->Level - Line.Level) * Style.IndentWidth;
527 if (!DryRun)
528 Whitespaces->replaceWhitespace(
529 *(*I)->First, /*Newlines=*/1, /*Spaces=*/Indent,
530 /*StartOfTokenColumn=*/Indent, Line.InPPDirective);
531 UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style, **I);
532 Penalty += Formatter.format(Indent, DryRun);
533 }
534 return true;
535 }
536
537 if (LBrace.Children.size() > 1)
538 return false; // Cannot merge multiple statements into a single line.
539
540 // We can't put the closing "}" on a line with a trailing comment.
541 if (LBrace.Children[0]->Last->isTrailingComment())
542 return false;
543
544 if (!DryRun) {
545 Whitespaces->replaceWhitespace(*LBrace.Children[0]->First,
546 /*Newlines=*/0, /*Spaces=*/1,
547 /*StartOfTokenColumn=*/State.Column,
548 State.Line->InPPDirective);
549 }
550
551 State.Column += 1 + LBrace.Children[0]->Last->TotalLength;
552 return true;
553 }
554
Daniel Jasperde0328a2013-08-16 11:20:30 +0000555 ContinuationIndenter *Indenter;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000556 WhitespaceManager *Whitespaces;
Daniel Jasperf7935112012-12-03 18:12:45 +0000557 FormatStyle Style;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +0000558 const AnnotatedLine &Line;
Manuel Klimekaf491072013-02-13 10:54:19 +0000559
560 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
561 QueueType Queue;
562 // Increasing count of \c StateNode items we have created. This is used
563 // to create a deterministic order independent of the container.
564 unsigned Count;
Daniel Jasperf7935112012-12-03 18:12:45 +0000565};
566
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000567class FormatTokenLexer {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000568public:
Manuel Klimek31c85922013-08-29 15:21:40 +0000569 FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr, FormatStyle &Style,
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000570 encoding::Encoding Encoding)
Manuel Klimek31c85922013-08-29 15:21:40 +0000571 : FormatTok(NULL), GreaterStashed(false), Column(0),
572 TrailingWhitespace(0), Lex(Lex), SourceMgr(SourceMgr), Style(Style),
573 IdentTable(getFormattingLangOpts()), Encoding(Encoding) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000574 Lex.SetKeepWhitespaceMode(true);
575 }
576
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000577 ArrayRef<FormatToken *> lex() {
578 assert(Tokens.empty());
579 do {
580 Tokens.push_back(getNextToken());
581 } while (Tokens.back()->Tok.isNot(tok::eof));
582 return Tokens;
583 }
584
585 IdentifierTable &getIdentTable() { return IdentTable; }
586
587private:
588 FormatToken *getNextToken() {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000589 if (GreaterStashed) {
Manuel Klimek591ab5a2013-05-28 13:42:28 +0000590 // Create a synthesized second '>' token.
Manuel Klimek31c85922013-08-29 15:21:40 +0000591 // FIXME: Increment Column and set OriginalColumn.
Manuel Klimek591ab5a2013-05-28 13:42:28 +0000592 Token Greater = FormatTok->Tok;
593 FormatTok = new (Allocator.Allocate()) FormatToken;
594 FormatTok->Tok = Greater;
Manuel Klimek5c24cca2013-05-23 10:56:37 +0000595 SourceLocation GreaterLocation =
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000596 FormatTok->Tok.getLocation().getLocWithOffset(1);
597 FormatTok->WhitespaceRange =
598 SourceRange(GreaterLocation, GreaterLocation);
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +0000599 FormatTok->TokenText = ">";
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000600 FormatTok->CodePointCount = 1;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000601 GreaterStashed = false;
602 return FormatTok;
603 }
604
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000605 FormatTok = new (Allocator.Allocate()) FormatToken;
Daniel Jasper8369aa52013-07-16 20:28:33 +0000606 readRawToken(*FormatTok);
Manuel Klimek9043c742013-05-27 15:23:34 +0000607 SourceLocation WhitespaceStart =
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000608 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
Manuel Klimek5c24cca2013-05-23 10:56:37 +0000609 if (SourceMgr.getFileOffset(WhitespaceStart) == 0)
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000610 FormatTok->IsFirst = true;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000611
612 // Consume and record whitespace until we find a significant token.
Manuel Klimek9043c742013-05-27 15:23:34 +0000613 unsigned WhitespaceLength = TrailingWhitespace;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000614 while (FormatTok->Tok.is(tok::unknown)) {
Manuel Klimek31c85922013-08-29 15:21:40 +0000615 for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) {
616 switch (FormatTok->TokenText[i]) {
617 case '\n':
618 ++FormatTok->NewlinesBefore;
619 // FIXME: This is technically incorrect, as it could also
620 // be a literal backslash at the end of the line.
621 if (i == 0 || FormatTok->TokenText[i-1] != '\\')
622 FormatTok->HasUnescapedNewline = true;
623 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
624 Column = 0;
625 break;
626 case ' ':
627 ++Column;
628 break;
629 case '\t':
630 Column += Style.IndentWidth - Column % Style.IndentWidth;
631 break;
632 default:
633 ++Column;
634 break;
635 }
636 }
637
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000638 WhitespaceLength += FormatTok->Tok.getLength();
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000639
Daniel Jasper8369aa52013-07-16 20:28:33 +0000640 readRawToken(*FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000641 }
Manuel Klimekef920692013-01-07 07:56:50 +0000642
Manuel Klimek1abf7892013-01-04 23:34:14 +0000643 // In case the token starts with escaped newlines, we want to
644 // take them into account as whitespace - this pattern is quite frequent
645 // in macro definitions.
646 // FIXME: What do we want to do with other escaped spaces, and escaped
647 // spaces or newlines in the middle of tokens?
648 // FIXME: Add a more explicit test.
Daniel Jasper8369aa52013-07-16 20:28:33 +0000649 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
650 FormatTok->TokenText[1] == '\n') {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000651 // FIXME: ++FormatTok->NewlinesBefore is missing...
Manuel Klimek5c24cca2013-05-23 10:56:37 +0000652 WhitespaceLength += 2;
Manuel Klimek31c85922013-08-29 15:21:40 +0000653 Column = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +0000654 FormatTok->TokenText = FormatTok->TokenText.substr(2);
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000655 }
Manuel Klimek31c85922013-08-29 15:21:40 +0000656 FormatTok->OriginalColumn = Column;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000657
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +0000658 TrailingWhitespace = 0;
659 if (FormatTok->Tok.is(tok::comment)) {
Manuel Klimek31c85922013-08-29 15:21:40 +0000660 // FIXME: Add the trimmed whitespace to Column.
Daniel Jasper8369aa52013-07-16 20:28:33 +0000661 StringRef UntrimmedText = FormatTok->TokenText;
662 FormatTok->TokenText = FormatTok->TokenText.rtrim();
663 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +0000664 } else if (FormatTok->Tok.is(tok::raw_identifier)) {
Daniel Jasper8369aa52013-07-16 20:28:33 +0000665 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000666 FormatTok->Tok.setIdentifierInfo(&Info);
667 FormatTok->Tok.setKind(Info.getTokenID());
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +0000668 } else if (FormatTok->Tok.is(tok::greatergreater)) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000669 FormatTok->Tok.setKind(tok::greater);
Daniel Jasper8369aa52013-07-16 20:28:33 +0000670 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000671 GreaterStashed = true;
672 }
673
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +0000674 // Now FormatTok is the next non-whitespace token.
Daniel Jasper8369aa52013-07-16 20:28:33 +0000675 FormatTok->CodePointCount =
676 encoding::getCodePointCount(FormatTok->TokenText, Encoding);
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000677
Alexander Kornienko632abb92013-09-02 13:58:14 +0000678 if (FormatTok->isOneOf(tok::string_literal, tok::comment)) {
679 StringRef Text = FormatTok->TokenText;
680 size_t FirstNewlinePos = Text.find('\n');
681 if (FirstNewlinePos != StringRef::npos) {
682 FormatTok->CodePointsInFirstLine = encoding::getCodePointCount(
683 Text.substr(0, FirstNewlinePos), Encoding);
684 FormatTok->CodePointsInLastLine = encoding::getCodePointCount(
685 Text.substr(Text.find_last_of('\n') + 1), Encoding);
686 }
687 }
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000688 // FIXME: Add the CodePointCount to Column.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000689 FormatTok->WhitespaceRange = SourceRange(
Manuel Klimek5c24cca2013-05-23 10:56:37 +0000690 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000691 return FormatTok;
692 }
693
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000694 FormatToken *FormatTok;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000695 bool GreaterStashed;
Manuel Klimek31c85922013-08-29 15:21:40 +0000696 unsigned Column;
Manuel Klimek9043c742013-05-27 15:23:34 +0000697 unsigned TrailingWhitespace;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000698 Lexer &Lex;
699 SourceManager &SourceMgr;
Manuel Klimek31c85922013-08-29 15:21:40 +0000700 FormatStyle &Style;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000701 IdentifierTable IdentTable;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000702 encoding::Encoding Encoding;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000703 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
704 SmallVector<FormatToken *, 16> Tokens;
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000705
Daniel Jasper8369aa52013-07-16 20:28:33 +0000706 void readRawToken(FormatToken &Tok) {
707 Lex.LexFromRawLexer(Tok.Tok);
708 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
709 Tok.Tok.getLength());
Daniel Jasper8369aa52013-07-16 20:28:33 +0000710 // For formatting, treat unterminated string literals like normal string
711 // literals.
712 if (Tok.is(tok::unknown) && !Tok.TokenText.empty() &&
713 Tok.TokenText[0] == '"') {
714 Tok.Tok.setKind(tok::string_literal);
715 Tok.IsUnterminatedLiteral = true;
716 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000717 }
718};
719
Daniel Jasperf7935112012-12-03 18:12:45 +0000720class Formatter : public UnwrappedLineConsumer {
721public:
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000722 Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
Daniel Jasperf7935112012-12-03 18:12:45 +0000723 const std::vector<CharSourceRange> &Ranges)
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000724 : Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000725 Whitespaces(SourceMgr, Style), Ranges(Ranges),
726 Encoding(encoding::detectEncoding(Lex.getBuffer())) {
Daniel Jasperfa21c072013-07-15 14:33:14 +0000727 DEBUG(llvm::dbgs() << "File encoding: "
728 << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
729 : "unknown")
730 << "\n");
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000731 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000732
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000733 virtual ~Formatter() {
734 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
735 delete AnnotatedLines[i];
736 }
737 AnnotatedLines.clear();
738 }
Daniel Jasper61bd3a12012-12-04 21:05:31 +0000739
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000740 tooling::Replacements format() {
Manuel Klimek31c85922013-08-29 15:21:40 +0000741 FormatTokenLexer Tokens(Lex, SourceMgr, Style, Encoding);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000742
743 UnwrappedLineParser Parser(Style, Tokens.lex(), *this);
Manuel Klimek1a18c402013-04-12 14:13:36 +0000744 bool StructuralError = Parser.parse();
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000745 TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in"));
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000746 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000747 Annotator.annotate(*AnnotatedLines[i]);
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000748 }
749 deriveLocalStyle();
750 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000751 Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000752 }
Daniel Jasperb67cc422013-04-09 17:46:55 +0000753
754 // Adapt level to the next line if this is a comment.
755 // FIXME: Can/should this be done in the UnwrappedLineParser?
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000756 const AnnotatedLine *NextNonCommentLine = NULL;
Daniel Jasperb67cc422013-04-09 17:46:55 +0000757 for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000758 if (NextNonCommentLine && AnnotatedLines[i]->First->is(tok::comment) &&
759 !AnnotatedLines[i]->First->Next)
760 AnnotatedLines[i]->Level = NextNonCommentLine->Level;
Daniel Jasperb67cc422013-04-09 17:46:55 +0000761 else
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000762 NextNonCommentLine = AnnotatedLines[i]->First->isNot(tok::r_brace)
763 ? AnnotatedLines[i]
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000764 : NULL;
Daniel Jasperb67cc422013-04-09 17:46:55 +0000765 }
766
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000767 std::vector<int> IndentForLevel;
768 bool PreviousLineWasTouched = false;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000769 const FormatToken *PreviousLineLastToken = 0;
Daniel Jasper1cb530f2013-05-10 13:00:49 +0000770 bool FormatPPDirective = false;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000771 for (std::vector<AnnotatedLine *>::iterator I = AnnotatedLines.begin(),
772 E = AnnotatedLines.end();
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000773 I != E; ++I) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000774 const AnnotatedLine &TheLine = **I;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000775 const FormatToken *FirstTok = TheLine.First;
776 int Offset = getIndentOffset(*TheLine.First);
Daniel Jasper1cb530f2013-05-10 13:00:49 +0000777
778 // Check whether this line is part of a formatted preprocessor directive.
Manuel Klimek591ab5a2013-05-28 13:42:28 +0000779 if (FirstTok->HasUnescapedNewline)
Daniel Jasper1cb530f2013-05-10 13:00:49 +0000780 FormatPPDirective = false;
781 if (!FormatPPDirective && TheLine.InPPDirective &&
782 (touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
783 FormatPPDirective = true;
784
Daniel Jasper12f9d8e2013-05-14 09:30:02 +0000785 // Determine indent and try to merge multiple unwrapped lines.
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000786 while (IndentForLevel.size() <= TheLine.Level)
787 IndentForLevel.push_back(-1);
788 IndentForLevel.resize(TheLine.Level + 1);
Daniel Jasper12f9d8e2013-05-14 09:30:02 +0000789 unsigned Indent = getIndent(IndentForLevel, TheLine.Level);
790 if (static_cast<int>(Indent) + Offset >= 0)
791 Indent += Offset;
792 tryFitMultipleLinesInOne(Indent, I, E);
793
Manuel Klimek591ab5a2013-05-28 13:42:28 +0000794 bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000795 if (TheLine.First->is(tok::eof)) {
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000796 if (PreviousLineWasTouched) {
Manuel Klimek591ab5a2013-05-28 13:42:28 +0000797 unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u);
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000798 Whitespaces.replaceWhitespace(*TheLine.First, NewLines, /*Indent*/ 0,
Manuel Klimek4fe43002013-05-22 12:51:29 +0000799 /*TargetColumn*/ 0);
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000800 }
801 } else if (TheLine.Type != LT_Invalid &&
Daniel Jasper1cb530f2013-05-10 13:00:49 +0000802 (WasMoved || FormatPPDirective || touchesLine(TheLine))) {
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000803 unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
Manuel Klimek591ab5a2013-05-28 13:42:28 +0000804 if (FirstTok->WhitespaceRange.isValid() &&
Manuel Klimek1a18c402013-04-12 14:13:36 +0000805 // Insert a break even if there is a structural error in case where
806 // we break apart a line consisting of multiple unwrapped lines.
Manuel Klimek591ab5a2013-05-28 13:42:28 +0000807 (FirstTok->NewlinesBefore == 0 || !StructuralError)) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000808 formatFirstToken(*TheLine.First, PreviousLineLastToken, Indent,
Manuel Klimek4fe43002013-05-22 12:51:29 +0000809 TheLine.InPPDirective);
Manuel Klimek1a18c402013-04-12 14:13:36 +0000810 } else {
Manuel Klimek31c85922013-08-29 15:21:40 +0000811 Indent = LevelIndent = FirstTok->OriginalColumn;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000812 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000813 ContinuationIndenter Indenter(Style, SourceMgr, Whitespaces, Encoding,
Daniel Jasperde0328a2013-08-16 11:20:30 +0000814 BinPackInconclusiveFunctions);
815
816 // If everything fits on a single line, just put it there.
817 unsigned ColumnLimit = Style.ColumnLimit;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000818 AnnotatedLine *NextLine = *(I + 1);
819 if ((I + 1) != E && NextLine->InPPDirective &&
820 !NextLine->First->HasUnescapedNewline)
821 ColumnLimit = getColumnLimit(TheLine.InPPDirective);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000822
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000823 if (TheLine.Last->TotalLength + Indent <= ColumnLimit) {
824 LineState State = Indenter.getInitialState(Indent, &TheLine);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000825 while (State.NextToken != NULL)
826 Indenter.addTokenToState(State, false, false);
827 } else if (Style.ColumnLimit == 0) {
828 NoColumnLimitFormatter Formatter(&Indenter);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000829 Formatter.format(Indent, &TheLine);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000830 } else {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000831 UnwrappedLineFormatter Formatter(&Indenter, &Whitespaces, Style,
832 TheLine);
833 Formatter.format(Indent);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000834 }
835
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000836 IndentForLevel[TheLine.Level] = LevelIndent;
837 PreviousLineWasTouched = true;
838 } else {
Manuel Klimek4fe43002013-05-22 12:51:29 +0000839 // Format the first token if necessary, and notify the WhitespaceManager
840 // about the unchanged whitespace.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000841 for (const FormatToken *Tok = TheLine.First; Tok != NULL;
842 Tok = Tok->Next) {
843 if (Tok == TheLine.First &&
844 (Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
Manuel Klimek31c85922013-08-29 15:21:40 +0000845 unsigned LevelIndent = Tok->OriginalColumn;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000846 // Remove trailing whitespace of the previous line if it was
847 // touched.
848 if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) {
849 formatFirstToken(*Tok, PreviousLineLastToken, LevelIndent,
850 TheLine.InPPDirective);
851 } else {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000852 Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
Manuel Klimek4fe43002013-05-22 12:51:29 +0000853 }
Daniel Jasper12f9d8e2013-05-14 09:30:02 +0000854
Manuel Klimek4fe43002013-05-22 12:51:29 +0000855 if (static_cast<int>(LevelIndent) - Offset >= 0)
856 LevelIndent -= Offset;
857 if (Tok->isNot(tok::comment))
858 IndentForLevel[TheLine.Level] = LevelIndent;
859 } else {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000860 Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
Manuel Klimek4fe43002013-05-22 12:51:29 +0000861 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000862 }
863 // If we did not reformat this unwrapped line, the column at the end of
864 // the last token is unchanged - thus, we can calculate the end of the
865 // last token.
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000866 PreviousLineWasTouched = false;
867 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000868 PreviousLineLastToken = TheLine.Last;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000869 }
870 return Whitespaces.generateReplacements();
871 }
872
873private:
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000874 void deriveLocalStyle() {
875 unsigned CountBoundToVariable = 0;
876 unsigned CountBoundToType = 0;
877 bool HasCpp03IncompatibleFormat = false;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000878 bool HasBinPackedFunction = false;
879 bool HasOnePerLineFunction = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000880 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000881 if (!AnnotatedLines[i]->First->Next)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000882 continue;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000883 FormatToken *Tok = AnnotatedLines[i]->First->Next;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000884 while (Tok->Next) {
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000885 if (Tok->Type == TT_PointerOrReference) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000886 bool SpacesBefore =
887 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
888 bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
889 Tok->Next->WhitespaceRange.getEnd();
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000890 if (SpacesBefore && !SpacesAfter)
891 ++CountBoundToVariable;
892 else if (!SpacesBefore && SpacesAfter)
893 ++CountBoundToType;
894 }
895
Daniel Jasper400adc62013-02-08 15:28:42 +0000896 if (Tok->Type == TT_TemplateCloser &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000897 Tok->Previous->Type == TT_TemplateCloser &&
898 Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd())
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000899 HasCpp03IncompatibleFormat = true;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000900
901 if (Tok->PackingKind == PPK_BinPacked)
902 HasBinPackedFunction = true;
903 if (Tok->PackingKind == PPK_OnePerLine)
904 HasOnePerLineFunction = true;
905
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000906 Tok = Tok->Next;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000907 }
908 }
909 if (Style.DerivePointerBinding) {
910 if (CountBoundToType > CountBoundToVariable)
911 Style.PointerBindsToType = true;
912 else if (CountBoundToType < CountBoundToVariable)
913 Style.PointerBindsToType = false;
914 }
915 if (Style.Standard == FormatStyle::LS_Auto) {
916 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
917 : FormatStyle::LS_Cpp03;
918 }
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000919 BinPackInconclusiveFunctions =
920 HasBinPackedFunction || !HasOnePerLineFunction;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000921 }
922
Manuel Klimekb95f5452013-02-08 17:38:27 +0000923 /// \brief Get the indent of \p Level from \p IndentForLevel.
924 ///
925 /// \p IndentForLevel must contain the indent for the level \c l
926 /// at \p IndentForLevel[l], or a value < 0 if the indent for
927 /// that level is unknown.
Daniel Jasper687af3b2013-02-14 14:26:07 +0000928 unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
Manuel Klimekb95f5452013-02-08 17:38:27 +0000929 if (IndentForLevel[Level] != -1)
930 return IndentForLevel[Level];
Manuel Klimekd076dcd2013-02-08 19:53:32 +0000931 if (Level == 0)
932 return 0;
Manuel Klimek13b97d82013-05-13 08:42:42 +0000933 return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
Manuel Klimekb95f5452013-02-08 17:38:27 +0000934 }
935
936 /// \brief Get the offset of the line relatively to the level.
937 ///
938 /// For example, 'public:' labels in classes are offset by 1 or 2
939 /// characters to the left from their level.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000940 int getIndentOffset(const FormatToken &RootToken) {
Alexander Kornienkofd433362013-03-27 17:08:02 +0000941 if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
Manuel Klimekb95f5452013-02-08 17:38:27 +0000942 return Style.AccessModifierOffset;
943 return 0;
944 }
945
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +0000946 /// \brief Tries to merge lines into one.
947 ///
948 /// This will change \c Line and \c AnnotatedLine to contain the merged line,
949 /// if possible; note that \c I will be incremented when lines are merged.
Daniel Jaspera67a8f02013-01-16 10:41:46 +0000950 void tryFitMultipleLinesInOne(unsigned Indent,
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000951 std::vector<AnnotatedLine *>::iterator &I,
952 std::vector<AnnotatedLine *>::iterator E) {
Daniel Jaspera67a8f02013-01-16 10:41:46 +0000953 // We can never merge stuff if there are trailing line comments.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000954 AnnotatedLine *TheLine = *I;
955 if (TheLine->Last->Type == TT_LineComment)
Daniel Jaspera67a8f02013-01-16 10:41:46 +0000956 return;
957
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000958 if (Indent > Style.ColumnLimit)
959 return;
960
Daniel Jasperc22f5b42013-02-28 11:05:57 +0000961 unsigned Limit = Style.ColumnLimit - Indent;
Daniel Jasper12ef4e52013-02-21 21:33:55 +0000962 // If we already exceed the column limit, we set 'Limit' to 0. The different
963 // tryMerge..() functions can then decide whether to still do merging.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000964 Limit = TheLine->Last->TotalLength > Limit
965 ? 0
966 : Limit - TheLine->Last->TotalLength;
Daniel Jasperc36492b2013-01-16 07:02:34 +0000967
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000968 if (I + 1 == E || (*(I + 1))->Type == LT_Invalid)
Daniel Jaspera67a8f02013-01-16 10:41:46 +0000969 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +0000970
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000971 if (TheLine->Last->is(tok::l_brace)) {
Daniel Jasper25837aa2013-01-14 14:14:23 +0000972 tryMergeSimpleBlock(I, E, Limit);
Daniel Jasper3a685df2013-05-16 12:12:21 +0000973 } else if (Style.AllowShortIfStatementsOnASingleLine &&
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000974 TheLine->First->is(tok::kw_if)) {
Daniel Jasper3a685df2013-05-16 12:12:21 +0000975 tryMergeSimpleControlStatement(I, E, Limit);
976 } else if (Style.AllowShortLoopsOnASingleLine &&
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000977 TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) {
Daniel Jasper3a685df2013-05-16 12:12:21 +0000978 tryMergeSimpleControlStatement(I, E, Limit);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000979 } else if (TheLine->InPPDirective && (TheLine->First->HasUnescapedNewline ||
980 TheLine->First->IsFirst)) {
Daniel Jasper39825ea2013-01-14 15:40:57 +0000981 tryMergeSimplePPDirective(I, E, Limit);
Daniel Jasper25837aa2013-01-14 14:14:23 +0000982 }
Daniel Jasper25837aa2013-01-14 14:14:23 +0000983 }
984
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000985 void tryMergeSimplePPDirective(std::vector<AnnotatedLine *>::iterator &I,
986 std::vector<AnnotatedLine *>::iterator E,
Daniel Jasper39825ea2013-01-14 15:40:57 +0000987 unsigned Limit) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +0000988 if (Limit == 0)
989 return;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000990 AnnotatedLine &Line = **I;
991 if (!(*(I + 1))->InPPDirective || (*(I + 1))->First->HasUnescapedNewline)
Daniel Jasper2ab0d012013-01-14 15:52:06 +0000992 return;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000993 if (I + 2 != E && (*(I + 2))->InPPDirective &&
994 !(*(I + 2))->First->HasUnescapedNewline)
Daniel Jasper39825ea2013-01-14 15:40:57 +0000995 return;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000996 if (1 + (*(I + 1))->Last->TotalLength > Limit)
Daniel Jaspera67a8f02013-01-16 10:41:46 +0000997 return;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000998 join(Line, **(++I));
Daniel Jasper39825ea2013-01-14 15:40:57 +0000999 }
1000
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001001 void tryMergeSimpleControlStatement(std::vector<AnnotatedLine *>::iterator &I,
1002 std::vector<AnnotatedLine *>::iterator E,
Daniel Jasper3a685df2013-05-16 12:12:21 +00001003 unsigned Limit) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001004 if (Limit == 0)
1005 return;
Manuel Klimeka027f302013-08-07 19:20:45 +00001006 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman &&
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001007 (*(I + 1))->First->is(tok::l_brace))
Manuel Klimeka027f302013-08-07 19:20:45 +00001008 return;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001009 if ((*(I + 1))->InPPDirective != (*I)->InPPDirective ||
1010 ((*(I + 1))->InPPDirective && (*(I + 1))->First->HasUnescapedNewline))
Manuel Klimekda087612013-01-18 14:46:43 +00001011 return;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001012 AnnotatedLine &Line = **I;
Daniel Jasperc36492b2013-01-16 07:02:34 +00001013 if (Line.Last->isNot(tok::r_paren))
1014 return;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001015 if (1 + (*(I + 1))->Last->TotalLength > Limit)
Daniel Jasper25837aa2013-01-14 14:14:23 +00001016 return;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001017 if ((*(I + 1))->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for,
1018 tok::kw_while) ||
1019 (*(I + 1))->First->Type == TT_LineComment)
Daniel Jasper25837aa2013-01-14 14:14:23 +00001020 return;
1021 // Only inline simple if's (no nested if or else).
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001022 if (I + 2 != E && Line.First->is(tok::kw_if) &&
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001023 (*(I + 2))->First->is(tok::kw_else))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001024 return;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001025 join(Line, **(++I));
Daniel Jasper25837aa2013-01-14 14:14:23 +00001026 }
1027
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001028 void tryMergeSimpleBlock(std::vector<AnnotatedLine *>::iterator &I,
1029 std::vector<AnnotatedLine *>::iterator E,
Daniel Jasperbbc84152013-01-29 11:27:30 +00001030 unsigned Limit) {
Daniel Jasperabca58c2013-05-15 14:09:55 +00001031 // No merging if the brace already is on the next line.
1032 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
1033 return;
1034
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001035 // First, check that the current line allows merging. This is the case if
1036 // we're not in a control flow statement and the last token is an opening
1037 // brace.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001038 AnnotatedLine &Line = **I;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001039 if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
1040 tok::kw_else, tok::kw_try, tok::kw_catch,
Daniel Jaspera9eb2aa2013-05-31 14:56:20 +00001041 tok::kw_for,
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001042 // This gets rid of all ObjC @ keywords and methods.
1043 tok::at, tok::minus, tok::plus))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001044 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001045
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001046 FormatToken *Tok = (*(I + 1))->First;
Daniel Jaspera9eb2aa2013-05-31 14:56:20 +00001047 if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
Alexander Kornienko1efe0a02013-07-04 14:47:51 +00001048 (Tok->getNextNonComment() == NULL ||
1049 Tok->getNextNonComment()->is(tok::semi))) {
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001050 // We merge empty blocks even if the line exceeds the column limit.
Daniel Jaspereef30492013-02-11 12:36:37 +00001051 Tok->SpacesRequiredBefore = 0;
Daniel Jasper12ef4e52013-02-21 21:33:55 +00001052 Tok->CanBreakBefore = true;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001053 join(Line, **(I + 1));
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001054 I += 1;
Daniel Jaspera9eb2aa2013-05-31 14:56:20 +00001055 } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) {
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001056 // Check that we still have three lines and they fit into the limit.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001057 if (I + 2 == E || (*(I + 2))->Type == LT_Invalid ||
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001058 !nextTwoLinesFitInto(I, Limit))
Daniel Jasper25837aa2013-01-14 14:14:23 +00001059 return;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001060
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001061 // Second, check that the next line does not contain any braces - if it
1062 // does, readability declines when putting it into a single line.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001063 if ((*(I + 1))->Last->Type == TT_LineComment || Tok->MustBreakBefore)
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001064 return;
1065 do {
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001066 if (Tok->isOneOf(tok::l_brace, tok::r_brace))
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001067 return;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001068 Tok = Tok->Next;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001069 } while (Tok != NULL);
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001070
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001071 // Last, check that the third line contains a single closing brace.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001072 Tok = (*(I + 2))->First;
Alexander Kornienko1efe0a02013-07-04 14:47:51 +00001073 if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) ||
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001074 Tok->MustBreakBefore)
1075 return;
1076
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001077 join(Line, **(I + 1));
1078 join(Line, **(I + 2));
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001079 I += 2;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001080 }
Daniel Jasper25837aa2013-01-14 14:14:23 +00001081 }
1082
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001083 bool nextTwoLinesFitInto(std::vector<AnnotatedLine *>::iterator I,
Daniel Jasper25837aa2013-01-14 14:14:23 +00001084 unsigned Limit) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001085 return 1 + (*(I + 1))->Last->TotalLength + 1 +
1086 (*(I + 2))->Last->TotalLength <=
Manuel Klimeka4fe1c12013-01-21 16:42:44 +00001087 Limit;
Manuel Klimekf4ab9ef2013-01-11 17:54:10 +00001088 }
1089
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001090 void join(AnnotatedLine &A, const AnnotatedLine &B) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001091 assert(!A.Last->Next);
1092 assert(!B.First->Previous);
1093 A.Last->Next = B.First;
1094 B.First->Previous = A.Last;
1095 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
1096 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
1097 Tok->TotalLength += LengthA;
1098 A.Last = Tok;
Daniel Jasperf1e4b7d2013-01-14 13:08:07 +00001099 }
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001100 }
1101
Daniel Jasper97b89482013-03-13 07:49:51 +00001102 bool touchesRanges(const CharSourceRange &Range) {
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001103 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1104 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
1105 Ranges[i].getBegin()) &&
1106 !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
1107 Range.getBegin()))
1108 return true;
1109 }
1110 return false;
1111 }
1112
1113 bool touchesLine(const AnnotatedLine &TheLine) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001114 const FormatToken *First = TheLine.First;
1115 const FormatToken *Last = TheLine.Last;
Daniel Jaspercdd06622013-05-14 10:31:09 +00001116 CharSourceRange LineRange = CharSourceRange::getCharRange(
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001117 First->WhitespaceRange.getBegin().getLocWithOffset(
1118 First->LastNewlineOffset),
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001119 Last->Tok.getLocation().getLocWithOffset(Last->TokenText.size() - 1));
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001120 return touchesRanges(LineRange);
1121 }
1122
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001123 bool touchesPPDirective(std::vector<AnnotatedLine *>::iterator I,
1124 std::vector<AnnotatedLine *>::iterator E) {
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001125 for (; I != E; ++I) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001126 if ((*I)->First->HasUnescapedNewline)
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001127 return false;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001128 if (touchesLine(**I))
Daniel Jasper1cb530f2013-05-10 13:00:49 +00001129 return true;
1130 }
1131 return false;
1132 }
1133
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001134 bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001135 const FormatToken *First = TheLine.First;
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001136 CharSourceRange LineRange = CharSourceRange::getCharRange(
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001137 First->WhitespaceRange.getBegin(),
1138 First->WhitespaceRange.getBegin().getLocWithOffset(
1139 First->LastNewlineOffset));
Daniel Jasperf71cf3b2013-03-07 20:50:00 +00001140 return touchesRanges(LineRange);
Manuel Klimek51bd6ec2013-01-10 19:49:59 +00001141 }
1142
1143 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001144 AnnotatedLines.push_back(new AnnotatedLine(TheLine));
Daniel Jasperf7935112012-12-03 18:12:45 +00001145 }
1146
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001147 /// \brief Add a new line and the required indent before the first Token
1148 /// of the \c UnwrappedLine if there was no structural parsing error.
1149 /// Returns the indent level of the \c UnwrappedLine.
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001150 void formatFirstToken(const FormatToken &RootToken,
1151 const FormatToken *PreviousToken, unsigned Indent,
Manuel Klimek4fe43002013-05-22 12:51:29 +00001152 bool InPPDirective) {
Daniel Jasperbbc84152013-01-29 11:27:30 +00001153 unsigned Newlines =
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001154 std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
Daniel Jasper1027c6e2013-06-03 16:16:41 +00001155 // Remove empty lines before "}" where applicable.
1156 if (RootToken.is(tok::r_brace) &&
1157 (!RootToken.Next ||
1158 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
1159 Newlines = std::min(Newlines, 1u);
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001160 if (Newlines == 0 && !RootToken.IsFirst)
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001161 Newlines = 1;
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001162
Manuel Klimek4fe43002013-05-22 12:51:29 +00001163 // Insert extra new line before access specifiers.
1164 if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001165 RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
Manuel Klimek4fe43002013-05-22 12:51:29 +00001166 ++Newlines;
Alexander Kornienkofd433362013-03-27 17:08:02 +00001167
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001168 Whitespaces.replaceWhitespace(
1169 RootToken, Newlines, Indent, Indent,
1170 InPPDirective && !RootToken.HasUnescapedNewline);
Manuel Klimek0b689fd2013-01-10 18:45:26 +00001171 }
1172
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001173 unsigned getColumnLimit(bool InPPDirective) const {
1174 // In preprocessor directives reserve two chars for trailing " \"
1175 return Style.ColumnLimit - (InPPDirective ? 2 : 0);
1176 }
1177
Daniel Jasperf7935112012-12-03 18:12:45 +00001178 FormatStyle Style;
1179 Lexer &Lex;
1180 SourceManager &SourceMgr;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001181 WhitespaceManager Whitespaces;
Daniel Jasperf7935112012-12-03 18:12:45 +00001182 std::vector<CharSourceRange> Ranges;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001183 std::vector<AnnotatedLine *> AnnotatedLines;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001184
1185 encoding::Encoding Encoding;
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001186 bool BinPackInconclusiveFunctions;
Daniel Jasperf7935112012-12-03 18:12:45 +00001187};
1188
Craig Topperaf35e852013-06-30 22:29:28 +00001189} // end anonymous namespace
1190
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00001191tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
1192 SourceManager &SourceMgr,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +00001193 std::vector<CharSourceRange> Ranges) {
1194 Formatter formatter(Style, Lex, SourceMgr, Ranges);
Daniel Jasperf7935112012-12-03 18:12:45 +00001195 return formatter.format();
1196}
1197
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001198tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
1199 std::vector<tooling::Range> Ranges,
1200 StringRef FileName) {
1201 FileManager Files((FileSystemOptions()));
1202 DiagnosticsEngine Diagnostics(
1203 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1204 new DiagnosticOptions);
1205 SourceManager SourceMgr(Diagnostics, Files);
1206 llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName);
1207 const clang::FileEntry *Entry =
1208 Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
1209 SourceMgr.overrideFileContents(Entry, Buf);
1210 FileID ID =
1211 SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
Alexander Kornienko1e808872013-06-28 12:51:24 +00001212 Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr,
1213 getFormattingLangOpts(Style.Standard));
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001214 SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
1215 std::vector<CharSourceRange> CharRanges;
1216 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1217 SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset());
1218 SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength());
1219 CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1220 }
1221 return reformat(Style, Lex, SourceMgr, CharRanges);
1222}
1223
Alexander Kornienko1e808872013-06-28 12:51:24 +00001224LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) {
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001225 LangOptions LangOpts;
1226 LangOpts.CPlusPlus = 1;
Alexander Kornienko1e808872013-06-28 12:51:24 +00001227 LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
Daniel Jasper55213652013-03-22 10:01:29 +00001228 LangOpts.LineComment = 1;
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001229 LangOpts.Bool = 1;
1230 LangOpts.ObjC1 = 1;
1231 LangOpts.ObjC2 = 1;
1232 return LangOpts;
1233}
1234
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001235} // namespace format
1236} // namespace clang