Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 1 | //===--- FormatToken.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 specific functions of \c FormatTokens and their |
| 12 | /// roles. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "FormatToken.h" |
| 17 | #include "ContinuationIndenter.h" |
| 18 | #include "clang/Format/Format.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/Support/Debug.h" |
Daniel Jasper | d9ff035 | 2015-05-11 13:52:13 +0000 | [diff] [blame] | 21 | #include <climits> |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 22 | |
| 23 | namespace clang { |
| 24 | namespace format { |
| 25 | |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 26 | // FIXME: This is copy&pasted from Sema. Put it in a common place and remove |
| 27 | // duplication. |
| 28 | bool FormatToken::isSimpleTypeSpecifier() const { |
| 29 | switch (Tok.getKind()) { |
| 30 | case tok::kw_short: |
| 31 | case tok::kw_long: |
| 32 | case tok::kw___int64: |
| 33 | case tok::kw___int128: |
| 34 | case tok::kw_signed: |
| 35 | case tok::kw_unsigned: |
| 36 | case tok::kw_void: |
| 37 | case tok::kw_char: |
| 38 | case tok::kw_int: |
| 39 | case tok::kw_half: |
| 40 | case tok::kw_float: |
| 41 | case tok::kw_double: |
| 42 | case tok::kw_wchar_t: |
| 43 | case tok::kw_bool: |
| 44 | case tok::kw___underlying_type: |
| 45 | case tok::annot_typename: |
| 46 | case tok::kw_char16_t: |
| 47 | case tok::kw_char32_t: |
| 48 | case tok::kw_typeof: |
| 49 | case tok::kw_decltype: |
| 50 | return true; |
| 51 | default: |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 56 | TokenRole::~TokenRole() {} |
| 57 | |
| 58 | void TokenRole::precomputeFormattingInfos(const FormatToken *Token) {} |
| 59 | |
Daniel Jasper | 0160347 | 2014-01-09 13:42:56 +0000 | [diff] [blame] | 60 | unsigned CommaSeparatedList::formatAfterToken(LineState &State, |
| 61 | ContinuationIndenter *Indenter, |
| 62 | bool DryRun) { |
Daniel Jasper | 60c2707 | 2015-05-13 08:16:00 +0000 | [diff] [blame] | 63 | if (State.NextToken == nullptr || !State.NextToken->Previous) |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 64 | return 0; |
| 65 | |
| 66 | // Ensure that we start on the opening brace. |
Daniel Jasper | 60c2707 | 2015-05-13 08:16:00 +0000 | [diff] [blame] | 67 | const FormatToken *LBrace = |
| 68 | State.NextToken->Previous->getPreviousNonComment(); |
| 69 | if (!LBrace || LBrace->isNot(tok::l_brace) || LBrace->BlockKind == BK_Block || |
Daniel Jasper | b596fb2 | 2013-10-24 10:31:50 +0000 | [diff] [blame] | 70 | LBrace->Type == TT_DictLiteral || |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 71 | LBrace->Next->Type == TT_DesignatedInitializerPeriod) |
| 72 | return 0; |
| 73 | |
Daniel Jasper | cb3f0ed | 2013-08-27 08:43:47 +0000 | [diff] [blame] | 74 | // Calculate the number of code points we have to format this list. As the |
| 75 | // first token is already placed, we have to subtract it. |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 76 | unsigned RemainingCodePoints = |
| 77 | Style.ColumnLimit - State.Column + State.NextToken->Previous->ColumnWidth; |
Daniel Jasper | cb3f0ed | 2013-08-27 08:43:47 +0000 | [diff] [blame] | 78 | |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 79 | // Find the best ColumnFormat, i.e. the best number of columns to use. |
Daniel Jasper | cb3f0ed | 2013-08-27 08:43:47 +0000 | [diff] [blame] | 80 | const ColumnFormat *Format = getColumnFormat(RemainingCodePoints); |
Daniel Jasper | 1a1a2ab | 2013-11-23 10:22:59 +0000 | [diff] [blame] | 81 | // If no ColumnFormat can be used, the braced list would generally be |
| 82 | // bin-packed. Add a severe penalty to this so that column layouts are |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 83 | // preferred if possible. |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 84 | if (!Format) |
Daniel Jasper | 1a1a2ab | 2013-11-23 10:22:59 +0000 | [diff] [blame] | 85 | return 10000; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 86 | |
| 87 | // Format the entire list. |
| 88 | unsigned Penalty = 0; |
| 89 | unsigned Column = 0; |
| 90 | unsigned Item = 0; |
| 91 | while (State.NextToken != LBrace->MatchingParen) { |
| 92 | bool NewLine = false; |
| 93 | unsigned ExtraSpaces = 0; |
| 94 | |
| 95 | // If the previous token was one of our commas, we are now on the next item. |
| 96 | if (Item < Commas.size() && State.NextToken->Previous == Commas[Item]) { |
| 97 | if (!State.NextToken->isTrailingComment()) { |
| 98 | ExtraSpaces += Format->ColumnSizes[Column] - ItemLengths[Item]; |
| 99 | ++Column; |
| 100 | } |
| 101 | ++Item; |
| 102 | } |
| 103 | |
| 104 | if (Column == Format->Columns || State.NextToken->MustBreakBefore) { |
| 105 | Column = 0; |
| 106 | NewLine = true; |
| 107 | } |
| 108 | |
| 109 | // Place token using the continuation indenter and store the penalty. |
| 110 | Penalty += Indenter->addTokenToState(State, NewLine, DryRun, ExtraSpaces); |
| 111 | } |
| 112 | return Penalty; |
| 113 | } |
| 114 | |
Daniel Jasper | 0160347 | 2014-01-09 13:42:56 +0000 | [diff] [blame] | 115 | unsigned CommaSeparatedList::formatFromToken(LineState &State, |
| 116 | ContinuationIndenter *Indenter, |
| 117 | bool DryRun) { |
| 118 | if (HasNestedBracedList) |
| 119 | State.Stack.back().AvoidBinPacking = true; |
| 120 | return 0; |
| 121 | } |
| 122 | |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 123 | // Returns the lengths in code points between Begin and End (both included), |
| 124 | // assuming that the entire sequence is put on a single line. |
| 125 | static unsigned CodePointsBetween(const FormatToken *Begin, |
| 126 | const FormatToken *End) { |
Daniel Jasper | 8863ada | 2013-08-26 08:10:17 +0000 | [diff] [blame] | 127 | assert(End->TotalLength >= Begin->TotalLength); |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 128 | return End->TotalLength - Begin->TotalLength + Begin->ColumnWidth; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { |
Daniel Jasper | 8863ada | 2013-08-26 08:10:17 +0000 | [diff] [blame] | 132 | // FIXME: At some point we might want to do this for other lists, too. |
Daniel Jasper | 0160347 | 2014-01-09 13:42:56 +0000 | [diff] [blame] | 133 | if (!Token->MatchingParen || Token->isNot(tok::l_brace)) |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 134 | return; |
| 135 | |
Daniel Jasper | 839922e | 2014-08-13 08:46:21 +0000 | [diff] [blame] | 136 | // In C++11 braced list style, we should not format in columns unless they |
| 137 | // have many items (20 or more) or we allow bin-packing of function |
| 138 | // parameters. |
| 139 | if (Style.Cpp11BracedListStyle && !Style.BinPackParameters && |
| 140 | Commas.size() < 19) |
Daniel Jasper | ae8e0d8 | 2014-04-17 11:32:02 +0000 | [diff] [blame] | 141 | return; |
| 142 | |
Daniel Jasper | 13404da | 2014-11-27 14:40:48 +0000 | [diff] [blame] | 143 | // Column format doesn't really make sense if we don't align after brackets. |
| 144 | if (!Style.AlignAfterOpenBracket) |
| 145 | return; |
| 146 | |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 147 | FormatToken *ItemBegin = Token->Next; |
Daniel Jasper | 60c2707 | 2015-05-13 08:16:00 +0000 | [diff] [blame] | 148 | while (ItemBegin->isTrailingComment()) |
| 149 | ItemBegin = ItemBegin->Next; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 150 | SmallVector<bool, 8> MustBreakBeforeItem; |
| 151 | |
| 152 | // The lengths of an item if it is put at the end of the line. This includes |
| 153 | // trailing comments which are otherwise ignored for column alignment. |
| 154 | SmallVector<unsigned, 8> EndOfLineItemLength; |
| 155 | |
Daniel Jasper | 731dde9 | 2015-05-15 09:41:59 +0000 | [diff] [blame^] | 156 | bool HasSeparatingComment = false; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 157 | for (unsigned i = 0, e = Commas.size() + 1; i != e; ++i) { |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 158 | // Skip comments on their own line. |
Daniel Jasper | 731dde9 | 2015-05-15 09:41:59 +0000 | [diff] [blame^] | 159 | while (ItemBegin->HasUnescapedNewline && ItemBegin->isTrailingComment()) { |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 160 | ItemBegin = ItemBegin->Next; |
Daniel Jasper | 731dde9 | 2015-05-15 09:41:59 +0000 | [diff] [blame^] | 161 | HasSeparatingComment = i > 0; |
| 162 | } |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 163 | |
| 164 | MustBreakBeforeItem.push_back(ItemBegin->MustBreakBefore); |
Daniel Jasper | 7c72d9f | 2013-10-24 14:14:49 +0000 | [diff] [blame] | 165 | if (ItemBegin->is(tok::l_brace)) |
| 166 | HasNestedBracedList = true; |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 167 | const FormatToken *ItemEnd = nullptr; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 168 | if (i == Commas.size()) { |
| 169 | ItemEnd = Token->MatchingParen; |
| 170 | const FormatToken *NonCommentEnd = ItemEnd->getPreviousNonComment(); |
| 171 | ItemLengths.push_back(CodePointsBetween(ItemBegin, NonCommentEnd)); |
| 172 | if (Style.Cpp11BracedListStyle) { |
| 173 | // In Cpp11 braced list style, the } and possibly other subsequent |
| 174 | // tokens will need to stay on a line with the last element. |
| 175 | while (ItemEnd->Next && !ItemEnd->Next->CanBreakBefore) |
| 176 | ItemEnd = ItemEnd->Next; |
| 177 | } else { |
| 178 | // In other braced lists styles, the "}" can be wrapped to the new line. |
| 179 | ItemEnd = Token->MatchingParen->Previous; |
| 180 | } |
| 181 | } else { |
| 182 | ItemEnd = Commas[i]; |
| 183 | // The comma is counted as part of the item when calculating the length. |
Daniel Jasper | 8863ada | 2013-08-26 08:10:17 +0000 | [diff] [blame] | 184 | ItemLengths.push_back(CodePointsBetween(ItemBegin, ItemEnd)); |
Daniel Jasper | 540dbe2 | 2014-09-19 08:28:43 +0000 | [diff] [blame] | 185 | |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 186 | // Consume trailing comments so the are included in EndOfLineItemLength. |
| 187 | if (ItemEnd->Next && !ItemEnd->Next->HasUnescapedNewline && |
| 188 | ItemEnd->Next->isTrailingComment()) |
| 189 | ItemEnd = ItemEnd->Next; |
| 190 | } |
| 191 | EndOfLineItemLength.push_back(CodePointsBetween(ItemBegin, ItemEnd)); |
Daniel Jasper | 8863ada | 2013-08-26 08:10:17 +0000 | [diff] [blame] | 192 | // If there is a trailing comma in the list, the next item will start at the |
| 193 | // closing brace. Don't create an extra item for this. |
| 194 | if (ItemEnd->getNextNonComment() == Token->MatchingParen) |
| 195 | break; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 196 | ItemBegin = ItemEnd->Next; |
| 197 | } |
| 198 | |
Daniel Jasper | 731dde9 | 2015-05-15 09:41:59 +0000 | [diff] [blame^] | 199 | // Don't use column layout for nested lists, lists with few elements and in |
| 200 | // presence of separating comments. |
| 201 | if (Token->NestingLevel != 0 || Commas.size() < 5 || HasSeparatingComment) |
Daniel Jasper | 0160347 | 2014-01-09 13:42:56 +0000 | [diff] [blame] | 202 | return; |
| 203 | |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 204 | // We can never place more than ColumnLimit / 3 items in a row (because of the |
| 205 | // spaces and the comma). |
Daniel Jasper | d57843d | 2015-05-11 13:35:40 +0000 | [diff] [blame] | 206 | for (unsigned Columns = 1; Columns <= Style.ColumnLimit / 3; ++Columns) { |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 207 | ColumnFormat Format; |
| 208 | Format.Columns = Columns; |
| 209 | Format.ColumnSizes.resize(Columns); |
Daniel Jasper | d57843d | 2015-05-11 13:35:40 +0000 | [diff] [blame] | 210 | std::vector<unsigned> MinSizeInColumn(Columns, UINT_MAX); |
Daniel Jasper | 7c72d9f | 2013-10-24 14:14:49 +0000 | [diff] [blame] | 211 | Format.LineCount = 1; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 212 | bool HasRowWithSufficientColumns = false; |
| 213 | unsigned Column = 0; |
| 214 | for (unsigned i = 0, e = ItemLengths.size(); i != e; ++i) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 215 | assert(i < MustBreakBeforeItem.size()); |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 216 | if (MustBreakBeforeItem[i] || Column == Columns) { |
| 217 | ++Format.LineCount; |
| 218 | Column = 0; |
| 219 | } |
| 220 | if (Column == Columns - 1) |
| 221 | HasRowWithSufficientColumns = true; |
Daniel Jasper | d57843d | 2015-05-11 13:35:40 +0000 | [diff] [blame] | 222 | unsigned Length = |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 223 | (Column == Columns - 1) ? EndOfLineItemLength[i] : ItemLengths[i]; |
Daniel Jasper | d57843d | 2015-05-11 13:35:40 +0000 | [diff] [blame] | 224 | Format.ColumnSizes[Column] = std::max(Format.ColumnSizes[Column], Length); |
| 225 | MinSizeInColumn[Column] = std::min(MinSizeInColumn[Column], Length); |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 226 | ++Column; |
| 227 | } |
| 228 | // If all rows are terminated early (e.g. by trailing comments), we don't |
| 229 | // need to look further. |
| 230 | if (!HasRowWithSufficientColumns) |
| 231 | break; |
| 232 | Format.TotalWidth = Columns - 1; // Width of the N-1 spaces. |
Daniel Jasper | d57843d | 2015-05-11 13:35:40 +0000 | [diff] [blame] | 233 | |
| 234 | for (unsigned i = 0; i < Columns; ++i) |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 235 | Format.TotalWidth += Format.ColumnSizes[i]; |
Daniel Jasper | d57843d | 2015-05-11 13:35:40 +0000 | [diff] [blame] | 236 | |
| 237 | // Don't use this Format, if the difference between the longest and shortest |
| 238 | // element in a column exceeds a threshold to avoid excessive spaces. |
| 239 | if ([&] { |
| 240 | for (unsigned i = 0; i < Columns - 1; ++i) |
| 241 | if (Format.ColumnSizes[i] - MinSizeInColumn[i] > 10) |
| 242 | return true; |
| 243 | return false; |
| 244 | }()) |
| 245 | continue; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 246 | |
| 247 | // Ignore layouts that are bound to violate the column limit. |
| 248 | if (Format.TotalWidth > Style.ColumnLimit) |
| 249 | continue; |
| 250 | |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 251 | Formats.push_back(Format); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | const CommaSeparatedList::ColumnFormat * |
| 256 | CommaSeparatedList::getColumnFormat(unsigned RemainingCharacters) const { |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 257 | const ColumnFormat *BestFormat = nullptr; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 258 | for (SmallVector<ColumnFormat, 4>::const_reverse_iterator |
| 259 | I = Formats.rbegin(), |
| 260 | E = Formats.rend(); |
| 261 | I != E; ++I) { |
| 262 | if (I->TotalWidth <= RemainingCharacters) { |
| 263 | if (BestFormat && I->LineCount > BestFormat->LineCount) |
| 264 | break; |
| 265 | BestFormat = &*I; |
| 266 | } |
| 267 | } |
| 268 | return BestFormat; |
| 269 | } |
| 270 | |
| 271 | } // namespace format |
| 272 | } // namespace clang |