15#ifndef LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H
16#define LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H
21#include "llvm/ADT/SmallVector.h"
44 : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {}
58 unsigned StartOfTokenColumn,
bool isAligned =
false,
59 bool InPPDirective =
false);
84 unsigned ReplaceChars,
85 StringRef PreviousPostfix,
86 StringRef CurrentPrefix,
bool InPPDirective,
87 unsigned Newlines,
int Spaces);
181 struct CellDescription {
184 unsigned EndIndex = 0;
185 bool HasSplit =
false;
186 CellDescription *NextColumnElement =
nullptr;
189 return Index ==
Other.Index && Cell ==
Other.Cell &&
190 EndIndex ==
Other.EndIndex;
192 constexpr bool operator!=(
const CellDescription &
Other)
const {
193 return !(*
this ==
Other);
197 struct CellDescriptions {
198 SmallVector<CellDescription> Cells;
199 SmallVector<unsigned> CellCounts;
200 unsigned InitialSpaces = 0;
204 bool isRectangular()
const {
205 if (CellCounts.size() < 2)
208 for (
auto NumberOfColumns : CellCounts)
209 if (NumberOfColumns != CellCounts[0])
218 void calculateLineBreakInformation();
221 void alignConsecutiveMacros();
224 void alignConsecutiveAssignments();
227 void alignConsecutiveBitFields();
230 void alignConsecutiveDeclarations();
233 void alignChainedConditionals();
236 void alignConsecutiveShortCaseStatements();
239 void alignTrailingComments();
243 void alignTrailingComments(
unsigned Start,
unsigned End,
unsigned Column);
246 void alignEscapedNewlines();
250 void alignEscapedNewlines(
unsigned Start,
unsigned End,
unsigned Column);
253 void alignArrayInitializers();
257 void alignArrayInitializers(
unsigned Start,
unsigned End);
261 void alignArrayInitializersRightJustified(CellDescriptions &&CellDescs);
265 void alignArrayInitializersLeftJustified(CellDescriptions &&CellDescs);
268 unsigned calculateCellWidth(
unsigned Start,
unsigned End,
269 bool WithSpaces =
false)
const;
273 CellDescriptions getCells(
unsigned Start,
unsigned End);
276 static bool isSplitCell(
const CellDescription &Cell);
279 template <
typename I>
280 auto getNetWidth(
const I &Start,
const I &End,
unsigned InitialSpaces)
const {
281 auto NetWidth = InitialSpaces;
282 for (
auto PrevIter = Start; PrevIter != End; ++PrevIter) {
285 if (Changes[PrevIter->Index].NewlinesBefore > 0)
288 calculateCellWidth(PrevIter->Index, PrevIter->EndIndex,
true) + 1;
294 template <
typename I>
295 unsigned getMaximumCellWidth(I CellIter,
unsigned NetWidth)
const {
297 calculateCellWidth(CellIter->Index, CellIter->EndIndex,
true);
298 if (Changes[CellIter->Index].NewlinesBefore == 0)
299 CellWidth += NetWidth;
300 for (
const auto *Next = CellIter->NextColumnElement; Next;
301 Next = Next->NextColumnElement) {
302 auto ThisWidth = calculateCellWidth(Next->Index, Next->EndIndex,
true);
303 if (Changes[Next->Index].NewlinesBefore == 0)
304 ThisWidth += NetWidth;
305 CellWidth = std::max(CellWidth, ThisWidth);
311 template <
typename I>
312 unsigned getMaximumNetWidth(
const I &CellStart,
const I &CellStop,
313 unsigned InitialSpaces,
unsigned CellCount,
314 unsigned MaxRowCount)
const {
315 auto MaxNetWidth = getNetWidth(CellStart, CellStop, InitialSpaces);
317 auto Offset = std::distance(CellStart, CellStop);
318 for (
const auto *Next = CellStop->NextColumnElement; Next;
319 Next = Next->NextColumnElement) {
320 if (RowCount >= MaxRowCount)
322 auto Start = (CellStart + RowCount * CellCount);
323 auto End = Start + Offset;
325 std::max(MaxNetWidth, getNetWidth(Start, End, InitialSpaces));
332 void alignToStartOfCell(
unsigned Start,
unsigned End);
335 static CellDescriptions linkCells(CellDescriptions &&CellDesc);
338 void generateChanges();
341 void storeReplacement(SourceRange Range, StringRef
Text);
342 void appendNewlineText(std::string &
Text,
unsigned Newlines);
343 void appendEscapedNewlineText(std::string &
Text,
unsigned Newlines,
344 unsigned PreviousEndOfTokenColumn,
345 unsigned EscapedNewlineColumn);
346 void appendIndentText(std::string &
Text,
unsigned IndentLevel,
347 unsigned Spaces,
unsigned WhitespaceStartColumn,
349 unsigned appendTabIndent(std::string &
Text,
unsigned Spaces,
350 unsigned Indentation);
352 SmallVector<Change, 16> Changes;
353 const SourceManager &SourceMgr;
354 tooling::Replacements Replaces;
355 const FormatStyle &Style;
Defines the SourceManager interface.
This file implements a token annotator, i.e.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)