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