39 : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {}
58 unsigned StartOfTokenColumn,
60 bool InPPDirective =
false,
61 unsigned IndentedFromColumn = 0);
86 unsigned ReplaceChars,
87 StringRef PreviousPostfix,
88 StringRef CurrentPrefix,
bool InPPDirective,
184 return std::make_tuple(
Tok->IndentLevel,
Tok->NestingLevel,
190 struct CellDescription {
193 unsigned EndIndex = 0;
194 bool HasSplit =
false;
195 CellDescription *NextColumnElement =
nullptr;
198 return Index ==
Other.Index && Cell ==
Other.Cell &&
199 EndIndex ==
Other.EndIndex;
202 return !(*
this ==
Other);
206 struct CellDescriptions {
207 SmallVector<CellDescription> Cells;
208 SmallVector<unsigned> CellCounts;
209 unsigned InitialSpaces = 0;
213 bool isRectangular()
const {
214 if (CellCounts.size() < 2)
217 for (
auto NumberOfColumns : CellCounts)
218 if (NumberOfColumns != CellCounts[0])
227 void calculateLineBreakInformation();
230 void alignConsecutiveMacros();
233 void alignConsecutiveAssignments();
236 void alignConsecutiveBitFields();
240 alignConsecutiveColons(
const FormatStyle::AlignConsecutiveStyle &AlignStyle,
244 void alignConsecutiveDeclarations();
247 void alignChainedConditionals();
250 void alignConsecutiveShortCaseStatements(
bool IsExpr);
253 void alignConsecutiveTableGenBreakingDAGArgColons();
256 void alignConsecutiveTableGenCondOperatorColons();
259 void alignConsecutiveTableGenDefinitions();
262 void alignTrailingComments();
266 void alignTrailingComments(
unsigned Start,
unsigned End,
unsigned Column);
269 void alignEscapedNewlines();
273 void alignEscapedNewlines(
unsigned Start,
unsigned End,
unsigned Column);
276 void alignArrayInitializers();
280 void alignArrayInitializers(
unsigned Start,
unsigned End);
284 void alignArrayInitializersRightJustified(CellDescriptions &&CellDescs);
288 void alignArrayInitializersLeftJustified(CellDescriptions &&CellDescs);
291 unsigned calculateCellWidth(
unsigned Start,
unsigned End,
292 bool WithSpaces =
false)
const;
296 CellDescriptions getCells(
unsigned Start,
unsigned End);
299 static bool isSplitCell(
const CellDescription &Cell);
302 template <
typename I>
303 auto getNetWidth(
const I &Start,
const I &End,
unsigned InitialSpaces)
const {
304 auto NetWidth = InitialSpaces;
305 for (
auto PrevIter = Start; PrevIter != End; ++PrevIter) {
308 assert(PrevIter->Index < Changes.size());
309 if (Changes[PrevIter->Index].NewlinesBefore > 0)
312 calculateCellWidth(PrevIter->Index, PrevIter->EndIndex,
true) + 1;
318 template <
typename I>
319 unsigned getMaximumCellWidth(I CellIter,
unsigned NetWidth)
const {
321 calculateCellWidth(CellIter->Index, CellIter->EndIndex,
true);
322 if (Changes[CellIter->Index].NewlinesBefore == 0)
323 CellWidth += NetWidth;
324 for (
const auto *
Next = CellIter->NextColumnElement;
Next;
326 auto ThisWidth = calculateCellWidth(
Next->Index,
Next->EndIndex,
true);
327 if (Changes[
Next->Index].NewlinesBefore == 0)
328 ThisWidth += NetWidth;
329 CellWidth = std::max(CellWidth, ThisWidth);
335 template <
typename I>
336 unsigned getMaximumNetWidth(
const I &CellStart,
const I &CellStop,
337 unsigned InitialSpaces,
unsigned CellCount,
338 unsigned MaxRowCount)
const {
339 auto MaxNetWidth = getNetWidth(CellStart, CellStop, InitialSpaces);
341 auto Offset = std::distance(CellStart, CellStop);
342 for (
const auto *
Next = CellStop->NextColumnElement;
Next;
344 if (RowCount >= MaxRowCount)
346 auto Start = (CellStart + RowCount * CellCount);
347 auto End = Start + Offset;
349 std::max(MaxNetWidth, getNetWidth(Start, End, InitialSpaces));
356 void alignToStartOfCell(
unsigned Start,
unsigned End);
359 static CellDescriptions linkCells(CellDescriptions &&CellDesc);
361 void setChangeSpaces(
unsigned Start,
unsigned Spaces);
364 void generateChanges();
367 void storeReplacement(SourceRange Range, StringRef
Text);
368 void appendNewlineText(std::string &
Text,
const Change &
C);
369 void appendEscapedNewlineText(std::string &
Text,
unsigned Newlines,
370 unsigned PreviousEndOfTokenColumn,
371 unsigned EscapedNewlineColumn);
373 unsigned Spaces,
unsigned WhitespaceStartColumn,
375 unsigned appendTabIndent(std::string &
Text,
unsigned Spaces,
376 unsigned Indentation);
378 SmallVector<Change, 16> Changes;
379 const SourceManager &SourceMgr;
380 tooling::Replacements Replaces;
381 const FormatStyle &Style;