clang: lib/Rewrite/Rewriter.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
20#include "llvm/ADT/RewriteBuffer.h"
21#include "llvm/ADT/RewriteRope.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/Error.h"
25#include "llvm/Support/IOSandbox.h"
26#include "llvm/Support/raw_ostream.h"
27#include
28#include
29#include
30#include
31
32using namespace clang;
33using llvm::RewriteBuffer;
34
35
36
37
38
39
40
42 return c == ' ' || c == '\t' || c == '\f' || c == '\v' || c == '\r';
43}
44
45
46
51
52 FileID StartFileID, EndFileID;
53 unsigned StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
54 unsigned EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
55
56 if (StartFileID != EndFileID)
57 return -1;
58
59
60
61 std::map<FileID, RewriteBuffer>::const_iterator I =
62 RewriteBuffers.find(StartFileID);
63 if (I != RewriteBuffers.end()) {
64 const RewriteBuffer &RB = I->second;
67 }
68
69
70
71 if (Range.isTokenRange())
73
74 return EndOff-StartOff;
75}
76
80
81
82
83
84
85
89 return {};
90
91 FileID StartFileID, EndFileID;
92 unsigned StartOff, EndOff;
93 StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
94 EndOff = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
95
96 if (StartFileID != EndFileID)
97 return {};
98
99
100
101 std::map<FileID, RewriteBuffer>::const_iterator I =
102 RewriteBuffers.find(StartFileID);
103 if (I == RewriteBuffers.end()) {
104
105 const char *Ptr = SourceMgr->getCharacterData(Range.getBegin());
106
107
108
109 if (Range.isTokenRange())
110 EndOff +=
112 return std::string(Ptr, Ptr+EndOff-StartOff);
113 }
114
115 const RewriteBuffer &RB = I->second;
116 EndOff = RB.getMappedOffset(EndOff, true);
117 StartOff = RB.getMappedOffset(StartOff);
118
119
120
121 if (Range.isTokenRange())
123
124
125 RewriteBuffer::iterator Start = RB.begin();
126 std::advance(Start, StartOff);
127 RewriteBuffer::iterator End = Start;
128 assert(EndOff >= StartOff && "Invalid iteration distance");
129 std::advance(End, EndOff-StartOff);
130
131 return std::string(Start, End);
132}
133
134unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
136 assert(Loc.isValid() && "Invalid location");
138 FID = V.first;
139 return V.second;
140}
141
142
144 std::map<FileID, RewriteBuffer>::iterator I =
145 RewriteBuffers.lower_bound(FID);
146 if (I != RewriteBuffers.end() && I->first == FID)
147 return I->second;
148 I = RewriteBuffers.insert(I, std::make_pair(FID, RewriteBuffer()));
149
150 StringRef MB = SourceMgr->getBufferData(FID);
151 I->second.Initialize(MB.begin(), MB.end());
152
153 return I->second;
154}
155
156
157
159 bool InsertAfter, bool indentNewLines) {
162 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
163
165 if (indentNewLines && Str.contains('\n')) {
166 StringRef MB = SourceMgr->getBufferData(FID);
167
168 unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1;
170 &SourceMgr->getSLocEntry(FID).getFile().getContentCache();
172
173
174 StringRef indentSpace;
175 {
176 unsigned i = lineOffs;
178 ++i;
179 indentSpace = MB.substr(lineOffs, i-lineOffs);
180 }
181
183 Str.split(lines, "\n");
184
185 for (unsigned i = 0, e = lines.size(); i != e; ++i) {
186 indentedStr += lines[i];
187 if (i < e-1) {
188 indentedStr += '\n';
189 indentedStr += indentSpace;
190 }
191 }
192 Str = indentedStr.str();
193 }
194
195 getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter);
196 return false;
197}
198
202 unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
206 getEditBuffer(FID).InsertText(StartOffs, Str, true);
207 return false;
208}
209
210
215 unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
217 return false;
218}
219
220
221
222
224 StringRef NewStr) {
227 unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
228
229 getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
230 return false;
231}
232
234 if ((range.getBegin())) return true;
235 if ((range.getEnd())) return true;
236 if (replacementRange.isInvalid()) return true;
239 unsigned newLength = getRangeSize(replacementRange);
241 unsigned newOffs = getLocationOffsetAndFileID(replacementRange.getBegin(),
242 FID);
243 StringRef MB = SourceMgr->getBufferData(FID);
244 return ReplaceText(start, origLength, MB.substr(newOffs, newLength));
245}
246
249 if (range.isInvalid()) return true;
250 if ((range.getBegin())) return true;
251 if ((range.getEnd())) return true;
252 if ((parentIndent)) return true;
253
254 FileID StartFileID, EndFileID, parentFileID;
255 unsigned StartOff, EndOff, parentOff;
256
257 StartOff = getLocationOffsetAndFileID(range.getBegin(), StartFileID);
258 EndOff = getLocationOffsetAndFileID(range.getEnd(), EndFileID);
259 parentOff = getLocationOffsetAndFileID(parentIndent, parentFileID);
260
261 if (StartFileID != EndFileID || StartFileID != parentFileID)
262 return true;
263 if (StartOff > EndOff)
264 return true;
265
266 FileID FID = StartFileID;
267 StringRef MB = SourceMgr->getBufferData(FID);
268
269 unsigned parentLineNo = SourceMgr->getLineNumber(FID, parentOff) - 1;
270 unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1;
271 unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1;
272
274 &SourceMgr->getSLocEntry(FID).getFile().getContentCache();
275
276
277 unsigned parentLineOffs = Content->SourceLineCache[parentLineNo];
278 unsigned startLineOffs = Content->SourceLineCache[startLineNo];
279
280
281 StringRef parentSpace, startSpace;
282 {
283 unsigned i = parentLineOffs;
285 ++i;
286 parentSpace = MB.substr(parentLineOffs, i-parentLineOffs);
287
288 i = startLineOffs;
290 ++i;
291 startSpace = MB.substr(startLineOffs, i-startLineOffs);
292 }
293 if (parentSpace.size() >= startSpace.size())
294 return true;
295 if (!startSpace.starts_with(parentSpace))
296 return true;
297
298 StringRef indent = startSpace.substr(parentSpace.size());
299
300
302 for (unsigned lineNo = startLineNo; lineNo <= endLineNo; ++lineNo) {
304 unsigned i = offs;
306 ++i;
307 StringRef origIndent = MB.substr(offs, i-offs);
308 if (origIndent.starts_with(startSpace))
309 RB.InsertText(offs, indent, false);
310 }
311
312 return false;
313}
314
316 bool AllWritten = true;
318 unsigned OverwriteFailure = Diag.getCustomDiagID(
324
325 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
326 if (auto Error = llvm::writeToOutput(Path, [&](llvm::raw_ostream &OS) {
327 I->second.write(OS);
328 return llvm::Error::success();
329 })) {
330 Diag.Report(OverwriteFailure)
331 << Entry->getName() << llvm::toString(std::move(Error));
332 AllWritten = false;
333 }
334 }
335 return !AllWritten;
336}
Defines the Diagnostic-related interfaces.
Defines the Diagnostic IDs-related interfaces.
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
static bool isWhitespaceExceptNL(unsigned char c)
Return true if this character is non-new-line whitespace: ' ', '\t', '\f', '\v', '\r'.
Definition Rewriter.cpp:41
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
__device__ __2f16 float c
Represents a character-granular source range.
static CharSourceRange getTokenRange(SourceRange R)
StringRef getName() const
The name of this FileEntry.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
bool makeAbsolutePath(SmallVectorImpl< char > &Path) const
Makes Path absolute taking into account FileSystemOptions and the working directory option.
static unsigned MeasureTokenLength(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
MeasureTokenLength - Relex the token at the specified location and return its length in bytes in the ...
std::map< FileID, llvm::RewriteBuffer >::iterator buffer_iterator
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:77
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:158
bool RemoveText(SourceLocation Start, unsigned Length, RewriteOptions opts=RewriteOptions())
RemoveText - Remove the specified text region.
Definition Rewriter.cpp:211
static bool isRewritable(SourceLocation Loc)
isRewritable - Return true if this location is a raw file location, which is rewritable.
buffer_iterator buffer_end()
SourceManager & getSourceMgr() const
buffer_iterator buffer_begin()
std::string getRewrittenText(CharSourceRange Range) const
getRewrittenText - Return the rewritten form of the text in the specified range.
Definition Rewriter.cpp:86
bool IncreaseIndentation(CharSourceRange range, SourceLocation parentIndent)
Increase indentation for the lines between the given source range.
Definition Rewriter.cpp:247
bool InsertTextAfterToken(SourceLocation Loc, StringRef Str)
Insert the specified string after the token in the specified location.
Definition Rewriter.cpp:199
llvm::RewriteBuffer & getEditBuffer(FileID FID)
getEditBuffer - This is like getRewriteBufferFor, but always returns a buffer, and allows you to writ...
Definition Rewriter.cpp:143
bool overwriteChangedFiles()
overwriteChangedFiles - Save all changed files to disk.
Definition Rewriter.cpp:315
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:223
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
DiagnosticsEngine & getDiagnostics() const
OptionalFileEntryRef getFileEntryRefForID(FileID FID) const
Returns the FileEntryRef for the provided FileID.
FileManager & getFileManager() const
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
One instance of this struct is kept for every file loaded or used.
LineOffsetMapping SourceLineCache
A bump pointer allocated array of offsets for each source line.
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
std::pair< FileID, unsigned > FileIDAndOffset
bool IncludeInsertsAtBeginOfRange
Given a source range, true to include previous inserts at the beginning of the range as part of the r...
bool IncludeInsertsAtEndOfRange
Given a source range, true to include previous inserts at the end of the range as part of the range i...
bool RemoveLineIfEmpty
If true and removing some text leaves a blank line also remove the empty line (false by default).