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/Basic/CharInfo.h"
12 #include "clang/Basic/LLVM.h"
13 #include "clang/Basic/SourceLocation.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "clang/Edit/Commit.h"
16 #include "clang/Edit/EditsReceiver.h"
17 #include "clang/Edit/FileOffset.h"
18 #include "clang/Lex/Lexer.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
28 using namespace clang;
31 void EditsReceiver::remove(CharSourceRange range) {
32 replace(range, StringRef());
35 void EditedSource::deconstructMacroArgLoc(SourceLocation Loc,
36 SourceLocation &ExpansionLoc,
37 MacroArgUse &ArgUse) {
38 assert(SourceMgr.isMacroArgExpansion(Loc));
39 SourceLocation DefArgLoc =
40 SourceMgr.getImmediateExpansionRange(Loc).getBegin();
41 SourceLocation ImmediateExpansionLoc =
42 SourceMgr.getImmediateExpansionRange(DefArgLoc).getBegin();
43 ExpansionLoc = ImmediateExpansionLoc;
44 while (SourceMgr.isMacroBodyExpansion(ExpansionLoc))
46 SourceMgr.getImmediateExpansionRange(ExpansionLoc).getBegin();
48 StringRef ArgName = Lexer::getSpelling(SourceMgr.getSpellingLoc(DefArgLoc),
49 Buf, SourceMgr, LangOpts);
50 ArgUse = MacroArgUse{nullptr, SourceLocation(), SourceLocation()};
52 ArgUse = {&IdentTable.get(ArgName), ImmediateExpansionLoc,
53 SourceMgr.getSpellingLoc(DefArgLoc)};
56 void EditedSource::startingCommit() {}
58 void EditedSource::finishedCommit() {
59 for (auto &ExpArg : CurrCommitMacroArgExps) {
60 SourceLocation ExpLoc;
62 std::tie(ExpLoc, ArgUse) = ExpArg;
63 auto &ArgUses = ExpansionToArgMap[ExpLoc.getRawEncoding()];
64 if (std::find(ArgUses.begin(), ArgUses.end(), ArgUse) == ArgUses.end())
65 ArgUses.push_back(ArgUse);
67 CurrCommitMacroArgExps.clear();
70 StringRef EditedSource::copyString(const Twine &twine) {
71 SmallString<128> Data;
72 return copyString(twine.toStringRef(Data));
75 bool EditedSource::canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs) {
76 FileEditsTy::iterator FA = getActionForOffset(Offs);
77 if (FA != FileEdits.end()) {
78 if (FA->first != Offs)
79 return false; // position has been removed.
82 if (SourceMgr.isMacroArgExpansion(OrigLoc)) {
83 SourceLocation ExpLoc;
85 deconstructMacroArgLoc(OrigLoc, ExpLoc, ArgUse);
86 auto I = ExpansionToArgMap.find(ExpLoc.getRawEncoding());
87 if (I != ExpansionToArgMap.end() &&
88 find_if(I->second, [&](const MacroArgUse &U) {
89 return ArgUse.Identifier == U.Identifier &&
90 std::tie(ArgUse.ImmediateExpansionLoc, ArgUse.UseLoc) !=
91 std::tie(U.ImmediateExpansionLoc, U.UseLoc);
92 }) != I->second.end()) {
93 // Trying to write in a macro argument input that has already been
94 // written by a previous commit for another expansion of the same macro
95 // argument name. For example:
98 // #define MAC(x) ((x)+(x))
102 // A commit modified the macro argument 'a' due to the first '(x)'
103 // expansion inside the macro definition, and a subsequent commit tried
104 // to modify 'a' again for the second '(x)' expansion. The edits of the
105 // second commit will be rejected.
112 bool EditedSource::commitInsert(SourceLocation OrigLoc,
113 FileOffset Offs, StringRef text,
114 bool beforePreviousInsertions) {
115 if (!canInsertInOffset(OrigLoc, Offs))
120 if (SourceMgr.isMacroArgExpansion(OrigLoc)) {
122 SourceLocation ExpLoc;
123 deconstructMacroArgLoc(OrigLoc, ExpLoc, ArgUse);
124 if (ArgUse.Identifier)
125 CurrCommitMacroArgExps.emplace_back(ExpLoc, ArgUse);
128 FileEdit &FA = FileEdits[Offs];
129 if (FA.Text.empty()) {
130 FA.Text = copyString(text);
134 if (beforePreviousInsertions)
135 FA.Text = copyString(Twine(text) + FA.Text);
137 FA.Text = copyString(Twine(FA.Text) + text);
142 bool EditedSource::commitInsertFromRange(SourceLocation OrigLoc,
144 FileOffset InsertFromRangeOffs, unsigned Len,
145 bool beforePreviousInsertions) {
149 SmallString<128> StrVec;
150 FileOffset BeginOffs = InsertFromRangeOffs;
151 FileOffset EndOffs = BeginOffs.getWithOffset(Len);
152 FileEditsTy::iterator I = FileEdits.upper_bound(BeginOffs);
153 if (I != FileEdits.begin())
156 for (; I != FileEdits.end(); ++I) {
157 FileEdit &FA = I->second;
158 FileOffset B = I->first;
159 FileOffset E = B.getWithOffset(FA.RemoveLen);
173 for (; I != FileEdits.end() && EndOffs > I->first; ++I) {
174 FileEdit &FA = I->second;
175 FileOffset B = I->first;
176 FileOffset E = B.getWithOffset(FA.RemoveLen);
179 bool Invalid = false;
180 StringRef text = getSourceText(BeginOffs, B, Invalid);
189 if (BeginOffs < EndOffs) {
190 bool Invalid = false;
191 StringRef text = getSourceText(BeginOffs, EndOffs, Invalid);
197 return commitInsert(OrigLoc, Offs, StrVec, beforePreviousInsertions);
200 void EditedSource::commitRemove(SourceLocation OrigLoc,
201 FileOffset BeginOffs, unsigned Len) {
205 FileOffset EndOffs = BeginOffs.getWithOffset(Len);
206 FileEditsTy::iterator I = FileEdits.upper_bound(BeginOffs);
207 if (I != FileEdits.begin())
210 for (; I != FileEdits.end(); ++I) {
211 FileEdit &FA = I->second;
212 FileOffset B = I->first;
213 FileOffset E = B.getWithOffset(FA.RemoveLen);
219 FileOffset TopBegin, TopEnd;
220 FileEdit *TopFA = nullptr;
222 if (I == FileEdits.end()) {
223 FileEditsTy::iterator
224 NewI = FileEdits.insert(I, std::make_pair(BeginOffs, FileEdit()));
225 NewI->second.RemoveLen = Len;
229 FileEdit &FA = I->second;
230 FileOffset B = I->first;
231 FileOffset E = B.getWithOffset(FA.RemoveLen);
233 FileEditsTy::iterator
234 NewI = FileEdits.insert(I, std::make_pair(BeginOffs, FileEdit()));
235 TopBegin = BeginOffs;
237 TopFA = &NewI->second;
238 TopFA->RemoveLen = Len;
243 if (TopEnd >= EndOffs)
245 unsigned diff = EndOffs.getOffset() - TopEnd.getOffset();
247 TopFA->RemoveLen += diff;
249 TopFA->Text = StringRef();
253 while (I != FileEdits.end()) {
254 FileEdit &FA = I->second;
255 FileOffset B = I->first;
256 FileOffset E = B.getWithOffset(FA.RemoveLen);
262 FileEdits.erase(I++);
267 unsigned diff = E.getOffset() - TopEnd.getOffset();
269 TopFA->RemoveLen += diff;
277 bool EditedSource::commit(const Commit &commit) {
278 if (!commit.isCommitable())
282 EditedSource &Editor;
284 CommitRAII(EditedSource &Editor) : Editor(Editor) {
285 Editor.startingCommit();
289 Editor.finishedCommit();
293 for (edit::Commit::edit_iterator
294 I = commit.edit_begin(), E = commit.edit_end(); I != E; ++I) {
295 const edit::Commit::Edit &edit = *I;
297 case edit::Commit::Act_Insert:
298 commitInsert(edit.OrigLoc, edit.Offset, edit.Text, edit.BeforePrev);
300 case edit::Commit::Act_InsertFromRange:
301 commitInsertFromRange(edit.OrigLoc, edit.Offset,
302 edit.InsertFromRangeOffs, edit.Length,
305 case edit::Commit::Act_Remove:
306 commitRemove(edit.OrigLoc, edit.Offset, edit.Length);
314 // Returns true if it is ok to make the two given characters adjacent.
315 static bool canBeJoined(char left, char right, const LangOptions &LangOpts) {
316 // FIXME: Should use TokenConcatenation to make sure we don't allow stuff like
317 // making two '<' adjacent.
318 return !(Lexer::isIdentifierBodyChar(left, LangOpts) &&
319 Lexer::isIdentifierBodyChar(right, LangOpts));
322 /// Returns true if it is ok to eliminate the trailing whitespace between
323 /// the given characters.
324 static bool canRemoveWhitespace(char left, char beforeWSpace, char right,
325 const LangOptions &LangOpts) {
326 if (!canBeJoined(left, right, LangOpts))
328 if (isWhitespace(left) || isWhitespace(right))
330 if (canBeJoined(beforeWSpace, right, LangOpts))
331 return false; // the whitespace was intentional, keep it.
335 /// Check the range that we are going to remove and:
336 /// -Remove any trailing whitespace if possible.
337 /// -Insert a space if removing the range is going to mess up the source tokens.
338 static void adjustRemoval(const SourceManager &SM, const LangOptions &LangOpts,
339 SourceLocation Loc, FileOffset offs,
340 unsigned &len, StringRef &text) {
341 assert(len && text.empty());
342 SourceLocation BeginTokLoc = Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
343 if (BeginTokLoc != Loc)
344 return; // the range is not at the beginning of a token, keep the range.
346 bool Invalid = false;
347 StringRef buffer = SM.getBufferData(offs.getFID(), &Invalid);
351 unsigned begin = offs.getOffset();
352 unsigned end = begin + len;
354 // Do not try to extend the removal if we're at the end of the buffer already.
355 if (end == buffer.size())
358 assert(begin < buffer.size() && end < buffer.size() && "Invalid range!");
360 // FIXME: Remove newline.
363 if (buffer[end] == ' ')
368 if (buffer[end] == ' ') {
369 assert((end + 1 != buffer.size() || buffer.data()[end + 1] == 0) &&
370 "buffer not zero-terminated!");
371 if (canRemoveWhitespace(/*left=*/buffer[begin-1],
372 /*beforeWSpace=*/buffer[end-1],
373 /*right=*/buffer.data()[end + 1], // zero-terminated
379 if (!canBeJoined(buffer[begin-1], buffer[end], LangOpts))
383 static void applyRewrite(EditsReceiver &receiver,
384 StringRef text, FileOffset offs, unsigned len,
385 const SourceManager &SM, const LangOptions &LangOpts,
386 bool shouldAdjustRemovals) {
387 assert(offs.getFID().isValid());
388 SourceLocation Loc = SM.getLocForStartOfFile(offs.getFID());
389 Loc = Loc.getLocWithOffset(offs.getOffset());
390 assert(Loc.isFileID());
392 if (text.empty() && shouldAdjustRemovals)
393 adjustRemoval(SM, LangOpts, Loc, offs, len, text);
395 CharSourceRange range = CharSourceRange::getCharRange(Loc,
396 Loc.getLocWithOffset(len));
400 receiver.remove(range);
405 receiver.replace(range, text);
407 receiver.insert(Loc, text);
410 void EditedSource::applyRewrites(EditsReceiver &receiver,
411 bool shouldAdjustRemovals) {
412 SmallString<128> StrVec;
413 FileOffset CurOffs, CurEnd;
416 if (FileEdits.empty())
419 FileEditsTy::iterator I = FileEdits.begin();
421 StrVec = I->second.Text;
422 CurLen = I->second.RemoveLen;
423 CurEnd = CurOffs.getWithOffset(CurLen);
426 for (FileEditsTy::iterator E = FileEdits.end(); I != E; ++I) {
427 FileOffset offs = I->first;
428 FileEdit act = I->second;
429 assert(offs >= CurEnd);
431 if (offs == CurEnd) {
433 CurLen += act.RemoveLen;
434 CurEnd.getWithOffset(act.RemoveLen);
438 applyRewrite(receiver, StrVec, CurOffs, CurLen, SourceMgr, LangOpts,
439 shouldAdjustRemovals);
442 CurLen = act.RemoveLen;
443 CurEnd = CurOffs.getWithOffset(CurLen);
446 applyRewrite(receiver, StrVec, CurOffs, CurLen, SourceMgr, LangOpts,
447 shouldAdjustRemovals);
450 void EditedSource::clearRewrites() {
455 StringRef EditedSource::getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
457 assert(BeginOffs.getFID() == EndOffs.getFID());
458 assert(BeginOffs <= EndOffs);
459 SourceLocation BLoc = SourceMgr.getLocForStartOfFile(BeginOffs.getFID());
460 BLoc = BLoc.getLocWithOffset(BeginOffs.getOffset());
461 assert(BLoc.isFileID());
463 ELoc = BLoc.getLocWithOffset(EndOffs.getOffset() - BeginOffs.getOffset());
464 return Lexer::getSourceText(CharSourceRange::getCharRange(BLoc, ELoc),
465 SourceMgr, LangOpts, &Invalid);
468 EditedSource::FileEditsTy::iterator
469 EditedSource::getActionForOffset(FileOffset Offs) {
470 FileEditsTy::iterator I = FileEdits.upper_bound(Offs);
471 if (I == FileEdits.begin())
472 return FileEdits.end();
474 FileEdit &FA = I->second;
475 FileOffset B = I->first;
476 FileOffset E = B.getWithOffset(FA.RemoveLen);
477 if (Offs >= B && Offs < E)
480 return FileEdits.end();