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