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 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 18 | #include "TokenAnnotator.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "UnwrappedLineParser.h" |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Daniel Jasper | 675d2e3 | 2012-12-21 10:20:02 +0000 | [diff] [blame] | 21 | #include "clang/Basic/OperatorPrecedence.h" |
Chandler Carruth | b99083e | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 23 | #include "clang/Format/Format.h" |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Lexer.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 27 | #include <string> |
| 28 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 29 | namespace clang { |
| 30 | namespace format { |
| 31 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 32 | FormatStyle getLLVMStyle() { |
| 33 | FormatStyle LLVMStyle; |
| 34 | LLVMStyle.ColumnLimit = 80; |
| 35 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 36 | LLVMStyle.PointerBindsToType = false; |
| 37 | LLVMStyle.DerivePointerBinding = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 38 | LLVMStyle.AccessModifierOffset = -2; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 39 | LLVMStyle.Standard = FormatStyle::LS_Cpp03; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 40 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 41 | LLVMStyle.SpacesBeforeTrailingComments = 1; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 42 | LLVMStyle.BinPackParameters = true; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 43 | LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | d75ff64 | 2013-01-28 15:40:20 +0000 | [diff] [blame] | 44 | LLVMStyle.AllowReturnTypeOnItsOwnLine = true; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 45 | LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 46 | LLVMStyle.AllowShortIfStatementsOnASingleLine = false; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 47 | LLVMStyle.ObjCSpaceBeforeProtocolList = true; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 48 | LLVMStyle.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 49 | return LLVMStyle; |
| 50 | } |
| 51 | |
| 52 | FormatStyle getGoogleStyle() { |
| 53 | FormatStyle GoogleStyle; |
| 54 | GoogleStyle.ColumnLimit = 80; |
| 55 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 56 | GoogleStyle.PointerBindsToType = true; |
| 57 | GoogleStyle.DerivePointerBinding = true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 58 | GoogleStyle.AccessModifierOffset = -1; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 59 | GoogleStyle.Standard = FormatStyle::LS_Auto; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 60 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 61 | GoogleStyle.SpacesBeforeTrailingComments = 2; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 62 | GoogleStyle.BinPackParameters = false; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 63 | GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; |
Daniel Jasper | d75ff64 | 2013-01-28 15:40:20 +0000 | [diff] [blame] | 64 | GoogleStyle.AllowReturnTypeOnItsOwnLine = false; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 65 | GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 66 | GoogleStyle.AllowShortIfStatementsOnASingleLine = false; |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 67 | GoogleStyle.ObjCSpaceBeforeProtocolList = false; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 68 | GoogleStyle.PenaltyExcessCharacter = 1000000; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 69 | return GoogleStyle; |
| 70 | } |
| 71 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 72 | FormatStyle getChromiumStyle() { |
| 73 | FormatStyle ChromiumStyle = getGoogleStyle(); |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 74 | ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 75 | ChromiumStyle.Standard = FormatStyle::LS_Cpp03; |
| 76 | ChromiumStyle.DerivePointerBinding = false; |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 77 | return ChromiumStyle; |
| 78 | } |
| 79 | |
Daniel Jasper | 15417ef | 2013-02-06 20:07:35 +0000 | [diff] [blame] | 80 | static bool isTrailingComment(const AnnotatedToken &Tok) { |
| 81 | return Tok.is(tok::comment) && |
| 82 | (Tok.Children.empty() || Tok.Children[0].MustBreakBefore); |
| 83 | } |
| 84 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 85 | // Returns the length of everything up to the first possible line break after |
| 86 | // the ), ], } or > matching \c Tok. |
| 87 | static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) { |
| 88 | if (Tok.MatchingParen == NULL) |
| 89 | return 0; |
| 90 | AnnotatedToken *End = Tok.MatchingParen; |
| 91 | while (!End->Children.empty() && !End->Children[0].CanBreakBefore) { |
| 92 | End = &End->Children[0]; |
| 93 | } |
| 94 | return End->TotalLength - Tok.TotalLength + 1; |
| 95 | } |
| 96 | |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 97 | /// \brief Manages the whitespaces around tokens and their replacements. |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 98 | /// |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 99 | /// This includes special handling for certain constructs, e.g. the alignment of |
| 100 | /// trailing line comments. |
| 101 | class WhitespaceManager { |
| 102 | public: |
| 103 | WhitespaceManager(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {} |
| 104 | |
| 105 | /// \brief Replaces the whitespace in front of \p Tok. Only call once for |
| 106 | /// each \c AnnotatedToken. |
| 107 | void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines, |
| 108 | unsigned Spaces, unsigned WhitespaceStartColumn, |
| 109 | const FormatStyle &Style) { |
Daniel Jasper | 821627e | 2013-01-21 22:49:20 +0000 | [diff] [blame] | 110 | // 2+ newlines mean an empty line separating logic scopes. |
| 111 | if (NewLines >= 2) |
| 112 | alignComments(); |
| 113 | |
| 114 | // Align line comments if they are trailing or if they continue other |
| 115 | // trailing comments. |
Daniel Jasper | 15417ef | 2013-02-06 20:07:35 +0000 | [diff] [blame] | 116 | if (isTrailingComment(Tok) && (Tok.Parent != NULL || !Comments.empty())) { |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 117 | if (Style.ColumnLimit >= |
| 118 | Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) { |
| 119 | Comments.push_back(StoredComment()); |
| 120 | Comments.back().Tok = Tok.FormatTok; |
| 121 | Comments.back().Spaces = Spaces; |
| 122 | Comments.back().NewLines = NewLines; |
Daniel Jasper | 474e462 | 2013-02-06 22:04:05 +0000 | [diff] [blame] | 123 | if (NewLines == 0) |
| 124 | Comments.back().MinColumn = WhitespaceStartColumn + Spaces; |
| 125 | else |
| 126 | Comments.back().MinColumn = Spaces; |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 127 | Comments.back().MaxColumn = |
| 128 | Style.ColumnLimit - Spaces - Tok.FormatTok.TokenLength; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 129 | return; |
| 130 | } |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 131 | } |
Daniel Jasper | 821627e | 2013-01-21 22:49:20 +0000 | [diff] [blame] | 132 | |
| 133 | // If this line does not have a trailing comment, align the stored comments. |
Daniel Jasper | 15417ef | 2013-02-06 20:07:35 +0000 | [diff] [blame] | 134 | if (Tok.Children.empty() && !isTrailingComment(Tok)) |
Daniel Jasper | 821627e | 2013-01-21 22:49:20 +0000 | [diff] [blame] | 135 | alignComments(); |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 136 | storeReplacement(Tok.FormatTok, |
| 137 | std::string(NewLines, '\n') + std::string(Spaces, ' ')); |
| 138 | } |
| 139 | |
| 140 | /// \brief Like \c replaceWhitespace, but additionally adds right-aligned |
| 141 | /// backslashes to escape newlines inside a preprocessor directive. |
| 142 | /// |
| 143 | /// This function and \c replaceWhitespace have the same behavior if |
| 144 | /// \c Newlines == 0. |
| 145 | void replacePPWhitespace(const AnnotatedToken &Tok, unsigned NewLines, |
| 146 | unsigned Spaces, unsigned WhitespaceStartColumn, |
| 147 | const FormatStyle &Style) { |
| 148 | std::string NewLineText; |
| 149 | if (NewLines > 0) { |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 150 | unsigned Offset = |
| 151 | std::min<int>(Style.ColumnLimit - 1, WhitespaceStartColumn); |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 152 | for (unsigned i = 0; i < NewLines; ++i) { |
| 153 | NewLineText += std::string(Style.ColumnLimit - Offset - 1, ' '); |
| 154 | NewLineText += "\\\n"; |
| 155 | Offset = 0; |
| 156 | } |
| 157 | } |
| 158 | storeReplacement(Tok.FormatTok, NewLineText + std::string(Spaces, ' ')); |
| 159 | } |
| 160 | |
| 161 | /// \brief Returns all the \c Replacements created during formatting. |
| 162 | const tooling::Replacements &generateReplacements() { |
| 163 | alignComments(); |
| 164 | return Replaces; |
| 165 | } |
| 166 | |
| 167 | private: |
| 168 | /// \brief Structure to store a comment for later layout and alignment. |
| 169 | struct StoredComment { |
| 170 | FormatToken Tok; |
| 171 | unsigned MinColumn; |
| 172 | unsigned MaxColumn; |
| 173 | unsigned NewLines; |
| 174 | unsigned Spaces; |
| 175 | }; |
| 176 | SmallVector<StoredComment, 16> Comments; |
| 177 | typedef SmallVector<StoredComment, 16>::iterator comment_iterator; |
| 178 | |
| 179 | /// \brief Try to align all stashed comments. |
| 180 | void alignComments() { |
| 181 | unsigned MinColumn = 0; |
| 182 | unsigned MaxColumn = UINT_MAX; |
| 183 | comment_iterator Start = Comments.begin(); |
| 184 | for (comment_iterator I = Comments.begin(), E = Comments.end(); I != E; |
| 185 | ++I) { |
| 186 | if (I->MinColumn > MaxColumn || I->MaxColumn < MinColumn) { |
| 187 | alignComments(Start, I, MinColumn); |
| 188 | MinColumn = I->MinColumn; |
| 189 | MaxColumn = I->MaxColumn; |
| 190 | Start = I; |
| 191 | } else { |
| 192 | MinColumn = std::max(MinColumn, I->MinColumn); |
| 193 | MaxColumn = std::min(MaxColumn, I->MaxColumn); |
| 194 | } |
| 195 | } |
| 196 | alignComments(Start, Comments.end(), MinColumn); |
| 197 | Comments.clear(); |
| 198 | } |
| 199 | |
| 200 | /// \brief Put all the comments between \p I and \p E into \p Column. |
| 201 | void alignComments(comment_iterator I, comment_iterator E, unsigned Column) { |
| 202 | while (I != E) { |
| 203 | unsigned Spaces = I->Spaces + Column - I->MinColumn; |
| 204 | storeReplacement(I->Tok, std::string(I->NewLines, '\n') + |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 205 | std::string(Spaces, ' ')); |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 206 | ++I; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 209 | |
| 210 | /// \brief Stores \p Text as the replacement for the whitespace in front of |
| 211 | /// \p Tok. |
| 212 | void storeReplacement(const FormatToken &Tok, const std::string Text) { |
Daniel Jasper | afcbd85 | 2013-01-30 09:46:12 +0000 | [diff] [blame] | 213 | // Don't create a replacement, if it does not change anything. |
| 214 | if (StringRef(SourceMgr.getCharacterData(Tok.WhiteSpaceStart), |
| 215 | Tok.WhiteSpaceLength) == Text) |
| 216 | return; |
| 217 | |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 218 | Replaces.insert(tooling::Replacement(SourceMgr, Tok.WhiteSpaceStart, |
| 219 | Tok.WhiteSpaceLength, Text)); |
| 220 | } |
| 221 | |
| 222 | SourceManager &SourceMgr; |
| 223 | tooling::Replacements Replaces; |
| 224 | }; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 225 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 226 | class UnwrappedLineFormatter { |
| 227 | public: |
Manuel Klimek | 94fc6f1 | 2013-01-10 19:17:33 +0000 | [diff] [blame] | 228 | UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 229 | const AnnotatedLine &Line, unsigned FirstIndent, |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 230 | const AnnotatedToken &RootToken, |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 231 | WhitespaceManager &Whitespaces, bool StructuralError) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 232 | : Style(Style), SourceMgr(SourceMgr), Line(Line), |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 233 | FirstIndent(FirstIndent), RootToken(RootToken), |
| 234 | Whitespaces(Whitespaces) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 237 | /// \brief Formats an \c UnwrappedLine. |
| 238 | /// |
| 239 | /// \returns The column after the last token in the last line of the |
| 240 | /// \c UnwrappedLine. |
| 241 | unsigned format() { |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 242 | // Initialize state dependent on indent. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 243 | LineState State; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 244 | State.Column = FirstIndent; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 245 | State.NextToken = &RootToken; |
Daniel Jasper | d399bff | 2013-02-05 09:41:21 +0000 | [diff] [blame] | 246 | State.Stack.push_back( |
| 247 | ParenState(FirstIndent + 4, FirstIndent, !Style.BinPackParameters, |
| 248 | /*HasMultiParameterLine=*/ false)); |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 249 | State.VariablePos = 0; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 250 | State.LineContainsContinuedForLoopSection = false; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 251 | State.ParenLevel = 0; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 252 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 253 | DEBUG({ |
| 254 | DebugTokenState(*State.NextToken); |
| 255 | }); |
| 256 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 257 | // The first token has already been indented and thus consumed. |
| 258 | moveStateToNextToken(State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 259 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 260 | // If everything fits on a single line, just put it there. |
| 261 | if (Line.Last->TotalLength <= getColumnLimit() - FirstIndent) { |
| 262 | while (State.NextToken != NULL) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 263 | addTokenToState(false, false, State); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 264 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 265 | return State.Column; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 266 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 267 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 268 | // If the ObjC method declaration does not fit on a line, we should format |
| 269 | // it with one arg per line. |
| 270 | if (Line.Type == LT_ObjCMethodDecl) |
| 271 | State.Stack.back().BreakBeforeParameter = true; |
| 272 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 273 | // Find best solution in solution space. |
| 274 | return analyzeSolutionSpace(State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | private: |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 278 | void DebugTokenState(const AnnotatedToken &AnnotatedTok) { |
| 279 | const Token &Tok = AnnotatedTok.FormatTok.Tok; |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 280 | llvm::errs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
| 281 | Tok.getLength()); |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 282 | llvm::errs(); |
| 283 | } |
| 284 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 285 | struct ParenState { |
Daniel Jasper | d399bff | 2013-02-05 09:41:21 +0000 | [diff] [blame] | 286 | ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking, |
| 287 | bool HasMultiParameterLine) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 288 | : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0), |
| 289 | BreakBeforeClosingBrace(false), QuestionColumn(0), |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 290 | AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false), |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 291 | HasMultiParameterLine(HasMultiParameterLine), ColonPos(0) { |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 292 | } |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 293 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 294 | /// \brief The position to which a specific parenthesis level needs to be |
| 295 | /// indented. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 296 | unsigned Indent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 297 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 298 | /// \brief The position of the last space on each level. |
| 299 | /// |
| 300 | /// Used e.g. to break like: |
| 301 | /// functionCall(Parameter, otherCall( |
| 302 | /// OtherParameter)); |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 303 | unsigned LastSpace; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 304 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 305 | /// \brief The position the first "<<" operator encountered on each level. |
| 306 | /// |
| 307 | /// Used to align "<<" operators. 0 if no such operator has been encountered |
| 308 | /// on a level. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 309 | unsigned FirstLessLess; |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 310 | |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 311 | /// \brief Whether a newline needs to be inserted before the block's closing |
| 312 | /// brace. |
| 313 | /// |
| 314 | /// We only want to insert a newline before the closing brace if there also |
| 315 | /// was a newline after the beginning left brace. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 316 | bool BreakBeforeClosingBrace; |
| 317 | |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 318 | /// \brief The column of a \c ? in a conditional expression; |
| 319 | unsigned QuestionColumn; |
| 320 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 321 | /// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple |
| 322 | /// lines, in this context. |
| 323 | bool AvoidBinPacking; |
| 324 | |
| 325 | /// \brief Break after the next comma (or all the commas in this context if |
| 326 | /// \c AvoidBinPacking is \c true). |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 327 | bool BreakBeforeParameter; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 328 | |
| 329 | /// \brief This context already has a line with more than one parameter. |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 330 | bool HasMultiParameterLine; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 331 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 332 | /// \brief The position of the colon in an ObjC method declaration/call. |
| 333 | unsigned ColonPos; |
| 334 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 335 | bool operator<(const ParenState &Other) const { |
| 336 | if (Indent != Other.Indent) |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 337 | return Indent < Other.Indent; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 338 | if (LastSpace != Other.LastSpace) |
| 339 | return LastSpace < Other.LastSpace; |
| 340 | if (FirstLessLess != Other.FirstLessLess) |
| 341 | return FirstLessLess < Other.FirstLessLess; |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 342 | if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace) |
| 343 | return BreakBeforeClosingBrace; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 344 | if (QuestionColumn != Other.QuestionColumn) |
| 345 | return QuestionColumn < Other.QuestionColumn; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 346 | if (AvoidBinPacking != Other.AvoidBinPacking) |
| 347 | return AvoidBinPacking; |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 348 | if (BreakBeforeParameter != Other.BreakBeforeParameter) |
| 349 | return BreakBeforeParameter; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 350 | if (HasMultiParameterLine != Other.HasMultiParameterLine) |
| 351 | return HasMultiParameterLine; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 352 | if (ColonPos != Other.ColonPos) |
| 353 | return ColonPos < Other.ColonPos; |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 354 | return false; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 355 | } |
| 356 | }; |
| 357 | |
| 358 | /// \brief The current state when indenting a unwrapped line. |
| 359 | /// |
| 360 | /// As the indenting tries different combinations this is copied by value. |
| 361 | struct LineState { |
| 362 | /// \brief The number of used columns in the current line. |
| 363 | unsigned Column; |
| 364 | |
| 365 | /// \brief The token that needs to be next formatted. |
| 366 | const AnnotatedToken *NextToken; |
| 367 | |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 368 | /// \brief The column of the first variable name in a variable declaration. |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 369 | /// |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 370 | /// Used to align further variables if necessary. |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 371 | unsigned VariablePos; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 372 | |
| 373 | /// \brief \c true if this line contains a continued for-loop section. |
| 374 | bool LineContainsContinuedForLoopSection; |
| 375 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 376 | /// \brief The level of nesting inside (), [], <> and {}. |
| 377 | unsigned ParenLevel; |
| 378 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 379 | /// \brief A stack keeping track of properties applying to parenthesis |
| 380 | /// levels. |
| 381 | std::vector<ParenState> Stack; |
| 382 | |
| 383 | /// \brief Comparison operator to be able to used \c LineState in \c map. |
| 384 | bool operator<(const LineState &Other) const { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 385 | if (Other.NextToken != NextToken) |
| 386 | return Other.NextToken > NextToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 387 | if (Other.Column != Column) |
| 388 | return Other.Column > Column; |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 389 | if (Other.VariablePos != VariablePos) |
| 390 | return Other.VariablePos < VariablePos; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 391 | if (Other.LineContainsContinuedForLoopSection != |
| 392 | LineContainsContinuedForLoopSection) |
| 393 | return LineContainsContinuedForLoopSection; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 394 | if (Other.ParenLevel != ParenLevel) |
| 395 | return Other.ParenLevel < ParenLevel; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 396 | return Other.Stack < Stack; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 397 | } |
| 398 | }; |
| 399 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 400 | /// \brief Appends the next token to \p State and updates information |
| 401 | /// necessary for indentation. |
| 402 | /// |
| 403 | /// Puts the token on the current line if \p Newline is \c true and adds a |
| 404 | /// line break and necessary indentation otherwise. |
| 405 | /// |
| 406 | /// If \p DryRun is \c false, also creates and stores the required |
| 407 | /// \c Replacement. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 408 | void addTokenToState(bool Newline, bool DryRun, LineState &State) { |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 409 | const AnnotatedToken &Current = *State.NextToken; |
| 410 | const AnnotatedToken &Previous = *State.NextToken->Parent; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 411 | assert(State.Stack.size()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 412 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 413 | if (Current.Type == TT_ImplicitStringLiteral) { |
| 414 | State.Column += State.NextToken->FormatTok.WhiteSpaceLength + |
| 415 | State.NextToken->FormatTok.TokenLength; |
| 416 | if (State.NextToken->Children.empty()) |
| 417 | State.NextToken = NULL; |
| 418 | else |
| 419 | State.NextToken = &State.NextToken->Children[0]; |
| 420 | return; |
| 421 | } |
| 422 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 423 | if (Newline) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 424 | unsigned WhitespaceStartColumn = State.Column; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 425 | if (Current.is(tok::r_brace)) { |
| 426 | State.Column = Line.Level * 2; |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 427 | } else if (Current.is(tok::string_literal) && |
| 428 | Previous.is(tok::string_literal)) { |
| 429 | State.Column = State.Column - Previous.FormatTok.TokenLength; |
| 430 | } else if (Current.is(tok::lessless) && |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 431 | State.Stack.back().FirstLessLess != 0) { |
| 432 | State.Column = State.Stack.back().FirstLessLess; |
| 433 | } else if (State.ParenLevel != 0 && |
Daniel Jasper | 5f2173e | 2013-01-28 07:43:15 +0000 | [diff] [blame] | 434 | (Previous.is(tok::equal) || Previous.is(tok::coloncolon) || |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 435 | Current.is(tok::period) || Current.is(tok::arrow) || |
| 436 | Current.is(tok::question))) { |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 437 | // Indent and extra 4 spaces after if we know the current expression is |
| 438 | // continued. Don't do that on the top level, as we already indent 4 |
| 439 | // there. |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 440 | State.Column = std::max(State.Stack.back().LastSpace, |
| 441 | State.Stack.back().Indent) + 4; |
| 442 | } else if (Current.Type == TT_ConditionalExpr) { |
| 443 | State.Column = State.Stack.back().QuestionColumn; |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 444 | } else if (Previous.is(tok::comma) && State.VariablePos != 0 && |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 445 | ((RootToken.is(tok::kw_for) && State.ParenLevel == 1) || |
| 446 | State.ParenLevel == 0)) { |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 447 | State.Column = State.VariablePos; |
Daniel Jasper | 83f25ba | 2013-01-28 15:16:31 +0000 | [diff] [blame] | 448 | } else if (State.NextToken->Parent->ClosesTemplateDeclaration || |
| 449 | Current.Type == TT_StartOfName) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 450 | State.Column = State.Stack.back().Indent - 4; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 451 | } else if (Current.Type == TT_ObjCSelectorName) { |
| 452 | if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) { |
| 453 | State.Column = |
| 454 | State.Stack.back().ColonPos - Current.FormatTok.TokenLength; |
| 455 | } else { |
| 456 | State.Column = State.Stack.back().Indent; |
| 457 | State.Stack.back().ColonPos = |
| 458 | State.Column + Current.FormatTok.TokenLength; |
| 459 | } |
| 460 | } else if (Previous.Type == TT_ObjCMethodExpr) { |
| 461 | State.Column = State.Stack.back().Indent + 4; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 462 | } else { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 463 | State.Column = State.Stack.back().Indent; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 466 | if (Previous.is(tok::comma) && !State.Stack.back().AvoidBinPacking) |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 467 | State.Stack.back().BreakBeforeParameter = false; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 468 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 469 | if (RootToken.is(tok::kw_for)) |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 470 | State.LineContainsContinuedForLoopSection = Previous.isNot(tok::semi); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 471 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 472 | if (!DryRun) { |
| 473 | if (!Line.InPPDirective) |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 474 | Whitespaces.replaceWhitespace(Current, 1, State.Column, |
| 475 | WhitespaceStartColumn, Style); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 476 | else |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 477 | Whitespaces.replacePPWhitespace(Current, 1, State.Column, |
| 478 | WhitespaceStartColumn, Style); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 479 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 480 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 481 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 482 | if (Current.is(tok::colon) && Current.Type != TT_ConditionalExpr) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 483 | State.Stack.back().Indent += 2; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 484 | } else { |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 485 | if (Current.is(tok::equal) && |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 486 | (RootToken.is(tok::kw_for) || State.ParenLevel == 0)) |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 487 | State.VariablePos = State.Column - Previous.FormatTok.TokenLength; |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 488 | |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 489 | unsigned Spaces = State.NextToken->SpacesRequiredBefore; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 490 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 491 | if (!DryRun) |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 492 | Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column, Style); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 493 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 494 | if (Current.Type == TT_ObjCSelectorName && |
| 495 | State.Stack.back().ColonPos == 0) { |
| 496 | if (State.Stack.back().Indent + Current.LongestObjCSelectorName > |
| 497 | State.Column + Spaces + Current.FormatTok.TokenLength) |
| 498 | State.Stack.back().ColonPos = |
| 499 | State.Stack.back().Indent + Current.LongestObjCSelectorName; |
| 500 | else |
| 501 | State.Stack.back().ColonPos = |
Daniel Jasper | 9e9e6e0 | 2013-02-06 16:00:26 +0000 | [diff] [blame] | 502 | State.Column + Spaces + Current.FormatTok.TokenLength; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Daniel Jasper | d4f2c2e | 2013-01-29 19:41:55 +0000 | [diff] [blame] | 505 | if (Current.Type != TT_LineComment && |
| 506 | (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) || |
| 507 | State.NextToken->Parent->Type == TT_TemplateOpener)) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 508 | State.Stack.back().Indent = State.Column + Spaces; |
Daniel Jasper | cda1650 | 2013-02-04 08:34:57 +0000 | [diff] [blame] | 509 | if (Previous.is(tok::comma) && !isTrailingComment(Current)) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 510 | State.Stack.back().HasMultiParameterLine = true; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 511 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 512 | State.Column += Spaces; |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 513 | if (Current.is(tok::l_paren) && Previous.is(tok::kw_if)) |
| 514 | // Treat the condition inside an if as if it was a second function |
| 515 | // parameter, i.e. let nested calls have an indent of 4. |
| 516 | State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(". |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 517 | else if (Previous.is(tok::comma) && State.ParenLevel != 0) |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 518 | // Top-level spaces are exempt as that mostly leads to better results. |
| 519 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 520 | else if ((Previous.Type == TT_BinaryOperator || |
Daniel Jasper | 02b771e | 2013-01-28 13:31:35 +0000 | [diff] [blame] | 521 | Previous.Type == TT_ConditionalExpr || |
| 522 | Previous.Type == TT_CtorInitializerColon) && |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 523 | getPrecedence(Previous) != prec::Assignment) |
| 524 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 525 | else if (Previous.ParameterCount > 1 && |
| 526 | (Previous.is(tok::l_paren) || Previous.is(tok::l_square) || |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 527 | Previous.is(tok::l_brace) || |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 528 | Previous.Type == TT_TemplateOpener)) |
| 529 | // If this function has multiple parameters, indent nested calls from |
| 530 | // the start of the first parameter. |
| 531 | State.Stack.back().LastSpace = State.Column; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 532 | } |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 533 | |
| 534 | // If we break after an {, we should also break before the corresponding }. |
| 535 | if (Newline && Previous.is(tok::l_brace)) |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 536 | State.Stack.back().BreakBeforeClosingBrace = true; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 537 | |
Daniel Jasper | 8159d2f | 2013-02-04 07:30:30 +0000 | [diff] [blame] | 538 | if (State.Stack.back().AvoidBinPacking && Newline && |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 539 | (Line.First.isNot(tok::kw_for) || State.ParenLevel != 1)) { |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 540 | // If we are breaking after '(', '{', '<', this is not bin packing unless |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 541 | // AllowAllParametersOfDeclarationOnNextLine is false. |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 542 | if ((Previous.isNot(tok::l_paren) && Previous.isNot(tok::l_brace) && |
| 543 | Previous.Type != TT_TemplateOpener) || |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 544 | (!Style.AllowAllParametersOfDeclarationOnNextLine && |
| 545 | Line.MustBeDeclaration)) |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 546 | State.Stack.back().BreakBeforeParameter = true; |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 547 | |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 548 | // Any break on this level means that the parent level has been broken |
| 549 | // and we need to avoid bin packing there. |
| 550 | for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { |
Daniel Jasper | 8159d2f | 2013-02-04 07:30:30 +0000 | [diff] [blame] | 551 | if (Line.First.isNot(tok::kw_for) || i != 1) |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 552 | State.Stack[i].BreakBeforeParameter = true; |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 555 | |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 556 | moveStateToNextToken(State); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 557 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 558 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 559 | /// \brief Mark the next token as consumed in \p State and modify its stacks |
| 560 | /// accordingly. |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 561 | void moveStateToNextToken(LineState &State) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 562 | const AnnotatedToken &Current = *State.NextToken; |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 563 | assert(State.Stack.size()); |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 564 | |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 565 | if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0) |
| 566 | State.Stack.back().FirstLessLess = State.Column; |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 567 | if (Current.is(tok::question)) |
| 568 | State.Stack.back().QuestionColumn = State.Column; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 569 | if (Current.is(tok::l_brace) && Current.MatchingParen != NULL && |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 570 | !Current.MatchingParen->MustBreakBefore) { |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 571 | if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit()) |
| 572 | State.Stack.back().BreakBeforeParameter = true; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 573 | } |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 574 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 575 | // Insert scopes created by fake parenthesis. |
| 576 | for (unsigned i = 0, e = Current.FakeLParens; i != e; ++i) { |
| 577 | ParenState NewParenState = State.Stack.back(); |
| 578 | NewParenState.Indent = std::max(State.Column, State.Stack.back().Indent); |
| 579 | State.Stack.push_back(NewParenState); |
| 580 | } |
| 581 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 582 | // 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] | 583 | // prepare for the following tokens. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 584 | if (Current.is(tok::l_paren) || Current.is(tok::l_square) || |
| 585 | Current.is(tok::l_brace) || |
| 586 | State.NextToken->Type == TT_TemplateOpener) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 587 | unsigned NewIndent; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 588 | bool AvoidBinPacking; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 589 | if (Current.is(tok::l_brace)) { |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 590 | NewIndent = 2 + State.Stack.back().LastSpace; |
| 591 | AvoidBinPacking = false; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 592 | } else { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 593 | NewIndent = 4 + State.Stack.back().LastSpace; |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 594 | AvoidBinPacking = !Style.BinPackParameters; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 595 | } |
Daniel Jasper | d399bff | 2013-02-05 09:41:21 +0000 | [diff] [blame] | 596 | State.Stack.push_back( |
| 597 | ParenState(NewIndent, State.Stack.back().LastSpace, AvoidBinPacking, |
| 598 | State.Stack.back().HasMultiParameterLine)); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 599 | ++State.ParenLevel; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 602 | // If this '[' opens an ObjC call, determine whether all parameters fit into |
| 603 | // one line and put one per line if they don't. |
| 604 | if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr && |
| 605 | Current.MatchingParen != NULL) { |
| 606 | if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit()) |
| 607 | State.Stack.back().BreakBeforeParameter = true; |
| 608 | } |
| 609 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 610 | // 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] | 611 | // stacks. |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 612 | if (Current.is(tok::r_paren) || Current.is(tok::r_square) || |
| 613 | (Current.is(tok::r_brace) && State.NextToken != &RootToken) || |
| 614 | State.NextToken->Type == TT_TemplateCloser) { |
Daniel Jasper | 604eb4c | 2013-01-11 10:22:12 +0000 | [diff] [blame] | 615 | State.Stack.pop_back(); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 616 | --State.ParenLevel; |
| 617 | } |
| 618 | |
| 619 | // Remove scopes created by fake parenthesis. |
| 620 | for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) { |
| 621 | State.Stack.pop_back(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 622 | } |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 623 | |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 624 | if (State.NextToken->Children.empty()) |
| 625 | State.NextToken = NULL; |
| 626 | else |
| 627 | State.NextToken = &State.NextToken->Children[0]; |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 628 | |
| 629 | State.Column += Current.FormatTok.TokenLength; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 632 | unsigned getColumnLimit() { |
| 633 | return Style.ColumnLimit - (Line.InPPDirective ? 1 : 0); |
| 634 | } |
| 635 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 636 | /// \brief An edge in the solution space starting from the \c LineState and |
| 637 | /// inserting a newline dependent on the \c bool. |
| 638 | typedef std::pair<bool, const LineState *> Edge; |
| 639 | |
| 640 | /// \brief An item in the prioritized BFS search queue. The \c LineState was |
| 641 | /// reached using the \c Edge. |
| 642 | typedef std::pair<LineState, Edge> QueueItem; |
| 643 | |
| 644 | /// \brief Analyze the entire solution space starting from \p InitialState. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 645 | /// |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 646 | /// This implements a variant of Dijkstra's algorithm on the graph that spans |
| 647 | /// the solution space (\c LineStates are the nodes). The algorithm tries to |
| 648 | /// find the shortest path (the one with lowest penalty) from \p InitialState |
| 649 | /// to a state where all tokens are placed. |
| 650 | unsigned analyzeSolutionSpace(const LineState &InitialState) { |
| 651 | // Insert start element into queue. |
| 652 | std::multimap<unsigned, QueueItem> Queue; |
| 653 | Queue.insert(std::pair<unsigned, QueueItem>( |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 654 | 0, QueueItem(InitialState, Edge(false, (const LineState *)0)))); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 655 | std::map<LineState, Edge> Seen; |
| 656 | |
| 657 | // While not empty, take first element and follow edges. |
| 658 | while (!Queue.empty()) { |
| 659 | unsigned Penalty = Queue.begin()->first; |
| 660 | QueueItem Item = Queue.begin()->second; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 661 | if (Item.first.NextToken == NULL) { |
| 662 | DEBUG(llvm::errs() << "\n---\nPenalty for line: " << Penalty << "\n"); |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 663 | break; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 664 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 665 | Queue.erase(Queue.begin()); |
| 666 | |
| 667 | if (Seen.find(Item.first) != Seen.end()) |
| 668 | continue; // State already examined with lower penalty. |
| 669 | |
| 670 | const LineState &SavedState = Seen.insert(std::pair<LineState, Edge>( |
| 671 | Item.first, |
| 672 | Edge(Item.second.first, Item.second.second))).first->first; |
| 673 | |
| 674 | addNextStateToQueue(SavedState, Penalty, /*NewLine=*/ false, Queue); |
| 675 | addNextStateToQueue(SavedState, Penalty, /*NewLine=*/ true, Queue); |
| 676 | } |
| 677 | |
| 678 | if (Queue.empty()) |
| 679 | // We were unable to find a solution, do nothing. |
| 680 | // FIXME: Add diagnostic? |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 681 | return 0; |
| 682 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 683 | // Reconstruct the solution. |
| 684 | // FIXME: Add debugging output. |
| 685 | Edge *CurrentEdge = &Queue.begin()->second.second; |
| 686 | while (CurrentEdge->second != NULL) { |
| 687 | LineState CurrentState = *CurrentEdge->second; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 688 | if (CurrentEdge->first) { |
| 689 | DEBUG(llvm::errs() << "Penalty for splitting before " |
| 690 | << CurrentState.NextToken->FormatTok.Tok.getName() |
| 691 | << ": " << CurrentState.NextToken->SplitPenalty |
| 692 | << "\n"); |
| 693 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 694 | addTokenToState(CurrentEdge->first, false, CurrentState); |
| 695 | CurrentEdge = &Seen[*CurrentEdge->second]; |
| 696 | } |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 697 | DEBUG(llvm::errs() << "---\n"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 698 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 699 | // Return the column after the last token of the solution. |
| 700 | return Queue.begin()->second.first.Column; |
| 701 | } |
| 702 | |
| 703 | /// \brief Add the following state to the analysis queue \p Queue. |
| 704 | /// |
| 705 | /// Assume the current state is \p OldState and has been reached with a |
| 706 | /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true. |
| 707 | void addNextStateToQueue(const LineState &OldState, unsigned Penalty, |
| 708 | bool NewLine, |
| 709 | std::multimap<unsigned, QueueItem> &Queue) { |
| 710 | if (NewLine && !canBreak(OldState)) |
| 711 | return; |
| 712 | if (!NewLine && mustBreak(OldState)) |
| 713 | return; |
| 714 | LineState State(OldState); |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 715 | if (NewLine) |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 716 | Penalty += State.NextToken->SplitPenalty; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 717 | addTokenToState(NewLine, true, State); |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 718 | if (State.Column > getColumnLimit()) { |
| 719 | unsigned ExcessCharacters = State.Column - getColumnLimit(); |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 720 | Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 721 | } |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 722 | Queue.insert(std::pair<unsigned, QueueItem>( |
| 723 | Penalty, QueueItem(State, Edge(NewLine, &OldState)))); |
| 724 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 725 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 726 | /// \brief Returns \c true, if a line break after \p State is allowed. |
| 727 | bool canBreak(const LineState &State) { |
| 728 | if (!State.NextToken->CanBreakBefore && |
| 729 | !(State.NextToken->is(tok::r_brace) && |
| 730 | State.Stack.back().BreakBeforeClosingBrace)) |
| 731 | return false; |
| 732 | // Trying to insert a parameter on a new line if there are already more than |
| 733 | // one parameter on the current line is bin packing. |
Daniel Jasper | d399bff | 2013-02-05 09:41:21 +0000 | [diff] [blame] | 734 | if (State.Stack.back().HasMultiParameterLine && |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 735 | State.Stack.back().AvoidBinPacking) |
| 736 | return false; |
| 737 | return true; |
| 738 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 739 | |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 740 | /// \brief Returns \c true, if a line break after \p State is mandatory. |
| 741 | bool mustBreak(const LineState &State) { |
| 742 | if (State.NextToken->MustBreakBefore) |
| 743 | return true; |
| 744 | if (State.NextToken->is(tok::r_brace) && |
| 745 | State.Stack.back().BreakBeforeClosingBrace) |
| 746 | return true; |
| 747 | if (State.NextToken->Parent->is(tok::semi) && |
| 748 | State.LineContainsContinuedForLoopSection) |
| 749 | return true; |
| 750 | if (State.NextToken->Parent->is(tok::comma) && |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 751 | State.Stack.back().BreakBeforeParameter && |
Daniel Jasper | cda1650 | 2013-02-04 08:34:57 +0000 | [diff] [blame] | 752 | !isTrailingComment(*State.NextToken)) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 753 | return true; |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 754 | // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding |
| 755 | // out whether it is the first parameter. Clean this up. |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 756 | if (State.NextToken->Type == TT_ObjCSelectorName && |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 757 | State.NextToken->LongestObjCSelectorName == 0 && |
| 758 | State.Stack.back().BreakBeforeParameter) |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 759 | return true; |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 760 | if ((State.NextToken->Type == TT_CtorInitializerColon || |
| 761 | (State.NextToken->Parent->ClosesTemplateDeclaration && |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 762 | State.ParenLevel == 0))) |
Daniel Jasper | 68ef0df | 2013-02-01 11:00:45 +0000 | [diff] [blame] | 763 | return true; |
| 764 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 767 | FormatStyle Style; |
| 768 | SourceManager &SourceMgr; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 769 | const AnnotatedLine &Line; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 770 | const unsigned FirstIndent; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 771 | const AnnotatedToken &RootToken; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 772 | WhitespaceManager &Whitespaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 773 | }; |
| 774 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 775 | class LexerBasedFormatTokenSource : public FormatTokenSource { |
| 776 | public: |
| 777 | LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr) |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 778 | : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 779 | IdentTable(Lex.getLangOpts()) { |
| 780 | Lex.SetKeepWhitespaceMode(true); |
| 781 | } |
| 782 | |
| 783 | virtual FormatToken getNextToken() { |
| 784 | if (GreaterStashed) { |
| 785 | FormatTok.NewlinesBefore = 0; |
| 786 | FormatTok.WhiteSpaceStart = |
| 787 | FormatTok.Tok.getLocation().getLocWithOffset(1); |
| 788 | FormatTok.WhiteSpaceLength = 0; |
| 789 | GreaterStashed = false; |
| 790 | return FormatTok; |
| 791 | } |
| 792 | |
| 793 | FormatTok = FormatToken(); |
| 794 | Lex.LexFromRawLexer(FormatTok.Tok); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 795 | StringRef Text = rawTokenText(FormatTok.Tok); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 796 | FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation(); |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 797 | if (SourceMgr.getFileOffset(FormatTok.WhiteSpaceStart) == 0) |
| 798 | FormatTok.IsFirst = true; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 799 | |
| 800 | // Consume and record whitespace until we find a significant token. |
| 801 | while (FormatTok.Tok.is(tok::unknown)) { |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 802 | unsigned Newlines = Text.count('\n'); |
| 803 | unsigned EscapedNewlines = Text.count("\\\n"); |
| 804 | FormatTok.NewlinesBefore += Newlines; |
| 805 | FormatTok.HasUnescapedNewline |= EscapedNewlines != Newlines; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 806 | FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength(); |
| 807 | |
| 808 | if (FormatTok.Tok.is(tok::eof)) |
| 809 | return FormatTok; |
| 810 | Lex.LexFromRawLexer(FormatTok.Tok); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 811 | Text = rawTokenText(FormatTok.Tok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 812 | } |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 813 | |
| 814 | // Now FormatTok is the next non-whitespace token. |
| 815 | FormatTok.TokenLength = Text.size(); |
| 816 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 817 | // In case the token starts with escaped newlines, we want to |
| 818 | // take them into account as whitespace - this pattern is quite frequent |
| 819 | // in macro definitions. |
| 820 | // FIXME: What do we want to do with other escaped spaces, and escaped |
| 821 | // spaces or newlines in the middle of tokens? |
| 822 | // FIXME: Add a more explicit test. |
| 823 | unsigned i = 0; |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 824 | while (i + 1 < Text.size() && Text[i] == '\\' && Text[i + 1] == '\n') { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 825 | // FIXME: ++FormatTok.NewlinesBefore is missing... |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 826 | FormatTok.WhiteSpaceLength += 2; |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 827 | FormatTok.TokenLength -= 2; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 828 | i += 2; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | if (FormatTok.Tok.is(tok::raw_identifier)) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 832 | IdentifierInfo &Info = IdentTable.get(Text); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 833 | FormatTok.Tok.setIdentifierInfo(&Info); |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 834 | FormatTok.Tok.setKind(Info.getTokenID()); |
| 835 | } |
| 836 | |
| 837 | if (FormatTok.Tok.is(tok::greatergreater)) { |
| 838 | FormatTok.Tok.setKind(tok::greater); |
| 839 | GreaterStashed = true; |
| 840 | } |
| 841 | |
| 842 | return FormatTok; |
| 843 | } |
| 844 | |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame^] | 845 | IdentifierTable &getIdentTable() { return IdentTable; } |
| 846 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 847 | private: |
| 848 | FormatToken FormatTok; |
| 849 | bool GreaterStashed; |
| 850 | Lexer &Lex; |
| 851 | SourceManager &SourceMgr; |
| 852 | IdentifierTable IdentTable; |
| 853 | |
| 854 | /// Returns the text of \c FormatTok. |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 855 | StringRef rawTokenText(Token &Tok) { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 856 | return StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
| 857 | Tok.getLength()); |
| 858 | } |
| 859 | }; |
| 860 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 861 | class Formatter : public UnwrappedLineConsumer { |
| 862 | public: |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 863 | Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style, Lexer &Lex, |
| 864 | SourceManager &SourceMgr, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 865 | const std::vector<CharSourceRange> &Ranges) |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 866 | : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr), |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 867 | Whitespaces(SourceMgr), Ranges(Ranges) { |
| 868 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 869 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 870 | virtual ~Formatter() {} |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 871 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 872 | void deriveLocalStyle() { |
| 873 | unsigned CountBoundToVariable = 0; |
| 874 | unsigned CountBoundToType = 0; |
| 875 | bool HasCpp03IncompatibleFormat = false; |
| 876 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 877 | if (AnnotatedLines[i].First.Children.empty()) |
| 878 | continue; |
| 879 | AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0]; |
| 880 | while (!Tok->Children.empty()) { |
| 881 | if (Tok->Type == TT_PointerOrReference) { |
| 882 | bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0; |
| 883 | bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0; |
| 884 | if (SpacesBefore && !SpacesAfter) |
| 885 | ++CountBoundToVariable; |
| 886 | else if (!SpacesBefore && SpacesAfter) |
| 887 | ++CountBoundToType; |
| 888 | } |
| 889 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 890 | if (Tok->Type == TT_TemplateCloser && |
| 891 | Tok->Parent->Type == TT_TemplateCloser && |
| 892 | Tok->FormatTok.WhiteSpaceLength == 0) |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 893 | HasCpp03IncompatibleFormat = true; |
| 894 | Tok = &Tok->Children[0]; |
| 895 | } |
| 896 | } |
| 897 | if (Style.DerivePointerBinding) { |
| 898 | if (CountBoundToType > CountBoundToVariable) |
| 899 | Style.PointerBindsToType = true; |
| 900 | else if (CountBoundToType < CountBoundToVariable) |
| 901 | Style.PointerBindsToType = false; |
| 902 | } |
| 903 | if (Style.Standard == FormatStyle::LS_Auto) { |
| 904 | Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11 |
| 905 | : FormatStyle::LS_Cpp03; |
| 906 | } |
| 907 | } |
| 908 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 909 | tooling::Replacements format() { |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 910 | LexerBasedFormatTokenSource Tokens(Lex, SourceMgr); |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 911 | UnwrappedLineParser Parser(Diag, Style, Tokens, *this); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 912 | StructuralError = Parser.parse(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 913 | unsigned PreviousEndOfLineColumn = 0; |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame^] | 914 | TokenAnnotator Annotator(Style, SourceMgr, Lex, |
| 915 | Tokens.getIdentTable().get("in")); |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 916 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 917 | Annotator.annotate(AnnotatedLines[i]); |
| 918 | } |
| 919 | deriveLocalStyle(); |
| 920 | for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { |
| 921 | Annotator.calculateFormattingInformation(AnnotatedLines[i]); |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 922 | } |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 923 | std::vector<int> IndentForLevel; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 924 | for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(), |
| 925 | E = AnnotatedLines.end(); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 926 | I != E; ++I) { |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 927 | const AnnotatedLine &TheLine = *I; |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 928 | int Offset = GetIndentOffset(TheLine.First); |
| 929 | while (IndentForLevel.size() <= TheLine.Level) |
| 930 | IndentForLevel.push_back(-1); |
| 931 | IndentForLevel.resize(TheLine.Level + 1); |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 932 | if (touchesRanges(TheLine) && TheLine.Type != LT_Invalid) { |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 933 | unsigned LevelIndent = GetIndent(IndentForLevel, TheLine.Level); |
| 934 | unsigned Indent = LevelIndent; |
| 935 | if (static_cast<int>(Indent) + Offset >= 0) |
| 936 | Indent += Offset; |
| 937 | if (!TheLine.First.FormatTok.WhiteSpaceStart.isValid() || |
| 938 | StructuralError) { |
| 939 | Indent = LevelIndent = SourceMgr.getSpellingColumnNumber( |
| 940 | TheLine.First.FormatTok.Tok.getLocation()) - 1; |
| 941 | } else { |
| 942 | formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective, |
| 943 | PreviousEndOfLineColumn); |
| 944 | } |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 945 | tryFitMultipleLinesInOne(Indent, I, E); |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 946 | UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent, |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 947 | TheLine.First, Whitespaces, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 948 | StructuralError); |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 949 | PreviousEndOfLineColumn = Formatter.format(); |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 950 | IndentForLevel[TheLine.Level] = LevelIndent; |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 951 | } else { |
| 952 | // If we did not reformat this unwrapped line, the column at the end of |
| 953 | // the last token is unchanged - thus, we can calculate the end of the |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 954 | // last token. |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 955 | PreviousEndOfLineColumn = |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 956 | SourceMgr.getSpellingColumnNumber( |
| 957 | TheLine.Last->FormatTok.Tok.getLocation()) + |
| 958 | Lex.MeasureTokenLength(TheLine.Last->FormatTok.Tok.getLocation(), |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 959 | SourceMgr, Lex.getLangOpts()) - 1; |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 960 | unsigned Indent = SourceMgr.getSpellingColumnNumber( |
| 961 | TheLine.First.FormatTok.Tok.getLocation()) - 1; |
| 962 | unsigned LevelIndent = Indent; |
| 963 | if (static_cast<int>(LevelIndent) - Offset >= 0) |
| 964 | LevelIndent -= Offset; |
| 965 | IndentForLevel[TheLine.Level] = LevelIndent; |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 968 | return Whitespaces.generateReplacements(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | private: |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 972 | /// \brief Get the indent of \p Level from \p IndentForLevel. |
| 973 | /// |
| 974 | /// \p IndentForLevel must contain the indent for the level \c l |
| 975 | /// at \p IndentForLevel[l], or a value < 0 if the indent for |
| 976 | /// that level is unknown. |
| 977 | unsigned GetIndent(const std::vector<int> IndentForLevel, |
| 978 | unsigned Level) { |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 979 | if (IndentForLevel[Level] != -1) |
| 980 | return IndentForLevel[Level]; |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 981 | if (Level == 0) |
| 982 | return 0; |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 983 | return GetIndent(IndentForLevel, Level - 1) + 2; |
| 984 | } |
| 985 | |
| 986 | /// \brief Get the offset of the line relatively to the level. |
| 987 | /// |
| 988 | /// For example, 'public:' labels in classes are offset by 1 or 2 |
| 989 | /// characters to the left from their level. |
| 990 | int GetIndentOffset(const AnnotatedToken &RootToken) { |
| 991 | bool IsAccessModifier = false; |
| 992 | if (RootToken.is(tok::kw_public) || RootToken.is(tok::kw_protected) || |
| 993 | RootToken.is(tok::kw_private)) |
| 994 | IsAccessModifier = true; |
| 995 | else if (RootToken.is(tok::at) && !RootToken.Children.empty() && |
| 996 | (RootToken.Children[0].isObjCAtKeyword(tok::objc_public) || |
| 997 | RootToken.Children[0].isObjCAtKeyword(tok::objc_protected) || |
| 998 | RootToken.Children[0].isObjCAtKeyword(tok::objc_package) || |
| 999 | RootToken.Children[0].isObjCAtKeyword(tok::objc_private))) |
| 1000 | IsAccessModifier = true; |
| 1001 | |
| 1002 | if (IsAccessModifier) |
| 1003 | return Style.AccessModifierOffset; |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1007 | /// \brief Tries to merge lines into one. |
| 1008 | /// |
| 1009 | /// This will change \c Line and \c AnnotatedLine to contain the merged line, |
| 1010 | /// if possible; note that \c I will be incremented when lines are merged. |
| 1011 | /// |
| 1012 | /// Returns whether the resulting \c Line can fit in a single line. |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1013 | void tryFitMultipleLinesInOne(unsigned Indent, |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1014 | std::vector<AnnotatedLine>::iterator &I, |
| 1015 | std::vector<AnnotatedLine>::iterator E) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1016 | unsigned Limit = Style.ColumnLimit - (I->InPPDirective ? 1 : 0) - Indent; |
| 1017 | |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1018 | // We can never merge stuff if there are trailing line comments. |
| 1019 | if (I->Last->Type == TT_LineComment) |
| 1020 | return; |
| 1021 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1022 | // Check whether the UnwrappedLine can be put onto a single line. If |
| 1023 | // so, this is bound to be the optimal solution (by definition) and we |
| 1024 | // don't need to analyze the entire solution space. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1025 | if (I->Last->TotalLength > Limit) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1026 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1027 | Limit -= I->Last->TotalLength; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1028 | |
Daniel Jasper | 9c8c40e | 2013-01-21 14:18:28 +0000 | [diff] [blame] | 1029 | if (I + 1 == E || (I + 1)->Type == LT_Invalid) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1030 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1031 | |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1032 | if (I->Last->is(tok::l_brace)) { |
| 1033 | tryMergeSimpleBlock(I, E, Limit); |
| 1034 | } else if (I->First.is(tok::kw_if)) { |
| 1035 | tryMergeSimpleIf(I, E, Limit); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1036 | } else if (I->InPPDirective && (I->First.FormatTok.HasUnescapedNewline || |
| 1037 | I->First.FormatTok.IsFirst)) { |
| 1038 | tryMergeSimplePPDirective(I, E, Limit); |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1039 | } |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1040 | return; |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1043 | void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I, |
| 1044 | std::vector<AnnotatedLine>::iterator E, |
| 1045 | unsigned Limit) { |
| 1046 | AnnotatedLine &Line = *I; |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 1047 | if (!(I + 1)->InPPDirective || (I + 1)->First.FormatTok.HasUnescapedNewline) |
| 1048 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1049 | if (I + 2 != E && (I + 2)->InPPDirective && |
| 1050 | !(I + 2)->First.FormatTok.HasUnescapedNewline) |
| 1051 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1052 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 1053 | return; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1054 | join(Line, *(++I)); |
| 1055 | } |
| 1056 | |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1057 | void tryMergeSimpleIf(std::vector<AnnotatedLine>::iterator &I, |
| 1058 | std::vector<AnnotatedLine>::iterator E, |
| 1059 | unsigned Limit) { |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 1060 | if (!Style.AllowShortIfStatementsOnASingleLine) |
| 1061 | return; |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 1062 | if ((I + 1)->InPPDirective != I->InPPDirective || |
| 1063 | ((I + 1)->InPPDirective && |
| 1064 | (I + 1)->First.FormatTok.HasUnescapedNewline)) |
| 1065 | return; |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1066 | AnnotatedLine &Line = *I; |
Daniel Jasper | 55b08e7 | 2013-01-16 07:02:34 +0000 | [diff] [blame] | 1067 | if (Line.Last->isNot(tok::r_paren)) |
| 1068 | return; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1069 | if (1 + (I + 1)->Last->TotalLength > Limit) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1070 | return; |
| 1071 | if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment) |
| 1072 | return; |
| 1073 | // Only inline simple if's (no nested if or else). |
| 1074 | if (I + 2 != E && (I + 2)->First.is(tok::kw_else)) |
| 1075 | return; |
| 1076 | join(Line, *(++I)); |
| 1077 | } |
| 1078 | |
| 1079 | void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I, |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1080 | std::vector<AnnotatedLine>::iterator E, |
| 1081 | unsigned Limit) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1082 | // First, check that the current line allows merging. This is the case if |
| 1083 | // we're not in a control flow statement and the last token is an opening |
| 1084 | // brace. |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1085 | AnnotatedLine &Line = *I; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1086 | bool AllowedTokens = |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1087 | Line.First.isNot(tok::kw_if) && Line.First.isNot(tok::kw_while) && |
| 1088 | Line.First.isNot(tok::kw_do) && Line.First.isNot(tok::r_brace) && |
| 1089 | Line.First.isNot(tok::kw_else) && Line.First.isNot(tok::kw_try) && |
| 1090 | Line.First.isNot(tok::kw_catch) && Line.First.isNot(tok::kw_for) && |
Nico Weber | 67015ed | 2013-01-11 21:14:08 +0000 | [diff] [blame] | 1091 | // This gets rid of all ObjC @ keywords and methods. |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1092 | Line.First.isNot(tok::at) && Line.First.isNot(tok::minus) && |
| 1093 | Line.First.isNot(tok::plus); |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1094 | if (!AllowedTokens) |
| 1095 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1096 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1097 | AnnotatedToken *Tok = &(I + 1)->First; |
| 1098 | if (Tok->Children.empty() && Tok->is(tok::r_brace) && |
| 1099 | !Tok->MustBreakBefore && Tok->TotalLength <= Limit) { |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 1100 | Tok->SpacesRequiredBefore = 0; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1101 | join(Line, *(I + 1)); |
| 1102 | I += 1; |
| 1103 | } else { |
| 1104 | // Check that we still have three lines and they fit into the limit. |
| 1105 | if (I + 2 == E || (I + 2)->Type == LT_Invalid || |
| 1106 | !nextTwoLinesFitInto(I, Limit)) |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1107 | return; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1108 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1109 | // Second, check that the next line does not contain any braces - if it |
| 1110 | // does, readability declines when putting it into a single line. |
| 1111 | if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore) |
| 1112 | return; |
| 1113 | do { |
| 1114 | if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace)) |
| 1115 | return; |
| 1116 | Tok = Tok->Children.empty() ? NULL : &Tok->Children.back(); |
| 1117 | } while (Tok != NULL); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1118 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1119 | // Last, check that the third line contains a single closing brace. |
| 1120 | Tok = &(I + 2)->First; |
| 1121 | if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) || |
| 1122 | Tok->MustBreakBefore) |
| 1123 | return; |
| 1124 | |
| 1125 | join(Line, *(I + 1)); |
| 1126 | join(Line, *(I + 2)); |
| 1127 | I += 2; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1128 | } |
Daniel Jasper | feb18f5 | 2013-01-14 14:14:23 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I, |
| 1132 | unsigned Limit) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1133 | return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <= |
| 1134 | Limit; |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1137 | void join(AnnotatedLine &A, const AnnotatedLine &B) { |
| 1138 | A.Last->Children.push_back(B.First); |
| 1139 | while (!A.Last->Children.empty()) { |
| 1140 | A.Last->Children[0].Parent = A.Last; |
| 1141 | A.Last = &A.Last->Children[0]; |
| 1142 | } |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1145 | bool touchesRanges(const AnnotatedLine &TheLine) { |
| 1146 | const FormatToken *First = &TheLine.First.FormatTok; |
| 1147 | const FormatToken *Last = &TheLine.Last->FormatTok; |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1148 | CharSourceRange LineRange = CharSourceRange::getTokenRange( |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1149 | First->Tok.getLocation(), Last->Tok.getLocation()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1150 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1151 | if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(), |
| 1152 | Ranges[i].getBegin()) && |
| 1153 | !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 1154 | LineRange.getBegin())) |
| 1155 | return true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1156 | } |
Manuel Klimek | f9ea2ed | 2013-01-10 19:49:59 +0000 | [diff] [blame] | 1157 | return false; |
| 1158 | } |
| 1159 | |
| 1160 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1161 | AnnotatedLines.push_back(AnnotatedLine(TheLine)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1164 | /// \brief Add a new line and the required indent before the first Token |
| 1165 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 1166 | /// Returns the indent level of the \c UnwrappedLine. |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 1167 | void formatFirstToken(const AnnotatedToken &RootToken, unsigned Indent, |
| 1168 | bool InPPDirective, unsigned PreviousEndOfLineColumn) { |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1169 | const FormatToken &Tok = RootToken.FormatTok; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1170 | |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1171 | unsigned Newlines = |
| 1172 | std::min(Tok.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1173 | if (Newlines == 0 && !Tok.IsFirst) |
| 1174 | Newlines = 1; |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1175 | |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1176 | if (!InPPDirective || Tok.HasUnescapedNewline) { |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1177 | Whitespaces.replaceWhitespace(RootToken, Newlines, Indent, 0, Style); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1178 | } else { |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1179 | Whitespaces.replacePPWhitespace(RootToken, Newlines, Indent, |
| 1180 | PreviousEndOfLineColumn, Style); |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1181 | } |
Manuel Klimek | 3f8c7f3 | 2013-01-10 18:45:26 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1184 | DiagnosticsEngine &Diag; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1185 | FormatStyle Style; |
| 1186 | Lexer &Lex; |
| 1187 | SourceManager &SourceMgr; |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 1188 | WhitespaceManager Whitespaces; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1189 | std::vector<CharSourceRange> Ranges; |
Daniel Jasper | 995e820 | 2013-01-14 13:08:07 +0000 | [diff] [blame] | 1190 | std::vector<AnnotatedLine> AnnotatedLines; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1191 | bool StructuralError; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1192 | }; |
| 1193 | |
Daniel Jasper | 1a1ce83 | 2013-01-29 11:27:30 +0000 | [diff] [blame] | 1194 | tooling::Replacements |
| 1195 | reformat(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, |
| 1196 | std::vector<CharSourceRange> Ranges, DiagnosticConsumer *DiagClient) { |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1197 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1198 | OwningPtr<DiagnosticConsumer> DiagPrinter; |
| 1199 | if (DiagClient == 0) { |
| 1200 | DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts)); |
| 1201 | DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP()); |
| 1202 | DiagClient = DiagPrinter.get(); |
| 1203 | } |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1204 | DiagnosticsEngine Diagnostics( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1205 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1206 | DiagClient, false); |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 1207 | Diagnostics.setSourceManager(&SourceMgr); |
| 1208 | Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1209 | return formatter.format(); |
| 1210 | } |
| 1211 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1212 | LangOptions getFormattingLangOpts() { |
| 1213 | LangOptions LangOpts; |
| 1214 | LangOpts.CPlusPlus = 1; |
| 1215 | LangOpts.CPlusPlus11 = 1; |
| 1216 | LangOpts.Bool = 1; |
| 1217 | LangOpts.ObjC1 = 1; |
| 1218 | LangOpts.ObjC2 = 1; |
| 1219 | return LangOpts; |
| 1220 | } |
| 1221 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1222 | } // namespace format |
| 1223 | } // namespace clang |