clang  12.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. More...
LLVM_NODISCARD Replacements merge (const Replacements &Replaces) const
 Merges Replaces into the current replacements. More...
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 209 of file Replacement.h.

Member Typedef Documentation

◆ const_iterator

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

Definition at line 214 of file Replacement.h.

◆ const_reverse_iterator

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

Definition at line 215 of file Replacement.h.

Constructor & Destructor Documentation

◆ Replacements() [1/2]

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

◆ Replacements() [2/2]

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

Definition at line 219 of file Replacement.h.

References clang::Error.

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 245 of file Replacement.cpp.

References End, clang::tooling::Replacement::getFilePath(), clang::tooling::Replacement::getLength(), clang::tooling::Replacement::getOffset(), clang::tooling::Replacement::getReplacementText(), clang::tooling::insert_conflict, max(), Offset, Text, and clang::tooling::wrong_file_path.

Referenced by clang::tooling::applyAtomicChanges(), clang::tooling::calculateRangesAfterReplacements(), clang::syntax::computeReplacements(), clang::tooling::AtomicChange::convertFromYAML(), clang::format::formatReplacements(), clang::tooling::AtomicChange::insert(), clang::tooling::HeaderIncludes::remove(), clang::tooling::AtomicChange::replace(), clang::tooling::ReplaceStmtWithText::run(), clang::tooling::ReplaceNodeWithTemplate::run(), clang::tooling::ReplaceStmtWithStmt::run(), and clang::tooling::ReplaceIfStmtWithItsBody::run().

◆ begin()

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

◆ clear()

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

Definition at line 274 of file Replacement.h.

References clear().

Referenced by clear().

◆ empty()

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

◆ end()

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

Definition at line 280 of file Replacement.h.

References end().

Referenced by end().

◆ getAffectedRanges()

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

◆ getShiftedCodePosition()

unsigned Replacements::getShiftedCodePosition ( unsigned  Position) const

Definition at line 546 of file Replacement.cpp.

Referenced by clang::tooling::AtomicChange::insert().

◆ 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 443 of file Replacement.cpp.

References empty(), and clang::First.

Referenced by clang::tooling::applyAtomicChanges(), clang::tooling::calculateRangesAfterReplacements(), clang::tooling::AtomicChange::insert(), clang::tooling::ReplacementError::message(), and clang::format::processReplacements().

◆ operator==()

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

Definition at line 286 of file Replacement.h.

References clang::tooling::applyAllReplacements(), Begin, End, and clang::Rewrite.

◆ rbegin()

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

Definition at line 282 of file Replacement.h.

References rbegin().

Referenced by clang::tooling::applyAllReplacements(), and rbegin().

◆ rend()

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

Definition at line 284 of file Replacement.h.

References rend().

Referenced by clang::tooling::applyAllReplacements(), and rend().

◆ size()

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

Definition at line 272 of file Replacement.h.

References size().

Referenced by size().

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