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