blob: 976b5466815ec224959ddd781d16b31d9780a4ab [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"
Edwin Vaned544aa72013-09-30 13:31:48 +000029#include "llvm/Support/Path.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000030#include "llvm/Support/YAMLTraits.h"
Manuel Klimek2ef908e2013-02-13 10:46:36 +000031#include <queue>
Daniel Jasper8b529712012-12-04 13:02:32 +000032#include <string>
33
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000034using clang::format::FormatStyle;
35
Alexander Kornienkod6538332013-05-07 15:32:14 +000036namespace llvm {
37namespace yaml {
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000038template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> {
39 static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) {
40 IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp);
41 IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript);
Daniel Jasper7052ce62014-01-19 09:04:08 +000042 IO.enumCase(Value, "Proto", FormatStyle::LK_Proto);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000043 }
44};
45
46template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> {
47 static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) {
48 IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03);
49 IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03);
50 IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11);
51 IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11);
52 IO.enumCase(Value, "Auto", FormatStyle::LS_Auto);
53 }
54};
55
56template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
57 static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) {
58 IO.enumCase(Value, "Never", FormatStyle::UT_Never);
59 IO.enumCase(Value, "false", FormatStyle::UT_Never);
60 IO.enumCase(Value, "Always", FormatStyle::UT_Always);
61 IO.enumCase(Value, "true", FormatStyle::UT_Always);
62 IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation);
63 }
64};
65
66template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
67 static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) {
68 IO.enumCase(Value, "Attach", FormatStyle::BS_Attach);
69 IO.enumCase(Value, "Linux", FormatStyle::BS_Linux);
70 IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
71 IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
Alexander Kornienko3a33f022013-12-12 09:49:52 +000072 IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000073 }
74};
75
Alexander Kornienkod6538332013-05-07 15:32:14 +000076template <>
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000077struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
Alexander Kornienkocabdd732013-11-29 15:19:43 +000078 static void enumeration(IO &IO,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000079 FormatStyle::NamespaceIndentationKind &Value) {
80 IO.enumCase(Value, "None", FormatStyle::NI_None);
81 IO.enumCase(Value, "Inner", FormatStyle::NI_Inner);
82 IO.enumCase(Value, "All", FormatStyle::NI_All);
Alexander Kornienkocabdd732013-11-29 15:19:43 +000083 }
84};
85
86template <>
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000087struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> {
Manuel Klimeka8eb9142013-05-13 12:51:40 +000088 static void enumeration(IO &IO,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000089 FormatStyle::SpaceBeforeParensOptions &Value) {
90 IO.enumCase(Value, "Never", FormatStyle::SBPO_Never);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +000091 IO.enumCase(Value, "ControlStatements",
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000092 FormatStyle::SBPO_ControlStatements);
93 IO.enumCase(Value, "Always", FormatStyle::SBPO_Always);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +000094
95 // For backward compatibility.
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000096 IO.enumCase(Value, "false", FormatStyle::SBPO_Never);
97 IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +000098 }
99};
100
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000101template <> struct MappingTraits<FormatStyle> {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000102 static void mapping(IO &IO, FormatStyle &Style) {
103 // When reading, read the language first, we need it for getPredefinedStyle.
104 IO.mapOptional("Language", Style.Language);
105
Alexander Kornienko49149672013-05-10 11:56:10 +0000106 if (IO.outputting()) {
Alexander Kornienkoe3648fb2013-09-02 16:39:23 +0000107 StringRef StylesArray[] = { "LLVM", "Google", "Chromium",
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000108 "Mozilla", "WebKit", "GNU" };
Alexander Kornienko49149672013-05-10 11:56:10 +0000109 ArrayRef<StringRef> Styles(StylesArray);
110 for (size_t i = 0, e = Styles.size(); i < e; ++i) {
111 StringRef StyleName(Styles[i]);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000112 FormatStyle PredefinedStyle;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000113 if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000114 Style == PredefinedStyle) {
Alexander Kornienko49149672013-05-10 11:56:10 +0000115 IO.mapOptional("# BasedOnStyle", StyleName);
116 break;
117 }
118 }
119 } else {
Alexander Kornienkod6538332013-05-07 15:32:14 +0000120 StringRef BasedOnStyle;
121 IO.mapOptional("BasedOnStyle", BasedOnStyle);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000122 if (!BasedOnStyle.empty()) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000123 FormatStyle::LanguageKind OldLanguage = Style.Language;
124 FormatStyle::LanguageKind Language =
125 ((FormatStyle *)IO.getContext())->Language;
126 if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000127 IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
128 return;
129 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000130 Style.Language = OldLanguage;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000131 }
Alexander Kornienkod6538332013-05-07 15:32:14 +0000132 }
133
134 IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
Daniel Jaspercdaffa42013-08-13 10:58:30 +0000135 IO.mapOptional("ConstructorInitializerIndentWidth",
136 Style.ConstructorInitializerIndentWidth);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000137 IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
Daniel Jasper552f4a72013-07-31 23:55:15 +0000138 IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000139 IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
140 Style.AllowAllParametersOfDeclarationOnNextLine);
141 IO.mapOptional("AllowShortIfStatementsOnASingleLine",
142 Style.AllowShortIfStatementsOnASingleLine);
Daniel Jasper3a685df2013-05-16 12:12:21 +0000143 IO.mapOptional("AllowShortLoopsOnASingleLine",
144 Style.AllowShortLoopsOnASingleLine);
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000145 IO.mapOptional("AllowShortFunctionsOnASingleLine",
146 Style.AllowShortFunctionsOnASingleLine);
Daniel Jasper61e6bbf2013-05-29 12:07:31 +0000147 IO.mapOptional("AlwaysBreakTemplateDeclarations",
148 Style.AlwaysBreakTemplateDeclarations);
Alexander Kornienko58611712013-07-04 12:02:44 +0000149 IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
150 Style.AlwaysBreakBeforeMultilineStrings);
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000151 IO.mapOptional("BreakBeforeBinaryOperators",
152 Style.BreakBeforeBinaryOperators);
Daniel Jasper165b29e2013-11-08 00:57:11 +0000153 IO.mapOptional("BreakBeforeTernaryOperators",
154 Style.BreakBeforeTernaryOperators);
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000155 IO.mapOptional("BreakConstructorInitializersBeforeComma",
156 Style.BreakConstructorInitializersBeforeComma);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000157 IO.mapOptional("BinPackParameters", Style.BinPackParameters);
158 IO.mapOptional("ColumnLimit", Style.ColumnLimit);
159 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
160 Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
161 IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000162 IO.mapOptional("ExperimentalAutoDetectBinPacking",
163 Style.ExperimentalAutoDetectBinPacking);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000164 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
165 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000166 IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
Daniel Jaspere9beea22014-01-28 15:20:33 +0000167 IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000168 IO.mapOptional("ObjCSpaceBeforeProtocolList",
169 Style.ObjCSpaceBeforeProtocolList);
Daniel Jasper33b909c2013-10-25 14:29:37 +0000170 IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
171 Style.PenaltyBreakBeforeFirstCallParameter);
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000172 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
173 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000174 IO.mapOptional("PenaltyBreakFirstLessLess",
175 Style.PenaltyBreakFirstLessLess);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000176 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
177 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
178 Style.PenaltyReturnTypeOnItsOwnLine);
179 IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
180 IO.mapOptional("SpacesBeforeTrailingComments",
181 Style.SpacesBeforeTrailingComments);
Daniel Jasper6ab54682013-07-16 18:22:10 +0000182 IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000183 IO.mapOptional("Standard", Style.Standard);
Manuel Klimek13b97d82013-05-13 08:42:42 +0000184 IO.mapOptional("IndentWidth", Style.IndentWidth);
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000185 IO.mapOptional("TabWidth", Style.TabWidth);
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000186 IO.mapOptional("UseTab", Style.UseTab);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000187 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
Manuel Klimek836c2862013-06-21 17:25:42 +0000188 IO.mapOptional("IndentFunctionDeclarationAfterType",
189 Style.IndentFunctionDeclarationAfterType);
Daniel Jasperb55acad2013-08-20 12:36:34 +0000190 IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
Daniel Jasperdd978ae2013-10-29 14:52:02 +0000191 IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
Daniel Jasperf110e202013-08-21 08:39:01 +0000192 IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
Daniel Jasperb55acad2013-08-20 12:36:34 +0000193 IO.mapOptional("SpacesInCStyleCastParentheses",
194 Style.SpacesInCStyleCastParentheses);
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000195 IO.mapOptional("SpacesInContainerLiterals",
196 Style.SpacesInContainerLiterals);
Daniel Jasperd94bff32013-09-25 15:15:02 +0000197 IO.mapOptional("SpaceBeforeAssignmentOperators",
198 Style.SpaceBeforeAssignmentOperators);
Daniel Jasper6633ab82013-10-18 10:38:14 +0000199 IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
Alexander Kornienkoce9161a2014-01-02 15:13:14 +0000200 IO.mapOptional("CommentPragmas", Style.CommentPragmas);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000201
202 // For backward compatibility.
203 if (!IO.outputting()) {
204 IO.mapOptional("SpaceAfterControlStatementKeyword",
205 Style.SpaceBeforeParens);
206 }
207 IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000208 }
209};
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000210
211// Allows to read vector<FormatStyle> while keeping default values.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000212// IO.getContext() should contain a pointer to the FormatStyle structure, that
213// will be used to get default values for missing keys.
214// If the first element has no Language specified, it will be treated as the
215// default one for the following elements.
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000216template <> struct DocumentListTraits<std::vector<FormatStyle> > {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000217 static size_t size(IO &IO, std::vector<FormatStyle> &Seq) {
218 return Seq.size();
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000219 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000220 static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000221 size_t Index) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000222 if (Index >= Seq.size()) {
223 assert(Index == Seq.size());
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000224 FormatStyle Template;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000225 if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000226 Template = Seq[0];
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000227 } else {
228 Template = *((const FormatStyle*)IO.getContext());
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000229 Template.Language = FormatStyle::LK_None;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000230 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000231 Seq.resize(Index + 1, Template);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000232 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000233 return Seq[Index];
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000234 }
235};
Alexander Kornienkod6538332013-05-07 15:32:14 +0000236}
237}
238
Daniel Jasperf7935112012-12-03 18:12:45 +0000239namespace clang {
240namespace format {
241
Daniel Jasperf7935112012-12-03 18:12:45 +0000242FormatStyle getLLVMStyle() {
243 FormatStyle LLVMStyle;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000244 LLVMStyle.Language = FormatStyle::LK_Cpp;
Daniel Jasperf7935112012-12-03 18:12:45 +0000245 LLVMStyle.AccessModifierOffset = -2;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000246 LLVMStyle.AlignEscapedNewlinesLeft = false;
Daniel Jasper552f4a72013-07-31 23:55:15 +0000247 LLVMStyle.AlignTrailingComments = true;
Daniel Jasperf7db4332013-01-29 16:03:49 +0000248 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000249 LLVMStyle.AllowShortFunctionsOnASingleLine = true;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000250 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000251 LLVMStyle.AllowShortLoopsOnASingleLine = false;
Alexander Kornienko58611712013-07-04 12:02:44 +0000252 LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000253 LLVMStyle.AlwaysBreakTemplateDeclarations = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000254 LLVMStyle.BinPackParameters = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000255 LLVMStyle.BreakBeforeBinaryOperators = false;
Daniel Jasper165b29e2013-11-08 00:57:11 +0000256 LLVMStyle.BreakBeforeTernaryOperators = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000257 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
258 LLVMStyle.BreakConstructorInitializersBeforeComma = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000259 LLVMStyle.ColumnLimit = 80;
260 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Daniel Jaspercdaffa42013-08-13 10:58:30 +0000261 LLVMStyle.ConstructorInitializerIndentWidth = 4;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000262 LLVMStyle.Cpp11BracedListStyle = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000263 LLVMStyle.DerivePointerBinding = false;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000264 LLVMStyle.ExperimentalAutoDetectBinPacking = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000265 LLVMStyle.IndentCaseLabels = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000266 LLVMStyle.IndentFunctionDeclarationAfterType = false;
267 LLVMStyle.IndentWidth = 2;
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000268 LLVMStyle.TabWidth = 8;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000269 LLVMStyle.MaxEmptyLinesToKeep = 1;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000270 LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000271 LLVMStyle.ObjCSpaceAfterProperty = false;
Nico Webera6087752013-01-10 20:12:55 +0000272 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000273 LLVMStyle.PointerBindsToType = false;
274 LLVMStyle.SpacesBeforeTrailingComments = 1;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000275 LLVMStyle.Standard = FormatStyle::LS_Cpp11;
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000276 LLVMStyle.UseTab = FormatStyle::UT_Never;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000277 LLVMStyle.SpacesInParentheses = false;
278 LLVMStyle.SpaceInEmptyParentheses = false;
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000279 LLVMStyle.SpacesInContainerLiterals = true;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000280 LLVMStyle.SpacesInCStyleCastParentheses = false;
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000281 LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
Daniel Jasperd94bff32013-09-25 15:15:02 +0000282 LLVMStyle.SpaceBeforeAssignmentOperators = true;
Daniel Jasper6633ab82013-10-18 10:38:14 +0000283 LLVMStyle.ContinuationIndentWidth = 4;
Daniel Jasperdd978ae2013-10-29 14:52:02 +0000284 LLVMStyle.SpacesInAngles = false;
Alexander Kornienkoce9161a2014-01-02 15:13:14 +0000285 LLVMStyle.CommentPragmas = "^ IWYU pragma:";
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000286
Daniel Jasper19a541e2013-12-19 16:45:34 +0000287 LLVMStyle.PenaltyBreakComment = 300;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000288 LLVMStyle.PenaltyBreakFirstLessLess = 120;
289 LLVMStyle.PenaltyBreakString = 1000;
290 LLVMStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000291 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
Daniel Jasper33b909c2013-10-25 14:29:37 +0000292 LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000293
Daniel Jasperf7935112012-12-03 18:12:45 +0000294 return LLVMStyle;
295}
296
Nico Weber514ecc82014-02-02 20:50:45 +0000297FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000298 FormatStyle GoogleStyle = getLLVMStyle();
Nico Weber514ecc82014-02-02 20:50:45 +0000299 GoogleStyle.Language = Language;
300
Daniel Jasperf7935112012-12-03 18:12:45 +0000301 GoogleStyle.AccessModifierOffset = -1;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000302 GoogleStyle.AlignEscapedNewlinesLeft = true;
Daniel Jasper085a2ed2013-04-24 13:46:00 +0000303 GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
Daniel Jasper5bd0b9e2013-05-23 18:05:18 +0000304 GoogleStyle.AllowShortLoopsOnASingleLine = true;
Alexander Kornienko58611712013-07-04 12:02:44 +0000305 GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000306 GoogleStyle.AlwaysBreakTemplateDeclarations = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000307 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
308 GoogleStyle.DerivePointerBinding = true;
309 GoogleStyle.IndentCaseLabels = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000310 GoogleStyle.IndentFunctionDeclarationAfterType = true;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000311 GoogleStyle.ObjCSpaceAfterProperty = false;
Nico Webera6087752013-01-10 20:12:55 +0000312 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000313 GoogleStyle.PointerBindsToType = true;
314 GoogleStyle.SpacesBeforeTrailingComments = 2;
315 GoogleStyle.Standard = FormatStyle::LS_Auto;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000316
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000317 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
Daniel Jasper33b909c2013-10-25 14:29:37 +0000318 GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000319
Nico Weber514ecc82014-02-02 20:50:45 +0000320 if (Language == FormatStyle::LK_JavaScript) {
321 GoogleStyle.BreakBeforeTernaryOperators = false;
322 GoogleStyle.MaxEmptyLinesToKeep = 2;
323 GoogleStyle.SpacesInContainerLiterals = false;
324 } else if (Language == FormatStyle::LK_Proto) {
325 GoogleStyle.AllowShortFunctionsOnASingleLine = false;
326 }
327
Daniel Jasperf7935112012-12-03 18:12:45 +0000328 return GoogleStyle;
329}
330
Nico Weber514ecc82014-02-02 20:50:45 +0000331FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
332 FormatStyle ChromiumStyle = getGoogleStyle(Language);
Daniel Jasperf7db4332013-01-29 16:03:49 +0000333 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Daniel Jasper085a2ed2013-04-24 13:46:00 +0000334 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000335 ChromiumStyle.AllowShortLoopsOnASingleLine = false;
Daniel Jasper2cf17bf2013-02-27 09:47:53 +0000336 ChromiumStyle.BinPackParameters = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000337 ChromiumStyle.DerivePointerBinding = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000338 ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000339 return ChromiumStyle;
340}
341
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000342FormatStyle getMozillaStyle() {
343 FormatStyle MozillaStyle = getLLVMStyle();
344 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000345 MozillaStyle.Cpp11BracedListStyle = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000346 MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
347 MozillaStyle.DerivePointerBinding = true;
348 MozillaStyle.IndentCaseLabels = true;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000349 MozillaStyle.ObjCSpaceAfterProperty = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000350 MozillaStyle.ObjCSpaceBeforeProtocolList = false;
351 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
352 MozillaStyle.PointerBindsToType = true;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000353 MozillaStyle.Standard = FormatStyle::LS_Cpp03;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000354 return MozillaStyle;
355}
356
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000357FormatStyle getWebKitStyle() {
358 FormatStyle Style = getLLVMStyle();
Daniel Jasper65ee3472013-07-31 23:16:02 +0000359 Style.AccessModifierOffset = -4;
Daniel Jasper552f4a72013-07-31 23:55:15 +0000360 Style.AlignTrailingComments = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000361 Style.BreakBeforeBinaryOperators = true;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000362 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000363 Style.BreakConstructorInitializersBeforeComma = true;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000364 Style.Cpp11BracedListStyle = false;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000365 Style.ColumnLimit = 0;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000366 Style.IndentWidth = 4;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000367 Style.NamespaceIndentation = FormatStyle::NI_Inner;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000368 Style.ObjCSpaceAfterProperty = true;
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000369 Style.PointerBindsToType = true;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000370 Style.Standard = FormatStyle::LS_Cpp03;
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000371 return Style;
372}
373
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000374FormatStyle getGNUStyle() {
375 FormatStyle Style = getLLVMStyle();
376 Style.BreakBeforeBinaryOperators = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000377 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000378 Style.BreakBeforeTernaryOperators = true;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000379 Style.Cpp11BracedListStyle = false;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000380 Style.ColumnLimit = 79;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000381 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000382 Style.Standard = FormatStyle::LS_Cpp03;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000383 return Style;
384}
385
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000386bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
387 FormatStyle *Style) {
388 if (Name.equals_lower("llvm")) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000389 *Style = getLLVMStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000390 } else if (Name.equals_lower("chromium")) {
Nico Weber514ecc82014-02-02 20:50:45 +0000391 *Style = getChromiumStyle(Language);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000392 } else if (Name.equals_lower("mozilla")) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000393 *Style = getMozillaStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000394 } else if (Name.equals_lower("google")) {
Nico Weber514ecc82014-02-02 20:50:45 +0000395 *Style = getGoogleStyle(Language);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000396 } else if (Name.equals_lower("webkit")) {
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000397 *Style = getWebKitStyle();
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000398 } else if (Name.equals_lower("gnu")) {
399 *Style = getGNUStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000400 } else {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000401 return false;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000402 }
Alexander Kornienkod6538332013-05-07 15:32:14 +0000403
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000404 Style->Language = Language;
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000405 return true;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000406}
407
408llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000409 assert(Style);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000410 FormatStyle::LanguageKind Language = Style->Language;
411 assert(Language != FormatStyle::LK_None);
Alexander Kornienko06e00332013-05-20 15:18:01 +0000412 if (Text.trim().empty())
413 return llvm::make_error_code(llvm::errc::invalid_argument);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000414
415 std::vector<FormatStyle> Styles;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000416 llvm::yaml::Input Input(Text);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000417 // DocumentListTraits<vector<FormatStyle>> uses the context to get default
418 // values for the fields, keys for which are missing from the configuration.
419 // Mapping also uses the context to get the language to find the correct
420 // base style.
421 Input.setContext(Style);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000422 Input >> Styles;
423 if (Input.error())
424 return Input.error();
425
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000426 for (unsigned i = 0; i < Styles.size(); ++i) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000427 // Ensures that only the first configuration can skip the Language option.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000428 if (Styles[i].Language == FormatStyle::LK_None && i != 0)
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000429 return llvm::make_error_code(llvm::errc::invalid_argument);
430 // Ensure that each language is configured at most once.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000431 for (unsigned j = 0; j < i; ++j) {
432 if (Styles[i].Language == Styles[j].Language) {
433 DEBUG(llvm::dbgs()
434 << "Duplicate languages in the config file on positions " << j
435 << " and " << i << "\n");
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000436 return llvm::make_error_code(llvm::errc::invalid_argument);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000437 }
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000438 }
439 }
440 // Look for a suitable configuration starting from the end, so we can
441 // find the configuration for the specific language first, and the default
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000442 // configuration (which can only be at slot 0) after it.
443 for (int i = Styles.size() - 1; i >= 0; --i) {
444 if (Styles[i].Language == Language ||
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000445 Styles[i].Language == FormatStyle::LK_None) {
446 *Style = Styles[i];
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000447 Style->Language = Language;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000448 return llvm::make_error_code(llvm::errc::success);
449 }
450 }
451 return llvm::make_error_code(llvm::errc::not_supported);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000452}
453
454std::string configurationAsText(const FormatStyle &Style) {
455 std::string Text;
456 llvm::raw_string_ostream Stream(Text);
457 llvm::yaml::Output Output(Stream);
458 // We use the same mapping method for input and output, so we need a non-const
459 // reference here.
460 FormatStyle NonConstStyle = Style;
461 Output << NonConstStyle;
Alexander Kornienko9a38ec22013-05-13 12:56:35 +0000462 return Stream.str();
Alexander Kornienkod6538332013-05-07 15:32:14 +0000463}
464
Craig Topperaf35e852013-06-30 22:29:28 +0000465namespace {
466
Daniel Jasperde0328a2013-08-16 11:20:30 +0000467class NoColumnLimitFormatter {
468public:
Daniel Jasperf110e202013-08-21 08:39:01 +0000469 NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {}
Daniel Jasperde0328a2013-08-16 11:20:30 +0000470
471 /// \brief Formats the line starting at \p State, simply keeping all of the
472 /// input's line breaking decisions.
Alexander Kornienkoa594ba82013-12-16 14:35:51 +0000473 void format(unsigned FirstIndent, const AnnotatedLine *Line) {
Daniel Jasper1c5d9df2013-09-06 07:54:20 +0000474 LineState State =
475 Indenter->getInitialState(FirstIndent, Line, /*DryRun=*/false);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000476 while (State.NextToken != NULL) {
477 bool Newline =
Alexander Kornienkoa594ba82013-12-16 14:35:51 +0000478 Indenter->mustBreak(State) ||
Daniel Jasperde0328a2013-08-16 11:20:30 +0000479 (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
480 Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
481 }
482 }
Daniel Jasperf110e202013-08-21 08:39:01 +0000483
Daniel Jasperde0328a2013-08-16 11:20:30 +0000484private:
485 ContinuationIndenter *Indenter;
486};
487
Daniel Jasper56f8b432013-11-06 23:12:09 +0000488class LineJoiner {
489public:
490 LineJoiner(const FormatStyle &Style) : Style(Style) {}
491
492 /// \brief Calculates how many lines can be merged into 1 starting at \p I.
493 unsigned
494 tryFitMultipleLinesInOne(unsigned Indent,
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000495 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
Daniel Jasper56f8b432013-11-06 23:12:09 +0000496 SmallVectorImpl<AnnotatedLine *>::const_iterator E) {
497 // We can never merge stuff if there are trailing line comments.
Daniel Jasper234379f2013-12-24 13:31:25 +0000498 const AnnotatedLine *TheLine = *I;
Daniel Jasper56f8b432013-11-06 23:12:09 +0000499 if (TheLine->Last->Type == TT_LineComment)
500 return 0;
501
Alexander Kornienkoecc232d2013-12-04 13:25:26 +0000502 if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
503 return 0;
504
Daniel Jasper98fb6e12013-11-08 17:33:27 +0000505 unsigned Limit =
506 Style.ColumnLimit == 0 ? UINT_MAX : Style.ColumnLimit - Indent;
Daniel Jasper56f8b432013-11-06 23:12:09 +0000507 // If we already exceed the column limit, we set 'Limit' to 0. The different
508 // tryMerge..() functions can then decide whether to still do merging.
509 Limit = TheLine->Last->TotalLength > Limit
510 ? 0
511 : Limit - TheLine->Last->TotalLength;
512
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000513 if (I + 1 == E || I[1]->Type == LT_Invalid)
Daniel Jasper56f8b432013-11-06 23:12:09 +0000514 return 0;
515
Daniel Jasper234379f2013-12-24 13:31:25 +0000516 if (TheLine->Last->Type == TT_FunctionLBrace &&
517 TheLine->First != TheLine->Last) {
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000518 return Style.AllowShortFunctionsOnASingleLine
519 ? tryMergeSimpleBlock(I, E, Limit)
520 : 0;
521 }
Daniel Jasper56f8b432013-11-06 23:12:09 +0000522 if (TheLine->Last->is(tok::l_brace)) {
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000523 return Style.BreakBeforeBraces == FormatStyle::BS_Attach
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000524 ? tryMergeSimpleBlock(I, E, Limit)
525 : 0;
526 }
527 if (I[1]->First->Type == TT_FunctionLBrace &&
528 Style.BreakBeforeBraces != FormatStyle::BS_Attach) {
Alp Tokerba5b4dc2013-12-30 02:06:29 +0000529 // Check for Limit <= 2 to account for the " {".
Daniel Jasper234379f2013-12-24 13:31:25 +0000530 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
531 return 0;
532 Limit -= 2;
533
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000534 unsigned MergedLines = 0;
535 if (Style.AllowShortFunctionsOnASingleLine) {
536 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
537 // If we managed to merge the block, count the function header, which is
538 // on a separate line.
539 if (MergedLines > 0)
540 ++MergedLines;
541 }
542 return MergedLines;
543 }
544 if (TheLine->First->is(tok::kw_if)) {
545 return Style.AllowShortIfStatementsOnASingleLine
546 ? tryMergeSimpleControlStatement(I, E, Limit)
547 : 0;
548 }
549 if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) {
550 return Style.AllowShortLoopsOnASingleLine
551 ? tryMergeSimpleControlStatement(I, E, Limit)
552 : 0;
553 }
554 if (TheLine->InPPDirective &&
555 (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {
Daniel Jasper56f8b432013-11-06 23:12:09 +0000556 return tryMergeSimplePPDirective(I, E, Limit);
557 }
558 return 0;
559 }
560
561private:
562 unsigned
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000563 tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
Daniel Jasper56f8b432013-11-06 23:12:09 +0000564 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
565 unsigned Limit) {
566 if (Limit == 0)
567 return 0;
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000568 if (!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline)
Daniel Jasper56f8b432013-11-06 23:12:09 +0000569 return 0;
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000570 if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline)
Daniel Jasper56f8b432013-11-06 23:12:09 +0000571 return 0;
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000572 if (1 + I[1]->Last->TotalLength > Limit)
Daniel Jasper56f8b432013-11-06 23:12:09 +0000573 return 0;
574 return 1;
575 }
576
577 unsigned tryMergeSimpleControlStatement(
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000578 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
Daniel Jasper56f8b432013-11-06 23:12:09 +0000579 SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) {
580 if (Limit == 0)
581 return 0;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000582 if ((Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
583 Style.BreakBeforeBraces == FormatStyle::BS_GNU) &&
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000584 I[1]->First->is(tok::l_brace))
Daniel Jasper56f8b432013-11-06 23:12:09 +0000585 return 0;
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000586 if (I[1]->InPPDirective != (*I)->InPPDirective ||
587 (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline))
Daniel Jasper56f8b432013-11-06 23:12:09 +0000588 return 0;
Daniel Jaspera0407742014-02-11 10:08:11 +0000589 Limit = limitConsideringMacros(I + 1, E, Limit);
Daniel Jasper56f8b432013-11-06 23:12:09 +0000590 AnnotatedLine &Line = **I;
591 if (Line.Last->isNot(tok::r_paren))
592 return 0;
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000593 if (1 + I[1]->Last->TotalLength > Limit)
Daniel Jasper56f8b432013-11-06 23:12:09 +0000594 return 0;
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000595 if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for,
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000596 tok::kw_while) ||
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000597 I[1]->First->Type == TT_LineComment)
Daniel Jasper56f8b432013-11-06 23:12:09 +0000598 return 0;
599 // Only inline simple if's (no nested if or else).
600 if (I + 2 != E && Line.First->is(tok::kw_if) &&
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000601 I[2]->First->is(tok::kw_else))
Daniel Jasper56f8b432013-11-06 23:12:09 +0000602 return 0;
603 return 1;
604 }
605
606 unsigned
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000607 tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
Daniel Jasper56f8b432013-11-06 23:12:09 +0000608 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
609 unsigned Limit) {
Daniel Jasper56f8b432013-11-06 23:12:09 +0000610 // First, check that the current line allows merging. This is the case if
611 // we're not in a control flow statement and the last token is an opening
612 // brace.
613 AnnotatedLine &Line = **I;
614 if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
615 tok::kw_else, tok::kw_try, tok::kw_catch,
616 tok::kw_for,
617 // This gets rid of all ObjC @ keywords and methods.
618 tok::at, tok::minus, tok::plus))
619 return 0;
620
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000621 FormatToken *Tok = I[1]->First;
Daniel Jasper56f8b432013-11-06 23:12:09 +0000622 if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
623 (Tok->getNextNonComment() == NULL ||
624 Tok->getNextNonComment()->is(tok::semi))) {
625 // We merge empty blocks even if the line exceeds the column limit.
626 Tok->SpacesRequiredBefore = 0;
627 Tok->CanBreakBefore = true;
628 return 1;
629 } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) {
630 // Check that we still have three lines and they fit into the limit.
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000631 if (I + 2 == E || I[2]->Type == LT_Invalid)
Daniel Jasper56f8b432013-11-06 23:12:09 +0000632 return 0;
Daniel Jaspera0407742014-02-11 10:08:11 +0000633 Limit = limitConsideringMacros(I + 2, E, Limit);
Daniel Jasper56f8b432013-11-06 23:12:09 +0000634
635 if (!nextTwoLinesFitInto(I, Limit))
636 return 0;
637
638 // Second, check that the next line does not contain any braces - if it
639 // does, readability declines when putting it into a single line.
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000640 if (I[1]->Last->Type == TT_LineComment || Tok->MustBreakBefore)
Daniel Jasper56f8b432013-11-06 23:12:09 +0000641 return 0;
642 do {
643 if (Tok->isOneOf(tok::l_brace, tok::r_brace))
644 return 0;
645 Tok = Tok->Next;
646 } while (Tok != NULL);
647
648 // Last, check that the third line contains a single closing brace.
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000649 Tok = I[2]->First;
Daniel Jasper56f8b432013-11-06 23:12:09 +0000650 if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) ||
651 Tok->MustBreakBefore)
652 return 0;
653
654 return 2;
655 }
656 return 0;
657 }
658
Daniel Jasper64989962014-02-07 13:45:27 +0000659 /// Returns the modified column limit for \p I if it is inside a macro and
660 /// needs a trailing '\'.
661 unsigned
Daniel Jaspera0407742014-02-11 10:08:11 +0000662 limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
Daniel Jasper64989962014-02-07 13:45:27 +0000663 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
664 unsigned Limit) {
665 if (I[0]->InPPDirective && I + 1 != E &&
666 !I[1]->First->HasUnescapedNewline && !I[1]->First->is(tok::eof)) {
667 return Limit < 2 ? 0 : Limit - 2;
668 }
669 return Limit;
670 }
671
Daniel Jasper56f8b432013-11-06 23:12:09 +0000672 bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
673 unsigned Limit) {
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000674 return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
Daniel Jasper56f8b432013-11-06 23:12:09 +0000675 }
676
Daniel Jasper234379f2013-12-24 13:31:25 +0000677 bool containsMustBreak(const AnnotatedLine *Line) {
678 for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
679 if (Tok->MustBreakBefore)
680 return true;
681 }
682 return false;
683 }
684
Daniel Jasper56f8b432013-11-06 23:12:09 +0000685 const FormatStyle &Style;
686};
687
Daniel Jasperf7935112012-12-03 18:12:45 +0000688class UnwrappedLineFormatter {
689public:
Daniel Jasper5500f612013-11-25 11:08:59 +0000690 UnwrappedLineFormatter(ContinuationIndenter *Indenter,
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000691 WhitespaceManager *Whitespaces,
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000692 const FormatStyle &Style)
Daniel Jasper5500f612013-11-25 11:08:59 +0000693 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
694 Joiner(Style) {}
Daniel Jasperf7935112012-12-03 18:12:45 +0000695
Daniel Jasper56f8b432013-11-06 23:12:09 +0000696 unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun,
Daniel Jasper9c199562013-11-28 15:58:55 +0000697 int AdditionalIndent = 0, bool FixBadIndentation = false) {
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000698 assert(!Lines.empty());
699 unsigned Penalty = 0;
700 std::vector<int> IndentForLevel;
701 for (unsigned i = 0, e = Lines[0]->Level; i != e; ++i)
702 IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent);
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000703 const AnnotatedLine *PreviousLine = NULL;
Daniel Jasper56f8b432013-11-06 23:12:09 +0000704 for (SmallVectorImpl<AnnotatedLine *>::const_iterator I = Lines.begin(),
705 E = Lines.end();
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000706 I != E; ++I) {
707 const AnnotatedLine &TheLine = **I;
708 const FormatToken *FirstTok = TheLine.First;
709 int Offset = getIndentOffset(*FirstTok);
710
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000711 // Determine indent and try to merge multiple unwrapped lines.
Daniel Jasperbad63ae2013-12-17 12:38:55 +0000712 unsigned Indent;
713 if (TheLine.InPPDirective) {
714 Indent = TheLine.Level * Style.IndentWidth;
715 } else {
716 while (IndentForLevel.size() <= TheLine.Level)
717 IndentForLevel.push_back(-1);
718 IndentForLevel.resize(TheLine.Level + 1);
719 Indent = getIndent(IndentForLevel, TheLine.Level);
720 }
721 unsigned LevelIndent = Indent;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000722 if (static_cast<int>(Indent) + Offset >= 0)
723 Indent += Offset;
Daniel Jasperbad63ae2013-12-17 12:38:55 +0000724
725 // Merge multiple lines if possible.
Daniel Jasper56f8b432013-11-06 23:12:09 +0000726 unsigned MergedLines = Joiner.tryFitMultipleLinesInOne(Indent, I, E);
Alexander Kornienko31e95542013-12-04 12:21:08 +0000727 if (MergedLines > 0 && Style.ColumnLimit == 0) {
728 // Disallow line merging if there is a break at the start of one of the
729 // input lines.
730 for (unsigned i = 0; i < MergedLines; ++i) {
731 if (I[i + 1]->First->NewlinesBefore > 0)
732 MergedLines = 0;
733 }
734 }
Daniel Jasper56f8b432013-11-06 23:12:09 +0000735 if (!DryRun) {
736 for (unsigned i = 0; i < MergedLines; ++i) {
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000737 join(*I[i], *I[i + 1]);
Daniel Jasper56f8b432013-11-06 23:12:09 +0000738 }
739 }
740 I += MergedLines;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000741
Daniel Jasper9c199562013-11-28 15:58:55 +0000742 bool FixIndentation =
743 FixBadIndentation && (LevelIndent != FirstTok->OriginalColumn);
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000744 if (TheLine.First->is(tok::eof)) {
Daniel Jasper5500f612013-11-25 11:08:59 +0000745 if (PreviousLine && PreviousLine->Affected && !DryRun) {
746 // Remove the file's trailing whitespace.
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000747 unsigned Newlines = std::min(FirstTok->NewlinesBefore, 1u);
748 Whitespaces->replaceWhitespace(*TheLine.First, Newlines,
749 /*IndentLevel=*/0, /*Spaces=*/0,
750 /*TargetColumn=*/0);
751 }
Daniel Jasper9c199562013-11-28 15:58:55 +0000752 } else if (TheLine.Type != LT_Invalid &&
753 (TheLine.Affected || FixIndentation)) {
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000754 if (FirstTok->WhitespaceRange.isValid()) {
755 if (!DryRun)
756 formatFirstToken(*TheLine.First, PreviousLine, TheLine.Level,
757 Indent, TheLine.InPPDirective);
758 } else {
759 Indent = LevelIndent = FirstTok->OriginalColumn;
760 }
761
762 // If everything fits on a single line, just put it there.
763 unsigned ColumnLimit = Style.ColumnLimit;
764 if (I + 1 != E) {
Alexander Kornienkoc3021612013-11-19 14:30:44 +0000765 AnnotatedLine *NextLine = I[1];
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000766 if (NextLine->InPPDirective && !NextLine->First->HasUnescapedNewline)
767 ColumnLimit = getColumnLimit(TheLine.InPPDirective);
768 }
769
770 if (TheLine.Last->TotalLength + Indent <= ColumnLimit) {
771 LineState State = Indenter->getInitialState(Indent, &TheLine, DryRun);
772 while (State.NextToken != NULL)
773 Indenter->addTokenToState(State, /*Newline=*/false, DryRun);
774 } else if (Style.ColumnLimit == 0) {
Daniel Jasper9c199562013-11-28 15:58:55 +0000775 // FIXME: Implement nested blocks for ColumnLimit = 0.
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000776 NoColumnLimitFormatter Formatter(Indenter);
777 if (!DryRun)
Alexander Kornienkoa594ba82013-12-16 14:35:51 +0000778 Formatter.format(Indent, &TheLine);
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000779 } else {
780 Penalty += format(TheLine, Indent, DryRun);
781 }
782
Daniel Jasperbad63ae2013-12-17 12:38:55 +0000783 if (!TheLine.InPPDirective)
784 IndentForLevel[TheLine.Level] = LevelIndent;
Daniel Jasper9c199562013-11-28 15:58:55 +0000785 } else if (TheLine.ChildrenAffected) {
786 format(TheLine.Children, DryRun);
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000787 } else {
788 // Format the first token if necessary, and notify the WhitespaceManager
789 // about the unchanged whitespace.
790 for (FormatToken *Tok = TheLine.First; Tok != NULL; Tok = Tok->Next) {
791 if (Tok == TheLine.First &&
792 (Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
793 unsigned LevelIndent = Tok->OriginalColumn;
794 if (!DryRun) {
Daniel Jasper9c199562013-11-28 15:58:55 +0000795 // Remove trailing whitespace of the previous line.
Daniel Jasper5500f612013-11-25 11:08:59 +0000796 if ((PreviousLine && PreviousLine->Affected) ||
797 TheLine.LeadingEmptyLinesAffected) {
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000798 formatFirstToken(*Tok, PreviousLine, TheLine.Level, LevelIndent,
799 TheLine.InPPDirective);
800 } else {
801 Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
802 }
803 }
804
805 if (static_cast<int>(LevelIndent) - Offset >= 0)
806 LevelIndent -= Offset;
Daniel Jasperbad63ae2013-12-17 12:38:55 +0000807 if (Tok->isNot(tok::comment) && !TheLine.InPPDirective)
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000808 IndentForLevel[TheLine.Level] = LevelIndent;
809 } else if (!DryRun) {
810 Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
811 }
812 }
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000813 }
814 if (!DryRun) {
815 for (FormatToken *Tok = TheLine.First; Tok != NULL; Tok = Tok->Next) {
816 Tok->Finalized = true;
817 }
818 }
819 PreviousLine = *I;
820 }
821 return Penalty;
822 }
823
824private:
825 /// \brief Formats an \c AnnotatedLine and returns the penalty.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000826 ///
827 /// If \p DryRun is \c false, directly applies the changes.
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000828 unsigned format(const AnnotatedLine &Line, unsigned FirstIndent,
829 bool DryRun) {
Daniel Jasper1c5d9df2013-09-06 07:54:20 +0000830 LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun);
Daniel Jasper4b866272013-02-01 11:00:45 +0000831
Daniel Jasperacc33662013-02-08 08:22:00 +0000832 // If the ObjC method declaration does not fit on a line, we should format
833 // it with one arg per line.
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000834 if (State.Line->Type == LT_ObjCMethodDecl)
Daniel Jasperacc33662013-02-08 08:22:00 +0000835 State.Stack.back().BreakBeforeParameter = true;
836
Daniel Jasper4b866272013-02-01 11:00:45 +0000837 // Find best solution in solution space.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000838 return analyzeSolutionSpace(State, DryRun);
Daniel Jasperf7935112012-12-03 18:12:45 +0000839 }
840
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000841 /// \brief An edge in the solution space from \c Previous->State to \c State,
842 /// inserting a newline dependent on the \c NewLine.
843 struct StateNode {
844 StateNode(const LineState &State, bool NewLine, StateNode *Previous)
Daniel Jasper12ef4e52013-02-21 21:33:55 +0000845 : State(State), NewLine(NewLine), Previous(Previous) {}
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000846 LineState State;
847 bool NewLine;
848 StateNode *Previous;
849 };
Daniel Jasper4b866272013-02-01 11:00:45 +0000850
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000851 /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
852 ///
853 /// In case of equal penalties, we want to prefer states that were inserted
854 /// first. During state generation we make sure that we insert states first
855 /// that break the line as late as possible.
856 typedef std::pair<unsigned, unsigned> OrderedPenalty;
857
858 /// \brief An item in the prioritized BFS search queue. The \c StateNode's
859 /// \c State has the given \c OrderedPenalty.
860 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
861
862 /// \brief The BFS queue type.
863 typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
864 std::greater<QueueItem> > QueueType;
Daniel Jasper4b866272013-02-01 11:00:45 +0000865
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000866 /// \brief Get the offset of the line relatively to the level.
867 ///
868 /// For example, 'public:' labels in classes are offset by 1 or 2
869 /// characters to the left from their level.
870 int getIndentOffset(const FormatToken &RootToken) {
871 if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
872 return Style.AccessModifierOffset;
873 return 0;
874 }
875
876 /// \brief Add a new line and the required indent before the first Token
877 /// of the \c UnwrappedLine if there was no structural parsing error.
878 void formatFirstToken(FormatToken &RootToken,
879 const AnnotatedLine *PreviousLine, unsigned IndentLevel,
880 unsigned Indent, bool InPPDirective) {
881 unsigned Newlines =
882 std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
883 // Remove empty lines before "}" where applicable.
884 if (RootToken.is(tok::r_brace) &&
885 (!RootToken.Next ||
886 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
887 Newlines = std::min(Newlines, 1u);
888 if (Newlines == 0 && !RootToken.IsFirst)
889 Newlines = 1;
890
Daniel Jasper11164bd2014-03-21 12:58:53 +0000891 // Remove empty lines after "{".
Daniel Jasper01b35482014-03-21 13:03:33 +0000892 if (PreviousLine && PreviousLine->Last->is(tok::l_brace) &&
893 PreviousLine->First->isNot(tok::kw_namespace))
Daniel Jasper11164bd2014-03-21 12:58:53 +0000894 Newlines = 1;
895
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000896 // Insert extra new line before access specifiers.
897 if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) &&
898 RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
899 ++Newlines;
900
901 // Remove empty lines after access specifiers.
902 if (PreviousLine && PreviousLine->First->isAccessSpecifier())
903 Newlines = std::min(1u, Newlines);
904
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000905 Whitespaces->replaceWhitespace(RootToken, Newlines, IndentLevel, Indent,
906 Indent, InPPDirective &&
907 !RootToken.HasUnescapedNewline);
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000908 }
909
910 /// \brief Get the indent of \p Level from \p IndentForLevel.
911 ///
912 /// \p IndentForLevel must contain the indent for the level \c l
913 /// at \p IndentForLevel[l], or a value < 0 if the indent for
914 /// that level is unknown.
915 unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
916 if (IndentForLevel[Level] != -1)
917 return IndentForLevel[Level];
918 if (Level == 0)
919 return 0;
920 return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
921 }
922
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000923 void join(AnnotatedLine &A, const AnnotatedLine &B) {
924 assert(!A.Last->Next);
925 assert(!B.First->Previous);
Daniel Jasper5500f612013-11-25 11:08:59 +0000926 if (B.Affected)
927 A.Affected = true;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000928 A.Last->Next = B.First;
929 B.First->Previous = A.Last;
Daniel Jasper98fb6e12013-11-08 17:33:27 +0000930 B.First->CanBreakBefore = true;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000931 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
932 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
933 Tok->TotalLength += LengthA;
934 A.Last = Tok;
935 }
936 }
937
938 unsigned getColumnLimit(bool InPPDirective) const {
939 // In preprocessor directives reserve two chars for trailing " \"
940 return Style.ColumnLimit - (InPPDirective ? 2 : 0);
941 }
942
Daniel Jasper4b866272013-02-01 11:00:45 +0000943 /// \brief Analyze the entire solution space starting from \p InitialState.
Daniel Jasperf7935112012-12-03 18:12:45 +0000944 ///
Daniel Jasper4b866272013-02-01 11:00:45 +0000945 /// This implements a variant of Dijkstra's algorithm on the graph that spans
946 /// the solution space (\c LineStates are the nodes). The algorithm tries to
947 /// find the shortest path (the one with lowest penalty) from \p InitialState
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000948 /// to a state where all tokens are placed. Returns the penalty.
949 ///
950 /// If \p DryRun is \c false, directly applies the changes.
951 unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun = false) {
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000952 std::set<LineState> Seen;
953
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000954 // Increasing count of \c StateNode items we have created. This is used to
955 // create a deterministic order independent of the container.
956 unsigned Count = 0;
957 QueueType Queue;
958
Daniel Jasper4b866272013-02-01 11:00:45 +0000959 // Insert start element into queue.
Daniel Jasper687af3b2013-02-14 14:26:07 +0000960 StateNode *Node =
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000961 new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
962 Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
963 ++Count;
Daniel Jasper4b866272013-02-01 11:00:45 +0000964
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000965 unsigned Penalty = 0;
966
Daniel Jasper4b866272013-02-01 11:00:45 +0000967 // While not empty, take first element and follow edges.
968 while (!Queue.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000969 Penalty = Queue.top().first.first;
Daniel Jasper687af3b2013-02-14 14:26:07 +0000970 StateNode *Node = Queue.top().second;
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000971 if (Node->State.NextToken == NULL) {
Alexander Kornienko49149672013-05-10 11:56:10 +0000972 DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
Daniel Jasper4b866272013-02-01 11:00:45 +0000973 break;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000974 }
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000975 Queue.pop();
Daniel Jasper4b866272013-02-01 11:00:45 +0000976
Daniel Jasperf8114cf2013-05-22 05:27:42 +0000977 // Cut off the analysis of certain solutions if the analysis gets too
978 // complex. See description of IgnoreStackForComparison.
979 if (Count > 10000)
980 Node->State.IgnoreStackForComparison = true;
981
Manuel Klimek2ef908e2013-02-13 10:46:36 +0000982 if (!Seen.insert(Node->State).second)
983 // State already examined with lower penalty.
984 continue;
Daniel Jasper4b866272013-02-01 11:00:45 +0000985
Manuel Klimek71814b42013-10-11 21:25:45 +0000986 FormatDecision LastFormat = Node->State.NextToken->Decision;
987 if (LastFormat == FD_Unformatted || LastFormat == FD_Continue)
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000988 addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue);
Manuel Klimek71814b42013-10-11 21:25:45 +0000989 if (LastFormat == FD_Unformatted || LastFormat == FD_Break)
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +0000990 addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue);
Daniel Jasper4b866272013-02-01 11:00:45 +0000991 }
992
Manuel Klimek71814b42013-10-11 21:25:45 +0000993 if (Queue.empty()) {
Daniel Jasper4b866272013-02-01 11:00:45 +0000994 // We were unable to find a solution, do nothing.
995 // FIXME: Add diagnostic?
Manuel Klimek71814b42013-10-11 21:25:45 +0000996 DEBUG(llvm::dbgs() << "Could not find a solution.\n");
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000997 return 0;
Manuel Klimek71814b42013-10-11 21:25:45 +0000998 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000999
Daniel Jasper4b866272013-02-01 11:00:45 +00001000 // Reconstruct the solution.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001001 if (!DryRun)
1002 reconstructPath(InitialState, Queue.top().second);
1003
Alexander Kornienko49149672013-05-10 11:56:10 +00001004 DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
1005 DEBUG(llvm::dbgs() << "---\n");
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001006
1007 return Penalty;
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001008 }
1009
1010 void reconstructPath(LineState &State, StateNode *Current) {
Manuel Klimek4c5c28b2013-05-29 15:10:11 +00001011 std::deque<StateNode *> Path;
1012 // We do not need a break before the initial token.
1013 while (Current->Previous) {
1014 Path.push_front(Current);
1015 Current = Current->Previous;
1016 }
1017 for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
1018 I != E; ++I) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001019 unsigned Penalty = 0;
1020 formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty);
1021 Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false);
1022
Manuel Klimek4c5c28b2013-05-29 15:10:11 +00001023 DEBUG({
1024 if ((*I)->NewLine) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +00001025 llvm::dbgs() << "Penalty for placing "
Manuel Klimek4c5c28b2013-05-29 15:10:11 +00001026 << (*I)->Previous->State.NextToken->Tok.getName() << ": "
Daniel Jasper8de9ed02013-08-22 15:00:41 +00001027 << Penalty << "\n";
Manuel Klimek4c5c28b2013-05-29 15:10:11 +00001028 }
1029 });
Manuel Klimek4c5c28b2013-05-29 15:10:11 +00001030 }
Daniel Jasper4b866272013-02-01 11:00:45 +00001031 }
1032
Manuel Klimekaf491072013-02-13 10:54:19 +00001033 /// \brief Add the following state to the analysis queue \c Queue.
Daniel Jasper4b866272013-02-01 11:00:45 +00001034 ///
Manuel Klimekaf491072013-02-13 10:54:19 +00001035 /// Assume the current state is \p PreviousNode and has been reached with a
Daniel Jasper4b866272013-02-01 11:00:45 +00001036 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
Manuel Klimekaf491072013-02-13 10:54:19 +00001037 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001038 bool NewLine, unsigned *Count, QueueType *Queue) {
Daniel Jasperde0328a2013-08-16 11:20:30 +00001039 if (NewLine && !Indenter->canBreak(PreviousNode->State))
Daniel Jasper4b866272013-02-01 11:00:45 +00001040 return;
Daniel Jasperde0328a2013-08-16 11:20:30 +00001041 if (!NewLine && Indenter->mustBreak(PreviousNode->State))
Daniel Jasper4b866272013-02-01 11:00:45 +00001042 return;
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001043
1044 StateNode *Node = new (Allocator.Allocate())
1045 StateNode(PreviousNode->State, NewLine, PreviousNode);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001046 if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty))
1047 return;
1048
Daniel Jasperde0328a2013-08-16 11:20:30 +00001049 Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
Manuel Klimek2ef908e2013-02-13 10:46:36 +00001050
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001051 Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node));
1052 ++(*Count);
Daniel Jasper4b866272013-02-01 11:00:45 +00001053 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001054
Daniel Jasperf3a5d002013-09-05 10:48:50 +00001055 /// \brief If the \p State's next token is an r_brace closing a nested block,
1056 /// format the nested block before it.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001057 ///
1058 /// Returns \c true if all children could be placed successfully and adapts
1059 /// \p Penalty as well as \p State. If \p DryRun is false, also directly
1060 /// creates changes using \c Whitespaces.
1061 ///
1062 /// The crucial idea here is that children always get formatted upon
1063 /// encountering the closing brace right after the nested block. Now, if we
1064 /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is
1065 /// \c false), the entire block has to be kept on the same line (which is only
1066 /// possible if it fits on the line, only contains a single statement, etc.
1067 ///
1068 /// If \p NewLine is true, we format the nested block on separate lines, i.e.
1069 /// break after the "{", format all lines with correct indentation and the put
1070 /// the closing "}" on yet another new line.
1071 ///
1072 /// This enables us to keep the simple structure of the
1073 /// \c UnwrappedLineFormatter, where we only have two options for each token:
1074 /// break or don't break.
1075 bool formatChildren(LineState &State, bool NewLine, bool DryRun,
1076 unsigned &Penalty) {
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001077 FormatToken &Previous = *State.NextToken->Previous;
Daniel Jasperdcd5da12013-10-20 17:28:32 +00001078 const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
1079 if (!LBrace || LBrace->isNot(tok::l_brace) ||
1080 LBrace->BlockKind != BK_Block || Previous.Children.size() == 0)
Daniel Jasperf3a5d002013-09-05 10:48:50 +00001081 // The previous token does not open a block. Nothing to do. We don't
1082 // assert so that we can simply call this function for all tokens.
Daniel Jasper36c28ce2013-09-06 08:54:24 +00001083 return true;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001084
1085 if (NewLine) {
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001086 int AdditionalIndent = State.Stack.back().Indent -
1087 Previous.Children[0]->Level * Style.IndentWidth;
Daniel Jasper9c199562013-11-28 15:58:55 +00001088 Penalty += format(Previous.Children, DryRun, AdditionalIndent,
1089 /*FixBadIndentation=*/true);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001090 return true;
1091 }
1092
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001093 // Cannot merge multiple statements into a single line.
Daniel Jasperdcd5da12013-10-20 17:28:32 +00001094 if (Previous.Children.size() > 1)
Alexander Kornienko3cfa9732013-11-20 16:33:05 +00001095 return false;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001096
1097 // We can't put the closing "}" on a line with a trailing comment.
Daniel Jasperdcd5da12013-10-20 17:28:32 +00001098 if (Previous.Children[0]->Last->isTrailingComment())
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001099 return false;
1100
1101 if (!DryRun) {
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +00001102 Whitespaces->replaceWhitespace(
Daniel Jasperdcd5da12013-10-20 17:28:32 +00001103 *Previous.Children[0]->First,
Alexander Kornienkoe2e03872013-10-14 00:46:35 +00001104 /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1,
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +00001105 /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001106 }
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001107 Penalty += format(*Previous.Children[0], State.Column + 1, DryRun);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001108
Daniel Jasperdcd5da12013-10-20 17:28:32 +00001109 State.Column += 1 + Previous.Children[0]->Last->TotalLength;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001110 return true;
1111 }
1112
Daniel Jasperde0328a2013-08-16 11:20:30 +00001113 ContinuationIndenter *Indenter;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001114 WhitespaceManager *Whitespaces;
Daniel Jasperf7935112012-12-03 18:12:45 +00001115 FormatStyle Style;
Daniel Jasper56f8b432013-11-06 23:12:09 +00001116 LineJoiner Joiner;
Manuel Klimekaf491072013-02-13 10:54:19 +00001117
1118 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
Daniel Jasperf7935112012-12-03 18:12:45 +00001119};
1120
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001121class FormatTokenLexer {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001122public:
Manuel Klimek31c85922013-08-29 15:21:40 +00001123 FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr, FormatStyle &Style,
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001124 encoding::Encoding Encoding)
Alexander Kornienko393e3082013-11-13 14:04:17 +00001125 : FormatTok(NULL), IsFirstToken(true), GreaterStashed(false), Column(0),
Manuel Klimek31c85922013-08-29 15:21:40 +00001126 TrailingWhitespace(0), Lex(Lex), SourceMgr(SourceMgr), Style(Style),
1127 IdentTable(getFormattingLangOpts()), Encoding(Encoding) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001128 Lex.SetKeepWhitespaceMode(true);
1129 }
1130
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001131 ArrayRef<FormatToken *> lex() {
1132 assert(Tokens.empty());
1133 do {
1134 Tokens.push_back(getNextToken());
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001135 tryMergePreviousTokens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001136 } while (Tokens.back()->Tok.isNot(tok::eof));
1137 return Tokens;
1138 }
1139
1140 IdentifierTable &getIdentTable() { return IdentTable; }
1141
1142private:
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001143 void tryMergePreviousTokens() {
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001144 if (tryMerge_TMacro())
1145 return;
1146
1147 if (Style.Language == FormatStyle::LK_JavaScript) {
1148 static tok::TokenKind JSIdentity[] = { tok::equalequal, tok::equal };
1149 static tok::TokenKind JSNotIdentity[] = { tok::exclaimequal, tok::equal };
1150 static tok::TokenKind JSShiftEqual[] = { tok::greater, tok::greater,
1151 tok::greaterequal };
1152 // FIXME: We probably need to change token type to mimic operator with the
1153 // correct priority.
1154 if (tryMergeTokens(JSIdentity))
1155 return;
1156 if (tryMergeTokens(JSNotIdentity))
1157 return;
1158 if (tryMergeTokens(JSShiftEqual))
1159 return;
1160 }
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001161 }
1162
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001163 bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds) {
1164 if (Tokens.size() < Kinds.size())
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001165 return false;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001166
1167 SmallVectorImpl<FormatToken *>::const_iterator First =
1168 Tokens.end() - Kinds.size();
1169 if (!First[0]->is(Kinds[0]))
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001170 return false;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001171 unsigned AddLength = 0;
1172 for (unsigned i = 1; i < Kinds.size(); ++i) {
1173 if (!First[i]->is(Kinds[i]) || First[i]->WhitespaceRange.getBegin() !=
1174 First[i]->WhitespaceRange.getEnd())
1175 return false;
1176 AddLength += First[i]->TokenText.size();
1177 }
1178 Tokens.resize(Tokens.size() - Kinds.size() + 1);
1179 First[0]->TokenText = StringRef(First[0]->TokenText.data(),
1180 First[0]->TokenText.size() + AddLength);
1181 First[0]->ColumnWidth += AddLength;
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001182 return true;
1183 }
1184
1185 bool tryMerge_TMacro() {
Alexander Kornienko81e32942013-09-16 20:20:49 +00001186 if (Tokens.size() < 4)
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001187 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001188 FormatToken *Last = Tokens.back();
1189 if (!Last->is(tok::r_paren))
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001190 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001191
1192 FormatToken *String = Tokens[Tokens.size() - 2];
1193 if (!String->is(tok::string_literal) || String->IsMultiline)
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001194 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001195
1196 if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001197 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001198
1199 FormatToken *Macro = Tokens[Tokens.size() - 4];
1200 if (Macro->TokenText != "_T")
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001201 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001202
1203 const char *Start = Macro->TokenText.data();
1204 const char *End = Last->TokenText.data() + Last->TokenText.size();
1205 String->TokenText = StringRef(Start, End - Start);
1206 String->IsFirst = Macro->IsFirst;
1207 String->LastNewlineOffset = Macro->LastNewlineOffset;
1208 String->WhitespaceRange = Macro->WhitespaceRange;
1209 String->OriginalColumn = Macro->OriginalColumn;
1210 String->ColumnWidth = encoding::columnWidthWithTabs(
1211 String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding);
1212
1213 Tokens.pop_back();
1214 Tokens.pop_back();
1215 Tokens.pop_back();
1216 Tokens.back() = String;
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001217 return true;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001218 }
1219
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001220 FormatToken *getNextToken() {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001221 if (GreaterStashed) {
Manuel Klimek591ab5a2013-05-28 13:42:28 +00001222 // Create a synthesized second '>' token.
Manuel Klimek31c85922013-08-29 15:21:40 +00001223 // FIXME: Increment Column and set OriginalColumn.
Manuel Klimek591ab5a2013-05-28 13:42:28 +00001224 Token Greater = FormatTok->Tok;
1225 FormatTok = new (Allocator.Allocate()) FormatToken;
1226 FormatTok->Tok = Greater;
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001227 SourceLocation GreaterLocation =
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001228 FormatTok->Tok.getLocation().getLocWithOffset(1);
1229 FormatTok->WhitespaceRange =
1230 SourceRange(GreaterLocation, GreaterLocation);
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001231 FormatTok->TokenText = ">";
Alexander Kornienko39856b72013-09-10 09:38:25 +00001232 FormatTok->ColumnWidth = 1;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001233 GreaterStashed = false;
1234 return FormatTok;
1235 }
1236
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001237 FormatTok = new (Allocator.Allocate()) FormatToken;
Daniel Jasper8369aa52013-07-16 20:28:33 +00001238 readRawToken(*FormatTok);
Manuel Klimek9043c742013-05-27 15:23:34 +00001239 SourceLocation WhitespaceStart =
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001240 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
Alexander Kornienko393e3082013-11-13 14:04:17 +00001241 FormatTok->IsFirst = IsFirstToken;
1242 IsFirstToken = false;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001243
1244 // Consume and record whitespace until we find a significant token.
Manuel Klimek9043c742013-05-27 15:23:34 +00001245 unsigned WhitespaceLength = TrailingWhitespace;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001246 while (FormatTok->Tok.is(tok::unknown)) {
Manuel Klimek31c85922013-08-29 15:21:40 +00001247 for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) {
1248 switch (FormatTok->TokenText[i]) {
1249 case '\n':
1250 ++FormatTok->NewlinesBefore;
1251 // FIXME: This is technically incorrect, as it could also
1252 // be a literal backslash at the end of the line.
Alexander Kornienko9e649af2013-09-11 12:25:57 +00001253 if (i == 0 || (FormatTok->TokenText[i - 1] != '\\' &&
1254 (FormatTok->TokenText[i - 1] != '\r' || i == 1 ||
1255 FormatTok->TokenText[i - 2] != '\\')))
Manuel Klimek31c85922013-08-29 15:21:40 +00001256 FormatTok->HasUnescapedNewline = true;
1257 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
1258 Column = 0;
1259 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001260 case '\r':
1261 case '\f':
1262 case '\v':
1263 Column = 0;
1264 break;
Manuel Klimek31c85922013-08-29 15:21:40 +00001265 case ' ':
1266 ++Column;
1267 break;
1268 case '\t':
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +00001269 Column += Style.TabWidth - Column % Style.TabWidth;
Manuel Klimek31c85922013-08-29 15:21:40 +00001270 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001271 case '\\':
1272 ++Column;
1273 if (i + 1 == e || (FormatTok->TokenText[i + 1] != '\r' &&
1274 FormatTok->TokenText[i + 1] != '\n'))
1275 FormatTok->Type = TT_ImplicitStringLiteral;
1276 break;
Manuel Klimek31c85922013-08-29 15:21:40 +00001277 default:
Daniel Jasper877615c2013-10-11 19:45:02 +00001278 FormatTok->Type = TT_ImplicitStringLiteral;
Manuel Klimek31c85922013-08-29 15:21:40 +00001279 ++Column;
1280 break;
1281 }
1282 }
1283
Daniel Jasper877615c2013-10-11 19:45:02 +00001284 if (FormatTok->Type == TT_ImplicitStringLiteral)
1285 break;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001286 WhitespaceLength += FormatTok->Tok.getLength();
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001287
Daniel Jasper8369aa52013-07-16 20:28:33 +00001288 readRawToken(*FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001289 }
Manuel Klimekef920692013-01-07 07:56:50 +00001290
Manuel Klimek1abf7892013-01-04 23:34:14 +00001291 // In case the token starts with escaped newlines, we want to
1292 // take them into account as whitespace - this pattern is quite frequent
1293 // in macro definitions.
Manuel Klimek1abf7892013-01-04 23:34:14 +00001294 // FIXME: Add a more explicit test.
Daniel Jasper8369aa52013-07-16 20:28:33 +00001295 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
1296 FormatTok->TokenText[1] == '\n') {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001297 // FIXME: ++FormatTok->NewlinesBefore is missing...
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001298 WhitespaceLength += 2;
Manuel Klimek31c85922013-08-29 15:21:40 +00001299 Column = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +00001300 FormatTok->TokenText = FormatTok->TokenText.substr(2);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001301 }
Alexander Kornienko39856b72013-09-10 09:38:25 +00001302
1303 FormatTok->WhitespaceRange = SourceRange(
1304 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
1305
Manuel Klimek31c85922013-08-29 15:21:40 +00001306 FormatTok->OriginalColumn = Column;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001307
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001308 TrailingWhitespace = 0;
1309 if (FormatTok->Tok.is(tok::comment)) {
Manuel Klimek31c85922013-08-29 15:21:40 +00001310 // FIXME: Add the trimmed whitespace to Column.
Daniel Jasper8369aa52013-07-16 20:28:33 +00001311 StringRef UntrimmedText = FormatTok->TokenText;
Alexander Kornienko9ab4a772013-09-06 17:24:54 +00001312 FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
Daniel Jasper8369aa52013-07-16 20:28:33 +00001313 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001314 } else if (FormatTok->Tok.is(tok::raw_identifier)) {
Daniel Jasper8369aa52013-07-16 20:28:33 +00001315 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001316 FormatTok->Tok.setIdentifierInfo(&Info);
1317 FormatTok->Tok.setKind(Info.getTokenID());
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001318 } else if (FormatTok->Tok.is(tok::greatergreater)) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001319 FormatTok->Tok.setKind(tok::greater);
Daniel Jasper8369aa52013-07-16 20:28:33 +00001320 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001321 GreaterStashed = true;
1322 }
1323
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001324 // Now FormatTok is the next non-whitespace token.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001325
Alexander Kornienko39856b72013-09-10 09:38:25 +00001326 StringRef Text = FormatTok->TokenText;
1327 size_t FirstNewlinePos = Text.find('\n');
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001328 if (FirstNewlinePos == StringRef::npos) {
1329 // FIXME: ColumnWidth actually depends on the start column, we need to
1330 // take this into account when the token is moved.
1331 FormatTok->ColumnWidth =
1332 encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding);
1333 Column += FormatTok->ColumnWidth;
1334 } else {
Alexander Kornienko39856b72013-09-10 09:38:25 +00001335 FormatTok->IsMultiline = true;
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001336 // FIXME: ColumnWidth actually depends on the start column, we need to
1337 // take this into account when the token is moved.
1338 FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
1339 Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
1340
Alexander Kornienko39856b72013-09-10 09:38:25 +00001341 // The last line of the token always starts in column 0.
1342 // Thus, the length can be precomputed even in the presence of tabs.
1343 FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
1344 Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth,
1345 Encoding);
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001346 Column = FormatTok->LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +00001347 }
Alexander Kornienko39856b72013-09-10 09:38:25 +00001348
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001349 return FormatTok;
1350 }
1351
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001352 FormatToken *FormatTok;
Alexander Kornienko393e3082013-11-13 14:04:17 +00001353 bool IsFirstToken;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001354 bool GreaterStashed;
Manuel Klimek31c85922013-08-29 15:21:40 +00001355 unsigned Column;
Manuel Klimek9043c742013-05-27 15:23:34 +00001356 unsigned TrailingWhitespace;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001357 Lexer &Lex;
1358 SourceManager &SourceMgr;
Manuel Klimek31c85922013-08-29 15:21:40 +00001359 FormatStyle &Style;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001360 IdentifierTable IdentTable;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001361 encoding::Encoding Encoding;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001362 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
1363 SmallVector<FormatToken *, 16> Tokens;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001364
Daniel Jasper8369aa52013-07-16 20:28:33 +00001365 void readRawToken(FormatToken &Tok) {
1366 Lex.LexFromRawLexer(Tok.Tok);
1367 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
1368 Tok.Tok.getLength());
Daniel Jasper8369aa52013-07-16 20:28:33 +00001369 // For formatting, treat unterminated string literals like normal string
1370 // literals.
Daniel Jasper86fee2f2014-01-31 12:49:42 +00001371 if (Tok.is(tok::unknown)) {
1372 if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') {
1373 Tok.Tok.setKind(tok::string_literal);
1374 Tok.IsUnterminatedLiteral = true;
1375 } else if (Style.Language == FormatStyle::LK_JavaScript &&
1376 Tok.TokenText == "''") {
1377 Tok.Tok.setKind(tok::char_constant);
1378 }
Daniel Jasper8369aa52013-07-16 20:28:33 +00001379 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001380 }
1381};
1382
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001383static StringRef getLanguageName(FormatStyle::LanguageKind Language) {
1384 switch (Language) {
1385 case FormatStyle::LK_Cpp:
1386 return "C++";
1387 case FormatStyle::LK_JavaScript:
1388 return "JavaScript";
Daniel Jasper7052ce62014-01-19 09:04:08 +00001389 case FormatStyle::LK_Proto:
1390 return "Proto";
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001391 default:
1392 return "Unknown";
1393 }
1394}
1395
Daniel Jasperf7935112012-12-03 18:12:45 +00001396class Formatter : public UnwrappedLineConsumer {
1397public:
Daniel Jasperd2ae41a2013-05-15 08:14:19 +00001398 Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
Daniel Jasperf7935112012-12-03 18:12:45 +00001399 const std::vector<CharSourceRange> &Ranges)
Daniel Jasperd2ae41a2013-05-15 08:14:19 +00001400 : Style(Style), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienko9e649af2013-09-11 12:25:57 +00001401 Whitespaces(SourceMgr, Style, inputUsesCRLF(Lex.getBuffer())),
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001402 Ranges(Ranges.begin(), Ranges.end()), UnwrappedLines(1),
Manuel Klimek71814b42013-10-11 21:25:45 +00001403 Encoding(encoding::detectEncoding(Lex.getBuffer())) {
Daniel Jasperfa21c072013-07-15 14:33:14 +00001404 DEBUG(llvm::dbgs() << "File encoding: "
1405 << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
1406 : "unknown")
1407 << "\n");
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001408 DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language)
1409 << "\n");
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001410 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001411
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001412 tooling::Replacements format() {
Manuel Klimek71814b42013-10-11 21:25:45 +00001413 tooling::Replacements Result;
Manuel Klimek31c85922013-08-29 15:21:40 +00001414 FormatTokenLexer Tokens(Lex, SourceMgr, Style, Encoding);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001415
1416 UnwrappedLineParser Parser(Style, Tokens.lex(), *this);
Manuel Klimek1a18c402013-04-12 14:13:36 +00001417 bool StructuralError = Parser.parse();
Manuel Klimek71814b42013-10-11 21:25:45 +00001418 assert(UnwrappedLines.rbegin()->empty());
1419 for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE;
1420 ++Run) {
1421 DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
1422 SmallVector<AnnotatedLine *, 16> AnnotatedLines;
1423 for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) {
1424 AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i]));
1425 }
1426 tooling::Replacements RunResult =
1427 format(AnnotatedLines, StructuralError, Tokens);
1428 DEBUG({
1429 llvm::dbgs() << "Replacements for run " << Run << ":\n";
1430 for (tooling::Replacements::iterator I = RunResult.begin(),
1431 E = RunResult.end();
1432 I != E; ++I) {
1433 llvm::dbgs() << I->toString() << "\n";
1434 }
1435 });
1436 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1437 delete AnnotatedLines[i];
1438 }
1439 Result.insert(RunResult.begin(), RunResult.end());
1440 Whitespaces.reset();
1441 }
1442 return Result;
1443 }
1444
1445 tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
1446 bool StructuralError, FormatTokenLexer &Tokens) {
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001447 TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in"));
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001448 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001449 Annotator.annotate(*AnnotatedLines[i]);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001450 }
Manuel Klimek71814b42013-10-11 21:25:45 +00001451 deriveLocalStyle(AnnotatedLines);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001452 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001453 Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001454 }
Daniel Jasper5500f612013-11-25 11:08:59 +00001455 computeAffectedLines(AnnotatedLines.begin(), AnnotatedLines.end());
Daniel Jasperb67cc422013-04-09 17:46:55 +00001456
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001457 Annotator.setCommentLineLevels(AnnotatedLines);
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001458 ContinuationIndenter Indenter(Style, SourceMgr, Whitespaces, Encoding,
1459 BinPackInconclusiveFunctions);
Daniel Jasper5500f612013-11-25 11:08:59 +00001460 UnwrappedLineFormatter Formatter(&Indenter, &Whitespaces, Style);
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001461 Formatter.format(AnnotatedLines, /*DryRun=*/false);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001462 return Whitespaces.generateReplacements();
1463 }
1464
1465private:
Daniel Jasper5500f612013-11-25 11:08:59 +00001466 // Determines which lines are affected by the SourceRanges given as input.
Daniel Jasper9c199562013-11-28 15:58:55 +00001467 // Returns \c true if at least one line between I and E or one of their
1468 // children is affected.
Daniel Jasper5500f612013-11-25 11:08:59 +00001469 bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I,
1470 SmallVectorImpl<AnnotatedLine *>::iterator E) {
1471 bool SomeLineAffected = false;
Daniel Jasper38c82402013-11-29 09:27:43 +00001472 const AnnotatedLine *PreviousLine = NULL;
Daniel Jasper5500f612013-11-25 11:08:59 +00001473 while (I != E) {
1474 AnnotatedLine *Line = *I;
1475 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
1476
1477 // If a line is part of a preprocessor directive, it needs to be formatted
1478 // if any token within the directive is affected.
1479 if (Line->InPPDirective) {
1480 FormatToken *Last = Line->Last;
1481 SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1;
1482 while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) {
1483 Last = (*PPEnd)->Last;
1484 ++PPEnd;
1485 }
1486
1487 if (affectsTokenRange(*Line->First, *Last,
1488 /*IncludeLeadingNewlines=*/false)) {
1489 SomeLineAffected = true;
1490 markAllAsAffected(I, PPEnd);
1491 }
1492 I = PPEnd;
1493 continue;
1494 }
1495
Daniel Jasper38c82402013-11-29 09:27:43 +00001496 if (nonPPLineAffected(Line, PreviousLine))
Daniel Jasper5500f612013-11-25 11:08:59 +00001497 SomeLineAffected = true;
Daniel Jasper5500f612013-11-25 11:08:59 +00001498
Daniel Jasper38c82402013-11-29 09:27:43 +00001499 PreviousLine = Line;
Daniel Jasper5500f612013-11-25 11:08:59 +00001500 ++I;
1501 }
1502 return SomeLineAffected;
1503 }
1504
Daniel Jasper9c199562013-11-28 15:58:55 +00001505 // Determines whether 'Line' is affected by the SourceRanges given as input.
1506 // Returns \c true if line or one if its children is affected.
Daniel Jasper38c82402013-11-29 09:27:43 +00001507 bool nonPPLineAffected(AnnotatedLine *Line,
1508 const AnnotatedLine *PreviousLine) {
Daniel Jasper9c199562013-11-28 15:58:55 +00001509 bool SomeLineAffected = false;
1510 Line->ChildrenAffected =
1511 computeAffectedLines(Line->Children.begin(), Line->Children.end());
1512 if (Line->ChildrenAffected)
1513 SomeLineAffected = true;
1514
1515 // Stores whether one of the line's tokens is directly affected.
1516 bool SomeTokenAffected = false;
1517 // Stores whether we need to look at the leading newlines of the next token
1518 // in order to determine whether it was affected.
1519 bool IncludeLeadingNewlines = false;
1520
1521 // Stores whether the first child line of any of this line's tokens is
1522 // affected.
1523 bool SomeFirstChildAffected = false;
1524
1525 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
1526 // Determine whether 'Tok' was affected.
1527 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
1528 SomeTokenAffected = true;
1529
1530 // Determine whether the first child of 'Tok' was affected.
1531 if (!Tok->Children.empty() && Tok->Children.front()->Affected)
1532 SomeFirstChildAffected = true;
1533
1534 IncludeLeadingNewlines = Tok->Children.empty();
1535 }
1536
1537 // Was this line moved, i.e. has it previously been on the same line as an
1538 // affected line?
Daniel Jasper38c82402013-11-29 09:27:43 +00001539 bool LineMoved = PreviousLine && PreviousLine->Affected &&
1540 Line->First->NewlinesBefore == 0;
Daniel Jasper9c199562013-11-28 15:58:55 +00001541
Daniel Jasper38c82402013-11-29 09:27:43 +00001542 bool IsContinuedComment = Line->First->is(tok::comment) &&
1543 Line->First->Next == NULL &&
1544 Line->First->NewlinesBefore < 2 && PreviousLine &&
Daniel Jasper0e81f1a2013-12-02 09:19:27 +00001545 PreviousLine->Affected &&
Daniel Jasper38c82402013-11-29 09:27:43 +00001546 PreviousLine->Last->is(tok::comment);
1547
1548 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
1549 IsContinuedComment) {
Daniel Jasper9c199562013-11-28 15:58:55 +00001550 Line->Affected = true;
Daniel Jasper9c199562013-11-28 15:58:55 +00001551 SomeLineAffected = true;
Daniel Jasper9c199562013-11-28 15:58:55 +00001552 }
1553 return SomeLineAffected;
1554 }
1555
Daniel Jasper5500f612013-11-25 11:08:59 +00001556 // Marks all lines between I and E as well as all their children as affected.
1557 void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I,
1558 SmallVectorImpl<AnnotatedLine *>::iterator E) {
1559 while (I != E) {
1560 (*I)->Affected = true;
1561 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
1562 ++I;
1563 }
1564 }
1565
1566 // Returns true if the range from 'First' to 'Last' intersects with one of the
1567 // input ranges.
1568 bool affectsTokenRange(const FormatToken &First, const FormatToken &Last,
1569 bool IncludeLeadingNewlines) {
1570 SourceLocation Start = First.WhitespaceRange.getBegin();
1571 if (!IncludeLeadingNewlines)
1572 Start = Start.getLocWithOffset(First.LastNewlineOffset);
Daniel Jasper5877bf12013-11-25 11:53:05 +00001573 SourceLocation End = Last.getStartOfNonWhitespace();
1574 if (Last.TokenText.size() > 0)
1575 End = End.getLocWithOffset(Last.TokenText.size() - 1);
Daniel Jasper5500f612013-11-25 11:08:59 +00001576 CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
1577 return affectsCharSourceRange(Range);
1578 }
1579
1580 // Returns true if one of the input ranges intersect the leading empty lines
1581 // before 'Tok'.
1582 bool affectsLeadingEmptyLines(const FormatToken &Tok) {
1583 CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
1584 Tok.WhitespaceRange.getBegin(),
1585 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
1586 return affectsCharSourceRange(EmptyLineRange);
1587 }
1588
1589 // Returns true if 'Range' intersects with one of the input ranges.
1590 bool affectsCharSourceRange(const CharSourceRange &Range) {
1591 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
1592 E = Ranges.end();
1593 I != E; ++I) {
1594 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
1595 !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
1596 return true;
1597 }
1598 return false;
1599 }
1600
Alexander Kornienko9e649af2013-09-11 12:25:57 +00001601 static bool inputUsesCRLF(StringRef Text) {
1602 return Text.count('\r') * 2 > Text.count('\n');
1603 }
1604
Manuel Klimek71814b42013-10-11 21:25:45 +00001605 void
1606 deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001607 unsigned CountBoundToVariable = 0;
1608 unsigned CountBoundToType = 0;
1609 bool HasCpp03IncompatibleFormat = false;
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001610 bool HasBinPackedFunction = false;
1611 bool HasOnePerLineFunction = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001612 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001613 if (!AnnotatedLines[i]->First->Next)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001614 continue;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001615 FormatToken *Tok = AnnotatedLines[i]->First->Next;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001616 while (Tok->Next) {
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001617 if (Tok->Type == TT_PointerOrReference) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001618 bool SpacesBefore =
1619 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
1620 bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
1621 Tok->Next->WhitespaceRange.getEnd();
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001622 if (SpacesBefore && !SpacesAfter)
1623 ++CountBoundToVariable;
1624 else if (!SpacesBefore && SpacesAfter)
1625 ++CountBoundToType;
1626 }
1627
Daniel Jasperfba84ff2013-10-12 05:16:06 +00001628 if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
1629 if (Tok->is(tok::coloncolon) &&
1630 Tok->Previous->Type == TT_TemplateOpener)
1631 HasCpp03IncompatibleFormat = true;
1632 if (Tok->Type == TT_TemplateCloser &&
1633 Tok->Previous->Type == TT_TemplateCloser)
1634 HasCpp03IncompatibleFormat = true;
1635 }
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001636
1637 if (Tok->PackingKind == PPK_BinPacked)
1638 HasBinPackedFunction = true;
1639 if (Tok->PackingKind == PPK_OnePerLine)
1640 HasOnePerLineFunction = true;
1641
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001642 Tok = Tok->Next;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001643 }
1644 }
1645 if (Style.DerivePointerBinding) {
1646 if (CountBoundToType > CountBoundToVariable)
1647 Style.PointerBindsToType = true;
1648 else if (CountBoundToType < CountBoundToVariable)
1649 Style.PointerBindsToType = false;
1650 }
1651 if (Style.Standard == FormatStyle::LS_Auto) {
1652 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
1653 : FormatStyle::LS_Cpp03;
1654 }
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001655 BinPackInconclusiveFunctions =
1656 HasBinPackedFunction || !HasOnePerLineFunction;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001657 }
1658
Craig Topperfb6b25b2014-03-15 04:29:04 +00001659 void consumeUnwrappedLine(const UnwrappedLine &TheLine) override {
Manuel Klimek71814b42013-10-11 21:25:45 +00001660 assert(!UnwrappedLines.empty());
1661 UnwrappedLines.back().push_back(TheLine);
1662 }
1663
Craig Topperfb6b25b2014-03-15 04:29:04 +00001664 void finishRun() override {
Manuel Klimek71814b42013-10-11 21:25:45 +00001665 UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
Daniel Jasperf7935112012-12-03 18:12:45 +00001666 }
1667
1668 FormatStyle Style;
1669 Lexer &Lex;
1670 SourceManager &SourceMgr;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001671 WhitespaceManager Whitespaces;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001672 SmallVector<CharSourceRange, 8> Ranges;
Manuel Klimek71814b42013-10-11 21:25:45 +00001673 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001674
1675 encoding::Encoding Encoding;
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001676 bool BinPackInconclusiveFunctions;
Daniel Jasperf7935112012-12-03 18:12:45 +00001677};
1678
Craig Topperaf35e852013-06-30 22:29:28 +00001679} // end anonymous namespace
1680
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00001681tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
1682 SourceManager &SourceMgr,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +00001683 std::vector<CharSourceRange> Ranges) {
1684 Formatter formatter(Style, Lex, SourceMgr, Ranges);
Daniel Jasperf7935112012-12-03 18:12:45 +00001685 return formatter.format();
1686}
1687
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001688tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
1689 std::vector<tooling::Range> Ranges,
1690 StringRef FileName) {
1691 FileManager Files((FileSystemOptions()));
1692 DiagnosticsEngine Diagnostics(
1693 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1694 new DiagnosticOptions);
1695 SourceManager SourceMgr(Diagnostics, Files);
1696 llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName);
1697 const clang::FileEntry *Entry =
1698 Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
1699 SourceMgr.overrideFileContents(Entry, Buf);
1700 FileID ID =
1701 SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
Alexander Kornienko1e808872013-06-28 12:51:24 +00001702 Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr,
1703 getFormattingLangOpts(Style.Standard));
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001704 SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
1705 std::vector<CharSourceRange> CharRanges;
1706 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
1707 SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset());
1708 SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength());
1709 CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1710 }
1711 return reformat(Style, Lex, SourceMgr, CharRanges);
1712}
1713
Alexander Kornienko1e808872013-06-28 12:51:24 +00001714LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) {
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001715 LangOptions LangOpts;
1716 LangOpts.CPlusPlus = 1;
Alexander Kornienko1e808872013-06-28 12:51:24 +00001717 LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
Daniel Jasper55213652013-03-22 10:01:29 +00001718 LangOpts.LineComment = 1;
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001719 LangOpts.Bool = 1;
1720 LangOpts.ObjC1 = 1;
1721 LangOpts.ObjC2 = 1;
1722 return LangOpts;
1723}
1724
Edwin Vaned544aa72013-09-30 13:31:48 +00001725const char *StyleOptionHelpDescription =
1726 "Coding style, currently supports:\n"
1727 " LLVM, Google, Chromium, Mozilla, WebKit.\n"
1728 "Use -style=file to load style configuration from\n"
1729 ".clang-format file located in one of the parent\n"
1730 "directories of the source file (or current\n"
1731 "directory for stdin).\n"
1732 "Use -style=\"{key: value, ...}\" to set specific\n"
1733 "parameters, e.g.:\n"
1734 " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
1735
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001736static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001737 if (FileName.endswith_lower(".js")) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001738 return FormatStyle::LK_JavaScript;
Daniel Jasper7052ce62014-01-19 09:04:08 +00001739 } else if (FileName.endswith_lower(".proto") ||
1740 FileName.endswith_lower(".protodevel")) {
1741 return FormatStyle::LK_Proto;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001742 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001743 return FormatStyle::LK_Cpp;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001744}
1745
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001746FormatStyle getStyle(StringRef StyleName, StringRef FileName,
1747 StringRef FallbackStyle) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001748 FormatStyle Style = getLLVMStyle();
1749 Style.Language = getLanguageByFileName(FileName);
1750 if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) {
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001751 llvm::errs() << "Invalid fallback style \"" << FallbackStyle
1752 << "\" using LLVM style\n";
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001753 return Style;
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001754 }
Edwin Vaned544aa72013-09-30 13:31:48 +00001755
1756 if (StyleName.startswith("{")) {
1757 // Parse YAML/JSON style from the command line.
1758 if (llvm::error_code ec = parseConfiguration(StyleName, &Style)) {
Alexander Kornienkoe2e03872013-10-14 00:46:35 +00001759 llvm::errs() << "Error parsing -style: " << ec.message() << ", using "
1760 << FallbackStyle << " style\n";
Edwin Vaned544aa72013-09-30 13:31:48 +00001761 }
1762 return Style;
1763 }
1764
1765 if (!StyleName.equals_lower("file")) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001766 if (!getPredefinedStyle(StyleName, Style.Language, &Style))
Edwin Vaned544aa72013-09-30 13:31:48 +00001767 llvm::errs() << "Invalid value for -style, using " << FallbackStyle
1768 << " style\n";
1769 return Style;
1770 }
1771
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001772 // Look for .clang-format/_clang-format file in the file's parent directories.
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001773 SmallString<128> UnsuitableConfigFiles;
Edwin Vaned544aa72013-09-30 13:31:48 +00001774 SmallString<128> Path(FileName);
1775 llvm::sys::fs::make_absolute(Path);
Alexander Kornienkoe2e03872013-10-14 00:46:35 +00001776 for (StringRef Directory = Path; !Directory.empty();
Edwin Vaned544aa72013-09-30 13:31:48 +00001777 Directory = llvm::sys::path::parent_path(Directory)) {
1778 if (!llvm::sys::fs::is_directory(Directory))
1779 continue;
1780 SmallString<128> ConfigFile(Directory);
1781
1782 llvm::sys::path::append(ConfigFile, ".clang-format");
1783 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1784 bool IsFile = false;
1785 // Ignore errors from is_regular_file: we only need to know if we can read
1786 // the file or not.
1787 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1788
1789 if (!IsFile) {
1790 // Try _clang-format too, since dotfiles are not commonly used on Windows.
1791 ConfigFile = Directory;
1792 llvm::sys::path::append(ConfigFile, "_clang-format");
1793 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1794 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1795 }
1796
1797 if (IsFile) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001798 std::unique_ptr<llvm::MemoryBuffer> Text;
Rafael Espindola1a3605c2013-10-25 19:00:49 +00001799 if (llvm::error_code ec =
1800 llvm::MemoryBuffer::getFile(ConfigFile.c_str(), Text)) {
Edwin Vaned544aa72013-09-30 13:31:48 +00001801 llvm::errs() << ec.message() << "\n";
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001802 break;
Edwin Vaned544aa72013-09-30 13:31:48 +00001803 }
1804 if (llvm::error_code ec = parseConfiguration(Text->getBuffer(), &Style)) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001805 if (ec == llvm::errc::not_supported) {
1806 if (!UnsuitableConfigFiles.empty())
1807 UnsuitableConfigFiles.append(", ");
1808 UnsuitableConfigFiles.append(ConfigFile);
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001809 continue;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001810 }
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001811 llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()
1812 << "\n";
1813 break;
Edwin Vaned544aa72013-09-30 13:31:48 +00001814 }
1815 DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
1816 return Style;
1817 }
1818 }
1819 llvm::errs() << "Can't find usable .clang-format, using " << FallbackStyle
1820 << " style\n";
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001821 if (!UnsuitableConfigFiles.empty()) {
1822 llvm::errs() << "Configuration file(s) do(es) not support "
1823 << getLanguageName(Style.Language) << ": "
1824 << UnsuitableConfigFiles << "\n";
1825 }
Edwin Vaned544aa72013-09-30 13:31:48 +00001826 return Style;
1827}
1828
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001829} // namespace format
1830} // namespace clang