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