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