clang 22.0.0git
clang::tooling::AtomicChange Class Reference

An atomic change is used to create and group a set of source edits, e.g. More...

#include "clang/Tooling/Refactoring/AtomicChange.h"

Public Member Functions

 AtomicChange (const SourceManager &SM, SourceLocation KeyPosition)
 Creates an atomic change around KeyPosition with the key being a concatenation of the file name and the offset of KeyPosition.
 AtomicChange (const SourceManager &SM, SourceLocation KeyPosition, llvm::Any Metadata)
 AtomicChange (llvm::StringRef FilePath, llvm::StringRef Key)
 Creates an atomic change for FilePath with a customized key.
 AtomicChange (AtomicChange &&)=default
 AtomicChange (const AtomicChange &)=default
AtomicChangeoperator= (AtomicChange &&)=default
AtomicChangeoperator= (const AtomicChange &)=default
bool operator== (const AtomicChange &Other) const
std::string toYAMLString ()
 Returns the atomic change as a YAML string.
const std::string & getKey () const
 Returns the key of this change, which is a concatenation of the file name and offset of the key position.
const std::string & getFilePath () const
 Returns the path of the file containing this atomic change.
void setError (llvm::StringRef Error)
 If this change could not be created successfully, e.g.
bool hasError () const
 Returns whether an error has been set on this list.
const std::string & getError () const
 Returns the error message or an empty string if it does not exist.
llvm::Error replace (const SourceManager &SM, const CharSourceRange &Range, llvm::StringRef ReplacementText)
 Adds a replacement that replaces the given Range with ReplacementText.
llvm::Error replace (const SourceManager &SM, SourceLocation Loc, unsigned Length, llvm::StringRef Text)
 Adds a replacement that replaces range [Loc, Loc+Length) with Text.
llvm::Error insert (const SourceManager &SM, SourceLocation Loc, llvm::StringRef Text, bool InsertAfter=true)
 Adds a replacement that inserts Text at Loc.
void addHeader (llvm::StringRef Header)
 Adds a header into the file that contains the key position.
void removeHeader (llvm::StringRef Header)
 Removes a header from the file that contains the key position.
const ReplacementsgetReplacements () const
 Returns a const reference to existing replacements.
ReplacementsgetReplacements ()
llvm::ArrayRef< std::string > getInsertedHeaders () const
llvm::ArrayRef< std::string > getRemovedHeaders () const
const llvm::Any & getMetadata () const

Static Public Member Functions

static AtomicChange convertFromYAML (llvm::StringRef YAMLContent)
 Converts a YAML-encoded automic change to AtomicChange.

Detailed Description

An atomic change is used to create and group a set of source edits, e.g.

replacements or header insertions. Edits in an AtomicChange should be related, e.g. replacements for the same type reference and the corresponding header insertion/deletion.

An AtomicChange is uniquely identified by a key and will either be fully applied or not applied at all.

Calling setError on an AtomicChange stores the error message and marks it as bad, i.e. none of its source edits will be applied.

Definition at line 37 of file AtomicChange.h.

Constructor & Destructor Documentation

◆ AtomicChange() [1/5]

clang::tooling::AtomicChange::AtomicChange ( const SourceManager & SM,
SourceLocation KeyPosition )

Creates an atomic change around KeyPosition with the key being a concatenation of the file name and the offset of KeyPosition.

KeyPosition should be the location of the key syntactical element that is being changed, e.g. the call to a refactored method.

Definition at line 196 of file AtomicChange.cpp.

References clang::FullSourceLoc::getDecomposedLoc(), clang::FileEntryRef::getName(), clang::FullSourceLoc::getSpellingLoc(), and SM.

Referenced by AtomicChange(), AtomicChange(), AtomicChange(), convertFromYAML(), operator=(), operator=(), and operator==().

◆ AtomicChange() [2/5]

clang::tooling::AtomicChange::AtomicChange ( const SourceManager & SM,
SourceLocation KeyPosition,
llvm::Any Metadata )

Definition at line 206 of file AtomicChange.cpp.

References AtomicChange(), and SM.

◆ AtomicChange() [3/5]

clang::tooling::AtomicChange::AtomicChange ( llvm::StringRef FilePath,
llvm::StringRef Key )
inline

Creates an atomic change for FilePath with a customized key.

Definition at line 49 of file AtomicChange.h.

◆ AtomicChange() [4/5]

clang::tooling::AtomicChange::AtomicChange ( AtomicChange && )
default

References AtomicChange().

◆ AtomicChange() [5/5]

clang::tooling::AtomicChange::AtomicChange ( const AtomicChange & )
default

References AtomicChange().

Member Function Documentation

◆ addHeader()

void clang::tooling::AtomicChange::addHeader ( llvm::StringRef Header)

Adds a header into the file that contains the key position.

Header can be in angle brackets or double quotation marks. By default (header is not quoted), header will be surrounded with double quotes.

Definition at line 290 of file AtomicChange.cpp.

◆ convertFromYAML()

AtomicChange clang::tooling::AtomicChange::convertFromYAML ( llvm::StringRef YAMLContent)
static

Converts a YAML-encoded automic change to AtomicChange.

Definition at line 240 of file AtomicChange.cpp.

References clang::tooling::Replacements::add(), and AtomicChange().

◆ getError()

const std::string & clang::tooling::AtomicChange::getError ( ) const
inline

Returns the error message or an empty string if it does not exist.

Definition at line 83 of file AtomicChange.h.

◆ getFilePath()

const std::string & clang::tooling::AtomicChange::getFilePath ( ) const
inline

Returns the path of the file containing this atomic change.

Definition at line 71 of file AtomicChange.h.

◆ getInsertedHeaders()

llvm::ArrayRef< std::string > clang::tooling::AtomicChange::getInsertedHeaders ( ) const
inline

Definition at line 121 of file AtomicChange.h.

◆ getKey()

const std::string & clang::tooling::AtomicChange::getKey ( ) const
inline

Returns the key of this change, which is a concatenation of the file name and offset of the key position.

Definition at line 68 of file AtomicChange.h.

◆ getMetadata()

const llvm::Any & clang::tooling::AtomicChange::getMetadata ( ) const
inline

Definition at line 129 of file AtomicChange.h.

◆ getRemovedHeaders()

llvm::ArrayRef< std::string > clang::tooling::AtomicChange::getRemovedHeaders ( ) const
inline

Definition at line 125 of file AtomicChange.h.

◆ getReplacements() [1/2]

Replacements & clang::tooling::AtomicChange::getReplacements ( )
inline

Definition at line 119 of file AtomicChange.h.

◆ getReplacements() [2/2]

const Replacements & clang::tooling::AtomicChange::getReplacements ( ) const
inline

Returns a const reference to existing replacements.

Definition at line 117 of file AtomicChange.h.

Referenced by clang::tooling::RenamingASTConsumer::HandleTranslationUnit().

◆ hasError()

bool clang::tooling::AtomicChange::hasError ( ) const
inline

Returns whether an error has been set on this list.

Definition at line 80 of file AtomicChange.h.

◆ insert()

llvm::Error clang::tooling::AtomicChange::insert ( const SourceManager & SM,
SourceLocation Loc,
llvm::StringRef Text,
bool InsertAfter = true )

Adds a replacement that inserts Text at Loc.

If this insertion conflicts with an existing insertion (at the same position), this will be inserted before/after the existing insertion depending on InsertAfter. Users should use replace with Length=0 instead if they do not want conflict resolving by default. If the conflicting replacement is not an insertion, an error is returned.

Returns
An llvm::Error carrying ReplacementError on error.

Definition at line 267 of file AtomicChange.cpp.

References clang::tooling::ReplacementError::get(), clang::tooling::ReplacementError::getExistingReplacement(), clang::tooling::Replacement::getFilePath(), clang::tooling::Replacement::getOffset(), clang::tooling::insert_conflict, SM, and clang::Text.

◆ operator=() [1/2]

AtomicChange & clang::tooling::AtomicChange::operator= ( AtomicChange && )
default

References AtomicChange().

◆ operator=() [2/2]

AtomicChange & clang::tooling::AtomicChange::operator= ( const AtomicChange & )
default

References AtomicChange(), and clang::Other.

◆ operator==()

bool clang::tooling::AtomicChange::operator== ( const AtomicChange & Other) const

Definition at line 222 of file AtomicChange.cpp.

References AtomicChange(), and clang::Other.

◆ removeHeader()

void clang::tooling::AtomicChange::removeHeader ( llvm::StringRef Header)

Removes a header from the file that contains the key position.

Definition at line 294 of file AtomicChange.cpp.

◆ replace() [1/2]

llvm::Error clang::tooling::AtomicChange::replace ( const SourceManager & SM,
const CharSourceRange & Range,
llvm::StringRef ReplacementText )

Adds a replacement that replaces the given Range with ReplacementText.

Returns
An llvm::Error carrying ReplacementError on error.

Definition at line 256 of file AtomicChange.cpp.

References SM.

Referenced by clang::tooling::convertChangesToFileReplacements(), and clang::tooling::createRenameAtomicChanges().

◆ replace() [2/2]

llvm::Error clang::tooling::AtomicChange::replace ( const SourceManager & SM,
SourceLocation Loc,
unsigned Length,
llvm::StringRef Text )

Adds a replacement that replaces range [Loc, Loc+Length) with Text.

Returns
An llvm::Error carrying ReplacementError on error.

Definition at line 262 of file AtomicChange.cpp.

References SM, and clang::Text.

◆ setError()

void clang::tooling::AtomicChange::setError ( llvm::StringRef Error)
inline

If this change could not be created successfully, e.g.

because of conflicts among replacements, use this to set an error description. Thereby, places that cannot be fixed automatically can be gathered when applying changes.

Definition at line 77 of file AtomicChange.h.

◆ toYAMLString()

std::string clang::tooling::AtomicChange::toYAMLString ( )

Returns the atomic change as a YAML string.

Definition at line 231 of file AtomicChange.cpp.


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