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 #define DEBUG_TYPE "format-formatter" 17 18 #include "ContinuationIndenter.h" 19 #include "TokenAnnotator.h" 20 #include "UnwrappedLineParser.h" 21 #include "WhitespaceManager.h" 22 #include "clang/Basic/Diagnostic.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/YAMLTraits.h" 30 #include "llvm/Support/Path.h" 31 #include <queue> 32 #include <string> 33 34 using clang::format::FormatStyle; 35 36 namespace llvm { 37 namespace yaml { 38 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> { 39 static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) { 40 IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp); 41 IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript); 42 } 43 }; 44 45 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> { 46 static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) { 47 IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03); 48 IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03); 49 IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11); 50 IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11); 51 IO.enumCase(Value, "Auto", FormatStyle::LS_Auto); 52 } 53 }; 54 55 template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> { 56 static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) { 57 IO.enumCase(Value, "Never", FormatStyle::UT_Never); 58 IO.enumCase(Value, "false", FormatStyle::UT_Never); 59 IO.enumCase(Value, "Always", FormatStyle::UT_Always); 60 IO.enumCase(Value, "true", FormatStyle::UT_Always); 61 IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation); 62 } 63 }; 64 65 template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> { 66 static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) { 67 IO.enumCase(Value, "Attach", FormatStyle::BS_Attach); 68 IO.enumCase(Value, "Linux", FormatStyle::BS_Linux); 69 IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup); 70 IO.enumCase(Value, "Allman", FormatStyle::BS_Allman); 71 IO.enumCase(Value, "GNU", FormatStyle::BS_GNU); 72 } 73 }; 74 75 template <> 76 struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> { 77 static void enumeration(IO &IO, 78 FormatStyle::NamespaceIndentationKind &Value) { 79 IO.enumCase(Value, "None", FormatStyle::NI_None); 80 IO.enumCase(Value, "Inner", FormatStyle::NI_Inner); 81 IO.enumCase(Value, "All", FormatStyle::NI_All); 82 } 83 }; 84 85 template <> 86 struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> { 87 static void enumeration(IO &IO, 88 FormatStyle::SpaceBeforeParensOptions &Value) { 89 IO.enumCase(Value, "Never", FormatStyle::SBPO_Never); 90 IO.enumCase(Value, "ControlStatements", 91 FormatStyle::SBPO_ControlStatements); 92 IO.enumCase(Value, "Always", FormatStyle::SBPO_Always); 93 94 // For backward compatibility. 95 IO.enumCase(Value, "false", FormatStyle::SBPO_Never); 96 IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements); 97 } 98 }; 99 100 template <> struct MappingTraits<FormatStyle> { 101 static void mapping(IO &IO, FormatStyle &Style) { 102 // When reading, read the language first, we need it for getPredefinedStyle. 103 IO.mapOptional("Language", Style.Language); 104 105 if (IO.outputting()) { 106 StringRef StylesArray[] = { "LLVM", "Google", "Chromium", 107 "Mozilla", "WebKit", "GNU" }; 108 ArrayRef<StringRef> Styles(StylesArray); 109 for (size_t i = 0, e = Styles.size(); i < e; ++i) { 110 StringRef StyleName(Styles[i]); 111 FormatStyle PredefinedStyle; 112 if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) && 113 Style == PredefinedStyle) { 114 IO.mapOptional("# BasedOnStyle", StyleName); 115 break; 116 } 117 } 118 } else { 119 StringRef BasedOnStyle; 120 IO.mapOptional("BasedOnStyle", BasedOnStyle); 121 if (!BasedOnStyle.empty()) { 122 FormatStyle::LanguageKind OldLanguage = Style.Language; 123 FormatStyle::LanguageKind Language = 124 ((FormatStyle *)IO.getContext())->Language; 125 if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) { 126 IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle)); 127 return; 128 } 129 Style.Language = OldLanguage; 130 } 131 } 132 133 IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); 134 IO.mapOptional("ConstructorInitializerIndentWidth", 135 Style.ConstructorInitializerIndentWidth); 136 IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); 137 IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); 138 IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", 139 Style.AllowAllParametersOfDeclarationOnNextLine); 140 IO.mapOptional("AllowShortIfStatementsOnASingleLine", 141 Style.AllowShortIfStatementsOnASingleLine); 142 IO.mapOptional("AllowShortLoopsOnASingleLine", 143 Style.AllowShortLoopsOnASingleLine); 144 IO.mapOptional("AllowShortFunctionsOnASingleLine", 145 Style.AllowShortFunctionsOnASingleLine); 146 IO.mapOptional("AlwaysBreakTemplateDeclarations", 147 Style.AlwaysBreakTemplateDeclarations); 148 IO.mapOptional("AlwaysBreakBeforeMultilineStrings", 149 Style.AlwaysBreakBeforeMultilineStrings); 150 IO.mapOptional("BreakBeforeBinaryOperators", 151 Style.BreakBeforeBinaryOperators); 152 IO.mapOptional("BreakBeforeTernaryOperators", 153 Style.BreakBeforeTernaryOperators); 154 IO.mapOptional("BreakConstructorInitializersBeforeComma", 155 Style.BreakConstructorInitializersBeforeComma); 156 IO.mapOptional("BinPackParameters", Style.BinPackParameters); 157 IO.mapOptional("ColumnLimit", Style.ColumnLimit); 158 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", 159 Style.ConstructorInitializerAllOnOneLineOrOnePerLine); 160 IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding); 161 IO.mapOptional("ExperimentalAutoDetectBinPacking", 162 Style.ExperimentalAutoDetectBinPacking); 163 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); 164 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); 165 IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); 166 IO.mapOptional("ObjCSpaceBeforeProtocolList", 167 Style.ObjCSpaceBeforeProtocolList); 168 IO.mapOptional("PenaltyBreakBeforeFirstCallParameter", 169 Style.PenaltyBreakBeforeFirstCallParameter); 170 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); 171 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); 172 IO.mapOptional("PenaltyBreakFirstLessLess", 173 Style.PenaltyBreakFirstLessLess); 174 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); 175 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", 176 Style.PenaltyReturnTypeOnItsOwnLine); 177 IO.mapOptional("PointerBindsToType", Style.PointerBindsToType); 178 IO.mapOptional("SpacesBeforeTrailingComments", 179 Style.SpacesBeforeTrailingComments); 180 IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); 181 IO.mapOptional("Standard", Style.Standard); 182 IO.mapOptional("IndentWidth", Style.IndentWidth); 183 IO.mapOptional("TabWidth", Style.TabWidth); 184 IO.mapOptional("UseTab", Style.UseTab); 185 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); 186 IO.mapOptional("IndentFunctionDeclarationAfterType", 187 Style.IndentFunctionDeclarationAfterType); 188 IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); 189 IO.mapOptional("SpacesInAngles", Style.SpacesInAngles); 190 IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); 191 IO.mapOptional("SpacesInCStyleCastParentheses", 192 Style.SpacesInCStyleCastParentheses); 193 IO.mapOptional("SpaceBeforeAssignmentOperators", 194 Style.SpaceBeforeAssignmentOperators); 195 IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth); 196 197 // For backward compatibility. 198 if (!IO.outputting()) { 199 IO.mapOptional("SpaceAfterControlStatementKeyword", 200 Style.SpaceBeforeParens); 201 } 202 IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens); 203 } 204 }; 205 206 // Allows to read vector<FormatStyle> while keeping default values. 207 // IO.getContext() should contain a pointer to the FormatStyle structure, that 208 // will be used to get default values for missing keys. 209 // If the first element has no Language specified, it will be treated as the 210 // default one for the following elements. 211 template <> struct DocumentListTraits<std::vector<FormatStyle> > { 212 static size_t size(IO &IO, std::vector<FormatStyle> &Seq) { 213 return Seq.size(); 214 } 215 static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq, 216 size_t Index) { 217 if (Index >= Seq.size()) { 218 assert(Index == Seq.size()); 219 FormatStyle Template; 220 if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) { 221 Template = Seq[0]; 222 } else { 223 Template = *((const FormatStyle*)IO.getContext()); 224 Template.Language = FormatStyle::LK_None; 225 } 226 Seq.resize(Index + 1, Template); 227 } 228 return Seq[Index]; 229 } 230 }; 231 } 232 } 233 234 namespace clang { 235 namespace format { 236 237 FormatStyle getLLVMStyle() { 238 FormatStyle LLVMStyle; 239 LLVMStyle.Language = FormatStyle::LK_Cpp; 240 LLVMStyle.AccessModifierOffset = -2; 241 LLVMStyle.AlignEscapedNewlinesLeft = false; 242 LLVMStyle.AlignTrailingComments = true; 243 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; 244 LLVMStyle.AllowShortFunctionsOnASingleLine = true; 245 LLVMStyle.AllowShortIfStatementsOnASingleLine = false; 246 LLVMStyle.AllowShortLoopsOnASingleLine = false; 247 LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; 248 LLVMStyle.AlwaysBreakTemplateDeclarations = false; 249 LLVMStyle.BinPackParameters = true; 250 LLVMStyle.BreakBeforeBinaryOperators = false; 251 LLVMStyle.BreakBeforeTernaryOperators = true; 252 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; 253 LLVMStyle.BreakConstructorInitializersBeforeComma = false; 254 LLVMStyle.ColumnLimit = 80; 255 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 256 LLVMStyle.ConstructorInitializerIndentWidth = 4; 257 LLVMStyle.Cpp11BracedListStyle = false; 258 LLVMStyle.DerivePointerBinding = false; 259 LLVMStyle.ExperimentalAutoDetectBinPacking = false; 260 LLVMStyle.IndentCaseLabels = false; 261 LLVMStyle.IndentFunctionDeclarationAfterType = false; 262 LLVMStyle.IndentWidth = 2; 263 LLVMStyle.TabWidth = 8; 264 LLVMStyle.MaxEmptyLinesToKeep = 1; 265 LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; 266 LLVMStyle.ObjCSpaceBeforeProtocolList = true; 267 LLVMStyle.PointerBindsToType = false; 268 LLVMStyle.SpacesBeforeTrailingComments = 1; 269 LLVMStyle.Standard = FormatStyle::LS_Cpp03; 270 LLVMStyle.UseTab = FormatStyle::UT_Never; 271 LLVMStyle.SpacesInParentheses = false; 272 LLVMStyle.SpaceInEmptyParentheses = false; 273 LLVMStyle.SpacesInCStyleCastParentheses = false; 274 LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; 275 LLVMStyle.SpaceBeforeAssignmentOperators = true; 276 LLVMStyle.ContinuationIndentWidth = 4; 277 LLVMStyle.SpacesInAngles = false; 278 279 LLVMStyle.PenaltyBreakComment = 300; 280 LLVMStyle.PenaltyBreakFirstLessLess = 120; 281 LLVMStyle.PenaltyBreakString = 1000; 282 LLVMStyle.PenaltyExcessCharacter = 1000000; 283 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; 284 LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19; 285 286 return LLVMStyle; 287 } 288 289 FormatStyle getGoogleStyle() { 290 FormatStyle GoogleStyle = getLLVMStyle(); 291 GoogleStyle.AccessModifierOffset = -1; 292 GoogleStyle.AlignEscapedNewlinesLeft = true; 293 GoogleStyle.AllowShortIfStatementsOnASingleLine = true; 294 GoogleStyle.AllowShortLoopsOnASingleLine = true; 295 GoogleStyle.AlwaysBreakBeforeMultilineStrings = true; 296 GoogleStyle.AlwaysBreakTemplateDeclarations = true; 297 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 298 GoogleStyle.Cpp11BracedListStyle = true; 299 GoogleStyle.DerivePointerBinding = true; 300 GoogleStyle.IndentCaseLabels = true; 301 GoogleStyle.IndentFunctionDeclarationAfterType = true; 302 GoogleStyle.ObjCSpaceBeforeProtocolList = false; 303 GoogleStyle.PointerBindsToType = true; 304 GoogleStyle.SpacesBeforeTrailingComments = 2; 305 GoogleStyle.Standard = FormatStyle::LS_Auto; 306 307 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; 308 GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1; 309 310 return GoogleStyle; 311 } 312 313 FormatStyle getGoogleJSStyle() { 314 FormatStyle GoogleJSStyle = getGoogleStyle(); 315 GoogleJSStyle.Language = FormatStyle::LK_JavaScript; 316 GoogleJSStyle.BreakBeforeTernaryOperators = false; 317 // FIXME: Currently unimplemented: 318 // var arr = [1, 2, 3]; // No space after [ or before ]. 319 // var obj = {a: 1, b: 2, c: 3}; // No space after ':'. 320 return GoogleJSStyle; 321 } 322 323 FormatStyle getChromiumStyle() { 324 FormatStyle ChromiumStyle = getGoogleStyle(); 325 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; 326 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; 327 ChromiumStyle.AllowShortLoopsOnASingleLine = false; 328 ChromiumStyle.BinPackParameters = false; 329 ChromiumStyle.DerivePointerBinding = false; 330 ChromiumStyle.Standard = FormatStyle::LS_Cpp03; 331 return ChromiumStyle; 332 } 333 334 FormatStyle getMozillaStyle() { 335 FormatStyle MozillaStyle = getLLVMStyle(); 336 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; 337 MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 338 MozillaStyle.DerivePointerBinding = true; 339 MozillaStyle.IndentCaseLabels = true; 340 MozillaStyle.ObjCSpaceBeforeProtocolList = false; 341 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; 342 MozillaStyle.PointerBindsToType = true; 343 return MozillaStyle; 344 } 345 346 FormatStyle getWebKitStyle() { 347 FormatStyle Style = getLLVMStyle(); 348 Style.AccessModifierOffset = -4; 349 Style.AlignTrailingComments = false; 350 Style.BreakBeforeBinaryOperators = true; 351 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 352 Style.BreakConstructorInitializersBeforeComma = true; 353 Style.ColumnLimit = 0; 354 Style.IndentWidth = 4; 355 Style.NamespaceIndentation = FormatStyle::NI_Inner; 356 Style.PointerBindsToType = true; 357 return Style; 358 } 359 360 FormatStyle getGNUStyle() { 361 FormatStyle Style = getLLVMStyle(); 362 Style.BreakBeforeBinaryOperators = true; 363 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 364 Style.BreakBeforeTernaryOperators = true; 365 Style.ColumnLimit = 79; 366 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 367 return Style; 368 } 369 370 bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language, 371 FormatStyle *Style) { 372 if (Name.equals_lower("llvm")) { 373 *Style = getLLVMStyle(); 374 } else if (Name.equals_lower("chromium")) { 375 *Style = getChromiumStyle(); 376 } else if (Name.equals_lower("mozilla")) { 377 *Style = getMozillaStyle(); 378 } else if (Name.equals_lower("google")) { 379 *Style = Language == FormatStyle::LK_JavaScript ? getGoogleJSStyle() 380 : getGoogleStyle(); 381 } else if (Name.equals_lower("webkit")) { 382 *Style = getWebKitStyle(); 383 } else if (Name.equals_lower("gnu")) { 384 *Style = getGNUStyle(); 385 } else { 386 return false; 387 } 388 389 Style->Language = Language; 390 return true; 391 } 392 393 llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { 394 assert(Style); 395 FormatStyle::LanguageKind Language = Style->Language; 396 assert(Language != FormatStyle::LK_None); 397 if (Text.trim().empty()) 398 return llvm::make_error_code(llvm::errc::invalid_argument); 399 400 std::vector<FormatStyle> Styles; 401 llvm::yaml::Input Input(Text); 402 // DocumentListTraits<vector<FormatStyle>> uses the context to get default 403 // values for the fields, keys for which are missing from the configuration. 404 // Mapping also uses the context to get the language to find the correct 405 // base style. 406 Input.setContext(Style); 407 Input >> Styles; 408 if (Input.error()) 409 return Input.error(); 410 411 for (unsigned i = 0; i < Styles.size(); ++i) { 412 // Ensures that only the first configuration can skip the Language option. 413 if (Styles[i].Language == FormatStyle::LK_None && i != 0) 414 return llvm::make_error_code(llvm::errc::invalid_argument); 415 // Ensure that each language is configured at most once. 416 for (unsigned j = 0; j < i; ++j) { 417 if (Styles[i].Language == Styles[j].Language) { 418 DEBUG(llvm::dbgs() 419 << "Duplicate languages in the config file on positions " << j 420 << " and " << i << "\n"); 421 return llvm::make_error_code(llvm::errc::invalid_argument); 422 } 423 } 424 } 425 // Look for a suitable configuration starting from the end, so we can 426 // find the configuration for the specific language first, and the default 427 // configuration (which can only be at slot 0) after it. 428 for (int i = Styles.size() - 1; i >= 0; --i) { 429 if (Styles[i].Language == Language || 430 Styles[i].Language == FormatStyle::LK_None) { 431 *Style = Styles[i]; 432 Style->Language = Language; 433 return llvm::make_error_code(llvm::errc::success); 434 } 435 } 436 return llvm::make_error_code(llvm::errc::not_supported); 437 } 438 439 std::string configurationAsText(const FormatStyle &Style) { 440 std::string Text; 441 llvm::raw_string_ostream Stream(Text); 442 llvm::yaml::Output Output(Stream); 443 // We use the same mapping method for input and output, so we need a non-const 444 // reference here. 445 FormatStyle NonConstStyle = Style; 446 Output << NonConstStyle; 447 return Stream.str(); 448 } 449 450 namespace { 451 452 class NoColumnLimitFormatter { 453 public: 454 NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {} 455 456 /// \brief Formats the line starting at \p State, simply keeping all of the 457 /// input's line breaking decisions. 458 void format(unsigned FirstIndent, const AnnotatedLine *Line) { 459 LineState State = 460 Indenter->getInitialState(FirstIndent, Line, /*DryRun=*/false); 461 while (State.NextToken != NULL) { 462 bool Newline = 463 Indenter->mustBreak(State) || 464 (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0); 465 Indenter->addTokenToState(State, Newline, /*DryRun=*/false); 466 } 467 } 468 469 private: 470 ContinuationIndenter *Indenter; 471 }; 472 473 class LineJoiner { 474 public: 475 LineJoiner(const FormatStyle &Style) : Style(Style) {} 476 477 /// \brief Calculates how many lines can be merged into 1 starting at \p I. 478 unsigned 479 tryFitMultipleLinesInOne(unsigned Indent, 480 SmallVectorImpl<AnnotatedLine *>::const_iterator I, 481 SmallVectorImpl<AnnotatedLine *>::const_iterator E) { 482 // We can never merge stuff if there are trailing line comments. 483 AnnotatedLine *TheLine = *I; 484 if (TheLine->Last->Type == TT_LineComment) 485 return 0; 486 487 if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit) 488 return 0; 489 490 unsigned Limit = 491 Style.ColumnLimit == 0 ? UINT_MAX : Style.ColumnLimit - Indent; 492 // If we already exceed the column limit, we set 'Limit' to 0. The different 493 // tryMerge..() functions can then decide whether to still do merging. 494 Limit = TheLine->Last->TotalLength > Limit 495 ? 0 496 : Limit - TheLine->Last->TotalLength; 497 498 if (I + 1 == E || I[1]->Type == LT_Invalid) 499 return 0; 500 501 if (TheLine->Last->Type == TT_FunctionLBrace) { 502 return Style.AllowShortFunctionsOnASingleLine 503 ? tryMergeSimpleBlock(I, E, Limit) 504 : 0; 505 } 506 if (TheLine->Last->is(tok::l_brace)) { 507 return Style.BreakBeforeBraces == FormatStyle::BS_Attach 508 ? tryMergeSimpleBlock(I, E, Limit) 509 : 0; 510 } 511 if (I[1]->First->Type == TT_FunctionLBrace && 512 Style.BreakBeforeBraces != FormatStyle::BS_Attach) { 513 // Reduce the column limit by the number of spaces we need to insert 514 // around braces. 515 Limit = Limit > 3 ? Limit - 3 : 0; 516 unsigned MergedLines = 0; 517 if (Style.AllowShortFunctionsOnASingleLine) { 518 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit); 519 // If we managed to merge the block, count the function header, which is 520 // on a separate line. 521 if (MergedLines > 0) 522 ++MergedLines; 523 } 524 return MergedLines; 525 } 526 if (TheLine->First->is(tok::kw_if)) { 527 return Style.AllowShortIfStatementsOnASingleLine 528 ? tryMergeSimpleControlStatement(I, E, Limit) 529 : 0; 530 } 531 if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) { 532 return Style.AllowShortLoopsOnASingleLine 533 ? tryMergeSimpleControlStatement(I, E, Limit) 534 : 0; 535 } 536 if (TheLine->InPPDirective && 537 (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) { 538 return tryMergeSimplePPDirective(I, E, Limit); 539 } 540 return 0; 541 } 542 543 private: 544 unsigned 545 tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 546 SmallVectorImpl<AnnotatedLine *>::const_iterator E, 547 unsigned Limit) { 548 if (Limit == 0) 549 return 0; 550 if (!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline) 551 return 0; 552 if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline) 553 return 0; 554 if (1 + I[1]->Last->TotalLength > Limit) 555 return 0; 556 return 1; 557 } 558 559 unsigned tryMergeSimpleControlStatement( 560 SmallVectorImpl<AnnotatedLine *>::const_iterator I, 561 SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) { 562 if (Limit == 0) 563 return 0; 564 if ((Style.BreakBeforeBraces == FormatStyle::BS_Allman || 565 Style.BreakBeforeBraces == FormatStyle::BS_GNU) && 566 I[1]->First->is(tok::l_brace)) 567 return 0; 568 if (I[1]->InPPDirective != (*I)->InPPDirective || 569 (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) 570 return 0; 571 AnnotatedLine &Line = **I; 572 if (Line.Last->isNot(tok::r_paren)) 573 return 0; 574 if (1 + I[1]->Last->TotalLength > Limit) 575 return 0; 576 if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, 577 tok::kw_while) || 578 I[1]->First->Type == TT_LineComment) 579 return 0; 580 // Only inline simple if's (no nested if or else). 581 if (I + 2 != E && Line.First->is(tok::kw_if) && 582 I[2]->First->is(tok::kw_else)) 583 return 0; 584 return 1; 585 } 586 587 unsigned 588 tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 589 SmallVectorImpl<AnnotatedLine *>::const_iterator E, 590 unsigned Limit) { 591 // First, check that the current line allows merging. This is the case if 592 // we're not in a control flow statement and the last token is an opening 593 // brace. 594 AnnotatedLine &Line = **I; 595 if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace, 596 tok::kw_else, tok::kw_try, tok::kw_catch, 597 tok::kw_for, 598 // This gets rid of all ObjC @ keywords and methods. 599 tok::at, tok::minus, tok::plus)) 600 return 0; 601 602 FormatToken *Tok = I[1]->First; 603 if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore && 604 (Tok->getNextNonComment() == NULL || 605 Tok->getNextNonComment()->is(tok::semi))) { 606 // We merge empty blocks even if the line exceeds the column limit. 607 Tok->SpacesRequiredBefore = 0; 608 Tok->CanBreakBefore = true; 609 return 1; 610 } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) { 611 // Check that we still have three lines and they fit into the limit. 612 if (I + 2 == E || I[2]->Type == LT_Invalid) 613 return 0; 614 615 if (!nextTwoLinesFitInto(I, Limit)) 616 return 0; 617 618 // Second, check that the next line does not contain any braces - if it 619 // does, readability declines when putting it into a single line. 620 if (I[1]->Last->Type == TT_LineComment || Tok->MustBreakBefore) 621 return 0; 622 do { 623 if (Tok->isOneOf(tok::l_brace, tok::r_brace)) 624 return 0; 625 Tok = Tok->Next; 626 } while (Tok != NULL); 627 628 // Last, check that the third line contains a single closing brace. 629 Tok = I[2]->First; 630 if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) || 631 Tok->MustBreakBefore) 632 return 0; 633 634 return 2; 635 } 636 return 0; 637 } 638 639 bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 640 unsigned Limit) { 641 return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit; 642 } 643 644 const FormatStyle &Style; 645 }; 646 647 class UnwrappedLineFormatter { 648 public: 649 UnwrappedLineFormatter(ContinuationIndenter *Indenter, 650 WhitespaceManager *Whitespaces, 651 const FormatStyle &Style) 652 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), 653 Joiner(Style) {} 654 655 unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun, 656 int AdditionalIndent = 0, bool FixBadIndentation = false) { 657 assert(!Lines.empty()); 658 unsigned Penalty = 0; 659 std::vector<int> IndentForLevel; 660 for (unsigned i = 0, e = Lines[0]->Level; i != e; ++i) 661 IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent); 662 const AnnotatedLine *PreviousLine = NULL; 663 for (SmallVectorImpl<AnnotatedLine *>::const_iterator I = Lines.begin(), 664 E = Lines.end(); 665 I != E; ++I) { 666 const AnnotatedLine &TheLine = **I; 667 const FormatToken *FirstTok = TheLine.First; 668 int Offset = getIndentOffset(*FirstTok); 669 670 // Determine indent and try to merge multiple unwrapped lines. 671 unsigned Indent; 672 if (TheLine.InPPDirective) { 673 Indent = TheLine.Level * Style.IndentWidth; 674 } else { 675 while (IndentForLevel.size() <= TheLine.Level) 676 IndentForLevel.push_back(-1); 677 IndentForLevel.resize(TheLine.Level + 1); 678 Indent = getIndent(IndentForLevel, TheLine.Level); 679 } 680 unsigned LevelIndent = Indent; 681 if (static_cast<int>(Indent) + Offset >= 0) 682 Indent += Offset; 683 684 // Merge multiple lines if possible. 685 unsigned MergedLines = Joiner.tryFitMultipleLinesInOne(Indent, I, E); 686 if (MergedLines > 0 && Style.ColumnLimit == 0) { 687 // Disallow line merging if there is a break at the start of one of the 688 // input lines. 689 for (unsigned i = 0; i < MergedLines; ++i) { 690 if (I[i + 1]->First->NewlinesBefore > 0) 691 MergedLines = 0; 692 } 693 } 694 if (!DryRun) { 695 for (unsigned i = 0; i < MergedLines; ++i) { 696 join(*I[i], *I[i + 1]); 697 } 698 } 699 I += MergedLines; 700 701 bool FixIndentation = 702 FixBadIndentation && (LevelIndent != FirstTok->OriginalColumn); 703 if (TheLine.First->is(tok::eof)) { 704 if (PreviousLine && PreviousLine->Affected && !DryRun) { 705 // Remove the file's trailing whitespace. 706 unsigned Newlines = std::min(FirstTok->NewlinesBefore, 1u); 707 Whitespaces->replaceWhitespace(*TheLine.First, Newlines, 708 /*IndentLevel=*/0, /*Spaces=*/0, 709 /*TargetColumn=*/0); 710 } 711 } else if (TheLine.Type != LT_Invalid && 712 (TheLine.Affected || FixIndentation)) { 713 if (FirstTok->WhitespaceRange.isValid()) { 714 if (!DryRun) 715 formatFirstToken(*TheLine.First, PreviousLine, TheLine.Level, 716 Indent, TheLine.InPPDirective); 717 } else { 718 Indent = LevelIndent = FirstTok->OriginalColumn; 719 } 720 721 // If everything fits on a single line, just put it there. 722 unsigned ColumnLimit = Style.ColumnLimit; 723 if (I + 1 != E) { 724 AnnotatedLine *NextLine = I[1]; 725 if (NextLine->InPPDirective && !NextLine->First->HasUnescapedNewline) 726 ColumnLimit = getColumnLimit(TheLine.InPPDirective); 727 } 728 729 if (TheLine.Last->TotalLength + Indent <= ColumnLimit) { 730 LineState State = Indenter->getInitialState(Indent, &TheLine, DryRun); 731 while (State.NextToken != NULL) 732 Indenter->addTokenToState(State, /*Newline=*/false, DryRun); 733 } else if (Style.ColumnLimit == 0) { 734 // FIXME: Implement nested blocks for ColumnLimit = 0. 735 NoColumnLimitFormatter Formatter(Indenter); 736 if (!DryRun) 737 Formatter.format(Indent, &TheLine); 738 } else { 739 Penalty += format(TheLine, Indent, DryRun); 740 } 741 742 if (!TheLine.InPPDirective) 743 IndentForLevel[TheLine.Level] = LevelIndent; 744 } else if (TheLine.ChildrenAffected) { 745 format(TheLine.Children, DryRun); 746 } else { 747 // Format the first token if necessary, and notify the WhitespaceManager 748 // about the unchanged whitespace. 749 for (FormatToken *Tok = TheLine.First; Tok != NULL; Tok = Tok->Next) { 750 if (Tok == TheLine.First && 751 (Tok->NewlinesBefore > 0 || Tok->IsFirst)) { 752 unsigned LevelIndent = Tok->OriginalColumn; 753 if (!DryRun) { 754 // Remove trailing whitespace of the previous line. 755 if ((PreviousLine && PreviousLine->Affected) || 756 TheLine.LeadingEmptyLinesAffected) { 757 formatFirstToken(*Tok, PreviousLine, TheLine.Level, LevelIndent, 758 TheLine.InPPDirective); 759 } else { 760 Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective); 761 } 762 } 763 764 if (static_cast<int>(LevelIndent) - Offset >= 0) 765 LevelIndent -= Offset; 766 if (Tok->isNot(tok::comment) && !TheLine.InPPDirective) 767 IndentForLevel[TheLine.Level] = LevelIndent; 768 } else if (!DryRun) { 769 Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective); 770 } 771 } 772 } 773 if (!DryRun) { 774 for (FormatToken *Tok = TheLine.First; Tok != NULL; Tok = Tok->Next) { 775 Tok->Finalized = true; 776 } 777 } 778 PreviousLine = *I; 779 } 780 return Penalty; 781 } 782 783 private: 784 /// \brief Formats an \c AnnotatedLine and returns the penalty. 785 /// 786 /// If \p DryRun is \c false, directly applies the changes. 787 unsigned format(const AnnotatedLine &Line, unsigned FirstIndent, 788 bool DryRun) { 789 LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun); 790 791 // If the ObjC method declaration does not fit on a line, we should format 792 // it with one arg per line. 793 if (State.Line->Type == LT_ObjCMethodDecl) 794 State.Stack.back().BreakBeforeParameter = true; 795 796 // Find best solution in solution space. 797 return analyzeSolutionSpace(State, DryRun); 798 } 799 800 /// \brief An edge in the solution space from \c Previous->State to \c State, 801 /// inserting a newline dependent on the \c NewLine. 802 struct StateNode { 803 StateNode(const LineState &State, bool NewLine, StateNode *Previous) 804 : State(State), NewLine(NewLine), Previous(Previous) {} 805 LineState State; 806 bool NewLine; 807 StateNode *Previous; 808 }; 809 810 /// \brief A pair of <penalty, count> that is used to prioritize the BFS on. 811 /// 812 /// In case of equal penalties, we want to prefer states that were inserted 813 /// first. During state generation we make sure that we insert states first 814 /// that break the line as late as possible. 815 typedef std::pair<unsigned, unsigned> OrderedPenalty; 816 817 /// \brief An item in the prioritized BFS search queue. The \c StateNode's 818 /// \c State has the given \c OrderedPenalty. 819 typedef std::pair<OrderedPenalty, StateNode *> QueueItem; 820 821 /// \brief The BFS queue type. 822 typedef std::priority_queue<QueueItem, std::vector<QueueItem>, 823 std::greater<QueueItem> > QueueType; 824 825 /// \brief Get the offset of the line relatively to the level. 826 /// 827 /// For example, 'public:' labels in classes are offset by 1 or 2 828 /// characters to the left from their level. 829 int getIndentOffset(const FormatToken &RootToken) { 830 if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier()) 831 return Style.AccessModifierOffset; 832 return 0; 833 } 834 835 /// \brief Add a new line and the required indent before the first Token 836 /// of the \c UnwrappedLine if there was no structural parsing error. 837 void formatFirstToken(FormatToken &RootToken, 838 const AnnotatedLine *PreviousLine, unsigned IndentLevel, 839 unsigned Indent, bool InPPDirective) { 840 unsigned Newlines = 841 std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); 842 // Remove empty lines before "}" where applicable. 843 if (RootToken.is(tok::r_brace) && 844 (!RootToken.Next || 845 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next))) 846 Newlines = std::min(Newlines, 1u); 847 if (Newlines == 0 && !RootToken.IsFirst) 848 Newlines = 1; 849 850 // Insert extra new line before access specifiers. 851 if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) && 852 RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1) 853 ++Newlines; 854 855 // Remove empty lines after access specifiers. 856 if (PreviousLine && PreviousLine->First->isAccessSpecifier()) 857 Newlines = std::min(1u, Newlines); 858 859 Whitespaces->replaceWhitespace(RootToken, Newlines, IndentLevel, Indent, 860 Indent, InPPDirective && 861 !RootToken.HasUnescapedNewline); 862 } 863 864 /// \brief Get the indent of \p Level from \p IndentForLevel. 865 /// 866 /// \p IndentForLevel must contain the indent for the level \c l 867 /// at \p IndentForLevel[l], or a value < 0 if the indent for 868 /// that level is unknown. 869 unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) { 870 if (IndentForLevel[Level] != -1) 871 return IndentForLevel[Level]; 872 if (Level == 0) 873 return 0; 874 return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth; 875 } 876 877 void join(AnnotatedLine &A, const AnnotatedLine &B) { 878 assert(!A.Last->Next); 879 assert(!B.First->Previous); 880 if (B.Affected) 881 A.Affected = true; 882 A.Last->Next = B.First; 883 B.First->Previous = A.Last; 884 B.First->CanBreakBefore = true; 885 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore; 886 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) { 887 Tok->TotalLength += LengthA; 888 A.Last = Tok; 889 } 890 } 891 892 unsigned getColumnLimit(bool InPPDirective) const { 893 // In preprocessor directives reserve two chars for trailing " \" 894 return Style.ColumnLimit - (InPPDirective ? 2 : 0); 895 } 896 897 /// \brief Analyze the entire solution space starting from \p InitialState. 898 /// 899 /// This implements a variant of Dijkstra's algorithm on the graph that spans 900 /// the solution space (\c LineStates are the nodes). The algorithm tries to 901 /// find the shortest path (the one with lowest penalty) from \p InitialState 902 /// to a state where all tokens are placed. Returns the penalty. 903 /// 904 /// If \p DryRun is \c false, directly applies the changes. 905 unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun = false) { 906 std::set<LineState> Seen; 907 908 // Increasing count of \c StateNode items we have created. This is used to 909 // create a deterministic order independent of the container. 910 unsigned Count = 0; 911 QueueType Queue; 912 913 // Insert start element into queue. 914 StateNode *Node = 915 new (Allocator.Allocate()) StateNode(InitialState, false, NULL); 916 Queue.push(QueueItem(OrderedPenalty(0, Count), Node)); 917 ++Count; 918 919 unsigned Penalty = 0; 920 921 // While not empty, take first element and follow edges. 922 while (!Queue.empty()) { 923 Penalty = Queue.top().first.first; 924 StateNode *Node = Queue.top().second; 925 if (Node->State.NextToken == NULL) { 926 DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n"); 927 break; 928 } 929 Queue.pop(); 930 931 // Cut off the analysis of certain solutions if the analysis gets too 932 // complex. See description of IgnoreStackForComparison. 933 if (Count > 10000) 934 Node->State.IgnoreStackForComparison = true; 935 936 if (!Seen.insert(Node->State).second) 937 // State already examined with lower penalty. 938 continue; 939 940 FormatDecision LastFormat = Node->State.NextToken->Decision; 941 if (LastFormat == FD_Unformatted || LastFormat == FD_Continue) 942 addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue); 943 if (LastFormat == FD_Unformatted || LastFormat == FD_Break) 944 addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue); 945 } 946 947 if (Queue.empty()) { 948 // We were unable to find a solution, do nothing. 949 // FIXME: Add diagnostic? 950 DEBUG(llvm::dbgs() << "Could not find a solution.\n"); 951 return 0; 952 } 953 954 // Reconstruct the solution. 955 if (!DryRun) 956 reconstructPath(InitialState, Queue.top().second); 957 958 DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n"); 959 DEBUG(llvm::dbgs() << "---\n"); 960 961 return Penalty; 962 } 963 964 void reconstructPath(LineState &State, StateNode *Current) { 965 std::deque<StateNode *> Path; 966 // We do not need a break before the initial token. 967 while (Current->Previous) { 968 Path.push_front(Current); 969 Current = Current->Previous; 970 } 971 for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end(); 972 I != E; ++I) { 973 unsigned Penalty = 0; 974 formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty); 975 Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false); 976 977 DEBUG({ 978 if ((*I)->NewLine) { 979 llvm::dbgs() << "Penalty for placing " 980 << (*I)->Previous->State.NextToken->Tok.getName() << ": " 981 << Penalty << "\n"; 982 } 983 }); 984 } 985 } 986 987 /// \brief Add the following state to the analysis queue \c Queue. 988 /// 989 /// Assume the current state is \p PreviousNode and has been reached with a 990 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true. 991 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode, 992 bool NewLine, unsigned *Count, QueueType *Queue) { 993 if (NewLine && !Indenter->canBreak(PreviousNode->State)) 994 return; 995 if (!NewLine && Indenter->mustBreak(PreviousNode->State)) 996 return; 997 998 StateNode *Node = new (Allocator.Allocate()) 999 StateNode(PreviousNode->State, NewLine, PreviousNode); 1000 if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty)) 1001 return; 1002 1003 Penalty += Indenter->addTokenToState(Node->State, NewLine, true); 1004 1005 Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node)); 1006 ++(*Count); 1007 } 1008 1009 /// \brief If the \p State's next token is an r_brace closing a nested block, 1010 /// format the nested block before it. 1011 /// 1012 /// Returns \c true if all children could be placed successfully and adapts 1013 /// \p Penalty as well as \p State. If \p DryRun is false, also directly 1014 /// creates changes using \c Whitespaces. 1015 /// 1016 /// The crucial idea here is that children always get formatted upon 1017 /// encountering the closing brace right after the nested block. Now, if we 1018 /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is 1019 /// \c false), the entire block has to be kept on the same line (which is only 1020 /// possible if it fits on the line, only contains a single statement, etc. 1021 /// 1022 /// If \p NewLine is true, we format the nested block on separate lines, i.e. 1023 /// break after the "{", format all lines with correct indentation and the put 1024 /// the closing "}" on yet another new line. 1025 /// 1026 /// This enables us to keep the simple structure of the 1027 /// \c UnwrappedLineFormatter, where we only have two options for each token: 1028 /// break or don't break. 1029 bool formatChildren(LineState &State, bool NewLine, bool DryRun, 1030 unsigned &Penalty) { 1031 FormatToken &Previous = *State.NextToken->Previous; 1032 const FormatToken *LBrace = State.NextToken->getPreviousNonComment(); 1033 if (!LBrace || LBrace->isNot(tok::l_brace) || 1034 LBrace->BlockKind != BK_Block || Previous.Children.size() == 0) 1035 // The previous token does not open a block. Nothing to do. We don't 1036 // assert so that we can simply call this function for all tokens. 1037 return true; 1038 1039 if (NewLine) { 1040 int AdditionalIndent = State.Stack.back().Indent - 1041 Previous.Children[0]->Level * Style.IndentWidth; 1042 Penalty += format(Previous.Children, DryRun, AdditionalIndent, 1043 /*FixBadIndentation=*/true); 1044 return true; 1045 } 1046 1047 // Cannot merge multiple statements into a single line. 1048 if (Previous.Children.size() > 1) 1049 return false; 1050 1051 // We can't put the closing "}" on a line with a trailing comment. 1052 if (Previous.Children[0]->Last->isTrailingComment()) 1053 return false; 1054 1055 if (!DryRun) { 1056 Whitespaces->replaceWhitespace( 1057 *Previous.Children[0]->First, 1058 /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1, 1059 /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective); 1060 } 1061 Penalty += format(*Previous.Children[0], State.Column + 1, DryRun); 1062 1063 State.Column += 1 + Previous.Children[0]->Last->TotalLength; 1064 return true; 1065 } 1066 1067 ContinuationIndenter *Indenter; 1068 WhitespaceManager *Whitespaces; 1069 FormatStyle Style; 1070 LineJoiner Joiner; 1071 1072 llvm::SpecificBumpPtrAllocator<StateNode> Allocator; 1073 }; 1074 1075 class FormatTokenLexer { 1076 public: 1077 FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr, FormatStyle &Style, 1078 encoding::Encoding Encoding) 1079 : FormatTok(NULL), IsFirstToken(true), GreaterStashed(false), Column(0), 1080 TrailingWhitespace(0), Lex(Lex), SourceMgr(SourceMgr), Style(Style), 1081 IdentTable(getFormattingLangOpts()), Encoding(Encoding) { 1082 Lex.SetKeepWhitespaceMode(true); 1083 } 1084 1085 ArrayRef<FormatToken *> lex() { 1086 assert(Tokens.empty()); 1087 do { 1088 Tokens.push_back(getNextToken()); 1089 tryMergePreviousTokens(); 1090 } while (Tokens.back()->Tok.isNot(tok::eof)); 1091 return Tokens; 1092 } 1093 1094 IdentifierTable &getIdentTable() { return IdentTable; } 1095 1096 private: 1097 void tryMergePreviousTokens() { 1098 if (tryMerge_TMacro()) 1099 return; 1100 1101 if (Style.Language == FormatStyle::LK_JavaScript) { 1102 static tok::TokenKind JSIdentity[] = { tok::equalequal, tok::equal }; 1103 static tok::TokenKind JSNotIdentity[] = { tok::exclaimequal, tok::equal }; 1104 static tok::TokenKind JSShiftEqual[] = { tok::greater, tok::greater, 1105 tok::greaterequal }; 1106 // FIXME: We probably need to change token type to mimic operator with the 1107 // correct priority. 1108 if (tryMergeTokens(JSIdentity)) 1109 return; 1110 if (tryMergeTokens(JSNotIdentity)) 1111 return; 1112 if (tryMergeTokens(JSShiftEqual)) 1113 return; 1114 } 1115 } 1116 1117 bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds) { 1118 if (Tokens.size() < Kinds.size()) 1119 return false; 1120 1121 SmallVectorImpl<FormatToken *>::const_iterator First = 1122 Tokens.end() - Kinds.size(); 1123 if (!First[0]->is(Kinds[0])) 1124 return false; 1125 unsigned AddLength = 0; 1126 for (unsigned i = 1; i < Kinds.size(); ++i) { 1127 if (!First[i]->is(Kinds[i]) || First[i]->WhitespaceRange.getBegin() != 1128 First[i]->WhitespaceRange.getEnd()) 1129 return false; 1130 AddLength += First[i]->TokenText.size(); 1131 } 1132 Tokens.resize(Tokens.size() - Kinds.size() + 1); 1133 First[0]->TokenText = StringRef(First[0]->TokenText.data(), 1134 First[0]->TokenText.size() + AddLength); 1135 First[0]->ColumnWidth += AddLength; 1136 return true; 1137 } 1138 1139 bool tryMerge_TMacro() { 1140 if (Tokens.size() < 4) 1141 return false; 1142 FormatToken *Last = Tokens.back(); 1143 if (!Last->is(tok::r_paren)) 1144 return false; 1145 1146 FormatToken *String = Tokens[Tokens.size() - 2]; 1147 if (!String->is(tok::string_literal) || String->IsMultiline) 1148 return false; 1149 1150 if (!Tokens[Tokens.size() - 3]->is(tok::l_paren)) 1151 return false; 1152 1153 FormatToken *Macro = Tokens[Tokens.size() - 4]; 1154 if (Macro->TokenText != "_T") 1155 return false; 1156 1157 const char *Start = Macro->TokenText.data(); 1158 const char *End = Last->TokenText.data() + Last->TokenText.size(); 1159 String->TokenText = StringRef(Start, End - Start); 1160 String->IsFirst = Macro->IsFirst; 1161 String->LastNewlineOffset = Macro->LastNewlineOffset; 1162 String->WhitespaceRange = Macro->WhitespaceRange; 1163 String->OriginalColumn = Macro->OriginalColumn; 1164 String->ColumnWidth = encoding::columnWidthWithTabs( 1165 String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding); 1166 1167 Tokens.pop_back(); 1168 Tokens.pop_back(); 1169 Tokens.pop_back(); 1170 Tokens.back() = String; 1171 return true; 1172 } 1173 1174 FormatToken *getNextToken() { 1175 if (GreaterStashed) { 1176 // Create a synthesized second '>' token. 1177 // FIXME: Increment Column and set OriginalColumn. 1178 Token Greater = FormatTok->Tok; 1179 FormatTok = new (Allocator.Allocate()) FormatToken; 1180 FormatTok->Tok = Greater; 1181 SourceLocation GreaterLocation = 1182 FormatTok->Tok.getLocation().getLocWithOffset(1); 1183 FormatTok->WhitespaceRange = 1184 SourceRange(GreaterLocation, GreaterLocation); 1185 FormatTok->TokenText = ">"; 1186 FormatTok->ColumnWidth = 1; 1187 GreaterStashed = false; 1188 return FormatTok; 1189 } 1190 1191 FormatTok = new (Allocator.Allocate()) FormatToken; 1192 readRawToken(*FormatTok); 1193 SourceLocation WhitespaceStart = 1194 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace); 1195 FormatTok->IsFirst = IsFirstToken; 1196 IsFirstToken = false; 1197 1198 // Consume and record whitespace until we find a significant token. 1199 unsigned WhitespaceLength = TrailingWhitespace; 1200 while (FormatTok->Tok.is(tok::unknown)) { 1201 for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) { 1202 switch (FormatTok->TokenText[i]) { 1203 case '\n': 1204 ++FormatTok->NewlinesBefore; 1205 // FIXME: This is technically incorrect, as it could also 1206 // be a literal backslash at the end of the line. 1207 if (i == 0 || (FormatTok->TokenText[i - 1] != '\\' && 1208 (FormatTok->TokenText[i - 1] != '\r' || i == 1 || 1209 FormatTok->TokenText[i - 2] != '\\'))) 1210 FormatTok->HasUnescapedNewline = true; 1211 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1; 1212 Column = 0; 1213 break; 1214 case '\r': 1215 case '\f': 1216 case '\v': 1217 Column = 0; 1218 break; 1219 case ' ': 1220 ++Column; 1221 break; 1222 case '\t': 1223 Column += Style.TabWidth - Column % Style.TabWidth; 1224 break; 1225 case '\\': 1226 ++Column; 1227 if (i + 1 == e || (FormatTok->TokenText[i + 1] != '\r' && 1228 FormatTok->TokenText[i + 1] != '\n')) 1229 FormatTok->Type = TT_ImplicitStringLiteral; 1230 break; 1231 default: 1232 FormatTok->Type = TT_ImplicitStringLiteral; 1233 ++Column; 1234 break; 1235 } 1236 } 1237 1238 if (FormatTok->Type == TT_ImplicitStringLiteral) 1239 break; 1240 WhitespaceLength += FormatTok->Tok.getLength(); 1241 1242 readRawToken(*FormatTok); 1243 } 1244 1245 // In case the token starts with escaped newlines, we want to 1246 // take them into account as whitespace - this pattern is quite frequent 1247 // in macro definitions. 1248 // FIXME: Add a more explicit test. 1249 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' && 1250 FormatTok->TokenText[1] == '\n') { 1251 // FIXME: ++FormatTok->NewlinesBefore is missing... 1252 WhitespaceLength += 2; 1253 Column = 0; 1254 FormatTok->TokenText = FormatTok->TokenText.substr(2); 1255 } 1256 1257 FormatTok->WhitespaceRange = SourceRange( 1258 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength)); 1259 1260 FormatTok->OriginalColumn = Column; 1261 1262 TrailingWhitespace = 0; 1263 if (FormatTok->Tok.is(tok::comment)) { 1264 // FIXME: Add the trimmed whitespace to Column. 1265 StringRef UntrimmedText = FormatTok->TokenText; 1266 FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f"); 1267 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size(); 1268 } else if (FormatTok->Tok.is(tok::raw_identifier)) { 1269 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText); 1270 FormatTok->Tok.setIdentifierInfo(&Info); 1271 FormatTok->Tok.setKind(Info.getTokenID()); 1272 } else if (FormatTok->Tok.is(tok::greatergreater)) { 1273 FormatTok->Tok.setKind(tok::greater); 1274 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1); 1275 GreaterStashed = true; 1276 } 1277 1278 // Now FormatTok is the next non-whitespace token. 1279 1280 StringRef Text = FormatTok->TokenText; 1281 size_t FirstNewlinePos = Text.find('\n'); 1282 if (FirstNewlinePos == StringRef::npos) { 1283 // FIXME: ColumnWidth actually depends on the start column, we need to 1284 // take this into account when the token is moved. 1285 FormatTok->ColumnWidth = 1286 encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding); 1287 Column += FormatTok->ColumnWidth; 1288 } else { 1289 FormatTok->IsMultiline = true; 1290 // FIXME: ColumnWidth actually depends on the start column, we need to 1291 // take this into account when the token is moved. 1292 FormatTok->ColumnWidth = encoding::columnWidthWithTabs( 1293 Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding); 1294 1295 // The last line of the token always starts in column 0. 1296 // Thus, the length can be precomputed even in the presence of tabs. 1297 FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs( 1298 Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth, 1299 Encoding); 1300 Column = FormatTok->LastLineColumnWidth; 1301 } 1302 1303 return FormatTok; 1304 } 1305 1306 FormatToken *FormatTok; 1307 bool IsFirstToken; 1308 bool GreaterStashed; 1309 unsigned Column; 1310 unsigned TrailingWhitespace; 1311 Lexer &Lex; 1312 SourceManager &SourceMgr; 1313 FormatStyle &Style; 1314 IdentifierTable IdentTable; 1315 encoding::Encoding Encoding; 1316 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; 1317 SmallVector<FormatToken *, 16> Tokens; 1318 1319 void readRawToken(FormatToken &Tok) { 1320 Lex.LexFromRawLexer(Tok.Tok); 1321 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()), 1322 Tok.Tok.getLength()); 1323 // For formatting, treat unterminated string literals like normal string 1324 // literals. 1325 if (Tok.is(tok::unknown) && !Tok.TokenText.empty() && 1326 Tok.TokenText[0] == '"') { 1327 Tok.Tok.setKind(tok::string_literal); 1328 Tok.IsUnterminatedLiteral = true; 1329 } 1330 } 1331 }; 1332 1333 static StringRef getLanguageName(FormatStyle::LanguageKind Language) { 1334 switch (Language) { 1335 case FormatStyle::LK_Cpp: 1336 return "C++"; 1337 case FormatStyle::LK_JavaScript: 1338 return "JavaScript"; 1339 default: 1340 return "Unknown"; 1341 } 1342 } 1343 1344 class Formatter : public UnwrappedLineConsumer { 1345 public: 1346 Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, 1347 const std::vector<CharSourceRange> &Ranges) 1348 : Style(Style), Lex(Lex), SourceMgr(SourceMgr), 1349 Whitespaces(SourceMgr, Style, inputUsesCRLF(Lex.getBuffer())), 1350 Ranges(Ranges.begin(), Ranges.end()), UnwrappedLines(1), 1351 Encoding(encoding::detectEncoding(Lex.getBuffer())) { 1352 DEBUG(llvm::dbgs() << "File encoding: " 1353 << (Encoding == encoding::Encoding_UTF8 ? "UTF8" 1354 : "unknown") 1355 << "\n"); 1356 DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language) 1357 << "\n"); 1358 } 1359 1360 tooling::Replacements format() { 1361 tooling::Replacements Result; 1362 FormatTokenLexer Tokens(Lex, SourceMgr, Style, Encoding); 1363 1364 UnwrappedLineParser Parser(Style, Tokens.lex(), *this); 1365 bool StructuralError = Parser.parse(); 1366 assert(UnwrappedLines.rbegin()->empty()); 1367 for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE; 1368 ++Run) { 1369 DEBUG(llvm::dbgs() << "Run " << Run << "...\n"); 1370 SmallVector<AnnotatedLine *, 16> AnnotatedLines; 1371 for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) { 1372 AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i])); 1373 } 1374 tooling::Replacements RunResult = 1375 format(AnnotatedLines, StructuralError, Tokens); 1376 DEBUG({ 1377 llvm::dbgs() << "Replacements for run " << Run << ":\n"; 1378 for (tooling::Replacements::iterator I = RunResult.begin(), 1379 E = RunResult.end(); 1380 I != E; ++I) { 1381 llvm::dbgs() << I->toString() << "\n"; 1382 } 1383 }); 1384 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 1385 delete AnnotatedLines[i]; 1386 } 1387 Result.insert(RunResult.begin(), RunResult.end()); 1388 Whitespaces.reset(); 1389 } 1390 return Result; 1391 } 1392 1393 tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, 1394 bool StructuralError, FormatTokenLexer &Tokens) { 1395 TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in")); 1396 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 1397 Annotator.annotate(*AnnotatedLines[i]); 1398 } 1399 deriveLocalStyle(AnnotatedLines); 1400 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 1401 Annotator.calculateFormattingInformation(*AnnotatedLines[i]); 1402 } 1403 computeAffectedLines(AnnotatedLines.begin(), AnnotatedLines.end()); 1404 1405 Annotator.setCommentLineLevels(AnnotatedLines); 1406 ContinuationIndenter Indenter(Style, SourceMgr, Whitespaces, Encoding, 1407 BinPackInconclusiveFunctions); 1408 UnwrappedLineFormatter Formatter(&Indenter, &Whitespaces, Style); 1409 Formatter.format(AnnotatedLines, /*DryRun=*/false); 1410 return Whitespaces.generateReplacements(); 1411 } 1412 1413 private: 1414 // Determines which lines are affected by the SourceRanges given as input. 1415 // Returns \c true if at least one line between I and E or one of their 1416 // children is affected. 1417 bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I, 1418 SmallVectorImpl<AnnotatedLine *>::iterator E) { 1419 bool SomeLineAffected = false; 1420 const AnnotatedLine *PreviousLine = NULL; 1421 while (I != E) { 1422 AnnotatedLine *Line = *I; 1423 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First); 1424 1425 // If a line is part of a preprocessor directive, it needs to be formatted 1426 // if any token within the directive is affected. 1427 if (Line->InPPDirective) { 1428 FormatToken *Last = Line->Last; 1429 SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1; 1430 while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) { 1431 Last = (*PPEnd)->Last; 1432 ++PPEnd; 1433 } 1434 1435 if (affectsTokenRange(*Line->First, *Last, 1436 /*IncludeLeadingNewlines=*/false)) { 1437 SomeLineAffected = true; 1438 markAllAsAffected(I, PPEnd); 1439 } 1440 I = PPEnd; 1441 continue; 1442 } 1443 1444 if (nonPPLineAffected(Line, PreviousLine)) 1445 SomeLineAffected = true; 1446 1447 PreviousLine = Line; 1448 ++I; 1449 } 1450 return SomeLineAffected; 1451 } 1452 1453 // Determines whether 'Line' is affected by the SourceRanges given as input. 1454 // Returns \c true if line or one if its children is affected. 1455 bool nonPPLineAffected(AnnotatedLine *Line, 1456 const AnnotatedLine *PreviousLine) { 1457 bool SomeLineAffected = false; 1458 Line->ChildrenAffected = 1459 computeAffectedLines(Line->Children.begin(), Line->Children.end()); 1460 if (Line->ChildrenAffected) 1461 SomeLineAffected = true; 1462 1463 // Stores whether one of the line's tokens is directly affected. 1464 bool SomeTokenAffected = false; 1465 // Stores whether we need to look at the leading newlines of the next token 1466 // in order to determine whether it was affected. 1467 bool IncludeLeadingNewlines = false; 1468 1469 // Stores whether the first child line of any of this line's tokens is 1470 // affected. 1471 bool SomeFirstChildAffected = false; 1472 1473 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) { 1474 // Determine whether 'Tok' was affected. 1475 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines)) 1476 SomeTokenAffected = true; 1477 1478 // Determine whether the first child of 'Tok' was affected. 1479 if (!Tok->Children.empty() && Tok->Children.front()->Affected) 1480 SomeFirstChildAffected = true; 1481 1482 IncludeLeadingNewlines = Tok->Children.empty(); 1483 } 1484 1485 // Was this line moved, i.e. has it previously been on the same line as an 1486 // affected line? 1487 bool LineMoved = PreviousLine && PreviousLine->Affected && 1488 Line->First->NewlinesBefore == 0; 1489 1490 bool IsContinuedComment = Line->First->is(tok::comment) && 1491 Line->First->Next == NULL && 1492 Line->First->NewlinesBefore < 2 && PreviousLine && 1493 PreviousLine->Affected && 1494 PreviousLine->Last->is(tok::comment); 1495 1496 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved || 1497 IsContinuedComment) { 1498 Line->Affected = true; 1499 SomeLineAffected = true; 1500 } 1501 return SomeLineAffected; 1502 } 1503 1504 // Marks all lines between I and E as well as all their children as affected. 1505 void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I, 1506 SmallVectorImpl<AnnotatedLine *>::iterator E) { 1507 while (I != E) { 1508 (*I)->Affected = true; 1509 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end()); 1510 ++I; 1511 } 1512 } 1513 1514 // Returns true if the range from 'First' to 'Last' intersects with one of the 1515 // input ranges. 1516 bool affectsTokenRange(const FormatToken &First, const FormatToken &Last, 1517 bool IncludeLeadingNewlines) { 1518 SourceLocation Start = First.WhitespaceRange.getBegin(); 1519 if (!IncludeLeadingNewlines) 1520 Start = Start.getLocWithOffset(First.LastNewlineOffset); 1521 SourceLocation End = Last.getStartOfNonWhitespace(); 1522 if (Last.TokenText.size() > 0) 1523 End = End.getLocWithOffset(Last.TokenText.size() - 1); 1524 CharSourceRange Range = CharSourceRange::getCharRange(Start, End); 1525 return affectsCharSourceRange(Range); 1526 } 1527 1528 // Returns true if one of the input ranges intersect the leading empty lines 1529 // before 'Tok'. 1530 bool affectsLeadingEmptyLines(const FormatToken &Tok) { 1531 CharSourceRange EmptyLineRange = CharSourceRange::getCharRange( 1532 Tok.WhitespaceRange.getBegin(), 1533 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset)); 1534 return affectsCharSourceRange(EmptyLineRange); 1535 } 1536 1537 // Returns true if 'Range' intersects with one of the input ranges. 1538 bool affectsCharSourceRange(const CharSourceRange &Range) { 1539 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(), 1540 E = Ranges.end(); 1541 I != E; ++I) { 1542 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) && 1543 !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin())) 1544 return true; 1545 } 1546 return false; 1547 } 1548 1549 static bool inputUsesCRLF(StringRef Text) { 1550 return Text.count('\r') * 2 > Text.count('\n'); 1551 } 1552 1553 void 1554 deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { 1555 unsigned CountBoundToVariable = 0; 1556 unsigned CountBoundToType = 0; 1557 bool HasCpp03IncompatibleFormat = false; 1558 bool HasBinPackedFunction = false; 1559 bool HasOnePerLineFunction = false; 1560 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 1561 if (!AnnotatedLines[i]->First->Next) 1562 continue; 1563 FormatToken *Tok = AnnotatedLines[i]->First->Next; 1564 while (Tok->Next) { 1565 if (Tok->Type == TT_PointerOrReference) { 1566 bool SpacesBefore = 1567 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd(); 1568 bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() != 1569 Tok->Next->WhitespaceRange.getEnd(); 1570 if (SpacesBefore && !SpacesAfter) 1571 ++CountBoundToVariable; 1572 else if (!SpacesBefore && SpacesAfter) 1573 ++CountBoundToType; 1574 } 1575 1576 if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) { 1577 if (Tok->is(tok::coloncolon) && 1578 Tok->Previous->Type == TT_TemplateOpener) 1579 HasCpp03IncompatibleFormat = true; 1580 if (Tok->Type == TT_TemplateCloser && 1581 Tok->Previous->Type == TT_TemplateCloser) 1582 HasCpp03IncompatibleFormat = true; 1583 } 1584 1585 if (Tok->PackingKind == PPK_BinPacked) 1586 HasBinPackedFunction = true; 1587 if (Tok->PackingKind == PPK_OnePerLine) 1588 HasOnePerLineFunction = true; 1589 1590 Tok = Tok->Next; 1591 } 1592 } 1593 if (Style.DerivePointerBinding) { 1594 if (CountBoundToType > CountBoundToVariable) 1595 Style.PointerBindsToType = true; 1596 else if (CountBoundToType < CountBoundToVariable) 1597 Style.PointerBindsToType = false; 1598 } 1599 if (Style.Standard == FormatStyle::LS_Auto) { 1600 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 1601 : FormatStyle::LS_Cpp03; 1602 } 1603 BinPackInconclusiveFunctions = 1604 HasBinPackedFunction || !HasOnePerLineFunction; 1605 } 1606 1607 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { 1608 assert(!UnwrappedLines.empty()); 1609 UnwrappedLines.back().push_back(TheLine); 1610 } 1611 1612 virtual void finishRun() { 1613 UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>()); 1614 } 1615 1616 FormatStyle Style; 1617 Lexer &Lex; 1618 SourceManager &SourceMgr; 1619 WhitespaceManager Whitespaces; 1620 SmallVector<CharSourceRange, 8> Ranges; 1621 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines; 1622 1623 encoding::Encoding Encoding; 1624 bool BinPackInconclusiveFunctions; 1625 }; 1626 1627 } // end anonymous namespace 1628 1629 tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, 1630 SourceManager &SourceMgr, 1631 std::vector<CharSourceRange> Ranges) { 1632 Formatter formatter(Style, Lex, SourceMgr, Ranges); 1633 return formatter.format(); 1634 } 1635 1636 tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, 1637 std::vector<tooling::Range> Ranges, 1638 StringRef FileName) { 1639 FileManager Files((FileSystemOptions())); 1640 DiagnosticsEngine Diagnostics( 1641 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), 1642 new DiagnosticOptions); 1643 SourceManager SourceMgr(Diagnostics, Files); 1644 llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName); 1645 const clang::FileEntry *Entry = 1646 Files.getVirtualFile(FileName, Buf->getBufferSize(), 0); 1647 SourceMgr.overrideFileContents(Entry, Buf); 1648 FileID ID = 1649 SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); 1650 Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr, 1651 getFormattingLangOpts(Style.Standard)); 1652 SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID); 1653 std::vector<CharSourceRange> CharRanges; 1654 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { 1655 SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset()); 1656 SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength()); 1657 CharRanges.push_back(CharSourceRange::getCharRange(Start, End)); 1658 } 1659 return reformat(Style, Lex, SourceMgr, CharRanges); 1660 } 1661 1662 LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) { 1663 LangOptions LangOpts; 1664 LangOpts.CPlusPlus = 1; 1665 LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1; 1666 LangOpts.LineComment = 1; 1667 LangOpts.Bool = 1; 1668 LangOpts.ObjC1 = 1; 1669 LangOpts.ObjC2 = 1; 1670 return LangOpts; 1671 } 1672 1673 const char *StyleOptionHelpDescription = 1674 "Coding style, currently supports:\n" 1675 " LLVM, Google, Chromium, Mozilla, WebKit.\n" 1676 "Use -style=file to load style configuration from\n" 1677 ".clang-format file located in one of the parent\n" 1678 "directories of the source file (or current\n" 1679 "directory for stdin).\n" 1680 "Use -style=\"{key: value, ...}\" to set specific\n" 1681 "parameters, e.g.:\n" 1682 " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""; 1683 1684 static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) { 1685 if (FileName.endswith_lower(".js")) { 1686 return FormatStyle::LK_JavaScript; 1687 } 1688 return FormatStyle::LK_Cpp; 1689 } 1690 1691 FormatStyle getStyle(StringRef StyleName, StringRef FileName, 1692 StringRef FallbackStyle) { 1693 FormatStyle Style = getLLVMStyle(); 1694 Style.Language = getLanguageByFileName(FileName); 1695 if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) { 1696 llvm::errs() << "Invalid fallback style \"" << FallbackStyle 1697 << "\" using LLVM style\n"; 1698 return Style; 1699 } 1700 1701 if (StyleName.startswith("{")) { 1702 // Parse YAML/JSON style from the command line. 1703 if (llvm::error_code ec = parseConfiguration(StyleName, &Style)) { 1704 llvm::errs() << "Error parsing -style: " << ec.message() << ", using " 1705 << FallbackStyle << " style\n"; 1706 } 1707 return Style; 1708 } 1709 1710 if (!StyleName.equals_lower("file")) { 1711 if (!getPredefinedStyle(StyleName, Style.Language, &Style)) 1712 llvm::errs() << "Invalid value for -style, using " << FallbackStyle 1713 << " style\n"; 1714 return Style; 1715 } 1716 1717 // Look for .clang-format/_clang-format file in the file's parent directories. 1718 SmallString<128> UnsuitableConfigFiles; 1719 SmallString<128> Path(FileName); 1720 llvm::sys::fs::make_absolute(Path); 1721 for (StringRef Directory = Path; !Directory.empty(); 1722 Directory = llvm::sys::path::parent_path(Directory)) { 1723 if (!llvm::sys::fs::is_directory(Directory)) 1724 continue; 1725 SmallString<128> ConfigFile(Directory); 1726 1727 llvm::sys::path::append(ConfigFile, ".clang-format"); 1728 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); 1729 bool IsFile = false; 1730 // Ignore errors from is_regular_file: we only need to know if we can read 1731 // the file or not. 1732 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); 1733 1734 if (!IsFile) { 1735 // Try _clang-format too, since dotfiles are not commonly used on Windows. 1736 ConfigFile = Directory; 1737 llvm::sys::path::append(ConfigFile, "_clang-format"); 1738 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); 1739 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); 1740 } 1741 1742 if (IsFile) { 1743 OwningPtr<llvm::MemoryBuffer> Text; 1744 if (llvm::error_code ec = 1745 llvm::MemoryBuffer::getFile(ConfigFile.c_str(), Text)) { 1746 llvm::errs() << ec.message() << "\n"; 1747 break; 1748 } 1749 if (llvm::error_code ec = parseConfiguration(Text->getBuffer(), &Style)) { 1750 if (ec == llvm::errc::not_supported) { 1751 if (!UnsuitableConfigFiles.empty()) 1752 UnsuitableConfigFiles.append(", "); 1753 UnsuitableConfigFiles.append(ConfigFile); 1754 continue; 1755 } 1756 llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message() 1757 << "\n"; 1758 break; 1759 } 1760 DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n"); 1761 return Style; 1762 } 1763 } 1764 llvm::errs() << "Can't find usable .clang-format, using " << FallbackStyle 1765 << " style\n"; 1766 if (!UnsuitableConfigFiles.empty()) { 1767 llvm::errs() << "Configuration file(s) do(es) not support " 1768 << getLanguageName(Style.Language) << ": " 1769 << UnsuitableConfigFiles << "\n"; 1770 } 1771 return Style; 1772 } 1773 1774 } // namespace format 1775 } // namespace clang 1776