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