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(SourceRange Range) const;
88 
89  /// InsertText - Insert the specified string at the specified location in the
90  /// original buffer. This method returns true (and does nothing) if the input
91  /// location was not rewritable, false otherwise.
92  ///
93  /// \param indentNewLines if true new lines in the string are indented
94  /// using the indentation of the source line in position \p Loc.
95  bool InsertText(SourceLocation Loc, StringRef Str,
96  bool InsertAfter = true, bool indentNewLines = false);
97 
98  /// InsertTextAfter - Insert the specified string at the specified location in
99  /// the original buffer. This method returns true (and does nothing) if
100  /// the input location was not rewritable, false otherwise. Text is
101  /// inserted after any other text that has been previously inserted
102  /// at the some point (the default behavior for InsertText).
103  bool InsertTextAfter(SourceLocation Loc, StringRef Str) {
104  return InsertText(Loc, Str);
105  }
106 
107  /// Insert the specified string after the token in the
108  /// specified location.
109  bool InsertTextAfterToken(SourceLocation Loc, StringRef Str);
110 
111  /// InsertText - Insert the specified string at the specified location in the
112  /// original buffer. This method returns true (and does nothing) if the input
113  /// location was not rewritable, false otherwise. Text is
114  /// inserted before any other text that has been previously inserted
115  /// at the some point.
116  bool InsertTextBefore(SourceLocation Loc, StringRef Str) {
117  return InsertText(Loc, Str, false);
118  }
119 
120  /// RemoveText - Remove the specified text region.
121  bool RemoveText(SourceLocation Start, unsigned Length,
122  RewriteOptions opts = RewriteOptions());
123 
124  /// Remove the specified text region.
126  RewriteOptions opts = RewriteOptions()) {
127  return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
128  }
129 
130  /// Remove the specified text region.
132  return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
133  }
134 
135  /// ReplaceText - This method replaces a range of characters in the input
136  /// buffer with a new string. This is effectively a combined "remove/insert"
137  /// operation.
138  bool ReplaceText(SourceLocation Start, unsigned OrigLength,
139  StringRef NewStr);
140 
141  /// ReplaceText - This method replaces a range of characters in the input
142  /// buffer with a new string. This is effectively a combined "remove/insert"
143  /// operation.
144  bool ReplaceText(SourceRange range, StringRef NewStr) {
145  return ReplaceText(range.getBegin(), getRangeSize(range), NewStr);
146  }
147 
148  /// ReplaceText - This method replaces a range of characters in the input
149  /// buffer with a new string. This is effectively a combined "remove/insert"
150  /// operation.
151  bool ReplaceText(SourceRange range, SourceRange replacementRange);
152 
153  /// Increase indentation for the lines between the given source range.
154  /// To determine what the indentation should be, 'parentIndent' is used
155  /// that should be at a source location with an indentation one degree
156  /// lower than the given range.
157  bool IncreaseIndentation(CharSourceRange range, SourceLocation parentIndent);
158  bool IncreaseIndentation(SourceRange range, SourceLocation parentIndent) {
160  parentIndent);
161  }
162 
163  /// getEditBuffer - This is like getRewriteBufferFor, but always returns a
164  /// buffer, and allows you to write on it directly. This is useful if you
165  /// want efficient low-level access to apis for scribbling on one specific
166  /// FileID's buffer.
168 
169  /// getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
170  /// If no modification has been made to it, return null.
172  std::map<FileID, RewriteBuffer>::const_iterator I =
173  RewriteBuffers.find(FID);
174  return I == RewriteBuffers.end() ? nullptr : &I->second;
175  }
176 
177  // Iterators over rewrite buffers.
178  buffer_iterator buffer_begin() { return RewriteBuffers.begin(); }
179  buffer_iterator buffer_end() { return RewriteBuffers.end(); }
180  const_buffer_iterator buffer_begin() const { return RewriteBuffers.begin(); }
181  const_buffer_iterator buffer_end() const { return RewriteBuffers.end(); }
182 
183  /// overwriteChangedFiles - Save all changed files to disk.
184  ///
185  /// Returns true if any files were not saved successfully.
186  /// Outputs diagnostics via the source manager's diagnostic engine
187  /// in case of an error.
188  bool overwriteChangedFiles();
189 
190 private:
191  unsigned getLocationOffsetAndFileID(SourceLocation Loc, FileID &FID) const;
192 };
193 
194 } // namespace clang
195 
196 #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:330
std::map< FileID, RewriteBuffer >::const_iterator const_buffer_iterator
Definition: Rewriter.h:55
const_buffer_iterator buffer_end() const
Definition: Rewriter.h:181
static CharSourceRange getTokenRange(SourceRange R)
bool RemoveText(SourceLocation Start, unsigned Length, RewriteOptions opts=RewriteOptions())
RemoveText - Remove the specified text region.
Definition: Rewriter.cpp:294
RewriteBuffer & getEditBuffer(FileID FID)
getEditBuffer - This is like getRewriteBufferFor, but always returns a buffer, and allows you to writ...
Definition: Rewriter.cpp:226
RewriteBuffer - As code is rewritten, SourceBuffer&#39;s from the original input with modifications get a...
Definition: RewriteBuffer.h:25
iterator end() const
Definition: RewriteBuffer.h:38
std::string getRewrittenText(SourceRange Range) const
getRewrittenText - Return the rewritten form of the text in the specified range.
Definition: Rewriter.cpp:173
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:306
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:49
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:179
Represents a character-granular source range.
bool RemoveText(SourceRange range, RewriteOptions opts=RewriteOptions())
Remove the specified text region.
Definition: Rewriter.h:131
bool IncreaseIndentation(SourceRange range, SourceLocation parentIndent)
Definition: Rewriter.h:158
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:451
const SourceManager & SM
Definition: Format.cpp:1568
const_buffer_iterator buffer_begin() const
Definition: Rewriter.h:180
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:103
const RewriteBuffer * getRewriteBufferFor(FileID FID) const
getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
Definition: Rewriter.h:171
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:144
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:241
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:116
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:125
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:178
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
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:282
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