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