clang 10.0.0git

Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
clang::Rewriter Class Reference

Rewriter - This is the main interface to the rewrite buffers. More...

`#include "clang/Rewrite/Core/Rewriter.h"`

## Classes
struct | RewriteOptions

## Public Types
using | buffer_iterator = std::map< FileID, RewriteBuffer >::iterator
using | const_buffer_iterator = std::map< FileID, RewriteBuffer >::const_iterator

## Public Member Functions
| Rewriter ()=default
| Rewriter (SourceManager &SM, const LangOptions &LO)
void | setSourceMgr (SourceManager &SM, const LangOptions &LO)
SourceManager & | getSourceMgr () const
const LangOptions & | getLangOpts () const
int | getRangeSize (SourceRange Range, RewriteOptions opts=RewriteOptions()) const
| getRangeSize - Return the size in bytes of the specified range if they are in the same file. More...
int | getRangeSize (const CharSourceRange &Range, RewriteOptions opts=RewriteOptions()) const
| getRangeSize - Return the size in bytes of the specified range if they are in the same file. More...
std::string | getRewrittenText (CharSourceRange Range) const
| getRewrittenText - Return the rewritten form of the text in the specified range. More...
std::string | getRewrittenText (SourceRange Range) const
| getRewrittenText - Return the rewritten form of the text in the specified range. More...
bool | InsertText (SourceLocation Loc, StringRef Str, bool InsertAfter=true, bool indentNewLines=false)
| InsertText - Insert the specified string at the specified location in the original buffer. More...
bool | InsertTextAfter (SourceLocation Loc, StringRef Str)
| InsertTextAfter - Insert the specified string at the specified location in the original buffer. More...
bool | InsertTextAfterToken (SourceLocation Loc, StringRef Str)
| Insert the specified string after the token in the specified location. More...
bool | InsertTextBefore (SourceLocation Loc, StringRef Str)
| InsertText - Insert the specified string at the specified location in the original buffer. More...
bool | RemoveText (SourceLocation Start, unsigned Length, RewriteOptions opts=RewriteOptions())
| RemoveText - Remove the specified text region. More...
bool | RemoveText (CharSourceRange range, RewriteOptions opts=RewriteOptions())
| Remove the specified text region. More...
bool | RemoveText (SourceRange range, RewriteOptions opts=RewriteOptions())
| Remove the specified text region. More...
bool | ReplaceText (SourceLocation Start, unsigned OrigLength, StringRef NewStr)
| ReplaceText - This method replaces a range of characters in the input buffer with a new string. More...
bool | ReplaceText (CharSourceRange range, StringRef NewStr)
| ReplaceText - This method replaces a range of characters in the input buffer with a new string. More...
bool | ReplaceText (SourceRange range, StringRef NewStr)
| ReplaceText - This method replaces a range of characters in the input buffer with a new string. More...
bool | ReplaceText (SourceRange range, SourceRange replacementRange)
| ReplaceText - This method replaces a range of characters in the input buffer with a new string. More...
bool | IncreaseIndentation (CharSourceRange range, SourceLocation parentIndent)
| Increase indentation for the lines between the given source range. More...
bool | IncreaseIndentation (SourceRange range, SourceLocation parentIndent)
RewriteBuffer & | getEditBuffer (FileID FID)
| getEditBuffer - This is like getRewriteBufferFor, but always returns a buffer, and allows you to write on it directly. More...
const RewriteBuffer * | getRewriteBufferFor (FileID FID) const
| getRewriteBufferFor - Return the rewrite buffer for the specified FileID. More...
buffer_iterator | buffer_begin ()
buffer_iterator | buffer_end ()
const_buffer_iterator | buffer_begin () const
const_buffer_iterator | buffer_end () const
bool | overwriteChangedFiles ()
| overwriteChangedFiles - Save all changed files to disk. More...

## Static Public Member Functions
static bool | isRewritable (SourceLocation Loc)
| isRewritable - Return true if this location is a raw file location, which is rewritable. More...

## Detailed Description

Rewriter - This is the main interface to the rewrite buffers.

Its primary job is to dispatch high-level requests to the low-level RewriteBuffers that are involved.

Definition at line 32 of file Rewriter.h.

## Member Typedef Documentation

## ◆ buffer_iterator

using clang::Rewriter::buffer_iterator = std::map<FileID, RewriteBuffer>::iterator

Definition at line 65 of file Rewriter.h.

## ◆ const_buffer_iterator

using clang::Rewriter::const_buffer_iterator = std::map<FileID, RewriteBuffer>::const_iterator

Definition at line 66 of file Rewriter.h.

## Constructor & Destructor Documentation

## ◆ Rewriter() [1/2]

clang::Rewriter::Rewriter | ( |  | ) |
| explicit default

## ◆ Rewriter() [2/2]

clang::Rewriter::Rewriter | ( | SourceManager & | SM,
|  | const LangOptions & | LO
| ) |  |
| inline explicit

Definition at line 69 of file Rewriter.h.

## Member Function Documentation

## ◆ buffer_begin() [1/2]

buffer_iterator clang::Rewriter::buffer_begin | ( |  | ) |
| inline

Definition at line 205 of file Rewriter.h.

Referenced by clang::FixItRewriter::buffer_begin().

## ◆ buffer_begin() [2/2]

const_buffer_iterator clang::Rewriter::buffer_begin | ( |  | ) | const
| inline

Definition at line 207 of file Rewriter.h.

## ◆ buffer_end() [1/2]

buffer_iterator clang::Rewriter::buffer_end | ( |  | ) |
| inline

Definition at line 206 of file Rewriter.h.

Referenced by clang::FixItRewriter::buffer_end().

## ◆ buffer_end() [2/2]

const_buffer_iterator clang::Rewriter::buffer_end | ( |  | ) | const
| inline

Definition at line 208 of file Rewriter.h.

References overwriteChangedFiles().

## ◆ getEditBuffer()

RewriteBuffer & Rewriter::getEditBuffer | ( | FileID | FID | ) |

getEditBuffer - This is like getRewriteBufferFor, but always returns a buffer, and allows you to write on it directly.

getEditBuffer - Get or create a RewriteBuffer for the specified FileID.

This is useful if you want efficient low-level access to apis for scribbling on one specific FileID's buffer.

Definition at line 240 of file Rewriter.cpp.

References clang::RewriteBuffer::Initialize().

Referenced by clang::html::AddLineNumbers(), clang::tooling::applyAllReplacements(), clang::html::EscapeText(), clang::index::SimpleFormatContext::getRewrittenText(), clang::html::HighlightRange(), IncreaseIndentation(), clang::RewriteMacrosInInput(), and clang::html::SyntaxHighlight().

## ◆ getLangOpts()

const LangOptions& clang::Rewriter::getLangOpts | ( |  | ) | const
| inline

Definition at line 78 of file Rewriter.h.

Referenced by EmitAlphaCounter(), and clang::html::HighlightRange().

## ◆ getRangeSize() [1/2]

int Rewriter::getRangeSize | ( | SourceRange | Range,
|  | RewriteOptions | opts = `RewriteOptions()`
| ) |  | const

getRangeSize - Return the size in bytes of the specified range if they are in the same file.

If not, this returns -1.

Definition at line 175 of file Rewriter.cpp.

References getRangeSize(), and clang::CharSourceRange::getTokenRange().

Referenced by hasSuperInitCall(), isRewritable(), RemoveText(), ReplaceText(), and clang::FixItRewriter::WriteFixedFile().

## ◆ getRangeSize() [2/2]

int Rewriter::getRangeSize | ( | const CharSourceRange & | Range,
|  | RewriteOptions | opts = `RewriteOptions()`
| ) |  | const

getRangeSize - Return the size in bytes of the specified range if they are in the same file.

If not, this returns -1.

Definition at line 145 of file Rewriter.cpp.

References clang::CharSourceRange::getBegin(), clang::CharSourceRange::getEnd(), clang::Rewriter::RewriteOptions::IncludeInsertsAtBeginOfRange, clang::Rewriter::RewriteOptions::IncludeInsertsAtEndOfRange, clang::CharSourceRange::isTokenRange(), and clang::Lexer::MeasureTokenLength().

## ◆ getRewriteBufferFor()

const RewriteBuffer* clang::Rewriter::getRewriteBufferFor | ( | FileID | FID | ) | const
| inline

getRewriteBufferFor - Return the rewrite buffer for the specified FileID.

If no modification has been made to it, return null.

Definition at line 198 of file Rewriter.h.

References clang::RewriteBuffer::end().

Referenced by applyEditsToTemp(), clang::FixItRewriter::IsModified(), clang::RewriteMacrosInInput(), Write_ProtocolExprReferencedMetadata(), and clang::FixItRewriter::WriteFixedFile().

## ◆ getRewrittenText() [1/2]

std::string Rewriter::getRewrittenText | ( | CharSourceRange | Range | ) | const

getRewrittenText - Return the rewritten form of the text in the specified range.

If the start or end of the range was unrewritable or if they are in different buffers, this returns an empty string.

Note that this method is not particularly efficient.

Definition at line 184 of file Rewriter.cpp.

References clang::RewriteBuffer::begin(), End, clang::CharSourceRange::getBegin(), clang::CharSourceRange::getEnd(), clang::CharSourceRange::isTokenRange(), clang::SourceLocation::isValid(), clang::Lexer::MeasureTokenLength(), and V.

Referenced by getRewrittenText(), and isRewritable().

## ◆ getRewrittenText() [2/2]

std::string clang::Rewriter::getRewrittenText | ( | SourceRange | Range | ) | const
| inline

getRewrittenText - Return the rewritten form of the text in the specified range.

If the start or end of the range was unrewritable or if they are in different buffers, this returns an empty string.

Note that this method is not particularly efficient.

Definition at line 105 of file Rewriter.h.

References getRewrittenText(), clang::CharSourceRange::getTokenRange(), and InsertText().

## ◆ getSourceMgr()

SourceManager& clang::Rewriter::getSourceMgr | ( |  | ) | const
| inline

Definition at line 77 of file Rewriter.h.

Referenced by clang::html::AddHeaderFooterInternalBuiltinCSS(), clang::html::AddLineNumbers(), clang::tooling::Replacement::apply(), clang::tooling::RefactoringTool::applyAllReplacements(), EmitAlphaCounter(), clang::html::EscapeText(), clang::tooling::formatAndApplyAllReplacements(), HandlePopUpPieceEndTag(), and clang::html::HighlightRange().

## ◆ IncreaseIndentation() [1/2]

bool Rewriter::IncreaseIndentation | ( | CharSourceRange | range,
|  | SourceLocation | parentIndent
| ) |  |

Increase indentation for the lines between the given source range.

To determine what the indentation should be, 'parentIndent' is used that should be at a source location with an indentation one degree lower than the given range.

Definition at line 344 of file Rewriter.cpp.

References Filename, clang::CharSourceRange::getBegin(), clang::CharSourceRange::getEnd(), clang::RewriteBuffer::InsertText(), clang::CharSourceRange::isInvalid(), isWhitespaceExceptNL(), clang::transformer::remove(), clang::DiagnosticsEngine::Report(), and clang::SrcMgr::ContentCache::SourceLineCache.

Referenced by clang::arcmt::getFileRemappings(), IncreaseIndentation(), and ReplaceText().

## ◆ IncreaseIndentation() [2/2]

bool clang::Rewriter::IncreaseIndentation | ( | SourceRange | range,
|  | SourceLocation | parentIndent
| ) |  |
| inline

Definition at line 185 of file Rewriter.h.

References getEditBuffer(), clang::CharSourceRange::getTokenRange(), and IncreaseIndentation().

## ◆ InsertText()

bool Rewriter::InsertText | ( | SourceLocation | Loc,
|  | StringRef | Str,
|  | bool | InsertAfter = `true`,
|  | bool | indentNewLines = `false`
| ) |  |

InsertText - Insert the specified string at the specified location in the original buffer.

This method returns true (and does nothing) if the input location was not rewritable, false otherwise.

Parameters

  indentNewLines | if true new lines in the string are indented using the indentation of the source line in position `Loc`.

Definition at line 255 of file Rewriter.cpp.

References isWhitespaceExceptNL(), and clang::SrcMgr::ContentCache::SourceLineCache.

Referenced by clang::arcmt::getFileRemappings(), getRewrittenText(), hasSuperInitCall(), InsertTextAfter(), InsertTextBefore(), and clang::FixItRewriter::WriteFixedFile().

## ◆ InsertTextAfter()

bool clang::Rewriter::InsertTextAfter | ( | SourceLocation | Loc,
|  | StringRef | Str
| ) |  |
| inline

InsertTextAfter - Insert the specified string at the specified location in the original buffer.

This method returns true (and does nothing) if the input location was not rewritable, false otherwise. Text is inserted after any other text that has been previously inserted at the some point (the default behavior for InsertText).

Definition at line 123 of file Rewriter.h.

References InsertText(), and InsertTextAfterToken().

Referenced by clang::html::AddHeaderFooterInternalBuiltinCSS().

## ◆ InsertTextAfterToken()

bool Rewriter::InsertTextAfterToken | ( | SourceLocation | Loc,
|  | StringRef | Str
| ) |  |

Insert the specified string after the token in the specified location.

Definition at line 296 of file Rewriter.cpp.

References getRangeSize(), and clang::Rewriter::RewriteOptions::IncludeInsertsAtBeginOfRange.

Referenced by InsertTextAfter().

## ◆ InsertTextBefore()

bool clang::Rewriter::InsertTextBefore | ( | SourceLocation | Loc,
|  | StringRef | Str
| ) |  |
| inline

InsertText - Insert the specified string at the specified location in the original buffer.

This method returns true (and does nothing) if the input location was not rewritable, false otherwise. Text is inserted before any other text that has been previously inserted at the some point.

Definition at line 136 of file Rewriter.h.

References InsertText(), RemoveText(), and clang::Rewriter::RewriteOptions::RewriteOptions().

Referenced by clang::html::AddHeaderFooterInternalBuiltinCSS(), and HandlePopUpPieceEndTag().

## ◆ isRewritable()

static bool clang::Rewriter::isRewritable | ( | SourceLocation | Loc | ) |
| inline static

isRewritable - Return true if this location is a raw file location, which is rewritable.

Locations from macros, etc are not rewritable.

Definition at line 82 of file Rewriter.h.

References getRangeSize(), getRewrittenText(), clang::SourceLocation::isFileID(), and clang::Rewriter::RewriteOptions::RewriteOptions().

## ◆ overwriteChangedFiles()

bool Rewriter::overwriteChangedFiles | ( |  | ) |

overwriteChangedFiles - Save all changed files to disk.

Returns true if any files were not saved successfully. Outputs diagnostics via the source manager's diagnostic engine in case of an error.

Definition at line 465 of file Rewriter.cpp.

References clang::FileEntry::getName().

Referenced by clang::tooling::RefactoringTool::applyAllReplacements(), and buffer_end().

## ◆ RemoveText() [1/3]

bool Rewriter::RemoveText | ( | SourceLocation | Start,
|  | unsigned | Length,
|  | RewriteOptions | opts = `RewriteOptions()`
| ) |  |

RemoveText - Remove the specified text region.

Definition at line 308 of file Rewriter.cpp.

References clang::Rewriter::RewriteOptions::RemoveLineIfEmpty.

Referenced by clang::arcmt::getFileRemappings(), InsertTextBefore(), and RemoveText().

## ◆ RemoveText() [2/3]

bool clang::Rewriter::RemoveText | ( | CharSourceRange | range,
|  | RewriteOptions | opts = `RewriteOptions()`
| ) |  |
| inline

Remove the specified text region.

Definition at line 145 of file Rewriter.h.

References clang::CharSourceRange::getBegin(), getRangeSize(), and RemoveText().

## ◆ RemoveText() [3/3]

bool clang::Rewriter::RemoveText | ( | SourceRange | range,
|  | RewriteOptions | opts = `RewriteOptions()`
| ) |  |
| inline

Remove the specified text region.

Definition at line 151 of file Rewriter.h.

References clang::SourceRange::getBegin(), getRangeSize(), RemoveText(), and ReplaceText().

## ◆ ReplaceText() [1/4]

bool Rewriter::ReplaceText | ( | SourceLocation | Start,
|  | unsigned | OrigLength,
|  | StringRef | NewStr
| ) |  |

ReplaceText - This method replaces a range of characters in the input buffer with a new string.

This is effectively a combined "remove/insert" operation.

Definition at line 320 of file Rewriter.cpp.

Referenced by clang::tooling::Replacement::apply(), hasSuperInitCall(), RemoveText(), ReplaceText(), and clang::FixItRewriter::WriteFixedFile().

## ◆ ReplaceText() [2/4]

bool clang::Rewriter::ReplaceText | ( | CharSourceRange | range,
|  | StringRef | NewStr
| ) |  |
| inline

ReplaceText - This method replaces a range of characters in the input buffer with a new string.

This is effectively a combined "remove/insert" operation.

Definition at line 164 of file Rewriter.h.

References clang::CharSourceRange::getBegin(), getRangeSize(), and ReplaceText().

## ◆ ReplaceText() [3/4]

bool clang::Rewriter::ReplaceText | ( | SourceRange | range,
|  | StringRef | NewStr
| ) |  |
| inline

ReplaceText - This method replaces a range of characters in the input buffer with a new string.

This is effectively a combined "remove/insert" operation.

Definition at line 171 of file Rewriter.h.

References clang::SourceRange::getBegin(), getRangeSize(), IncreaseIndentation(), and ReplaceText().

## ◆ ReplaceText() [4/4]

bool Rewriter::ReplaceText | ( | SourceRange | range,
|  | SourceRange | replacementRange
| ) |  |

ReplaceText - This method replaces a range of characters in the input buffer with a new string.

This is effectively a combined "remove/insert" operation.

Definition at line 330 of file Rewriter.cpp.

References clang::SourceRange::getBegin(), clang::SourceRange::getEnd(), getRangeSize(), clang::SourceRange::isInvalid(), and clang::RewriteBuffer::ReplaceText().

## ◆ setSourceMgr()

void clang::Rewriter::setSourceMgr | ( | SourceManager & | SM,
|  | const LangOptions & | LO
| ) |  |
| inline

Definition at line 72 of file Rewriter.h.

References SM.

Referenced by clang::CreateModernObjCRewriter(), clang::CreateObjCRewriter(), and clang::RewriteMacrosInInput().

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

- include/clang/Rewrite/Core/Rewriter.h
- lib/Rewrite/Rewriter.cpp