1 //===--- 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 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "ContinuationIndenter.h" 17 #include "TokenAnnotator.h" 18 #include "UnwrappedLineFormatter.h" 19 #include "UnwrappedLineParser.h" 20 #include "WhitespaceManager.h" 21 #include "clang/Basic/Diagnostic.h" 22 #include "clang/Basic/DiagnosticOptions.h" 23 #include "clang/Basic/SourceManager.h" 24 #include "clang/Format/Format.h" 25 #include "clang/Lex/Lexer.h" 26 #include "llvm/ADT/STLExtras.h" 27 #include "llvm/Support/Allocator.h" 28 #include "llvm/Support/Debug.h" 29 #include "llvm/Support/Path.h" 30 #include "llvm/Support/YAMLTraits.h" 31 #include <queue> 32 #include <string> 33 34 #define DEBUG_TYPE "format-formatter" 35 36 using clang::format::FormatStyle; 37 38 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string) 39 40 namespace llvm { 41 namespace yaml { 42 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> { 43 static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) { 44 IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp); 45 IO.enumCase(Value, "Java", FormatStyle::LK_Java); 46 IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript); 47 IO.enumCase(Value, "Proto", FormatStyle::LK_Proto); 48 } 49 }; 50 51 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> { 52 static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) { 53 IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03); 54 IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03); 55 IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11); 56 IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11); 57 IO.enumCase(Value, "Auto", FormatStyle::LS_Auto); 58 } 59 }; 60 61 template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> { 62 static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) { 63 IO.enumCase(Value, "Never", FormatStyle::UT_Never); 64 IO.enumCase(Value, "false", FormatStyle::UT_Never); 65 IO.enumCase(Value, "Always", FormatStyle::UT_Always); 66 IO.enumCase(Value, "true", FormatStyle::UT_Always); 67 IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation); 68 } 69 }; 70 71 template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> { 72 static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) { 73 IO.enumCase(Value, "None", FormatStyle::SFS_None); 74 IO.enumCase(Value, "false", FormatStyle::SFS_None); 75 IO.enumCase(Value, "All", FormatStyle::SFS_All); 76 IO.enumCase(Value, "true", FormatStyle::SFS_All); 77 IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline); 78 IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty); 79 } 80 }; 81 82 template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> { 83 static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) { 84 IO.enumCase(Value, "All", FormatStyle::BOS_All); 85 IO.enumCase(Value, "true", FormatStyle::BOS_All); 86 IO.enumCase(Value, "None", FormatStyle::BOS_None); 87 IO.enumCase(Value, "false", FormatStyle::BOS_None); 88 IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment); 89 } 90 }; 91 92 template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> { 93 static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) { 94 IO.enumCase(Value, "Attach", FormatStyle::BS_Attach); 95 IO.enumCase(Value, "Linux", FormatStyle::BS_Linux); 96 IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup); 97 IO.enumCase(Value, "Allman", FormatStyle::BS_Allman); 98 IO.enumCase(Value, "GNU", FormatStyle::BS_GNU); 99 } 100 }; 101 102 template <> 103 struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> { 104 static void enumeration(IO &IO, 105 FormatStyle::NamespaceIndentationKind &Value) { 106 IO.enumCase(Value, "None", FormatStyle::NI_None); 107 IO.enumCase(Value, "Inner", FormatStyle::NI_Inner); 108 IO.enumCase(Value, "All", FormatStyle::NI_All); 109 } 110 }; 111 112 template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> { 113 static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) { 114 IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle); 115 IO.enumCase(Value, "Left", FormatStyle::PAS_Left); 116 IO.enumCase(Value, "Right", FormatStyle::PAS_Right); 117 118 // For backward compatibility. 119 IO.enumCase(Value, "true", FormatStyle::PAS_Left); 120 IO.enumCase(Value, "false", FormatStyle::PAS_Right); 121 } 122 }; 123 124 template <> 125 struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> { 126 static void enumeration(IO &IO, 127 FormatStyle::SpaceBeforeParensOptions &Value) { 128 IO.enumCase(Value, "Never", FormatStyle::SBPO_Never); 129 IO.enumCase(Value, "ControlStatements", 130 FormatStyle::SBPO_ControlStatements); 131 IO.enumCase(Value, "Always", FormatStyle::SBPO_Always); 132 133 // For backward compatibility. 134 IO.enumCase(Value, "false", FormatStyle::SBPO_Never); 135 IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements); 136 } 137 }; 138 139 template <> struct MappingTraits<FormatStyle> { 140 static void mapping(IO &IO, FormatStyle &Style) { 141 // When reading, read the language first, we need it for getPredefinedStyle. 142 IO.mapOptional("Language", Style.Language); 143 144 if (IO.outputting()) { 145 StringRef StylesArray[] = {"LLVM", "Google", "Chromium", 146 "Mozilla", "WebKit", "GNU"}; 147 ArrayRef<StringRef> Styles(StylesArray); 148 for (size_t i = 0, e = Styles.size(); i < e; ++i) { 149 StringRef StyleName(Styles[i]); 150 FormatStyle PredefinedStyle; 151 if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) && 152 Style == PredefinedStyle) { 153 IO.mapOptional("# BasedOnStyle", StyleName); 154 break; 155 } 156 } 157 } else { 158 StringRef BasedOnStyle; 159 IO.mapOptional("BasedOnStyle", BasedOnStyle); 160 if (!BasedOnStyle.empty()) { 161 FormatStyle::LanguageKind OldLanguage = Style.Language; 162 FormatStyle::LanguageKind Language = 163 ((FormatStyle *)IO.getContext())->Language; 164 if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) { 165 IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle)); 166 return; 167 } 168 Style.Language = OldLanguage; 169 } 170 } 171 172 IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); 173 IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket); 174 IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); 175 IO.mapOptional("AlignOperands", Style.AlignOperands); 176 IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); 177 IO.mapOptional("AlignConsecutiveAssignments", 178 Style.AlignConsecutiveAssignments); 179 IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", 180 Style.AllowAllParametersOfDeclarationOnNextLine); 181 IO.mapOptional("AllowShortBlocksOnASingleLine", 182 Style.AllowShortBlocksOnASingleLine); 183 IO.mapOptional("AllowShortCaseLabelsOnASingleLine", 184 Style.AllowShortCaseLabelsOnASingleLine); 185 IO.mapOptional("AllowShortIfStatementsOnASingleLine", 186 Style.AllowShortIfStatementsOnASingleLine); 187 IO.mapOptional("AllowShortLoopsOnASingleLine", 188 Style.AllowShortLoopsOnASingleLine); 189 IO.mapOptional("AllowShortFunctionsOnASingleLine", 190 Style.AllowShortFunctionsOnASingleLine); 191 IO.mapOptional("AlwaysBreakAfterDefinitionReturnType", 192 Style.AlwaysBreakAfterDefinitionReturnType); 193 IO.mapOptional("AlwaysBreakTemplateDeclarations", 194 Style.AlwaysBreakTemplateDeclarations); 195 IO.mapOptional("AlwaysBreakBeforeMultilineStrings", 196 Style.AlwaysBreakBeforeMultilineStrings); 197 IO.mapOptional("BreakBeforeBinaryOperators", 198 Style.BreakBeforeBinaryOperators); 199 IO.mapOptional("BreakBeforeTernaryOperators", 200 Style.BreakBeforeTernaryOperators); 201 IO.mapOptional("BreakConstructorInitializersBeforeComma", 202 Style.BreakConstructorInitializersBeforeComma); 203 IO.mapOptional("BinPackParameters", Style.BinPackParameters); 204 IO.mapOptional("BinPackArguments", Style.BinPackArguments); 205 IO.mapOptional("ColumnLimit", Style.ColumnLimit); 206 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", 207 Style.ConstructorInitializerAllOnOneLineOrOnePerLine); 208 IO.mapOptional("ConstructorInitializerIndentWidth", 209 Style.ConstructorInitializerIndentWidth); 210 IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment); 211 IO.mapOptional("ExperimentalAutoDetectBinPacking", 212 Style.ExperimentalAutoDetectBinPacking); 213 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); 214 IO.mapOptional("IndentWrappedFunctionNames", 215 Style.IndentWrappedFunctionNames); 216 IO.mapOptional("IndentFunctionDeclarationAfterType", 217 Style.IndentWrappedFunctionNames); 218 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); 219 IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks", 220 Style.KeepEmptyLinesAtTheStartOfBlocks); 221 IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); 222 IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth); 223 IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty); 224 IO.mapOptional("ObjCSpaceBeforeProtocolList", 225 Style.ObjCSpaceBeforeProtocolList); 226 IO.mapOptional("PenaltyBreakBeforeFirstCallParameter", 227 Style.PenaltyBreakBeforeFirstCallParameter); 228 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); 229 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); 230 IO.mapOptional("PenaltyBreakFirstLessLess", 231 Style.PenaltyBreakFirstLessLess); 232 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); 233 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", 234 Style.PenaltyReturnTypeOnItsOwnLine); 235 IO.mapOptional("PointerAlignment", Style.PointerAlignment); 236 IO.mapOptional("SpacesBeforeTrailingComments", 237 Style.SpacesBeforeTrailingComments); 238 IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); 239 IO.mapOptional("Standard", Style.Standard); 240 IO.mapOptional("IndentWidth", Style.IndentWidth); 241 IO.mapOptional("TabWidth", Style.TabWidth); 242 IO.mapOptional("UseTab", Style.UseTab); 243 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); 244 IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); 245 IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets); 246 IO.mapOptional("SpacesInAngles", Style.SpacesInAngles); 247 IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); 248 IO.mapOptional("SpacesInCStyleCastParentheses", 249 Style.SpacesInCStyleCastParentheses); 250 IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); 251 IO.mapOptional("SpacesInContainerLiterals", 252 Style.SpacesInContainerLiterals); 253 IO.mapOptional("SpaceBeforeAssignmentOperators", 254 Style.SpaceBeforeAssignmentOperators); 255 IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth); 256 IO.mapOptional("CommentPragmas", Style.CommentPragmas); 257 IO.mapOptional("ForEachMacros", Style.ForEachMacros); 258 259 // For backward compatibility. 260 if (!IO.outputting()) { 261 IO.mapOptional("SpaceAfterControlStatementKeyword", 262 Style.SpaceBeforeParens); 263 IO.mapOptional("PointerBindsToType", Style.PointerAlignment); 264 IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment); 265 } 266 IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens); 267 IO.mapOptional("DisableFormat", Style.DisableFormat); 268 } 269 }; 270 271 // Allows to read vector<FormatStyle> while keeping default values. 272 // IO.getContext() should contain a pointer to the FormatStyle structure, that 273 // will be used to get default values for missing keys. 274 // If the first element has no Language specified, it will be treated as the 275 // default one for the following elements. 276 template <> struct DocumentListTraits<std::vector<FormatStyle>> { 277 static size_t size(IO &IO, std::vector<FormatStyle> &Seq) { 278 return Seq.size(); 279 } 280 static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq, 281 size_t Index) { 282 if (Index >= Seq.size()) { 283 assert(Index == Seq.size()); 284 FormatStyle Template; 285 if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) { 286 Template = Seq[0]; 287 } else { 288 Template = *((const FormatStyle *)IO.getContext()); 289 Template.Language = FormatStyle::LK_None; 290 } 291 Seq.resize(Index + 1, Template); 292 } 293 return Seq[Index]; 294 } 295 }; 296 } // namespace yaml 297 } // namespace llvm 298 299 namespace clang { 300 namespace format { 301 302 const std::error_category &getParseCategory() { 303 static ParseErrorCategory C; 304 return C; 305 } 306 std::error_code make_error_code(ParseError e) { 307 return std::error_code(static_cast<int>(e), getParseCategory()); 308 } 309 310 const char *ParseErrorCategory::name() const LLVM_NOEXCEPT { 311 return "clang-format.parse_error"; 312 } 313 314 std::string ParseErrorCategory::message(int EV) const { 315 switch (static_cast<ParseError>(EV)) { 316 case ParseError::Success: 317 return "Success"; 318 case ParseError::Error: 319 return "Invalid argument"; 320 case ParseError::Unsuitable: 321 return "Unsuitable"; 322 } 323 llvm_unreachable("unexpected parse error"); 324 } 325 326 FormatStyle getLLVMStyle() { 327 FormatStyle LLVMStyle; 328 LLVMStyle.Language = FormatStyle::LK_Cpp; 329 LLVMStyle.AccessModifierOffset = -2; 330 LLVMStyle.AlignEscapedNewlinesLeft = false; 331 LLVMStyle.AlignAfterOpenBracket = true; 332 LLVMStyle.AlignOperands = true; 333 LLVMStyle.AlignTrailingComments = true; 334 LLVMStyle.AlignConsecutiveAssignments = false; 335 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; 336 LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 337 LLVMStyle.AllowShortBlocksOnASingleLine = false; 338 LLVMStyle.AllowShortCaseLabelsOnASingleLine = false; 339 LLVMStyle.AllowShortIfStatementsOnASingleLine = false; 340 LLVMStyle.AllowShortLoopsOnASingleLine = false; 341 LLVMStyle.AlwaysBreakAfterDefinitionReturnType = false; 342 LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; 343 LLVMStyle.AlwaysBreakTemplateDeclarations = false; 344 LLVMStyle.BinPackParameters = true; 345 LLVMStyle.BinPackArguments = true; 346 LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 347 LLVMStyle.BreakBeforeTernaryOperators = true; 348 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; 349 LLVMStyle.BreakConstructorInitializersBeforeComma = false; 350 LLVMStyle.ColumnLimit = 80; 351 LLVMStyle.CommentPragmas = "^ IWYU pragma:"; 352 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 353 LLVMStyle.ConstructorInitializerIndentWidth = 4; 354 LLVMStyle.ContinuationIndentWidth = 4; 355 LLVMStyle.Cpp11BracedListStyle = true; 356 LLVMStyle.DerivePointerAlignment = false; 357 LLVMStyle.ExperimentalAutoDetectBinPacking = false; 358 LLVMStyle.ForEachMacros.push_back("foreach"); 359 LLVMStyle.ForEachMacros.push_back("Q_FOREACH"); 360 LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH"); 361 LLVMStyle.IndentCaseLabels = false; 362 LLVMStyle.IndentWrappedFunctionNames = false; 363 LLVMStyle.IndentWidth = 2; 364 LLVMStyle.TabWidth = 8; 365 LLVMStyle.MaxEmptyLinesToKeep = 1; 366 LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true; 367 LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; 368 LLVMStyle.ObjCBlockIndentWidth = 2; 369 LLVMStyle.ObjCSpaceAfterProperty = false; 370 LLVMStyle.ObjCSpaceBeforeProtocolList = true; 371 LLVMStyle.PointerAlignment = FormatStyle::PAS_Right; 372 LLVMStyle.SpacesBeforeTrailingComments = 1; 373 LLVMStyle.Standard = FormatStyle::LS_Cpp11; 374 LLVMStyle.UseTab = FormatStyle::UT_Never; 375 LLVMStyle.SpacesInParentheses = false; 376 LLVMStyle.SpacesInSquareBrackets = false; 377 LLVMStyle.SpaceInEmptyParentheses = false; 378 LLVMStyle.SpacesInContainerLiterals = true; 379 LLVMStyle.SpacesInCStyleCastParentheses = false; 380 LLVMStyle.SpaceAfterCStyleCast = false; 381 LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; 382 LLVMStyle.SpaceBeforeAssignmentOperators = true; 383 LLVMStyle.SpacesInAngles = false; 384 385 LLVMStyle.PenaltyBreakComment = 300; 386 LLVMStyle.PenaltyBreakFirstLessLess = 120; 387 LLVMStyle.PenaltyBreakString = 1000; 388 LLVMStyle.PenaltyExcessCharacter = 1000000; 389 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; 390 LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19; 391 392 LLVMStyle.DisableFormat = false; 393 394 return LLVMStyle; 395 } 396 397 FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { 398 FormatStyle GoogleStyle = getLLVMStyle(); 399 GoogleStyle.Language = Language; 400 401 GoogleStyle.AccessModifierOffset = -1; 402 GoogleStyle.AlignEscapedNewlinesLeft = true; 403 GoogleStyle.AllowShortIfStatementsOnASingleLine = true; 404 GoogleStyle.AllowShortLoopsOnASingleLine = true; 405 GoogleStyle.AlwaysBreakBeforeMultilineStrings = true; 406 GoogleStyle.AlwaysBreakTemplateDeclarations = true; 407 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 408 GoogleStyle.DerivePointerAlignment = true; 409 GoogleStyle.IndentCaseLabels = true; 410 GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false; 411 GoogleStyle.ObjCSpaceAfterProperty = false; 412 GoogleStyle.ObjCSpaceBeforeProtocolList = false; 413 GoogleStyle.PointerAlignment = FormatStyle::PAS_Left; 414 GoogleStyle.SpacesBeforeTrailingComments = 2; 415 GoogleStyle.Standard = FormatStyle::LS_Auto; 416 417 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; 418 GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1; 419 420 if (Language == FormatStyle::LK_Java) { 421 GoogleStyle.AlignAfterOpenBracket = false; 422 GoogleStyle.AlignOperands = false; 423 GoogleStyle.AlignTrailingComments = false; 424 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 425 GoogleStyle.AllowShortIfStatementsOnASingleLine = false; 426 GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; 427 GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 428 GoogleStyle.ColumnLimit = 100; 429 GoogleStyle.SpaceAfterCStyleCast = true; 430 GoogleStyle.SpacesBeforeTrailingComments = 1; 431 } else if (Language == FormatStyle::LK_JavaScript) { 432 GoogleStyle.BreakBeforeTernaryOperators = false; 433 GoogleStyle.MaxEmptyLinesToKeep = 3; 434 GoogleStyle.SpacesInContainerLiterals = false; 435 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 436 GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; 437 } else if (Language == FormatStyle::LK_Proto) { 438 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 439 GoogleStyle.SpacesInContainerLiterals = false; 440 } 441 442 return GoogleStyle; 443 } 444 445 FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { 446 FormatStyle ChromiumStyle = getGoogleStyle(Language); 447 if (Language == FormatStyle::LK_Java) { 448 ChromiumStyle.AllowShortIfStatementsOnASingleLine = true; 449 ChromiumStyle.IndentWidth = 4; 450 ChromiumStyle.ContinuationIndentWidth = 8; 451 } else { 452 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; 453 ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 454 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; 455 ChromiumStyle.AllowShortLoopsOnASingleLine = false; 456 ChromiumStyle.BinPackParameters = false; 457 ChromiumStyle.DerivePointerAlignment = false; 458 } 459 return ChromiumStyle; 460 } 461 462 FormatStyle getMozillaStyle() { 463 FormatStyle MozillaStyle = getLLVMStyle(); 464 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; 465 MozillaStyle.Cpp11BracedListStyle = false; 466 MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 467 MozillaStyle.DerivePointerAlignment = true; 468 MozillaStyle.IndentCaseLabels = true; 469 MozillaStyle.ObjCSpaceAfterProperty = true; 470 MozillaStyle.ObjCSpaceBeforeProtocolList = false; 471 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; 472 MozillaStyle.PointerAlignment = FormatStyle::PAS_Left; 473 MozillaStyle.Standard = FormatStyle::LS_Cpp03; 474 return MozillaStyle; 475 } 476 477 FormatStyle getWebKitStyle() { 478 FormatStyle Style = getLLVMStyle(); 479 Style.AccessModifierOffset = -4; 480 Style.AlignAfterOpenBracket = false; 481 Style.AlignOperands = false; 482 Style.AlignTrailingComments = false; 483 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 484 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 485 Style.BreakConstructorInitializersBeforeComma = true; 486 Style.Cpp11BracedListStyle = false; 487 Style.ColumnLimit = 0; 488 Style.IndentWidth = 4; 489 Style.NamespaceIndentation = FormatStyle::NI_Inner; 490 Style.ObjCBlockIndentWidth = 4; 491 Style.ObjCSpaceAfterProperty = true; 492 Style.PointerAlignment = FormatStyle::PAS_Left; 493 Style.Standard = FormatStyle::LS_Cpp03; 494 return Style; 495 } 496 497 FormatStyle getGNUStyle() { 498 FormatStyle Style = getLLVMStyle(); 499 Style.AlwaysBreakAfterDefinitionReturnType = true; 500 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 501 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 502 Style.BreakBeforeTernaryOperators = true; 503 Style.Cpp11BracedListStyle = false; 504 Style.ColumnLimit = 79; 505 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 506 Style.Standard = FormatStyle::LS_Cpp03; 507 return Style; 508 } 509 510 FormatStyle getNoStyle() { 511 FormatStyle NoStyle = getLLVMStyle(); 512 NoStyle.DisableFormat = true; 513 return NoStyle; 514 } 515 516 bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language, 517 FormatStyle *Style) { 518 if (Name.equals_lower("llvm")) { 519 *Style = getLLVMStyle(); 520 } else if (Name.equals_lower("chromium")) { 521 *Style = getChromiumStyle(Language); 522 } else if (Name.equals_lower("mozilla")) { 523 *Style = getMozillaStyle(); 524 } else if (Name.equals_lower("google")) { 525 *Style = getGoogleStyle(Language); 526 } else if (Name.equals_lower("webkit")) { 527 *Style = getWebKitStyle(); 528 } else if (Name.equals_lower("gnu")) { 529 *Style = getGNUStyle(); 530 } else if (Name.equals_lower("none")) { 531 *Style = getNoStyle(); 532 } else { 533 return false; 534 } 535 536 Style->Language = Language; 537 return true; 538 } 539 540 std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { 541 assert(Style); 542 FormatStyle::LanguageKind Language = Style->Language; 543 assert(Language != FormatStyle::LK_None); 544 if (Text.trim().empty()) 545 return make_error_code(ParseError::Error); 546 547 std::vector<FormatStyle> Styles; 548 llvm::yaml::Input Input(Text); 549 // DocumentListTraits<vector<FormatStyle>> uses the context to get default 550 // values for the fields, keys for which are missing from the configuration. 551 // Mapping also uses the context to get the language to find the correct 552 // base style. 553 Input.setContext(Style); 554 Input >> Styles; 555 if (Input.error()) 556 return Input.error(); 557 558 for (unsigned i = 0; i < Styles.size(); ++i) { 559 // Ensures that only the first configuration can skip the Language option. 560 if (Styles[i].Language == FormatStyle::LK_None && i != 0) 561 return make_error_code(ParseError::Error); 562 // Ensure that each language is configured at most once. 563 for (unsigned j = 0; j < i; ++j) { 564 if (Styles[i].Language == Styles[j].Language) { 565 DEBUG(llvm::dbgs() 566 << "Duplicate languages in the config file on positions " << j 567 << " and " << i << "\n"); 568 return make_error_code(ParseError::Error); 569 } 570 } 571 } 572 // Look for a suitable configuration starting from the end, so we can 573 // find the configuration for the specific language first, and the default 574 // configuration (which can only be at slot 0) after it. 575 for (int i = Styles.size() - 1; i >= 0; --i) { 576 if (Styles[i].Language == Language || 577 Styles[i].Language == FormatStyle::LK_None) { 578 *Style = Styles[i]; 579 Style->Language = Language; 580 return make_error_code(ParseError::Success); 581 } 582 } 583 return make_error_code(ParseError::Unsuitable); 584 } 585 586 std::string configurationAsText(const FormatStyle &Style) { 587 std::string Text; 588 llvm::raw_string_ostream Stream(Text); 589 llvm::yaml::Output Output(Stream); 590 // We use the same mapping method for input and output, so we need a non-const 591 // reference here. 592 FormatStyle NonConstStyle = Style; 593 Output << NonConstStyle; 594 return Stream.str(); 595 } 596 597 namespace { 598 599 class FormatTokenLexer { 600 public: 601 FormatTokenLexer(SourceManager &SourceMgr, FileID ID, FormatStyle &Style, 602 encoding::Encoding Encoding) 603 : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false), 604 LessStashed(false), Column(0), TrailingWhitespace(0), 605 SourceMgr(SourceMgr), ID(ID), Style(Style), 606 IdentTable(getFormattingLangOpts(Style)), Keywords(IdentTable), 607 Encoding(Encoding), FirstInLineIndex(0), FormattingDisabled(false) { 608 Lex.reset(new Lexer(ID, SourceMgr.getBuffer(ID), SourceMgr, 609 getFormattingLangOpts(Style))); 610 Lex->SetKeepWhitespaceMode(true); 611 612 for (const std::string &ForEachMacro : Style.ForEachMacros) 613 ForEachMacros.push_back(&IdentTable.get(ForEachMacro)); 614 std::sort(ForEachMacros.begin(), ForEachMacros.end()); 615 } 616 617 ArrayRef<FormatToken *> lex() { 618 assert(Tokens.empty()); 619 assert(FirstInLineIndex == 0); 620 do { 621 Tokens.push_back(getNextToken()); 622 tryMergePreviousTokens(); 623 if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline) 624 FirstInLineIndex = Tokens.size() - 1; 625 } while (Tokens.back()->Tok.isNot(tok::eof)); 626 return Tokens; 627 } 628 629 const AdditionalKeywords &getKeywords() { return Keywords; } 630 631 private: 632 void tryMergePreviousTokens() { 633 if (tryMerge_TMacro()) 634 return; 635 if (tryMergeConflictMarkers()) 636 return; 637 if (tryMergeLessLess()) 638 return; 639 640 if (Style.Language == FormatStyle::LK_JavaScript) { 641 if (tryMergeJSRegexLiteral()) 642 return; 643 if (tryMergeEscapeSequence()) 644 return; 645 if (tryMergeTemplateString()) 646 return; 647 648 static const tok::TokenKind JSIdentity[] = {tok::equalequal, tok::equal}; 649 static const tok::TokenKind JSNotIdentity[] = {tok::exclaimequal, 650 tok::equal}; 651 static const tok::TokenKind JSShiftEqual[] = {tok::greater, tok::greater, 652 tok::greaterequal}; 653 static const tok::TokenKind JSRightArrow[] = {tok::equal, tok::greater}; 654 // FIXME: Investigate what token type gives the correct operator priority. 655 if (tryMergeTokens(JSIdentity, TT_BinaryOperator)) 656 return; 657 if (tryMergeTokens(JSNotIdentity, TT_BinaryOperator)) 658 return; 659 if (tryMergeTokens(JSShiftEqual, TT_BinaryOperator)) 660 return; 661 if (tryMergeTokens(JSRightArrow, TT_JsFatArrow)) 662 return; 663 } 664 } 665 666 bool tryMergeLessLess() { 667 // Merge X,less,less,Y into X,lessless,Y unless X or Y is less. 668 if (Tokens.size() < 3) 669 return false; 670 671 bool FourthTokenIsLess = false; 672 if (Tokens.size() > 3) 673 FourthTokenIsLess = (Tokens.end() - 4)[0]->is(tok::less); 674 675 auto First = Tokens.end() - 3; 676 if (First[2]->is(tok::less) || First[1]->isNot(tok::less) || 677 First[0]->isNot(tok::less) || FourthTokenIsLess) 678 return false; 679 680 // Only merge if there currently is no whitespace between the two "<". 681 if (First[1]->WhitespaceRange.getBegin() != 682 First[1]->WhitespaceRange.getEnd()) 683 return false; 684 685 First[0]->Tok.setKind(tok::lessless); 686 First[0]->TokenText = "<<"; 687 First[0]->ColumnWidth += 1; 688 Tokens.erase(Tokens.end() - 2); 689 return true; 690 } 691 692 bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType) { 693 if (Tokens.size() < Kinds.size()) 694 return false; 695 696 SmallVectorImpl<FormatToken *>::const_iterator First = 697 Tokens.end() - Kinds.size(); 698 if (!First[0]->is(Kinds[0])) 699 return false; 700 unsigned AddLength = 0; 701 for (unsigned i = 1; i < Kinds.size(); ++i) { 702 if (!First[i]->is(Kinds[i]) || 703 First[i]->WhitespaceRange.getBegin() != 704 First[i]->WhitespaceRange.getEnd()) 705 return false; 706 AddLength += First[i]->TokenText.size(); 707 } 708 Tokens.resize(Tokens.size() - Kinds.size() + 1); 709 First[0]->TokenText = StringRef(First[0]->TokenText.data(), 710 First[0]->TokenText.size() + AddLength); 711 First[0]->ColumnWidth += AddLength; 712 First[0]->Type = NewType; 713 return true; 714 } 715 716 // Tries to merge an escape sequence, i.e. a "\\" and the following 717 // character. Use e.g. inside JavaScript regex literals. 718 bool tryMergeEscapeSequence() { 719 if (Tokens.size() < 2) 720 return false; 721 FormatToken *Previous = Tokens[Tokens.size() - 2]; 722 if (Previous->isNot(tok::unknown) || Previous->TokenText != "\\") 723 return false; 724 ++Previous->ColumnWidth; 725 StringRef Text = Previous->TokenText; 726 Previous->TokenText = StringRef(Text.data(), Text.size() + 1); 727 resetLexer(SourceMgr.getFileOffset(Tokens.back()->Tok.getLocation()) + 1); 728 Tokens.resize(Tokens.size() - 1); 729 Column = Previous->OriginalColumn + Previous->ColumnWidth; 730 return true; 731 } 732 733 // Try to determine whether the current token ends a JavaScript regex literal. 734 // We heuristically assume that this is a regex literal if we find two 735 // unescaped slashes on a line and the token before the first slash is one of 736 // "(;,{}![:?", a binary operator or 'return', as those cannot be followed by 737 // a division. 738 bool tryMergeJSRegexLiteral() { 739 if (Tokens.size() < 2) 740 return false; 741 // If a regex literal ends in "\//", this gets represented by an unknown 742 // token "\" and a comment. 743 bool MightEndWithEscapedSlash = 744 Tokens.back()->is(tok::comment) && 745 Tokens.back()->TokenText.startswith("//") && 746 Tokens[Tokens.size() - 2]->TokenText == "\\"; 747 if (!MightEndWithEscapedSlash && 748 (Tokens.back()->isNot(tok::slash) || 749 (Tokens[Tokens.size() - 2]->is(tok::unknown) && 750 Tokens[Tokens.size() - 2]->TokenText == "\\"))) 751 return false; 752 unsigned TokenCount = 0; 753 unsigned LastColumn = Tokens.back()->OriginalColumn; 754 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) { 755 ++TokenCount; 756 if (I[0]->isOneOf(tok::slash, tok::slashequal) && I + 1 != E && 757 (I[1]->isOneOf(tok::l_paren, tok::semi, tok::l_brace, tok::r_brace, 758 tok::exclaim, tok::l_square, tok::colon, tok::comma, 759 tok::question, tok::kw_return) || 760 I[1]->isBinaryOperator())) { 761 if (MightEndWithEscapedSlash) { 762 // This regex literal ends in '\//'. Skip past the '//' of the last 763 // token and re-start lexing from there. 764 SourceLocation Loc = Tokens.back()->Tok.getLocation(); 765 resetLexer(SourceMgr.getFileOffset(Loc) + 2); 766 } 767 Tokens.resize(Tokens.size() - TokenCount); 768 Tokens.back()->Tok.setKind(tok::unknown); 769 Tokens.back()->Type = TT_RegexLiteral; 770 Tokens.back()->ColumnWidth += LastColumn - I[0]->OriginalColumn; 771 return true; 772 } 773 774 // There can't be a newline inside a regex literal. 775 if (I[0]->NewlinesBefore > 0) 776 return false; 777 } 778 return false; 779 } 780 781 bool tryMergeTemplateString() { 782 if (Tokens.size() < 2) 783 return false; 784 785 FormatToken *EndBacktick = Tokens.back(); 786 // Backticks get lexed as tok::unknown tokens. If a template string contains 787 // a comment start, it gets lexed as a tok::comment, or tok::unknown if 788 // unterminated. 789 if (!EndBacktick->isOneOf(tok::comment, tok::string_literal, 790 tok::char_constant, tok::unknown)) 791 return false; 792 size_t CommentBacktickPos = EndBacktick->TokenText.find('`'); 793 // Unknown token that's not actually a backtick, or a comment that doesn't 794 // contain a backtick. 795 if (CommentBacktickPos == StringRef::npos) 796 return false; 797 798 unsigned TokenCount = 0; 799 bool IsMultiline = false; 800 unsigned EndColumnInFirstLine = 801 EndBacktick->OriginalColumn + EndBacktick->ColumnWidth; 802 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; I++) { 803 ++TokenCount; 804 if (I[0]->NewlinesBefore > 0 || I[0]->IsMultiline) 805 IsMultiline = true; 806 807 // If there was a preceding template string, this must be the start of a 808 // template string, not the end. 809 if (I[0]->is(TT_TemplateString)) 810 return false; 811 812 if (I[0]->isNot(tok::unknown) || I[0]->TokenText != "`") { 813 // Keep track of the rhs offset of the last token to wrap across lines - 814 // its the rhs offset of the first line of the template string, used to 815 // determine its width. 816 if (I[0]->IsMultiline) 817 EndColumnInFirstLine = I[0]->OriginalColumn + I[0]->ColumnWidth; 818 // If the token has newlines, the token before it (if it exists) is the 819 // rhs end of the previous line. 820 if (I[0]->NewlinesBefore > 0 && (I + 1 != E)) 821 EndColumnInFirstLine = I[1]->OriginalColumn + I[1]->ColumnWidth; 822 823 continue; 824 } 825 826 Tokens.resize(Tokens.size() - TokenCount); 827 Tokens.back()->Type = TT_TemplateString; 828 const char *EndOffset = 829 EndBacktick->TokenText.data() + 1 + CommentBacktickPos; 830 if (CommentBacktickPos != 0) { 831 // If the backtick was not the first character (e.g. in a comment), 832 // re-lex after the backtick position. 833 SourceLocation Loc = EndBacktick->Tok.getLocation(); 834 resetLexer(SourceMgr.getFileOffset(Loc) + CommentBacktickPos + 1); 835 } 836 Tokens.back()->TokenText = 837 StringRef(Tokens.back()->TokenText.data(), 838 EndOffset - Tokens.back()->TokenText.data()); 839 840 unsigned EndOriginalColumn = EndBacktick->OriginalColumn; 841 if (EndOriginalColumn == 0) { 842 SourceLocation Loc = EndBacktick->Tok.getLocation(); 843 EndOriginalColumn = SourceMgr.getSpellingColumnNumber(Loc); 844 } 845 // If the ` is further down within the token (e.g. in a comment). 846 EndOriginalColumn += CommentBacktickPos; 847 848 if (IsMultiline) { 849 // ColumnWidth is from backtick to last token in line. 850 // LastLineColumnWidth is 0 to backtick. 851 // x = `some content 852 // until here`; 853 Tokens.back()->ColumnWidth = 854 EndColumnInFirstLine - Tokens.back()->OriginalColumn; 855 Tokens.back()->LastLineColumnWidth = EndOriginalColumn; 856 Tokens.back()->IsMultiline = true; 857 } else { 858 // Token simply spans from start to end, +1 for the ` itself. 859 Tokens.back()->ColumnWidth = 860 EndOriginalColumn - Tokens.back()->OriginalColumn + 1; 861 } 862 return true; 863 } 864 return false; 865 } 866 867 bool tryMerge_TMacro() { 868 if (Tokens.size() < 4) 869 return false; 870 FormatToken *Last = Tokens.back(); 871 if (!Last->is(tok::r_paren)) 872 return false; 873 874 FormatToken *String = Tokens[Tokens.size() - 2]; 875 if (!String->is(tok::string_literal) || String->IsMultiline) 876 return false; 877 878 if (!Tokens[Tokens.size() - 3]->is(tok::l_paren)) 879 return false; 880 881 FormatToken *Macro = Tokens[Tokens.size() - 4]; 882 if (Macro->TokenText != "_T") 883 return false; 884 885 const char *Start = Macro->TokenText.data(); 886 const char *End = Last->TokenText.data() + Last->TokenText.size(); 887 String->TokenText = StringRef(Start, End - Start); 888 String->IsFirst = Macro->IsFirst; 889 String->LastNewlineOffset = Macro->LastNewlineOffset; 890 String->WhitespaceRange = Macro->WhitespaceRange; 891 String->OriginalColumn = Macro->OriginalColumn; 892 String->ColumnWidth = encoding::columnWidthWithTabs( 893 String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding); 894 String->NewlinesBefore = Macro->NewlinesBefore; 895 String->HasUnescapedNewline = Macro->HasUnescapedNewline; 896 897 Tokens.pop_back(); 898 Tokens.pop_back(); 899 Tokens.pop_back(); 900 Tokens.back() = String; 901 return true; 902 } 903 904 bool tryMergeConflictMarkers() { 905 if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof)) 906 return false; 907 908 // Conflict lines look like: 909 // <marker> <text from the vcs> 910 // For example: 911 // >>>>>>> /file/in/file/system at revision 1234 912 // 913 // We merge all tokens in a line that starts with a conflict marker 914 // into a single token with a special token type that the unwrapped line 915 // parser will use to correctly rebuild the underlying code. 916 917 FileID ID; 918 // Get the position of the first token in the line. 919 unsigned FirstInLineOffset; 920 std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc( 921 Tokens[FirstInLineIndex]->getStartOfNonWhitespace()); 922 StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer(); 923 // Calculate the offset of the start of the current line. 924 auto LineOffset = Buffer.rfind('\n', FirstInLineOffset); 925 if (LineOffset == StringRef::npos) { 926 LineOffset = 0; 927 } else { 928 ++LineOffset; 929 } 930 931 auto FirstSpace = Buffer.find_first_of(" \n", LineOffset); 932 StringRef LineStart; 933 if (FirstSpace == StringRef::npos) { 934 LineStart = Buffer.substr(LineOffset); 935 } else { 936 LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset); 937 } 938 939 TokenType Type = TT_Unknown; 940 if (LineStart == "<<<<<<<" || LineStart == ">>>>") { 941 Type = TT_ConflictStart; 942 } else if (LineStart == "|||||||" || LineStart == "=======" || 943 LineStart == "====") { 944 Type = TT_ConflictAlternative; 945 } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") { 946 Type = TT_ConflictEnd; 947 } 948 949 if (Type != TT_Unknown) { 950 FormatToken *Next = Tokens.back(); 951 952 Tokens.resize(FirstInLineIndex + 1); 953 // We do not need to build a complete token here, as we will skip it 954 // during parsing anyway (as we must not touch whitespace around conflict 955 // markers). 956 Tokens.back()->Type = Type; 957 Tokens.back()->Tok.setKind(tok::kw___unknown_anytype); 958 959 Tokens.push_back(Next); 960 return true; 961 } 962 963 return false; 964 } 965 966 FormatToken *getStashedToken() { 967 // Create a synthesized second '>' or '<' token. 968 Token Tok = FormatTok->Tok; 969 StringRef TokenText = FormatTok->TokenText; 970 971 unsigned OriginalColumn = FormatTok->OriginalColumn; 972 FormatTok = new (Allocator.Allocate()) FormatToken; 973 FormatTok->Tok = Tok; 974 SourceLocation TokLocation = 975 FormatTok->Tok.getLocation().getLocWithOffset(Tok.getLength() - 1); 976 FormatTok->Tok.setLocation(TokLocation); 977 FormatTok->WhitespaceRange = SourceRange(TokLocation, TokLocation); 978 FormatTok->TokenText = TokenText; 979 FormatTok->ColumnWidth = 1; 980 FormatTok->OriginalColumn = OriginalColumn + 1; 981 982 return FormatTok; 983 } 984 985 FormatToken *getNextToken() { 986 if (GreaterStashed) { 987 GreaterStashed = false; 988 return getStashedToken(); 989 } 990 if (LessStashed) { 991 LessStashed = false; 992 return getStashedToken(); 993 } 994 995 FormatTok = new (Allocator.Allocate()) FormatToken; 996 readRawToken(*FormatTok); 997 SourceLocation WhitespaceStart = 998 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace); 999 FormatTok->IsFirst = IsFirstToken; 1000 IsFirstToken = false; 1001 1002 // Consume and record whitespace until we find a significant token. 1003 unsigned WhitespaceLength = TrailingWhitespace; 1004 while (FormatTok->Tok.is(tok::unknown)) { 1005 StringRef Text = FormatTok->TokenText; 1006 auto EscapesNewline = [&](int pos) { 1007 // A '\r' here is just part of '\r\n'. Skip it. 1008 if (pos >= 0 && Text[pos] == '\r') 1009 --pos; 1010 // See whether there is an odd number of '\' before this. 1011 unsigned count = 0; 1012 for (; pos >= 0; --pos, ++count) 1013 if (Text[pos] != '\\') 1014 break; 1015 return count & 1; 1016 }; 1017 // FIXME: This miscounts tok:unknown tokens that are not just 1018 // whitespace, e.g. a '`' character. 1019 for (int i = 0, e = Text.size(); i != e; ++i) { 1020 switch (Text[i]) { 1021 case '\n': 1022 ++FormatTok->NewlinesBefore; 1023 FormatTok->HasUnescapedNewline = !EscapesNewline(i - 1); 1024 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1; 1025 Column = 0; 1026 break; 1027 case '\r': 1028 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1; 1029 Column = 0; 1030 break; 1031 case '\f': 1032 case '\v': 1033 Column = 0; 1034 break; 1035 case ' ': 1036 ++Column; 1037 break; 1038 case '\t': 1039 Column += Style.TabWidth - Column % Style.TabWidth; 1040 break; 1041 case '\\': 1042 if (i + 1 == e || (Text[i + 1] != '\r' && Text[i + 1] != '\n')) 1043 FormatTok->Type = TT_ImplicitStringLiteral; 1044 break; 1045 default: 1046 FormatTok->Type = TT_ImplicitStringLiteral; 1047 ++Column; 1048 break; 1049 } 1050 } 1051 1052 if (FormatTok->is(TT_ImplicitStringLiteral)) 1053 break; 1054 WhitespaceLength += FormatTok->Tok.getLength(); 1055 1056 readRawToken(*FormatTok); 1057 } 1058 1059 // In case the token starts with escaped newlines, we want to 1060 // take them into account as whitespace - this pattern is quite frequent 1061 // in macro definitions. 1062 // FIXME: Add a more explicit test. 1063 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' && 1064 FormatTok->TokenText[1] == '\n') { 1065 ++FormatTok->NewlinesBefore; 1066 WhitespaceLength += 2; 1067 FormatTok->LastNewlineOffset = 2; 1068 Column = 0; 1069 FormatTok->TokenText = FormatTok->TokenText.substr(2); 1070 } 1071 1072 FormatTok->WhitespaceRange = SourceRange( 1073 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength)); 1074 1075 FormatTok->OriginalColumn = Column; 1076 1077 TrailingWhitespace = 0; 1078 if (FormatTok->Tok.is(tok::comment)) { 1079 // FIXME: Add the trimmed whitespace to Column. 1080 StringRef UntrimmedText = FormatTok->TokenText; 1081 FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f"); 1082 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size(); 1083 } else if (FormatTok->Tok.is(tok::raw_identifier)) { 1084 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText); 1085 FormatTok->Tok.setIdentifierInfo(&Info); 1086 FormatTok->Tok.setKind(Info.getTokenID()); 1087 if (Style.Language == FormatStyle::LK_Java && 1088 FormatTok->isOneOf(tok::kw_struct, tok::kw_union, tok::kw_delete)) { 1089 FormatTok->Tok.setKind(tok::identifier); 1090 FormatTok->Tok.setIdentifierInfo(nullptr); 1091 } 1092 } else if (FormatTok->Tok.is(tok::greatergreater)) { 1093 FormatTok->Tok.setKind(tok::greater); 1094 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1); 1095 GreaterStashed = true; 1096 } else if (FormatTok->Tok.is(tok::lessless)) { 1097 FormatTok->Tok.setKind(tok::less); 1098 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1); 1099 LessStashed = true; 1100 } 1101 1102 // Now FormatTok is the next non-whitespace token. 1103 1104 StringRef Text = FormatTok->TokenText; 1105 size_t FirstNewlinePos = Text.find('\n'); 1106 if (FirstNewlinePos == StringRef::npos) { 1107 // FIXME: ColumnWidth actually depends on the start column, we need to 1108 // take this into account when the token is moved. 1109 FormatTok->ColumnWidth = 1110 encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding); 1111 Column += FormatTok->ColumnWidth; 1112 } else { 1113 FormatTok->IsMultiline = true; 1114 // FIXME: ColumnWidth actually depends on the start column, we need to 1115 // take this into account when the token is moved. 1116 FormatTok->ColumnWidth = encoding::columnWidthWithTabs( 1117 Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding); 1118 1119 // The last line of the token always starts in column 0. 1120 // Thus, the length can be precomputed even in the presence of tabs. 1121 FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs( 1122 Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth, 1123 Encoding); 1124 Column = FormatTok->LastLineColumnWidth; 1125 } 1126 1127 if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() && 1128 Tokens.back()->Tok.getIdentifierInfo()->getPPKeywordID() == 1129 tok::pp_define) && 1130 std::find(ForEachMacros.begin(), ForEachMacros.end(), 1131 FormatTok->Tok.getIdentifierInfo()) != ForEachMacros.end()) 1132 FormatTok->Type = TT_ForEachMacro; 1133 1134 return FormatTok; 1135 } 1136 1137 FormatToken *FormatTok; 1138 bool IsFirstToken; 1139 bool GreaterStashed, LessStashed; 1140 unsigned Column; 1141 unsigned TrailingWhitespace; 1142 std::unique_ptr<Lexer> Lex; 1143 SourceManager &SourceMgr; 1144 FileID ID; 1145 FormatStyle &Style; 1146 IdentifierTable IdentTable; 1147 AdditionalKeywords Keywords; 1148 encoding::Encoding Encoding; 1149 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; 1150 // Index (in 'Tokens') of the last token that starts a new line. 1151 unsigned FirstInLineIndex; 1152 SmallVector<FormatToken *, 16> Tokens; 1153 SmallVector<IdentifierInfo *, 8> ForEachMacros; 1154 1155 bool FormattingDisabled; 1156 1157 void readRawToken(FormatToken &Tok) { 1158 Lex->LexFromRawLexer(Tok.Tok); 1159 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()), 1160 Tok.Tok.getLength()); 1161 // For formatting, treat unterminated string literals like normal string 1162 // literals. 1163 if (Tok.is(tok::unknown)) { 1164 if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') { 1165 Tok.Tok.setKind(tok::string_literal); 1166 Tok.IsUnterminatedLiteral = true; 1167 } else if (Style.Language == FormatStyle::LK_JavaScript && 1168 Tok.TokenText == "''") { 1169 Tok.Tok.setKind(tok::char_constant); 1170 } 1171 } 1172 1173 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" || 1174 Tok.TokenText == "/* clang-format on */")) { 1175 FormattingDisabled = false; 1176 } 1177 1178 Tok.Finalized = FormattingDisabled; 1179 1180 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" || 1181 Tok.TokenText == "/* clang-format off */")) { 1182 FormattingDisabled = true; 1183 } 1184 } 1185 1186 void resetLexer(unsigned Offset) { 1187 StringRef Buffer = SourceMgr.getBufferData(ID); 1188 Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), 1189 getFormattingLangOpts(Style), Buffer.begin(), 1190 Buffer.begin() + Offset, Buffer.end())); 1191 Lex->SetKeepWhitespaceMode(true); 1192 } 1193 }; 1194 1195 static StringRef getLanguageName(FormatStyle::LanguageKind Language) { 1196 switch (Language) { 1197 case FormatStyle::LK_Cpp: 1198 return "C++"; 1199 case FormatStyle::LK_Java: 1200 return "Java"; 1201 case FormatStyle::LK_JavaScript: 1202 return "JavaScript"; 1203 case FormatStyle::LK_Proto: 1204 return "Proto"; 1205 default: 1206 return "Unknown"; 1207 } 1208 } 1209 1210 class Formatter : public UnwrappedLineConsumer { 1211 public: 1212 Formatter(const FormatStyle &Style, SourceManager &SourceMgr, FileID ID, 1213 ArrayRef<CharSourceRange> Ranges) 1214 : Style(Style), ID(ID), SourceMgr(SourceMgr), 1215 Whitespaces(SourceMgr, Style, 1216 inputUsesCRLF(SourceMgr.getBufferData(ID))), 1217 Ranges(Ranges.begin(), Ranges.end()), UnwrappedLines(1), 1218 Encoding(encoding::detectEncoding(SourceMgr.getBufferData(ID))) { 1219 DEBUG(llvm::dbgs() << "File encoding: " 1220 << (Encoding == encoding::Encoding_UTF8 ? "UTF8" 1221 : "unknown") 1222 << "\n"); 1223 DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language) 1224 << "\n"); 1225 } 1226 1227 tooling::Replacements format(bool *IncompleteFormat) { 1228 tooling::Replacements Result; 1229 FormatTokenLexer Tokens(SourceMgr, ID, Style, Encoding); 1230 1231 UnwrappedLineParser Parser(Style, Tokens.getKeywords(), Tokens.lex(), 1232 *this); 1233 Parser.parse(); 1234 assert(UnwrappedLines.rbegin()->empty()); 1235 for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE; 1236 ++Run) { 1237 DEBUG(llvm::dbgs() << "Run " << Run << "...\n"); 1238 SmallVector<AnnotatedLine *, 16> AnnotatedLines; 1239 for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) { 1240 AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i])); 1241 } 1242 tooling::Replacements RunResult = 1243 format(AnnotatedLines, Tokens, IncompleteFormat); 1244 DEBUG({ 1245 llvm::dbgs() << "Replacements for run " << Run << ":\n"; 1246 for (tooling::Replacements::iterator I = RunResult.begin(), 1247 E = RunResult.end(); 1248 I != E; ++I) { 1249 llvm::dbgs() << I->toString() << "\n"; 1250 } 1251 }); 1252 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 1253 delete AnnotatedLines[i]; 1254 } 1255 Result.insert(RunResult.begin(), RunResult.end()); 1256 Whitespaces.reset(); 1257 } 1258 return Result; 1259 } 1260 1261 tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, 1262 FormatTokenLexer &Tokens, 1263 bool *IncompleteFormat) { 1264 TokenAnnotator Annotator(Style, Tokens.getKeywords()); 1265 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 1266 Annotator.annotate(*AnnotatedLines[i]); 1267 } 1268 deriveLocalStyle(AnnotatedLines); 1269 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 1270 Annotator.calculateFormattingInformation(*AnnotatedLines[i]); 1271 } 1272 computeAffectedLines(AnnotatedLines.begin(), AnnotatedLines.end()); 1273 1274 Annotator.setCommentLineLevels(AnnotatedLines); 1275 ContinuationIndenter Indenter(Style, Tokens.getKeywords(), SourceMgr, 1276 Whitespaces, Encoding, 1277 BinPackInconclusiveFunctions); 1278 UnwrappedLineFormatter(&Indenter, &Whitespaces, Style, Tokens.getKeywords(), 1279 IncompleteFormat) 1280 .format(AnnotatedLines); 1281 return Whitespaces.generateReplacements(); 1282 } 1283 1284 private: 1285 // Determines which lines are affected by the SourceRanges given as input. 1286 // Returns \c true if at least one line between I and E or one of their 1287 // children is affected. 1288 bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I, 1289 SmallVectorImpl<AnnotatedLine *>::iterator E) { 1290 bool SomeLineAffected = false; 1291 const AnnotatedLine *PreviousLine = nullptr; 1292 while (I != E) { 1293 AnnotatedLine *Line = *I; 1294 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First); 1295 1296 // If a line is part of a preprocessor directive, it needs to be formatted 1297 // if any token within the directive is affected. 1298 if (Line->InPPDirective) { 1299 FormatToken *Last = Line->Last; 1300 SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1; 1301 while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) { 1302 Last = (*PPEnd)->Last; 1303 ++PPEnd; 1304 } 1305 1306 if (affectsTokenRange(*Line->First, *Last, 1307 /*IncludeLeadingNewlines=*/false)) { 1308 SomeLineAffected = true; 1309 markAllAsAffected(I, PPEnd); 1310 } 1311 I = PPEnd; 1312 continue; 1313 } 1314 1315 if (nonPPLineAffected(Line, PreviousLine)) 1316 SomeLineAffected = true; 1317 1318 PreviousLine = Line; 1319 ++I; 1320 } 1321 return SomeLineAffected; 1322 } 1323 1324 // Determines whether 'Line' is affected by the SourceRanges given as input. 1325 // Returns \c true if line or one if its children is affected. 1326 bool nonPPLineAffected(AnnotatedLine *Line, 1327 const AnnotatedLine *PreviousLine) { 1328 bool SomeLineAffected = false; 1329 Line->ChildrenAffected = 1330 computeAffectedLines(Line->Children.begin(), Line->Children.end()); 1331 if (Line->ChildrenAffected) 1332 SomeLineAffected = true; 1333 1334 // Stores whether one of the line's tokens is directly affected. 1335 bool SomeTokenAffected = false; 1336 // Stores whether we need to look at the leading newlines of the next token 1337 // in order to determine whether it was affected. 1338 bool IncludeLeadingNewlines = false; 1339 1340 // Stores whether the first child line of any of this line's tokens is 1341 // affected. 1342 bool SomeFirstChildAffected = false; 1343 1344 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) { 1345 // Determine whether 'Tok' was affected. 1346 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines)) 1347 SomeTokenAffected = true; 1348 1349 // Determine whether the first child of 'Tok' was affected. 1350 if (!Tok->Children.empty() && Tok->Children.front()->Affected) 1351 SomeFirstChildAffected = true; 1352 1353 IncludeLeadingNewlines = Tok->Children.empty(); 1354 } 1355 1356 // Was this line moved, i.e. has it previously been on the same line as an 1357 // affected line? 1358 bool LineMoved = PreviousLine && PreviousLine->Affected && 1359 Line->First->NewlinesBefore == 0; 1360 1361 bool IsContinuedComment = 1362 Line->First->is(tok::comment) && Line->First->Next == nullptr && 1363 Line->First->NewlinesBefore < 2 && PreviousLine && 1364 PreviousLine->Affected && PreviousLine->Last->is(tok::comment); 1365 1366 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved || 1367 IsContinuedComment) { 1368 Line->Affected = true; 1369 SomeLineAffected = true; 1370 } 1371 return SomeLineAffected; 1372 } 1373 1374 // Marks all lines between I and E as well as all their children as affected. 1375 void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I, 1376 SmallVectorImpl<AnnotatedLine *>::iterator E) { 1377 while (I != E) { 1378 (*I)->Affected = true; 1379 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end()); 1380 ++I; 1381 } 1382 } 1383 1384 // Returns true if the range from 'First' to 'Last' intersects with one of the 1385 // input ranges. 1386 bool affectsTokenRange(const FormatToken &First, const FormatToken &Last, 1387 bool IncludeLeadingNewlines) { 1388 SourceLocation Start = First.WhitespaceRange.getBegin(); 1389 if (!IncludeLeadingNewlines) 1390 Start = Start.getLocWithOffset(First.LastNewlineOffset); 1391 SourceLocation End = Last.getStartOfNonWhitespace(); 1392 End = End.getLocWithOffset(Last.TokenText.size()); 1393 CharSourceRange Range = CharSourceRange::getCharRange(Start, End); 1394 return affectsCharSourceRange(Range); 1395 } 1396 1397 // Returns true if one of the input ranges intersect the leading empty lines 1398 // before 'Tok'. 1399 bool affectsLeadingEmptyLines(const FormatToken &Tok) { 1400 CharSourceRange EmptyLineRange = CharSourceRange::getCharRange( 1401 Tok.WhitespaceRange.getBegin(), 1402 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset)); 1403 return affectsCharSourceRange(EmptyLineRange); 1404 } 1405 1406 // Returns true if 'Range' intersects with one of the input ranges. 1407 bool affectsCharSourceRange(const CharSourceRange &Range) { 1408 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(), 1409 E = Ranges.end(); 1410 I != E; ++I) { 1411 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) && 1412 !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin())) 1413 return true; 1414 } 1415 return false; 1416 } 1417 1418 static bool inputUsesCRLF(StringRef Text) { 1419 return Text.count('\r') * 2 > Text.count('\n'); 1420 } 1421 1422 void 1423 deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { 1424 unsigned CountBoundToVariable = 0; 1425 unsigned CountBoundToType = 0; 1426 bool HasCpp03IncompatibleFormat = false; 1427 bool HasBinPackedFunction = false; 1428 bool HasOnePerLineFunction = false; 1429 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 1430 if (!AnnotatedLines[i]->First->Next) 1431 continue; 1432 FormatToken *Tok = AnnotatedLines[i]->First->Next; 1433 while (Tok->Next) { 1434 if (Tok->is(TT_PointerOrReference)) { 1435 bool SpacesBefore = 1436 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd(); 1437 bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() != 1438 Tok->Next->WhitespaceRange.getEnd(); 1439 if (SpacesBefore && !SpacesAfter) 1440 ++CountBoundToVariable; 1441 else if (!SpacesBefore && SpacesAfter) 1442 ++CountBoundToType; 1443 } 1444 1445 if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) { 1446 if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener)) 1447 HasCpp03IncompatibleFormat = true; 1448 if (Tok->is(TT_TemplateCloser) && 1449 Tok->Previous->is(TT_TemplateCloser)) 1450 HasCpp03IncompatibleFormat = true; 1451 } 1452 1453 if (Tok->PackingKind == PPK_BinPacked) 1454 HasBinPackedFunction = true; 1455 if (Tok->PackingKind == PPK_OnePerLine) 1456 HasOnePerLineFunction = true; 1457 1458 Tok = Tok->Next; 1459 } 1460 } 1461 if (Style.DerivePointerAlignment) { 1462 if (CountBoundToType > CountBoundToVariable) 1463 Style.PointerAlignment = FormatStyle::PAS_Left; 1464 else if (CountBoundToType < CountBoundToVariable) 1465 Style.PointerAlignment = FormatStyle::PAS_Right; 1466 } 1467 if (Style.Standard == FormatStyle::LS_Auto) { 1468 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 1469 : FormatStyle::LS_Cpp03; 1470 } 1471 BinPackInconclusiveFunctions = 1472 HasBinPackedFunction || !HasOnePerLineFunction; 1473 } 1474 1475 void consumeUnwrappedLine(const UnwrappedLine &TheLine) override { 1476 assert(!UnwrappedLines.empty()); 1477 UnwrappedLines.back().push_back(TheLine); 1478 } 1479 1480 void finishRun() override { 1481 UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>()); 1482 } 1483 1484 FormatStyle Style; 1485 FileID ID; 1486 SourceManager &SourceMgr; 1487 WhitespaceManager Whitespaces; 1488 SmallVector<CharSourceRange, 8> Ranges; 1489 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines; 1490 1491 encoding::Encoding Encoding; 1492 bool BinPackInconclusiveFunctions; 1493 }; 1494 1495 } // end anonymous namespace 1496 1497 tooling::Replacements reformat(const FormatStyle &Style, 1498 SourceManager &SourceMgr, FileID ID, 1499 ArrayRef<CharSourceRange> Ranges, 1500 bool *IncompleteFormat) { 1501 if (Style.DisableFormat) 1502 return tooling::Replacements(); 1503 Formatter formatter(Style, SourceMgr, ID, Ranges); 1504 return formatter.format(IncompleteFormat); 1505 } 1506 1507 tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, 1508 ArrayRef<tooling::Range> Ranges, 1509 StringRef FileName, bool *IncompleteFormat) { 1510 if (Style.DisableFormat) 1511 return tooling::Replacements(); 1512 1513 FileManager Files((FileSystemOptions())); 1514 DiagnosticsEngine Diagnostics( 1515 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), 1516 new DiagnosticOptions); 1517 SourceManager SourceMgr(Diagnostics, Files); 1518 std::unique_ptr<llvm::MemoryBuffer> Buf = 1519 llvm::MemoryBuffer::getMemBuffer(Code, FileName); 1520 const clang::FileEntry *Entry = 1521 Files.getVirtualFile(FileName, Buf->getBufferSize(), 0); 1522 SourceMgr.overrideFileContents(Entry, std::move(Buf)); 1523 FileID ID = 1524 SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); 1525 SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID); 1526 std::vector<CharSourceRange> CharRanges; 1527 for (const tooling::Range &Range : Ranges) { 1528 SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset()); 1529 SourceLocation End = Start.getLocWithOffset(Range.getLength()); 1530 CharRanges.push_back(CharSourceRange::getCharRange(Start, End)); 1531 } 1532 return reformat(Style, SourceMgr, ID, CharRanges, IncompleteFormat); 1533 } 1534 1535 LangOptions getFormattingLangOpts(const FormatStyle &Style) { 1536 LangOptions LangOpts; 1537 LangOpts.CPlusPlus = 1; 1538 LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; 1539 LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; 1540 LangOpts.LineComment = 1; 1541 bool AlternativeOperators = Style.Language == FormatStyle::LK_Cpp; 1542 LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0; 1543 LangOpts.Bool = 1; 1544 LangOpts.ObjC1 = 1; 1545 LangOpts.ObjC2 = 1; 1546 LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally. 1547 return LangOpts; 1548 } 1549 1550 const char *StyleOptionHelpDescription = 1551 "Coding style, currently supports:\n" 1552 " LLVM, Google, Chromium, Mozilla, WebKit.\n" 1553 "Use -style=file to load style configuration from\n" 1554 ".clang-format file located in one of the parent\n" 1555 "directories of the source file (or current\n" 1556 "directory for stdin).\n" 1557 "Use -style=\"{key: value, ...}\" to set specific\n" 1558 "parameters, e.g.:\n" 1559 " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""; 1560 1561 static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) { 1562 if (FileName.endswith(".java")) { 1563 return FormatStyle::LK_Java; 1564 } else if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts")) { 1565 // JavaScript or TypeScript. 1566 return FormatStyle::LK_JavaScript; 1567 } else if (FileName.endswith_lower(".proto") || 1568 FileName.endswith_lower(".protodevel")) { 1569 return FormatStyle::LK_Proto; 1570 } 1571 return FormatStyle::LK_Cpp; 1572 } 1573 1574 FormatStyle getStyle(StringRef StyleName, StringRef FileName, 1575 StringRef FallbackStyle) { 1576 FormatStyle Style = getLLVMStyle(); 1577 Style.Language = getLanguageByFileName(FileName); 1578 if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) { 1579 llvm::errs() << "Invalid fallback style \"" << FallbackStyle 1580 << "\" using LLVM style\n"; 1581 return Style; 1582 } 1583 1584 if (StyleName.startswith("{")) { 1585 // Parse YAML/JSON style from the command line. 1586 if (std::error_code ec = parseConfiguration(StyleName, &Style)) { 1587 llvm::errs() << "Error parsing -style: " << ec.message() << ", using " 1588 << FallbackStyle << " style\n"; 1589 } 1590 return Style; 1591 } 1592 1593 if (!StyleName.equals_lower("file")) { 1594 if (!getPredefinedStyle(StyleName, Style.Language, &Style)) 1595 llvm::errs() << "Invalid value for -style, using " << FallbackStyle 1596 << " style\n"; 1597 return Style; 1598 } 1599 1600 // Look for .clang-format/_clang-format file in the file's parent directories. 1601 SmallString<128> UnsuitableConfigFiles; 1602 SmallString<128> Path(FileName); 1603 llvm::sys::fs::make_absolute(Path); 1604 for (StringRef Directory = Path; !Directory.empty(); 1605 Directory = llvm::sys::path::parent_path(Directory)) { 1606 if (!llvm::sys::fs::is_directory(Directory)) 1607 continue; 1608 SmallString<128> ConfigFile(Directory); 1609 1610 llvm::sys::path::append(ConfigFile, ".clang-format"); 1611 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); 1612 bool IsFile = false; 1613 // Ignore errors from is_regular_file: we only need to know if we can read 1614 // the file or not. 1615 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); 1616 1617 if (!IsFile) { 1618 // Try _clang-format too, since dotfiles are not commonly used on Windows. 1619 ConfigFile = Directory; 1620 llvm::sys::path::append(ConfigFile, "_clang-format"); 1621 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); 1622 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); 1623 } 1624 1625 if (IsFile) { 1626 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = 1627 llvm::MemoryBuffer::getFile(ConfigFile.c_str()); 1628 if (std::error_code EC = Text.getError()) { 1629 llvm::errs() << EC.message() << "\n"; 1630 break; 1631 } 1632 if (std::error_code ec = 1633 parseConfiguration(Text.get()->getBuffer(), &Style)) { 1634 if (ec == ParseError::Unsuitable) { 1635 if (!UnsuitableConfigFiles.empty()) 1636 UnsuitableConfigFiles.append(", "); 1637 UnsuitableConfigFiles.append(ConfigFile); 1638 continue; 1639 } 1640 llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message() 1641 << "\n"; 1642 break; 1643 } 1644 DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n"); 1645 return Style; 1646 } 1647 } 1648 if (!UnsuitableConfigFiles.empty()) { 1649 llvm::errs() << "Configuration file(s) do(es) not support " 1650 << getLanguageName(Style.Language) << ": " 1651 << UnsuitableConfigFiles << "\n"; 1652 } 1653 return Style; 1654 } 1655 1656 } // namespace format 1657 } // namespace clang 1658