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