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