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 | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 42 | TT_ObjCSelectorStart, |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 43 | TT_ObjCProperty, |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 44 | TT_OverloadedOperator, |
| 45 | TT_PointerOrReference, |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 46 | TT_PureVirtualSpecifier, |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 47 | TT_TemplateCloser, |
| 48 | TT_TemplateOpener, |
| 49 | TT_TrailingUnaryOperator, |
| 50 | TT_UnaryOperator, |
| 51 | TT_Unknown |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | enum LineType { |
| 55 | LT_Invalid, |
| 56 | LT_Other, |
| 57 | LT_PreprocessorDirective, |
| 58 | LT_VirtualFunctionDecl, |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 59 | LT_ObjCDecl, // An @interface, @implementation, or @protocol line. |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 60 | LT_ObjCMethodDecl, |
| 61 | LT_ObjCProperty // An @property line. |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 64 | class AnnotatedToken { |
| 65 | public: |
| 66 | AnnotatedToken(const FormatToken &FormatTok) |
Manuel Klimek | 94fc6f1 | 2013-01-10 19:17:33 +0000 | [diff] [blame] | 67 | : FormatTok(FormatTok), Type(TT_Unknown), SpaceRequiredBefore(false), |
| 68 | CanBreakBefore(false), MustBreakBefore(false), |
| 69 | ClosesTemplateDeclaration(false), Parent(NULL) {} |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 70 | |
| 71 | bool is(tok::TokenKind Kind) const { |
| 72 | return FormatTok.Tok.is(Kind); |
| 73 | } |
| 74 | bool isNot(tok::TokenKind Kind) const { |
| 75 | return FormatTok.Tok.isNot(Kind); |
| 76 | } |
| 77 | bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const { |
| 78 | return FormatTok.Tok.isObjCAtKeyword(Kind); |
| 79 | } |
| 80 | |
| 81 | FormatToken FormatTok; |
| 82 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 83 | TokenType Type; |
| 84 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 85 | bool SpaceRequiredBefore; |
| 86 | bool CanBreakBefore; |
| 87 | bool MustBreakBefore; |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 88 | |
| 89 | bool ClosesTemplateDeclaration; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 90 | |
| 91 | std::vector<AnnotatedToken> Children; |
| 92 | AnnotatedToken *Parent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 95 | static prec::Level getPrecedence(const AnnotatedToken &Tok) { |
| 96 | return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 99 | FormatStyle getLLVMStyle() { |
| 100 | FormatStyle LLVMStyle; |
| 101 | LLVMStyle.ColumnLimit = 80; |
| 102 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
| 103 | LLVMStyle.PointerAndReferenceBindToType = false; |
| 104 | LLVMStyle.AccessModifierOffset = -2; |
| 105 | LLVMStyle.SplitTemplateClosingGreater = true; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 106 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 107 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 108 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 109 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 110 | LLVMStyle.ObjCSpaceBeforeReturnType = true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 111 | return LLVMStyle; |
| 112 | } |
| 113 | |
| 114 | FormatStyle getGoogleStyle() { |
| 115 | FormatStyle GoogleStyle; |
| 116 | GoogleStyle.ColumnLimit = 80; |
| 117 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
| 118 | GoogleStyle.PointerAndReferenceBindToType = true; |
| 119 | GoogleStyle.AccessModifierOffset = -1; |
| 120 | GoogleStyle.SplitTemplateClosingGreater = false; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 121 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 122 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 123 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 124 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 125 | GoogleStyle.ObjCSpaceBeforeReturnType = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 126 | return GoogleStyle; |
| 127 | } |
| 128 | |
| 129 | struct OptimizationParameters { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 130 | unsigned PenaltyIndentLevel; |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 131 | unsigned PenaltyLevelDecrease; |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 132 | unsigned PenaltyExcessCharacter; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 135 | /// \brief Replaces the whitespace in front of \p Tok. Only call once for |
| 136 | /// each \c FormatToken. |
| 137 | static void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines, |
| 138 | unsigned Spaces, const FormatStyle &Style, |
| 139 | SourceManager &SourceMgr, |
| 140 | tooling::Replacements &Replaces) { |
| 141 | Replaces.insert(tooling::Replacement( |
| 142 | SourceMgr, Tok.FormatTok.WhiteSpaceStart, Tok.FormatTok.WhiteSpaceLength, |
| 143 | std::string(NewLines, '\n') + std::string(Spaces, ' '))); |
| 144 | } |
| 145 | |
| 146 | /// \brief Like \c replaceWhitespace, but additionally adds right-aligned |
| 147 | /// backslashes to escape newlines inside a preprocessor directive. |
| 148 | /// |
| 149 | /// This function and \c replaceWhitespace have the same behavior if |
| 150 | /// \c Newlines == 0. |
| 151 | static void replacePPWhitespace( |
| 152 | const AnnotatedToken &Tok, unsigned NewLines, unsigned Spaces, |
| 153 | unsigned WhitespaceStartColumn, const FormatStyle &Style, |
| 154 | SourceManager &SourceMgr, tooling::Replacements &Replaces) { |
| 155 | std::string NewLineText; |
| 156 | if (NewLines > 0) { |
| 157 | unsigned Offset = std::min<int>(Style.ColumnLimit - 1, |
| 158 | WhitespaceStartColumn); |
| 159 | for (unsigned i = 0; i < NewLines; ++i) { |
| 160 | NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' '); |
| 161 | NewLineText += "\\\n"; |
| 162 | Offset = 0; |
| 163 | } |
| 164 | } |
| 165 | Replaces.insert(tooling::Replacement(SourceMgr, Tok.FormatTok.WhiteSpaceStart, |
| 166 | Tok.FormatTok.WhiteSpaceLength, |
| 167 | NewLineText + std::string(Spaces, ' '))); |
| 168 | } |
| 169 | |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 170 | /// \brief Checks whether the (remaining) \c UnwrappedLine starting with |
| 171 | /// \p RootToken fits into \p Limit columns. |
| 172 | bool fitsIntoLimit(const AnnotatedToken &RootToken, unsigned Limit) { |
| 173 | unsigned Columns = RootToken.FormatTok.TokenLength; |
| 174 | bool FitsOnALine = true; |
| 175 | const AnnotatedToken *Tok = &RootToken; |
| 176 | while (!Tok->Children.empty()) { |
| 177 | Tok = &Tok->Children[0]; |
| 178 | Columns += (Tok->SpaceRequiredBefore ? 1 : 0) + Tok->FormatTok.TokenLength; |
| 179 | // A special case for the colon of a constructor initializer as this only |
| 180 | // needs to be put on a new line if the line needs to be split. |
| 181 | if (Columns > Limit || |
| 182 | (Tok->MustBreakBefore && Tok->Type != TT_CtorInitializerColon)) { |
| 183 | FitsOnALine = false; |
| 184 | break; |
| 185 | } |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 186 | } |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 187 | return FitsOnALine; |
| 188 | } |
| 189 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 190 | class UnwrappedLineFormatter { |
| 191 | public: |
Manuel Klimek | 94fc6f1 | 2013-01-10 19:17:33 +0000 | [diff] [blame] | 192 | UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, |
| 193 | const UnwrappedLine &Line, unsigned FirstIndent, |
| 194 | bool FitsOnALine, LineType CurrentLineType, |
| 195 | const AnnotatedToken &RootToken, |
| 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), |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 199 | CurrentLineType(CurrentLineType), RootToken(RootToken), |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 200 | Replaces(Replaces) { |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 201 | Parameters.PenaltyIndentLevel = 15; |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 202 | Parameters.PenaltyLevelDecrease = 30; |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 203 | Parameters.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 206 | /// \brief Formats an \c UnwrappedLine. |
| 207 | /// |
| 208 | /// \returns The column after the last token in the last line of the |
| 209 | /// \c UnwrappedLine. |
| 210 | unsigned format() { |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 211 | // Initialize state dependent on indent. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 212 | LineState State; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 213 | State.Column = FirstIndent; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 214 | State.NextToken = &RootToken; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 215 | State.Stack.push_back(ParenState(FirstIndent + 4, FirstIndent)); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 216 | State.ForLoopVariablePos = 0; |
| 217 | State.LineContainsContinuedForLoopSection = false; |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 218 | State.StartOfLineLevel = 1; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 219 | |
| 220 | // The first token has already been indented and thus consumed. |
| 221 | moveStateToNextToken(State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 222 | |
| 223 | // 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] | 224 | while (State.NextToken != NULL) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 225 | if (FitsOnALine) { |
| 226 | addTokenToState(false, false, State); |
| 227 | } else { |
| 228 | unsigned NoBreak = calcPenalty(State, false, UINT_MAX); |
| 229 | unsigned Break = calcPenalty(State, true, NoBreak); |
| 230 | addTokenToState(Break < NoBreak, false, State); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 231 | if (State.NextToken != NULL && |
| 232 | State.NextToken->Parent->Type == TT_CtorInitializerColon) { |
| 233 | if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine && |
| 234 | !fitsIntoLimit(*State.NextToken, |
| 235 | getColumnLimit() - State.Column - 1)) |
| 236 | State.Stack.back().BreakAfterComma = true; |
| 237 | } |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 238 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 239 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 240 | return State.Column; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | private: |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 244 | struct ParenState { |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 245 | ParenState(unsigned Indent, unsigned LastSpace) |
| 246 | : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0), |
| 247 | BreakBeforeClosingBrace(false), BreakAfterComma(false) {} |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 248 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 249 | /// \brief The position to which a specific parenthesis level needs to be |
| 250 | /// indented. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 251 | unsigned Indent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 252 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 253 | /// \brief The position of the last space on each level. |
| 254 | /// |
| 255 | /// Used e.g. to break like: |
| 256 | /// functionCall(Parameter, otherCall( |
| 257 | /// OtherParameter)); |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 258 | unsigned LastSpace; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 259 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 260 | /// \brief The position the first "<<" operator encountered on each level. |
| 261 | /// |
| 262 | /// Used to align "<<" operators. 0 if no such operator has been encountered |
| 263 | /// on a level. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 264 | unsigned FirstLessLess; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 265 | |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 266 | /// \brief Whether a newline needs to be inserted before the block's closing |
| 267 | /// brace. |
| 268 | /// |
| 269 | /// We only want to insert a newline before the closing brace if there also |
| 270 | /// was a newline after the beginning left brace. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 271 | bool BreakBeforeClosingBrace; |
| 272 | |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 273 | bool BreakAfterComma; |
| 274 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 275 | bool operator<(const ParenState &Other) const { |
| 276 | if (Indent != Other.Indent) |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 277 | return Indent < Other.Indent; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 278 | if (LastSpace != Other.LastSpace) |
| 279 | return LastSpace < Other.LastSpace; |
| 280 | if (FirstLessLess != Other.FirstLessLess) |
| 281 | return FirstLessLess < Other.FirstLessLess; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 282 | if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace) |
| 283 | return BreakBeforeClosingBrace; |
| 284 | return BreakAfterComma; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 285 | } |
| 286 | }; |
| 287 | |
| 288 | /// \brief The current state when indenting a unwrapped line. |
| 289 | /// |
| 290 | /// As the indenting tries different combinations this is copied by value. |
| 291 | struct LineState { |
| 292 | /// \brief The number of used columns in the current line. |
| 293 | unsigned Column; |
| 294 | |
| 295 | /// \brief The token that needs to be next formatted. |
| 296 | const AnnotatedToken *NextToken; |
| 297 | |
| 298 | /// \brief The parenthesis level of the first token on the current line. |
| 299 | unsigned StartOfLineLevel; |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 300 | |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 301 | /// \brief The column of the first variable in a for-loop declaration. |
| 302 | /// |
| 303 | /// Used to align the second variable if necessary. |
| 304 | unsigned ForLoopVariablePos; |
| 305 | |
| 306 | /// \brief \c true if this line contains a continued for-loop section. |
| 307 | bool LineContainsContinuedForLoopSection; |
| 308 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 309 | /// \brief A stack keeping track of properties applying to parenthesis |
| 310 | /// levels. |
| 311 | std::vector<ParenState> Stack; |
| 312 | |
| 313 | /// \brief Comparison operator to be able to used \c LineState in \c map. |
| 314 | bool operator<(const LineState &Other) const { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 315 | if (Other.NextToken != NextToken) |
| 316 | return Other.NextToken > NextToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 317 | if (Other.Column != Column) |
| 318 | return Other.Column > Column; |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 319 | if (Other.StartOfLineLevel != StartOfLineLevel) |
| 320 | return Other.StartOfLineLevel > StartOfLineLevel; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 321 | if (Other.ForLoopVariablePos != ForLoopVariablePos) |
| 322 | return Other.ForLoopVariablePos < ForLoopVariablePos; |
| 323 | if (Other.LineContainsContinuedForLoopSection != |
| 324 | LineContainsContinuedForLoopSection) |
| 325 | return LineContainsContinuedForLoopSection; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 326 | return Other.Stack < Stack; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 327 | } |
| 328 | }; |
| 329 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 330 | /// \brief Appends the next token to \p State and updates information |
| 331 | /// necessary for indentation. |
| 332 | /// |
| 333 | /// Puts the token on the current line if \p Newline is \c true and adds a |
| 334 | /// line break and necessary indentation otherwise. |
| 335 | /// |
| 336 | /// If \p DryRun is \c false, also creates and stores the required |
| 337 | /// \c Replacement. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 338 | void addTokenToState(bool Newline, bool DryRun, LineState &State) { |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 339 | const AnnotatedToken &Current = *State.NextToken; |
| 340 | const AnnotatedToken &Previous = *State.NextToken->Parent; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 341 | assert(State.Stack.size()); |
| 342 | unsigned ParenLevel = State.Stack.size() - 1; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 343 | |
| 344 | if (Newline) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 345 | unsigned WhitespaceStartColumn = State.Column; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 346 | if (Current.is(tok::r_brace)) { |
| 347 | State.Column = Line.Level * 2; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 348 | } else if (Current.is(tok::string_literal) && |
| 349 | Previous.is(tok::string_literal)) { |
| 350 | State.Column = State.Column - Previous.FormatTok.TokenLength; |
| 351 | } else if (Current.is(tok::lessless) && |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 352 | State.Stack[ParenLevel].FirstLessLess != 0) { |
| 353 | State.Column = State.Stack[ParenLevel].FirstLessLess; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 354 | } else if (ParenLevel != 0 && |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 355 | (Previous.is(tok::equal) || Current.is(tok::arrow) || |
| 356 | Current.is(tok::period) || Previous.is(tok::question) || |
| 357 | Previous.Type == TT_ConditionalExpr)) { |
| 358 | // Indent and extra 4 spaces after if we know the current expression is |
| 359 | // continued. Don't do that on the top level, as we already indent 4 |
| 360 | // there. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 361 | State.Column = State.Stack[ParenLevel].Indent + 4; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 362 | } else if (RootToken.is(tok::kw_for) && Previous.is(tok::comma)) { |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 363 | State.Column = State.ForLoopVariablePos; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 364 | } else if (State.NextToken->Parent->ClosesTemplateDeclaration) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 365 | State.Column = State.Stack[ParenLevel].Indent - 4; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 366 | } else { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 367 | State.Column = State.Stack[ParenLevel].Indent; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 370 | // A line starting with a closing brace is assumed to be correct for the |
| 371 | // same level as before the opening brace. |
| 372 | State.StartOfLineLevel = ParenLevel + (Current.is(tok::r_brace) ? 0 : 1); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 373 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 374 | if (RootToken.is(tok::kw_for)) |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 375 | State.LineContainsContinuedForLoopSection = Previous.isNot(tok::semi); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 376 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 377 | if (!DryRun) { |
| 378 | if (!Line.InPPDirective) |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 379 | replaceWhitespace(Current.FormatTok, 1, State.Column, Style, |
| 380 | SourceMgr, Replaces); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 381 | else |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 382 | replacePPWhitespace(Current.FormatTok, 1, State.Column, |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 383 | WhitespaceStartColumn, Style, SourceMgr, |
| 384 | Replaces); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 385 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 386 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 387 | State.Stack[ParenLevel].LastSpace = State.Column; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 388 | if (Current.is(tok::colon) && CurrentLineType != LT_ObjCMethodDecl && |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 389 | 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 | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 588 | const LineType CurrentLineType; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 589 | const AnnotatedToken &RootToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 590 | tooling::Replacements &Replaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 591 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 592 | // A map from an indent state to a pair (Result, Used-StopAt). |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 593 | typedef std::map<LineState, std::pair<unsigned, unsigned> > StateMap; |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 594 | StateMap Memory; |
| 595 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 596 | OptimizationParameters Parameters; |
| 597 | }; |
| 598 | |
| 599 | /// \brief Determines extra information about the tokens comprising an |
| 600 | /// \c UnwrappedLine. |
| 601 | class TokenAnnotator { |
| 602 | public: |
| 603 | TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style, |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 604 | SourceManager &SourceMgr, Lexer &Lex) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 605 | : Style(Style), SourceMgr(SourceMgr), Lex(Lex), |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 606 | RootToken(Line.RootToken) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 607 | |
| 608 | /// \brief A parser that gathers additional information about tokens. |
| 609 | /// |
| 610 | /// The \c TokenAnnotator tries to matches parenthesis and square brakets and |
| 611 | /// store a parenthesis levels. It also tries to resolve matching "<" and ">" |
| 612 | /// into template parameter lists. |
| 613 | class AnnotatingParser { |
| 614 | public: |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 615 | AnnotatingParser(AnnotatedToken &RootToken) |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 616 | : CurrentToken(&RootToken), KeywordVirtualFound(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() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 654 | while (CurrentToken != NULL) { |
| 655 | if (CurrentToken->is(tok::r_square)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 656 | next(); |
| 657 | return true; |
| 658 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 659 | if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_brace)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 660 | return false; |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 661 | if (!consumeToken()) |
| 662 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 663 | } |
| 664 | return false; |
| 665 | } |
| 666 | |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 667 | bool parseBrace() { |
| 668 | while (CurrentToken != NULL) { |
| 669 | if (CurrentToken->is(tok::r_brace)) { |
| 670 | next(); |
| 671 | return true; |
| 672 | } |
| 673 | if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square)) |
| 674 | return false; |
| 675 | if (!consumeToken()) |
| 676 | return false; |
| 677 | } |
| 678 | // Lines can currently end with '{'. |
| 679 | return true; |
| 680 | } |
| 681 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 682 | bool parseConditional() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 683 | while (CurrentToken != NULL) { |
| 684 | if (CurrentToken->is(tok::colon)) { |
| 685 | CurrentToken->Type = TT_ConditionalExpr; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 686 | next(); |
| 687 | return true; |
| 688 | } |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 689 | if (!consumeToken()) |
| 690 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 691 | } |
| 692 | return false; |
| 693 | } |
| 694 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 695 | bool parseTemplateDeclaration() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 696 | if (CurrentToken != NULL && CurrentToken->is(tok::less)) { |
| 697 | CurrentToken->Type = TT_TemplateOpener; |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 698 | next(); |
| 699 | if (!parseAngle()) |
| 700 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 701 | CurrentToken->Parent->ClosesTemplateDeclaration = true; |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 702 | parseLine(); |
| 703 | return true; |
| 704 | } |
| 705 | return false; |
| 706 | } |
| 707 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 708 | bool consumeToken() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 709 | AnnotatedToken *Tok = CurrentToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 710 | next(); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 711 | switch (Tok->FormatTok.Tok.getKind()) { |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 712 | case tok::plus: |
| 713 | case tok::minus: |
| 714 | // At the start of the line, +/- specific ObjectiveC method |
| 715 | // declarations. |
| 716 | if (Tok->Parent == NULL) |
| 717 | Tok->Type = TT_ObjCMethodSpecifier; |
| 718 | break; |
| 719 | case tok::l_paren: { |
| 720 | bool ParensWereObjCReturnType = |
| 721 | Tok->Parent && Tok->Parent->Type == TT_ObjCMethodSpecifier; |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 722 | if (!parseParens()) |
| 723 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 724 | if (CurrentToken != NULL && CurrentToken->is(tok::colon)) { |
| 725 | CurrentToken->Type = TT_CtorInitializerColon; |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 726 | next(); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 727 | } else if (CurrentToken != NULL && ParensWereObjCReturnType) { |
| 728 | CurrentToken->Type = TT_ObjCSelectorStart; |
| 729 | next(); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 730 | } |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 731 | } break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 732 | case tok::l_square: |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 733 | if (!parseSquare()) |
| 734 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 735 | break; |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 736 | case tok::l_brace: |
| 737 | if (!parseBrace()) |
| 738 | return false; |
| 739 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 740 | case tok::less: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 741 | if (parseAngle()) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 742 | Tok->Type = TT_TemplateOpener; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 743 | else { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 744 | Tok->Type = TT_BinaryOperator; |
| 745 | CurrentToken = Tok; |
| 746 | next(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 747 | } |
| 748 | break; |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 749 | case tok::r_paren: |
| 750 | case tok::r_square: |
| 751 | return false; |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 752 | case tok::r_brace: |
| 753 | // Lines can start with '}'. |
| 754 | if (Tok->Parent != NULL) |
| 755 | return false; |
| 756 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 757 | case tok::greater: |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 758 | Tok->Type = TT_BinaryOperator; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 759 | break; |
| 760 | case tok::kw_operator: |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 761 | if (CurrentToken->is(tok::l_paren)) { |
| 762 | CurrentToken->Type = TT_OverloadedOperator; |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 763 | next(); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 764 | if (CurrentToken != NULL && CurrentToken->is(tok::r_paren)) { |
| 765 | CurrentToken->Type = TT_OverloadedOperator; |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 766 | next(); |
| 767 | } |
| 768 | } else { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 769 | while (CurrentToken != NULL && CurrentToken->isNot(tok::l_paren)) { |
| 770 | CurrentToken->Type = TT_OverloadedOperator; |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 771 | next(); |
| 772 | } |
| 773 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 774 | break; |
| 775 | case tok::question: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 776 | parseConditional(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 777 | break; |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 778 | case tok::kw_template: |
| 779 | parseTemplateDeclaration(); |
| 780 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 781 | default: |
| 782 | break; |
| 783 | } |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 784 | return true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 787 | void parseIncludeDirective() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 788 | while (CurrentToken != NULL) { |
| 789 | if (CurrentToken->is(tok::slash)) |
| 790 | CurrentToken->Type = TT_DirectorySeparator; |
| 791 | else if (CurrentToken->is(tok::less)) |
| 792 | CurrentToken->Type = TT_TemplateOpener; |
| 793 | else if (CurrentToken->is(tok::greater)) |
| 794 | CurrentToken->Type = TT_TemplateCloser; |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 795 | next(); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | void parsePreprocessorDirective() { |
| 800 | next(); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 801 | if (CurrentToken == NULL) |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 802 | return; |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 803 | // Hashes in the middle of a line can lead to any strange token |
| 804 | // sequence. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 805 | if (CurrentToken->FormatTok.Tok.getIdentifierInfo() == NULL) |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 806 | return; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 807 | switch ( |
| 808 | CurrentToken->FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) { |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 809 | case tok::pp_include: |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 810 | case tok::pp_import: |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 811 | parseIncludeDirective(); |
| 812 | break; |
| 813 | default: |
| 814 | break; |
| 815 | } |
| 816 | } |
| 817 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 818 | LineType parseLine() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 819 | if (CurrentToken->is(tok::hash)) { |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 820 | parsePreprocessorDirective(); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 821 | return LT_PreprocessorDirective; |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 822 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 823 | while (CurrentToken != NULL) { |
| 824 | if (CurrentToken->is(tok::kw_virtual)) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 825 | KeywordVirtualFound = true; |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 826 | if (!consumeToken()) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 827 | return LT_Invalid; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 828 | } |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 829 | if (KeywordVirtualFound) |
| 830 | return LT_VirtualFunctionDecl; |
| 831 | return LT_Other; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | void next() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 835 | if (CurrentToken != NULL && !CurrentToken->Children.empty()) |
| 836 | CurrentToken = &CurrentToken->Children[0]; |
| 837 | else |
| 838 | CurrentToken = NULL; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | private: |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 842 | AnnotatedToken *CurrentToken; |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 843 | bool KeywordVirtualFound; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 844 | }; |
| 845 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 846 | void createAnnotatedTokens(AnnotatedToken &Current) { |
| 847 | if (!Current.FormatTok.Children.empty()) { |
| 848 | Current.Children.push_back(AnnotatedToken(Current.FormatTok.Children[0])); |
| 849 | Current.Children.back().Parent = &Current; |
| 850 | createAnnotatedTokens(Current.Children.back()); |
| 851 | } |
| 852 | } |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 853 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 854 | void calculateExtraInformation(AnnotatedToken &Current) { |
| 855 | Current.SpaceRequiredBefore = spaceRequiredBefore(Current); |
| 856 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 857 | if (Current.FormatTok.MustBreakBefore) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 858 | Current.MustBreakBefore = true; |
| 859 | } else { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 860 | if (Current.Type == TT_CtorInitializerColon || Current.Parent->Type == |
| 861 | TT_LineComment || (Current.is(tok::string_literal) && |
| 862 | Current.Parent->is(tok::string_literal))) { |
| 863 | Current.MustBreakBefore = true; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 864 | } else { |
| 865 | Current.MustBreakBefore = false; |
| 866 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 867 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 868 | Current.CanBreakBefore = Current.MustBreakBefore || canBreakBefore(Current); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 869 | if (!Current.Children.empty()) |
| 870 | calculateExtraInformation(Current.Children[0]); |
| 871 | } |
| 872 | |
| 873 | bool annotate() { |
| 874 | createAnnotatedTokens(RootToken); |
| 875 | |
| 876 | AnnotatingParser Parser(RootToken); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 877 | CurrentLineType = Parser.parseLine(); |
| 878 | if (CurrentLineType == LT_Invalid) |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 879 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 880 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 881 | determineTokenTypes(RootToken, /*IsRHS=*/false); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 882 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 883 | if (RootToken.Type == TT_ObjCMethodSpecifier) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 884 | CurrentLineType = LT_ObjCMethodDecl; |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 885 | else if (RootToken.Type == TT_ObjCDecl) |
| 886 | CurrentLineType = LT_ObjCDecl; |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 887 | else if (RootToken.Type == TT_ObjCProperty) |
| 888 | CurrentLineType = LT_ObjCProperty; |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 889 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 890 | if (!RootToken.Children.empty()) |
| 891 | calculateExtraInformation(RootToken.Children[0]); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 892 | return true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 895 | LineType getLineType() { |
| 896 | return CurrentLineType; |
| 897 | } |
| 898 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 899 | const AnnotatedToken &getRootToken() { |
| 900 | return RootToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | private: |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 904 | void determineTokenTypes(AnnotatedToken &Current, bool IsRHS) { |
| 905 | if (getPrecedence(Current) == prec::Assignment || |
| 906 | Current.is(tok::kw_return) || Current.is(tok::kw_throw)) |
| 907 | IsRHS = true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 908 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 909 | if (Current.Type == TT_Unknown) { |
| 910 | if (Current.is(tok::star) || Current.is(tok::amp)) { |
| 911 | Current.Type = determineStarAmpUsage(Current, IsRHS); |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 912 | } else if (Current.is(tok::minus) || Current.is(tok::plus) || |
| 913 | Current.is(tok::caret)) { |
| 914 | Current.Type = determinePlusMinusCaretUsage(Current); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 915 | } else if (Current.is(tok::minusminus) || Current.is(tok::plusplus)) { |
| 916 | Current.Type = determineIncrementUsage(Current); |
| 917 | } else if (Current.is(tok::exclaim)) { |
| 918 | Current.Type = TT_UnaryOperator; |
| 919 | } else if (isBinaryOperator(Current)) { |
| 920 | Current.Type = TT_BinaryOperator; |
| 921 | } else if (Current.is(tok::comment)) { |
| 922 | std::string Data(Lexer::getSpelling(Current.FormatTok.Tok, SourceMgr, |
| 923 | Lex.getLangOpts())); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 924 | if (StringRef(Data).startswith("//")) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 925 | Current.Type = TT_LineComment; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 926 | else |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 927 | Current.Type = TT_BlockComment; |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 928 | } else if (Current.is(tok::r_paren) && |
| 929 | (Current.Parent->Type == TT_PointerOrReference || |
| 930 | Current.Parent->Type == TT_TemplateCloser)) { |
| 931 | // FIXME: We need to get smarter and understand more cases of casts. |
| 932 | Current.Type = TT_CastRParen; |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 933 | } else if (Current.is(tok::at) && Current.Children.size()) { |
| 934 | switch (Current.Children[0].FormatTok.Tok.getObjCKeywordID()) { |
| 935 | case tok::objc_interface: |
| 936 | case tok::objc_implementation: |
| 937 | case tok::objc_protocol: |
| 938 | Current.Type = TT_ObjCDecl; |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 939 | break; |
| 940 | case tok::objc_property: |
| 941 | Current.Type = TT_ObjCProperty; |
| 942 | break; |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 943 | default: |
| 944 | break; |
| 945 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 946 | } |
| 947 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 948 | |
| 949 | if (!Current.Children.empty()) |
| 950 | determineTokenTypes(Current.Children[0], IsRHS); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 953 | bool isBinaryOperator(const AnnotatedToken &Tok) { |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 954 | // 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] | 955 | return getPrecedence(Tok) > prec::Comma; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 958 | TokenType determineStarAmpUsage(const AnnotatedToken &Tok, bool IsRHS) { |
| 959 | if (Tok.Parent == NULL) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 960 | return TT_UnaryOperator; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 961 | if (Tok.Children.size() == 0) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 962 | return TT_Unknown; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 963 | const FormatToken &PrevToken = Tok.Parent->FormatTok; |
| 964 | const FormatToken &NextToken = Tok.Children[0].FormatTok; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 965 | |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 966 | if (PrevToken.Tok.is(tok::l_paren) || PrevToken.Tok.is(tok::l_square) || |
| 967 | PrevToken.Tok.is(tok::comma) || PrevToken.Tok.is(tok::kw_return) || |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 968 | PrevToken.Tok.is(tok::colon) || Tok.Parent->Type == TT_BinaryOperator || |
| 969 | Tok.Parent->Type == TT_CastRParen) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 970 | return TT_UnaryOperator; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 971 | |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 972 | if (PrevToken.Tok.isLiteral() || NextToken.Tok.isLiteral() || |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 973 | NextToken.Tok.is(tok::plus) || NextToken.Tok.is(tok::minus) || |
| 974 | NextToken.Tok.is(tok::plusplus) || NextToken.Tok.is(tok::minusminus) || |
| 975 | NextToken.Tok.is(tok::tilde) || NextToken.Tok.is(tok::exclaim) || |
| 976 | NextToken.Tok.is(tok::kw_alignof) || NextToken.Tok.is(tok::kw_sizeof)) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 977 | return TT_BinaryOperator; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 978 | |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 979 | if (NextToken.Tok.is(tok::comma) || NextToken.Tok.is(tok::r_paren) || |
| 980 | NextToken.Tok.is(tok::greater)) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 981 | return TT_PointerOrReference; |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 982 | |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 983 | // It is very unlikely that we are going to find a pointer or reference type |
| 984 | // definition on the RHS of an assignment. |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 985 | if (IsRHS) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 986 | return TT_BinaryOperator; |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 987 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 988 | return TT_PointerOrReference; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 991 | TokenType determinePlusMinusCaretUsage(const AnnotatedToken &Tok) { |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 992 | // Use heuristics to recognize unary operators. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 993 | if (Tok.Parent->is(tok::equal) || Tok.Parent->is(tok::l_paren) || |
| 994 | Tok.Parent->is(tok::comma) || Tok.Parent->is(tok::l_square) || |
| 995 | Tok.Parent->is(tok::question) || Tok.Parent->is(tok::colon) || |
Nico Weber | 81ed2f1 | 2013-01-10 19:36:35 +0000 | [diff] [blame] | 996 | Tok.Parent->is(tok::kw_return) || Tok.Parent->is(tok::kw_case) || |
| 997 | Tok.Parent->is(tok::at)) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 998 | return TT_UnaryOperator; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 999 | |
| 1000 | // There can't be to consecutive binary operators. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1001 | if (Tok.Parent->Type == TT_BinaryOperator) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1002 | return TT_UnaryOperator; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1003 | |
| 1004 | // Fall back to marking the token as binary operator. |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1005 | return TT_BinaryOperator; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | /// \brief Determine whether ++/-- are pre- or post-increments/-decrements. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1009 | TokenType determineIncrementUsage(const AnnotatedToken &Tok) { |
| 1010 | if (Tok.Parent != NULL && Tok.Parent->is(tok::identifier)) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1011 | return TT_TrailingUnaryOperator; |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1012 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1013 | return TT_UnaryOperator; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1016 | bool spaceRequiredBetween(const AnnotatedToken &Left, |
| 1017 | const AnnotatedToken &Right) { |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 1018 | if (Right.is(tok::hashhash)) |
| 1019 | return Left.is(tok::hash); |
| 1020 | if (Left.is(tok::hashhash) || Left.is(tok::hash)) |
| 1021 | return Right.is(tok::hash); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1022 | if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma)) |
| 1023 | return false; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1024 | if (Right.is(tok::less) && |
| 1025 | (Left.is(tok::kw_template) || |
| 1026 | (CurrentLineType == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList))) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1027 | return true; |
| 1028 | if (Left.is(tok::arrow) || Right.is(tok::arrow)) |
| 1029 | return false; |
| 1030 | if (Left.is(tok::exclaim) || Left.is(tok::tilde)) |
| 1031 | return false; |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 1032 | if (Left.is(tok::at) && |
| 1033 | (Right.is(tok::identifier) || Right.is(tok::string_literal) || |
| 1034 | Right.is(tok::char_constant) || Right.is(tok::numeric_constant) || |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1035 | Right.is(tok::l_paren) || Right.is(tok::l_brace) || |
| 1036 | Right.is(tok::kw_true) || Right.is(tok::kw_false))) |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1037 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1038 | if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less)) |
| 1039 | return false; |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 1040 | if (Right.is(tok::amp) || Right.is(tok::star)) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1041 | return Left.FormatTok.Tok.isLiteral() || |
Daniel Jasper | d7610b8 | 2012-12-24 16:51:15 +0000 | [diff] [blame] | 1042 | (Left.isNot(tok::star) && Left.isNot(tok::amp) && |
| 1043 | !Style.PointerAndReferenceBindToType); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1044 | if (Left.is(tok::amp) || Left.is(tok::star)) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1045 | return Right.FormatTok.Tok.isLiteral() || |
| 1046 | Style.PointerAndReferenceBindToType; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1047 | if (Right.is(tok::star) && Left.is(tok::l_paren)) |
| 1048 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1049 | if (Left.is(tok::l_square) || Right.is(tok::l_square) || |
| 1050 | Right.is(tok::r_square)) |
| 1051 | return false; |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 1052 | if (Left.is(tok::coloncolon) || |
| 1053 | (Right.is(tok::coloncolon) && |
| 1054 | (Left.is(tok::identifier) || Left.is(tok::greater)))) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1055 | return false; |
| 1056 | if (Left.is(tok::period) || Right.is(tok::period)) |
| 1057 | return false; |
| 1058 | if (Left.is(tok::colon) || Right.is(tok::colon)) |
| 1059 | return true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1060 | if (Left.is(tok::l_paren)) |
| 1061 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1062 | if (Right.is(tok::l_paren)) { |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1063 | return CurrentLineType == LT_ObjCDecl || Left.is(tok::kw_if) || |
| 1064 | Left.is(tok::kw_for) || Left.is(tok::kw_while) || |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1065 | Left.is(tok::kw_switch) || Left.is(tok::kw_return) || |
| 1066 | Left.is(tok::kw_catch); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1067 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1068 | if (Left.is(tok::at) && |
| 1069 | Right.FormatTok.Tok.getObjCKeywordID() != tok::objc_not_keyword) |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1070 | return false; |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1071 | if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) |
| 1072 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1073 | return true; |
| 1074 | } |
| 1075 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1076 | bool spaceRequiredBefore(const AnnotatedToken &Tok) { |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1077 | if (CurrentLineType == LT_ObjCMethodDecl) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1078 | if (Tok.is(tok::identifier) && !Tok.Children.empty() && |
| 1079 | Tok.Children[0].is(tok::colon) && Tok.Parent->is(tok::identifier)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1080 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1081 | if (Tok.is(tok::colon)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1082 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1083 | if (Tok.Parent->Type == TT_ObjCMethodSpecifier) |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1084 | return Style.ObjCSpaceBeforeReturnType || Tok.isNot(tok::l_paren); |
| 1085 | if (Tok.Type == TT_ObjCSelectorStart) |
| 1086 | return !Style.ObjCSpaceBeforeReturnType; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1087 | if (Tok.Parent->is(tok::r_paren) && Tok.is(tok::identifier)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1088 | // Don't space between ')' and <id> |
| 1089 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1090 | if (Tok.Parent->is(tok::colon) && Tok.is(tok::l_paren)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1091 | // Don't space between ':' and '(' |
| 1092 | return false; |
| 1093 | } |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 1094 | if (CurrentLineType == LT_ObjCProperty && |
| 1095 | (Tok.is(tok::equal) || Tok.Parent->is(tok::equal))) |
| 1096 | return false; |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1097 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1098 | if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1099 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1100 | if (Tok.Type == TT_OverloadedOperator) |
| 1101 | return Tok.is(tok::identifier) || Tok.is(tok::kw_new) || |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1102 | Tok.is(tok::kw_delete) || Tok.is(tok::kw_bool); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1103 | if (Tok.Parent->Type == TT_OverloadedOperator) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1104 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1105 | if (Tok.is(tok::colon)) |
| 1106 | return RootToken.isNot(tok::kw_case) && (!Tok.Children.empty()); |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 1107 | if (Tok.Parent->Type == TT_UnaryOperator || |
| 1108 | Tok.Parent->Type == TT_CastRParen) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1109 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1110 | if (Tok.Type == TT_UnaryOperator) |
| 1111 | return Tok.Parent->isNot(tok::l_paren) && |
Nico Weber | 81ed2f1 | 2013-01-10 19:36:35 +0000 | [diff] [blame] | 1112 | Tok.Parent->isNot(tok::l_square) && |
| 1113 | Tok.Parent->isNot(tok::at); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1114 | if (Tok.Parent->is(tok::greater) && Tok.is(tok::greater)) { |
| 1115 | return Tok.Type == TT_TemplateCloser && Tok.Parent->Type == |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1116 | TT_TemplateCloser && Style.SplitTemplateClosingGreater; |
| 1117 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1118 | if (Tok.Type == TT_DirectorySeparator || |
| 1119 | Tok.Parent->Type == TT_DirectorySeparator) |
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.Type == TT_BinaryOperator || Tok.Parent->Type == TT_BinaryOperator) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1122 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1123 | if (Tok.Parent->Type == TT_TemplateCloser && Tok.is(tok::l_paren)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1124 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1125 | if (Tok.is(tok::less) && RootToken.is(tok::hash)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1126 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1127 | if (Tok.Type == TT_TrailingUnaryOperator) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1128 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1129 | return spaceRequiredBetween(*Tok.Parent, Tok); |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1132 | bool canBreakBefore(const AnnotatedToken &Right) { |
| 1133 | const AnnotatedToken &Left = *Right.Parent; |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1134 | if (CurrentLineType == LT_ObjCMethodDecl) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1135 | if (Right.is(tok::identifier) && !Right.Children.empty() && |
| 1136 | Right.Children[0].is(tok::colon) && Left.is(tok::identifier)) |
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 (CurrentLineType == LT_ObjCMethodDecl && Right.is(tok::identifier) && |
| 1139 | Left.is(tok::l_paren) && Left.Parent->is(tok::colon)) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1140 | // Don't break this identifier as ':' or identifier |
| 1141 | // before it will break. |
| 1142 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1143 | if (Right.is(tok::colon) && Left.is(tok::identifier) && |
| 1144 | Left.CanBreakBefore) |
Daniel Jasper | da92771 | 2013-01-07 15:36:15 +0000 | [diff] [blame] | 1145 | // Don't break at ':' if identifier before it can beak. |
| 1146 | return false; |
| 1147 | } |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1148 | if (Left.ClosesTemplateDeclaration) |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 1149 | return true; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1150 | if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser || |
Daniel Jasper | 2db356d | 2013-01-08 20:03:18 +0000 | [diff] [blame] | 1151 | Left.Type == TT_UnaryOperator || Right.Type == TT_ConditionalExpr) |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1152 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1153 | if (Left.is(tok::equal) && CurrentLineType == LT_VirtualFunctionDecl) |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1154 | return false; |
| 1155 | |
Daniel Jasper | 043835a | 2013-01-09 09:33:39 +0000 | [diff] [blame] | 1156 | if (Right.is(tok::comment)) |
| 1157 | return !Right.Children.empty(); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1158 | if (Right.is(tok::r_paren) || Right.is(tok::l_brace) || |
Daniel Jasper | 043835a | 2013-01-09 09:33:39 +0000 | [diff] [blame] | 1159 | Right.is(tok::greater)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1160 | return false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1161 | return (isBinaryOperator(Left) && Left.isNot(tok::lessless)) || |
| 1162 | Left.is(tok::comma) || Right.is(tok::lessless) || |
| 1163 | Right.is(tok::arrow) || Right.is(tok::period) || |
| 1164 | Right.is(tok::colon) || Left.is(tok::semi) || |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 1165 | Left.is(tok::l_brace) || Left.is(tok::question) || |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 1166 | Right.is(tok::r_brace) || Left.Type == TT_ConditionalExpr || |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1167 | (Left.is(tok::r_paren) && Left.Type != TT_CastRParen && |
| 1168 | Right.is(tok::identifier)) || |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1169 | (Left.is(tok::l_paren) && !Right.is(tok::r_paren)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1172 | FormatStyle Style; |
| 1173 | SourceManager &SourceMgr; |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 1174 | Lexer &Lex; |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1175 | LineType CurrentLineType; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1176 | AnnotatedToken RootToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1177 | }; |
| 1178 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1179 | class LexerBasedFormatTokenSource : public FormatTokenSource { |
| 1180 | public: |
| 1181 | LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1182 | : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1183 | IdentTable(Lex.getLangOpts()) { |
| 1184 | Lex.SetKeepWhitespaceMode(true); |
| 1185 | } |
| 1186 | |
| 1187 | virtual FormatToken getNextToken() { |
| 1188 | if (GreaterStashed) { |
| 1189 | FormatTok.NewlinesBefore = 0; |
| 1190 | FormatTok.WhiteSpaceStart = |
| 1191 | FormatTok.Tok.getLocation().getLocWithOffset(1); |
| 1192 | FormatTok.WhiteSpaceLength = 0; |
| 1193 | GreaterStashed = false; |
| 1194 | return FormatTok; |
| 1195 | } |
| 1196 | |
| 1197 | FormatTok = FormatToken(); |
| 1198 | Lex.LexFromRawLexer(FormatTok.Tok); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1199 | StringRef Text = rawTokenText(FormatTok.Tok); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1200 | FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation(); |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 1201 | if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0) |
| 1202 | FormatTok.IsFirst = true; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1203 | |
| 1204 | // Consume and record whitespace until we find a significant token. |
| 1205 | while (FormatTok.Tok.is(tok::unknown)) { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 1206 | FormatTok.NewlinesBefore += Text.count('\n'); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1207 | FormatTok.HasUnescapedNewline = Text.count("\\\n") != |
| 1208 | FormatTok.NewlinesBefore; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1209 | FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength(); |
| 1210 | |
| 1211 | if (FormatTok.Tok.is(tok::eof)) |
| 1212 | return FormatTok; |
| 1213 | Lex.LexFromRawLexer(FormatTok.Tok); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1214 | Text = rawTokenText(FormatTok.Tok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1215 | } |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1216 | |
| 1217 | // Now FormatTok is the next non-whitespace token. |
| 1218 | FormatTok.TokenLength = Text.size(); |
| 1219 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1220 | // In case the token starts with escaped newlines, we want to |
| 1221 | // take them into account as whitespace - this pattern is quite frequent |
| 1222 | // in macro definitions. |
| 1223 | // FIXME: What do we want to do with other escaped spaces, and escaped |
| 1224 | // spaces or newlines in the middle of tokens? |
| 1225 | // FIXME: Add a more explicit test. |
| 1226 | unsigned i = 0; |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1227 | while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1228 | FormatTok.WhiteSpaceLength += 2; |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1229 | FormatTok.TokenLength -= 2; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1230 | i += 2; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | if (FormatTok.Tok.is(tok::raw_identifier)) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1234 | IdentifierInfo &Info = IdentTable.get(Text); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1235 | FormatTok.Tok.setIdentifierInfo(&Info); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1236 | FormatTok.Tok.setKind(Info.getTokenID()); |
| 1237 | } |
| 1238 | |
| 1239 | if (FormatTok.Tok.is(tok::greatergreater)) { |
| 1240 | FormatTok.Tok.setKind(tok::greater); |
| 1241 | GreaterStashed = true; |
| 1242 | } |
| 1243 | |
| 1244 | return FormatTok; |
| 1245 | } |
| 1246 | |
| 1247 | private: |
| 1248 | FormatToken FormatTok; |
| 1249 | bool GreaterStashed; |
| 1250 | Lexer &Lex; |
| 1251 | SourceManager &SourceMgr; |
| 1252 | IdentifierTable IdentTable; |
| 1253 | |
| 1254 | /// Returns the text of \c FormatTok. |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1255 | StringRef rawTokenText(Token &Tok) { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1256 | return StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
| 1257 | Tok.getLength()); |
| 1258 | } |
| 1259 | }; |
| 1260 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1261 | class Formatter : public UnwrappedLineConsumer { |
| 1262 | public: |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1263 | Formatter(clang::DiagnosticsEngine &Diag, const FormatStyle &Style, |
| 1264 | Lexer &Lex, SourceManager &SourceMgr, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1265 | const std::vector<CharSourceRange> &Ranges) |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1266 | : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr), |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1267 | Ranges(Ranges) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1268 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1269 | virtual ~Formatter() {} |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 1270 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1271 | tooling::Replacements format() { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 1272 | LexerBasedFormatTokenSource Tokens(Lex, SourceMgr); |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1273 | UnwrappedLineParser Parser(Diag, Style, Tokens, *this); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1274 | StructuralError = Parser.parse(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1275 | unsigned PreviousEndOfLineColumn = 0; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1276 | for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(), |
| 1277 | E = UnwrappedLines.end(); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1278 | I != E; ++I) { |
| 1279 | const UnwrappedLine &TheLine = *I; |
| 1280 | if (touchesRanges(TheLine)) { |
| 1281 | TokenAnnotator Annotator(TheLine, Style, SourceMgr, Lex); |
| 1282 | if (!Annotator.annotate()) |
| 1283 | break; |
| 1284 | unsigned Indent = formatFirstToken(Annotator.getRootToken(), |
| 1285 | TheLine.Level, TheLine.InPPDirective, |
| 1286 | PreviousEndOfLineColumn); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1287 | unsigned Limit = Style.ColumnLimit - (TheLine.InPPDirective ? 1 : 0) - |
| 1288 | Indent; |
| 1289 | // Check whether the UnwrappedLine can be put onto a single line. If |
| 1290 | // so, this is bound to be the optimal solution (by definition) and we |
| 1291 | // don't need to analyze the entire solution space. |
| 1292 | bool FitsOnALine = fitsIntoLimit(Annotator.getRootToken(), Limit); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1293 | UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent, |
| 1294 | FitsOnALine, Annotator.getLineType(), |
| 1295 | Annotator.getRootToken(), Replaces, |
| 1296 | StructuralError); |
| 1297 | PreviousEndOfLineColumn = Formatter.format(); |
| 1298 | } else { |
| 1299 | // If we did not reformat this unwrapped line, the column at the end of |
| 1300 | // the last token is unchanged - thus, we can calculate the end of the |
| 1301 | // last token, and return the result. |
| 1302 | const FormatToken *Last = getLastLine(TheLine); |
| 1303 | PreviousEndOfLineColumn = |
| 1304 | SourceMgr.getSpellingColumnNumber(Last->Tok.getLocation()) + |
| 1305 | Lex.MeasureTokenLength(Last->Tok.getLocation(), SourceMgr, |
| 1306 | Lex.getLangOpts()) - |
| 1307 | 1; |
| 1308 | } |
| 1309 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1310 | return Replaces; |
| 1311 | } |
| 1312 | |
| 1313 | private: |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1314 | const FormatToken *getLastLine(const UnwrappedLine &TheLine) { |
| 1315 | const FormatToken *Last = &TheLine.RootToken; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1316 | while (!Last->Children.empty()) |
| 1317 | Last = &Last->Children.back(); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1318 | return Last; |
| 1319 | } |
| 1320 | |
| 1321 | bool touchesRanges(const UnwrappedLine &TheLine) { |
| 1322 | const FormatToken *First = &TheLine.RootToken; |
| 1323 | const FormatToken *Last = getLastLine(TheLine); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1324 | CharSourceRange LineRange = CharSourceRange::getTokenRange( |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1325 | First->Tok.getLocation(), |
| 1326 | Last->Tok.getLocation()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1327 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1328 | if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(), |
| 1329 | Ranges[i].getBegin()) && |
| 1330 | !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 1331 | LineRange.getBegin())) |
| 1332 | return true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1333 | } |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1334 | return false; |
| 1335 | } |
| 1336 | |
| 1337 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
| 1338 | UnwrappedLines.push_back(TheLine); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1341 | /// \brief Add a new line and the required indent before the first Token |
| 1342 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 1343 | /// Returns the indent level of the \c UnwrappedLine. |
| 1344 | unsigned formatFirstToken(const AnnotatedToken &RootToken, unsigned Level, |
| 1345 | bool InPPDirective, |
| 1346 | unsigned PreviousEndOfLineColumn) { |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1347 | const FormatToken &Tok = RootToken.FormatTok; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1348 | if (!Tok.WhiteSpaceStart.isValid() || StructuralError) |
| 1349 | return SourceMgr.getSpellingColumnNumber(Tok.Tok.getLocation()) - 1; |
| 1350 | |
| 1351 | unsigned Newlines = std::min(Tok.NewlinesBefore, |
| 1352 | Style.MaxEmptyLinesToKeep + 1); |
| 1353 | if (Newlines == 0 && !Tok.IsFirst) |
| 1354 | Newlines = 1; |
| 1355 | unsigned Indent = Level * 2; |
| 1356 | |
| 1357 | bool IsAccessModifier = false; |
| 1358 | if (RootToken.is(tok::kw_public) || RootToken.is(tok::kw_protected) || |
| 1359 | RootToken.is(tok::kw_private)) |
| 1360 | IsAccessModifier = true; |
| 1361 | else if (RootToken.is(tok::at) && !RootToken.Children.empty() && |
| 1362 | (RootToken.Children[0].isObjCAtKeyword(tok::objc_public) || |
| 1363 | RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) || |
| 1364 | RootToken.Children[0].isObjCAtKeyword(tok::objc_package) || |
| 1365 | RootToken.Children[0].isObjCAtKeyword(tok::objc_private))) |
| 1366 | IsAccessModifier = true; |
| 1367 | |
| 1368 | if (IsAccessModifier && |
| 1369 | static_cast<int>(Indent) + Style.AccessModifierOffset >= 0) |
| 1370 | Indent += Style.AccessModifierOffset; |
| 1371 | if (!InPPDirective || Tok.HasUnescapedNewline) { |
| 1372 | replaceWhitespace(Tok, Newlines, Indent, Style, SourceMgr, Replaces); |
| 1373 | } else { |
| 1374 | replacePPWhitespace(Tok, Newlines, Indent, PreviousEndOfLineColumn, Style, |
| 1375 | SourceMgr, Replaces); |
| 1376 | } |
| 1377 | return Indent; |
| 1378 | } |
| 1379 | |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1380 | clang::DiagnosticsEngine &Diag; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1381 | FormatStyle Style; |
| 1382 | Lexer &Lex; |
| 1383 | SourceManager &SourceMgr; |
| 1384 | tooling::Replacements Replaces; |
| 1385 | std::vector<CharSourceRange> Ranges; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1386 | std::vector<UnwrappedLine> UnwrappedLines; |
| 1387 | bool StructuralError; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1388 | }; |
| 1389 | |
| 1390 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 1391 | SourceManager &SourceMgr, |
| 1392 | std::vector<CharSourceRange> Ranges) { |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1393 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 1394 | TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts); |
| 1395 | DiagnosticPrinter.BeginSourceFile(Lex.getLangOpts(), Lex.getPP()); |
| 1396 | DiagnosticsEngine Diagnostics( |
| 1397 | llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
| 1398 | &DiagnosticPrinter, false); |
| 1399 | Diagnostics.setSourceManager(&SourceMgr); |
| 1400 | Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1401 | return formatter.format(); |
| 1402 | } |
| 1403 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1404 | LangOptions getFormattingLangOpts() { |
| 1405 | LangOptions LangOpts; |
| 1406 | LangOpts.CPlusPlus = 1; |
| 1407 | LangOpts.CPlusPlus11 = 1; |
| 1408 | LangOpts.Bool = 1; |
| 1409 | LangOpts.ObjC1 = 1; |
| 1410 | LangOpts.ObjC2 = 1; |
| 1411 | return LangOpts; |
| 1412 | } |
| 1413 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1414 | } // namespace format |
| 1415 | } // namespace clang |