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