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