Daniel Jasper | bac016b | 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 | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "format-formatter" |
| 17 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 18 | #include "BreakableToken.h" |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 19 | #include "TokenAnnotator.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "UnwrappedLineParser.h" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 21 | #include "WhitespaceManager.h" |
Daniel Jasper | 8a99945 | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Daniel Jasper | 675d2e3 | 2012-12-21 10:20:02 +0000 | [diff] [blame] | 23 | #include "clang/Basic/OperatorPrecedence.h" |
Chandler Carruth | b99083e | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 25 | #include "clang/Format/Format.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Lexer.h" |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Allocator.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 30 | #include "llvm/Support/YAMLTraits.h" |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 31 | #include <queue> |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 32 | #include <string> |
| 33 | |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 34 | namespace llvm { |
| 35 | namespace yaml { |
| 36 | template <> |
| 37 | struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> { |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 38 | static void enumeration(IO &IO, |
| 39 | clang::format::FormatStyle::LanguageStandard &Value) { |
| 40 | IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03); |
| 41 | IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11); |
| 42 | IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto); |
| 43 | } |
| 44 | }; |
| 45 | |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 46 | template <> |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 47 | struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> { |
| 48 | static void |
| 49 | enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) { |
| 50 | IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach); |
| 51 | IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux); |
| 52 | IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 53 | } |
| 54 | }; |
| 55 | |
| 56 | template <> struct MappingTraits<clang::format::FormatStyle> { |
| 57 | static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 58 | if (IO.outputting()) { |
| 59 | StringRef StylesArray[] = { "LLVM", "Google", "Chromium", "Mozilla" }; |
| 60 | ArrayRef<StringRef> Styles(StylesArray); |
| 61 | for (size_t i = 0, e = Styles.size(); i < e; ++i) { |
| 62 | StringRef StyleName(Styles[i]); |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 63 | clang::format::FormatStyle PredefinedStyle; |
| 64 | if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) && |
| 65 | Style == PredefinedStyle) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 66 | IO.mapOptional("# BasedOnStyle", StyleName); |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | } else { |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 71 | StringRef BasedOnStyle; |
| 72 | IO.mapOptional("BasedOnStyle", BasedOnStyle); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 73 | if (!BasedOnStyle.empty()) |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 74 | if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) { |
| 75 | IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle)); |
| 76 | return; |
| 77 | } |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); |
| 81 | IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); |
| 82 | IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", |
| 83 | Style.AllowAllParametersOfDeclarationOnNextLine); |
| 84 | IO.mapOptional("AllowShortIfStatementsOnASingleLine", |
| 85 | Style.AllowShortIfStatementsOnASingleLine); |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 86 | IO.mapOptional("AllowShortLoopsOnASingleLine", |
| 87 | Style.AllowShortLoopsOnASingleLine); |
Daniel Jasper | bbc8776 | 2013-05-29 12:07:31 +0000 | [diff] [blame] | 88 | IO.mapOptional("AlwaysBreakTemplateDeclarations", |
| 89 | Style.AlwaysBreakTemplateDeclarations); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 90 | IO.mapOptional("BinPackParameters", Style.BinPackParameters); |
| 91 | IO.mapOptional("ColumnLimit", Style.ColumnLimit); |
| 92 | IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", |
| 93 | Style.ConstructorInitializerAllOnOneLineOrOnePerLine); |
| 94 | IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding); |
| 95 | IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); |
| 96 | IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); |
| 97 | IO.mapOptional("ObjCSpaceBeforeProtocolList", |
| 98 | Style.ObjCSpaceBeforeProtocolList); |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 99 | IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); |
| 100 | IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 101 | IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); |
| 102 | IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", |
| 103 | Style.PenaltyReturnTypeOnItsOwnLine); |
| 104 | IO.mapOptional("PointerBindsToType", Style.PointerBindsToType); |
| 105 | IO.mapOptional("SpacesBeforeTrailingComments", |
| 106 | Style.SpacesBeforeTrailingComments); |
Daniel Jasper | 1bee073 | 2013-05-23 18:05:18 +0000 | [diff] [blame] | 107 | IO.mapOptional("SpacesInBracedLists", Style.SpacesInBracedLists); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 108 | IO.mapOptional("Standard", Style.Standard); |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 109 | IO.mapOptional("IndentWidth", Style.IndentWidth); |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 110 | IO.mapOptional("UseTab", Style.UseTab); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 111 | IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); |
Manuel Klimek | a9a7f10 | 2013-06-21 17:25:42 +0000 | [diff] [blame] | 112 | IO.mapOptional("IndentFunctionDeclarationAfterType", |
| 113 | Style.IndentFunctionDeclarationAfterType); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 114 | } |
| 115 | }; |
| 116 | } |
| 117 | } |
| 118 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 119 | namespace clang { |
| 120 | namespace format { |
| 121 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 122 | FormatStyle getLLVMStyle() { |
| 123 | FormatStyle LLVMStyle; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 124 | LLVMStyle.AccessModifierOffset = -2; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 125 | LLVMStyle.AlignEscapedNewlinesLeft = false; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 126 | LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 127 | LLVMStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 128 | LLVMStyle.AllowShortLoopsOnASingleLine = false; |
Daniel Jasper | bbc8776 | 2013-05-29 12:07:31 +0000 | [diff] [blame] | 129 | LLVMStyle.AlwaysBreakTemplateDeclarations = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 130 | LLVMStyle.BinPackParameters = true; |
| 131 | LLVMStyle.ColumnLimit = 80; |
| 132 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
| 133 | LLVMStyle.DerivePointerBinding = false; |
| 134 | LLVMStyle.IndentCaseLabels = false; |
| 135 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 136 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 137 | LLVMStyle.PenaltyBreakComment = 45; |
| 138 | LLVMStyle.PenaltyBreakString = 1000; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 139 | LLVMStyle.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 140 | LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 75; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 141 | LLVMStyle.PointerBindsToType = false; |
| 142 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
Daniel Jasper | 2424eef | 2013-05-23 10:15:45 +0000 | [diff] [blame] | 143 | LLVMStyle.SpacesInBracedLists = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 144 | LLVMStyle.Standard = FormatStyle::LS_Cpp03; |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 145 | LLVMStyle.IndentWidth = 2; |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 146 | LLVMStyle.UseTab = false; |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 147 | LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
Manuel Klimek | a9a7f10 | 2013-06-21 17:25:42 +0000 | [diff] [blame] | 148 | LLVMStyle.IndentFunctionDeclarationAfterType = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 149 | return LLVMStyle; |
| 150 | } |
| 151 | |
| 152 | FormatStyle getGoogleStyle() { |
| 153 | FormatStyle GoogleStyle; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 154 | GoogleStyle.AccessModifierOffset = -1; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 155 | GoogleStyle.AlignEscapedNewlinesLeft = true; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 156 | GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 94d6ad7 | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 157 | GoogleStyle.AllowShortIfStatementsOnASingleLine = true; |
Daniel Jasper | 1bee073 | 2013-05-23 18:05:18 +0000 | [diff] [blame] | 158 | GoogleStyle.AllowShortLoopsOnASingleLine = true; |
Daniel Jasper | bbc8776 | 2013-05-29 12:07:31 +0000 | [diff] [blame] | 159 | GoogleStyle.AlwaysBreakTemplateDeclarations = true; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 160 | GoogleStyle.BinPackParameters = true; |
| 161 | GoogleStyle.ColumnLimit = 80; |
| 162 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
| 163 | GoogleStyle.DerivePointerBinding = true; |
| 164 | GoogleStyle.IndentCaseLabels = true; |
| 165 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 166 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 167 | GoogleStyle.PenaltyBreakComment = 45; |
| 168 | GoogleStyle.PenaltyBreakString = 1000; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 169 | GoogleStyle.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 170 | GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 171 | GoogleStyle.PointerBindsToType = true; |
| 172 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
Daniel Jasper | 2424eef | 2013-05-23 10:15:45 +0000 | [diff] [blame] | 173 | GoogleStyle.SpacesInBracedLists = false; |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 174 | GoogleStyle.Standard = FormatStyle::LS_Auto; |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 175 | GoogleStyle.IndentWidth = 2; |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 176 | GoogleStyle.UseTab = false; |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 177 | GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach; |
Manuel Klimek | a9a7f10 | 2013-06-21 17:25:42 +0000 | [diff] [blame] | 178 | GoogleStyle.IndentFunctionDeclarationAfterType = true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 179 | return GoogleStyle; |
| 180 | } |
| 181 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 182 | FormatStyle getChromiumStyle() { |
| 183 | FormatStyle ChromiumStyle = getGoogleStyle(); |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 184 | ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
Daniel Jasper | 94d6ad7 | 2013-04-24 13:46:00 +0000 | [diff] [blame] | 185 | ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 186 | ChromiumStyle.AllowShortLoopsOnASingleLine = false; |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 187 | ChromiumStyle.BinPackParameters = false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 188 | ChromiumStyle.Standard = FormatStyle::LS_Cpp03; |
| 189 | ChromiumStyle.DerivePointerBinding = false; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 190 | return ChromiumStyle; |
| 191 | } |
| 192 | |
Alexander Kornienko | fb59486 | 2013-05-06 14:11:27 +0000 | [diff] [blame] | 193 | FormatStyle getMozillaStyle() { |
| 194 | FormatStyle MozillaStyle = getLLVMStyle(); |
| 195 | MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
| 196 | MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
| 197 | MozillaStyle.DerivePointerBinding = true; |
| 198 | MozillaStyle.IndentCaseLabels = true; |
| 199 | MozillaStyle.ObjCSpaceBeforeProtocolList = false; |
| 200 | MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
| 201 | MozillaStyle.PointerBindsToType = true; |
| 202 | return MozillaStyle; |
| 203 | } |
| 204 | |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 205 | bool getPredefinedStyle(StringRef Name, FormatStyle *Style) { |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 206 | if (Name.equals_lower("llvm")) |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 207 | *Style = getLLVMStyle(); |
| 208 | else if (Name.equals_lower("chromium")) |
| 209 | *Style = getChromiumStyle(); |
| 210 | else if (Name.equals_lower("mozilla")) |
| 211 | *Style = getMozillaStyle(); |
| 212 | else if (Name.equals_lower("google")) |
| 213 | *Style = getGoogleStyle(); |
| 214 | else |
| 215 | return false; |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 216 | |
Alexander Kornienko | 885f87b | 2013-05-19 00:53:30 +0000 | [diff] [blame] | 217 | return true; |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { |
Alexander Kornienko | 107db3c | 2013-05-20 15:18:01 +0000 | [diff] [blame] | 221 | if (Text.trim().empty()) |
| 222 | return llvm::make_error_code(llvm::errc::invalid_argument); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 223 | llvm::yaml::Input Input(Text); |
| 224 | Input >> *Style; |
| 225 | return Input.error(); |
| 226 | } |
| 227 | |
| 228 | std::string configurationAsText(const FormatStyle &Style) { |
| 229 | std::string Text; |
| 230 | llvm::raw_string_ostream Stream(Text); |
| 231 | llvm::yaml::Output Output(Stream); |
| 232 | // We use the same mapping method for input and output, so we need a non-const |
| 233 | // reference here. |
| 234 | FormatStyle NonConstStyle = Style; |
| 235 | Output << NonConstStyle; |
Alexander Kornienko | 2b6acb6 | 2013-05-13 12:56:35 +0000 | [diff] [blame] | 236 | return Stream.str(); |
Alexander Kornienko | d71ec16 | 2013-05-07 15:32:14 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 239 | // Returns the length of everything up to the first possible line break after |
| 240 | // the ), ], } or > matching \c Tok. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 241 | static unsigned getLengthToMatchingParen(const FormatToken &Tok) { |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 242 | if (Tok.MatchingParen == NULL) |
| 243 | return 0; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 244 | FormatToken *End = Tok.MatchingParen; |
| 245 | while (End->Next && !End->Next->CanBreakBefore) { |
| 246 | End = End->Next; |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 247 | } |
| 248 | return End->TotalLength - Tok.TotalLength + 1; |
| 249 | } |
| 250 | |
Craig Topper | 83f81d7 | 2013-06-30 22:29:28 +0000 | [diff] [blame^] | 251 | namespace { |
| 252 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 253 | class UnwrappedLineFormatter { |
| 254 | public: |
Manuel Klimek | 94fc6f1 | 2013-01-10 19:17:33 +0000 | [diff] [blame] | 255 | UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 256 | const AnnotatedLine &Line, unsigned FirstIndent, |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 257 | const FormatToken *RootToken, |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 258 | WhitespaceManager &Whitespaces, |
| 259 | encoding::Encoding Encoding) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 260 | : Style(Style), SourceMgr(SourceMgr), Line(Line), |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 261 | FirstIndent(FirstIndent), RootToken(RootToken), |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 262 | Whitespaces(Whitespaces), Count(0), Encoding(Encoding) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 263 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 264 | /// \brief Formats an \c UnwrappedLine. |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 265 | void format(const AnnotatedLine *NextLine) { |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 266 | // Initialize state dependent on indent. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 267 | LineState State; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 268 | State.Column = FirstIndent; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 269 | State.NextToken = RootToken; |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 270 | State.Stack.push_back( |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 271 | ParenState(FirstIndent, FirstIndent, /*AvoidBinPacking=*/false, |
| 272 | /*NoLineBreak=*/false)); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 273 | State.LineContainsContinuedForLoopSection = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 274 | State.ParenLevel = 0; |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 275 | State.StartOfStringLiteral = 0; |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 276 | State.StartOfLineLevel = State.ParenLevel; |
Daniel Jasper | ce912f4 | 2013-06-03 09:54:46 +0000 | [diff] [blame] | 277 | State.LowestCallLevel = State.ParenLevel; |
Daniel Jasper | 54b4e44 | 2013-05-22 05:27:42 +0000 | [diff] [blame] | 278 | State.IgnoreStackForComparison = false; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 279 | |
| 280 | // The first token has already been indented and thus consumed. |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 281 | moveStateToNextToken(State, /*DryRun=*/false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 282 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 283 | // If everything fits on a single line, just put it there. |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 284 | unsigned ColumnLimit = Style.ColumnLimit; |
| 285 | if (NextLine && NextLine->InPPDirective && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 286 | !NextLine->First->HasUnescapedNewline) |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 287 | ColumnLimit = getColumnLimit(); |
| 288 | if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) { |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 289 | while (State.NextToken != NULL) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 290 | addTokenToState(false, false, State); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 291 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 292 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 293 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 294 | // If the ObjC method declaration does not fit on a line, we should format |
| 295 | // it with one arg per line. |
| 296 | if (Line.Type == LT_ObjCMethodDecl) |
| 297 | State.Stack.back().BreakBeforeParameter = true; |
| 298 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 299 | // Find best solution in solution space. |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 300 | analyzeSolutionSpace(State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | private: |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 304 | void DebugTokenState(const FormatToken &FormatTok) { |
| 305 | const Token &Tok = FormatTok.Tok; |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 306 | llvm::dbgs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 307 | Tok.getLength()); |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 308 | llvm::dbgs(); |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 311 | struct ParenState { |
Daniel Jasper | d399bff | 2013-02-05 09:41:21 +0000 | [diff] [blame] | 312 | ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking, |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 313 | bool NoLineBreak) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 314 | : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0), |
| 315 | BreakBeforeClosingBrace(false), QuestionColumn(0), |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 316 | AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false), |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 317 | NoLineBreak(NoLineBreak), ColonPos(0), StartOfFunctionCall(0), |
| 318 | NestedNameSpecifierContinuation(0), CallContinuation(0), |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 319 | VariablePos(0), ForFakeParenthesis(false) {} |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 320 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 321 | /// \brief The position to which a specific parenthesis level needs to be |
| 322 | /// indented. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 323 | unsigned Indent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 324 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 325 | /// \brief The position of the last space on each level. |
| 326 | /// |
| 327 | /// Used e.g. to break like: |
| 328 | /// functionCall(Parameter, otherCall( |
| 329 | /// OtherParameter)); |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 330 | unsigned LastSpace; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 331 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 332 | /// \brief The position the first "<<" operator encountered on each level. |
| 333 | /// |
| 334 | /// Used to align "<<" operators. 0 if no such operator has been encountered |
| 335 | /// on a level. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 336 | unsigned FirstLessLess; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 337 | |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 338 | /// \brief Whether a newline needs to be inserted before the block's closing |
| 339 | /// brace. |
| 340 | /// |
| 341 | /// We only want to insert a newline before the closing brace if there also |
| 342 | /// was a newline after the beginning left brace. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 343 | bool BreakBeforeClosingBrace; |
| 344 | |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 345 | /// \brief The column of a \c ? in a conditional expression; |
| 346 | unsigned QuestionColumn; |
| 347 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 348 | /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple |
| 349 | /// lines, in this context. |
| 350 | bool AvoidBinPacking; |
| 351 | |
| 352 | /// \brief Break after the next comma (or all the commas in this context if |
| 353 | /// \c AvoidBinPacking is \c true). |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 354 | bool BreakBeforeParameter; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 355 | |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 356 | /// \brief Line breaking in this context would break a formatting rule. |
| 357 | bool NoLineBreak; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 358 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 359 | /// \brief The position of the colon in an ObjC method declaration/call. |
| 360 | unsigned ColonPos; |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 361 | |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 362 | /// \brief The start of the most recent function in a builder-type call. |
| 363 | unsigned StartOfFunctionCall; |
| 364 | |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 365 | /// \brief If a nested name specifier was broken over multiple lines, this |
| 366 | /// contains the start column of the second line. Otherwise 0. |
| 367 | unsigned NestedNameSpecifierContinuation; |
| 368 | |
| 369 | /// \brief If a call expression was broken over multiple lines, this |
| 370 | /// contains the start column of the second line. Otherwise 0. |
| 371 | unsigned CallContinuation; |
| 372 | |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 373 | /// \brief The column of the first variable name in a variable declaration. |
| 374 | /// |
| 375 | /// Used to align further variables if necessary. |
| 376 | unsigned VariablePos; |
| 377 | |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 378 | /// \brief \c true if this \c ParenState was created for a fake parenthesis. |
| 379 | /// |
| 380 | /// Does not need to be considered for memoization / the comparison function |
| 381 | /// as otherwise identical states will have the same fake/non-fake |
| 382 | /// \c ParenStates. |
| 383 | bool ForFakeParenthesis; |
| 384 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 385 | bool operator<(const ParenState &Other) const { |
| 386 | if (Indent != Other.Indent) |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 387 | return Indent < Other.Indent; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 388 | if (LastSpace != Other.LastSpace) |
| 389 | return LastSpace < Other.LastSpace; |
| 390 | if (FirstLessLess != Other.FirstLessLess) |
| 391 | return FirstLessLess < Other.FirstLessLess; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 392 | if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace) |
| 393 | return BreakBeforeClosingBrace; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 394 | if (QuestionColumn != Other.QuestionColumn) |
| 395 | return QuestionColumn < Other.QuestionColumn; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 396 | if (AvoidBinPacking != Other.AvoidBinPacking) |
| 397 | return AvoidBinPacking; |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 398 | if (BreakBeforeParameter != Other.BreakBeforeParameter) |
| 399 | return BreakBeforeParameter; |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 400 | if (NoLineBreak != Other.NoLineBreak) |
| 401 | return NoLineBreak; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 402 | if (ColonPos != Other.ColonPos) |
| 403 | return ColonPos < Other.ColonPos; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 404 | if (StartOfFunctionCall != Other.StartOfFunctionCall) |
| 405 | return StartOfFunctionCall < Other.StartOfFunctionCall; |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 406 | if (CallContinuation != Other.CallContinuation) |
| 407 | return CallContinuation < Other.CallContinuation; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 408 | if (VariablePos != Other.VariablePos) |
| 409 | return VariablePos < Other.VariablePos; |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 410 | return false; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 411 | } |
| 412 | }; |
| 413 | |
| 414 | /// \brief The current state when indenting a unwrapped line. |
| 415 | /// |
| 416 | /// As the indenting tries different combinations this is copied by value. |
| 417 | struct LineState { |
| 418 | /// \brief The number of used columns in the current line. |
| 419 | unsigned Column; |
| 420 | |
| 421 | /// \brief The token that needs to be next formatted. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 422 | const FormatToken *NextToken; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 423 | |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 424 | /// \brief \c true if this line contains a continued for-loop section. |
| 425 | bool LineContainsContinuedForLoopSection; |
| 426 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 427 | /// \brief The level of nesting inside (), [], <> and {}. |
| 428 | unsigned ParenLevel; |
| 429 | |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 430 | /// \brief The \c ParenLevel at the start of this line. |
| 431 | unsigned StartOfLineLevel; |
| 432 | |
Daniel Jasper | ce912f4 | 2013-06-03 09:54:46 +0000 | [diff] [blame] | 433 | /// \brief The lowest \c ParenLevel of "." or "->" on the current line. |
| 434 | unsigned LowestCallLevel; |
Daniel Jasper | 259a038 | 2013-05-27 11:50:16 +0000 | [diff] [blame] | 435 | |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 436 | /// \brief The start column of the string literal, if we're in a string |
| 437 | /// literal sequence, 0 otherwise. |
| 438 | unsigned StartOfStringLiteral; |
| 439 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 440 | /// \brief A stack keeping track of properties applying to parenthesis |
| 441 | /// levels. |
| 442 | std::vector<ParenState> Stack; |
| 443 | |
Daniel Jasper | 54b4e44 | 2013-05-22 05:27:42 +0000 | [diff] [blame] | 444 | /// \brief Ignore the stack of \c ParenStates for state comparison. |
| 445 | /// |
| 446 | /// In long and deeply nested unwrapped lines, the current algorithm can |
| 447 | /// be insufficient for finding the best formatting with a reasonable amount |
| 448 | /// of time and memory. Setting this flag will effectively lead to the |
| 449 | /// algorithm not analyzing some combinations. However, these combinations |
| 450 | /// rarely contain the optimal solution: In short, accepting a higher |
| 451 | /// penalty early would need to lead to different values in the \c |
| 452 | /// ParenState stack (in an otherwise identical state) and these different |
| 453 | /// values would need to lead to a significant amount of avoided penalty |
| 454 | /// later. |
| 455 | /// |
| 456 | /// FIXME: Come up with a better algorithm instead. |
| 457 | bool IgnoreStackForComparison; |
| 458 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 459 | /// \brief Comparison operator to be able to used \c LineState in \c map. |
| 460 | bool operator<(const LineState &Other) const { |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 461 | if (NextToken != Other.NextToken) |
| 462 | return NextToken < Other.NextToken; |
| 463 | if (Column != Other.Column) |
| 464 | return Column < Other.Column; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 465 | if (LineContainsContinuedForLoopSection != |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 466 | Other.LineContainsContinuedForLoopSection) |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 467 | return LineContainsContinuedForLoopSection; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 468 | if (ParenLevel != Other.ParenLevel) |
| 469 | return ParenLevel < Other.ParenLevel; |
| 470 | if (StartOfLineLevel != Other.StartOfLineLevel) |
| 471 | return StartOfLineLevel < Other.StartOfLineLevel; |
Daniel Jasper | ce912f4 | 2013-06-03 09:54:46 +0000 | [diff] [blame] | 472 | if (LowestCallLevel != Other.LowestCallLevel) |
| 473 | return LowestCallLevel < Other.LowestCallLevel; |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 474 | if (StartOfStringLiteral != Other.StartOfStringLiteral) |
| 475 | return StartOfStringLiteral < Other.StartOfStringLiteral; |
Daniel Jasper | 54b4e44 | 2013-05-22 05:27:42 +0000 | [diff] [blame] | 476 | if (IgnoreStackForComparison || Other.IgnoreStackForComparison) |
| 477 | return false; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 478 | return Stack < Other.Stack; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 479 | } |
| 480 | }; |
| 481 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 482 | /// \brief Appends the next token to \p State and updates information |
| 483 | /// necessary for indentation. |
| 484 | /// |
Nico Weber | 1907c57 | 2013-06-26 02:42:46 +0000 | [diff] [blame] | 485 | /// Puts the token on the current line if \p Newline is \c false and adds a |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 486 | /// line break and necessary indentation otherwise. |
| 487 | /// |
| 488 | /// If \p DryRun is \c false, also creates and stores the required |
| 489 | /// \c Replacement. |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 490 | unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 491 | const FormatToken &Current = *State.NextToken; |
| 492 | const FormatToken &Previous = *State.NextToken->Previous; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 493 | |
Daniel Jasper | 92f9faf | 2013-03-20 15:58:10 +0000 | [diff] [blame] | 494 | if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) { |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 495 | // FIXME: Is this correct? |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 496 | int WhitespaceLength = SourceMgr.getSpellingColumnNumber( |
| 497 | State.NextToken->WhitespaceRange.getEnd()) - |
| 498 | SourceMgr.getSpellingColumnNumber( |
| 499 | State.NextToken->WhitespaceRange.getBegin()); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 500 | State.Column += WhitespaceLength + State.NextToken->CodePointCount; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 501 | State.NextToken = State.NextToken->Next; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 502 | return 0; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 505 | // If we are continuing an expression, we want to indent an extra 4 spaces. |
| 506 | unsigned ContinuationIndent = |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 507 | std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 508 | if (Newline) { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 509 | if (Current.is(tok::r_brace)) { |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 510 | State.Column = Line.Level * Style.IndentWidth; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 511 | } else if (Current.is(tok::string_literal) && |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 512 | State.StartOfStringLiteral != 0) { |
| 513 | State.Column = State.StartOfStringLiteral; |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 514 | State.Stack.back().BreakBeforeParameter = true; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 515 | } else if (Current.is(tok::lessless) && |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 516 | State.Stack.back().FirstLessLess != 0) { |
| 517 | State.Column = State.Stack.back().FirstLessLess; |
Daniel Jasper | 5ad390d | 2013-05-28 11:30:49 +0000 | [diff] [blame] | 518 | } else if (Current.isOneOf(tok::period, tok::arrow) && |
| 519 | Current.Type != TT_DesignatedInitializerPeriod) { |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 520 | if (State.Stack.back().CallContinuation == 0) { |
| 521 | State.Column = ContinuationIndent; |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 522 | State.Stack.back().CallContinuation = State.Column; |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 523 | } else { |
| 524 | State.Column = State.Stack.back().CallContinuation; |
| 525 | } |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 526 | } else if (Current.Type == TT_ConditionalExpr) { |
| 527 | State.Column = State.Stack.back().QuestionColumn; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 528 | } else if (Previous.is(tok::comma) && |
| 529 | State.Stack.back().VariablePos != 0) { |
| 530 | State.Column = State.Stack.back().VariablePos; |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 531 | } else if (Previous.ClosesTemplateDeclaration || |
Daniel Jasper | 53e72cd | 2013-05-06 08:27:33 +0000 | [diff] [blame] | 532 | (Current.Type == TT_StartOfName && State.ParenLevel == 0 && |
Manuel Klimek | a9a7f10 | 2013-06-21 17:25:42 +0000 | [diff] [blame] | 533 | (!Style.IndentFunctionDeclarationAfterType || |
| 534 | Line.StartsDefinition))) { |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 535 | State.Column = State.Stack.back().Indent; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 536 | } else if (Current.Type == TT_ObjCSelectorName) { |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 537 | if (State.Stack.back().ColonPos > Current.CodePointCount) { |
| 538 | State.Column = State.Stack.back().ColonPos - Current.CodePointCount; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 539 | } else { |
| 540 | State.Column = State.Stack.back().Indent; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 541 | State.Stack.back().ColonPos = State.Column + Current.CodePointCount; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 542 | } |
Daniel Jasper | b2f063a | 2013-05-08 10:00:18 +0000 | [diff] [blame] | 543 | } else if (Current.Type == TT_StartOfName || |
| 544 | Previous.isOneOf(tok::coloncolon, tok::equal) || |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 545 | Previous.Type == TT_ObjCMethodExpr) { |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 546 | State.Column = ContinuationIndent; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 547 | } else { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 548 | State.Column = State.Stack.back().Indent; |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 549 | // Ensure that we fall back to indenting 4 spaces instead of just |
| 550 | // flushing continuations left. |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 551 | if (State.Column == FirstIndent) |
| 552 | State.Column += 4; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 555 | if (Current.is(tok::question)) |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 556 | State.Stack.back().BreakBeforeParameter = true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 557 | if ((Previous.isOneOf(tok::comma, tok::semi) && |
| 558 | !State.Stack.back().AvoidBinPacking) || |
| 559 | Previous.Type == TT_BinaryOperator) |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 560 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | 33f4b90 | 2013-05-15 09:35:08 +0000 | [diff] [blame] | 561 | if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0) |
| 562 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 563 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 564 | if (!DryRun) { |
Daniel Jasper | 1ef81d5 | 2013-02-26 13:10:34 +0000 | [diff] [blame] | 565 | unsigned NewLines = 1; |
Alexander Kornienko | e3f1197 | 2013-06-12 19:04:12 +0000 | [diff] [blame] | 566 | if (Current.is(tok::comment)) |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 567 | NewLines = std::max( |
| 568 | NewLines, |
| 569 | std::min(Current.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1)); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 570 | Whitespaces.replaceWhitespace(Current, NewLines, State.Column, |
| 571 | State.Column, Line.InPPDirective); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 572 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 573 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 574 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | 5ad390d | 2013-05-28 11:30:49 +0000 | [diff] [blame] | 575 | if (Current.isOneOf(tok::arrow, tok::period) && |
| 576 | Current.Type != TT_DesignatedInitializerPeriod) |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 577 | State.Stack.back().LastSpace += Current.CodePointCount; |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 578 | State.StartOfLineLevel = State.ParenLevel; |
Daniel Jasper | ce912f4 | 2013-06-03 09:54:46 +0000 | [diff] [blame] | 579 | State.LowestCallLevel = State.ParenLevel; |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 580 | |
| 581 | // Any break on this level means that the parent level has been broken |
| 582 | // and we need to avoid bin packing there. |
| 583 | for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { |
| 584 | State.Stack[i].BreakBeforeParameter = true; |
| 585 | } |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 586 | const FormatToken *TokenBefore = Current.getPreviousNoneComment(); |
Daniel Jasper | 01218ff | 2013-04-15 22:36:37 +0000 | [diff] [blame] | 587 | if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) && |
Daniel Jasper | 33f4b90 | 2013-05-15 09:35:08 +0000 | [diff] [blame] | 588 | TokenBefore->Type != TT_TemplateCloser && |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 589 | TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope()) |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 590 | State.Stack.back().BreakBeforeParameter = true; |
| 591 | |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 592 | // If we break after {, we should also break before the corresponding }. |
| 593 | if (Previous.is(tok::l_brace)) |
| 594 | State.Stack.back().BreakBeforeClosingBrace = true; |
| 595 | |
| 596 | if (State.Stack.back().AvoidBinPacking) { |
| 597 | // If we are breaking after '(', '{', '<', this is not bin packing |
| 598 | // unless AllowAllParametersOfDeclarationOnNextLine is false. |
Daniel Jasper | d741f02 | 2013-05-14 20:39:56 +0000 | [diff] [blame] | 599 | if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) || |
| 600 | Previous.Type == TT_BinaryOperator) || |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 601 | (!Style.AllowAllParametersOfDeclarationOnNextLine && |
| 602 | Line.MustBeDeclaration)) |
| 603 | State.Stack.back().BreakBeforeParameter = true; |
| 604 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 605 | } else { |
Daniel Jasper | 9c3e71a | 2013-02-25 15:59:54 +0000 | [diff] [blame] | 606 | if (Current.is(tok::equal) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 607 | (RootToken->is(tok::kw_for) || State.ParenLevel == 0) && |
Daniel Jasper | adc0f09 | 2013-04-05 09:38:50 +0000 | [diff] [blame] | 608 | State.Stack.back().VariablePos == 0) { |
| 609 | State.Stack.back().VariablePos = State.Column; |
| 610 | // Move over * and & if they are bound to the variable name. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 611 | const FormatToken *Tok = &Previous; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 612 | while (Tok && State.Stack.back().VariablePos >= Tok->CodePointCount) { |
| 613 | State.Stack.back().VariablePos -= Tok->CodePointCount; |
Daniel Jasper | adc0f09 | 2013-04-05 09:38:50 +0000 | [diff] [blame] | 614 | if (Tok->SpacesRequiredBefore != 0) |
| 615 | break; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 616 | Tok = Tok->Previous; |
Daniel Jasper | adc0f09 | 2013-04-05 09:38:50 +0000 | [diff] [blame] | 617 | } |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 618 | if (Previous.PartOfMultiVariableDeclStmt) |
| 619 | State.Stack.back().LastSpace = State.Stack.back().VariablePos; |
| 620 | } |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 621 | |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 622 | unsigned Spaces = State.NextToken->SpacesRequiredBefore; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 623 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 624 | if (!DryRun) |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 625 | Whitespaces.replaceWhitespace(Current, 0, Spaces, |
| 626 | State.Column + Spaces); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 627 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 628 | if (Current.Type == TT_ObjCSelectorName && |
| 629 | State.Stack.back().ColonPos == 0) { |
| 630 | if (State.Stack.back().Indent + Current.LongestObjCSelectorName > |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 631 | State.Column + Spaces + Current.CodePointCount) |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 632 | State.Stack.back().ColonPos = |
| 633 | State.Stack.back().Indent + Current.LongestObjCSelectorName; |
| 634 | else |
| 635 | State.Stack.back().ColonPos = |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 636 | State.Column + Spaces + Current.CodePointCount; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 639 | if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr && |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 640 | Current.Type != TT_LineComment) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 641 | State.Stack.back().Indent = State.Column + Spaces; |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 642 | if (Previous.is(tok::comma) && !Current.isTrailingComment() && |
| 643 | State.Stack.back().AvoidBinPacking) |
| 644 | State.Stack.back().NoLineBreak = true; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 645 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 646 | State.Column += Spaces; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 647 | if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for)) |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 648 | // Treat the condition inside an if as if it was a second function |
| 649 | // parameter, i.e. let nested calls have an indent of 4. |
| 650 | State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(". |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 651 | else if (Previous.is(tok::comma)) |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 652 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 653 | else if ((Previous.Type == TT_BinaryOperator || |
Daniel Jasper | 02b771e | 2013-01-28 13:31:35 +0000 | [diff] [blame] | 654 | Previous.Type == TT_ConditionalExpr || |
| 655 | Previous.Type == TT_CtorInitializerColon) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 656 | !(Previous.getPrecedence() == prec::Assignment && |
Daniel Jasper | 512843a | 2013-05-27 12:45:09 +0000 | [diff] [blame] | 657 | Current.FakeLParens.empty())) |
| 658 | // Always indent relative to the RHS of the expression unless this is a |
| 659 | // simple assignment without binary expression on the RHS. |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 660 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 661 | else if (Previous.Type == TT_InheritanceColon) |
| 662 | State.Stack.back().Indent = State.Column; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 663 | else if (Previous.opensScope() && !Current.FakeLParens.empty()) |
| 664 | // If this function has multiple parameters or a binary expression |
| 665 | // parameter, indent nested calls from the start of the first parameter. |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 666 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 667 | } |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 668 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 669 | return moveStateToNextToken(State, DryRun); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 670 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 671 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 672 | /// \brief Mark the next token as consumed in \p State and modify its stacks |
| 673 | /// accordingly. |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 674 | unsigned moveStateToNextToken(LineState &State, bool DryRun) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 675 | const FormatToken &Current = *State.NextToken; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 676 | assert(State.Stack.size()); |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 677 | |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 678 | if (Current.Type == TT_InheritanceColon) |
| 679 | State.Stack.back().AvoidBinPacking = true; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 680 | if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0) |
| 681 | State.Stack.back().FirstLessLess = State.Column; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 682 | if (Current.is(tok::question)) |
| 683 | State.Stack.back().QuestionColumn = State.Column; |
Daniel Jasper | ce912f4 | 2013-06-03 09:54:46 +0000 | [diff] [blame] | 684 | if (Current.isOneOf(tok::period, tok::arrow)) { |
| 685 | State.LowestCallLevel = std::min(State.LowestCallLevel, State.ParenLevel); |
| 686 | if (Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0) |
| 687 | State.Stack.back().StartOfFunctionCall = |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 688 | Current.LastInChainOfCalls ? 0 |
| 689 | : State.Column + Current.CodePointCount; |
Daniel Jasper | ce912f4 | 2013-06-03 09:54:46 +0000 | [diff] [blame] | 690 | } |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 691 | if (Current.Type == TT_CtorInitializerColon) { |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 692 | // Indent 2 from the column, so: |
| 693 | // SomeClass::SomeClass() |
| 694 | // : First(...), ... |
| 695 | // Next(...) |
| 696 | // ^ line up here. |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 697 | State.Stack.back().Indent = State.Column + 2; |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 698 | if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) |
| 699 | State.Stack.back().AvoidBinPacking = true; |
| 700 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 701 | } |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 702 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 703 | // If return returns a binary expression, align after it. |
| 704 | if (Current.is(tok::kw_return) && !Current.FakeLParens.empty()) |
| 705 | State.Stack.back().LastSpace = State.Column + 7; |
| 706 | |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 707 | // In ObjC method declaration we align on the ":" of parameters, but we need |
| 708 | // to ensure that we indent parameters on subsequent lines by at least 4. |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 709 | if (Current.Type == TT_ObjCMethodSpecifier) |
| 710 | State.Stack.back().Indent += 4; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 711 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 712 | // Insert scopes created by fake parenthesis. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 713 | const FormatToken *Previous = Current.getPreviousNoneComment(); |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 714 | // Don't add extra indentation for the first fake parenthesis after |
| 715 | // 'return', assignements or opening <({[. The indentation for these cases |
| 716 | // is special cased. |
| 717 | bool SkipFirstExtraIndent = |
| 718 | Current.is(tok::kw_return) || |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 719 | (Previous && (Previous->opensScope() || |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 720 | Previous->getPrecedence() == prec::Assignment)); |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 721 | for (SmallVector<prec::Level, 4>::const_reverse_iterator |
| 722 | I = Current.FakeLParens.rbegin(), |
| 723 | E = Current.FakeLParens.rend(); |
| 724 | I != E; ++I) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 725 | ParenState NewParenState = State.Stack.back(); |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 726 | NewParenState.ForFakeParenthesis = true; |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 727 | NewParenState.Indent = |
| 728 | std::max(std::max(State.Column, NewParenState.Indent), |
| 729 | State.Stack.back().LastSpace); |
| 730 | |
| 731 | // Always indent conditional expressions. Never indent expression where |
| 732 | // the 'operator' is ',', ';' or an assignment (i.e. *I <= |
| 733 | // prec::Assignment) as those have different indentation rules. Indent |
| 734 | // other expression, unless the indentation needs to be skipped. |
| 735 | if (*I == prec::Conditional || |
| 736 | (!SkipFirstExtraIndent && *I > prec::Assignment)) |
| 737 | NewParenState.Indent += 4; |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 738 | if (Previous && !Previous->opensScope()) |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 739 | NewParenState.BreakBeforeParameter = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 740 | State.Stack.push_back(NewParenState); |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 741 | SkipFirstExtraIndent = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 744 | // If we encounter an opening (, [, { or <, we add a level to our stacks to |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 745 | // prepare for the following tokens. |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 746 | if (Current.opensScope()) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 747 | unsigned NewIndent; |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 748 | unsigned LastSpace = State.Stack.back().LastSpace; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 749 | bool AvoidBinPacking; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 750 | if (Current.is(tok::l_brace)) { |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 751 | NewIndent = Style.IndentWidth + LastSpace; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 752 | const FormatToken *NextNoComment = Current.getNextNoneComment(); |
Daniel Jasper | 5ad390d | 2013-05-28 11:30:49 +0000 | [diff] [blame] | 753 | AvoidBinPacking = NextNoComment && |
| 754 | NextNoComment->Type == TT_DesignatedInitializerPeriod; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 755 | } else { |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 756 | NewIndent = |
| 757 | 4 + std::max(LastSpace, State.Stack.back().StartOfFunctionCall); |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 758 | AvoidBinPacking = !Style.BinPackParameters; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 759 | } |
Daniel Jasper | fca24bc | 2013-04-25 13:31:51 +0000 | [diff] [blame] | 760 | |
Daniel Jasper | c3df5ff | 2013-05-13 09:19:24 +0000 | [diff] [blame] | 761 | State.Stack.push_back(ParenState(NewIndent, LastSpace, AvoidBinPacking, |
| 762 | State.Stack.back().NoLineBreak)); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 763 | ++State.ParenLevel; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 766 | // If this '[' opens an ObjC call, determine whether all parameters fit into |
| 767 | // one line and put one per line if they don't. |
| 768 | if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr && |
| 769 | Current.MatchingParen != NULL) { |
| 770 | if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit()) |
| 771 | State.Stack.back().BreakBeforeParameter = true; |
| 772 | } |
| 773 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 774 | // If we encounter a closing ), ], } or >, we can remove a level from our |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 775 | // stacks. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 776 | if (Current.isOneOf(tok::r_paren, tok::r_square) || |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 777 | (Current.is(tok::r_brace) && State.NextToken != RootToken) || |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 778 | State.NextToken->Type == TT_TemplateCloser) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 779 | State.Stack.pop_back(); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 780 | --State.ParenLevel; |
| 781 | } |
| 782 | |
| 783 | // Remove scopes created by fake parenthesis. |
| 784 | for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) { |
Daniel Jasper | abfc9c1 | 2013-04-04 19:31:00 +0000 | [diff] [blame] | 785 | unsigned VariablePos = State.Stack.back().VariablePos; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 786 | State.Stack.pop_back(); |
Daniel Jasper | abfc9c1 | 2013-04-04 19:31:00 +0000 | [diff] [blame] | 787 | State.Stack.back().VariablePos = VariablePos; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 788 | } |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 789 | |
Daniel Jasper | 27c7f54 | 2013-05-13 20:50:15 +0000 | [diff] [blame] | 790 | if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) { |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 791 | State.StartOfStringLiteral = State.Column; |
Daniel Jasper | 27c7f54 | 2013-05-13 20:50:15 +0000 | [diff] [blame] | 792 | } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash, |
| 793 | tok::string_literal)) { |
Daniel Jasper | 9a2f8d0 | 2013-05-16 04:26:02 +0000 | [diff] [blame] | 794 | State.StartOfStringLiteral = 0; |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 797 | State.Column += Current.CodePointCount; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 798 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 799 | State.NextToken = State.NextToken->Next; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 800 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 801 | return breakProtrudingToken(Current, State, DryRun); |
| 802 | } |
| 803 | |
| 804 | /// \brief If the current token sticks out over the end of the line, break |
| 805 | /// it if possible. |
Manuel Klimek | 2a9805d | 2013-05-14 09:04:24 +0000 | [diff] [blame] | 806 | /// |
| 807 | /// \returns An extra penalty if a token was broken, otherwise 0. |
| 808 | /// |
| 809 | /// Note that the penalty of the token protruding the allowed line length is |
| 810 | /// already handled in \c addNextStateToQueue; the returned penalty will only |
| 811 | /// cover the cost of the additional line breaks. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 812 | unsigned breakProtrudingToken(const FormatToken &Current, LineState &State, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 813 | bool DryRun) { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 814 | llvm::OwningPtr<BreakableToken> Token; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 815 | unsigned StartColumn = State.Column - Current.CodePointCount; |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 816 | unsigned OriginalStartColumn = |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 817 | SourceMgr.getSpellingColumnNumber(Current.getStartOfNonWhitespace()) - |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 818 | 1; |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 819 | |
Daniel Jasper | 5d5b424 | 2013-05-16 12:59:13 +0000 | [diff] [blame] | 820 | if (Current.is(tok::string_literal) && |
| 821 | Current.Type != TT_ImplicitStringLiteral) { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 822 | // Only break up default narrow strings. |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 823 | if (!Current.TokenText.startswith("\"")) |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 824 | return 0; |
| 825 | |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 826 | Token.reset(new BreakableStringLiteral(Current, StartColumn, |
| 827 | Line.InPPDirective, Encoding)); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 828 | } else if (Current.Type == TT_BlockComment) { |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 829 | Token.reset(new BreakableBlockComment( |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 830 | Style, Current, StartColumn, OriginalStartColumn, !Current.Previous, |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 831 | Line.InPPDirective, Encoding)); |
Daniel Jasper | 7ff96ed | 2013-05-06 10:24:51 +0000 | [diff] [blame] | 832 | } else if (Current.Type == TT_LineComment && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 833 | (Current.Previous == NULL || |
| 834 | Current.Previous->Type != TT_ImplicitStringLiteral)) { |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 835 | Token.reset(new BreakableLineComment(Current, StartColumn, |
| 836 | Line.InPPDirective, Encoding)); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 837 | } else { |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 838 | return 0; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 839 | } |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 840 | if (Current.UnbreakableTailLength >= getColumnLimit()) |
Manuel Klimek | 2a9805d | 2013-05-14 09:04:24 +0000 | [diff] [blame] | 841 | return 0; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 842 | |
Alexander Kornienko | c36c5c2 | 2013-06-19 19:50:11 +0000 | [diff] [blame] | 843 | unsigned RemainingSpace = getColumnLimit() - Current.UnbreakableTailLength; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 844 | bool BreakInserted = false; |
| 845 | unsigned Penalty = 0; |
Alexander Kornienko | c36c5c2 | 2013-06-19 19:50:11 +0000 | [diff] [blame] | 846 | unsigned RemainingTokenColumns = 0; |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 847 | for (unsigned LineIndex = 0, EndIndex = Token->getLineCount(); |
| 848 | LineIndex != EndIndex; ++LineIndex) { |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 849 | if (!DryRun) |
| 850 | Token->replaceWhitespaceBefore(LineIndex, Whitespaces); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 851 | unsigned TailOffset = 0; |
Alexander Kornienko | c36c5c2 | 2013-06-19 19:50:11 +0000 | [diff] [blame] | 852 | RemainingTokenColumns = Token->getLineLengthAfterSplit( |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 853 | LineIndex, TailOffset, StringRef::npos); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 854 | while (RemainingTokenColumns > RemainingSpace) { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 855 | BreakableToken::Split Split = |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 856 | Token->getSplit(LineIndex, TailOffset, getColumnLimit()); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 857 | if (Split.first == StringRef::npos) |
| 858 | break; |
| 859 | assert(Split.first != 0); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 860 | unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit( |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 861 | LineIndex, TailOffset + Split.first + Split.second, |
| 862 | StringRef::npos); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 863 | assert(NewRemainingTokenColumns < RemainingTokenColumns); |
Alexander Kornienko | 16a0ec6 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 864 | if (!DryRun) |
| 865 | Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces); |
Alexander Kornienko | 2785b9a | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 866 | Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString |
| 867 | : Style.PenaltyBreakComment; |
| 868 | unsigned ColumnsUsed = |
| 869 | Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first); |
| 870 | if (ColumnsUsed > getColumnLimit()) { |
| 871 | Penalty += |
| 872 | Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit()); |
| 873 | } |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 874 | TailOffset += Split.first + Split.second; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 875 | RemainingTokenColumns = NewRemainingTokenColumns; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 876 | BreakInserted = true; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 877 | } |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Alexander Kornienko | c36c5c2 | 2013-06-19 19:50:11 +0000 | [diff] [blame] | 880 | State.Column = RemainingTokenColumns; |
| 881 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 882 | if (BreakInserted) { |
Alexander Kornienko | 22d0e29 | 2013-06-17 12:59:44 +0000 | [diff] [blame] | 883 | // If we break the token inside a parameter list, we need to break before |
| 884 | // the next parameter on all levels, so that the next parameter is clearly |
| 885 | // visible. Line comments already introduce a break. |
| 886 | if (Current.Type != TT_LineComment) { |
| 887 | for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) |
| 888 | State.Stack[i].BreakBeforeParameter = true; |
| 889 | } |
| 890 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 891 | State.Stack.back().LastSpace = StartColumn; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 892 | } |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 893 | return Penalty; |
| 894 | } |
| 895 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 896 | unsigned getColumnLimit() { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 897 | // In preprocessor directives reserve two chars for trailing " \" |
| 898 | return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0); |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 901 | /// \brief An edge in the solution space from \c Previous->State to \c State, |
| 902 | /// inserting a newline dependent on the \c NewLine. |
| 903 | struct StateNode { |
| 904 | StateNode(const LineState &State, bool NewLine, StateNode *Previous) |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 905 | : State(State), NewLine(NewLine), Previous(Previous) {} |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 906 | LineState State; |
| 907 | bool NewLine; |
| 908 | StateNode *Previous; |
| 909 | }; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 910 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 911 | /// \brief A pair of <penalty, count> that is used to prioritize the BFS on. |
| 912 | /// |
| 913 | /// In case of equal penalties, we want to prefer states that were inserted |
| 914 | /// first. During state generation we make sure that we insert states first |
| 915 | /// that break the line as late as possible. |
| 916 | typedef std::pair<unsigned, unsigned> OrderedPenalty; |
| 917 | |
| 918 | /// \brief An item in the prioritized BFS search queue. The \c StateNode's |
| 919 | /// \c State has the given \c OrderedPenalty. |
| 920 | typedef std::pair<OrderedPenalty, StateNode *> QueueItem; |
| 921 | |
| 922 | /// \brief The BFS queue type. |
| 923 | typedef std::priority_queue<QueueItem, std::vector<QueueItem>, |
| 924 | std::greater<QueueItem> > QueueType; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 925 | |
| 926 | /// \brief Analyze the entire solution space starting from \p InitialState. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 927 | /// |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 928 | /// This implements a variant of Dijkstra's algorithm on the graph that spans |
| 929 | /// the solution space (\c LineStates are the nodes). The algorithm tries to |
| 930 | /// find the shortest path (the one with lowest penalty) from \p InitialState |
| 931 | /// to a state where all tokens are placed. |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 932 | void analyzeSolutionSpace(LineState &InitialState) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 933 | std::set<LineState> Seen; |
| 934 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 935 | // Insert start element into queue. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 936 | StateNode *Node = |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 937 | new (Allocator.Allocate()) StateNode(InitialState, false, NULL); |
| 938 | Queue.push(QueueItem(OrderedPenalty(0, Count), Node)); |
| 939 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 940 | |
| 941 | // While not empty, take first element and follow edges. |
| 942 | while (!Queue.empty()) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 943 | unsigned Penalty = Queue.top().first.first; |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 944 | StateNode *Node = Queue.top().second; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 945 | if (Node->State.NextToken == NULL) { |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 946 | DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n"); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 947 | break; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 948 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 949 | Queue.pop(); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 950 | |
Daniel Jasper | 54b4e44 | 2013-05-22 05:27:42 +0000 | [diff] [blame] | 951 | // Cut off the analysis of certain solutions if the analysis gets too |
| 952 | // complex. See description of IgnoreStackForComparison. |
| 953 | if (Count > 10000) |
| 954 | Node->State.IgnoreStackForComparison = true; |
| 955 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 956 | if (!Seen.insert(Node->State).second) |
| 957 | // State already examined with lower penalty. |
| 958 | continue; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 959 | |
Nico Weber | 2726877 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 960 | addNextStateToQueue(Penalty, Node, /*NewLine=*/false); |
| 961 | addNextStateToQueue(Penalty, Node, /*NewLine=*/true); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | if (Queue.empty()) |
| 965 | // We were unable to find a solution, do nothing. |
| 966 | // FIXME: Add diagnostic? |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 967 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 968 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 969 | // Reconstruct the solution. |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 970 | reconstructPath(InitialState, Queue.top().second); |
Alexander Kornienko | dd25631 | 2013-05-10 11:56:10 +0000 | [diff] [blame] | 971 | DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n"); |
| 972 | DEBUG(llvm::dbgs() << "---\n"); |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | void reconstructPath(LineState &State, StateNode *Current) { |
Manuel Klimek | 9c333b9 | 2013-05-29 15:10:11 +0000 | [diff] [blame] | 976 | std::deque<StateNode *> Path; |
| 977 | // We do not need a break before the initial token. |
| 978 | while (Current->Previous) { |
| 979 | Path.push_front(Current); |
| 980 | Current = Current->Previous; |
| 981 | } |
| 982 | for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end(); |
| 983 | I != E; ++I) { |
| 984 | DEBUG({ |
| 985 | if ((*I)->NewLine) { |
| 986 | llvm::dbgs() << "Penalty for splitting before " |
| 987 | << (*I)->Previous->State.NextToken->Tok.getName() << ": " |
| 988 | << (*I)->Previous->State.NextToken->SplitPenalty << "\n"; |
| 989 | } |
| 990 | }); |
| 991 | addTokenToState((*I)->NewLine, false, State); |
| 992 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 995 | /// \brief Add the following state to the analysis queue \c Queue. |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 996 | /// |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 997 | /// Assume the current state is \p PreviousNode and has been reached with a |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 998 | /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true. |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 999 | void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode, |
| 1000 | bool NewLine) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1001 | if (NewLine && !canBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1002 | return; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1003 | if (!NewLine && mustBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1004 | return; |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 1005 | if (NewLine) |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1006 | Penalty += PreviousNode->State.NextToken->SplitPenalty; |
| 1007 | |
| 1008 | StateNode *Node = new (Allocator.Allocate()) |
| 1009 | StateNode(PreviousNode->State, NewLine, PreviousNode); |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 1010 | Penalty += addTokenToState(NewLine, true, Node->State); |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1011 | if (Node->State.Column > getColumnLimit()) { |
| 1012 | unsigned ExcessCharacters = Node->State.Column - getColumnLimit(); |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 1013 | Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 1014 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 1015 | |
| 1016 | Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node)); |
| 1017 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1018 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1019 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1020 | /// \brief Returns \c true, if a line break after \p State is allowed. |
| 1021 | bool canBreak(const LineState &State) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1022 | const FormatToken &Current = *State.NextToken; |
| 1023 | const FormatToken &Previous = *Current.Previous; |
| 1024 | assert(&Previous == Current.Previous); |
Daniel Jasper | 399914b | 2013-05-17 09:35:01 +0000 | [diff] [blame] | 1025 | if (!Current.CanBreakBefore && |
| 1026 | !(Current.is(tok::r_brace) && |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1027 | State.Stack.back().BreakBeforeClosingBrace)) |
| 1028 | return false; |
Daniel Jasper | 399914b | 2013-05-17 09:35:01 +0000 | [diff] [blame] | 1029 | // The opening "{" of a braced list has to be on the same line as the first |
| 1030 | // element if it is nested in another braced init list or function call. |
| 1031 | if (!Current.MustBreakBefore && Previous.is(tok::l_brace) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1032 | Previous.Previous && |
| 1033 | Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma)) |
Daniel Jasper | 399914b | 2013-05-17 09:35:01 +0000 | [diff] [blame] | 1034 | return false; |
Daniel Jasper | 259a038 | 2013-05-27 11:50:16 +0000 | [diff] [blame] | 1035 | // This prevents breaks like: |
| 1036 | // ... |
| 1037 | // SomeParameter, OtherParameter).DoSomething( |
| 1038 | // ... |
| 1039 | // As they hide "DoSomething" and are generally bad for readability. |
Daniel Jasper | ce912f4 | 2013-06-03 09:54:46 +0000 | [diff] [blame] | 1040 | if (Previous.opensScope() && State.LowestCallLevel < State.StartOfLineLevel) |
Daniel Jasper | 259a038 | 2013-05-27 11:50:16 +0000 | [diff] [blame] | 1041 | return false; |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 1042 | return !State.Stack.back().NoLineBreak; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1043 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1044 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1045 | /// \brief Returns \c true, if a line break after \p State is mandatory. |
| 1046 | bool mustBreak(const LineState &State) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1047 | const FormatToken &Current = *State.NextToken; |
| 1048 | const FormatToken &Previous = *Current.Previous; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1049 | if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1050 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1051 | if (Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1052 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1053 | if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1054 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1055 | if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) || |
| 1056 | Current.Type == TT_ConditionalExpr) && |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 1057 | State.Stack.back().BreakBeforeParameter && |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1058 | !Current.isTrailingComment() && |
| 1059 | !Current.isOneOf(tok::r_paren, tok::r_brace)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1060 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1061 | |
| 1062 | // If we need to break somewhere inside the LHS of a binary expression, we |
| 1063 | // should also break after the operator. |
| 1064 | if (Previous.Type == TT_BinaryOperator && |
Daniel Jasper | 69c4371 | 2013-05-28 07:42:44 +0000 | [diff] [blame] | 1065 | Current.Type != TT_BinaryOperator && // Special case for ">>". |
Daniel Jasper | 5ef8aac | 2013-06-03 08:42:05 +0000 | [diff] [blame] | 1066 | !Current.isTrailingComment() && |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1067 | !Previous.isOneOf(tok::lessless, tok::question) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1068 | Previous.getPrecedence() != prec::Assignment && |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 1069 | State.Stack.back().BreakBeforeParameter) |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 1070 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1071 | |
| 1072 | // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding |
| 1073 | // out whether it is the first parameter. Clean this up. |
| 1074 | if (Current.Type == TT_ObjCSelectorName && |
| 1075 | Current.LongestObjCSelectorName == 0 && |
| 1076 | State.Stack.back().BreakBeforeParameter) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1077 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1078 | if ((Current.Type == TT_CtorInitializerColon || |
| 1079 | (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0))) |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 1080 | return true; |
Daniel Jasper | 11e1380 | 2013-05-08 14:12:04 +0000 | [diff] [blame] | 1081 | |
Daniel Jasper | 33f4b90 | 2013-05-15 09:35:08 +0000 | [diff] [blame] | 1082 | if (Current.Type == TT_StartOfName && Line.MightBeFunctionDecl && |
| 1083 | State.Stack.back().BreakBeforeParameter && State.ParenLevel == 0) |
| 1084 | return true; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 1085 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 1088 | // Returns the total number of columns required for the remaining tokens. |
| 1089 | unsigned getRemainingLength(const LineState &State) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1090 | if (State.NextToken && State.NextToken->Previous) |
| 1091 | return Line.Last->TotalLength - State.NextToken->Previous->TotalLength; |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 1092 | return 0; |
| 1093 | } |
| 1094 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1095 | FormatStyle Style; |
| 1096 | SourceManager &SourceMgr; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1097 | const AnnotatedLine &Line; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1098 | const unsigned FirstIndent; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1099 | const FormatToken *RootToken; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1100 | WhitespaceManager &Whitespaces; |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 1101 | |
| 1102 | llvm::SpecificBumpPtrAllocator<StateNode> Allocator; |
| 1103 | QueueType Queue; |
| 1104 | // Increasing count of \c StateNode items we have created. This is used |
| 1105 | // to create a deterministic order independent of the container. |
| 1106 | unsigned Count; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1107 | encoding::Encoding Encoding; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1108 | }; |
| 1109 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1110 | class FormatTokenLexer { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1111 | public: |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1112 | FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr, |
| 1113 | encoding::Encoding Encoding) |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1114 | : FormatTok(NULL), GreaterStashed(false), TrailingWhitespace(0), Lex(Lex), |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1115 | SourceMgr(SourceMgr), IdentTable(Lex.getLangOpts()), |
| 1116 | Encoding(Encoding) { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1117 | Lex.SetKeepWhitespaceMode(true); |
| 1118 | } |
| 1119 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1120 | ArrayRef<FormatToken *> lex() { |
| 1121 | assert(Tokens.empty()); |
| 1122 | do { |
| 1123 | Tokens.push_back(getNextToken()); |
| 1124 | } while (Tokens.back()->Tok.isNot(tok::eof)); |
| 1125 | return Tokens; |
| 1126 | } |
| 1127 | |
| 1128 | IdentifierTable &getIdentTable() { return IdentTable; } |
| 1129 | |
| 1130 | private: |
| 1131 | FormatToken *getNextToken() { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1132 | if (GreaterStashed) { |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1133 | // Create a synthesized second '>' token. |
| 1134 | Token Greater = FormatTok->Tok; |
| 1135 | FormatTok = new (Allocator.Allocate()) FormatToken; |
| 1136 | FormatTok->Tok = Greater; |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1137 | SourceLocation GreaterLocation = |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1138 | FormatTok->Tok.getLocation().getLocWithOffset(1); |
| 1139 | FormatTok->WhitespaceRange = |
| 1140 | SourceRange(GreaterLocation, GreaterLocation); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1141 | FormatTok->TokenText = ">"; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1142 | FormatTok->CodePointCount = 1; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1143 | GreaterStashed = false; |
| 1144 | return FormatTok; |
| 1145 | } |
| 1146 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1147 | FormatTok = new (Allocator.Allocate()) FormatToken; |
| 1148 | Lex.LexFromRawLexer(FormatTok->Tok); |
| 1149 | StringRef Text = rawTokenText(FormatTok->Tok); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 1150 | SourceLocation WhitespaceStart = |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1151 | FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace); |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1152 | if (SourceMgr.getFileOffset(WhitespaceStart) == 0) |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1153 | FormatTok->IsFirst = true; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1154 | |
| 1155 | // Consume and record whitespace until we find a significant token. |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 1156 | unsigned WhitespaceLength = TrailingWhitespace; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1157 | while (FormatTok->Tok.is(tok::unknown)) { |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1158 | unsigned Newlines = Text.count('\n'); |
Daniel Jasper | 1eee6c4 | 2013-03-04 13:43:19 +0000 | [diff] [blame] | 1159 | if (Newlines > 0) |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1160 | FormatTok->LastNewlineOffset = WhitespaceLength + Text.rfind('\n') + 1; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1161 | FormatTok->NewlinesBefore += Newlines; |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1162 | unsigned EscapedNewlines = Text.count("\\\n"); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1163 | FormatTok->HasUnescapedNewline |= EscapedNewlines != Newlines; |
| 1164 | WhitespaceLength += FormatTok->Tok.getLength(); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1165 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1166 | Lex.LexFromRawLexer(FormatTok->Tok); |
| 1167 | Text = rawTokenText(FormatTok->Tok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1168 | } |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1169 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1170 | // In case the token starts with escaped newlines, we want to |
| 1171 | // take them into account as whitespace - this pattern is quite frequent |
| 1172 | // in macro definitions. |
| 1173 | // FIXME: What do we want to do with other escaped spaces, and escaped |
| 1174 | // spaces or newlines in the middle of tokens? |
| 1175 | // FIXME: Add a more explicit test. |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1176 | while (Text.size() > 1 && Text[0] == '\\' && Text[1] == '\n') { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1177 | // FIXME: ++FormatTok->NewlinesBefore is missing... |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1178 | WhitespaceLength += 2; |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1179 | Text = Text.substr(2); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1182 | TrailingWhitespace = 0; |
| 1183 | if (FormatTok->Tok.is(tok::comment)) { |
| 1184 | StringRef UntrimmedText = Text; |
| 1185 | Text = Text.rtrim(); |
| 1186 | TrailingWhitespace = UntrimmedText.size() - Text.size(); |
| 1187 | } else if (FormatTok->Tok.is(tok::raw_identifier)) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1188 | IdentifierInfo &Info = IdentTable.get(Text); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1189 | FormatTok->Tok.setIdentifierInfo(&Info); |
| 1190 | FormatTok->Tok.setKind(Info.getTokenID()); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1191 | } else if (FormatTok->Tok.is(tok::greatergreater)) { |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1192 | FormatTok->Tok.setKind(tok::greater); |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1193 | Text = Text.substr(0, 1); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1194 | GreaterStashed = true; |
| 1195 | } |
| 1196 | |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1197 | // Now FormatTok is the next non-whitespace token. |
| 1198 | FormatTok->TokenText = Text; |
| 1199 | FormatTok->CodePointCount = encoding::getCodePointCount(Text, Encoding); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1200 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1201 | FormatTok->WhitespaceRange = SourceRange( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1202 | WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength)); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1203 | return FormatTok; |
| 1204 | } |
| 1205 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1206 | FormatToken *FormatTok; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1207 | bool GreaterStashed; |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 1208 | unsigned TrailingWhitespace; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1209 | Lexer &Lex; |
| 1210 | SourceManager &SourceMgr; |
| 1211 | IdentifierTable IdentTable; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1212 | encoding::Encoding Encoding; |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1213 | llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; |
| 1214 | SmallVector<FormatToken *, 16> Tokens; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1215 | |
| 1216 | /// Returns the text of \c FormatTok. |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1217 | StringRef rawTokenText(Token &Tok) { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1218 | return StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
| 1219 | Tok.getLength()); |
| 1220 | } |
| 1221 | }; |
| 1222 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1223 | class Formatter : public UnwrappedLineConsumer { |
| 1224 | public: |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 1225 | Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1226 | const std::vector<CharSourceRange> &Ranges) |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 1227 | : Style(Style), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1228 | Whitespaces(SourceMgr, Style), Ranges(Ranges), |
| 1229 | Encoding(encoding::detectEncoding(Lex.getBuffer())) { |
| 1230 | DEBUG(llvm::dbgs() |
| 1231 | << "File encoding: " |
| 1232 | << (Encoding == encoding::Encoding_UTF8 ? "UTF8" : "unknown") |
| 1233 | << "\n"); |
| 1234 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1235 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1236 | virtual ~Formatter() {} |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 1237 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1238 | tooling::Replacements format() { |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1239 | FormatTokenLexer Tokens(Lex, SourceMgr, Encoding); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1240 | |
| 1241 | UnwrappedLineParser Parser(Style, Tokens.lex(), *this); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1242 | bool StructuralError = Parser.parse(); |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1243 | TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in")); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1244 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1245 | Annotator.annotate(AnnotatedLines[i]); |
| 1246 | } |
| 1247 | deriveLocalStyle(); |
| 1248 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1249 | Annotator.calculateFormattingInformation(AnnotatedLines[i]); |
| 1250 | } |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1251 | |
| 1252 | // Adapt level to the next line if this is a comment. |
| 1253 | // FIXME: Can/should this be done in the UnwrappedLineParser? |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 1254 | const AnnotatedLine *NextNoneCommentLine = NULL; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1255 | for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1256 | if (NextNoneCommentLine && AnnotatedLines[i].First->is(tok::comment) && |
| 1257 | !AnnotatedLines[i].First->Next) |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1258 | AnnotatedLines[i].Level = NextNoneCommentLine->Level; |
| 1259 | else |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1260 | NextNoneCommentLine = |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1261 | AnnotatedLines[i].First->isNot(tok::r_brace) ? &AnnotatedLines[i] |
| 1262 | : NULL; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1265 | std::vector<int> IndentForLevel; |
| 1266 | bool PreviousLineWasTouched = false; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1267 | const FormatToken *PreviousLineLastToken = 0; |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1268 | bool FormatPPDirective = false; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1269 | for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(), |
| 1270 | E = AnnotatedLines.end(); |
| 1271 | I != E; ++I) { |
| 1272 | const AnnotatedLine &TheLine = *I; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1273 | const FormatToken *FirstTok = TheLine.First; |
| 1274 | int Offset = getIndentOffset(*TheLine.First); |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1275 | |
| 1276 | // Check whether this line is part of a formatted preprocessor directive. |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1277 | if (FirstTok->HasUnescapedNewline) |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1278 | FormatPPDirective = false; |
| 1279 | if (!FormatPPDirective && TheLine.InPPDirective && |
| 1280 | (touchesLine(TheLine) || touchesPPDirective(I + 1, E))) |
| 1281 | FormatPPDirective = true; |
| 1282 | |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 1283 | // Determine indent and try to merge multiple unwrapped lines. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1284 | while (IndentForLevel.size() <= TheLine.Level) |
| 1285 | IndentForLevel.push_back(-1); |
| 1286 | IndentForLevel.resize(TheLine.Level + 1); |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 1287 | unsigned Indent = getIndent(IndentForLevel, TheLine.Level); |
| 1288 | if (static_cast<int>(Indent) + Offset >= 0) |
| 1289 | Indent += Offset; |
| 1290 | tryFitMultipleLinesInOne(Indent, I, E); |
| 1291 | |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1292 | bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1293 | if (TheLine.First->is(tok::eof)) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1294 | if (PreviousLineWasTouched) { |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1295 | unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u); |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1296 | Whitespaces.replaceWhitespace(*TheLine.First, NewLines, /*Indent*/ 0, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1297 | /*TargetColumn*/ 0); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1298 | } |
| 1299 | } else if (TheLine.Type != LT_Invalid && |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1300 | (WasMoved || FormatPPDirective || touchesLine(TheLine))) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1301 | unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level); |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1302 | if (FirstTok->WhitespaceRange.isValid() && |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1303 | // Insert a break even if there is a structural error in case where |
| 1304 | // we break apart a line consisting of multiple unwrapped lines. |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1305 | (FirstTok->NewlinesBefore == 0 || !StructuralError)) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1306 | formatFirstToken(*TheLine.First, PreviousLineLastToken, Indent, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1307 | TheLine.InPPDirective); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1308 | } else { |
| 1309 | Indent = LevelIndent = |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 1310 | SourceMgr.getSpellingColumnNumber(FirstTok->Tok.getLocation()) - |
| 1311 | 1; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1312 | } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1313 | UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent, |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1314 | TheLine.First, Whitespaces, Encoding); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1315 | Formatter.format(I + 1 != E ? &*(I + 1) : NULL); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1316 | IndentForLevel[TheLine.Level] = LevelIndent; |
| 1317 | PreviousLineWasTouched = true; |
| 1318 | } else { |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1319 | // Format the first token if necessary, and notify the WhitespaceManager |
| 1320 | // about the unchanged whitespace. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1321 | for (const FormatToken *Tok = TheLine.First; Tok != NULL; |
| 1322 | Tok = Tok->Next) { |
| 1323 | if (Tok == TheLine.First && |
| 1324 | (Tok->NewlinesBefore > 0 || Tok->IsFirst)) { |
| 1325 | unsigned LevelIndent = |
| 1326 | SourceMgr.getSpellingColumnNumber(Tok->Tok.getLocation()) - 1; |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1327 | // Remove trailing whitespace of the previous line if it was |
| 1328 | // touched. |
| 1329 | if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) { |
| 1330 | formatFirstToken(*Tok, PreviousLineLastToken, LevelIndent, |
| 1331 | TheLine.InPPDirective); |
| 1332 | } else { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1333 | Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1334 | } |
Daniel Jasper | 1fb8d88 | 2013-05-14 09:30:02 +0000 | [diff] [blame] | 1335 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1336 | if (static_cast<int>(LevelIndent) - Offset >= 0) |
| 1337 | LevelIndent -= Offset; |
| 1338 | if (Tok->isNot(tok::comment)) |
| 1339 | IndentForLevel[TheLine.Level] = LevelIndent; |
| 1340 | } else { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1341 | Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1342 | } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1343 | } |
| 1344 | // If we did not reformat this unwrapped line, the column at the end of |
| 1345 | // the last token is unchanged - thus, we can calculate the end of the |
| 1346 | // last token. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1347 | PreviousLineWasTouched = false; |
| 1348 | } |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1349 | PreviousLineLastToken = I->Last; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1350 | } |
| 1351 | return Whitespaces.generateReplacements(); |
| 1352 | } |
| 1353 | |
| 1354 | private: |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1355 | void deriveLocalStyle() { |
| 1356 | unsigned CountBoundToVariable = 0; |
| 1357 | unsigned CountBoundToType = 0; |
| 1358 | bool HasCpp03IncompatibleFormat = false; |
| 1359 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1360 | if (!AnnotatedLines[i].First->Next) |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1361 | continue; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1362 | FormatToken *Tok = AnnotatedLines[i].First->Next; |
| 1363 | while (Tok->Next) { |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1364 | if (Tok->Type == TT_PointerOrReference) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1365 | bool SpacesBefore = |
| 1366 | Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd(); |
| 1367 | bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() != |
| 1368 | Tok->Next->WhitespaceRange.getEnd(); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1369 | if (SpacesBefore && !SpacesAfter) |
| 1370 | ++CountBoundToVariable; |
| 1371 | else if (!SpacesBefore && SpacesAfter) |
| 1372 | ++CountBoundToType; |
| 1373 | } |
| 1374 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1375 | if (Tok->Type == TT_TemplateCloser && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1376 | Tok->Previous->Type == TT_TemplateCloser && |
| 1377 | Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd()) |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1378 | HasCpp03IncompatibleFormat = true; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1379 | Tok = Tok->Next; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | if (Style.DerivePointerBinding) { |
| 1383 | if (CountBoundToType > CountBoundToVariable) |
| 1384 | Style.PointerBindsToType = true; |
| 1385 | else if (CountBoundToType < CountBoundToVariable) |
| 1386 | Style.PointerBindsToType = false; |
| 1387 | } |
| 1388 | if (Style.Standard == FormatStyle::LS_Auto) { |
| 1389 | Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 |
| 1390 | : FormatStyle::LS_Cpp03; |
| 1391 | } |
| 1392 | } |
| 1393 | |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1394 | /// \brief Get the indent of \p Level from \p IndentForLevel. |
| 1395 | /// |
| 1396 | /// \p IndentForLevel must contain the indent for the level \c l |
| 1397 | /// at \p IndentForLevel[l], or a value < 0 if the indent for |
| 1398 | /// that level is unknown. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1399 | unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) { |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1400 | if (IndentForLevel[Level] != -1) |
| 1401 | return IndentForLevel[Level]; |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 1402 | if (Level == 0) |
| 1403 | return 0; |
Manuel Klimek | 07a64ec | 2013-05-13 08:42:42 +0000 | [diff] [blame] | 1404 | return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth; |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | /// \brief Get the offset of the line relatively to the level. |
| 1408 | /// |
| 1409 | /// For example, 'public:' labels in classes are offset by 1 or 2 |
| 1410 | /// characters to the left from their level. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1411 | int getIndentOffset(const FormatToken &RootToken) { |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1412 | if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier()) |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1413 | return Style.AccessModifierOffset; |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1417 | /// \brief Tries to merge lines into one. |
| 1418 | /// |
| 1419 | /// This will change \c Line and \c AnnotatedLine to contain the merged line, |
| 1420 | /// if possible; note that \c I will be incremented when lines are merged. |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1421 | void tryFitMultipleLinesInOne(unsigned Indent, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1422 | std::vector<AnnotatedLine>::iterator &I, |
| 1423 | std::vector<AnnotatedLine>::iterator E) { |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1424 | // We can never merge stuff if there are trailing line comments. |
| 1425 | if (I->Last->Type == TT_LineComment) |
| 1426 | return; |
| 1427 | |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 1428 | unsigned Limit = Style.ColumnLimit - Indent; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1429 | // If we already exceed the column limit, we set 'Limit' to 0. The different |
| 1430 | // tryMerge..() functions can then decide whether to still do merging. |
| 1431 | Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1432 | |
Daniel Jasper | 9c8c40e | 2013-01-21 14:18:28 +0000 | [diff] [blame] | 1433 | if (I + 1 == E || (I + 1)->Type == LT_Invalid) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1434 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1435 | |
Daniel Jasper | 5be59ba | 2013-05-15 14:09:55 +0000 | [diff] [blame] | 1436 | if (I->Last->is(tok::l_brace)) { |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1437 | tryMergeSimpleBlock(I, E, Limit); |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 1438 | } else if (Style.AllowShortIfStatementsOnASingleLine && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1439 | I->First->is(tok::kw_if)) { |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 1440 | tryMergeSimpleControlStatement(I, E, Limit); |
| 1441 | } else if (Style.AllowShortLoopsOnASingleLine && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1442 | I->First->isOneOf(tok::kw_for, tok::kw_while)) { |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 1443 | tryMergeSimpleControlStatement(I, E, Limit); |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1444 | } else if (I->InPPDirective && |
| 1445 | (I->First->HasUnescapedNewline || I->First->IsFirst)) { |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1446 | tryMergeSimplePPDirective(I, E, Limit); |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1447 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1450 | void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I, |
| 1451 | std::vector<AnnotatedLine>::iterator E, |
| 1452 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1453 | if (Limit == 0) |
| 1454 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1455 | AnnotatedLine &Line = *I; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1456 | if (!(I + 1)->InPPDirective || (I + 1)->First->HasUnescapedNewline) |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 1457 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1458 | if (I + 2 != E && (I + 2)->InPPDirective && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1459 | !(I + 2)->First->HasUnescapedNewline) |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1460 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1461 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1462 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1463 | join(Line, *(++I)); |
| 1464 | } |
| 1465 | |
Daniel Jasper | f11bbb9 | 2013-05-16 12:12:21 +0000 | [diff] [blame] | 1466 | void tryMergeSimpleControlStatement(std::vector<AnnotatedLine>::iterator &I, |
| 1467 | std::vector<AnnotatedLine>::iterator E, |
| 1468 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1469 | if (Limit == 0) |
| 1470 | return; |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 1471 | if ((I + 1)->InPPDirective != I->InPPDirective || |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1472 | ((I + 1)->InPPDirective && (I + 1)->First->HasUnescapedNewline)) |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 1473 | return; |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1474 | AnnotatedLine &Line = *I; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1475 | if (Line.Last->isNot(tok::r_paren)) |
| 1476 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1477 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1478 | return; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1479 | if ((I + 1)->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, |
| 1480 | tok::kw_while) || |
| 1481 | (I + 1)->First->Type == TT_LineComment) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1482 | return; |
| 1483 | // Only inline simple if's (no nested if or else). |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1484 | if (I + 2 != E && Line.First->is(tok::kw_if) && |
| 1485 | (I + 2)->First->is(tok::kw_else)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1486 | return; |
| 1487 | join(Line, *(++I)); |
| 1488 | } |
| 1489 | |
| 1490 | void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I, |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1491 | std::vector<AnnotatedLine>::iterator E, |
| 1492 | unsigned Limit) { |
Daniel Jasper | 5be59ba | 2013-05-15 14:09:55 +0000 | [diff] [blame] | 1493 | // No merging if the brace already is on the next line. |
| 1494 | if (Style.BreakBeforeBraces != FormatStyle::BS_Attach) |
| 1495 | return; |
| 1496 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1497 | // First, check that the current line allows merging. This is the case if |
| 1498 | // we're not in a control flow statement and the last token is an opening |
| 1499 | // brace. |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1500 | AnnotatedLine &Line = *I; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1501 | if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace, |
| 1502 | tok::kw_else, tok::kw_try, tok::kw_catch, |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 1503 | tok::kw_for, |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1504 | // This gets rid of all ObjC @ keywords and methods. |
| 1505 | tok::at, tok::minus, tok::plus)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1506 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1507 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1508 | FormatToken *Tok = (I + 1)->First; |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 1509 | if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore && |
| 1510 | (Tok->getNextNoneComment() == NULL || |
| 1511 | Tok->getNextNoneComment()->is(tok::semi))) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1512 | // We merge empty blocks even if the line exceeds the column limit. |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 1513 | Tok->SpacesRequiredBefore = 0; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1514 | Tok->CanBreakBefore = true; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1515 | join(Line, *(I + 1)); |
| 1516 | I += 1; |
Daniel Jasper | 8893b8a | 2013-05-31 14:56:20 +0000 | [diff] [blame] | 1517 | } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1518 | // Check that we still have three lines and they fit into the limit. |
| 1519 | if (I + 2 == E || (I + 2)->Type == LT_Invalid || |
| 1520 | !nextTwoLinesFitInto(I, Limit)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1521 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1522 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1523 | // Second, check that the next line does not contain any braces - if it |
| 1524 | // does, readability declines when putting it into a single line. |
| 1525 | if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore) |
| 1526 | return; |
| 1527 | do { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1528 | if (Tok->isOneOf(tok::l_brace, tok::r_brace)) |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1529 | return; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1530 | Tok = Tok->Next; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1531 | } while (Tok != NULL); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1532 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1533 | // Last, check that the third line contains a single closing brace. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1534 | Tok = (I + 2)->First; |
Daniel Jasper | 058f6f8 | 2013-05-16 10:17:39 +0000 | [diff] [blame] | 1535 | if (Tok->getNextNoneComment() != NULL || Tok->isNot(tok::r_brace) || |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1536 | Tok->MustBreakBefore) |
| 1537 | return; |
| 1538 | |
| 1539 | join(Line, *(I + 1)); |
| 1540 | join(Line, *(I + 2)); |
| 1541 | I += 2; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1542 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I, |
| 1546 | unsigned Limit) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1547 | return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <= |
| 1548 | Limit; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1551 | void join(AnnotatedLine &A, const AnnotatedLine &B) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1552 | assert(!A.Last->Next); |
| 1553 | assert(!B.First->Previous); |
| 1554 | A.Last->Next = B.First; |
| 1555 | B.First->Previous = A.Last; |
| 1556 | unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore; |
| 1557 | for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) { |
| 1558 | Tok->TotalLength += LengthA; |
| 1559 | A.Last = Tok; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1560 | } |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 1563 | bool touchesRanges(const CharSourceRange &Range) { |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1564 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 1565 | if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), |
| 1566 | Ranges[i].getBegin()) && |
| 1567 | !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 1568 | Range.getBegin())) |
| 1569 | return true; |
| 1570 | } |
| 1571 | return false; |
| 1572 | } |
| 1573 | |
| 1574 | bool touchesLine(const AnnotatedLine &TheLine) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1575 | const FormatToken *First = TheLine.First; |
| 1576 | const FormatToken *Last = TheLine.Last; |
Daniel Jasper | 84f5ddf | 2013-05-14 10:31:09 +0000 | [diff] [blame] | 1577 | CharSourceRange LineRange = CharSourceRange::getCharRange( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1578 | First->WhitespaceRange.getBegin().getLocWithOffset( |
| 1579 | First->LastNewlineOffset), |
Alexander Kornienko | 54e6c9d | 2013-06-07 17:45:07 +0000 | [diff] [blame] | 1580 | Last->Tok.getLocation().getLocWithOffset(Last->TokenText.size() - 1)); |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1581 | return touchesRanges(LineRange); |
| 1582 | } |
| 1583 | |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1584 | bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I, |
| 1585 | std::vector<AnnotatedLine>::iterator E) { |
| 1586 | for (; I != E; ++I) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1587 | if (I->First->HasUnescapedNewline) |
Daniel Jasper | 89b3a7f | 2013-05-10 13:00:49 +0000 | [diff] [blame] | 1588 | return false; |
| 1589 | if (touchesLine(*I)) |
| 1590 | return true; |
| 1591 | } |
| 1592 | return false; |
| 1593 | } |
| 1594 | |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1595 | bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1596 | const FormatToken *First = TheLine.First; |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1597 | CharSourceRange LineRange = CharSourceRange::getCharRange( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 1598 | First->WhitespaceRange.getBegin(), |
| 1599 | First->WhitespaceRange.getBegin().getLocWithOffset( |
| 1600 | First->LastNewlineOffset)); |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1601 | return touchesRanges(LineRange); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1605 | AnnotatedLines.push_back(AnnotatedLine(TheLine)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1608 | /// \brief Add a new line and the required indent before the first Token |
| 1609 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 1610 | /// Returns the indent level of the \c UnwrappedLine. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1611 | void formatFirstToken(const FormatToken &RootToken, |
| 1612 | const FormatToken *PreviousToken, unsigned Indent, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1613 | bool InPPDirective) { |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1614 | unsigned Newlines = |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1615 | std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); |
Daniel Jasper | 15f33f0 | 2013-06-03 16:16:41 +0000 | [diff] [blame] | 1616 | // Remove empty lines before "}" where applicable. |
| 1617 | if (RootToken.is(tok::r_brace) && |
| 1618 | (!RootToken.Next || |
| 1619 | (RootToken.Next->is(tok::semi) && !RootToken.Next->Next))) |
| 1620 | Newlines = std::min(Newlines, 1u); |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1621 | if (Newlines == 0 && !RootToken.IsFirst) |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1622 | Newlines = 1; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1623 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1624 | // Insert extra new line before access specifiers. |
| 1625 | if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) && |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1626 | RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1) |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 1627 | ++Newlines; |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1628 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 1629 | Whitespaces.replaceWhitespace( |
| 1630 | RootToken, Newlines, Indent, Indent, |
| 1631 | InPPDirective && !RootToken.HasUnescapedNewline); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1634 | FormatStyle Style; |
| 1635 | Lexer &Lex; |
| 1636 | SourceManager &SourceMgr; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1637 | WhitespaceManager Whitespaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1638 | std::vector<CharSourceRange> Ranges; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1639 | std::vector<AnnotatedLine> AnnotatedLines; |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 1640 | |
| 1641 | encoding::Encoding Encoding; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1642 | }; |
| 1643 | |
Craig Topper | 83f81d7 | 2013-06-30 22:29:28 +0000 | [diff] [blame^] | 1644 | } // end anonymous namespace |
| 1645 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1646 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 1647 | SourceManager &SourceMgr, |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 1648 | std::vector<CharSourceRange> Ranges) { |
| 1649 | Formatter formatter(Style, Lex, SourceMgr, Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1650 | return formatter.format(); |
| 1651 | } |
| 1652 | |
Daniel Jasper | 8a99945 | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1653 | tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, |
| 1654 | std::vector<tooling::Range> Ranges, |
| 1655 | StringRef FileName) { |
| 1656 | FileManager Files((FileSystemOptions())); |
| 1657 | DiagnosticsEngine Diagnostics( |
| 1658 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
| 1659 | new DiagnosticOptions); |
| 1660 | SourceManager SourceMgr(Diagnostics, Files); |
| 1661 | llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName); |
| 1662 | const clang::FileEntry *Entry = |
| 1663 | Files.getVirtualFile(FileName, Buf->getBufferSize(), 0); |
| 1664 | SourceMgr.overrideFileContents(Entry, Buf); |
| 1665 | FileID ID = |
| 1666 | SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1667 | Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr, |
| 1668 | getFormattingLangOpts(Style.Standard)); |
Daniel Jasper | 8a99945 | 2013-05-16 10:40:07 +0000 | [diff] [blame] | 1669 | SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID); |
| 1670 | std::vector<CharSourceRange> CharRanges; |
| 1671 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 1672 | SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset()); |
| 1673 | SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength()); |
| 1674 | CharRanges.push_back(CharSourceRange::getCharRange(Start, End)); |
| 1675 | } |
| 1676 | return reformat(Style, Lex, SourceMgr, CharRanges); |
| 1677 | } |
| 1678 | |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1679 | LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1680 | LangOptions LangOpts; |
| 1681 | LangOpts.CPlusPlus = 1; |
Alexander Kornienko | a1753f4 | 2013-06-28 12:51:24 +0000 | [diff] [blame] | 1682 | LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1; |
Daniel Jasper | b64eca0 | 2013-03-22 10:01:29 +0000 | [diff] [blame] | 1683 | LangOpts.LineComment = 1; |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1684 | LangOpts.Bool = 1; |
| 1685 | LangOpts.ObjC1 = 1; |
| 1686 | LangOpts.ObjC2 = 1; |
| 1687 | return LangOpts; |
| 1688 | } |
| 1689 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1690 | } // namespace format |
| 1691 | } // namespace clang |