clang 8.0.0

RewriteBuffer.h
Go to the documentation of this file.
1 //===- RewriteBuffer.h - Buffer rewriting interface -------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_REWRITE_CORE_REWRITEBUFFER_H
11 #define LLVM_CLANG_REWRITE_CORE_REWRITEBUFFER_H
12
13 #include "clang/Basic/LLVM.h"
14 #include "clang/Rewrite/Core/DeltaTree.h"
15 #include "clang/Rewrite/Core/RewriteRope.h"
16 #include "llvm/ADT/StringRef.h"
17
18 namespace clang {
19
20 /// RewriteBuffer - As code is rewritten, SourceBuffer's from the original
21 /// input with modifications get a new RewriteBuffer associated with them. The
22 /// RewriteBuffer captures the modified text itself as well as information used
23 /// to map between SourceLocation's in the original input and offsets in the
24 /// RewriteBuffer. For example, if text is inserted into the buffer, any
25 /// locations after the insertion point have to be mapped.
26 class RewriteBuffer {
27 friend class Rewriter;
28
29 /// Deltas - Keep track of all the deltas in the source code due to insertions
30 /// and deletions.
31 DeltaTree Deltas;
32
33 RewriteRope Buffer;
34
35 public:
36 using iterator = RewriteRope::const_iterator;
37
38 iterator begin() const { return Buffer.begin(); }
39 iterator end() const { return Buffer.end(); }
40 unsigned size() const { return Buffer.size(); }
41
42 /// Initialize - Start this rewrite buffer out with a copy of the unmodified
43 /// input buffer.
44 void Initialize(const char *BufStart, const char *BufEnd) {
45 Buffer.assign(BufStart, BufEnd);
46 }
47 void Initialize(StringRef Input) {
48 Initialize(Input.begin(), Input.end());
49 }
50
51 /// Write to \p Stream the result of applying all changes to the
52 /// original buffer.
53 /// Note that it isn't safe to use this function to overwrite memory mapped
54 /// files in-place (PR17960). Consider using a higher-level utility such as
55 /// Rewriter::overwriteChangedFiles() instead.
56 ///
57 /// The original buffer is not actually changed.
58 raw_ostream &write(raw_ostream &Stream) const;
59
60 /// RemoveText - Remove the specified text.
61 void RemoveText(unsigned OrigOffset, unsigned Size,
62 bool removeLineIfEmpty = false);
63
64 /// InsertText - Insert some text at the specified point, where the offset in
65 /// the buffer is specified relative to the original SourceBuffer. The
66 /// text is inserted after the specified location.
67 void InsertText(unsigned OrigOffset, StringRef Str,
68 bool InsertAfter = true);
69
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,
90 StringRef NewStr);
91
92 private:
93 /// getMappedOffset - Given an offset into the original SourceBuffer that this
94 /// RewriteBuffer is based on, map it into the offset space of the
95 /// RewriteBuffer. If AfterInserts is true and if the OrigOffset indicates a
96 /// position where text is inserted, the location returned will be after any
97 /// inserted text at the position.
98 unsigned getMappedOffset(unsigned OrigOffset,
99 bool AfterInserts = false) const{
100 return Deltas.getDeltaAt(2*OrigOffset+AfterInserts)+OrigOffset;
101 }
102
103 /// AddInsertDelta - When an insertion is made at a position, this
104 /// method is used to record that information.
105 void AddInsertDelta(unsigned OrigOffset, int Change) {
106 return Deltas.AddDelta(2*OrigOffset, Change);
107 }
108
109 /// AddReplaceDelta - When a replacement/deletion is made at a position, this
110 /// method is used to record that information.
111 void AddReplaceDelta(unsigned OrigOffset, int Change) {
112 return Deltas.AddDelta(2*OrigOffset+1, Change);
113 }
114 };
115
116 } // namespace clang
117
118 #endif // LLVM_CLANG_REWRITE_CORE_REWRITEBUFFER_H
clang::DeltaTree::AddDelta
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:456
clang::RewriteBuffer::Initialize
void Initialize(StringRef Input)
Definition: RewriteBuffer.h:47
clang::RewriteRope
RewriteRope - A powerful string class.
Definition: RewriteRope.h:167
clang::RewriteBuffer::Initialize
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:44
clang::RewriteBuffer
RewriteBuffer - As code is rewritten, SourceBuffer&#39;s from the original input with modifications get a...
Definition: RewriteBuffer.h:26
clang::RewriteBuffer::end
iterator end() const
Definition: RewriteBuffer.h:39
clang::RewriteRope::const_iterator
RopePieceBTree::iterator const_iterator
Definition: RewriteRope.h:181
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::RewriteRope::end
iterator end() const
Definition: RewriteRope.h:184
clang::RewriteRope::begin
iterator begin() const
Definition: RewriteRope.h:183
clang::RewriteBuffer::InsertTextAfter
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
clang::DeltaTree::getDeltaAt
int getDeltaAt(unsigned FileIndex) const
getDeltaAt - Return the accumulated delta at the specified file offset.
Definition: DeltaTree.cpp:409
clang::RewriteBuffer::InsertText
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...
Definition: Rewriter.cpp:105
clang::RewriteBuffer::begin
iterator begin() const
Definition: RewriteBuffer.h:38
clang::DeltaTree
DeltaTree - a multiway search tree (BTree) structure with some fancy features.
Definition: DeltaTree.h:26
clang::RewriteBuffer::ReplaceText
void ReplaceText(unsigned OrigOffset, unsigned OrigLength, StringRef NewStr)
ReplaceText - This method replaces a range of characters in the input buffer with a new string...
Definition: Rewriter.cpp:120
clang::RewriteBuffer::write
raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer.
Definition: Rewriter.cpp:38
clang::RewriteRope::size
unsigned size() const
Definition: RewriteRope.h:185
clang::RewriteBuffer::RemoveText
void RemoveText(unsigned OrigOffset, unsigned Size, bool removeLineIfEmpty=false)
RemoveText - Remove the specified text.
Definition: Rewriter.cpp:62
clang::RewriteBuffer::InsertTextBefore
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
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::RopePieceBTreeIterator
RopePieceBTreeIterator - This class provides read-only forward iteration over bytes that are in a Rop...
Definition: RewriteRope.h:87
clang::RewriteRope::assign
void assign(const char *Start, const char *End)
Definition: RewriteRope.h:191
clang::RewriteBuffer::size
unsigned size() const
Definition: RewriteBuffer.h:40
RewriteRope.h
clang::Rewriter
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:33
DeltaTree.h