1 //===----- EditedSource.cpp - Collection of source edits ------------------===//
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 #include "clang/Edit/EditedSource.h"
11 #include "clang/Edit/Commit.h"
12 #include "clang/Edit/EditsReceiver.h"
13 #include "clang/Lex/Lexer.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
18 using namespace clang;
21 void EditsReceiver::remove(CharSourceRange range) {
22 replace(range, StringRef());
25 StringRef EditedSource::copyString(const Twine &twine) {
26 llvm::SmallString<128> Data;
27 return copyString(twine.toStringRef(Data));
30 bool EditedSource::canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs) {
31 FileEditsTy::iterator FA = getActionForOffset(Offs);
32 if (FA != FileEdits.end()) {
33 if (FA->first != Offs)
34 return false; // position has been removed.
37 if (SourceMgr.isMacroArgExpansion(OrigLoc)) {
39 DefArgLoc = SourceMgr.getImmediateExpansionRange(OrigLoc).first;
41 ExpLoc = SourceMgr.getImmediateExpansionRange(DefArgLoc).first;
42 llvm::DenseMap<unsigned, SourceLocation>::iterator
43 I = ExpansionToArgMap.find(ExpLoc.getRawEncoding());
44 if (I != ExpansionToArgMap.end() && I->second != DefArgLoc)
45 return false; // Trying to write in a macro argument input that has
46 // already been written for another argument of the same macro.
52 bool EditedSource::commitInsert(SourceLocation OrigLoc,
53 FileOffset Offs, StringRef text,
54 bool beforePreviousInsertions) {
55 if (!canInsertInOffset(OrigLoc, Offs))
60 if (SourceMgr.isMacroArgExpansion(OrigLoc)) {
62 DefArgLoc = SourceMgr.getImmediateExpansionRange(OrigLoc).first;
64 ExpLoc = SourceMgr.getImmediateExpansionRange(DefArgLoc).first;
65 ExpansionToArgMap[ExpLoc.getRawEncoding()] = DefArgLoc;
68 FileEdit &FA = FileEdits[Offs];
69 if (FA.Text.empty()) {
70 FA.Text = copyString(text);
75 if (beforePreviousInsertions)
76 concat = Twine(text) + FA.Text;
78 concat = Twine(FA.Text) + text;
80 FA.Text = copyString(concat);
84 bool EditedSource::commitInsertFromRange(SourceLocation OrigLoc,
86 FileOffset InsertFromRangeOffs, unsigned Len,
87 bool beforePreviousInsertions) {
91 llvm::SmallString<128> StrVec;
92 FileOffset BeginOffs = InsertFromRangeOffs;
93 FileOffset EndOffs = BeginOffs.getWithOffset(Len);
94 FileEditsTy::iterator I = FileEdits.upper_bound(BeginOffs);
95 if (I != FileEdits.begin())
98 for (; I != FileEdits.end(); ++I) {
99 FileEdit &FA = I->second;
100 FileOffset B = I->first;
101 FileOffset E = B.getWithOffset(FA.RemoveLen);
104 if (BeginOffs >= B) {
112 for (; I != FileEdits.end() && EndOffs > I->first; ++I) {
113 FileEdit &FA = I->second;
114 FileOffset B = I->first;
115 FileOffset E = B.getWithOffset(FA.RemoveLen);
118 bool Invalid = false;
119 StringRef text = getSourceText(BeginOffs, B, Invalid);
128 if (BeginOffs < EndOffs) {
129 bool Invalid = false;
130 StringRef text = getSourceText(BeginOffs, EndOffs, Invalid);
136 return commitInsert(OrigLoc, Offs, StrVec.str(), beforePreviousInsertions);
139 void EditedSource::commitRemove(SourceLocation OrigLoc,
140 FileOffset BeginOffs, unsigned Len) {
144 FileOffset EndOffs = BeginOffs.getWithOffset(Len);
145 FileEditsTy::iterator I = FileEdits.upper_bound(BeginOffs);
146 if (I != FileEdits.begin())
149 for (; I != FileEdits.end(); ++I) {
150 FileEdit &FA = I->second;
151 FileOffset B = I->first;
152 FileOffset E = B.getWithOffset(FA.RemoveLen);
158 FileOffset TopBegin, TopEnd;
161 if (I == FileEdits.end()) {
162 FileEditsTy::iterator
163 NewI = FileEdits.insert(I, std::make_pair(BeginOffs, FileEdit()));
164 NewI->second.RemoveLen = Len;
168 FileEdit &FA = I->second;
169 FileOffset B = I->first;
170 FileOffset E = B.getWithOffset(FA.RemoveLen);
172 FileEditsTy::iterator
173 NewI = FileEdits.insert(I, std::make_pair(BeginOffs, FileEdit()));
174 TopBegin = BeginOffs;
176 TopFA = &NewI->second;
177 TopFA->RemoveLen = Len;
182 if (TopEnd >= EndOffs)
184 unsigned diff = EndOffs.getOffset() - TopEnd.getOffset();
186 TopFA->RemoveLen += diff;
190 while (I != FileEdits.end()) {
191 FileEdit &FA = I->second;
192 FileOffset B = I->first;
193 FileOffset E = B.getWithOffset(FA.RemoveLen);
199 FileEdits.erase(I++);
204 unsigned diff = E.getOffset() - TopEnd.getOffset();
206 TopFA->RemoveLen += diff;
214 bool EditedSource::commit(const Commit &commit) {
215 if (!commit.isCommitable())
218 for (edit::Commit::edit_iterator
219 I = commit.edit_begin(), E = commit.edit_end(); I != E; ++I) {
220 const edit::Commit::Edit &edit = *I;
222 case edit::Commit::Act_Insert:
223 commitInsert(edit.OrigLoc, edit.Offset, edit.Text, edit.BeforePrev);
225 case edit::Commit::Act_InsertFromRange:
226 commitInsertFromRange(edit.OrigLoc, edit.Offset,
227 edit.InsertFromRangeOffs, edit.Length,
230 case edit::Commit::Act_Remove:
231 commitRemove(edit.OrigLoc, edit.Offset, edit.Length);
239 static void applyRewrite(EditsReceiver &receiver,
240 StringRef text, FileOffset offs, unsigned len,
241 const SourceManager &SM) {
242 assert(!offs.getFID().isInvalid());
243 SourceLocation Loc = SM.getLocForStartOfFile(offs.getFID());
244 Loc = Loc.getLocWithOffset(offs.getOffset());
245 assert(Loc.isFileID());
246 CharSourceRange range = CharSourceRange::getCharRange(Loc,
247 Loc.getLocWithOffset(len));
251 receiver.remove(range);
256 receiver.replace(range, text);
258 receiver.insert(Loc, text);
261 void EditedSource::applyRewrites(EditsReceiver &receiver) {
262 llvm::SmallString<128> StrVec;
263 FileOffset CurOffs, CurEnd;
266 if (FileEdits.empty())
269 FileEditsTy::iterator I = FileEdits.begin();
271 StrVec = I->second.Text;
272 CurLen = I->second.RemoveLen;
273 CurEnd = CurOffs.getWithOffset(CurLen);
276 for (FileEditsTy::iterator E = FileEdits.end(); I != E; ++I) {
277 FileOffset offs = I->first;
278 FileEdit act = I->second;
279 assert(offs >= CurEnd);
281 if (offs == CurEnd) {
283 CurLen += act.RemoveLen;
284 CurEnd.getWithOffset(act.RemoveLen);
288 applyRewrite(receiver, StrVec.str(), CurOffs, CurLen, SourceMgr);
291 CurLen = act.RemoveLen;
292 CurEnd = CurOffs.getWithOffset(CurLen);
295 applyRewrite(receiver, StrVec.str(), CurOffs, CurLen, SourceMgr);
298 void EditedSource::clearRewrites() {
303 StringRef EditedSource::getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
305 assert(BeginOffs.getFID() == EndOffs.getFID());
306 assert(BeginOffs <= EndOffs);
307 SourceLocation BLoc = SourceMgr.getLocForStartOfFile(BeginOffs.getFID());
308 BLoc = BLoc.getLocWithOffset(BeginOffs.getOffset());
309 assert(BLoc.isFileID());
311 ELoc = BLoc.getLocWithOffset(EndOffs.getOffset() - BeginOffs.getOffset());
312 return Lexer::getSourceText(CharSourceRange::getCharRange(BLoc, ELoc),
313 SourceMgr, LangOpts, &Invalid);
316 EditedSource::FileEditsTy::iterator
317 EditedSource::getActionForOffset(FileOffset Offs) {
318 FileEditsTy::iterator I = FileEdits.upper_bound(Offs);
319 if (I == FileEdits.begin())
320 return FileEdits.end();
322 FileEdit &FA = I->second;
323 FileOffset B = I->first;
324 FileOffset E = B.getWithOffset(FA.RemoveLen);
325 if (Offs >= B && Offs < E)
328 return FileEdits.end();