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