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