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" |
Daniel Jasper | 675d2e3 | 2012-12-21 10:20:02 +0000 | [diff] [blame] | 21 | #include "clang/Basic/OperatorPrecedence.h" |
Chandler Carruth | b99083e | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Lexer.h" |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 26 | namespace clang { |
| 27 | namespace format { |
| 28 | |
| 29 | // FIXME: Move somewhere sane. |
| 30 | struct TokenAnnotation { |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 31 | enum TokenType { |
| 32 | TT_Unknown, |
| 33 | TT_TemplateOpener, |
| 34 | TT_TemplateCloser, |
| 35 | TT_BinaryOperator, |
| 36 | TT_UnaryOperator, |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 37 | TT_TrailingUnaryOperator, |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 38 | TT_OverloadedOperator, |
| 39 | TT_PointerOrReference, |
| 40 | TT_ConditionalExpr, |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 41 | TT_CtorInitializerColon, |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 42 | TT_LineComment, |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 43 | TT_BlockComment, |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 44 | TT_DirectorySeparator, |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 45 | TT_ObjCMethodSpecifier |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 46 | }; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 47 | |
| 48 | TokenType Type; |
| 49 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 50 | bool SpaceRequiredBefore; |
| 51 | bool CanBreakBefore; |
| 52 | bool MustBreakBefore; |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 53 | |
| 54 | bool ClosesTemplateDeclaration; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 57 | static prec::Level getPrecedence(const FormatToken &Tok) { |
| 58 | return getBinOpPrecedence(Tok.Tok.getKind(), true, true); |
| 59 | } |
| 60 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 61 | using llvm::MutableArrayRef; |
| 62 | |
| 63 | FormatStyle getLLVMStyle() { |
| 64 | FormatStyle LLVMStyle; |
| 65 | LLVMStyle.ColumnLimit = 80; |
| 66 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
| 67 | LLVMStyle.PointerAndReferenceBindToType = false; |
| 68 | LLVMStyle.AccessModifierOffset = -2; |
| 69 | LLVMStyle.SplitTemplateClosingGreater = true; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 70 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 71 | return LLVMStyle; |
| 72 | } |
| 73 | |
| 74 | FormatStyle getGoogleStyle() { |
| 75 | FormatStyle GoogleStyle; |
| 76 | GoogleStyle.ColumnLimit = 80; |
| 77 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
| 78 | GoogleStyle.PointerAndReferenceBindToType = true; |
| 79 | GoogleStyle.AccessModifierOffset = -1; |
| 80 | GoogleStyle.SplitTemplateClosingGreater = false; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 81 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 82 | return GoogleStyle; |
| 83 | } |
| 84 | |
| 85 | struct OptimizationParameters { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 86 | unsigned PenaltyIndentLevel; |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 87 | unsigned PenaltyLevelDecrease; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | class UnwrappedLineFormatter { |
| 91 | public: |
| 92 | UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, |
| 93 | const UnwrappedLine &Line, |
| 94 | const std::vector<TokenAnnotation> &Annotations, |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 95 | tooling::Replacements &Replaces, bool StructuralError) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 96 | : Style(Style), SourceMgr(SourceMgr), Line(Line), |
| 97 | Annotations(Annotations), Replaces(Replaces), |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 98 | StructuralError(StructuralError) { |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 99 | Parameters.PenaltyIndentLevel = 15; |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 100 | Parameters.PenaltyLevelDecrease = 10; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void format() { |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 104 | // Format first token and initialize indent. |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 105 | unsigned Indent = formatFirstToken(); |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 106 | |
| 107 | // Initialize state dependent on indent. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 108 | IndentState State; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 109 | State.Column = Indent; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 110 | State.ConsumedTokens = 0; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 111 | State.Indent.push_back(Indent + 4); |
| 112 | State.LastSpace.push_back(Indent); |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 113 | State.FirstLessLess.push_back(0); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 114 | State.ForLoopVariablePos = 0; |
| 115 | State.LineContainsContinuedForLoopSection = false; |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 116 | State.StartOfLineLevel = 1; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 117 | |
| 118 | // The first token has already been indented and thus consumed. |
| 119 | moveStateToNextToken(State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 120 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 121 | // Check whether the UnwrappedLine can be put onto a single line. If so, |
| 122 | // this is bound to be the optimal solution (by definition) and we don't |
| 123 | // need to analyze the entire solution space. |
| 124 | unsigned Columns = State.Column; |
| 125 | bool FitsOnALine = true; |
| 126 | for (unsigned i = 1, n = Line.Tokens.size(); i != n; ++i) { |
| 127 | Columns += (Annotations[i].SpaceRequiredBefore ? 1 : 0) + |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 128 | Line.Tokens[i].Tok.getLength(); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 129 | // A special case for the colon of a constructor initializer as this only |
| 130 | // needs to be put on a new line if the line needs to be split. |
| 131 | if (Columns > Style.ColumnLimit || |
| 132 | (Annotations[i].MustBreakBefore && |
| 133 | Annotations[i].Type != TokenAnnotation::TT_CtorInitializerColon)) { |
| 134 | FitsOnALine = false; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 139 | // Start iterating at 1 as we have correctly formatted of Token #0 above. |
| 140 | for (unsigned i = 1, n = Line.Tokens.size(); i != n; ++i) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 141 | if (FitsOnALine) { |
| 142 | addTokenToState(false, false, State); |
| 143 | } else { |
| 144 | unsigned NoBreak = calcPenalty(State, false, UINT_MAX); |
| 145 | unsigned Break = calcPenalty(State, true, NoBreak); |
| 146 | addTokenToState(Break < NoBreak, false, State); |
| 147 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | /// \brief The current state when indenting a unwrapped line. |
| 153 | /// |
| 154 | /// As the indenting tries different combinations this is copied by value. |
| 155 | struct IndentState { |
| 156 | /// \brief The number of used columns in the current line. |
| 157 | unsigned Column; |
| 158 | |
| 159 | /// \brief The number of tokens already consumed. |
| 160 | unsigned ConsumedTokens; |
| 161 | |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 162 | /// \brief The parenthesis level of the first token on the current line. |
| 163 | unsigned StartOfLineLevel; |
| 164 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 165 | /// \brief The position to which a specific parenthesis level needs to be |
| 166 | /// indented. |
| 167 | std::vector<unsigned> Indent; |
| 168 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 169 | /// \brief The position of the last space on each level. |
| 170 | /// |
| 171 | /// Used e.g. to break like: |
| 172 | /// functionCall(Parameter, otherCall( |
| 173 | /// OtherParameter)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 174 | std::vector<unsigned> LastSpace; |
| 175 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 176 | /// \brief The position the first "<<" operator encountered on each level. |
| 177 | /// |
| 178 | /// Used to align "<<" operators. 0 if no such operator has been encountered |
| 179 | /// on a level. |
| 180 | std::vector<unsigned> FirstLessLess; |
| 181 | |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 182 | /// \brief The column of the first variable in a for-loop declaration. |
| 183 | /// |
| 184 | /// Used to align the second variable if necessary. |
| 185 | unsigned ForLoopVariablePos; |
| 186 | |
| 187 | /// \brief \c true if this line contains a continued for-loop section. |
| 188 | bool LineContainsContinuedForLoopSection; |
| 189 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 190 | /// \brief Comparison operator to be able to used \c IndentState in \c map. |
| 191 | bool operator<(const IndentState &Other) const { |
| 192 | if (Other.ConsumedTokens != ConsumedTokens) |
| 193 | return Other.ConsumedTokens > ConsumedTokens; |
| 194 | if (Other.Column != Column) |
| 195 | return Other.Column > Column; |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 196 | if (Other.StartOfLineLevel != StartOfLineLevel) |
| 197 | return Other.StartOfLineLevel > StartOfLineLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 198 | if (Other.Indent.size() != Indent.size()) |
| 199 | return Other.Indent.size() > Indent.size(); |
| 200 | for (int i = 0, e = Indent.size(); i != e; ++i) { |
| 201 | if (Other.Indent[i] != Indent[i]) |
| 202 | return Other.Indent[i] > Indent[i]; |
| 203 | } |
| 204 | if (Other.LastSpace.size() != LastSpace.size()) |
| 205 | return Other.LastSpace.size() > LastSpace.size(); |
| 206 | for (int i = 0, e = LastSpace.size(); i != e; ++i) { |
| 207 | if (Other.LastSpace[i] != LastSpace[i]) |
| 208 | return Other.LastSpace[i] > LastSpace[i]; |
| 209 | } |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 210 | if (Other.FirstLessLess.size() != FirstLessLess.size()) |
| 211 | return Other.FirstLessLess.size() > FirstLessLess.size(); |
| 212 | for (int i = 0, e = FirstLessLess.size(); i != e; ++i) { |
| 213 | if (Other.FirstLessLess[i] != FirstLessLess[i]) |
| 214 | return Other.FirstLessLess[i] > FirstLessLess[i]; |
| 215 | } |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 216 | if (Other.ForLoopVariablePos != ForLoopVariablePos) |
| 217 | return Other.ForLoopVariablePos < ForLoopVariablePos; |
| 218 | if (Other.LineContainsContinuedForLoopSection != |
| 219 | LineContainsContinuedForLoopSection) |
| 220 | return LineContainsContinuedForLoopSection; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | }; |
| 224 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 225 | /// \brief Appends the next token to \p State and updates information |
| 226 | /// necessary for indentation. |
| 227 | /// |
| 228 | /// Puts the token on the current line if \p Newline is \c true and adds a |
| 229 | /// line break and necessary indentation otherwise. |
| 230 | /// |
| 231 | /// If \p DryRun is \c false, also creates and stores the required |
| 232 | /// \c Replacement. |
| 233 | void addTokenToState(bool Newline, bool DryRun, IndentState &State) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 234 | unsigned Index = State.ConsumedTokens; |
| 235 | const FormatToken &Current = Line.Tokens[Index]; |
| 236 | const FormatToken &Previous = Line.Tokens[Index - 1]; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 237 | unsigned ParenLevel = State.Indent.size() - 1; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 238 | |
| 239 | if (Newline) { |
| 240 | if (Current.Tok.is(tok::string_literal) && |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 241 | Previous.Tok.is(tok::string_literal)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 242 | State.Column = State.Column - Previous.Tok.getLength(); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 243 | } else if (Current.Tok.is(tok::lessless) && |
| 244 | State.FirstLessLess[ParenLevel] != 0) { |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 245 | State.Column = State.FirstLessLess[ParenLevel]; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 246 | } else if (ParenLevel != 0 && |
| 247 | (Previous.Tok.is(tok::equal) || Current.Tok.is(tok::arrow) || |
| 248 | Current.Tok.is(tok::period))) { |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 249 | // Indent and extra 4 spaces after '=' as it continues an expression. |
| 250 | // Don't do that on the top level, as we already indent 4 there. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 251 | State.Column = State.Indent[ParenLevel] + 4; |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 252 | } else if ( |
| 253 | Line.Tokens[0].Tok.is(tok::kw_for) && Previous.Tok.is(tok::comma)) { |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 254 | State.Column = State.ForLoopVariablePos; |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 255 | } else if (Annotations[Index - 1].ClosesTemplateDeclaration) { |
| 256 | State.Column = State.Indent[ParenLevel] - 4; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 257 | } else { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 258 | State.Column = State.Indent[ParenLevel]; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 261 | State.StartOfLineLevel = ParenLevel + 1; |
| 262 | |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 263 | if (Line.Tokens[0].Tok.is(tok::kw_for)) |
| 264 | State.LineContainsContinuedForLoopSection = |
| 265 | Previous.Tok.isNot(tok::semi); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 266 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 267 | if (!DryRun) |
| 268 | replaceWhitespace(Current, 1, State.Column); |
| 269 | |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 270 | State.LastSpace[ParenLevel] = State.Indent[ParenLevel]; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 271 | if (Current.Tok.is(tok::colon) && |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 272 | Annotations[Index].Type != TokenAnnotation::TT_ConditionalExpr && |
| 273 | Annotations[0].Type != TokenAnnotation::TT_ObjCMethodSpecifier) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 274 | State.Indent[ParenLevel] += 2; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 275 | } else { |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 276 | if (Current.Tok.is(tok::equal) && Line.Tokens[0].Tok.is(tok::kw_for)) |
| 277 | State.ForLoopVariablePos = State.Column - Previous.Tok.getLength(); |
| 278 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 279 | unsigned Spaces = Annotations[Index].SpaceRequiredBefore ? 1 : 0; |
| 280 | if (Annotations[Index].Type == TokenAnnotation::TT_LineComment) |
| 281 | Spaces = 2; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 282 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 283 | if (!DryRun) |
| 284 | replaceWhitespace(Current, 0, Spaces); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 285 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 286 | // FIXME: Look into using this alignment at other ParenLevels. |
| 287 | if (ParenLevel == 0 && (getPrecedence(Previous) == prec::Assignment || |
| 288 | Previous.Tok.is(tok::kw_return))) |
| 289 | State.Indent[ParenLevel] = State.Column + Spaces; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 290 | if (Previous.Tok.is(tok::l_paren) || |
| 291 | Annotations[Index - 1].Type == TokenAnnotation::TT_TemplateOpener) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 292 | State.Indent[ParenLevel] = State.Column; |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 293 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 294 | // Top-level spaces are exempt as that mostly leads to better results. |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 295 | State.Column += Spaces; |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 296 | if (Spaces > 0 && ParenLevel != 0) |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 297 | State.LastSpace[ParenLevel] = State.Column; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 298 | } |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 299 | moveStateToNextToken(State); |
| 300 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 301 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 302 | /// \brief Mark the next token as consumed in \p State and modify its stacks |
| 303 | /// accordingly. |
| 304 | void moveStateToNextToken(IndentState &State) { |
| 305 | unsigned Index = State.ConsumedTokens; |
| 306 | const FormatToken &Current = Line.Tokens[Index]; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 307 | unsigned ParenLevel = State.Indent.size() - 1; |
| 308 | |
| 309 | if (Current.Tok.is(tok::lessless) && State.FirstLessLess[ParenLevel] == 0) |
| 310 | State.FirstLessLess[ParenLevel] = State.Column; |
| 311 | |
| 312 | State.Column += Current.Tok.getLength(); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 313 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 314 | // 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] | 315 | // prepare for the following tokens. |
| 316 | if (Current.Tok.is(tok::l_paren) || Current.Tok.is(tok::l_square) || |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 317 | Current.Tok.is(tok::l_brace) || |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 318 | Annotations[Index].Type == TokenAnnotation::TT_TemplateOpener) { |
| 319 | State.Indent.push_back(4 + State.LastSpace.back()); |
| 320 | State.LastSpace.push_back(State.LastSpace.back()); |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 321 | State.FirstLessLess.push_back(0); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 324 | // 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] | 325 | // stacks. |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 326 | if (Current.Tok.is(tok::r_paren) || Current.Tok.is(tok::r_square) || |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 327 | (Current.Tok.is(tok::r_brace) && State.ConsumedTokens > 0) || |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 328 | Annotations[Index].Type == TokenAnnotation::TT_TemplateCloser) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 329 | State.Indent.pop_back(); |
| 330 | State.LastSpace.pop_back(); |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 331 | State.FirstLessLess.pop_back(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | ++State.ConsumedTokens; |
| 335 | } |
| 336 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 337 | /// \brief Calculate the panelty for splitting after the token at \p Index. |
| 338 | unsigned splitPenalty(unsigned Index) { |
| 339 | assert(Index < Line.Tokens.size() && |
| 340 | "Tried to calculate penalty for splitting after the last token"); |
| 341 | const FormatToken &Left = Line.Tokens[Index]; |
| 342 | const FormatToken &Right = Line.Tokens[Index + 1]; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 343 | |
| 344 | // In for-loops, prefer breaking at ',' and ';'. |
| 345 | if (Line.Tokens[0].Tok.is(tok::kw_for) && |
| 346 | (Left.Tok.isNot(tok::comma) && Left.Tok.isNot(tok::semi))) |
| 347 | return 20; |
| 348 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 349 | if (Left.Tok.is(tok::semi) || Left.Tok.is(tok::comma) || |
| 350 | Annotations[Index].ClosesTemplateDeclaration) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 351 | return 0; |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 352 | if (Left.Tok.is(tok::l_paren)) |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 353 | return 20; |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 354 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 355 | prec::Level Level = getPrecedence(Line.Tokens[Index]); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 356 | if (Level != prec::Unknown) |
| 357 | return Level; |
| 358 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 359 | if (Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period)) |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 360 | return 50; |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 361 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 362 | return 3; |
| 363 | } |
| 364 | |
| 365 | /// \brief Calculate the number of lines needed to format the remaining part |
| 366 | /// of the unwrapped line. |
| 367 | /// |
| 368 | /// Assumes the formatting so far has led to |
| 369 | /// the \c IndentState \p State. If \p NewLine is set, a new line will be |
| 370 | /// added after the previous token. |
| 371 | /// |
| 372 | /// \param StopAt is used for optimization. If we can determine that we'll |
| 373 | /// definitely need at least \p StopAt additional lines, we already know of a |
| 374 | /// better solution. |
| 375 | unsigned calcPenalty(IndentState State, bool NewLine, unsigned StopAt) { |
| 376 | // We are at the end of the unwrapped line, so we don't need any more lines. |
| 377 | if (State.ConsumedTokens >= Line.Tokens.size()) |
| 378 | return 0; |
| 379 | |
| 380 | if (!NewLine && Annotations[State.ConsumedTokens].MustBreakBefore) |
| 381 | return UINT_MAX; |
| 382 | if (NewLine && !Annotations[State.ConsumedTokens].CanBreakBefore) |
| 383 | return UINT_MAX; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 384 | if (!NewLine && Line.Tokens[State.ConsumedTokens - 1].Tok.is(tok::semi) && |
| 385 | State.LineContainsContinuedForLoopSection) |
| 386 | return UINT_MAX; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 387 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 388 | unsigned CurrentPenalty = 0; |
| 389 | if (NewLine) { |
| 390 | CurrentPenalty += Parameters.PenaltyIndentLevel * State.Indent.size() + |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 391 | splitPenalty(State.ConsumedTokens - 1); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 392 | } else { |
| 393 | if (State.Indent.size() < State.StartOfLineLevel) |
| 394 | CurrentPenalty += Parameters.PenaltyLevelDecrease * |
| 395 | (State.StartOfLineLevel - State.Indent.size()); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 398 | addTokenToState(NewLine, true, State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 399 | |
| 400 | // Exceeding column limit is bad. |
| 401 | if (State.Column > Style.ColumnLimit) |
| 402 | return UINT_MAX; |
| 403 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 404 | if (StopAt <= CurrentPenalty) |
| 405 | return UINT_MAX; |
| 406 | StopAt -= CurrentPenalty; |
| 407 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 408 | StateMap::iterator I = Memory.find(State); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 409 | if (I != Memory.end()) { |
| 410 | // If this state has already been examined, we can safely return the |
| 411 | // previous result if we |
| 412 | // - have not hit the optimatization (and thus returned UINT_MAX) OR |
| 413 | // - are now computing for a smaller or equal StopAt. |
| 414 | unsigned SavedResult = I->second.first; |
| 415 | unsigned SavedStopAt = I->second.second; |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 416 | if (SavedResult != UINT_MAX) |
| 417 | return SavedResult + CurrentPenalty; |
| 418 | else if (StopAt <= SavedStopAt) |
| 419 | return UINT_MAX; |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 420 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 421 | |
| 422 | unsigned NoBreak = calcPenalty(State, false, StopAt); |
| 423 | unsigned WithBreak = calcPenalty(State, true, std::min(StopAt, NoBreak)); |
| 424 | unsigned Result = std::min(NoBreak, WithBreak); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 425 | |
| 426 | // We have to store 'Result' without adding 'CurrentPenalty' as the latter |
| 427 | // can depend on 'NewLine'. |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 428 | Memory[State] = std::pair<unsigned, unsigned>(Result, StopAt); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 429 | |
| 430 | return Result == UINT_MAX ? UINT_MAX : Result + CurrentPenalty; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /// \brief Replaces the whitespace in front of \p Tok. Only call once for |
| 434 | /// each \c FormatToken. |
| 435 | void replaceWhitespace(const FormatToken &Tok, unsigned NewLines, |
| 436 | unsigned Spaces) { |
| 437 | Replaces.insert(tooling::Replacement( |
| 438 | SourceMgr, Tok.WhiteSpaceStart, Tok.WhiteSpaceLength, |
| 439 | std::string(NewLines, '\n') + std::string(Spaces, ' '))); |
| 440 | } |
| 441 | |
| 442 | /// \brief Add a new line and the required indent before the first Token |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 443 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 444 | /// Returns the indent level of the \c UnwrappedLine. |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 445 | unsigned formatFirstToken() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 446 | const FormatToken &Token = Line.Tokens[0]; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 447 | if (!Token.WhiteSpaceStart.isValid() || StructuralError) |
| 448 | return SourceMgr.getSpellingColumnNumber(Token.Tok.getLocation()) - 1; |
| 449 | |
| 450 | unsigned Newlines = |
| 451 | std::min(Token.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); |
| 452 | unsigned Offset = SourceMgr.getFileOffset(Token.WhiteSpaceStart); |
| 453 | if (Newlines == 0 && Offset != 0) |
| 454 | Newlines = 1; |
| 455 | unsigned Indent = Line.Level * 2; |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 456 | if ((Token.Tok.is(tok::kw_public) || Token.Tok.is(tok::kw_protected) || |
| 457 | Token.Tok.is(tok::kw_private)) && |
| 458 | static_cast<int>(Indent) + Style.AccessModifierOffset >= 0) |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 459 | Indent += Style.AccessModifierOffset; |
| 460 | replaceWhitespace(Token, Newlines, Indent); |
| 461 | return Indent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | FormatStyle Style; |
| 465 | SourceManager &SourceMgr; |
| 466 | const UnwrappedLine &Line; |
| 467 | const std::vector<TokenAnnotation> &Annotations; |
| 468 | tooling::Replacements &Replaces; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 469 | bool StructuralError; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 470 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 471 | // A map from an indent state to a pair (Result, Used-StopAt). |
| 472 | typedef std::map<IndentState, std::pair<unsigned, unsigned> > StateMap; |
| 473 | StateMap Memory; |
| 474 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 475 | OptimizationParameters Parameters; |
| 476 | }; |
| 477 | |
| 478 | /// \brief Determines extra information about the tokens comprising an |
| 479 | /// \c UnwrappedLine. |
| 480 | class TokenAnnotator { |
| 481 | public: |
| 482 | TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style, |
| 483 | SourceManager &SourceMgr) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 484 | : Line(Line), Style(Style), SourceMgr(SourceMgr) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /// \brief A parser that gathers additional information about tokens. |
| 488 | /// |
| 489 | /// The \c TokenAnnotator tries to matches parenthesis and square brakets and |
| 490 | /// store a parenthesis levels. It also tries to resolve matching "<" and ">" |
| 491 | /// into template parameter lists. |
| 492 | class AnnotatingParser { |
| 493 | public: |
Manuel Klimek | 0be4b36 | 2012-12-03 20:55:42 +0000 | [diff] [blame] | 494 | AnnotatingParser(const SmallVector<FormatToken, 16> &Tokens, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 495 | std::vector<TokenAnnotation> &Annotations) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 496 | : Tokens(Tokens), Annotations(Annotations), Index(0) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 499 | bool parseAngle() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 500 | while (Index < Tokens.size()) { |
| 501 | if (Tokens[Index].Tok.is(tok::greater)) { |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 502 | Annotations[Index].Type = TokenAnnotation::TT_TemplateCloser; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 503 | next(); |
| 504 | return true; |
| 505 | } |
| 506 | if (Tokens[Index].Tok.is(tok::r_paren) || |
| 507 | Tokens[Index].Tok.is(tok::r_square)) |
| 508 | return false; |
| 509 | if (Tokens[Index].Tok.is(tok::pipepipe) || |
| 510 | Tokens[Index].Tok.is(tok::ampamp) || |
| 511 | Tokens[Index].Tok.is(tok::question) || |
| 512 | Tokens[Index].Tok.is(tok::colon)) |
| 513 | return false; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 514 | consumeToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 515 | } |
| 516 | return false; |
| 517 | } |
| 518 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 519 | bool parseParens() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 520 | while (Index < Tokens.size()) { |
| 521 | if (Tokens[Index].Tok.is(tok::r_paren)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 522 | next(); |
| 523 | return true; |
| 524 | } |
| 525 | if (Tokens[Index].Tok.is(tok::r_square)) |
| 526 | return false; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 527 | consumeToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 528 | } |
| 529 | return false; |
| 530 | } |
| 531 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 532 | bool parseSquare() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 533 | while (Index < Tokens.size()) { |
| 534 | if (Tokens[Index].Tok.is(tok::r_square)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 535 | next(); |
| 536 | return true; |
| 537 | } |
| 538 | if (Tokens[Index].Tok.is(tok::r_paren)) |
| 539 | return false; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 540 | consumeToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 541 | } |
| 542 | return false; |
| 543 | } |
| 544 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 545 | bool parseConditional() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 546 | while (Index < Tokens.size()) { |
| 547 | if (Tokens[Index].Tok.is(tok::colon)) { |
| 548 | Annotations[Index].Type = TokenAnnotation::TT_ConditionalExpr; |
| 549 | next(); |
| 550 | return true; |
| 551 | } |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 552 | consumeToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 553 | } |
| 554 | return false; |
| 555 | } |
| 556 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 557 | bool parseTemplateDeclaration() { |
| 558 | if (Index < Tokens.size() && Tokens[Index].Tok.is(tok::less)) { |
| 559 | Annotations[Index].Type = TokenAnnotation::TT_TemplateOpener; |
| 560 | next(); |
| 561 | if (!parseAngle()) |
| 562 | return false; |
| 563 | Annotations[Index - 1].ClosesTemplateDeclaration = true; |
| 564 | parseLine(); |
| 565 | return true; |
| 566 | } |
| 567 | return false; |
| 568 | } |
| 569 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 570 | void consumeToken() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 571 | unsigned CurrentIndex = Index; |
| 572 | next(); |
| 573 | switch (Tokens[CurrentIndex].Tok.getKind()) { |
| 574 | case tok::l_paren: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 575 | parseParens(); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 576 | if (Index < Tokens.size() && Tokens[Index].Tok.is(tok::colon)) { |
| 577 | Annotations[Index].Type = TokenAnnotation::TT_CtorInitializerColon; |
| 578 | next(); |
| 579 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 580 | break; |
| 581 | case tok::l_square: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 582 | parseSquare(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 583 | break; |
| 584 | case tok::less: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 585 | if (parseAngle()) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 586 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_TemplateOpener; |
| 587 | else { |
| 588 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator; |
| 589 | Index = CurrentIndex + 1; |
| 590 | } |
| 591 | break; |
| 592 | case tok::greater: |
| 593 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator; |
| 594 | break; |
| 595 | case tok::kw_operator: |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 596 | if (Tokens[Index].Tok.is(tok::l_paren)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 597 | Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator; |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 598 | next(); |
| 599 | if (Index < Tokens.size() && Tokens[Index].Tok.is(tok::r_paren)) { |
| 600 | Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator; |
| 601 | next(); |
| 602 | } |
| 603 | } else { |
| 604 | while (Index < Tokens.size() && !Tokens[Index].Tok.is(tok::l_paren)) { |
| 605 | Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator; |
| 606 | next(); |
| 607 | } |
| 608 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 609 | break; |
| 610 | case tok::question: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 611 | parseConditional(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 612 | break; |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 613 | case tok::kw_template: |
| 614 | parseTemplateDeclaration(); |
| 615 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 616 | default: |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 621 | void parseIncludeDirective() { |
| 622 | while (Index < Tokens.size()) { |
| 623 | if (Tokens[Index].Tok.is(tok::slash)) |
| 624 | Annotations[Index].Type = TokenAnnotation::TT_DirectorySeparator; |
| 625 | else if (Tokens[Index].Tok.is(tok::less)) |
| 626 | Annotations[Index].Type = TokenAnnotation::TT_TemplateOpener; |
| 627 | else if (Tokens[Index].Tok.is(tok::greater)) |
| 628 | Annotations[Index].Type = TokenAnnotation::TT_TemplateCloser; |
| 629 | next(); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | void parsePreprocessorDirective() { |
| 634 | next(); |
| 635 | if (Index >= Tokens.size()) |
| 636 | return; |
| 637 | switch (Tokens[Index].Tok.getIdentifierInfo()->getPPKeywordID()) { |
| 638 | case tok::pp_include: |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 639 | case tok::pp_import: |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 640 | parseIncludeDirective(); |
| 641 | break; |
| 642 | default: |
| 643 | break; |
| 644 | } |
| 645 | } |
| 646 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 647 | void parseLine() { |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 648 | if (Tokens[Index].Tok.is(tok::hash)) { |
| 649 | parsePreprocessorDirective(); |
| 650 | return; |
| 651 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 652 | while (Index < Tokens.size()) { |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 653 | consumeToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
| 657 | void next() { |
| 658 | ++Index; |
| 659 | } |
| 660 | |
| 661 | private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 662 | const SmallVector<FormatToken, 16> &Tokens; |
| 663 | std::vector<TokenAnnotation> &Annotations; |
| 664 | unsigned Index; |
| 665 | }; |
| 666 | |
| 667 | void annotate() { |
| 668 | Annotations.clear(); |
| 669 | for (int i = 0, e = Line.Tokens.size(); i != e; ++i) { |
| 670 | Annotations.push_back(TokenAnnotation()); |
| 671 | } |
| 672 | |
Manuel Klimek | 0be4b36 | 2012-12-03 20:55:42 +0000 | [diff] [blame] | 673 | AnnotatingParser Parser(Line.Tokens, Annotations); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 674 | Parser.parseLine(); |
| 675 | |
| 676 | determineTokenTypes(); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 677 | bool IsObjCMethodDecl = |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 678 | (Line.Tokens.size() > 0 && |
| 679 | (Annotations[0].Type == TokenAnnotation::TT_ObjCMethodSpecifier)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 680 | for (int i = 1, e = Line.Tokens.size(); i != e; ++i) { |
| 681 | TokenAnnotation &Annotation = Annotations[i]; |
| 682 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 683 | Annotation.CanBreakBefore = canBreakBefore(i); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 684 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 685 | if (Annotation.Type == TokenAnnotation::TT_CtorInitializerColon) { |
| 686 | Annotation.MustBreakBefore = true; |
| 687 | Annotation.SpaceRequiredBefore = true; |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 688 | } else if (Annotation.Type == TokenAnnotation::TT_OverloadedOperator) { |
| 689 | Annotation.SpaceRequiredBefore = |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 690 | Line.Tokens[i].Tok.is(tok::identifier) || |
| 691 | Line.Tokens[i].Tok.is(tok::kw_new) || |
| 692 | Line.Tokens[i].Tok.is(tok::kw_delete); |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 693 | } else if ( |
| 694 | Annotations[i - 1].Type == TokenAnnotation::TT_OverloadedOperator) { |
| 695 | Annotation.SpaceRequiredBefore = false; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 696 | } else if (IsObjCMethodDecl && Line.Tokens[i].Tok.is(tok::identifier) && |
| 697 | (i != e - 1) && Line.Tokens[i + 1].Tok.is(tok::colon) && |
| 698 | Line.Tokens[i - 1].Tok.is(tok::identifier)) { |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 699 | Annotation.CanBreakBefore = true; |
| 700 | Annotation.SpaceRequiredBefore = true; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 701 | } else if (IsObjCMethodDecl && Line.Tokens[i].Tok.is(tok::identifier) && |
| 702 | Line.Tokens[i - 1].Tok.is(tok::l_paren) && |
| 703 | Line.Tokens[i - 2].Tok.is(tok::colon)) { |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 704 | // Don't break this identifier as ':' or identifier |
| 705 | // before it will break. |
| 706 | Annotation.CanBreakBefore = false; |
| 707 | } else if (Line.Tokens[i].Tok.is(tok::at) && |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 708 | Line.Tokens[i - 2].Tok.is(tok::at)) { |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 709 | // Don't put two objc's '@' on the same line. This could happen, |
Fariborz Jahanian | 5f04ef5 | 2012-12-21 17:14:23 +0000 | [diff] [blame] | 710 | // as in, @optional @property ... |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 711 | Annotation.MustBreakBefore = true; |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 712 | } else if (Line.Tokens[i].Tok.is(tok::colon)) { |
Daniel Jasper | dfbb319 | 2012-12-05 16:24:48 +0000 | [diff] [blame] | 713 | Annotation.SpaceRequiredBefore = |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 714 | Line.Tokens[0].Tok.isNot(tok::kw_case) && !IsObjCMethodDecl && |
| 715 | (i != e - 1); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 716 | // Don't break at ':' if identifier before it can beak. |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 717 | if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::identifier) && |
| 718 | Annotations[i - 1].CanBreakBefore) |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 719 | Annotation.CanBreakBefore = false; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 720 | } else if ( |
| 721 | Annotations[i - 1].Type == TokenAnnotation::TT_ObjCMethodSpecifier) { |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 722 | Annotation.SpaceRequiredBefore = true; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 723 | } else if (Annotations[i - 1].Type == TokenAnnotation::TT_UnaryOperator) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 724 | Annotation.SpaceRequiredBefore = false; |
| 725 | } else if (Annotation.Type == TokenAnnotation::TT_UnaryOperator) { |
| 726 | Annotation.SpaceRequiredBefore = |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 727 | Line.Tokens[i - 1].Tok.isNot(tok::l_paren) && |
| 728 | Line.Tokens[i - 1].Tok.isNot(tok::l_square); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 729 | } else if (Line.Tokens[i - 1].Tok.is(tok::greater) && |
| 730 | Line.Tokens[i].Tok.is(tok::greater)) { |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 731 | if (Annotation.Type == TokenAnnotation::TT_TemplateCloser && |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 732 | Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser) |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 733 | Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 734 | else |
| 735 | Annotation.SpaceRequiredBefore = false; |
| 736 | } else if ( |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 737 | Annotation.Type == TokenAnnotation::TT_DirectorySeparator || |
| 738 | Annotations[i - 1].Type == TokenAnnotation::TT_DirectorySeparator) { |
| 739 | Annotation.SpaceRequiredBefore = false; |
| 740 | } else if ( |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 741 | Annotation.Type == TokenAnnotation::TT_BinaryOperator || |
| 742 | Annotations[i - 1].Type == TokenAnnotation::TT_BinaryOperator) { |
| 743 | Annotation.SpaceRequiredBefore = true; |
| 744 | } else if ( |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 745 | Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser && |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 746 | Line.Tokens[i].Tok.is(tok::l_paren)) { |
| 747 | Annotation.SpaceRequiredBefore = false; |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 748 | } else if (Line.Tokens[i].Tok.is(tok::less) && |
| 749 | Line.Tokens[0].Tok.is(tok::hash)) { |
| 750 | Annotation.SpaceRequiredBefore = true; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 751 | } else if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::r_paren) && |
| 752 | Line.Tokens[i].Tok.is(tok::identifier)) { |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 753 | // Don't space between ')' and <id> |
| 754 | Annotation.SpaceRequiredBefore = false; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 755 | } else if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::colon) && |
| 756 | Line.Tokens[i].Tok.is(tok::l_paren)) { |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 757 | // Don't space between ':' and '(' |
| 758 | Annotation.SpaceRequiredBefore = false; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 759 | } else if (Annotation.Type == TokenAnnotation::TT_TrailingUnaryOperator) { |
| 760 | Annotation.SpaceRequiredBefore = false; |
| 761 | } else { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 762 | Annotation.SpaceRequiredBefore = |
| 763 | spaceRequiredBetween(Line.Tokens[i - 1].Tok, Line.Tokens[i].Tok); |
| 764 | } |
| 765 | |
| 766 | if (Annotations[i - 1].Type == TokenAnnotation::TT_LineComment || |
| 767 | (Line.Tokens[i].Tok.is(tok::string_literal) && |
| 768 | Line.Tokens[i - 1].Tok.is(tok::string_literal))) { |
| 769 | Annotation.MustBreakBefore = true; |
| 770 | } |
| 771 | |
| 772 | if (Annotation.MustBreakBefore) |
| 773 | Annotation.CanBreakBefore = true; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | const std::vector<TokenAnnotation> &getAnnotations() { |
| 778 | return Annotations; |
| 779 | } |
| 780 | |
| 781 | private: |
| 782 | void determineTokenTypes() { |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 783 | bool IsRHS = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 784 | for (int i = 0, e = Line.Tokens.size(); i != e; ++i) { |
| 785 | TokenAnnotation &Annotation = Annotations[i]; |
| 786 | const FormatToken &Tok = Line.Tokens[i]; |
| 787 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 788 | if (getPrecedence(Tok) == prec::Assignment) |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 789 | IsRHS = true; |
| 790 | else if (Tok.Tok.is(tok::kw_return)) |
| 791 | IsRHS = true; |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 792 | |
Daniel Jasper | 675d2e3 | 2012-12-21 10:20:02 +0000 | [diff] [blame] | 793 | if (Annotation.Type != TokenAnnotation::TT_Unknown) |
| 794 | continue; |
| 795 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 796 | if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp)) { |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 797 | Annotation.Type = determineStarAmpUsage(i, IsRHS); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 798 | } else if (Tok.Tok.is(tok::minus) || Tok.Tok.is(tok::plus)) { |
| 799 | Annotation.Type = determinePlusMinusUsage(i); |
| 800 | } else if (Tok.Tok.is(tok::minusminus) || Tok.Tok.is(tok::plusplus)) { |
| 801 | Annotation.Type = determineIncrementUsage(i); |
| 802 | } else if (Tok.Tok.is(tok::exclaim)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 803 | Annotation.Type = TokenAnnotation::TT_UnaryOperator; |
Daniel Jasper | 675d2e3 | 2012-12-21 10:20:02 +0000 | [diff] [blame] | 804 | } else if (isBinaryOperator(Line.Tokens[i])) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 805 | Annotation.Type = TokenAnnotation::TT_BinaryOperator; |
Daniel Jasper | 675d2e3 | 2012-12-21 10:20:02 +0000 | [diff] [blame] | 806 | } else if (Tok.Tok.is(tok::comment)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 807 | StringRef Data(SourceMgr.getCharacterData(Tok.Tok.getLocation()), |
| 808 | Tok.Tok.getLength()); |
| 809 | if (Data.startswith("//")) |
| 810 | Annotation.Type = TokenAnnotation::TT_LineComment; |
| 811 | else |
| 812 | Annotation.Type = TokenAnnotation::TT_BlockComment; |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 817 | bool isBinaryOperator(const FormatToken &Tok) { |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 818 | // 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] | 819 | return getPrecedence(Tok) > prec::Comma; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 822 | TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index, bool IsRHS) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 823 | if (Index == Annotations.size()) |
| 824 | return TokenAnnotation::TT_Unknown; |
| 825 | |
| 826 | if (Index == 0 || Line.Tokens[Index - 1].Tok.is(tok::l_paren) || |
| 827 | Line.Tokens[Index - 1].Tok.is(tok::comma) || |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 828 | Line.Tokens[Index - 1].Tok.is(tok::kw_return) || |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 829 | Line.Tokens[Index - 1].Tok.is(tok::colon) || |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 830 | Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator) |
| 831 | return TokenAnnotation::TT_UnaryOperator; |
| 832 | |
| 833 | if (Line.Tokens[Index - 1].Tok.isLiteral() || |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 834 | Line.Tokens[Index + 1].Tok.isLiteral() || |
| 835 | Line.Tokens[Index + 1].Tok.is(tok::kw_sizeof)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 836 | return TokenAnnotation::TT_BinaryOperator; |
| 837 | |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 838 | // It is very unlikely that we are going to find a pointer or reference type |
| 839 | // definition on the RHS of an assignment. |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 840 | if (IsRHS) |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 841 | return TokenAnnotation::TT_BinaryOperator; |
| 842 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 843 | return TokenAnnotation::TT_PointerOrReference; |
| 844 | } |
| 845 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 846 | TokenAnnotation::TokenType determinePlusMinusUsage(unsigned Index) { |
| 847 | // At the start of the line, +/- specific ObjectiveC method declarations. |
| 848 | if (Index == 0) |
| 849 | return TokenAnnotation::TT_ObjCMethodSpecifier; |
| 850 | |
| 851 | // Use heuristics to recognize unary operators. |
| 852 | const Token &PreviousTok = Line.Tokens[Index - 1].Tok; |
| 853 | if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) || |
| 854 | PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square) || |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame^] | 855 | PreviousTok.is(tok::question) || PreviousTok.is(tok::colon) || |
| 856 | PreviousTok.is(tok::kw_return) || PreviousTok.is(tok::kw_case)) |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 857 | return TokenAnnotation::TT_UnaryOperator; |
| 858 | |
| 859 | // There can't be to consecutive binary operators. |
| 860 | if (Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator) |
| 861 | return TokenAnnotation::TT_UnaryOperator; |
| 862 | |
| 863 | // Fall back to marking the token as binary operator. |
| 864 | return TokenAnnotation::TT_BinaryOperator; |
| 865 | } |
| 866 | |
| 867 | /// \brief Determine whether ++/-- are pre- or post-increments/-decrements. |
| 868 | TokenAnnotation::TokenType determineIncrementUsage(unsigned Index) { |
| 869 | if (Index != 0 && Line.Tokens[Index - 1].Tok.is(tok::identifier)) |
| 870 | return TokenAnnotation::TT_TrailingUnaryOperator; |
| 871 | |
| 872 | return TokenAnnotation::TT_UnaryOperator; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | bool spaceRequiredBetween(Token Left, Token Right) { |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 876 | if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma)) |
| 877 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 878 | if (Left.is(tok::kw_template) && Right.is(tok::less)) |
| 879 | return true; |
| 880 | if (Left.is(tok::arrow) || Right.is(tok::arrow)) |
| 881 | return false; |
| 882 | if (Left.is(tok::exclaim) || Left.is(tok::tilde)) |
| 883 | return false; |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 884 | if (Left.is(tok::at) && Right.is(tok::identifier)) |
| 885 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 886 | if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less)) |
| 887 | return false; |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 888 | if (Right.is(tok::amp) || Right.is(tok::star)) |
| 889 | return Left.isLiteral() || |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 890 | (Left.isNot(tok::star) && Left.isNot(tok::amp) && |
| 891 | !Style.PointerAndReferenceBindToType); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 892 | if (Left.is(tok::amp) || Left.is(tok::star)) |
| 893 | return Right.isLiteral() || Style.PointerAndReferenceBindToType; |
| 894 | if (Right.is(tok::star) && Left.is(tok::l_paren)) |
| 895 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 896 | if (Left.is(tok::l_square) || Right.is(tok::l_square) || |
| 897 | Right.is(tok::r_square)) |
| 898 | return false; |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 899 | if (Left.is(tok::coloncolon) || |
| 900 | (Right.is(tok::coloncolon) && |
| 901 | (Left.is(tok::identifier) || Left.is(tok::greater)))) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 902 | return false; |
| 903 | if (Left.is(tok::period) || Right.is(tok::period)) |
| 904 | return false; |
| 905 | if (Left.is(tok::colon) || Right.is(tok::colon)) |
| 906 | return true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 907 | if (Left.is(tok::l_paren)) |
| 908 | return false; |
| 909 | if (Left.is(tok::hash)) |
| 910 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 911 | if (Right.is(tok::l_paren)) { |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 912 | return Left.is(tok::kw_if) || Left.is(tok::kw_for) || |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 913 | Left.is(tok::kw_while) || Left.is(tok::kw_switch) || |
| 914 | (Left.isNot(tok::identifier) && Left.isNot(tok::kw_sizeof) && |
| 915 | Left.isNot(tok::kw_typeof)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 916 | } |
| 917 | return true; |
| 918 | } |
| 919 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 920 | bool canBreakBefore(unsigned i) { |
| 921 | if (Annotations[i - 1].Type == TokenAnnotation::TT_PointerOrReference || |
| 922 | Annotations[i].Type == TokenAnnotation::TT_ConditionalExpr) { |
| 923 | return false; |
| 924 | } |
| 925 | const FormatToken &Left = Line.Tokens[i - 1]; |
| 926 | const FormatToken &Right = Line.Tokens[i]; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 927 | if (Right.Tok.is(tok::r_paren) || Right.Tok.is(tok::l_brace) || |
| 928 | Right.Tok.is(tok::comment) || Right.Tok.is(tok::greater)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 929 | return false; |
Daniel Jasper | 675d2e3 | 2012-12-21 10:20:02 +0000 | [diff] [blame] | 930 | return (isBinaryOperator(Left) && Left.Tok.isNot(tok::lessless)) || |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 931 | Left.Tok.is(tok::comma) || Right.Tok.is(tok::lessless) || |
| 932 | Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period) || |
| 933 | Right.Tok.is(tok::colon) || Left.Tok.is(tok::semi) || |
| 934 | Left.Tok.is(tok::l_brace) || |
| 935 | (Left.Tok.is(tok::l_paren) && !Right.Tok.is(tok::r_paren)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | const UnwrappedLine &Line; |
| 939 | FormatStyle Style; |
| 940 | SourceManager &SourceMgr; |
| 941 | std::vector<TokenAnnotation> Annotations; |
| 942 | }; |
| 943 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 944 | class LexerBasedFormatTokenSource : public FormatTokenSource { |
| 945 | public: |
| 946 | LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 947 | : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 948 | IdentTable(Lex.getLangOpts()) { |
| 949 | Lex.SetKeepWhitespaceMode(true); |
| 950 | } |
| 951 | |
| 952 | virtual FormatToken getNextToken() { |
| 953 | if (GreaterStashed) { |
| 954 | FormatTok.NewlinesBefore = 0; |
| 955 | FormatTok.WhiteSpaceStart = |
| 956 | FormatTok.Tok.getLocation().getLocWithOffset(1); |
| 957 | FormatTok.WhiteSpaceLength = 0; |
| 958 | GreaterStashed = false; |
| 959 | return FormatTok; |
| 960 | } |
| 961 | |
| 962 | FormatTok = FormatToken(); |
| 963 | Lex.LexFromRawLexer(FormatTok.Tok); |
| 964 | FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation(); |
| 965 | |
| 966 | // Consume and record whitespace until we find a significant token. |
| 967 | while (FormatTok.Tok.is(tok::unknown)) { |
| 968 | FormatTok.NewlinesBefore += tokenText(FormatTok.Tok).count('\n'); |
| 969 | FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength(); |
| 970 | |
| 971 | if (FormatTok.Tok.is(tok::eof)) |
| 972 | return FormatTok; |
| 973 | Lex.LexFromRawLexer(FormatTok.Tok); |
| 974 | } |
| 975 | |
| 976 | if (FormatTok.Tok.is(tok::raw_identifier)) { |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 977 | IdentifierInfo &Info = IdentTable.get(tokenText(FormatTok.Tok)); |
| 978 | FormatTok.Tok.setIdentifierInfo(&Info); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 979 | FormatTok.Tok.setKind(Info.getTokenID()); |
| 980 | } |
| 981 | |
| 982 | if (FormatTok.Tok.is(tok::greatergreater)) { |
| 983 | FormatTok.Tok.setKind(tok::greater); |
| 984 | GreaterStashed = true; |
| 985 | } |
| 986 | |
| 987 | return FormatTok; |
| 988 | } |
| 989 | |
| 990 | private: |
| 991 | FormatToken FormatTok; |
| 992 | bool GreaterStashed; |
| 993 | Lexer &Lex; |
| 994 | SourceManager &SourceMgr; |
| 995 | IdentifierTable IdentTable; |
| 996 | |
| 997 | /// Returns the text of \c FormatTok. |
| 998 | StringRef tokenText(Token &Tok) { |
| 999 | return StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
| 1000 | Tok.getLength()); |
| 1001 | } |
| 1002 | }; |
| 1003 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1004 | class Formatter : public UnwrappedLineConsumer { |
| 1005 | public: |
| 1006 | Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, |
| 1007 | const std::vector<CharSourceRange> &Ranges) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1008 | : Style(Style), Lex(Lex), SourceMgr(SourceMgr), Ranges(Ranges), |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1009 | StructuralError(false) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 1012 | virtual ~Formatter() { |
| 1013 | } |
| 1014 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1015 | tooling::Replacements format() { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1016 | LexerBasedFormatTokenSource Tokens(Lex, SourceMgr); |
| 1017 | UnwrappedLineParser Parser(Style, Tokens, *this); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1018 | StructuralError = Parser.parse(); |
| 1019 | for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(), |
| 1020 | E = UnwrappedLines.end(); |
| 1021 | I != E; ++I) |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 1022 | formatUnwrappedLine(*I); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1023 | return Replaces; |
| 1024 | } |
| 1025 | |
| 1026 | private: |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 1027 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1028 | UnwrappedLines.push_back(TheLine); |
| 1029 | } |
| 1030 | |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 1031 | void formatUnwrappedLine(const UnwrappedLine &TheLine) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1032 | if (TheLine.Tokens.size() == 0) |
| 1033 | return; |
| 1034 | |
| 1035 | CharSourceRange LineRange = |
| 1036 | CharSourceRange::getTokenRange(TheLine.Tokens.front().Tok.getLocation(), |
| 1037 | TheLine.Tokens.back().Tok.getLocation()); |
| 1038 | |
| 1039 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 1040 | if (SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(), |
| 1041 | Ranges[i].getBegin()) || |
| 1042 | SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 1043 | LineRange.getBegin())) |
| 1044 | continue; |
| 1045 | |
| 1046 | TokenAnnotator Annotator(TheLine, Style, SourceMgr); |
| 1047 | Annotator.annotate(); |
| 1048 | UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1049 | Annotator.getAnnotations(), Replaces, |
| 1050 | StructuralError); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1051 | Formatter.format(); |
| 1052 | return; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | FormatStyle Style; |
| 1057 | Lexer &Lex; |
| 1058 | SourceManager &SourceMgr; |
| 1059 | tooling::Replacements Replaces; |
| 1060 | std::vector<CharSourceRange> Ranges; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1061 | std::vector<UnwrappedLine> UnwrappedLines; |
| 1062 | bool StructuralError; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1063 | }; |
| 1064 | |
| 1065 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 1066 | SourceManager &SourceMgr, |
| 1067 | std::vector<CharSourceRange> Ranges) { |
| 1068 | Formatter formatter(Style, Lex, SourceMgr, Ranges); |
| 1069 | return formatter.format(); |
| 1070 | } |
| 1071 | |
| 1072 | } // namespace format |
| 1073 | } // namespace clang |