FormatToken.cpp revision 451f1e0e5040db6a114217e7bd9767bf751d5bd3
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
25TokenRole::~TokenRole() {}
26
27void TokenRole::precomputeFormattingInfos(const FormatToken *Token) {}
28
29unsigned CommaSeparatedList::format(LineState &State,
30                                    ContinuationIndenter *Indenter,
31                                    bool DryRun) {
32  if (!State.NextToken->Previous || !State.NextToken->Previous->Previous ||
33      Commas.size() <= 2)
34    return 0;
35
36  // Ensure that we start on the opening brace.
37  const FormatToken *LBrace = State.NextToken->Previous->Previous;
38  if (LBrace->isNot(tok::l_brace) ||
39      LBrace->Next->Type == TT_DesignatedInitializerPeriod)
40    return 0;
41
42  // Calculate the number of code points we have to format this list. As the
43  // first token is already placed, we have to subtract it.
44  unsigned RemainingCodePoints = Style.ColumnLimit - State.Column +
45                                 State.NextToken->Previous->CodePointCount;
46
47  // Find the best ColumnFormat, i.e. the best number of columns to use.
48  const ColumnFormat *Format = getColumnFormat(RemainingCodePoints);
49  if (!Format)
50    return 0;
51
52  // Format the entire list.
53  unsigned Penalty = 0;
54  unsigned Column = 0;
55  unsigned Item = 0;
56  while (State.NextToken != LBrace->MatchingParen) {
57    bool NewLine = false;
58    unsigned ExtraSpaces = 0;
59
60    // If the previous token was one of our commas, we are now on the next item.
61    if (Item < Commas.size() && State.NextToken->Previous == Commas[Item]) {
62      if (!State.NextToken->isTrailingComment()) {
63        ExtraSpaces += Format->ColumnSizes[Column] - ItemLengths[Item];
64        ++Column;
65      }
66      ++Item;
67    }
68
69    if (Column == Format->Columns || State.NextToken->MustBreakBefore) {
70      Column = 0;
71      NewLine = true;
72    }
73
74    // Place token using the continuation indenter and store the penalty.
75    Penalty += Indenter->addTokenToState(State, NewLine, DryRun, ExtraSpaces);
76  }
77  return Penalty;
78}
79
80// Returns the lengths in code points between Begin and End (both included),
81// assuming that the entire sequence is put on a single line.
82static unsigned CodePointsBetween(const FormatToken *Begin,
83                                  const FormatToken *End) {
84  assert(End->TotalLength >= Begin->TotalLength);
85  return End->TotalLength - Begin->TotalLength + Begin->CodePointCount;
86}
87
88void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
89  // FIXME: At some point we might want to do this for other lists, too.
90  if (!Token->MatchingParen || Token->isNot(tok::l_brace))
91    return;
92
93  FormatToken *ItemBegin = Token->Next;
94  SmallVector<bool, 8> MustBreakBeforeItem;
95
96  // The lengths of an item if it is put at the end of the line. This includes
97  // trailing comments which are otherwise ignored for column alignment.
98  SmallVector<unsigned, 8> EndOfLineItemLength;
99
100  for (unsigned i = 0, e = Commas.size() + 1; i != e; ++i) {
101    // Skip comments on their own line.
102    while (ItemBegin->HasUnescapedNewline && ItemBegin->isTrailingComment())
103      ItemBegin = ItemBegin->Next;
104
105    MustBreakBeforeItem.push_back(ItemBegin->MustBreakBefore);
106    const FormatToken *ItemEnd = NULL;
107    if (i == Commas.size()) {
108      ItemEnd = Token->MatchingParen;
109      const FormatToken *NonCommentEnd = ItemEnd->getPreviousNonComment();
110      ItemLengths.push_back(CodePointsBetween(ItemBegin, NonCommentEnd));
111      if (Style.Cpp11BracedListStyle) {
112        // In Cpp11 braced list style, the } and possibly other subsequent
113        // tokens will need to stay on a line with the last element.
114        while (ItemEnd->Next && !ItemEnd->Next->CanBreakBefore)
115          ItemEnd = ItemEnd->Next;
116      } else {
117        // In other braced lists styles, the "}" can be wrapped to the new line.
118        ItemEnd = Token->MatchingParen->Previous;
119      }
120    } else {
121      ItemEnd = Commas[i];
122      // The comma is counted as part of the item when calculating the length.
123      ItemLengths.push_back(CodePointsBetween(ItemBegin, ItemEnd));
124      // Consume trailing comments so the are included in EndOfLineItemLength.
125      if (ItemEnd->Next && !ItemEnd->Next->HasUnescapedNewline &&
126          ItemEnd->Next->isTrailingComment())
127        ItemEnd = ItemEnd->Next;
128    }
129    EndOfLineItemLength.push_back(CodePointsBetween(ItemBegin, ItemEnd));
130    // If there is a trailing comma in the list, the next item will start at the
131    // closing brace. Don't create an extra item for this.
132    if (ItemEnd->getNextNonComment() == Token->MatchingParen)
133      break;
134    ItemBegin = ItemEnd->Next;
135  }
136
137  // We can never place more than ColumnLimit / 3 items in a row (because of the
138  // spaces and the comma).
139  for (unsigned Columns = 1; Columns <= Style.ColumnLimit / 3; ++Columns) {
140    ColumnFormat Format;
141    Format.Columns = Columns;
142    Format.ColumnSizes.resize(Columns);
143    Format.LineCount = 0;
144    bool HasRowWithSufficientColumns = false;
145    unsigned Column = 0;
146    for (unsigned i = 0, e = ItemLengths.size(); i != e; ++i) {
147      if (MustBreakBeforeItem[i] || Column == Columns) {
148        ++Format.LineCount;
149        Column = 0;
150      }
151      if (Column == Columns - 1)
152        HasRowWithSufficientColumns = true;
153      unsigned length =
154          (Column == Columns - 1) ? EndOfLineItemLength[i] : ItemLengths[i];
155      Format.ColumnSizes[Column] =
156          std::max(Format.ColumnSizes[Column], length);
157      ++Column;
158    }
159    // If all rows are terminated early (e.g. by trailing comments), we don't
160    // need to look further.
161    if (!HasRowWithSufficientColumns)
162      break;
163    Format.TotalWidth = Columns - 1; // Width of the N-1 spaces.
164    for (unsigned i = 0; i < Columns; ++i) {
165      Format.TotalWidth += Format.ColumnSizes[i];
166    }
167
168    // Ignore layouts that are bound to violate the column limit.
169    if (Format.TotalWidth > Style.ColumnLimit)
170      continue;
171
172    Formats.push_back(Format);
173  }
174}
175
176const CommaSeparatedList::ColumnFormat *
177CommaSeparatedList::getColumnFormat(unsigned RemainingCharacters) const {
178  const ColumnFormat *BestFormat = NULL;
179  for (SmallVector<ColumnFormat, 4>::const_reverse_iterator
180           I = Formats.rbegin(),
181           E = Formats.rend();
182       I != E; ++I) {
183    if (I->TotalWidth <= RemainingCharacters) {
184      if (BestFormat && I->LineCount > BestFormat->LineCount)
185        break;
186      BestFormat = &*I;
187    }
188  }
189  return BestFormat;
190}
191
192} // namespace format
193} // namespace clang
194