blob: 5c239069c073dfa60b87f50c05655a46f515d742 [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
Daniel Jasperde0328a2013-08-16 11:20:30 +000016#include "ContinuationIndenter.h"
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000017#include "TokenAnnotator.h"
Daniel Jasper0df50932014-12-10 19:00:42 +000018#include "UnwrappedLineFormatter.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "UnwrappedLineParser.h"
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000020#include "WhitespaceManager.h"
Daniel Jasperec04c0d2013-05-16 10:40:07 +000021#include "clang/Basic/Diagnostic.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000022#include "clang/Basic/DiagnosticOptions.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"
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +000030#include "llvm/Support/Regex.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "llvm/Support/YAMLTraits.h"
Manuel Klimek2ef908e2013-02-13 10:46:36 +000032#include <queue>
Daniel Jasper8b529712012-12-04 13:02:32 +000033#include <string>
34
Chandler Carruth10346662014-04-22 03:17:02 +000035#define DEBUG_TYPE "format-formatter"
36
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000037using clang::format::FormatStyle;
38
Daniel Jaspere1e43192014-04-01 12:55:11 +000039LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string)
40
Alexander Kornienkod6538332013-05-07 15:32:14 +000041namespace llvm {
42namespace yaml {
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000043template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> {
44 static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) {
45 IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp);
Daniel Jasperc58c70e2014-09-15 11:21:46 +000046 IO.enumCase(Value, "Java", FormatStyle::LK_Java);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000047 IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript);
Daniel Jasper7052ce62014-01-19 09:04:08 +000048 IO.enumCase(Value, "Proto", FormatStyle::LK_Proto);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000049 }
50};
51
52template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> {
53 static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) {
54 IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03);
55 IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03);
56 IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11);
57 IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11);
58 IO.enumCase(Value, "Auto", FormatStyle::LS_Auto);
59 }
60};
61
62template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
63 static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) {
64 IO.enumCase(Value, "Never", FormatStyle::UT_Never);
65 IO.enumCase(Value, "false", FormatStyle::UT_Never);
66 IO.enumCase(Value, "Always", FormatStyle::UT_Always);
67 IO.enumCase(Value, "true", FormatStyle::UT_Always);
68 IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation);
69 }
70};
71
Daniel Jasperd74cf402014-04-08 12:46:38 +000072template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> {
73 static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) {
74 IO.enumCase(Value, "None", FormatStyle::SFS_None);
75 IO.enumCase(Value, "false", FormatStyle::SFS_None);
76 IO.enumCase(Value, "All", FormatStyle::SFS_All);
77 IO.enumCase(Value, "true", FormatStyle::SFS_All);
78 IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline);
Daniel Jasper9e709352014-11-26 10:43:58 +000079 IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty);
Daniel Jasperd74cf402014-04-08 12:46:38 +000080 }
81};
82
Daniel Jasperac043c92014-09-15 11:11:00 +000083template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> {
84 static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) {
85 IO.enumCase(Value, "All", FormatStyle::BOS_All);
86 IO.enumCase(Value, "true", FormatStyle::BOS_All);
87 IO.enumCase(Value, "None", FormatStyle::BOS_None);
88 IO.enumCase(Value, "false", FormatStyle::BOS_None);
89 IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment);
90 }
91};
92
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000093template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
94 static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) {
95 IO.enumCase(Value, "Attach", FormatStyle::BS_Attach);
96 IO.enumCase(Value, "Linux", FormatStyle::BS_Linux);
Birunthan Mohanathas305fa9c2015-07-12 03:13:54 +000097 IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000098 IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
99 IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000100 IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000101 }
102};
103
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000104template <> struct ScalarEnumerationTraits<FormatStyle::DefinitionReturnTypeBreakingStyle> {
105 static void enumeration(IO &IO, FormatStyle::DefinitionReturnTypeBreakingStyle &Value) {
106 IO.enumCase(Value, "None", FormatStyle::DRTBS_None);
107 IO.enumCase(Value, "All", FormatStyle::DRTBS_All);
108 IO.enumCase(Value, "TopLevel", FormatStyle::DRTBS_TopLevel);
109
110 // For backward compatibility.
111 IO.enumCase(Value, "false", FormatStyle::DRTBS_None);
112 IO.enumCase(Value, "true", FormatStyle::DRTBS_All);
113 }
114};
115
Alexander Kornienkod6538332013-05-07 15:32:14 +0000116template <>
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000117struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000118 static void enumeration(IO &IO,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000119 FormatStyle::NamespaceIndentationKind &Value) {
120 IO.enumCase(Value, "None", FormatStyle::NI_None);
121 IO.enumCase(Value, "Inner", FormatStyle::NI_Inner);
122 IO.enumCase(Value, "All", FormatStyle::NI_All);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000123 }
124};
125
Jacques Pienaarfc275112015-02-18 23:48:37 +0000126template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> {
127 static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) {
Daniel Jasper553d4872014-06-17 12:40:34 +0000128 IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle);
129 IO.enumCase(Value, "Left", FormatStyle::PAS_Left);
130 IO.enumCase(Value, "Right", FormatStyle::PAS_Right);
131
Alp Toker958027b2014-07-14 19:42:55 +0000132 // For backward compatibility.
Daniel Jasper553d4872014-06-17 12:40:34 +0000133 IO.enumCase(Value, "true", FormatStyle::PAS_Left);
134 IO.enumCase(Value, "false", FormatStyle::PAS_Right);
135 }
136};
137
138template <>
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000139struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> {
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000140 static void enumeration(IO &IO,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000141 FormatStyle::SpaceBeforeParensOptions &Value) {
142 IO.enumCase(Value, "Never", FormatStyle::SBPO_Never);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000143 IO.enumCase(Value, "ControlStatements",
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000144 FormatStyle::SBPO_ControlStatements);
145 IO.enumCase(Value, "Always", FormatStyle::SBPO_Always);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000146
147 // For backward compatibility.
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000148 IO.enumCase(Value, "false", FormatStyle::SBPO_Never);
149 IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000150 }
151};
152
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000153template <> struct MappingTraits<FormatStyle> {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000154 static void mapping(IO &IO, FormatStyle &Style) {
155 // When reading, read the language first, we need it for getPredefinedStyle.
156 IO.mapOptional("Language", Style.Language);
157
Alexander Kornienko49149672013-05-10 11:56:10 +0000158 if (IO.outputting()) {
Jacques Pienaarfc275112015-02-18 23:48:37 +0000159 StringRef StylesArray[] = {"LLVM", "Google", "Chromium",
160 "Mozilla", "WebKit", "GNU"};
Alexander Kornienko49149672013-05-10 11:56:10 +0000161 ArrayRef<StringRef> Styles(StylesArray);
162 for (size_t i = 0, e = Styles.size(); i < e; ++i) {
163 StringRef StyleName(Styles[i]);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000164 FormatStyle PredefinedStyle;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000165 if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000166 Style == PredefinedStyle) {
Alexander Kornienko49149672013-05-10 11:56:10 +0000167 IO.mapOptional("# BasedOnStyle", StyleName);
168 break;
169 }
170 }
171 } else {
Alexander Kornienkod6538332013-05-07 15:32:14 +0000172 StringRef BasedOnStyle;
173 IO.mapOptional("BasedOnStyle", BasedOnStyle);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000174 if (!BasedOnStyle.empty()) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000175 FormatStyle::LanguageKind OldLanguage = Style.Language;
176 FormatStyle::LanguageKind Language =
177 ((FormatStyle *)IO.getContext())->Language;
178 if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000179 IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
180 return;
181 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000182 Style.Language = OldLanguage;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000183 }
Alexander Kornienkod6538332013-05-07 15:32:14 +0000184 }
185
Birunthan Mohanathas50a6f912015-06-28 14:52:34 +0000186 // For backward compatibility.
187 if (!IO.outputting()) {
188 IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment);
189 IO.mapOptional("IndentFunctionDeclarationAfterType",
190 Style.IndentWrappedFunctionNames);
191 IO.mapOptional("PointerBindsToType", Style.PointerAlignment);
192 IO.mapOptional("SpaceAfterControlStatementKeyword",
193 Style.SpaceBeforeParens);
194 }
195
Alexander Kornienkod6538332013-05-07 15:32:14 +0000196 IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
Daniel Jasper3aa9a6a2014-11-18 23:55:27 +0000197 IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000198 IO.mapOptional("AlignConsecutiveAssignments",
199 Style.AlignConsecutiveAssignments);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000200 IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
Daniel Jasper3219e432014-12-02 13:24:51 +0000201 IO.mapOptional("AlignOperands", Style.AlignOperands);
Daniel Jasper552f4a72013-07-31 23:55:15 +0000202 IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000203 IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
204 Style.AllowAllParametersOfDeclarationOnNextLine);
Daniel Jasper17605d32014-05-14 09:33:35 +0000205 IO.mapOptional("AllowShortBlocksOnASingleLine",
206 Style.AllowShortBlocksOnASingleLine);
Daniel Jasperb87899b2014-09-10 13:11:45 +0000207 IO.mapOptional("AllowShortCaseLabelsOnASingleLine",
208 Style.AllowShortCaseLabelsOnASingleLine);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000209 IO.mapOptional("AllowShortFunctionsOnASingleLine",
210 Style.AllowShortFunctionsOnASingleLine);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000211 IO.mapOptional("AllowShortIfStatementsOnASingleLine",
212 Style.AllowShortIfStatementsOnASingleLine);
Daniel Jasper3a685df2013-05-16 12:12:21 +0000213 IO.mapOptional("AllowShortLoopsOnASingleLine",
214 Style.AllowShortLoopsOnASingleLine);
Daniel Jasperca4ea1c2014-08-05 12:16:31 +0000215 IO.mapOptional("AlwaysBreakAfterDefinitionReturnType",
216 Style.AlwaysBreakAfterDefinitionReturnType);
Alexander Kornienko58611712013-07-04 12:02:44 +0000217 IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
218 Style.AlwaysBreakBeforeMultilineStrings);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000219 IO.mapOptional("AlwaysBreakTemplateDeclarations",
220 Style.AlwaysBreakTemplateDeclarations);
221 IO.mapOptional("BinPackArguments", Style.BinPackArguments);
222 IO.mapOptional("BinPackParameters", Style.BinPackParameters);
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000223 IO.mapOptional("BreakBeforeBinaryOperators",
224 Style.BreakBeforeBinaryOperators);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000225 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
Daniel Jasper165b29e2013-11-08 00:57:11 +0000226 IO.mapOptional("BreakBeforeTernaryOperators",
227 Style.BreakBeforeTernaryOperators);
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000228 IO.mapOptional("BreakConstructorInitializersBeforeComma",
229 Style.BreakConstructorInitializersBeforeComma);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000230 IO.mapOptional("ColumnLimit", Style.ColumnLimit);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000231 IO.mapOptional("CommentPragmas", Style.CommentPragmas);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000232 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
233 Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
Daniel Jasper50d634b2014-10-28 16:53:38 +0000234 IO.mapOptional("ConstructorInitializerIndentWidth",
235 Style.ConstructorInitializerIndentWidth);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000236 IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
237 IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
Daniel Jasper553d4872014-06-17 12:40:34 +0000238 IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000239 IO.mapOptional("DisableFormat", Style.DisableFormat);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000240 IO.mapOptional("ExperimentalAutoDetectBinPacking",
241 Style.ExperimentalAutoDetectBinPacking);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000242 IO.mapOptional("ForEachMacros", Style.ForEachMacros);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000243 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000244 IO.mapOptional("IndentWidth", Style.IndentWidth);
245 IO.mapOptional("IndentWrappedFunctionNames",
246 Style.IndentWrappedFunctionNames);
Daniel Jaspera26fc5c2014-03-21 13:43:14 +0000247 IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks",
248 Style.KeepEmptyLinesAtTheStartOfBlocks);
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000249 IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin);
250 IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000251 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000252 IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
Daniel Jasper50d634b2014-10-28 16:53:38 +0000253 IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth);
Daniel Jaspere9beea22014-01-28 15:20:33 +0000254 IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000255 IO.mapOptional("ObjCSpaceBeforeProtocolList",
256 Style.ObjCSpaceBeforeProtocolList);
Daniel Jasper33b909c2013-10-25 14:29:37 +0000257 IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
258 Style.PenaltyBreakBeforeFirstCallParameter);
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000259 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000260 IO.mapOptional("PenaltyBreakFirstLessLess",
261 Style.PenaltyBreakFirstLessLess);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000262 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000263 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
264 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
265 Style.PenaltyReturnTypeOnItsOwnLine);
Daniel Jasper553d4872014-06-17 12:40:34 +0000266 IO.mapOptional("PointerAlignment", Style.PointerAlignment);
Daniel Jasperdb986eb2014-09-03 07:37:29 +0000267 IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
Daniel Jasperd94bff32013-09-25 15:15:02 +0000268 IO.mapOptional("SpaceBeforeAssignmentOperators",
269 Style.SpaceBeforeAssignmentOperators);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000270 IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
271 IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
272 IO.mapOptional("SpacesBeforeTrailingComments",
273 Style.SpacesBeforeTrailingComments);
274 IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
275 IO.mapOptional("SpacesInContainerLiterals",
276 Style.SpacesInContainerLiterals);
277 IO.mapOptional("SpacesInCStyleCastParentheses",
278 Style.SpacesInCStyleCastParentheses);
279 IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
280 IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
281 IO.mapOptional("Standard", Style.Standard);
282 IO.mapOptional("TabWidth", Style.TabWidth);
283 IO.mapOptional("UseTab", Style.UseTab);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000284 }
285};
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000286
287// Allows to read vector<FormatStyle> while keeping default values.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000288// IO.getContext() should contain a pointer to the FormatStyle structure, that
289// will be used to get default values for missing keys.
290// If the first element has no Language specified, it will be treated as the
291// default one for the following elements.
Jacques Pienaarfc275112015-02-18 23:48:37 +0000292template <> struct DocumentListTraits<std::vector<FormatStyle>> {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000293 static size_t size(IO &IO, std::vector<FormatStyle> &Seq) {
294 return Seq.size();
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000295 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000296 static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000297 size_t Index) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000298 if (Index >= Seq.size()) {
299 assert(Index == Seq.size());
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000300 FormatStyle Template;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000301 if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000302 Template = Seq[0];
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000303 } else {
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000304 Template = *((const FormatStyle *)IO.getContext());
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000305 Template.Language = FormatStyle::LK_None;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000306 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000307 Seq.resize(Index + 1, Template);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000308 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000309 return Seq[Index];
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000310 }
311};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000312}
313}
Alexander Kornienkod6538332013-05-07 15:32:14 +0000314
Daniel Jasperf7935112012-12-03 18:12:45 +0000315namespace clang {
316namespace format {
317
Rafael Espindola6d0d89b2014-06-12 03:31:26 +0000318const std::error_category &getParseCategory() {
Rafael Espindolad0136702014-06-12 02:50:04 +0000319 static ParseErrorCategory C;
320 return C;
321}
322std::error_code make_error_code(ParseError e) {
Rafael Espindola6d0d89b2014-06-12 03:31:26 +0000323 return std::error_code(static_cast<int>(e), getParseCategory());
Rafael Espindolad0136702014-06-12 02:50:04 +0000324}
325
326const char *ParseErrorCategory::name() const LLVM_NOEXCEPT {
327 return "clang-format.parse_error";
328}
329
330std::string ParseErrorCategory::message(int EV) const {
331 switch (static_cast<ParseError>(EV)) {
332 case ParseError::Success:
333 return "Success";
334 case ParseError::Error:
335 return "Invalid argument";
336 case ParseError::Unsuitable:
337 return "Unsuitable";
338 }
Saleem Abdulrasoolfbfbaf62014-06-12 19:33:26 +0000339 llvm_unreachable("unexpected parse error");
Rafael Espindolad0136702014-06-12 02:50:04 +0000340}
341
Daniel Jasperf7935112012-12-03 18:12:45 +0000342FormatStyle getLLVMStyle() {
343 FormatStyle LLVMStyle;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000344 LLVMStyle.Language = FormatStyle::LK_Cpp;
Daniel Jasperf7935112012-12-03 18:12:45 +0000345 LLVMStyle.AccessModifierOffset = -2;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000346 LLVMStyle.AlignEscapedNewlinesLeft = false;
Daniel Jasper3aa9a6a2014-11-18 23:55:27 +0000347 LLVMStyle.AlignAfterOpenBracket = true;
Daniel Jasper3219e432014-12-02 13:24:51 +0000348 LLVMStyle.AlignOperands = true;
Daniel Jasper552f4a72013-07-31 23:55:15 +0000349 LLVMStyle.AlignTrailingComments = true;
Daniel Jaspera44991332015-04-29 13:06:49 +0000350 LLVMStyle.AlignConsecutiveAssignments = false;
Daniel Jasperf7db4332013-01-29 16:03:49 +0000351 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasperd74cf402014-04-08 12:46:38 +0000352 LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
Daniel Jasper17605d32014-05-14 09:33:35 +0000353 LLVMStyle.AllowShortBlocksOnASingleLine = false;
Daniel Jasperb87899b2014-09-10 13:11:45 +0000354 LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000355 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000356 LLVMStyle.AllowShortLoopsOnASingleLine = false;
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000357 LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None;
Alexander Kornienko58611712013-07-04 12:02:44 +0000358 LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000359 LLVMStyle.AlwaysBreakTemplateDeclarations = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000360 LLVMStyle.BinPackParameters = true;
Daniel Jasper18210d72014-10-09 09:52:05 +0000361 LLVMStyle.BinPackArguments = true;
Daniel Jasperac043c92014-09-15 11:11:00 +0000362 LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
Daniel Jasper165b29e2013-11-08 00:57:11 +0000363 LLVMStyle.BreakBeforeTernaryOperators = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000364 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
365 LLVMStyle.BreakConstructorInitializersBeforeComma = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000366 LLVMStyle.ColumnLimit = 80;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000367 LLVMStyle.CommentPragmas = "^ IWYU pragma:";
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000368 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Daniel Jaspercdaffa42013-08-13 10:58:30 +0000369 LLVMStyle.ConstructorInitializerIndentWidth = 4;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000370 LLVMStyle.ContinuationIndentWidth = 4;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000371 LLVMStyle.Cpp11BracedListStyle = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000372 LLVMStyle.DerivePointerAlignment = false;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000373 LLVMStyle.ExperimentalAutoDetectBinPacking = false;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000374 LLVMStyle.ForEachMacros.push_back("foreach");
375 LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
376 LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000377 LLVMStyle.IndentCaseLabels = false;
Daniel Jasperc75e1ef2014-07-09 08:42:42 +0000378 LLVMStyle.IndentWrappedFunctionNames = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000379 LLVMStyle.IndentWidth = 2;
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000380 LLVMStyle.TabWidth = 8;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000381 LLVMStyle.MaxEmptyLinesToKeep = 1;
Daniel Jaspera26fc5c2014-03-21 13:43:14 +0000382 LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000383 LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
Daniel Jasper50d634b2014-10-28 16:53:38 +0000384 LLVMStyle.ObjCBlockIndentWidth = 2;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000385 LLVMStyle.ObjCSpaceAfterProperty = false;
Nico Webera6087752013-01-10 20:12:55 +0000386 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000387 LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000388 LLVMStyle.SpacesBeforeTrailingComments = 1;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000389 LLVMStyle.Standard = FormatStyle::LS_Cpp11;
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000390 LLVMStyle.UseTab = FormatStyle::UT_Never;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000391 LLVMStyle.SpacesInParentheses = false;
Daniel Jasperad981f82014-08-26 11:41:14 +0000392 LLVMStyle.SpacesInSquareBrackets = false;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000393 LLVMStyle.SpaceInEmptyParentheses = false;
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000394 LLVMStyle.SpacesInContainerLiterals = true;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000395 LLVMStyle.SpacesInCStyleCastParentheses = false;
Daniel Jasperdb986eb2014-09-03 07:37:29 +0000396 LLVMStyle.SpaceAfterCStyleCast = false;
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000397 LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
Daniel Jasperd94bff32013-09-25 15:15:02 +0000398 LLVMStyle.SpaceBeforeAssignmentOperators = true;
Daniel Jasperdd978ae2013-10-29 14:52:02 +0000399 LLVMStyle.SpacesInAngles = false;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000400
Daniel Jasper19a541e2013-12-19 16:45:34 +0000401 LLVMStyle.PenaltyBreakComment = 300;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000402 LLVMStyle.PenaltyBreakFirstLessLess = 120;
403 LLVMStyle.PenaltyBreakString = 1000;
404 LLVMStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000405 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
Daniel Jasper33b909c2013-10-25 14:29:37 +0000406 LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000407
Daniel Jasperc64b09a2014-05-22 15:12:22 +0000408 LLVMStyle.DisableFormat = false;
409
Daniel Jasperf7935112012-12-03 18:12:45 +0000410 return LLVMStyle;
411}
412
Nico Weber514ecc82014-02-02 20:50:45 +0000413FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000414 FormatStyle GoogleStyle = getLLVMStyle();
Nico Weber514ecc82014-02-02 20:50:45 +0000415 GoogleStyle.Language = Language;
416
Daniel Jasperf7935112012-12-03 18:12:45 +0000417 GoogleStyle.AccessModifierOffset = -1;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000418 GoogleStyle.AlignEscapedNewlinesLeft = true;
Daniel Jasper085a2ed2013-04-24 13:46:00 +0000419 GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
Daniel Jasper5bd0b9e2013-05-23 18:05:18 +0000420 GoogleStyle.AllowShortLoopsOnASingleLine = true;
Alexander Kornienko58611712013-07-04 12:02:44 +0000421 GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000422 GoogleStyle.AlwaysBreakTemplateDeclarations = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000423 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000424 GoogleStyle.DerivePointerAlignment = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000425 GoogleStyle.IndentCaseLabels = true;
Daniel Jaspera26fc5c2014-03-21 13:43:14 +0000426 GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000427 GoogleStyle.ObjCSpaceAfterProperty = false;
Nico Webera6087752013-01-10 20:12:55 +0000428 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Daniel Jasper553d4872014-06-17 12:40:34 +0000429 GoogleStyle.PointerAlignment = FormatStyle::PAS_Left;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000430 GoogleStyle.SpacesBeforeTrailingComments = 2;
431 GoogleStyle.Standard = FormatStyle::LS_Auto;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000432
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000433 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
Daniel Jasper33b909c2013-10-25 14:29:37 +0000434 GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000435
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000436 if (Language == FormatStyle::LK_Java) {
Daniel Jasper3aa9a6a2014-11-18 23:55:27 +0000437 GoogleStyle.AlignAfterOpenBracket = false;
Daniel Jasper3219e432014-12-02 13:24:51 +0000438 GoogleStyle.AlignOperands = false;
Daniel Jasperfd4ed182015-01-04 20:40:45 +0000439 GoogleStyle.AlignTrailingComments = false;
Daniel Jasper9e709352014-11-26 10:43:58 +0000440 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
Daniel Jasperfd4ed182015-01-04 20:40:45 +0000441 GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper1cd3c712015-01-14 12:24:59 +0000442 GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000443 GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
444 GoogleStyle.ColumnLimit = 100;
445 GoogleStyle.SpaceAfterCStyleCast = true;
Daniel Jasper61d81972014-11-14 08:22:46 +0000446 GoogleStyle.SpacesBeforeTrailingComments = 1;
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000447 } else if (Language == FormatStyle::LK_JavaScript) {
Daniel Jaspere551bb72014-11-05 17:22:31 +0000448 GoogleStyle.BreakBeforeTernaryOperators = false;
Daniel Jasper8f83a902014-05-09 10:28:58 +0000449 GoogleStyle.MaxEmptyLinesToKeep = 3;
Nico Weber514ecc82014-02-02 20:50:45 +0000450 GoogleStyle.SpacesInContainerLiterals = false;
Daniel Jasper67f8ad22014-09-30 17:57:06 +0000451 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
Daniel Jasper1cd3c712015-01-14 12:24:59 +0000452 GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
Nico Weber514ecc82014-02-02 20:50:45 +0000453 } else if (Language == FormatStyle::LK_Proto) {
Daniel Jasperd74cf402014-04-08 12:46:38 +0000454 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
Daniel Jasper783bac62014-04-15 09:54:30 +0000455 GoogleStyle.SpacesInContainerLiterals = false;
Nico Weber514ecc82014-02-02 20:50:45 +0000456 }
457
Daniel Jasperf7935112012-12-03 18:12:45 +0000458 return GoogleStyle;
459}
460
Nico Weber514ecc82014-02-02 20:50:45 +0000461FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
462 FormatStyle ChromiumStyle = getGoogleStyle(Language);
Nico Weber450425c2014-11-26 16:43:18 +0000463 if (Language == FormatStyle::LK_Java) {
Daniel Jasperfd4ed182015-01-04 20:40:45 +0000464 ChromiumStyle.AllowShortIfStatementsOnASingleLine = true;
Nico Weber450425c2014-11-26 16:43:18 +0000465 ChromiumStyle.IndentWidth = 4;
466 ChromiumStyle.ContinuationIndentWidth = 8;
467 } else {
468 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
469 ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
470 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
471 ChromiumStyle.AllowShortLoopsOnASingleLine = false;
472 ChromiumStyle.BinPackParameters = false;
473 ChromiumStyle.DerivePointerAlignment = false;
474 }
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000475 ChromiumStyle.MacroBlockBegin = "^IPC_BEGIN_MESSAGE_MAP$";
476 ChromiumStyle.MacroBlockBegin = "^IPC_END_MESSAGE_MAP$";
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000477 return ChromiumStyle;
478}
479
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000480FormatStyle getMozillaStyle() {
481 FormatStyle MozillaStyle = getLLVMStyle();
482 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Birunthan Mohanathasa0810022015-06-29 15:18:58 +0000483 MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000484 MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
485 FormatStyle::DRTBS_TopLevel;
Birunthan Mohanathasa0810022015-06-29 15:18:58 +0000486 MozillaStyle.AlwaysBreakTemplateDeclarations = true;
Birunthan Mohanathas305fa9c2015-07-12 03:13:54 +0000487 MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
Birunthan Mohanathasa0810022015-06-29 15:18:58 +0000488 MozillaStyle.BreakConstructorInitializersBeforeComma = true;
489 MozillaStyle.ConstructorInitializerIndentWidth = 2;
490 MozillaStyle.ContinuationIndentWidth = 2;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000491 MozillaStyle.Cpp11BracedListStyle = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000492 MozillaStyle.IndentCaseLabels = true;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000493 MozillaStyle.ObjCSpaceAfterProperty = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000494 MozillaStyle.ObjCSpaceBeforeProtocolList = false;
495 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
Daniel Jasper553d4872014-06-17 12:40:34 +0000496 MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000497 return MozillaStyle;
498}
499
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000500FormatStyle getWebKitStyle() {
501 FormatStyle Style = getLLVMStyle();
Daniel Jasper65ee3472013-07-31 23:16:02 +0000502 Style.AccessModifierOffset = -4;
Daniel Jasper3aa9a6a2014-11-18 23:55:27 +0000503 Style.AlignAfterOpenBracket = false;
Daniel Jasper3219e432014-12-02 13:24:51 +0000504 Style.AlignOperands = false;
Daniel Jasper552f4a72013-07-31 23:55:15 +0000505 Style.AlignTrailingComments = false;
Daniel Jasperac043c92014-09-15 11:11:00 +0000506 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000507 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000508 Style.BreakConstructorInitializersBeforeComma = true;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000509 Style.Cpp11BracedListStyle = false;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000510 Style.ColumnLimit = 0;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000511 Style.IndentWidth = 4;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000512 Style.NamespaceIndentation = FormatStyle::NI_Inner;
Daniel Jasper50d634b2014-10-28 16:53:38 +0000513 Style.ObjCBlockIndentWidth = 4;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000514 Style.ObjCSpaceAfterProperty = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000515 Style.PointerAlignment = FormatStyle::PAS_Left;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000516 Style.Standard = FormatStyle::LS_Cpp03;
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000517 return Style;
518}
519
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000520FormatStyle getGNUStyle() {
521 FormatStyle Style = getLLVMStyle();
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000522 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
Daniel Jasperac043c92014-09-15 11:11:00 +0000523 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000524 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000525 Style.BreakBeforeTernaryOperators = true;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000526 Style.Cpp11BracedListStyle = false;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000527 Style.ColumnLimit = 79;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000528 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000529 Style.Standard = FormatStyle::LS_Cpp03;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000530 return Style;
531}
532
Daniel Jasperc64b09a2014-05-22 15:12:22 +0000533FormatStyle getNoStyle() {
534 FormatStyle NoStyle = getLLVMStyle();
535 NoStyle.DisableFormat = true;
536 return NoStyle;
537}
538
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000539bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
540 FormatStyle *Style) {
541 if (Name.equals_lower("llvm")) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000542 *Style = getLLVMStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000543 } else if (Name.equals_lower("chromium")) {
Nico Weber514ecc82014-02-02 20:50:45 +0000544 *Style = getChromiumStyle(Language);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000545 } else if (Name.equals_lower("mozilla")) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000546 *Style = getMozillaStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000547 } else if (Name.equals_lower("google")) {
Nico Weber514ecc82014-02-02 20:50:45 +0000548 *Style = getGoogleStyle(Language);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000549 } else if (Name.equals_lower("webkit")) {
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000550 *Style = getWebKitStyle();
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000551 } else if (Name.equals_lower("gnu")) {
552 *Style = getGNUStyle();
Daniel Jasperc64b09a2014-05-22 15:12:22 +0000553 } else if (Name.equals_lower("none")) {
554 *Style = getNoStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000555 } else {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000556 return false;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000557 }
Alexander Kornienkod6538332013-05-07 15:32:14 +0000558
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000559 Style->Language = Language;
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000560 return true;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000561}
562
Rafael Espindolac0809172014-06-12 14:02:15 +0000563std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000564 assert(Style);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000565 FormatStyle::LanguageKind Language = Style->Language;
566 assert(Language != FormatStyle::LK_None);
Alexander Kornienko06e00332013-05-20 15:18:01 +0000567 if (Text.trim().empty())
Rafael Espindolad0136702014-06-12 02:50:04 +0000568 return make_error_code(ParseError::Error);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000569
570 std::vector<FormatStyle> Styles;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000571 llvm::yaml::Input Input(Text);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000572 // DocumentListTraits<vector<FormatStyle>> uses the context to get default
573 // values for the fields, keys for which are missing from the configuration.
574 // Mapping also uses the context to get the language to find the correct
575 // base style.
576 Input.setContext(Style);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000577 Input >> Styles;
578 if (Input.error())
579 return Input.error();
580
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000581 for (unsigned i = 0; i < Styles.size(); ++i) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000582 // Ensures that only the first configuration can skip the Language option.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000583 if (Styles[i].Language == FormatStyle::LK_None && i != 0)
Rafael Espindolad0136702014-06-12 02:50:04 +0000584 return make_error_code(ParseError::Error);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000585 // Ensure that each language is configured at most once.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000586 for (unsigned j = 0; j < i; ++j) {
587 if (Styles[i].Language == Styles[j].Language) {
588 DEBUG(llvm::dbgs()
589 << "Duplicate languages in the config file on positions " << j
590 << " and " << i << "\n");
Rafael Espindolad0136702014-06-12 02:50:04 +0000591 return make_error_code(ParseError::Error);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000592 }
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000593 }
594 }
595 // Look for a suitable configuration starting from the end, so we can
596 // find the configuration for the specific language first, and the default
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000597 // configuration (which can only be at slot 0) after it.
598 for (int i = Styles.size() - 1; i >= 0; --i) {
599 if (Styles[i].Language == Language ||
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000600 Styles[i].Language == FormatStyle::LK_None) {
601 *Style = Styles[i];
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000602 Style->Language = Language;
Rafael Espindolad0136702014-06-12 02:50:04 +0000603 return make_error_code(ParseError::Success);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000604 }
605 }
Rafael Espindolad0136702014-06-12 02:50:04 +0000606 return make_error_code(ParseError::Unsuitable);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000607}
608
609std::string configurationAsText(const FormatStyle &Style) {
610 std::string Text;
611 llvm::raw_string_ostream Stream(Text);
612 llvm::yaml::Output Output(Stream);
613 // We use the same mapping method for input and output, so we need a non-const
614 // reference here.
615 FormatStyle NonConstStyle = Style;
616 Output << NonConstStyle;
Alexander Kornienko9a38ec22013-05-13 12:56:35 +0000617 return Stream.str();
Alexander Kornienkod6538332013-05-07 15:32:14 +0000618}
619
Craig Topperaf35e852013-06-30 22:29:28 +0000620namespace {
621
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000622class FormatTokenLexer {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000623public:
Daniel Jasper23376252014-09-09 14:37:39 +0000624 FormatTokenLexer(SourceManager &SourceMgr, FileID ID, FormatStyle &Style,
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000625 encoding::Encoding Encoding)
Craig Topper2145bc02014-05-09 08:15:10 +0000626 : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false),
Jacques Pienaarfc275112015-02-18 23:48:37 +0000627 LessStashed(false), Column(0), TrailingWhitespace(0),
628 SourceMgr(SourceMgr), ID(ID), Style(Style),
629 IdentTable(getFormattingLangOpts(Style)), Keywords(IdentTable),
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000630 Encoding(Encoding), FirstInLineIndex(0), FormattingDisabled(false),
631 MacroBlockBeginRegex(Style.MacroBlockBegin),
632 MacroBlockEndRegex(Style.MacroBlockEnd) {
Daniel Jasper23376252014-09-09 14:37:39 +0000633 Lex.reset(new Lexer(ID, SourceMgr.getBuffer(ID), SourceMgr,
634 getFormattingLangOpts(Style)));
635 Lex->SetKeepWhitespaceMode(true);
Daniel Jaspere1e43192014-04-01 12:55:11 +0000636
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000637 for (const std::string &ForEachMacro : Style.ForEachMacros)
Daniel Jaspere1e43192014-04-01 12:55:11 +0000638 ForEachMacros.push_back(&IdentTable.get(ForEachMacro));
639 std::sort(ForEachMacros.begin(), ForEachMacros.end());
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000640 }
641
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000642 ArrayRef<FormatToken *> lex() {
643 assert(Tokens.empty());
Manuel Klimek68b03042014-04-14 09:14:11 +0000644 assert(FirstInLineIndex == 0);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000645 do {
646 Tokens.push_back(getNextToken());
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000647 tryMergePreviousTokens();
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000648 if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline)
Manuel Klimek68b03042014-04-14 09:14:11 +0000649 FirstInLineIndex = Tokens.size() - 1;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000650 } while (Tokens.back()->Tok.isNot(tok::eof));
651 return Tokens;
652 }
653
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000654 const AdditionalKeywords &getKeywords() { return Keywords; }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000655
656private:
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000657 void tryMergePreviousTokens() {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000658 if (tryMerge_TMacro())
659 return;
Manuel Klimek68b03042014-04-14 09:14:11 +0000660 if (tryMergeConflictMarkers())
661 return;
Jacques Pienaarfc275112015-02-18 23:48:37 +0000662 if (tryMergeLessLess())
663 return;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000664
665 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000666 if (tryMergeJSRegexLiteral())
667 return;
Daniel Jasper23376252014-09-09 14:37:39 +0000668 if (tryMergeEscapeSequence())
669 return;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000670 if (tryMergeTemplateString())
671 return;
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000672
Benjamin Kramer28b45ce2015-03-08 16:06:46 +0000673 static const tok::TokenKind JSIdentity[] = {tok::equalequal, tok::equal};
674 static const tok::TokenKind JSNotIdentity[] = {tok::exclaimequal,
675 tok::equal};
676 static const tok::TokenKind JSShiftEqual[] = {tok::greater, tok::greater,
677 tok::greaterequal};
678 static const tok::TokenKind JSRightArrow[] = {tok::equal, tok::greater};
Manuel Klimek79e06082015-05-21 12:23:34 +0000679 // FIXME: Investigate what token type gives the correct operator priority.
680 if (tryMergeTokens(JSIdentity, TT_BinaryOperator))
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000681 return;
Manuel Klimek79e06082015-05-21 12:23:34 +0000682 if (tryMergeTokens(JSNotIdentity, TT_BinaryOperator))
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000683 return;
Manuel Klimek79e06082015-05-21 12:23:34 +0000684 if (tryMergeTokens(JSShiftEqual, TT_BinaryOperator))
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000685 return;
Manuel Klimek79e06082015-05-21 12:23:34 +0000686 if (tryMergeTokens(JSRightArrow, TT_JsFatArrow))
Daniel Jasper78214392014-05-19 07:27:02 +0000687 return;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000688 }
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000689 }
690
Jacques Pienaarfc275112015-02-18 23:48:37 +0000691 bool tryMergeLessLess() {
692 // Merge X,less,less,Y into X,lessless,Y unless X or Y is less.
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000693 if (Tokens.size() < 3)
694 return false;
Jacques Pienaarfc275112015-02-18 23:48:37 +0000695
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000696 bool FourthTokenIsLess = false;
697 if (Tokens.size() > 3)
698 FourthTokenIsLess = (Tokens.end() - 4)[0]->is(tok::less);
Jacques Pienaarfc275112015-02-18 23:48:37 +0000699
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000700 auto First = Tokens.end() - 3;
701 if (First[2]->is(tok::less) || First[1]->isNot(tok::less) ||
702 First[0]->isNot(tok::less) || FourthTokenIsLess)
Jacques Pienaarfc275112015-02-18 23:48:37 +0000703 return false;
704
705 // Only merge if there currently is no whitespace between the two "<".
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000706 if (First[1]->WhitespaceRange.getBegin() !=
707 First[1]->WhitespaceRange.getEnd())
Jacques Pienaarfc275112015-02-18 23:48:37 +0000708 return false;
709
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000710 First[0]->Tok.setKind(tok::lessless);
711 First[0]->TokenText = "<<";
712 First[0]->ColumnWidth += 1;
Jacques Pienaarfc275112015-02-18 23:48:37 +0000713 Tokens.erase(Tokens.end() - 2);
714 return true;
715 }
716
Manuel Klimek79e06082015-05-21 12:23:34 +0000717 bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000718 if (Tokens.size() < Kinds.size())
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000719 return false;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000720
721 SmallVectorImpl<FormatToken *>::const_iterator First =
722 Tokens.end() - Kinds.size();
723 if (!First[0]->is(Kinds[0]))
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000724 return false;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000725 unsigned AddLength = 0;
726 for (unsigned i = 1; i < Kinds.size(); ++i) {
Jacques Pienaarfc275112015-02-18 23:48:37 +0000727 if (!First[i]->is(Kinds[i]) ||
728 First[i]->WhitespaceRange.getBegin() !=
729 First[i]->WhitespaceRange.getEnd())
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000730 return false;
731 AddLength += First[i]->TokenText.size();
732 }
733 Tokens.resize(Tokens.size() - Kinds.size() + 1);
734 First[0]->TokenText = StringRef(First[0]->TokenText.data(),
735 First[0]->TokenText.size() + AddLength);
736 First[0]->ColumnWidth += AddLength;
Manuel Klimek79e06082015-05-21 12:23:34 +0000737 First[0]->Type = NewType;
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000738 return true;
739 }
740
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000741 // Tries to merge an escape sequence, i.e. a "\\" and the following
Alp Tokerc3f36af2014-05-15 01:35:53 +0000742 // character. Use e.g. inside JavaScript regex literals.
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000743 bool tryMergeEscapeSequence() {
744 if (Tokens.size() < 2)
745 return false;
746 FormatToken *Previous = Tokens[Tokens.size() - 2];
Daniel Jasper49a9a282014-10-29 16:51:38 +0000747 if (Previous->isNot(tok::unknown) || Previous->TokenText != "\\")
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000748 return false;
Daniel Jasper49a9a282014-10-29 16:51:38 +0000749 ++Previous->ColumnWidth;
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000750 StringRef Text = Previous->TokenText;
Daniel Jasper49a9a282014-10-29 16:51:38 +0000751 Previous->TokenText = StringRef(Text.data(), Text.size() + 1);
752 resetLexer(SourceMgr.getFileOffset(Tokens.back()->Tok.getLocation()) + 1);
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000753 Tokens.resize(Tokens.size() - 1);
Daniel Jasper49a9a282014-10-29 16:51:38 +0000754 Column = Previous->OriginalColumn + Previous->ColumnWidth;
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000755 return true;
756 }
757
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000758 // Try to determine whether the current token ends a JavaScript regex literal.
759 // We heuristically assume that this is a regex literal if we find two
760 // unescaped slashes on a line and the token before the first slash is one of
Daniel Jasperf7405c12014-05-08 07:45:18 +0000761 // "(;,{}![:?", a binary operator or 'return', as those cannot be followed by
762 // a division.
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000763 bool tryMergeJSRegexLiteral() {
Daniel Jasper23376252014-09-09 14:37:39 +0000764 if (Tokens.size() < 2)
765 return false;
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000766
767 // If this is a string literal with a slash inside, compute the slash's
768 // offset and try to find the beginning of the regex literal.
769 // Also look at tok::unknown, as it can be an unterminated char literal.
770 size_t SlashInStringPos = StringRef::npos;
771 if (Tokens.back()->isOneOf(tok::string_literal, tok::char_constant,
772 tok::unknown)) {
773 // Start search from position 1 as otherwise, this is an unknown token
774 // for an unterminated /*-comment which is handled elsewhere.
775 SlashInStringPos = Tokens.back()->TokenText.find('/', 1);
776 if (SlashInStringPos == StringRef::npos)
777 return false;
778 }
779
Daniel Jasper23376252014-09-09 14:37:39 +0000780 // If a regex literal ends in "\//", this gets represented by an unknown
781 // token "\" and a comment.
782 bool MightEndWithEscapedSlash =
783 Tokens.back()->is(tok::comment) &&
784 Tokens.back()->TokenText.startswith("//") &&
785 Tokens[Tokens.size() - 2]->TokenText == "\\";
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000786 if (!MightEndWithEscapedSlash && SlashInStringPos == StringRef::npos &&
Daniel Jasper23376252014-09-09 14:37:39 +0000787 (Tokens.back()->isNot(tok::slash) ||
788 (Tokens[Tokens.size() - 2]->is(tok::unknown) &&
789 Tokens[Tokens.size() - 2]->TokenText == "\\")))
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000790 return false;
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000791
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000792 unsigned TokenCount = 0;
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000793 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
794 ++TokenCount;
Daniel Jasperf7372152015-07-02 14:14:04 +0000795 auto Prev = I + 1;
796 while (Prev != E && Prev[0]->is(tok::comment))
797 ++Prev;
Daniel Jasperc553ae12015-07-02 13:20:45 +0000798 if (I[0]->isOneOf(tok::slash, tok::slashequal) &&
Daniel Jasperf7372152015-07-02 14:14:04 +0000799 (Prev == E ||
800 ((Prev[0]->isOneOf(tok::l_paren, tok::semi, tok::l_brace,
801 tok::r_brace, tok::exclaim, tok::l_square,
802 tok::colon, tok::comma, tok::question,
803 tok::kw_return) ||
804 Prev[0]->isBinaryOperator())))) {
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000805 unsigned LastColumn = Tokens.back()->OriginalColumn;
806 SourceLocation Loc = Tokens.back()->Tok.getLocation();
Daniel Jasper23376252014-09-09 14:37:39 +0000807 if (MightEndWithEscapedSlash) {
Daniel Jasper23376252014-09-09 14:37:39 +0000808 // This regex literal ends in '\//'. Skip past the '//' of the last
809 // token and re-start lexing from there.
Daniel Jasper49a9a282014-10-29 16:51:38 +0000810 resetLexer(SourceMgr.getFileOffset(Loc) + 2);
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000811 } else if (SlashInStringPos != StringRef::npos) {
812 // This regex literal ends in a string_literal with a slash inside.
813 // Calculate end column and reset lexer appropriately.
814 resetLexer(SourceMgr.getFileOffset(Loc) + SlashInStringPos + 1);
815 LastColumn += SlashInStringPos;
Daniel Jasper23376252014-09-09 14:37:39 +0000816 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000817 Tokens.resize(Tokens.size() - TokenCount);
818 Tokens.back()->Tok.setKind(tok::unknown);
819 Tokens.back()->Type = TT_RegexLiteral;
Daniel Jasperf1446202015-07-02 15:00:44 +0000820 // Treat regex literals like other string_literals.
821 Tokens.back()->Tok.setKind(tok::string_literal);
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000822 Tokens.back()->ColumnWidth += LastColumn - I[0]->OriginalColumn;
823 return true;
824 }
825
826 // There can't be a newline inside a regex literal.
827 if (I[0]->NewlinesBefore > 0)
828 return false;
829 }
830 return false;
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000831 }
832
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000833 bool tryMergeTemplateString() {
834 if (Tokens.size() < 2)
835 return false;
836
837 FormatToken *EndBacktick = Tokens.back();
Daniel Jasperf69b9222015-05-02 08:05:38 +0000838 // Backticks get lexed as tok::unknown tokens. If a template string contains
Daniel Jasper0d6ac272015-04-16 08:20:51 +0000839 // a comment start, it gets lexed as a tok::comment, or tok::unknown if
840 // unterminated.
Daniel Jasper2ebb0c52015-06-14 07:16:57 +0000841 if (!EndBacktick->isOneOf(tok::comment, tok::string_literal,
842 tok::char_constant, tok::unknown))
Daniel Jasper0d6ac272015-04-16 08:20:51 +0000843 return false;
844 size_t CommentBacktickPos = EndBacktick->TokenText.find('`');
845 // Unknown token that's not actually a backtick, or a comment that doesn't
846 // contain a backtick.
847 if (CommentBacktickPos == StringRef::npos)
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000848 return false;
849
850 unsigned TokenCount = 0;
851 bool IsMultiline = false;
Daniel Jasperf69b9222015-05-02 08:05:38 +0000852 unsigned EndColumnInFirstLine =
853 EndBacktick->OriginalColumn + EndBacktick->ColumnWidth;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000854 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; I++) {
855 ++TokenCount;
Daniel Jasper553a5b02015-07-02 13:08:28 +0000856 if (I[0]->IsMultiline)
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000857 IsMultiline = true;
858
859 // If there was a preceding template string, this must be the start of a
860 // template string, not the end.
861 if (I[0]->is(TT_TemplateString))
862 return false;
863
864 if (I[0]->isNot(tok::unknown) || I[0]->TokenText != "`") {
865 // Keep track of the rhs offset of the last token to wrap across lines -
866 // its the rhs offset of the first line of the template string, used to
867 // determine its width.
868 if (I[0]->IsMultiline)
869 EndColumnInFirstLine = I[0]->OriginalColumn + I[0]->ColumnWidth;
870 // If the token has newlines, the token before it (if it exists) is the
871 // rhs end of the previous line.
Daniel Jasper553a5b02015-07-02 13:08:28 +0000872 if (I[0]->NewlinesBefore > 0 && (I + 1 != E)) {
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000873 EndColumnInFirstLine = I[1]->OriginalColumn + I[1]->ColumnWidth;
Daniel Jasper553a5b02015-07-02 13:08:28 +0000874 IsMultiline = true;
875 }
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000876 continue;
877 }
878
879 Tokens.resize(Tokens.size() - TokenCount);
880 Tokens.back()->Type = TT_TemplateString;
Daniel Jasper0d6ac272015-04-16 08:20:51 +0000881 const char *EndOffset =
882 EndBacktick->TokenText.data() + 1 + CommentBacktickPos;
883 if (CommentBacktickPos != 0) {
884 // If the backtick was not the first character (e.g. in a comment),
885 // re-lex after the backtick position.
886 SourceLocation Loc = EndBacktick->Tok.getLocation();
887 resetLexer(SourceMgr.getFileOffset(Loc) + CommentBacktickPos + 1);
888 }
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000889 Tokens.back()->TokenText =
890 StringRef(Tokens.back()->TokenText.data(),
891 EndOffset - Tokens.back()->TokenText.data());
Daniel Jasperf69b9222015-05-02 08:05:38 +0000892
893 unsigned EndOriginalColumn = EndBacktick->OriginalColumn;
894 if (EndOriginalColumn == 0) {
895 SourceLocation Loc = EndBacktick->Tok.getLocation();
896 EndOriginalColumn = SourceMgr.getSpellingColumnNumber(Loc);
897 }
898 // If the ` is further down within the token (e.g. in a comment).
899 EndOriginalColumn += CommentBacktickPos;
900
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000901 if (IsMultiline) {
902 // ColumnWidth is from backtick to last token in line.
903 // LastLineColumnWidth is 0 to backtick.
904 // x = `some content
905 // until here`;
906 Tokens.back()->ColumnWidth =
907 EndColumnInFirstLine - Tokens.back()->OriginalColumn;
Daniel Jasper553a5b02015-07-02 13:08:28 +0000908 // +1 for the ` itself.
909 Tokens.back()->LastLineColumnWidth = EndOriginalColumn + 1;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000910 Tokens.back()->IsMultiline = true;
911 } else {
912 // Token simply spans from start to end, +1 for the ` itself.
913 Tokens.back()->ColumnWidth =
Daniel Jasperf69b9222015-05-02 08:05:38 +0000914 EndOriginalColumn - Tokens.back()->OriginalColumn + 1;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000915 }
916 return true;
917 }
918 return false;
919 }
920
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000921 bool tryMerge_TMacro() {
Alexander Kornienko81e32942013-09-16 20:20:49 +0000922 if (Tokens.size() < 4)
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000923 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +0000924 FormatToken *Last = Tokens.back();
925 if (!Last->is(tok::r_paren))
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000926 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +0000927
928 FormatToken *String = Tokens[Tokens.size() - 2];
929 if (!String->is(tok::string_literal) || String->IsMultiline)
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000930 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +0000931
932 if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000933 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +0000934
935 FormatToken *Macro = Tokens[Tokens.size() - 4];
936 if (Macro->TokenText != "_T")
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000937 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +0000938
939 const char *Start = Macro->TokenText.data();
940 const char *End = Last->TokenText.data() + Last->TokenText.size();
941 String->TokenText = StringRef(Start, End - Start);
942 String->IsFirst = Macro->IsFirst;
943 String->LastNewlineOffset = Macro->LastNewlineOffset;
944 String->WhitespaceRange = Macro->WhitespaceRange;
945 String->OriginalColumn = Macro->OriginalColumn;
946 String->ColumnWidth = encoding::columnWidthWithTabs(
947 String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding);
Daniel Jaspere99c72f2015-03-26 14:47:35 +0000948 String->NewlinesBefore = Macro->NewlinesBefore;
949 String->HasUnescapedNewline = Macro->HasUnescapedNewline;
Alexander Kornienko81e32942013-09-16 20:20:49 +0000950
951 Tokens.pop_back();
952 Tokens.pop_back();
953 Tokens.pop_back();
954 Tokens.back() = String;
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000955 return true;
Alexander Kornienko81e32942013-09-16 20:20:49 +0000956 }
957
Manuel Klimek68b03042014-04-14 09:14:11 +0000958 bool tryMergeConflictMarkers() {
959 if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof))
960 return false;
961
962 // Conflict lines look like:
963 // <marker> <text from the vcs>
964 // For example:
965 // >>>>>>> /file/in/file/system at revision 1234
966 //
967 // We merge all tokens in a line that starts with a conflict marker
968 // into a single token with a special token type that the unwrapped line
969 // parser will use to correctly rebuild the underlying code.
970
971 FileID ID;
972 // Get the position of the first token in the line.
973 unsigned FirstInLineOffset;
974 std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc(
975 Tokens[FirstInLineIndex]->getStartOfNonWhitespace());
976 StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer();
977 // Calculate the offset of the start of the current line.
978 auto LineOffset = Buffer.rfind('\n', FirstInLineOffset);
979 if (LineOffset == StringRef::npos) {
980 LineOffset = 0;
981 } else {
982 ++LineOffset;
983 }
984
985 auto FirstSpace = Buffer.find_first_of(" \n", LineOffset);
986 StringRef LineStart;
987 if (FirstSpace == StringRef::npos) {
988 LineStart = Buffer.substr(LineOffset);
989 } else {
990 LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset);
991 }
992
993 TokenType Type = TT_Unknown;
994 if (LineStart == "<<<<<<<" || LineStart == ">>>>") {
995 Type = TT_ConflictStart;
996 } else if (LineStart == "|||||||" || LineStart == "=======" ||
997 LineStart == "====") {
998 Type = TT_ConflictAlternative;
999 } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") {
1000 Type = TT_ConflictEnd;
1001 }
1002
1003 if (Type != TT_Unknown) {
1004 FormatToken *Next = Tokens.back();
1005
1006 Tokens.resize(FirstInLineIndex + 1);
1007 // We do not need to build a complete token here, as we will skip it
1008 // during parsing anyway (as we must not touch whitespace around conflict
1009 // markers).
1010 Tokens.back()->Type = Type;
1011 Tokens.back()->Tok.setKind(tok::kw___unknown_anytype);
1012
1013 Tokens.push_back(Next);
1014 return true;
1015 }
1016
1017 return false;
1018 }
1019
Jacques Pienaarfc275112015-02-18 23:48:37 +00001020 FormatToken *getStashedToken() {
1021 // Create a synthesized second '>' or '<' token.
1022 Token Tok = FormatTok->Tok;
1023 StringRef TokenText = FormatTok->TokenText;
1024
1025 unsigned OriginalColumn = FormatTok->OriginalColumn;
1026 FormatTok = new (Allocator.Allocate()) FormatToken;
1027 FormatTok->Tok = Tok;
1028 SourceLocation TokLocation =
Jacques Pienaar411b2512015-02-24 23:23:24 +00001029 FormatTok->Tok.getLocation().getLocWithOffset(Tok.getLength() - 1);
1030 FormatTok->Tok.setLocation(TokLocation);
Jacques Pienaarfc275112015-02-18 23:48:37 +00001031 FormatTok->WhitespaceRange = SourceRange(TokLocation, TokLocation);
1032 FormatTok->TokenText = TokenText;
1033 FormatTok->ColumnWidth = 1;
Jacques Pienaar411b2512015-02-24 23:23:24 +00001034 FormatTok->OriginalColumn = OriginalColumn + 1;
1035
Jacques Pienaarfc275112015-02-18 23:48:37 +00001036 return FormatTok;
1037 }
1038
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001039 FormatToken *getNextToken() {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001040 if (GreaterStashed) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001041 GreaterStashed = false;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001042 return getStashedToken();
1043 }
1044 if (LessStashed) {
1045 LessStashed = false;
1046 return getStashedToken();
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001047 }
1048
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001049 FormatTok = new (Allocator.Allocate()) FormatToken;
Daniel Jasper8369aa52013-07-16 20:28:33 +00001050 readRawToken(*FormatTok);
Manuel Klimek9043c742013-05-27 15:23:34 +00001051 SourceLocation WhitespaceStart =
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001052 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
Alexander Kornienko393e3082013-11-13 14:04:17 +00001053 FormatTok->IsFirst = IsFirstToken;
1054 IsFirstToken = false;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001055
1056 // Consume and record whitespace until we find a significant token.
Manuel Klimek9043c742013-05-27 15:23:34 +00001057 unsigned WhitespaceLength = TrailingWhitespace;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001058 while (FormatTok->Tok.is(tok::unknown)) {
Daniel Jaspere2408e32015-05-06 11:16:43 +00001059 StringRef Text = FormatTok->TokenText;
1060 auto EscapesNewline = [&](int pos) {
1061 // A '\r' here is just part of '\r\n'. Skip it.
1062 if (pos >= 0 && Text[pos] == '\r')
1063 --pos;
1064 // See whether there is an odd number of '\' before this.
1065 unsigned count = 0;
1066 for (; pos >= 0; --pos, ++count)
Daniel Jasperf0fd1c62015-05-10 08:00:25 +00001067 if (Text[pos] != '\\')
Daniel Jaspere2408e32015-05-06 11:16:43 +00001068 break;
1069 return count & 1;
1070 };
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001071 // FIXME: This miscounts tok:unknown tokens that are not just
1072 // whitespace, e.g. a '`' character.
Daniel Jaspere2408e32015-05-06 11:16:43 +00001073 for (int i = 0, e = Text.size(); i != e; ++i) {
1074 switch (Text[i]) {
Manuel Klimek31c85922013-08-29 15:21:40 +00001075 case '\n':
1076 ++FormatTok->NewlinesBefore;
Daniel Jaspere2408e32015-05-06 11:16:43 +00001077 FormatTok->HasUnescapedNewline = !EscapesNewline(i - 1);
Manuel Klimek31c85922013-08-29 15:21:40 +00001078 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
1079 Column = 0;
1080 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001081 case '\r':
Daniel Jasper30029c62015-02-05 11:05:31 +00001082 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
1083 Column = 0;
1084 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001085 case '\f':
1086 case '\v':
1087 Column = 0;
1088 break;
Manuel Klimek31c85922013-08-29 15:21:40 +00001089 case ' ':
1090 ++Column;
1091 break;
1092 case '\t':
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +00001093 Column += Style.TabWidth - Column % Style.TabWidth;
Manuel Klimek31c85922013-08-29 15:21:40 +00001094 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001095 case '\\':
Daniel Jaspere2408e32015-05-06 11:16:43 +00001096 if (i + 1 == e || (Text[i + 1] != '\r' && Text[i + 1] != '\n'))
Daniel Jasper877615c2013-10-11 19:45:02 +00001097 FormatTok->Type = TT_ImplicitStringLiteral;
1098 break;
Manuel Klimek31c85922013-08-29 15:21:40 +00001099 default:
Daniel Jasper877615c2013-10-11 19:45:02 +00001100 FormatTok->Type = TT_ImplicitStringLiteral;
Manuel Klimek31c85922013-08-29 15:21:40 +00001101 break;
1102 }
1103 }
1104
Daniel Jaspera98b7b02014-11-25 10:05:17 +00001105 if (FormatTok->is(TT_ImplicitStringLiteral))
Daniel Jasper877615c2013-10-11 19:45:02 +00001106 break;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001107 WhitespaceLength += FormatTok->Tok.getLength();
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001108
Daniel Jasper8369aa52013-07-16 20:28:33 +00001109 readRawToken(*FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001110 }
Manuel Klimekef920692013-01-07 07:56:50 +00001111
Manuel Klimek1abf7892013-01-04 23:34:14 +00001112 // In case the token starts with escaped newlines, we want to
1113 // take them into account as whitespace - this pattern is quite frequent
1114 // in macro definitions.
Manuel Klimek1abf7892013-01-04 23:34:14 +00001115 // FIXME: Add a more explicit test.
Daniel Jasper8369aa52013-07-16 20:28:33 +00001116 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
1117 FormatTok->TokenText[1] == '\n') {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001118 ++FormatTok->NewlinesBefore;
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001119 WhitespaceLength += 2;
Daniel Jaspere2408e32015-05-06 11:16:43 +00001120 FormatTok->LastNewlineOffset = 2;
Manuel Klimek31c85922013-08-29 15:21:40 +00001121 Column = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +00001122 FormatTok->TokenText = FormatTok->TokenText.substr(2);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001123 }
Alexander Kornienko39856b72013-09-10 09:38:25 +00001124
1125 FormatTok->WhitespaceRange = SourceRange(
1126 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
1127
Manuel Klimek31c85922013-08-29 15:21:40 +00001128 FormatTok->OriginalColumn = Column;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001129
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001130 TrailingWhitespace = 0;
1131 if (FormatTok->Tok.is(tok::comment)) {
Manuel Klimek31c85922013-08-29 15:21:40 +00001132 // FIXME: Add the trimmed whitespace to Column.
Daniel Jasper8369aa52013-07-16 20:28:33 +00001133 StringRef UntrimmedText = FormatTok->TokenText;
Alexander Kornienko9ab4a772013-09-06 17:24:54 +00001134 FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
Daniel Jasper8369aa52013-07-16 20:28:33 +00001135 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001136 } else if (FormatTok->Tok.is(tok::raw_identifier)) {
Daniel Jasper8369aa52013-07-16 20:28:33 +00001137 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001138 FormatTok->Tok.setIdentifierInfo(&Info);
1139 FormatTok->Tok.setKind(Info.getTokenID());
Daniel Jasperfe2cf662014-11-19 14:11:11 +00001140 if (Style.Language == FormatStyle::LK_Java &&
1141 FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete)) {
1142 FormatTok->Tok.setKind(tok::identifier);
1143 FormatTok->Tok.setIdentifierInfo(nullptr);
1144 }
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001145 } else if (FormatTok->Tok.is(tok::greatergreater)) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001146 FormatTok->Tok.setKind(tok::greater);
Daniel Jasper8369aa52013-07-16 20:28:33 +00001147 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001148 GreaterStashed = true;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001149 } else if (FormatTok->Tok.is(tok::lessless)) {
1150 FormatTok->Tok.setKind(tok::less);
1151 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
1152 LessStashed = true;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001153 }
1154
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001155 // Now FormatTok is the next non-whitespace token.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001156
Alexander Kornienko39856b72013-09-10 09:38:25 +00001157 StringRef Text = FormatTok->TokenText;
1158 size_t FirstNewlinePos = Text.find('\n');
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001159 if (FirstNewlinePos == StringRef::npos) {
1160 // FIXME: ColumnWidth actually depends on the start column, we need to
1161 // take this into account when the token is moved.
1162 FormatTok->ColumnWidth =
1163 encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding);
1164 Column += FormatTok->ColumnWidth;
1165 } else {
Alexander Kornienko39856b72013-09-10 09:38:25 +00001166 FormatTok->IsMultiline = true;
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001167 // FIXME: ColumnWidth actually depends on the start column, we need to
1168 // take this into account when the token is moved.
1169 FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
1170 Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
1171
Alexander Kornienko39856b72013-09-10 09:38:25 +00001172 // The last line of the token always starts in column 0.
1173 // Thus, the length can be precomputed even in the presence of tabs.
1174 FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
1175 Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth,
1176 Encoding);
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001177 Column = FormatTok->LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +00001178 }
Alexander Kornienko39856b72013-09-10 09:38:25 +00001179
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001180 if (Style.Language == FormatStyle::LK_Cpp) {
1181 if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
1182 Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
1183 tok::pp_define) &&
1184 std::find(ForEachMacros.begin(), ForEachMacros.end(),
1185 FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end()) {
1186 FormatTok->Type = TT_ForEachMacro;
1187 } else if (FormatTok->is(tok::identifier)) {
1188 if (MacroBlockBeginRegex.match(Text)) {
1189 FormatTok->Type = TT_MacroBlockBegin;
1190 } else if (MacroBlockEndRegex.match(Text)) {
1191 FormatTok->Type = TT_MacroBlockEnd;
1192 }
1193 }
1194 }
Daniel Jaspere1e43192014-04-01 12:55:11 +00001195
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001196 return FormatTok;
1197 }
1198
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001199 FormatToken *FormatTok;
Alexander Kornienko393e3082013-11-13 14:04:17 +00001200 bool IsFirstToken;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001201 bool GreaterStashed, LessStashed;
Manuel Klimek31c85922013-08-29 15:21:40 +00001202 unsigned Column;
Manuel Klimek9043c742013-05-27 15:23:34 +00001203 unsigned TrailingWhitespace;
Daniel Jasper23376252014-09-09 14:37:39 +00001204 std::unique_ptr<Lexer> Lex;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001205 SourceManager &SourceMgr;
Daniel Jasper23376252014-09-09 14:37:39 +00001206 FileID ID;
Manuel Klimek31c85922013-08-29 15:21:40 +00001207 FormatStyle &Style;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001208 IdentifierTable IdentTable;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001209 AdditionalKeywords Keywords;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001210 encoding::Encoding Encoding;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001211 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
Manuel Klimek68b03042014-04-14 09:14:11 +00001212 // Index (in 'Tokens') of the last token that starts a new line.
1213 unsigned FirstInLineIndex;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001214 SmallVector<FormatToken *, 16> Tokens;
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001215 SmallVector<IdentifierInfo *, 8> ForEachMacros;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001216
NAKAMURA Takumi7160c4d2014-08-06 16:53:13 +00001217 bool FormattingDisabled;
Daniel Jasper471894432014-08-06 13:40:26 +00001218
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001219 llvm::Regex MacroBlockBeginRegex;
1220 llvm::Regex MacroBlockEndRegex;
1221
Daniel Jasper8369aa52013-07-16 20:28:33 +00001222 void readRawToken(FormatToken &Tok) {
Daniel Jasper23376252014-09-09 14:37:39 +00001223 Lex->LexFromRawLexer(Tok.Tok);
Daniel Jasper8369aa52013-07-16 20:28:33 +00001224 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
1225 Tok.Tok.getLength());
Daniel Jasper8369aa52013-07-16 20:28:33 +00001226 // For formatting, treat unterminated string literals like normal string
1227 // literals.
Daniel Jasper86fee2f2014-01-31 12:49:42 +00001228 if (Tok.is(tok::unknown)) {
1229 if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') {
1230 Tok.Tok.setKind(tok::string_literal);
1231 Tok.IsUnterminatedLiteral = true;
1232 } else if (Style.Language == FormatStyle::LK_JavaScript &&
1233 Tok.TokenText == "''") {
1234 Tok.Tok.setKind(tok::char_constant);
1235 }
Daniel Jasper8369aa52013-07-16 20:28:33 +00001236 }
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001237
1238 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" ||
1239 Tok.TokenText == "/* clang-format on */")) {
Daniel Jasper471894432014-08-06 13:40:26 +00001240 FormattingDisabled = false;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001241 }
1242
Daniel Jasper471894432014-08-06 13:40:26 +00001243 Tok.Finalized = FormattingDisabled;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001244
1245 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" ||
1246 Tok.TokenText == "/* clang-format off */")) {
Daniel Jasper471894432014-08-06 13:40:26 +00001247 FormattingDisabled = true;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001248 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001249 }
Daniel Jasper49a9a282014-10-29 16:51:38 +00001250
1251 void resetLexer(unsigned Offset) {
1252 StringRef Buffer = SourceMgr.getBufferData(ID);
1253 Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID),
1254 getFormattingLangOpts(Style), Buffer.begin(),
1255 Buffer.begin() + Offset, Buffer.end()));
1256 Lex->SetKeepWhitespaceMode(true);
Daniel Jasper55c384e2015-07-02 14:01:34 +00001257 TrailingWhitespace = 0;
Daniel Jasper49a9a282014-10-29 16:51:38 +00001258 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001259};
1260
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001261static StringRef getLanguageName(FormatStyle::LanguageKind Language) {
1262 switch (Language) {
1263 case FormatStyle::LK_Cpp:
1264 return "C++";
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001265 case FormatStyle::LK_Java:
1266 return "Java";
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001267 case FormatStyle::LK_JavaScript:
1268 return "JavaScript";
Daniel Jasper7052ce62014-01-19 09:04:08 +00001269 case FormatStyle::LK_Proto:
1270 return "Proto";
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001271 default:
1272 return "Unknown";
1273 }
1274}
1275
Daniel Jasperf7935112012-12-03 18:12:45 +00001276class Formatter : public UnwrappedLineConsumer {
1277public:
Daniel Jasper23376252014-09-09 14:37:39 +00001278 Formatter(const FormatStyle &Style, SourceManager &SourceMgr, FileID ID,
Benjamin Kramerd0eed3a2014-10-03 18:52:48 +00001279 ArrayRef<CharSourceRange> Ranges)
Daniel Jasper23376252014-09-09 14:37:39 +00001280 : Style(Style), ID(ID), SourceMgr(SourceMgr),
1281 Whitespaces(SourceMgr, Style,
1282 inputUsesCRLF(SourceMgr.getBufferData(ID))),
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001283 Ranges(Ranges.begin(), Ranges.end()), UnwrappedLines(1),
Daniel Jasper23376252014-09-09 14:37:39 +00001284 Encoding(encoding::detectEncoding(SourceMgr.getBufferData(ID))) {
Daniel Jasperfa21c072013-07-15 14:33:14 +00001285 DEBUG(llvm::dbgs() << "File encoding: "
1286 << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
1287 : "unknown")
1288 << "\n");
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001289 DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language)
1290 << "\n");
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001291 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001292
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001293 tooling::Replacements format(bool *IncompleteFormat) {
Manuel Klimek71814b42013-10-11 21:25:45 +00001294 tooling::Replacements Result;
Daniel Jasper23376252014-09-09 14:37:39 +00001295 FormatTokenLexer Tokens(SourceMgr, ID, Style, Encoding);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001296
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001297 UnwrappedLineParser Parser(Style, Tokens.getKeywords(), Tokens.lex(),
1298 *this);
Manuel Klimek20e0af62015-05-06 11:56:29 +00001299 Parser.parse();
Manuel Klimek71814b42013-10-11 21:25:45 +00001300 assert(UnwrappedLines.rbegin()->empty());
1301 for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE;
1302 ++Run) {
1303 DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
1304 SmallVector<AnnotatedLine *, 16> AnnotatedLines;
1305 for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) {
1306 AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i]));
1307 }
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001308 tooling::Replacements RunResult =
1309 format(AnnotatedLines, Tokens, IncompleteFormat);
Manuel Klimek71814b42013-10-11 21:25:45 +00001310 DEBUG({
1311 llvm::dbgs() << "Replacements for run " << Run << ":\n";
1312 for (tooling::Replacements::iterator I = RunResult.begin(),
1313 E = RunResult.end();
1314 I != E; ++I) {
1315 llvm::dbgs() << I->toString() << "\n";
1316 }
1317 });
1318 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1319 delete AnnotatedLines[i];
1320 }
1321 Result.insert(RunResult.begin(), RunResult.end());
1322 Whitespaces.reset();
1323 }
1324 return Result;
1325 }
1326
1327 tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001328 FormatTokenLexer &Tokens,
1329 bool *IncompleteFormat) {
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001330 TokenAnnotator Annotator(Style, Tokens.getKeywords());
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001331 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001332 Annotator.annotate(*AnnotatedLines[i]);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001333 }
Manuel Klimek71814b42013-10-11 21:25:45 +00001334 deriveLocalStyle(AnnotatedLines);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001335 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001336 Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001337 }
Daniel Jasper5500f612013-11-25 11:08:59 +00001338 computeAffectedLines(AnnotatedLines.begin(), AnnotatedLines.end());
Daniel Jasperb67cc422013-04-09 17:46:55 +00001339
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001340 Annotator.setCommentLineLevels(AnnotatedLines);
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001341 ContinuationIndenter Indenter(Style, Tokens.getKeywords(), SourceMgr,
1342 Whitespaces, Encoding,
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001343 BinPackInconclusiveFunctions);
Manuel Klimekd3585db2015-05-11 08:21:35 +00001344 UnwrappedLineFormatter(&Indenter, &Whitespaces, Style, Tokens.getKeywords(),
1345 IncompleteFormat)
1346 .format(AnnotatedLines);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001347 return Whitespaces.generateReplacements();
1348 }
1349
1350private:
Daniel Jasper5500f612013-11-25 11:08:59 +00001351 // Determines which lines are affected by the SourceRanges given as input.
Daniel Jasper9c199562013-11-28 15:58:55 +00001352 // Returns \c true if at least one line between I and E or one of their
1353 // children is affected.
Daniel Jasper5500f612013-11-25 11:08:59 +00001354 bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I,
1355 SmallVectorImpl<AnnotatedLine *>::iterator E) {
1356 bool SomeLineAffected = false;
Craig Topper2145bc02014-05-09 08:15:10 +00001357 const AnnotatedLine *PreviousLine = nullptr;
Daniel Jasper5500f612013-11-25 11:08:59 +00001358 while (I != E) {
1359 AnnotatedLine *Line = *I;
1360 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
1361
1362 // If a line is part of a preprocessor directive, it needs to be formatted
1363 // if any token within the directive is affected.
1364 if (Line->InPPDirective) {
1365 FormatToken *Last = Line->Last;
1366 SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1;
1367 while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) {
1368 Last = (*PPEnd)->Last;
1369 ++PPEnd;
1370 }
1371
1372 if (affectsTokenRange(*Line->First, *Last,
1373 /*IncludeLeadingNewlines=*/false)) {
1374 SomeLineAffected = true;
1375 markAllAsAffected(I, PPEnd);
1376 }
1377 I = PPEnd;
1378 continue;
1379 }
1380
Daniel Jasper38c82402013-11-29 09:27:43 +00001381 if (nonPPLineAffected(Line, PreviousLine))
Daniel Jasper5500f612013-11-25 11:08:59 +00001382 SomeLineAffected = true;
Daniel Jasper5500f612013-11-25 11:08:59 +00001383
Daniel Jasper38c82402013-11-29 09:27:43 +00001384 PreviousLine = Line;
Daniel Jasper5500f612013-11-25 11:08:59 +00001385 ++I;
1386 }
1387 return SomeLineAffected;
1388 }
1389
Daniel Jasper9c199562013-11-28 15:58:55 +00001390 // Determines whether 'Line' is affected by the SourceRanges given as input.
1391 // Returns \c true if line or one if its children is affected.
Daniel Jasper38c82402013-11-29 09:27:43 +00001392 bool nonPPLineAffected(AnnotatedLine *Line,
1393 const AnnotatedLine *PreviousLine) {
Daniel Jasper9c199562013-11-28 15:58:55 +00001394 bool SomeLineAffected = false;
1395 Line->ChildrenAffected =
1396 computeAffectedLines(Line->Children.begin(), Line->Children.end());
1397 if (Line->ChildrenAffected)
1398 SomeLineAffected = true;
1399
1400 // Stores whether one of the line's tokens is directly affected.
1401 bool SomeTokenAffected = false;
1402 // Stores whether we need to look at the leading newlines of the next token
1403 // in order to determine whether it was affected.
1404 bool IncludeLeadingNewlines = false;
1405
1406 // Stores whether the first child line of any of this line's tokens is
1407 // affected.
1408 bool SomeFirstChildAffected = false;
1409
1410 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
1411 // Determine whether 'Tok' was affected.
1412 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
1413 SomeTokenAffected = true;
1414
1415 // Determine whether the first child of 'Tok' was affected.
1416 if (!Tok->Children.empty() && Tok->Children.front()->Affected)
1417 SomeFirstChildAffected = true;
1418
1419 IncludeLeadingNewlines = Tok->Children.empty();
1420 }
1421
1422 // Was this line moved, i.e. has it previously been on the same line as an
1423 // affected line?
Daniel Jasper38c82402013-11-29 09:27:43 +00001424 bool LineMoved = PreviousLine && PreviousLine->Affected &&
1425 Line->First->NewlinesBefore == 0;
Daniel Jasper9c199562013-11-28 15:58:55 +00001426
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001427 bool IsContinuedComment =
1428 Line->First->is(tok::comment) && Line->First->Next == nullptr &&
1429 Line->First->NewlinesBefore < 2 && PreviousLine &&
1430 PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
Daniel Jasper38c82402013-11-29 09:27:43 +00001431
1432 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
1433 IsContinuedComment) {
Daniel Jasper9c199562013-11-28 15:58:55 +00001434 Line->Affected = true;
Daniel Jasper9c199562013-11-28 15:58:55 +00001435 SomeLineAffected = true;
Daniel Jasper9c199562013-11-28 15:58:55 +00001436 }
1437 return SomeLineAffected;
1438 }
1439
Daniel Jasper5500f612013-11-25 11:08:59 +00001440 // Marks all lines between I and E as well as all their children as affected.
1441 void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I,
1442 SmallVectorImpl<AnnotatedLine *>::iterator E) {
1443 while (I != E) {
1444 (*I)->Affected = true;
1445 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
1446 ++I;
1447 }
1448 }
1449
1450 // Returns true if the range from 'First' to 'Last' intersects with one of the
1451 // input ranges.
1452 bool affectsTokenRange(const FormatToken &First, const FormatToken &Last,
1453 bool IncludeLeadingNewlines) {
1454 SourceLocation Start = First.WhitespaceRange.getBegin();
1455 if (!IncludeLeadingNewlines)
1456 Start = Start.getLocWithOffset(First.LastNewlineOffset);
Daniel Jasper5877bf12013-11-25 11:53:05 +00001457 SourceLocation End = Last.getStartOfNonWhitespace();
Daniel Jasperac29eac2014-10-29 23:40:50 +00001458 End = End.getLocWithOffset(Last.TokenText.size());
Daniel Jasper5500f612013-11-25 11:08:59 +00001459 CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
1460 return affectsCharSourceRange(Range);
1461 }
1462
1463 // Returns true if one of the input ranges intersect the leading empty lines
1464 // before 'Tok'.
1465 bool affectsLeadingEmptyLines(const FormatToken &Tok) {
1466 CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
1467 Tok.WhitespaceRange.getBegin(),
1468 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
1469 return affectsCharSourceRange(EmptyLineRange);
1470 }
1471
1472 // Returns true if 'Range' intersects with one of the input ranges.
1473 bool affectsCharSourceRange(const CharSourceRange &Range) {
1474 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
1475 E = Ranges.end();
1476 I != E; ++I) {
1477 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
1478 !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
1479 return true;
1480 }
1481 return false;
1482 }
1483
Alexander Kornienko9e649af2013-09-11 12:25:57 +00001484 static bool inputUsesCRLF(StringRef Text) {
1485 return Text.count('\r') * 2 > Text.count('\n');
1486 }
1487
Daniel Jasper352f0df2015-07-18 16:35:30 +00001488 bool
1489 hasCpp03IncompatibleFormat(const SmallVectorImpl<AnnotatedLine *> &Lines) {
1490 for (const AnnotatedLine* Line : Lines) {
1491 if (hasCpp03IncompatibleFormat(Line->Children))
1492 return true;
1493 for (FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next) {
1494 if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
1495 if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener))
1496 return true;
1497 if (Tok->is(TT_TemplateCloser) &&
1498 Tok->Previous->is(TT_TemplateCloser))
1499 return true;
1500 }
1501 }
1502 }
1503 return false;
1504 }
1505
1506 int countVariableAlignments(const SmallVectorImpl<AnnotatedLine *> &Lines) {
1507 int AlignmentDiff = 0;
1508 for (const AnnotatedLine* Line : Lines) {
1509 AlignmentDiff += countVariableAlignments(Line->Children);
1510 for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) {
1511 if (!Tok->is(TT_PointerOrReference))
1512 continue;
1513 bool SpaceBefore =
1514 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
1515 bool SpaceAfter = Tok->Next->WhitespaceRange.getBegin() !=
1516 Tok->Next->WhitespaceRange.getEnd();
1517 if (SpaceBefore && !SpaceAfter)
1518 ++AlignmentDiff;
1519 if (!SpaceBefore && SpaceAfter)
1520 --AlignmentDiff;
1521 }
1522 }
1523 return AlignmentDiff;
1524 }
1525
Manuel Klimek71814b42013-10-11 21:25:45 +00001526 void
1527 deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001528 bool HasBinPackedFunction = false;
1529 bool HasOnePerLineFunction = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001530 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001531 if (!AnnotatedLines[i]->First->Next)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001532 continue;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001533 FormatToken *Tok = AnnotatedLines[i]->First->Next;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001534 while (Tok->Next) {
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001535 if (Tok->PackingKind == PPK_BinPacked)
1536 HasBinPackedFunction = true;
1537 if (Tok->PackingKind == PPK_OnePerLine)
1538 HasOnePerLineFunction = true;
1539
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001540 Tok = Tok->Next;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001541 }
1542 }
Daniel Jasper352f0df2015-07-18 16:35:30 +00001543 if (Style.DerivePointerAlignment)
1544 Style.PointerAlignment = countVariableAlignments(AnnotatedLines) <= 0
1545 ? FormatStyle::PAS_Left
1546 : FormatStyle::PAS_Right;
1547 if (Style.Standard == FormatStyle::LS_Auto)
1548 Style.Standard = hasCpp03IncompatibleFormat(AnnotatedLines)
1549 ? FormatStyle::LS_Cpp11
1550 : FormatStyle::LS_Cpp03;
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001551 BinPackInconclusiveFunctions =
1552 HasBinPackedFunction || !HasOnePerLineFunction;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001553 }
1554
Craig Topperfb6b25b2014-03-15 04:29:04 +00001555 void consumeUnwrappedLine(const UnwrappedLine &TheLine) override {
Manuel Klimek71814b42013-10-11 21:25:45 +00001556 assert(!UnwrappedLines.empty());
1557 UnwrappedLines.back().push_back(TheLine);
1558 }
1559
Craig Topperfb6b25b2014-03-15 04:29:04 +00001560 void finishRun() override {
Manuel Klimek71814b42013-10-11 21:25:45 +00001561 UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
Daniel Jasperf7935112012-12-03 18:12:45 +00001562 }
1563
1564 FormatStyle Style;
Daniel Jasper23376252014-09-09 14:37:39 +00001565 FileID ID;
Daniel Jasperf7935112012-12-03 18:12:45 +00001566 SourceManager &SourceMgr;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001567 WhitespaceManager Whitespaces;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001568 SmallVector<CharSourceRange, 8> Ranges;
Manuel Klimek71814b42013-10-11 21:25:45 +00001569 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001570
1571 encoding::Encoding Encoding;
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001572 bool BinPackInconclusiveFunctions;
Daniel Jasperf7935112012-12-03 18:12:45 +00001573};
1574
Craig Topperaf35e852013-06-30 22:29:28 +00001575} // end anonymous namespace
1576
Daniel Jasper23376252014-09-09 14:37:39 +00001577tooling::Replacements reformat(const FormatStyle &Style,
1578 SourceManager &SourceMgr, FileID ID,
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001579 ArrayRef<CharSourceRange> Ranges,
1580 bool *IncompleteFormat) {
Daniel Jasper23376252014-09-09 14:37:39 +00001581 if (Style.DisableFormat)
1582 return tooling::Replacements();
1583 Formatter formatter(Style, SourceMgr, ID, Ranges);
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001584 return formatter.format(IncompleteFormat);
Daniel Jasperf7935112012-12-03 18:12:45 +00001585}
1586
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001587tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
Benjamin Kramerd0eed3a2014-10-03 18:52:48 +00001588 ArrayRef<tooling::Range> Ranges,
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001589 StringRef FileName, bool *IncompleteFormat) {
Daniel Jasper23376252014-09-09 14:37:39 +00001590 if (Style.DisableFormat)
1591 return tooling::Replacements();
1592
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001593 FileManager Files((FileSystemOptions()));
1594 DiagnosticsEngine Diagnostics(
1595 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1596 new DiagnosticOptions);
1597 SourceManager SourceMgr(Diagnostics, Files);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001598 std::unique_ptr<llvm::MemoryBuffer> Buf =
1599 llvm::MemoryBuffer::getMemBuffer(Code, FileName);
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001600 const clang::FileEntry *Entry =
1601 Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
David Blaikie49cc3182014-08-27 20:54:45 +00001602 SourceMgr.overrideFileContents(Entry, std::move(Buf));
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001603 FileID ID =
1604 SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001605 SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
1606 std::vector<CharSourceRange> CharRanges;
Benjamin Kramerd0eed3a2014-10-03 18:52:48 +00001607 for (const tooling::Range &Range : Ranges) {
1608 SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset());
1609 SourceLocation End = Start.getLocWithOffset(Range.getLength());
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001610 CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1611 }
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001612 return reformat(Style, SourceMgr, ID, CharRanges, IncompleteFormat);
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001613}
1614
Daniel Jasper4db69bd2014-09-04 18:23:42 +00001615LangOptions getFormattingLangOpts(const FormatStyle &Style) {
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001616 LangOptions LangOpts;
1617 LangOpts.CPlusPlus = 1;
Daniel Jasper4db69bd2014-09-04 18:23:42 +00001618 LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
1619 LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
Daniel Jasper55213652013-03-22 10:01:29 +00001620 LangOpts.LineComment = 1;
Daniel Jasper1662bfe2015-04-03 21:15:46 +00001621 bool AlternativeOperators = Style.Language == FormatStyle::LK_Cpp;
Daniel Jasper30a24062014-11-14 09:02:28 +00001622 LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001623 LangOpts.Bool = 1;
1624 LangOpts.ObjC1 = 1;
1625 LangOpts.ObjC2 = 1;
Nico Weberfac23712015-02-04 15:26:27 +00001626 LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001627 return LangOpts;
1628}
1629
Edwin Vaned544aa72013-09-30 13:31:48 +00001630const char *StyleOptionHelpDescription =
1631 "Coding style, currently supports:\n"
1632 " LLVM, Google, Chromium, Mozilla, WebKit.\n"
1633 "Use -style=file to load style configuration from\n"
1634 ".clang-format file located in one of the parent\n"
1635 "directories of the source file (or current\n"
1636 "directory for stdin).\n"
1637 "Use -style=\"{key: value, ...}\" to set specific\n"
1638 "parameters, e.g.:\n"
1639 " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
1640
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001641static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001642 if (FileName.endswith(".java")) {
1643 return FormatStyle::LK_Java;
Daniel Jasper8c68a642015-03-11 14:58:38 +00001644 } else if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts")) {
1645 // JavaScript or TypeScript.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001646 return FormatStyle::LK_JavaScript;
Daniel Jasper7052ce62014-01-19 09:04:08 +00001647 } else if (FileName.endswith_lower(".proto") ||
1648 FileName.endswith_lower(".protodevel")) {
1649 return FormatStyle::LK_Proto;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001650 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001651 return FormatStyle::LK_Cpp;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001652}
1653
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001654FormatStyle getStyle(StringRef StyleName, StringRef FileName,
1655 StringRef FallbackStyle) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001656 FormatStyle Style = getLLVMStyle();
1657 Style.Language = getLanguageByFileName(FileName);
1658 if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) {
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001659 llvm::errs() << "Invalid fallback style \"" << FallbackStyle
1660 << "\" using LLVM style\n";
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001661 return Style;
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001662 }
Edwin Vaned544aa72013-09-30 13:31:48 +00001663
1664 if (StyleName.startswith("{")) {
1665 // Parse YAML/JSON style from the command line.
Rafael Espindolac0809172014-06-12 14:02:15 +00001666 if (std::error_code ec = parseConfiguration(StyleName, &Style)) {
Alexander Kornienkoe2e03872013-10-14 00:46:35 +00001667 llvm::errs() << "Error parsing -style: " << ec.message() << ", using "
1668 << FallbackStyle << " style\n";
Edwin Vaned544aa72013-09-30 13:31:48 +00001669 }
1670 return Style;
1671 }
1672
1673 if (!StyleName.equals_lower("file")) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001674 if (!getPredefinedStyle(StyleName, Style.Language, &Style))
Edwin Vaned544aa72013-09-30 13:31:48 +00001675 llvm::errs() << "Invalid value for -style, using " << FallbackStyle
1676 << " style\n";
1677 return Style;
1678 }
1679
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001680 // Look for .clang-format/_clang-format file in the file's parent directories.
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001681 SmallString<128> UnsuitableConfigFiles;
Edwin Vaned544aa72013-09-30 13:31:48 +00001682 SmallString<128> Path(FileName);
1683 llvm::sys::fs::make_absolute(Path);
Alexander Kornienkoe2e03872013-10-14 00:46:35 +00001684 for (StringRef Directory = Path; !Directory.empty();
Edwin Vaned544aa72013-09-30 13:31:48 +00001685 Directory = llvm::sys::path::parent_path(Directory)) {
1686 if (!llvm::sys::fs::is_directory(Directory))
1687 continue;
1688 SmallString<128> ConfigFile(Directory);
1689
1690 llvm::sys::path::append(ConfigFile, ".clang-format");
1691 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1692 bool IsFile = false;
1693 // Ignore errors from is_regular_file: we only need to know if we can read
1694 // the file or not.
1695 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1696
1697 if (!IsFile) {
1698 // Try _clang-format too, since dotfiles are not commonly used on Windows.
1699 ConfigFile = Directory;
1700 llvm::sys::path::append(ConfigFile, "_clang-format");
1701 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1702 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1703 }
1704
1705 if (IsFile) {
Rafael Espindola2d2b4202014-07-06 17:43:24 +00001706 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
1707 llvm::MemoryBuffer::getFile(ConfigFile.c_str());
1708 if (std::error_code EC = Text.getError()) {
1709 llvm::errs() << EC.message() << "\n";
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001710 break;
Edwin Vaned544aa72013-09-30 13:31:48 +00001711 }
Rafael Espindola2d2b4202014-07-06 17:43:24 +00001712 if (std::error_code ec =
1713 parseConfiguration(Text.get()->getBuffer(), &Style)) {
Rafael Espindolad0136702014-06-12 02:50:04 +00001714 if (ec == ParseError::Unsuitable) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001715 if (!UnsuitableConfigFiles.empty())
1716 UnsuitableConfigFiles.append(", ");
1717 UnsuitableConfigFiles.append(ConfigFile);
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001718 continue;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001719 }
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001720 llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()
1721 << "\n";
1722 break;
Edwin Vaned544aa72013-09-30 13:31:48 +00001723 }
1724 DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
1725 return Style;
1726 }
1727 }
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001728 if (!UnsuitableConfigFiles.empty()) {
1729 llvm::errs() << "Configuration file(s) do(es) not support "
1730 << getLanguageName(Style.Language) << ": "
1731 << UnsuitableConfigFiles << "\n";
1732 }
Edwin Vaned544aa72013-09-30 13:31:48 +00001733 return Style;
1734}
1735
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001736} // namespace format
1737} // namespace clang