blob: 6bfd9cb5ceca114001d6d3192726546ff32cc92b [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"
Daniel Jasperde0328a2013-08-16 11:20:30 +000017#include "ContinuationIndenter.h"
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000018#include "TokenAnnotator.h"
Daniel Jasper0df50932014-12-10 19:00:42 +000019#include "UnwrappedLineFormatter.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "UnwrappedLineParser.h"
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000021#include "WhitespaceManager.h"
Daniel Jasperec04c0d2013-05-16 10:40:07 +000022#include "clang/Basic/Diagnostic.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000023#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruth44eb4f62013-01-02 10:28:36 +000024#include "clang/Basic/SourceManager.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000025#include "clang/Lex/Lexer.h"
Alexander Kornienkoffd6d042013-03-27 11:52:18 +000026#include "llvm/ADT/STLExtras.h"
Manuel Klimek2ef908e2013-02-13 10:46:36 +000027#include "llvm/Support/Allocator.h"
Manuel Klimek24998102013-01-16 14:55:28 +000028#include "llvm/Support/Debug.h"
Edwin Vaned544aa72013-09-30 13:31:48 +000029#include "llvm/Support/Path.h"
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +000030#include "llvm/Support/Regex.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "llvm/Support/YAMLTraits.h"
Manuel Klimek2ef908e2013-02-13 10:46:36 +000032#include <queue>
Daniel Jasper8b529712012-12-04 13:02:32 +000033#include <string>
34
Chandler Carruth10346662014-04-22 03:17:02 +000035#define DEBUG_TYPE "format-formatter"
36
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000037using clang::format::FormatStyle;
38
Daniel Jaspere1e43192014-04-01 12:55:11 +000039LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string)
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +000040LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::IncludeCategory)
Daniel Jaspere1e43192014-04-01 12:55:11 +000041
Alexander Kornienkod6538332013-05-07 15:32:14 +000042namespace llvm {
43namespace yaml {
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000044template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> {
45 static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) {
46 IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp);
Daniel Jasperc58c70e2014-09-15 11:21:46 +000047 IO.enumCase(Value, "Java", FormatStyle::LK_Java);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000048 IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript);
Daniel Jasper7052ce62014-01-19 09:04:08 +000049 IO.enumCase(Value, "Proto", FormatStyle::LK_Proto);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000050 }
51};
52
53template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> {
54 static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) {
55 IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03);
56 IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03);
57 IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11);
58 IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11);
59 IO.enumCase(Value, "Auto", FormatStyle::LS_Auto);
60 }
61};
62
63template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
64 static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) {
65 IO.enumCase(Value, "Never", FormatStyle::UT_Never);
66 IO.enumCase(Value, "false", FormatStyle::UT_Never);
67 IO.enumCase(Value, "Always", FormatStyle::UT_Always);
68 IO.enumCase(Value, "true", FormatStyle::UT_Always);
69 IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation);
70 }
71};
72
Daniel Jasperd74cf402014-04-08 12:46:38 +000073template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> {
74 static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) {
75 IO.enumCase(Value, "None", FormatStyle::SFS_None);
76 IO.enumCase(Value, "false", FormatStyle::SFS_None);
77 IO.enumCase(Value, "All", FormatStyle::SFS_All);
78 IO.enumCase(Value, "true", FormatStyle::SFS_All);
79 IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline);
Daniel Jasper9e709352014-11-26 10:43:58 +000080 IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty);
Daniel Jasperd74cf402014-04-08 12:46:38 +000081 }
82};
83
Daniel Jasperac043c92014-09-15 11:11:00 +000084template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> {
85 static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) {
86 IO.enumCase(Value, "All", FormatStyle::BOS_All);
87 IO.enumCase(Value, "true", FormatStyle::BOS_All);
88 IO.enumCase(Value, "None", FormatStyle::BOS_None);
89 IO.enumCase(Value, "false", FormatStyle::BOS_None);
90 IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment);
91 }
92};
93
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000094template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
95 static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) {
96 IO.enumCase(Value, "Attach", FormatStyle::BS_Attach);
97 IO.enumCase(Value, "Linux", FormatStyle::BS_Linux);
Birunthan Mohanathas305fa9c2015-07-12 03:13:54 +000098 IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +000099 IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
100 IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000101 IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
Roman Kashitsyn291f64f2015-08-10 13:43:19 +0000102 IO.enumCase(Value, "WebKit", FormatStyle::BS_WebKit);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000103 IO.enumCase(Value, "Custom", FormatStyle::BS_Custom);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000104 }
105};
106
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000107template <>
108struct ScalarEnumerationTraits<FormatStyle::DefinitionReturnTypeBreakingStyle> {
109 static void
110 enumeration(IO &IO, FormatStyle::DefinitionReturnTypeBreakingStyle &Value) {
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000111 IO.enumCase(Value, "None", FormatStyle::DRTBS_None);
112 IO.enumCase(Value, "All", FormatStyle::DRTBS_All);
113 IO.enumCase(Value, "TopLevel", FormatStyle::DRTBS_TopLevel);
114
115 // For backward compatibility.
116 IO.enumCase(Value, "false", FormatStyle::DRTBS_None);
117 IO.enumCase(Value, "true", FormatStyle::DRTBS_All);
118 }
119};
120
Alexander Kornienkod6538332013-05-07 15:32:14 +0000121template <>
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000122struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000123 static void enumeration(IO &IO,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000124 FormatStyle::NamespaceIndentationKind &Value) {
125 IO.enumCase(Value, "None", FormatStyle::NI_None);
126 IO.enumCase(Value, "Inner", FormatStyle::NI_Inner);
127 IO.enumCase(Value, "All", FormatStyle::NI_All);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000128 }
129};
130
Jacques Pienaarfc275112015-02-18 23:48:37 +0000131template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> {
132 static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) {
Daniel Jasper553d4872014-06-17 12:40:34 +0000133 IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle);
134 IO.enumCase(Value, "Left", FormatStyle::PAS_Left);
135 IO.enumCase(Value, "Right", FormatStyle::PAS_Right);
136
Alp Toker958027b2014-07-14 19:42:55 +0000137 // For backward compatibility.
Daniel Jasper553d4872014-06-17 12:40:34 +0000138 IO.enumCase(Value, "true", FormatStyle::PAS_Left);
139 IO.enumCase(Value, "false", FormatStyle::PAS_Right);
140 }
141};
142
143template <>
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000144struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> {
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000145 static void enumeration(IO &IO,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000146 FormatStyle::SpaceBeforeParensOptions &Value) {
147 IO.enumCase(Value, "Never", FormatStyle::SBPO_Never);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000148 IO.enumCase(Value, "ControlStatements",
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000149 FormatStyle::SBPO_ControlStatements);
150 IO.enumCase(Value, "Always", FormatStyle::SBPO_Always);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000151
152 // For backward compatibility.
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000153 IO.enumCase(Value, "false", FormatStyle::SBPO_Never);
154 IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements);
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000155 }
156};
157
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000158template <> struct MappingTraits<FormatStyle> {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000159 static void mapping(IO &IO, FormatStyle &Style) {
160 // When reading, read the language first, we need it for getPredefinedStyle.
161 IO.mapOptional("Language", Style.Language);
162
Alexander Kornienko49149672013-05-10 11:56:10 +0000163 if (IO.outputting()) {
Jacques Pienaarfc275112015-02-18 23:48:37 +0000164 StringRef StylesArray[] = {"LLVM", "Google", "Chromium",
165 "Mozilla", "WebKit", "GNU"};
Alexander Kornienko49149672013-05-10 11:56:10 +0000166 ArrayRef<StringRef> Styles(StylesArray);
167 for (size_t i = 0, e = Styles.size(); i < e; ++i) {
168 StringRef StyleName(Styles[i]);
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000169 FormatStyle PredefinedStyle;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000170 if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000171 Style == PredefinedStyle) {
Alexander Kornienko49149672013-05-10 11:56:10 +0000172 IO.mapOptional("# BasedOnStyle", StyleName);
173 break;
174 }
175 }
176 } else {
Alexander Kornienkod6538332013-05-07 15:32:14 +0000177 StringRef BasedOnStyle;
178 IO.mapOptional("BasedOnStyle", BasedOnStyle);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000179 if (!BasedOnStyle.empty()) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000180 FormatStyle::LanguageKind OldLanguage = Style.Language;
181 FormatStyle::LanguageKind Language =
182 ((FormatStyle *)IO.getContext())->Language;
183 if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000184 IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
185 return;
186 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000187 Style.Language = OldLanguage;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000188 }
Alexander Kornienkod6538332013-05-07 15:32:14 +0000189 }
190
Birunthan Mohanathas50a6f912015-06-28 14:52:34 +0000191 // For backward compatibility.
192 if (!IO.outputting()) {
193 IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment);
194 IO.mapOptional("IndentFunctionDeclarationAfterType",
195 Style.IndentWrappedFunctionNames);
196 IO.mapOptional("PointerBindsToType", Style.PointerAlignment);
197 IO.mapOptional("SpaceAfterControlStatementKeyword",
198 Style.SpaceBeforeParens);
199 }
200
Alexander Kornienkod6538332013-05-07 15:32:14 +0000201 IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
Daniel Jasper3aa9a6a2014-11-18 23:55:27 +0000202 IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000203 IO.mapOptional("AlignConsecutiveAssignments",
204 Style.AlignConsecutiveAssignments);
Daniel Jaspere12597c2015-10-01 10:06:54 +0000205 IO.mapOptional("AlignConsecutiveDeclarations",
206 Style.AlignConsecutiveDeclarations);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000207 IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
Daniel Jasper3219e432014-12-02 13:24:51 +0000208 IO.mapOptional("AlignOperands", Style.AlignOperands);
Daniel Jasper552f4a72013-07-31 23:55:15 +0000209 IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000210 IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
211 Style.AllowAllParametersOfDeclarationOnNextLine);
Daniel Jasper17605d32014-05-14 09:33:35 +0000212 IO.mapOptional("AllowShortBlocksOnASingleLine",
213 Style.AllowShortBlocksOnASingleLine);
Daniel Jasperb87899b2014-09-10 13:11:45 +0000214 IO.mapOptional("AllowShortCaseLabelsOnASingleLine",
215 Style.AllowShortCaseLabelsOnASingleLine);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000216 IO.mapOptional("AllowShortFunctionsOnASingleLine",
217 Style.AllowShortFunctionsOnASingleLine);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000218 IO.mapOptional("AllowShortIfStatementsOnASingleLine",
219 Style.AllowShortIfStatementsOnASingleLine);
Daniel Jasper3a685df2013-05-16 12:12:21 +0000220 IO.mapOptional("AllowShortLoopsOnASingleLine",
221 Style.AllowShortLoopsOnASingleLine);
Daniel Jasperca4ea1c2014-08-05 12:16:31 +0000222 IO.mapOptional("AlwaysBreakAfterDefinitionReturnType",
223 Style.AlwaysBreakAfterDefinitionReturnType);
Alexander Kornienko58611712013-07-04 12:02:44 +0000224 IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
225 Style.AlwaysBreakBeforeMultilineStrings);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000226 IO.mapOptional("AlwaysBreakTemplateDeclarations",
227 Style.AlwaysBreakTemplateDeclarations);
228 IO.mapOptional("BinPackArguments", Style.BinPackArguments);
229 IO.mapOptional("BinPackParameters", Style.BinPackParameters);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000230 IO.mapOptional("BraceWrapping", Style.BraceWrapping);
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000231 IO.mapOptional("BreakBeforeBinaryOperators",
232 Style.BreakBeforeBinaryOperators);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000233 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
Daniel Jasper165b29e2013-11-08 00:57:11 +0000234 IO.mapOptional("BreakBeforeTernaryOperators",
235 Style.BreakBeforeTernaryOperators);
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000236 IO.mapOptional("BreakConstructorInitializersBeforeComma",
237 Style.BreakConstructorInitializersBeforeComma);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000238 IO.mapOptional("ColumnLimit", Style.ColumnLimit);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000239 IO.mapOptional("CommentPragmas", Style.CommentPragmas);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000240 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
241 Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
Daniel Jasper50d634b2014-10-28 16:53:38 +0000242 IO.mapOptional("ConstructorInitializerIndentWidth",
243 Style.ConstructorInitializerIndentWidth);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000244 IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
245 IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
Daniel Jasper553d4872014-06-17 12:40:34 +0000246 IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000247 IO.mapOptional("DisableFormat", Style.DisableFormat);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000248 IO.mapOptional("ExperimentalAutoDetectBinPacking",
249 Style.ExperimentalAutoDetectBinPacking);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000250 IO.mapOptional("ForEachMacros", Style.ForEachMacros);
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000251 IO.mapOptional("IncludeCategories", Style.IncludeCategories);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000252 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000253 IO.mapOptional("IndentWidth", Style.IndentWidth);
254 IO.mapOptional("IndentWrappedFunctionNames",
255 Style.IndentWrappedFunctionNames);
Daniel Jaspera26fc5c2014-03-21 13:43:14 +0000256 IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks",
257 Style.KeepEmptyLinesAtTheStartOfBlocks);
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000258 IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin);
259 IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000260 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000261 IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
Daniel Jasper50d634b2014-10-28 16:53:38 +0000262 IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth);
Daniel Jaspere9beea22014-01-28 15:20:33 +0000263 IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000264 IO.mapOptional("ObjCSpaceBeforeProtocolList",
265 Style.ObjCSpaceBeforeProtocolList);
Daniel Jasper33b909c2013-10-25 14:29:37 +0000266 IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
267 Style.PenaltyBreakBeforeFirstCallParameter);
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000268 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000269 IO.mapOptional("PenaltyBreakFirstLessLess",
270 Style.PenaltyBreakFirstLessLess);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000271 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000272 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
273 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
274 Style.PenaltyReturnTypeOnItsOwnLine);
Daniel Jasper553d4872014-06-17 12:40:34 +0000275 IO.mapOptional("PointerAlignment", Style.PointerAlignment);
Daniel Jasperdb986eb2014-09-03 07:37:29 +0000276 IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
Daniel Jasperd94bff32013-09-25 15:15:02 +0000277 IO.mapOptional("SpaceBeforeAssignmentOperators",
278 Style.SpaceBeforeAssignmentOperators);
Birunthan Mohanathas35cfbd72015-06-28 14:51:17 +0000279 IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
280 IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
281 IO.mapOptional("SpacesBeforeTrailingComments",
282 Style.SpacesBeforeTrailingComments);
283 IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
284 IO.mapOptional("SpacesInContainerLiterals",
285 Style.SpacesInContainerLiterals);
286 IO.mapOptional("SpacesInCStyleCastParentheses",
287 Style.SpacesInCStyleCastParentheses);
288 IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
289 IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
290 IO.mapOptional("Standard", Style.Standard);
291 IO.mapOptional("TabWidth", Style.TabWidth);
292 IO.mapOptional("UseTab", Style.UseTab);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000293 }
294};
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000295
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000296template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
297 static void mapping(IO &IO, FormatStyle::BraceWrappingFlags &Wrapping) {
298 IO.mapOptional("AfterClass", Wrapping.AfterClass);
299 IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement);
300 IO.mapOptional("AfterEnum", Wrapping.AfterEnum);
301 IO.mapOptional("AfterFunction", Wrapping.AfterFunction);
302 IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace);
303 IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration);
304 IO.mapOptional("AfterStruct", Wrapping.AfterStruct);
305 IO.mapOptional("AfterUnion", Wrapping.AfterUnion);
306 IO.mapOptional("BeforeCatch", Wrapping.BeforeCatch);
307 IO.mapOptional("BeforeElse", Wrapping.BeforeElse);
308 IO.mapOptional("IndentBraces", Wrapping.IndentBraces);
309 }
310};
311
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000312template <> struct MappingTraits<FormatStyle::IncludeCategory> {
313 static void mapping(IO &IO, FormatStyle::IncludeCategory &Category) {
314 IO.mapOptional("Regex", Category.Regex);
315 IO.mapOptional("Priority", Category.Priority);
316 }
317};
318
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000319// Allows to read vector<FormatStyle> while keeping default values.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000320// IO.getContext() should contain a pointer to the FormatStyle structure, that
321// will be used to get default values for missing keys.
322// If the first element has no Language specified, it will be treated as the
323// default one for the following elements.
Jacques Pienaarfc275112015-02-18 23:48:37 +0000324template <> struct DocumentListTraits<std::vector<FormatStyle>> {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000325 static size_t size(IO &IO, std::vector<FormatStyle> &Seq) {
326 return Seq.size();
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000327 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000328 static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq,
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000329 size_t Index) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000330 if (Index >= Seq.size()) {
331 assert(Index == Seq.size());
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000332 FormatStyle Template;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000333 if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000334 Template = Seq[0];
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000335 } else {
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000336 Template = *((const FormatStyle *)IO.getContext());
Alexander Kornienko6d2c88e2013-12-10 10:30:34 +0000337 Template.Language = FormatStyle::LK_None;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000338 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000339 Seq.resize(Index + 1, Template);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000340 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000341 return Seq[Index];
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000342 }
343};
Daniel Jasperd89ae9d2015-09-23 08:30:47 +0000344} // namespace yaml
345} // namespace llvm
Alexander Kornienkod6538332013-05-07 15:32:14 +0000346
Daniel Jasperf7935112012-12-03 18:12:45 +0000347namespace clang {
348namespace format {
349
Rafael Espindola6d0d89b2014-06-12 03:31:26 +0000350const std::error_category &getParseCategory() {
Rafael Espindolad0136702014-06-12 02:50:04 +0000351 static ParseErrorCategory C;
352 return C;
353}
354std::error_code make_error_code(ParseError e) {
Rafael Espindola6d0d89b2014-06-12 03:31:26 +0000355 return std::error_code(static_cast<int>(e), getParseCategory());
Rafael Espindolad0136702014-06-12 02:50:04 +0000356}
357
358const char *ParseErrorCategory::name() const LLVM_NOEXCEPT {
359 return "clang-format.parse_error";
360}
361
362std::string ParseErrorCategory::message(int EV) const {
363 switch (static_cast<ParseError>(EV)) {
364 case ParseError::Success:
365 return "Success";
366 case ParseError::Error:
367 return "Invalid argument";
368 case ParseError::Unsuitable:
369 return "Unsuitable";
370 }
Saleem Abdulrasoolfbfbaf62014-06-12 19:33:26 +0000371 llvm_unreachable("unexpected parse error");
Rafael Espindolad0136702014-06-12 02:50:04 +0000372}
373
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000374static FormatStyle expandPresets(const FormatStyle &Style) {
Daniel Jasper55bbe662015-10-07 04:06:10 +0000375 if (Style.BreakBeforeBraces == FormatStyle::BS_Custom)
376 return Style;
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000377 FormatStyle Expanded = Style;
378 Expanded.BraceWrapping = {false, false, false, false, false, false,
379 false, false, false, false, false};
380 switch (Style.BreakBeforeBraces) {
381 case FormatStyle::BS_Linux:
382 Expanded.BraceWrapping.AfterClass = true;
383 Expanded.BraceWrapping.AfterFunction = true;
384 Expanded.BraceWrapping.AfterNamespace = true;
385 Expanded.BraceWrapping.BeforeElse = true;
386 break;
387 case FormatStyle::BS_Mozilla:
388 Expanded.BraceWrapping.AfterClass = true;
389 Expanded.BraceWrapping.AfterEnum = true;
390 Expanded.BraceWrapping.AfterFunction = true;
391 Expanded.BraceWrapping.AfterStruct = true;
392 Expanded.BraceWrapping.AfterUnion = true;
393 break;
394 case FormatStyle::BS_Stroustrup:
395 Expanded.BraceWrapping.AfterFunction = true;
396 Expanded.BraceWrapping.BeforeCatch = true;
397 Expanded.BraceWrapping.BeforeElse = true;
398 break;
399 case FormatStyle::BS_Allman:
400 Expanded.BraceWrapping.AfterClass = true;
401 Expanded.BraceWrapping.AfterControlStatement = true;
402 Expanded.BraceWrapping.AfterEnum = true;
403 Expanded.BraceWrapping.AfterFunction = true;
404 Expanded.BraceWrapping.AfterNamespace = true;
405 Expanded.BraceWrapping.AfterObjCDeclaration = true;
406 Expanded.BraceWrapping.AfterStruct = true;
407 Expanded.BraceWrapping.BeforeCatch = true;
408 Expanded.BraceWrapping.BeforeElse = true;
409 break;
410 case FormatStyle::BS_GNU:
411 Expanded.BraceWrapping = {true, true, true, true, true, true,
412 true, true, true, true, true};
413 break;
414 case FormatStyle::BS_WebKit:
415 Expanded.BraceWrapping.AfterFunction = true;
416 break;
417 default:
418 break;
419 }
420 return Expanded;
421}
422
Daniel Jasperf7935112012-12-03 18:12:45 +0000423FormatStyle getLLVMStyle() {
424 FormatStyle LLVMStyle;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000425 LLVMStyle.Language = FormatStyle::LK_Cpp;
Daniel Jasperf7935112012-12-03 18:12:45 +0000426 LLVMStyle.AccessModifierOffset = -2;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000427 LLVMStyle.AlignEscapedNewlinesLeft = false;
Daniel Jasper3aa9a6a2014-11-18 23:55:27 +0000428 LLVMStyle.AlignAfterOpenBracket = true;
Daniel Jasper3219e432014-12-02 13:24:51 +0000429 LLVMStyle.AlignOperands = true;
Daniel Jasper552f4a72013-07-31 23:55:15 +0000430 LLVMStyle.AlignTrailingComments = true;
Daniel Jaspera44991332015-04-29 13:06:49 +0000431 LLVMStyle.AlignConsecutiveAssignments = false;
Daniel Jaspere12597c2015-10-01 10:06:54 +0000432 LLVMStyle.AlignConsecutiveDeclarations = false;
Daniel Jasperf7db4332013-01-29 16:03:49 +0000433 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
Daniel Jasperd74cf402014-04-08 12:46:38 +0000434 LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
Daniel Jasper17605d32014-05-14 09:33:35 +0000435 LLVMStyle.AllowShortBlocksOnASingleLine = false;
Daniel Jasperb87899b2014-09-10 13:11:45 +0000436 LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000437 LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper3a685df2013-05-16 12:12:21 +0000438 LLVMStyle.AllowShortLoopsOnASingleLine = false;
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000439 LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None;
Alexander Kornienko58611712013-07-04 12:02:44 +0000440 LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000441 LLVMStyle.AlwaysBreakTemplateDeclarations = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000442 LLVMStyle.BinPackParameters = true;
Daniel Jasper18210d72014-10-09 09:52:05 +0000443 LLVMStyle.BinPackArguments = true;
Daniel Jasperac043c92014-09-15 11:11:00 +0000444 LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
Daniel Jasper165b29e2013-11-08 00:57:11 +0000445 LLVMStyle.BreakBeforeTernaryOperators = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000446 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
Daniel Jasper55bbe662015-10-07 04:06:10 +0000447 LLVMStyle.BraceWrapping = {false, false, false, false, false, false,
448 false, false, false, false, false};
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000449 LLVMStyle.BreakConstructorInitializersBeforeComma = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000450 LLVMStyle.ColumnLimit = 80;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000451 LLVMStyle.CommentPragmas = "^ IWYU pragma:";
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000452 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
Daniel Jaspercdaffa42013-08-13 10:58:30 +0000453 LLVMStyle.ConstructorInitializerIndentWidth = 4;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000454 LLVMStyle.ContinuationIndentWidth = 4;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000455 LLVMStyle.Cpp11BracedListStyle = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000456 LLVMStyle.DerivePointerAlignment = false;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000457 LLVMStyle.ExperimentalAutoDetectBinPacking = false;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000458 LLVMStyle.ForEachMacros.push_back("foreach");
459 LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
460 LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
Daniel Jasper85c472d2015-09-29 07:53:08 +0000461 LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2},
462 {"^(<|\"(gtest|isl|json)/)", 3},
463 {".*", 1}};
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000464 LLVMStyle.IndentCaseLabels = false;
Daniel Jasperc75e1ef2014-07-09 08:42:42 +0000465 LLVMStyle.IndentWrappedFunctionNames = false;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000466 LLVMStyle.IndentWidth = 2;
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000467 LLVMStyle.TabWidth = 8;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000468 LLVMStyle.MaxEmptyLinesToKeep = 1;
Daniel Jaspera26fc5c2014-03-21 13:43:14 +0000469 LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000470 LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
Daniel Jasper50d634b2014-10-28 16:53:38 +0000471 LLVMStyle.ObjCBlockIndentWidth = 2;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000472 LLVMStyle.ObjCSpaceAfterProperty = false;
Nico Webera6087752013-01-10 20:12:55 +0000473 LLVMStyle.ObjCSpaceBeforeProtocolList = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000474 LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000475 LLVMStyle.SpacesBeforeTrailingComments = 1;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000476 LLVMStyle.Standard = FormatStyle::LS_Cpp11;
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000477 LLVMStyle.UseTab = FormatStyle::UT_Never;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000478 LLVMStyle.SpacesInParentheses = false;
Daniel Jasperad981f82014-08-26 11:41:14 +0000479 LLVMStyle.SpacesInSquareBrackets = false;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000480 LLVMStyle.SpaceInEmptyParentheses = false;
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000481 LLVMStyle.SpacesInContainerLiterals = true;
Daniel Jasperb55acad2013-08-20 12:36:34 +0000482 LLVMStyle.SpacesInCStyleCastParentheses = false;
Daniel Jasperdb986eb2014-09-03 07:37:29 +0000483 LLVMStyle.SpaceAfterCStyleCast = false;
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000484 LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
Daniel Jasperd94bff32013-09-25 15:15:02 +0000485 LLVMStyle.SpaceBeforeAssignmentOperators = true;
Daniel Jasperdd978ae2013-10-29 14:52:02 +0000486 LLVMStyle.SpacesInAngles = false;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000487
Daniel Jasper19a541e2013-12-19 16:45:34 +0000488 LLVMStyle.PenaltyBreakComment = 300;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000489 LLVMStyle.PenaltyBreakFirstLessLess = 120;
490 LLVMStyle.PenaltyBreakString = 1000;
491 LLVMStyle.PenaltyExcessCharacter = 1000000;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000492 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
Daniel Jasper33b909c2013-10-25 14:29:37 +0000493 LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000494
Daniel Jasperc64b09a2014-05-22 15:12:22 +0000495 LLVMStyle.DisableFormat = false;
496
Daniel Jasperf7935112012-12-03 18:12:45 +0000497 return LLVMStyle;
498}
499
Nico Weber514ecc82014-02-02 20:50:45 +0000500FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000501 FormatStyle GoogleStyle = getLLVMStyle();
Nico Weber514ecc82014-02-02 20:50:45 +0000502 GoogleStyle.Language = Language;
503
Daniel Jasperf7935112012-12-03 18:12:45 +0000504 GoogleStyle.AccessModifierOffset = -1;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000505 GoogleStyle.AlignEscapedNewlinesLeft = true;
Daniel Jasper085a2ed2013-04-24 13:46:00 +0000506 GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
Daniel Jasper5bd0b9e2013-05-23 18:05:18 +0000507 GoogleStyle.AllowShortLoopsOnASingleLine = true;
Alexander Kornienko58611712013-07-04 12:02:44 +0000508 GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000509 GoogleStyle.AlwaysBreakTemplateDeclarations = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000510 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000511 GoogleStyle.DerivePointerAlignment = true;
Daniel Jasper85c472d2015-09-29 07:53:08 +0000512 GoogleStyle.IncludeCategories = {{"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}};
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000513 GoogleStyle.IndentCaseLabels = true;
Daniel Jaspera26fc5c2014-03-21 13:43:14 +0000514 GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000515 GoogleStyle.ObjCSpaceAfterProperty = false;
Nico Webera6087752013-01-10 20:12:55 +0000516 GoogleStyle.ObjCSpaceBeforeProtocolList = false;
Daniel Jasper553d4872014-06-17 12:40:34 +0000517 GoogleStyle.PointerAlignment = FormatStyle::PAS_Left;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000518 GoogleStyle.SpacesBeforeTrailingComments = 2;
519 GoogleStyle.Standard = FormatStyle::LS_Auto;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000520
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000521 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
Daniel Jasper33b909c2013-10-25 14:29:37 +0000522 GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
Daniel Jasper4e9678f2013-07-11 20:41:21 +0000523
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000524 if (Language == FormatStyle::LK_Java) {
Daniel Jasper3aa9a6a2014-11-18 23:55:27 +0000525 GoogleStyle.AlignAfterOpenBracket = false;
Daniel Jasper3219e432014-12-02 13:24:51 +0000526 GoogleStyle.AlignOperands = false;
Daniel Jasperfd4ed182015-01-04 20:40:45 +0000527 GoogleStyle.AlignTrailingComments = false;
Daniel Jasper9e709352014-11-26 10:43:58 +0000528 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
Daniel Jasperfd4ed182015-01-04 20:40:45 +0000529 GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
Daniel Jasper1cd3c712015-01-14 12:24:59 +0000530 GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000531 GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
532 GoogleStyle.ColumnLimit = 100;
533 GoogleStyle.SpaceAfterCStyleCast = true;
Daniel Jasper61d81972014-11-14 08:22:46 +0000534 GoogleStyle.SpacesBeforeTrailingComments = 1;
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000535 } else if (Language == FormatStyle::LK_JavaScript) {
Daniel Jaspere551bb72014-11-05 17:22:31 +0000536 GoogleStyle.BreakBeforeTernaryOperators = false;
Daniel Jasper8f83a902014-05-09 10:28:58 +0000537 GoogleStyle.MaxEmptyLinesToKeep = 3;
Nico Weber514ecc82014-02-02 20:50:45 +0000538 GoogleStyle.SpacesInContainerLiterals = false;
Daniel Jasper67f8ad22014-09-30 17:57:06 +0000539 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
Daniel Jasper1cd3c712015-01-14 12:24:59 +0000540 GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
Nico Weber514ecc82014-02-02 20:50:45 +0000541 } else if (Language == FormatStyle::LK_Proto) {
Daniel Jasperd74cf402014-04-08 12:46:38 +0000542 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
Daniel Jasper783bac62014-04-15 09:54:30 +0000543 GoogleStyle.SpacesInContainerLiterals = false;
Nico Weber514ecc82014-02-02 20:50:45 +0000544 }
545
Daniel Jasperf7935112012-12-03 18:12:45 +0000546 return GoogleStyle;
547}
548
Nico Weber514ecc82014-02-02 20:50:45 +0000549FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
550 FormatStyle ChromiumStyle = getGoogleStyle(Language);
Nico Weber450425c2014-11-26 16:43:18 +0000551 if (Language == FormatStyle::LK_Java) {
Daniel Jasperfd4ed182015-01-04 20:40:45 +0000552 ChromiumStyle.AllowShortIfStatementsOnASingleLine = true;
Nico Weber450425c2014-11-26 16:43:18 +0000553 ChromiumStyle.IndentWidth = 4;
554 ChromiumStyle.ContinuationIndentWidth = 8;
555 } else {
556 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
557 ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
558 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
559 ChromiumStyle.AllowShortLoopsOnASingleLine = false;
560 ChromiumStyle.BinPackParameters = false;
561 ChromiumStyle.DerivePointerAlignment = false;
562 }
Daniel Jasper1b750ed2013-01-14 16:24:39 +0000563 return ChromiumStyle;
564}
565
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000566FormatStyle getMozillaStyle() {
567 FormatStyle MozillaStyle = getLLVMStyle();
568 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
Birunthan Mohanathasa0810022015-06-29 15:18:58 +0000569 MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000570 MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
571 FormatStyle::DRTBS_TopLevel;
Birunthan Mohanathasa0810022015-06-29 15:18:58 +0000572 MozillaStyle.AlwaysBreakTemplateDeclarations = true;
Birunthan Mohanathas305fa9c2015-07-12 03:13:54 +0000573 MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
Birunthan Mohanathasa0810022015-06-29 15:18:58 +0000574 MozillaStyle.BreakConstructorInitializersBeforeComma = true;
575 MozillaStyle.ConstructorInitializerIndentWidth = 2;
576 MozillaStyle.ContinuationIndentWidth = 2;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000577 MozillaStyle.Cpp11BracedListStyle = false;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000578 MozillaStyle.IndentCaseLabels = true;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000579 MozillaStyle.ObjCSpaceAfterProperty = true;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000580 MozillaStyle.ObjCSpaceBeforeProtocolList = false;
581 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
Daniel Jasper553d4872014-06-17 12:40:34 +0000582 MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
Alexander Kornienkoc8602662013-05-06 14:11:27 +0000583 return MozillaStyle;
584}
585
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000586FormatStyle getWebKitStyle() {
587 FormatStyle Style = getLLVMStyle();
Daniel Jasper65ee3472013-07-31 23:16:02 +0000588 Style.AccessModifierOffset = -4;
Daniel Jasper3aa9a6a2014-11-18 23:55:27 +0000589 Style.AlignAfterOpenBracket = false;
Daniel Jasper3219e432014-12-02 13:24:51 +0000590 Style.AlignOperands = false;
Daniel Jasper552f4a72013-07-31 23:55:15 +0000591 Style.AlignTrailingComments = false;
Daniel Jasperac043c92014-09-15 11:11:00 +0000592 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
Roman Kashitsyn291f64f2015-08-10 13:43:19 +0000593 Style.BreakBeforeBraces = FormatStyle::BS_WebKit;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000594 Style.BreakConstructorInitializersBeforeComma = true;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000595 Style.Cpp11BracedListStyle = false;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000596 Style.ColumnLimit = 0;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000597 Style.IndentWidth = 4;
Daniel Jasper65ee3472013-07-31 23:16:02 +0000598 Style.NamespaceIndentation = FormatStyle::NI_Inner;
Daniel Jasper50d634b2014-10-28 16:53:38 +0000599 Style.ObjCBlockIndentWidth = 4;
Daniel Jaspere9beea22014-01-28 15:20:33 +0000600 Style.ObjCSpaceAfterProperty = true;
Daniel Jasper553d4872014-06-17 12:40:34 +0000601 Style.PointerAlignment = FormatStyle::PAS_Left;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000602 Style.Standard = FormatStyle::LS_Cpp03;
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000603 return Style;
604}
605
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000606FormatStyle getGNUStyle() {
607 FormatStyle Style = getLLVMStyle();
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000608 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
Daniel Jasperac043c92014-09-15 11:11:00 +0000609 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000610 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000611 Style.BreakBeforeTernaryOperators = true;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000612 Style.Cpp11BracedListStyle = false;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000613 Style.ColumnLimit = 79;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000614 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
Chandler Carruthf8b72662014-03-02 12:37:31 +0000615 Style.Standard = FormatStyle::LS_Cpp03;
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000616 return Style;
617}
618
Daniel Jasperc64b09a2014-05-22 15:12:22 +0000619FormatStyle getNoStyle() {
620 FormatStyle NoStyle = getLLVMStyle();
621 NoStyle.DisableFormat = true;
622 return NoStyle;
623}
624
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000625bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
626 FormatStyle *Style) {
627 if (Name.equals_lower("llvm")) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000628 *Style = getLLVMStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000629 } else if (Name.equals_lower("chromium")) {
Nico Weber514ecc82014-02-02 20:50:45 +0000630 *Style = getChromiumStyle(Language);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000631 } else if (Name.equals_lower("mozilla")) {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000632 *Style = getMozillaStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000633 } else if (Name.equals_lower("google")) {
Nico Weber514ecc82014-02-02 20:50:45 +0000634 *Style = getGoogleStyle(Language);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000635 } else if (Name.equals_lower("webkit")) {
Daniel Jasperffefb3d2013-07-24 13:10:59 +0000636 *Style = getWebKitStyle();
Alexander Kornienkofe7a57f2013-12-10 15:42:15 +0000637 } else if (Name.equals_lower("gnu")) {
638 *Style = getGNUStyle();
Daniel Jasperc64b09a2014-05-22 15:12:22 +0000639 } else if (Name.equals_lower("none")) {
640 *Style = getNoStyle();
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000641 } else {
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000642 return false;
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000643 }
Alexander Kornienkod6538332013-05-07 15:32:14 +0000644
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000645 Style->Language = Language;
Alexander Kornienko006b5c82013-05-19 00:53:30 +0000646 return true;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000647}
648
Rafael Espindolac0809172014-06-12 14:02:15 +0000649std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000650 assert(Style);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000651 FormatStyle::LanguageKind Language = Style->Language;
652 assert(Language != FormatStyle::LK_None);
Alexander Kornienko06e00332013-05-20 15:18:01 +0000653 if (Text.trim().empty())
Rafael Espindolad0136702014-06-12 02:50:04 +0000654 return make_error_code(ParseError::Error);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000655
656 std::vector<FormatStyle> Styles;
Alexander Kornienkod6538332013-05-07 15:32:14 +0000657 llvm::yaml::Input Input(Text);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000658 // DocumentListTraits<vector<FormatStyle>> uses the context to get default
659 // values for the fields, keys for which are missing from the configuration.
660 // Mapping also uses the context to get the language to find the correct
661 // base style.
662 Input.setContext(Style);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000663 Input >> Styles;
664 if (Input.error())
665 return Input.error();
666
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000667 for (unsigned i = 0; i < Styles.size(); ++i) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000668 // Ensures that only the first configuration can skip the Language option.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000669 if (Styles[i].Language == FormatStyle::LK_None && i != 0)
Rafael Espindolad0136702014-06-12 02:50:04 +0000670 return make_error_code(ParseError::Error);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000671 // Ensure that each language is configured at most once.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000672 for (unsigned j = 0; j < i; ++j) {
673 if (Styles[i].Language == Styles[j].Language) {
674 DEBUG(llvm::dbgs()
675 << "Duplicate languages in the config file on positions " << j
676 << " and " << i << "\n");
Rafael Espindolad0136702014-06-12 02:50:04 +0000677 return make_error_code(ParseError::Error);
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000678 }
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000679 }
680 }
681 // Look for a suitable configuration starting from the end, so we can
682 // find the configuration for the specific language first, and the default
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000683 // configuration (which can only be at slot 0) after it.
684 for (int i = Styles.size() - 1; i >= 0; --i) {
685 if (Styles[i].Language == Language ||
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000686 Styles[i].Language == FormatStyle::LK_None) {
687 *Style = Styles[i];
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000688 Style->Language = Language;
Rafael Espindolad0136702014-06-12 02:50:04 +0000689 return make_error_code(ParseError::Success);
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000690 }
691 }
Rafael Espindolad0136702014-06-12 02:50:04 +0000692 return make_error_code(ParseError::Unsuitable);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000693}
694
695std::string configurationAsText(const FormatStyle &Style) {
696 std::string Text;
697 llvm::raw_string_ostream Stream(Text);
698 llvm::yaml::Output Output(Stream);
699 // We use the same mapping method for input and output, so we need a non-const
700 // reference here.
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000701 FormatStyle NonConstStyle = expandPresets(Style);
Alexander Kornienkod6538332013-05-07 15:32:14 +0000702 Output << NonConstStyle;
Alexander Kornienko9a38ec22013-05-13 12:56:35 +0000703 return Stream.str();
Alexander Kornienkod6538332013-05-07 15:32:14 +0000704}
705
Craig Topperaf35e852013-06-30 22:29:28 +0000706namespace {
707
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000708class FormatTokenLexer {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000709public:
Daniel Jasper23376252014-09-09 14:37:39 +0000710 FormatTokenLexer(SourceManager &SourceMgr, FileID ID, FormatStyle &Style,
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000711 encoding::Encoding Encoding)
Craig Topper2145bc02014-05-09 08:15:10 +0000712 : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false),
Jacques Pienaarfc275112015-02-18 23:48:37 +0000713 LessStashed(false), Column(0), TrailingWhitespace(0),
714 SourceMgr(SourceMgr), ID(ID), Style(Style),
715 IdentTable(getFormattingLangOpts(Style)), Keywords(IdentTable),
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000716 Encoding(Encoding), FirstInLineIndex(0), FormattingDisabled(false),
717 MacroBlockBeginRegex(Style.MacroBlockBegin),
718 MacroBlockEndRegex(Style.MacroBlockEnd) {
Daniel Jasper23376252014-09-09 14:37:39 +0000719 Lex.reset(new Lexer(ID, SourceMgr.getBuffer(ID), SourceMgr,
720 getFormattingLangOpts(Style)));
721 Lex->SetKeepWhitespaceMode(true);
Daniel Jaspere1e43192014-04-01 12:55:11 +0000722
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000723 for (const std::string &ForEachMacro : Style.ForEachMacros)
Daniel Jaspere1e43192014-04-01 12:55:11 +0000724 ForEachMacros.push_back(&IdentTable.get(ForEachMacro));
725 std::sort(ForEachMacros.begin(), ForEachMacros.end());
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000726 }
727
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000728 ArrayRef<FormatToken *> lex() {
729 assert(Tokens.empty());
Manuel Klimek68b03042014-04-14 09:14:11 +0000730 assert(FirstInLineIndex == 0);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000731 do {
732 Tokens.push_back(getNextToken());
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000733 tryMergePreviousTokens();
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000734 if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline)
Manuel Klimek68b03042014-04-14 09:14:11 +0000735 FirstInLineIndex = Tokens.size() - 1;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000736 } while (Tokens.back()->Tok.isNot(tok::eof));
737 return Tokens;
738 }
739
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000740 const AdditionalKeywords &getKeywords() { return Keywords; }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000741
742private:
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000743 void tryMergePreviousTokens() {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000744 if (tryMerge_TMacro())
745 return;
Manuel Klimek68b03042014-04-14 09:14:11 +0000746 if (tryMergeConflictMarkers())
747 return;
Jacques Pienaarfc275112015-02-18 23:48:37 +0000748 if (tryMergeLessLess())
749 return;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000750
751 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000752 if (tryMergeJSRegexLiteral())
753 return;
Daniel Jasper23376252014-09-09 14:37:39 +0000754 if (tryMergeEscapeSequence())
755 return;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000756 if (tryMergeTemplateString())
757 return;
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000758
Benjamin Kramer28b45ce2015-03-08 16:06:46 +0000759 static const tok::TokenKind JSIdentity[] = {tok::equalequal, tok::equal};
760 static const tok::TokenKind JSNotIdentity[] = {tok::exclaimequal,
761 tok::equal};
762 static const tok::TokenKind JSShiftEqual[] = {tok::greater, tok::greater,
763 tok::greaterequal};
764 static const tok::TokenKind JSRightArrow[] = {tok::equal, tok::greater};
Manuel Klimek79e06082015-05-21 12:23:34 +0000765 // FIXME: Investigate what token type gives the correct operator priority.
766 if (tryMergeTokens(JSIdentity, TT_BinaryOperator))
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000767 return;
Manuel Klimek79e06082015-05-21 12:23:34 +0000768 if (tryMergeTokens(JSNotIdentity, TT_BinaryOperator))
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000769 return;
Manuel Klimek79e06082015-05-21 12:23:34 +0000770 if (tryMergeTokens(JSShiftEqual, TT_BinaryOperator))
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000771 return;
Manuel Klimek79e06082015-05-21 12:23:34 +0000772 if (tryMergeTokens(JSRightArrow, TT_JsFatArrow))
Daniel Jasper78214392014-05-19 07:27:02 +0000773 return;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000774 }
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000775 }
776
Jacques Pienaarfc275112015-02-18 23:48:37 +0000777 bool tryMergeLessLess() {
778 // Merge X,less,less,Y into X,lessless,Y unless X or Y is less.
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000779 if (Tokens.size() < 3)
780 return false;
Jacques Pienaarfc275112015-02-18 23:48:37 +0000781
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000782 bool FourthTokenIsLess = false;
783 if (Tokens.size() > 3)
784 FourthTokenIsLess = (Tokens.end() - 4)[0]->is(tok::less);
Jacques Pienaarfc275112015-02-18 23:48:37 +0000785
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000786 auto First = Tokens.end() - 3;
787 if (First[2]->is(tok::less) || First[1]->isNot(tok::less) ||
788 First[0]->isNot(tok::less) || FourthTokenIsLess)
Jacques Pienaarfc275112015-02-18 23:48:37 +0000789 return false;
790
791 // Only merge if there currently is no whitespace between the two "<".
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000792 if (First[1]->WhitespaceRange.getBegin() !=
793 First[1]->WhitespaceRange.getEnd())
Jacques Pienaarfc275112015-02-18 23:48:37 +0000794 return false;
795
Jacques Pienaar68a7dbf2015-02-20 21:09:01 +0000796 First[0]->Tok.setKind(tok::lessless);
797 First[0]->TokenText = "<<";
798 First[0]->ColumnWidth += 1;
Jacques Pienaarfc275112015-02-18 23:48:37 +0000799 Tokens.erase(Tokens.end() - 2);
800 return true;
801 }
802
Manuel Klimek79e06082015-05-21 12:23:34 +0000803 bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000804 if (Tokens.size() < Kinds.size())
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000805 return false;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000806
807 SmallVectorImpl<FormatToken *>::const_iterator First =
808 Tokens.end() - Kinds.size();
809 if (!First[0]->is(Kinds[0]))
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000810 return false;
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000811 unsigned AddLength = 0;
812 for (unsigned i = 1; i < Kinds.size(); ++i) {
Jacques Pienaarfc275112015-02-18 23:48:37 +0000813 if (!First[i]->is(Kinds[i]) ||
814 First[i]->WhitespaceRange.getBegin() !=
815 First[i]->WhitespaceRange.getEnd())
Alexander Kornienkocabdd732013-11-29 15:19:43 +0000816 return false;
817 AddLength += First[i]->TokenText.size();
818 }
819 Tokens.resize(Tokens.size() - Kinds.size() + 1);
820 First[0]->TokenText = StringRef(First[0]->TokenText.data(),
821 First[0]->TokenText.size() + AddLength);
822 First[0]->ColumnWidth += AddLength;
Manuel Klimek79e06082015-05-21 12:23:34 +0000823 First[0]->Type = NewType;
Alexander Kornienko9aa62402013-11-21 12:43:57 +0000824 return true;
825 }
826
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000827 // Tries to merge an escape sequence, i.e. a "\\" and the following
Alp Tokerc3f36af2014-05-15 01:35:53 +0000828 // character. Use e.g. inside JavaScript regex literals.
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000829 bool tryMergeEscapeSequence() {
830 if (Tokens.size() < 2)
831 return false;
832 FormatToken *Previous = Tokens[Tokens.size() - 2];
Daniel Jasper49a9a282014-10-29 16:51:38 +0000833 if (Previous->isNot(tok::unknown) || Previous->TokenText != "\\")
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000834 return false;
Daniel Jasper49a9a282014-10-29 16:51:38 +0000835 ++Previous->ColumnWidth;
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000836 StringRef Text = Previous->TokenText;
Daniel Jasper49a9a282014-10-29 16:51:38 +0000837 Previous->TokenText = StringRef(Text.data(), Text.size() + 1);
838 resetLexer(SourceMgr.getFileOffset(Tokens.back()->Tok.getLocation()) + 1);
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000839 Tokens.resize(Tokens.size() - 1);
Daniel Jasper49a9a282014-10-29 16:51:38 +0000840 Column = Previous->OriginalColumn + Previous->ColumnWidth;
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000841 return true;
842 }
843
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000844 // Try to determine whether the current token ends a JavaScript regex literal.
845 // We heuristically assume that this is a regex literal if we find two
846 // unescaped slashes on a line and the token before the first slash is one of
Daniel Jasperf7405c12014-05-08 07:45:18 +0000847 // "(;,{}![:?", a binary operator or 'return', as those cannot be followed by
848 // a division.
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000849 bool tryMergeJSRegexLiteral() {
Daniel Jasper23376252014-09-09 14:37:39 +0000850 if (Tokens.size() < 2)
851 return false;
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000852
853 // If this is a string literal with a slash inside, compute the slash's
854 // offset and try to find the beginning of the regex literal.
855 // Also look at tok::unknown, as it can be an unterminated char literal.
856 size_t SlashInStringPos = StringRef::npos;
857 if (Tokens.back()->isOneOf(tok::string_literal, tok::char_constant,
858 tok::unknown)) {
859 // Start search from position 1 as otherwise, this is an unknown token
860 // for an unterminated /*-comment which is handled elsewhere.
861 SlashInStringPos = Tokens.back()->TokenText.find('/', 1);
862 if (SlashInStringPos == StringRef::npos)
863 return false;
864 }
865
Daniel Jasper23376252014-09-09 14:37:39 +0000866 // If a regex literal ends in "\//", this gets represented by an unknown
867 // token "\" and a comment.
868 bool MightEndWithEscapedSlash =
869 Tokens.back()->is(tok::comment) &&
870 Tokens.back()->TokenText.startswith("//") &&
871 Tokens[Tokens.size() - 2]->TokenText == "\\";
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000872 if (!MightEndWithEscapedSlash && SlashInStringPos == StringRef::npos &&
Daniel Jasper23376252014-09-09 14:37:39 +0000873 (Tokens.back()->isNot(tok::slash) ||
874 (Tokens[Tokens.size() - 2]->is(tok::unknown) &&
875 Tokens[Tokens.size() - 2]->TokenText == "\\")))
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000876 return false;
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000877
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000878 unsigned TokenCount = 0;
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000879 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
880 ++TokenCount;
Daniel Jasperf7372152015-07-02 14:14:04 +0000881 auto Prev = I + 1;
882 while (Prev != E && Prev[0]->is(tok::comment))
883 ++Prev;
Daniel Jasperc553ae12015-07-02 13:20:45 +0000884 if (I[0]->isOneOf(tok::slash, tok::slashequal) &&
Daniel Jasperf7372152015-07-02 14:14:04 +0000885 (Prev == E ||
886 ((Prev[0]->isOneOf(tok::l_paren, tok::semi, tok::l_brace,
887 tok::r_brace, tok::exclaim, tok::l_square,
888 tok::colon, tok::comma, tok::question,
889 tok::kw_return) ||
890 Prev[0]->isBinaryOperator())))) {
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000891 unsigned LastColumn = Tokens.back()->OriginalColumn;
892 SourceLocation Loc = Tokens.back()->Tok.getLocation();
Daniel Jasper23376252014-09-09 14:37:39 +0000893 if (MightEndWithEscapedSlash) {
Daniel Jasper23376252014-09-09 14:37:39 +0000894 // This regex literal ends in '\//'. Skip past the '//' of the last
895 // token and re-start lexing from there.
Daniel Jasper49a9a282014-10-29 16:51:38 +0000896 resetLexer(SourceMgr.getFileOffset(Loc) + 2);
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000897 } else if (SlashInStringPos != StringRef::npos) {
898 // This regex literal ends in a string_literal with a slash inside.
899 // Calculate end column and reset lexer appropriately.
900 resetLexer(SourceMgr.getFileOffset(Loc) + SlashInStringPos + 1);
901 LastColumn += SlashInStringPos;
Daniel Jasper23376252014-09-09 14:37:39 +0000902 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000903 Tokens.resize(Tokens.size() - TokenCount);
904 Tokens.back()->Tok.setKind(tok::unknown);
905 Tokens.back()->Type = TT_RegexLiteral;
Daniel Jasperf1446202015-07-02 15:00:44 +0000906 // Treat regex literals like other string_literals.
907 Tokens.back()->Tok.setKind(tok::string_literal);
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000908 Tokens.back()->ColumnWidth += LastColumn - I[0]->OriginalColumn;
909 return true;
910 }
911
912 // There can't be a newline inside a regex literal.
913 if (I[0]->NewlinesBefore > 0)
914 return false;
915 }
916 return false;
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000917 }
918
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000919 bool tryMergeTemplateString() {
920 if (Tokens.size() < 2)
921 return false;
922
923 FormatToken *EndBacktick = Tokens.back();
Daniel Jasperf69b9222015-05-02 08:05:38 +0000924 // Backticks get lexed as tok::unknown tokens. If a template string contains
Daniel Jasper0d6ac272015-04-16 08:20:51 +0000925 // a comment start, it gets lexed as a tok::comment, or tok::unknown if
926 // unterminated.
Daniel Jasper2ebb0c52015-06-14 07:16:57 +0000927 if (!EndBacktick->isOneOf(tok::comment, tok::string_literal,
928 tok::char_constant, tok::unknown))
Daniel Jasper0d6ac272015-04-16 08:20:51 +0000929 return false;
930 size_t CommentBacktickPos = EndBacktick->TokenText.find('`');
931 // Unknown token that's not actually a backtick, or a comment that doesn't
932 // contain a backtick.
933 if (CommentBacktickPos == StringRef::npos)
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000934 return false;
935
936 unsigned TokenCount = 0;
937 bool IsMultiline = false;
Daniel Jasperf69b9222015-05-02 08:05:38 +0000938 unsigned EndColumnInFirstLine =
939 EndBacktick->OriginalColumn + EndBacktick->ColumnWidth;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000940 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; I++) {
941 ++TokenCount;
Daniel Jasper553a5b02015-07-02 13:08:28 +0000942 if (I[0]->IsMultiline)
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000943 IsMultiline = true;
944
945 // If there was a preceding template string, this must be the start of a
946 // template string, not the end.
947 if (I[0]->is(TT_TemplateString))
948 return false;
949
950 if (I[0]->isNot(tok::unknown) || I[0]->TokenText != "`") {
951 // Keep track of the rhs offset of the last token to wrap across lines -
952 // its the rhs offset of the first line of the template string, used to
953 // determine its width.
954 if (I[0]->IsMultiline)
955 EndColumnInFirstLine = I[0]->OriginalColumn + I[0]->ColumnWidth;
956 // If the token has newlines, the token before it (if it exists) is the
957 // rhs end of the previous line.
Daniel Jasper553a5b02015-07-02 13:08:28 +0000958 if (I[0]->NewlinesBefore > 0 && (I + 1 != E)) {
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000959 EndColumnInFirstLine = I[1]->OriginalColumn + I[1]->ColumnWidth;
Daniel Jasper553a5b02015-07-02 13:08:28 +0000960 IsMultiline = true;
961 }
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000962 continue;
963 }
964
965 Tokens.resize(Tokens.size() - TokenCount);
966 Tokens.back()->Type = TT_TemplateString;
Daniel Jasper0d6ac272015-04-16 08:20:51 +0000967 const char *EndOffset =
968 EndBacktick->TokenText.data() + 1 + CommentBacktickPos;
969 if (CommentBacktickPos != 0) {
970 // If the backtick was not the first character (e.g. in a comment),
971 // re-lex after the backtick position.
972 SourceLocation Loc = EndBacktick->Tok.getLocation();
973 resetLexer(SourceMgr.getFileOffset(Loc) + CommentBacktickPos + 1);
974 }
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000975 Tokens.back()->TokenText =
976 StringRef(Tokens.back()->TokenText.data(),
977 EndOffset - Tokens.back()->TokenText.data());
Daniel Jasperf69b9222015-05-02 08:05:38 +0000978
979 unsigned EndOriginalColumn = EndBacktick->OriginalColumn;
980 if (EndOriginalColumn == 0) {
981 SourceLocation Loc = EndBacktick->Tok.getLocation();
982 EndOriginalColumn = SourceMgr.getSpellingColumnNumber(Loc);
983 }
984 // If the ` is further down within the token (e.g. in a comment).
985 EndOriginalColumn += CommentBacktickPos;
986
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000987 if (IsMultiline) {
988 // ColumnWidth is from backtick to last token in line.
989 // LastLineColumnWidth is 0 to backtick.
990 // x = `some content
991 // until here`;
992 Tokens.back()->ColumnWidth =
993 EndColumnInFirstLine - Tokens.back()->OriginalColumn;
Daniel Jasper553a5b02015-07-02 13:08:28 +0000994 // +1 for the ` itself.
995 Tokens.back()->LastLineColumnWidth = EndOriginalColumn + 1;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000996 Tokens.back()->IsMultiline = true;
997 } else {
998 // Token simply spans from start to end, +1 for the ` itself.
999 Tokens.back()->ColumnWidth =
Daniel Jasperf69b9222015-05-02 08:05:38 +00001000 EndOriginalColumn - Tokens.back()->OriginalColumn + 1;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001001 }
1002 return true;
1003 }
1004 return false;
1005 }
1006
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001007 bool tryMerge_TMacro() {
Alexander Kornienko81e32942013-09-16 20:20:49 +00001008 if (Tokens.size() < 4)
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001009 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001010 FormatToken *Last = Tokens.back();
1011 if (!Last->is(tok::r_paren))
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001012 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001013
1014 FormatToken *String = Tokens[Tokens.size() - 2];
1015 if (!String->is(tok::string_literal) || String->IsMultiline)
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001016 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001017
1018 if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001019 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001020
1021 FormatToken *Macro = Tokens[Tokens.size() - 4];
1022 if (Macro->TokenText != "_T")
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001023 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001024
1025 const char *Start = Macro->TokenText.data();
1026 const char *End = Last->TokenText.data() + Last->TokenText.size();
1027 String->TokenText = StringRef(Start, End - Start);
1028 String->IsFirst = Macro->IsFirst;
1029 String->LastNewlineOffset = Macro->LastNewlineOffset;
1030 String->WhitespaceRange = Macro->WhitespaceRange;
1031 String->OriginalColumn = Macro->OriginalColumn;
1032 String->ColumnWidth = encoding::columnWidthWithTabs(
1033 String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding);
Daniel Jaspere99c72f2015-03-26 14:47:35 +00001034 String->NewlinesBefore = Macro->NewlinesBefore;
1035 String->HasUnescapedNewline = Macro->HasUnescapedNewline;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001036
1037 Tokens.pop_back();
1038 Tokens.pop_back();
1039 Tokens.pop_back();
1040 Tokens.back() = String;
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001041 return true;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001042 }
1043
Manuel Klimek68b03042014-04-14 09:14:11 +00001044 bool tryMergeConflictMarkers() {
1045 if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof))
1046 return false;
1047
1048 // Conflict lines look like:
1049 // <marker> <text from the vcs>
1050 // For example:
1051 // >>>>>>> /file/in/file/system at revision 1234
1052 //
1053 // We merge all tokens in a line that starts with a conflict marker
1054 // into a single token with a special token type that the unwrapped line
1055 // parser will use to correctly rebuild the underlying code.
1056
1057 FileID ID;
1058 // Get the position of the first token in the line.
1059 unsigned FirstInLineOffset;
1060 std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc(
1061 Tokens[FirstInLineIndex]->getStartOfNonWhitespace());
1062 StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer();
1063 // Calculate the offset of the start of the current line.
1064 auto LineOffset = Buffer.rfind('\n', FirstInLineOffset);
1065 if (LineOffset == StringRef::npos) {
1066 LineOffset = 0;
1067 } else {
1068 ++LineOffset;
1069 }
1070
1071 auto FirstSpace = Buffer.find_first_of(" \n", LineOffset);
1072 StringRef LineStart;
1073 if (FirstSpace == StringRef::npos) {
1074 LineStart = Buffer.substr(LineOffset);
1075 } else {
1076 LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset);
1077 }
1078
1079 TokenType Type = TT_Unknown;
1080 if (LineStart == "<<<<<<<" || LineStart == ">>>>") {
1081 Type = TT_ConflictStart;
1082 } else if (LineStart == "|||||||" || LineStart == "=======" ||
1083 LineStart == "====") {
1084 Type = TT_ConflictAlternative;
1085 } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") {
1086 Type = TT_ConflictEnd;
1087 }
1088
1089 if (Type != TT_Unknown) {
1090 FormatToken *Next = Tokens.back();
1091
1092 Tokens.resize(FirstInLineIndex + 1);
1093 // We do not need to build a complete token here, as we will skip it
1094 // during parsing anyway (as we must not touch whitespace around conflict
1095 // markers).
1096 Tokens.back()->Type = Type;
1097 Tokens.back()->Tok.setKind(tok::kw___unknown_anytype);
1098
1099 Tokens.push_back(Next);
1100 return true;
1101 }
1102
1103 return false;
1104 }
1105
Jacques Pienaarfc275112015-02-18 23:48:37 +00001106 FormatToken *getStashedToken() {
1107 // Create a synthesized second '>' or '<' token.
1108 Token Tok = FormatTok->Tok;
1109 StringRef TokenText = FormatTok->TokenText;
1110
1111 unsigned OriginalColumn = FormatTok->OriginalColumn;
1112 FormatTok = new (Allocator.Allocate()) FormatToken;
1113 FormatTok->Tok = Tok;
1114 SourceLocation TokLocation =
Jacques Pienaar411b2512015-02-24 23:23:24 +00001115 FormatTok->Tok.getLocation().getLocWithOffset(Tok.getLength() - 1);
1116 FormatTok->Tok.setLocation(TokLocation);
Jacques Pienaarfc275112015-02-18 23:48:37 +00001117 FormatTok->WhitespaceRange = SourceRange(TokLocation, TokLocation);
1118 FormatTok->TokenText = TokenText;
1119 FormatTok->ColumnWidth = 1;
Jacques Pienaar411b2512015-02-24 23:23:24 +00001120 FormatTok->OriginalColumn = OriginalColumn + 1;
1121
Jacques Pienaarfc275112015-02-18 23:48:37 +00001122 return FormatTok;
1123 }
1124
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001125 FormatToken *getNextToken() {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001126 if (GreaterStashed) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001127 GreaterStashed = false;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001128 return getStashedToken();
1129 }
1130 if (LessStashed) {
1131 LessStashed = false;
1132 return getStashedToken();
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001133 }
1134
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001135 FormatTok = new (Allocator.Allocate()) FormatToken;
Daniel Jasper8369aa52013-07-16 20:28:33 +00001136 readRawToken(*FormatTok);
Manuel Klimek9043c742013-05-27 15:23:34 +00001137 SourceLocation WhitespaceStart =
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001138 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
Alexander Kornienko393e3082013-11-13 14:04:17 +00001139 FormatTok->IsFirst = IsFirstToken;
1140 IsFirstToken = false;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001141
1142 // Consume and record whitespace until we find a significant token.
Manuel Klimek9043c742013-05-27 15:23:34 +00001143 unsigned WhitespaceLength = TrailingWhitespace;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001144 while (FormatTok->Tok.is(tok::unknown)) {
Daniel Jaspere2408e32015-05-06 11:16:43 +00001145 StringRef Text = FormatTok->TokenText;
1146 auto EscapesNewline = [&](int pos) {
1147 // A '\r' here is just part of '\r\n'. Skip it.
1148 if (pos >= 0 && Text[pos] == '\r')
1149 --pos;
1150 // See whether there is an odd number of '\' before this.
1151 unsigned count = 0;
1152 for (; pos >= 0; --pos, ++count)
Daniel Jasperf0fd1c62015-05-10 08:00:25 +00001153 if (Text[pos] != '\\')
Daniel Jaspere2408e32015-05-06 11:16:43 +00001154 break;
1155 return count & 1;
1156 };
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001157 // FIXME: This miscounts tok:unknown tokens that are not just
1158 // whitespace, e.g. a '`' character.
Daniel Jaspere2408e32015-05-06 11:16:43 +00001159 for (int i = 0, e = Text.size(); i != e; ++i) {
1160 switch (Text[i]) {
Manuel Klimek31c85922013-08-29 15:21:40 +00001161 case '\n':
1162 ++FormatTok->NewlinesBefore;
Daniel Jaspere2408e32015-05-06 11:16:43 +00001163 FormatTok->HasUnescapedNewline = !EscapesNewline(i - 1);
Manuel Klimek31c85922013-08-29 15:21:40 +00001164 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
1165 Column = 0;
1166 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001167 case '\r':
Daniel Jasper30029c62015-02-05 11:05:31 +00001168 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
1169 Column = 0;
1170 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001171 case '\f':
1172 case '\v':
1173 Column = 0;
1174 break;
Manuel Klimek31c85922013-08-29 15:21:40 +00001175 case ' ':
1176 ++Column;
1177 break;
1178 case '\t':
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +00001179 Column += Style.TabWidth - Column % Style.TabWidth;
Manuel Klimek31c85922013-08-29 15:21:40 +00001180 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001181 case '\\':
Daniel Jaspere2408e32015-05-06 11:16:43 +00001182 if (i + 1 == e || (Text[i + 1] != '\r' && Text[i + 1] != '\n'))
Daniel Jasper877615c2013-10-11 19:45:02 +00001183 FormatTok->Type = TT_ImplicitStringLiteral;
1184 break;
Manuel Klimek31c85922013-08-29 15:21:40 +00001185 default:
Daniel Jasper877615c2013-10-11 19:45:02 +00001186 FormatTok->Type = TT_ImplicitStringLiteral;
Manuel Klimek31c85922013-08-29 15:21:40 +00001187 break;
1188 }
1189 }
1190
Daniel Jaspera98b7b02014-11-25 10:05:17 +00001191 if (FormatTok->is(TT_ImplicitStringLiteral))
Daniel Jasper877615c2013-10-11 19:45:02 +00001192 break;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001193 WhitespaceLength += FormatTok->Tok.getLength();
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001194
Daniel Jasper8369aa52013-07-16 20:28:33 +00001195 readRawToken(*FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001196 }
Manuel Klimekef920692013-01-07 07:56:50 +00001197
Manuel Klimek1abf7892013-01-04 23:34:14 +00001198 // In case the token starts with escaped newlines, we want to
1199 // take them into account as whitespace - this pattern is quite frequent
1200 // in macro definitions.
Manuel Klimek1abf7892013-01-04 23:34:14 +00001201 // FIXME: Add a more explicit test.
Daniel Jasper8369aa52013-07-16 20:28:33 +00001202 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
1203 FormatTok->TokenText[1] == '\n') {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001204 ++FormatTok->NewlinesBefore;
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001205 WhitespaceLength += 2;
Daniel Jaspere2408e32015-05-06 11:16:43 +00001206 FormatTok->LastNewlineOffset = 2;
Manuel Klimek31c85922013-08-29 15:21:40 +00001207 Column = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +00001208 FormatTok->TokenText = FormatTok->TokenText.substr(2);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001209 }
Alexander Kornienko39856b72013-09-10 09:38:25 +00001210
1211 FormatTok->WhitespaceRange = SourceRange(
1212 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
1213
Manuel Klimek31c85922013-08-29 15:21:40 +00001214 FormatTok->OriginalColumn = Column;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001215
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001216 TrailingWhitespace = 0;
1217 if (FormatTok->Tok.is(tok::comment)) {
Manuel Klimek31c85922013-08-29 15:21:40 +00001218 // FIXME: Add the trimmed whitespace to Column.
Daniel Jasper8369aa52013-07-16 20:28:33 +00001219 StringRef UntrimmedText = FormatTok->TokenText;
Alexander Kornienko9ab4a772013-09-06 17:24:54 +00001220 FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
Daniel Jasper8369aa52013-07-16 20:28:33 +00001221 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001222 } else if (FormatTok->Tok.is(tok::raw_identifier)) {
Daniel Jasper8369aa52013-07-16 20:28:33 +00001223 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001224 FormatTok->Tok.setIdentifierInfo(&Info);
1225 FormatTok->Tok.setKind(Info.getTokenID());
Daniel Jasperfe2cf662014-11-19 14:11:11 +00001226 if (Style.Language == FormatStyle::LK_Java &&
1227 FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete)) {
1228 FormatTok->Tok.setKind(tok::identifier);
1229 FormatTok->Tok.setIdentifierInfo(nullptr);
1230 }
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001231 } else if (FormatTok->Tok.is(tok::greatergreater)) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001232 FormatTok->Tok.setKind(tok::greater);
Daniel Jasper8369aa52013-07-16 20:28:33 +00001233 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001234 GreaterStashed = true;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001235 } else if (FormatTok->Tok.is(tok::lessless)) {
1236 FormatTok->Tok.setKind(tok::less);
1237 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
1238 LessStashed = true;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001239 }
1240
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001241 // Now FormatTok is the next non-whitespace token.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001242
Alexander Kornienko39856b72013-09-10 09:38:25 +00001243 StringRef Text = FormatTok->TokenText;
1244 size_t FirstNewlinePos = Text.find('\n');
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001245 if (FirstNewlinePos == StringRef::npos) {
1246 // FIXME: ColumnWidth actually depends on the start column, we need to
1247 // take this into account when the token is moved.
1248 FormatTok->ColumnWidth =
1249 encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding);
1250 Column += FormatTok->ColumnWidth;
1251 } else {
Alexander Kornienko39856b72013-09-10 09:38:25 +00001252 FormatTok->IsMultiline = true;
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001253 // FIXME: ColumnWidth actually depends on the start column, we need to
1254 // take this into account when the token is moved.
1255 FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
1256 Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
1257
Alexander Kornienko39856b72013-09-10 09:38:25 +00001258 // The last line of the token always starts in column 0.
1259 // Thus, the length can be precomputed even in the presence of tabs.
1260 FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
1261 Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth,
1262 Encoding);
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001263 Column = FormatTok->LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +00001264 }
Alexander Kornienko39856b72013-09-10 09:38:25 +00001265
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001266 if (Style.Language == FormatStyle::LK_Cpp) {
1267 if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
1268 Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
1269 tok::pp_define) &&
1270 std::find(ForEachMacros.begin(), ForEachMacros.end(),
1271 FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end()) {
1272 FormatTok->Type = TT_ForEachMacro;
1273 } else if (FormatTok->is(tok::identifier)) {
1274 if (MacroBlockBeginRegex.match(Text)) {
1275 FormatTok->Type = TT_MacroBlockBegin;
1276 } else if (MacroBlockEndRegex.match(Text)) {
1277 FormatTok->Type = TT_MacroBlockEnd;
1278 }
1279 }
1280 }
Daniel Jaspere1e43192014-04-01 12:55:11 +00001281
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001282 return FormatTok;
1283 }
1284
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001285 FormatToken *FormatTok;
Alexander Kornienko393e3082013-11-13 14:04:17 +00001286 bool IsFirstToken;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001287 bool GreaterStashed, LessStashed;
Manuel Klimek31c85922013-08-29 15:21:40 +00001288 unsigned Column;
Manuel Klimek9043c742013-05-27 15:23:34 +00001289 unsigned TrailingWhitespace;
Daniel Jasper23376252014-09-09 14:37:39 +00001290 std::unique_ptr<Lexer> Lex;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001291 SourceManager &SourceMgr;
Daniel Jasper23376252014-09-09 14:37:39 +00001292 FileID ID;
Manuel Klimek31c85922013-08-29 15:21:40 +00001293 FormatStyle &Style;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001294 IdentifierTable IdentTable;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001295 AdditionalKeywords Keywords;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001296 encoding::Encoding Encoding;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001297 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
Manuel Klimek68b03042014-04-14 09:14:11 +00001298 // Index (in 'Tokens') of the last token that starts a new line.
1299 unsigned FirstInLineIndex;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001300 SmallVector<FormatToken *, 16> Tokens;
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001301 SmallVector<IdentifierInfo *, 8> ForEachMacros;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001302
NAKAMURA Takumi7160c4d2014-08-06 16:53:13 +00001303 bool FormattingDisabled;
Daniel Jasper471894432014-08-06 13:40:26 +00001304
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001305 llvm::Regex MacroBlockBeginRegex;
1306 llvm::Regex MacroBlockEndRegex;
1307
Daniel Jasper8369aa52013-07-16 20:28:33 +00001308 void readRawToken(FormatToken &Tok) {
Daniel Jasper23376252014-09-09 14:37:39 +00001309 Lex->LexFromRawLexer(Tok.Tok);
Daniel Jasper8369aa52013-07-16 20:28:33 +00001310 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
1311 Tok.Tok.getLength());
Daniel Jasper8369aa52013-07-16 20:28:33 +00001312 // For formatting, treat unterminated string literals like normal string
1313 // literals.
Daniel Jasper86fee2f2014-01-31 12:49:42 +00001314 if (Tok.is(tok::unknown)) {
1315 if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') {
1316 Tok.Tok.setKind(tok::string_literal);
1317 Tok.IsUnterminatedLiteral = true;
1318 } else if (Style.Language == FormatStyle::LK_JavaScript &&
1319 Tok.TokenText == "''") {
1320 Tok.Tok.setKind(tok::char_constant);
1321 }
Daniel Jasper8369aa52013-07-16 20:28:33 +00001322 }
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001323
1324 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" ||
1325 Tok.TokenText == "/* clang-format on */")) {
Daniel Jasper471894432014-08-06 13:40:26 +00001326 FormattingDisabled = false;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001327 }
1328
Daniel Jasper471894432014-08-06 13:40:26 +00001329 Tok.Finalized = FormattingDisabled;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001330
1331 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" ||
1332 Tok.TokenText == "/* clang-format off */")) {
Daniel Jasper471894432014-08-06 13:40:26 +00001333 FormattingDisabled = true;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001334 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001335 }
Daniel Jasper49a9a282014-10-29 16:51:38 +00001336
1337 void resetLexer(unsigned Offset) {
1338 StringRef Buffer = SourceMgr.getBufferData(ID);
1339 Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID),
1340 getFormattingLangOpts(Style), Buffer.begin(),
1341 Buffer.begin() + Offset, Buffer.end()));
1342 Lex->SetKeepWhitespaceMode(true);
Daniel Jasper55c384e2015-07-02 14:01:34 +00001343 TrailingWhitespace = 0;
Daniel Jasper49a9a282014-10-29 16:51:38 +00001344 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001345};
1346
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001347static StringRef getLanguageName(FormatStyle::LanguageKind Language) {
1348 switch (Language) {
1349 case FormatStyle::LK_Cpp:
1350 return "C++";
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001351 case FormatStyle::LK_Java:
1352 return "Java";
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001353 case FormatStyle::LK_JavaScript:
1354 return "JavaScript";
Daniel Jasper7052ce62014-01-19 09:04:08 +00001355 case FormatStyle::LK_Proto:
1356 return "Proto";
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001357 default:
1358 return "Unknown";
1359 }
1360}
1361
Daniel Jasperf7935112012-12-03 18:12:45 +00001362class Formatter : public UnwrappedLineConsumer {
1363public:
Daniel Jasper23376252014-09-09 14:37:39 +00001364 Formatter(const FormatStyle &Style, SourceManager &SourceMgr, FileID ID,
Benjamin Kramerd0eed3a2014-10-03 18:52:48 +00001365 ArrayRef<CharSourceRange> Ranges)
Daniel Jasper23376252014-09-09 14:37:39 +00001366 : Style(Style), ID(ID), SourceMgr(SourceMgr),
1367 Whitespaces(SourceMgr, Style,
1368 inputUsesCRLF(SourceMgr.getBufferData(ID))),
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001369 Ranges(Ranges.begin(), Ranges.end()), UnwrappedLines(1),
Daniel Jasper23376252014-09-09 14:37:39 +00001370 Encoding(encoding::detectEncoding(SourceMgr.getBufferData(ID))) {
Daniel Jasperfa21c072013-07-15 14:33:14 +00001371 DEBUG(llvm::dbgs() << "File encoding: "
1372 << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
1373 : "unknown")
1374 << "\n");
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001375 DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language)
1376 << "\n");
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001377 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001378
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001379 tooling::Replacements format(bool *IncompleteFormat) {
Manuel Klimek71814b42013-10-11 21:25:45 +00001380 tooling::Replacements Result;
Daniel Jasper23376252014-09-09 14:37:39 +00001381 FormatTokenLexer Tokens(SourceMgr, ID, Style, Encoding);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001382
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001383 UnwrappedLineParser Parser(Style, Tokens.getKeywords(), Tokens.lex(),
1384 *this);
Manuel Klimek20e0af62015-05-06 11:56:29 +00001385 Parser.parse();
Manuel Klimek71814b42013-10-11 21:25:45 +00001386 assert(UnwrappedLines.rbegin()->empty());
1387 for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE;
1388 ++Run) {
1389 DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
1390 SmallVector<AnnotatedLine *, 16> AnnotatedLines;
1391 for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) {
1392 AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i]));
1393 }
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001394 tooling::Replacements RunResult =
1395 format(AnnotatedLines, Tokens, IncompleteFormat);
Manuel Klimek71814b42013-10-11 21:25:45 +00001396 DEBUG({
1397 llvm::dbgs() << "Replacements for run " << Run << ":\n";
1398 for (tooling::Replacements::iterator I = RunResult.begin(),
1399 E = RunResult.end();
1400 I != E; ++I) {
1401 llvm::dbgs() << I->toString() << "\n";
1402 }
1403 });
1404 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1405 delete AnnotatedLines[i];
1406 }
1407 Result.insert(RunResult.begin(), RunResult.end());
1408 Whitespaces.reset();
1409 }
1410 return Result;
1411 }
1412
1413 tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001414 FormatTokenLexer &Tokens,
1415 bool *IncompleteFormat) {
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001416 TokenAnnotator Annotator(Style, Tokens.getKeywords());
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001417 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001418 Annotator.annotate(*AnnotatedLines[i]);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001419 }
Manuel Klimek71814b42013-10-11 21:25:45 +00001420 deriveLocalStyle(AnnotatedLines);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001421 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001422 Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001423 }
Daniel Jasper5500f612013-11-25 11:08:59 +00001424 computeAffectedLines(AnnotatedLines.begin(), AnnotatedLines.end());
Daniel Jasperb67cc422013-04-09 17:46:55 +00001425
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001426 Annotator.setCommentLineLevels(AnnotatedLines);
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001427 ContinuationIndenter Indenter(Style, Tokens.getKeywords(), SourceMgr,
1428 Whitespaces, Encoding,
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001429 BinPackInconclusiveFunctions);
Manuel Klimekd3585db2015-05-11 08:21:35 +00001430 UnwrappedLineFormatter(&Indenter, &Whitespaces, Style, Tokens.getKeywords(),
1431 IncompleteFormat)
1432 .format(AnnotatedLines);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001433 return Whitespaces.generateReplacements();
1434 }
1435
1436private:
Daniel Jasper5500f612013-11-25 11:08:59 +00001437 // Determines which lines are affected by the SourceRanges given as input.
Daniel Jasper9c199562013-11-28 15:58:55 +00001438 // Returns \c true if at least one line between I and E or one of their
1439 // children is affected.
Daniel Jasper5500f612013-11-25 11:08:59 +00001440 bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I,
1441 SmallVectorImpl<AnnotatedLine *>::iterator E) {
1442 bool SomeLineAffected = false;
Craig Topper2145bc02014-05-09 08:15:10 +00001443 const AnnotatedLine *PreviousLine = nullptr;
Daniel Jasper5500f612013-11-25 11:08:59 +00001444 while (I != E) {
1445 AnnotatedLine *Line = *I;
1446 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
1447
1448 // If a line is part of a preprocessor directive, it needs to be formatted
1449 // if any token within the directive is affected.
1450 if (Line->InPPDirective) {
1451 FormatToken *Last = Line->Last;
1452 SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1;
1453 while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) {
1454 Last = (*PPEnd)->Last;
1455 ++PPEnd;
1456 }
1457
1458 if (affectsTokenRange(*Line->First, *Last,
1459 /*IncludeLeadingNewlines=*/false)) {
1460 SomeLineAffected = true;
1461 markAllAsAffected(I, PPEnd);
1462 }
1463 I = PPEnd;
1464 continue;
1465 }
1466
Daniel Jasper38c82402013-11-29 09:27:43 +00001467 if (nonPPLineAffected(Line, PreviousLine))
Daniel Jasper5500f612013-11-25 11:08:59 +00001468 SomeLineAffected = true;
Daniel Jasper5500f612013-11-25 11:08:59 +00001469
Daniel Jasper38c82402013-11-29 09:27:43 +00001470 PreviousLine = Line;
Daniel Jasper5500f612013-11-25 11:08:59 +00001471 ++I;
1472 }
1473 return SomeLineAffected;
1474 }
1475
Daniel Jasper9c199562013-11-28 15:58:55 +00001476 // Determines whether 'Line' is affected by the SourceRanges given as input.
1477 // Returns \c true if line or one if its children is affected.
Daniel Jasper38c82402013-11-29 09:27:43 +00001478 bool nonPPLineAffected(AnnotatedLine *Line,
1479 const AnnotatedLine *PreviousLine) {
Daniel Jasper9c199562013-11-28 15:58:55 +00001480 bool SomeLineAffected = false;
1481 Line->ChildrenAffected =
1482 computeAffectedLines(Line->Children.begin(), Line->Children.end());
1483 if (Line->ChildrenAffected)
1484 SomeLineAffected = true;
1485
1486 // Stores whether one of the line's tokens is directly affected.
1487 bool SomeTokenAffected = false;
1488 // Stores whether we need to look at the leading newlines of the next token
1489 // in order to determine whether it was affected.
1490 bool IncludeLeadingNewlines = false;
1491
1492 // Stores whether the first child line of any of this line's tokens is
1493 // affected.
1494 bool SomeFirstChildAffected = false;
1495
1496 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
1497 // Determine whether 'Tok' was affected.
1498 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
1499 SomeTokenAffected = true;
1500
1501 // Determine whether the first child of 'Tok' was affected.
1502 if (!Tok->Children.empty() && Tok->Children.front()->Affected)
1503 SomeFirstChildAffected = true;
1504
1505 IncludeLeadingNewlines = Tok->Children.empty();
1506 }
1507
1508 // Was this line moved, i.e. has it previously been on the same line as an
1509 // affected line?
Daniel Jasper38c82402013-11-29 09:27:43 +00001510 bool LineMoved = PreviousLine && PreviousLine->Affected &&
1511 Line->First->NewlinesBefore == 0;
Daniel Jasper9c199562013-11-28 15:58:55 +00001512
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001513 bool IsContinuedComment =
1514 Line->First->is(tok::comment) && Line->First->Next == nullptr &&
1515 Line->First->NewlinesBefore < 2 && PreviousLine &&
1516 PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
Daniel Jasper38c82402013-11-29 09:27:43 +00001517
1518 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
1519 IsContinuedComment) {
Daniel Jasper9c199562013-11-28 15:58:55 +00001520 Line->Affected = true;
Daniel Jasper9c199562013-11-28 15:58:55 +00001521 SomeLineAffected = true;
Daniel Jasper9c199562013-11-28 15:58:55 +00001522 }
1523 return SomeLineAffected;
1524 }
1525
Daniel Jasper5500f612013-11-25 11:08:59 +00001526 // Marks all lines between I and E as well as all their children as affected.
1527 void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I,
1528 SmallVectorImpl<AnnotatedLine *>::iterator E) {
1529 while (I != E) {
1530 (*I)->Affected = true;
1531 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
1532 ++I;
1533 }
1534 }
1535
1536 // Returns true if the range from 'First' to 'Last' intersects with one of the
1537 // input ranges.
1538 bool affectsTokenRange(const FormatToken &First, const FormatToken &Last,
1539 bool IncludeLeadingNewlines) {
1540 SourceLocation Start = First.WhitespaceRange.getBegin();
1541 if (!IncludeLeadingNewlines)
1542 Start = Start.getLocWithOffset(First.LastNewlineOffset);
Daniel Jasper5877bf12013-11-25 11:53:05 +00001543 SourceLocation End = Last.getStartOfNonWhitespace();
Daniel Jasperac29eac2014-10-29 23:40:50 +00001544 End = End.getLocWithOffset(Last.TokenText.size());
Daniel Jasper5500f612013-11-25 11:08:59 +00001545 CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
1546 return affectsCharSourceRange(Range);
1547 }
1548
1549 // Returns true if one of the input ranges intersect the leading empty lines
1550 // before 'Tok'.
1551 bool affectsLeadingEmptyLines(const FormatToken &Tok) {
1552 CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
1553 Tok.WhitespaceRange.getBegin(),
1554 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
1555 return affectsCharSourceRange(EmptyLineRange);
1556 }
1557
1558 // Returns true if 'Range' intersects with one of the input ranges.
1559 bool affectsCharSourceRange(const CharSourceRange &Range) {
1560 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
1561 E = Ranges.end();
1562 I != E; ++I) {
1563 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
1564 !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
1565 return true;
1566 }
1567 return false;
1568 }
1569
Alexander Kornienko9e649af2013-09-11 12:25:57 +00001570 static bool inputUsesCRLF(StringRef Text) {
1571 return Text.count('\r') * 2 > Text.count('\n');
1572 }
1573
Daniel Jasper352f0df2015-07-18 16:35:30 +00001574 bool
1575 hasCpp03IncompatibleFormat(const SmallVectorImpl<AnnotatedLine *> &Lines) {
1576 for (const AnnotatedLine* Line : Lines) {
1577 if (hasCpp03IncompatibleFormat(Line->Children))
1578 return true;
1579 for (FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next) {
1580 if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
1581 if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener))
1582 return true;
1583 if (Tok->is(TT_TemplateCloser) &&
1584 Tok->Previous->is(TT_TemplateCloser))
1585 return true;
1586 }
1587 }
1588 }
1589 return false;
1590 }
1591
1592 int countVariableAlignments(const SmallVectorImpl<AnnotatedLine *> &Lines) {
1593 int AlignmentDiff = 0;
1594 for (const AnnotatedLine* Line : Lines) {
1595 AlignmentDiff += countVariableAlignments(Line->Children);
1596 for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) {
1597 if (!Tok->is(TT_PointerOrReference))
1598 continue;
1599 bool SpaceBefore =
1600 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
1601 bool SpaceAfter = Tok->Next->WhitespaceRange.getBegin() !=
1602 Tok->Next->WhitespaceRange.getEnd();
1603 if (SpaceBefore && !SpaceAfter)
1604 ++AlignmentDiff;
1605 if (!SpaceBefore && SpaceAfter)
1606 --AlignmentDiff;
1607 }
1608 }
1609 return AlignmentDiff;
1610 }
1611
Manuel Klimek71814b42013-10-11 21:25:45 +00001612 void
1613 deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001614 bool HasBinPackedFunction = false;
1615 bool HasOnePerLineFunction = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001616 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001617 if (!AnnotatedLines[i]->First->Next)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001618 continue;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001619 FormatToken *Tok = AnnotatedLines[i]->First->Next;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001620 while (Tok->Next) {
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001621 if (Tok->PackingKind == PPK_BinPacked)
1622 HasBinPackedFunction = true;
1623 if (Tok->PackingKind == PPK_OnePerLine)
1624 HasOnePerLineFunction = true;
1625
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001626 Tok = Tok->Next;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001627 }
1628 }
Daniel Jasper352f0df2015-07-18 16:35:30 +00001629 if (Style.DerivePointerAlignment)
1630 Style.PointerAlignment = countVariableAlignments(AnnotatedLines) <= 0
1631 ? FormatStyle::PAS_Left
1632 : FormatStyle::PAS_Right;
1633 if (Style.Standard == FormatStyle::LS_Auto)
1634 Style.Standard = hasCpp03IncompatibleFormat(AnnotatedLines)
1635 ? FormatStyle::LS_Cpp11
1636 : FormatStyle::LS_Cpp03;
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001637 BinPackInconclusiveFunctions =
1638 HasBinPackedFunction || !HasOnePerLineFunction;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001639 }
1640
Craig Topperfb6b25b2014-03-15 04:29:04 +00001641 void consumeUnwrappedLine(const UnwrappedLine &TheLine) override {
Manuel Klimek71814b42013-10-11 21:25:45 +00001642 assert(!UnwrappedLines.empty());
1643 UnwrappedLines.back().push_back(TheLine);
1644 }
1645
Craig Topperfb6b25b2014-03-15 04:29:04 +00001646 void finishRun() override {
Manuel Klimek71814b42013-10-11 21:25:45 +00001647 UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
Daniel Jasperf7935112012-12-03 18:12:45 +00001648 }
1649
1650 FormatStyle Style;
Daniel Jasper23376252014-09-09 14:37:39 +00001651 FileID ID;
Daniel Jasperf7935112012-12-03 18:12:45 +00001652 SourceManager &SourceMgr;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001653 WhitespaceManager Whitespaces;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001654 SmallVector<CharSourceRange, 8> Ranges;
Manuel Klimek71814b42013-10-11 21:25:45 +00001655 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001656
1657 encoding::Encoding Encoding;
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001658 bool BinPackInconclusiveFunctions;
Daniel Jasperf7935112012-12-03 18:12:45 +00001659};
1660
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001661struct IncludeDirective {
1662 StringRef Filename;
1663 StringRef Text;
1664 unsigned Offset;
Daniel Jasper85c472d2015-09-29 07:53:08 +00001665 unsigned Category;
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001666};
1667
Craig Topperaf35e852013-06-30 22:29:28 +00001668} // end anonymous namespace
1669
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001670// Determines whether 'Ranges' intersects with ('Start', 'End').
1671static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start,
1672 unsigned End) {
1673 for (auto Range : Ranges) {
1674 if (Range.getOffset() < End &&
1675 Range.getOffset() + Range.getLength() > Start)
1676 return true;
1677 }
1678 return false;
1679}
1680
1681// Sorts a block of includes given by 'Includes' alphabetically adding the
1682// necessary replacement to 'Replaces'. 'Includes' must be in strict source
1683// order.
1684static void sortIncludes(const FormatStyle &Style,
1685 const SmallVectorImpl<IncludeDirective> &Includes,
1686 ArrayRef<tooling::Range> Ranges, StringRef FileName,
1687 tooling::Replacements &Replaces) {
1688 if (!affectsRange(Ranges, Includes.front().Offset,
1689 Includes.back().Offset + Includes.back().Text.size()))
1690 return;
1691 SmallVector<unsigned, 16> Indices;
1692 for (unsigned i = 0, e = Includes.size(); i != e; ++i)
1693 Indices.push_back(i);
1694 std::sort(Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) {
Daniel Jasper85c472d2015-09-29 07:53:08 +00001695 return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) <
1696 std::tie(Includes[RHSI].Category, Includes[RHSI].Filename);
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001697 });
1698
1699 // If the #includes are out of order, we generate a single replacement fixing
1700 // the entire block. Otherwise, no replacement is generated.
1701 bool OutOfOrder = false;
1702 for (unsigned i = 1, e = Indices.size(); i != e; ++i) {
1703 if (Indices[i] != i) {
1704 OutOfOrder = true;
1705 break;
1706 }
1707 }
1708 if (!OutOfOrder)
1709 return;
1710
1711 std::string result = Includes[Indices[0]].Text;
1712 for (unsigned i = 1, e = Indices.size(); i != e; ++i) {
1713 result += "\n";
1714 result += Includes[Indices[i]].Text;
1715 }
1716
1717 // Sorting #includes shouldn't change their total number of characters.
1718 // This would otherwise mess up 'Ranges'.
1719 assert(result.size() ==
1720 Includes.back().Offset + Includes.back().Text.size() -
1721 Includes.front().Offset);
1722
1723 Replaces.insert(tooling::Replacement(FileName, Includes.front().Offset,
1724 result.size(), result));
1725}
1726
1727tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
1728 ArrayRef<tooling::Range> Ranges,
1729 StringRef FileName) {
1730 tooling::Replacements Replaces;
1731 unsigned Prev = 0;
1732 unsigned SearchFrom = 0;
Daniel Jasper85c472d2015-09-29 07:53:08 +00001733 llvm::Regex IncludeRegex(
1734 R"(^[\t\ ]*#[\t\ ]*include[^"<]*(["<][^">]*[">]))");
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001735 SmallVector<StringRef, 4> Matches;
1736 SmallVector<IncludeDirective, 16> IncludesInBlock;
Daniel Jasper85c472d2015-09-29 07:53:08 +00001737
1738 // In compiled files, consider the first #include to be the main #include of
1739 // the file if it is not a system #include. This ensures that the header
1740 // doesn't have hidden dependencies
1741 // (http://llvm.org/docs/CodingStandards.html#include-style).
1742 //
1743 // FIXME: Do some sanity checking, e.g. edit distance of the base name, to fix
1744 // cases where the first #include is unlikely to be the main header.
1745 bool LookForMainHeader = FileName.endswith(".c") ||
1746 FileName.endswith(".cc") ||
1747 FileName.endswith(".cpp")||
1748 FileName.endswith(".c++")||
1749 FileName.endswith(".cxx");
1750
1751 // Create pre-compiled regular expressions for the #include categories.
1752 SmallVector<llvm::Regex, 4> CategoryRegexs;
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +00001753 for (const auto &Category : Style.IncludeCategories)
1754 CategoryRegexs.emplace_back(Category.Regex);
Daniel Jasper85c472d2015-09-29 07:53:08 +00001755
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001756 for (;;) {
1757 auto Pos = Code.find('\n', SearchFrom);
1758 StringRef Line =
1759 Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
1760 if (!Line.endswith("\\")) {
1761 if (IncludeRegex.match(Line, &Matches)) {
Daniel Jasper85c472d2015-09-29 07:53:08 +00001762 unsigned Category;
1763 if (LookForMainHeader && !Matches[1].startswith("<")) {
1764 Category = 0;
1765 } else {
1766 Category = UINT_MAX;
1767 for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i) {
1768 if (CategoryRegexs[i].match(Matches[1])) {
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +00001769 Category = Style.IncludeCategories[i].Priority;
Daniel Jasper85c472d2015-09-29 07:53:08 +00001770 break;
1771 }
1772 }
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001773 }
Daniel Jasper85c472d2015-09-29 07:53:08 +00001774 LookForMainHeader = false;
1775 IncludesInBlock.push_back({Matches[1], Line, Prev, Category});
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001776 } else if (!IncludesInBlock.empty()) {
1777 sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces);
1778 IncludesInBlock.clear();
1779 }
1780 Prev = Pos + 1;
1781 }
1782 if (Pos == StringRef::npos || Pos + 1 == Code.size())
1783 break;
1784 SearchFrom = Pos + 1;
1785 }
1786 if (!IncludesInBlock.empty())
1787 sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces);
1788 return Replaces;
1789}
1790
Daniel Jasper23376252014-09-09 14:37:39 +00001791tooling::Replacements reformat(const FormatStyle &Style,
1792 SourceManager &SourceMgr, FileID ID,
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001793 ArrayRef<CharSourceRange> Ranges,
1794 bool *IncompleteFormat) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001795 FormatStyle Expanded = expandPresets(Style);
1796 if (Expanded.DisableFormat)
Daniel Jasper23376252014-09-09 14:37:39 +00001797 return tooling::Replacements();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001798 Formatter formatter(Expanded, SourceMgr, ID, Ranges);
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001799 return formatter.format(IncompleteFormat);
Daniel Jasperf7935112012-12-03 18:12:45 +00001800}
1801
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001802tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
Benjamin Kramerd0eed3a2014-10-03 18:52:48 +00001803 ArrayRef<tooling::Range> Ranges,
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001804 StringRef FileName, bool *IncompleteFormat) {
Daniel Jasper23376252014-09-09 14:37:39 +00001805 if (Style.DisableFormat)
1806 return tooling::Replacements();
1807
Benjamin Kramer2e2351a2015-10-06 10:04:08 +00001808 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
1809 new vfs::InMemoryFileSystem);
1810 FileManager Files(FileSystemOptions(), InMemoryFileSystem);
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001811 DiagnosticsEngine Diagnostics(
1812 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1813 new DiagnosticOptions);
1814 SourceManager SourceMgr(Diagnostics, Files);
Benjamin Kramer2e2351a2015-10-06 10:04:08 +00001815 InMemoryFileSystem->addFile(FileName, 0,
1816 llvm::MemoryBuffer::getMemBuffer(Code, FileName));
1817 FileID ID = SourceMgr.createFileID(Files.getFile(FileName), SourceLocation(),
1818 clang::SrcMgr::C_User);
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001819 SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
1820 std::vector<CharSourceRange> CharRanges;
Benjamin Kramerd0eed3a2014-10-03 18:52:48 +00001821 for (const tooling::Range &Range : Ranges) {
1822 SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset());
1823 SourceLocation End = Start.getLocWithOffset(Range.getLength());
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001824 CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1825 }
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001826 return reformat(Style, SourceMgr, ID, CharRanges, IncompleteFormat);
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001827}
1828
Daniel Jasper4db69bd2014-09-04 18:23:42 +00001829LangOptions getFormattingLangOpts(const FormatStyle &Style) {
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001830 LangOptions LangOpts;
1831 LangOpts.CPlusPlus = 1;
Daniel Jasper4db69bd2014-09-04 18:23:42 +00001832 LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
1833 LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
Daniel Jasper55213652013-03-22 10:01:29 +00001834 LangOpts.LineComment = 1;
Daniel Jasper1662bfe2015-04-03 21:15:46 +00001835 bool AlternativeOperators = Style.Language == FormatStyle::LK_Cpp;
Daniel Jasper30a24062014-11-14 09:02:28 +00001836 LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001837 LangOpts.Bool = 1;
1838 LangOpts.ObjC1 = 1;
1839 LangOpts.ObjC2 = 1;
Nico Weberfac23712015-02-04 15:26:27 +00001840 LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
Saleem Abdulrasoold170c4b2015-10-04 17:51:05 +00001841 LangOpts.DeclSpecKeyword = 1; // To get __declspec.
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001842 return LangOpts;
1843}
1844
Edwin Vaned544aa72013-09-30 13:31:48 +00001845const char *StyleOptionHelpDescription =
1846 "Coding style, currently supports:\n"
1847 " LLVM, Google, Chromium, Mozilla, WebKit.\n"
1848 "Use -style=file to load style configuration from\n"
1849 ".clang-format file located in one of the parent\n"
1850 "directories of the source file (or current\n"
1851 "directory for stdin).\n"
1852 "Use -style=\"{key: value, ...}\" to set specific\n"
1853 "parameters, e.g.:\n"
1854 " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
1855
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001856static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001857 if (FileName.endswith(".java")) {
1858 return FormatStyle::LK_Java;
Daniel Jasper8c68a642015-03-11 14:58:38 +00001859 } else if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts")) {
1860 // JavaScript or TypeScript.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001861 return FormatStyle::LK_JavaScript;
Daniel Jasper7052ce62014-01-19 09:04:08 +00001862 } else if (FileName.endswith_lower(".proto") ||
1863 FileName.endswith_lower(".protodevel")) {
1864 return FormatStyle::LK_Proto;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001865 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001866 return FormatStyle::LK_Cpp;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001867}
1868
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001869FormatStyle getStyle(StringRef StyleName, StringRef FileName,
1870 StringRef FallbackStyle) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001871 FormatStyle Style = getLLVMStyle();
1872 Style.Language = getLanguageByFileName(FileName);
1873 if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) {
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001874 llvm::errs() << "Invalid fallback style \"" << FallbackStyle
1875 << "\" using LLVM style\n";
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001876 return Style;
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001877 }
Edwin Vaned544aa72013-09-30 13:31:48 +00001878
1879 if (StyleName.startswith("{")) {
1880 // Parse YAML/JSON style from the command line.
Rafael Espindolac0809172014-06-12 14:02:15 +00001881 if (std::error_code ec = parseConfiguration(StyleName, &Style)) {
Alexander Kornienkoe2e03872013-10-14 00:46:35 +00001882 llvm::errs() << "Error parsing -style: " << ec.message() << ", using "
1883 << FallbackStyle << " style\n";
Edwin Vaned544aa72013-09-30 13:31:48 +00001884 }
1885 return Style;
1886 }
1887
1888 if (!StyleName.equals_lower("file")) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001889 if (!getPredefinedStyle(StyleName, Style.Language, &Style))
Edwin Vaned544aa72013-09-30 13:31:48 +00001890 llvm::errs() << "Invalid value for -style, using " << FallbackStyle
1891 << " style\n";
1892 return Style;
1893 }
1894
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001895 // Look for .clang-format/_clang-format file in the file's parent directories.
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001896 SmallString<128> UnsuitableConfigFiles;
Edwin Vaned544aa72013-09-30 13:31:48 +00001897 SmallString<128> Path(FileName);
1898 llvm::sys::fs::make_absolute(Path);
Alexander Kornienkoe2e03872013-10-14 00:46:35 +00001899 for (StringRef Directory = Path; !Directory.empty();
Edwin Vaned544aa72013-09-30 13:31:48 +00001900 Directory = llvm::sys::path::parent_path(Directory)) {
1901 if (!llvm::sys::fs::is_directory(Directory))
1902 continue;
1903 SmallString<128> ConfigFile(Directory);
1904
1905 llvm::sys::path::append(ConfigFile, ".clang-format");
1906 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1907 bool IsFile = false;
1908 // Ignore errors from is_regular_file: we only need to know if we can read
1909 // the file or not.
1910 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1911
1912 if (!IsFile) {
1913 // Try _clang-format too, since dotfiles are not commonly used on Windows.
1914 ConfigFile = Directory;
1915 llvm::sys::path::append(ConfigFile, "_clang-format");
1916 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1917 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1918 }
1919
1920 if (IsFile) {
Rafael Espindola2d2b4202014-07-06 17:43:24 +00001921 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
1922 llvm::MemoryBuffer::getFile(ConfigFile.c_str());
1923 if (std::error_code EC = Text.getError()) {
1924 llvm::errs() << EC.message() << "\n";
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001925 break;
Edwin Vaned544aa72013-09-30 13:31:48 +00001926 }
Rafael Espindola2d2b4202014-07-06 17:43:24 +00001927 if (std::error_code ec =
1928 parseConfiguration(Text.get()->getBuffer(), &Style)) {
Rafael Espindolad0136702014-06-12 02:50:04 +00001929 if (ec == ParseError::Unsuitable) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001930 if (!UnsuitableConfigFiles.empty())
1931 UnsuitableConfigFiles.append(", ");
1932 UnsuitableConfigFiles.append(ConfigFile);
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001933 continue;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001934 }
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001935 llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()
1936 << "\n";
1937 break;
Edwin Vaned544aa72013-09-30 13:31:48 +00001938 }
1939 DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
1940 return Style;
1941 }
1942 }
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001943 if (!UnsuitableConfigFiles.empty()) {
1944 llvm::errs() << "Configuration file(s) do(es) not support "
1945 << getLanguageName(Style.Language) << ": "
1946 << UnsuitableConfigFiles << "\n";
1947 }
Edwin Vaned544aa72013-09-30 13:31:48 +00001948 return Style;
1949}
1950
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001951} // namespace format
1952} // namespace clang