clang 19.0.0git
Public Types | Public Member Functions | List of all members
clang::tooling::Replacements Class Reference

Maintains a set of replacements that are conflict-free. More...

#include "clang/Tooling/Core/Replacement.h"

Public Types

using const_iterator = ReplacementsImpl::const_iterator
 
using const_reverse_iterator = ReplacementsImpl::const_reverse_iterator
 

Public Member Functions

 Replacements ()=default
 
 Replacements (const Replacement &R)
 
llvm::Error add (const Replacement &R)
 Adds a new replacement R to the current set of replacements.
 
Replacements merge (const Replacements &Replaces) const
 Merges Replaces into the current replacements.
 
std::vector< RangegetAffectedRanges () const
 
unsigned getShiftedCodePosition (unsigned Position) const
 
unsigned size () const
 
void clear ()
 
bool empty () const
 
const_iterator begin () const
 
const_iterator end () const
 
const_reverse_iterator rbegin () const
 
const_reverse_iterator rend () const
 
bool operator== (const Replacements &RHS) const
 

Detailed Description

Maintains a set of replacements that are conflict-free.

Two replacements are considered conflicts if they overlap or have the same offset (i.e. order-dependent).

Definition at line 212 of file Replacement.h.

Member Typedef Documentation

◆ const_iterator

using clang::tooling::Replacements::const_iterator = ReplacementsImpl::const_iterator

Definition at line 217 of file Replacement.h.

◆ const_reverse_iterator

using clang::tooling::Replacements::const_reverse_iterator = ReplacementsImpl::const_reverse_iterator

Definition at line 218 of file Replacement.h.

Constructor & Destructor Documentation

◆ Replacements() [1/2]

clang::tooling::Replacements::Replacements ( )
default

Referenced by merge().

◆ Replacements() [2/2]

clang::tooling::Replacements::Replacements ( const Replacement R)
inlineexplicit

Definition at line 222 of file Replacement.h.

Member Function Documentation

◆ add()

llvm::Error Replacements::add ( const Replacement R)

Adds a new replacement R to the current set of replacements.

R must have the same file path as all existing replacements. Returns success if the replacement is successfully inserted; otherwise, it returns an llvm::Error, i.e. there is a conflict between R and the existing replacements (i.e. they are order-dependent) or R's file path is different from the filepath of existing replacements. Callers must explicitly check the Error returned, and the returned error can be converted to a string message with llvm::toString(). This prevents users from adding order-dependent replacements. To control the order in which order-dependent replacements are applied, use merge({R}) with R referring to the changed code after applying all existing replacements. Two replacements A and B are considered order-independent if applying them in either order produces the same result. Note that the range of the replacement that is applied later still refers to the original code. These include (but not restricted to) replacements that:

  • don't overlap (being directly adjacent is fine) and
  • are overlapping deletions.
  • are insertions at the same offset and applying them in either order has the same effect, i.e. X + Y = Y + X when inserting X and Y respectively.
  • are identical replacements, i.e. applying the same replacement twice is equivalent to applying it once. Examples:

Replacement A(0, 0, "a") and B(0, 0, "aa") are order-independent since applying them in either order gives replacement (0, 0, "aaa"). However, A(0, 0, "a") and B(0, 0, "b") are order-dependent since applying A first gives (0, 0, "ab") while applying B first gives (B, A, "ba").

  1. Replacement A(0, 2, "123") and B(0, 2, "123") are order-independent since applying them in either order gives (0, 2, "123").
  2. Replacement A(0, 3, "123") and B(2, 3, "321") are order-independent since either order gives (0, 5, "12321").
  3. Replacement A(0, 3, "ab") and B(0, 3, "ab") are order-independent since applying the same replacement twice is equivalent to applying it once. Replacements with offset UINT_MAX are special - we do not detect conflicts for such replacements since users may add them intentionally as a special category of replacements.

Definition at line 246 of file Replacement.cpp.

References clang::tooling::Replacement::getFilePath(), clang::tooling::Replacement::getLength(), clang::tooling::Replacement::getOffset(), clang::tooling::Replacement::getReplacementText(), clang::tooling::insert_conflict, clang::tooling::Range::overlapsWith(), and clang::tooling::wrong_file_path.

Referenced by clang::format::WhitespaceManager::addReplacement(), clang::tooling::applyAtomicChanges(), clang::tooling::calculateRangesAfterReplacements(), clang::syntax::computeReplacements(), clang::tooling::AtomicChange::convertFromYAML(), clang::format::internal::reformat(), clang::format::replaceToken(), clang::tooling::ReplaceStmtWithText::run(), clang::tooling::ReplaceNodeWithTemplate::run(), clang::tooling::ReplaceStmtWithStmt::run(), clang::tooling::ReplaceIfStmtWithItsBody::run(), clang::format::sortCppIncludes(), and clang::format::sortJavaImports().

◆ begin()

const_iterator clang::tooling::Replacements::begin ( ) const
inline

◆ clear()

void clang::tooling::Replacements::clear ( )
inline

Definition at line 277 of file Replacement.h.

◆ empty()

bool clang::tooling::Replacements::empty ( ) const
inline

◆ end()

const_iterator clang::tooling::Replacements::end ( ) const
inline

Definition at line 283 of file Replacement.h.

◆ getAffectedRanges()

std::vector< Range > Replacements::getAffectedRanges ( ) const

◆ getShiftedCodePosition()

unsigned Replacements::getShiftedCodePosition ( unsigned  Position) const

◆ merge()

Replacements Replacements::merge ( const Replacements Replaces) const

Merges Replaces into the current replacements.

Replaces refers to code after applying the current replacements.

Definition at line 444 of file Replacement.cpp.

References empty(), clang::First, Replacements(), and clang::Result.

Referenced by clang::tooling::applyAtomicChanges(), clang::tooling::calculateRangesAfterReplacements(), clang::format::processReplacements(), and clang::format::internal::reformat().

◆ operator==()

bool clang::tooling::Replacements::operator== ( const Replacements RHS) const
inline

Definition at line 289 of file Replacement.h.

◆ rbegin()

const_reverse_iterator clang::tooling::Replacements::rbegin ( ) const
inline

Definition at line 285 of file Replacement.h.

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

◆ rend()

const_reverse_iterator clang::tooling::Replacements::rend ( ) const
inline

Definition at line 287 of file Replacement.h.

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

◆ size()

unsigned clang::tooling::Replacements::size ( ) const
inline

Definition at line 275 of file Replacement.h.


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