comparison clang/lib/Format/WhitespaceManager.h @ 223:5f17cb93ff66 llvm-original

LLVM13 (2021/7/18)
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:43:00 +0900
parents 0572611fdcc8
children c4bab56944e8
comparison
equal deleted inserted replaced
222:81f6424ef0e3 223:5f17cb93ff66
16 #define LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H 16 #define LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H
17 17
18 #include "TokenAnnotator.h" 18 #include "TokenAnnotator.h"
19 #include "clang/Basic/SourceManager.h" 19 #include "clang/Basic/SourceManager.h"
20 #include "clang/Format/Format.h" 20 #include "clang/Format/Format.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include <algorithm>
21 #include <string> 23 #include <string>
22 #include <tuple> 24 #include <tuple>
23 25
24 namespace clang { 26 namespace clang {
25 namespace format { 27 namespace format {
171 ConditionalsLevel); 173 ConditionalsLevel);
172 } 174 }
173 }; 175 };
174 176
175 private: 177 private:
178 struct CellDescription {
179 unsigned Index = 0;
180 unsigned Cell = 0;
181 unsigned EndIndex = 0;
182 bool HasSplit = false;
183 CellDescription *NextColumnElement = nullptr;
184
185 constexpr bool operator==(const CellDescription &Other) const {
186 return Index == Other.Index && Cell == Other.Cell &&
187 EndIndex == Other.EndIndex;
188 }
189 constexpr bool operator!=(const CellDescription &Other) const {
190 return !(*this == Other);
191 }
192 };
193
194 struct CellDescriptions {
195 SmallVector<CellDescription> Cells;
196 unsigned CellCount = 0;
197 unsigned InitialSpaces = 0;
198 };
199
176 /// Calculate \c IsTrailingComment, \c TokenLength for the last tokens 200 /// Calculate \c IsTrailingComment, \c TokenLength for the last tokens
177 /// or token parts in a line and \c PreviousEndOfTokenColumn and 201 /// or token parts in a line and \c PreviousEndOfTokenColumn and
178 /// \c EscapedNewlineColumn for the first tokens or token parts in a line. 202 /// \c EscapedNewlineColumn for the first tokens or token parts in a line.
179 void calculateLineBreakInformation(); 203 void calculateLineBreakInformation();
180 204
204 void alignEscapedNewlines(); 228 void alignEscapedNewlines();
205 229
206 /// Align escaped newlines from change \p Start to change \p End at 230 /// Align escaped newlines from change \p Start to change \p End at
207 /// the specified \p Column. 231 /// the specified \p Column.
208 void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column); 232 void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column);
233
234 /// Align Array Initializers over all \c Changes.
235 void alignArrayInitializers();
236
237 /// Align Array Initializers from change \p Start to change \p End at
238 /// the specified \p Column.
239 void alignArrayInitializers(unsigned Start, unsigned End);
240
241 /// Align Array Initializers being careful to right justify the columns
242 /// as described by \p CellDescs.
243 void alignArrayInitializersRightJustified(CellDescriptions &&CellDescs);
244
245 /// Align Array Initializers being careful to leftt justify the columns
246 /// as described by \p CellDescs.
247 void alignArrayInitializersLeftJustified(CellDescriptions &&CellDescs);
248
249 /// Calculate the cell width between two indexes.
250 unsigned calculateCellWidth(unsigned Start, unsigned End,
251 bool WithSpaces = false) const;
252
253 /// Get a set of fully specified CellDescriptions between \p Start and
254 /// \p End of the change list.
255 CellDescriptions getCells(unsigned Start, unsigned End);
256
257 /// Does this \p Cell contain a split element?
258 static bool isSplitCell(const CellDescription &Cell);
259
260 /// Get the width of the preceeding cells from \p Start to \p End.
261 template <typename I>
262 auto getNetWidth(const I &Start, const I &End, unsigned InitialSpaces) const {
263 auto NetWidth = InitialSpaces;
264 for (auto PrevIter = Start; PrevIter != End; ++PrevIter) {
265 // If we broke the line the initial spaces are already
266 // accounted for.
267 if (Changes[PrevIter->Index].NewlinesBefore > 0)
268 NetWidth = 0;
269 NetWidth +=
270 calculateCellWidth(PrevIter->Index, PrevIter->EndIndex, true) + 1;
271 }
272 return NetWidth;
273 }
274
275 /// Get the maximum width of a cell in a sequence of columns.
276 template <typename I>
277 unsigned getMaximumCellWidth(I CellIter, unsigned NetWidth) const {
278 unsigned CellWidth =
279 calculateCellWidth(CellIter->Index, CellIter->EndIndex, true);
280 if (Changes[CellIter->Index].NewlinesBefore == 0)
281 CellWidth += NetWidth;
282 for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
283 Next = Next->NextColumnElement) {
284 auto ThisWidth = calculateCellWidth(Next->Index, Next->EndIndex, true);
285 if (Changes[Next->Index].NewlinesBefore == 0)
286 ThisWidth += NetWidth;
287 CellWidth = std::max(CellWidth, ThisWidth);
288 }
289 return CellWidth;
290 }
291
292 /// Get The maximum width of all columns to a given cell.
293 template <typename I>
294 unsigned getMaximumNetWidth(const I &CellStart, const I &CellStop,
295 unsigned InitialSpaces,
296 unsigned CellCount) const {
297 auto MaxNetWidth = getNetWidth(CellStart, CellStop, InitialSpaces);
298 auto RowCount = 1U;
299 auto Offset = std::distance(CellStart, CellStop);
300 for (const auto *Next = CellStop->NextColumnElement; Next != nullptr;
301 Next = Next->NextColumnElement) {
302 auto Start = (CellStart + RowCount * CellCount);
303 auto End = Start + Offset;
304 MaxNetWidth =
305 std::max(MaxNetWidth, getNetWidth(Start, End, InitialSpaces));
306 ++RowCount;
307 }
308 return MaxNetWidth;
309 }
310
311 /// Align a split cell with a newline to the first element in the cell.
312 void alignToStartOfCell(unsigned Start, unsigned End);
313
314 /// Link the Cell pointers in the list of Cells.
315 static CellDescriptions linkCells(CellDescriptions &&CellDesc);
209 316
210 /// Fill \c Replaces with the replacements for all effective changes. 317 /// Fill \c Replaces with the replacements for all effective changes.
211 void generateChanges(); 318 void generateChanges();
212 319
213 /// Stores \p Text as the replacement for the whitespace in \p Range. 320 /// Stores \p Text as the replacement for the whitespace in \p Range.