clang 22.0.0git
clang::format::WhitespaceManager Class Reference

Manages the whitespaces around tokens and their replacements. More...

#include "/home/buildbot/as-worker-4/publish-doxygen-docs/llvm-project/clang/lib/Format/WhitespaceManager.h"

Classes

struct  Change
 Represents a change before a token, a break inside a token, or the layout of an unchanged token (or whitespace within). More...

Public Member Functions

 WhitespaceManager (const SourceManager &SourceMgr, const FormatStyle &Style, bool UseCRLF)
bool useCRLF () const
void replaceWhitespace (FormatToken &Tok, unsigned Newlines, unsigned Spaces, unsigned StartOfTokenColumn, bool IsAligned=false, bool InPPDirective=false)
 Replaces the whitespace in front of Tok.
void addUntouchableToken (const FormatToken &Tok, bool InPPDirective)
 Adds information about an unchangeable token's whitespace.
llvm::Error addReplacement (const tooling::Replacement &Replacement)
void replaceWhitespaceInToken (const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars, StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective, unsigned Newlines, int Spaces)
 Inserts or replaces whitespace in the middle of a token.
const tooling::ReplacementsgenerateReplacements ()
 Returns all the Replacements created during formatting.

Static Public Member Functions

static bool inputUsesCRLF (StringRef Text, bool DefaultToCRLF)
 Infers whether the input is using CRLF.

Detailed Description

Manages the whitespaces around tokens and their replacements.

This includes special handling for certain constructs, e.g. the alignment of trailing line comments.

To guarantee correctness of alignment operations, the WhitespaceManager must be informed about every token in the source file; for each token, there must be exactly one call to either replaceWhitespace or addUntouchableToken.

There may be multiple calls to breakToken for a given token.

Definition at line 35 of file WhitespaceManager.h.

Constructor & Destructor Documentation

◆ WhitespaceManager()

clang::format::WhitespaceManager::WhitespaceManager ( const SourceManager & SourceMgr,
const FormatStyle & Style,
bool UseCRLF )
inline

Definition at line 37 of file WhitespaceManager.h.

Member Function Documentation

◆ addReplacement()

llvm::Error clang::format::WhitespaceManager::addReplacement ( const tooling::Replacement & Replacement)

Definition at line 78 of file WhitespaceManager.cpp.

◆ addUntouchableToken()

void clang::format::WhitespaceManager::addUntouchableToken ( const FormatToken & Tok,
bool InPPDirective )

Adds information about an unchangeable token's whitespace.

Needs to be called for every token for which replaceWhitespace was not called.

Definition at line 66 of file WhitespaceManager.cpp.

References clang::format::MR_ExpandedArg, and Tok.

◆ generateReplacements()

const tooling::Replacements & clang::format::WhitespaceManager::generateReplacements ( )

Returns all the Replacements created during formatting.

Definition at line 103 of file WhitespaceManager.cpp.

◆ inputUsesCRLF()

bool clang::format::WhitespaceManager::inputUsesCRLF ( StringRef Text,
bool DefaultToCRLF )
static

Infers whether the input is using CRLF.

Definition at line 82 of file WhitespaceManager.cpp.

References clang::Text.

◆ replaceWhitespace()

void clang::format::WhitespaceManager::replaceWhitespace ( FormatToken & Tok,
unsigned Newlines,
unsigned Spaces,
unsigned StartOfTokenColumn,
bool IsAligned = false,
bool InPPDirective = false )

Replaces the whitespace in front of Tok.

Only call once for each AnnotatedToken.

StartOfTokenColumn is the column at which the token will start after this replacement. It is needed for determining how Spaces is turned into tabs and spaces for some format styles.

Definition at line 53 of file WhitespaceManager.cpp.

References clang::format::FD_Break, clang::format::FD_Continue, clang::format::MR_ExpandedArg, Newlines, and Tok.

Referenced by clang::format::BreakableLineCommentSection::adaptStartOfLine(), and clang::format::BreakableLineCommentSection::reflow().

◆ replaceWhitespaceInToken()

void clang::format::WhitespaceManager::replaceWhitespaceInToken ( const FormatToken & Tok,
unsigned Offset,
unsigned ReplaceChars,
StringRef PreviousPostfix,
StringRef CurrentPrefix,
bool InPPDirective,
unsigned Newlines,
int Spaces )

Inserts or replaces whitespace in the middle of a token.

Inserts PreviousPostfix, Newlines, Spaces and CurrentPrefix (in this order) at Offset inside Tok, replacing ReplaceChars characters.

Note: Spaces can be negative to retain information about initial relative column offset between a line of a block comment and the start of the comment. This negative offset may be compensated by trailing comment alignment here. In all other cases negative Spaces will be truncated to 0.

When InPPDirective is true, escaped newlines are inserted. Spaces is used to align backslashes correctly.

Definition at line 88 of file WhitespaceManager.cpp.

References clang::SourceLocation::getLocWithOffset(), clang::format::MR_ExpandedArg, Newlines, and Tok.

Referenced by clang::format::BreakableBlockComment::adaptStartOfLine(), clang::format::BreakableLineCommentSection::adaptStartOfLine(), clang::format::BreakableComment::compressWhitespace(), clang::format::BreakableBlockComment::insertBreak(), clang::format::BreakableLineCommentSection::insertBreak(), clang::format::BreakableStringLiteral::insertBreak(), clang::format::BreakableStringLiteralUsingOperators::insertBreak(), clang::format::BreakableBlockComment::reflow(), clang::format::BreakableLineCommentSection::reflow(), and clang::format::BreakableStringLiteralUsingOperators::updateAfterBroken().

◆ useCRLF()

bool clang::format::WhitespaceManager::useCRLF ( ) const
inline

Definition at line 41 of file WhitespaceManager.h.


The documentation for this class was generated from the following files: