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 | /// |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "format-formatter" |
| 17 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 18 | #include "BreakableToken.h" |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 19 | #include "TokenAnnotator.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "UnwrappedLineParser.h" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 21 | #include "WhitespaceManager.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" |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Allocator.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 31 | #include <queue> |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 32 | #include <string> |
| 33 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 34 | namespace clang { |
| 35 | namespace format { |
| 36 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 37 | FormatStyle getLLVMStyle() { |
| 38 | FormatStyle LLVMStyle; |
| 39 | LLVMStyle.ColumnLimit = 80; |
| 40 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 41 | LLVMStyle.PointerBindsToType = false; |
| 42 | LLVMStyle.DerivePointerBinding = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 43 | LLVMStyle.AccessModifierOffset = -2; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 44 | LLVMStyle.Standard = FormatStyle::LS_Cpp03; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 45 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 46 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 47 | LLVMStyle.BinPackParameters = true; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 48 | LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 49 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 50 | LLVMStyle.AllowShortIfStatementsOnASingleLine = false; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 51 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 52 | LLVMStyle.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 53 | LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 75; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 54 | return LLVMStyle; |
| 55 | } |
| 56 | |
| 57 | FormatStyle getGoogleStyle() { |
| 58 | FormatStyle GoogleStyle; |
| 59 | GoogleStyle.ColumnLimit = 80; |
| 60 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 61 | GoogleStyle.PointerBindsToType = true; |
| 62 | GoogleStyle.DerivePointerBinding = true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 63 | GoogleStyle.AccessModifierOffset = -1; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 64 | GoogleStyle.Standard = FormatStyle::LS_Auto; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 65 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 66 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 67 | GoogleStyle.BinPackParameters = true; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 68 | GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 69 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 70 | GoogleStyle.AllowShortIfStatementsOnASingleLine = false; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 71 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 72 | GoogleStyle.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 73 | GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 74 | return GoogleStyle; |
| 75 | } |
| 76 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 77 | FormatStyle getChromiumStyle() { |
| 78 | FormatStyle ChromiumStyle = getGoogleStyle(); |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 79 | ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 80 | ChromiumStyle.BinPackParameters = false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 81 | ChromiumStyle.Standard = FormatStyle::LS_Cpp03; |
| 82 | ChromiumStyle.DerivePointerBinding = false; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 83 | return ChromiumStyle; |
| 84 | } |
| 85 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 86 | // Returns the length of everything up to the first possible line break after |
| 87 | // the ), ], } or > matching \c Tok. |
| 88 | static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) { |
| 89 | if (Tok.MatchingParen == NULL) |
| 90 | return 0; |
| 91 | AnnotatedToken *End = Tok.MatchingParen; |
| 92 | while (!End->Children.empty() && !End->Children[0].CanBreakBefore) { |
| 93 | End = &End->Children[0]; |
| 94 | } |
| 95 | return End->TotalLength - Tok.TotalLength + 1; |
| 96 | } |
| 97 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 98 | class UnwrappedLineFormatter { |
| 99 | public: |
Manuel Klimek | 94fc6f1 | 2013-01-10 19:17:33 +0000 | [diff] [blame] | 100 | UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 101 | const AnnotatedLine &Line, unsigned FirstIndent, |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 102 | const AnnotatedToken &RootToken, |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 103 | WhitespaceManager &Whitespaces) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 104 | : Style(Style), SourceMgr(SourceMgr), Line(Line), |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 105 | FirstIndent(FirstIndent), RootToken(RootToken), |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 106 | Whitespaces(Whitespaces), Count(0) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 107 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 108 | /// \brief Formats an \c UnwrappedLine. |
| 109 | /// |
| 110 | /// \returns The column after the last token in the last line of the |
| 111 | /// \c UnwrappedLine. |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 112 | unsigned format(const AnnotatedLine *NextLine) { |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 113 | // Initialize state dependent on indent. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 114 | LineState State; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 115 | State.Column = FirstIndent; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 116 | State.NextToken = &RootToken; |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 117 | State.Stack.push_back( |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 118 | ParenState(FirstIndent, FirstIndent, !Style.BinPackParameters, |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 119 | /*HasMultiParameterLine=*/ false)); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 120 | State.LineContainsContinuedForLoopSection = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 121 | State.ParenLevel = 0; |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 122 | State.StartOfStringLiteral = 0; |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 123 | State.StartOfLineLevel = State.ParenLevel; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 124 | |
| 125 | // The first token has already been indented and thus consumed. |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 126 | moveStateToNextToken(State, /*DryRun=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 127 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 128 | // If everything fits on a single line, just put it there. |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 129 | unsigned ColumnLimit = Style.ColumnLimit; |
| 130 | if (NextLine && NextLine->InPPDirective && |
| 131 | !NextLine->First.FormatTok.HasUnescapedNewline) |
| 132 | ColumnLimit = getColumnLimit(); |
| 133 | if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) { |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 134 | while (State.NextToken != NULL) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 135 | addTokenToState(false, false, State); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 136 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 137 | return State.Column; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 138 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 139 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 140 | // If the ObjC method declaration does not fit on a line, we should format |
| 141 | // it with one arg per line. |
| 142 | if (Line.Type == LT_ObjCMethodDecl) |
| 143 | State.Stack.back().BreakBeforeParameter = true; |
| 144 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 145 | // Find best solution in solution space. |
| 146 | return analyzeSolutionSpace(State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | private: |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 150 | void DebugTokenState(const AnnotatedToken &AnnotatedTok) { |
| 151 | const Token &Tok = AnnotatedTok.FormatTok.Tok; |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 152 | llvm::errs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
| 153 | Tok.getLength()); |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 154 | llvm::errs(); |
| 155 | } |
| 156 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 157 | struct ParenState { |
Daniel Jasper | d399bff | 2013-02-05 09:41:21 +0000 | [diff] [blame] | 158 | ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking, |
| 159 | bool HasMultiParameterLine) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 160 | : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0), |
| 161 | BreakBeforeClosingBrace(false), QuestionColumn(0), |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 162 | AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false), |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 163 | HasMultiParameterLine(HasMultiParameterLine), ColonPos(0), |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 164 | StartOfFunctionCall(0), NestedNameSpecifierContinuation(0), |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 165 | CallContinuation(0), VariablePos(0) {} |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 166 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 167 | /// \brief The position to which a specific parenthesis level needs to be |
| 168 | /// indented. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 169 | unsigned Indent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 170 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 171 | /// \brief The position of the last space on each level. |
| 172 | /// |
| 173 | /// Used e.g. to break like: |
| 174 | /// functionCall(Parameter, otherCall( |
| 175 | /// OtherParameter)); |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 176 | unsigned LastSpace; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 177 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 178 | /// \brief The position the first "<<" operator encountered on each level. |
| 179 | /// |
| 180 | /// Used to align "<<" operators. 0 if no such operator has been encountered |
| 181 | /// on a level. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 182 | unsigned FirstLessLess; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 183 | |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 184 | /// \brief Whether a newline needs to be inserted before the block's closing |
| 185 | /// brace. |
| 186 | /// |
| 187 | /// We only want to insert a newline before the closing brace if there also |
| 188 | /// was a newline after the beginning left brace. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 189 | bool BreakBeforeClosingBrace; |
| 190 | |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 191 | /// \brief The column of a \c ? in a conditional expression; |
| 192 | unsigned QuestionColumn; |
| 193 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 194 | /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple |
| 195 | /// lines, in this context. |
| 196 | bool AvoidBinPacking; |
| 197 | |
| 198 | /// \brief Break after the next comma (or all the commas in this context if |
| 199 | /// \c AvoidBinPacking is \c true). |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 200 | bool BreakBeforeParameter; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 201 | |
| 202 | /// \brief This context already has a line with more than one parameter. |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 203 | bool HasMultiParameterLine; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 204 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 205 | /// \brief The position of the colon in an ObjC method declaration/call. |
| 206 | unsigned ColonPos; |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 207 | |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 208 | /// \brief The start of the most recent function in a builder-type call. |
| 209 | unsigned StartOfFunctionCall; |
| 210 | |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 211 | /// \brief If a nested name specifier was broken over multiple lines, this |
| 212 | /// contains the start column of the second line. Otherwise 0. |
| 213 | unsigned NestedNameSpecifierContinuation; |
| 214 | |
| 215 | /// \brief If a call expression was broken over multiple lines, this |
| 216 | /// contains the start column of the second line. Otherwise 0. |
| 217 | unsigned CallContinuation; |
| 218 | |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 219 | /// \brief The column of the first variable name in a variable declaration. |
| 220 | /// |
| 221 | /// Used to align further variables if necessary. |
| 222 | unsigned VariablePos; |
| 223 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 224 | bool operator<(const ParenState &Other) const { |
| 225 | if (Indent != Other.Indent) |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 226 | return Indent < Other.Indent; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 227 | if (LastSpace != Other.LastSpace) |
| 228 | return LastSpace < Other.LastSpace; |
| 229 | if (FirstLessLess != Other.FirstLessLess) |
| 230 | return FirstLessLess < Other.FirstLessLess; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 231 | if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace) |
| 232 | return BreakBeforeClosingBrace; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 233 | if (QuestionColumn != Other.QuestionColumn) |
| 234 | return QuestionColumn < Other.QuestionColumn; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 235 | if (AvoidBinPacking != Other.AvoidBinPacking) |
| 236 | return AvoidBinPacking; |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 237 | if (BreakBeforeParameter != Other.BreakBeforeParameter) |
| 238 | return BreakBeforeParameter; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 239 | if (HasMultiParameterLine != Other.HasMultiParameterLine) |
| 240 | return HasMultiParameterLine; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 241 | if (ColonPos != Other.ColonPos) |
| 242 | return ColonPos < Other.ColonPos; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 243 | if (StartOfFunctionCall != Other.StartOfFunctionCall) |
| 244 | return StartOfFunctionCall < Other.StartOfFunctionCall; |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 245 | if (NestedNameSpecifierContinuation != |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 246 | Other.NestedNameSpecifierContinuation) |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 247 | return NestedNameSpecifierContinuation < |
| 248 | Other.NestedNameSpecifierContinuation; |
| 249 | if (CallContinuation != Other.CallContinuation) |
| 250 | return CallContinuation < Other.CallContinuation; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 251 | if (VariablePos != Other.VariablePos) |
| 252 | return VariablePos < Other.VariablePos; |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 253 | return false; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 254 | } |
| 255 | }; |
| 256 | |
| 257 | /// \brief The current state when indenting a unwrapped line. |
| 258 | /// |
| 259 | /// As the indenting tries different combinations this is copied by value. |
| 260 | struct LineState { |
| 261 | /// \brief The number of used columns in the current line. |
| 262 | unsigned Column; |
| 263 | |
| 264 | /// \brief The token that needs to be next formatted. |
| 265 | const AnnotatedToken *NextToken; |
| 266 | |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 267 | /// \brief \c true if this line contains a continued for-loop section. |
| 268 | bool LineContainsContinuedForLoopSection; |
| 269 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 270 | /// \brief The level of nesting inside (), [], <> and {}. |
| 271 | unsigned ParenLevel; |
| 272 | |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 273 | /// \brief The \c ParenLevel at the start of this line. |
| 274 | unsigned StartOfLineLevel; |
| 275 | |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 276 | /// \brief The start column of the string literal, if we're in a string |
| 277 | /// literal sequence, 0 otherwise. |
| 278 | unsigned StartOfStringLiteral; |
| 279 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 280 | /// \brief A stack keeping track of properties applying to parenthesis |
| 281 | /// levels. |
| 282 | std::vector<ParenState> Stack; |
| 283 | |
| 284 | /// \brief Comparison operator to be able to used \c LineState in \c map. |
| 285 | bool operator<(const LineState &Other) const { |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 286 | if (NextToken != Other.NextToken) |
| 287 | return NextToken < Other.NextToken; |
| 288 | if (Column != Other.Column) |
| 289 | return Column < Other.Column; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 290 | if (LineContainsContinuedForLoopSection != |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 291 | Other.LineContainsContinuedForLoopSection) |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 292 | return LineContainsContinuedForLoopSection; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 293 | if (ParenLevel != Other.ParenLevel) |
| 294 | return ParenLevel < Other.ParenLevel; |
| 295 | if (StartOfLineLevel != Other.StartOfLineLevel) |
| 296 | return StartOfLineLevel < Other.StartOfLineLevel; |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 297 | if (StartOfStringLiteral != Other.StartOfStringLiteral) |
| 298 | return StartOfStringLiteral < Other.StartOfStringLiteral; |
Daniel Jasper | d789670 | 2013-02-19 09:28:55 +0000 | [diff] [blame] | 299 | return Stack < Other.Stack; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 300 | } |
| 301 | }; |
| 302 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 303 | /// \brief Appends the next token to \p State and updates information |
| 304 | /// necessary for indentation. |
| 305 | /// |
| 306 | /// Puts the token on the current line if \p Newline is \c true and adds a |
| 307 | /// line break and necessary indentation otherwise. |
| 308 | /// |
| 309 | /// If \p DryRun is \c false, also creates and stores the required |
| 310 | /// \c Replacement. |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 311 | unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) { |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 312 | const AnnotatedToken &Current = *State.NextToken; |
| 313 | const AnnotatedToken &Previous = *State.NextToken->Parent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 314 | |
Daniel Jasper | 92f9faf | 2013-03-20 15:58:10 +0000 | [diff] [blame] | 315 | if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) { |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 316 | State.Column += State.NextToken->FormatTok.WhiteSpaceLength + |
| 317 | State.NextToken->FormatTok.TokenLength; |
| 318 | if (State.NextToken->Children.empty()) |
| 319 | State.NextToken = NULL; |
| 320 | else |
| 321 | State.NextToken = &State.NextToken->Children[0]; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 322 | return 0; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 325 | // If we are continuing an expression, we want to indent an extra 4 spaces. |
| 326 | unsigned ContinuationIndent = |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 327 | std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 328 | if (Newline) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 329 | unsigned WhitespaceStartColumn = State.Column; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 330 | if (Current.is(tok::r_brace)) { |
| 331 | State.Column = Line.Level * 2; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 332 | } else if (Current.is(tok::string_literal) && |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 333 | State.StartOfStringLiteral != 0) { |
| 334 | State.Column = State.StartOfStringLiteral; |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 335 | State.Stack.back().BreakBeforeParameter = true; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 336 | } else if (Current.is(tok::lessless) && |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 337 | State.Stack.back().FirstLessLess != 0) { |
| 338 | State.Column = State.Stack.back().FirstLessLess; |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 339 | } else if (Previous.is(tok::coloncolon)) { |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 340 | if (State.Stack.back().NestedNameSpecifierContinuation == 0) { |
| 341 | State.Column = ContinuationIndent; |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 342 | State.Stack.back().NestedNameSpecifierContinuation = State.Column; |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 343 | } else { |
| 344 | State.Column = State.Stack.back().NestedNameSpecifierContinuation; |
| 345 | } |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 346 | } else if (Current.isOneOf(tok::period, tok::arrow)) { |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 347 | if (State.Stack.back().CallContinuation == 0) { |
| 348 | State.Column = ContinuationIndent; |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 349 | State.Stack.back().CallContinuation = State.Column; |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 350 | } else { |
| 351 | State.Column = State.Stack.back().CallContinuation; |
| 352 | } |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 353 | } else if (Current.Type == TT_ConditionalExpr) { |
| 354 | State.Column = State.Stack.back().QuestionColumn; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 355 | } else if (Previous.is(tok::comma) && |
| 356 | State.Stack.back().VariablePos != 0) { |
| 357 | State.Column = State.Stack.back().VariablePos; |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 358 | } else if (Previous.ClosesTemplateDeclaration || |
| 359 | (Current.Type == TT_StartOfName && State.ParenLevel == 0)) { |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 360 | State.Column = State.Stack.back().Indent; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 361 | } else if (Current.Type == TT_ObjCSelectorName) { |
| 362 | if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) { |
| 363 | State.Column = |
| 364 | State.Stack.back().ColonPos - Current.FormatTok.TokenLength; |
| 365 | } else { |
| 366 | State.Column = State.Stack.back().Indent; |
| 367 | State.Stack.back().ColonPos = |
| 368 | State.Column + Current.FormatTok.TokenLength; |
| 369 | } |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 370 | } else if (Current.Type == TT_StartOfName || Previous.is(tok::equal) || |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 371 | Previous.Type == TT_ObjCMethodExpr) { |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 372 | State.Column = ContinuationIndent; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 373 | } else { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 374 | State.Column = State.Stack.back().Indent; |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 375 | // Ensure that we fall back to indenting 4 spaces instead of just |
| 376 | // flushing continuations left. |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 377 | if (State.Column == FirstIndent) |
| 378 | State.Column += 4; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 381 | if (Current.is(tok::question)) |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 382 | State.Stack.back().BreakBeforeParameter = true; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 383 | if (Previous.isOneOf(tok::comma, tok::semi) && |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 384 | !State.Stack.back().AvoidBinPacking) |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 385 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 386 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 387 | if (!DryRun) { |
Daniel Jasper | 1ef81d5 | 2013-02-26 13:10:34 +0000 | [diff] [blame] | 388 | unsigned NewLines = 1; |
| 389 | if (Current.Type == TT_LineComment) |
| 390 | NewLines = |
| 391 | std::max(NewLines, std::min(Current.FormatTok.NewlinesBefore, |
| 392 | Style.MaxEmptyLinesToKeep + 1)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 393 | if (!Line.InPPDirective) |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 394 | Whitespaces.replaceWhitespace(Current, NewLines, State.Column, |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 395 | WhitespaceStartColumn); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 396 | else |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 397 | Whitespaces.replacePPWhitespace(Current, NewLines, State.Column, |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 398 | WhitespaceStartColumn); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 399 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 400 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 401 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 402 | State.StartOfLineLevel = State.ParenLevel; |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 403 | |
| 404 | // Any break on this level means that the parent level has been broken |
| 405 | // and we need to avoid bin packing there. |
| 406 | for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { |
| 407 | State.Stack[i].BreakBeforeParameter = true; |
| 408 | } |
Daniel Jasper | 01218ff | 2013-04-15 22:36:37 +0000 | [diff] [blame] | 409 | const AnnotatedToken *TokenBefore = Current.getPreviousNoneComment(); |
| 410 | if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) && |
| 411 | !TokenBefore->opensScope()) |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 412 | State.Stack.back().BreakBeforeParameter = true; |
| 413 | |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 414 | // If we break after {, we should also break before the corresponding }. |
| 415 | if (Previous.is(tok::l_brace)) |
| 416 | State.Stack.back().BreakBeforeClosingBrace = true; |
| 417 | |
| 418 | if (State.Stack.back().AvoidBinPacking) { |
| 419 | // If we are breaking after '(', '{', '<', this is not bin packing |
| 420 | // unless AllowAllParametersOfDeclarationOnNextLine is false. |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 421 | if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace)) || |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 422 | (!Style.AllowAllParametersOfDeclarationOnNextLine && |
| 423 | Line.MustBeDeclaration)) |
| 424 | State.Stack.back().BreakBeforeParameter = true; |
| 425 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 426 | } else { |
Daniel Jasper | 9c3e71a | 2013-02-25 15:59:54 +0000 | [diff] [blame] | 427 | if (Current.is(tok::equal) && |
Daniel Jasper | adc0f09 | 2013-04-05 09:38:50 +0000 | [diff] [blame] | 428 | (RootToken.is(tok::kw_for) || State.ParenLevel == 0) && |
| 429 | State.Stack.back().VariablePos == 0) { |
| 430 | State.Stack.back().VariablePos = State.Column; |
| 431 | // Move over * and & if they are bound to the variable name. |
| 432 | const AnnotatedToken *Tok = &Previous; |
| 433 | while (Tok && |
| 434 | State.Stack.back().VariablePos >= Tok->FormatTok.TokenLength) { |
| 435 | State.Stack.back().VariablePos -= Tok->FormatTok.TokenLength; |
| 436 | if (Tok->SpacesRequiredBefore != 0) |
| 437 | break; |
| 438 | Tok = Tok->Parent; |
| 439 | } |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 440 | if (Previous.PartOfMultiVariableDeclStmt) |
| 441 | State.Stack.back().LastSpace = State.Stack.back().VariablePos; |
| 442 | } |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 443 | |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 444 | unsigned Spaces = State.NextToken->SpacesRequiredBefore; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 445 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 446 | if (!DryRun) |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 447 | Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 448 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 449 | if (Current.Type == TT_ObjCSelectorName && |
| 450 | State.Stack.back().ColonPos == 0) { |
| 451 | if (State.Stack.back().Indent + Current.LongestObjCSelectorName > |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 452 | State.Column + Spaces + Current.FormatTok.TokenLength) |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 453 | State.Stack.back().ColonPos = |
| 454 | State.Stack.back().Indent + Current.LongestObjCSelectorName; |
| 455 | else |
| 456 | State.Stack.back().ColonPos = |
Daniel Jasper | 9e9e6e0 | 2013-02-06 16:00:26 +0000 | [diff] [blame] | 457 | State.Column + Spaces + Current.FormatTok.TokenLength; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 460 | if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr && |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 461 | Current.Type != TT_LineComment) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 462 | State.Stack.back().Indent = State.Column + Spaces; |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 463 | if (Previous.is(tok::comma) && !Current.isTrailingComment()) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 464 | State.Stack.back().HasMultiParameterLine = true; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 465 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 466 | State.Column += Spaces; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 467 | if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for)) |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 468 | // Treat the condition inside an if as if it was a second function |
| 469 | // parameter, i.e. let nested calls have an indent of 4. |
| 470 | State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(". |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 471 | else if (Previous.is(tok::comma)) |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 472 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 473 | else if ((Previous.Type == TT_BinaryOperator || |
Daniel Jasper | 02b771e | 2013-01-28 13:31:35 +0000 | [diff] [blame] | 474 | Previous.Type == TT_ConditionalExpr || |
| 475 | Previous.Type == TT_CtorInitializerColon) && |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 476 | getPrecedence(Previous) != prec::Assignment) |
| 477 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 478 | else if (Previous.Type == TT_InheritanceColon) |
| 479 | State.Stack.back().Indent = State.Column; |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 480 | else if (Previous.opensScope() && Previous.ParameterCount > 1) |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 481 | // If this function has multiple parameters, indent nested calls from |
| 482 | // the start of the first parameter. |
| 483 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 484 | } |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 485 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 486 | return moveStateToNextToken(State, DryRun); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 487 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 488 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 489 | /// \brief Mark the next token as consumed in \p State and modify its stacks |
| 490 | /// accordingly. |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 491 | unsigned moveStateToNextToken(LineState &State, bool DryRun) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 492 | const AnnotatedToken &Current = *State.NextToken; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 493 | assert(State.Stack.size()); |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 494 | |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 495 | if (Current.Type == TT_InheritanceColon) |
| 496 | State.Stack.back().AvoidBinPacking = true; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 497 | if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0) |
| 498 | State.Stack.back().FirstLessLess = State.Column; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 499 | if (Current.is(tok::question)) |
| 500 | State.Stack.back().QuestionColumn = State.Column; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 501 | if (Current.isOneOf(tok::period, tok::arrow) && |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 502 | Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0) |
| 503 | State.Stack.back().StartOfFunctionCall = |
| 504 | Current.LastInChainOfCalls ? 0 : State.Column; |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 505 | if (Current.Type == TT_CtorInitializerColon) { |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 506 | State.Stack.back().Indent = State.Column + 2; |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 507 | if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) |
| 508 | State.Stack.back().AvoidBinPacking = true; |
| 509 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 510 | } |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 511 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 512 | // If return returns a binary expression, align after it. |
| 513 | if (Current.is(tok::kw_return) && !Current.FakeLParens.empty()) |
| 514 | State.Stack.back().LastSpace = State.Column + 7; |
| 515 | |
Daniel Jasper | 3776ef3 | 2013-04-03 07:21:51 +0000 | [diff] [blame] | 516 | // In ObjC method declaration we align on the ":" of parameters, but we need |
| 517 | // to ensure that we indent parameters on subsequent lines by at least 4. |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 518 | if (Current.Type == TT_ObjCMethodSpecifier) |
| 519 | State.Stack.back().Indent += 4; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 520 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 521 | // Insert scopes created by fake parenthesis. |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 522 | const AnnotatedToken *Previous = Current.getPreviousNoneComment(); |
| 523 | // Don't add extra indentation for the first fake parenthesis after |
| 524 | // 'return', assignements or opening <({[. The indentation for these cases |
| 525 | // is special cased. |
| 526 | bool SkipFirstExtraIndent = |
| 527 | Current.is(tok::kw_return) || |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 528 | (Previous && (Previous->opensScope() || |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 529 | getPrecedence(*Previous) == prec::Assignment)); |
| 530 | for (SmallVector<prec::Level, 4>::const_reverse_iterator |
| 531 | I = Current.FakeLParens.rbegin(), |
| 532 | E = Current.FakeLParens.rend(); |
| 533 | I != E; ++I) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 534 | ParenState NewParenState = State.Stack.back(); |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 535 | NewParenState.Indent = |
| 536 | std::max(std::max(State.Column, NewParenState.Indent), |
| 537 | State.Stack.back().LastSpace); |
| 538 | |
| 539 | // Always indent conditional expressions. Never indent expression where |
| 540 | // the 'operator' is ',', ';' or an assignment (i.e. *I <= |
| 541 | // prec::Assignment) as those have different indentation rules. Indent |
| 542 | // other expression, unless the indentation needs to be skipped. |
| 543 | if (*I == prec::Conditional || |
| 544 | (!SkipFirstExtraIndent && *I > prec::Assignment)) |
| 545 | NewParenState.Indent += 4; |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 546 | if (Previous && !Previous->opensScope()) |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 547 | NewParenState.BreakBeforeParameter = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 548 | State.Stack.push_back(NewParenState); |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 549 | SkipFirstExtraIndent = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 552 | // 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] | 553 | // prepare for the following tokens. |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 554 | if (Current.opensScope()) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 555 | unsigned NewIndent; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 556 | bool AvoidBinPacking; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 557 | if (Current.is(tok::l_brace)) { |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 558 | NewIndent = 2 + State.Stack.back().LastSpace; |
| 559 | AvoidBinPacking = false; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 560 | } else { |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 561 | NewIndent = 4 + std::max(State.Stack.back().LastSpace, |
| 562 | State.Stack.back().StartOfFunctionCall); |
Daniel Jasper | 3a39ac7 | 2013-02-28 09:39:12 +0000 | [diff] [blame] | 563 | AvoidBinPacking = |
| 564 | !Style.BinPackParameters || State.Stack.back().AvoidBinPacking; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 565 | } |
Daniel Jasper | d399bff | 2013-02-05 09:41:21 +0000 | [diff] [blame] | 566 | State.Stack.push_back( |
| 567 | ParenState(NewIndent, State.Stack.back().LastSpace, AvoidBinPacking, |
| 568 | State.Stack.back().HasMultiParameterLine)); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 569 | ++State.ParenLevel; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 572 | // If this '[' opens an ObjC call, determine whether all parameters fit into |
| 573 | // one line and put one per line if they don't. |
| 574 | if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr && |
| 575 | Current.MatchingParen != NULL) { |
| 576 | if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit()) |
| 577 | State.Stack.back().BreakBeforeParameter = true; |
| 578 | } |
| 579 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 580 | // 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] | 581 | // stacks. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 582 | if (Current.isOneOf(tok::r_paren, tok::r_square) || |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 583 | (Current.is(tok::r_brace) && State.NextToken != &RootToken) || |
| 584 | State.NextToken->Type == TT_TemplateCloser) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 585 | State.Stack.pop_back(); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 586 | --State.ParenLevel; |
| 587 | } |
| 588 | |
| 589 | // Remove scopes created by fake parenthesis. |
| 590 | for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) { |
Daniel Jasper | abfc9c1 | 2013-04-04 19:31:00 +0000 | [diff] [blame] | 591 | unsigned VariablePos = State.Stack.back().VariablePos; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 592 | State.Stack.pop_back(); |
Daniel Jasper | abfc9c1 | 2013-04-04 19:31:00 +0000 | [diff] [blame] | 593 | State.Stack.back().VariablePos = VariablePos; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 594 | } |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 595 | |
Manuel Klimek | e9a6226 | 2013-02-20 15:32:58 +0000 | [diff] [blame] | 596 | if (Current.is(tok::string_literal)) { |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 597 | State.StartOfStringLiteral = State.Column; |
| 598 | } else if (Current.isNot(tok::comment)) { |
| 599 | State.StartOfStringLiteral = 0; |
| 600 | } |
| 601 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 602 | State.Column += Current.FormatTok.TokenLength; |
| 603 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 604 | if (State.NextToken->Children.empty()) |
| 605 | State.NextToken = NULL; |
| 606 | else |
| 607 | State.NextToken = &State.NextToken->Children[0]; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 608 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 609 | return breakProtrudingToken(Current, State, DryRun); |
| 610 | } |
| 611 | |
| 612 | /// \brief If the current token sticks out over the end of the line, break |
| 613 | /// it if possible. |
| 614 | unsigned breakProtrudingToken(const AnnotatedToken &Current, LineState &State, |
| 615 | bool DryRun) { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 616 | llvm::OwningPtr<BreakableToken> Token; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 617 | unsigned StartColumn = State.Column - Current.FormatTok.TokenLength; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 618 | if (Current.is(tok::string_literal)) { |
| 619 | // Only break up default narrow strings. |
| 620 | const char *LiteralData = Current.FormatTok.Tok.getLiteralData(); |
| 621 | if (!LiteralData || *LiteralData != '"') |
| 622 | return 0; |
| 623 | |
| 624 | Token.reset(new BreakableStringLiteral(Current.FormatTok, StartColumn)); |
| 625 | } else if (Current.Type == TT_BlockComment) { |
| 626 | BreakableBlockComment *BBC = |
| 627 | new BreakableBlockComment(SourceMgr, Current, StartColumn); |
| 628 | if (!DryRun) |
| 629 | BBC->alignLines(Whitespaces); |
| 630 | Token.reset(BBC); |
| 631 | } else { |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | if (Token->getPrefixLength() + Token->getSuffixLength(0) > |
| 636 | getColumnLimit()) { |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | bool BreakInserted = false; |
| 641 | unsigned Penalty = 0; |
| 642 | for (unsigned LineIndex = 0; LineIndex < Token->getLineCount(); |
| 643 | ++LineIndex) { |
| 644 | unsigned TokenLineSize = Token->getLineSize(LineIndex); |
| 645 | unsigned TailOffset = 0; |
| 646 | unsigned RemainingLength = |
| 647 | Token->getLineLengthAfterSplit(LineIndex, TailOffset); |
| 648 | while (RemainingLength > getColumnLimit()) { |
| 649 | unsigned DecorationLength = |
| 650 | RemainingLength - (TokenLineSize - TailOffset); |
| 651 | if (DecorationLength + 1 > getColumnLimit()) { |
| 652 | // Can't reduce line length by splitting here. |
| 653 | break; |
| 654 | } |
| 655 | BreakableToken::Split Split = |
| 656 | Token->getSplit(LineIndex, TailOffset, getColumnLimit()); |
| 657 | if (Split.first == StringRef::npos) |
| 658 | break; |
| 659 | assert(Split.first != 0); |
| 660 | if (!DryRun) { |
| 661 | Token->insertBreak(LineIndex, TailOffset, Split, Line.InPPDirective, |
| 662 | Whitespaces); |
| 663 | } |
| 664 | TailOffset += Split.first + Split.second; |
Alexander Kornienko | e2657dd | 2013-04-15 15:47:34 +0000 | [diff] [blame] | 665 | unsigned NewRemainingLength = |
| 666 | Token->getLineLengthAfterSplit(LineIndex, TailOffset); |
| 667 | assert(NewRemainingLength < RemainingLength); |
| 668 | RemainingLength = NewRemainingLength; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 669 | Penalty += Style.PenaltyExcessCharacter; |
| 670 | BreakInserted = true; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 671 | } |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 672 | State.Column = RemainingLength; |
| 673 | if (!DryRun) { |
| 674 | Token->trimLine(LineIndex, TailOffset, Line.InPPDirective, Whitespaces); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | if (BreakInserted) { |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 679 | for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) |
| 680 | State.Stack[i].BreakBeforeParameter = true; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 681 | State.Stack.back().LastSpace = StartColumn; |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 682 | } |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 683 | return Penalty; |
| 684 | } |
| 685 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 686 | unsigned getColumnLimit() { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 687 | // In preprocessor directives reserve two chars for trailing " \" |
| 688 | return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0); |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 691 | /// \brief An edge in the solution space from \c Previous->State to \c State, |
| 692 | /// inserting a newline dependent on the \c NewLine. |
| 693 | struct StateNode { |
| 694 | StateNode(const LineState &State, bool NewLine, StateNode *Previous) |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 695 | : State(State), NewLine(NewLine), Previous(Previous) {} |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 696 | LineState State; |
| 697 | bool NewLine; |
| 698 | StateNode *Previous; |
| 699 | }; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 700 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 701 | /// \brief A pair of <penalty, count> that is used to prioritize the BFS on. |
| 702 | /// |
| 703 | /// In case of equal penalties, we want to prefer states that were inserted |
| 704 | /// first. During state generation we make sure that we insert states first |
| 705 | /// that break the line as late as possible. |
| 706 | typedef std::pair<unsigned, unsigned> OrderedPenalty; |
| 707 | |
| 708 | /// \brief An item in the prioritized BFS search queue. The \c StateNode's |
| 709 | /// \c State has the given \c OrderedPenalty. |
| 710 | typedef std::pair<OrderedPenalty, StateNode *> QueueItem; |
| 711 | |
| 712 | /// \brief The BFS queue type. |
| 713 | typedef std::priority_queue<QueueItem, std::vector<QueueItem>, |
| 714 | std::greater<QueueItem> > QueueType; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 715 | |
| 716 | /// \brief Analyze the entire solution space starting from \p InitialState. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 717 | /// |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 718 | /// This implements a variant of Dijkstra's algorithm on the graph that spans |
| 719 | /// the solution space (\c LineStates are the nodes). The algorithm tries to |
| 720 | /// find the shortest path (the one with lowest penalty) from \p InitialState |
| 721 | /// to a state where all tokens are placed. |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 722 | unsigned analyzeSolutionSpace(LineState &InitialState) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 723 | std::set<LineState> Seen; |
| 724 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 725 | // Insert start element into queue. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 726 | StateNode *Node = |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 727 | new (Allocator.Allocate()) StateNode(InitialState, false, NULL); |
| 728 | Queue.push(QueueItem(OrderedPenalty(0, Count), Node)); |
| 729 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 730 | |
| 731 | // While not empty, take first element and follow edges. |
| 732 | while (!Queue.empty()) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 733 | unsigned Penalty = Queue.top().first.first; |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 734 | StateNode *Node = Queue.top().second; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 735 | if (Node->State.NextToken == NULL) { |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 736 | DEBUG(llvm::errs() << "\n---\nPenalty for line: " << Penalty << "\n"); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 737 | break; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 738 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 739 | Queue.pop(); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 740 | |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 741 | if (!Seen.insert(Node->State).second) |
| 742 | // State already examined with lower penalty. |
| 743 | continue; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 744 | |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 745 | addNextStateToQueue(Penalty, Node, /*NewLine=*/ false); |
| 746 | addNextStateToQueue(Penalty, Node, /*NewLine=*/ true); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | if (Queue.empty()) |
| 750 | // We were unable to find a solution, do nothing. |
| 751 | // FIXME: Add diagnostic? |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 752 | return 0; |
| 753 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 754 | // Reconstruct the solution. |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 755 | reconstructPath(InitialState, Queue.top().second); |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 756 | DEBUG(llvm::errs() << "---\n"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 757 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 758 | // Return the column after the last token of the solution. |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 759 | return Queue.top().second->State.Column; |
| 760 | } |
| 761 | |
| 762 | void reconstructPath(LineState &State, StateNode *Current) { |
| 763 | // FIXME: This recursive implementation limits the possible number |
| 764 | // of tokens per line if compiled into a binary with small stack space. |
| 765 | // To become more independent of stack frame limitations we would need |
| 766 | // to also change the TokenAnnotator. |
| 767 | if (Current->Previous == NULL) |
| 768 | return; |
| 769 | reconstructPath(State, Current->Previous); |
| 770 | DEBUG({ |
| 771 | if (Current->NewLine) { |
Daniel Jasper | a03ab10 | 2013-02-13 20:33:44 +0000 | [diff] [blame] | 772 | llvm::errs() |
| 773 | << "Penalty for splitting before " |
| 774 | << Current->Previous->State.NextToken->FormatTok.Tok.getName() |
| 775 | << ": " << Current->Previous->State.NextToken->SplitPenalty << "\n"; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 776 | } |
| 777 | }); |
| 778 | addTokenToState(Current->NewLine, false, State); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 781 | /// \brief Add the following state to the analysis queue \c Queue. |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 782 | /// |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 783 | /// Assume the current state is \p PreviousNode and has been reached with a |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 784 | /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true. |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 785 | void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode, |
| 786 | bool NewLine) { |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 787 | if (NewLine && !canBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 788 | return; |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 789 | if (!NewLine && mustBreak(PreviousNode->State)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 790 | return; |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 791 | if (NewLine) |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 792 | Penalty += PreviousNode->State.NextToken->SplitPenalty; |
| 793 | |
| 794 | StateNode *Node = new (Allocator.Allocate()) |
| 795 | StateNode(PreviousNode->State, NewLine, PreviousNode); |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 796 | Penalty += addTokenToState(NewLine, true, Node->State); |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 797 | if (Node->State.Column > getColumnLimit()) { |
| 798 | unsigned ExcessCharacters = Node->State.Column - getColumnLimit(); |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 799 | Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 800 | } |
Manuel Klimek | 32a2fd7 | 2013-02-13 10:46:36 +0000 | [diff] [blame] | 801 | |
| 802 | Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node)); |
| 803 | ++Count; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 804 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 805 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 806 | /// \brief Returns \c true, if a line break after \p State is allowed. |
| 807 | bool canBreak(const LineState &State) { |
| 808 | if (!State.NextToken->CanBreakBefore && |
| 809 | !(State.NextToken->is(tok::r_brace) && |
| 810 | State.Stack.back().BreakBeforeClosingBrace)) |
| 811 | return false; |
| 812 | // Trying to insert a parameter on a new line if there are already more than |
| 813 | // one parameter on the current line is bin packing. |
Daniel Jasper | d399bff | 2013-02-05 09:41:21 +0000 | [diff] [blame] | 814 | if (State.Stack.back().HasMultiParameterLine && |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 815 | State.Stack.back().AvoidBinPacking) |
| 816 | return false; |
| 817 | return true; |
| 818 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 819 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 820 | /// \brief Returns \c true, if a line break after \p State is mandatory. |
| 821 | bool mustBreak(const LineState &State) { |
| 822 | if (State.NextToken->MustBreakBefore) |
| 823 | return true; |
| 824 | if (State.NextToken->is(tok::r_brace) && |
| 825 | State.Stack.back().BreakBeforeClosingBrace) |
| 826 | return true; |
| 827 | if (State.NextToken->Parent->is(tok::semi) && |
| 828 | State.LineContainsContinuedForLoopSection) |
| 829 | return true; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 830 | if ((State.NextToken->Parent->isOneOf(tok::comma, tok::semi) || |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 831 | State.NextToken->is(tok::question) || |
| 832 | State.NextToken->Type == TT_ConditionalExpr) && |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 833 | State.Stack.back().BreakBeforeParameter && |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 834 | !State.NextToken->isTrailingComment() && |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 835 | State.NextToken->isNot(tok::r_paren) && |
| 836 | State.NextToken->isNot(tok::r_brace)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 837 | return true; |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 838 | // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding |
| 839 | // out whether it is the first parameter. Clean this up. |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 840 | if (State.NextToken->Type == TT_ObjCSelectorName && |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 841 | State.NextToken->LongestObjCSelectorName == 0 && |
| 842 | State.Stack.back().BreakBeforeParameter) |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 843 | return true; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 844 | if ((State.NextToken->Type == TT_CtorInitializerColon || |
| 845 | (State.NextToken->Parent->ClosesTemplateDeclaration && |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 846 | State.ParenLevel == 0))) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 847 | return true; |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 848 | if (State.NextToken->Type == TT_InlineASMColon) |
| 849 | return true; |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 850 | // This prevents breaks like: |
| 851 | // ... |
| 852 | // SomeParameter, OtherParameter).DoSomething( |
| 853 | // ... |
| 854 | // As they hide "DoSomething" and generally bad for readability. |
| 855 | if (State.NextToken->isOneOf(tok::period, tok::arrow) && |
| 856 | getRemainingLength(State) + State.Column > getColumnLimit() && |
| 857 | State.ParenLevel < State.StartOfLineLevel) |
| 858 | return true; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 859 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 862 | // Returns the total number of columns required for the remaining tokens. |
| 863 | unsigned getRemainingLength(const LineState &State) { |
| 864 | if (State.NextToken && State.NextToken->Parent) |
| 865 | return Line.Last->TotalLength - State.NextToken->Parent->TotalLength; |
| 866 | return 0; |
| 867 | } |
| 868 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 869 | FormatStyle Style; |
| 870 | SourceManager &SourceMgr; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 871 | const AnnotatedLine &Line; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 872 | const unsigned FirstIndent; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 873 | const AnnotatedToken &RootToken; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 874 | WhitespaceManager &Whitespaces; |
Manuel Klimek | 62a48fb | 2013-02-13 10:54:19 +0000 | [diff] [blame] | 875 | |
| 876 | llvm::SpecificBumpPtrAllocator<StateNode> Allocator; |
| 877 | QueueType Queue; |
| 878 | // Increasing count of \c StateNode items we have created. This is used |
| 879 | // to create a deterministic order independent of the container. |
| 880 | unsigned Count; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 881 | }; |
| 882 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 883 | class LexerBasedFormatTokenSource : public FormatTokenSource { |
| 884 | public: |
| 885 | LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 886 | : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 887 | IdentTable(Lex.getLangOpts()) { |
| 888 | Lex.SetKeepWhitespaceMode(true); |
| 889 | } |
| 890 | |
| 891 | virtual FormatToken getNextToken() { |
| 892 | if (GreaterStashed) { |
| 893 | FormatTok.NewlinesBefore = 0; |
| 894 | FormatTok.WhiteSpaceStart = |
| 895 | FormatTok.Tok.getLocation().getLocWithOffset(1); |
| 896 | FormatTok.WhiteSpaceLength = 0; |
| 897 | GreaterStashed = false; |
| 898 | return FormatTok; |
| 899 | } |
| 900 | |
| 901 | FormatTok = FormatToken(); |
| 902 | Lex.LexFromRawLexer(FormatTok.Tok); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 903 | StringRef Text = rawTokenText(FormatTok.Tok); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 904 | FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation(); |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 905 | if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0) |
| 906 | FormatTok.IsFirst = true; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 907 | |
| 908 | // Consume and record whitespace until we find a significant token. |
| 909 | while (FormatTok.Tok.is(tok::unknown)) { |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 910 | unsigned Newlines = Text.count('\n'); |
Daniel Jasper | 1eee6c4 | 2013-03-04 13:43:19 +0000 | [diff] [blame] | 911 | if (Newlines > 0) |
| 912 | FormatTok.LastNewlineOffset = |
| 913 | FormatTok.WhiteSpaceLength + Text.rfind('\n') + 1; |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 914 | unsigned EscapedNewlines = Text.count("\\\n"); |
| 915 | FormatTok.NewlinesBefore += Newlines; |
| 916 | FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 917 | FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength(); |
| 918 | |
| 919 | if (FormatTok.Tok.is(tok::eof)) |
| 920 | return FormatTok; |
| 921 | Lex.LexFromRawLexer(FormatTok.Tok); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 922 | Text = rawTokenText(FormatTok.Tok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 923 | } |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 924 | |
| 925 | // Now FormatTok is the next non-whitespace token. |
| 926 | FormatTok.TokenLength = Text.size(); |
| 927 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 928 | // In case the token starts with escaped newlines, we want to |
| 929 | // take them into account as whitespace - this pattern is quite frequent |
| 930 | // in macro definitions. |
| 931 | // FIXME: What do we want to do with other escaped spaces, and escaped |
| 932 | // spaces or newlines in the middle of tokens? |
| 933 | // FIXME: Add a more explicit test. |
| 934 | unsigned i = 0; |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 935 | while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 936 | // FIXME: ++FormatTok.NewlinesBefore is missing... |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 937 | FormatTok.WhiteSpaceLength += 2; |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 938 | FormatTok.TokenLength -= 2; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 939 | i += 2; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | if (FormatTok.Tok.is(tok::raw_identifier)) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 943 | IdentifierInfo &Info = IdentTable.get(Text); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 944 | FormatTok.Tok.setIdentifierInfo(&Info); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 945 | FormatTok.Tok.setKind(Info.getTokenID()); |
| 946 | } |
| 947 | |
| 948 | if (FormatTok.Tok.is(tok::greatergreater)) { |
| 949 | FormatTok.Tok.setKind(tok::greater); |
Daniel Jasper | b6f02f3 | 2013-02-28 10:06:05 +0000 | [diff] [blame] | 950 | FormatTok.TokenLength = 1; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 951 | GreaterStashed = true; |
| 952 | } |
| 953 | |
Daniel Jasper | 812c045 | 2013-03-01 16:45:59 +0000 | [diff] [blame] | 954 | // If we reformat comments, we remove trailing whitespace. Update the length |
| 955 | // accordingly. |
| 956 | if (FormatTok.Tok.is(tok::comment)) |
| 957 | FormatTok.TokenLength = Text.rtrim().size(); |
| 958 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 959 | return FormatTok; |
| 960 | } |
| 961 | |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 962 | IdentifierTable &getIdentTable() { return IdentTable; } |
| 963 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 964 | private: |
| 965 | FormatToken FormatTok; |
| 966 | bool GreaterStashed; |
| 967 | Lexer &Lex; |
| 968 | SourceManager &SourceMgr; |
| 969 | IdentifierTable IdentTable; |
| 970 | |
| 971 | /// Returns the text of \c FormatTok. |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 972 | StringRef rawTokenText(Token &Tok) { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 973 | return StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
| 974 | Tok.getLength()); |
| 975 | } |
| 976 | }; |
| 977 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 978 | class Formatter : public UnwrappedLineConsumer { |
| 979 | public: |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 980 | Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex, |
| 981 | SourceManager &SourceMgr, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 982 | const std::vector<CharSourceRange> &Ranges) |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 983 | : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 984 | Whitespaces(SourceMgr, Style), Ranges(Ranges) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 985 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 986 | virtual ~Formatter() {} |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 987 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 988 | tooling::Replacements format() { |
| 989 | LexerBasedFormatTokenSource Tokens(Lex, SourceMgr); |
| 990 | UnwrappedLineParser Parser(Diag, Style, Tokens, *this); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 991 | bool StructuralError = Parser.parse(); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 992 | unsigned PreviousEndOfLineColumn = 0; |
| 993 | TokenAnnotator Annotator(Style, SourceMgr, Lex, |
| 994 | Tokens.getIdentTable().get("in")); |
| 995 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 996 | Annotator.annotate(AnnotatedLines[i]); |
| 997 | } |
| 998 | deriveLocalStyle(); |
| 999 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1000 | Annotator.calculateFormattingInformation(AnnotatedLines[i]); |
| 1001 | } |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1002 | |
| 1003 | // Adapt level to the next line if this is a comment. |
| 1004 | // FIXME: Can/should this be done in the UnwrappedLineParser? |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 1005 | const AnnotatedLine *NextNoneCommentLine = NULL; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1006 | for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) { |
| 1007 | if (NextNoneCommentLine && AnnotatedLines[i].First.is(tok::comment) && |
| 1008 | AnnotatedLines[i].First.Children.empty()) |
| 1009 | AnnotatedLines[i].Level = NextNoneCommentLine->Level; |
| 1010 | else |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1011 | NextNoneCommentLine = |
| 1012 | AnnotatedLines[i].First.isNot(tok::r_brace) ? &AnnotatedLines[i] |
| 1013 | : NULL; |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1016 | std::vector<int> IndentForLevel; |
| 1017 | bool PreviousLineWasTouched = false; |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1018 | const AnnotatedToken *PreviousLineLastToken = 0; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1019 | for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(), |
| 1020 | E = AnnotatedLines.end(); |
| 1021 | I != E; ++I) { |
| 1022 | const AnnotatedLine &TheLine = *I; |
| 1023 | const FormatToken &FirstTok = TheLine.First.FormatTok; |
| 1024 | int Offset = getIndentOffset(TheLine.First); |
| 1025 | while (IndentForLevel.size() <= TheLine.Level) |
| 1026 | IndentForLevel.push_back(-1); |
| 1027 | IndentForLevel.resize(TheLine.Level + 1); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1028 | bool WasMoved = PreviousLineWasTouched && FirstTok.NewlinesBefore == 0; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1029 | if (TheLine.First.is(tok::eof)) { |
| 1030 | if (PreviousLineWasTouched) { |
| 1031 | unsigned NewLines = std::min(FirstTok.NewlinesBefore, 1u); |
| 1032 | Whitespaces.replaceWhitespace(TheLine.First, NewLines, /*Indent*/ 0, |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 1033 | /*WhitespaceStartColumn*/ 0); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } else if (TheLine.Type != LT_Invalid && |
| 1036 | (WasMoved || touchesLine(TheLine))) { |
| 1037 | unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level); |
| 1038 | unsigned Indent = LevelIndent; |
| 1039 | if (static_cast<int>(Indent) + Offset >= 0) |
| 1040 | Indent += Offset; |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1041 | if (FirstTok.WhiteSpaceStart.isValid() && |
| 1042 | // Insert a break even if there is a structural error in case where |
| 1043 | // we break apart a line consisting of multiple unwrapped lines. |
| 1044 | (FirstTok.NewlinesBefore == 0 || !StructuralError)) { |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1045 | formatFirstToken(TheLine.First, PreviousLineLastToken, Indent, |
| 1046 | TheLine.InPPDirective, PreviousEndOfLineColumn); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1047 | } else { |
| 1048 | Indent = LevelIndent = |
| 1049 | SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1050 | } |
| 1051 | tryFitMultipleLinesInOne(Indent, I, E); |
| 1052 | UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent, |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 1053 | TheLine.First, Whitespaces); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1054 | PreviousEndOfLineColumn = |
| 1055 | Formatter.format(I + 1 != E ? &*(I + 1) : NULL); |
| 1056 | IndentForLevel[TheLine.Level] = LevelIndent; |
| 1057 | PreviousLineWasTouched = true; |
| 1058 | } else { |
| 1059 | if (FirstTok.NewlinesBefore > 0 || FirstTok.IsFirst) { |
| 1060 | unsigned Indent = |
| 1061 | SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1; |
| 1062 | unsigned LevelIndent = Indent; |
| 1063 | if (static_cast<int>(LevelIndent) - Offset >= 0) |
| 1064 | LevelIndent -= Offset; |
Daniel Jasper | 83a90e5 | 2013-03-20 14:31:47 +0000 | [diff] [blame] | 1065 | if (TheLine.First.isNot(tok::comment)) |
| 1066 | IndentForLevel[TheLine.Level] = LevelIndent; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1067 | |
| 1068 | // Remove trailing whitespace of the previous line if it was touched. |
| 1069 | if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1070 | formatFirstToken(TheLine.First, PreviousLineLastToken, Indent, |
| 1071 | TheLine.InPPDirective, PreviousEndOfLineColumn); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1072 | } |
| 1073 | // If we did not reformat this unwrapped line, the column at the end of |
| 1074 | // the last token is unchanged - thus, we can calculate the end of the |
| 1075 | // last token. |
| 1076 | SourceLocation LastLoc = TheLine.Last->FormatTok.Tok.getLocation(); |
| 1077 | PreviousEndOfLineColumn = |
| 1078 | SourceMgr.getSpellingColumnNumber(LastLoc) + |
| 1079 | Lex.MeasureTokenLength(LastLoc, SourceMgr, Lex.getLangOpts()) - 1; |
| 1080 | PreviousLineWasTouched = false; |
Daniel Jasper | c363dbb | 2013-03-22 16:25:51 +0000 | [diff] [blame] | 1081 | if (TheLine.Last->is(tok::comment)) |
| 1082 | Whitespaces.addUntouchableComment(SourceMgr.getSpellingColumnNumber( |
| 1083 | TheLine.Last->FormatTok.Tok.getLocation()) - 1); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1084 | } |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1085 | PreviousLineLastToken = I->Last; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1086 | } |
| 1087 | return Whitespaces.generateReplacements(); |
| 1088 | } |
| 1089 | |
| 1090 | private: |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1091 | void deriveLocalStyle() { |
| 1092 | unsigned CountBoundToVariable = 0; |
| 1093 | unsigned CountBoundToType = 0; |
| 1094 | bool HasCpp03IncompatibleFormat = false; |
| 1095 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 1096 | if (AnnotatedLines[i].First.Children.empty()) |
| 1097 | continue; |
| 1098 | AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0]; |
| 1099 | while (!Tok->Children.empty()) { |
| 1100 | if (Tok->Type == TT_PointerOrReference) { |
| 1101 | bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0; |
| 1102 | bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0; |
| 1103 | if (SpacesBefore && !SpacesAfter) |
| 1104 | ++CountBoundToVariable; |
| 1105 | else if (!SpacesBefore && SpacesAfter) |
| 1106 | ++CountBoundToType; |
| 1107 | } |
| 1108 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1109 | if (Tok->Type == TT_TemplateCloser && |
| 1110 | Tok->Parent->Type == TT_TemplateCloser && |
| 1111 | Tok->FormatTok.WhiteSpaceLength == 0) |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1112 | HasCpp03IncompatibleFormat = true; |
| 1113 | Tok = &Tok->Children[0]; |
| 1114 | } |
| 1115 | } |
| 1116 | if (Style.DerivePointerBinding) { |
| 1117 | if (CountBoundToType > CountBoundToVariable) |
| 1118 | Style.PointerBindsToType = true; |
| 1119 | else if (CountBoundToType < CountBoundToVariable) |
| 1120 | Style.PointerBindsToType = false; |
| 1121 | } |
| 1122 | if (Style.Standard == FormatStyle::LS_Auto) { |
| 1123 | Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 |
| 1124 | : FormatStyle::LS_Cpp03; |
| 1125 | } |
| 1126 | } |
| 1127 | |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1128 | /// \brief Get the indent of \p Level from \p IndentForLevel. |
| 1129 | /// |
| 1130 | /// \p IndentForLevel must contain the indent for the level \c l |
| 1131 | /// at \p IndentForLevel[l], or a value < 0 if the indent for |
| 1132 | /// that level is unknown. |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1133 | unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) { |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1134 | if (IndentForLevel[Level] != -1) |
| 1135 | return IndentForLevel[Level]; |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 1136 | if (Level == 0) |
| 1137 | return 0; |
Daniel Jasper | c78c6b3 | 2013-02-14 09:58:41 +0000 | [diff] [blame] | 1138 | return getIndent(IndentForLevel, Level - 1) + 2; |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /// \brief Get the offset of the line relatively to the level. |
| 1142 | /// |
| 1143 | /// For example, 'public:' labels in classes are offset by 1 or 2 |
| 1144 | /// characters to the left from their level. |
Daniel Jasper | c78c6b3 | 2013-02-14 09:58:41 +0000 | [diff] [blame] | 1145 | int getIndentOffset(const AnnotatedToken &RootToken) { |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1146 | if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier()) |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1147 | return Style.AccessModifierOffset; |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1151 | /// \brief Tries to merge lines into one. |
| 1152 | /// |
| 1153 | /// This will change \c Line and \c AnnotatedLine to contain the merged line, |
| 1154 | /// if possible; note that \c I will be incremented when lines are merged. |
| 1155 | /// |
| 1156 | /// Returns whether the resulting \c Line can fit in a single line. |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1157 | void tryFitMultipleLinesInOne(unsigned Indent, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1158 | std::vector<AnnotatedLine>::iterator &I, |
| 1159 | std::vector<AnnotatedLine>::iterator E) { |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1160 | // We can never merge stuff if there are trailing line comments. |
| 1161 | if (I->Last->Type == TT_LineComment) |
| 1162 | return; |
| 1163 | |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 1164 | unsigned Limit = Style.ColumnLimit - Indent; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1165 | // If we already exceed the column limit, we set 'Limit' to 0. The different |
| 1166 | // tryMerge..() functions can then decide whether to still do merging. |
| 1167 | Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1168 | |
Daniel Jasper | 9c8c40e | 2013-01-21 14:18:28 +0000 | [diff] [blame] | 1169 | if (I + 1 == E || (I + 1)->Type == LT_Invalid) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1170 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1171 | |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1172 | if (I->Last->is(tok::l_brace)) { |
| 1173 | tryMergeSimpleBlock(I, E, Limit); |
| 1174 | } else if (I->First.is(tok::kw_if)) { |
| 1175 | tryMergeSimpleIf(I, E, Limit); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1176 | } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline || |
| 1177 | I->First.FormatTok.IsFirst)) { |
| 1178 | tryMergeSimplePPDirective(I, E, Limit); |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1179 | } |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1180 | return; |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1183 | void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I, |
| 1184 | std::vector<AnnotatedLine>::iterator E, |
| 1185 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1186 | if (Limit == 0) |
| 1187 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1188 | AnnotatedLine &Line = *I; |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 1189 | if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline) |
| 1190 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1191 | if (I + 2 != E && (I + 2)->InPPDirective && |
| 1192 | !(I + 2)->First.FormatTok.HasUnescapedNewline) |
| 1193 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1194 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1195 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1196 | join(Line, *(++I)); |
| 1197 | } |
| 1198 | |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1199 | void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I, |
| 1200 | std::vector<AnnotatedLine>::iterator E, |
| 1201 | unsigned Limit) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1202 | if (Limit == 0) |
| 1203 | return; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 1204 | if (!Style.AllowShortIfStatementsOnASingleLine) |
| 1205 | return; |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 1206 | if ((I + 1)->InPPDirective != I->InPPDirective || |
| 1207 | ((I + 1)->InPPDirective && |
| 1208 | (I + 1)->First.FormatTok.HasUnescapedNewline)) |
| 1209 | return; |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1210 | AnnotatedLine &Line = *I; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1211 | if (Line.Last->isNot(tok::r_paren)) |
| 1212 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1213 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1214 | return; |
| 1215 | if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment) |
| 1216 | return; |
| 1217 | // Only inline simple if's (no nested if or else). |
| 1218 | if (I + 2 != E && (I + 2)->First.is(tok::kw_else)) |
| 1219 | return; |
| 1220 | join(Line, *(++I)); |
| 1221 | } |
| 1222 | |
| 1223 | void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I, |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1224 | std::vector<AnnotatedLine>::iterator E, |
| 1225 | unsigned Limit) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1226 | // First, check that the current line allows merging. This is the case if |
| 1227 | // we're not in a control flow statement and the last token is an opening |
| 1228 | // brace. |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1229 | AnnotatedLine &Line = *I; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1230 | if (Line.First.isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace, |
| 1231 | tok::kw_else, tok::kw_try, tok::kw_catch, |
| 1232 | tok::kw_for, |
| 1233 | // This gets rid of all ObjC @ keywords and methods. |
| 1234 | tok::at, tok::minus, tok::plus)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1235 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1236 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1237 | AnnotatedToken *Tok = &(I + 1)->First; |
| 1238 | if (Tok->Children.empty() && Tok->is(tok::r_brace) && |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1239 | !Tok->MustBreakBefore) { |
| 1240 | // We merge empty blocks even if the line exceeds the column limit. |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 1241 | Tok->SpacesRequiredBefore = 0; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1242 | Tok->CanBreakBefore = true; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1243 | join(Line, *(I + 1)); |
| 1244 | I += 1; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1245 | } else if (Limit != 0) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1246 | // Check that we still have three lines and they fit into the limit. |
| 1247 | if (I + 2 == E || (I + 2)->Type == LT_Invalid || |
| 1248 | !nextTwoLinesFitInto(I, Limit)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1249 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1250 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1251 | // Second, check that the next line does not contain any braces - if it |
| 1252 | // does, readability declines when putting it into a single line. |
| 1253 | if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore) |
| 1254 | return; |
| 1255 | do { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1256 | if (Tok->isOneOf(tok::l_brace, tok::r_brace)) |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1257 | return; |
| 1258 | Tok = Tok->Children.empty() ? NULL : &Tok->Children.back(); |
| 1259 | } while (Tok != NULL); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1260 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1261 | // Last, check that the third line contains a single closing brace. |
| 1262 | Tok = &(I + 2)->First; |
| 1263 | if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) || |
| 1264 | Tok->MustBreakBefore) |
| 1265 | return; |
| 1266 | |
| 1267 | join(Line, *(I + 1)); |
| 1268 | join(Line, *(I + 2)); |
| 1269 | I += 2; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1270 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I, |
| 1274 | unsigned Limit) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1275 | return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <= |
| 1276 | Limit; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1279 | void join(AnnotatedLine &A, const AnnotatedLine &B) { |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1280 | unsigned LengthA = A.Last->TotalLength + B.First.SpacesRequiredBefore; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1281 | A.Last->Children.push_back(B.First); |
| 1282 | while (!A.Last->Children.empty()) { |
| 1283 | A.Last->Children[0].Parent = A.Last; |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1284 | A.Last->Children[0].TotalLength += LengthA; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1285 | A.Last = &A.Last->Children[0]; |
| 1286 | } |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 1289 | bool touchesRanges(const CharSourceRange &Range) { |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1290 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 1291 | if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), |
| 1292 | Ranges[i].getBegin()) && |
| 1293 | !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 1294 | Range.getBegin())) |
| 1295 | return true; |
| 1296 | } |
| 1297 | return false; |
| 1298 | } |
| 1299 | |
| 1300 | bool touchesLine(const AnnotatedLine &TheLine) { |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1301 | const FormatToken *First = &TheLine.First.FormatTok; |
| 1302 | const FormatToken *Last = &TheLine.Last->FormatTok; |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1303 | CharSourceRange LineRange = CharSourceRange::getTokenRange( |
Daniel Jasper | 1eee6c4 | 2013-03-04 13:43:19 +0000 | [diff] [blame] | 1304 | First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset), |
| 1305 | Last->Tok.getLocation()); |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 1306 | return touchesRanges(LineRange); |
| 1307 | } |
| 1308 | |
| 1309 | bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) { |
| 1310 | const FormatToken *First = &TheLine.First.FormatTok; |
| 1311 | CharSourceRange LineRange = CharSourceRange::getCharRange( |
| 1312 | First->WhiteSpaceStart, |
| 1313 | First->WhiteSpaceStart.getLocWithOffset(First->LastNewlineOffset)); |
| 1314 | return touchesRanges(LineRange); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1318 | AnnotatedLines.push_back(AnnotatedLine(TheLine)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1321 | /// \brief Add a new line and the required indent before the first Token |
| 1322 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 1323 | /// Returns the indent level of the \c UnwrappedLine. |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1324 | void formatFirstToken(const AnnotatedToken &RootToken, |
| 1325 | const AnnotatedToken *PreviousToken, unsigned Indent, |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1326 | bool InPPDirective, unsigned PreviousEndOfLineColumn) { |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1327 | const FormatToken &Tok = RootToken.FormatTok; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1328 | |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1329 | unsigned Newlines = |
| 1330 | std::min(Tok.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1331 | if (Newlines == 0 && !Tok.IsFirst) |
| 1332 | Newlines = 1; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1333 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1334 | if (!InPPDirective || Tok.HasUnescapedNewline) { |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 1335 | // Insert extra new line before access specifiers. |
| 1336 | if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) && |
| 1337 | RootToken.isAccessSpecifier() && Tok.NewlinesBefore == 1) |
| 1338 | ++Newlines; |
| 1339 | |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 1340 | Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1341 | } else { |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1342 | Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent, |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 1343 | PreviousEndOfLineColumn); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1344 | } |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1347 | DiagnosticsEngine &Diag; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1348 | FormatStyle Style; |
| 1349 | Lexer &Lex; |
| 1350 | SourceManager &SourceMgr; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1351 | WhitespaceManager Whitespaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1352 | std::vector<CharSourceRange> Ranges; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1353 | std::vector<AnnotatedLine> AnnotatedLines; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1354 | }; |
| 1355 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1356 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 1357 | SourceManager &SourceMgr, |
| 1358 | std::vector<CharSourceRange> Ranges, |
| 1359 | DiagnosticConsumer *DiagClient) { |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1360 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1361 | OwningPtr<DiagnosticConsumer> DiagPrinter; |
| 1362 | if (DiagClient == 0) { |
| 1363 | DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts)); |
| 1364 | DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP()); |
| 1365 | DiagClient = DiagPrinter.get(); |
| 1366 | } |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1367 | DiagnosticsEngine Diagnostics( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1368 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1369 | DiagClient, false); |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1370 | Diagnostics.setSourceManager(&SourceMgr); |
| 1371 | Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1372 | return formatter.format(); |
| 1373 | } |
| 1374 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1375 | LangOptions getFormattingLangOpts() { |
| 1376 | LangOptions LangOpts; |
| 1377 | LangOpts.CPlusPlus = 1; |
| 1378 | LangOpts.CPlusPlus11 = 1; |
Daniel Jasper | b64eca0 | 2013-03-22 10:01:29 +0000 | [diff] [blame] | 1379 | LangOpts.LineComment = 1; |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1380 | LangOpts.Bool = 1; |
| 1381 | LangOpts.ObjC1 = 1; |
| 1382 | LangOpts.ObjC2 = 1; |
| 1383 | return LangOpts; |
| 1384 | } |
| 1385 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1386 | } // namespace format |
| 1387 | } // namespace clang |