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