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