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