LLVM 22.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"
16
17namespace clang {
18class Rewriter;
19} // namespace clang
20
21namespace llvm {
22
23class raw_ostream;
24
25/// RewriteBuffer - As code is rewritten, SourceBuffer's from the original
26/// input with modifications get a new RewriteBuffer associated with them. The
27/// RewriteBuffer captures the modified text itself as well as information used
28/// to map between SourceLocation's in the original input and offsets in the
29/// RewriteBuffer. For example, if text is inserted into the buffer, any
30/// locations after the insertion point have to be mapped.
32 friend class clang::Rewriter;
33
34 /// Deltas - Keep track of all the deltas in the source code due to insertions
35 /// and deletions.
36 DeltaTree Deltas;
37
38 RewriteRope Buffer;
39
40public:
42
43 iterator begin() const { return Buffer.begin(); }
44 iterator end() const { return Buffer.end(); }
45 unsigned size() const { return Buffer.size(); }
46
47 /// Initialize - Start this rewrite buffer out with a copy of the unmodified
48 /// input buffer.
49 void Initialize(const char *BufStart, const char *BufEnd) {
50 Buffer.assign(BufStart, BufEnd);
51 }
52 void Initialize(StringRef Input) { Initialize(Input.begin(), Input.end()); }
53
54 /// Write to \p Stream the result of applying all changes to the
55 /// original buffer.
56 /// Note that it isn't safe to use this function to overwrite memory mapped
57 /// files in-place (PR17960). Consider using a higher-level utility such as
58 /// Rewriter::overwriteChangedFiles() instead.
59 ///
60 /// The original buffer is not actually changed.
61 LLVM_ABI raw_ostream &write(raw_ostream &Stream) const;
62
63 /// RemoveText - Remove the specified text.
64 LLVM_ABI void RemoveText(unsigned OrigOffset, unsigned Size,
65 bool removeLineIfEmpty = false);
66
67 /// InsertText - Insert some text at the specified point, where the offset in
68 /// the buffer is specified relative to the original SourceBuffer. The
69 /// text is inserted after the specified location.
70 LLVM_ABI void InsertText(unsigned OrigOffset, StringRef Str,
71 bool InsertAfter = true);
72
73 /// InsertTextBefore - Insert some text before the specified point, where the
74 /// offset in the buffer is specified relative to the original
75 /// SourceBuffer. The text is inserted before the specified location. This is
76 /// method is the same as InsertText with "InsertAfter == false".
77 void InsertTextBefore(unsigned OrigOffset, StringRef Str) {
78 InsertText(OrigOffset, Str, false);
79 }
80
81 /// InsertTextAfter - Insert some text at the specified point, where the
82 /// offset in the buffer is specified relative to the original SourceBuffer.
83 /// The text is inserted after the specified location.
84 void InsertTextAfter(unsigned OrigOffset, StringRef Str) {
85 InsertText(OrigOffset, Str);
86 }
87
88 /// ReplaceText - This method replaces a range of characters in the input
89 /// buffer with a new string. This is effectively a combined "remove/insert"
90 /// operation.
91 LLVM_ABI void ReplaceText(unsigned OrigOffset, unsigned OrigLength,
92 StringRef NewStr);
93
94private:
95 /// getMappedOffset - Given an offset into the original SourceBuffer that this
96 /// RewriteBuffer is based on, map it into the offset space of the
97 /// RewriteBuffer. If AfterInserts is true and if the OrigOffset indicates a
98 /// position where text is inserted, the location returned will be after any
99 /// inserted text at the position.
100 unsigned getMappedOffset(unsigned OrigOffset,
101 bool AfterInserts = false) const {
102 return Deltas.getDeltaAt(2 * OrigOffset + AfterInserts) + OrigOffset;
103 }
104
105 /// AddInsertDelta - When an insertion is made at a position, this
106 /// method is used to record that information.
107 void AddInsertDelta(unsigned OrigOffset, int Change) {
108 return Deltas.AddDelta(2 * OrigOffset, Change);
109 }
110
111 /// AddReplaceDelta - When a replacement/deletion is made at a position, this
112 /// method is used to record that information.
113 void AddReplaceDelta(unsigned OrigOffset, int Change) {
114 return Deltas.AddDelta(2 * OrigOffset + 1, Change);
115 }
116};
117
118} // namespace llvm
119
120#endif // LLVM_ADT_REWRITEBUFFER_H
#define LLVM_ABI
Definition Compiler.h:213
The Input class is used to parse a yaml document into in-memory structs and vectors.
DeltaTree - a multiway search tree (BTree) structure with some fancy features.
Definition DeltaTree.h:27
LLVM_ABI 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...
LLVM_ABI int getDeltaAt(unsigned FileIndex) const
getDeltaAt - Return the accumulated delta at the specified file offset.
RewriteBuffer - As code is rewritten, SourceBuffer's from the original input with modifications get a...
LLVM_ABI void RemoveText(unsigned OrigOffset, unsigned Size, bool removeLineIfEmpty=false)
RemoveText - Remove the specified text.
LLVM_ABI raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer.
iterator end() const
LLVM_ABI 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
void InsertTextBefore(unsigned OrigOffset, StringRef Str)
InsertTextBefore - Insert some text before the specified point, where the offset in the buffer is spe...
friend class clang::Rewriter
iterator begin() const
void Initialize(const char *BufStart, const char *BufEnd)
Initialize - Start this rewrite buffer out with a copy of the unmodified input buffer.
RewriteRope::const_iterator iterator
void Initialize(StringRef Input)
LLVM_ABI 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...
RewriteRope - A powerful string class.
RopePieceBTree::iterator const_iterator
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.