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