Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Rewrite/Core/Rewriter.h
Line
Count
Source (jump to first uncovered line)
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"
18
#include "clang/Basic/SourceLocation.h"
19
#include "clang/Rewrite/Core/RewriteBuffer.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).
41
    bool IncludeInsertsAtBeginOfRange = true;
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).
45
    bool IncludeInsertsAtEndOfRange = true;
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
51
2.69k
    RewriteOptions() {}
52
  };
53
54
  using buffer_iterator = std::map<FileID, RewriteBuffer>::iterator;
55
  using const_buffer_iterator = std::map<FileID, RewriteBuffer>::const_iterator;
56
57
170
  explicit Rewriter() = default;
58
  explicit Rewriter(SourceManager &SM, const LangOptions &LO)
59
13.2k
      : SourceMgr(&SM), LangOpts(&LO) {}
60
61
168
  void setSourceMgr(SourceManager &SM, const LangOptions &LO) {
62
168
    SourceMgr = &SM;
63
168
    LangOpts = &LO;
64
168
  }
65
66
41.3k
  SourceManager &getSourceMgr() const { return *SourceMgr; }
67
704
  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
51.9k
  static bool isRewritable(SourceLocation Loc) {
72
51.9k
    return Loc.isFileID();
73
51.9k
  }
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
174
  std::string getRewrittenText(SourceRange Range) const {
95
174
    return getRewrittenText(CharSourceRange::getTokenRange(Range));
96
174
  }
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
88
  bool InsertTextAfter(SourceLocation Loc, StringRef Str) {
113
88
    return InsertText(Loc, Str);
114
88
  }
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
697
  bool InsertTextBefore(SourceLocation Loc, StringRef Str) {
126
697
    return InsertText(Loc, Str, false);
127
697
  }
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.
134
  bool RemoveText(CharSourceRange range,
135
402
                  RewriteOptions opts = RewriteOptions()) {
136
402
    return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
137
402
  }
138
139
  /// Remove the specified text region.
140
0
  bool RemoveText(SourceRange range, RewriteOptions opts = RewriteOptions()) {
141
0
    return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
142
0
  }
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
0
  bool IncreaseIndentation(SourceRange range, SourceLocation parentIndent) {
175
0
    return IncreaseIndentation(CharSourceRange::getTokenRange(range),
176
0
                               parentIndent);
177
0
  }
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.
183
  RewriteBuffer &getEditBuffer(FileID FID);
184
185
  /// getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
186
  /// If no modification has been made to it, return null.
187
259
  const RewriteBuffer *getRewriteBufferFor(FileID FID) const {
188
259
    std::map<FileID, RewriteBuffer>::const_iterator I =
189
259
      RewriteBuffers.find(FID);
190
259
    return I == RewriteBuffers.end() ? 
nullptr0
: &I->second;
191
259
  }
192
193
  // Iterators over rewrite buffers.
194
167
  buffer_iterator buffer_begin() { return RewriteBuffers.begin(); }
195
167
  buffer_iterator buffer_end() { return RewriteBuffers.end(); }
196
0
  const_buffer_iterator buffer_begin() const { return RewriteBuffers.begin(); }
197
0
  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