blob: 6370be903ddefe60db5f244538d878ab83f230a7 [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 Jasper85c472d2015-09-29 07:53:08 +000016#include "clang/Format/Format.h"
Eric Liu4cfb88a2016-04-25 15:09:22 +000017#include "AffectedRangeManager.h"
Daniel Jasperde0328a2013-08-16 11:20:30 +000018#include "ContinuationIndenter.h"
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000019#include "TokenAnnotator.h"
Daniel Jasper0df50932014-12-10 19:00:42 +000020#include "UnwrappedLineFormatter.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "UnwrappedLineParser.h"
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000022#include "WhitespaceManager.h"
Daniel Jasperec04c0d2013-05-16 10:40:07 +000023#include "clang/Basic/Diagnostic.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000024#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruth44eb4f62013-01-02 10:28:36 +000025#include "clang/Basic/SourceManager.h"
Marianne Mailhot-Sarrasin4988fa12016-04-14 14:47:37 +000026#include "clang/Basic/VirtualFileSystem.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000027#include "clang/Lex/Lexer.h"
Alexander Kornienkoffd6d042013-03-27 11:52:18 +000028#include "llvm/ADT/STLExtras.h"
Manuel Klimek2ef908e2013-02-13 10:46:36 +000029#include "llvm/Support/Allocator.h"
Manuel Klimek24998102013-01-16 14:55:28 +000030#include "llvm/Support/Debug.h"
Edwin Vaned544aa72013-09-30 13:31:48 +000031#include "llvm/Support/Path.h"
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +000032#include "llvm/Support/Regex.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000033#include "llvm/Support/YAMLTraits.h"
Eric Liu4cfb88a2016-04-25 15:09:22 +000034#include <memory>
Manuel Klimek2ef908e2013-02-13 10:46:36 +000035#include <queue>
Daniel Jasper8b529712012-12-04 13:02:32 +000036#include <string>
37
Chandler Carruth10346662014-04-22 03:17:02 +000038#define DEBUG_TYPE "format-formatter"
39
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000040using clang::format::FormatStyle;
41
Daniel Jaspere1e43192014-04-01 12:55:11 +000042LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string)
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +000043LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::IncludeCategory)
Daniel Jaspere1e43192014-04-01 12:55:11 +000044
Alexander Kornienkod6538332013-05-07 15:32:14 +000045namespace llvm {
46namespace yaml {
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000047template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> {
48 static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) {
49 IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp);
Daniel Jasperc58c70e2014-09-15 11:21:46 +000050 IO.enumCase(Value, "Java", FormatStyle::LK_Java);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000051 IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript);
Daniel Jasper7052ce62014-01-19 09:04:08 +000052 IO.enumCase(Value, "Proto", FormatStyle::LK_Proto);
Daniel Jasper498f5582015-12-25 08:53:31 +000053 IO.enumCase(Value, "TableGen", FormatStyle::LK_TableGen);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000054 }
55};
56
57template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> {
58 static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) {
59 IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03);
60 IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03);
61 IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11);
62 IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11);
63 IO.enumCase(Value, "Auto", FormatStyle::LS_Auto);
64 }
65};
66
67template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
68 static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) {
69 IO.enumCase(Value, "Never", FormatStyle::UT_Never);
70 IO.enumCase(Value, "false", FormatStyle::UT_Never);
71 IO.enumCase(Value, "Always", FormatStyle::UT_Always);
72 IO.enumCase(Value, "true", FormatStyle::UT_Always);
73 IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation);
Marianne Mailhot-Sarrasin51fe2792016-04-14 14:52:26 +000074 IO.enumCase(Value, "ForContinuationAndIndentation",
75 FormatStyle::UT_ForContinuationAndIndentation);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000076 }
77};
78
Daniel Jasperabd1f572016-03-02 22:44:03 +000079template <> struct ScalarEnumerationTraits<FormatStyle::JavaScriptQuoteStyle> {
80 static void enumeration(IO &IO, FormatStyle::JavaScriptQuoteStyle &Value) {
81 IO.enumCase(Value, "Leave", FormatStyle::JSQS_Leave);
82 IO.enumCase(Value, "Single", FormatStyle::JSQS_Single);
83 IO.enumCase(Value, "Double", FormatStyle::JSQS_Double);
84 }
85};
86
Daniel Jasperd74cf402014-04-08 12:46:38 +000087template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> {
88 static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) {
89 IO.enumCase(Value, "None", FormatStyle::SFS_None);
90 IO.enumCase(Value, "false", FormatStyle::SFS_None);
91 IO.enumCase(Value, "All", FormatStyle::SFS_All);
92 IO.enumCase(Value, "true", FormatStyle::SFS_All);
93 IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline);
Daniel Jasper9e709352014-11-26 10:43:58 +000094 IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty);
Daniel Jasperd74cf402014-04-08 12:46:38 +000095 }
96};
97
Daniel Jasperac043c92014-09-15 11:11:00 +000098template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> {
99 static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) {
100 IO.enumCase(Value, "All", FormatStyle::BOS_All);
101 IO.enumCase(Value, "true", FormatStyle::BOS_All);
102 IO.enumCase(Value, "None", FormatStyle::BOS_None);
103 IO.enumCase(Value, "false", FormatStyle::BOS_None);
104 IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment);
105 }
106};
107
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000108template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
109 static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) {
110 IO.enumCase(Value, "Attach", FormatStyle::BS_Attach);
111 IO.enumCase(Value, "Linux", FormatStyle::BS_Linux);
Birunthan Mohanathas305fa9c2015-07-12 03:13:54 +0000112 IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000113 IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
114 IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000115 IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
Roman Kashitsyn291f64f2015-08-10 13:43:19 +0000116 IO.enumCase(Value, "WebKit", FormatStyle::BS_WebKit);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000117 IO.enumCase(Value, "Custom", FormatStyle::BS_Custom);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000118 }
119};
120
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000121template <>
Zachary Turner448592e2015-12-18 22:20:15 +0000122struct ScalarEnumerationTraits<FormatStyle::ReturnTypeBreakingStyle> {
123 static void enumeration(IO &IO, FormatStyle::ReturnTypeBreakingStyle &Value) {
124 IO.enumCase(Value, "None", FormatStyle::RTBS_None);
125 IO.enumCase(Value, "All", FormatStyle::RTBS_All);
126 IO.enumCase(Value, "TopLevel", FormatStyle::RTBS_TopLevel);
127 IO.enumCase(Value, "TopLevelDefinitions",
128 FormatStyle::RTBS_TopLevelDefinitions);
129 IO.enumCase(Value, "AllDefinitions", FormatStyle::RTBS_AllDefinitions);
130 }
131};
132
133template <>
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000134struct ScalarEnumerationTraits<FormatStyle::DefinitionReturnTypeBreakingStyle> {
135 static void
136 enumeration(IO &IO, FormatStyle::DefinitionReturnTypeBreakingStyle &Value) {
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000137 IO.enumCase(Value, "None", FormatStyle::DRTBS_None);
138 IO.enumCase(Value, "All", FormatStyle::DRTBS_All);
139 IO.enumCase(Value, "TopLevel", FormatStyle::DRTBS_TopLevel);
140
141 // For backward compatibility.
142 IO.enumCase(Value, "false", FormatStyle::DRTBS_None);
143 IO.enumCase(Value, "true", FormatStyle::DRTBS_All);
144 }
145};
146
Alexander Kornienkod6538332013-05-07 15:32:14 +0000147template <>
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000148struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000149 static void enumeration(IO &IO,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000150 FormatStyle::NamespaceIndentationKind &Value) {
151 IO.enumCase(Value, "None", FormatStyle::NI_None);
152 IO.enumCase(Value, "Inner", FormatStyle::NI_Inner);
153 IO.enumCase(Value, "All", FormatStyle::NI_All);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000154 }
155};
156
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000157template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> {
158 static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
159 IO.enumCase(Value, "Align", FormatStyle::BAS_Align);
160 IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign);
161 IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak);
162
163 // For backward compatibility.
164 IO.enumCase(Value, "true", FormatStyle::BAS_Align);
165 IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign);
166 }
167};
168
Jacques Pienaarfc275112015-02-18 23:48:37 +0000169template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> {
170 static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) {
Daniel Jasper553d4872014-06-17 12:40:34 +0000171 IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle);
172 IO.enumCase(Value, "Left", FormatStyle::PAS_Left);
173 IO.enumCase(Value, "Right", FormatStyle::PAS_Right);
174
Alp Toker958027b2014-07-14 19:42:55 +0000175 // For backward compatibility.
Daniel Jasper553d4872014-06-17 12:40:34 +0000176 IO.enumCase(Value, "true", FormatStyle::PAS_Left);
177 IO.enumCase(Value, "false", FormatStyle::PAS_Right);
178 }
179};
180
181template <>
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000182struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> {
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000183 static void enumeration(IO &IO,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000184 FormatStyle::SpaceBeforeParensOptions &Value) {
185 IO.enumCase(Value, "Never", FormatStyle::SBPO_Never);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000186 IO.enumCase(Value, "ControlStatements",
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000187 FormatStyle::SBPO_ControlStatements);
188 IO.enumCase(Value, "Always", FormatStyle::SBPO_Always);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000189
190 // For backward compatibility.
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000191 IO.enumCase(Value, "false", FormatStyle::SBPO_Never);
192 IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000193 }
194};
195
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000196template <> struct MappingTraits<FormatStyle> {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000197 static void mapping(IO &IO, FormatStyle &Style) {
198 // When reading, read the language first, we need it for getPredefinedStyle.
199 IO.mapOptional("Language", Style.Language);
200
Alexander Kornienko49149672013-05-10 11:56:10 +0000201 if (IO.outputting()) {
Jacques Pienaarfc275112015-02-18 23:48:37 +0000202 StringRef StylesArray[] = {"LLVM", "Google", "Chromium",
203 "Mozilla", "WebKit", "GNU"};
Alexander Kornienko49149672013-05-10 11:56:10 +0000204 ArrayRef<StringRef> Styles(StylesArray);
205 for (size_t i = 0, e = Styles.size(); i < e; ++i) {
206 StringRef StyleName(Styles[i]);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000207 FormatStyle PredefinedStyle;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000208 if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000209 Style == PredefinedStyle) {
Alexander Kornienko49149672013-05-10 11:56:10 +0000210 IO.mapOptional("# BasedOnStyle", StyleName);
211 break;
212 }
213 }
214 } else {
Alexander Kornienkod6538332013-05-07 15:32:14 +0000215 StringRef BasedOnStyle;
216 IO.mapOptional("BasedOnStyle", BasedOnStyle);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000217 if (!BasedOnStyle.empty()) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000218 FormatStyle::LanguageKind OldLanguage = Style.Language;
219 FormatStyle::LanguageKind Language =
220 ((FormatStyle *)IO.getContext())->Language;
221 if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000222 IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
223 return;
224 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000225 Style.Language = OldLanguage;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000226 }
Alexander Kornienkod6538332013-05-07 15:32:14 +0000227 }
228
Birunthan Mohanathas50a6f912015-06-28 14:52:34 +0000229 // For backward compatibility.
230 if (!IO.outputting()) {
231 IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment);
232 IO.mapOptional("IndentFunctionDeclarationAfterType",
233 Style.IndentWrappedFunctionNames);
234 IO.mapOptional("PointerBindsToType", Style.PointerAlignment);
235 IO.mapOptional("SpaceAfterControlStatementKeyword",
236 Style.SpaceBeforeParens);
237 }
238
Alexander Kornienkod6538332013-05-07 15:32:14 +0000239 IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
Daniel Jasper3aa9a6a2014-11-18 23:55:27 +0000240 IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000241 IO.mapOptional("AlignConsecutiveAssignments",
242 Style.AlignConsecutiveAssignments);
Daniel Jaspere12597c2015-10-01 10:06:54 +0000243 IO.mapOptional("AlignConsecutiveDeclarations",
244 Style.AlignConsecutiveDeclarations);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000245 IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
Daniel Jasper3219e432014-12-02 13:24:51 +0000246 IO.mapOptional("AlignOperands", Style.AlignOperands);
Daniel Jasper552f4a72013-07-31 23:55:15 +0000247 IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000248 IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
249 Style.AllowAllParametersOfDeclarationOnNextLine);
Daniel Jasper17605d32014-05-14 09:33:35 +0000250 IO.mapOptional("AllowShortBlocksOnASingleLine",
251 Style.AllowShortBlocksOnASingleLine);
Daniel Jasperb87899b2014-09-10 13:11:45 +0000252 IO.mapOptional("AllowShortCaseLabelsOnASingleLine",
253 Style.AllowShortCaseLabelsOnASingleLine);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000254 IO.mapOptional("AllowShortFunctionsOnASingleLine",
255 Style.AllowShortFunctionsOnASingleLine);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000256 IO.mapOptional("AllowShortIfStatementsOnASingleLine",
257 Style.AllowShortIfStatementsOnASingleLine);
Daniel Jasper3a685df2013-05-16 12:12:21 +0000258 IO.mapOptional("AllowShortLoopsOnASingleLine",
259 Style.AllowShortLoopsOnASingleLine);
Daniel Jasperca4ea1c2014-08-05 12:16:31 +0000260 IO.mapOptional("AlwaysBreakAfterDefinitionReturnType",
261 Style.AlwaysBreakAfterDefinitionReturnType);
Zachary Turner448592e2015-12-18 22:20:15 +0000262 IO.mapOptional("AlwaysBreakAfterReturnType",
263 Style.AlwaysBreakAfterReturnType);
264 // If AlwaysBreakAfterDefinitionReturnType was specified but
265 // AlwaysBreakAfterReturnType was not, initialize the latter from the
266 // former for backwards compatibility.
267 if (Style.AlwaysBreakAfterDefinitionReturnType != FormatStyle::DRTBS_None &&
268 Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) {
269 if (Style.AlwaysBreakAfterDefinitionReturnType == FormatStyle::DRTBS_All)
270 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
271 else if (Style.AlwaysBreakAfterDefinitionReturnType ==
272 FormatStyle::DRTBS_TopLevel)
273 Style.AlwaysBreakAfterReturnType =
274 FormatStyle::RTBS_TopLevelDefinitions;
275 }
276
Alexander Kornienko58611712013-07-04 12:02:44 +0000277 IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
278 Style.AlwaysBreakBeforeMultilineStrings);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000279 IO.mapOptional("AlwaysBreakTemplateDeclarations",
280 Style.AlwaysBreakTemplateDeclarations);
281 IO.mapOptional("BinPackArguments", Style.BinPackArguments);
282 IO.mapOptional("BinPackParameters", Style.BinPackParameters);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000283 IO.mapOptional("BraceWrapping", Style.BraceWrapping);
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000284 IO.mapOptional("BreakBeforeBinaryOperators",
285 Style.BreakBeforeBinaryOperators);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000286 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
Daniel Jasper165b29e2013-11-08 00:57:11 +0000287 IO.mapOptional("BreakBeforeTernaryOperators",
288 Style.BreakBeforeTernaryOperators);
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000289 IO.mapOptional("BreakConstructorInitializersBeforeComma",
290 Style.BreakConstructorInitializersBeforeComma);
Daniel Jaspere1a7b762016-02-01 11:21:02 +0000291 IO.mapOptional("BreakAfterJavaFieldAnnotations",
292 Style.BreakAfterJavaFieldAnnotations);
293 IO.mapOptional("BreakStringLiterals", Style.BreakStringLiterals);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000294 IO.mapOptional("ColumnLimit", Style.ColumnLimit);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000295 IO.mapOptional("CommentPragmas", Style.CommentPragmas);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000296 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
297 Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
Daniel Jasper50d634b2014-10-28 16:53:38 +0000298 IO.mapOptional("ConstructorInitializerIndentWidth",
299 Style.ConstructorInitializerIndentWidth);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000300 IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
301 IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
Daniel Jasper553d4872014-06-17 12:40:34 +0000302 IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000303 IO.mapOptional("DisableFormat", Style.DisableFormat);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000304 IO.mapOptional("ExperimentalAutoDetectBinPacking",
305 Style.ExperimentalAutoDetectBinPacking);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000306 IO.mapOptional("ForEachMacros", Style.ForEachMacros);
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000307 IO.mapOptional("IncludeCategories", Style.IncludeCategories);
Daniel Jasper9c8ff352016-03-21 14:11:27 +0000308 IO.mapOptional("IncludeIsMainRegex", Style.IncludeIsMainRegex);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000309 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000310 IO.mapOptional("IndentWidth", Style.IndentWidth);
311 IO.mapOptional("IndentWrappedFunctionNames",
312 Style.IndentWrappedFunctionNames);
Daniel Jaspera26fc5c2014-03-21 13:43:14 +0000313 IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks",
314 Style.KeepEmptyLinesAtTheStartOfBlocks);
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000315 IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin);
316 IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000317 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000318 IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
Daniel Jasper50d634b2014-10-28 16:53:38 +0000319 IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth);
Daniel Jaspere9beea22014-01-28 15:20:33 +0000320 IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000321 IO.mapOptional("ObjCSpaceBeforeProtocolList",
322 Style.ObjCSpaceBeforeProtocolList);
Daniel Jasper33b909c2013-10-25 14:29:37 +0000323 IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
324 Style.PenaltyBreakBeforeFirstCallParameter);
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000325 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000326 IO.mapOptional("PenaltyBreakFirstLessLess",
327 Style.PenaltyBreakFirstLessLess);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000328 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000329 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
330 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
331 Style.PenaltyReturnTypeOnItsOwnLine);
Daniel Jasper553d4872014-06-17 12:40:34 +0000332 IO.mapOptional("PointerAlignment", Style.PointerAlignment);
Daniel Jaspera0a50392015-12-01 13:28:53 +0000333 IO.mapOptional("ReflowComments", Style.ReflowComments);
334 IO.mapOptional("SortIncludes", Style.SortIncludes);
Daniel Jasperdb986eb2014-09-03 07:37:29 +0000335 IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
Daniel Jasperd94bff32013-09-25 15:15:02 +0000336 IO.mapOptional("SpaceBeforeAssignmentOperators",
337 Style.SpaceBeforeAssignmentOperators);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000338 IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
339 IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
340 IO.mapOptional("SpacesBeforeTrailingComments",
341 Style.SpacesBeforeTrailingComments);
342 IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
343 IO.mapOptional("SpacesInContainerLiterals",
344 Style.SpacesInContainerLiterals);
345 IO.mapOptional("SpacesInCStyleCastParentheses",
346 Style.SpacesInCStyleCastParentheses);
347 IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
348 IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
349 IO.mapOptional("Standard", Style.Standard);
350 IO.mapOptional("TabWidth", Style.TabWidth);
351 IO.mapOptional("UseTab", Style.UseTab);
Daniel Jasperabd1f572016-03-02 22:44:03 +0000352 IO.mapOptional("JavaScriptQuotes", Style.JavaScriptQuotes);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000353 }
354};
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000355
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000356template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
357 static void mapping(IO &IO, FormatStyle::BraceWrappingFlags &Wrapping) {
358 IO.mapOptional("AfterClass", Wrapping.AfterClass);
359 IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement);
360 IO.mapOptional("AfterEnum", Wrapping.AfterEnum);
361 IO.mapOptional("AfterFunction", Wrapping.AfterFunction);
362 IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace);
363 IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration);
364 IO.mapOptional("AfterStruct", Wrapping.AfterStruct);
365 IO.mapOptional("AfterUnion", Wrapping.AfterUnion);
366 IO.mapOptional("BeforeCatch", Wrapping.BeforeCatch);
367 IO.mapOptional("BeforeElse", Wrapping.BeforeElse);
368 IO.mapOptional("IndentBraces", Wrapping.IndentBraces);
369 }
370};
371
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000372template <> struct MappingTraits<FormatStyle::IncludeCategory> {
373 static void mapping(IO &IO, FormatStyle::IncludeCategory &Category) {
374 IO.mapOptional("Regex", Category.Regex);
375 IO.mapOptional("Priority", Category.Priority);
376 }
377};
378
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000379// Allows to read vector<FormatStyle> while keeping default values.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000380// IO.getContext() should contain a pointer to the FormatStyle structure, that
381// will be used to get default values for missing keys.
382// If the first element has no Language specified, it will be treated as the
383// default one for the following elements.
Jacques Pienaarfc275112015-02-18 23:48:37 +0000384template <> struct DocumentListTraits<std::vector<FormatStyle>> {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000385 static size_t size(IO &IO, std::vector<FormatStyle> &Seq) {
386 return Seq.size();
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000387 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000388 static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000389 size_t Index) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000390 if (Index >= Seq.size()) {
391 assert(Index == Seq.size());
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000392 FormatStyle Template;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000393 if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000394 Template = Seq[0];
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000395 } else {
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000396 Template = *((const FormatStyle *)IO.getContext());
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000397 Template.Language = FormatStyle::LK_None;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000398 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000399 Seq.resize(Index + 1, Template);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000400 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000401 return Seq[Index];
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000402 }
403};
Daniel Jasperd89ae9d2015-09-23 08:30:47 +0000404} // namespace yaml
405} // namespace llvm
Alexander Kornienkod6538332013-05-07 15:32:14 +0000406
Daniel Jasperf7935112012-12-03 18:12:45 +0000407namespace clang {
408namespace format {
409
Rafael Espindola6d0d89b2014-06-12 03:31:26 +0000410const std::error_category &getParseCategory() {
Rafael Espindolad0136702014-06-12 02:50:04 +0000411 static ParseErrorCategory C;
412 return C;
413}
414std::error_code make_error_code(ParseError e) {
Rafael Espindola6d0d89b2014-06-12 03:31:26 +0000415 return std::error_code(static_cast<int>(e), getParseCategory());
Rafael Espindolad0136702014-06-12 02:50:04 +0000416}
417
418const char *ParseErrorCategory::name() const LLVM_NOEXCEPT {
419 return "clang-format.parse_error";
420}
421
422std::string ParseErrorCategory::message(int EV) const {
423 switch (static_cast<ParseError>(EV)) {
424 case ParseError::Success:
425 return "Success";
426 case ParseError::Error:
427 return "Invalid argument";
428 case ParseError::Unsuitable:
429 return "Unsuitable";
430 }
Saleem Abdulrasoolfbfbaf62014-06-12 19:33:26 +0000431 llvm_unreachable("unexpected parse error");
Rafael Espindolad0136702014-06-12 02:50:04 +0000432}
433
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000434static FormatStyle expandPresets(const FormatStyle &Style) {
Daniel Jasper55bbe662015-10-07 04:06:10 +0000435 if (Style.BreakBeforeBraces == FormatStyle::BS_Custom)
436 return Style;
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000437 FormatStyle Expanded = Style;
438 Expanded.BraceWrapping = {false, false, false, false, false, false,
439 false, false, false, false, false};
440 switch (Style.BreakBeforeBraces) {
441 case FormatStyle::BS_Linux:
442 Expanded.BraceWrapping.AfterClass = true;
443 Expanded.BraceWrapping.AfterFunction = true;
444 Expanded.BraceWrapping.AfterNamespace = true;
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000445 break;
446 case FormatStyle::BS_Mozilla:
447 Expanded.BraceWrapping.AfterClass = true;
448 Expanded.BraceWrapping.AfterEnum = true;
449 Expanded.BraceWrapping.AfterFunction = true;
450 Expanded.BraceWrapping.AfterStruct = true;
451 Expanded.BraceWrapping.AfterUnion = true;
452 break;
453 case FormatStyle::BS_Stroustrup:
454 Expanded.BraceWrapping.AfterFunction = true;
455 Expanded.BraceWrapping.BeforeCatch = true;
456 Expanded.BraceWrapping.BeforeElse = true;
457 break;
458 case FormatStyle::BS_Allman:
459 Expanded.BraceWrapping.AfterClass = true;
460 Expanded.BraceWrapping.AfterControlStatement = true;
461 Expanded.BraceWrapping.AfterEnum = true;
462 Expanded.BraceWrapping.AfterFunction = true;
463 Expanded.BraceWrapping.AfterNamespace = true;
464 Expanded.BraceWrapping.AfterObjCDeclaration = true;
465 Expanded.BraceWrapping.AfterStruct = true;
466 Expanded.BraceWrapping.BeforeCatch = true;
467 Expanded.BraceWrapping.BeforeElse = true;
468 break;
469 case FormatStyle::BS_GNU:
470 Expanded.BraceWrapping = {true, true, true, true, true, true,
471 true, true, true, true, true};
472 break;
473 case FormatStyle::BS_WebKit:
474 Expanded.BraceWrapping.AfterFunction = true;
475 break;
476 default:
477 break;
478 }
479 return Expanded;
480}
481
Daniel Jasperf7935112012-12-03 18:12:45 +0000482FormatStyle getLLVMStyle() {
483 FormatStyle LLVMStyle;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000484 LLVMStyle.Language = FormatStyle::LK_Cpp;
Daniel Jasperf7935112012-12-03 18:12:45 +0000485 LLVMStyle.AccessModifierOffset = -2;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000486 LLVMStyle.AlignEscapedNewlinesLeft = false;
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000487 LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align;
Daniel Jasper3219e432014-12-02 13:24:51 +0000488 LLVMStyle.AlignOperands = true;
Daniel Jasper552f4a72013-07-31 23:55:15 +0000489 LLVMStyle.AlignTrailingComments = true;
Daniel Jaspera44991332015-04-29 13:06:49 +0000490 LLVMStyle.AlignConsecutiveAssignments = false;
Daniel Jaspere12597c2015-10-01 10:06:54 +0000491 LLVMStyle.AlignConsecutiveDeclarations = false;
Daniel Jasperf7db4332013-01-29 16:03:49 +0000492 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasperd74cf402014-04-08 12:46:38 +0000493 LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
Daniel Jasper17605d32014-05-14 09:33:35 +0000494 LLVMStyle.AllowShortBlocksOnASingleLine = false;
Daniel Jasperb87899b2014-09-10 13:11:45 +0000495 LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000496 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000497 LLVMStyle.AllowShortLoopsOnASingleLine = false;
Zachary Turner448592e2015-12-18 22:20:15 +0000498 LLVMStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000499 LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None;
Alexander Kornienko58611712013-07-04 12:02:44 +0000500 LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000501 LLVMStyle.AlwaysBreakTemplateDeclarations = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000502 LLVMStyle.BinPackParameters = true;
Daniel Jasper18210d72014-10-09 09:52:05 +0000503 LLVMStyle.BinPackArguments = true;
Daniel Jasperac043c92014-09-15 11:11:00 +0000504 LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
Daniel Jasper165b29e2013-11-08 00:57:11 +0000505 LLVMStyle.BreakBeforeTernaryOperators = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000506 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
Daniel Jasper55bbe662015-10-07 04:06:10 +0000507 LLVMStyle.BraceWrapping = {false, false, false, false, false, false,
508 false, false, false, false, false};
Nico Weber2cd92f12015-10-15 16:03:01 +0000509 LLVMStyle.BreakAfterJavaFieldAnnotations = false;
Daniel Jaspere1a7b762016-02-01 11:21:02 +0000510 LLVMStyle.BreakConstructorInitializersBeforeComma = false;
511 LLVMStyle.BreakStringLiterals = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000512 LLVMStyle.ColumnLimit = 80;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000513 LLVMStyle.CommentPragmas = "^ IWYU pragma:";
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000514 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Daniel Jaspercdaffa42013-08-13 10:58:30 +0000515 LLVMStyle.ConstructorInitializerIndentWidth = 4;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000516 LLVMStyle.ContinuationIndentWidth = 4;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000517 LLVMStyle.Cpp11BracedListStyle = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000518 LLVMStyle.DerivePointerAlignment = false;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000519 LLVMStyle.ExperimentalAutoDetectBinPacking = false;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000520 LLVMStyle.ForEachMacros.push_back("foreach");
521 LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
522 LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
Daniel Jasper85c472d2015-09-29 07:53:08 +0000523 LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2},
524 {"^(<|\"(gtest|isl|json)/)", 3},
525 {".*", 1}};
Daniel Jasper9c8ff352016-03-21 14:11:27 +0000526 LLVMStyle.IncludeIsMainRegex = "$";
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000527 LLVMStyle.IndentCaseLabels = false;
Daniel Jasperc75e1ef2014-07-09 08:42:42 +0000528 LLVMStyle.IndentWrappedFunctionNames = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000529 LLVMStyle.IndentWidth = 2;
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000530 LLVMStyle.TabWidth = 8;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000531 LLVMStyle.MaxEmptyLinesToKeep = 1;
Daniel Jaspera26fc5c2014-03-21 13:43:14 +0000532 LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000533 LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
Daniel Jasper50d634b2014-10-28 16:53:38 +0000534 LLVMStyle.ObjCBlockIndentWidth = 2;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000535 LLVMStyle.ObjCSpaceAfterProperty = false;
Nico Webera6087752013-01-10 20:12:55 +0000536 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000537 LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000538 LLVMStyle.SpacesBeforeTrailingComments = 1;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000539 LLVMStyle.Standard = FormatStyle::LS_Cpp11;
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000540 LLVMStyle.UseTab = FormatStyle::UT_Never;
Daniel Jasperabd1f572016-03-02 22:44:03 +0000541 LLVMStyle.JavaScriptQuotes = FormatStyle::JSQS_Leave;
Daniel Jaspera0a50392015-12-01 13:28:53 +0000542 LLVMStyle.ReflowComments = true;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000543 LLVMStyle.SpacesInParentheses = false;
Daniel Jasperad981f82014-08-26 11:41:14 +0000544 LLVMStyle.SpacesInSquareBrackets = false;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000545 LLVMStyle.SpaceInEmptyParentheses = false;
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000546 LLVMStyle.SpacesInContainerLiterals = true;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000547 LLVMStyle.SpacesInCStyleCastParentheses = false;
Daniel Jasperdb986eb2014-09-03 07:37:29 +0000548 LLVMStyle.SpaceAfterCStyleCast = false;
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000549 LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
Daniel Jasperd94bff32013-09-25 15:15:02 +0000550 LLVMStyle.SpaceBeforeAssignmentOperators = true;
Daniel Jasperdd978ae2013-10-29 14:52:02 +0000551 LLVMStyle.SpacesInAngles = false;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000552
Daniel Jasper19a541e2013-12-19 16:45:34 +0000553 LLVMStyle.PenaltyBreakComment = 300;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000554 LLVMStyle.PenaltyBreakFirstLessLess = 120;
555 LLVMStyle.PenaltyBreakString = 1000;
556 LLVMStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000557 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
Daniel Jasper33b909c2013-10-25 14:29:37 +0000558 LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000559
Daniel Jasperc64b09a2014-05-22 15:12:22 +0000560 LLVMStyle.DisableFormat = false;
Daniel Jasperda446772015-11-16 12:38:56 +0000561 LLVMStyle.SortIncludes = true;
Daniel Jasperc64b09a2014-05-22 15:12:22 +0000562
Daniel Jasperf7935112012-12-03 18:12:45 +0000563 return LLVMStyle;
564}
565
Nico Weber514ecc82014-02-02 20:50:45 +0000566FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000567 FormatStyle GoogleStyle = getLLVMStyle();
Nico Weber514ecc82014-02-02 20:50:45 +0000568 GoogleStyle.Language = Language;
569
Daniel Jasperf7935112012-12-03 18:12:45 +0000570 GoogleStyle.AccessModifierOffset = -1;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000571 GoogleStyle.AlignEscapedNewlinesLeft = true;
Daniel Jasper085a2ed2013-04-24 13:46:00 +0000572 GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
Daniel Jasper5bd0b9e2013-05-23 18:05:18 +0000573 GoogleStyle.AllowShortLoopsOnASingleLine = true;
Alexander Kornienko58611712013-07-04 12:02:44 +0000574 GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000575 GoogleStyle.AlwaysBreakTemplateDeclarations = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000576 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000577 GoogleStyle.DerivePointerAlignment = true;
Daniel Jasper85c472d2015-09-29 07:53:08 +0000578 GoogleStyle.IncludeCategories = {{"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}};
Daniel Jasper9c8ff352016-03-21 14:11:27 +0000579 GoogleStyle.IncludeIsMainRegex = "([-_](test|unittest))?$";
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000580 GoogleStyle.IndentCaseLabels = true;
Daniel Jaspera26fc5c2014-03-21 13:43:14 +0000581 GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000582 GoogleStyle.ObjCSpaceAfterProperty = false;
Nico Webera6087752013-01-10 20:12:55 +0000583 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Daniel Jasper553d4872014-06-17 12:40:34 +0000584 GoogleStyle.PointerAlignment = FormatStyle::PAS_Left;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000585 GoogleStyle.SpacesBeforeTrailingComments = 2;
586 GoogleStyle.Standard = FormatStyle::LS_Auto;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000587
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000588 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
Daniel Jasper33b909c2013-10-25 14:29:37 +0000589 GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000590
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000591 if (Language == FormatStyle::LK_Java) {
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000592 GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Daniel Jasper3219e432014-12-02 13:24:51 +0000593 GoogleStyle.AlignOperands = false;
Daniel Jasperfd4ed182015-01-04 20:40:45 +0000594 GoogleStyle.AlignTrailingComments = false;
Daniel Jasper9e709352014-11-26 10:43:58 +0000595 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
Daniel Jasperfd4ed182015-01-04 20:40:45 +0000596 GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper1cd3c712015-01-14 12:24:59 +0000597 GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000598 GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
599 GoogleStyle.ColumnLimit = 100;
600 GoogleStyle.SpaceAfterCStyleCast = true;
Daniel Jasper61d81972014-11-14 08:22:46 +0000601 GoogleStyle.SpacesBeforeTrailingComments = 1;
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000602 } else if (Language == FormatStyle::LK_JavaScript) {
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000603 GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
Daniel Jasper41a2bf72015-12-21 13:52:19 +0000604 GoogleStyle.AlignOperands = false;
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000605 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
606 GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
Daniel Jaspere551bb72014-11-05 17:22:31 +0000607 GoogleStyle.BreakBeforeTernaryOperators = false;
Daniel Jasper2bc38702016-02-22 20:24:11 +0000608 GoogleStyle.CommentPragmas = "@(export|return|see|visibility) ";
Daniel Jasper8f83a902014-05-09 10:28:58 +0000609 GoogleStyle.MaxEmptyLinesToKeep = 3;
Nico Weber514ecc82014-02-02 20:50:45 +0000610 GoogleStyle.SpacesInContainerLiterals = false;
Daniel Jasperabd1f572016-03-02 22:44:03 +0000611 GoogleStyle.JavaScriptQuotes = FormatStyle::JSQS_Single;
Nico Weber514ecc82014-02-02 20:50:45 +0000612 } else if (Language == FormatStyle::LK_Proto) {
Daniel Jasperd74cf402014-04-08 12:46:38 +0000613 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
Daniel Jasper783bac62014-04-15 09:54:30 +0000614 GoogleStyle.SpacesInContainerLiterals = false;
Nico Weber514ecc82014-02-02 20:50:45 +0000615 }
616
Daniel Jasperf7935112012-12-03 18:12:45 +0000617 return GoogleStyle;
618}
619
Nico Weber514ecc82014-02-02 20:50:45 +0000620FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
621 FormatStyle ChromiumStyle = getGoogleStyle(Language);
Nico Weber450425c2014-11-26 16:43:18 +0000622 if (Language == FormatStyle::LK_Java) {
Daniel Jasperfd4ed182015-01-04 20:40:45 +0000623 ChromiumStyle.AllowShortIfStatementsOnASingleLine = true;
Nico Weber2cd92f12015-10-15 16:03:01 +0000624 ChromiumStyle.BreakAfterJavaFieldAnnotations = true;
Nico Weber450425c2014-11-26 16:43:18 +0000625 ChromiumStyle.ContinuationIndentWidth = 8;
Nico Weber2cd92f12015-10-15 16:03:01 +0000626 ChromiumStyle.IndentWidth = 4;
Nico Weber450425c2014-11-26 16:43:18 +0000627 } else {
628 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
629 ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
630 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
631 ChromiumStyle.AllowShortLoopsOnASingleLine = false;
632 ChromiumStyle.BinPackParameters = false;
633 ChromiumStyle.DerivePointerAlignment = false;
634 }
Nico Weberb10423a2015-12-22 22:42:56 +0000635 ChromiumStyle.SortIncludes = false;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000636 return ChromiumStyle;
637}
638
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000639FormatStyle getMozillaStyle() {
640 FormatStyle MozillaStyle = getLLVMStyle();
641 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Birunthan Mohanathasa0810022015-06-29 15:18:58 +0000642 MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
Zachary Turner448592e2015-12-18 22:20:15 +0000643 MozillaStyle.AlwaysBreakAfterReturnType =
644 FormatStyle::RTBS_TopLevelDefinitions;
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000645 MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
646 FormatStyle::DRTBS_TopLevel;
Birunthan Mohanathasa0810022015-06-29 15:18:58 +0000647 MozillaStyle.AlwaysBreakTemplateDeclarations = true;
Birunthan Mohanathas305fa9c2015-07-12 03:13:54 +0000648 MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
Birunthan Mohanathasa0810022015-06-29 15:18:58 +0000649 MozillaStyle.BreakConstructorInitializersBeforeComma = true;
650 MozillaStyle.ConstructorInitializerIndentWidth = 2;
651 MozillaStyle.ContinuationIndentWidth = 2;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000652 MozillaStyle.Cpp11BracedListStyle = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000653 MozillaStyle.IndentCaseLabels = true;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000654 MozillaStyle.ObjCSpaceAfterProperty = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000655 MozillaStyle.ObjCSpaceBeforeProtocolList = false;
656 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
Daniel Jasper553d4872014-06-17 12:40:34 +0000657 MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000658 return MozillaStyle;
659}
660
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000661FormatStyle getWebKitStyle() {
662 FormatStyle Style = getLLVMStyle();
Daniel Jasper65ee3472013-07-31 23:16:02 +0000663 Style.AccessModifierOffset = -4;
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000664 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Daniel Jasper3219e432014-12-02 13:24:51 +0000665 Style.AlignOperands = false;
Daniel Jasper552f4a72013-07-31 23:55:15 +0000666 Style.AlignTrailingComments = false;
Daniel Jasperac043c92014-09-15 11:11:00 +0000667 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
Roman Kashitsyn291f64f2015-08-10 13:43:19 +0000668 Style.BreakBeforeBraces = FormatStyle::BS_WebKit;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000669 Style.BreakConstructorInitializersBeforeComma = true;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000670 Style.Cpp11BracedListStyle = false;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000671 Style.ColumnLimit = 0;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000672 Style.IndentWidth = 4;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000673 Style.NamespaceIndentation = FormatStyle::NI_Inner;
Daniel Jasper50d634b2014-10-28 16:53:38 +0000674 Style.ObjCBlockIndentWidth = 4;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000675 Style.ObjCSpaceAfterProperty = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000676 Style.PointerAlignment = FormatStyle::PAS_Left;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000677 Style.Standard = FormatStyle::LS_Cpp03;
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000678 return Style;
679}
680
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000681FormatStyle getGNUStyle() {
682 FormatStyle Style = getLLVMStyle();
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000683 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
Zachary Turner448592e2015-12-18 22:20:15 +0000684 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
Daniel Jasperac043c92014-09-15 11:11:00 +0000685 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000686 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000687 Style.BreakBeforeTernaryOperators = true;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000688 Style.Cpp11BracedListStyle = false;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000689 Style.ColumnLimit = 79;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000690 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000691 Style.Standard = FormatStyle::LS_Cpp03;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000692 return Style;
693}
694
Daniel Jasperc64b09a2014-05-22 15:12:22 +0000695FormatStyle getNoStyle() {
696 FormatStyle NoStyle = getLLVMStyle();
697 NoStyle.DisableFormat = true;
Daniel Jasperda446772015-11-16 12:38:56 +0000698 NoStyle.SortIncludes = false;
Daniel Jasperc64b09a2014-05-22 15:12:22 +0000699 return NoStyle;
700}
701
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000702bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
703 FormatStyle *Style) {
704 if (Name.equals_lower("llvm")) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000705 *Style = getLLVMStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000706 } else if (Name.equals_lower("chromium")) {
Nico Weber514ecc82014-02-02 20:50:45 +0000707 *Style = getChromiumStyle(Language);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000708 } else if (Name.equals_lower("mozilla")) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000709 *Style = getMozillaStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000710 } else if (Name.equals_lower("google")) {
Nico Weber514ecc82014-02-02 20:50:45 +0000711 *Style = getGoogleStyle(Language);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000712 } else if (Name.equals_lower("webkit")) {
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000713 *Style = getWebKitStyle();
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000714 } else if (Name.equals_lower("gnu")) {
715 *Style = getGNUStyle();
Daniel Jasperc64b09a2014-05-22 15:12:22 +0000716 } else if (Name.equals_lower("none")) {
717 *Style = getNoStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000718 } else {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000719 return false;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000720 }
Alexander Kornienkod6538332013-05-07 15:32:14 +0000721
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000722 Style->Language = Language;
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000723 return true;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000724}
725
Rafael Espindolac0809172014-06-12 14:02:15 +0000726std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000727 assert(Style);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000728 FormatStyle::LanguageKind Language = Style->Language;
729 assert(Language != FormatStyle::LK_None);
Alexander Kornienko06e00332013-05-20 15:18:01 +0000730 if (Text.trim().empty())
Rafael Espindolad0136702014-06-12 02:50:04 +0000731 return make_error_code(ParseError::Error);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000732
733 std::vector<FormatStyle> Styles;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000734 llvm::yaml::Input Input(Text);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000735 // DocumentListTraits<vector<FormatStyle>> uses the context to get default
736 // values for the fields, keys for which are missing from the configuration.
737 // Mapping also uses the context to get the language to find the correct
738 // base style.
739 Input.setContext(Style);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000740 Input >> Styles;
741 if (Input.error())
742 return Input.error();
743
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000744 for (unsigned i = 0; i < Styles.size(); ++i) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000745 // Ensures that only the first configuration can skip the Language option.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000746 if (Styles[i].Language == FormatStyle::LK_None && i != 0)
Rafael Espindolad0136702014-06-12 02:50:04 +0000747 return make_error_code(ParseError::Error);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000748 // Ensure that each language is configured at most once.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000749 for (unsigned j = 0; j < i; ++j) {
750 if (Styles[i].Language == Styles[j].Language) {
751 DEBUG(llvm::dbgs()
752 << "Duplicate languages in the config file on positions " << j
753 << " and " << i << "\n");
Rafael Espindolad0136702014-06-12 02:50:04 +0000754 return make_error_code(ParseError::Error);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000755 }
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000756 }
757 }
758 // Look for a suitable configuration starting from the end, so we can
759 // find the configuration for the specific language first, and the default
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000760 // configuration (which can only be at slot 0) after it.
761 for (int i = Styles.size() - 1; i >= 0; --i) {
762 if (Styles[i].Language == Language ||
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000763 Styles[i].Language == FormatStyle::LK_None) {
764 *Style = Styles[i];
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000765 Style->Language = Language;
Rafael Espindolad0136702014-06-12 02:50:04 +0000766 return make_error_code(ParseError::Success);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000767 }
768 }
Rafael Espindolad0136702014-06-12 02:50:04 +0000769 return make_error_code(ParseError::Unsuitable);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000770}
771
772std::string configurationAsText(const FormatStyle &Style) {
773 std::string Text;
774 llvm::raw_string_ostream Stream(Text);
775 llvm::yaml::Output Output(Stream);
776 // We use the same mapping method for input and output, so we need a non-const
777 // reference here.
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000778 FormatStyle NonConstStyle = expandPresets(Style);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000779 Output << NonConstStyle;
Alexander Kornienko9a38ec22013-05-13 12:56:35 +0000780 return Stream.str();
Alexander Kornienkod6538332013-05-07 15:32:14 +0000781}
782
Craig Topperaf35e852013-06-30 22:29:28 +0000783namespace {
784
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000785class FormatTokenLexer {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000786public:
Eric Liu4cfb88a2016-04-25 15:09:22 +0000787 FormatTokenLexer(SourceManager &SourceMgr, FileID ID,
788 const FormatStyle &Style, encoding::Encoding Encoding)
Craig Topper2145bc02014-05-09 08:15:10 +0000789 : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false),
Jacques Pienaarfc275112015-02-18 23:48:37 +0000790 LessStashed(false), Column(0), TrailingWhitespace(0),
791 SourceMgr(SourceMgr), ID(ID), Style(Style),
792 IdentTable(getFormattingLangOpts(Style)), Keywords(IdentTable),
Daniel Jasper97439922016-03-17 13:03:41 +0000793 Encoding(Encoding), FirstInLineIndex(0), FormattingDisabled(false),
794 MacroBlockBeginRegex(Style.MacroBlockBegin),
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000795 MacroBlockEndRegex(Style.MacroBlockEnd) {
Daniel Jasper23376252014-09-09 14:37:39 +0000796 Lex.reset(new Lexer(ID, SourceMgr.getBuffer(ID), SourceMgr,
797 getFormattingLangOpts(Style)));
798 Lex->SetKeepWhitespaceMode(true);
Daniel Jaspere1e43192014-04-01 12:55:11 +0000799
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000800 for (const std::string &ForEachMacro : Style.ForEachMacros)
Daniel Jaspere1e43192014-04-01 12:55:11 +0000801 ForEachMacros.push_back(&IdentTable.get(ForEachMacro));
802 std::sort(ForEachMacros.begin(), ForEachMacros.end());
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000803 }
804
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000805 ArrayRef<FormatToken *> lex() {
806 assert(Tokens.empty());
Manuel Klimek68b03042014-04-14 09:14:11 +0000807 assert(FirstInLineIndex == 0);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000808 do {
809 Tokens.push_back(getNextToken());
Daniel Jasper265309e2015-10-18 07:02:28 +0000810 if (Style.Language == FormatStyle::LK_JavaScript)
811 tryParseJSRegexLiteral();
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000812 tryMergePreviousTokens();
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000813 if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline)
Manuel Klimek68b03042014-04-14 09:14:11 +0000814 FirstInLineIndex = Tokens.size() - 1;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000815 } while (Tokens.back()->Tok.isNot(tok::eof));
816 return Tokens;
817 }
818
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000819 const AdditionalKeywords &getKeywords() { return Keywords; }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000820
821private:
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000822 void tryMergePreviousTokens() {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000823 if (tryMerge_TMacro())
824 return;
Manuel Klimek68b03042014-04-14 09:14:11 +0000825 if (tryMergeConflictMarkers())
826 return;
Jacques Pienaarfc275112015-02-18 23:48:37 +0000827 if (tryMergeLessLess())
828 return;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000829
830 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000831 if (tryMergeTemplateString())
832 return;
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000833
Benjamin Kramer28b45ce2015-03-08 16:06:46 +0000834 static const tok::TokenKind JSIdentity[] = {tok::equalequal, tok::equal};
835 static const tok::TokenKind JSNotIdentity[] = {tok::exclaimequal,
836 tok::equal};
837 static const tok::TokenKind JSShiftEqual[] = {tok::greater, tok::greater,
838 tok::greaterequal};
839 static const tok::TokenKind JSRightArrow[] = {tok::equal, tok::greater};
Manuel Klimek79e06082015-05-21 12:23:34 +0000840 // FIXME: Investigate what token type gives the correct operator priority.
841 if (tryMergeTokens(JSIdentity, TT_BinaryOperator))
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000842 return;
Manuel Klimek79e06082015-05-21 12:23:34 +0000843 if (tryMergeTokens(JSNotIdentity, TT_BinaryOperator))
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000844 return;
Manuel Klimek79e06082015-05-21 12:23:34 +0000845 if (tryMergeTokens(JSShiftEqual, TT_BinaryOperator))
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000846 return;
Manuel Klimek79e06082015-05-21 12:23:34 +0000847 if (tryMergeTokens(JSRightArrow, TT_JsFatArrow))
Daniel Jasper78214392014-05-19 07:27:02 +0000848 return;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000849 }
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000850 }
851
Jacques Pienaarfc275112015-02-18 23:48:37 +0000852 bool tryMergeLessLess() {
853 // Merge X,less,less,Y into X,lessless,Y unless X or Y is less.
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000854 if (Tokens.size() < 3)
855 return false;
Jacques Pienaarfc275112015-02-18 23:48:37 +0000856
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000857 bool FourthTokenIsLess = false;
858 if (Tokens.size() > 3)
859 FourthTokenIsLess = (Tokens.end() - 4)[0]->is(tok::less);
Jacques Pienaarfc275112015-02-18 23:48:37 +0000860
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000861 auto First = Tokens.end() - 3;
862 if (First[2]->is(tok::less) || First[1]->isNot(tok::less) ||
863 First[0]->isNot(tok::less) || FourthTokenIsLess)
Jacques Pienaarfc275112015-02-18 23:48:37 +0000864 return false;
865
866 // Only merge if there currently is no whitespace between the two "<".
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000867 if (First[1]->WhitespaceRange.getBegin() !=
868 First[1]->WhitespaceRange.getEnd())
Jacques Pienaarfc275112015-02-18 23:48:37 +0000869 return false;
870
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000871 First[0]->Tok.setKind(tok::lessless);
872 First[0]->TokenText = "<<";
873 First[0]->ColumnWidth += 1;
Jacques Pienaarfc275112015-02-18 23:48:37 +0000874 Tokens.erase(Tokens.end() - 2);
875 return true;
876 }
877
Manuel Klimek79e06082015-05-21 12:23:34 +0000878 bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000879 if (Tokens.size() < Kinds.size())
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000880 return false;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000881
882 SmallVectorImpl<FormatToken *>::const_iterator First =
883 Tokens.end() - Kinds.size();
884 if (!First[0]->is(Kinds[0]))
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000885 return false;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000886 unsigned AddLength = 0;
887 for (unsigned i = 1; i < Kinds.size(); ++i) {
Jacques Pienaarfc275112015-02-18 23:48:37 +0000888 if (!First[i]->is(Kinds[i]) ||
889 First[i]->WhitespaceRange.getBegin() !=
890 First[i]->WhitespaceRange.getEnd())
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000891 return false;
892 AddLength += First[i]->TokenText.size();
893 }
894 Tokens.resize(Tokens.size() - Kinds.size() + 1);
895 First[0]->TokenText = StringRef(First[0]->TokenText.data(),
896 First[0]->TokenText.size() + AddLength);
897 First[0]->ColumnWidth += AddLength;
Manuel Klimek79e06082015-05-21 12:23:34 +0000898 First[0]->Type = NewType;
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000899 return true;
900 }
901
Daniel Jasper265309e2015-10-18 07:02:28 +0000902 // Returns \c true if \p Tok can only be followed by an operand in JavaScript.
903 bool precedesOperand(FormatToken *Tok) {
904 // NB: This is not entirely correct, as an r_paren can introduce an operand
905 // location in e.g. `if (foo) /bar/.exec(...);`. That is a rare enough
906 // corner case to not matter in practice, though.
907 return Tok->isOneOf(tok::period, tok::l_paren, tok::comma, tok::l_brace,
908 tok::r_brace, tok::l_square, tok::semi, tok::exclaim,
909 tok::colon, tok::question, tok::tilde) ||
910 Tok->isOneOf(tok::kw_return, tok::kw_do, tok::kw_case, tok::kw_throw,
911 tok::kw_else, tok::kw_new, tok::kw_delete, tok::kw_void,
912 tok::kw_typeof, Keywords.kw_instanceof,
913 Keywords.kw_in) ||
914 Tok->isBinaryOperator();
915 }
916
917 bool canPrecedeRegexLiteral(FormatToken *Prev) {
918 if (!Prev)
919 return true;
920
921 // Regex literals can only follow after prefix unary operators, not after
922 // postfix unary operators. If the '++' is followed by a non-operand
923 // introducing token, the slash here is the operand and not the start of a
924 // regex.
925 if (Prev->isOneOf(tok::plusplus, tok::minusminus))
926 return (Tokens.size() < 3 || precedesOperand(Tokens[Tokens.size() - 3]));
927
928 // The previous token must introduce an operand location where regex
929 // literals can occur.
930 if (!precedesOperand(Prev))
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000931 return false;
Daniel Jasper265309e2015-10-18 07:02:28 +0000932
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000933 return true;
934 }
935
Daniel Jasper265309e2015-10-18 07:02:28 +0000936 // Tries to parse a JavaScript Regex literal starting at the current token,
937 // if that begins with a slash and is in a location where JavaScript allows
938 // regex literals. Changes the current token to a regex literal and updates
939 // its text if successful.
940 void tryParseJSRegexLiteral() {
941 FormatToken *RegexToken = Tokens.back();
942 if (!RegexToken->isOneOf(tok::slash, tok::slashequal))
943 return;
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000944
Daniel Jasper265309e2015-10-18 07:02:28 +0000945 FormatToken *Prev = nullptr;
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000946 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
Daniel Jasper265309e2015-10-18 07:02:28 +0000947 // NB: Because previous pointers are not initialized yet, this cannot use
948 // Token.getPreviousNonComment.
949 if ((*I)->isNot(tok::comment)) {
950 Prev = *I;
951 break;
Daniel Jasper8d0e2232015-10-12 03:13:48 +0000952 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000953 }
Daniel Jasper265309e2015-10-18 07:02:28 +0000954
955 if (!canPrecedeRegexLiteral(Prev))
956 return;
957
958 // 'Manually' lex ahead in the current file buffer.
959 const char *Offset = Lex->getBufferLocation();
960 const char *RegexBegin = Offset - RegexToken->TokenText.size();
961 StringRef Buffer = Lex->getBuffer();
962 bool InCharacterClass = false;
963 bool HaveClosingSlash = false;
964 for (; !HaveClosingSlash && Offset != Buffer.end(); ++Offset) {
965 // Regular expressions are terminated with a '/', which can only be
966 // escaped using '\' or a character class between '[' and ']'.
967 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.5.
968 switch (*Offset) {
969 case '\\':
970 // Skip the escaped character.
971 ++Offset;
972 break;
973 case '[':
974 InCharacterClass = true;
975 break;
976 case ']':
977 InCharacterClass = false;
978 break;
979 case '/':
980 if (!InCharacterClass)
981 HaveClosingSlash = true;
982 break;
983 }
984 }
985
986 RegexToken->Type = TT_RegexLiteral;
987 // Treat regex literals like other string_literals.
988 RegexToken->Tok.setKind(tok::string_literal);
989 RegexToken->TokenText = StringRef(RegexBegin, Offset - RegexBegin);
990 RegexToken->ColumnWidth = RegexToken->TokenText.size();
991
992 resetLexer(SourceMgr.getFileOffset(Lex->getSourceLocation(Offset)));
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000993 }
994
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000995 bool tryMergeTemplateString() {
996 if (Tokens.size() < 2)
997 return false;
998
999 FormatToken *EndBacktick = Tokens.back();
Daniel Jasperf69b9222015-05-02 08:05:38 +00001000 // Backticks get lexed as tok::unknown tokens. If a template string contains
Daniel Jasper0d6ac272015-04-16 08:20:51 +00001001 // a comment start, it gets lexed as a tok::comment, or tok::unknown if
1002 // unterminated.
Daniel Jasper2ebb0c52015-06-14 07:16:57 +00001003 if (!EndBacktick->isOneOf(tok::comment, tok::string_literal,
1004 tok::char_constant, tok::unknown))
Daniel Jasper0d6ac272015-04-16 08:20:51 +00001005 return false;
1006 size_t CommentBacktickPos = EndBacktick->TokenText.find('`');
1007 // Unknown token that's not actually a backtick, or a comment that doesn't
1008 // contain a backtick.
1009 if (CommentBacktickPos == StringRef::npos)
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001010 return false;
1011
1012 unsigned TokenCount = 0;
1013 bool IsMultiline = false;
Daniel Jasperf69b9222015-05-02 08:05:38 +00001014 unsigned EndColumnInFirstLine =
1015 EndBacktick->OriginalColumn + EndBacktick->ColumnWidth;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001016 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; I++) {
1017 ++TokenCount;
Daniel Jasper553a5b02015-07-02 13:08:28 +00001018 if (I[0]->IsMultiline)
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001019 IsMultiline = true;
1020
1021 // If there was a preceding template string, this must be the start of a
1022 // template string, not the end.
1023 if (I[0]->is(TT_TemplateString))
1024 return false;
1025
1026 if (I[0]->isNot(tok::unknown) || I[0]->TokenText != "`") {
1027 // Keep track of the rhs offset of the last token to wrap across lines -
1028 // its the rhs offset of the first line of the template string, used to
1029 // determine its width.
1030 if (I[0]->IsMultiline)
1031 EndColumnInFirstLine = I[0]->OriginalColumn + I[0]->ColumnWidth;
1032 // If the token has newlines, the token before it (if it exists) is the
1033 // rhs end of the previous line.
Daniel Jasper553a5b02015-07-02 13:08:28 +00001034 if (I[0]->NewlinesBefore > 0 && (I + 1 != E)) {
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001035 EndColumnInFirstLine = I[1]->OriginalColumn + I[1]->ColumnWidth;
Daniel Jasper553a5b02015-07-02 13:08:28 +00001036 IsMultiline = true;
1037 }
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001038 continue;
1039 }
1040
1041 Tokens.resize(Tokens.size() - TokenCount);
1042 Tokens.back()->Type = TT_TemplateString;
Daniel Jasper0d6ac272015-04-16 08:20:51 +00001043 const char *EndOffset =
1044 EndBacktick->TokenText.data() + 1 + CommentBacktickPos;
1045 if (CommentBacktickPos != 0) {
1046 // If the backtick was not the first character (e.g. in a comment),
1047 // re-lex after the backtick position.
1048 SourceLocation Loc = EndBacktick->Tok.getLocation();
1049 resetLexer(SourceMgr.getFileOffset(Loc) + CommentBacktickPos + 1);
1050 }
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001051 Tokens.back()->TokenText =
1052 StringRef(Tokens.back()->TokenText.data(),
1053 EndOffset - Tokens.back()->TokenText.data());
Daniel Jasperf69b9222015-05-02 08:05:38 +00001054
1055 unsigned EndOriginalColumn = EndBacktick->OriginalColumn;
1056 if (EndOriginalColumn == 0) {
1057 SourceLocation Loc = EndBacktick->Tok.getLocation();
1058 EndOriginalColumn = SourceMgr.getSpellingColumnNumber(Loc);
1059 }
1060 // If the ` is further down within the token (e.g. in a comment).
1061 EndOriginalColumn += CommentBacktickPos;
1062
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001063 if (IsMultiline) {
1064 // ColumnWidth is from backtick to last token in line.
1065 // LastLineColumnWidth is 0 to backtick.
1066 // x = `some content
1067 // until here`;
1068 Tokens.back()->ColumnWidth =
1069 EndColumnInFirstLine - Tokens.back()->OriginalColumn;
Daniel Jasper553a5b02015-07-02 13:08:28 +00001070 // +1 for the ` itself.
1071 Tokens.back()->LastLineColumnWidth = EndOriginalColumn + 1;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001072 Tokens.back()->IsMultiline = true;
1073 } else {
1074 // Token simply spans from start to end, +1 for the ` itself.
1075 Tokens.back()->ColumnWidth =
Daniel Jasperf69b9222015-05-02 08:05:38 +00001076 EndOriginalColumn - Tokens.back()->OriginalColumn + 1;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001077 }
1078 return true;
1079 }
1080 return false;
1081 }
1082
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001083 bool tryMerge_TMacro() {
Alexander Kornienko81e32942013-09-16 20:20:49 +00001084 if (Tokens.size() < 4)
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001085 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001086 FormatToken *Last = Tokens.back();
1087 if (!Last->is(tok::r_paren))
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001088 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001089
1090 FormatToken *String = Tokens[Tokens.size() - 2];
1091 if (!String->is(tok::string_literal) || String->IsMultiline)
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001092 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001093
1094 if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001095 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001096
1097 FormatToken *Macro = Tokens[Tokens.size() - 4];
1098 if (Macro->TokenText != "_T")
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001099 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001100
1101 const char *Start = Macro->TokenText.data();
1102 const char *End = Last->TokenText.data() + Last->TokenText.size();
1103 String->TokenText = StringRef(Start, End - Start);
1104 String->IsFirst = Macro->IsFirst;
1105 String->LastNewlineOffset = Macro->LastNewlineOffset;
1106 String->WhitespaceRange = Macro->WhitespaceRange;
1107 String->OriginalColumn = Macro->OriginalColumn;
1108 String->ColumnWidth = encoding::columnWidthWithTabs(
1109 String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding);
Daniel Jaspere99c72f2015-03-26 14:47:35 +00001110 String->NewlinesBefore = Macro->NewlinesBefore;
1111 String->HasUnescapedNewline = Macro->HasUnescapedNewline;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001112
1113 Tokens.pop_back();
1114 Tokens.pop_back();
1115 Tokens.pop_back();
1116 Tokens.back() = String;
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001117 return true;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001118 }
1119
Manuel Klimek68b03042014-04-14 09:14:11 +00001120 bool tryMergeConflictMarkers() {
1121 if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof))
1122 return false;
1123
1124 // Conflict lines look like:
1125 // <marker> <text from the vcs>
1126 // For example:
1127 // >>>>>>> /file/in/file/system at revision 1234
1128 //
1129 // We merge all tokens in a line that starts with a conflict marker
1130 // into a single token with a special token type that the unwrapped line
1131 // parser will use to correctly rebuild the underlying code.
1132
1133 FileID ID;
1134 // Get the position of the first token in the line.
1135 unsigned FirstInLineOffset;
1136 std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc(
1137 Tokens[FirstInLineIndex]->getStartOfNonWhitespace());
1138 StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer();
1139 // Calculate the offset of the start of the current line.
1140 auto LineOffset = Buffer.rfind('\n', FirstInLineOffset);
1141 if (LineOffset == StringRef::npos) {
1142 LineOffset = 0;
1143 } else {
1144 ++LineOffset;
1145 }
1146
1147 auto FirstSpace = Buffer.find_first_of(" \n", LineOffset);
1148 StringRef LineStart;
1149 if (FirstSpace == StringRef::npos) {
1150 LineStart = Buffer.substr(LineOffset);
1151 } else {
1152 LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset);
1153 }
1154
1155 TokenType Type = TT_Unknown;
1156 if (LineStart == "<<<<<<<" || LineStart == ">>>>") {
1157 Type = TT_ConflictStart;
1158 } else if (LineStart == "|||||||" || LineStart == "=======" ||
1159 LineStart == "====") {
1160 Type = TT_ConflictAlternative;
1161 } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") {
1162 Type = TT_ConflictEnd;
1163 }
1164
1165 if (Type != TT_Unknown) {
1166 FormatToken *Next = Tokens.back();
1167
1168 Tokens.resize(FirstInLineIndex + 1);
1169 // We do not need to build a complete token here, as we will skip it
1170 // during parsing anyway (as we must not touch whitespace around conflict
1171 // markers).
1172 Tokens.back()->Type = Type;
1173 Tokens.back()->Tok.setKind(tok::kw___unknown_anytype);
1174
1175 Tokens.push_back(Next);
1176 return true;
1177 }
1178
1179 return false;
1180 }
1181
Jacques Pienaarfc275112015-02-18 23:48:37 +00001182 FormatToken *getStashedToken() {
1183 // Create a synthesized second '>' or '<' token.
1184 Token Tok = FormatTok->Tok;
1185 StringRef TokenText = FormatTok->TokenText;
1186
1187 unsigned OriginalColumn = FormatTok->OriginalColumn;
1188 FormatTok = new (Allocator.Allocate()) FormatToken;
1189 FormatTok->Tok = Tok;
1190 SourceLocation TokLocation =
Jacques Pienaar411b2512015-02-24 23:23:24 +00001191 FormatTok->Tok.getLocation().getLocWithOffset(Tok.getLength() - 1);
1192 FormatTok->Tok.setLocation(TokLocation);
Jacques Pienaarfc275112015-02-18 23:48:37 +00001193 FormatTok->WhitespaceRange = SourceRange(TokLocation, TokLocation);
1194 FormatTok->TokenText = TokenText;
1195 FormatTok->ColumnWidth = 1;
Jacques Pienaar411b2512015-02-24 23:23:24 +00001196 FormatTok->OriginalColumn = OriginalColumn + 1;
1197
Jacques Pienaarfc275112015-02-18 23:48:37 +00001198 return FormatTok;
1199 }
1200
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001201 FormatToken *getNextToken() {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001202 if (GreaterStashed) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001203 GreaterStashed = false;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001204 return getStashedToken();
1205 }
1206 if (LessStashed) {
1207 LessStashed = false;
1208 return getStashedToken();
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001209 }
1210
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001211 FormatTok = new (Allocator.Allocate()) FormatToken;
Daniel Jasper8369aa52013-07-16 20:28:33 +00001212 readRawToken(*FormatTok);
Manuel Klimek9043c742013-05-27 15:23:34 +00001213 SourceLocation WhitespaceStart =
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001214 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
Alexander Kornienko393e3082013-11-13 14:04:17 +00001215 FormatTok->IsFirst = IsFirstToken;
1216 IsFirstToken = false;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001217
1218 // Consume and record whitespace until we find a significant token.
Manuel Klimek9043c742013-05-27 15:23:34 +00001219 unsigned WhitespaceLength = TrailingWhitespace;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001220 while (FormatTok->Tok.is(tok::unknown)) {
Daniel Jaspere2408e32015-05-06 11:16:43 +00001221 StringRef Text = FormatTok->TokenText;
1222 auto EscapesNewline = [&](int pos) {
1223 // A '\r' here is just part of '\r\n'. Skip it.
1224 if (pos >= 0 && Text[pos] == '\r')
1225 --pos;
1226 // See whether there is an odd number of '\' before this.
1227 unsigned count = 0;
1228 for (; pos >= 0; --pos, ++count)
Daniel Jasperf0fd1c62015-05-10 08:00:25 +00001229 if (Text[pos] != '\\')
Daniel Jaspere2408e32015-05-06 11:16:43 +00001230 break;
1231 return count & 1;
1232 };
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001233 // FIXME: This miscounts tok:unknown tokens that are not just
1234 // whitespace, e.g. a '`' character.
Daniel Jaspere2408e32015-05-06 11:16:43 +00001235 for (int i = 0, e = Text.size(); i != e; ++i) {
1236 switch (Text[i]) {
Manuel Klimek31c85922013-08-29 15:21:40 +00001237 case '\n':
1238 ++FormatTok->NewlinesBefore;
Daniel Jaspere2408e32015-05-06 11:16:43 +00001239 FormatTok->HasUnescapedNewline = !EscapesNewline(i - 1);
Manuel Klimek31c85922013-08-29 15:21:40 +00001240 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
1241 Column = 0;
1242 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001243 case '\r':
Daniel Jasper30029c62015-02-05 11:05:31 +00001244 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
1245 Column = 0;
1246 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001247 case '\f':
1248 case '\v':
1249 Column = 0;
1250 break;
Manuel Klimek31c85922013-08-29 15:21:40 +00001251 case ' ':
1252 ++Column;
1253 break;
1254 case '\t':
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +00001255 Column += Style.TabWidth - Column % Style.TabWidth;
Manuel Klimek31c85922013-08-29 15:21:40 +00001256 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001257 case '\\':
Daniel Jaspere2408e32015-05-06 11:16:43 +00001258 if (i + 1 == e || (Text[i + 1] != '\r' && Text[i + 1] != '\n'))
Daniel Jasper877615c2013-10-11 19:45:02 +00001259 FormatTok->Type = TT_ImplicitStringLiteral;
1260 break;
Manuel Klimek31c85922013-08-29 15:21:40 +00001261 default:
Daniel Jasper877615c2013-10-11 19:45:02 +00001262 FormatTok->Type = TT_ImplicitStringLiteral;
Manuel Klimek31c85922013-08-29 15:21:40 +00001263 break;
1264 }
Daniel Jaspere1f72a62016-01-09 21:12:45 +00001265 if (FormatTok->Type == TT_ImplicitStringLiteral)
1266 break;
Manuel Klimek31c85922013-08-29 15:21:40 +00001267 }
1268
Daniel Jaspera98b7b02014-11-25 10:05:17 +00001269 if (FormatTok->is(TT_ImplicitStringLiteral))
Daniel Jasper877615c2013-10-11 19:45:02 +00001270 break;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001271 WhitespaceLength += FormatTok->Tok.getLength();
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001272
Daniel Jasper8369aa52013-07-16 20:28:33 +00001273 readRawToken(*FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001274 }
Manuel Klimekef920692013-01-07 07:56:50 +00001275
Manuel Klimek1abf7892013-01-04 23:34:14 +00001276 // In case the token starts with escaped newlines, we want to
1277 // take them into account as whitespace - this pattern is quite frequent
1278 // in macro definitions.
Manuel Klimek1abf7892013-01-04 23:34:14 +00001279 // FIXME: Add a more explicit test.
Daniel Jasper8369aa52013-07-16 20:28:33 +00001280 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
1281 FormatTok->TokenText[1] == '\n') {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001282 ++FormatTok->NewlinesBefore;
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001283 WhitespaceLength += 2;
Daniel Jaspere2408e32015-05-06 11:16:43 +00001284 FormatTok->LastNewlineOffset = 2;
Manuel Klimek31c85922013-08-29 15:21:40 +00001285 Column = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +00001286 FormatTok->TokenText = FormatTok->TokenText.substr(2);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001287 }
Alexander Kornienko39856b72013-09-10 09:38:25 +00001288
1289 FormatTok->WhitespaceRange = SourceRange(
1290 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
1291
Manuel Klimek31c85922013-08-29 15:21:40 +00001292 FormatTok->OriginalColumn = Column;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001293
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001294 TrailingWhitespace = 0;
1295 if (FormatTok->Tok.is(tok::comment)) {
Manuel Klimek31c85922013-08-29 15:21:40 +00001296 // FIXME: Add the trimmed whitespace to Column.
Daniel Jasper8369aa52013-07-16 20:28:33 +00001297 StringRef UntrimmedText = FormatTok->TokenText;
Alexander Kornienko9ab4a772013-09-06 17:24:54 +00001298 FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
Daniel Jasper8369aa52013-07-16 20:28:33 +00001299 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001300 } else if (FormatTok->Tok.is(tok::raw_identifier)) {
Daniel Jasper8369aa52013-07-16 20:28:33 +00001301 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001302 FormatTok->Tok.setIdentifierInfo(&Info);
1303 FormatTok->Tok.setKind(Info.getTokenID());
Daniel Jasperfe2cf662014-11-19 14:11:11 +00001304 if (Style.Language == FormatStyle::LK_Java &&
Daniel Jasper72a1b6a2015-12-22 15:47:56 +00001305 FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete,
1306 tok::kw_operator)) {
Daniel Jasperfe2cf662014-11-19 14:11:11 +00001307 FormatTok->Tok.setKind(tok::identifier);
1308 FormatTok->Tok.setIdentifierInfo(nullptr);
Daniel Jasper09840ef2015-11-20 15:58:50 +00001309 } else if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasper72a1b6a2015-12-22 15:47:56 +00001310 FormatTok->isOneOf(tok::kw_struct, tok::kw_union,
1311 tok::kw_operator)) {
Daniel Jasper09840ef2015-11-20 15:58:50 +00001312 FormatTok->Tok.setKind(tok::identifier);
1313 FormatTok->Tok.setIdentifierInfo(nullptr);
Daniel Jasperfe2cf662014-11-19 14:11:11 +00001314 }
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001315 } else if (FormatTok->Tok.is(tok::greatergreater)) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001316 FormatTok->Tok.setKind(tok::greater);
Daniel Jasper8369aa52013-07-16 20:28:33 +00001317 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001318 GreaterStashed = true;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001319 } else if (FormatTok->Tok.is(tok::lessless)) {
1320 FormatTok->Tok.setKind(tok::less);
1321 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
1322 LessStashed = true;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001323 }
1324
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001325 // Now FormatTok is the next non-whitespace token.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001326
Alexander Kornienko39856b72013-09-10 09:38:25 +00001327 StringRef Text = FormatTok->TokenText;
1328 size_t FirstNewlinePos = Text.find('\n');
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001329 if (FirstNewlinePos == StringRef::npos) {
1330 // FIXME: ColumnWidth actually depends on the start column, we need to
1331 // take this into account when the token is moved.
1332 FormatTok->ColumnWidth =
1333 encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding);
1334 Column += FormatTok->ColumnWidth;
1335 } else {
Alexander Kornienko39856b72013-09-10 09:38:25 +00001336 FormatTok->IsMultiline = true;
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001337 // FIXME: ColumnWidth actually depends on the start column, we need to
1338 // take this into account when the token is moved.
1339 FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
1340 Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
1341
Alexander Kornienko39856b72013-09-10 09:38:25 +00001342 // The last line of the token always starts in column 0.
1343 // Thus, the length can be precomputed even in the presence of tabs.
1344 FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
1345 Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth,
1346 Encoding);
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001347 Column = FormatTok->LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +00001348 }
Alexander Kornienko39856b72013-09-10 09:38:25 +00001349
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001350 if (Style.Language == FormatStyle::LK_Cpp) {
1351 if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
1352 Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
1353 tok::pp_define) &&
1354 std::find(ForEachMacros.begin(), ForEachMacros.end(),
Eric Liu4cfb88a2016-04-25 15:09:22 +00001355 FormatTok->Tok.getIdentifierInfo()) !=
1356 ForEachMacros.end()) {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001357 FormatTok->Type = TT_ForEachMacro;
1358 } else if (FormatTok->is(tok::identifier)) {
1359 if (MacroBlockBeginRegex.match(Text)) {
1360 FormatTok->Type = TT_MacroBlockBegin;
1361 } else if (MacroBlockEndRegex.match(Text)) {
1362 FormatTok->Type = TT_MacroBlockEnd;
1363 }
1364 }
1365 }
Daniel Jaspere1e43192014-04-01 12:55:11 +00001366
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001367 return FormatTok;
1368 }
1369
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001370 FormatToken *FormatTok;
Alexander Kornienko393e3082013-11-13 14:04:17 +00001371 bool IsFirstToken;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001372 bool GreaterStashed, LessStashed;
Manuel Klimek31c85922013-08-29 15:21:40 +00001373 unsigned Column;
Manuel Klimek9043c742013-05-27 15:23:34 +00001374 unsigned TrailingWhitespace;
Daniel Jasper23376252014-09-09 14:37:39 +00001375 std::unique_ptr<Lexer> Lex;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001376 SourceManager &SourceMgr;
Daniel Jasper23376252014-09-09 14:37:39 +00001377 FileID ID;
Eric Liu4cfb88a2016-04-25 15:09:22 +00001378 const FormatStyle &Style;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001379 IdentifierTable IdentTable;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001380 AdditionalKeywords Keywords;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001381 encoding::Encoding Encoding;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001382 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
Manuel Klimek68b03042014-04-14 09:14:11 +00001383 // Index (in 'Tokens') of the last token that starts a new line.
1384 unsigned FirstInLineIndex;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001385 SmallVector<FormatToken *, 16> Tokens;
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001386 SmallVector<IdentifierInfo *, 8> ForEachMacros;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001387
NAKAMURA Takumi7160c4d2014-08-06 16:53:13 +00001388 bool FormattingDisabled;
Daniel Jasper471894432014-08-06 13:40:26 +00001389
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001390 llvm::Regex MacroBlockBeginRegex;
1391 llvm::Regex MacroBlockEndRegex;
1392
Daniel Jasper8369aa52013-07-16 20:28:33 +00001393 void readRawToken(FormatToken &Tok) {
Daniel Jasper23376252014-09-09 14:37:39 +00001394 Lex->LexFromRawLexer(Tok.Tok);
Daniel Jasper8369aa52013-07-16 20:28:33 +00001395 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
1396 Tok.Tok.getLength());
Daniel Jasper8369aa52013-07-16 20:28:33 +00001397 // For formatting, treat unterminated string literals like normal string
1398 // literals.
Daniel Jasper86fee2f2014-01-31 12:49:42 +00001399 if (Tok.is(tok::unknown)) {
1400 if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') {
1401 Tok.Tok.setKind(tok::string_literal);
1402 Tok.IsUnterminatedLiteral = true;
1403 } else if (Style.Language == FormatStyle::LK_JavaScript &&
1404 Tok.TokenText == "''") {
Daniel Jasperabd1f572016-03-02 22:44:03 +00001405 Tok.Tok.setKind(tok::string_literal);
Daniel Jasper86fee2f2014-01-31 12:49:42 +00001406 }
Daniel Jasper8369aa52013-07-16 20:28:33 +00001407 }
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001408
Daniel Jasperabd1f572016-03-02 22:44:03 +00001409 if (Style.Language == FormatStyle::LK_JavaScript &&
1410 Tok.is(tok::char_constant)) {
1411 Tok.Tok.setKind(tok::string_literal);
1412 }
1413
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001414 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" ||
1415 Tok.TokenText == "/* clang-format on */")) {
Daniel Jasper471894432014-08-06 13:40:26 +00001416 FormattingDisabled = false;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001417 }
1418
Daniel Jasper471894432014-08-06 13:40:26 +00001419 Tok.Finalized = FormattingDisabled;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001420
1421 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" ||
1422 Tok.TokenText == "/* clang-format off */")) {
Daniel Jasper471894432014-08-06 13:40:26 +00001423 FormattingDisabled = true;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001424 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001425 }
Daniel Jasper49a9a282014-10-29 16:51:38 +00001426
1427 void resetLexer(unsigned Offset) {
1428 StringRef Buffer = SourceMgr.getBufferData(ID);
1429 Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID),
1430 getFormattingLangOpts(Style), Buffer.begin(),
1431 Buffer.begin() + Offset, Buffer.end()));
1432 Lex->SetKeepWhitespaceMode(true);
Daniel Jasper55c384e2015-07-02 14:01:34 +00001433 TrailingWhitespace = 0;
Daniel Jasper49a9a282014-10-29 16:51:38 +00001434 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001435};
1436
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001437static StringRef getLanguageName(FormatStyle::LanguageKind Language) {
1438 switch (Language) {
1439 case FormatStyle::LK_Cpp:
1440 return "C++";
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001441 case FormatStyle::LK_Java:
1442 return "Java";
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001443 case FormatStyle::LK_JavaScript:
1444 return "JavaScript";
Daniel Jasper7052ce62014-01-19 09:04:08 +00001445 case FormatStyle::LK_Proto:
1446 return "Proto";
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001447 default:
1448 return "Unknown";
1449 }
1450}
1451
Eric Liu4cfb88a2016-04-25 15:09:22 +00001452class Environment {
Daniel Jasperf7935112012-12-03 18:12:45 +00001453public:
Eric Liu4cfb88a2016-04-25 15:09:22 +00001454 Environment(const FormatStyle &Style, SourceManager &SM, FileID ID,
1455 ArrayRef<CharSourceRange> Ranges)
1456 : Style(Style), ID(ID), CharRanges(Ranges.begin(), Ranges.end()), SM(SM) {
1457 }
1458
1459 Environment(const FormatStyle &Style, FileID ID,
1460 std::unique_ptr<FileManager> FileMgr,
1461 std::unique_ptr<SourceManager> VirtualSM,
1462 std::unique_ptr<DiagnosticsEngine> Diagnostics,
Eric Liuc5cad392016-04-28 07:51:47 +00001463 const std::vector<CharSourceRange> &CharRanges)
Eric Liu4cfb88a2016-04-25 15:09:22 +00001464 : Style(Style), ID(ID), CharRanges(CharRanges.begin(), CharRanges.end()),
1465 SM(*VirtualSM), FileMgr(std::move(FileMgr)),
1466 VirtualSM(std::move(VirtualSM)), Diagnostics(std::move(Diagnostics)) {}
1467
1468 // This sets up an virtual file system with file \p FileName containing \p
1469 // Code.
1470 static std::unique_ptr<Environment>
1471 CreateVirtualEnvironment(const FormatStyle &Style, StringRef Code,
1472 StringRef FileName,
1473 ArrayRef<tooling::Range> Ranges) {
1474 // This is referenced by `FileMgr` and will be released by `FileMgr` when it
1475 // is deleted.
1476 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
1477 new vfs::InMemoryFileSystem);
1478 // This is passed to `SM` as reference, so the pointer has to be referenced
1479 // in `Environment` so that `FileMgr` can out-live this function scope.
1480 std::unique_ptr<FileManager> FileMgr(
1481 new FileManager(FileSystemOptions(), InMemoryFileSystem));
1482 // This is passed to `SM` as reference, so the pointer has to be referenced
1483 // by `Environment` due to the same reason above.
1484 std::unique_ptr<DiagnosticsEngine> Diagnostics(new DiagnosticsEngine(
1485 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1486 new DiagnosticOptions));
1487 // This will be stored as reference, so the pointer has to be stored in
1488 // due to the same reason above.
1489 std::unique_ptr<SourceManager> VirtualSM(
1490 new SourceManager(*Diagnostics, *FileMgr));
1491 InMemoryFileSystem->addFile(
1492 FileName, 0, llvm::MemoryBuffer::getMemBuffer(
1493 Code, FileName, /*RequiresNullTerminator=*/false));
1494 FileID ID = VirtualSM->createFileID(
1495 FileMgr->getFile(FileName), SourceLocation(), clang::SrcMgr::C_User);
1496 assert(ID.isValid());
1497 SourceLocation StartOfFile = VirtualSM->getLocForStartOfFile(ID);
1498 std::vector<CharSourceRange> CharRanges;
1499 for (const tooling::Range &Range : Ranges) {
1500 SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset());
1501 SourceLocation End = Start.getLocWithOffset(Range.getLength());
1502 CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1503 }
1504 return llvm::make_unique<Environment>(Style, ID, std::move(FileMgr),
1505 std::move(VirtualSM),
1506 std::move(Diagnostics), CharRanges);
1507 }
1508
1509 FormatStyle &getFormatStyle() { return Style; }
1510
1511 const FormatStyle &getFormatStyle() const { return Style; }
1512
1513 FileID getFileID() const { return ID; }
1514
1515 StringRef getFileName() const { return FileName; }
1516
1517 ArrayRef<CharSourceRange> getCharRanges() const { return CharRanges; }
1518
1519 SourceManager &getSourceManager() { return SM; }
1520
1521private:
1522 FormatStyle Style;
1523 FileID ID;
1524 StringRef FileName;
1525 SmallVector<CharSourceRange, 8> CharRanges;
1526 SourceManager &SM;
1527
1528 // The order of these fields are important - they should be in the same order
1529 // as they are created in `CreateVirtualEnvironment` so that they can be
1530 // deleted in the reverse order as they are created.
1531 std::unique_ptr<FileManager> FileMgr;
1532 std::unique_ptr<SourceManager> VirtualSM;
1533 std::unique_ptr<DiagnosticsEngine> Diagnostics;
1534};
1535
1536class TokenAnalyzer : public UnwrappedLineConsumer {
1537public:
1538 TokenAnalyzer(Environment &Env)
1539 : Env(Env), AffectedRangeMgr(Env.getSourceManager(), Env.getCharRanges()),
1540 UnwrappedLines(1),
1541 Encoding(encoding::detectEncoding(
1542 Env.getSourceManager().getBufferData(Env.getFileID()))) {
Daniel Jasperfa21c072013-07-15 14:33:14 +00001543 DEBUG(llvm::dbgs() << "File encoding: "
1544 << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
1545 : "unknown")
1546 << "\n");
Eric Liu4cfb88a2016-04-25 15:09:22 +00001547 DEBUG(llvm::dbgs() << "Language: "
1548 << getLanguageName(Env.getFormatStyle().Language)
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001549 << "\n");
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001550 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001551
Eric Liu4cfb88a2016-04-25 15:09:22 +00001552 tooling::Replacements process() {
Manuel Klimek71814b42013-10-11 21:25:45 +00001553 tooling::Replacements Result;
Eric Liu4cfb88a2016-04-25 15:09:22 +00001554 FormatTokenLexer Tokens(Env.getSourceManager(), Env.getFileID(),
1555 Env.getFormatStyle(), Encoding);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001556
Eric Liu4cfb88a2016-04-25 15:09:22 +00001557 UnwrappedLineParser Parser(Env.getFormatStyle(), Tokens.getKeywords(),
1558 Tokens.lex(), *this);
Manuel Klimek20e0af62015-05-06 11:56:29 +00001559 Parser.parse();
Manuel Klimek71814b42013-10-11 21:25:45 +00001560 assert(UnwrappedLines.rbegin()->empty());
1561 for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE;
1562 ++Run) {
1563 DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
1564 SmallVector<AnnotatedLine *, 16> AnnotatedLines;
Eric Liu4cfb88a2016-04-25 15:09:22 +00001565
1566 TokenAnnotator Annotator(Env.getFormatStyle(), Tokens.getKeywords());
Manuel Klimek71814b42013-10-11 21:25:45 +00001567 for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) {
1568 AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i]));
Eric Liu4cfb88a2016-04-25 15:09:22 +00001569 Annotator.annotate(*AnnotatedLines.back());
Manuel Klimek71814b42013-10-11 21:25:45 +00001570 }
Eric Liu4cfb88a2016-04-25 15:09:22 +00001571
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001572 tooling::Replacements RunResult =
Eric Liu4cfb88a2016-04-25 15:09:22 +00001573 analyze(Annotator, AnnotatedLines, Tokens, Result);
1574
Manuel Klimek71814b42013-10-11 21:25:45 +00001575 DEBUG({
1576 llvm::dbgs() << "Replacements for run " << Run << ":\n";
1577 for (tooling::Replacements::iterator I = RunResult.begin(),
1578 E = RunResult.end();
1579 I != E; ++I) {
1580 llvm::dbgs() << I->toString() << "\n";
1581 }
1582 });
1583 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1584 delete AnnotatedLines[i];
1585 }
1586 Result.insert(RunResult.begin(), RunResult.end());
Manuel Klimek71814b42013-10-11 21:25:45 +00001587 }
1588 return Result;
1589 }
1590
Eric Liu4cfb88a2016-04-25 15:09:22 +00001591protected:
1592 virtual tooling::Replacements
1593 analyze(TokenAnnotator &Annotator,
1594 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
1595 FormatTokenLexer &Tokens, tooling::Replacements &Result) = 0;
1596
1597 void consumeUnwrappedLine(const UnwrappedLine &TheLine) override {
1598 assert(!UnwrappedLines.empty());
1599 UnwrappedLines.back().push_back(TheLine);
1600 }
1601
1602 void finishRun() override {
1603 UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
1604 }
1605
1606 // Stores Style, FileID and SourceManager etc.
1607 Environment &Env;
1608 // AffectedRangeMgr stores ranges to be fixed.
1609 AffectedRangeManager AffectedRangeMgr;
1610 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
1611 encoding::Encoding Encoding;
1612};
1613
1614class Formatter : public TokenAnalyzer {
1615public:
1616 Formatter(Environment &Env, bool *IncompleteFormat)
1617 : TokenAnalyzer(Env), IncompleteFormat(IncompleteFormat) {}
1618
1619 tooling::Replacements
1620 analyze(TokenAnnotator &Annotator,
1621 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
1622 FormatTokenLexer &Tokens, tooling::Replacements &Result) override {
Manuel Klimek71814b42013-10-11 21:25:45 +00001623 deriveLocalStyle(AnnotatedLines);
Eric Liu4cfb88a2016-04-25 15:09:22 +00001624 AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(),
1625 AnnotatedLines.end());
1626
1627 if (Env.getFormatStyle().Language == FormatStyle::LK_JavaScript &&
1628 Env.getFormatStyle().JavaScriptQuotes != FormatStyle::JSQS_Leave)
Daniel Jasper97439922016-03-17 13:03:41 +00001629 requoteJSStringLiteral(AnnotatedLines, Result);
1630
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001631 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001632 Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001633 }
Daniel Jasperb67cc422013-04-09 17:46:55 +00001634
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001635 Annotator.setCommentLineLevels(AnnotatedLines);
Eric Liu4cfb88a2016-04-25 15:09:22 +00001636
1637 WhitespaceManager Whitespaces(
1638 Env.getSourceManager(), Env.getFormatStyle(),
1639 inputUsesCRLF(Env.getSourceManager().getBufferData(Env.getFileID())));
1640 ContinuationIndenter Indenter(Env.getFormatStyle(), Tokens.getKeywords(),
1641 Env.getSourceManager(), Whitespaces, Encoding,
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001642 BinPackInconclusiveFunctions);
Eric Liu4cfb88a2016-04-25 15:09:22 +00001643 UnwrappedLineFormatter(&Indenter, &Whitespaces, Env.getFormatStyle(),
1644 Tokens.getKeywords(), IncompleteFormat)
Manuel Klimekd3585db2015-05-11 08:21:35 +00001645 .format(AnnotatedLines);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001646 return Whitespaces.generateReplacements();
1647 }
1648
1649private:
Daniel Jasper97439922016-03-17 13:03:41 +00001650 // If the last token is a double/single-quoted string literal, generates a
1651 // replacement with a single/double quoted string literal, re-escaping the
1652 // contents in the process.
1653 void requoteJSStringLiteral(SmallVectorImpl<AnnotatedLine *> &Lines,
Eric Liu4cfb88a2016-04-25 15:09:22 +00001654 tooling::Replacements &Result) {
Daniel Jasper97439922016-03-17 13:03:41 +00001655 for (AnnotatedLine *Line : Lines) {
1656 requoteJSStringLiteral(Line->Children, Result);
1657 if (!Line->Affected)
1658 continue;
1659 for (FormatToken *FormatTok = Line->First; FormatTok;
1660 FormatTok = FormatTok->Next) {
1661 StringRef Input = FormatTok->TokenText;
1662 if (!FormatTok->isStringLiteral() ||
1663 // NB: testing for not starting with a double quote to avoid
1664 // breaking
1665 // `template strings`.
Eric Liu4cfb88a2016-04-25 15:09:22 +00001666 (Env.getFormatStyle().JavaScriptQuotes ==
1667 FormatStyle::JSQS_Single &&
Daniel Jasper97439922016-03-17 13:03:41 +00001668 !Input.startswith("\"")) ||
Eric Liu4cfb88a2016-04-25 15:09:22 +00001669 (Env.getFormatStyle().JavaScriptQuotes ==
1670 FormatStyle::JSQS_Double &&
Daniel Jasper97439922016-03-17 13:03:41 +00001671 !Input.startswith("\'")))
1672 continue;
1673
1674 // Change start and end quote.
Eric Liu4cfb88a2016-04-25 15:09:22 +00001675 bool IsSingle =
1676 Env.getFormatStyle().JavaScriptQuotes == FormatStyle::JSQS_Single;
Daniel Jasper97439922016-03-17 13:03:41 +00001677 SourceLocation Start = FormatTok->Tok.getLocation();
1678 auto Replace = [&](SourceLocation Start, unsigned Length,
1679 StringRef ReplacementText) {
Eric Liu4cfb88a2016-04-25 15:09:22 +00001680 Result.insert(tooling::Replacement(Env.getSourceManager(), Start,
1681 Length, ReplacementText));
Daniel Jasper97439922016-03-17 13:03:41 +00001682 };
1683 Replace(Start, 1, IsSingle ? "'" : "\"");
1684 Replace(FormatTok->Tok.getEndLoc().getLocWithOffset(-1), 1,
1685 IsSingle ? "'" : "\"");
1686
1687 // Escape internal quotes.
1688 size_t ColumnWidth = FormatTok->TokenText.size();
1689 bool Escaped = false;
1690 for (size_t i = 1; i < Input.size() - 1; i++) {
1691 switch (Input[i]) {
Eric Liu4cfb88a2016-04-25 15:09:22 +00001692 case '\\':
1693 if (!Escaped && i + 1 < Input.size() &&
1694 ((IsSingle && Input[i + 1] == '"') ||
1695 (!IsSingle && Input[i + 1] == '\''))) {
1696 // Remove this \, it's escaping a " or ' that no longer needs
1697 // escaping
1698 ColumnWidth--;
1699 Replace(Start.getLocWithOffset(i), 1, "");
1700 continue;
1701 }
1702 Escaped = !Escaped;
1703 break;
1704 case '\"':
1705 case '\'':
1706 if (!Escaped && IsSingle == (Input[i] == '\'')) {
1707 // Escape the quote.
1708 Replace(Start.getLocWithOffset(i), 0, "\\");
1709 ColumnWidth++;
1710 }
1711 Escaped = false;
1712 break;
1713 default:
1714 Escaped = false;
1715 break;
Daniel Jasper97439922016-03-17 13:03:41 +00001716 }
1717 }
1718
1719 // For formatting, count the number of non-escaped single quotes in them
1720 // and adjust ColumnWidth to take the added escapes into account.
Eric Liu4cfb88a2016-04-25 15:09:22 +00001721 // FIXME(martinprobst): this might conflict with code breaking a long
1722 // string literal (which clang-format doesn't do, yet). For that to
1723 // work, this code would have to modify TokenText directly.
Daniel Jasper97439922016-03-17 13:03:41 +00001724 FormatTok->ColumnWidth = ColumnWidth;
1725 }
1726 }
1727 }
1728
Alexander Kornienko9e649af2013-09-11 12:25:57 +00001729 static bool inputUsesCRLF(StringRef Text) {
1730 return Text.count('\r') * 2 > Text.count('\n');
1731 }
1732
Daniel Jasper352f0df2015-07-18 16:35:30 +00001733 bool
1734 hasCpp03IncompatibleFormat(const SmallVectorImpl<AnnotatedLine *> &Lines) {
Eric Liu4cfb88a2016-04-25 15:09:22 +00001735 for (const AnnotatedLine *Line : Lines) {
Daniel Jasper352f0df2015-07-18 16:35:30 +00001736 if (hasCpp03IncompatibleFormat(Line->Children))
1737 return true;
1738 for (FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next) {
1739 if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
1740 if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener))
1741 return true;
1742 if (Tok->is(TT_TemplateCloser) &&
1743 Tok->Previous->is(TT_TemplateCloser))
1744 return true;
1745 }
1746 }
1747 }
1748 return false;
1749 }
1750
1751 int countVariableAlignments(const SmallVectorImpl<AnnotatedLine *> &Lines) {
1752 int AlignmentDiff = 0;
Eric Liu4cfb88a2016-04-25 15:09:22 +00001753 for (const AnnotatedLine *Line : Lines) {
Daniel Jasper352f0df2015-07-18 16:35:30 +00001754 AlignmentDiff += countVariableAlignments(Line->Children);
1755 for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) {
1756 if (!Tok->is(TT_PointerOrReference))
1757 continue;
1758 bool SpaceBefore =
1759 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
1760 bool SpaceAfter = Tok->Next->WhitespaceRange.getBegin() !=
1761 Tok->Next->WhitespaceRange.getEnd();
1762 if (SpaceBefore && !SpaceAfter)
1763 ++AlignmentDiff;
1764 if (!SpaceBefore && SpaceAfter)
1765 --AlignmentDiff;
1766 }
1767 }
1768 return AlignmentDiff;
1769 }
1770
Manuel Klimek71814b42013-10-11 21:25:45 +00001771 void
1772 deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001773 bool HasBinPackedFunction = false;
1774 bool HasOnePerLineFunction = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001775 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001776 if (!AnnotatedLines[i]->First->Next)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001777 continue;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001778 FormatToken *Tok = AnnotatedLines[i]->First->Next;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001779 while (Tok->Next) {
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001780 if (Tok->PackingKind == PPK_BinPacked)
1781 HasBinPackedFunction = true;
1782 if (Tok->PackingKind == PPK_OnePerLine)
1783 HasOnePerLineFunction = true;
1784
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001785 Tok = Tok->Next;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001786 }
1787 }
Eric Liu4cfb88a2016-04-25 15:09:22 +00001788 if (Env.getFormatStyle().DerivePointerAlignment)
1789 Env.getFormatStyle().PointerAlignment =
1790 countVariableAlignments(AnnotatedLines) <= 0 ? FormatStyle::PAS_Left
1791 : FormatStyle::PAS_Right;
1792 if (Env.getFormatStyle().Standard == FormatStyle::LS_Auto)
1793 Env.getFormatStyle().Standard = hasCpp03IncompatibleFormat(AnnotatedLines)
1794 ? FormatStyle::LS_Cpp11
1795 : FormatStyle::LS_Cpp03;
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001796 BinPackInconclusiveFunctions =
1797 HasBinPackedFunction || !HasOnePerLineFunction;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001798 }
1799
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001800 bool BinPackInconclusiveFunctions;
Eric Liu4cfb88a2016-04-25 15:09:22 +00001801 bool *IncompleteFormat;
1802};
1803
1804// This class clean up the erroneous/redundant code around the given ranges in
1805// file.
1806class Cleaner : public TokenAnalyzer {
1807public:
1808 Cleaner(Environment &Env)
1809 : TokenAnalyzer(Env),
1810 DeletedTokens(FormatTokenLess(Env.getSourceManager())) {}
1811
1812 // FIXME: eliminate unused parameters.
1813 tooling::Replacements
1814 analyze(TokenAnnotator &Annotator,
1815 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
1816 FormatTokenLexer &Tokens, tooling::Replacements &Result) override {
1817 // FIXME: in the current implementation the granularity of affected range
1818 // is an annotated line. However, this is not sufficient. Furthermore,
1819 // redundant code introduced by replacements does not necessarily
1820 // intercept with ranges of replacements that result in the redundancy.
1821 // To determine if some redundant code is actually introduced by
1822 // replacements(e.g. deletions), we need to come up with a more
1823 // sophisticated way of computing affected ranges.
1824 AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(),
1825 AnnotatedLines.end());
1826
1827 checkEmptyNamespace(AnnotatedLines);
1828
1829 return generateFixes();
1830 }
1831
1832private:
1833 bool containsOnlyComments(const AnnotatedLine &Line) {
1834 for (FormatToken *Tok = Line.First; Tok != nullptr; Tok = Tok->Next) {
1835 if (Tok->isNot(tok::comment))
1836 return false;
1837 }
1838 return true;
1839 }
1840
1841 // Iterate through all lines and remove any empty (nested) namespaces.
1842 void checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
1843 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1844 auto &Line = *AnnotatedLines[i];
1845 if (Line.startsWith(tok::kw_namespace) ||
1846 Line.startsWith(tok::kw_inline, tok::kw_namespace)) {
1847 checkEmptyNamespace(AnnotatedLines, i, i);
1848 }
1849 }
1850
1851 for (auto Line : DeletedLines) {
1852 FormatToken *Tok = AnnotatedLines[Line]->First;
1853 while (Tok) {
1854 deleteToken(Tok);
1855 Tok = Tok->Next;
1856 }
1857 }
1858 }
1859
1860 // The function checks if the namespace, which starts from \p CurrentLine, and
1861 // its nested namespaces are empty and delete them if they are empty. It also
1862 // sets \p NewLine to the last line checked.
1863 // Returns true if the current namespace is empty.
1864 bool checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
1865 unsigned CurrentLine, unsigned &NewLine) {
1866 unsigned InitLine = CurrentLine, End = AnnotatedLines.size();
1867 if (Env.getFormatStyle().BraceWrapping.AfterNamespace) {
1868 // If the left brace is in a new line, we should consume it first so that
1869 // it does not make the namespace non-empty.
1870 // FIXME: error handling if there is no left brace.
1871 if (!AnnotatedLines[++CurrentLine]->startsWith(tok::l_brace)) {
1872 NewLine = CurrentLine;
1873 return false;
1874 }
1875 } else if (!AnnotatedLines[CurrentLine]->endsWith(tok::l_brace)) {
1876 return false;
1877 }
1878 while (++CurrentLine < End) {
1879 if (AnnotatedLines[CurrentLine]->startsWith(tok::r_brace))
1880 break;
1881
1882 if (AnnotatedLines[CurrentLine]->startsWith(tok::kw_namespace) ||
1883 AnnotatedLines[CurrentLine]->startsWith(tok::kw_inline,
1884 tok::kw_namespace)) {
1885 if (!checkEmptyNamespace(AnnotatedLines, CurrentLine, NewLine))
1886 return false;
1887 CurrentLine = NewLine;
1888 continue;
1889 }
1890
1891 if (containsOnlyComments(*AnnotatedLines[CurrentLine]))
1892 continue;
1893
1894 // If there is anything other than comments or nested namespaces in the
1895 // current namespace, the namespace cannot be empty.
1896 NewLine = CurrentLine;
1897 return false;
1898 }
1899
1900 NewLine = CurrentLine;
1901 if (CurrentLine >= End)
1902 return false;
1903
1904 // Check if the empty namespace is actually affected by changed ranges.
1905 if (!AffectedRangeMgr.affectsCharSourceRange(CharSourceRange::getCharRange(
1906 AnnotatedLines[InitLine]->First->Tok.getLocation(),
1907 AnnotatedLines[CurrentLine]->Last->Tok.getEndLoc())))
1908 return false;
1909
1910 for (unsigned i = InitLine; i <= CurrentLine; ++i) {
1911 DeletedLines.insert(i);
1912 }
1913
1914 return true;
1915 }
1916
1917 // Delete the given token.
1918 inline void deleteToken(FormatToken *Tok) {
1919 if (Tok)
1920 DeletedTokens.insert(Tok);
1921 }
1922
1923 tooling::Replacements generateFixes() {
1924 tooling::Replacements Fixes;
1925 std::vector<FormatToken *> Tokens;
1926 std::copy(DeletedTokens.begin(), DeletedTokens.end(),
1927 std::back_inserter(Tokens));
1928
1929 // Merge multiple continuous token deletions into one big deletion so that
1930 // the number of replacements can be reduced. This makes computing affected
1931 // ranges more efficient when we run reformat on the changed code.
1932 unsigned Idx = 0;
1933 while (Idx < Tokens.size()) {
1934 unsigned St = Idx, End = Idx;
1935 while ((End + 1) < Tokens.size() &&
1936 Tokens[End]->Next == Tokens[End + 1]) {
1937 End++;
1938 }
1939 auto SR = CharSourceRange::getCharRange(Tokens[St]->Tok.getLocation(),
1940 Tokens[End]->Tok.getEndLoc());
1941 Fixes.insert(tooling::Replacement(Env.getSourceManager(), SR, ""));
1942 Idx = End + 1;
1943 }
1944
1945 return Fixes;
1946 }
1947
1948 // Class for less-than inequality comparason for the set `RedundantTokens`.
1949 // We store tokens in the order they appear in the translation unit so that
1950 // we do not need to sort them in `generateFixes()`.
1951 struct FormatTokenLess {
1952 FormatTokenLess(SourceManager &SM) : SM(SM) {}
1953
1954 bool operator()(const FormatToken *LHS, const FormatToken *RHS) {
1955 return SM.isBeforeInTranslationUnit(LHS->Tok.getLocation(),
1956 RHS->Tok.getLocation());
1957 }
1958 SourceManager &SM;
1959 };
1960
1961 // Tokens to be deleted.
1962 std::set<FormatToken *, FormatTokenLess> DeletedTokens;
1963 // The line numbers of lines to be deleted.
1964 std::set<unsigned> DeletedLines;
Daniel Jasperf7935112012-12-03 18:12:45 +00001965};
1966
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001967struct IncludeDirective {
1968 StringRef Filename;
1969 StringRef Text;
1970 unsigned Offset;
Daniel Jasperd2629dc2015-12-16 10:10:16 +00001971 int Category;
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001972};
1973
Craig Topperaf35e852013-06-30 22:29:28 +00001974} // end anonymous namespace
1975
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001976// Determines whether 'Ranges' intersects with ('Start', 'End').
1977static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start,
1978 unsigned End) {
1979 for (auto Range : Ranges) {
1980 if (Range.getOffset() < End &&
1981 Range.getOffset() + Range.getLength() > Start)
1982 return true;
1983 }
1984 return false;
1985}
1986
1987// Sorts a block of includes given by 'Includes' alphabetically adding the
1988// necessary replacement to 'Replaces'. 'Includes' must be in strict source
1989// order.
1990static void sortIncludes(const FormatStyle &Style,
1991 const SmallVectorImpl<IncludeDirective> &Includes,
1992 ArrayRef<tooling::Range> Ranges, StringRef FileName,
Daniel Jasperb68aabf2015-11-23 08:36:35 +00001993 tooling::Replacements &Replaces, unsigned *Cursor) {
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001994 if (!affectsRange(Ranges, Includes.front().Offset,
1995 Includes.back().Offset + Includes.back().Text.size()))
1996 return;
1997 SmallVector<unsigned, 16> Indices;
1998 for (unsigned i = 0, e = Includes.size(); i != e; ++i)
1999 Indices.push_back(i);
Daniel Jasper94a96fc2016-03-03 17:34:14 +00002000 std::stable_sort(
2001 Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) {
2002 return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) <
2003 std::tie(Includes[RHSI].Category, Includes[RHSI].Filename);
2004 });
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002005
2006 // If the #includes are out of order, we generate a single replacement fixing
2007 // the entire block. Otherwise, no replacement is generated.
2008 bool OutOfOrder = false;
2009 for (unsigned i = 1, e = Indices.size(); i != e; ++i) {
2010 if (Indices[i] != i) {
2011 OutOfOrder = true;
2012 break;
2013 }
2014 }
2015 if (!OutOfOrder)
2016 return;
2017
Daniel Jasperb68aabf2015-11-23 08:36:35 +00002018 std::string result;
2019 bool CursorMoved = false;
2020 for (unsigned Index : Indices) {
2021 if (!result.empty())
2022 result += "\n";
2023 result += Includes[Index].Text;
2024
2025 if (Cursor && !CursorMoved) {
2026 unsigned Start = Includes[Index].Offset;
2027 unsigned End = Start + Includes[Index].Text.size();
2028 if (*Cursor >= Start && *Cursor < End) {
2029 *Cursor = Includes.front().Offset + result.size() + *Cursor - End;
2030 CursorMoved = true;
2031 }
2032 }
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002033 }
2034
2035 // Sorting #includes shouldn't change their total number of characters.
2036 // This would otherwise mess up 'Ranges'.
2037 assert(result.size() ==
2038 Includes.back().Offset + Includes.back().Text.size() -
2039 Includes.front().Offset);
2040
2041 Replaces.insert(tooling::Replacement(FileName, Includes.front().Offset,
2042 result.size(), result));
2043}
2044
2045tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
2046 ArrayRef<tooling::Range> Ranges,
Daniel Jasperb68aabf2015-11-23 08:36:35 +00002047 StringRef FileName, unsigned *Cursor) {
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002048 tooling::Replacements Replaces;
Daniel Jasperda446772015-11-16 12:38:56 +00002049 if (!Style.SortIncludes)
2050 return Replaces;
2051
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002052 unsigned Prev = 0;
2053 unsigned SearchFrom = 0;
Daniel Jasper85c472d2015-09-29 07:53:08 +00002054 llvm::Regex IncludeRegex(
Nico Weberff063702015-10-21 17:13:45 +00002055 R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))");
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002056 SmallVector<StringRef, 4> Matches;
2057 SmallVector<IncludeDirective, 16> IncludesInBlock;
Daniel Jasper85c472d2015-09-29 07:53:08 +00002058
2059 // In compiled files, consider the first #include to be the main #include of
2060 // the file if it is not a system #include. This ensures that the header
2061 // doesn't have hidden dependencies
2062 // (http://llvm.org/docs/CodingStandards.html#include-style).
2063 //
2064 // FIXME: Do some sanity checking, e.g. edit distance of the base name, to fix
2065 // cases where the first #include is unlikely to be the main header.
Daniel Jasper0bfdeb42015-12-21 12:14:17 +00002066 bool IsSource = FileName.endswith(".c") || FileName.endswith(".cc") ||
2067 FileName.endswith(".cpp") || FileName.endswith(".c++") ||
2068 FileName.endswith(".cxx") || FileName.endswith(".m") ||
2069 FileName.endswith(".mm");
2070 StringRef FileStem = llvm::sys::path::stem(FileName);
Daniel Jasper32d75fa2015-12-21 13:40:49 +00002071 bool FirstIncludeBlock = true;
Daniel Jaspera252f5d2015-12-21 17:28:24 +00002072 bool MainIncludeFound = false;
Daniel Jasper85c472d2015-09-29 07:53:08 +00002073
2074 // Create pre-compiled regular expressions for the #include categories.
2075 SmallVector<llvm::Regex, 4> CategoryRegexs;
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +00002076 for (const auto &Category : Style.IncludeCategories)
2077 CategoryRegexs.emplace_back(Category.Regex);
Daniel Jasper85c472d2015-09-29 07:53:08 +00002078
Daniel Jasper9b8c7c72015-11-21 09:17:08 +00002079 bool FormattingOff = false;
2080
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002081 for (;;) {
2082 auto Pos = Code.find('\n', SearchFrom);
2083 StringRef Line =
2084 Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
Daniel Jasper9b8c7c72015-11-21 09:17:08 +00002085
2086 StringRef Trimmed = Line.trim();
2087 if (Trimmed == "// clang-format off")
2088 FormattingOff = true;
2089 else if (Trimmed == "// clang-format on")
2090 FormattingOff = false;
2091
2092 if (!FormattingOff && !Line.endswith("\\")) {
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002093 if (IncludeRegex.match(Line, &Matches)) {
Nico Weberff063702015-10-21 17:13:45 +00002094 StringRef IncludeName = Matches[2];
Daniel Jasper0bfdeb42015-12-21 12:14:17 +00002095 int Category = INT_MAX;
2096 for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i) {
2097 if (CategoryRegexs[i].match(IncludeName)) {
2098 Category = Style.IncludeCategories[i].Priority;
2099 break;
Daniel Jasper85c472d2015-09-29 07:53:08 +00002100 }
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002101 }
Daniel Jaspera252f5d2015-12-21 17:28:24 +00002102 if (IsSource && !MainIncludeFound && Category > 0 &&
2103 FirstIncludeBlock && IncludeName.startswith("\"")) {
Daniel Jasper0bfdeb42015-12-21 12:14:17 +00002104 StringRef HeaderStem =
2105 llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
Daniel Jaspera252f5d2015-12-21 17:28:24 +00002106 if (FileStem.startswith(HeaderStem)) {
Daniel Jasper9c8ff352016-03-21 14:11:27 +00002107 llvm::Regex MainIncludeRegex(
2108 (HeaderStem + Style.IncludeIsMainRegex).str());
2109 if (MainIncludeRegex.match(FileStem)) {
2110 Category = 0;
2111 MainIncludeFound = true;
2112 }
Daniel Jaspera252f5d2015-12-21 17:28:24 +00002113 }
Daniel Jasper0bfdeb42015-12-21 12:14:17 +00002114 }
Nico Weberff063702015-10-21 17:13:45 +00002115 IncludesInBlock.push_back({IncludeName, Line, Prev, Category});
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002116 } else if (!IncludesInBlock.empty()) {
Daniel Jasperb68aabf2015-11-23 08:36:35 +00002117 sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces,
2118 Cursor);
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002119 IncludesInBlock.clear();
Daniel Jasper32d75fa2015-12-21 13:40:49 +00002120 FirstIncludeBlock = false;
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002121 }
2122 Prev = Pos + 1;
2123 }
2124 if (Pos == StringRef::npos || Pos + 1 == Code.size())
2125 break;
2126 SearchFrom = Pos + 1;
2127 }
2128 if (!IncludesInBlock.empty())
Daniel Jasperb68aabf2015-11-23 08:36:35 +00002129 sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces, Cursor);
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00002130 return Replaces;
2131}
2132
Eric Liu4cfb88a2016-04-25 15:09:22 +00002133template <typename T>
2134static tooling::Replacements
2135processReplacements(T ProcessFunc, StringRef Code,
2136 const tooling::Replacements &Replaces,
2137 const FormatStyle &Style) {
Manuel Klimekb12e5a52016-03-01 12:37:30 +00002138 if (Replaces.empty())
2139 return tooling::Replacements();
2140
2141 std::string NewCode = applyAllReplacements(Code, Replaces);
2142 std::vector<tooling::Range> ChangedRanges =
Eric Liu4c1ef97a2016-03-29 16:31:53 +00002143 tooling::calculateChangedRanges(Replaces);
Manuel Klimekb12e5a52016-03-01 12:37:30 +00002144 StringRef FileName = Replaces.begin()->getFilePath();
Eric Liu4cfb88a2016-04-25 15:09:22 +00002145
Manuel Klimekb12e5a52016-03-01 12:37:30 +00002146 tooling::Replacements FormatReplaces =
Eric Liu4cfb88a2016-04-25 15:09:22 +00002147 ProcessFunc(Style, NewCode, ChangedRanges, FileName);
Manuel Klimekb12e5a52016-03-01 12:37:30 +00002148
Eric Liu4cfb88a2016-04-25 15:09:22 +00002149 return mergeReplacements(Replaces, FormatReplaces);
Manuel Klimekb12e5a52016-03-01 12:37:30 +00002150}
2151
Eric Liu4cfb88a2016-04-25 15:09:22 +00002152tooling::Replacements formatReplacements(StringRef Code,
2153 const tooling::Replacements &Replaces,
2154 const FormatStyle &Style) {
2155 // We need to use lambda function here since there are two versions of
2156 // `reformat`.
2157 auto Reformat = [](const FormatStyle &Style, StringRef Code,
2158 std::vector<tooling::Range> Ranges,
2159 StringRef FileName) -> tooling::Replacements {
2160 return reformat(Style, Code, Ranges, FileName);
2161 };
2162 return processReplacements(Reformat, Code, Replaces, Style);
2163}
2164
2165tooling::Replacements
2166cleanupAroundReplacements(StringRef Code, const tooling::Replacements &Replaces,
2167 const FormatStyle &Style) {
2168 // We need to use lambda function here since there are two versions of
2169 // `cleanup`.
2170 auto Cleanup = [](const FormatStyle &Style, StringRef Code,
2171 std::vector<tooling::Range> Ranges,
2172 StringRef FileName) -> tooling::Replacements {
2173 return cleanup(Style, Code, Ranges, FileName);
2174 };
2175 return processReplacements(Cleanup, Code, Replaces, Style);
2176}
2177
2178tooling::Replacements reformat(const FormatStyle &Style, SourceManager &SM,
2179 FileID ID, ArrayRef<CharSourceRange> Ranges,
Manuel Klimekec5c3db2015-05-07 12:26:30 +00002180 bool *IncompleteFormat) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00002181 FormatStyle Expanded = expandPresets(Style);
2182 if (Expanded.DisableFormat)
Daniel Jasper23376252014-09-09 14:37:39 +00002183 return tooling::Replacements();
Eric Liu4cfb88a2016-04-25 15:09:22 +00002184
2185 Environment Env(Expanded, SM, ID, Ranges);
2186 Formatter Format(Env, IncompleteFormat);
2187 return Format.process();
Daniel Jasperf7935112012-12-03 18:12:45 +00002188}
2189
Daniel Jasperec04c0d2013-05-16 10:40:07 +00002190tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
Benjamin Kramerd0eed3a2014-10-03 18:52:48 +00002191 ArrayRef<tooling::Range> Ranges,
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00002192 StringRef FileName, bool *IncompleteFormat) {
Eric Liu4cfb88a2016-04-25 15:09:22 +00002193 FormatStyle Expanded = expandPresets(Style);
2194 if (Expanded.DisableFormat)
Daniel Jasper23376252014-09-09 14:37:39 +00002195 return tooling::Replacements();
2196
Eric Liu4cfb88a2016-04-25 15:09:22 +00002197 std::unique_ptr<Environment> Env =
2198 Environment::CreateVirtualEnvironment(Expanded, Code, FileName, Ranges);
2199 Formatter Format(*Env, IncompleteFormat);
2200 return Format.process();
2201}
2202
2203tooling::Replacements cleanup(const FormatStyle &Style, SourceManager &SM,
2204 FileID ID, ArrayRef<CharSourceRange> Ranges) {
2205 Environment Env(Style, SM, ID, Ranges);
2206 Cleaner Clean(Env);
2207 return Clean.process();
2208}
2209
2210tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code,
2211 ArrayRef<tooling::Range> Ranges,
2212 StringRef FileName) {
2213 std::unique_ptr<Environment> Env =
2214 Environment::CreateVirtualEnvironment(Style, Code, FileName, Ranges);
2215 Cleaner Clean(*Env);
2216 return Clean.process();
Daniel Jasperec04c0d2013-05-16 10:40:07 +00002217}
2218
Daniel Jasper4db69bd2014-09-04 18:23:42 +00002219LangOptions getFormattingLangOpts(const FormatStyle &Style) {
Daniel Jasperc1fa2812013-01-10 13:08:12 +00002220 LangOptions LangOpts;
2221 LangOpts.CPlusPlus = 1;
Daniel Jasper4db69bd2014-09-04 18:23:42 +00002222 LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
2223 LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
Daniel Jasper55213652013-03-22 10:01:29 +00002224 LangOpts.LineComment = 1;
Daniel Jasper1662bfe2015-04-03 21:15:46 +00002225 bool AlternativeOperators = Style.Language == FormatStyle::LK_Cpp;
Daniel Jasper30a24062014-11-14 09:02:28 +00002226 LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
Daniel Jasperc1fa2812013-01-10 13:08:12 +00002227 LangOpts.Bool = 1;
2228 LangOpts.ObjC1 = 1;
2229 LangOpts.ObjC2 = 1;
Eric Liu4cfb88a2016-04-25 15:09:22 +00002230 LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
Saleem Abdulrasoold170c4b2015-10-04 17:51:05 +00002231 LangOpts.DeclSpecKeyword = 1; // To get __declspec.
Daniel Jasperc1fa2812013-01-10 13:08:12 +00002232 return LangOpts;
2233}
2234
Edwin Vaned544aa72013-09-30 13:31:48 +00002235const char *StyleOptionHelpDescription =
2236 "Coding style, currently supports:\n"
2237 " LLVM, Google, Chromium, Mozilla, WebKit.\n"
2238 "Use -style=file to load style configuration from\n"
2239 ".clang-format file located in one of the parent\n"
2240 "directories of the source file (or current\n"
2241 "directory for stdin).\n"
2242 "Use -style=\"{key: value, ...}\" to set specific\n"
2243 "parameters, e.g.:\n"
2244 " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
2245
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00002246static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
Daniel Jasper498f5582015-12-25 08:53:31 +00002247 if (FileName.endswith(".java"))
Daniel Jasperc58c70e2014-09-15 11:21:46 +00002248 return FormatStyle::LK_Java;
Daniel Jasper498f5582015-12-25 08:53:31 +00002249 if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts"))
2250 return FormatStyle::LK_JavaScript; // JavaScript or TypeScript.
2251 if (FileName.endswith_lower(".proto") ||
2252 FileName.endswith_lower(".protodevel"))
Daniel Jasper7052ce62014-01-19 09:04:08 +00002253 return FormatStyle::LK_Proto;
Daniel Jasper498f5582015-12-25 08:53:31 +00002254 if (FileName.endswith_lower(".td"))
2255 return FormatStyle::LK_TableGen;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00002256 return FormatStyle::LK_Cpp;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00002257}
2258
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00002259FormatStyle getStyle(StringRef StyleName, StringRef FileName,
Eric Liu547d8792016-03-24 13:22:42 +00002260 StringRef FallbackStyle, vfs::FileSystem *FS) {
2261 if (!FS) {
2262 FS = vfs::getRealFileSystem().get();
2263 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00002264 FormatStyle Style = getLLVMStyle();
2265 Style.Language = getLanguageByFileName(FileName);
2266 if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) {
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00002267 llvm::errs() << "Invalid fallback style \"" << FallbackStyle
2268 << "\" using LLVM style\n";
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00002269 return Style;
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00002270 }
Edwin Vaned544aa72013-09-30 13:31:48 +00002271
2272 if (StyleName.startswith("{")) {
2273 // Parse YAML/JSON style from the command line.
Rafael Espindolac0809172014-06-12 14:02:15 +00002274 if (std::error_code ec = parseConfiguration(StyleName, &Style)) {
Alexander Kornienkoe2e03872013-10-14 00:46:35 +00002275 llvm::errs() << "Error parsing -style: " << ec.message() << ", using "
2276 << FallbackStyle << " style\n";
Edwin Vaned544aa72013-09-30 13:31:48 +00002277 }
2278 return Style;
2279 }
2280
2281 if (!StyleName.equals_lower("file")) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00002282 if (!getPredefinedStyle(StyleName, Style.Language, &Style))
Edwin Vaned544aa72013-09-30 13:31:48 +00002283 llvm::errs() << "Invalid value for -style, using " << FallbackStyle
2284 << " style\n";
2285 return Style;
2286 }
2287
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00002288 // Look for .clang-format/_clang-format file in the file's parent directories.
Alexander Kornienkocabdd732013-11-29 15:19:43 +00002289 SmallString<128> UnsuitableConfigFiles;
Edwin Vaned544aa72013-09-30 13:31:48 +00002290 SmallString<128> Path(FileName);
2291 llvm::sys::fs::make_absolute(Path);
Alexander Kornienkoe2e03872013-10-14 00:46:35 +00002292 for (StringRef Directory = Path; !Directory.empty();
Edwin Vaned544aa72013-09-30 13:31:48 +00002293 Directory = llvm::sys::path::parent_path(Directory)) {
Eric Liu547d8792016-03-24 13:22:42 +00002294
2295 auto Status = FS->status(Directory);
2296 if (!Status ||
2297 Status->getType() != llvm::sys::fs::file_type::directory_file) {
Edwin Vaned544aa72013-09-30 13:31:48 +00002298 continue;
Eric Liu547d8792016-03-24 13:22:42 +00002299 }
2300
Edwin Vaned544aa72013-09-30 13:31:48 +00002301 SmallString<128> ConfigFile(Directory);
2302
2303 llvm::sys::path::append(ConfigFile, ".clang-format");
2304 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
Eric Liud4758322016-03-24 13:22:37 +00002305
Eric Liu547d8792016-03-24 13:22:42 +00002306 Status = FS->status(ConfigFile.str());
2307 bool IsFile =
2308 Status && (Status->getType() == llvm::sys::fs::file_type::regular_file);
Edwin Vaned544aa72013-09-30 13:31:48 +00002309 if (!IsFile) {
2310 // Try _clang-format too, since dotfiles are not commonly used on Windows.
2311 ConfigFile = Directory;
2312 llvm::sys::path::append(ConfigFile, "_clang-format");
2313 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
Eric Liu547d8792016-03-24 13:22:42 +00002314 Status = FS->status(ConfigFile.str());
2315 IsFile = Status &&
2316 (Status->getType() == llvm::sys::fs::file_type::regular_file);
Edwin Vaned544aa72013-09-30 13:31:48 +00002317 }
2318
2319 if (IsFile) {
Rafael Espindola2d2b4202014-07-06 17:43:24 +00002320 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
Eric Liu547d8792016-03-24 13:22:42 +00002321 FS->getBufferForFile(ConfigFile.str());
Rafael Espindola2d2b4202014-07-06 17:43:24 +00002322 if (std::error_code EC = Text.getError()) {
2323 llvm::errs() << EC.message() << "\n";
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00002324 break;
Edwin Vaned544aa72013-09-30 13:31:48 +00002325 }
Rafael Espindola2d2b4202014-07-06 17:43:24 +00002326 if (std::error_code ec =
2327 parseConfiguration(Text.get()->getBuffer(), &Style)) {
Rafael Espindolad0136702014-06-12 02:50:04 +00002328 if (ec == ParseError::Unsuitable) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +00002329 if (!UnsuitableConfigFiles.empty())
2330 UnsuitableConfigFiles.append(", ");
2331 UnsuitableConfigFiles.append(ConfigFile);
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00002332 continue;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00002333 }
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00002334 llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()
2335 << "\n";
2336 break;
Edwin Vaned544aa72013-09-30 13:31:48 +00002337 }
2338 DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
2339 return Style;
2340 }
2341 }
Alexander Kornienkocabdd732013-11-29 15:19:43 +00002342 if (!UnsuitableConfigFiles.empty()) {
2343 llvm::errs() << "Configuration file(s) do(es) not support "
2344 << getLanguageName(Style.Language) << ": "
2345 << UnsuitableConfigFiles << "\n";
2346 }
Edwin Vaned544aa72013-09-30 13:31:48 +00002347 return Style;
2348}
2349
Daniel Jasper8d1832e2013-01-07 13:26:07 +00002350} // namespace format
2351} // namespace clang