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