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