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