39 : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {}
58 unsigned StartOfTokenColumn,
bool IsAligned =
false,
59 bool InPPDirective =
false,
60 unsigned IndentedFromColumn = 0);
85 unsigned ReplaceChars,
86 StringRef PreviousPostfix,
87 StringRef CurrentPrefix,
bool InPPDirective,
183 return std::make_tuple(
Tok->IndentLevel,
Tok->NestingLevel,
189 struct CellDescription {
192 unsigned EndIndex = 0;
193 bool HasSplit =
false;
194 CellDescription *NextColumnElement =
nullptr;
197 return Index ==
Other.Index && Cell ==
Other.Cell &&
198 EndIndex ==
Other.EndIndex;
201 return !(*
this ==
Other);
205 struct CellDescriptions {
206 SmallVector<CellDescription> Cells;
207 SmallVector<unsigned> CellCounts;
208 unsigned InitialSpaces = 0;
212 bool isRectangular()
const {
213 if (CellCounts.size() < 2)
216 for (
auto NumberOfColumns : CellCounts)
217 if (NumberOfColumns != CellCounts[0])
226 void calculateLineBreakInformation();
229 void alignConsecutiveMacros();
232 void alignConsecutiveAssignments();
235 void alignConsecutiveBitFields();
239 alignConsecutiveColons(
const FormatStyle::AlignConsecutiveStyle &AlignStyle,
243 void alignConsecutiveDeclarations();
246 void alignChainedConditionals();
249 void alignConsecutiveShortCaseStatements(
bool IsExpr);
252 void alignConsecutiveTableGenBreakingDAGArgColons();
255 void alignConsecutiveTableGenCondOperatorColons();
258 void alignConsecutiveTableGenDefinitions();
261 void alignTrailingComments();
265 void alignTrailingComments(
unsigned Start,
unsigned End,
unsigned Column);
268 void alignEscapedNewlines();
272 void alignEscapedNewlines(
unsigned Start,
unsigned End,
unsigned Column);
275 void alignArrayInitializers();
279 void alignArrayInitializers(
unsigned Start,
unsigned End);
283 void alignArrayInitializersRightJustified(CellDescriptions &&CellDescs);
287 void alignArrayInitializersLeftJustified(CellDescriptions &&CellDescs);
290 unsigned calculateCellWidth(
unsigned Start,
unsigned End,
291 bool WithSpaces =
false)
const;
295 CellDescriptions getCells(
unsigned Start,
unsigned End);
298 static bool isSplitCell(
const CellDescription &Cell);
301 template <
typename I>
302 auto getNetWidth(
const I &Start,
const I &End,
unsigned InitialSpaces)
const {
303 auto NetWidth = InitialSpaces;
304 for (
auto PrevIter = Start; PrevIter != End; ++PrevIter) {
307 assert(PrevIter->Index < Changes.size());
308 if (Changes[PrevIter->Index].NewlinesBefore > 0)
311 calculateCellWidth(PrevIter->Index, PrevIter->EndIndex,
true) + 1;
317 template <
typename I>
318 unsigned getMaximumCellWidth(I CellIter,
unsigned NetWidth)
const {
320 calculateCellWidth(CellIter->Index, CellIter->EndIndex,
true);
321 if (Changes[CellIter->Index].NewlinesBefore == 0)
322 CellWidth += NetWidth;
323 for (
const auto *
Next = CellIter->NextColumnElement;
Next;
325 auto ThisWidth = calculateCellWidth(
Next->Index,
Next->EndIndex,
true);
326 if (Changes[
Next->Index].NewlinesBefore == 0)
327 ThisWidth += NetWidth;
328 CellWidth = std::max(CellWidth, ThisWidth);
334 template <
typename I>
335 unsigned getMaximumNetWidth(
const I &CellStart,
const I &CellStop,
336 unsigned InitialSpaces,
unsigned CellCount,
337 unsigned MaxRowCount)
const {
338 auto MaxNetWidth = getNetWidth(CellStart, CellStop, InitialSpaces);
340 auto Offset = std::distance(CellStart, CellStop);
341 for (
const auto *
Next = CellStop->NextColumnElement;
Next;
343 if (RowCount >= MaxRowCount)
345 auto Start = (CellStart + RowCount * CellCount);
346 auto End = Start + Offset;
348 std::max(MaxNetWidth, getNetWidth(Start, End, InitialSpaces));
355 void alignToStartOfCell(
unsigned Start,
unsigned End);
358 static CellDescriptions linkCells(CellDescriptions &&CellDesc);
361 void generateChanges();
364 void storeReplacement(SourceRange Range, StringRef
Text);
365 void appendNewlineText(std::string &
Text,
const Change &
C);
366 void appendEscapedNewlineText(std::string &
Text,
unsigned Newlines,
367 unsigned PreviousEndOfTokenColumn,
368 unsigned EscapedNewlineColumn);
370 unsigned Spaces,
unsigned WhitespaceStartColumn,
372 unsigned appendTabIndent(std::string &
Text,
unsigned Spaces,
373 unsigned Indentation);
375 SmallVector<Change, 16> Changes;
376 const SourceManager &SourceMgr;
377 tooling::Replacements Replaces;
378 const FormatStyle &Style;