blob: 0afa9aa9ce2db02f29cdff555c6439f3f037c408 [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 Jasper8d0e2232015-10-12 03:13:48 +0000879 bool InCharacterClass = false;
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000880 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) {
881 ++TokenCount;
Daniel Jasperf7372152015-07-02 14:14:04 +0000882 auto Prev = I + 1;
883 while (Prev != E && Prev[0]->is(tok::comment))
884 ++Prev;
Daniel Jasper8d0e2232015-10-12 03:13:48 +0000885 // Slashes in character classes (delimited by [ and ]) do not need
886 // escaping. Escaping of the squares themselves is already handled by
887 // \c tryMergeEscapeSequence(), a plain tok::r_square must be non-escaped.
888 if (I[0]->is(tok::r_square))
889 InCharacterClass = true;
890 if (I[0]->is(tok::l_square)) {
891 if (!InCharacterClass)
892 return false;
893 InCharacterClass = false;
894 }
895 if (!InCharacterClass && I[0]->isOneOf(tok::slash, tok::slashequal) &&
Daniel Jasperf7372152015-07-02 14:14:04 +0000896 (Prev == E ||
897 ((Prev[0]->isOneOf(tok::l_paren, tok::semi, tok::l_brace,
898 tok::r_brace, tok::exclaim, tok::l_square,
899 tok::colon, tok::comma, tok::question,
900 tok::kw_return) ||
901 Prev[0]->isBinaryOperator())))) {
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000902 unsigned LastColumn = Tokens.back()->OriginalColumn;
903 SourceLocation Loc = Tokens.back()->Tok.getLocation();
Daniel Jasper23376252014-09-09 14:37:39 +0000904 if (MightEndWithEscapedSlash) {
Daniel Jasper23376252014-09-09 14:37:39 +0000905 // This regex literal ends in '\//'. Skip past the '//' of the last
906 // token and re-start lexing from there.
Daniel Jasper49a9a282014-10-29 16:51:38 +0000907 resetLexer(SourceMgr.getFileOffset(Loc) + 2);
Daniel Jasper6b8d26c2015-06-24 16:01:02 +0000908 } else if (SlashInStringPos != StringRef::npos) {
909 // This regex literal ends in a string_literal with a slash inside.
910 // Calculate end column and reset lexer appropriately.
911 resetLexer(SourceMgr.getFileOffset(Loc) + SlashInStringPos + 1);
912 LastColumn += SlashInStringPos;
Daniel Jasper23376252014-09-09 14:37:39 +0000913 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000914 Tokens.resize(Tokens.size() - TokenCount);
915 Tokens.back()->Tok.setKind(tok::unknown);
916 Tokens.back()->Type = TT_RegexLiteral;
Daniel Jasperf1446202015-07-02 15:00:44 +0000917 // Treat regex literals like other string_literals.
918 Tokens.back()->Tok.setKind(tok::string_literal);
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000919 Tokens.back()->ColumnWidth += LastColumn - I[0]->OriginalColumn;
920 return true;
921 }
922
923 // There can't be a newline inside a regex literal.
924 if (I[0]->NewlinesBefore > 0)
925 return false;
926 }
927 return false;
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000928 }
929
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000930 bool tryMergeTemplateString() {
931 if (Tokens.size() < 2)
932 return false;
933
934 FormatToken *EndBacktick = Tokens.back();
Daniel Jasperf69b9222015-05-02 08:05:38 +0000935 // Backticks get lexed as tok::unknown tokens. If a template string contains
Daniel Jasper0d6ac272015-04-16 08:20:51 +0000936 // a comment start, it gets lexed as a tok::comment, or tok::unknown if
937 // unterminated.
Daniel Jasper2ebb0c52015-06-14 07:16:57 +0000938 if (!EndBacktick->isOneOf(tok::comment, tok::string_literal,
939 tok::char_constant, tok::unknown))
Daniel Jasper0d6ac272015-04-16 08:20:51 +0000940 return false;
941 size_t CommentBacktickPos = EndBacktick->TokenText.find('`');
942 // Unknown token that's not actually a backtick, or a comment that doesn't
943 // contain a backtick.
944 if (CommentBacktickPos == StringRef::npos)
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000945 return false;
946
947 unsigned TokenCount = 0;
948 bool IsMultiline = false;
Daniel Jasperf69b9222015-05-02 08:05:38 +0000949 unsigned EndColumnInFirstLine =
950 EndBacktick->OriginalColumn + EndBacktick->ColumnWidth;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000951 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; I++) {
952 ++TokenCount;
Daniel Jasper553a5b02015-07-02 13:08:28 +0000953 if (I[0]->IsMultiline)
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000954 IsMultiline = true;
955
956 // If there was a preceding template string, this must be the start of a
957 // template string, not the end.
958 if (I[0]->is(TT_TemplateString))
959 return false;
960
961 if (I[0]->isNot(tok::unknown) || I[0]->TokenText != "`") {
962 // Keep track of the rhs offset of the last token to wrap across lines -
963 // its the rhs offset of the first line of the template string, used to
964 // determine its width.
965 if (I[0]->IsMultiline)
966 EndColumnInFirstLine = I[0]->OriginalColumn + I[0]->ColumnWidth;
967 // If the token has newlines, the token before it (if it exists) is the
968 // rhs end of the previous line.
Daniel Jasper553a5b02015-07-02 13:08:28 +0000969 if (I[0]->NewlinesBefore > 0 && (I + 1 != E)) {
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000970 EndColumnInFirstLine = I[1]->OriginalColumn + I[1]->ColumnWidth;
Daniel Jasper553a5b02015-07-02 13:08:28 +0000971 IsMultiline = true;
972 }
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000973 continue;
974 }
975
976 Tokens.resize(Tokens.size() - TokenCount);
977 Tokens.back()->Type = TT_TemplateString;
Daniel Jasper0d6ac272015-04-16 08:20:51 +0000978 const char *EndOffset =
979 EndBacktick->TokenText.data() + 1 + CommentBacktickPos;
980 if (CommentBacktickPos != 0) {
981 // If the backtick was not the first character (e.g. in a comment),
982 // re-lex after the backtick position.
983 SourceLocation Loc = EndBacktick->Tok.getLocation();
984 resetLexer(SourceMgr.getFileOffset(Loc) + CommentBacktickPos + 1);
985 }
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000986 Tokens.back()->TokenText =
987 StringRef(Tokens.back()->TokenText.data(),
988 EndOffset - Tokens.back()->TokenText.data());
Daniel Jasperf69b9222015-05-02 08:05:38 +0000989
990 unsigned EndOriginalColumn = EndBacktick->OriginalColumn;
991 if (EndOriginalColumn == 0) {
992 SourceLocation Loc = EndBacktick->Tok.getLocation();
993 EndOriginalColumn = SourceMgr.getSpellingColumnNumber(Loc);
994 }
995 // If the ` is further down within the token (e.g. in a comment).
996 EndOriginalColumn += CommentBacktickPos;
997
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000998 if (IsMultiline) {
999 // ColumnWidth is from backtick to last token in line.
1000 // LastLineColumnWidth is 0 to backtick.
1001 // x = `some content
1002 // until here`;
1003 Tokens.back()->ColumnWidth =
1004 EndColumnInFirstLine - Tokens.back()->OriginalColumn;
Daniel Jasper553a5b02015-07-02 13:08:28 +00001005 // +1 for the ` itself.
1006 Tokens.back()->LastLineColumnWidth = EndOriginalColumn + 1;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001007 Tokens.back()->IsMultiline = true;
1008 } else {
1009 // Token simply spans from start to end, +1 for the ` itself.
1010 Tokens.back()->ColumnWidth =
Daniel Jasperf69b9222015-05-02 08:05:38 +00001011 EndOriginalColumn - Tokens.back()->OriginalColumn + 1;
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001012 }
1013 return true;
1014 }
1015 return false;
1016 }
1017
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001018 bool tryMerge_TMacro() {
Alexander Kornienko81e32942013-09-16 20:20:49 +00001019 if (Tokens.size() < 4)
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001020 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001021 FormatToken *Last = Tokens.back();
1022 if (!Last->is(tok::r_paren))
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001023 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001024
1025 FormatToken *String = Tokens[Tokens.size() - 2];
1026 if (!String->is(tok::string_literal) || String->IsMultiline)
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001027 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001028
1029 if (!Tokens[Tokens.size() - 3]->is(tok::l_paren))
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001030 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001031
1032 FormatToken *Macro = Tokens[Tokens.size() - 4];
1033 if (Macro->TokenText != "_T")
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001034 return false;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001035
1036 const char *Start = Macro->TokenText.data();
1037 const char *End = Last->TokenText.data() + Last->TokenText.size();
1038 String->TokenText = StringRef(Start, End - Start);
1039 String->IsFirst = Macro->IsFirst;
1040 String->LastNewlineOffset = Macro->LastNewlineOffset;
1041 String->WhitespaceRange = Macro->WhitespaceRange;
1042 String->OriginalColumn = Macro->OriginalColumn;
1043 String->ColumnWidth = encoding::columnWidthWithTabs(
1044 String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding);
Daniel Jaspere99c72f2015-03-26 14:47:35 +00001045 String->NewlinesBefore = Macro->NewlinesBefore;
1046 String->HasUnescapedNewline = Macro->HasUnescapedNewline;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001047
1048 Tokens.pop_back();
1049 Tokens.pop_back();
1050 Tokens.pop_back();
1051 Tokens.back() = String;
Alexander Kornienko9aa62402013-11-21 12:43:57 +00001052 return true;
Alexander Kornienko81e32942013-09-16 20:20:49 +00001053 }
1054
Manuel Klimek68b03042014-04-14 09:14:11 +00001055 bool tryMergeConflictMarkers() {
1056 if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof))
1057 return false;
1058
1059 // Conflict lines look like:
1060 // <marker> <text from the vcs>
1061 // For example:
1062 // >>>>>>> /file/in/file/system at revision 1234
1063 //
1064 // We merge all tokens in a line that starts with a conflict marker
1065 // into a single token with a special token type that the unwrapped line
1066 // parser will use to correctly rebuild the underlying code.
1067
1068 FileID ID;
1069 // Get the position of the first token in the line.
1070 unsigned FirstInLineOffset;
1071 std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc(
1072 Tokens[FirstInLineIndex]->getStartOfNonWhitespace());
1073 StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer();
1074 // Calculate the offset of the start of the current line.
1075 auto LineOffset = Buffer.rfind('\n', FirstInLineOffset);
1076 if (LineOffset == StringRef::npos) {
1077 LineOffset = 0;
1078 } else {
1079 ++LineOffset;
1080 }
1081
1082 auto FirstSpace = Buffer.find_first_of(" \n", LineOffset);
1083 StringRef LineStart;
1084 if (FirstSpace == StringRef::npos) {
1085 LineStart = Buffer.substr(LineOffset);
1086 } else {
1087 LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset);
1088 }
1089
1090 TokenType Type = TT_Unknown;
1091 if (LineStart == "<<<<<<<" || LineStart == ">>>>") {
1092 Type = TT_ConflictStart;
1093 } else if (LineStart == "|||||||" || LineStart == "=======" ||
1094 LineStart == "====") {
1095 Type = TT_ConflictAlternative;
1096 } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") {
1097 Type = TT_ConflictEnd;
1098 }
1099
1100 if (Type != TT_Unknown) {
1101 FormatToken *Next = Tokens.back();
1102
1103 Tokens.resize(FirstInLineIndex + 1);
1104 // We do not need to build a complete token here, as we will skip it
1105 // during parsing anyway (as we must not touch whitespace around conflict
1106 // markers).
1107 Tokens.back()->Type = Type;
1108 Tokens.back()->Tok.setKind(tok::kw___unknown_anytype);
1109
1110 Tokens.push_back(Next);
1111 return true;
1112 }
1113
1114 return false;
1115 }
1116
Jacques Pienaarfc275112015-02-18 23:48:37 +00001117 FormatToken *getStashedToken() {
1118 // Create a synthesized second '>' or '<' token.
1119 Token Tok = FormatTok->Tok;
1120 StringRef TokenText = FormatTok->TokenText;
1121
1122 unsigned OriginalColumn = FormatTok->OriginalColumn;
1123 FormatTok = new (Allocator.Allocate()) FormatToken;
1124 FormatTok->Tok = Tok;
1125 SourceLocation TokLocation =
Jacques Pienaar411b2512015-02-24 23:23:24 +00001126 FormatTok->Tok.getLocation().getLocWithOffset(Tok.getLength() - 1);
1127 FormatTok->Tok.setLocation(TokLocation);
Jacques Pienaarfc275112015-02-18 23:48:37 +00001128 FormatTok->WhitespaceRange = SourceRange(TokLocation, TokLocation);
1129 FormatTok->TokenText = TokenText;
1130 FormatTok->ColumnWidth = 1;
Jacques Pienaar411b2512015-02-24 23:23:24 +00001131 FormatTok->OriginalColumn = OriginalColumn + 1;
1132
Jacques Pienaarfc275112015-02-18 23:48:37 +00001133 return FormatTok;
1134 }
1135
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001136 FormatToken *getNextToken() {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001137 if (GreaterStashed) {
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001138 GreaterStashed = false;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001139 return getStashedToken();
1140 }
1141 if (LessStashed) {
1142 LessStashed = false;
1143 return getStashedToken();
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001144 }
1145
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001146 FormatTok = new (Allocator.Allocate()) FormatToken;
Daniel Jasper8369aa52013-07-16 20:28:33 +00001147 readRawToken(*FormatTok);
Manuel Klimek9043c742013-05-27 15:23:34 +00001148 SourceLocation WhitespaceStart =
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001149 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
Alexander Kornienko393e3082013-11-13 14:04:17 +00001150 FormatTok->IsFirst = IsFirstToken;
1151 IsFirstToken = false;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001152
1153 // Consume and record whitespace until we find a significant token.
Manuel Klimek9043c742013-05-27 15:23:34 +00001154 unsigned WhitespaceLength = TrailingWhitespace;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001155 while (FormatTok->Tok.is(tok::unknown)) {
Daniel Jaspere2408e32015-05-06 11:16:43 +00001156 StringRef Text = FormatTok->TokenText;
1157 auto EscapesNewline = [&](int pos) {
1158 // A '\r' here is just part of '\r\n'. Skip it.
1159 if (pos >= 0 && Text[pos] == '\r')
1160 --pos;
1161 // See whether there is an odd number of '\' before this.
1162 unsigned count = 0;
1163 for (; pos >= 0; --pos, ++count)
Daniel Jasperf0fd1c62015-05-10 08:00:25 +00001164 if (Text[pos] != '\\')
Daniel Jaspere2408e32015-05-06 11:16:43 +00001165 break;
1166 return count & 1;
1167 };
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001168 // FIXME: This miscounts tok:unknown tokens that are not just
1169 // whitespace, e.g. a '`' character.
Daniel Jaspere2408e32015-05-06 11:16:43 +00001170 for (int i = 0, e = Text.size(); i != e; ++i) {
1171 switch (Text[i]) {
Manuel Klimek31c85922013-08-29 15:21:40 +00001172 case '\n':
1173 ++FormatTok->NewlinesBefore;
Daniel Jaspere2408e32015-05-06 11:16:43 +00001174 FormatTok->HasUnescapedNewline = !EscapesNewline(i - 1);
Manuel Klimek31c85922013-08-29 15:21:40 +00001175 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
1176 Column = 0;
1177 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001178 case '\r':
Daniel Jasper30029c62015-02-05 11:05:31 +00001179 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
1180 Column = 0;
1181 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001182 case '\f':
1183 case '\v':
1184 Column = 0;
1185 break;
Manuel Klimek31c85922013-08-29 15:21:40 +00001186 case ' ':
1187 ++Column;
1188 break;
1189 case '\t':
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +00001190 Column += Style.TabWidth - Column % Style.TabWidth;
Manuel Klimek31c85922013-08-29 15:21:40 +00001191 break;
Daniel Jasper877615c2013-10-11 19:45:02 +00001192 case '\\':
Daniel Jaspere2408e32015-05-06 11:16:43 +00001193 if (i + 1 == e || (Text[i + 1] != '\r' && Text[i + 1] != '\n'))
Daniel Jasper877615c2013-10-11 19:45:02 +00001194 FormatTok->Type = TT_ImplicitStringLiteral;
1195 break;
Manuel Klimek31c85922013-08-29 15:21:40 +00001196 default:
Daniel Jasper877615c2013-10-11 19:45:02 +00001197 FormatTok->Type = TT_ImplicitStringLiteral;
Manuel Klimek31c85922013-08-29 15:21:40 +00001198 break;
1199 }
1200 }
1201
Daniel Jaspera98b7b02014-11-25 10:05:17 +00001202 if (FormatTok->is(TT_ImplicitStringLiteral))
Daniel Jasper877615c2013-10-11 19:45:02 +00001203 break;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001204 WhitespaceLength += FormatTok->Tok.getLength();
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001205
Daniel Jasper8369aa52013-07-16 20:28:33 +00001206 readRawToken(*FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001207 }
Manuel Klimekef920692013-01-07 07:56:50 +00001208
Manuel Klimek1abf7892013-01-04 23:34:14 +00001209 // In case the token starts with escaped newlines, we want to
1210 // take them into account as whitespace - this pattern is quite frequent
1211 // in macro definitions.
Manuel Klimek1abf7892013-01-04 23:34:14 +00001212 // FIXME: Add a more explicit test.
Daniel Jasper8369aa52013-07-16 20:28:33 +00001213 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
1214 FormatTok->TokenText[1] == '\n') {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001215 ++FormatTok->NewlinesBefore;
Manuel Klimek5c24cca2013-05-23 10:56:37 +00001216 WhitespaceLength += 2;
Daniel Jaspere2408e32015-05-06 11:16:43 +00001217 FormatTok->LastNewlineOffset = 2;
Manuel Klimek31c85922013-08-29 15:21:40 +00001218 Column = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +00001219 FormatTok->TokenText = FormatTok->TokenText.substr(2);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001220 }
Alexander Kornienko39856b72013-09-10 09:38:25 +00001221
1222 FormatTok->WhitespaceRange = SourceRange(
1223 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
1224
Manuel Klimek31c85922013-08-29 15:21:40 +00001225 FormatTok->OriginalColumn = Column;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001226
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001227 TrailingWhitespace = 0;
1228 if (FormatTok->Tok.is(tok::comment)) {
Manuel Klimek31c85922013-08-29 15:21:40 +00001229 // FIXME: Add the trimmed whitespace to Column.
Daniel Jasper8369aa52013-07-16 20:28:33 +00001230 StringRef UntrimmedText = FormatTok->TokenText;
Alexander Kornienko9ab4a772013-09-06 17:24:54 +00001231 FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
Daniel Jasper8369aa52013-07-16 20:28:33 +00001232 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001233 } else if (FormatTok->Tok.is(tok::raw_identifier)) {
Daniel Jasper8369aa52013-07-16 20:28:33 +00001234 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001235 FormatTok->Tok.setIdentifierInfo(&Info);
1236 FormatTok->Tok.setKind(Info.getTokenID());
Daniel Jasperfe2cf662014-11-19 14:11:11 +00001237 if (Style.Language == FormatStyle::LK_Java &&
1238 FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete)) {
1239 FormatTok->Tok.setKind(tok::identifier);
1240 FormatTok->Tok.setIdentifierInfo(nullptr);
1241 }
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001242 } else if (FormatTok->Tok.is(tok::greatergreater)) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001243 FormatTok->Tok.setKind(tok::greater);
Daniel Jasper8369aa52013-07-16 20:28:33 +00001244 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001245 GreaterStashed = true;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001246 } else if (FormatTok->Tok.is(tok::lessless)) {
1247 FormatTok->Tok.setKind(tok::less);
1248 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
1249 LessStashed = true;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001250 }
1251
Alexander Kornienkoee4ca9b2013-06-07 17:45:07 +00001252 // Now FormatTok is the next non-whitespace token.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001253
Alexander Kornienko39856b72013-09-10 09:38:25 +00001254 StringRef Text = FormatTok->TokenText;
1255 size_t FirstNewlinePos = Text.find('\n');
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001256 if (FirstNewlinePos == StringRef::npos) {
1257 // FIXME: ColumnWidth actually depends on the start column, we need to
1258 // take this into account when the token is moved.
1259 FormatTok->ColumnWidth =
1260 encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding);
1261 Column += FormatTok->ColumnWidth;
1262 } else {
Alexander Kornienko39856b72013-09-10 09:38:25 +00001263 FormatTok->IsMultiline = true;
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001264 // FIXME: ColumnWidth actually depends on the start column, we need to
1265 // take this into account when the token is moved.
1266 FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
1267 Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
1268
Alexander Kornienko39856b72013-09-10 09:38:25 +00001269 // The last line of the token always starts in column 0.
1270 // Thus, the length can be precomputed even in the presence of tabs.
1271 FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
1272 Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth,
1273 Encoding);
Alexander Kornienko917f9e02013-09-10 12:29:48 +00001274 Column = FormatTok->LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +00001275 }
Alexander Kornienko39856b72013-09-10 09:38:25 +00001276
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001277 if (Style.Language == FormatStyle::LK_Cpp) {
1278 if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&
1279 Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() ==
1280 tok::pp_define) &&
1281 std::find(ForEachMacros.begin(), ForEachMacros.end(),
1282 FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end()) {
1283 FormatTok->Type = TT_ForEachMacro;
1284 } else if (FormatTok->is(tok::identifier)) {
1285 if (MacroBlockBeginRegex.match(Text)) {
1286 FormatTok->Type = TT_MacroBlockBegin;
1287 } else if (MacroBlockEndRegex.match(Text)) {
1288 FormatTok->Type = TT_MacroBlockEnd;
1289 }
1290 }
1291 }
Daniel Jaspere1e43192014-04-01 12:55:11 +00001292
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001293 return FormatTok;
1294 }
1295
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001296 FormatToken *FormatTok;
Alexander Kornienko393e3082013-11-13 14:04:17 +00001297 bool IsFirstToken;
Jacques Pienaarfc275112015-02-18 23:48:37 +00001298 bool GreaterStashed, LessStashed;
Manuel Klimek31c85922013-08-29 15:21:40 +00001299 unsigned Column;
Manuel Klimek9043c742013-05-27 15:23:34 +00001300 unsigned TrailingWhitespace;
Daniel Jasper23376252014-09-09 14:37:39 +00001301 std::unique_ptr<Lexer> Lex;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001302 SourceManager &SourceMgr;
Daniel Jasper23376252014-09-09 14:37:39 +00001303 FileID ID;
Manuel Klimek31c85922013-08-29 15:21:40 +00001304 FormatStyle &Style;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001305 IdentifierTable IdentTable;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001306 AdditionalKeywords Keywords;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001307 encoding::Encoding Encoding;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001308 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
Manuel Klimek68b03042014-04-14 09:14:11 +00001309 // Index (in 'Tokens') of the last token that starts a new line.
1310 unsigned FirstInLineIndex;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001311 SmallVector<FormatToken *, 16> Tokens;
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001312 SmallVector<IdentifierInfo *, 8> ForEachMacros;
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001313
NAKAMURA Takumi7160c4d2014-08-06 16:53:13 +00001314 bool FormattingDisabled;
Daniel Jasper471894432014-08-06 13:40:26 +00001315
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001316 llvm::Regex MacroBlockBeginRegex;
1317 llvm::Regex MacroBlockEndRegex;
1318
Daniel Jasper8369aa52013-07-16 20:28:33 +00001319 void readRawToken(FormatToken &Tok) {
Daniel Jasper23376252014-09-09 14:37:39 +00001320 Lex->LexFromRawLexer(Tok.Tok);
Daniel Jasper8369aa52013-07-16 20:28:33 +00001321 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
1322 Tok.Tok.getLength());
Daniel Jasper8369aa52013-07-16 20:28:33 +00001323 // For formatting, treat unterminated string literals like normal string
1324 // literals.
Daniel Jasper86fee2f2014-01-31 12:49:42 +00001325 if (Tok.is(tok::unknown)) {
1326 if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') {
1327 Tok.Tok.setKind(tok::string_literal);
1328 Tok.IsUnterminatedLiteral = true;
1329 } else if (Style.Language == FormatStyle::LK_JavaScript &&
1330 Tok.TokenText == "''") {
1331 Tok.Tok.setKind(tok::char_constant);
1332 }
Daniel Jasper8369aa52013-07-16 20:28:33 +00001333 }
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001334
1335 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" ||
1336 Tok.TokenText == "/* clang-format on */")) {
Daniel Jasper471894432014-08-06 13:40:26 +00001337 FormattingDisabled = false;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001338 }
1339
Daniel Jasper471894432014-08-06 13:40:26 +00001340 Tok.Finalized = FormattingDisabled;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001341
1342 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" ||
1343 Tok.TokenText == "/* clang-format off */")) {
Daniel Jasper471894432014-08-06 13:40:26 +00001344 FormattingDisabled = true;
Roman Kashitsyn650ecb52014-09-11 14:47:20 +00001345 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001346 }
Daniel Jasper49a9a282014-10-29 16:51:38 +00001347
1348 void resetLexer(unsigned Offset) {
1349 StringRef Buffer = SourceMgr.getBufferData(ID);
1350 Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID),
1351 getFormattingLangOpts(Style), Buffer.begin(),
1352 Buffer.begin() + Offset, Buffer.end()));
1353 Lex->SetKeepWhitespaceMode(true);
Daniel Jasper55c384e2015-07-02 14:01:34 +00001354 TrailingWhitespace = 0;
Daniel Jasper49a9a282014-10-29 16:51:38 +00001355 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +00001356};
1357
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001358static StringRef getLanguageName(FormatStyle::LanguageKind Language) {
1359 switch (Language) {
1360 case FormatStyle::LK_Cpp:
1361 return "C++";
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001362 case FormatStyle::LK_Java:
1363 return "Java";
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001364 case FormatStyle::LK_JavaScript:
1365 return "JavaScript";
Daniel Jasper7052ce62014-01-19 09:04:08 +00001366 case FormatStyle::LK_Proto:
1367 return "Proto";
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001368 default:
1369 return "Unknown";
1370 }
1371}
1372
Daniel Jasperf7935112012-12-03 18:12:45 +00001373class Formatter : public UnwrappedLineConsumer {
1374public:
Daniel Jasper23376252014-09-09 14:37:39 +00001375 Formatter(const FormatStyle &Style, SourceManager &SourceMgr, FileID ID,
Benjamin Kramerd0eed3a2014-10-03 18:52:48 +00001376 ArrayRef<CharSourceRange> Ranges)
Daniel Jasper23376252014-09-09 14:37:39 +00001377 : Style(Style), ID(ID), SourceMgr(SourceMgr),
1378 Whitespaces(SourceMgr, Style,
1379 inputUsesCRLF(SourceMgr.getBufferData(ID))),
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001380 Ranges(Ranges.begin(), Ranges.end()), UnwrappedLines(1),
Daniel Jasper23376252014-09-09 14:37:39 +00001381 Encoding(encoding::detectEncoding(SourceMgr.getBufferData(ID))) {
Daniel Jasperfa21c072013-07-15 14:33:14 +00001382 DEBUG(llvm::dbgs() << "File encoding: "
1383 << (Encoding == encoding::Encoding_UTF8 ? "UTF8"
1384 : "unknown")
1385 << "\n");
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001386 DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language)
1387 << "\n");
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001388 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001389
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001390 tooling::Replacements format(bool *IncompleteFormat) {
Manuel Klimek71814b42013-10-11 21:25:45 +00001391 tooling::Replacements Result;
Daniel Jasper23376252014-09-09 14:37:39 +00001392 FormatTokenLexer Tokens(SourceMgr, ID, Style, Encoding);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001393
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001394 UnwrappedLineParser Parser(Style, Tokens.getKeywords(), Tokens.lex(),
1395 *this);
Manuel Klimek20e0af62015-05-06 11:56:29 +00001396 Parser.parse();
Manuel Klimek71814b42013-10-11 21:25:45 +00001397 assert(UnwrappedLines.rbegin()->empty());
1398 for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE;
1399 ++Run) {
1400 DEBUG(llvm::dbgs() << "Run " << Run << "...\n");
1401 SmallVector<AnnotatedLine *, 16> AnnotatedLines;
1402 for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) {
1403 AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i]));
1404 }
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001405 tooling::Replacements RunResult =
1406 format(AnnotatedLines, Tokens, IncompleteFormat);
Manuel Klimek71814b42013-10-11 21:25:45 +00001407 DEBUG({
1408 llvm::dbgs() << "Replacements for run " << Run << ":\n";
1409 for (tooling::Replacements::iterator I = RunResult.begin(),
1410 E = RunResult.end();
1411 I != E; ++I) {
1412 llvm::dbgs() << I->toString() << "\n";
1413 }
1414 });
1415 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
1416 delete AnnotatedLines[i];
1417 }
1418 Result.insert(RunResult.begin(), RunResult.end());
1419 Whitespaces.reset();
1420 }
1421 return Result;
1422 }
1423
1424 tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001425 FormatTokenLexer &Tokens,
1426 bool *IncompleteFormat) {
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001427 TokenAnnotator Annotator(Style, Tokens.getKeywords());
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001428 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001429 Annotator.annotate(*AnnotatedLines[i]);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001430 }
Manuel Klimek71814b42013-10-11 21:25:45 +00001431 deriveLocalStyle(AnnotatedLines);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001432 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001433 Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001434 }
Daniel Jasper5500f612013-11-25 11:08:59 +00001435 computeAffectedLines(AnnotatedLines.begin(), AnnotatedLines.end());
Daniel Jasperb67cc422013-04-09 17:46:55 +00001436
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001437 Annotator.setCommentLineLevels(AnnotatedLines);
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001438 ContinuationIndenter Indenter(Style, Tokens.getKeywords(), SourceMgr,
1439 Whitespaces, Encoding,
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001440 BinPackInconclusiveFunctions);
Manuel Klimekd3585db2015-05-11 08:21:35 +00001441 UnwrappedLineFormatter(&Indenter, &Whitespaces, Style, Tokens.getKeywords(),
1442 IncompleteFormat)
1443 .format(AnnotatedLines);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001444 return Whitespaces.generateReplacements();
1445 }
1446
1447private:
Daniel Jasper5500f612013-11-25 11:08:59 +00001448 // Determines which lines are affected by the SourceRanges given as input.
Daniel Jasper9c199562013-11-28 15:58:55 +00001449 // Returns \c true if at least one line between I and E or one of their
1450 // children is affected.
Daniel Jasper5500f612013-11-25 11:08:59 +00001451 bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I,
1452 SmallVectorImpl<AnnotatedLine *>::iterator E) {
1453 bool SomeLineAffected = false;
Craig Topper2145bc02014-05-09 08:15:10 +00001454 const AnnotatedLine *PreviousLine = nullptr;
Daniel Jasper5500f612013-11-25 11:08:59 +00001455 while (I != E) {
1456 AnnotatedLine *Line = *I;
1457 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
1458
1459 // If a line is part of a preprocessor directive, it needs to be formatted
1460 // if any token within the directive is affected.
1461 if (Line->InPPDirective) {
1462 FormatToken *Last = Line->Last;
1463 SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1;
1464 while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) {
1465 Last = (*PPEnd)->Last;
1466 ++PPEnd;
1467 }
1468
1469 if (affectsTokenRange(*Line->First, *Last,
1470 /*IncludeLeadingNewlines=*/false)) {
1471 SomeLineAffected = true;
1472 markAllAsAffected(I, PPEnd);
1473 }
1474 I = PPEnd;
1475 continue;
1476 }
1477
Daniel Jasper38c82402013-11-29 09:27:43 +00001478 if (nonPPLineAffected(Line, PreviousLine))
Daniel Jasper5500f612013-11-25 11:08:59 +00001479 SomeLineAffected = true;
Daniel Jasper5500f612013-11-25 11:08:59 +00001480
Daniel Jasper38c82402013-11-29 09:27:43 +00001481 PreviousLine = Line;
Daniel Jasper5500f612013-11-25 11:08:59 +00001482 ++I;
1483 }
1484 return SomeLineAffected;
1485 }
1486
Daniel Jasper9c199562013-11-28 15:58:55 +00001487 // Determines whether 'Line' is affected by the SourceRanges given as input.
1488 // Returns \c true if line or one if its children is affected.
Daniel Jasper38c82402013-11-29 09:27:43 +00001489 bool nonPPLineAffected(AnnotatedLine *Line,
1490 const AnnotatedLine *PreviousLine) {
Daniel Jasper9c199562013-11-28 15:58:55 +00001491 bool SomeLineAffected = false;
1492 Line->ChildrenAffected =
1493 computeAffectedLines(Line->Children.begin(), Line->Children.end());
1494 if (Line->ChildrenAffected)
1495 SomeLineAffected = true;
1496
1497 // Stores whether one of the line's tokens is directly affected.
1498 bool SomeTokenAffected = false;
1499 // Stores whether we need to look at the leading newlines of the next token
1500 // in order to determine whether it was affected.
1501 bool IncludeLeadingNewlines = false;
1502
1503 // Stores whether the first child line of any of this line's tokens is
1504 // affected.
1505 bool SomeFirstChildAffected = false;
1506
1507 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
1508 // Determine whether 'Tok' was affected.
1509 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines))
1510 SomeTokenAffected = true;
1511
1512 // Determine whether the first child of 'Tok' was affected.
1513 if (!Tok->Children.empty() && Tok->Children.front()->Affected)
1514 SomeFirstChildAffected = true;
1515
1516 IncludeLeadingNewlines = Tok->Children.empty();
1517 }
1518
1519 // Was this line moved, i.e. has it previously been on the same line as an
1520 // affected line?
Daniel Jasper38c82402013-11-29 09:27:43 +00001521 bool LineMoved = PreviousLine && PreviousLine->Affected &&
1522 Line->First->NewlinesBefore == 0;
Daniel Jasper9c199562013-11-28 15:58:55 +00001523
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001524 bool IsContinuedComment =
1525 Line->First->is(tok::comment) && Line->First->Next == nullptr &&
1526 Line->First->NewlinesBefore < 2 && PreviousLine &&
1527 PreviousLine->Affected && PreviousLine->Last->is(tok::comment);
Daniel Jasper38c82402013-11-29 09:27:43 +00001528
1529 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
1530 IsContinuedComment) {
Daniel Jasper9c199562013-11-28 15:58:55 +00001531 Line->Affected = true;
Daniel Jasper9c199562013-11-28 15:58:55 +00001532 SomeLineAffected = true;
Daniel Jasper9c199562013-11-28 15:58:55 +00001533 }
1534 return SomeLineAffected;
1535 }
1536
Daniel Jasper5500f612013-11-25 11:08:59 +00001537 // Marks all lines between I and E as well as all their children as affected.
1538 void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I,
1539 SmallVectorImpl<AnnotatedLine *>::iterator E) {
1540 while (I != E) {
1541 (*I)->Affected = true;
1542 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end());
1543 ++I;
1544 }
1545 }
1546
1547 // Returns true if the range from 'First' to 'Last' intersects with one of the
1548 // input ranges.
1549 bool affectsTokenRange(const FormatToken &First, const FormatToken &Last,
1550 bool IncludeLeadingNewlines) {
1551 SourceLocation Start = First.WhitespaceRange.getBegin();
1552 if (!IncludeLeadingNewlines)
1553 Start = Start.getLocWithOffset(First.LastNewlineOffset);
Daniel Jasper5877bf12013-11-25 11:53:05 +00001554 SourceLocation End = Last.getStartOfNonWhitespace();
Daniel Jasperac29eac2014-10-29 23:40:50 +00001555 End = End.getLocWithOffset(Last.TokenText.size());
Daniel Jasper5500f612013-11-25 11:08:59 +00001556 CharSourceRange Range = CharSourceRange::getCharRange(Start, End);
1557 return affectsCharSourceRange(Range);
1558 }
1559
1560 // Returns true if one of the input ranges intersect the leading empty lines
1561 // before 'Tok'.
1562 bool affectsLeadingEmptyLines(const FormatToken &Tok) {
1563 CharSourceRange EmptyLineRange = CharSourceRange::getCharRange(
1564 Tok.WhitespaceRange.getBegin(),
1565 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset));
1566 return affectsCharSourceRange(EmptyLineRange);
1567 }
1568
1569 // Returns true if 'Range' intersects with one of the input ranges.
1570 bool affectsCharSourceRange(const CharSourceRange &Range) {
1571 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
1572 E = Ranges.end();
1573 I != E; ++I) {
1574 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) &&
1575 !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin()))
1576 return true;
1577 }
1578 return false;
1579 }
1580
Alexander Kornienko9e649af2013-09-11 12:25:57 +00001581 static bool inputUsesCRLF(StringRef Text) {
1582 return Text.count('\r') * 2 > Text.count('\n');
1583 }
1584
Daniel Jasper352f0df2015-07-18 16:35:30 +00001585 bool
1586 hasCpp03IncompatibleFormat(const SmallVectorImpl<AnnotatedLine *> &Lines) {
1587 for (const AnnotatedLine* Line : Lines) {
1588 if (hasCpp03IncompatibleFormat(Line->Children))
1589 return true;
1590 for (FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next) {
1591 if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) {
1592 if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener))
1593 return true;
1594 if (Tok->is(TT_TemplateCloser) &&
1595 Tok->Previous->is(TT_TemplateCloser))
1596 return true;
1597 }
1598 }
1599 }
1600 return false;
1601 }
1602
1603 int countVariableAlignments(const SmallVectorImpl<AnnotatedLine *> &Lines) {
1604 int AlignmentDiff = 0;
1605 for (const AnnotatedLine* Line : Lines) {
1606 AlignmentDiff += countVariableAlignments(Line->Children);
1607 for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) {
1608 if (!Tok->is(TT_PointerOrReference))
1609 continue;
1610 bool SpaceBefore =
1611 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
1612 bool SpaceAfter = Tok->Next->WhitespaceRange.getBegin() !=
1613 Tok->Next->WhitespaceRange.getEnd();
1614 if (SpaceBefore && !SpaceAfter)
1615 ++AlignmentDiff;
1616 if (!SpaceBefore && SpaceAfter)
1617 --AlignmentDiff;
1618 }
1619 }
1620 return AlignmentDiff;
1621 }
1622
Manuel Klimek71814b42013-10-11 21:25:45 +00001623 void
1624 deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001625 bool HasBinPackedFunction = false;
1626 bool HasOnePerLineFunction = false;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001627 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001628 if (!AnnotatedLines[i]->First->Next)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001629 continue;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001630 FormatToken *Tok = AnnotatedLines[i]->First->Next;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001631 while (Tok->Next) {
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001632 if (Tok->PackingKind == PPK_BinPacked)
1633 HasBinPackedFunction = true;
1634 if (Tok->PackingKind == PPK_OnePerLine)
1635 HasOnePerLineFunction = true;
1636
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001637 Tok = Tok->Next;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001638 }
1639 }
Daniel Jasper352f0df2015-07-18 16:35:30 +00001640 if (Style.DerivePointerAlignment)
1641 Style.PointerAlignment = countVariableAlignments(AnnotatedLines) <= 0
1642 ? FormatStyle::PAS_Left
1643 : FormatStyle::PAS_Right;
1644 if (Style.Standard == FormatStyle::LS_Auto)
1645 Style.Standard = hasCpp03IncompatibleFormat(AnnotatedLines)
1646 ? FormatStyle::LS_Cpp11
1647 : FormatStyle::LS_Cpp03;
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001648 BinPackInconclusiveFunctions =
1649 HasBinPackedFunction || !HasOnePerLineFunction;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001650 }
1651
Craig Topperfb6b25b2014-03-15 04:29:04 +00001652 void consumeUnwrappedLine(const UnwrappedLine &TheLine) override {
Manuel Klimek71814b42013-10-11 21:25:45 +00001653 assert(!UnwrappedLines.empty());
1654 UnwrappedLines.back().push_back(TheLine);
1655 }
1656
Craig Topperfb6b25b2014-03-15 04:29:04 +00001657 void finishRun() override {
Manuel Klimek71814b42013-10-11 21:25:45 +00001658 UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>());
Daniel Jasperf7935112012-12-03 18:12:45 +00001659 }
1660
1661 FormatStyle Style;
Daniel Jasper23376252014-09-09 14:37:39 +00001662 FileID ID;
Daniel Jasperf7935112012-12-03 18:12:45 +00001663 SourceManager &SourceMgr;
Daniel Jasperaa701fa2013-01-18 08:44:07 +00001664 WhitespaceManager Whitespaces;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001665 SmallVector<CharSourceRange, 8> Ranges;
Manuel Klimek71814b42013-10-11 21:25:45 +00001666 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001667
1668 encoding::Encoding Encoding;
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001669 bool BinPackInconclusiveFunctions;
Daniel Jasperf7935112012-12-03 18:12:45 +00001670};
1671
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001672struct IncludeDirective {
1673 StringRef Filename;
1674 StringRef Text;
1675 unsigned Offset;
Daniel Jasper85c472d2015-09-29 07:53:08 +00001676 unsigned Category;
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001677};
1678
Craig Topperaf35e852013-06-30 22:29:28 +00001679} // end anonymous namespace
1680
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001681// Determines whether 'Ranges' intersects with ('Start', 'End').
1682static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start,
1683 unsigned End) {
1684 for (auto Range : Ranges) {
1685 if (Range.getOffset() < End &&
1686 Range.getOffset() + Range.getLength() > Start)
1687 return true;
1688 }
1689 return false;
1690}
1691
1692// Sorts a block of includes given by 'Includes' alphabetically adding the
1693// necessary replacement to 'Replaces'. 'Includes' must be in strict source
1694// order.
1695static void sortIncludes(const FormatStyle &Style,
1696 const SmallVectorImpl<IncludeDirective> &Includes,
1697 ArrayRef<tooling::Range> Ranges, StringRef FileName,
1698 tooling::Replacements &Replaces) {
1699 if (!affectsRange(Ranges, Includes.front().Offset,
1700 Includes.back().Offset + Includes.back().Text.size()))
1701 return;
1702 SmallVector<unsigned, 16> Indices;
1703 for (unsigned i = 0, e = Includes.size(); i != e; ++i)
1704 Indices.push_back(i);
1705 std::sort(Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) {
Daniel Jasper85c472d2015-09-29 07:53:08 +00001706 return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) <
1707 std::tie(Includes[RHSI].Category, Includes[RHSI].Filename);
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001708 });
1709
1710 // If the #includes are out of order, we generate a single replacement fixing
1711 // the entire block. Otherwise, no replacement is generated.
1712 bool OutOfOrder = false;
1713 for (unsigned i = 1, e = Indices.size(); i != e; ++i) {
1714 if (Indices[i] != i) {
1715 OutOfOrder = true;
1716 break;
1717 }
1718 }
1719 if (!OutOfOrder)
1720 return;
1721
1722 std::string result = Includes[Indices[0]].Text;
1723 for (unsigned i = 1, e = Indices.size(); i != e; ++i) {
1724 result += "\n";
1725 result += Includes[Indices[i]].Text;
1726 }
1727
1728 // Sorting #includes shouldn't change their total number of characters.
1729 // This would otherwise mess up 'Ranges'.
1730 assert(result.size() ==
1731 Includes.back().Offset + Includes.back().Text.size() -
1732 Includes.front().Offset);
1733
1734 Replaces.insert(tooling::Replacement(FileName, Includes.front().Offset,
1735 result.size(), result));
1736}
1737
1738tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
1739 ArrayRef<tooling::Range> Ranges,
1740 StringRef FileName) {
1741 tooling::Replacements Replaces;
1742 unsigned Prev = 0;
1743 unsigned SearchFrom = 0;
Daniel Jasper85c472d2015-09-29 07:53:08 +00001744 llvm::Regex IncludeRegex(
1745 R"(^[\t\ ]*#[\t\ ]*include[^"<]*(["<][^">]*[">]))");
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001746 SmallVector<StringRef, 4> Matches;
1747 SmallVector<IncludeDirective, 16> IncludesInBlock;
Daniel Jasper85c472d2015-09-29 07:53:08 +00001748
1749 // In compiled files, consider the first #include to be the main #include of
1750 // the file if it is not a system #include. This ensures that the header
1751 // doesn't have hidden dependencies
1752 // (http://llvm.org/docs/CodingStandards.html#include-style).
1753 //
1754 // FIXME: Do some sanity checking, e.g. edit distance of the base name, to fix
1755 // cases where the first #include is unlikely to be the main header.
1756 bool LookForMainHeader = FileName.endswith(".c") ||
1757 FileName.endswith(".cc") ||
1758 FileName.endswith(".cpp")||
1759 FileName.endswith(".c++")||
1760 FileName.endswith(".cxx");
1761
1762 // Create pre-compiled regular expressions for the #include categories.
1763 SmallVector<llvm::Regex, 4> CategoryRegexs;
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +00001764 for (const auto &Category : Style.IncludeCategories)
1765 CategoryRegexs.emplace_back(Category.Regex);
Daniel Jasper85c472d2015-09-29 07:53:08 +00001766
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001767 for (;;) {
1768 auto Pos = Code.find('\n', SearchFrom);
1769 StringRef Line =
1770 Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
1771 if (!Line.endswith("\\")) {
1772 if (IncludeRegex.match(Line, &Matches)) {
Daniel Jasper85c472d2015-09-29 07:53:08 +00001773 unsigned Category;
1774 if (LookForMainHeader && !Matches[1].startswith("<")) {
1775 Category = 0;
1776 } else {
1777 Category = UINT_MAX;
1778 for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i) {
1779 if (CategoryRegexs[i].match(Matches[1])) {
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +00001780 Category = Style.IncludeCategories[i].Priority;
Daniel Jasper85c472d2015-09-29 07:53:08 +00001781 break;
1782 }
1783 }
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001784 }
Daniel Jasper85c472d2015-09-29 07:53:08 +00001785 LookForMainHeader = false;
1786 IncludesInBlock.push_back({Matches[1], Line, Prev, Category});
Daniel Jasperd89ae9d2015-09-23 08:30:47 +00001787 } else if (!IncludesInBlock.empty()) {
1788 sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces);
1789 IncludesInBlock.clear();
1790 }
1791 Prev = Pos + 1;
1792 }
1793 if (Pos == StringRef::npos || Pos + 1 == Code.size())
1794 break;
1795 SearchFrom = Pos + 1;
1796 }
1797 if (!IncludesInBlock.empty())
1798 sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces);
1799 return Replaces;
1800}
1801
Daniel Jasper23376252014-09-09 14:37:39 +00001802tooling::Replacements reformat(const FormatStyle &Style,
1803 SourceManager &SourceMgr, FileID ID,
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001804 ArrayRef<CharSourceRange> Ranges,
1805 bool *IncompleteFormat) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001806 FormatStyle Expanded = expandPresets(Style);
1807 if (Expanded.DisableFormat)
Daniel Jasper23376252014-09-09 14:37:39 +00001808 return tooling::Replacements();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001809 Formatter formatter(Expanded, SourceMgr, ID, Ranges);
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001810 return formatter.format(IncompleteFormat);
Daniel Jasperf7935112012-12-03 18:12:45 +00001811}
1812
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001813tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
Benjamin Kramerd0eed3a2014-10-03 18:52:48 +00001814 ArrayRef<tooling::Range> Ranges,
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001815 StringRef FileName, bool *IncompleteFormat) {
Daniel Jasper23376252014-09-09 14:37:39 +00001816 if (Style.DisableFormat)
1817 return tooling::Replacements();
1818
Benjamin Kramer2e2351a2015-10-06 10:04:08 +00001819 IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
1820 new vfs::InMemoryFileSystem);
1821 FileManager Files(FileSystemOptions(), InMemoryFileSystem);
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001822 DiagnosticsEngine Diagnostics(
1823 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
1824 new DiagnosticOptions);
1825 SourceManager SourceMgr(Diagnostics, Files);
Benjamin Kramer2e2351a2015-10-06 10:04:08 +00001826 InMemoryFileSystem->addFile(FileName, 0,
1827 llvm::MemoryBuffer::getMemBuffer(Code, FileName));
1828 FileID ID = SourceMgr.createFileID(Files.getFile(FileName), SourceLocation(),
1829 clang::SrcMgr::C_User);
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001830 SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
1831 std::vector<CharSourceRange> CharRanges;
Benjamin Kramerd0eed3a2014-10-03 18:52:48 +00001832 for (const tooling::Range &Range : Ranges) {
1833 SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset());
1834 SourceLocation End = Start.getLocWithOffset(Range.getLength());
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001835 CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
1836 }
Manuel Klimekec5c3db2015-05-07 12:26:30 +00001837 return reformat(Style, SourceMgr, ID, CharRanges, IncompleteFormat);
Daniel Jasperec04c0d2013-05-16 10:40:07 +00001838}
1839
Daniel Jasper4db69bd2014-09-04 18:23:42 +00001840LangOptions getFormattingLangOpts(const FormatStyle &Style) {
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001841 LangOptions LangOpts;
1842 LangOpts.CPlusPlus = 1;
Daniel Jasper4db69bd2014-09-04 18:23:42 +00001843 LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
1844 LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
Daniel Jasper55213652013-03-22 10:01:29 +00001845 LangOpts.LineComment = 1;
Daniel Jasper1662bfe2015-04-03 21:15:46 +00001846 bool AlternativeOperators = Style.Language == FormatStyle::LK_Cpp;
Daniel Jasper30a24062014-11-14 09:02:28 +00001847 LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001848 LangOpts.Bool = 1;
1849 LangOpts.ObjC1 = 1;
1850 LangOpts.ObjC2 = 1;
Nico Weberfac23712015-02-04 15:26:27 +00001851 LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
Saleem Abdulrasoold170c4b2015-10-04 17:51:05 +00001852 LangOpts.DeclSpecKeyword = 1; // To get __declspec.
Daniel Jasperc1fa2812013-01-10 13:08:12 +00001853 return LangOpts;
1854}
1855
Edwin Vaned544aa72013-09-30 13:31:48 +00001856const char *StyleOptionHelpDescription =
1857 "Coding style, currently supports:\n"
1858 " LLVM, Google, Chromium, Mozilla, WebKit.\n"
1859 "Use -style=file to load style configuration from\n"
1860 ".clang-format file located in one of the parent\n"
1861 "directories of the source file (or current\n"
1862 "directory for stdin).\n"
1863 "Use -style=\"{key: value, ...}\" to set specific\n"
1864 "parameters, e.g.:\n"
1865 " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
1866
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001867static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001868 if (FileName.endswith(".java")) {
1869 return FormatStyle::LK_Java;
Daniel Jasper8c68a642015-03-11 14:58:38 +00001870 } else if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts")) {
1871 // JavaScript or TypeScript.
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001872 return FormatStyle::LK_JavaScript;
Daniel Jasper7052ce62014-01-19 09:04:08 +00001873 } else if (FileName.endswith_lower(".proto") ||
1874 FileName.endswith_lower(".protodevel")) {
1875 return FormatStyle::LK_Proto;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001876 }
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001877 return FormatStyle::LK_Cpp;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001878}
1879
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001880FormatStyle getStyle(StringRef StyleName, StringRef FileName,
1881 StringRef FallbackStyle) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001882 FormatStyle Style = getLLVMStyle();
1883 Style.Language = getLanguageByFileName(FileName);
1884 if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) {
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001885 llvm::errs() << "Invalid fallback style \"" << FallbackStyle
1886 << "\" using LLVM style\n";
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001887 return Style;
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001888 }
Edwin Vaned544aa72013-09-30 13:31:48 +00001889
1890 if (StyleName.startswith("{")) {
1891 // Parse YAML/JSON style from the command line.
Rafael Espindolac0809172014-06-12 14:02:15 +00001892 if (std::error_code ec = parseConfiguration(StyleName, &Style)) {
Alexander Kornienkoe2e03872013-10-14 00:46:35 +00001893 llvm::errs() << "Error parsing -style: " << ec.message() << ", using "
1894 << FallbackStyle << " style\n";
Edwin Vaned544aa72013-09-30 13:31:48 +00001895 }
1896 return Style;
1897 }
1898
1899 if (!StyleName.equals_lower("file")) {
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001900 if (!getPredefinedStyle(StyleName, Style.Language, &Style))
Edwin Vaned544aa72013-09-30 13:31:48 +00001901 llvm::errs() << "Invalid value for -style, using " << FallbackStyle
1902 << " style\n";
1903 return Style;
1904 }
1905
Alexander Kornienkoc1637f12013-12-10 11:28:13 +00001906 // Look for .clang-format/_clang-format file in the file's parent directories.
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001907 SmallString<128> UnsuitableConfigFiles;
Edwin Vaned544aa72013-09-30 13:31:48 +00001908 SmallString<128> Path(FileName);
1909 llvm::sys::fs::make_absolute(Path);
Alexander Kornienkoe2e03872013-10-14 00:46:35 +00001910 for (StringRef Directory = Path; !Directory.empty();
Edwin Vaned544aa72013-09-30 13:31:48 +00001911 Directory = llvm::sys::path::parent_path(Directory)) {
1912 if (!llvm::sys::fs::is_directory(Directory))
1913 continue;
1914 SmallString<128> ConfigFile(Directory);
1915
1916 llvm::sys::path::append(ConfigFile, ".clang-format");
1917 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1918 bool IsFile = false;
1919 // Ignore errors from is_regular_file: we only need to know if we can read
1920 // the file or not.
1921 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1922
1923 if (!IsFile) {
1924 // Try _clang-format too, since dotfiles are not commonly used on Windows.
1925 ConfigFile = Directory;
1926 llvm::sys::path::append(ConfigFile, "_clang-format");
1927 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
1928 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
1929 }
1930
1931 if (IsFile) {
Rafael Espindola2d2b4202014-07-06 17:43:24 +00001932 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
1933 llvm::MemoryBuffer::getFile(ConfigFile.c_str());
1934 if (std::error_code EC = Text.getError()) {
1935 llvm::errs() << EC.message() << "\n";
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001936 break;
Edwin Vaned544aa72013-09-30 13:31:48 +00001937 }
Rafael Espindola2d2b4202014-07-06 17:43:24 +00001938 if (std::error_code ec =
1939 parseConfiguration(Text.get()->getBuffer(), &Style)) {
Rafael Espindolad0136702014-06-12 02:50:04 +00001940 if (ec == ParseError::Unsuitable) {
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001941 if (!UnsuitableConfigFiles.empty())
1942 UnsuitableConfigFiles.append(", ");
1943 UnsuitableConfigFiles.append(ConfigFile);
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001944 continue;
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001945 }
Alexander Kornienkobc4ae442013-12-02 15:21:38 +00001946 llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message()
1947 << "\n";
1948 break;
Edwin Vaned544aa72013-09-30 13:31:48 +00001949 }
1950 DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
1951 return Style;
1952 }
1953 }
Alexander Kornienkocabdd732013-11-29 15:19:43 +00001954 if (!UnsuitableConfigFiles.empty()) {
1955 llvm::errs() << "Configuration file(s) do(es) not support "
1956 << getLanguageName(Style.Language) << ": "
1957 << UnsuitableConfigFiles << "\n";
1958 }
Edwin Vaned544aa72013-09-30 13:31:48 +00001959 return Style;
1960}
1961
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001962} // namespace format
1963} // namespace clang