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