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 "clang/Format/Format.h" 17 #include "AffectedRangeManager.h" 18 #include "ContinuationIndenter.h" 19 #include "FormatTokenLexer.h" 20 #include "NamespaceEndCommentsFixer.h" 21 #include "SortJavaScriptImports.h" 22 #include "TokenAnalyzer.h" 23 #include "TokenAnnotator.h" 24 #include "UnwrappedLineFormatter.h" 25 #include "UnwrappedLineParser.h" 26 #include "UsingDeclarationsSorter.h" 27 #include "WhitespaceManager.h" 28 #include "clang/Basic/Diagnostic.h" 29 #include "clang/Basic/DiagnosticOptions.h" 30 #include "clang/Basic/SourceManager.h" 31 #include "clang/Basic/VirtualFileSystem.h" 32 #include "clang/Lex/Lexer.h" 33 #include "llvm/ADT/STLExtras.h" 34 #include "llvm/Support/Allocator.h" 35 #include "llvm/Support/Debug.h" 36 #include "llvm/Support/Path.h" 37 #include "llvm/Support/Regex.h" 38 #include "llvm/Support/YAMLTraits.h" 39 #include <algorithm> 40 #include <memory> 41 #include <string> 42 43 #define DEBUG_TYPE "format-formatter" 44 45 using clang::format::FormatStyle; 46 47 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string) 48 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::format::FormatStyle::IncludeCategory) 49 50 namespace llvm { 51 namespace yaml { 52 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> { 53 static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) { 54 IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp); 55 IO.enumCase(Value, "Java", FormatStyle::LK_Java); 56 IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript); 57 IO.enumCase(Value, "ObjC", FormatStyle::LK_ObjC); 58 IO.enumCase(Value, "Proto", FormatStyle::LK_Proto); 59 IO.enumCase(Value, "TableGen", FormatStyle::LK_TableGen); 60 } 61 }; 62 63 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> { 64 static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) { 65 IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03); 66 IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03); 67 IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11); 68 IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11); 69 IO.enumCase(Value, "Auto", FormatStyle::LS_Auto); 70 } 71 }; 72 73 template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> { 74 static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) { 75 IO.enumCase(Value, "Never", FormatStyle::UT_Never); 76 IO.enumCase(Value, "false", FormatStyle::UT_Never); 77 IO.enumCase(Value, "Always", FormatStyle::UT_Always); 78 IO.enumCase(Value, "true", FormatStyle::UT_Always); 79 IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation); 80 IO.enumCase(Value, "ForContinuationAndIndentation", 81 FormatStyle::UT_ForContinuationAndIndentation); 82 } 83 }; 84 85 template <> struct ScalarEnumerationTraits<FormatStyle::JavaScriptQuoteStyle> { 86 static void enumeration(IO &IO, FormatStyle::JavaScriptQuoteStyle &Value) { 87 IO.enumCase(Value, "Leave", FormatStyle::JSQS_Leave); 88 IO.enumCase(Value, "Single", FormatStyle::JSQS_Single); 89 IO.enumCase(Value, "Double", FormatStyle::JSQS_Double); 90 } 91 }; 92 93 template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> { 94 static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) { 95 IO.enumCase(Value, "None", FormatStyle::SFS_None); 96 IO.enumCase(Value, "false", FormatStyle::SFS_None); 97 IO.enumCase(Value, "All", FormatStyle::SFS_All); 98 IO.enumCase(Value, "true", FormatStyle::SFS_All); 99 IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline); 100 IO.enumCase(Value, "InlineOnly", FormatStyle::SFS_InlineOnly); 101 IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty); 102 } 103 }; 104 105 template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> { 106 static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) { 107 IO.enumCase(Value, "All", FormatStyle::BOS_All); 108 IO.enumCase(Value, "true", FormatStyle::BOS_All); 109 IO.enumCase(Value, "None", FormatStyle::BOS_None); 110 IO.enumCase(Value, "false", FormatStyle::BOS_None); 111 IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment); 112 } 113 }; 114 115 template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> { 116 static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) { 117 IO.enumCase(Value, "Attach", FormatStyle::BS_Attach); 118 IO.enumCase(Value, "Linux", FormatStyle::BS_Linux); 119 IO.enumCase(Value, "Mozilla", FormatStyle::BS_Mozilla); 120 IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup); 121 IO.enumCase(Value, "Allman", FormatStyle::BS_Allman); 122 IO.enumCase(Value, "GNU", FormatStyle::BS_GNU); 123 IO.enumCase(Value, "WebKit", FormatStyle::BS_WebKit); 124 IO.enumCase(Value, "Custom", FormatStyle::BS_Custom); 125 } 126 }; 127 128 template <> struct ScalarEnumerationTraits<FormatStyle::BreakConstructorInitializersStyle> { 129 static void enumeration(IO &IO, FormatStyle::BreakConstructorInitializersStyle &Value) { 130 IO.enumCase(Value, "BeforeColon", FormatStyle::BCIS_BeforeColon); 131 IO.enumCase(Value, "BeforeComma", FormatStyle::BCIS_BeforeComma); 132 IO.enumCase(Value, "AfterColon", FormatStyle::BCIS_AfterColon); 133 } 134 }; 135 136 template <> 137 struct ScalarEnumerationTraits<FormatStyle::ReturnTypeBreakingStyle> { 138 static void enumeration(IO &IO, FormatStyle::ReturnTypeBreakingStyle &Value) { 139 IO.enumCase(Value, "None", FormatStyle::RTBS_None); 140 IO.enumCase(Value, "All", FormatStyle::RTBS_All); 141 IO.enumCase(Value, "TopLevel", FormatStyle::RTBS_TopLevel); 142 IO.enumCase(Value, "TopLevelDefinitions", 143 FormatStyle::RTBS_TopLevelDefinitions); 144 IO.enumCase(Value, "AllDefinitions", FormatStyle::RTBS_AllDefinitions); 145 } 146 }; 147 148 template <> 149 struct ScalarEnumerationTraits<FormatStyle::DefinitionReturnTypeBreakingStyle> { 150 static void 151 enumeration(IO &IO, FormatStyle::DefinitionReturnTypeBreakingStyle &Value) { 152 IO.enumCase(Value, "None", FormatStyle::DRTBS_None); 153 IO.enumCase(Value, "All", FormatStyle::DRTBS_All); 154 IO.enumCase(Value, "TopLevel", FormatStyle::DRTBS_TopLevel); 155 156 // For backward compatibility. 157 IO.enumCase(Value, "false", FormatStyle::DRTBS_None); 158 IO.enumCase(Value, "true", FormatStyle::DRTBS_All); 159 } 160 }; 161 162 template <> 163 struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> { 164 static void enumeration(IO &IO, 165 FormatStyle::NamespaceIndentationKind &Value) { 166 IO.enumCase(Value, "None", FormatStyle::NI_None); 167 IO.enumCase(Value, "Inner", FormatStyle::NI_Inner); 168 IO.enumCase(Value, "All", FormatStyle::NI_All); 169 } 170 }; 171 172 template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> { 173 static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) { 174 IO.enumCase(Value, "Align", FormatStyle::BAS_Align); 175 IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign); 176 IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak); 177 178 // For backward compatibility. 179 IO.enumCase(Value, "true", FormatStyle::BAS_Align); 180 IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign); 181 } 182 }; 183 184 template <> struct ScalarEnumerationTraits<FormatStyle::EscapedNewlineAlignmentStyle> { 185 static void enumeration(IO &IO, FormatStyle::EscapedNewlineAlignmentStyle &Value) { 186 IO.enumCase(Value, "DontAlign", FormatStyle::ENAS_DontAlign); 187 IO.enumCase(Value, "Left", FormatStyle::ENAS_Left); 188 IO.enumCase(Value, "Right", FormatStyle::ENAS_Right); 189 190 // For backward compatibility. 191 IO.enumCase(Value, "true", FormatStyle::ENAS_Left); 192 IO.enumCase(Value, "false", FormatStyle::ENAS_Right); 193 } 194 }; 195 196 template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> { 197 static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) { 198 IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle); 199 IO.enumCase(Value, "Left", FormatStyle::PAS_Left); 200 IO.enumCase(Value, "Right", FormatStyle::PAS_Right); 201 202 // For backward compatibility. 203 IO.enumCase(Value, "true", FormatStyle::PAS_Left); 204 IO.enumCase(Value, "false", FormatStyle::PAS_Right); 205 } 206 }; 207 208 template <> 209 struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> { 210 static void enumeration(IO &IO, 211 FormatStyle::SpaceBeforeParensOptions &Value) { 212 IO.enumCase(Value, "Never", FormatStyle::SBPO_Never); 213 IO.enumCase(Value, "ControlStatements", 214 FormatStyle::SBPO_ControlStatements); 215 IO.enumCase(Value, "Always", FormatStyle::SBPO_Always); 216 217 // For backward compatibility. 218 IO.enumCase(Value, "false", FormatStyle::SBPO_Never); 219 IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements); 220 } 221 }; 222 223 template <> struct MappingTraits<FormatStyle> { 224 static void mapping(IO &IO, FormatStyle &Style) { 225 // When reading, read the language first, we need it for getPredefinedStyle. 226 IO.mapOptional("Language", Style.Language); 227 228 if (IO.outputting()) { 229 StringRef StylesArray[] = {"LLVM", "Google", "Chromium", 230 "Mozilla", "WebKit", "GNU"}; 231 ArrayRef<StringRef> Styles(StylesArray); 232 for (size_t i = 0, e = Styles.size(); i < e; ++i) { 233 StringRef StyleName(Styles[i]); 234 FormatStyle PredefinedStyle; 235 if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) && 236 Style == PredefinedStyle) { 237 IO.mapOptional("# BasedOnStyle", StyleName); 238 break; 239 } 240 } 241 } else { 242 StringRef BasedOnStyle; 243 IO.mapOptional("BasedOnStyle", BasedOnStyle); 244 if (!BasedOnStyle.empty()) { 245 FormatStyle::LanguageKind OldLanguage = Style.Language; 246 FormatStyle::LanguageKind Language = 247 ((FormatStyle *)IO.getContext())->Language; 248 if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) { 249 IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle)); 250 return; 251 } 252 Style.Language = OldLanguage; 253 } 254 } 255 256 // For backward compatibility. 257 if (!IO.outputting()) { 258 IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlines); 259 IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment); 260 IO.mapOptional("IndentFunctionDeclarationAfterType", 261 Style.IndentWrappedFunctionNames); 262 IO.mapOptional("PointerBindsToType", Style.PointerAlignment); 263 IO.mapOptional("SpaceAfterControlStatementKeyword", 264 Style.SpaceBeforeParens); 265 } 266 267 IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); 268 IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket); 269 IO.mapOptional("AlignConsecutiveAssignments", 270 Style.AlignConsecutiveAssignments); 271 IO.mapOptional("AlignConsecutiveDeclarations", 272 Style.AlignConsecutiveDeclarations); 273 IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines); 274 IO.mapOptional("AlignOperands", Style.AlignOperands); 275 IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); 276 IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", 277 Style.AllowAllParametersOfDeclarationOnNextLine); 278 IO.mapOptional("AllowShortBlocksOnASingleLine", 279 Style.AllowShortBlocksOnASingleLine); 280 IO.mapOptional("AllowShortCaseLabelsOnASingleLine", 281 Style.AllowShortCaseLabelsOnASingleLine); 282 IO.mapOptional("AllowShortFunctionsOnASingleLine", 283 Style.AllowShortFunctionsOnASingleLine); 284 IO.mapOptional("AllowShortIfStatementsOnASingleLine", 285 Style.AllowShortIfStatementsOnASingleLine); 286 IO.mapOptional("AllowShortLoopsOnASingleLine", 287 Style.AllowShortLoopsOnASingleLine); 288 IO.mapOptional("AlwaysBreakAfterDefinitionReturnType", 289 Style.AlwaysBreakAfterDefinitionReturnType); 290 IO.mapOptional("AlwaysBreakAfterReturnType", 291 Style.AlwaysBreakAfterReturnType); 292 // If AlwaysBreakAfterDefinitionReturnType was specified but 293 // AlwaysBreakAfterReturnType was not, initialize the latter from the 294 // former for backwards compatibility. 295 if (Style.AlwaysBreakAfterDefinitionReturnType != FormatStyle::DRTBS_None && 296 Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) { 297 if (Style.AlwaysBreakAfterDefinitionReturnType == FormatStyle::DRTBS_All) 298 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 299 else if (Style.AlwaysBreakAfterDefinitionReturnType == 300 FormatStyle::DRTBS_TopLevel) 301 Style.AlwaysBreakAfterReturnType = 302 FormatStyle::RTBS_TopLevelDefinitions; 303 } 304 305 IO.mapOptional("AlwaysBreakBeforeMultilineStrings", 306 Style.AlwaysBreakBeforeMultilineStrings); 307 IO.mapOptional("AlwaysBreakTemplateDeclarations", 308 Style.AlwaysBreakTemplateDeclarations); 309 IO.mapOptional("BinPackArguments", Style.BinPackArguments); 310 IO.mapOptional("BinPackParameters", Style.BinPackParameters); 311 IO.mapOptional("BraceWrapping", Style.BraceWrapping); 312 IO.mapOptional("BreakBeforeBinaryOperators", 313 Style.BreakBeforeBinaryOperators); 314 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); 315 IO.mapOptional("BreakBeforeInheritanceComma", 316 Style.BreakBeforeInheritanceComma); 317 IO.mapOptional("BreakBeforeTernaryOperators", 318 Style.BreakBeforeTernaryOperators); 319 320 bool BreakConstructorInitializersBeforeComma = false; 321 IO.mapOptional("BreakConstructorInitializersBeforeComma", 322 BreakConstructorInitializersBeforeComma); 323 IO.mapOptional("BreakConstructorInitializers", 324 Style.BreakConstructorInitializers); 325 // If BreakConstructorInitializersBeforeComma was specified but 326 // BreakConstructorInitializers was not, initialize the latter from the 327 // former for backwards compatibility. 328 if (BreakConstructorInitializersBeforeComma && 329 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon) 330 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 331 332 IO.mapOptional("BreakAfterJavaFieldAnnotations", 333 Style.BreakAfterJavaFieldAnnotations); 334 IO.mapOptional("BreakStringLiterals", Style.BreakStringLiterals); 335 IO.mapOptional("ColumnLimit", Style.ColumnLimit); 336 IO.mapOptional("CommentPragmas", Style.CommentPragmas); 337 IO.mapOptional("CompactNamespaces", Style.CompactNamespaces); 338 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", 339 Style.ConstructorInitializerAllOnOneLineOrOnePerLine); 340 IO.mapOptional("ConstructorInitializerIndentWidth", 341 Style.ConstructorInitializerIndentWidth); 342 IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth); 343 IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); 344 IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment); 345 IO.mapOptional("DisableFormat", Style.DisableFormat); 346 IO.mapOptional("ExperimentalAutoDetectBinPacking", 347 Style.ExperimentalAutoDetectBinPacking); 348 IO.mapOptional("FixNamespaceComments", Style.FixNamespaceComments); 349 IO.mapOptional("ForEachMacros", Style.ForEachMacros); 350 IO.mapOptional("IncludeCategories", Style.IncludeCategories); 351 IO.mapOptional("IncludeIsMainRegex", Style.IncludeIsMainRegex); 352 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); 353 IO.mapOptional("IndentWidth", Style.IndentWidth); 354 IO.mapOptional("IndentWrappedFunctionNames", 355 Style.IndentWrappedFunctionNames); 356 IO.mapOptional("JavaScriptQuotes", Style.JavaScriptQuotes); 357 IO.mapOptional("JavaScriptWrapImports", Style.JavaScriptWrapImports); 358 IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks", 359 Style.KeepEmptyLinesAtTheStartOfBlocks); 360 IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin); 361 IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd); 362 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); 363 IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); 364 IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth); 365 IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty); 366 IO.mapOptional("ObjCSpaceBeforeProtocolList", 367 Style.ObjCSpaceBeforeProtocolList); 368 IO.mapOptional("PenaltyBreakAssignment", 369 Style.PenaltyBreakAssignment); 370 IO.mapOptional("PenaltyBreakBeforeFirstCallParameter", 371 Style.PenaltyBreakBeforeFirstCallParameter); 372 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); 373 IO.mapOptional("PenaltyBreakFirstLessLess", 374 Style.PenaltyBreakFirstLessLess); 375 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); 376 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); 377 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", 378 Style.PenaltyReturnTypeOnItsOwnLine); 379 IO.mapOptional("PointerAlignment", Style.PointerAlignment); 380 IO.mapOptional("ReflowComments", Style.ReflowComments); 381 IO.mapOptional("SortIncludes", Style.SortIncludes); 382 IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations); 383 IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); 384 IO.mapOptional("SpaceAfterTemplateKeyword", Style.SpaceAfterTemplateKeyword); 385 IO.mapOptional("SpaceBeforeAssignmentOperators", 386 Style.SpaceBeforeAssignmentOperators); 387 IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens); 388 IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); 389 IO.mapOptional("SpacesBeforeTrailingComments", 390 Style.SpacesBeforeTrailingComments); 391 IO.mapOptional("SpacesInAngles", Style.SpacesInAngles); 392 IO.mapOptional("SpacesInContainerLiterals", 393 Style.SpacesInContainerLiterals); 394 IO.mapOptional("SpacesInCStyleCastParentheses", 395 Style.SpacesInCStyleCastParentheses); 396 IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); 397 IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets); 398 IO.mapOptional("Standard", Style.Standard); 399 IO.mapOptional("TabWidth", Style.TabWidth); 400 IO.mapOptional("UseTab", Style.UseTab); 401 } 402 }; 403 404 template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> { 405 static void mapping(IO &IO, FormatStyle::BraceWrappingFlags &Wrapping) { 406 IO.mapOptional("AfterClass", Wrapping.AfterClass); 407 IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement); 408 IO.mapOptional("AfterEnum", Wrapping.AfterEnum); 409 IO.mapOptional("AfterFunction", Wrapping.AfterFunction); 410 IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace); 411 IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration); 412 IO.mapOptional("AfterStruct", Wrapping.AfterStruct); 413 IO.mapOptional("AfterUnion", Wrapping.AfterUnion); 414 IO.mapOptional("BeforeCatch", Wrapping.BeforeCatch); 415 IO.mapOptional("BeforeElse", Wrapping.BeforeElse); 416 IO.mapOptional("IndentBraces", Wrapping.IndentBraces); 417 IO.mapOptional("SplitEmptyFunctionBody", Wrapping.SplitEmptyFunctionBody); 418 } 419 }; 420 421 template <> struct MappingTraits<FormatStyle::IncludeCategory> { 422 static void mapping(IO &IO, FormatStyle::IncludeCategory &Category) { 423 IO.mapOptional("Regex", Category.Regex); 424 IO.mapOptional("Priority", Category.Priority); 425 } 426 }; 427 428 // Allows to read vector<FormatStyle> while keeping default values. 429 // IO.getContext() should contain a pointer to the FormatStyle structure, that 430 // will be used to get default values for missing keys. 431 // If the first element has no Language specified, it will be treated as the 432 // default one for the following elements. 433 template <> struct DocumentListTraits<std::vector<FormatStyle>> { 434 static size_t size(IO &IO, std::vector<FormatStyle> &Seq) { 435 return Seq.size(); 436 } 437 static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq, 438 size_t Index) { 439 if (Index >= Seq.size()) { 440 assert(Index == Seq.size()); 441 FormatStyle Template; 442 if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) { 443 Template = Seq[0]; 444 } else { 445 Template = *((const FormatStyle *)IO.getContext()); 446 Template.Language = FormatStyle::LK_None; 447 } 448 Seq.resize(Index + 1, Template); 449 } 450 return Seq[Index]; 451 } 452 }; 453 } // namespace yaml 454 } // namespace llvm 455 456 namespace clang { 457 namespace format { 458 459 const std::error_category &getParseCategory() { 460 static ParseErrorCategory C; 461 return C; 462 } 463 std::error_code make_error_code(ParseError e) { 464 return std::error_code(static_cast<int>(e), getParseCategory()); 465 } 466 467 inline llvm::Error make_string_error(const llvm::Twine &Message) { 468 return llvm::make_error<llvm::StringError>(Message, 469 llvm::inconvertibleErrorCode()); 470 } 471 472 const char *ParseErrorCategory::name() const noexcept { 473 return "clang-format.parse_error"; 474 } 475 476 std::string ParseErrorCategory::message(int EV) const { 477 switch (static_cast<ParseError>(EV)) { 478 case ParseError::Success: 479 return "Success"; 480 case ParseError::Error: 481 return "Invalid argument"; 482 case ParseError::Unsuitable: 483 return "Unsuitable"; 484 } 485 llvm_unreachable("unexpected parse error"); 486 } 487 488 static FormatStyle expandPresets(const FormatStyle &Style) { 489 if (Style.BreakBeforeBraces == FormatStyle::BS_Custom) 490 return Style; 491 FormatStyle Expanded = Style; 492 Expanded.BraceWrapping = {false, false, false, false, false, false, 493 false, false, false, false, false, true}; 494 switch (Style.BreakBeforeBraces) { 495 case FormatStyle::BS_Linux: 496 Expanded.BraceWrapping.AfterClass = true; 497 Expanded.BraceWrapping.AfterFunction = true; 498 Expanded.BraceWrapping.AfterNamespace = true; 499 break; 500 case FormatStyle::BS_Mozilla: 501 Expanded.BraceWrapping.AfterClass = true; 502 Expanded.BraceWrapping.AfterEnum = true; 503 Expanded.BraceWrapping.AfterFunction = true; 504 Expanded.BraceWrapping.AfterStruct = true; 505 Expanded.BraceWrapping.AfterUnion = true; 506 Expanded.BraceWrapping.SplitEmptyFunctionBody = false; 507 break; 508 case FormatStyle::BS_Stroustrup: 509 Expanded.BraceWrapping.AfterFunction = true; 510 Expanded.BraceWrapping.BeforeCatch = true; 511 Expanded.BraceWrapping.BeforeElse = true; 512 break; 513 case FormatStyle::BS_Allman: 514 Expanded.BraceWrapping.AfterClass = true; 515 Expanded.BraceWrapping.AfterControlStatement = true; 516 Expanded.BraceWrapping.AfterEnum = true; 517 Expanded.BraceWrapping.AfterFunction = true; 518 Expanded.BraceWrapping.AfterNamespace = true; 519 Expanded.BraceWrapping.AfterObjCDeclaration = true; 520 Expanded.BraceWrapping.AfterStruct = true; 521 Expanded.BraceWrapping.BeforeCatch = true; 522 Expanded.BraceWrapping.BeforeElse = true; 523 break; 524 case FormatStyle::BS_GNU: 525 Expanded.BraceWrapping = {true, true, true, true, true, true, 526 true, true, true, true, true, true}; 527 break; 528 case FormatStyle::BS_WebKit: 529 Expanded.BraceWrapping.AfterFunction = true; 530 break; 531 default: 532 break; 533 } 534 return Expanded; 535 } 536 537 FormatStyle getLLVMStyle() { 538 FormatStyle LLVMStyle; 539 LLVMStyle.Language = FormatStyle::LK_Cpp; 540 LLVMStyle.AccessModifierOffset = -2; 541 LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right; 542 LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align; 543 LLVMStyle.AlignOperands = true; 544 LLVMStyle.AlignTrailingComments = true; 545 LLVMStyle.AlignConsecutiveAssignments = false; 546 LLVMStyle.AlignConsecutiveDeclarations = false; 547 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; 548 LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 549 LLVMStyle.AllowShortBlocksOnASingleLine = false; 550 LLVMStyle.AllowShortCaseLabelsOnASingleLine = false; 551 LLVMStyle.AllowShortIfStatementsOnASingleLine = false; 552 LLVMStyle.AllowShortLoopsOnASingleLine = false; 553 LLVMStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 554 LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None; 555 LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; 556 LLVMStyle.AlwaysBreakTemplateDeclarations = false; 557 LLVMStyle.BinPackArguments = true; 558 LLVMStyle.BinPackParameters = true; 559 LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 560 LLVMStyle.BreakBeforeTernaryOperators = true; 561 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; 562 LLVMStyle.BraceWrapping = {false, false, false, false, false, false, 563 false, false, false, false, false, true}; 564 LLVMStyle.BreakAfterJavaFieldAnnotations = false; 565 LLVMStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 566 LLVMStyle.BreakBeforeInheritanceComma = false; 567 LLVMStyle.BreakStringLiterals = true; 568 LLVMStyle.ColumnLimit = 80; 569 LLVMStyle.CommentPragmas = "^ IWYU pragma:"; 570 LLVMStyle.CompactNamespaces = false; 571 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 572 LLVMStyle.ConstructorInitializerIndentWidth = 4; 573 LLVMStyle.ContinuationIndentWidth = 4; 574 LLVMStyle.Cpp11BracedListStyle = true; 575 LLVMStyle.DerivePointerAlignment = false; 576 LLVMStyle.ExperimentalAutoDetectBinPacking = false; 577 LLVMStyle.FixNamespaceComments = true; 578 LLVMStyle.ForEachMacros.push_back("foreach"); 579 LLVMStyle.ForEachMacros.push_back("Q_FOREACH"); 580 LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH"); 581 LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2}, 582 {"^(<|\"(gtest|isl|json)/)", 3}, 583 {".*", 1}}; 584 LLVMStyle.IncludeIsMainRegex = "$"; 585 LLVMStyle.IndentCaseLabels = false; 586 LLVMStyle.IndentWrappedFunctionNames = false; 587 LLVMStyle.IndentWidth = 2; 588 LLVMStyle.JavaScriptQuotes = FormatStyle::JSQS_Leave; 589 LLVMStyle.JavaScriptWrapImports = true; 590 LLVMStyle.TabWidth = 8; 591 LLVMStyle.MaxEmptyLinesToKeep = 1; 592 LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true; 593 LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; 594 LLVMStyle.ObjCBlockIndentWidth = 2; 595 LLVMStyle.ObjCSpaceAfterProperty = false; 596 LLVMStyle.ObjCSpaceBeforeProtocolList = true; 597 LLVMStyle.PointerAlignment = FormatStyle::PAS_Right; 598 LLVMStyle.SpacesBeforeTrailingComments = 1; 599 LLVMStyle.Standard = FormatStyle::LS_Cpp11; 600 LLVMStyle.UseTab = FormatStyle::UT_Never; 601 LLVMStyle.ReflowComments = true; 602 LLVMStyle.SpacesInParentheses = false; 603 LLVMStyle.SpacesInSquareBrackets = false; 604 LLVMStyle.SpaceInEmptyParentheses = false; 605 LLVMStyle.SpacesInContainerLiterals = true; 606 LLVMStyle.SpacesInCStyleCastParentheses = false; 607 LLVMStyle.SpaceAfterCStyleCast = false; 608 LLVMStyle.SpaceAfterTemplateKeyword = true; 609 LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; 610 LLVMStyle.SpaceBeforeAssignmentOperators = true; 611 LLVMStyle.SpacesInAngles = false; 612 613 LLVMStyle.PenaltyBreakAssignment = prec::Assignment; 614 LLVMStyle.PenaltyBreakComment = 300; 615 LLVMStyle.PenaltyBreakFirstLessLess = 120; 616 LLVMStyle.PenaltyBreakString = 1000; 617 LLVMStyle.PenaltyExcessCharacter = 1000000; 618 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; 619 LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19; 620 621 LLVMStyle.DisableFormat = false; 622 LLVMStyle.SortIncludes = true; 623 LLVMStyle.SortUsingDeclarations = true; 624 625 return LLVMStyle; 626 } 627 628 FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { 629 FormatStyle GoogleStyle = getLLVMStyle(); 630 GoogleStyle.Language = Language; 631 632 GoogleStyle.AccessModifierOffset = -1; 633 GoogleStyle.AlignEscapedNewlines = FormatStyle::ENAS_Left; 634 GoogleStyle.AllowShortIfStatementsOnASingleLine = true; 635 GoogleStyle.AllowShortLoopsOnASingleLine = true; 636 GoogleStyle.AlwaysBreakBeforeMultilineStrings = true; 637 GoogleStyle.AlwaysBreakTemplateDeclarations = true; 638 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 639 GoogleStyle.DerivePointerAlignment = true; 640 GoogleStyle.IncludeCategories = {{"^<.*\\.h>", 1}, {"^<.*", 2}, {".*", 3}}; 641 GoogleStyle.IncludeIsMainRegex = "([-_](test|unittest))?$"; 642 GoogleStyle.IndentCaseLabels = true; 643 GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false; 644 GoogleStyle.ObjCSpaceAfterProperty = false; 645 GoogleStyle.ObjCSpaceBeforeProtocolList = false; 646 GoogleStyle.PointerAlignment = FormatStyle::PAS_Left; 647 GoogleStyle.SpacesBeforeTrailingComments = 2; 648 GoogleStyle.Standard = FormatStyle::LS_Auto; 649 650 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; 651 GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1; 652 653 if (Language == FormatStyle::LK_Java) { 654 GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 655 GoogleStyle.AlignOperands = false; 656 GoogleStyle.AlignTrailingComments = false; 657 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 658 GoogleStyle.AllowShortIfStatementsOnASingleLine = false; 659 GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; 660 GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 661 GoogleStyle.ColumnLimit = 100; 662 GoogleStyle.SpaceAfterCStyleCast = true; 663 GoogleStyle.SpacesBeforeTrailingComments = 1; 664 } else if (Language == FormatStyle::LK_JavaScript) { 665 GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 666 GoogleStyle.AlignOperands = false; 667 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 668 GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; 669 GoogleStyle.BreakBeforeTernaryOperators = false; 670 // taze:, triple slash directives (`/// <...`), @tag followed by { for a lot 671 // of JSDoc tags, and @see, which is commonly followed by overlong URLs. 672 GoogleStyle.CommentPragmas = 673 "(taze:|^/[ \t]*<|(@[A-Za-z_0-9-]+[ \\t]*{)|@see)"; 674 GoogleStyle.MaxEmptyLinesToKeep = 3; 675 GoogleStyle.NamespaceIndentation = FormatStyle::NI_All; 676 GoogleStyle.SpacesInContainerLiterals = false; 677 GoogleStyle.JavaScriptQuotes = FormatStyle::JSQS_Single; 678 GoogleStyle.JavaScriptWrapImports = false; 679 } else if (Language == FormatStyle::LK_Proto) { 680 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 681 GoogleStyle.SpacesInContainerLiterals = false; 682 } else if (Language == FormatStyle::LK_ObjC) { 683 GoogleStyle.ColumnLimit = 100; 684 } 685 686 return GoogleStyle; 687 } 688 689 FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { 690 FormatStyle ChromiumStyle = getGoogleStyle(Language); 691 if (Language == FormatStyle::LK_Java) { 692 ChromiumStyle.AllowShortIfStatementsOnASingleLine = true; 693 ChromiumStyle.BreakAfterJavaFieldAnnotations = true; 694 ChromiumStyle.ContinuationIndentWidth = 8; 695 ChromiumStyle.IndentWidth = 4; 696 } else if (Language == FormatStyle::LK_JavaScript) { 697 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; 698 ChromiumStyle.AllowShortLoopsOnASingleLine = false; 699 } else { 700 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; 701 ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 702 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; 703 ChromiumStyle.AllowShortLoopsOnASingleLine = false; 704 ChromiumStyle.BinPackParameters = false; 705 ChromiumStyle.DerivePointerAlignment = false; 706 if (Language == FormatStyle::LK_ObjC) 707 ChromiumStyle.ColumnLimit = 80; 708 } 709 return ChromiumStyle; 710 } 711 712 FormatStyle getMozillaStyle() { 713 FormatStyle MozillaStyle = getLLVMStyle(); 714 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; 715 MozillaStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 716 MozillaStyle.AlwaysBreakAfterReturnType = 717 FormatStyle::RTBS_TopLevel; 718 MozillaStyle.AlwaysBreakAfterDefinitionReturnType = 719 FormatStyle::DRTBS_TopLevel; 720 MozillaStyle.AlwaysBreakTemplateDeclarations = true; 721 MozillaStyle.BinPackParameters = false; 722 MozillaStyle.BinPackArguments = false; 723 MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 724 MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 725 MozillaStyle.BreakBeforeInheritanceComma = true; 726 MozillaStyle.ConstructorInitializerIndentWidth = 2; 727 MozillaStyle.ContinuationIndentWidth = 2; 728 MozillaStyle.Cpp11BracedListStyle = false; 729 MozillaStyle.FixNamespaceComments = false; 730 MozillaStyle.IndentCaseLabels = true; 731 MozillaStyle.ObjCSpaceAfterProperty = true; 732 MozillaStyle.ObjCSpaceBeforeProtocolList = false; 733 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; 734 MozillaStyle.PointerAlignment = FormatStyle::PAS_Left; 735 MozillaStyle.SpaceAfterTemplateKeyword = false; 736 return MozillaStyle; 737 } 738 739 FormatStyle getWebKitStyle() { 740 FormatStyle Style = getLLVMStyle(); 741 Style.AccessModifierOffset = -4; 742 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 743 Style.AlignOperands = false; 744 Style.AlignTrailingComments = false; 745 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 746 Style.BreakBeforeBraces = FormatStyle::BS_WebKit; 747 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 748 Style.Cpp11BracedListStyle = false; 749 Style.ColumnLimit = 0; 750 Style.FixNamespaceComments = false; 751 Style.IndentWidth = 4; 752 Style.NamespaceIndentation = FormatStyle::NI_Inner; 753 Style.ObjCBlockIndentWidth = 4; 754 Style.ObjCSpaceAfterProperty = true; 755 Style.PointerAlignment = FormatStyle::PAS_Left; 756 return Style; 757 } 758 759 FormatStyle getGNUStyle() { 760 FormatStyle Style = getLLVMStyle(); 761 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 762 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 763 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 764 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 765 Style.BreakBeforeTernaryOperators = true; 766 Style.Cpp11BracedListStyle = false; 767 Style.ColumnLimit = 79; 768 Style.FixNamespaceComments = false; 769 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 770 Style.Standard = FormatStyle::LS_Cpp03; 771 return Style; 772 } 773 774 FormatStyle getNoStyle() { 775 FormatStyle NoStyle = getLLVMStyle(); 776 NoStyle.DisableFormat = true; 777 NoStyle.SortIncludes = false; 778 NoStyle.SortUsingDeclarations = false; 779 return NoStyle; 780 } 781 782 bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language, 783 FormatStyle *Style) { 784 if (Name.equals_lower("llvm")) { 785 *Style = getLLVMStyle(); 786 } else if (Name.equals_lower("chromium")) { 787 *Style = getChromiumStyle(Language); 788 } else if (Name.equals_lower("mozilla")) { 789 *Style = getMozillaStyle(); 790 } else if (Name.equals_lower("google")) { 791 *Style = getGoogleStyle(Language); 792 } else if (Name.equals_lower("webkit")) { 793 *Style = getWebKitStyle(); 794 } else if (Name.equals_lower("gnu")) { 795 *Style = getGNUStyle(); 796 } else if (Name.equals_lower("none")) { 797 *Style = getNoStyle(); 798 } else { 799 return false; 800 } 801 802 Style->Language = Language; 803 return true; 804 } 805 806 std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { 807 assert(Style); 808 FormatStyle::LanguageKind Language = Style->Language; 809 assert(Language != FormatStyle::LK_None); 810 if (Text.trim().empty()) 811 return make_error_code(ParseError::Error); 812 813 std::vector<FormatStyle> Styles; 814 llvm::yaml::Input Input(Text); 815 // DocumentListTraits<vector<FormatStyle>> uses the context to get default 816 // values for the fields, keys for which are missing from the configuration. 817 // Mapping also uses the context to get the language to find the correct 818 // base style. 819 Input.setContext(Style); 820 Input >> Styles; 821 if (Input.error()) 822 return Input.error(); 823 824 for (unsigned i = 0; i < Styles.size(); ++i) { 825 // Ensures that only the first configuration can skip the Language option. 826 if (Styles[i].Language == FormatStyle::LK_None && i != 0) 827 return make_error_code(ParseError::Error); 828 // Ensure that each language is configured at most once. 829 for (unsigned j = 0; j < i; ++j) { 830 if (Styles[i].Language == Styles[j].Language) { 831 DEBUG(llvm::dbgs() 832 << "Duplicate languages in the config file on positions " << j 833 << " and " << i << "\n"); 834 return make_error_code(ParseError::Error); 835 } 836 } 837 } 838 // Look for a suitable configuration starting from the end, so we can 839 // find the configuration for the specific language first, and the default 840 // configuration (which can only be at slot 0) after it. 841 for (int i = Styles.size() - 1; i >= 0; --i) { 842 if (Styles[i].Language == Language || 843 Styles[i].Language == FormatStyle::LK_None) { 844 *Style = Styles[i]; 845 Style->Language = Language; 846 return make_error_code(ParseError::Success); 847 } 848 } 849 return make_error_code(ParseError::Unsuitable); 850 } 851 852 std::string configurationAsText(const FormatStyle &Style) { 853 std::string Text; 854 llvm::raw_string_ostream Stream(Text); 855 llvm::yaml::Output Output(Stream); 856 // We use the same mapping method for input and output, so we need a non-const 857 // reference here. 858 FormatStyle NonConstStyle = expandPresets(Style); 859 Output << NonConstStyle; 860 return Stream.str(); 861 } 862 863 namespace { 864 865 class JavaScriptRequoter : public TokenAnalyzer { 866 public: 867 JavaScriptRequoter(const Environment &Env, const FormatStyle &Style) 868 : TokenAnalyzer(Env, Style) {} 869 870 tooling::Replacements 871 analyze(TokenAnnotator &Annotator, 872 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, 873 FormatTokenLexer &Tokens) override { 874 AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(), 875 AnnotatedLines.end()); 876 tooling::Replacements Result; 877 requoteJSStringLiteral(AnnotatedLines, Result); 878 return Result; 879 } 880 881 private: 882 // Replaces double/single-quoted string literal as appropriate, re-escaping 883 // the contents in the process. 884 void requoteJSStringLiteral(SmallVectorImpl<AnnotatedLine *> &Lines, 885 tooling::Replacements &Result) { 886 for (AnnotatedLine *Line : Lines) { 887 requoteJSStringLiteral(Line->Children, Result); 888 if (!Line->Affected) 889 continue; 890 for (FormatToken *FormatTok = Line->First; FormatTok; 891 FormatTok = FormatTok->Next) { 892 StringRef Input = FormatTok->TokenText; 893 if (FormatTok->Finalized || !FormatTok->isStringLiteral() || 894 // NB: testing for not starting with a double quote to avoid 895 // breaking `template strings`. 896 (Style.JavaScriptQuotes == FormatStyle::JSQS_Single && 897 !Input.startswith("\"")) || 898 (Style.JavaScriptQuotes == FormatStyle::JSQS_Double && 899 !Input.startswith("\'"))) 900 continue; 901 902 // Change start and end quote. 903 bool IsSingle = Style.JavaScriptQuotes == FormatStyle::JSQS_Single; 904 SourceLocation Start = FormatTok->Tok.getLocation(); 905 auto Replace = [&](SourceLocation Start, unsigned Length, 906 StringRef ReplacementText) { 907 auto Err = Result.add(tooling::Replacement( 908 Env.getSourceManager(), Start, Length, ReplacementText)); 909 // FIXME: handle error. For now, print error message and skip the 910 // replacement for release version. 911 if (Err) { 912 llvm::errs() << llvm::toString(std::move(Err)) << "\n"; 913 assert(false); 914 } 915 }; 916 Replace(Start, 1, IsSingle ? "'" : "\""); 917 Replace(FormatTok->Tok.getEndLoc().getLocWithOffset(-1), 1, 918 IsSingle ? "'" : "\""); 919 920 // Escape internal quotes. 921 bool Escaped = false; 922 for (size_t i = 1; i < Input.size() - 1; i++) { 923 switch (Input[i]) { 924 case '\\': 925 if (!Escaped && i + 1 < Input.size() && 926 ((IsSingle && Input[i + 1] == '"') || 927 (!IsSingle && Input[i + 1] == '\''))) { 928 // Remove this \, it's escaping a " or ' that no longer needs 929 // escaping 930 Replace(Start.getLocWithOffset(i), 1, ""); 931 continue; 932 } 933 Escaped = !Escaped; 934 break; 935 case '\"': 936 case '\'': 937 if (!Escaped && IsSingle == (Input[i] == '\'')) { 938 // Escape the quote. 939 Replace(Start.getLocWithOffset(i), 0, "\\"); 940 } 941 Escaped = false; 942 break; 943 default: 944 Escaped = false; 945 break; 946 } 947 } 948 } 949 } 950 } 951 }; 952 953 class Formatter : public TokenAnalyzer { 954 public: 955 Formatter(const Environment &Env, const FormatStyle &Style, 956 FormattingAttemptStatus *Status) 957 : TokenAnalyzer(Env, Style), Status(Status) {} 958 959 tooling::Replacements 960 analyze(TokenAnnotator &Annotator, 961 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, 962 FormatTokenLexer &Tokens) override { 963 tooling::Replacements Result; 964 deriveLocalStyle(AnnotatedLines); 965 AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(), 966 AnnotatedLines.end()); 967 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 968 Annotator.calculateFormattingInformation(*AnnotatedLines[i]); 969 } 970 Annotator.setCommentLineLevels(AnnotatedLines); 971 972 WhitespaceManager Whitespaces( 973 Env.getSourceManager(), Style, 974 inputUsesCRLF(Env.getSourceManager().getBufferData(Env.getFileID()))); 975 ContinuationIndenter Indenter(Style, Tokens.getKeywords(), 976 Env.getSourceManager(), Whitespaces, Encoding, 977 BinPackInconclusiveFunctions); 978 UnwrappedLineFormatter(&Indenter, &Whitespaces, Style, Tokens.getKeywords(), 979 Env.getSourceManager(), Status) 980 .format(AnnotatedLines); 981 for (const auto &R : Whitespaces.generateReplacements()) 982 if (Result.add(R)) 983 return Result; 984 return Result; 985 } 986 987 private: 988 989 static bool inputUsesCRLF(StringRef Text) { 990 return Text.count('\r') * 2 > Text.count('\n'); 991 } 992 993 bool 994 hasCpp03IncompatibleFormat(const SmallVectorImpl<AnnotatedLine *> &Lines) { 995 for (const AnnotatedLine *Line : Lines) { 996 if (hasCpp03IncompatibleFormat(Line->Children)) 997 return true; 998 for (FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next) { 999 if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) { 1000 if (Tok->is(tok::coloncolon) && Tok->Previous->is(TT_TemplateOpener)) 1001 return true; 1002 if (Tok->is(TT_TemplateCloser) && 1003 Tok->Previous->is(TT_TemplateCloser)) 1004 return true; 1005 } 1006 } 1007 } 1008 return false; 1009 } 1010 1011 int countVariableAlignments(const SmallVectorImpl<AnnotatedLine *> &Lines) { 1012 int AlignmentDiff = 0; 1013 for (const AnnotatedLine *Line : Lines) { 1014 AlignmentDiff += countVariableAlignments(Line->Children); 1015 for (FormatToken *Tok = Line->First; Tok && Tok->Next; Tok = Tok->Next) { 1016 if (!Tok->is(TT_PointerOrReference)) 1017 continue; 1018 bool SpaceBefore = 1019 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd(); 1020 bool SpaceAfter = Tok->Next->WhitespaceRange.getBegin() != 1021 Tok->Next->WhitespaceRange.getEnd(); 1022 if (SpaceBefore && !SpaceAfter) 1023 ++AlignmentDiff; 1024 if (!SpaceBefore && SpaceAfter) 1025 --AlignmentDiff; 1026 } 1027 } 1028 return AlignmentDiff; 1029 } 1030 1031 void 1032 deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { 1033 bool HasBinPackedFunction = false; 1034 bool HasOnePerLineFunction = false; 1035 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 1036 if (!AnnotatedLines[i]->First->Next) 1037 continue; 1038 FormatToken *Tok = AnnotatedLines[i]->First->Next; 1039 while (Tok->Next) { 1040 if (Tok->PackingKind == PPK_BinPacked) 1041 HasBinPackedFunction = true; 1042 if (Tok->PackingKind == PPK_OnePerLine) 1043 HasOnePerLineFunction = true; 1044 1045 Tok = Tok->Next; 1046 } 1047 } 1048 if (Style.DerivePointerAlignment) 1049 Style.PointerAlignment = countVariableAlignments(AnnotatedLines) <= 0 1050 ? FormatStyle::PAS_Left 1051 : FormatStyle::PAS_Right; 1052 if (Style.Standard == FormatStyle::LS_Auto) 1053 Style.Standard = hasCpp03IncompatibleFormat(AnnotatedLines) 1054 ? FormatStyle::LS_Cpp11 1055 : FormatStyle::LS_Cpp03; 1056 BinPackInconclusiveFunctions = 1057 HasBinPackedFunction || !HasOnePerLineFunction; 1058 } 1059 1060 bool BinPackInconclusiveFunctions; 1061 FormattingAttemptStatus *Status; 1062 }; 1063 1064 // This class clean up the erroneous/redundant code around the given ranges in 1065 // file. 1066 class Cleaner : public TokenAnalyzer { 1067 public: 1068 Cleaner(const Environment &Env, const FormatStyle &Style) 1069 : TokenAnalyzer(Env, Style), 1070 DeletedTokens(FormatTokenLess(Env.getSourceManager())) {} 1071 1072 // FIXME: eliminate unused parameters. 1073 tooling::Replacements 1074 analyze(TokenAnnotator &Annotator, 1075 SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, 1076 FormatTokenLexer &Tokens) override { 1077 // FIXME: in the current implementation the granularity of affected range 1078 // is an annotated line. However, this is not sufficient. Furthermore, 1079 // redundant code introduced by replacements does not necessarily 1080 // intercept with ranges of replacements that result in the redundancy. 1081 // To determine if some redundant code is actually introduced by 1082 // replacements(e.g. deletions), we need to come up with a more 1083 // sophisticated way of computing affected ranges. 1084 AffectedRangeMgr.computeAffectedLines(AnnotatedLines.begin(), 1085 AnnotatedLines.end()); 1086 1087 checkEmptyNamespace(AnnotatedLines); 1088 1089 for (auto &Line : AnnotatedLines) { 1090 if (Line->Affected) { 1091 cleanupRight(Line->First, tok::comma, tok::comma); 1092 cleanupRight(Line->First, TT_CtorInitializerColon, tok::comma); 1093 cleanupRight(Line->First, tok::l_paren, tok::comma); 1094 cleanupLeft(Line->First, tok::comma, tok::r_paren); 1095 cleanupLeft(Line->First, TT_CtorInitializerComma, tok::l_brace); 1096 cleanupLeft(Line->First, TT_CtorInitializerColon, tok::l_brace); 1097 cleanupLeft(Line->First, TT_CtorInitializerColon, tok::equal); 1098 } 1099 } 1100 1101 return generateFixes(); 1102 } 1103 1104 private: 1105 bool containsOnlyComments(const AnnotatedLine &Line) { 1106 for (FormatToken *Tok = Line.First; Tok != nullptr; Tok = Tok->Next) { 1107 if (Tok->isNot(tok::comment)) 1108 return false; 1109 } 1110 return true; 1111 } 1112 1113 // Iterate through all lines and remove any empty (nested) namespaces. 1114 void checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { 1115 std::set<unsigned> DeletedLines; 1116 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 1117 auto &Line = *AnnotatedLines[i]; 1118 if (Line.startsWith(tok::kw_namespace) || 1119 Line.startsWith(tok::kw_inline, tok::kw_namespace)) { 1120 checkEmptyNamespace(AnnotatedLines, i, i, DeletedLines); 1121 } 1122 } 1123 1124 for (auto Line : DeletedLines) { 1125 FormatToken *Tok = AnnotatedLines[Line]->First; 1126 while (Tok) { 1127 deleteToken(Tok); 1128 Tok = Tok->Next; 1129 } 1130 } 1131 } 1132 1133 // The function checks if the namespace, which starts from \p CurrentLine, and 1134 // its nested namespaces are empty and delete them if they are empty. It also 1135 // sets \p NewLine to the last line checked. 1136 // Returns true if the current namespace is empty. 1137 bool checkEmptyNamespace(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, 1138 unsigned CurrentLine, unsigned &NewLine, 1139 std::set<unsigned> &DeletedLines) { 1140 unsigned InitLine = CurrentLine, End = AnnotatedLines.size(); 1141 if (Style.BraceWrapping.AfterNamespace) { 1142 // If the left brace is in a new line, we should consume it first so that 1143 // it does not make the namespace non-empty. 1144 // FIXME: error handling if there is no left brace. 1145 if (!AnnotatedLines[++CurrentLine]->startsWith(tok::l_brace)) { 1146 NewLine = CurrentLine; 1147 return false; 1148 } 1149 } else if (!AnnotatedLines[CurrentLine]->endsWith(tok::l_brace)) { 1150 return false; 1151 } 1152 while (++CurrentLine < End) { 1153 if (AnnotatedLines[CurrentLine]->startsWith(tok::r_brace)) 1154 break; 1155 1156 if (AnnotatedLines[CurrentLine]->startsWith(tok::kw_namespace) || 1157 AnnotatedLines[CurrentLine]->startsWith(tok::kw_inline, 1158 tok::kw_namespace)) { 1159 if (!checkEmptyNamespace(AnnotatedLines, CurrentLine, NewLine, 1160 DeletedLines)) 1161 return false; 1162 CurrentLine = NewLine; 1163 continue; 1164 } 1165 1166 if (containsOnlyComments(*AnnotatedLines[CurrentLine])) 1167 continue; 1168 1169 // If there is anything other than comments or nested namespaces in the 1170 // current namespace, the namespace cannot be empty. 1171 NewLine = CurrentLine; 1172 return false; 1173 } 1174 1175 NewLine = CurrentLine; 1176 if (CurrentLine >= End) 1177 return false; 1178 1179 // Check if the empty namespace is actually affected by changed ranges. 1180 if (!AffectedRangeMgr.affectsCharSourceRange(CharSourceRange::getCharRange( 1181 AnnotatedLines[InitLine]->First->Tok.getLocation(), 1182 AnnotatedLines[CurrentLine]->Last->Tok.getEndLoc()))) 1183 return false; 1184 1185 for (unsigned i = InitLine; i <= CurrentLine; ++i) { 1186 DeletedLines.insert(i); 1187 } 1188 1189 return true; 1190 } 1191 1192 // Checks pairs {start, start->next},..., {end->previous, end} and deletes one 1193 // of the token in the pair if the left token has \p LK token kind and the 1194 // right token has \p RK token kind. If \p DeleteLeft is true, the left token 1195 // is deleted on match; otherwise, the right token is deleted. 1196 template <typename LeftKind, typename RightKind> 1197 void cleanupPair(FormatToken *Start, LeftKind LK, RightKind RK, 1198 bool DeleteLeft) { 1199 auto NextNotDeleted = [this](const FormatToken &Tok) -> FormatToken * { 1200 for (auto *Res = Tok.Next; Res; Res = Res->Next) 1201 if (!Res->is(tok::comment) && 1202 DeletedTokens.find(Res) == DeletedTokens.end()) 1203 return Res; 1204 return nullptr; 1205 }; 1206 for (auto *Left = Start; Left;) { 1207 auto *Right = NextNotDeleted(*Left); 1208 if (!Right) 1209 break; 1210 if (Left->is(LK) && Right->is(RK)) { 1211 deleteToken(DeleteLeft ? Left : Right); 1212 for (auto *Tok = Left->Next; Tok && Tok != Right; Tok = Tok->Next) 1213 deleteToken(Tok); 1214 // If the right token is deleted, we should keep the left token 1215 // unchanged and pair it with the new right token. 1216 if (!DeleteLeft) 1217 continue; 1218 } 1219 Left = Right; 1220 } 1221 } 1222 1223 template <typename LeftKind, typename RightKind> 1224 void cleanupLeft(FormatToken *Start, LeftKind LK, RightKind RK) { 1225 cleanupPair(Start, LK, RK, /*DeleteLeft=*/true); 1226 } 1227 1228 template <typename LeftKind, typename RightKind> 1229 void cleanupRight(FormatToken *Start, LeftKind LK, RightKind RK) { 1230 cleanupPair(Start, LK, RK, /*DeleteLeft=*/false); 1231 } 1232 1233 // Delete the given token. 1234 inline void deleteToken(FormatToken *Tok) { 1235 if (Tok) 1236 DeletedTokens.insert(Tok); 1237 } 1238 1239 tooling::Replacements generateFixes() { 1240 tooling::Replacements Fixes; 1241 std::vector<FormatToken *> Tokens; 1242 std::copy(DeletedTokens.begin(), DeletedTokens.end(), 1243 std::back_inserter(Tokens)); 1244 1245 // Merge multiple continuous token deletions into one big deletion so that 1246 // the number of replacements can be reduced. This makes computing affected 1247 // ranges more efficient when we run reformat on the changed code. 1248 unsigned Idx = 0; 1249 while (Idx < Tokens.size()) { 1250 unsigned St = Idx, End = Idx; 1251 while ((End + 1) < Tokens.size() && 1252 Tokens[End]->Next == Tokens[End + 1]) { 1253 End++; 1254 } 1255 auto SR = CharSourceRange::getCharRange(Tokens[St]->Tok.getLocation(), 1256 Tokens[End]->Tok.getEndLoc()); 1257 auto Err = 1258 Fixes.add(tooling::Replacement(Env.getSourceManager(), SR, "")); 1259 // FIXME: better error handling. for now just print error message and skip 1260 // for the release version. 1261 if (Err) { 1262 llvm::errs() << llvm::toString(std::move(Err)) << "\n"; 1263 assert(false && "Fixes must not conflict!"); 1264 } 1265 Idx = End + 1; 1266 } 1267 1268 return Fixes; 1269 } 1270 1271 // Class for less-than inequality comparason for the set `RedundantTokens`. 1272 // We store tokens in the order they appear in the translation unit so that 1273 // we do not need to sort them in `generateFixes()`. 1274 struct FormatTokenLess { 1275 FormatTokenLess(const SourceManager &SM) : SM(SM) {} 1276 1277 bool operator()(const FormatToken *LHS, const FormatToken *RHS) const { 1278 return SM.isBeforeInTranslationUnit(LHS->Tok.getLocation(), 1279 RHS->Tok.getLocation()); 1280 } 1281 const SourceManager &SM; 1282 }; 1283 1284 // Tokens to be deleted. 1285 std::set<FormatToken *, FormatTokenLess> DeletedTokens; 1286 }; 1287 1288 struct IncludeDirective { 1289 StringRef Filename; 1290 StringRef Text; 1291 unsigned Offset; 1292 int Category; 1293 }; 1294 1295 } // end anonymous namespace 1296 1297 // Determines whether 'Ranges' intersects with ('Start', 'End'). 1298 static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start, 1299 unsigned End) { 1300 for (auto Range : Ranges) { 1301 if (Range.getOffset() < End && 1302 Range.getOffset() + Range.getLength() > Start) 1303 return true; 1304 } 1305 return false; 1306 } 1307 1308 // Returns a pair (Index, OffsetToEOL) describing the position of the cursor 1309 // before sorting/deduplicating. Index is the index of the include under the 1310 // cursor in the original set of includes. If this include has duplicates, it is 1311 // the index of the first of the duplicates as the others are going to be 1312 // removed. OffsetToEOL describes the cursor's position relative to the end of 1313 // its current line. 1314 // If `Cursor` is not on any #include, `Index` will be UINT_MAX. 1315 static std::pair<unsigned, unsigned> 1316 FindCursorIndex(const SmallVectorImpl<IncludeDirective> &Includes, 1317 const SmallVectorImpl<unsigned> &Indices, unsigned Cursor) { 1318 unsigned CursorIndex = UINT_MAX; 1319 unsigned OffsetToEOL = 0; 1320 for (int i = 0, e = Includes.size(); i != e; ++i) { 1321 unsigned Start = Includes[Indices[i]].Offset; 1322 unsigned End = Start + Includes[Indices[i]].Text.size(); 1323 if (!(Cursor >= Start && Cursor < End)) 1324 continue; 1325 CursorIndex = Indices[i]; 1326 OffsetToEOL = End - Cursor; 1327 // Put the cursor on the only remaining #include among the duplicate 1328 // #includes. 1329 while (--i >= 0 && Includes[CursorIndex].Text == Includes[Indices[i]].Text) 1330 CursorIndex = i; 1331 break; 1332 } 1333 return std::make_pair(CursorIndex, OffsetToEOL); 1334 } 1335 1336 // Sorts and deduplicate a block of includes given by 'Includes' alphabetically 1337 // adding the necessary replacement to 'Replaces'. 'Includes' must be in strict 1338 // source order. 1339 // #include directives with the same text will be deduplicated, and only the 1340 // first #include in the duplicate #includes remains. If the `Cursor` is 1341 // provided and put on a deleted #include, it will be moved to the remaining 1342 // #include in the duplicate #includes. 1343 static void sortCppIncludes(const FormatStyle &Style, 1344 const SmallVectorImpl<IncludeDirective> &Includes, 1345 ArrayRef<tooling::Range> Ranges, StringRef FileName, 1346 tooling::Replacements &Replaces, unsigned *Cursor) { 1347 unsigned IncludesBeginOffset = Includes.front().Offset; 1348 unsigned IncludesEndOffset = 1349 Includes.back().Offset + Includes.back().Text.size(); 1350 unsigned IncludesBlockSize = IncludesEndOffset - IncludesBeginOffset; 1351 if (!affectsRange(Ranges, IncludesBeginOffset, IncludesEndOffset)) 1352 return; 1353 SmallVector<unsigned, 16> Indices; 1354 for (unsigned i = 0, e = Includes.size(); i != e; ++i) 1355 Indices.push_back(i); 1356 std::stable_sort( 1357 Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) { 1358 return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) < 1359 std::tie(Includes[RHSI].Category, Includes[RHSI].Filename); 1360 }); 1361 // The index of the include on which the cursor will be put after 1362 // sorting/deduplicating. 1363 unsigned CursorIndex; 1364 // The offset from cursor to the end of line. 1365 unsigned CursorToEOLOffset; 1366 if (Cursor) 1367 std::tie(CursorIndex, CursorToEOLOffset) = 1368 FindCursorIndex(Includes, Indices, *Cursor); 1369 1370 // Deduplicate #includes. 1371 Indices.erase(std::unique(Indices.begin(), Indices.end(), 1372 [&](unsigned LHSI, unsigned RHSI) { 1373 return Includes[LHSI].Text == Includes[RHSI].Text; 1374 }), 1375 Indices.end()); 1376 1377 // If the #includes are out of order, we generate a single replacement fixing 1378 // the entire block. Otherwise, no replacement is generated. 1379 if (Indices.size() == Includes.size() && 1380 std::is_sorted(Indices.begin(), Indices.end())) 1381 return; 1382 1383 std::string result; 1384 for (unsigned Index : Indices) { 1385 if (!result.empty()) 1386 result += "\n"; 1387 result += Includes[Index].Text; 1388 if (Cursor && CursorIndex == Index) 1389 *Cursor = IncludesBeginOffset + result.size() - CursorToEOLOffset; 1390 } 1391 1392 auto Err = Replaces.add(tooling::Replacement( 1393 FileName, Includes.front().Offset, IncludesBlockSize, result)); 1394 // FIXME: better error handling. For now, just skip the replacement for the 1395 // release version. 1396 if (Err) { 1397 llvm::errs() << llvm::toString(std::move(Err)) << "\n"; 1398 assert(false); 1399 } 1400 } 1401 1402 namespace { 1403 1404 // This class manages priorities of #include categories and calculates 1405 // priorities for headers. 1406 class IncludeCategoryManager { 1407 public: 1408 IncludeCategoryManager(const FormatStyle &Style, StringRef FileName) 1409 : Style(Style), FileName(FileName) { 1410 FileStem = llvm::sys::path::stem(FileName); 1411 for (const auto &Category : Style.IncludeCategories) 1412 CategoryRegexs.emplace_back(Category.Regex); 1413 IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") || 1414 FileName.endswith(".cpp") || FileName.endswith(".c++") || 1415 FileName.endswith(".cxx") || FileName.endswith(".m") || 1416 FileName.endswith(".mm"); 1417 } 1418 1419 // Returns the priority of the category which \p IncludeName belongs to. 1420 // If \p CheckMainHeader is true and \p IncludeName is a main header, returns 1421 // 0. Otherwise, returns the priority of the matching category or INT_MAX. 1422 int getIncludePriority(StringRef IncludeName, bool CheckMainHeader) { 1423 int Ret = INT_MAX; 1424 for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i) 1425 if (CategoryRegexs[i].match(IncludeName)) { 1426 Ret = Style.IncludeCategories[i].Priority; 1427 break; 1428 } 1429 if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName)) 1430 Ret = 0; 1431 return Ret; 1432 } 1433 1434 private: 1435 bool isMainHeader(StringRef IncludeName) const { 1436 if (!IncludeName.startswith("\"")) 1437 return false; 1438 StringRef HeaderStem = 1439 llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1)); 1440 if (FileStem.startswith(HeaderStem)) { 1441 llvm::Regex MainIncludeRegex( 1442 (HeaderStem + Style.IncludeIsMainRegex).str()); 1443 if (MainIncludeRegex.match(FileStem)) 1444 return true; 1445 } 1446 return false; 1447 } 1448 1449 const FormatStyle &Style; 1450 bool IsMainFile; 1451 StringRef FileName; 1452 StringRef FileStem; 1453 SmallVector<llvm::Regex, 4> CategoryRegexs; 1454 }; 1455 1456 const char IncludeRegexPattern[] = 1457 R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))"; 1458 1459 } // anonymous namespace 1460 1461 tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code, 1462 ArrayRef<tooling::Range> Ranges, 1463 StringRef FileName, 1464 tooling::Replacements &Replaces, 1465 unsigned *Cursor) { 1466 unsigned Prev = 0; 1467 unsigned SearchFrom = 0; 1468 llvm::Regex IncludeRegex(IncludeRegexPattern); 1469 SmallVector<StringRef, 4> Matches; 1470 SmallVector<IncludeDirective, 16> IncludesInBlock; 1471 1472 // In compiled files, consider the first #include to be the main #include of 1473 // the file if it is not a system #include. This ensures that the header 1474 // doesn't have hidden dependencies 1475 // (http://llvm.org/docs/CodingStandards.html#include-style). 1476 // 1477 // FIXME: Do some sanity checking, e.g. edit distance of the base name, to fix 1478 // cases where the first #include is unlikely to be the main header. 1479 IncludeCategoryManager Categories(Style, FileName); 1480 bool FirstIncludeBlock = true; 1481 bool MainIncludeFound = false; 1482 bool FormattingOff = false; 1483 1484 for (;;) { 1485 auto Pos = Code.find('\n', SearchFrom); 1486 StringRef Line = 1487 Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev); 1488 1489 StringRef Trimmed = Line.trim(); 1490 if (Trimmed == "// clang-format off") 1491 FormattingOff = true; 1492 else if (Trimmed == "// clang-format on") 1493 FormattingOff = false; 1494 1495 if (!FormattingOff && !Line.endswith("\\")) { 1496 if (IncludeRegex.match(Line, &Matches)) { 1497 StringRef IncludeName = Matches[2]; 1498 int Category = Categories.getIncludePriority( 1499 IncludeName, 1500 /*CheckMainHeader=*/!MainIncludeFound && FirstIncludeBlock); 1501 if (Category == 0) 1502 MainIncludeFound = true; 1503 IncludesInBlock.push_back({IncludeName, Line, Prev, Category}); 1504 } else if (!IncludesInBlock.empty()) { 1505 sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces, 1506 Cursor); 1507 IncludesInBlock.clear(); 1508 FirstIncludeBlock = false; 1509 } 1510 Prev = Pos + 1; 1511 } 1512 if (Pos == StringRef::npos || Pos + 1 == Code.size()) 1513 break; 1514 SearchFrom = Pos + 1; 1515 } 1516 if (!IncludesInBlock.empty()) 1517 sortCppIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces, Cursor); 1518 return Replaces; 1519 } 1520 1521 bool isMpegTS(StringRef Code) { 1522 // MPEG transport streams use the ".ts" file extension. clang-format should 1523 // not attempt to format those. MPEG TS' frame format starts with 0x47 every 1524 // 189 bytes - detect that and return. 1525 return Code.size() > 188 && Code[0] == 0x47 && Code[188] == 0x47; 1526 } 1527 1528 tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, 1529 ArrayRef<tooling::Range> Ranges, 1530 StringRef FileName, unsigned *Cursor) { 1531 tooling::Replacements Replaces; 1532 if (!Style.SortIncludes) 1533 return Replaces; 1534 if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript && 1535 isMpegTS(Code)) 1536 return Replaces; 1537 if (Style.Language == FormatStyle::LanguageKind::LK_JavaScript) 1538 return sortJavaScriptImports(Style, Code, Ranges, FileName); 1539 sortCppIncludes(Style, Code, Ranges, FileName, Replaces, Cursor); 1540 return Replaces; 1541 } 1542 1543 template <typename T> 1544 static llvm::Expected<tooling::Replacements> 1545 processReplacements(T ProcessFunc, StringRef Code, 1546 const tooling::Replacements &Replaces, 1547 const FormatStyle &Style) { 1548 if (Replaces.empty()) 1549 return tooling::Replacements(); 1550 1551 auto NewCode = applyAllReplacements(Code, Replaces); 1552 if (!NewCode) 1553 return NewCode.takeError(); 1554 std::vector<tooling::Range> ChangedRanges = Replaces.getAffectedRanges(); 1555 StringRef FileName = Replaces.begin()->getFilePath(); 1556 1557 tooling::Replacements FormatReplaces = 1558 ProcessFunc(Style, *NewCode, ChangedRanges, FileName); 1559 1560 return Replaces.merge(FormatReplaces); 1561 } 1562 1563 llvm::Expected<tooling::Replacements> 1564 formatReplacements(StringRef Code, const tooling::Replacements &Replaces, 1565 const FormatStyle &Style) { 1566 // We need to use lambda function here since there are two versions of 1567 // `sortIncludes`. 1568 auto SortIncludes = [](const FormatStyle &Style, StringRef Code, 1569 std::vector<tooling::Range> Ranges, 1570 StringRef FileName) -> tooling::Replacements { 1571 return sortIncludes(Style, Code, Ranges, FileName); 1572 }; 1573 auto SortedReplaces = 1574 processReplacements(SortIncludes, Code, Replaces, Style); 1575 if (!SortedReplaces) 1576 return SortedReplaces.takeError(); 1577 1578 // We need to use lambda function here since there are two versions of 1579 // `reformat`. 1580 auto Reformat = [](const FormatStyle &Style, StringRef Code, 1581 std::vector<tooling::Range> Ranges, 1582 StringRef FileName) -> tooling::Replacements { 1583 return reformat(Style, Code, Ranges, FileName); 1584 }; 1585 return processReplacements(Reformat, Code, *SortedReplaces, Style); 1586 } 1587 1588 namespace { 1589 1590 inline bool isHeaderInsertion(const tooling::Replacement &Replace) { 1591 return Replace.getOffset() == UINT_MAX && Replace.getLength() == 0 && 1592 llvm::Regex(IncludeRegexPattern).match(Replace.getReplacementText()); 1593 } 1594 1595 inline bool isHeaderDeletion(const tooling::Replacement &Replace) { 1596 return Replace.getOffset() == UINT_MAX && Replace.getLength() == 1; 1597 } 1598 1599 // Returns the offset after skipping a sequence of tokens, matched by \p 1600 // GetOffsetAfterSequence, from the start of the code. 1601 // \p GetOffsetAfterSequence should be a function that matches a sequence of 1602 // tokens and returns an offset after the sequence. 1603 unsigned getOffsetAfterTokenSequence( 1604 StringRef FileName, StringRef Code, const FormatStyle &Style, 1605 llvm::function_ref<unsigned(const SourceManager &, Lexer &, Token &)> 1606 GetOffsetAfterSequence) { 1607 std::unique_ptr<Environment> Env = 1608 Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{}); 1609 const SourceManager &SourceMgr = Env->getSourceManager(); 1610 Lexer Lex(Env->getFileID(), SourceMgr.getBuffer(Env->getFileID()), SourceMgr, 1611 getFormattingLangOpts(Style)); 1612 Token Tok; 1613 // Get the first token. 1614 Lex.LexFromRawLexer(Tok); 1615 return GetOffsetAfterSequence(SourceMgr, Lex, Tok); 1616 } 1617 1618 // Check if a sequence of tokens is like "#<Name> <raw_identifier>". If it is, 1619 // \p Tok will be the token after this directive; otherwise, it can be any token 1620 // after the given \p Tok (including \p Tok). 1621 bool checkAndConsumeDirectiveWithName(Lexer &Lex, StringRef Name, Token &Tok) { 1622 bool Matched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) && 1623 Tok.is(tok::raw_identifier) && 1624 Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) && 1625 Tok.is(tok::raw_identifier); 1626 if (Matched) 1627 Lex.LexFromRawLexer(Tok); 1628 return Matched; 1629 } 1630 1631 void skipComments(Lexer &Lex, Token &Tok) { 1632 while (Tok.is(tok::comment)) 1633 if (Lex.LexFromRawLexer(Tok)) 1634 return; 1635 } 1636 1637 // Returns the offset after header guard directives and any comments 1638 // before/after header guards. If no header guard presents in the code, this 1639 // will returns the offset after skipping all comments from the start of the 1640 // code. 1641 unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName, 1642 StringRef Code, 1643 const FormatStyle &Style) { 1644 return getOffsetAfterTokenSequence( 1645 FileName, Code, Style, 1646 [](const SourceManager &SM, Lexer &Lex, Token Tok) { 1647 skipComments(Lex, Tok); 1648 unsigned InitialOffset = SM.getFileOffset(Tok.getLocation()); 1649 if (checkAndConsumeDirectiveWithName(Lex, "ifndef", Tok)) { 1650 skipComments(Lex, Tok); 1651 if (checkAndConsumeDirectiveWithName(Lex, "define", Tok)) 1652 return SM.getFileOffset(Tok.getLocation()); 1653 } 1654 return InitialOffset; 1655 }); 1656 } 1657 1658 // Check if a sequence of tokens is like 1659 // "#include ("header.h" | <header.h>)". 1660 // If it is, \p Tok will be the token after this directive; otherwise, it can be 1661 // any token after the given \p Tok (including \p Tok). 1662 bool checkAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) { 1663 auto Matched = [&]() { 1664 Lex.LexFromRawLexer(Tok); 1665 return true; 1666 }; 1667 if (Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) && 1668 Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "include") { 1669 if (Lex.LexFromRawLexer(Tok)) 1670 return false; 1671 if (Tok.is(tok::string_literal)) 1672 return Matched(); 1673 if (Tok.is(tok::less)) { 1674 while (!Lex.LexFromRawLexer(Tok) && Tok.isNot(tok::greater)) { 1675 } 1676 if (Tok.is(tok::greater)) 1677 return Matched(); 1678 } 1679 } 1680 return false; 1681 } 1682 1683 // Returns the offset of the last #include directive after which a new 1684 // #include can be inserted. This ignores #include's after the #include block(s) 1685 // in the beginning of a file to avoid inserting headers into code sections 1686 // where new #include's should not be added by default. 1687 // These code sections include: 1688 // - raw string literals (containing #include). 1689 // - #if blocks. 1690 // - Special #include's among declarations (e.g. functions). 1691 // 1692 // If no #include after which a new #include can be inserted, this returns the 1693 // offset after skipping all comments from the start of the code. 1694 // Inserting after an #include is not allowed if it comes after code that is not 1695 // #include (e.g. pre-processing directive that is not #include, declarations). 1696 unsigned getMaxHeaderInsertionOffset(StringRef FileName, StringRef Code, 1697 const FormatStyle &Style) { 1698 return getOffsetAfterTokenSequence( 1699 FileName, Code, Style, 1700 [](const SourceManager &SM, Lexer &Lex, Token Tok) { 1701 skipComments(Lex, Tok); 1702 unsigned MaxOffset = SM.getFileOffset(Tok.getLocation()); 1703 while (checkAndConsumeInclusiveDirective(Lex, Tok)) 1704 MaxOffset = SM.getFileOffset(Tok.getLocation()); 1705 return MaxOffset; 1706 }); 1707 } 1708 1709 bool isDeletedHeader(llvm::StringRef HeaderName, 1710 const std::set<llvm::StringRef> &HeadersToDelete) { 1711 return HeadersToDelete.count(HeaderName) || 1712 HeadersToDelete.count(HeaderName.trim("\"<>")); 1713 } 1714 1715 // FIXME: insert empty lines between newly created blocks. 1716 tooling::Replacements 1717 fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces, 1718 const FormatStyle &Style) { 1719 if (!Style.isCpp()) 1720 return Replaces; 1721 1722 tooling::Replacements HeaderInsertions; 1723 std::set<llvm::StringRef> HeadersToDelete; 1724 tooling::Replacements Result; 1725 for (const auto &R : Replaces) { 1726 if (isHeaderInsertion(R)) { 1727 // Replacements from \p Replaces must be conflict-free already, so we can 1728 // simply consume the error. 1729 llvm::consumeError(HeaderInsertions.add(R)); 1730 } else if (isHeaderDeletion(R)) { 1731 HeadersToDelete.insert(R.getReplacementText()); 1732 } else if (R.getOffset() == UINT_MAX) { 1733 llvm::errs() << "Insertions other than header #include insertion are " 1734 "not supported! " 1735 << R.getReplacementText() << "\n"; 1736 } else { 1737 llvm::consumeError(Result.add(R)); 1738 } 1739 } 1740 if (HeaderInsertions.empty() && HeadersToDelete.empty()) 1741 return Replaces; 1742 1743 llvm::Regex IncludeRegex(IncludeRegexPattern); 1744 llvm::Regex DefineRegex(R"(^[\t\ ]*#[\t\ ]*define[\t\ ]*[^\\]*$)"); 1745 SmallVector<StringRef, 4> Matches; 1746 1747 StringRef FileName = Replaces.begin()->getFilePath(); 1748 IncludeCategoryManager Categories(Style, FileName); 1749 1750 // Record the offset of the end of the last include in each category. 1751 std::map<int, int> CategoryEndOffsets; 1752 // All possible priorities. 1753 // Add 0 for main header and INT_MAX for headers that are not in any category. 1754 std::set<int> Priorities = {0, INT_MAX}; 1755 for (const auto &Category : Style.IncludeCategories) 1756 Priorities.insert(Category.Priority); 1757 int FirstIncludeOffset = -1; 1758 // All new headers should be inserted after this offset. 1759 unsigned MinInsertOffset = 1760 getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style); 1761 StringRef TrimmedCode = Code.drop_front(MinInsertOffset); 1762 // Max insertion offset in the original code. 1763 unsigned MaxInsertOffset = 1764 MinInsertOffset + 1765 getMaxHeaderInsertionOffset(FileName, TrimmedCode, Style); 1766 SmallVector<StringRef, 32> Lines; 1767 TrimmedCode.split(Lines, '\n'); 1768 unsigned Offset = MinInsertOffset; 1769 unsigned NextLineOffset; 1770 std::set<StringRef> ExistingIncludes; 1771 for (auto Line : Lines) { 1772 NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1); 1773 if (IncludeRegex.match(Line, &Matches)) { 1774 // The header name with quotes or angle brackets. 1775 StringRef IncludeName = Matches[2]; 1776 ExistingIncludes.insert(IncludeName); 1777 // Only record the offset of current #include if we can insert after it. 1778 if (Offset <= MaxInsertOffset) { 1779 int Category = Categories.getIncludePriority( 1780 IncludeName, /*CheckMainHeader=*/FirstIncludeOffset < 0); 1781 CategoryEndOffsets[Category] = NextLineOffset; 1782 if (FirstIncludeOffset < 0) 1783 FirstIncludeOffset = Offset; 1784 } 1785 if (isDeletedHeader(IncludeName, HeadersToDelete)) { 1786 // If this is the last line without trailing newline, we need to make 1787 // sure we don't delete across the file boundary. 1788 unsigned Length = std::min(Line.size() + 1, Code.size() - Offset); 1789 llvm::Error Err = 1790 Result.add(tooling::Replacement(FileName, Offset, Length, "")); 1791 if (Err) { 1792 // Ignore the deletion on conflict. 1793 llvm::errs() << "Failed to add header deletion replacement for " 1794 << IncludeName << ": " << llvm::toString(std::move(Err)) 1795 << "\n"; 1796 } 1797 } 1798 } 1799 Offset = NextLineOffset; 1800 } 1801 1802 // Populate CategoryEndOfssets: 1803 // - Ensure that CategoryEndOffset[Highest] is always populated. 1804 // - If CategoryEndOffset[Priority] isn't set, use the next higher value that 1805 // is set, up to CategoryEndOffset[Highest]. 1806 auto Highest = Priorities.begin(); 1807 if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) { 1808 if (FirstIncludeOffset >= 0) 1809 CategoryEndOffsets[*Highest] = FirstIncludeOffset; 1810 else 1811 CategoryEndOffsets[*Highest] = MinInsertOffset; 1812 } 1813 // By this point, CategoryEndOffset[Highest] is always set appropriately: 1814 // - to an appropriate location before/after existing #includes, or 1815 // - to right after the header guard, or 1816 // - to the beginning of the file. 1817 for (auto I = ++Priorities.begin(), E = Priorities.end(); I != E; ++I) 1818 if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end()) 1819 CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)]; 1820 1821 bool NeedNewLineAtEnd = !Code.empty() && Code.back() != '\n'; 1822 for (const auto &R : HeaderInsertions) { 1823 auto IncludeDirective = R.getReplacementText(); 1824 bool Matched = IncludeRegex.match(IncludeDirective, &Matches); 1825 assert(Matched && "Header insertion replacement must have replacement text " 1826 "'#include ...'"); 1827 (void)Matched; 1828 auto IncludeName = Matches[2]; 1829 if (ExistingIncludes.find(IncludeName) != ExistingIncludes.end()) { 1830 DEBUG(llvm::dbgs() << "Skip adding existing include : " << IncludeName 1831 << "\n"); 1832 continue; 1833 } 1834 int Category = 1835 Categories.getIncludePriority(IncludeName, /*CheckMainHeader=*/true); 1836 Offset = CategoryEndOffsets[Category]; 1837 std::string NewInclude = !IncludeDirective.endswith("\n") 1838 ? (IncludeDirective + "\n").str() 1839 : IncludeDirective.str(); 1840 // When inserting headers at end of the code, also append '\n' to the code 1841 // if it does not end with '\n'. 1842 if (NeedNewLineAtEnd && Offset == Code.size()) { 1843 NewInclude = "\n" + NewInclude; 1844 NeedNewLineAtEnd = false; 1845 } 1846 auto NewReplace = tooling::Replacement(FileName, Offset, 0, NewInclude); 1847 auto Err = Result.add(NewReplace); 1848 if (Err) { 1849 llvm::consumeError(std::move(Err)); 1850 unsigned NewOffset = Result.getShiftedCodePosition(Offset); 1851 NewReplace = tooling::Replacement(FileName, NewOffset, 0, NewInclude); 1852 Result = Result.merge(tooling::Replacements(NewReplace)); 1853 } 1854 } 1855 return Result; 1856 } 1857 1858 } // anonymous namespace 1859 1860 llvm::Expected<tooling::Replacements> 1861 cleanupAroundReplacements(StringRef Code, const tooling::Replacements &Replaces, 1862 const FormatStyle &Style) { 1863 // We need to use lambda function here since there are two versions of 1864 // `cleanup`. 1865 auto Cleanup = [](const FormatStyle &Style, StringRef Code, 1866 std::vector<tooling::Range> Ranges, 1867 StringRef FileName) -> tooling::Replacements { 1868 return cleanup(Style, Code, Ranges, FileName); 1869 }; 1870 // Make header insertion replacements insert new headers into correct blocks. 1871 tooling::Replacements NewReplaces = 1872 fixCppIncludeInsertions(Code, Replaces, Style); 1873 return processReplacements(Cleanup, Code, NewReplaces, Style); 1874 } 1875 1876 tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, 1877 ArrayRef<tooling::Range> Ranges, 1878 StringRef FileName, 1879 FormattingAttemptStatus *Status) { 1880 FormatStyle Expanded = expandPresets(Style); 1881 if (Expanded.DisableFormat) 1882 return tooling::Replacements(); 1883 if (Expanded.Language == FormatStyle::LK_JavaScript && isMpegTS(Code)) 1884 return tooling::Replacements(); 1885 1886 typedef std::function<tooling::Replacements(const Environment &)> 1887 AnalyzerPass; 1888 SmallVector<AnalyzerPass, 4> Passes; 1889 1890 if (Style.Language == FormatStyle::LK_Cpp) { 1891 if (Style.FixNamespaceComments) 1892 Passes.emplace_back([&](const Environment &Env) { 1893 return NamespaceEndCommentsFixer(Env, Expanded).process(); 1894 }); 1895 1896 if (Style.SortUsingDeclarations) 1897 Passes.emplace_back([&](const Environment &Env) { 1898 return UsingDeclarationsSorter(Env, Expanded).process(); 1899 }); 1900 } 1901 1902 if (Style.Language == FormatStyle::LK_JavaScript && 1903 Style.JavaScriptQuotes != FormatStyle::JSQS_Leave) 1904 Passes.emplace_back([&](const Environment &Env) { 1905 return JavaScriptRequoter(Env, Expanded).process(); 1906 }); 1907 1908 Passes.emplace_back([&](const Environment &Env) { 1909 return Formatter(Env, Expanded, Status).process(); 1910 }); 1911 1912 std::unique_ptr<Environment> Env = 1913 Environment::CreateVirtualEnvironment(Code, FileName, Ranges); 1914 llvm::Optional<std::string> CurrentCode = None; 1915 tooling::Replacements Fixes; 1916 for (size_t I = 0, E = Passes.size(); I < E; ++I) { 1917 tooling::Replacements PassFixes = Passes[I](*Env); 1918 auto NewCode = applyAllReplacements( 1919 CurrentCode ? StringRef(*CurrentCode) : Code, PassFixes); 1920 if (NewCode) { 1921 Fixes = Fixes.merge(PassFixes); 1922 if (I + 1 < E) { 1923 CurrentCode = std::move(*NewCode); 1924 Env = Environment::CreateVirtualEnvironment( 1925 *CurrentCode, FileName, 1926 tooling::calculateRangesAfterReplacements(Fixes, Ranges)); 1927 } 1928 } 1929 } 1930 1931 return Fixes; 1932 } 1933 1934 tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, 1935 ArrayRef<tooling::Range> Ranges, 1936 StringRef FileName) { 1937 // cleanups only apply to C++ (they mostly concern ctor commas etc.) 1938 if (Style.Language != FormatStyle::LK_Cpp) 1939 return tooling::Replacements(); 1940 std::unique_ptr<Environment> Env = 1941 Environment::CreateVirtualEnvironment(Code, FileName, Ranges); 1942 Cleaner Clean(*Env, Style); 1943 return Clean.process(); 1944 } 1945 1946 tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, 1947 ArrayRef<tooling::Range> Ranges, 1948 StringRef FileName, bool *IncompleteFormat) { 1949 FormattingAttemptStatus Status; 1950 auto Result = reformat(Style, Code, Ranges, FileName, &Status); 1951 if (!Status.FormatComplete) 1952 *IncompleteFormat = true; 1953 return Result; 1954 } 1955 1956 tooling::Replacements fixNamespaceEndComments(const FormatStyle &Style, 1957 StringRef Code, 1958 ArrayRef<tooling::Range> Ranges, 1959 StringRef FileName) { 1960 std::unique_ptr<Environment> Env = 1961 Environment::CreateVirtualEnvironment(Code, FileName, Ranges); 1962 NamespaceEndCommentsFixer Fix(*Env, Style); 1963 return Fix.process(); 1964 } 1965 1966 tooling::Replacements sortUsingDeclarations(const FormatStyle &Style, 1967 StringRef Code, 1968 ArrayRef<tooling::Range> Ranges, 1969 StringRef FileName) { 1970 std::unique_ptr<Environment> Env = 1971 Environment::CreateVirtualEnvironment(Code, FileName, Ranges); 1972 UsingDeclarationsSorter Sorter(*Env, Style); 1973 return Sorter.process(); 1974 } 1975 1976 LangOptions getFormattingLangOpts(const FormatStyle &Style) { 1977 LangOptions LangOpts; 1978 LangOpts.CPlusPlus = 1; 1979 LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; 1980 LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; 1981 LangOpts.CPlusPlus1z = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; 1982 LangOpts.LineComment = 1; 1983 bool AlternativeOperators = Style.isCpp(); 1984 LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0; 1985 LangOpts.Bool = 1; 1986 LangOpts.ObjC1 = 1; 1987 LangOpts.ObjC2 = 1; 1988 LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally. 1989 LangOpts.DeclSpecKeyword = 1; // To get __declspec. 1990 return LangOpts; 1991 } 1992 1993 const char *StyleOptionHelpDescription = 1994 "Coding style, currently supports:\n" 1995 " LLVM, Google, Chromium, Mozilla, WebKit.\n" 1996 "Use -style=file to load style configuration from\n" 1997 ".clang-format file located in one of the parent\n" 1998 "directories of the source file (or current\n" 1999 "directory for stdin).\n" 2000 "Use -style=\"{key: value, ...}\" to set specific\n" 2001 "parameters, e.g.:\n" 2002 " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""; 2003 2004 static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) { 2005 if (FileName.endswith(".java")) 2006 return FormatStyle::LK_Java; 2007 if (FileName.endswith_lower(".js") || FileName.endswith_lower(".ts")) 2008 return FormatStyle::LK_JavaScript; // JavaScript or TypeScript. 2009 if (FileName.endswith(".m") || FileName.endswith(".mm")) 2010 return FormatStyle::LK_ObjC; 2011 if (FileName.endswith_lower(".proto") || 2012 FileName.endswith_lower(".protodevel")) 2013 return FormatStyle::LK_Proto; 2014 if (FileName.endswith_lower(".td")) 2015 return FormatStyle::LK_TableGen; 2016 return FormatStyle::LK_Cpp; 2017 } 2018 2019 llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, 2020 StringRef FallbackStyleName, 2021 StringRef Code, vfs::FileSystem *FS) { 2022 if (!FS) { 2023 FS = vfs::getRealFileSystem().get(); 2024 } 2025 FormatStyle Style = getLLVMStyle(); 2026 Style.Language = getLanguageByFileName(FileName); 2027 2028 // This is a very crude detection of whether a header contains ObjC code that 2029 // should be improved over time and probably be done on tokens, not one the 2030 // bare content of the file. 2031 if (Style.Language == FormatStyle::LK_Cpp && FileName.endswith(".h") && 2032 (Code.contains("\n- (") || Code.contains("\n+ ("))) 2033 Style.Language = FormatStyle::LK_ObjC; 2034 2035 FormatStyle FallbackStyle = getNoStyle(); 2036 if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle)) 2037 return make_string_error("Invalid fallback style \"" + FallbackStyleName); 2038 2039 if (StyleName.startswith("{")) { 2040 // Parse YAML/JSON style from the command line. 2041 if (std::error_code ec = parseConfiguration(StyleName, &Style)) 2042 return make_string_error("Error parsing -style: " + ec.message()); 2043 return Style; 2044 } 2045 2046 if (!StyleName.equals_lower("file")) { 2047 if (!getPredefinedStyle(StyleName, Style.Language, &Style)) 2048 return make_string_error("Invalid value for -style"); 2049 return Style; 2050 } 2051 2052 // Look for .clang-format/_clang-format file in the file's parent directories. 2053 SmallString<128> UnsuitableConfigFiles; 2054 SmallString<128> Path(FileName); 2055 if (std::error_code EC = FS->makeAbsolute(Path)) 2056 return make_string_error(EC.message()); 2057 2058 for (StringRef Directory = Path; !Directory.empty(); 2059 Directory = llvm::sys::path::parent_path(Directory)) { 2060 2061 auto Status = FS->status(Directory); 2062 if (!Status || 2063 Status->getType() != llvm::sys::fs::file_type::directory_file) { 2064 continue; 2065 } 2066 2067 SmallString<128> ConfigFile(Directory); 2068 2069 llvm::sys::path::append(ConfigFile, ".clang-format"); 2070 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); 2071 2072 Status = FS->status(ConfigFile.str()); 2073 bool FoundConfigFile = 2074 Status && (Status->getType() == llvm::sys::fs::file_type::regular_file); 2075 if (!FoundConfigFile) { 2076 // Try _clang-format too, since dotfiles are not commonly used on Windows. 2077 ConfigFile = Directory; 2078 llvm::sys::path::append(ConfigFile, "_clang-format"); 2079 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); 2080 Status = FS->status(ConfigFile.str()); 2081 FoundConfigFile = Status && (Status->getType() == 2082 llvm::sys::fs::file_type::regular_file); 2083 } 2084 2085 if (FoundConfigFile) { 2086 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = 2087 FS->getBufferForFile(ConfigFile.str()); 2088 if (std::error_code EC = Text.getError()) 2089 return make_string_error(EC.message()); 2090 if (std::error_code ec = 2091 parseConfiguration(Text.get()->getBuffer(), &Style)) { 2092 if (ec == ParseError::Unsuitable) { 2093 if (!UnsuitableConfigFiles.empty()) 2094 UnsuitableConfigFiles.append(", "); 2095 UnsuitableConfigFiles.append(ConfigFile); 2096 continue; 2097 } 2098 return make_string_error("Error reading " + ConfigFile + ": " + 2099 ec.message()); 2100 } 2101 DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n"); 2102 return Style; 2103 } 2104 } 2105 if (!UnsuitableConfigFiles.empty()) 2106 return make_string_error("Configuration file(s) do(es) not support " + 2107 getLanguageName(Style.Language) + ": " + 2108 UnsuitableConfigFiles); 2109 return FallbackStyle; 2110 } 2111 2112 } // namespace format 2113 } // namespace clang 2114