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 | /// |
| 14 | /// This is EXPERIMENTAL code under heavy development. It is not in a state yet, |
| 15 | /// where it can be used to format real code. |
| 16 | /// |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "format-formatter" |
| 20 | |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "UnwrappedLineParser.h" |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Daniel Jasper | 675d2e3 | 2012-12-21 10:20:02 +0000 | [diff] [blame] | 23 | #include "clang/Basic/OperatorPrecedence.h" |
Chandler Carruth | b99083e | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 25 | #include "clang/Format/Format.h" |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 27 | #include "clang/Lex/Lexer.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 29 | #include <string> |
| 30 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 31 | // Uncomment to get debug output from tests: |
| 32 | // #define DEBUG_WITH_TYPE(T, X) do { X; } while(0) |
| 33 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 34 | namespace clang { |
| 35 | namespace format { |
| 36 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 37 | enum TokenType { |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 38 | TT_BinaryOperator, |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 39 | TT_BlockComment, |
| 40 | TT_CastRParen, |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 41 | TT_ConditionalExpr, |
| 42 | TT_CtorInitializerColon, |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 43 | TT_ImplicitStringLiteral, |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 44 | TT_LineComment, |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 45 | TT_ObjCBlockLParen, |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 46 | TT_ObjCDecl, |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 47 | TT_ObjCMethodSpecifier, |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 48 | TT_ObjCMethodExpr, |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 49 | TT_ObjCProperty, |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 50 | TT_OverloadedOperator, |
| 51 | TT_PointerOrReference, |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 52 | TT_PureVirtualSpecifier, |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 53 | TT_TemplateCloser, |
| 54 | TT_TemplateOpener, |
| 55 | TT_TrailingUnaryOperator, |
| 56 | TT_UnaryOperator, |
| 57 | TT_Unknown |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | enum LineType { |
| 61 | LT_Invalid, |
| 62 | LT_Other, |
Daniel Jasper | 3298327 | 2013-01-22 14:28:24 +0000 | [diff] [blame] | 63 | LT_BuilderTypeCall, |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 64 | LT_PreprocessorDirective, |
| 65 | LT_VirtualFunctionDecl, |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 66 | LT_ObjCDecl, // An @interface, @implementation, or @protocol line. |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 67 | LT_ObjCMethodDecl, |
| 68 | LT_ObjCProperty // An @property line. |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 71 | class AnnotatedToken { |
| 72 | public: |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 73 | explicit AnnotatedToken(const FormatToken &FormatTok) |
Manuel Klimek | 94fc6f1 | 2013-01-10 19:17:33 +0000 | [diff] [blame] | 74 | : FormatTok(FormatTok), Type(TT_Unknown), SpaceRequiredBefore(false), |
| 75 | CanBreakBefore(false), MustBreakBefore(false), |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 76 | ClosesTemplateDeclaration(false), MatchingParen(NULL), Parent(NULL) {} |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 77 | |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 78 | bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); } |
| 79 | bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); } |
| 80 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 81 | bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const { |
| 82 | return FormatTok.Tok.isObjCAtKeyword(Kind); |
| 83 | } |
| 84 | |
| 85 | FormatToken FormatTok; |
| 86 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 87 | TokenType Type; |
| 88 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 89 | bool SpaceRequiredBefore; |
| 90 | bool CanBreakBefore; |
| 91 | bool MustBreakBefore; |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 92 | |
| 93 | bool ClosesTemplateDeclaration; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 94 | |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 95 | AnnotatedToken *MatchingParen; |
| 96 | |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 97 | /// \brief The total length of the line up to and including this token. |
| 98 | unsigned TotalLength; |
| 99 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 100 | std::vector<AnnotatedToken> Children; |
| 101 | AnnotatedToken *Parent; |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 102 | |
| 103 | const AnnotatedToken *getPreviousNoneComment() const { |
| 104 | AnnotatedToken *Tok = Parent; |
| 105 | while (Tok != NULL && Tok->is(tok::comment)) |
| 106 | Tok = Tok->Parent; |
| 107 | return Tok; |
| 108 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 111 | class AnnotatedLine { |
| 112 | public: |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 113 | AnnotatedLine(const UnwrappedLine &Line) |
| 114 | : First(Line.Tokens.front()), Level(Line.Level), |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 115 | InPPDirective(Line.InPPDirective), |
| 116 | MustBeDeclaration(Line.MustBeDeclaration) { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 117 | assert(!Line.Tokens.empty()); |
| 118 | AnnotatedToken *Current = &First; |
| 119 | for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(), |
| 120 | E = Line.Tokens.end(); |
| 121 | I != E; ++I) { |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 122 | Current->Children.push_back(AnnotatedToken(*I)); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 123 | Current->Children[0].Parent = Current; |
| 124 | Current = &Current->Children[0]; |
| 125 | } |
| 126 | Last = Current; |
| 127 | } |
| 128 | AnnotatedLine(const AnnotatedLine &Other) |
| 129 | : First(Other.First), Type(Other.Type), Level(Other.Level), |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 130 | InPPDirective(Other.InPPDirective), |
| 131 | MustBeDeclaration(Other.MustBeDeclaration) { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 132 | Last = &First; |
| 133 | while (!Last->Children.empty()) { |
| 134 | Last->Children[0].Parent = Last; |
| 135 | Last = &Last->Children[0]; |
| 136 | } |
| 137 | } |
| 138 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 139 | AnnotatedToken First; |
| 140 | AnnotatedToken *Last; |
| 141 | |
| 142 | LineType Type; |
| 143 | unsigned Level; |
| 144 | bool InPPDirective; |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 145 | bool MustBeDeclaration; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 148 | static prec::Level getPrecedence(const AnnotatedToken &Tok) { |
| 149 | return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 152 | FormatStyle getLLVMStyle() { |
| 153 | FormatStyle LLVMStyle; |
| 154 | LLVMStyle.ColumnLimit = 80; |
| 155 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
| 156 | LLVMStyle.PointerAndReferenceBindToType = false; |
| 157 | LLVMStyle.AccessModifierOffset = -2; |
| 158 | LLVMStyle.SplitTemplateClosingGreater = true; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 159 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 160 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 161 | LLVMStyle.BinPackParameters = true; |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 162 | LLVMStyle.AllowAllParametersOnNextLine = true; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 163 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 164 | LLVMStyle.AllowShortIfStatementsOnASingleLine = false; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 165 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 166 | return LLVMStyle; |
| 167 | } |
| 168 | |
| 169 | FormatStyle getGoogleStyle() { |
| 170 | FormatStyle GoogleStyle; |
| 171 | GoogleStyle.ColumnLimit = 80; |
| 172 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
| 173 | GoogleStyle.PointerAndReferenceBindToType = true; |
| 174 | GoogleStyle.AccessModifierOffset = -1; |
| 175 | GoogleStyle.SplitTemplateClosingGreater = false; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 176 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 177 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 178 | GoogleStyle.BinPackParameters = false; |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 179 | GoogleStyle.AllowAllParametersOnNextLine = true; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 180 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 181 | GoogleStyle.AllowShortIfStatementsOnASingleLine = false; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 182 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 183 | return GoogleStyle; |
| 184 | } |
| 185 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 186 | FormatStyle getChromiumStyle() { |
| 187 | FormatStyle ChromiumStyle = getGoogleStyle(); |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 188 | ChromiumStyle.AllowAllParametersOnNextLine = false; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 189 | return ChromiumStyle; |
| 190 | } |
| 191 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 192 | struct OptimizationParameters { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 193 | unsigned PenaltyIndentLevel; |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 194 | unsigned PenaltyLevelDecrease; |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 195 | unsigned PenaltyExcessCharacter; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 198 | /// \brief Manages the whitespaces around tokens and their replacements. |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 199 | /// |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 200 | /// This includes special handling for certain constructs, e.g. the alignment of |
| 201 | /// trailing line comments. |
| 202 | class WhitespaceManager { |
| 203 | public: |
| 204 | WhitespaceManager(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {} |
| 205 | |
| 206 | /// \brief Replaces the whitespace in front of \p Tok. Only call once for |
| 207 | /// each \c AnnotatedToken. |
| 208 | void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines, |
| 209 | unsigned Spaces, unsigned WhitespaceStartColumn, |
| 210 | const FormatStyle &Style) { |
Daniel Jasper | 821627e | 2013-01-21 22:49:20 +0000 | [diff] [blame] | 211 | // 2+ newlines mean an empty line separating logic scopes. |
| 212 | if (NewLines >= 2) |
| 213 | alignComments(); |
| 214 | |
| 215 | // Align line comments if they are trailing or if they continue other |
| 216 | // trailing comments. |
| 217 | if (Tok.Type == TT_LineComment && |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 218 | (Tok.Parent != NULL || !Comments.empty())) { |
| 219 | if (Style.ColumnLimit >= |
| 220 | Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) { |
| 221 | Comments.push_back(StoredComment()); |
| 222 | Comments.back().Tok = Tok.FormatTok; |
| 223 | Comments.back().Spaces = Spaces; |
| 224 | Comments.back().NewLines = NewLines; |
| 225 | Comments.back().MinColumn = WhitespaceStartColumn + Spaces; |
| 226 | Comments.back().MaxColumn = Style.ColumnLimit - |
| 227 | Spaces - Tok.FormatTok.TokenLength; |
| 228 | return; |
| 229 | } |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 230 | } |
Daniel Jasper | 821627e | 2013-01-21 22:49:20 +0000 | [diff] [blame] | 231 | |
| 232 | // If this line does not have a trailing comment, align the stored comments. |
| 233 | if (Tok.Children.empty() && Tok.Type != TT_LineComment) |
| 234 | alignComments(); |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 235 | storeReplacement(Tok.FormatTok, |
| 236 | std::string(NewLines, '\n') + std::string(Spaces, ' ')); |
| 237 | } |
| 238 | |
| 239 | /// \brief Like \c replaceWhitespace, but additionally adds right-aligned |
| 240 | /// backslashes to escape newlines inside a preprocessor directive. |
| 241 | /// |
| 242 | /// This function and \c replaceWhitespace have the same behavior if |
| 243 | /// \c Newlines == 0. |
| 244 | void replacePPWhitespace(const AnnotatedToken &Tok, unsigned NewLines, |
| 245 | unsigned Spaces, unsigned WhitespaceStartColumn, |
| 246 | const FormatStyle &Style) { |
| 247 | std::string NewLineText; |
| 248 | if (NewLines > 0) { |
| 249 | unsigned Offset = std::min<int>(Style.ColumnLimit - 1, |
| 250 | WhitespaceStartColumn); |
| 251 | for (unsigned i = 0; i < NewLines; ++i) { |
| 252 | NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' '); |
| 253 | NewLineText += "\\\n"; |
| 254 | Offset = 0; |
| 255 | } |
| 256 | } |
| 257 | storeReplacement(Tok.FormatTok, NewLineText + std::string(Spaces, ' ')); |
| 258 | } |
| 259 | |
| 260 | /// \brief Returns all the \c Replacements created during formatting. |
| 261 | const tooling::Replacements &generateReplacements() { |
| 262 | alignComments(); |
| 263 | return Replaces; |
| 264 | } |
| 265 | |
| 266 | private: |
| 267 | /// \brief Structure to store a comment for later layout and alignment. |
| 268 | struct StoredComment { |
| 269 | FormatToken Tok; |
| 270 | unsigned MinColumn; |
| 271 | unsigned MaxColumn; |
| 272 | unsigned NewLines; |
| 273 | unsigned Spaces; |
| 274 | }; |
| 275 | SmallVector<StoredComment, 16> Comments; |
| 276 | typedef SmallVector<StoredComment, 16>::iterator comment_iterator; |
| 277 | |
| 278 | /// \brief Try to align all stashed comments. |
| 279 | void alignComments() { |
| 280 | unsigned MinColumn = 0; |
| 281 | unsigned MaxColumn = UINT_MAX; |
| 282 | comment_iterator Start = Comments.begin(); |
| 283 | for (comment_iterator I = Comments.begin(), E = Comments.end(); I != E; |
| 284 | ++I) { |
| 285 | if (I->MinColumn > MaxColumn || I->MaxColumn < MinColumn) { |
| 286 | alignComments(Start, I, MinColumn); |
| 287 | MinColumn = I->MinColumn; |
| 288 | MaxColumn = I->MaxColumn; |
| 289 | Start = I; |
| 290 | } else { |
| 291 | MinColumn = std::max(MinColumn, I->MinColumn); |
| 292 | MaxColumn = std::min(MaxColumn, I->MaxColumn); |
| 293 | } |
| 294 | } |
| 295 | alignComments(Start, Comments.end(), MinColumn); |
| 296 | Comments.clear(); |
| 297 | } |
| 298 | |
| 299 | /// \brief Put all the comments between \p I and \p E into \p Column. |
| 300 | void alignComments(comment_iterator I, comment_iterator E, unsigned Column) { |
| 301 | while (I != E) { |
| 302 | unsigned Spaces = I->Spaces + Column - I->MinColumn; |
| 303 | storeReplacement(I->Tok, std::string(I->NewLines, '\n') + |
| 304 | std::string(Spaces, ' ')); |
| 305 | ++I; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 308 | |
| 309 | /// \brief Stores \p Text as the replacement for the whitespace in front of |
| 310 | /// \p Tok. |
| 311 | void storeReplacement(const FormatToken &Tok, const std::string Text) { |
| 312 | Replaces.insert(tooling::Replacement(SourceMgr, Tok.WhiteSpaceStart, |
| 313 | Tok.WhiteSpaceLength, Text)); |
| 314 | } |
| 315 | |
| 316 | SourceManager &SourceMgr; |
| 317 | tooling::Replacements Replaces; |
| 318 | }; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 319 | |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 320 | /// \brief Returns if a token is an Objective-C selector name. |
| 321 | /// |
Nico Weber | ea86563 | 2013-01-12 22:51:13 +0000 | [diff] [blame] | 322 | /// For example, "bar" is a selector name in [foo bar:(4 + 5)]. |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 323 | static bool isObjCSelectorName(const AnnotatedToken &Tok) { |
| 324 | return Tok.is(tok::identifier) && !Tok.Children.empty() && |
| 325 | Tok.Children[0].is(tok::colon) && |
| 326 | Tok.Children[0].Type == TT_ObjCMethodExpr; |
| 327 | } |
| 328 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 329 | class UnwrappedLineFormatter { |
| 330 | public: |
Manuel Klimek | 94fc6f1 | 2013-01-10 19:17:33 +0000 | [diff] [blame] | 331 | UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 332 | const AnnotatedLine &Line, unsigned FirstIndent, |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 333 | const AnnotatedToken &RootToken, |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 334 | WhitespaceManager &Whitespaces, bool StructuralError) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 335 | : Style(Style), SourceMgr(SourceMgr), Line(Line), |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 336 | FirstIndent(FirstIndent), RootToken(RootToken), |
| 337 | Whitespaces(Whitespaces) { |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 338 | Parameters.PenaltyIndentLevel = 20; |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 339 | Parameters.PenaltyLevelDecrease = 30; |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 340 | Parameters.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 343 | /// \brief Formats an \c UnwrappedLine. |
| 344 | /// |
| 345 | /// \returns The column after the last token in the last line of the |
| 346 | /// \c UnwrappedLine. |
| 347 | unsigned format() { |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 348 | // Initialize state dependent on indent. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 349 | LineState State; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 350 | State.Column = FirstIndent; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 351 | State.NextToken = &RootToken; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 352 | State.Stack.push_back(ParenState(FirstIndent + 4, FirstIndent)); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 353 | State.ForLoopVariablePos = 0; |
| 354 | State.LineContainsContinuedForLoopSection = false; |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 355 | State.StartOfLineLevel = 1; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 356 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 357 | DEBUG({ |
| 358 | DebugTokenState(*State.NextToken); |
| 359 | }); |
| 360 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 361 | // The first token has already been indented and thus consumed. |
| 362 | moveStateToNextToken(State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 363 | |
| 364 | // Start iterating at 1 as we have correctly formatted of Token #0 above. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 365 | while (State.NextToken != NULL) { |
Daniel Jasper | 7d1185d | 2013-01-18 09:19:33 +0000 | [diff] [blame] | 366 | if (State.NextToken->Type == TT_ImplicitStringLiteral) { |
| 367 | // Calculating the column is important for aligning trailing comments. |
| 368 | // FIXME: This does not seem to happen in conjunction with escaped |
| 369 | // newlines. If it does, fix! |
| 370 | State.Column += State.NextToken->FormatTok.WhiteSpaceLength + |
| 371 | State.NextToken->FormatTok.TokenLength; |
| 372 | State.NextToken = State.NextToken->Children.empty() ? NULL : |
| 373 | &State.NextToken->Children[0]; |
| 374 | } else if (Line.Last->TotalLength <= getColumnLimit() - FirstIndent) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 375 | addTokenToState(false, false, State); |
| 376 | } else { |
| 377 | unsigned NoBreak = calcPenalty(State, false, UINT_MAX); |
| 378 | unsigned Break = calcPenalty(State, true, NoBreak); |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 379 | DEBUG({ |
| 380 | if (Break < NoBreak) |
| 381 | llvm::errs() << "\n"; |
| 382 | else |
| 383 | llvm::errs() << " "; |
| 384 | llvm::errs() << "<"; |
| 385 | DebugPenalty(Break, Break < NoBreak); |
| 386 | llvm::errs() << "/"; |
| 387 | DebugPenalty(NoBreak, !(Break < NoBreak)); |
| 388 | llvm::errs() << "> "; |
| 389 | DebugTokenState(*State.NextToken); |
| 390 | }); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 391 | addTokenToState(Break < NoBreak, false, State); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 392 | if (State.NextToken != NULL && |
| 393 | State.NextToken->Parent->Type == TT_CtorInitializerColon) { |
| 394 | if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine && |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 395 | Line.Last->TotalLength > getColumnLimit() - State.Column - 1) |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 396 | State.Stack.back().BreakAfterComma = true; |
| 397 | } |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 398 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 399 | } |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 400 | DEBUG(llvm::errs() << "\n"); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 401 | return State.Column; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | private: |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 405 | void DebugTokenState(const AnnotatedToken &AnnotatedTok) { |
| 406 | const Token &Tok = AnnotatedTok.FormatTok.Tok; |
| 407 | llvm::errs() |
| 408 | << StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
| 409 | Tok.getLength()); |
| 410 | llvm::errs(); |
| 411 | } |
| 412 | |
| 413 | void DebugPenalty(unsigned Penalty, bool Winner) { |
| 414 | llvm::errs().changeColor(Winner ? raw_ostream::GREEN : raw_ostream::RED); |
| 415 | if (Penalty == UINT_MAX) |
| 416 | llvm::errs() << "MAX"; |
| 417 | else |
| 418 | llvm::errs() << Penalty; |
| 419 | llvm::errs().resetColor(); |
| 420 | } |
| 421 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 422 | struct ParenState { |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 423 | ParenState(unsigned Indent, unsigned LastSpace) |
| 424 | : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0), |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 425 | BreakBeforeClosingBrace(false), BreakAfterComma(false), |
| 426 | HasMultiParameterLine(false) {} |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 427 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 428 | /// \brief The position to which a specific parenthesis level needs to be |
| 429 | /// indented. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 430 | unsigned Indent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 431 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 432 | /// \brief The position of the last space on each level. |
| 433 | /// |
| 434 | /// Used e.g. to break like: |
| 435 | /// functionCall(Parameter, otherCall( |
| 436 | /// OtherParameter)); |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 437 | unsigned LastSpace; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 438 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 439 | /// \brief The position the first "<<" operator encountered on each level. |
| 440 | /// |
| 441 | /// Used to align "<<" operators. 0 if no such operator has been encountered |
| 442 | /// on a level. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 443 | unsigned FirstLessLess; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 444 | |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 445 | /// \brief Whether a newline needs to be inserted before the block's closing |
| 446 | /// brace. |
| 447 | /// |
| 448 | /// We only want to insert a newline before the closing brace if there also |
| 449 | /// was a newline after the beginning left brace. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 450 | bool BreakBeforeClosingBrace; |
| 451 | |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 452 | bool BreakAfterComma; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 453 | bool HasMultiParameterLine; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 454 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 455 | bool operator<(const ParenState &Other) const { |
| 456 | if (Indent != Other.Indent) |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 457 | return Indent < Other.Indent; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 458 | if (LastSpace != Other.LastSpace) |
| 459 | return LastSpace < Other.LastSpace; |
| 460 | if (FirstLessLess != Other.FirstLessLess) |
| 461 | return FirstLessLess < Other.FirstLessLess; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 462 | if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace) |
| 463 | return BreakBeforeClosingBrace; |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 464 | if (BreakAfterComma != Other.BreakAfterComma) |
| 465 | return BreakAfterComma; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 466 | if (HasMultiParameterLine != Other.HasMultiParameterLine) |
| 467 | return HasMultiParameterLine; |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 468 | return false; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 469 | } |
| 470 | }; |
| 471 | |
| 472 | /// \brief The current state when indenting a unwrapped line. |
| 473 | /// |
| 474 | /// As the indenting tries different combinations this is copied by value. |
| 475 | struct LineState { |
| 476 | /// \brief The number of used columns in the current line. |
| 477 | unsigned Column; |
| 478 | |
| 479 | /// \brief The token that needs to be next formatted. |
| 480 | const AnnotatedToken *NextToken; |
| 481 | |
| 482 | /// \brief The parenthesis level of the first token on the current line. |
| 483 | unsigned StartOfLineLevel; |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 484 | |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 485 | /// \brief The column of the first variable in a for-loop declaration. |
| 486 | /// |
| 487 | /// Used to align the second variable if necessary. |
| 488 | unsigned ForLoopVariablePos; |
| 489 | |
| 490 | /// \brief \c true if this line contains a continued for-loop section. |
| 491 | bool LineContainsContinuedForLoopSection; |
| 492 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 493 | /// \brief A stack keeping track of properties applying to parenthesis |
| 494 | /// levels. |
| 495 | std::vector<ParenState> Stack; |
| 496 | |
| 497 | /// \brief Comparison operator to be able to used \c LineState in \c map. |
| 498 | bool operator<(const LineState &Other) const { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 499 | if (Other.NextToken != NextToken) |
| 500 | return Other.NextToken > NextToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 501 | if (Other.Column != Column) |
| 502 | return Other.Column > Column; |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 503 | if (Other.StartOfLineLevel != StartOfLineLevel) |
| 504 | return Other.StartOfLineLevel > StartOfLineLevel; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 505 | if (Other.ForLoopVariablePos != ForLoopVariablePos) |
| 506 | return Other.ForLoopVariablePos < ForLoopVariablePos; |
| 507 | if (Other.LineContainsContinuedForLoopSection != |
| 508 | LineContainsContinuedForLoopSection) |
| 509 | return LineContainsContinuedForLoopSection; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 510 | return Other.Stack < Stack; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 511 | } |
| 512 | }; |
| 513 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 514 | /// \brief Appends the next token to \p State and updates information |
| 515 | /// necessary for indentation. |
| 516 | /// |
| 517 | /// Puts the token on the current line if \p Newline is \c true and adds a |
| 518 | /// line break and necessary indentation otherwise. |
| 519 | /// |
| 520 | /// If \p DryRun is \c false, also creates and stores the required |
| 521 | /// \c Replacement. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 522 | void addTokenToState(bool Newline, bool DryRun, LineState &State) { |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 523 | const AnnotatedToken &Current = *State.NextToken; |
| 524 | const AnnotatedToken &Previous = *State.NextToken->Parent; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 525 | assert(State.Stack.size()); |
| 526 | unsigned ParenLevel = State.Stack.size() - 1; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 527 | |
| 528 | if (Newline) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 529 | unsigned WhitespaceStartColumn = State.Column; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 530 | if (Current.is(tok::r_brace)) { |
| 531 | State.Column = Line.Level * 2; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 532 | } else if (Current.is(tok::string_literal) && |
| 533 | Previous.is(tok::string_literal)) { |
| 534 | State.Column = State.Column - Previous.FormatTok.TokenLength; |
| 535 | } else if (Current.is(tok::lessless) && |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 536 | State.Stack[ParenLevel].FirstLessLess != 0) { |
| 537 | State.Column = State.Stack[ParenLevel].FirstLessLess; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 538 | } else if (ParenLevel != 0 && |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 539 | (Previous.is(tok::equal) || Current.is(tok::arrow) || |
| 540 | Current.is(tok::period) || Previous.is(tok::question) || |
| 541 | Previous.Type == TT_ConditionalExpr)) { |
| 542 | // Indent and extra 4 spaces after if we know the current expression is |
| 543 | // continued. Don't do that on the top level, as we already indent 4 |
| 544 | // there. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 545 | State.Column = State.Stack[ParenLevel].Indent + 4; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 546 | } else if (RootToken.is(tok::kw_for) && Previous.is(tok::comma)) { |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 547 | State.Column = State.ForLoopVariablePos; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 548 | } else if (State.NextToken->Parent->ClosesTemplateDeclaration) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 549 | State.Column = State.Stack[ParenLevel].Indent - 4; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 550 | } else { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 551 | State.Column = State.Stack[ParenLevel].Indent; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 554 | // A line starting with a closing brace is assumed to be correct for the |
| 555 | // same level as before the opening brace. |
| 556 | State.StartOfLineLevel = ParenLevel + (Current.is(tok::r_brace) ? 0 : 1); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 557 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 558 | if (RootToken.is(tok::kw_for)) |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 559 | State.LineContainsContinuedForLoopSection = Previous.isNot(tok::semi); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 560 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 561 | if (!DryRun) { |
| 562 | if (!Line.InPPDirective) |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 563 | Whitespaces.replaceWhitespace(Current, 1, State.Column, |
| 564 | WhitespaceStartColumn, Style); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 565 | else |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 566 | Whitespaces.replacePPWhitespace(Current, 1, State.Column, |
| 567 | WhitespaceStartColumn, Style); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 568 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 569 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 570 | State.Stack[ParenLevel].LastSpace = State.Column; |
Nico Weber | f681fa8 | 2013-01-12 07:05:25 +0000 | [diff] [blame] | 571 | if (Current.is(tok::colon) && State.NextToken->Type != TT_ConditionalExpr) |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 572 | State.Stack[ParenLevel].Indent += 2; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 573 | } else { |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 574 | if (Current.is(tok::equal) && RootToken.is(tok::kw_for)) |
| 575 | State.ForLoopVariablePos = State.Column - |
| 576 | Previous.FormatTok.TokenLength; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 577 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 578 | unsigned Spaces = State.NextToken->SpaceRequiredBefore ? 1 : 0; |
| 579 | if (State.NextToken->Type == TT_LineComment) |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 580 | Spaces = Style.SpacesBeforeTrailingComments; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 581 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 582 | if (!DryRun) |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 583 | Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column, Style); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 584 | |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 585 | // FIXME: Do we need to do this for assignments nested in other |
| 586 | // expressions? |
| 587 | if (RootToken.isNot(tok::kw_for) && ParenLevel == 0 && |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 588 | (getPrecedence(Previous) == prec::Assignment || |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 589 | Previous.is(tok::kw_return))) |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 590 | State.Stack[ParenLevel].Indent = State.Column + Spaces; |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 591 | if (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) || |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 592 | State.NextToken->Parent->Type == TT_TemplateOpener) |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 593 | State.Stack[ParenLevel].Indent = State.Column + Spaces; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 594 | if (Current.getPreviousNoneComment() != NULL && |
| 595 | Current.getPreviousNoneComment()->is(tok::comma) && |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 596 | Current.isNot(tok::comment)) |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 597 | State.Stack[ParenLevel].HasMultiParameterLine = true; |
| 598 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 599 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 600 | // Top-level spaces that are not part of assignments are exempt as that |
| 601 | // mostly leads to better results. |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 602 | State.Column += Spaces; |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 603 | if (Spaces > 0 && |
| 604 | (ParenLevel != 0 || getPrecedence(Previous) == prec::Assignment)) |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 605 | State.Stack[ParenLevel].LastSpace = State.Column; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 606 | } |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 607 | |
| 608 | // If we break after an {, we should also break before the corresponding }. |
| 609 | if (Newline && Previous.is(tok::l_brace)) |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 610 | State.Stack.back().BreakBeforeClosingBrace = true; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 611 | |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 612 | if (!Style.BinPackParameters && Newline) { |
| 613 | // If we are breaking after '(', '{', '<', this is not bin packing unless |
| 614 | // AllowAllParametersOnNextLine is false. |
| 615 | if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace) && |
| 616 | Previous.Type != TT_TemplateOpener) || |
| 617 | !Style.AllowAllParametersOnNextLine) |
| 618 | State.Stack.back().BreakAfterComma = true; |
| 619 | |
| 620 | // Any break on this level means that the parent level has been broken |
| 621 | // and we need to avoid bin packing there. |
| 622 | for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { |
| 623 | State.Stack[i].BreakAfterComma = true; |
| 624 | } |
| 625 | } |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 626 | |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 627 | moveStateToNextToken(State); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 628 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 629 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 630 | /// \brief Mark the next token as consumed in \p State and modify its stacks |
| 631 | /// accordingly. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 632 | void moveStateToNextToken(LineState &State) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 633 | const AnnotatedToken &Current = *State.NextToken; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 634 | assert(State.Stack.size()); |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 635 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 636 | if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0) |
| 637 | State.Stack.back().FirstLessLess = State.Column; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 638 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 639 | // 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] | 640 | // prepare for the following tokens. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 641 | if (Current.is(tok::l_paren) || Current.is(tok::l_square) || |
| 642 | Current.is(tok::l_brace) || |
| 643 | State.NextToken->Type == TT_TemplateOpener) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 644 | unsigned NewIndent; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 645 | if (Current.is(tok::l_brace)) { |
| 646 | // FIXME: This does not work with nested static initializers. |
| 647 | // Implement a better handling for static initializers and similar |
| 648 | // constructs. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 649 | NewIndent = Line.Level * 2 + 2; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 650 | } else { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 651 | NewIndent = 4 + State.Stack.back().LastSpace; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 652 | } |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 653 | State.Stack.push_back( |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 654 | ParenState(NewIndent, State.Stack.back().LastSpace)); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 657 | // 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] | 658 | // stacks. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 659 | if (Current.is(tok::r_paren) || Current.is(tok::r_square) || |
| 660 | (Current.is(tok::r_brace) && State.NextToken != &RootToken) || |
| 661 | State.NextToken->Type == TT_TemplateCloser) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 662 | State.Stack.pop_back(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 663 | } |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 664 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 665 | if (State.NextToken->Children.empty()) |
| 666 | State.NextToken = NULL; |
| 667 | else |
| 668 | State.NextToken = &State.NextToken->Children[0]; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 669 | |
| 670 | State.Column += Current.FormatTok.TokenLength; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Nico Weber | decf7bc | 2013-01-07 15:15:29 +0000 | [diff] [blame] | 673 | /// \brief Calculate the penalty for splitting after the token at \p Index. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 674 | unsigned splitPenalty(const AnnotatedToken &Tok) { |
| 675 | const AnnotatedToken &Left = Tok; |
| 676 | const AnnotatedToken &Right = Tok.Children[0]; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 677 | |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 678 | if (Left.is(tok::l_brace) && Right.isNot(tok::l_brace)) |
| 679 | return 50; |
| 680 | if (Left.is(tok::equal) && Right.is(tok::l_brace)) |
| 681 | return 150; |
| 682 | |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 683 | // In for-loops, prefer breaking at ',' and ';'. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 684 | if (RootToken.is(tok::kw_for) && |
| 685 | (Left.isNot(tok::comma) && Left.isNot(tok::semi))) |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 686 | return 20; |
| 687 | |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 688 | if (Left.is(tok::semi) || Left.is(tok::comma)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 689 | return 0; |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 690 | |
| 691 | // In Objective-C method expressions, prefer breaking before "param:" over |
| 692 | // breaking after it. |
| 693 | if (isObjCSelectorName(Right)) |
| 694 | return 0; |
| 695 | if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr) |
| 696 | return 20; |
| 697 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 698 | if (Left.is(tok::l_paren)) |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 699 | return 20; |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 700 | |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 701 | if (Left.is(tok::question) || Left.Type == TT_ConditionalExpr) |
| 702 | return prec::Assignment; |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 703 | prec::Level Level = getPrecedence(Left); |
| 704 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 705 | if (Level != prec::Unknown) |
| 706 | return Level; |
| 707 | |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 708 | if (Right.is(tok::arrow) || Right.is(tok::period)) { |
Daniel Jasper | 3298327 | 2013-01-22 14:28:24 +0000 | [diff] [blame] | 709 | if (Left.is(tok::r_paren) && Line.Type == LT_BuilderTypeCall) |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 710 | return 15; // Should be smaller than breaking at a nested comma. |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 711 | return 150; |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 712 | } |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 713 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 714 | return 3; |
| 715 | } |
| 716 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 717 | unsigned getColumnLimit() { |
| 718 | return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0); |
| 719 | } |
| 720 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 721 | /// \brief Calculate the number of lines needed to format the remaining part |
| 722 | /// of the unwrapped line. |
| 723 | /// |
| 724 | /// Assumes the formatting so far has led to |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 725 | /// the \c LineSta \p State. If \p NewLine is set, a new line will be |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 726 | /// added after the previous token. |
| 727 | /// |
| 728 | /// \param StopAt is used for optimization. If we can determine that we'll |
| 729 | /// definitely need at least \p StopAt additional lines, we already know of a |
| 730 | /// better solution. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 731 | unsigned calcPenalty(LineState State, bool NewLine, unsigned StopAt) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 732 | // We are at the end of the unwrapped line, so we don't need any more lines. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 733 | if (State.NextToken == NULL) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 734 | return 0; |
| 735 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 736 | if (!NewLine && State.NextToken->MustBreakBefore) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 737 | return UINT_MAX; |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 738 | if (NewLine && !State.NextToken->CanBreakBefore && |
| 739 | !(State.NextToken->is(tok::r_brace) && |
| 740 | State.Stack.back().BreakBeforeClosingBrace)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 741 | return UINT_MAX; |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 742 | if (!NewLine && State.NextToken->is(tok::r_brace) && |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 743 | State.Stack.back().BreakBeforeClosingBrace) |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 744 | return UINT_MAX; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 745 | if (!NewLine && State.NextToken->Parent->is(tok::semi) && |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 746 | State.LineContainsContinuedForLoopSection) |
| 747 | return UINT_MAX; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 748 | if (!NewLine && State.NextToken->Parent->is(tok::comma) && |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 749 | State.NextToken->isNot(tok::comment) && |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 750 | State.Stack.back().BreakAfterComma) |
| 751 | return UINT_MAX; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 752 | // Trying to insert a parameter on a new line if there are already more than |
| 753 | // one parameter on the current line is bin packing. |
| 754 | if (NewLine && State.NextToken->Parent->is(tok::comma) && |
| 755 | State.Stack.back().HasMultiParameterLine && !Style.BinPackParameters) |
| 756 | return UINT_MAX; |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 757 | if (!NewLine && (State.NextToken->Type == TT_CtorInitializerColon || |
| 758 | (State.NextToken->Parent->ClosesTemplateDeclaration && |
| 759 | State.Stack.size() == 1))) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 760 | return UINT_MAX; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 761 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 762 | unsigned CurrentPenalty = 0; |
| 763 | if (NewLine) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 764 | CurrentPenalty += Parameters.PenaltyIndentLevel * State.Stack.size() + |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 765 | splitPenalty(*State.NextToken->Parent); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 766 | } else { |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 767 | if (State.Stack.size() < State.StartOfLineLevel && |
| 768 | State.NextToken->is(tok::identifier)) |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 769 | CurrentPenalty += Parameters.PenaltyLevelDecrease * |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 770 | (State.StartOfLineLevel - State.Stack.size()); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 773 | addTokenToState(NewLine, true, State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 774 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 775 | // Exceeding column limit is bad, assign penalty. |
| 776 | if (State.Column > getColumnLimit()) { |
| 777 | unsigned ExcessCharacters = State.Column - getColumnLimit(); |
| 778 | CurrentPenalty += Parameters.PenaltyExcessCharacter * ExcessCharacters; |
| 779 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 780 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 781 | if (StopAt <= CurrentPenalty) |
| 782 | return UINT_MAX; |
| 783 | StopAt -= CurrentPenalty; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 784 | StateMap::iterator I = Memory.find(State); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 785 | if (I != Memory.end()) { |
| 786 | // If this state has already been examined, we can safely return the |
| 787 | // previous result if we |
| 788 | // - have not hit the optimatization (and thus returned UINT_MAX) OR |
| 789 | // - are now computing for a smaller or equal StopAt. |
| 790 | unsigned SavedResult = I->second.first; |
| 791 | unsigned SavedStopAt = I->second.second; |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 792 | if (SavedResult != UINT_MAX) |
| 793 | return SavedResult + CurrentPenalty; |
| 794 | else if (StopAt <= SavedStopAt) |
| 795 | return UINT_MAX; |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 796 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 797 | |
| 798 | unsigned NoBreak = calcPenalty(State, false, StopAt); |
| 799 | unsigned WithBreak = calcPenalty(State, true, std::min(StopAt, NoBreak)); |
| 800 | unsigned Result = std::min(NoBreak, WithBreak); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 801 | |
| 802 | // We have to store 'Result' without adding 'CurrentPenalty' as the latter |
| 803 | // can depend on 'NewLine'. |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 804 | Memory[State] = std::pair<unsigned, unsigned>(Result, StopAt); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 805 | |
| 806 | return Result == UINT_MAX ? UINT_MAX : Result + CurrentPenalty; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 809 | FormatStyle Style; |
| 810 | SourceManager &SourceMgr; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 811 | const AnnotatedLine &Line; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 812 | const unsigned FirstIndent; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 813 | const AnnotatedToken &RootToken; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 814 | WhitespaceManager &Whitespaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 815 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 816 | // A map from an indent state to a pair (Result, Used-StopAt). |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 817 | typedef std::map<LineState, std::pair<unsigned, unsigned> > StateMap; |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 818 | StateMap Memory; |
| 819 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 820 | OptimizationParameters Parameters; |
| 821 | }; |
| 822 | |
| 823 | /// \brief Determines extra information about the tokens comprising an |
| 824 | /// \c UnwrappedLine. |
| 825 | class TokenAnnotator { |
| 826 | public: |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 827 | TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex, |
| 828 | AnnotatedLine &Line) |
| 829 | : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Line(Line) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 830 | |
| 831 | /// \brief A parser that gathers additional information about tokens. |
| 832 | /// |
| 833 | /// The \c TokenAnnotator tries to matches parenthesis and square brakets and |
| 834 | /// store a parenthesis levels. It also tries to resolve matching "<" and ">" |
| 835 | /// into template parameter lists. |
| 836 | class AnnotatingParser { |
| 837 | public: |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 838 | AnnotatingParser(AnnotatedToken &RootToken) |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 839 | : CurrentToken(&RootToken), KeywordVirtualFound(false), |
| 840 | ColonIsObjCMethodExpr(false) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 841 | |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 842 | /// \brief A helper class to manage AnnotatingParser::ColonIsObjCMethodExpr. |
| 843 | struct ObjCSelectorRAII { |
| 844 | AnnotatingParser &P; |
| 845 | bool ColonWasObjCMethodExpr; |
| 846 | |
| 847 | ObjCSelectorRAII(AnnotatingParser &P) |
| 848 | : P(P), ColonWasObjCMethodExpr(P.ColonIsObjCMethodExpr) {} |
| 849 | |
| 850 | ~ObjCSelectorRAII() { P.ColonIsObjCMethodExpr = ColonWasObjCMethodExpr; } |
| 851 | |
| 852 | void markStart(AnnotatedToken &Left) { |
| 853 | P.ColonIsObjCMethodExpr = true; |
| 854 | Left.Type = TT_ObjCMethodExpr; |
| 855 | } |
| 856 | |
| 857 | void markEnd(AnnotatedToken &Right) { Right.Type = TT_ObjCMethodExpr; } |
| 858 | }; |
| 859 | |
| 860 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 861 | bool parseAngle() { |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 862 | if (CurrentToken == NULL) |
| 863 | return false; |
| 864 | AnnotatedToken *Left = CurrentToken->Parent; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 865 | while (CurrentToken != NULL) { |
| 866 | if (CurrentToken->is(tok::greater)) { |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 867 | Left->MatchingParen = CurrentToken; |
| 868 | CurrentToken->MatchingParen = Left; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 869 | CurrentToken->Type = TT_TemplateCloser; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 870 | next(); |
| 871 | return true; |
| 872 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 873 | if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square) || |
| 874 | CurrentToken->is(tok::r_brace)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 875 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 876 | if (CurrentToken->is(tok::pipepipe) || CurrentToken->is(tok::ampamp) || |
| 877 | CurrentToken->is(tok::question) || CurrentToken->is(tok::colon)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 878 | return false; |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 879 | if (!consumeToken()) |
| 880 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 881 | } |
| 882 | return false; |
| 883 | } |
| 884 | |
Nico Weber | 5096a44 | 2013-01-17 17:17:19 +0000 | [diff] [blame] | 885 | bool parseParens(bool LookForDecls = false) { |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 886 | if (CurrentToken == NULL) |
| 887 | return false; |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 888 | bool StartsObjCMethodExpr = false; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 889 | AnnotatedToken *Left = CurrentToken->Parent; |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 890 | if (CurrentToken->is(tok::caret)) { |
| 891 | // ^( starts a block. |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 892 | Left->Type = TT_ObjCBlockLParen; |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 893 | } else if (AnnotatedToken *MaybeSel = Left->Parent) { |
| 894 | // @selector( starts a selector. |
| 895 | if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Parent && |
| 896 | MaybeSel->Parent->is(tok::at)) { |
| 897 | StartsObjCMethodExpr = true; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | ObjCSelectorRAII objCSelector(*this); |
| 902 | if (StartsObjCMethodExpr) |
| 903 | objCSelector.markStart(*Left); |
| 904 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 905 | while (CurrentToken != NULL) { |
Nico Weber | 5096a44 | 2013-01-17 17:17:19 +0000 | [diff] [blame] | 906 | // LookForDecls is set when "if (" has been seen. Check for |
| 907 | // 'identifier' '*' 'identifier' followed by not '=' -- this |
| 908 | // '*' has to be a binary operator but determineStarAmpUsage() will |
| 909 | // categorize it as an unary operator, so set the right type here. |
| 910 | if (LookForDecls && !CurrentToken->Children.empty()) { |
| 911 | AnnotatedToken &Prev = *CurrentToken->Parent; |
| 912 | AnnotatedToken &Next = CurrentToken->Children[0]; |
| 913 | if (Prev.Parent->is(tok::identifier) && |
| 914 | (Prev.is(tok::star) || Prev.is(tok::amp)) && |
| 915 | CurrentToken->is(tok::identifier) && Next.isNot(tok::equal)) { |
| 916 | Prev.Type = TT_BinaryOperator; |
| 917 | LookForDecls = false; |
| 918 | } |
| 919 | } |
| 920 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 921 | if (CurrentToken->is(tok::r_paren)) { |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 922 | Left->MatchingParen = CurrentToken; |
| 923 | CurrentToken->MatchingParen = Left; |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 924 | |
| 925 | if (StartsObjCMethodExpr) |
| 926 | objCSelector.markEnd(*CurrentToken); |
| 927 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 928 | next(); |
| 929 | return true; |
| 930 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 931 | if (CurrentToken->is(tok::r_square) || CurrentToken->is(tok::r_brace)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 932 | return false; |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 933 | if (!consumeToken()) |
| 934 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 935 | } |
| 936 | return false; |
| 937 | } |
| 938 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 939 | bool parseSquare() { |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 940 | if (!CurrentToken) |
| 941 | return false; |
| 942 | |
| 943 | // A '[' could be an index subscript (after an indentifier or after |
| 944 | // ')' or ']'), or it could be the start of an Objective-C method |
| 945 | // expression. |
Nico Weber | 3f29fbb | 2013-01-21 19:29:31 +0000 | [diff] [blame] | 946 | AnnotatedToken *Left = CurrentToken->Parent; |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 947 | bool StartsObjCMethodExpr = |
Nico Weber | 3f29fbb | 2013-01-21 19:29:31 +0000 | [diff] [blame] | 948 | !Left->Parent || Left->Parent->is(tok::colon) || |
| 949 | Left->Parent->is(tok::l_square) || Left->Parent->is(tok::l_paren) || |
| 950 | Left->Parent->is(tok::kw_return) || Left->Parent->is(tok::kw_throw) || |
| 951 | getBinOpPrecedence(Left->Parent->FormatTok.Tok.getKind(), |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 952 | true, true) > prec::Unknown; |
| 953 | |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 954 | ObjCSelectorRAII objCSelector(*this); |
| 955 | if (StartsObjCMethodExpr) |
Nico Weber | 3f29fbb | 2013-01-21 19:29:31 +0000 | [diff] [blame] | 956 | objCSelector.markStart(*Left); |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 957 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 958 | while (CurrentToken != NULL) { |
| 959 | if (CurrentToken->is(tok::r_square)) { |
Daniel Jasper | ffee171 | 2013-01-22 11:46:26 +0000 | [diff] [blame] | 960 | if (!CurrentToken->Children.empty() && |
| 961 | CurrentToken->Children[0].is(tok::l_paren)) { |
| 962 | // An ObjC method call can't be followed by an open parenthesis. |
| 963 | // FIXME: Do we incorrectly label ":" with this? |
| 964 | StartsObjCMethodExpr = false; |
| 965 | Left->Type = TT_Unknown; |
| 966 | } |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 967 | if (StartsObjCMethodExpr) |
| 968 | objCSelector.markEnd(*CurrentToken); |
Nico Weber | 05bf827 | 2013-01-21 19:35:06 +0000 | [diff] [blame] | 969 | Left->MatchingParen = CurrentToken; |
| 970 | CurrentToken->MatchingParen = Left; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 971 | next(); |
| 972 | return true; |
| 973 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 974 | if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_brace)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 975 | return false; |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 976 | if (!consumeToken()) |
| 977 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 978 | } |
| 979 | return false; |
| 980 | } |
| 981 | |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 982 | bool parseBrace() { |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 983 | // Lines are fine to end with '{'. |
| 984 | if (CurrentToken == NULL) |
| 985 | return true; |
| 986 | AnnotatedToken *Left = CurrentToken->Parent; |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 987 | while (CurrentToken != NULL) { |
| 988 | if (CurrentToken->is(tok::r_brace)) { |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 989 | Left->MatchingParen = CurrentToken; |
| 990 | CurrentToken->MatchingParen = Left; |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 991 | next(); |
| 992 | return true; |
| 993 | } |
| 994 | if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square)) |
| 995 | return false; |
| 996 | if (!consumeToken()) |
| 997 | return false; |
| 998 | } |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 999 | return true; |
| 1000 | } |
| 1001 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 1002 | bool parseConditional() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1003 | while (CurrentToken != NULL) { |
| 1004 | if (CurrentToken->is(tok::colon)) { |
| 1005 | CurrentToken->Type = TT_ConditionalExpr; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1006 | next(); |
| 1007 | return true; |
| 1008 | } |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1009 | if (!consumeToken()) |
| 1010 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1011 | } |
| 1012 | return false; |
| 1013 | } |
| 1014 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1015 | bool parseTemplateDeclaration() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1016 | if (CurrentToken != NULL && CurrentToken->is(tok::less)) { |
| 1017 | CurrentToken->Type = TT_TemplateOpener; |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1018 | next(); |
| 1019 | if (!parseAngle()) |
| 1020 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1021 | CurrentToken->Parent->ClosesTemplateDeclaration = true; |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1022 | return true; |
| 1023 | } |
| 1024 | return false; |
| 1025 | } |
| 1026 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1027 | bool consumeToken() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1028 | AnnotatedToken *Tok = CurrentToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1029 | next(); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1030 | switch (Tok->FormatTok.Tok.getKind()) { |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1031 | case tok::plus: |
| 1032 | case tok::minus: |
| 1033 | // At the start of the line, +/- specific ObjectiveC method |
| 1034 | // declarations. |
| 1035 | if (Tok->Parent == NULL) |
| 1036 | Tok->Type = TT_ObjCMethodSpecifier; |
| 1037 | break; |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1038 | case tok::colon: |
| 1039 | // Colons from ?: are handled in parseConditional(). |
Daniel Jasper | 1f2b078 | 2013-01-16 16:23:19 +0000 | [diff] [blame] | 1040 | if (Tok->Parent->is(tok::r_paren)) |
| 1041 | Tok->Type = TT_CtorInitializerColon; |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1042 | if (ColonIsObjCMethodExpr) |
| 1043 | Tok->Type = TT_ObjCMethodExpr; |
| 1044 | break; |
Nico Weber | 5096a44 | 2013-01-17 17:17:19 +0000 | [diff] [blame] | 1045 | case tok::kw_if: |
| 1046 | case tok::kw_while: |
Manuel Klimek | 092a2c7 | 2013-01-23 10:09:28 +0000 | [diff] [blame] | 1047 | if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) { |
Nico Weber | 5096a44 | 2013-01-17 17:17:19 +0000 | [diff] [blame] | 1048 | next(); |
| 1049 | if (!parseParens(/*LookForDecls=*/true)) |
| 1050 | return false; |
| 1051 | } |
| 1052 | break; |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 1053 | case tok::l_paren: |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1054 | if (!parseParens()) |
| 1055 | return false; |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 1056 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1057 | case tok::l_square: |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1058 | if (!parseSquare()) |
| 1059 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1060 | break; |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 1061 | case tok::l_brace: |
| 1062 | if (!parseBrace()) |
| 1063 | return false; |
| 1064 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1065 | case tok::less: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 1066 | if (parseAngle()) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1067 | Tok->Type = TT_TemplateOpener; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1068 | else { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1069 | Tok->Type = TT_BinaryOperator; |
| 1070 | CurrentToken = Tok; |
| 1071 | next(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1072 | } |
| 1073 | break; |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1074 | case tok::r_paren: |
| 1075 | case tok::r_square: |
| 1076 | return false; |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 1077 | case tok::r_brace: |
| 1078 | // Lines can start with '}'. |
| 1079 | if (Tok->Parent != NULL) |
| 1080 | return false; |
| 1081 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1082 | case tok::greater: |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1083 | Tok->Type = TT_BinaryOperator; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1084 | break; |
| 1085 | case tok::kw_operator: |
Manuel Klimek | 092a2c7 | 2013-01-23 10:09:28 +0000 | [diff] [blame] | 1086 | if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1087 | CurrentToken->Type = TT_OverloadedOperator; |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 1088 | next(); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1089 | if (CurrentToken != NULL && CurrentToken->is(tok::r_paren)) { |
| 1090 | CurrentToken->Type = TT_OverloadedOperator; |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 1091 | next(); |
| 1092 | } |
| 1093 | } else { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1094 | while (CurrentToken != NULL && CurrentToken->isNot(tok::l_paren)) { |
| 1095 | CurrentToken->Type = TT_OverloadedOperator; |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 1096 | next(); |
| 1097 | } |
| 1098 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1099 | break; |
| 1100 | case tok::question: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 1101 | parseConditional(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1102 | break; |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1103 | case tok::kw_template: |
| 1104 | parseTemplateDeclaration(); |
| 1105 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1106 | default: |
| 1107 | break; |
| 1108 | } |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1109 | return true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1112 | void parseIncludeDirective() { |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 1113 | next(); |
| 1114 | if (CurrentToken != NULL && CurrentToken->is(tok::less)) { |
| 1115 | next(); |
| 1116 | while (CurrentToken != NULL) { |
Daniel Jasper | 7d1185d | 2013-01-18 09:19:33 +0000 | [diff] [blame] | 1117 | if (CurrentToken->isNot(tok::comment) || |
| 1118 | !CurrentToken->Children.empty()) |
| 1119 | CurrentToken->Type = TT_ImplicitStringLiteral; |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 1120 | next(); |
| 1121 | } |
| 1122 | } else { |
| 1123 | while (CurrentToken != NULL) { |
| 1124 | next(); |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | void parseWarningOrError() { |
| 1130 | next(); |
| 1131 | // We still want to format the whitespace left of the first token of the |
| 1132 | // warning or error. |
| 1133 | next(); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1134 | while (CurrentToken != NULL) { |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 1135 | CurrentToken->Type = TT_ImplicitStringLiteral; |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1136 | next(); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | void parsePreprocessorDirective() { |
| 1141 | next(); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1142 | if (CurrentToken == NULL) |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1143 | return; |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 1144 | // Hashes in the middle of a line can lead to any strange token |
| 1145 | // sequence. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1146 | if (CurrentToken->FormatTok.Tok.getIdentifierInfo() == NULL) |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 1147 | return; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1148 | switch ( |
| 1149 | CurrentToken->FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) { |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1150 | case tok::pp_include: |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 1151 | case tok::pp_import: |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1152 | parseIncludeDirective(); |
| 1153 | break; |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 1154 | case tok::pp_error: |
| 1155 | case tok::pp_warning: |
| 1156 | parseWarningOrError(); |
| 1157 | break; |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1158 | default: |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1163 | LineType parseLine() { |
Daniel Jasper | 3298327 | 2013-01-22 14:28:24 +0000 | [diff] [blame] | 1164 | int PeriodsAndArrows = 0; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1165 | if (CurrentToken->is(tok::hash)) { |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1166 | parsePreprocessorDirective(); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1167 | return LT_PreprocessorDirective; |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1168 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1169 | while (CurrentToken != NULL) { |
Daniel Jasper | 3298327 | 2013-01-22 14:28:24 +0000 | [diff] [blame] | 1170 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1171 | if (CurrentToken->is(tok::kw_virtual)) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1172 | KeywordVirtualFound = true; |
Daniel Jasper | 3298327 | 2013-01-22 14:28:24 +0000 | [diff] [blame] | 1173 | if (CurrentToken->is(tok::period) || CurrentToken->is(tok::arrow)) |
| 1174 | ++PeriodsAndArrows; |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1175 | if (!consumeToken()) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1176 | return LT_Invalid; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1177 | } |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1178 | if (KeywordVirtualFound) |
| 1179 | return LT_VirtualFunctionDecl; |
Daniel Jasper | 3298327 | 2013-01-22 14:28:24 +0000 | [diff] [blame] | 1180 | |
| 1181 | // Assume a builder-type call if there are 2 or more "." and "->". |
| 1182 | if (PeriodsAndArrows >= 2) |
| 1183 | return LT_BuilderTypeCall; |
| 1184 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1185 | return LT_Other; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | void next() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1189 | if (CurrentToken != NULL && !CurrentToken->Children.empty()) |
| 1190 | CurrentToken = &CurrentToken->Children[0]; |
| 1191 | else |
| 1192 | CurrentToken = NULL; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | private: |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1196 | AnnotatedToken *CurrentToken; |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1197 | bool KeywordVirtualFound; |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1198 | bool ColonIsObjCMethodExpr; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1199 | }; |
| 1200 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1201 | void calculateExtraInformation(AnnotatedToken &Current) { |
| 1202 | Current.SpaceRequiredBefore = spaceRequiredBefore(Current); |
| 1203 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1204 | if (Current.FormatTok.MustBreakBefore) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1205 | Current.MustBreakBefore = true; |
| 1206 | } else { |
Daniel Jasper | 487f64b | 2013-01-13 16:10:20 +0000 | [diff] [blame] | 1207 | if (Current.Type == TT_LineComment) { |
| 1208 | Current.MustBreakBefore = Current.FormatTok.NewlinesBefore > 0; |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 1209 | } else if ((Current.Parent->is(tok::comment) && |
| 1210 | Current.FormatTok.NewlinesBefore > 0) || |
Daniel Jasper | 487f64b | 2013-01-13 16:10:20 +0000 | [diff] [blame] | 1211 | (Current.is(tok::string_literal) && |
| 1212 | Current.Parent->is(tok::string_literal))) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1213 | Current.MustBreakBefore = true; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1214 | } else { |
| 1215 | Current.MustBreakBefore = false; |
| 1216 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1217 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1218 | Current.CanBreakBefore = Current.MustBreakBefore || canBreakBefore(Current); |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1219 | if (Current.MustBreakBefore) |
| 1220 | Current.TotalLength = Current.Parent->TotalLength + Style.ColumnLimit; |
| 1221 | else |
| 1222 | Current.TotalLength = Current.Parent->TotalLength + |
| 1223 | Current.FormatTok.TokenLength + |
| 1224 | (Current.SpaceRequiredBefore ? 1 : 0); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1225 | if (!Current.Children.empty()) |
| 1226 | calculateExtraInformation(Current.Children[0]); |
| 1227 | } |
| 1228 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1229 | void annotate() { |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1230 | AnnotatingParser Parser(Line.First); |
| 1231 | Line.Type = Parser.parseLine(); |
| 1232 | if (Line.Type == LT_Invalid) |
| 1233 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1234 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1235 | determineTokenTypes(Line.First, /*IsExpression=*/ false); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1236 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1237 | if (Line.First.Type == TT_ObjCMethodSpecifier) |
| 1238 | Line.Type = LT_ObjCMethodDecl; |
| 1239 | else if (Line.First.Type == TT_ObjCDecl) |
| 1240 | Line.Type = LT_ObjCDecl; |
| 1241 | else if (Line.First.Type == TT_ObjCProperty) |
| 1242 | Line.Type = LT_ObjCProperty; |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1243 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1244 | Line.First.SpaceRequiredBefore = true; |
| 1245 | Line.First.MustBreakBefore = Line.First.FormatTok.MustBreakBefore; |
| 1246 | Line.First.CanBreakBefore = Line.First.MustBreakBefore; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1247 | |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1248 | Line.First.TotalLength = Line.First.FormatTok.TokenLength; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1249 | if (!Line.First.Children.empty()) |
| 1250 | calculateExtraInformation(Line.First.Children[0]); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | private: |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1254 | void determineTokenTypes(AnnotatedToken &Current, bool IsExpression) { |
| 1255 | if (getPrecedence(Current) == prec::Assignment) { |
| 1256 | IsExpression = true; |
| 1257 | AnnotatedToken *Previous = Current.Parent; |
| 1258 | while (Previous != NULL) { |
| 1259 | if (Previous->Type == TT_BinaryOperator) |
| 1260 | Previous->Type = TT_PointerOrReference; |
| 1261 | Previous = Previous->Parent; |
| 1262 | } |
| 1263 | } |
| 1264 | if (Current.is(tok::kw_return) || Current.is(tok::kw_throw) || |
Daniel Jasper | 20d3583 | 2013-01-23 12:58:14 +0000 | [diff] [blame^] | 1265 | (Current.is(tok::l_paren) && !Line.MustBeDeclaration && |
| 1266 | (Current.Parent == NULL || Current.Parent->isNot(tok::kw_for)))) |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1267 | IsExpression = true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1268 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1269 | if (Current.Type == TT_Unknown) { |
| 1270 | if (Current.is(tok::star) || Current.is(tok::amp)) { |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1271 | Current.Type = determineStarAmpUsage(Current, IsExpression); |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 1272 | } else if (Current.is(tok::minus) || Current.is(tok::plus) || |
| 1273 | Current.is(tok::caret)) { |
| 1274 | Current.Type = determinePlusMinusCaretUsage(Current); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1275 | } else if (Current.is(tok::minusminus) || Current.is(tok::plusplus)) { |
| 1276 | Current.Type = determineIncrementUsage(Current); |
| 1277 | } else if (Current.is(tok::exclaim)) { |
| 1278 | Current.Type = TT_UnaryOperator; |
| 1279 | } else if (isBinaryOperator(Current)) { |
| 1280 | Current.Type = TT_BinaryOperator; |
| 1281 | } else if (Current.is(tok::comment)) { |
| 1282 | std::string Data(Lexer::getSpelling(Current.FormatTok.Tok, SourceMgr, |
| 1283 | Lex.getLangOpts())); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 1284 | if (StringRef(Data).startswith("//")) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1285 | Current.Type = TT_LineComment; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1286 | else |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1287 | Current.Type = TT_BlockComment; |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 1288 | } else if (Current.is(tok::r_paren) && |
| 1289 | (Current.Parent->Type == TT_PointerOrReference || |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 1290 | Current.Parent->Type == TT_TemplateCloser) && |
| 1291 | (Current.Children.empty() || |
| 1292 | (Current.Children[0].isNot(tok::equal) && |
| 1293 | Current.Children[0].isNot(tok::semi) && |
| 1294 | Current.Children[0].isNot(tok::l_brace)))) { |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 1295 | // FIXME: We need to get smarter and understand more cases of casts. |
| 1296 | Current.Type = TT_CastRParen; |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1297 | } else if (Current.is(tok::at) && Current.Children.size()) { |
| 1298 | switch (Current.Children[0].FormatTok.Tok.getObjCKeywordID()) { |
| 1299 | case tok::objc_interface: |
| 1300 | case tok::objc_implementation: |
| 1301 | case tok::objc_protocol: |
| 1302 | Current.Type = TT_ObjCDecl; |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 1303 | break; |
| 1304 | case tok::objc_property: |
| 1305 | Current.Type = TT_ObjCProperty; |
| 1306 | break; |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1307 | default: |
| 1308 | break; |
| 1309 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1310 | } |
| 1311 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1312 | |
| 1313 | if (!Current.Children.empty()) |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1314 | determineTokenTypes(Current.Children[0], IsExpression); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1317 | bool isBinaryOperator(const AnnotatedToken &Tok) { |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1318 | // Comma is a binary operator, but does not behave as such wrt. formatting. |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1319 | return getPrecedence(Tok) > prec::Comma; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1322 | /// \brief Returns the previous token ignoring comments. |
| 1323 | const AnnotatedToken *getPreviousToken(const AnnotatedToken &Tok) { |
| 1324 | const AnnotatedToken *PrevToken = Tok.Parent; |
| 1325 | while (PrevToken != NULL && PrevToken->is(tok::comment)) |
| 1326 | PrevToken = PrevToken->Parent; |
| 1327 | return PrevToken; |
| 1328 | } |
| 1329 | |
| 1330 | /// \brief Returns the next token ignoring comments. |
| 1331 | const AnnotatedToken *getNextToken(const AnnotatedToken &Tok) { |
| 1332 | if (Tok.Children.empty()) |
| 1333 | return NULL; |
| 1334 | const AnnotatedToken *NextToken = &Tok.Children[0]; |
| 1335 | while (NextToken->is(tok::comment)) { |
| 1336 | if (NextToken->Children.empty()) |
| 1337 | return NULL; |
| 1338 | NextToken = &NextToken->Children[0]; |
| 1339 | } |
| 1340 | return NextToken; |
| 1341 | } |
| 1342 | |
| 1343 | /// \brief Return the type of the given token assuming it is * or &. |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1344 | TokenType determineStarAmpUsage(const AnnotatedToken &Tok, |
| 1345 | bool IsExpression) { |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1346 | const AnnotatedToken *PrevToken = getPreviousToken(Tok); |
| 1347 | if (PrevToken == NULL) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1348 | return TT_UnaryOperator; |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1349 | |
| 1350 | const AnnotatedToken *NextToken = getNextToken(Tok); |
| 1351 | if (NextToken == NULL) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1352 | return TT_Unknown; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1353 | |
Daniel Jasper | ffee171 | 2013-01-22 11:46:26 +0000 | [diff] [blame] | 1354 | if (NextToken->is(tok::l_square) && NextToken->Type != TT_ObjCMethodExpr) |
| 1355 | return TT_PointerOrReference; |
| 1356 | |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1357 | if (PrevToken->is(tok::l_paren) || PrevToken->is(tok::l_square) || |
| 1358 | PrevToken->is(tok::l_brace) || PrevToken->is(tok::comma) || |
| 1359 | PrevToken->is(tok::kw_return) || PrevToken->is(tok::colon) || |
| 1360 | PrevToken->Type == TT_BinaryOperator || |
Daniel Jasper | 48bd7b7 | 2013-01-16 16:04:06 +0000 | [diff] [blame] | 1361 | PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1362 | return TT_UnaryOperator; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1363 | |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1364 | if (PrevToken->FormatTok.Tok.isLiteral() || PrevToken->is(tok::r_paren) || |
| 1365 | PrevToken->is(tok::r_square) || NextToken->FormatTok.Tok.isLiteral() || |
| 1366 | NextToken->is(tok::plus) || NextToken->is(tok::minus) || |
| 1367 | NextToken->is(tok::plusplus) || NextToken->is(tok::minusminus) || |
| 1368 | NextToken->is(tok::tilde) || NextToken->is(tok::exclaim) || |
| 1369 | NextToken->is(tok::l_paren) || NextToken->is(tok::l_square) || |
| 1370 | NextToken->is(tok::kw_alignof) || NextToken->is(tok::kw_sizeof)) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1371 | return TT_BinaryOperator; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1372 | |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1373 | if (NextToken->is(tok::comma) || NextToken->is(tok::r_paren) || |
| 1374 | NextToken->is(tok::greater)) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1375 | return TT_PointerOrReference; |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 1376 | |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 1377 | // It is very unlikely that we are going to find a pointer or reference type |
| 1378 | // definition on the RHS of an assignment. |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1379 | if (IsExpression) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1380 | return TT_BinaryOperator; |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 1381 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1382 | return TT_PointerOrReference; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 1385 | TokenType determinePlusMinusCaretUsage(const AnnotatedToken &Tok) { |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1386 | const AnnotatedToken *PrevToken = getPreviousToken(Tok); |
| 1387 | if (PrevToken == NULL) |
| 1388 | return TT_UnaryOperator; |
| 1389 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1390 | // Use heuristics to recognize unary operators. |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1391 | if (PrevToken->is(tok::equal) || PrevToken->is(tok::l_paren) || |
| 1392 | PrevToken->is(tok::comma) || PrevToken->is(tok::l_square) || |
| 1393 | PrevToken->is(tok::question) || PrevToken->is(tok::colon) || |
| 1394 | PrevToken->is(tok::kw_return) || PrevToken->is(tok::kw_case) || |
| 1395 | PrevToken->is(tok::at) || PrevToken->is(tok::l_brace)) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1396 | return TT_UnaryOperator; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1397 | |
| 1398 | // There can't be to consecutive binary operators. |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1399 | if (PrevToken->Type == TT_BinaryOperator) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1400 | return TT_UnaryOperator; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1401 | |
| 1402 | // Fall back to marking the token as binary operator. |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1403 | return TT_BinaryOperator; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /// \brief Determine whether ++/-- are pre- or post-increments/-decrements. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1407 | TokenType determineIncrementUsage(const AnnotatedToken &Tok) { |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1408 | const AnnotatedToken *PrevToken = getPreviousToken(Tok); |
| 1409 | if (PrevToken == NULL) |
Daniel Jasper | 4abbb53 | 2013-01-14 12:18:19 +0000 | [diff] [blame] | 1410 | return TT_UnaryOperator; |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1411 | if (PrevToken->is(tok::r_paren) || PrevToken->is(tok::r_square) || |
| 1412 | PrevToken->is(tok::identifier)) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1413 | return TT_TrailingUnaryOperator; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1414 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1415 | return TT_UnaryOperator; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1418 | bool spaceRequiredBetween(const AnnotatedToken &Left, |
| 1419 | const AnnotatedToken &Right) { |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 1420 | if (Right.is(tok::hashhash)) |
| 1421 | return Left.is(tok::hash); |
| 1422 | if (Left.is(tok::hashhash) || Left.is(tok::hash)) |
| 1423 | return Right.is(tok::hash); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1424 | if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma)) |
| 1425 | return false; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1426 | if (Right.is(tok::less) && |
| 1427 | (Left.is(tok::kw_template) || |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1428 | (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList))) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1429 | return true; |
| 1430 | if (Left.is(tok::arrow) || Right.is(tok::arrow)) |
| 1431 | return false; |
| 1432 | if (Left.is(tok::exclaim) || Left.is(tok::tilde)) |
| 1433 | return false; |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 1434 | if (Left.is(tok::at) && |
| 1435 | (Right.is(tok::identifier) || Right.is(tok::string_literal) || |
| 1436 | Right.is(tok::char_constant) || Right.is(tok::numeric_constant) || |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1437 | Right.is(tok::l_paren) || Right.is(tok::l_brace) || |
| 1438 | Right.is(tok::kw_true) || Right.is(tok::kw_false))) |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1439 | return false; |
Daniel Jasper | 6b825c2 | 2013-01-16 07:19:28 +0000 | [diff] [blame] | 1440 | if (Left.is(tok::coloncolon)) |
| 1441 | return false; |
| 1442 | if (Right.is(tok::coloncolon)) |
| 1443 | return Left.isNot(tok::identifier) && Left.isNot(tok::greater); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1444 | if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less)) |
| 1445 | return false; |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 1446 | if (Right.is(tok::amp) || Right.is(tok::star)) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1447 | return Left.FormatTok.Tok.isLiteral() || |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 1448 | (Left.isNot(tok::star) && Left.isNot(tok::amp) && |
| 1449 | !Style.PointerAndReferenceBindToType); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1450 | if (Left.is(tok::amp) || Left.is(tok::star)) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1451 | return Right.FormatTok.Tok.isLiteral() || |
| 1452 | Style.PointerAndReferenceBindToType; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1453 | if (Right.is(tok::star) && Left.is(tok::l_paren)) |
| 1454 | return false; |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1455 | if (Left.is(tok::l_square) || Right.is(tok::r_square)) |
| 1456 | return false; |
| 1457 | if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1458 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1459 | if (Left.is(tok::period) || Right.is(tok::period)) |
| 1460 | return false; |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1461 | if (Left.is(tok::colon)) |
| 1462 | return Left.Type != TT_ObjCMethodExpr; |
| 1463 | if (Right.is(tok::colon)) |
| 1464 | return Right.Type != TT_ObjCMethodExpr; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1465 | if (Left.is(tok::l_paren)) |
| 1466 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1467 | if (Right.is(tok::l_paren)) { |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1468 | return Line.Type == LT_ObjCDecl || Left.is(tok::kw_if) || |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1469 | Left.is(tok::kw_for) || Left.is(tok::kw_while) || |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1470 | Left.is(tok::kw_switch) || Left.is(tok::kw_return) || |
Daniel Jasper | 088dab5 | 2013-01-11 16:09:04 +0000 | [diff] [blame] | 1471 | Left.is(tok::kw_catch) || Left.is(tok::kw_new) || |
| 1472 | Left.is(tok::kw_delete); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1473 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1474 | if (Left.is(tok::at) && |
| 1475 | Right.FormatTok.Tok.getObjCKeywordID() != tok::objc_not_keyword) |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1476 | return false; |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1477 | if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) |
| 1478 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1479 | return true; |
| 1480 | } |
| 1481 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1482 | bool spaceRequiredBefore(const AnnotatedToken &Tok) { |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1483 | if (Line.Type == LT_ObjCMethodDecl) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1484 | if (Tok.is(tok::identifier) && !Tok.Children.empty() && |
| 1485 | Tok.Children[0].is(tok::colon) && Tok.Parent->is(tok::identifier)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1486 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1487 | if (Tok.is(tok::colon)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1488 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1489 | if (Tok.Parent->Type == TT_ObjCMethodSpecifier) |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1490 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1491 | if (Tok.Parent->is(tok::r_paren) && Tok.is(tok::identifier)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1492 | // Don't space between ')' and <id> |
| 1493 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1494 | if (Tok.Parent->is(tok::colon) && Tok.is(tok::l_paren)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1495 | // Don't space between ':' and '(' |
| 1496 | return false; |
| 1497 | } |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1498 | if (Line.Type == LT_ObjCProperty && |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 1499 | (Tok.is(tok::equal) || Tok.Parent->is(tok::equal))) |
| 1500 | return false; |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1501 | |
Daniel Jasper | 4e9008a | 2013-01-13 08:19:51 +0000 | [diff] [blame] | 1502 | if (Tok.Parent->is(tok::comma)) |
| 1503 | return true; |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1504 | if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1505 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1506 | if (Tok.Type == TT_OverloadedOperator) |
| 1507 | return Tok.is(tok::identifier) || Tok.is(tok::kw_new) || |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1508 | Tok.is(tok::kw_delete) || Tok.is(tok::kw_bool); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1509 | if (Tok.Parent->Type == TT_OverloadedOperator) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1510 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1511 | if (Tok.is(tok::colon)) |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1512 | return Line.First.isNot(tok::kw_case) && !Tok.Children.empty() && |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1513 | Tok.Type != TT_ObjCMethodExpr; |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 1514 | if (Tok.Parent->Type == TT_UnaryOperator || |
| 1515 | Tok.Parent->Type == TT_CastRParen) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1516 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1517 | if (Tok.Type == TT_UnaryOperator) |
| 1518 | return Tok.Parent->isNot(tok::l_paren) && |
Nico Weber | cd45833 | 2013-01-12 23:48:49 +0000 | [diff] [blame] | 1519 | Tok.Parent->isNot(tok::l_square) && Tok.Parent->isNot(tok::at) && |
| 1520 | (Tok.Parent->isNot(tok::colon) || |
| 1521 | Tok.Parent->Type != TT_ObjCMethodExpr); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1522 | if (Tok.Parent->is(tok::greater) && Tok.is(tok::greater)) { |
| 1523 | return Tok.Type == TT_TemplateCloser && Tok.Parent->Type == |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1524 | TT_TemplateCloser && Style.SplitTemplateClosingGreater; |
| 1525 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1526 | if (Tok.Type == TT_BinaryOperator || Tok.Parent->Type == TT_BinaryOperator) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1527 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1528 | if (Tok.Parent->Type == TT_TemplateCloser && Tok.is(tok::l_paren)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1529 | return false; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1530 | if (Tok.is(tok::less) && Line.First.is(tok::hash)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1531 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1532 | if (Tok.Type == TT_TrailingUnaryOperator) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1533 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1534 | return spaceRequiredBetween(*Tok.Parent, Tok); |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1537 | bool canBreakBefore(const AnnotatedToken &Right) { |
| 1538 | const AnnotatedToken &Left = *Right.Parent; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1539 | if (Line.Type == LT_ObjCMethodDecl) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1540 | if (Right.is(tok::identifier) && !Right.Children.empty() && |
| 1541 | Right.Children[0].is(tok::colon) && Left.is(tok::identifier)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1542 | return true; |
Nico Weber | 774b973 | 2013-01-12 07:00:16 +0000 | [diff] [blame] | 1543 | if (Right.is(tok::identifier) && Left.is(tok::l_paren) && |
| 1544 | Left.Parent->is(tok::colon)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1545 | // Don't break this identifier as ':' or identifier |
| 1546 | // before it will break. |
| 1547 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1548 | if (Right.is(tok::colon) && Left.is(tok::identifier) && |
| 1549 | Left.CanBreakBefore) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1550 | // Don't break at ':' if identifier before it can beak. |
| 1551 | return false; |
| 1552 | } |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1553 | if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr) |
| 1554 | return false; |
| 1555 | if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr) |
| 1556 | return true; |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 1557 | if (isObjCSelectorName(Right)) |
| 1558 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1559 | if (Left.ClosesTemplateDeclaration) |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 1560 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1561 | if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser || |
Daniel Jasper | 2db356d | 2013-01-08 20:03:18 +0000 | [diff] [blame] | 1562 | Left.Type == TT_UnaryOperator || Right.Type == TT_ConditionalExpr) |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1563 | return false; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1564 | if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1565 | return false; |
| 1566 | |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 1567 | if (Right.Type == TT_LineComment) |
Daniel Jasper | 487f64b | 2013-01-13 16:10:20 +0000 | [diff] [blame] | 1568 | // We rely on MustBreakBefore being set correctly here as we should not |
| 1569 | // change the "binding" behavior of a comment. |
| 1570 | return false; |
| 1571 | |
Daniel Jasper | 60ca75d | 2013-01-17 13:31:52 +0000 | [diff] [blame] | 1572 | // Allow breaking after a trailing 'const', e.g. after a method declaration, |
| 1573 | // unless it is follow by ';', '{' or '='. |
| 1574 | if (Left.is(tok::kw_const) && Left.Parent != NULL && |
| 1575 | Left.Parent->is(tok::r_paren)) |
| 1576 | return Right.isNot(tok::l_brace) && Right.isNot(tok::semi) && |
| 1577 | Right.isNot(tok::equal); |
| 1578 | |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 1579 | // We only break before r_brace if there was a corresponding break before |
| 1580 | // the l_brace, which is tracked by BreakBeforeClosingBrace. |
| 1581 | if (Right.is(tok::r_brace)) |
| 1582 | return false; |
| 1583 | |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1584 | if (Right.is(tok::r_paren) || Right.is(tok::greater)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1585 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1586 | return (isBinaryOperator(Left) && Left.isNot(tok::lessless)) || |
| 1587 | Left.is(tok::comma) || Right.is(tok::lessless) || |
| 1588 | Right.is(tok::arrow) || Right.is(tok::period) || |
| 1589 | Right.is(tok::colon) || Left.is(tok::semi) || |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 1590 | Left.is(tok::l_brace) || Left.is(tok::question) || Left.Type == |
| 1591 | TT_ConditionalExpr || (Left.is(tok::r_paren) && Left.Type != |
| 1592 | TT_CastRParen && Right.is(tok::identifier)) || |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1593 | (Left.is(tok::l_paren) && !Right.is(tok::r_paren)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1596 | FormatStyle Style; |
| 1597 | SourceManager &SourceMgr; |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 1598 | Lexer &Lex; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1599 | AnnotatedLine &Line; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1600 | }; |
| 1601 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1602 | class LexerBasedFormatTokenSource : public FormatTokenSource { |
| 1603 | public: |
| 1604 | LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1605 | : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1606 | IdentTable(Lex.getLangOpts()) { |
| 1607 | Lex.SetKeepWhitespaceMode(true); |
| 1608 | } |
| 1609 | |
| 1610 | virtual FormatToken getNextToken() { |
| 1611 | if (GreaterStashed) { |
| 1612 | FormatTok.NewlinesBefore = 0; |
| 1613 | FormatTok.WhiteSpaceStart = |
| 1614 | FormatTok.Tok.getLocation().getLocWithOffset(1); |
| 1615 | FormatTok.WhiteSpaceLength = 0; |
| 1616 | GreaterStashed = false; |
| 1617 | return FormatTok; |
| 1618 | } |
| 1619 | |
| 1620 | FormatTok = FormatToken(); |
| 1621 | Lex.LexFromRawLexer(FormatTok.Tok); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1622 | StringRef Text = rawTokenText(FormatTok.Tok); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1623 | FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation(); |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 1624 | if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0) |
| 1625 | FormatTok.IsFirst = true; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1626 | |
| 1627 | // Consume and record whitespace until we find a significant token. |
| 1628 | while (FormatTok.Tok.is(tok::unknown)) { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 1629 | FormatTok.NewlinesBefore += Text.count('\n'); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1630 | FormatTok.HasUnescapedNewline = Text.count("\\\n") != |
| 1631 | FormatTok.NewlinesBefore; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1632 | FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength(); |
| 1633 | |
| 1634 | if (FormatTok.Tok.is(tok::eof)) |
| 1635 | return FormatTok; |
| 1636 | Lex.LexFromRawLexer(FormatTok.Tok); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1637 | Text = rawTokenText(FormatTok.Tok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1638 | } |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1639 | |
| 1640 | // Now FormatTok is the next non-whitespace token. |
| 1641 | FormatTok.TokenLength = Text.size(); |
| 1642 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1643 | // In case the token starts with escaped newlines, we want to |
| 1644 | // take them into account as whitespace - this pattern is quite frequent |
| 1645 | // in macro definitions. |
| 1646 | // FIXME: What do we want to do with other escaped spaces, and escaped |
| 1647 | // spaces or newlines in the middle of tokens? |
| 1648 | // FIXME: Add a more explicit test. |
| 1649 | unsigned i = 0; |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1650 | while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1651 | // FIXME: ++FormatTok.NewlinesBefore is missing... |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1652 | FormatTok.WhiteSpaceLength += 2; |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1653 | FormatTok.TokenLength -= 2; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1654 | i += 2; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | if (FormatTok.Tok.is(tok::raw_identifier)) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1658 | IdentifierInfo &Info = IdentTable.get(Text); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1659 | FormatTok.Tok.setIdentifierInfo(&Info); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1660 | FormatTok.Tok.setKind(Info.getTokenID()); |
| 1661 | } |
| 1662 | |
| 1663 | if (FormatTok.Tok.is(tok::greatergreater)) { |
| 1664 | FormatTok.Tok.setKind(tok::greater); |
| 1665 | GreaterStashed = true; |
| 1666 | } |
| 1667 | |
| 1668 | return FormatTok; |
| 1669 | } |
| 1670 | |
| 1671 | private: |
| 1672 | FormatToken FormatTok; |
| 1673 | bool GreaterStashed; |
| 1674 | Lexer &Lex; |
| 1675 | SourceManager &SourceMgr; |
| 1676 | IdentifierTable IdentTable; |
| 1677 | |
| 1678 | /// Returns the text of \c FormatTok. |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1679 | StringRef rawTokenText(Token &Tok) { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1680 | return StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
| 1681 | Tok.getLength()); |
| 1682 | } |
| 1683 | }; |
| 1684 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1685 | class Formatter : public UnwrappedLineConsumer { |
| 1686 | public: |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1687 | Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex, |
| 1688 | SourceManager &SourceMgr, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1689 | const std::vector<CharSourceRange> &Ranges) |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1690 | : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr), |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1691 | Whitespaces(SourceMgr), Ranges(Ranges) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1692 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1693 | virtual ~Formatter() {} |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 1694 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1695 | tooling::Replacements format() { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1696 | LexerBasedFormatTokenSource Tokens(Lex, SourceMgr); |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1697 | UnwrappedLineParser Parser(Diag, Style, Tokens, *this); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1698 | StructuralError = Parser.parse(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1699 | unsigned PreviousEndOfLineColumn = 0; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1700 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1701 | TokenAnnotator Annotator(Style, SourceMgr, Lex, AnnotatedLines[i]); |
| 1702 | Annotator.annotate(); |
| 1703 | } |
| 1704 | for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(), |
| 1705 | E = AnnotatedLines.end(); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1706 | I != E; ++I) { |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1707 | const AnnotatedLine &TheLine = *I; |
| 1708 | if (touchesRanges(TheLine) && TheLine.Type != LT_Invalid) { |
| 1709 | unsigned Indent = formatFirstToken(TheLine.First, TheLine.Level, |
| 1710 | TheLine.InPPDirective, |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1711 | PreviousEndOfLineColumn); |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1712 | tryFitMultipleLinesInOne(Indent, I, E); |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1713 | UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent, |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1714 | TheLine.First, Whitespaces, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1715 | StructuralError); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1716 | PreviousEndOfLineColumn = Formatter.format(); |
| 1717 | } else { |
| 1718 | // If we did not reformat this unwrapped line, the column at the end of |
| 1719 | // the last token is unchanged - thus, we can calculate the end of the |
| 1720 | // last token, and return the result. |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1721 | PreviousEndOfLineColumn = |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1722 | SourceMgr.getSpellingColumnNumber( |
| 1723 | TheLine.Last->FormatTok.Tok.getLocation()) + |
| 1724 | Lex.MeasureTokenLength(TheLine.Last->FormatTok.Tok.getLocation(), |
| 1725 | SourceMgr, Lex.getLangOpts()) - |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1726 | 1; |
| 1727 | } |
| 1728 | } |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1729 | return Whitespaces.generateReplacements(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | private: |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1733 | /// \brief Tries to merge lines into one. |
| 1734 | /// |
| 1735 | /// This will change \c Line and \c AnnotatedLine to contain the merged line, |
| 1736 | /// if possible; note that \c I will be incremented when lines are merged. |
| 1737 | /// |
| 1738 | /// Returns whether the resulting \c Line can fit in a single line. |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1739 | void tryFitMultipleLinesInOne(unsigned Indent, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1740 | std::vector<AnnotatedLine>::iterator &I, |
| 1741 | std::vector<AnnotatedLine>::iterator E) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1742 | unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent; |
| 1743 | |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1744 | // We can never merge stuff if there are trailing line comments. |
| 1745 | if (I->Last->Type == TT_LineComment) |
| 1746 | return; |
| 1747 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1748 | // Check whether the UnwrappedLine can be put onto a single line. If |
| 1749 | // so, this is bound to be the optimal solution (by definition) and we |
| 1750 | // don't need to analyze the entire solution space. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1751 | if (I->Last->TotalLength > Limit) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1752 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1753 | Limit -= I->Last->TotalLength; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1754 | |
Daniel Jasper | 9c8c40e | 2013-01-21 14:18:28 +0000 | [diff] [blame] | 1755 | if (I + 1 == E || (I + 1)->Type == LT_Invalid) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1756 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1757 | |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1758 | if (I->Last->is(tok::l_brace)) { |
| 1759 | tryMergeSimpleBlock(I, E, Limit); |
| 1760 | } else if (I->First.is(tok::kw_if)) { |
| 1761 | tryMergeSimpleIf(I, E, Limit); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1762 | } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline || |
| 1763 | I->First.FormatTok.IsFirst)) { |
| 1764 | tryMergeSimplePPDirective(I, E, Limit); |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1765 | } |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1766 | return; |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1769 | void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I, |
| 1770 | std::vector<AnnotatedLine>::iterator E, |
| 1771 | unsigned Limit) { |
| 1772 | AnnotatedLine &Line = *I; |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 1773 | if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline) |
| 1774 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1775 | if (I + 2 != E && (I + 2)->InPPDirective && |
| 1776 | !(I + 2)->First.FormatTok.HasUnescapedNewline) |
| 1777 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1778 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1779 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1780 | join(Line, *(++I)); |
| 1781 | } |
| 1782 | |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1783 | void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I, |
| 1784 | std::vector<AnnotatedLine>::iterator E, |
| 1785 | unsigned Limit) { |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 1786 | if (!Style.AllowShortIfStatementsOnASingleLine) |
| 1787 | return; |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 1788 | if ((I + 1)->InPPDirective != I->InPPDirective || |
| 1789 | ((I + 1)->InPPDirective && |
| 1790 | (I + 1)->First.FormatTok.HasUnescapedNewline)) |
| 1791 | return; |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1792 | AnnotatedLine &Line = *I; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1793 | if (Line.Last->isNot(tok::r_paren)) |
| 1794 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1795 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1796 | return; |
| 1797 | if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment) |
| 1798 | return; |
| 1799 | // Only inline simple if's (no nested if or else). |
| 1800 | if (I + 2 != E && (I + 2)->First.is(tok::kw_else)) |
| 1801 | return; |
| 1802 | join(Line, *(++I)); |
| 1803 | } |
| 1804 | |
| 1805 | void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I, |
| 1806 | std::vector<AnnotatedLine>::iterator E, |
| 1807 | unsigned Limit){ |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1808 | // First, check that the current line allows merging. This is the case if |
| 1809 | // we're not in a control flow statement and the last token is an opening |
| 1810 | // brace. |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1811 | AnnotatedLine &Line = *I; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1812 | bool AllowedTokens = |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1813 | Line.First.isNot(tok::kw_if) && Line.First.isNot(tok::kw_while) && |
| 1814 | Line.First.isNot(tok::kw_do) && Line.First.isNot(tok::r_brace) && |
| 1815 | Line.First.isNot(tok::kw_else) && Line.First.isNot(tok::kw_try) && |
| 1816 | Line.First.isNot(tok::kw_catch) && Line.First.isNot(tok::kw_for) && |
Nico Weber | 67015ed | 2013-01-11 21:14:08 +0000 | [diff] [blame] | 1817 | // This gets rid of all ObjC @ keywords and methods. |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1818 | Line.First.isNot(tok::at) && Line.First.isNot(tok::minus) && |
| 1819 | Line.First.isNot(tok::plus); |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1820 | if (!AllowedTokens) |
| 1821 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1822 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1823 | AnnotatedToken *Tok = &(I + 1)->First; |
| 1824 | if (Tok->Children.empty() && Tok->is(tok::r_brace) && |
| 1825 | !Tok->MustBreakBefore && Tok->TotalLength <= Limit) { |
| 1826 | Tok->SpaceRequiredBefore = false; |
| 1827 | join(Line, *(I + 1)); |
| 1828 | I += 1; |
| 1829 | } else { |
| 1830 | // Check that we still have three lines and they fit into the limit. |
| 1831 | if (I + 2 == E || (I + 2)->Type == LT_Invalid || |
| 1832 | !nextTwoLinesFitInto(I, Limit)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1833 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1834 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1835 | // Second, check that the next line does not contain any braces - if it |
| 1836 | // does, readability declines when putting it into a single line. |
| 1837 | if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore) |
| 1838 | return; |
| 1839 | do { |
| 1840 | if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace)) |
| 1841 | return; |
| 1842 | Tok = Tok->Children.empty() ? NULL : &Tok->Children.back(); |
| 1843 | } while (Tok != NULL); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1844 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1845 | // Last, check that the third line contains a single closing brace. |
| 1846 | Tok = &(I + 2)->First; |
| 1847 | if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) || |
| 1848 | Tok->MustBreakBefore) |
| 1849 | return; |
| 1850 | |
| 1851 | join(Line, *(I + 1)); |
| 1852 | join(Line, *(I + 2)); |
| 1853 | I += 2; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1854 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I, |
| 1858 | unsigned Limit) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1859 | return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <= |
| 1860 | Limit; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1863 | void join(AnnotatedLine &A, const AnnotatedLine &B) { |
| 1864 | A.Last->Children.push_back(B.First); |
| 1865 | while (!A.Last->Children.empty()) { |
| 1866 | A.Last->Children[0].Parent = A.Last; |
| 1867 | A.Last = &A.Last->Children[0]; |
| 1868 | } |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1871 | bool touchesRanges(const AnnotatedLine &TheLine) { |
| 1872 | const FormatToken *First = &TheLine.First.FormatTok; |
| 1873 | const FormatToken *Last = &TheLine.Last->FormatTok; |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1874 | CharSourceRange LineRange = CharSourceRange::getTokenRange( |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1875 | First->Tok.getLocation(), |
| 1876 | Last->Tok.getLocation()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1877 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1878 | if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(), |
| 1879 | Ranges[i].getBegin()) && |
| 1880 | !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 1881 | LineRange.getBegin())) |
| 1882 | return true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1883 | } |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1884 | return false; |
| 1885 | } |
| 1886 | |
| 1887 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1888 | AnnotatedLines.push_back(AnnotatedLine(TheLine)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1891 | /// \brief Add a new line and the required indent before the first Token |
| 1892 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 1893 | /// Returns the indent level of the \c UnwrappedLine. |
| 1894 | unsigned formatFirstToken(const AnnotatedToken &RootToken, unsigned Level, |
| 1895 | bool InPPDirective, |
| 1896 | unsigned PreviousEndOfLineColumn) { |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1897 | const FormatToken &Tok = RootToken.FormatTok; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1898 | if (!Tok.WhiteSpaceStart.isValid() || StructuralError) |
| 1899 | return SourceMgr.getSpellingColumnNumber(Tok.Tok.getLocation()) - 1; |
| 1900 | |
| 1901 | unsigned Newlines = std::min(Tok.NewlinesBefore, |
| 1902 | Style.MaxEmptyLinesToKeep + 1); |
| 1903 | if (Newlines == 0 && !Tok.IsFirst) |
| 1904 | Newlines = 1; |
| 1905 | unsigned Indent = Level * 2; |
| 1906 | |
| 1907 | bool IsAccessModifier = false; |
| 1908 | if (RootToken.is(tok::kw_public) || RootToken.is(tok::kw_protected) || |
| 1909 | RootToken.is(tok::kw_private)) |
| 1910 | IsAccessModifier = true; |
| 1911 | else if (RootToken.is(tok::at) && !RootToken.Children.empty() && |
| 1912 | (RootToken.Children[0].isObjCAtKeyword(tok::objc_public) || |
| 1913 | RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) || |
| 1914 | RootToken.Children[0].isObjCAtKeyword(tok::objc_package) || |
| 1915 | RootToken.Children[0].isObjCAtKeyword(tok::objc_private))) |
| 1916 | IsAccessModifier = true; |
| 1917 | |
| 1918 | if (IsAccessModifier && |
| 1919 | static_cast<int>(Indent) + Style.AccessModifierOffset >= 0) |
| 1920 | Indent += Style.AccessModifierOffset; |
| 1921 | if (!InPPDirective || Tok.HasUnescapedNewline) { |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1922 | Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0, Style); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1923 | } else { |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1924 | Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent, |
| 1925 | PreviousEndOfLineColumn, Style); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1926 | } |
| 1927 | return Indent; |
| 1928 | } |
| 1929 | |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1930 | DiagnosticsEngine &Diag; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1931 | FormatStyle Style; |
| 1932 | Lexer &Lex; |
| 1933 | SourceManager &SourceMgr; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1934 | WhitespaceManager Whitespaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1935 | std::vector<CharSourceRange> Ranges; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1936 | std::vector<AnnotatedLine> AnnotatedLines; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1937 | bool StructuralError; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1938 | }; |
| 1939 | |
| 1940 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 1941 | SourceManager &SourceMgr, |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1942 | std::vector<CharSourceRange> Ranges, |
| 1943 | DiagnosticConsumer *DiagClient) { |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1944 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1945 | OwningPtr<DiagnosticConsumer> DiagPrinter; |
| 1946 | if (DiagClient == 0) { |
| 1947 | DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts)); |
| 1948 | DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP()); |
| 1949 | DiagClient = DiagPrinter.get(); |
| 1950 | } |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1951 | DiagnosticsEngine Diagnostics( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1952 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1953 | DiagClient, false); |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1954 | Diagnostics.setSourceManager(&SourceMgr); |
| 1955 | Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1956 | return formatter.format(); |
| 1957 | } |
| 1958 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1959 | LangOptions getFormattingLangOpts() { |
| 1960 | LangOptions LangOpts; |
| 1961 | LangOpts.CPlusPlus = 1; |
| 1962 | LangOpts.CPlusPlus11 = 1; |
| 1963 | LangOpts.Bool = 1; |
| 1964 | LangOpts.ObjC1 = 1; |
| 1965 | LangOpts.ObjC2 = 1; |
| 1966 | return LangOpts; |
| 1967 | } |
| 1968 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1969 | } // namespace format |
| 1970 | } // namespace clang |