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