Daniel Jasper | f793511 | 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 | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "UnwrappedLineParser.h" |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "clang/Lex/Lexer.h" |
| 23 | |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Daniel Jasper | f793511 | 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 | aa1c920 | 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, |
| 37 | TT_OverloadedOperator, |
| 38 | TT_PointerOrReference, |
| 39 | TT_ConditionalExpr, |
| 40 | TT_LineComment, |
| 41 | TT_BlockComment |
| 42 | }; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 43 | |
| 44 | TokenType Type; |
| 45 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 46 | bool SpaceRequiredBefore; |
| 47 | bool CanBreakBefore; |
| 48 | bool MustBreakBefore; |
| 49 | }; |
| 50 | |
| 51 | using llvm::MutableArrayRef; |
| 52 | |
| 53 | FormatStyle getLLVMStyle() { |
| 54 | FormatStyle LLVMStyle; |
| 55 | LLVMStyle.ColumnLimit = 80; |
| 56 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
| 57 | LLVMStyle.PointerAndReferenceBindToType = false; |
| 58 | LLVMStyle.AccessModifierOffset = -2; |
| 59 | LLVMStyle.SplitTemplateClosingGreater = true; |
| 60 | return LLVMStyle; |
| 61 | } |
| 62 | |
| 63 | FormatStyle getGoogleStyle() { |
| 64 | FormatStyle GoogleStyle; |
| 65 | GoogleStyle.ColumnLimit = 80; |
| 66 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
| 67 | GoogleStyle.PointerAndReferenceBindToType = true; |
| 68 | GoogleStyle.AccessModifierOffset = -1; |
| 69 | GoogleStyle.SplitTemplateClosingGreater = false; |
| 70 | return GoogleStyle; |
| 71 | } |
| 72 | |
| 73 | struct OptimizationParameters { |
| 74 | unsigned PenaltyExtraLine; |
| 75 | unsigned PenaltyIndentLevel; |
| 76 | }; |
| 77 | |
| 78 | class UnwrappedLineFormatter { |
| 79 | public: |
| 80 | UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, |
| 81 | const UnwrappedLine &Line, |
| 82 | const std::vector<TokenAnnotation> &Annotations, |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 83 | tooling::Replacements &Replaces, bool StructuralError) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 84 | : Style(Style), |
| 85 | SourceMgr(SourceMgr), |
| 86 | Line(Line), |
| 87 | Annotations(Annotations), |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 88 | Replaces(Replaces), |
| 89 | StructuralError(StructuralError) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 90 | Parameters.PenaltyExtraLine = 100; |
| 91 | Parameters.PenaltyIndentLevel = 5; |
| 92 | } |
| 93 | |
| 94 | void format() { |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 95 | // Format first token and initialize indent. |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 96 | unsigned Indent = formatFirstToken(); |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 97 | |
| 98 | // Initialize state dependent on indent. |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 99 | IndentState State; |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 100 | State.Column = Indent; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 101 | State.CtorInitializerOnNewLine = false; |
| 102 | State.InCtorInitializer = false; |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 103 | State.ConsumedTokens = 0; |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 104 | State.Indent.push_back(Indent + 4); |
| 105 | State.LastSpace.push_back(Indent); |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 106 | State.FirstLessLess.push_back(0); |
| 107 | |
| 108 | // The first token has already been indented and thus consumed. |
| 109 | moveStateToNextToken(State); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 110 | |
| 111 | // Start iterating at 1 as we have correctly formatted of Token #0 above. |
| 112 | for (unsigned i = 1, n = Line.Tokens.size(); i != n; ++i) { |
| 113 | unsigned NoBreak = calcPenalty(State, false, UINT_MAX); |
| 114 | unsigned Break = calcPenalty(State, true, NoBreak); |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 115 | addTokenToState(Break < NoBreak, false, State); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | /// \brief The current state when indenting a unwrapped line. |
| 121 | /// |
| 122 | /// As the indenting tries different combinations this is copied by value. |
| 123 | struct IndentState { |
| 124 | /// \brief The number of used columns in the current line. |
| 125 | unsigned Column; |
| 126 | |
| 127 | /// \brief The number of tokens already consumed. |
| 128 | unsigned ConsumedTokens; |
| 129 | |
| 130 | /// \brief The position to which a specific parenthesis level needs to be |
| 131 | /// indented. |
| 132 | std::vector<unsigned> Indent; |
| 133 | |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 134 | /// \brief The position of the last space on each level. |
| 135 | /// |
| 136 | /// Used e.g. to break like: |
| 137 | /// functionCall(Parameter, otherCall( |
| 138 | /// OtherParameter)); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 139 | std::vector<unsigned> LastSpace; |
| 140 | |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 141 | /// \brief The position the first "<<" operator encountered on each level. |
| 142 | /// |
| 143 | /// Used to align "<<" operators. 0 if no such operator has been encountered |
| 144 | /// on a level. |
| 145 | std::vector<unsigned> FirstLessLess; |
| 146 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 147 | bool CtorInitializerOnNewLine; |
| 148 | bool InCtorInitializer; |
| 149 | |
| 150 | /// \brief Comparison operator to be able to used \c IndentState in \c map. |
| 151 | bool operator<(const IndentState &Other) const { |
| 152 | if (Other.ConsumedTokens != ConsumedTokens) |
| 153 | return Other.ConsumedTokens > ConsumedTokens; |
| 154 | if (Other.Column != Column) |
| 155 | return Other.Column > Column; |
| 156 | if (Other.Indent.size() != Indent.size()) |
| 157 | return Other.Indent.size() > Indent.size(); |
| 158 | for (int i = 0, e = Indent.size(); i != e; ++i) { |
| 159 | if (Other.Indent[i] != Indent[i]) |
| 160 | return Other.Indent[i] > Indent[i]; |
| 161 | } |
| 162 | if (Other.LastSpace.size() != LastSpace.size()) |
| 163 | return Other.LastSpace.size() > LastSpace.size(); |
| 164 | for (int i = 0, e = LastSpace.size(); i != e; ++i) { |
| 165 | if (Other.LastSpace[i] != LastSpace[i]) |
| 166 | return Other.LastSpace[i] > LastSpace[i]; |
| 167 | } |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 168 | if (Other.FirstLessLess.size() != FirstLessLess.size()) |
| 169 | return Other.FirstLessLess.size() > FirstLessLess.size(); |
| 170 | for (int i = 0, e = FirstLessLess.size(); i != e; ++i) { |
| 171 | if (Other.FirstLessLess[i] != FirstLessLess[i]) |
| 172 | return Other.FirstLessLess[i] > FirstLessLess[i]; |
| 173 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 174 | return false; |
| 175 | } |
| 176 | }; |
| 177 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 178 | /// \brief Appends the next token to \p State and updates information |
| 179 | /// necessary for indentation. |
| 180 | /// |
| 181 | /// Puts the token on the current line if \p Newline is \c true and adds a |
| 182 | /// line break and necessary indentation otherwise. |
| 183 | /// |
| 184 | /// If \p DryRun is \c false, also creates and stores the required |
| 185 | /// \c Replacement. |
| 186 | void addTokenToState(bool Newline, bool DryRun, IndentState &State) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 187 | unsigned Index = State.ConsumedTokens; |
| 188 | const FormatToken &Current = Line.Tokens[Index]; |
| 189 | const FormatToken &Previous = Line.Tokens[Index - 1]; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 190 | unsigned ParenLevel = State.Indent.size() - 1; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 191 | |
| 192 | if (Newline) { |
| 193 | if (Current.Tok.is(tok::string_literal) && |
| 194 | Previous.Tok.is(tok::string_literal)) |
| 195 | State.Column = State.Column - Previous.Tok.getLength(); |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 196 | else if (Current.Tok.is(tok::lessless) && |
| 197 | State.FirstLessLess[ParenLevel] != 0) |
| 198 | State.Column = State.FirstLessLess[ParenLevel]; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 199 | else if (Previous.Tok.is(tok::equal) && ParenLevel != 0) |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 200 | // Indent and extra 4 spaces after '=' as it continues an expression. |
| 201 | // Don't do that on the top level, as we already indent 4 there. |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 202 | State.Column = State.Indent[ParenLevel] + 4; |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 203 | else |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 204 | State.Column = State.Indent[ParenLevel]; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 205 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 206 | if (!DryRun) |
| 207 | replaceWhitespace(Current, 1, State.Column); |
| 208 | |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 209 | State.LastSpace[ParenLevel] = State.Indent[ParenLevel]; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 210 | if (Current.Tok.is(tok::colon) && |
| 211 | Annotations[Index].Type != TokenAnnotation::TT_ConditionalExpr) { |
| 212 | State.Indent[ParenLevel] += 2; |
| 213 | State.CtorInitializerOnNewLine = true; |
| 214 | State.InCtorInitializer = true; |
| 215 | } |
| 216 | } else { |
| 217 | unsigned Spaces = Annotations[Index].SpaceRequiredBefore ? 1 : 0; |
| 218 | if (Annotations[Index].Type == TokenAnnotation::TT_LineComment) |
| 219 | Spaces = 2; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 220 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 221 | if (!DryRun) |
| 222 | replaceWhitespace(Current, 0, Spaces); |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 223 | |
| 224 | if (Previous.Tok.is(tok::l_paren) || |
| 225 | Annotations[Index - 1].Type == TokenAnnotation::TT_TemplateOpener) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 226 | State.Indent[ParenLevel] = State.Column; |
| 227 | if (Current.Tok.is(tok::colon)) { |
| 228 | State.Indent[ParenLevel] = State.Column + 3; |
| 229 | State.InCtorInitializer = true; |
| 230 | } |
| 231 | // Top-level spaces are exempt as that mostly leads to better results. |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 232 | State.Column += Spaces; |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 233 | if (Spaces > 0 && ParenLevel != 0) |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 234 | State.LastSpace[ParenLevel] = State.Column; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 235 | } |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 236 | moveStateToNextToken(State); |
| 237 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 238 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 239 | /// \brief Mark the next token as consumed in \p State and modify its stacks |
| 240 | /// accordingly. |
| 241 | void moveStateToNextToken(IndentState &State) { |
| 242 | unsigned Index = State.ConsumedTokens; |
| 243 | const FormatToken &Current = Line.Tokens[Index]; |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 244 | unsigned ParenLevel = State.Indent.size() - 1; |
| 245 | |
| 246 | if (Current.Tok.is(tok::lessless) && State.FirstLessLess[ParenLevel] == 0) |
| 247 | State.FirstLessLess[ParenLevel] = State.Column; |
| 248 | |
| 249 | State.Column += Current.Tok.getLength(); |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 250 | |
| 251 | // If we encounter an opening (, [ or <, we add a level to our stacks to |
| 252 | // prepare for the following tokens. |
| 253 | if (Current.Tok.is(tok::l_paren) || Current.Tok.is(tok::l_square) || |
| 254 | Annotations[Index].Type == TokenAnnotation::TT_TemplateOpener) { |
| 255 | State.Indent.push_back(4 + State.LastSpace.back()); |
| 256 | State.LastSpace.push_back(State.LastSpace.back()); |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 257 | State.FirstLessLess.push_back(0); |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // If we encounter a closing ), ] or >, we can remove a level from our |
| 261 | // stacks. |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 262 | if (Current.Tok.is(tok::r_paren) || Current.Tok.is(tok::r_square) || |
| 263 | Annotations[Index].Type == TokenAnnotation::TT_TemplateCloser) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 264 | State.Indent.pop_back(); |
| 265 | State.LastSpace.pop_back(); |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 266 | State.FirstLessLess.pop_back(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | ++State.ConsumedTokens; |
| 270 | } |
| 271 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 272 | unsigned splitPenalty(const FormatToken &Token) { |
| 273 | if (Token.Tok.is(tok::semi)) |
| 274 | return 0; |
| 275 | if (Token.Tok.is(tok::comma)) |
| 276 | return 1; |
| 277 | if (Token.Tok.is(tok::equal) || Token.Tok.is(tok::l_paren) || |
| 278 | Token.Tok.is(tok::pipepipe) || Token.Tok.is(tok::ampamp)) |
| 279 | return 2; |
| 280 | return 3; |
| 281 | } |
| 282 | |
| 283 | /// \brief Calculate the number of lines needed to format the remaining part |
| 284 | /// of the unwrapped line. |
| 285 | /// |
| 286 | /// Assumes the formatting so far has led to |
| 287 | /// the \c IndentState \p State. If \p NewLine is set, a new line will be |
| 288 | /// added after the previous token. |
| 289 | /// |
| 290 | /// \param StopAt is used for optimization. If we can determine that we'll |
| 291 | /// definitely need at least \p StopAt additional lines, we already know of a |
| 292 | /// better solution. |
| 293 | unsigned calcPenalty(IndentState State, bool NewLine, unsigned StopAt) { |
| 294 | // We are at the end of the unwrapped line, so we don't need any more lines. |
| 295 | if (State.ConsumedTokens >= Line.Tokens.size()) |
| 296 | return 0; |
| 297 | |
| 298 | if (!NewLine && Annotations[State.ConsumedTokens].MustBreakBefore) |
| 299 | return UINT_MAX; |
| 300 | if (NewLine && !Annotations[State.ConsumedTokens].CanBreakBefore) |
| 301 | return UINT_MAX; |
| 302 | |
| 303 | if (State.ConsumedTokens > 0 && !NewLine && |
| 304 | State.CtorInitializerOnNewLine && |
| 305 | Line.Tokens[State.ConsumedTokens - 1].Tok.is(tok::comma)) |
| 306 | return UINT_MAX; |
| 307 | |
| 308 | if (NewLine && State.InCtorInitializer && !State.CtorInitializerOnNewLine) |
| 309 | return UINT_MAX; |
| 310 | |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 311 | unsigned CurrentPenalty = 0; |
| 312 | if (NewLine) { |
| 313 | CurrentPenalty += Parameters.PenaltyIndentLevel * State.Indent.size() + |
| 314 | Parameters.PenaltyExtraLine + |
| 315 | splitPenalty(Line.Tokens[State.ConsumedTokens - 1]); |
| 316 | } |
| 317 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 318 | addTokenToState(NewLine, true, State); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 319 | |
| 320 | // Exceeding column limit is bad. |
| 321 | if (State.Column > Style.ColumnLimit) |
| 322 | return UINT_MAX; |
| 323 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 324 | if (StopAt <= CurrentPenalty) |
| 325 | return UINT_MAX; |
| 326 | StopAt -= CurrentPenalty; |
| 327 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 328 | StateMap::iterator I = Memory.find(State); |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 329 | if (I != Memory.end()) { |
| 330 | // If this state has already been examined, we can safely return the |
| 331 | // previous result if we |
| 332 | // - have not hit the optimatization (and thus returned UINT_MAX) OR |
| 333 | // - are now computing for a smaller or equal StopAt. |
| 334 | unsigned SavedResult = I->second.first; |
| 335 | unsigned SavedStopAt = I->second.second; |
| 336 | if (SavedResult != UINT_MAX || StopAt <= SavedStopAt) |
| 337 | return SavedResult; |
| 338 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 339 | |
| 340 | unsigned NoBreak = calcPenalty(State, false, StopAt); |
| 341 | unsigned WithBreak = calcPenalty(State, true, std::min(StopAt, NoBreak)); |
| 342 | unsigned Result = std::min(NoBreak, WithBreak); |
| 343 | if (Result != UINT_MAX) |
| 344 | Result += CurrentPenalty; |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 345 | Memory[State] = std::pair<unsigned, unsigned>(Result, StopAt); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 346 | return Result; |
| 347 | } |
| 348 | |
| 349 | /// \brief Replaces the whitespace in front of \p Tok. Only call once for |
| 350 | /// each \c FormatToken. |
| 351 | void replaceWhitespace(const FormatToken &Tok, unsigned NewLines, |
| 352 | unsigned Spaces) { |
| 353 | Replaces.insert(tooling::Replacement( |
| 354 | SourceMgr, Tok.WhiteSpaceStart, Tok.WhiteSpaceLength, |
| 355 | std::string(NewLines, '\n') + std::string(Spaces, ' '))); |
| 356 | } |
| 357 | |
| 358 | /// \brief Add a new line and the required indent before the first Token |
Alexander Kornienko | bc09a7e | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 359 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 360 | /// Returns the indent level of the \c UnwrappedLine. |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 361 | unsigned formatFirstToken() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 362 | const FormatToken &Token = Line.Tokens[0]; |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 363 | if (!Token.WhiteSpaceStart.isValid() || StructuralError) |
| 364 | return SourceMgr.getSpellingColumnNumber(Token.Tok.getLocation()) - 1; |
| 365 | |
| 366 | unsigned Newlines = |
| 367 | std::min(Token.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); |
| 368 | unsigned Offset = SourceMgr.getFileOffset(Token.WhiteSpaceStart); |
| 369 | if (Newlines == 0 && Offset != 0) |
| 370 | Newlines = 1; |
| 371 | unsigned Indent = Line.Level * 2; |
| 372 | if (Token.Tok.is(tok::kw_public) || Token.Tok.is(tok::kw_protected) || |
| 373 | Token.Tok.is(tok::kw_private)) |
| 374 | Indent += Style.AccessModifierOffset; |
| 375 | replaceWhitespace(Token, Newlines, Indent); |
| 376 | return Indent; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | FormatStyle Style; |
| 380 | SourceManager &SourceMgr; |
| 381 | const UnwrappedLine &Line; |
| 382 | const std::vector<TokenAnnotation> &Annotations; |
| 383 | tooling::Replacements &Replaces; |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 384 | bool StructuralError; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 385 | |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 386 | // A map from an indent state to a pair (Result, Used-StopAt). |
| 387 | typedef std::map<IndentState, std::pair<unsigned, unsigned> > StateMap; |
| 388 | StateMap Memory; |
| 389 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 390 | OptimizationParameters Parameters; |
| 391 | }; |
| 392 | |
| 393 | /// \brief Determines extra information about the tokens comprising an |
| 394 | /// \c UnwrappedLine. |
| 395 | class TokenAnnotator { |
| 396 | public: |
| 397 | TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style, |
| 398 | SourceManager &SourceMgr) |
| 399 | : Line(Line), |
| 400 | Style(Style), |
| 401 | SourceMgr(SourceMgr) { |
| 402 | } |
| 403 | |
| 404 | /// \brief A parser that gathers additional information about tokens. |
| 405 | /// |
| 406 | /// The \c TokenAnnotator tries to matches parenthesis and square brakets and |
| 407 | /// store a parenthesis levels. It also tries to resolve matching "<" and ">" |
| 408 | /// into template parameter lists. |
| 409 | class AnnotatingParser { |
| 410 | public: |
Manuel Klimek | 6a5619d | 2012-12-03 20:55:42 +0000 | [diff] [blame] | 411 | AnnotatingParser(const SmallVector<FormatToken, 16> &Tokens, |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 412 | std::vector<TokenAnnotation> &Annotations) |
Manuel Klimek | 6a5619d | 2012-12-03 20:55:42 +0000 | [diff] [blame] | 413 | : Tokens(Tokens), |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 414 | Annotations(Annotations), |
| 415 | Index(0) { |
| 416 | } |
| 417 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 418 | bool parseAngle() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 419 | while (Index < Tokens.size()) { |
| 420 | if (Tokens[Index].Tok.is(tok::greater)) { |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 421 | Annotations[Index].Type = TokenAnnotation::TT_TemplateCloser; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 422 | next(); |
| 423 | return true; |
| 424 | } |
| 425 | if (Tokens[Index].Tok.is(tok::r_paren) || |
| 426 | Tokens[Index].Tok.is(tok::r_square)) |
| 427 | return false; |
| 428 | if (Tokens[Index].Tok.is(tok::pipepipe) || |
| 429 | Tokens[Index].Tok.is(tok::ampamp) || |
| 430 | Tokens[Index].Tok.is(tok::question) || |
| 431 | Tokens[Index].Tok.is(tok::colon)) |
| 432 | return false; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 433 | consumeToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 434 | } |
| 435 | return false; |
| 436 | } |
| 437 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 438 | bool parseParens() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 439 | while (Index < Tokens.size()) { |
| 440 | if (Tokens[Index].Tok.is(tok::r_paren)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 441 | next(); |
| 442 | return true; |
| 443 | } |
| 444 | if (Tokens[Index].Tok.is(tok::r_square)) |
| 445 | return false; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 446 | consumeToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 447 | } |
| 448 | return false; |
| 449 | } |
| 450 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 451 | bool parseSquare() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 452 | while (Index < Tokens.size()) { |
| 453 | if (Tokens[Index].Tok.is(tok::r_square)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 454 | next(); |
| 455 | return true; |
| 456 | } |
| 457 | if (Tokens[Index].Tok.is(tok::r_paren)) |
| 458 | return false; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 459 | consumeToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 460 | } |
| 461 | return false; |
| 462 | } |
| 463 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 464 | bool parseConditional() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 465 | while (Index < Tokens.size()) { |
| 466 | if (Tokens[Index].Tok.is(tok::colon)) { |
| 467 | Annotations[Index].Type = TokenAnnotation::TT_ConditionalExpr; |
| 468 | next(); |
| 469 | return true; |
| 470 | } |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 471 | consumeToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 472 | } |
| 473 | return false; |
| 474 | } |
| 475 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 476 | void consumeToken() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 477 | unsigned CurrentIndex = Index; |
| 478 | next(); |
| 479 | switch (Tokens[CurrentIndex].Tok.getKind()) { |
| 480 | case tok::l_paren: |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 481 | parseParens(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 482 | break; |
| 483 | case tok::l_square: |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 484 | parseSquare(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 485 | break; |
| 486 | case tok::less: |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 487 | if (parseAngle()) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 488 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_TemplateOpener; |
| 489 | else { |
| 490 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator; |
| 491 | Index = CurrentIndex + 1; |
| 492 | } |
| 493 | break; |
| 494 | case tok::greater: |
| 495 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator; |
| 496 | break; |
| 497 | case tok::kw_operator: |
| 498 | if (!Tokens[Index].Tok.is(tok::l_paren)) |
| 499 | Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator; |
| 500 | next(); |
| 501 | break; |
| 502 | case tok::question: |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 503 | parseConditional(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 504 | break; |
| 505 | default: |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | void parseLine() { |
| 511 | while (Index < Tokens.size()) { |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 512 | consumeToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | void next() { |
| 517 | ++Index; |
| 518 | } |
| 519 | |
| 520 | private: |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 521 | const SmallVector<FormatToken, 16> &Tokens; |
| 522 | std::vector<TokenAnnotation> &Annotations; |
| 523 | unsigned Index; |
| 524 | }; |
| 525 | |
| 526 | void annotate() { |
| 527 | Annotations.clear(); |
| 528 | for (int i = 0, e = Line.Tokens.size(); i != e; ++i) { |
| 529 | Annotations.push_back(TokenAnnotation()); |
| 530 | } |
| 531 | |
Manuel Klimek | 6a5619d | 2012-12-03 20:55:42 +0000 | [diff] [blame] | 532 | AnnotatingParser Parser(Line.Tokens, Annotations); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 533 | Parser.parseLine(); |
| 534 | |
| 535 | determineTokenTypes(); |
| 536 | |
| 537 | for (int i = 1, e = Line.Tokens.size(); i != e; ++i) { |
| 538 | TokenAnnotation &Annotation = Annotations[i]; |
| 539 | |
| 540 | Annotation.CanBreakBefore = |
| 541 | canBreakBetween(Line.Tokens[i - 1], Line.Tokens[i]); |
| 542 | |
| 543 | if (Line.Tokens[i].Tok.is(tok::colon)) { |
Daniel Jasper | 55b6b64 | 2012-12-05 16:24:48 +0000 | [diff] [blame] | 544 | Annotation.SpaceRequiredBefore = |
| 545 | Line.Tokens[0].Tok.isNot(tok::kw_case) && i != e - 1; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 546 | } else if (Annotations[i - 1].Type == TokenAnnotation::TT_UnaryOperator) { |
| 547 | Annotation.SpaceRequiredBefore = false; |
| 548 | } else if (Annotation.Type == TokenAnnotation::TT_UnaryOperator) { |
| 549 | Annotation.SpaceRequiredBefore = |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 550 | Line.Tokens[i - 1].Tok.isNot(tok::l_paren) && |
| 551 | Line.Tokens[i - 1].Tok.isNot(tok::l_square); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 552 | } else if (Line.Tokens[i - 1].Tok.is(tok::greater) && |
| 553 | Line.Tokens[i].Tok.is(tok::greater)) { |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 554 | if (Annotation.Type == TokenAnnotation::TT_TemplateCloser && |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 555 | Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser) |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 556 | Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 557 | else |
| 558 | Annotation.SpaceRequiredBefore = false; |
| 559 | } else if ( |
| 560 | Annotation.Type == TokenAnnotation::TT_BinaryOperator || |
| 561 | Annotations[i - 1].Type == TokenAnnotation::TT_BinaryOperator) { |
| 562 | Annotation.SpaceRequiredBefore = true; |
| 563 | } else if ( |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 564 | Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser && |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 565 | Line.Tokens[i].Tok.is(tok::l_paren)) { |
| 566 | Annotation.SpaceRequiredBefore = false; |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 567 | } else if (Line.Tokens[i].Tok.is(tok::less) && |
| 568 | Line.Tokens[0].Tok.is(tok::hash)) { |
| 569 | Annotation.SpaceRequiredBefore = true; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 570 | } else { |
| 571 | Annotation.SpaceRequiredBefore = |
| 572 | spaceRequiredBetween(Line.Tokens[i - 1].Tok, Line.Tokens[i].Tok); |
| 573 | } |
| 574 | |
| 575 | if (Annotations[i - 1].Type == TokenAnnotation::TT_LineComment || |
| 576 | (Line.Tokens[i].Tok.is(tok::string_literal) && |
| 577 | Line.Tokens[i - 1].Tok.is(tok::string_literal))) { |
| 578 | Annotation.MustBreakBefore = true; |
| 579 | } |
| 580 | |
| 581 | if (Annotation.MustBreakBefore) |
| 582 | Annotation.CanBreakBefore = true; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | const std::vector<TokenAnnotation> &getAnnotations() { |
| 587 | return Annotations; |
| 588 | } |
| 589 | |
| 590 | private: |
| 591 | void determineTokenTypes() { |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 592 | bool AssignmentEncountered = false; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 593 | for (int i = 0, e = Line.Tokens.size(); i != e; ++i) { |
| 594 | TokenAnnotation &Annotation = Annotations[i]; |
| 595 | const FormatToken &Tok = Line.Tokens[i]; |
| 596 | |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 597 | if (Tok.Tok.is(tok::equal) || Tok.Tok.is(tok::plusequal) || |
| 598 | Tok.Tok.is(tok::minusequal) || Tok.Tok.is(tok::starequal) || |
| 599 | Tok.Tok.is(tok::slashequal)) |
| 600 | AssignmentEncountered = true; |
Daniel Jasper | 426702d | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 601 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 602 | if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp)) |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 603 | Annotation.Type = determineStarAmpUsage(i, AssignmentEncountered); |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 604 | else if (isUnaryOperator(i)) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 605 | Annotation.Type = TokenAnnotation::TT_UnaryOperator; |
| 606 | else if (isBinaryOperator(Line.Tokens[i])) |
| 607 | Annotation.Type = TokenAnnotation::TT_BinaryOperator; |
| 608 | else if (Tok.Tok.is(tok::comment)) { |
| 609 | StringRef Data(SourceMgr.getCharacterData(Tok.Tok.getLocation()), |
| 610 | Tok.Tok.getLength()); |
| 611 | if (Data.startswith("//")) |
| 612 | Annotation.Type = TokenAnnotation::TT_LineComment; |
| 613 | else |
| 614 | Annotation.Type = TokenAnnotation::TT_BlockComment; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 619 | bool isUnaryOperator(unsigned Index) { |
| 620 | const Token &Tok = Line.Tokens[Index].Tok; |
| 621 | if (Tok.isNot(tok::minus) && Tok.isNot(tok::plus)) |
| 622 | return false; |
| 623 | const Token &PreviousTok = Line.Tokens[Index - 1].Tok; |
| 624 | if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) || |
| 625 | PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square)) |
| 626 | return true; |
| 627 | return Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator; |
| 628 | } |
| 629 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 630 | bool isBinaryOperator(const FormatToken &Tok) { |
| 631 | switch (Tok.Tok.getKind()) { |
| 632 | case tok::equal: |
| 633 | case tok::equalequal: |
Daniel Jasper | 426702d | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 634 | case tok::exclaimequal: |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 635 | case tok::star: |
| 636 | //case tok::amp: |
| 637 | case tok::plus: |
| 638 | case tok::slash: |
| 639 | case tok::minus: |
| 640 | case tok::ampamp: |
| 641 | case tok::pipe: |
| 642 | case tok::pipepipe: |
| 643 | case tok::percent: |
| 644 | return true; |
| 645 | default: |
| 646 | return false; |
| 647 | } |
| 648 | } |
| 649 | |
Daniel Jasper | 426702d | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 650 | TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index, |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 651 | bool AssignmentEncountered) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 652 | if (Index == Annotations.size()) |
| 653 | return TokenAnnotation::TT_Unknown; |
| 654 | |
| 655 | if (Index == 0 || Line.Tokens[Index - 1].Tok.is(tok::l_paren) || |
| 656 | Line.Tokens[Index - 1].Tok.is(tok::comma) || |
| 657 | Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator) |
| 658 | return TokenAnnotation::TT_UnaryOperator; |
| 659 | |
| 660 | if (Line.Tokens[Index - 1].Tok.isLiteral() || |
| 661 | Line.Tokens[Index + 1].Tok.isLiteral()) |
| 662 | return TokenAnnotation::TT_BinaryOperator; |
| 663 | |
Daniel Jasper | 426702d | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 664 | // It is very unlikely that we are going to find a pointer or reference type |
| 665 | // definition on the RHS of an assignment. |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 666 | if (AssignmentEncountered) |
Daniel Jasper | 426702d | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 667 | return TokenAnnotation::TT_BinaryOperator; |
| 668 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 669 | return TokenAnnotation::TT_PointerOrReference; |
| 670 | } |
| 671 | |
| 672 | bool isIfForOrWhile(Token Tok) { |
| 673 | return Tok.is(tok::kw_if) || Tok.is(tok::kw_for) || Tok.is(tok::kw_while); |
| 674 | } |
| 675 | |
| 676 | bool spaceRequiredBetween(Token Left, Token Right) { |
| 677 | if (Left.is(tok::kw_template) && Right.is(tok::less)) |
| 678 | return true; |
| 679 | if (Left.is(tok::arrow) || Right.is(tok::arrow)) |
| 680 | return false; |
| 681 | if (Left.is(tok::exclaim) || Left.is(tok::tilde)) |
| 682 | return false; |
| 683 | if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less)) |
| 684 | return false; |
| 685 | if (Left.is(tok::amp) || Left.is(tok::star)) |
| 686 | return Right.isLiteral() || Style.PointerAndReferenceBindToType; |
| 687 | if (Right.is(tok::star) && Left.is(tok::l_paren)) |
| 688 | return false; |
| 689 | if (Right.is(tok::amp) || Right.is(tok::star)) |
| 690 | return Left.isLiteral() || !Style.PointerAndReferenceBindToType; |
| 691 | if (Left.is(tok::l_square) || Right.is(tok::l_square) || |
| 692 | Right.is(tok::r_square)) |
| 693 | return false; |
| 694 | if (Left.is(tok::coloncolon) || Right.is(tok::coloncolon)) |
| 695 | return false; |
| 696 | if (Left.is(tok::period) || Right.is(tok::period)) |
| 697 | return false; |
| 698 | if (Left.is(tok::colon) || Right.is(tok::colon)) |
| 699 | return true; |
| 700 | if ((Left.is(tok::plusplus) && Right.isAnyIdentifier()) || |
| 701 | (Left.isAnyIdentifier() && Right.is(tok::plusplus)) || |
| 702 | (Left.is(tok::minusminus) && Right.isAnyIdentifier()) || |
| 703 | (Left.isAnyIdentifier() && Right.is(tok::minusminus))) |
| 704 | return false; |
| 705 | if (Left.is(tok::l_paren)) |
| 706 | return false; |
| 707 | if (Left.is(tok::hash)) |
| 708 | return false; |
| 709 | if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma)) |
| 710 | return false; |
| 711 | if (Right.is(tok::l_paren)) { |
| 712 | return !Left.isAnyIdentifier() || isIfForOrWhile(Left); |
| 713 | } |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | bool canBreakBetween(const FormatToken &Left, const FormatToken &Right) { |
| 718 | if (Right.Tok.is(tok::r_paren)) |
| 719 | return false; |
| 720 | if (isBinaryOperator(Left)) |
| 721 | return true; |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame^] | 722 | if (Right.Tok.is(tok::lessless)) |
| 723 | return true; |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 724 | return Right.Tok.is(tok::colon) || Left.Tok.is(tok::comma) || |
| 725 | Left.Tok.is(tok::semi) || Left.Tok.is(tok::equal) || |
| 726 | Left.Tok.is(tok::ampamp) || Left.Tok.is(tok::pipepipe) || |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 727 | (Left.Tok.is(tok::l_paren) && !Right.Tok.is(tok::r_paren)); |
| 728 | } |
| 729 | |
| 730 | const UnwrappedLine &Line; |
| 731 | FormatStyle Style; |
| 732 | SourceManager &SourceMgr; |
| 733 | std::vector<TokenAnnotation> Annotations; |
| 734 | }; |
| 735 | |
| 736 | class Formatter : public UnwrappedLineConsumer { |
| 737 | public: |
| 738 | Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, |
| 739 | const std::vector<CharSourceRange> &Ranges) |
| 740 | : Style(Style), |
| 741 | Lex(Lex), |
| 742 | SourceMgr(SourceMgr), |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 743 | Ranges(Ranges), |
| 744 | StructuralError(false) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Daniel Jasper | 61bd3a1 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 747 | virtual ~Formatter() { |
| 748 | } |
| 749 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 750 | tooling::Replacements format() { |
| 751 | UnwrappedLineParser Parser(Lex, SourceMgr, *this); |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 752 | StructuralError = Parser.parse(); |
| 753 | for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(), |
| 754 | E = UnwrappedLines.end(); |
| 755 | I != E; ++I) |
Alexander Kornienko | bc09a7e | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 756 | formatUnwrappedLine(*I); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 757 | return Replaces; |
| 758 | } |
| 759 | |
| 760 | private: |
Alexander Kornienko | bc09a7e | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 761 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 762 | UnwrappedLines.push_back(TheLine); |
| 763 | } |
| 764 | |
Alexander Kornienko | bc09a7e | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 765 | void formatUnwrappedLine(const UnwrappedLine &TheLine) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 766 | if (TheLine.Tokens.size() == 0) |
| 767 | return; |
| 768 | |
| 769 | CharSourceRange LineRange = |
| 770 | CharSourceRange::getTokenRange(TheLine.Tokens.front().Tok.getLocation(), |
| 771 | TheLine.Tokens.back().Tok.getLocation()); |
| 772 | |
| 773 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 774 | if (SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(), |
| 775 | Ranges[i].getBegin()) || |
| 776 | SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 777 | LineRange.getBegin())) |
| 778 | continue; |
| 779 | |
| 780 | TokenAnnotator Annotator(TheLine, Style, SourceMgr); |
| 781 | Annotator.annotate(); |
| 782 | UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 783 | Annotator.getAnnotations(), Replaces, |
| 784 | StructuralError); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 785 | Formatter.format(); |
| 786 | return; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | FormatStyle Style; |
| 791 | Lexer &Lex; |
| 792 | SourceManager &SourceMgr; |
| 793 | tooling::Replacements Replaces; |
| 794 | std::vector<CharSourceRange> Ranges; |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 795 | std::vector<UnwrappedLine> UnwrappedLines; |
| 796 | bool StructuralError; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 797 | }; |
| 798 | |
| 799 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 800 | SourceManager &SourceMgr, |
| 801 | std::vector<CharSourceRange> Ranges) { |
| 802 | Formatter formatter(Style, Lex, SourceMgr, Ranges); |
| 803 | return formatter.format(); |
| 804 | } |
| 805 | |
| 806 | } // namespace format |
| 807 | } // namespace clang |