clang 8.0.0

Rewriter.h
Go to the documentation of this file.
1 //===- Rewriter.h - Code 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 // This file defines the Rewriter class, which is used for code
11 // transformations.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_REWRITE_CORE_REWRITER_H
16 #define LLVM_CLANG_REWRITE_CORE_REWRITER_H
17
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/Rewrite/Core/RewriteBuffer.h"
21 #include "llvm/ADT/StringRef.h"
22 #include <map>
23 #include <string>
24
25 namespace clang {
26
27 class LangOptions;
28 class SourceManager;
29
30 /// Rewriter - This is the main interface to the rewrite buffers. Its primary
31 /// job is to dispatch high-level requests to the low-level RewriteBuffers that
32 /// are involved.
33 class Rewriter {
34 SourceManager *SourceMgr = nullptr;
35 const LangOptions *LangOpts = nullptr;
36 std::map<FileID, RewriteBuffer> RewriteBuffers;
37
38 public:
39 struct RewriteOptions {
40 /// Given a source range, true to include previous inserts at the
41 /// beginning of the range as part of the range itself (true by default).
42 bool IncludeInsertsAtBeginOfRange = true;
43
44 /// Given a source range, true to include previous inserts at the
45 /// end of the range as part of the range itself (true by default).
46 bool IncludeInsertsAtEndOfRange = true;
47
48 /// If true and removing some text leaves a blank line
49 /// also remove the empty line (false by default).
50 bool RemoveLineIfEmpty = false;
51
52 RewriteOptions() {}
53 };
54
55 using buffer_iterator = std::map<FileID, RewriteBuffer>::iterator;
56 using const_buffer_iterator = std::map<FileID, RewriteBuffer>::const_iterator;
57
58 explicit Rewriter() = default;
59 explicit Rewriter(SourceManager &SM, const LangOptions &LO)
60 : SourceMgr(&SM), LangOpts(&LO) {}
61
62 void setSourceMgr(SourceManager &SM, const LangOptions &LO) {
63 SourceMgr = &SM;
64 LangOpts = &LO;
65 }
66
67 SourceManager &getSourceMgr() const { return *SourceMgr; }
68 const LangOptions &getLangOpts() const { return *LangOpts; }
69
70 /// isRewritable - Return true if this location is a raw file location, which
71 /// is rewritable. Locations from macros, etc are not rewritable.
72 static bool isRewritable(SourceLocation Loc) {
73 return Loc.isFileID();
74 }
75
76 /// getRangeSize - Return the size in bytes of the specified range if they
77 /// are in the same file. If not, this returns -1.
78 int getRangeSize(SourceRange Range,
79 RewriteOptions opts = RewriteOptions()) const;
80 int getRangeSize(const CharSourceRange &Range,
81 RewriteOptions opts = RewriteOptions()) const;
82
83 /// getRewrittenText - Return the rewritten form of the text in the specified
84 /// range. If the start or end of the range was unrewritable or if they are
85 /// in different buffers, this returns an empty string.
86 ///
87 /// Note that this method is not particularly efficient.
88 std::string getRewrittenText(SourceRange Range) const;
89
90 /// InsertText - Insert the specified string at the specified location in the
91 /// original buffer. This method returns true (and does nothing) if the input
92 /// location was not rewritable, false otherwise.
93 ///
94 /// \param indentNewLines if true new lines in the string are indented
95 /// using the indentation of the source line in position \p Loc.
96 bool InsertText(SourceLocation Loc, StringRef Str,
97 bool InsertAfter = true, bool indentNewLines = false);
98
99 /// InsertTextAfter - Insert the specified string at the specified location in
100 /// the original buffer. This method returns true (and does nothing) if
101 /// the input location was not rewritable, false otherwise. Text is
102 /// inserted after any other text that has been previously inserted
103 /// at the some point (the default behavior for InsertText).
104 bool InsertTextAfter(SourceLocation Loc, StringRef Str) {
105 return InsertText(Loc, Str);
106 }
107
108 /// Insert the specified string after the token in the
109 /// specified location.
110 bool InsertTextAfterToken(SourceLocation Loc, StringRef Str);
111
112 /// InsertText - Insert the specified string at the specified location in the
113 /// original buffer. This method returns true (and does nothing) if the input
114 /// location was not rewritable, false otherwise. Text is
115 /// inserted before any other text that has been previously inserted
116 /// at the some point.
117 bool InsertTextBefore(SourceLocation Loc, StringRef Str) {
118 return InsertText(Loc, Str, false);
119 }
120
121 /// RemoveText - Remove the specified text region.
122 bool RemoveText(SourceLocation Start, unsigned Length,
123 RewriteOptions opts = RewriteOptions());
124
125 /// Remove the specified text region.
126 bool RemoveText(CharSourceRange range,
127 RewriteOptions opts = RewriteOptions()) {
128 return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
129 }
130
131 /// Remove the specified text region.
132 bool RemoveText(SourceRange range, RewriteOptions opts = RewriteOptions()) {
133 return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
134 }
135
136 /// ReplaceText - This method replaces a range of characters in the input
137 /// buffer with a new string. This is effectively a combined "remove/insert"
138 /// operation.
139 bool ReplaceText(SourceLocation Start, unsigned OrigLength,
140 StringRef NewStr);
141
142 /// ReplaceText - This method replaces a range of characters in the input
143 /// buffer with a new string. This is effectively a combined "remove/insert"
144 /// operation.
145 bool ReplaceText(SourceRange range, StringRef NewStr) {
146 return ReplaceText(range.getBegin(), getRangeSize(range), NewStr);
147 }
148
149 /// ReplaceText - This method replaces a range of characters in the input
150 /// buffer with a new string. This is effectively a combined "remove/insert"
151 /// operation.
152 bool ReplaceText(SourceRange range, SourceRange replacementRange);
153
154 /// Increase indentation for the lines between the given source range.
155 /// To determine what the indentation should be, 'parentIndent' is used
156 /// that should be at a source location with an indentation one degree
157 /// lower than the given range.
158 bool IncreaseIndentation(CharSourceRange range, SourceLocation parentIndent);
159 bool IncreaseIndentation(SourceRange range, SourceLocation parentIndent) {
160 return IncreaseIndentation(CharSourceRange::getTokenRange(range),
161 parentIndent);
162 }
163
164 /// getEditBuffer - This is like getRewriteBufferFor, but always returns a
165 /// buffer, and allows you to write on it directly. This is useful if you
166 /// want efficient low-level access to apis for scribbling on one specific
167 /// FileID's buffer.
168 RewriteBuffer &getEditBuffer(FileID FID);
169
170 /// getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
171 /// If no modification has been made to it, return null.
172 const RewriteBuffer *getRewriteBufferFor(FileID FID) const {
173 std::map<FileID, RewriteBuffer>::const_iterator I =
174 RewriteBuffers.find(FID);
175 return I == RewriteBuffers.end() ? nullptr : &I->second;
176 }
177
178 // Iterators over rewrite buffers.
179 buffer_iterator buffer_begin() { return RewriteBuffers.begin(); }
180 buffer_iterator buffer_end() { return RewriteBuffers.end(); }
181 const_buffer_iterator buffer_begin() const { return RewriteBuffers.begin(); }
182 const_buffer_iterator buffer_end() const { return RewriteBuffers.end(); }
183
184 /// overwriteChangedFiles - Save all changed files to disk.
185 ///
186 /// Returns true if any files were not saved successfully.
187 /// Outputs diagnostics via the source manager's diagnostic engine
188 /// in case of an error.
189 bool overwriteChangedFiles();
190
191 private:
192 unsigned getLocationOffsetAndFileID(SourceLocation Loc, FileID &FID) const;
193 };
194
195 } // namespace clang
196
197 #endif // LLVM_CLANG_REWRITE_CORE_REWRITER_H
clang::Rewriter::IncreaseIndentation
bool IncreaseIndentation(CharSourceRange range, SourceLocation parentIndent)
Increase indentation for the lines between the given source range.
Definition: Rewriter.cpp:331
clang::Rewriter::const_buffer_iterator
std::map< FileID, RewriteBuffer >::const_iterator const_buffer_iterator
Definition: Rewriter.h:56
clang::Rewriter::buffer_end
const_buffer_iterator buffer_end() const
Definition: Rewriter.h:182
clang::CharSourceRange::getTokenRange
static CharSourceRange getTokenRange(SourceRange R)
Definition: SourceLocation.h:244
clang::Rewriter::RemoveText
bool RemoveText(SourceLocation Start, unsigned Length, RewriteOptions opts=RewriteOptions())
RemoveText - Remove the specified text region.
Definition: Rewriter.cpp:295
clang::Rewriter::RewriteOptions::RewriteOptions
RewriteOptions()
Definition: Rewriter.h:52
clang::Rewriter::getEditBuffer
RewriteBuffer & getEditBuffer(FileID FID)
getEditBuffer - This is like getRewriteBufferFor, but always returns a buffer, and allows you to writ...
Definition: Rewriter.cpp:227
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::Rewriter::getRewrittenText
std::string getRewrittenText(SourceRange Range) const
getRewrittenText - Return the rewritten form of the text in the specified range.
Definition: Rewriter.cpp:174
clang::CharSourceRange::getBegin
SourceLocation getBegin() const
Definition: SourceLocation.h:266
clang::Rewriter::RewriteOptions
Definition: Rewriter.h:39
clang::Rewriter::ReplaceText
bool ReplaceText(SourceLocation Start, unsigned OrigLength, StringRef NewStr)
ReplaceText - This method replaces a range of characters in the input buffer with a new string...
Definition: Rewriter.cpp:307
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
RewriteBuffer.h
clang::Rewriter::Rewriter
Rewriter(SourceManager &SM, const LangOptions &LO)
Definition: Rewriter.h:59
clang::Rewriter::getSourceMgr
SourceManager & getSourceMgr() const
Definition: Rewriter.h:67
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::SourceLocation::isFileID
bool isFileID() const
Definition: SourceLocation.h:100
clang::Rewriter::Rewriter
Rewriter()=default
clang::Rewriter::buffer_end
buffer_iterator buffer_end()
Definition: Rewriter.h:180
clang::CharSourceRange
Represents a character-granular source range.
Definition: SourceLocation.h:236
clang::Rewriter::RemoveText
bool RemoveText(SourceRange range, RewriteOptions opts=RewriteOptions())
Remove the specified text region.
Definition: Rewriter.h:132
clang::Rewriter::IncreaseIndentation
bool IncreaseIndentation(SourceRange range, SourceLocation parentIndent)
Definition: Rewriter.h:159
clang::Rewriter::getRangeSize
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...
Definition: Rewriter.cpp:165
clang::Rewriter::overwriteChangedFiles
bool overwriteChangedFiles()
overwriteChangedFiles - Save all changed files to disk.
Definition: Rewriter.cpp:452
SM
const SourceManager & SM
Definition: Format.cpp:1490
clang::Rewriter::buffer_begin
const_buffer_iterator buffer_begin() const
Definition: Rewriter.h:181
clang::Rewriter::setSourceMgr
void setSourceMgr(SourceManager &SM, const LangOptions &LO)
Definition: Rewriter.h:62
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::Rewriter::InsertTextAfter
bool InsertTextAfter(SourceLocation Loc, StringRef Str)
InsertTextAfter - Insert the specified string at the specified location in the original buffer...
Definition: Rewriter.h:104
clang::Rewriter::getRewriteBufferFor
const RewriteBuffer * getRewriteBufferFor(FileID FID) const
getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
Definition: Rewriter.h:172
clang::Rewriter::ReplaceText
bool ReplaceText(SourceRange range, StringRef NewStr)
ReplaceText - This method replaces a range of characters in the input buffer with a new string...
Definition: Rewriter.h:145
clang::Rewriter::InsertText
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...
Definition: Rewriter.cpp:242
clang::Rewriter::RewriteOptions::RemoveLineIfEmpty
bool RemoveLineIfEmpty
If true and removing some text leaves a blank line also remove the empty line (false by default)...
Definition: Rewriter.h:50
clang::Rewriter::buffer_iterator
std::map< FileID, RewriteBuffer >::iterator buffer_iterator
Definition: Rewriter.h:55
clang::Rewriter::InsertTextBefore
bool InsertTextBefore(SourceLocation Loc, StringRef Str)
InsertText - Insert the specified string at the specified location in the original buffer...
Definition: Rewriter.h:117
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:40
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::Rewriter::RemoveText
bool RemoveText(CharSourceRange range, RewriteOptions opts=RewriteOptions())
Remove the specified text region.
Definition: Rewriter.h:126
clang::Rewriter::getLangOpts
const LangOptions & getLangOpts() const
Definition: Rewriter.h:68
clang::Rewriter::RewriteOptions::IncludeInsertsAtBeginOfRange
bool IncludeInsertsAtBeginOfRange
Given a source range, true to include previous inserts at the beginning of the range as part of the r...
Definition: Rewriter.h:42
clang::Rewriter::buffer_begin
buffer_iterator buffer_begin()
Definition: Rewriter.h:179
clang::Rewriter
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:33
SourceLocation.h
Defines the clang::SourceLocation class and associated facilities.
clang::Rewriter::isRewritable
static bool isRewritable(SourceLocation Loc)
isRewritable - Return true if this location is a raw file location, which is rewritable.
Definition: Rewriter.h:72
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:198
clang::Rewriter::InsertTextAfterToken
bool InsertTextAfterToken(SourceLocation Loc, StringRef Str)
Insert the specified string after the token in the specified location.
Definition: Rewriter.cpp:283
clang::SourceRange::getBegin
SourceLocation getBegin() const
Definition: SourceLocation.h:207
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582
clang::Rewriter::RewriteOptions::IncludeInsertsAtEndOfRange
bool IncludeInsertsAtEndOfRange
Given a source range, true to include previous inserts at the end of the range as part of the range i...
Definition: Rewriter.h:46