clang  6.0.0svn
Public Member Functions | Static Public Member Functions | List of all members
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. More...
 
 AtomicChange (llvm::StringRef FilePath, llvm::StringRef Key)
 Creates an atomic change for FilePath with a customized key. More...
 
 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. More...
 
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. More...
 
const std::string & getFilePath () const
 Returns the path of the file containing this atomic change. More...
 
void setError (llvm::StringRef Error)
 If this change could not be created successfully, e.g. More...
 
bool hasError () const
 Returns whether an error has been set on this list. More...
 
const std::string & getError () const
 Returns the error message or an empty string if it does not exist. More...
 
llvm::Error replace (const SourceManager &SM, const CharSourceRange &Range, llvm::StringRef ReplacementText)
 Adds a replacement that replaces the given Range with ReplacementText. More...
 
llvm::Error replace (const SourceManager &SM, SourceLocation Loc, unsigned Length, llvm::StringRef Text)
 Adds a replacement that replaces range [Loc, Loc+Length) with Text. More...
 
llvm::Error insert (const SourceManager &SM, SourceLocation Loc, llvm::StringRef Text, bool InsertAfter=true)
 Adds a replacement that inserts Text at Loc. More...
 
void addHeader (llvm::StringRef Header)
 Adds a header into the file that contains the key position. More...
 
void removeHeader (llvm::StringRef Header)
 Removes a header from the file that contains the key position. More...
 
const ReplacementsgetReplacements () const
 Returns a const reference to existing replacements. More...
 
llvm::ArrayRef< std::string > getInsertedHeaders () const
 
llvm::ArrayRef< std::string > getRemovedHeaders () const
 

Static Public Member Functions

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

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/4]

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 197 of file AtomicChange.cpp.

References clang::FullSourceLoc::getDecomposedLoc(), clang::SourceManager::getFileEntryForID(), and clang::FullSourceLoc::getSpellingLoc().

◆ AtomicChange() [2/4]

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

Creates an atomic change for FilePath with a customized key.

Definition at line 46 of file AtomicChange.h.

References convertFromYAML(), operator=(), operator==(), and toYAMLString().

◆ AtomicChange() [3/4]

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

◆ AtomicChange() [4/4]

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

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 287 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 237 of file AtomicChange.cpp.

Referenced by 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 80 of file AtomicChange.h.

References clang::Error, and replace().

◆ getFilePath()

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

Returns the path of the file containing this atomic change.

Definition at line 68 of file AtomicChange.h.

◆ getInsertedHeaders()

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

Definition at line 116 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 65 of file AtomicChange.h.

◆ getRemovedHeaders()

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

Definition at line 120 of file AtomicChange.h.

◆ getReplacements()

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

Returns a const reference to existing replacements.

Definition at line 114 of file AtomicChange.h.

Referenced by clang::tooling::convertChangesToFileReplacements().

◆ hasError()

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

Returns whether an error has been set on this list.

Definition at line 77 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 264 of file AtomicChange.cpp.

References clang::tooling::Replacements::add(), clang::tooling::ReplacementError::get(), clang::tooling::ReplacementError::getExistingReplacement(), clang::tooling::Replacement::getFilePath(), clang::tooling::Replacement::getOffset(), clang::tooling::Replacements::getShiftedCodePosition(), clang::tooling::Replacements::merge(), and Text.

◆ operator=() [1/2]

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

Referenced by AtomicChange().

◆ operator=() [2/2]

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

◆ operator==()

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

Definition at line 218 of file AtomicChange.cpp.

Referenced by AtomicChange().

◆ removeHeader()

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

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

Definition at line 291 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 253 of file AtomicChange.cpp.

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

Referenced by getError().

◆ 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 259 of file AtomicChange.cpp.

◆ 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 74 of file AtomicChange.h.

◆ toYAMLString()

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

Returns the atomic change as a YAML string.

Definition at line 227 of file AtomicChange.cpp.

Referenced by AtomicChange().


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