1 //===--- Rewriter.h - Code rewriting interface ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Rewriter class, which is used for code
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_REWRITE_CORE_REWRITER_H
16 #define LLVM_CLANG_REWRITE_CORE_REWRITER_H
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Rewrite/Core/RewriteBuffer.h"
28 /// Rewriter - This is the main interface to the rewrite buffers. Its primary
29 /// job is to dispatch high-level requests to the low-level RewriteBuffers that
32 SourceManager *SourceMgr;
33 const LangOptions *LangOpts;
34 std::map<FileID, RewriteBuffer> RewriteBuffers;
36 struct RewriteOptions {
37 /// \brief Given a source range, true to include previous inserts at the
38 /// beginning of the range as part of the range itself (true by default).
39 bool IncludeInsertsAtBeginOfRange;
40 /// \brief Given a source range, true to include previous inserts at the
41 /// end of the range as part of the range itself (true by default).
42 bool IncludeInsertsAtEndOfRange;
43 /// \brief If true and removing some text leaves a blank line
44 /// also remove the empty line (false by default).
45 bool RemoveLineIfEmpty;
48 : IncludeInsertsAtBeginOfRange(true),
49 IncludeInsertsAtEndOfRange(true),
50 RemoveLineIfEmpty(false) { }
53 typedef std::map<FileID, RewriteBuffer>::iterator buffer_iterator;
54 typedef std::map<FileID, RewriteBuffer>::const_iterator const_buffer_iterator;
56 explicit Rewriter(SourceManager &SM, const LangOptions &LO)
57 : SourceMgr(&SM), LangOpts(&LO) {}
58 explicit Rewriter() : SourceMgr(nullptr), LangOpts(nullptr) {}
60 void setSourceMgr(SourceManager &SM, const LangOptions &LO) {
64 SourceManager &getSourceMgr() const { return *SourceMgr; }
65 const LangOptions &getLangOpts() const { return *LangOpts; }
67 /// isRewritable - Return true if this location is a raw file location, which
68 /// is rewritable. Locations from macros, etc are not rewritable.
69 static bool isRewritable(SourceLocation Loc) {
70 return Loc.isFileID();
73 /// getRangeSize - Return the size in bytes of the specified range if they
74 /// are in the same file. If not, this returns -1.
75 int getRangeSize(SourceRange Range,
76 RewriteOptions opts = RewriteOptions()) const;
77 int getRangeSize(const CharSourceRange &Range,
78 RewriteOptions opts = RewriteOptions()) const;
80 /// getRewrittenText - Return the rewritten form of the text in the specified
81 /// range. If the start or end of the range was unrewritable or if they are
82 /// in different buffers, this returns an empty string.
84 /// Note that this method is not particularly efficient.
86 std::string getRewrittenText(SourceRange Range) const;
88 /// InsertText - Insert the specified string at the specified location in the
89 /// original buffer. This method returns true (and does nothing) if the input
90 /// location was not rewritable, false otherwise.
92 /// \param indentNewLines if true new lines in the string are indented
93 /// using the indentation of the source line in position \p Loc.
94 bool InsertText(SourceLocation Loc, StringRef Str,
95 bool InsertAfter = true, bool indentNewLines = false);
97 /// InsertTextAfter - Insert the specified string at the specified location in
98 /// the original buffer. This method returns true (and does nothing) if
99 /// the input location was not rewritable, false otherwise. Text is
100 /// inserted after any other text that has been previously inserted
101 /// at the some point (the default behavior for InsertText).
102 bool InsertTextAfter(SourceLocation Loc, StringRef Str) {
103 return InsertText(Loc, Str);
106 /// \brief Insert the specified string after the token in the
107 /// specified location.
108 bool InsertTextAfterToken(SourceLocation Loc, StringRef Str);
110 /// InsertText - Insert the specified string at the specified location in the
111 /// original buffer. This method returns true (and does nothing) if the input
112 /// location was not rewritable, false otherwise. Text is
113 /// inserted before any other text that has been previously inserted
114 /// at the some point.
115 bool InsertTextBefore(SourceLocation Loc, StringRef Str) {
116 return InsertText(Loc, Str, false);
119 /// RemoveText - Remove the specified text region.
120 bool RemoveText(SourceLocation Start, unsigned Length,
121 RewriteOptions opts = RewriteOptions());
123 /// \brief Remove the specified text region.
124 bool RemoveText(CharSourceRange range,
125 RewriteOptions opts = RewriteOptions()) {
126 return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
129 /// \brief Remove the specified text region.
130 bool RemoveText(SourceRange range, RewriteOptions opts = RewriteOptions()) {
131 return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
134 /// ReplaceText - This method replaces a range of characters in the input
135 /// buffer with a new string. This is effectively a combined "remove/insert"
137 bool ReplaceText(SourceLocation Start, unsigned OrigLength,
140 /// ReplaceText - This method replaces a range of characters in the input
141 /// buffer with a new string. This is effectively a combined "remove/insert"
143 bool ReplaceText(SourceRange range, StringRef NewStr) {
144 return ReplaceText(range.getBegin(), getRangeSize(range), NewStr);
147 /// ReplaceText - This method replaces a range of characters in the input
148 /// buffer with a new string. This is effectively a combined "remove/insert"
150 bool ReplaceText(SourceRange range, SourceRange replacementRange);
152 /// \brief Increase indentation for the lines between the given source range.
153 /// To determine what the indentation should be, 'parentIndent' is used
154 /// that should be at a source location with an indentation one degree
155 /// lower than the given range.
156 bool IncreaseIndentation(CharSourceRange range, SourceLocation parentIndent);
157 bool IncreaseIndentation(SourceRange range, SourceLocation parentIndent) {
158 return IncreaseIndentation(CharSourceRange::getTokenRange(range),
162 /// getEditBuffer - This is like getRewriteBufferFor, but always returns a
163 /// buffer, and allows you to write on it directly. This is useful if you
164 /// want efficient low-level access to apis for scribbling on one specific
166 RewriteBuffer &getEditBuffer(FileID FID);
168 /// getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
169 /// If no modification has been made to it, return null.
170 const RewriteBuffer *getRewriteBufferFor(FileID FID) const {
171 std::map<FileID, RewriteBuffer>::const_iterator I =
172 RewriteBuffers.find(FID);
173 return I == RewriteBuffers.end() ? nullptr : &I->second;
176 // Iterators over rewrite buffers.
177 buffer_iterator buffer_begin() { return RewriteBuffers.begin(); }
178 buffer_iterator buffer_end() { return RewriteBuffers.end(); }
179 const_buffer_iterator buffer_begin() const { return RewriteBuffers.begin(); }
180 const_buffer_iterator buffer_end() const { return RewriteBuffers.end(); }
182 /// overwriteChangedFiles - Save all changed files to disk.
184 /// Returns true if any files were not saved successfully.
185 /// Outputs diagnostics via the source manager's diagnostic engine
186 /// in case of an error.
187 bool overwriteChangedFiles();
190 unsigned getLocationOffsetAndFileID(SourceLocation Loc, FileID &FID) const;
193 } // end namespace clang