blob: 49e0ce83375b2ee53b30fd8c778f958df6a7da25 [file] [log] [blame]
Daniel Jasperd4a03db2013-08-22 15:00:41 +00001//===--- 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
Daniel Jasper451f1e02013-08-27 08:43:47 +000042 // 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 +
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +000045 State.NextToken->Previous->ColumnWidth;
Daniel Jasper451f1e02013-08-27 08:43:47 +000046
Daniel Jasperd4a03db2013-08-22 15:00:41 +000047 // Find the best ColumnFormat, i.e. the best number of columns to use.
Daniel Jasper451f1e02013-08-27 08:43:47 +000048 const ColumnFormat *Format = getColumnFormat(RemainingCodePoints);
Daniel Jasperd4a03db2013-08-22 15:00:41 +000049 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) {
Daniel Jasper332c6772013-08-26 08:10:17 +000084 assert(End->TotalLength >= Begin->TotalLength);
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +000085 return End->TotalLength - Begin->TotalLength + Begin->ColumnWidth;
Daniel Jasperd4a03db2013-08-22 15:00:41 +000086}
87
88void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
Daniel Jasper332c6772013-08-26 08:10:17 +000089 // FIXME: At some point we might want to do this for other lists, too.
90 if (!Token->MatchingParen || Token->isNot(tok::l_brace))
Daniel Jasperd4a03db2013-08-22 15:00:41 +000091 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) {
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000101 // 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.
Daniel Jasper332c6772013-08-26 08:10:17 +0000123 ItemLengths.push_back(CodePointsBetween(ItemBegin, ItemEnd));
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000124 // 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));
Daniel Jasper332c6772013-08-26 08:10:17 +0000130 // 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;
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000134 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