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.empty())
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 alignTrailingComments();
240 void alignTrailingComments(
unsigned Start,
unsigned End,
unsigned Column);
243 void alignEscapedNewlines();
247 void alignEscapedNewlines(
unsigned Start,
unsigned End,
unsigned Column);
250 void alignArrayInitializers();
254 void alignArrayInitializers(
unsigned Start,
unsigned End);
258 void alignArrayInitializersRightJustified(CellDescriptions &&CellDescs);
262 void alignArrayInitializersLeftJustified(CellDescriptions &&CellDescs);
265 unsigned calculateCellWidth(
unsigned Start,
unsigned End,
266 bool WithSpaces =
false)
const;
270 CellDescriptions getCells(
unsigned Start,
unsigned End);
273 static bool isSplitCell(
const CellDescription &Cell);
276 template <
typename I>
277 auto getNetWidth(
const I &Start,
const I &End,
unsigned InitialSpaces)
const {
278 auto NetWidth = InitialSpaces;
279 for (
auto PrevIter = Start; PrevIter != End; ++PrevIter) {
282 if (Changes[PrevIter->Index].NewlinesBefore > 0)
285 calculateCellWidth(PrevIter->Index, PrevIter->EndIndex,
true) + 1;
291 template <
typename I>
292 unsigned getMaximumCellWidth(I CellIter,
unsigned NetWidth)
const {
294 calculateCellWidth(CellIter->Index, CellIter->EndIndex,
true);
295 if (Changes[CellIter->Index].NewlinesBefore == 0)
296 CellWidth += NetWidth;
297 for (
const auto *Next = CellIter->NextColumnElement; Next;
298 Next = Next->NextColumnElement) {
299 auto ThisWidth = calculateCellWidth(Next->Index, Next->EndIndex,
true);
300 if (Changes[Next->Index].NewlinesBefore == 0)
301 ThisWidth += NetWidth;
302 CellWidth = std::max(CellWidth, ThisWidth);
308 template <
typename I>
309 unsigned getMaximumNetWidth(
const I &CellStart,
const I &CellStop,
310 unsigned InitialSpaces,
unsigned CellCount,
311 unsigned MaxRowCount)
const {
312 auto MaxNetWidth = getNetWidth(CellStart, CellStop, InitialSpaces);
314 auto Offset = std::distance(CellStart, CellStop);
315 for (
const auto *Next = CellStop->NextColumnElement; Next;
316 Next = Next->NextColumnElement) {
317 if (RowCount > MaxRowCount)
319 auto Start = (CellStart + RowCount * CellCount);
320 auto End = Start +
Offset;
322 std::max(MaxNetWidth, getNetWidth(Start, End, InitialSpaces));
329 void alignToStartOfCell(
unsigned Start,
unsigned End);
332 static CellDescriptions linkCells(CellDescriptions &&CellDesc);
335 void generateChanges();
338 void storeReplacement(SourceRange Range, StringRef
Text);
339 void appendNewlineText(std::string &
Text,
unsigned Newlines);
340 void appendEscapedNewlineText(std::string &
Text,
unsigned Newlines,
341 unsigned PreviousEndOfTokenColumn,
342 unsigned EscapedNewlineColumn);
343 void appendIndentText(std::string &
Text,
unsigned IndentLevel,
344 unsigned Spaces,
unsigned WhitespaceStartColumn,
346 unsigned appendTabIndent(std::string &
Text,
unsigned Spaces,
347 unsigned Indentation);
349 SmallVector<Change, 16> Changes;
350 const SourceManager &SourceMgr;
351 tooling::Replacements Replaces;
352 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)