clang API Documentation
00001 //===--- Format.cpp - Format C++ code -------------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 /// 00010 /// \file 00011 /// \brief This file implements functions declared in Format.h. This will be 00012 /// split into separate files as we go. 00013 /// 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "ContinuationIndenter.h" 00017 #include "TokenAnnotator.h" 00018 #include "UnwrappedLineParser.h" 00019 #include "WhitespaceManager.h" 00020 #include "clang/Basic/Diagnostic.h" 00021 #include "clang/Basic/DiagnosticOptions.h" 00022 #include "clang/Basic/SourceManager.h" 00023 #include "clang/Format/Format.h" 00024 #include "clang/Lex/Lexer.h" 00025 #include "llvm/ADT/STLExtras.h" 00026 #include "llvm/Support/Allocator.h" 00027 #include "llvm/Support/Debug.h" 00028 #include "llvm/Support/Path.h" 00029 #include "llvm/Support/YAMLTraits.h" 00030 #include <queue> 00031 #include <string> 00032 00033 #define DEBUG_TYPE "format-formatter" 00034 00035 using clang::format::FormatStyle; 00036 00037 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string) 00038 00039 namespace llvm { 00040 namespace yaml { 00041 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageKind> { 00042 static void enumeration(IO &IO, FormatStyle::LanguageKind &Value) { 00043 IO.enumCase(Value, "Cpp", FormatStyle::LK_Cpp); 00044 IO.enumCase(Value, "Java", FormatStyle::LK_Java); 00045 IO.enumCase(Value, "JavaScript", FormatStyle::LK_JavaScript); 00046 IO.enumCase(Value, "Proto", FormatStyle::LK_Proto); 00047 } 00048 }; 00049 00050 template <> struct ScalarEnumerationTraits<FormatStyle::LanguageStandard> { 00051 static void enumeration(IO &IO, FormatStyle::LanguageStandard &Value) { 00052 IO.enumCase(Value, "Cpp03", FormatStyle::LS_Cpp03); 00053 IO.enumCase(Value, "C++03", FormatStyle::LS_Cpp03); 00054 IO.enumCase(Value, "Cpp11", FormatStyle::LS_Cpp11); 00055 IO.enumCase(Value, "C++11", FormatStyle::LS_Cpp11); 00056 IO.enumCase(Value, "Auto", FormatStyle::LS_Auto); 00057 } 00058 }; 00059 00060 template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> { 00061 static void enumeration(IO &IO, FormatStyle::UseTabStyle &Value) { 00062 IO.enumCase(Value, "Never", FormatStyle::UT_Never); 00063 IO.enumCase(Value, "false", FormatStyle::UT_Never); 00064 IO.enumCase(Value, "Always", FormatStyle::UT_Always); 00065 IO.enumCase(Value, "true", FormatStyle::UT_Always); 00066 IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation); 00067 } 00068 }; 00069 00070 template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> { 00071 static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) { 00072 IO.enumCase(Value, "None", FormatStyle::SFS_None); 00073 IO.enumCase(Value, "false", FormatStyle::SFS_None); 00074 IO.enumCase(Value, "All", FormatStyle::SFS_All); 00075 IO.enumCase(Value, "true", FormatStyle::SFS_All); 00076 IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline); 00077 } 00078 }; 00079 00080 template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> { 00081 static void enumeration(IO &IO, FormatStyle::BinaryOperatorStyle &Value) { 00082 IO.enumCase(Value, "All", FormatStyle::BOS_All); 00083 IO.enumCase(Value, "true", FormatStyle::BOS_All); 00084 IO.enumCase(Value, "None", FormatStyle::BOS_None); 00085 IO.enumCase(Value, "false", FormatStyle::BOS_None); 00086 IO.enumCase(Value, "NonAssignment", FormatStyle::BOS_NonAssignment); 00087 } 00088 }; 00089 00090 template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> { 00091 static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) { 00092 IO.enumCase(Value, "Attach", FormatStyle::BS_Attach); 00093 IO.enumCase(Value, "Linux", FormatStyle::BS_Linux); 00094 IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup); 00095 IO.enumCase(Value, "Allman", FormatStyle::BS_Allman); 00096 IO.enumCase(Value, "GNU", FormatStyle::BS_GNU); 00097 } 00098 }; 00099 00100 template <> 00101 struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> { 00102 static void enumeration(IO &IO, 00103 FormatStyle::NamespaceIndentationKind &Value) { 00104 IO.enumCase(Value, "None", FormatStyle::NI_None); 00105 IO.enumCase(Value, "Inner", FormatStyle::NI_Inner); 00106 IO.enumCase(Value, "All", FormatStyle::NI_All); 00107 } 00108 }; 00109 00110 template <> 00111 struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> { 00112 static void enumeration(IO &IO, 00113 FormatStyle::PointerAlignmentStyle &Value) { 00114 IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle); 00115 IO.enumCase(Value, "Left", FormatStyle::PAS_Left); 00116 IO.enumCase(Value, "Right", FormatStyle::PAS_Right); 00117 00118 // For backward compatibility. 00119 IO.enumCase(Value, "true", FormatStyle::PAS_Left); 00120 IO.enumCase(Value, "false", FormatStyle::PAS_Right); 00121 } 00122 }; 00123 00124 template <> 00125 struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> { 00126 static void enumeration(IO &IO, 00127 FormatStyle::SpaceBeforeParensOptions &Value) { 00128 IO.enumCase(Value, "Never", FormatStyle::SBPO_Never); 00129 IO.enumCase(Value, "ControlStatements", 00130 FormatStyle::SBPO_ControlStatements); 00131 IO.enumCase(Value, "Always", FormatStyle::SBPO_Always); 00132 00133 // For backward compatibility. 00134 IO.enumCase(Value, "false", FormatStyle::SBPO_Never); 00135 IO.enumCase(Value, "true", FormatStyle::SBPO_ControlStatements); 00136 } 00137 }; 00138 00139 template <> struct MappingTraits<FormatStyle> { 00140 static void mapping(IO &IO, FormatStyle &Style) { 00141 // When reading, read the language first, we need it for getPredefinedStyle. 00142 IO.mapOptional("Language", Style.Language); 00143 00144 if (IO.outputting()) { 00145 StringRef StylesArray[] = { "LLVM", "Google", "Chromium", 00146 "Mozilla", "WebKit", "GNU" }; 00147 ArrayRef<StringRef> Styles(StylesArray); 00148 for (size_t i = 0, e = Styles.size(); i < e; ++i) { 00149 StringRef StyleName(Styles[i]); 00150 FormatStyle PredefinedStyle; 00151 if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) && 00152 Style == PredefinedStyle) { 00153 IO.mapOptional("# BasedOnStyle", StyleName); 00154 break; 00155 } 00156 } 00157 } else { 00158 StringRef BasedOnStyle; 00159 IO.mapOptional("BasedOnStyle", BasedOnStyle); 00160 if (!BasedOnStyle.empty()) { 00161 FormatStyle::LanguageKind OldLanguage = Style.Language; 00162 FormatStyle::LanguageKind Language = 00163 ((FormatStyle *)IO.getContext())->Language; 00164 if (!getPredefinedStyle(BasedOnStyle, Language, &Style)) { 00165 IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle)); 00166 return; 00167 } 00168 Style.Language = OldLanguage; 00169 } 00170 } 00171 00172 IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); 00173 IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); 00174 IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); 00175 IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", 00176 Style.AllowAllParametersOfDeclarationOnNextLine); 00177 IO.mapOptional("AllowShortBlocksOnASingleLine", 00178 Style.AllowShortBlocksOnASingleLine); 00179 IO.mapOptional("AllowShortCaseLabelsOnASingleLine", 00180 Style.AllowShortCaseLabelsOnASingleLine); 00181 IO.mapOptional("AllowShortIfStatementsOnASingleLine", 00182 Style.AllowShortIfStatementsOnASingleLine); 00183 IO.mapOptional("AllowShortLoopsOnASingleLine", 00184 Style.AllowShortLoopsOnASingleLine); 00185 IO.mapOptional("AllowShortFunctionsOnASingleLine", 00186 Style.AllowShortFunctionsOnASingleLine); 00187 IO.mapOptional("AlwaysBreakAfterDefinitionReturnType", 00188 Style.AlwaysBreakAfterDefinitionReturnType); 00189 IO.mapOptional("AlwaysBreakTemplateDeclarations", 00190 Style.AlwaysBreakTemplateDeclarations); 00191 IO.mapOptional("AlwaysBreakBeforeMultilineStrings", 00192 Style.AlwaysBreakBeforeMultilineStrings); 00193 IO.mapOptional("BreakBeforeBinaryOperators", 00194 Style.BreakBeforeBinaryOperators); 00195 IO.mapOptional("BreakBeforeTernaryOperators", 00196 Style.BreakBeforeTernaryOperators); 00197 IO.mapOptional("BreakConstructorInitializersBeforeComma", 00198 Style.BreakConstructorInitializersBeforeComma); 00199 IO.mapOptional("BinPackParameters", Style.BinPackParameters); 00200 IO.mapOptional("BinPackArguments", Style.BinPackArguments); 00201 IO.mapOptional("ColumnLimit", Style.ColumnLimit); 00202 IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", 00203 Style.ConstructorInitializerAllOnOneLineOrOnePerLine); 00204 IO.mapOptional("ConstructorInitializerIndentWidth", 00205 Style.ConstructorInitializerIndentWidth); 00206 IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment); 00207 IO.mapOptional("ExperimentalAutoDetectBinPacking", 00208 Style.ExperimentalAutoDetectBinPacking); 00209 IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); 00210 IO.mapOptional("IndentWrappedFunctionNames", 00211 Style.IndentWrappedFunctionNames); 00212 IO.mapOptional("IndentFunctionDeclarationAfterType", 00213 Style.IndentWrappedFunctionNames); 00214 IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); 00215 IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks", 00216 Style.KeepEmptyLinesAtTheStartOfBlocks); 00217 IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); 00218 IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth); 00219 IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty); 00220 IO.mapOptional("ObjCSpaceBeforeProtocolList", 00221 Style.ObjCSpaceBeforeProtocolList); 00222 IO.mapOptional("PenaltyBreakBeforeFirstCallParameter", 00223 Style.PenaltyBreakBeforeFirstCallParameter); 00224 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); 00225 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); 00226 IO.mapOptional("PenaltyBreakFirstLessLess", 00227 Style.PenaltyBreakFirstLessLess); 00228 IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); 00229 IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", 00230 Style.PenaltyReturnTypeOnItsOwnLine); 00231 IO.mapOptional("PointerAlignment", Style.PointerAlignment); 00232 IO.mapOptional("SpacesBeforeTrailingComments", 00233 Style.SpacesBeforeTrailingComments); 00234 IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle); 00235 IO.mapOptional("Standard", Style.Standard); 00236 IO.mapOptional("IndentWidth", Style.IndentWidth); 00237 IO.mapOptional("TabWidth", Style.TabWidth); 00238 IO.mapOptional("UseTab", Style.UseTab); 00239 IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); 00240 IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); 00241 IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets); 00242 IO.mapOptional("SpacesInAngles", Style.SpacesInAngles); 00243 IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); 00244 IO.mapOptional("SpacesInCStyleCastParentheses", 00245 Style.SpacesInCStyleCastParentheses); 00246 IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); 00247 IO.mapOptional("SpacesInContainerLiterals", 00248 Style.SpacesInContainerLiterals); 00249 IO.mapOptional("SpaceBeforeAssignmentOperators", 00250 Style.SpaceBeforeAssignmentOperators); 00251 IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth); 00252 IO.mapOptional("CommentPragmas", Style.CommentPragmas); 00253 IO.mapOptional("ForEachMacros", Style.ForEachMacros); 00254 00255 // For backward compatibility. 00256 if (!IO.outputting()) { 00257 IO.mapOptional("SpaceAfterControlStatementKeyword", 00258 Style.SpaceBeforeParens); 00259 IO.mapOptional("PointerBindsToType", Style.PointerAlignment); 00260 IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment); 00261 } 00262 IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens); 00263 IO.mapOptional("DisableFormat", Style.DisableFormat); 00264 } 00265 }; 00266 00267 // Allows to read vector<FormatStyle> while keeping default values. 00268 // IO.getContext() should contain a pointer to the FormatStyle structure, that 00269 // will be used to get default values for missing keys. 00270 // If the first element has no Language specified, it will be treated as the 00271 // default one for the following elements. 00272 template <> struct DocumentListTraits<std::vector<FormatStyle> > { 00273 static size_t size(IO &IO, std::vector<FormatStyle> &Seq) { 00274 return Seq.size(); 00275 } 00276 static FormatStyle &element(IO &IO, std::vector<FormatStyle> &Seq, 00277 size_t Index) { 00278 if (Index >= Seq.size()) { 00279 assert(Index == Seq.size()); 00280 FormatStyle Template; 00281 if (Seq.size() > 0 && Seq[0].Language == FormatStyle::LK_None) { 00282 Template = Seq[0]; 00283 } else { 00284 Template = *((const FormatStyle *)IO.getContext()); 00285 Template.Language = FormatStyle::LK_None; 00286 } 00287 Seq.resize(Index + 1, Template); 00288 } 00289 return Seq[Index]; 00290 } 00291 }; 00292 } 00293 } 00294 00295 namespace clang { 00296 namespace format { 00297 00298 const std::error_category &getParseCategory() { 00299 static ParseErrorCategory C; 00300 return C; 00301 } 00302 std::error_code make_error_code(ParseError e) { 00303 return std::error_code(static_cast<int>(e), getParseCategory()); 00304 } 00305 00306 const char *ParseErrorCategory::name() const LLVM_NOEXCEPT { 00307 return "clang-format.parse_error"; 00308 } 00309 00310 std::string ParseErrorCategory::message(int EV) const { 00311 switch (static_cast<ParseError>(EV)) { 00312 case ParseError::Success: 00313 return "Success"; 00314 case ParseError::Error: 00315 return "Invalid argument"; 00316 case ParseError::Unsuitable: 00317 return "Unsuitable"; 00318 } 00319 llvm_unreachable("unexpected parse error"); 00320 } 00321 00322 FormatStyle getLLVMStyle() { 00323 FormatStyle LLVMStyle; 00324 LLVMStyle.Language = FormatStyle::LK_Cpp; 00325 LLVMStyle.AccessModifierOffset = -2; 00326 LLVMStyle.AlignEscapedNewlinesLeft = false; 00327 LLVMStyle.AlignTrailingComments = true; 00328 LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; 00329 LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 00330 LLVMStyle.AllowShortBlocksOnASingleLine = false; 00331 LLVMStyle.AllowShortCaseLabelsOnASingleLine = false; 00332 LLVMStyle.AllowShortIfStatementsOnASingleLine = false; 00333 LLVMStyle.AllowShortLoopsOnASingleLine = false; 00334 LLVMStyle.AlwaysBreakAfterDefinitionReturnType = false; 00335 LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; 00336 LLVMStyle.AlwaysBreakTemplateDeclarations = false; 00337 LLVMStyle.BinPackParameters = true; 00338 LLVMStyle.BinPackArguments = true; 00339 LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 00340 LLVMStyle.BreakBeforeTernaryOperators = true; 00341 LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; 00342 LLVMStyle.BreakConstructorInitializersBeforeComma = false; 00343 LLVMStyle.ColumnLimit = 80; 00344 LLVMStyle.CommentPragmas = "^ IWYU pragma:"; 00345 LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 00346 LLVMStyle.ConstructorInitializerIndentWidth = 4; 00347 LLVMStyle.ContinuationIndentWidth = 4; 00348 LLVMStyle.Cpp11BracedListStyle = true; 00349 LLVMStyle.DerivePointerAlignment = false; 00350 LLVMStyle.ExperimentalAutoDetectBinPacking = false; 00351 LLVMStyle.ForEachMacros.push_back("foreach"); 00352 LLVMStyle.ForEachMacros.push_back("Q_FOREACH"); 00353 LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH"); 00354 LLVMStyle.IndentCaseLabels = false; 00355 LLVMStyle.IndentWrappedFunctionNames = false; 00356 LLVMStyle.IndentWidth = 2; 00357 LLVMStyle.TabWidth = 8; 00358 LLVMStyle.MaxEmptyLinesToKeep = 1; 00359 LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true; 00360 LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; 00361 LLVMStyle.ObjCBlockIndentWidth = 2; 00362 LLVMStyle.ObjCSpaceAfterProperty = false; 00363 LLVMStyle.ObjCSpaceBeforeProtocolList = true; 00364 LLVMStyle.PointerAlignment = FormatStyle::PAS_Right; 00365 LLVMStyle.SpacesBeforeTrailingComments = 1; 00366 LLVMStyle.Standard = FormatStyle::LS_Cpp11; 00367 LLVMStyle.UseTab = FormatStyle::UT_Never; 00368 LLVMStyle.SpacesInParentheses = false; 00369 LLVMStyle.SpacesInSquareBrackets = false; 00370 LLVMStyle.SpaceInEmptyParentheses = false; 00371 LLVMStyle.SpacesInContainerLiterals = true; 00372 LLVMStyle.SpacesInCStyleCastParentheses = false; 00373 LLVMStyle.SpaceAfterCStyleCast = false; 00374 LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; 00375 LLVMStyle.SpaceBeforeAssignmentOperators = true; 00376 LLVMStyle.SpacesInAngles = false; 00377 00378 LLVMStyle.PenaltyBreakComment = 300; 00379 LLVMStyle.PenaltyBreakFirstLessLess = 120; 00380 LLVMStyle.PenaltyBreakString = 1000; 00381 LLVMStyle.PenaltyExcessCharacter = 1000000; 00382 LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; 00383 LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19; 00384 00385 LLVMStyle.DisableFormat = false; 00386 00387 return LLVMStyle; 00388 } 00389 00390 FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { 00391 FormatStyle GoogleStyle = getLLVMStyle(); 00392 GoogleStyle.Language = Language; 00393 00394 GoogleStyle.AccessModifierOffset = -1; 00395 GoogleStyle.AlignEscapedNewlinesLeft = true; 00396 GoogleStyle.AllowShortIfStatementsOnASingleLine = true; 00397 GoogleStyle.AllowShortLoopsOnASingleLine = true; 00398 GoogleStyle.AlwaysBreakBeforeMultilineStrings = true; 00399 GoogleStyle.AlwaysBreakTemplateDeclarations = true; 00400 GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 00401 GoogleStyle.DerivePointerAlignment = true; 00402 GoogleStyle.IndentCaseLabels = true; 00403 GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false; 00404 GoogleStyle.ObjCSpaceAfterProperty = false; 00405 GoogleStyle.ObjCSpaceBeforeProtocolList = false; 00406 GoogleStyle.PointerAlignment = FormatStyle::PAS_Left; 00407 GoogleStyle.SpacesBeforeTrailingComments = 2; 00408 GoogleStyle.Standard = FormatStyle::LS_Auto; 00409 00410 GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; 00411 GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1; 00412 00413 if (Language == FormatStyle::LK_Java) { 00414 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 00415 GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 00416 GoogleStyle.ColumnLimit = 100; 00417 GoogleStyle.SpaceAfterCStyleCast = true; 00418 GoogleStyle.SpacesBeforeTrailingComments = 1; 00419 } else if (Language == FormatStyle::LK_JavaScript) { 00420 GoogleStyle.BreakBeforeTernaryOperators = false; 00421 GoogleStyle.MaxEmptyLinesToKeep = 3; 00422 GoogleStyle.SpacesInContainerLiterals = false; 00423 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 00424 } else if (Language == FormatStyle::LK_Proto) { 00425 GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 00426 GoogleStyle.SpacesInContainerLiterals = false; 00427 } 00428 00429 return GoogleStyle; 00430 } 00431 00432 FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { 00433 FormatStyle ChromiumStyle = getGoogleStyle(Language); 00434 ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; 00435 ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 00436 ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; 00437 ChromiumStyle.AllowShortLoopsOnASingleLine = false; 00438 ChromiumStyle.BinPackParameters = false; 00439 ChromiumStyle.DerivePointerAlignment = false; 00440 return ChromiumStyle; 00441 } 00442 00443 FormatStyle getMozillaStyle() { 00444 FormatStyle MozillaStyle = getLLVMStyle(); 00445 MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; 00446 MozillaStyle.Cpp11BracedListStyle = false; 00447 MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 00448 MozillaStyle.DerivePointerAlignment = true; 00449 MozillaStyle.IndentCaseLabels = true; 00450 MozillaStyle.ObjCSpaceAfterProperty = true; 00451 MozillaStyle.ObjCSpaceBeforeProtocolList = false; 00452 MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; 00453 MozillaStyle.PointerAlignment = FormatStyle::PAS_Left; 00454 MozillaStyle.Standard = FormatStyle::LS_Cpp03; 00455 return MozillaStyle; 00456 } 00457 00458 FormatStyle getWebKitStyle() { 00459 FormatStyle Style = getLLVMStyle(); 00460 Style.AccessModifierOffset = -4; 00461 Style.AlignTrailingComments = false; 00462 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 00463 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 00464 Style.BreakConstructorInitializersBeforeComma = true; 00465 Style.Cpp11BracedListStyle = false; 00466 Style.ColumnLimit = 0; 00467 Style.IndentWidth = 4; 00468 Style.NamespaceIndentation = FormatStyle::NI_Inner; 00469 Style.ObjCBlockIndentWidth = 4; 00470 Style.ObjCSpaceAfterProperty = true; 00471 Style.PointerAlignment = FormatStyle::PAS_Left; 00472 Style.Standard = FormatStyle::LS_Cpp03; 00473 return Style; 00474 } 00475 00476 FormatStyle getGNUStyle() { 00477 FormatStyle Style = getLLVMStyle(); 00478 Style.AlwaysBreakAfterDefinitionReturnType = true; 00479 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 00480 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 00481 Style.BreakBeforeTernaryOperators = true; 00482 Style.Cpp11BracedListStyle = false; 00483 Style.ColumnLimit = 79; 00484 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 00485 Style.Standard = FormatStyle::LS_Cpp03; 00486 return Style; 00487 } 00488 00489 FormatStyle getNoStyle() { 00490 FormatStyle NoStyle = getLLVMStyle(); 00491 NoStyle.DisableFormat = true; 00492 return NoStyle; 00493 } 00494 00495 bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language, 00496 FormatStyle *Style) { 00497 if (Name.equals_lower("llvm")) { 00498 *Style = getLLVMStyle(); 00499 } else if (Name.equals_lower("chromium")) { 00500 *Style = getChromiumStyle(Language); 00501 } else if (Name.equals_lower("mozilla")) { 00502 *Style = getMozillaStyle(); 00503 } else if (Name.equals_lower("google")) { 00504 *Style = getGoogleStyle(Language); 00505 } else if (Name.equals_lower("webkit")) { 00506 *Style = getWebKitStyle(); 00507 } else if (Name.equals_lower("gnu")) { 00508 *Style = getGNUStyle(); 00509 } else if (Name.equals_lower("none")) { 00510 *Style = getNoStyle(); 00511 } else { 00512 return false; 00513 } 00514 00515 Style->Language = Language; 00516 return true; 00517 } 00518 00519 std::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { 00520 assert(Style); 00521 FormatStyle::LanguageKind Language = Style->Language; 00522 assert(Language != FormatStyle::LK_None); 00523 if (Text.trim().empty()) 00524 return make_error_code(ParseError::Error); 00525 00526 std::vector<FormatStyle> Styles; 00527 llvm::yaml::Input Input(Text); 00528 // DocumentListTraits<vector<FormatStyle>> uses the context to get default 00529 // values for the fields, keys for which are missing from the configuration. 00530 // Mapping also uses the context to get the language to find the correct 00531 // base style. 00532 Input.setContext(Style); 00533 Input >> Styles; 00534 if (Input.error()) 00535 return Input.error(); 00536 00537 for (unsigned i = 0; i < Styles.size(); ++i) { 00538 // Ensures that only the first configuration can skip the Language option. 00539 if (Styles[i].Language == FormatStyle::LK_None && i != 0) 00540 return make_error_code(ParseError::Error); 00541 // Ensure that each language is configured at most once. 00542 for (unsigned j = 0; j < i; ++j) { 00543 if (Styles[i].Language == Styles[j].Language) { 00544 DEBUG(llvm::dbgs() 00545 << "Duplicate languages in the config file on positions " << j 00546 << " and " << i << "\n"); 00547 return make_error_code(ParseError::Error); 00548 } 00549 } 00550 } 00551 // Look for a suitable configuration starting from the end, so we can 00552 // find the configuration for the specific language first, and the default 00553 // configuration (which can only be at slot 0) after it. 00554 for (int i = Styles.size() - 1; i >= 0; --i) { 00555 if (Styles[i].Language == Language || 00556 Styles[i].Language == FormatStyle::LK_None) { 00557 *Style = Styles[i]; 00558 Style->Language = Language; 00559 return make_error_code(ParseError::Success); 00560 } 00561 } 00562 return make_error_code(ParseError::Unsuitable); 00563 } 00564 00565 std::string configurationAsText(const FormatStyle &Style) { 00566 std::string Text; 00567 llvm::raw_string_ostream Stream(Text); 00568 llvm::yaml::Output Output(Stream); 00569 // We use the same mapping method for input and output, so we need a non-const 00570 // reference here. 00571 FormatStyle NonConstStyle = Style; 00572 Output << NonConstStyle; 00573 return Stream.str(); 00574 } 00575 00576 namespace { 00577 00578 bool startsExternCBlock(const AnnotatedLine &Line) { 00579 const FormatToken *Next = Line.First->getNextNonComment(); 00580 const FormatToken *NextNext = Next ? Next->getNextNonComment() : nullptr; 00581 return Line.First->is(tok::kw_extern) && Next && Next->isStringLiteral() && 00582 NextNext && NextNext->is(tok::l_brace); 00583 } 00584 00585 class NoColumnLimitFormatter { 00586 public: 00587 NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {} 00588 00589 /// \brief Formats the line starting at \p State, simply keeping all of the 00590 /// input's line breaking decisions. 00591 void format(unsigned FirstIndent, const AnnotatedLine *Line) { 00592 LineState State = 00593 Indenter->getInitialState(FirstIndent, Line, /*DryRun=*/false); 00594 while (State.NextToken) { 00595 bool Newline = 00596 Indenter->mustBreak(State) || 00597 (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0); 00598 Indenter->addTokenToState(State, Newline, /*DryRun=*/false); 00599 } 00600 } 00601 00602 private: 00603 ContinuationIndenter *Indenter; 00604 }; 00605 00606 class LineJoiner { 00607 public: 00608 LineJoiner(const FormatStyle &Style) : Style(Style) {} 00609 00610 /// \brief Calculates how many lines can be merged into 1 starting at \p I. 00611 unsigned 00612 tryFitMultipleLinesInOne(unsigned Indent, 00613 SmallVectorImpl<AnnotatedLine *>::const_iterator I, 00614 SmallVectorImpl<AnnotatedLine *>::const_iterator E) { 00615 // We can never merge stuff if there are trailing line comments. 00616 const AnnotatedLine *TheLine = *I; 00617 if (TheLine->Last->Type == TT_LineComment) 00618 return 0; 00619 00620 if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit) 00621 return 0; 00622 00623 unsigned Limit = 00624 Style.ColumnLimit == 0 ? UINT_MAX : Style.ColumnLimit - Indent; 00625 // If we already exceed the column limit, we set 'Limit' to 0. The different 00626 // tryMerge..() functions can then decide whether to still do merging. 00627 Limit = TheLine->Last->TotalLength > Limit 00628 ? 0 00629 : Limit - TheLine->Last->TotalLength; 00630 00631 if (I + 1 == E || I[1]->Type == LT_Invalid || I[1]->First->MustBreakBefore) 00632 return 0; 00633 00634 // FIXME: TheLine->Level != 0 might or might not be the right check to do. 00635 // If necessary, change to something smarter. 00636 bool MergeShortFunctions = 00637 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All || 00638 (Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline && 00639 TheLine->Level != 0); 00640 00641 if (TheLine->Last->Type == TT_FunctionLBrace && 00642 TheLine->First != TheLine->Last) { 00643 return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0; 00644 } 00645 if (TheLine->Last->is(tok::l_brace)) { 00646 return Style.BreakBeforeBraces == FormatStyle::BS_Attach 00647 ? tryMergeSimpleBlock(I, E, Limit) 00648 : 0; 00649 } 00650 if (I[1]->First->Type == TT_FunctionLBrace && 00651 Style.BreakBeforeBraces != FormatStyle::BS_Attach) { 00652 // Check for Limit <= 2 to account for the " {". 00653 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine))) 00654 return 0; 00655 Limit -= 2; 00656 00657 unsigned MergedLines = 0; 00658 if (MergeShortFunctions) { 00659 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit); 00660 // If we managed to merge the block, count the function header, which is 00661 // on a separate line. 00662 if (MergedLines > 0) 00663 ++MergedLines; 00664 } 00665 return MergedLines; 00666 } 00667 if (TheLine->First->is(tok::kw_if)) { 00668 return Style.AllowShortIfStatementsOnASingleLine 00669 ? tryMergeSimpleControlStatement(I, E, Limit) 00670 : 0; 00671 } 00672 if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) { 00673 return Style.AllowShortLoopsOnASingleLine 00674 ? tryMergeSimpleControlStatement(I, E, Limit) 00675 : 0; 00676 } 00677 if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) { 00678 return Style.AllowShortCaseLabelsOnASingleLine 00679 ? tryMergeShortCaseLabels(I, E, Limit) 00680 : 0; 00681 } 00682 if (TheLine->InPPDirective && 00683 (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) { 00684 return tryMergeSimplePPDirective(I, E, Limit); 00685 } 00686 return 0; 00687 } 00688 00689 private: 00690 unsigned 00691 tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 00692 SmallVectorImpl<AnnotatedLine *>::const_iterator E, 00693 unsigned Limit) { 00694 if (Limit == 0) 00695 return 0; 00696 if (!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline) 00697 return 0; 00698 if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline) 00699 return 0; 00700 if (1 + I[1]->Last->TotalLength > Limit) 00701 return 0; 00702 return 1; 00703 } 00704 00705 unsigned tryMergeSimpleControlStatement( 00706 SmallVectorImpl<AnnotatedLine *>::const_iterator I, 00707 SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) { 00708 if (Limit == 0) 00709 return 0; 00710 if ((Style.BreakBeforeBraces == FormatStyle::BS_Allman || 00711 Style.BreakBeforeBraces == FormatStyle::BS_GNU) && 00712 (I[1]->First->is(tok::l_brace) && !Style.AllowShortBlocksOnASingleLine)) 00713 return 0; 00714 if (I[1]->InPPDirective != (*I)->InPPDirective || 00715 (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) 00716 return 0; 00717 Limit = limitConsideringMacros(I + 1, E, Limit); 00718 AnnotatedLine &Line = **I; 00719 if (Line.Last->isNot(tok::r_paren)) 00720 return 0; 00721 if (1 + I[1]->Last->TotalLength > Limit) 00722 return 0; 00723 if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, 00724 tok::kw_while) || 00725 I[1]->First->Type == TT_LineComment) 00726 return 0; 00727 // Only inline simple if's (no nested if or else). 00728 if (I + 2 != E && Line.First->is(tok::kw_if) && 00729 I[2]->First->is(tok::kw_else)) 00730 return 0; 00731 return 1; 00732 } 00733 00734 unsigned tryMergeShortCaseLabels( 00735 SmallVectorImpl<AnnotatedLine *>::const_iterator I, 00736 SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) { 00737 if (Limit == 0 || I + 1 == E || 00738 I[1]->First->isOneOf(tok::kw_case, tok::kw_default)) 00739 return 0; 00740 unsigned NumStmts = 0; 00741 unsigned Length = 0; 00742 for (; NumStmts < 3; ++NumStmts) { 00743 if (I + 1 + NumStmts == E) 00744 break; 00745 const AnnotatedLine *Line = I[1 + NumStmts]; 00746 if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace)) 00747 break; 00748 if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch, 00749 tok::kw_while)) 00750 return 0; 00751 Length += I[1 + NumStmts]->Last->TotalLength + 1; // 1 for the space. 00752 } 00753 if (NumStmts == 0 || NumStmts == 3 || Length > Limit) 00754 return 0; 00755 return NumStmts; 00756 } 00757 00758 unsigned 00759 tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 00760 SmallVectorImpl<AnnotatedLine *>::const_iterator E, 00761 unsigned Limit) { 00762 AnnotatedLine &Line = **I; 00763 00764 // Don't merge ObjC @ keywords and methods. 00765 if (Line.First->isOneOf(tok::at, tok::minus, tok::plus)) 00766 return 0; 00767 00768 // Check that the current line allows merging. This depends on whether we 00769 // are in a control flow statements as well as several style flags. 00770 if (Line.First->isOneOf(tok::kw_else, tok::kw_case)) 00771 return 0; 00772 if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try, 00773 tok::kw_catch, tok::kw_for, tok::r_brace)) { 00774 if (!Style.AllowShortBlocksOnASingleLine) 00775 return 0; 00776 if (!Style.AllowShortIfStatementsOnASingleLine && 00777 Line.First->is(tok::kw_if)) 00778 return 0; 00779 if (!Style.AllowShortLoopsOnASingleLine && 00780 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for)) 00781 return 0; 00782 // FIXME: Consider an option to allow short exception handling clauses on 00783 // a single line. 00784 if (Line.First->isOneOf(tok::kw_try, tok::kw_catch)) 00785 return 0; 00786 } 00787 00788 FormatToken *Tok = I[1]->First; 00789 if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore && 00790 (Tok->getNextNonComment() == nullptr || 00791 Tok->getNextNonComment()->is(tok::semi))) { 00792 // We merge empty blocks even if the line exceeds the column limit. 00793 Tok->SpacesRequiredBefore = 0; 00794 Tok->CanBreakBefore = true; 00795 return 1; 00796 } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace) && 00797 !startsExternCBlock(Line)) { 00798 // We don't merge short records. 00799 if (Line.First->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct)) 00800 return 0; 00801 00802 // Check that we still have three lines and they fit into the limit. 00803 if (I + 2 == E || I[2]->Type == LT_Invalid) 00804 return 0; 00805 Limit = limitConsideringMacros(I + 2, E, Limit); 00806 00807 if (!nextTwoLinesFitInto(I, Limit)) 00808 return 0; 00809 00810 // Second, check that the next line does not contain any braces - if it 00811 // does, readability declines when putting it into a single line. 00812 if (I[1]->Last->Type == TT_LineComment) 00813 return 0; 00814 do { 00815 if (Tok->is(tok::l_brace) && Tok->BlockKind != BK_BracedInit) 00816 return 0; 00817 Tok = Tok->Next; 00818 } while (Tok); 00819 00820 // Last, check that the third line starts with a closing brace. 00821 Tok = I[2]->First; 00822 if (Tok->isNot(tok::r_brace)) 00823 return 0; 00824 00825 return 2; 00826 } 00827 return 0; 00828 } 00829 00830 /// Returns the modified column limit for \p I if it is inside a macro and 00831 /// needs a trailing '\'. 00832 unsigned 00833 limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 00834 SmallVectorImpl<AnnotatedLine *>::const_iterator E, 00835 unsigned Limit) { 00836 if (I[0]->InPPDirective && I + 1 != E && 00837 !I[1]->First->HasUnescapedNewline && !I[1]->First->is(tok::eof)) { 00838 return Limit < 2 ? 0 : Limit - 2; 00839 } 00840 return Limit; 00841 } 00842 00843 bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 00844 unsigned Limit) { 00845 if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore) 00846 return false; 00847 return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit; 00848 } 00849 00850 bool containsMustBreak(const AnnotatedLine *Line) { 00851 for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) { 00852 if (Tok->MustBreakBefore) 00853 return true; 00854 } 00855 return false; 00856 } 00857 00858 const FormatStyle &Style; 00859 }; 00860 00861 class UnwrappedLineFormatter { 00862 public: 00863 UnwrappedLineFormatter(ContinuationIndenter *Indenter, 00864 WhitespaceManager *Whitespaces, 00865 const FormatStyle &Style) 00866 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), 00867 Joiner(Style) {} 00868 00869 unsigned format(const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun, 00870 int AdditionalIndent = 0, bool FixBadIndentation = false) { 00871 // Try to look up already computed penalty in DryRun-mode. 00872 std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned> CacheKey( 00873 &Lines, AdditionalIndent); 00874 auto CacheIt = PenaltyCache.find(CacheKey); 00875 if (DryRun && CacheIt != PenaltyCache.end()) 00876 return CacheIt->second; 00877 00878 assert(!Lines.empty()); 00879 unsigned Penalty = 0; 00880 std::vector<int> IndentForLevel; 00881 for (unsigned i = 0, e = Lines[0]->Level; i != e; ++i) 00882 IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent); 00883 const AnnotatedLine *PreviousLine = nullptr; 00884 for (SmallVectorImpl<AnnotatedLine *>::const_iterator I = Lines.begin(), 00885 E = Lines.end(); 00886 I != E; ++I) { 00887 const AnnotatedLine &TheLine = **I; 00888 const FormatToken *FirstTok = TheLine.First; 00889 int Offset = getIndentOffset(*FirstTok); 00890 00891 // Determine indent and try to merge multiple unwrapped lines. 00892 unsigned Indent; 00893 if (TheLine.InPPDirective) { 00894 Indent = TheLine.Level * Style.IndentWidth; 00895 } else { 00896 while (IndentForLevel.size() <= TheLine.Level) 00897 IndentForLevel.push_back(-1); 00898 IndentForLevel.resize(TheLine.Level + 1); 00899 Indent = getIndent(IndentForLevel, TheLine.Level); 00900 } 00901 unsigned LevelIndent = Indent; 00902 if (static_cast<int>(Indent) + Offset >= 0) 00903 Indent += Offset; 00904 00905 // Merge multiple lines if possible. 00906 unsigned MergedLines = Joiner.tryFitMultipleLinesInOne(Indent, I, E); 00907 if (MergedLines > 0 && Style.ColumnLimit == 0) { 00908 // Disallow line merging if there is a break at the start of one of the 00909 // input lines. 00910 for (unsigned i = 0; i < MergedLines; ++i) { 00911 if (I[i + 1]->First->NewlinesBefore > 0) 00912 MergedLines = 0; 00913 } 00914 } 00915 if (!DryRun) { 00916 for (unsigned i = 0; i < MergedLines; ++i) { 00917 join(*I[i], *I[i + 1]); 00918 } 00919 } 00920 I += MergedLines; 00921 00922 bool FixIndentation = 00923 FixBadIndentation && (LevelIndent != FirstTok->OriginalColumn); 00924 if (TheLine.First->is(tok::eof)) { 00925 if (PreviousLine && PreviousLine->Affected && !DryRun) { 00926 // Remove the file's trailing whitespace. 00927 unsigned Newlines = std::min(FirstTok->NewlinesBefore, 1u); 00928 Whitespaces->replaceWhitespace(*TheLine.First, Newlines, 00929 /*IndentLevel=*/0, /*Spaces=*/0, 00930 /*TargetColumn=*/0); 00931 } 00932 } else if (TheLine.Type != LT_Invalid && 00933 (TheLine.Affected || FixIndentation)) { 00934 if (FirstTok->WhitespaceRange.isValid()) { 00935 if (!DryRun) 00936 formatFirstToken(*TheLine.First, PreviousLine, TheLine.Level, 00937 Indent, TheLine.InPPDirective); 00938 } else { 00939 Indent = LevelIndent = FirstTok->OriginalColumn; 00940 } 00941 00942 // If everything fits on a single line, just put it there. 00943 unsigned ColumnLimit = Style.ColumnLimit; 00944 if (I + 1 != E) { 00945 AnnotatedLine *NextLine = I[1]; 00946 if (NextLine->InPPDirective && !NextLine->First->HasUnescapedNewline) 00947 ColumnLimit = getColumnLimit(TheLine.InPPDirective); 00948 } 00949 00950 if (TheLine.Last->TotalLength + Indent <= ColumnLimit) { 00951 LineState State = Indenter->getInitialState(Indent, &TheLine, DryRun); 00952 while (State.NextToken) { 00953 formatChildren(State, /*Newline=*/false, /*DryRun=*/false, Penalty); 00954 Indenter->addTokenToState(State, /*Newline=*/false, DryRun); 00955 } 00956 } else if (Style.ColumnLimit == 0) { 00957 // FIXME: Implement nested blocks for ColumnLimit = 0. 00958 NoColumnLimitFormatter Formatter(Indenter); 00959 if (!DryRun) 00960 Formatter.format(Indent, &TheLine); 00961 } else { 00962 Penalty += format(TheLine, Indent, DryRun); 00963 } 00964 00965 if (!TheLine.InPPDirective) 00966 IndentForLevel[TheLine.Level] = LevelIndent; 00967 } else if (TheLine.ChildrenAffected) { 00968 format(TheLine.Children, DryRun); 00969 } else { 00970 // Format the first token if necessary, and notify the WhitespaceManager 00971 // about the unchanged whitespace. 00972 for (FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next) { 00973 if (Tok == TheLine.First && 00974 (Tok->NewlinesBefore > 0 || Tok->IsFirst)) { 00975 unsigned LevelIndent = Tok->OriginalColumn; 00976 if (!DryRun) { 00977 // Remove trailing whitespace of the previous line. 00978 if ((PreviousLine && PreviousLine->Affected) || 00979 TheLine.LeadingEmptyLinesAffected) { 00980 formatFirstToken(*Tok, PreviousLine, TheLine.Level, LevelIndent, 00981 TheLine.InPPDirective); 00982 } else { 00983 Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective); 00984 } 00985 } 00986 00987 if (static_cast<int>(LevelIndent) - Offset >= 0) 00988 LevelIndent -= Offset; 00989 if (Tok->isNot(tok::comment) && !TheLine.InPPDirective) 00990 IndentForLevel[TheLine.Level] = LevelIndent; 00991 } else if (!DryRun) { 00992 Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective); 00993 } 00994 } 00995 } 00996 if (!DryRun) { 00997 for (FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next) { 00998 Tok->Finalized = true; 00999 } 01000 } 01001 PreviousLine = *I; 01002 } 01003 PenaltyCache[CacheKey] = Penalty; 01004 return Penalty; 01005 } 01006 01007 private: 01008 /// \brief Formats an \c AnnotatedLine and returns the penalty. 01009 /// 01010 /// If \p DryRun is \c false, directly applies the changes. 01011 unsigned format(const AnnotatedLine &Line, unsigned FirstIndent, 01012 bool DryRun) { 01013 LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun); 01014 01015 // If the ObjC method declaration does not fit on a line, we should format 01016 // it with one arg per line. 01017 if (State.Line->Type == LT_ObjCMethodDecl) 01018 State.Stack.back().BreakBeforeParameter = true; 01019 01020 // Find best solution in solution space. 01021 return analyzeSolutionSpace(State, DryRun); 01022 } 01023 01024 /// \brief An edge in the solution space from \c Previous->State to \c State, 01025 /// inserting a newline dependent on the \c NewLine. 01026 struct StateNode { 01027 StateNode(const LineState &State, bool NewLine, StateNode *Previous) 01028 : State(State), NewLine(NewLine), Previous(Previous) {} 01029 LineState State; 01030 bool NewLine; 01031 StateNode *Previous; 01032 }; 01033 01034 /// \brief A pair of <penalty, count> that is used to prioritize the BFS on. 01035 /// 01036 /// In case of equal penalties, we want to prefer states that were inserted 01037 /// first. During state generation we make sure that we insert states first 01038 /// that break the line as late as possible. 01039 typedef std::pair<unsigned, unsigned> OrderedPenalty; 01040 01041 /// \brief An item in the prioritized BFS search queue. The \c StateNode's 01042 /// \c State has the given \c OrderedPenalty. 01043 typedef std::pair<OrderedPenalty, StateNode *> QueueItem; 01044 01045 /// \brief The BFS queue type. 01046 typedef std::priority_queue<QueueItem, std::vector<QueueItem>, 01047 std::greater<QueueItem> > QueueType; 01048 01049 /// \brief Get the offset of the line relatively to the level. 01050 /// 01051 /// For example, 'public:' labels in classes are offset by 1 or 2 01052 /// characters to the left from their level. 01053 int getIndentOffset(const FormatToken &RootToken) { 01054 if (Style.Language == FormatStyle::LK_Java) 01055 return 0; 01056 if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier()) 01057 return Style.AccessModifierOffset; 01058 return 0; 01059 } 01060 01061 /// \brief Add a new line and the required indent before the first Token 01062 /// of the \c UnwrappedLine if there was no structural parsing error. 01063 void formatFirstToken(FormatToken &RootToken, 01064 const AnnotatedLine *PreviousLine, unsigned IndentLevel, 01065 unsigned Indent, bool InPPDirective) { 01066 unsigned Newlines = 01067 std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); 01068 // Remove empty lines before "}" where applicable. 01069 if (RootToken.is(tok::r_brace) && 01070 (!RootToken.Next || 01071 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next))) 01072 Newlines = std::min(Newlines, 1u); 01073 if (Newlines == 0 && !RootToken.IsFirst) 01074 Newlines = 1; 01075 if (RootToken.IsFirst && !RootToken.HasUnescapedNewline) 01076 Newlines = 0; 01077 01078 // Remove empty lines after "{". 01079 if (!Style.KeepEmptyLinesAtTheStartOfBlocks && PreviousLine && 01080 PreviousLine->Last->is(tok::l_brace) && 01081 PreviousLine->First->isNot(tok::kw_namespace) && 01082 !startsExternCBlock(*PreviousLine)) 01083 Newlines = 1; 01084 01085 // Insert extra new line before access specifiers. 01086 if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) && 01087 RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1) 01088 ++Newlines; 01089 01090 // Remove empty lines after access specifiers. 01091 if (PreviousLine && PreviousLine->First->isAccessSpecifier()) 01092 Newlines = std::min(1u, Newlines); 01093 01094 Whitespaces->replaceWhitespace(RootToken, Newlines, IndentLevel, Indent, 01095 Indent, InPPDirective && 01096 !RootToken.HasUnescapedNewline); 01097 } 01098 01099 /// \brief Get the indent of \p Level from \p IndentForLevel. 01100 /// 01101 /// \p IndentForLevel must contain the indent for the level \c l 01102 /// at \p IndentForLevel[l], or a value < 0 if the indent for 01103 /// that level is unknown. 01104 unsigned getIndent(ArrayRef<int> IndentForLevel, unsigned Level) { 01105 if (IndentForLevel[Level] != -1) 01106 return IndentForLevel[Level]; 01107 if (Level == 0) 01108 return 0; 01109 return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth; 01110 } 01111 01112 void join(AnnotatedLine &A, const AnnotatedLine &B) { 01113 assert(!A.Last->Next); 01114 assert(!B.First->Previous); 01115 if (B.Affected) 01116 A.Affected = true; 01117 A.Last->Next = B.First; 01118 B.First->Previous = A.Last; 01119 B.First->CanBreakBefore = true; 01120 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore; 01121 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) { 01122 Tok->TotalLength += LengthA; 01123 A.Last = Tok; 01124 } 01125 } 01126 01127 unsigned getColumnLimit(bool InPPDirective) const { 01128 // In preprocessor directives reserve two chars for trailing " \" 01129 return Style.ColumnLimit - (InPPDirective ? 2 : 0); 01130 } 01131 01132 struct CompareLineStatePointers { 01133 bool operator()(LineState *obj1, LineState *obj2) const { 01134 return *obj1 < *obj2; 01135 } 01136 }; 01137 01138 /// \brief Analyze the entire solution space starting from \p InitialState. 01139 /// 01140 /// This implements a variant of Dijkstra's algorithm on the graph that spans 01141 /// the solution space (\c LineStates are the nodes). The algorithm tries to 01142 /// find the shortest path (the one with lowest penalty) from \p InitialState 01143 /// to a state where all tokens are placed. Returns the penalty. 01144 /// 01145 /// If \p DryRun is \c false, directly applies the changes. 01146 unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun = false) { 01147 std::set<LineState *, CompareLineStatePointers> Seen; 01148 01149 // Increasing count of \c StateNode items we have created. This is used to 01150 // create a deterministic order independent of the container. 01151 unsigned Count = 0; 01152 QueueType Queue; 01153 01154 // Insert start element into queue. 01155 StateNode *Node = 01156 new (Allocator.Allocate()) StateNode(InitialState, false, nullptr); 01157 Queue.push(QueueItem(OrderedPenalty(0, Count), Node)); 01158 ++Count; 01159 01160 unsigned Penalty = 0; 01161 01162 // While not empty, take first element and follow edges. 01163 while (!Queue.empty()) { 01164 Penalty = Queue.top().first.first; 01165 StateNode *Node = Queue.top().second; 01166 if (!Node->State.NextToken) { 01167 DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n"); 01168 break; 01169 } 01170 Queue.pop(); 01171 01172 // Cut off the analysis of certain solutions if the analysis gets too 01173 // complex. See description of IgnoreStackForComparison. 01174 if (Count > 10000) 01175 Node->State.IgnoreStackForComparison = true; 01176 01177 if (!Seen.insert(&Node->State).second) 01178 // State already examined with lower penalty. 01179 continue; 01180 01181 FormatDecision LastFormat = Node->State.NextToken->Decision; 01182 if (LastFormat == FD_Unformatted || LastFormat == FD_Continue) 01183 addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue); 01184 if (LastFormat == FD_Unformatted || LastFormat == FD_Break) 01185 addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue); 01186 } 01187 01188 if (Queue.empty()) { 01189 // We were unable to find a solution, do nothing. 01190 // FIXME: Add diagnostic? 01191 DEBUG(llvm::dbgs() << "Could not find a solution.\n"); 01192 return 0; 01193 } 01194 01195 // Reconstruct the solution. 01196 if (!DryRun) 01197 reconstructPath(InitialState, Queue.top().second); 01198 01199 DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n"); 01200 DEBUG(llvm::dbgs() << "---\n"); 01201 01202 return Penalty; 01203 } 01204 01205 void reconstructPath(LineState &State, StateNode *Current) { 01206 std::deque<StateNode *> Path; 01207 // We do not need a break before the initial token. 01208 while (Current->Previous) { 01209 Path.push_front(Current); 01210 Current = Current->Previous; 01211 } 01212 for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end(); 01213 I != E; ++I) { 01214 unsigned Penalty = 0; 01215 formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty); 01216 Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false); 01217 01218 DEBUG({ 01219 if ((*I)->NewLine) { 01220 llvm::dbgs() << "Penalty for placing " 01221 << (*I)->Previous->State.NextToken->Tok.getName() << ": " 01222 << Penalty << "\n"; 01223 } 01224 }); 01225 } 01226 } 01227 01228 /// \brief Add the following state to the analysis queue \c Queue. 01229 /// 01230 /// Assume the current state is \p PreviousNode and has been reached with a 01231 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true. 01232 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode, 01233 bool NewLine, unsigned *Count, QueueType *Queue) { 01234 if (NewLine && !Indenter->canBreak(PreviousNode->State)) 01235 return; 01236 if (!NewLine && Indenter->mustBreak(PreviousNode->State)) 01237 return; 01238 01239 StateNode *Node = new (Allocator.Allocate()) 01240 StateNode(PreviousNode->State, NewLine, PreviousNode); 01241 if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty)) 01242 return; 01243 01244 Penalty += Indenter->addTokenToState(Node->State, NewLine, true); 01245 01246 Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node)); 01247 ++(*Count); 01248 } 01249 01250 /// \brief If the \p State's next token is an r_brace closing a nested block, 01251 /// format the nested block before it. 01252 /// 01253 /// Returns \c true if all children could be placed successfully and adapts 01254 /// \p Penalty as well as \p State. If \p DryRun is false, also directly 01255 /// creates changes using \c Whitespaces. 01256 /// 01257 /// The crucial idea here is that children always get formatted upon 01258 /// encountering the closing brace right after the nested block. Now, if we 01259 /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is 01260 /// \c false), the entire block has to be kept on the same line (which is only 01261 /// possible if it fits on the line, only contains a single statement, etc. 01262 /// 01263 /// If \p NewLine is true, we format the nested block on separate lines, i.e. 01264 /// break after the "{", format all lines with correct indentation and the put 01265 /// the closing "}" on yet another new line. 01266 /// 01267 /// This enables us to keep the simple structure of the 01268 /// \c UnwrappedLineFormatter, where we only have two options for each token: 01269 /// break or don't break. 01270 bool formatChildren(LineState &State, bool NewLine, bool DryRun, 01271 unsigned &Penalty) { 01272 FormatToken &Previous = *State.NextToken->Previous; 01273 const FormatToken *LBrace = State.NextToken->getPreviousNonComment(); 01274 if (!LBrace || LBrace->isNot(tok::l_brace) || 01275 LBrace->BlockKind != BK_Block || Previous.Children.size() == 0) 01276 // The previous token does not open a block. Nothing to do. We don't 01277 // assert so that we can simply call this function for all tokens. 01278 return true; 01279 01280 if (NewLine) { 01281 int AdditionalIndent = 01282 State.FirstIndent - State.Line->Level * Style.IndentWidth; 01283 if (State.Stack.size() < 2 || 01284 !State.Stack[State.Stack.size() - 2].JSFunctionInlined) { 01285 AdditionalIndent = State.Stack.back().Indent - 01286 Previous.Children[0]->Level * Style.IndentWidth; 01287 } 01288 01289 Penalty += format(Previous.Children, DryRun, AdditionalIndent, 01290 /*FixBadIndentation=*/true); 01291 return true; 01292 } 01293 01294 if (Previous.Children[0]->First->MustBreakBefore) 01295 return false; 01296 01297 // Cannot merge multiple statements into a single line. 01298 if (Previous.Children.size() > 1) 01299 return false; 01300 01301 // Cannot merge into one line if this line ends on a comment. 01302 if (Previous.is(tok::comment)) 01303 return false; 01304 01305 // We can't put the closing "}" on a line with a trailing comment. 01306 if (Previous.Children[0]->Last->isTrailingComment()) 01307 return false; 01308 01309 // If the child line exceeds the column limit, we wouldn't want to merge it. 01310 // We add +2 for the trailing " }". 01311 if (Style.ColumnLimit > 0 && 01312 Previous.Children[0]->Last->TotalLength + State.Column + 2 > 01313 Style.ColumnLimit) 01314 return false; 01315 01316 if (!DryRun) { 01317 Whitespaces->replaceWhitespace( 01318 *Previous.Children[0]->First, 01319 /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1, 01320 /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective); 01321 } 01322 Penalty += format(*Previous.Children[0], State.Column + 1, DryRun); 01323 01324 State.Column += 1 + Previous.Children[0]->Last->TotalLength; 01325 return true; 01326 } 01327 01328 ContinuationIndenter *Indenter; 01329 WhitespaceManager *Whitespaces; 01330 FormatStyle Style; 01331 LineJoiner Joiner; 01332 01333 llvm::SpecificBumpPtrAllocator<StateNode> Allocator; 01334 01335 // Cache to store the penalty of formatting a vector of AnnotatedLines 01336 // starting from a specific additional offset. Improves performance if there 01337 // are many nested blocks. 01338 std::map<std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned>, 01339 unsigned> PenaltyCache; 01340 }; 01341 01342 class FormatTokenLexer { 01343 public: 01344 FormatTokenLexer(SourceManager &SourceMgr, FileID ID, FormatStyle &Style, 01345 encoding::Encoding Encoding) 01346 : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false), 01347 Column(0), TrailingWhitespace(0), SourceMgr(SourceMgr), ID(ID), 01348 Style(Style), IdentTable(getFormattingLangOpts(Style)), 01349 Keywords(IdentTable), Encoding(Encoding), FirstInLineIndex(0), 01350 FormattingDisabled(false) { 01351 Lex.reset(new Lexer(ID, SourceMgr.getBuffer(ID), SourceMgr, 01352 getFormattingLangOpts(Style))); 01353 Lex->SetKeepWhitespaceMode(true); 01354 01355 for (const std::string &ForEachMacro : Style.ForEachMacros) 01356 ForEachMacros.push_back(&IdentTable.get(ForEachMacro)); 01357 std::sort(ForEachMacros.begin(), ForEachMacros.end()); 01358 } 01359 01360 ArrayRef<FormatToken *> lex() { 01361 assert(Tokens.empty()); 01362 assert(FirstInLineIndex == 0); 01363 do { 01364 Tokens.push_back(getNextToken()); 01365 tryMergePreviousTokens(); 01366 if (Tokens.back()->NewlinesBefore > 0) 01367 FirstInLineIndex = Tokens.size() - 1; 01368 } while (Tokens.back()->Tok.isNot(tok::eof)); 01369 return Tokens; 01370 } 01371 01372 const AdditionalKeywords &getKeywords() { return Keywords; } 01373 01374 private: 01375 void tryMergePreviousTokens() { 01376 if (tryMerge_TMacro()) 01377 return; 01378 if (tryMergeConflictMarkers()) 01379 return; 01380 01381 if (Style.Language == FormatStyle::LK_JavaScript) { 01382 if (tryMergeJSRegexLiteral()) 01383 return; 01384 if (tryMergeEscapeSequence()) 01385 return; 01386 01387 static tok::TokenKind JSIdentity[] = { tok::equalequal, tok::equal }; 01388 static tok::TokenKind JSNotIdentity[] = { tok::exclaimequal, tok::equal }; 01389 static tok::TokenKind JSShiftEqual[] = { tok::greater, tok::greater, 01390 tok::greaterequal }; 01391 static tok::TokenKind JSRightArrow[] = { tok::equal, tok::greater }; 01392 // FIXME: We probably need to change token type to mimic operator with the 01393 // correct priority. 01394 if (tryMergeTokens(JSIdentity)) 01395 return; 01396 if (tryMergeTokens(JSNotIdentity)) 01397 return; 01398 if (tryMergeTokens(JSShiftEqual)) 01399 return; 01400 if (tryMergeTokens(JSRightArrow)) 01401 return; 01402 } 01403 } 01404 01405 bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds) { 01406 if (Tokens.size() < Kinds.size()) 01407 return false; 01408 01409 SmallVectorImpl<FormatToken *>::const_iterator First = 01410 Tokens.end() - Kinds.size(); 01411 if (!First[0]->is(Kinds[0])) 01412 return false; 01413 unsigned AddLength = 0; 01414 for (unsigned i = 1; i < Kinds.size(); ++i) { 01415 if (!First[i]->is(Kinds[i]) || First[i]->WhitespaceRange.getBegin() != 01416 First[i]->WhitespaceRange.getEnd()) 01417 return false; 01418 AddLength += First[i]->TokenText.size(); 01419 } 01420 Tokens.resize(Tokens.size() - Kinds.size() + 1); 01421 First[0]->TokenText = StringRef(First[0]->TokenText.data(), 01422 First[0]->TokenText.size() + AddLength); 01423 First[0]->ColumnWidth += AddLength; 01424 return true; 01425 } 01426 01427 // Tries to merge an escape sequence, i.e. a "\\" and the following 01428 // character. Use e.g. inside JavaScript regex literals. 01429 bool tryMergeEscapeSequence() { 01430 if (Tokens.size() < 2) 01431 return false; 01432 FormatToken *Previous = Tokens[Tokens.size() - 2]; 01433 if (Previous->isNot(tok::unknown) || Previous->TokenText != "\\") 01434 return false; 01435 ++Previous->ColumnWidth; 01436 StringRef Text = Previous->TokenText; 01437 Previous->TokenText = StringRef(Text.data(), Text.size() + 1); 01438 resetLexer(SourceMgr.getFileOffset(Tokens.back()->Tok.getLocation()) + 1); 01439 Tokens.resize(Tokens.size() - 1); 01440 Column = Previous->OriginalColumn + Previous->ColumnWidth; 01441 return true; 01442 } 01443 01444 // Try to determine whether the current token ends a JavaScript regex literal. 01445 // We heuristically assume that this is a regex literal if we find two 01446 // unescaped slashes on a line and the token before the first slash is one of 01447 // "(;,{}![:?", a binary operator or 'return', as those cannot be followed by 01448 // a division. 01449 bool tryMergeJSRegexLiteral() { 01450 if (Tokens.size() < 2) 01451 return false; 01452 // If a regex literal ends in "\//", this gets represented by an unknown 01453 // token "\" and a comment. 01454 bool MightEndWithEscapedSlash = 01455 Tokens.back()->is(tok::comment) && 01456 Tokens.back()->TokenText.startswith("//") && 01457 Tokens[Tokens.size() - 2]->TokenText == "\\"; 01458 if (!MightEndWithEscapedSlash && 01459 (Tokens.back()->isNot(tok::slash) || 01460 (Tokens[Tokens.size() - 2]->is(tok::unknown) && 01461 Tokens[Tokens.size() - 2]->TokenText == "\\"))) 01462 return false; 01463 unsigned TokenCount = 0; 01464 unsigned LastColumn = Tokens.back()->OriginalColumn; 01465 for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; ++I) { 01466 ++TokenCount; 01467 if (I[0]->is(tok::slash) && I + 1 != E && 01468 (I[1]->isOneOf(tok::l_paren, tok::semi, tok::l_brace, tok::r_brace, 01469 tok::exclaim, tok::l_square, tok::colon, tok::comma, 01470 tok::question, tok::kw_return) || 01471 I[1]->isBinaryOperator())) { 01472 if (MightEndWithEscapedSlash) { 01473 // This regex literal ends in '\//'. Skip past the '//' of the last 01474 // token and re-start lexing from there. 01475 SourceLocation Loc = Tokens.back()->Tok.getLocation(); 01476 resetLexer(SourceMgr.getFileOffset(Loc) + 2); 01477 } 01478 Tokens.resize(Tokens.size() - TokenCount); 01479 Tokens.back()->Tok.setKind(tok::unknown); 01480 Tokens.back()->Type = TT_RegexLiteral; 01481 Tokens.back()->ColumnWidth += LastColumn - I[0]->OriginalColumn; 01482 return true; 01483 } 01484 01485 // There can't be a newline inside a regex literal. 01486 if (I[0]->NewlinesBefore > 0) 01487 return false; 01488 } 01489 return false; 01490 } 01491 01492 bool tryMerge_TMacro() { 01493 if (Tokens.size() < 4) 01494 return false; 01495 FormatToken *Last = Tokens.back(); 01496 if (!Last->is(tok::r_paren)) 01497 return false; 01498 01499 FormatToken *String = Tokens[Tokens.size() - 2]; 01500 if (!String->is(tok::string_literal) || String->IsMultiline) 01501 return false; 01502 01503 if (!Tokens[Tokens.size() - 3]->is(tok::l_paren)) 01504 return false; 01505 01506 FormatToken *Macro = Tokens[Tokens.size() - 4]; 01507 if (Macro->TokenText != "_T") 01508 return false; 01509 01510 const char *Start = Macro->TokenText.data(); 01511 const char *End = Last->TokenText.data() + Last->TokenText.size(); 01512 String->TokenText = StringRef(Start, End - Start); 01513 String->IsFirst = Macro->IsFirst; 01514 String->LastNewlineOffset = Macro->LastNewlineOffset; 01515 String->WhitespaceRange = Macro->WhitespaceRange; 01516 String->OriginalColumn = Macro->OriginalColumn; 01517 String->ColumnWidth = encoding::columnWidthWithTabs( 01518 String->TokenText, String->OriginalColumn, Style.TabWidth, Encoding); 01519 01520 Tokens.pop_back(); 01521 Tokens.pop_back(); 01522 Tokens.pop_back(); 01523 Tokens.back() = String; 01524 return true; 01525 } 01526 01527 bool tryMergeConflictMarkers() { 01528 if (Tokens.back()->NewlinesBefore == 0 && Tokens.back()->isNot(tok::eof)) 01529 return false; 01530 01531 // Conflict lines look like: 01532 // <marker> <text from the vcs> 01533 // For example: 01534 // >>>>>>> /file/in/file/system at revision 1234 01535 // 01536 // We merge all tokens in a line that starts with a conflict marker 01537 // into a single token with a special token type that the unwrapped line 01538 // parser will use to correctly rebuild the underlying code. 01539 01540 FileID ID; 01541 // Get the position of the first token in the line. 01542 unsigned FirstInLineOffset; 01543 std::tie(ID, FirstInLineOffset) = SourceMgr.getDecomposedLoc( 01544 Tokens[FirstInLineIndex]->getStartOfNonWhitespace()); 01545 StringRef Buffer = SourceMgr.getBuffer(ID)->getBuffer(); 01546 // Calculate the offset of the start of the current line. 01547 auto LineOffset = Buffer.rfind('\n', FirstInLineOffset); 01548 if (LineOffset == StringRef::npos) { 01549 LineOffset = 0; 01550 } else { 01551 ++LineOffset; 01552 } 01553 01554 auto FirstSpace = Buffer.find_first_of(" \n", LineOffset); 01555 StringRef LineStart; 01556 if (FirstSpace == StringRef::npos) { 01557 LineStart = Buffer.substr(LineOffset); 01558 } else { 01559 LineStart = Buffer.substr(LineOffset, FirstSpace - LineOffset); 01560 } 01561 01562 TokenType Type = TT_Unknown; 01563 if (LineStart == "<<<<<<<" || LineStart == ">>>>") { 01564 Type = TT_ConflictStart; 01565 } else if (LineStart == "|||||||" || LineStart == "=======" || 01566 LineStart == "====") { 01567 Type = TT_ConflictAlternative; 01568 } else if (LineStart == ">>>>>>>" || LineStart == "<<<<") { 01569 Type = TT_ConflictEnd; 01570 } 01571 01572 if (Type != TT_Unknown) { 01573 FormatToken *Next = Tokens.back(); 01574 01575 Tokens.resize(FirstInLineIndex + 1); 01576 // We do not need to build a complete token here, as we will skip it 01577 // during parsing anyway (as we must not touch whitespace around conflict 01578 // markers). 01579 Tokens.back()->Type = Type; 01580 Tokens.back()->Tok.setKind(tok::kw___unknown_anytype); 01581 01582 Tokens.push_back(Next); 01583 return true; 01584 } 01585 01586 return false; 01587 } 01588 01589 FormatToken *getNextToken() { 01590 if (GreaterStashed) { 01591 // Create a synthesized second '>' token. 01592 // FIXME: Increment Column and set OriginalColumn. 01593 Token Greater = FormatTok->Tok; 01594 FormatTok = new (Allocator.Allocate()) FormatToken; 01595 FormatTok->Tok = Greater; 01596 SourceLocation GreaterLocation = 01597 FormatTok->Tok.getLocation().getLocWithOffset(1); 01598 FormatTok->WhitespaceRange = 01599 SourceRange(GreaterLocation, GreaterLocation); 01600 FormatTok->TokenText = ">"; 01601 FormatTok->ColumnWidth = 1; 01602 GreaterStashed = false; 01603 return FormatTok; 01604 } 01605 01606 FormatTok = new (Allocator.Allocate()) FormatToken; 01607 readRawToken(*FormatTok); 01608 SourceLocation WhitespaceStart = 01609 FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace); 01610 FormatTok->IsFirst = IsFirstToken; 01611 IsFirstToken = false; 01612 01613 // Consume and record whitespace until we find a significant token. 01614 unsigned WhitespaceLength = TrailingWhitespace; 01615 while (FormatTok->Tok.is(tok::unknown)) { 01616 for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) { 01617 switch (FormatTok->TokenText[i]) { 01618 case '\n': 01619 ++FormatTok->NewlinesBefore; 01620 // FIXME: This is technically incorrect, as it could also 01621 // be a literal backslash at the end of the line. 01622 if (i == 0 || (FormatTok->TokenText[i - 1] != '\\' && 01623 (FormatTok->TokenText[i - 1] != '\r' || i == 1 || 01624 FormatTok->TokenText[i - 2] != '\\'))) 01625 FormatTok->HasUnescapedNewline = true; 01626 FormatTok->LastNewlineOffset = WhitespaceLength + i + 1; 01627 Column = 0; 01628 break; 01629 case '\r': 01630 case '\f': 01631 case '\v': 01632 Column = 0; 01633 break; 01634 case ' ': 01635 ++Column; 01636 break; 01637 case '\t': 01638 Column += Style.TabWidth - Column % Style.TabWidth; 01639 break; 01640 case '\\': 01641 ++Column; 01642 if (i + 1 == e || (FormatTok->TokenText[i + 1] != '\r' && 01643 FormatTok->TokenText[i + 1] != '\n')) 01644 FormatTok->Type = TT_ImplicitStringLiteral; 01645 break; 01646 default: 01647 FormatTok->Type = TT_ImplicitStringLiteral; 01648 ++Column; 01649 break; 01650 } 01651 } 01652 01653 if (FormatTok->Type == TT_ImplicitStringLiteral) 01654 break; 01655 WhitespaceLength += FormatTok->Tok.getLength(); 01656 01657 readRawToken(*FormatTok); 01658 } 01659 01660 // In case the token starts with escaped newlines, we want to 01661 // take them into account as whitespace - this pattern is quite frequent 01662 // in macro definitions. 01663 // FIXME: Add a more explicit test. 01664 while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' && 01665 FormatTok->TokenText[1] == '\n') { 01666 ++FormatTok->NewlinesBefore; 01667 WhitespaceLength += 2; 01668 Column = 0; 01669 FormatTok->TokenText = FormatTok->TokenText.substr(2); 01670 } 01671 01672 FormatTok->WhitespaceRange = SourceRange( 01673 WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength)); 01674 01675 FormatTok->OriginalColumn = Column; 01676 01677 TrailingWhitespace = 0; 01678 if (FormatTok->Tok.is(tok::comment)) { 01679 // FIXME: Add the trimmed whitespace to Column. 01680 StringRef UntrimmedText = FormatTok->TokenText; 01681 FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f"); 01682 TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size(); 01683 } else if (FormatTok->Tok.is(tok::raw_identifier)) { 01684 IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText); 01685 FormatTok->Tok.setIdentifierInfo(&Info); 01686 FormatTok->Tok.setKind(Info.getTokenID()); 01687 } else if (FormatTok->Tok.is(tok::greatergreater)) { 01688 FormatTok->Tok.setKind(tok::greater); 01689 FormatTok->TokenText = FormatTok->TokenText.substr(0, 1); 01690 GreaterStashed = true; 01691 } 01692 01693 // Now FormatTok is the next non-whitespace token. 01694 01695 StringRef Text = FormatTok->TokenText; 01696 size_t FirstNewlinePos = Text.find('\n'); 01697 if (FirstNewlinePos == StringRef::npos) { 01698 // FIXME: ColumnWidth actually depends on the start column, we need to 01699 // take this into account when the token is moved. 01700 FormatTok->ColumnWidth = 01701 encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding); 01702 Column += FormatTok->ColumnWidth; 01703 } else { 01704 FormatTok->IsMultiline = true; 01705 // FIXME: ColumnWidth actually depends on the start column, we need to 01706 // take this into account when the token is moved. 01707 FormatTok->ColumnWidth = encoding::columnWidthWithTabs( 01708 Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding); 01709 01710 // The last line of the token always starts in column 0. 01711 // Thus, the length can be precomputed even in the presence of tabs. 01712 FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs( 01713 Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth, 01714 Encoding); 01715 Column = FormatTok->LastLineColumnWidth; 01716 } 01717 01718 FormatTok->IsForEachMacro = 01719 std::binary_search(ForEachMacros.begin(), ForEachMacros.end(), 01720 FormatTok->Tok.getIdentifierInfo()); 01721 01722 return FormatTok; 01723 } 01724 01725 FormatToken *FormatTok; 01726 bool IsFirstToken; 01727 bool GreaterStashed; 01728 unsigned Column; 01729 unsigned TrailingWhitespace; 01730 std::unique_ptr<Lexer> Lex; 01731 SourceManager &SourceMgr; 01732 FileID ID; 01733 FormatStyle &Style; 01734 IdentifierTable IdentTable; 01735 AdditionalKeywords Keywords; 01736 encoding::Encoding Encoding; 01737 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; 01738 // Index (in 'Tokens') of the last token that starts a new line. 01739 unsigned FirstInLineIndex; 01740 SmallVector<FormatToken *, 16> Tokens; 01741 SmallVector<IdentifierInfo *, 8> ForEachMacros; 01742 01743 bool FormattingDisabled; 01744 01745 void readRawToken(FormatToken &Tok) { 01746 Lex->LexFromRawLexer(Tok.Tok); 01747 Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()), 01748 Tok.Tok.getLength()); 01749 // For formatting, treat unterminated string literals like normal string 01750 // literals. 01751 if (Tok.is(tok::unknown)) { 01752 if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') { 01753 Tok.Tok.setKind(tok::string_literal); 01754 Tok.IsUnterminatedLiteral = true; 01755 } else if (Style.Language == FormatStyle::LK_JavaScript && 01756 Tok.TokenText == "''") { 01757 Tok.Tok.setKind(tok::char_constant); 01758 } 01759 } 01760 01761 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format on" || 01762 Tok.TokenText == "/* clang-format on */")) { 01763 FormattingDisabled = false; 01764 } 01765 01766 Tok.Finalized = FormattingDisabled; 01767 01768 if (Tok.is(tok::comment) && (Tok.TokenText == "// clang-format off" || 01769 Tok.TokenText == "/* clang-format off */")) { 01770 FormattingDisabled = true; 01771 } 01772 } 01773 01774 void resetLexer(unsigned Offset) { 01775 StringRef Buffer = SourceMgr.getBufferData(ID); 01776 Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), 01777 getFormattingLangOpts(Style), Buffer.begin(), 01778 Buffer.begin() + Offset, Buffer.end())); 01779 Lex->SetKeepWhitespaceMode(true); 01780 } 01781 }; 01782 01783 static StringRef getLanguageName(FormatStyle::LanguageKind Language) { 01784 switch (Language) { 01785 case FormatStyle::LK_Cpp: 01786 return "C++"; 01787 case FormatStyle::LK_Java: 01788 return "Java"; 01789 case FormatStyle::LK_JavaScript: 01790 return "JavaScript"; 01791 case FormatStyle::LK_Proto: 01792 return "Proto"; 01793 default: 01794 return "Unknown"; 01795 } 01796 } 01797 01798 class Formatter : public UnwrappedLineConsumer { 01799 public: 01800 Formatter(const FormatStyle &Style, SourceManager &SourceMgr, FileID ID, 01801 ArrayRef<CharSourceRange> Ranges) 01802 : Style(Style), ID(ID), SourceMgr(SourceMgr), 01803 Whitespaces(SourceMgr, Style, 01804 inputUsesCRLF(SourceMgr.getBufferData(ID))), 01805 Ranges(Ranges.begin(), Ranges.end()), UnwrappedLines(1), 01806 Encoding(encoding::detectEncoding(SourceMgr.getBufferData(ID))) { 01807 DEBUG(llvm::dbgs() << "File encoding: " 01808 << (Encoding == encoding::Encoding_UTF8 ? "UTF8" 01809 : "unknown") 01810 << "\n"); 01811 DEBUG(llvm::dbgs() << "Language: " << getLanguageName(Style.Language) 01812 << "\n"); 01813 } 01814 01815 tooling::Replacements format() { 01816 tooling::Replacements Result; 01817 FormatTokenLexer Tokens(SourceMgr, ID, Style, Encoding); 01818 01819 UnwrappedLineParser Parser(Style, Tokens.getKeywords(), Tokens.lex(), 01820 *this); 01821 bool StructuralError = Parser.parse(); 01822 assert(UnwrappedLines.rbegin()->empty()); 01823 for (unsigned Run = 0, RunE = UnwrappedLines.size(); Run + 1 != RunE; 01824 ++Run) { 01825 DEBUG(llvm::dbgs() << "Run " << Run << "...\n"); 01826 SmallVector<AnnotatedLine *, 16> AnnotatedLines; 01827 for (unsigned i = 0, e = UnwrappedLines[Run].size(); i != e; ++i) { 01828 AnnotatedLines.push_back(new AnnotatedLine(UnwrappedLines[Run][i])); 01829 } 01830 tooling::Replacements RunResult = 01831 format(AnnotatedLines, StructuralError, Tokens); 01832 DEBUG({ 01833 llvm::dbgs() << "Replacements for run " << Run << ":\n"; 01834 for (tooling::Replacements::iterator I = RunResult.begin(), 01835 E = RunResult.end(); 01836 I != E; ++I) { 01837 llvm::dbgs() << I->toString() << "\n"; 01838 } 01839 }); 01840 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 01841 delete AnnotatedLines[i]; 01842 } 01843 Result.insert(RunResult.begin(), RunResult.end()); 01844 Whitespaces.reset(); 01845 } 01846 return Result; 01847 } 01848 01849 tooling::Replacements format(SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, 01850 bool StructuralError, FormatTokenLexer &Tokens) { 01851 TokenAnnotator Annotator(Style, Tokens.getKeywords()); 01852 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 01853 Annotator.annotate(*AnnotatedLines[i]); 01854 } 01855 deriveLocalStyle(AnnotatedLines); 01856 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 01857 Annotator.calculateFormattingInformation(*AnnotatedLines[i]); 01858 } 01859 computeAffectedLines(AnnotatedLines.begin(), AnnotatedLines.end()); 01860 01861 Annotator.setCommentLineLevels(AnnotatedLines); 01862 ContinuationIndenter Indenter(Style, Tokens.getKeywords(), SourceMgr, 01863 Whitespaces, Encoding, 01864 BinPackInconclusiveFunctions); 01865 UnwrappedLineFormatter Formatter(&Indenter, &Whitespaces, Style); 01866 Formatter.format(AnnotatedLines, /*DryRun=*/false); 01867 return Whitespaces.generateReplacements(); 01868 } 01869 01870 private: 01871 // Determines which lines are affected by the SourceRanges given as input. 01872 // Returns \c true if at least one line between I and E or one of their 01873 // children is affected. 01874 bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I, 01875 SmallVectorImpl<AnnotatedLine *>::iterator E) { 01876 bool SomeLineAffected = false; 01877 const AnnotatedLine *PreviousLine = nullptr; 01878 while (I != E) { 01879 AnnotatedLine *Line = *I; 01880 Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First); 01881 01882 // If a line is part of a preprocessor directive, it needs to be formatted 01883 // if any token within the directive is affected. 01884 if (Line->InPPDirective) { 01885 FormatToken *Last = Line->Last; 01886 SmallVectorImpl<AnnotatedLine *>::iterator PPEnd = I + 1; 01887 while (PPEnd != E && !(*PPEnd)->First->HasUnescapedNewline) { 01888 Last = (*PPEnd)->Last; 01889 ++PPEnd; 01890 } 01891 01892 if (affectsTokenRange(*Line->First, *Last, 01893 /*IncludeLeadingNewlines=*/false)) { 01894 SomeLineAffected = true; 01895 markAllAsAffected(I, PPEnd); 01896 } 01897 I = PPEnd; 01898 continue; 01899 } 01900 01901 if (nonPPLineAffected(Line, PreviousLine)) 01902 SomeLineAffected = true; 01903 01904 PreviousLine = Line; 01905 ++I; 01906 } 01907 return SomeLineAffected; 01908 } 01909 01910 // Determines whether 'Line' is affected by the SourceRanges given as input. 01911 // Returns \c true if line or one if its children is affected. 01912 bool nonPPLineAffected(AnnotatedLine *Line, 01913 const AnnotatedLine *PreviousLine) { 01914 bool SomeLineAffected = false; 01915 Line->ChildrenAffected = 01916 computeAffectedLines(Line->Children.begin(), Line->Children.end()); 01917 if (Line->ChildrenAffected) 01918 SomeLineAffected = true; 01919 01920 // Stores whether one of the line's tokens is directly affected. 01921 bool SomeTokenAffected = false; 01922 // Stores whether we need to look at the leading newlines of the next token 01923 // in order to determine whether it was affected. 01924 bool IncludeLeadingNewlines = false; 01925 01926 // Stores whether the first child line of any of this line's tokens is 01927 // affected. 01928 bool SomeFirstChildAffected = false; 01929 01930 for (FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) { 01931 // Determine whether 'Tok' was affected. 01932 if (affectsTokenRange(*Tok, *Tok, IncludeLeadingNewlines)) 01933 SomeTokenAffected = true; 01934 01935 // Determine whether the first child of 'Tok' was affected. 01936 if (!Tok->Children.empty() && Tok->Children.front()->Affected) 01937 SomeFirstChildAffected = true; 01938 01939 IncludeLeadingNewlines = Tok->Children.empty(); 01940 } 01941 01942 // Was this line moved, i.e. has it previously been on the same line as an 01943 // affected line? 01944 bool LineMoved = PreviousLine && PreviousLine->Affected && 01945 Line->First->NewlinesBefore == 0; 01946 01947 bool IsContinuedComment = 01948 Line->First->is(tok::comment) && Line->First->Next == nullptr && 01949 Line->First->NewlinesBefore < 2 && PreviousLine && 01950 PreviousLine->Affected && PreviousLine->Last->is(tok::comment); 01951 01952 if (SomeTokenAffected || SomeFirstChildAffected || LineMoved || 01953 IsContinuedComment) { 01954 Line->Affected = true; 01955 SomeLineAffected = true; 01956 } 01957 return SomeLineAffected; 01958 } 01959 01960 // Marks all lines between I and E as well as all their children as affected. 01961 void markAllAsAffected(SmallVectorImpl<AnnotatedLine *>::iterator I, 01962 SmallVectorImpl<AnnotatedLine *>::iterator E) { 01963 while (I != E) { 01964 (*I)->Affected = true; 01965 markAllAsAffected((*I)->Children.begin(), (*I)->Children.end()); 01966 ++I; 01967 } 01968 } 01969 01970 // Returns true if the range from 'First' to 'Last' intersects with one of the 01971 // input ranges. 01972 bool affectsTokenRange(const FormatToken &First, const FormatToken &Last, 01973 bool IncludeLeadingNewlines) { 01974 SourceLocation Start = First.WhitespaceRange.getBegin(); 01975 if (!IncludeLeadingNewlines) 01976 Start = Start.getLocWithOffset(First.LastNewlineOffset); 01977 SourceLocation End = Last.getStartOfNonWhitespace(); 01978 End = End.getLocWithOffset(Last.TokenText.size()); 01979 CharSourceRange Range = CharSourceRange::getCharRange(Start, End); 01980 return affectsCharSourceRange(Range); 01981 } 01982 01983 // Returns true if one of the input ranges intersect the leading empty lines 01984 // before 'Tok'. 01985 bool affectsLeadingEmptyLines(const FormatToken &Tok) { 01986 CharSourceRange EmptyLineRange = CharSourceRange::getCharRange( 01987 Tok.WhitespaceRange.getBegin(), 01988 Tok.WhitespaceRange.getBegin().getLocWithOffset(Tok.LastNewlineOffset)); 01989 return affectsCharSourceRange(EmptyLineRange); 01990 } 01991 01992 // Returns true if 'Range' intersects with one of the input ranges. 01993 bool affectsCharSourceRange(const CharSourceRange &Range) { 01994 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(), 01995 E = Ranges.end(); 01996 I != E; ++I) { 01997 if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), I->getBegin()) && 01998 !SourceMgr.isBeforeInTranslationUnit(I->getEnd(), Range.getBegin())) 01999 return true; 02000 } 02001 return false; 02002 } 02003 02004 static bool inputUsesCRLF(StringRef Text) { 02005 return Text.count('\r') * 2 > Text.count('\n'); 02006 } 02007 02008 void 02009 deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { 02010 unsigned CountBoundToVariable = 0; 02011 unsigned CountBoundToType = 0; 02012 bool HasCpp03IncompatibleFormat = false; 02013 bool HasBinPackedFunction = false; 02014 bool HasOnePerLineFunction = false; 02015 for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { 02016 if (!AnnotatedLines[i]->First->Next) 02017 continue; 02018 FormatToken *Tok = AnnotatedLines[i]->First->Next; 02019 while (Tok->Next) { 02020 if (Tok->Type == TT_PointerOrReference) { 02021 bool SpacesBefore = 02022 Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd(); 02023 bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() != 02024 Tok->Next->WhitespaceRange.getEnd(); 02025 if (SpacesBefore && !SpacesAfter) 02026 ++CountBoundToVariable; 02027 else if (!SpacesBefore && SpacesAfter) 02028 ++CountBoundToType; 02029 } 02030 02031 if (Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) { 02032 if (Tok->is(tok::coloncolon) && 02033 Tok->Previous->Type == TT_TemplateOpener) 02034 HasCpp03IncompatibleFormat = true; 02035 if (Tok->Type == TT_TemplateCloser && 02036 Tok->Previous->Type == TT_TemplateCloser) 02037 HasCpp03IncompatibleFormat = true; 02038 } 02039 02040 if (Tok->PackingKind == PPK_BinPacked) 02041 HasBinPackedFunction = true; 02042 if (Tok->PackingKind == PPK_OnePerLine) 02043 HasOnePerLineFunction = true; 02044 02045 Tok = Tok->Next; 02046 } 02047 } 02048 if (Style.DerivePointerAlignment) { 02049 if (CountBoundToType > CountBoundToVariable) 02050 Style.PointerAlignment = FormatStyle::PAS_Left; 02051 else if (CountBoundToType < CountBoundToVariable) 02052 Style.PointerAlignment = FormatStyle::PAS_Right; 02053 } 02054 if (Style.Standard == FormatStyle::LS_Auto) { 02055 Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 02056 : FormatStyle::LS_Cpp03; 02057 } 02058 BinPackInconclusiveFunctions = 02059 HasBinPackedFunction || !HasOnePerLineFunction; 02060 } 02061 02062 void consumeUnwrappedLine(const UnwrappedLine &TheLine) override { 02063 assert(!UnwrappedLines.empty()); 02064 UnwrappedLines.back().push_back(TheLine); 02065 } 02066 02067 void finishRun() override { 02068 UnwrappedLines.push_back(SmallVector<UnwrappedLine, 16>()); 02069 } 02070 02071 FormatStyle Style; 02072 FileID ID; 02073 SourceManager &SourceMgr; 02074 WhitespaceManager Whitespaces; 02075 SmallVector<CharSourceRange, 8> Ranges; 02076 SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines; 02077 02078 encoding::Encoding Encoding; 02079 bool BinPackInconclusiveFunctions; 02080 }; 02081 02082 } // end anonymous namespace 02083 02084 tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, 02085 SourceManager &SourceMgr, 02086 ArrayRef<CharSourceRange> Ranges) { 02087 if (Style.DisableFormat) 02088 return tooling::Replacements(); 02089 return reformat(Style, SourceMgr, 02090 SourceMgr.getFileID(Lex.getSourceLocation()), Ranges); 02091 } 02092 02093 tooling::Replacements reformat(const FormatStyle &Style, 02094 SourceManager &SourceMgr, FileID ID, 02095 ArrayRef<CharSourceRange> Ranges) { 02096 if (Style.DisableFormat) 02097 return tooling::Replacements(); 02098 Formatter formatter(Style, SourceMgr, ID, Ranges); 02099 return formatter.format(); 02100 } 02101 02102 tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, 02103 ArrayRef<tooling::Range> Ranges, 02104 StringRef FileName) { 02105 if (Style.DisableFormat) 02106 return tooling::Replacements(); 02107 02108 FileManager Files((FileSystemOptions())); 02109 DiagnosticsEngine Diagnostics( 02110 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), 02111 new DiagnosticOptions); 02112 SourceManager SourceMgr(Diagnostics, Files); 02113 std::unique_ptr<llvm::MemoryBuffer> Buf = 02114 llvm::MemoryBuffer::getMemBuffer(Code, FileName); 02115 const clang::FileEntry *Entry = 02116 Files.getVirtualFile(FileName, Buf->getBufferSize(), 0); 02117 SourceMgr.overrideFileContents(Entry, std::move(Buf)); 02118 FileID ID = 02119 SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); 02120 SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID); 02121 std::vector<CharSourceRange> CharRanges; 02122 for (const tooling::Range &Range : Ranges) { 02123 SourceLocation Start = StartOfFile.getLocWithOffset(Range.getOffset()); 02124 SourceLocation End = Start.getLocWithOffset(Range.getLength()); 02125 CharRanges.push_back(CharSourceRange::getCharRange(Start, End)); 02126 } 02127 return reformat(Style, SourceMgr, ID, CharRanges); 02128 } 02129 02130 LangOptions getFormattingLangOpts(const FormatStyle &Style) { 02131 LangOptions LangOpts; 02132 LangOpts.CPlusPlus = 1; 02133 LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; 02134 LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; 02135 LangOpts.LineComment = 1; 02136 bool AlternativeOperators = Style.Language != FormatStyle::LK_JavaScript && 02137 Style.Language != FormatStyle::LK_Java; 02138 LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0; 02139 LangOpts.Bool = 1; 02140 LangOpts.ObjC1 = 1; 02141 LangOpts.ObjC2 = 1; 02142 return LangOpts; 02143 } 02144 02145 const char *StyleOptionHelpDescription = 02146 "Coding style, currently supports:\n" 02147 " LLVM, Google, Chromium, Mozilla, WebKit.\n" 02148 "Use -style=file to load style configuration from\n" 02149 ".clang-format file located in one of the parent\n" 02150 "directories of the source file (or current\n" 02151 "directory for stdin).\n" 02152 "Use -style=\"{key: value, ...}\" to set specific\n" 02153 "parameters, e.g.:\n" 02154 " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""; 02155 02156 static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) { 02157 if (FileName.endswith(".java")) { 02158 return FormatStyle::LK_Java; 02159 } else if (FileName.endswith_lower(".js")) { 02160 return FormatStyle::LK_JavaScript; 02161 } else if (FileName.endswith_lower(".proto") || 02162 FileName.endswith_lower(".protodevel")) { 02163 return FormatStyle::LK_Proto; 02164 } 02165 return FormatStyle::LK_Cpp; 02166 } 02167 02168 FormatStyle getStyle(StringRef StyleName, StringRef FileName, 02169 StringRef FallbackStyle) { 02170 FormatStyle Style = getLLVMStyle(); 02171 Style.Language = getLanguageByFileName(FileName); 02172 if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) { 02173 llvm::errs() << "Invalid fallback style \"" << FallbackStyle 02174 << "\" using LLVM style\n"; 02175 return Style; 02176 } 02177 02178 if (StyleName.startswith("{")) { 02179 // Parse YAML/JSON style from the command line. 02180 if (std::error_code ec = parseConfiguration(StyleName, &Style)) { 02181 llvm::errs() << "Error parsing -style: " << ec.message() << ", using " 02182 << FallbackStyle << " style\n"; 02183 } 02184 return Style; 02185 } 02186 02187 if (!StyleName.equals_lower("file")) { 02188 if (!getPredefinedStyle(StyleName, Style.Language, &Style)) 02189 llvm::errs() << "Invalid value for -style, using " << FallbackStyle 02190 << " style\n"; 02191 return Style; 02192 } 02193 02194 // Look for .clang-format/_clang-format file in the file's parent directories. 02195 SmallString<128> UnsuitableConfigFiles; 02196 SmallString<128> Path(FileName); 02197 llvm::sys::fs::make_absolute(Path); 02198 for (StringRef Directory = Path; !Directory.empty(); 02199 Directory = llvm::sys::path::parent_path(Directory)) { 02200 if (!llvm::sys::fs::is_directory(Directory)) 02201 continue; 02202 SmallString<128> ConfigFile(Directory); 02203 02204 llvm::sys::path::append(ConfigFile, ".clang-format"); 02205 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); 02206 bool IsFile = false; 02207 // Ignore errors from is_regular_file: we only need to know if we can read 02208 // the file or not. 02209 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); 02210 02211 if (!IsFile) { 02212 // Try _clang-format too, since dotfiles are not commonly used on Windows. 02213 ConfigFile = Directory; 02214 llvm::sys::path::append(ConfigFile, "_clang-format"); 02215 DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); 02216 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); 02217 } 02218 02219 if (IsFile) { 02220 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = 02221 llvm::MemoryBuffer::getFile(ConfigFile.c_str()); 02222 if (std::error_code EC = Text.getError()) { 02223 llvm::errs() << EC.message() << "\n"; 02224 break; 02225 } 02226 if (std::error_code ec = 02227 parseConfiguration(Text.get()->getBuffer(), &Style)) { 02228 if (ec == ParseError::Unsuitable) { 02229 if (!UnsuitableConfigFiles.empty()) 02230 UnsuitableConfigFiles.append(", "); 02231 UnsuitableConfigFiles.append(ConfigFile); 02232 continue; 02233 } 02234 llvm::errs() << "Error reading " << ConfigFile << ": " << ec.message() 02235 << "\n"; 02236 break; 02237 } 02238 DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n"); 02239 return Style; 02240 } 02241 } 02242 llvm::errs() << "Can't find usable .clang-format, using " << FallbackStyle 02243 << " style\n"; 02244 if (!UnsuitableConfigFiles.empty()) { 02245 llvm::errs() << "Configuration file(s) do(es) not support " 02246 << getLanguageName(Style.Language) << ": " 02247 << UnsuitableConfigFiles << "\n"; 02248 } 02249 return Style; 02250 } 02251 02252 } // namespace format 02253 } // namespace clang