LLVM 20.0.0git
RewriteBuffer.h
Go to the documentation of this file.
1//===- RewriteBuffer.h - Buffer rewriting interface -------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_ADT_REWRITEBUFFER_H
10#define LLVM_ADT_REWRITEBUFFER_H
11
12#include "llvm/ADT/DeltaTree.h"
14#include "llvm/ADT/StringRef.h"
15
16namespace clang {
17class Rewriter;
18} // namespace clang
19
20namespace llvm {
21
22class raw_ostream;
23
24/// RewriteBuffer - As code is rewritten, SourceBuffer's from the original
25/// input with modifications get a new RewriteBuffer associated with them. The
26/// RewriteBuffer captures the modified text itself as well as information used
27/// to map between SourceLocation's in the original input and offsets in the
28/// RewriteBuffer. For example, if text is inserted into the buffer, any
29/// locations after the insertion point have to be mapped.
31 friend class clang::Rewriter;
32
33 /// Deltas - Keep track of all the deltas in the source code due to insertions
34 /// and deletions.
35 DeltaTree Deltas;
36
37 RewriteRope Buffer;
38
39public:
41
42 iterator begin() const { return Buffer.begin(); }
43 iterator end() const { return Buffer.end(); }
44 unsigned size() const { return Buffer.size(); }
45
46 /// Initialize - Start this rewrite buffer out with a copy of the unmodified
47 /// input buffer.
48 void Initialize(const char *BufStart, const char *BufEnd) {
49 Buffer.assign(BufStart, BufEnd);
50 }
51 void Initialize(StringRef Input) { Initialize(Input.begin(), Input.end()); }
52
53 /// Write to \p Stream the result of applying all changes to the
54 /// original buffer.
55 /// Note that it isn't safe to use this function to overwrite memory mapped
56 /// files in-place (PR17960). Consider using a higher-level utility such as
57 /// Rewriter::overwriteChangedFiles() instead.
58 ///
59 /// The original buffer is not actually changed.
60 raw_ostream &write(raw_ostream &Stream) const;
61
62 /// RemoveText - Remove the specified text.
63 void RemoveText(unsigned OrigOffset, unsigned Size,
64 bool removeLineIfEmpty = false);
65
66 /// InsertText - Insert some text at the specified point, where the offset in
67 /// the buffer is specified relative to the original SourceBuffer. The
68 /// text is inserted after the specified location.
69 void InsertText(unsigned OrigOffset, StringRef Str, bool InsertAfter = true);
70
71 /// InsertTextBefore - Insert some text before the specified point, where the
72 /// offset in the buffer is specified relative to the original
73 /// SourceBuffer. The text is inserted before the specified location. This is
74 /// method is the same as InsertText with "InsertAfter == false".
75 void InsertTextBefore(unsigned OrigOffset, StringRef Str) {
76 InsertText(OrigOffset, Str, false);
77 }
78
79 /// InsertTextAfter - Insert some text at the specified point, where the
80 /// offset in the buffer is specified relative to the original SourceBuffer.
81 /// The text is inserted after the specified location.
82 void InsertTextAfter(unsigned OrigOffset, StringRef Str) {
83 InsertText(OrigOffset, Str);
84 }
85
86 /// ReplaceText - This method replaces a range of characters in the input
87 /// buffer with a new string. This is effectively a combined "remove/insert"
88 /// operation.
89 void ReplaceText(unsigned OrigOffset, unsigned OrigLength, StringRef NewStr);
90
91private:
92 /// getMappedOffset - Given an offset into the original SourceBuffer that this
93 /// RewriteBuffer is based on, map it into the offset space of the
94 /// RewriteBuffer. If AfterInserts is true and if the OrigOffset indicates a
95 /// position where text is inserted, the location returned will be after any
96 /// inserted text at the position.
97 unsigned getMappedOffset(unsigned OrigOffset,
98 bool AfterInserts = false) const {
99 return Deltas.getDeltaAt(2 * OrigOffset + AfterInserts) + OrigOffset;
100 }
101
102 /// AddInsertDelta - When an insertion is made at a position, this
103 /// method is used to record that information.
104 void AddInsertDelta(unsigned OrigOffset, int Change) {
105 return Deltas.AddDelta(2 * OrigOffset, Change);
106 }
107
108 /// AddReplaceDelta - When a replacement/deletion is made at a position, this
109 /// method is used to record that information.
110 void AddReplaceDelta(unsigned OrigOffset, int Change) {
111 return Deltas.AddDelta(2 * OrigOffset + 1, Change);
112 }
113};
114
115} // namespace llvm
116
117#endif // LLVM_ADT_REWRITEBUFFER_H
uint64_t Size
Virtual Register Rewriter
Definition: VirtRegMap.cpp:261
DeltaTree - a multiway search tree (BTree) structure with some fancy features.
Definition: DeltaTree.h:25
void AddDelta(unsigned FileIndex, int Delta)
AddDelta - When a change is made that shifts around the text buffer, this method is used to record th...
Definition: DeltaTree.cpp:450
int getDeltaAt(unsigned FileIndex) const
getDeltaAt - Return the accumulated delta at the specified file offset.
Definition: DeltaTree.cpp:402
RewriteBuffer - As code is rewritten, SourceBuffer's from the original input with modifications get a...
Definition: RewriteBuffer.h:30
void RemoveText(unsigned OrigOffset, unsigned Size, bool removeLineIfEmpty=false)
RemoveText - Remove the specified text.
raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer.
iterator end() const
Definition: RewriteBuffer.h:43
void InsertText(unsigned OrigOffset, StringRef Str, bool InsertAfter=true)
InsertText - Insert some text at the specified point, where the offset in the buffer is specified rel...
unsigned size() const
Definition: RewriteBuffer.h:44
void InsertTextBefore(unsigned OrigOffset, StringRef Str)
InsertTextBefore - Insert some text before the specified point, where the offset in the buffer is spe...
Definition: RewriteBuffer.h:75
friend class clang::Rewriter
Definition: RewriteBuffer.h:31
iterator begin() const
Definition: RewriteBuffer.h:42
void Initialize(const char *BufStart, const char *BufEnd)
Initialize - Start this rewrite buffer out with a copy of the unmodified input buffer.
Definition: RewriteBuffer.h:48
void Initialize(StringRef Input)
Definition: RewriteBuffer.h:51
void ReplaceText(unsigned OrigOffset, unsigned OrigLength, StringRef NewStr)
ReplaceText - This method replaces a range of characters in the input buffer with a new string.
void InsertTextAfter(unsigned OrigOffset, StringRef Str)
InsertTextAfter - Insert some text at the specified point, where the offset in the buffer is specifie...
Definition: RewriteBuffer.h:82
RewriteRope - A powerful string class.
Definition: RewriteRope.h:171
unsigned size() const
Definition: RewriteRope.h:193
iterator end() const
Definition: RewriteRope.h:192
RopePieceBTree::iterator const_iterator
Definition: RewriteRope.h:189
iterator begin() const
Definition: RewriteRope.h:191
void assign(const char *Start, const char *End)
Definition: RewriteRope.h:197
RopePieceBTreeIterator - This class provides read-only forward iteration over bytes that are in a Rop...
Definition: RewriteRope.h:86
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
iterator begin() const
Definition: StringRef.h:116
iterator end() const
Definition: StringRef.h:118
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18