1 //===----- Commit.cpp - A unit of 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/Commit.h"
11 #include "clang/Basic/SourceManager.h"
12 #include "clang/Edit/EditedSource.h"
13 #include "clang/Lex/Lexer.h"
14 #include "clang/Lex/PPConditionalDirectiveRecord.h"
16 using namespace clang;
19 SourceLocation Commit::Edit::getFileLocation(SourceManager &SM) const {
20 SourceLocation Loc = SM.getLocForStartOfFile(Offset.getFID());
21 Loc = Loc.getLocWithOffset(Offset.getOffset());
22 assert(Loc.isFileID());
26 CharSourceRange Commit::Edit::getFileRange(SourceManager &SM) const {
27 SourceLocation Loc = getFileLocation(SM);
28 return CharSourceRange::getCharRange(Loc, Loc.getLocWithOffset(Length));
31 CharSourceRange Commit::Edit::getInsertFromRange(SourceManager &SM) const {
32 SourceLocation Loc = SM.getLocForStartOfFile(InsertFromRangeOffs.getFID());
33 Loc = Loc.getLocWithOffset(InsertFromRangeOffs.getOffset());
34 assert(Loc.isFileID());
35 return CharSourceRange::getCharRange(Loc, Loc.getLocWithOffset(Length));
38 Commit::Commit(EditedSource &Editor)
39 : SourceMgr(Editor.getSourceManager()), LangOpts(Editor.getLangOpts()),
40 PPRec(Editor.getPPCondDirectiveRecord()),
42 ForceCommitInSystemHeader(Editor.getForceCommitInSystemHeader()),
43 IsCommitable(true) { }
45 bool Commit::insert(SourceLocation loc, StringRef text,
46 bool afterToken, bool beforePreviousInsertions) {
51 if ((!afterToken && !canInsert(loc, Offs)) ||
52 ( afterToken && !canInsertAfterToken(loc, Offs, loc))) {
57 addInsert(loc, Offs, text, beforePreviousInsertions);
61 bool Commit::insertFromRange(SourceLocation loc,
62 CharSourceRange range,
63 bool afterToken, bool beforePreviousInsertions) {
66 if (!canRemoveRange(range, RangeOffs, RangeLen)) {
72 if ((!afterToken && !canInsert(loc, Offs)) ||
73 ( afterToken && !canInsertAfterToken(loc, Offs, loc))) {
79 PPRec->areInDifferentConditionalDirectiveRegion(loc, range.getBegin())) {
84 addInsertFromRange(loc, Offs, RangeOffs, RangeLen, beforePreviousInsertions);
88 bool Commit::remove(CharSourceRange range) {
91 if (!canRemoveRange(range, Offs, Len)) {
96 addRemove(range.getBegin(), Offs, Len);
100 bool Commit::insertWrap(StringRef before, CharSourceRange range,
102 bool commitableBefore = insert(range.getBegin(), before, /*afterToken=*/false,
103 /*beforePreviousInsertions=*/true);
104 bool commitableAfter;
105 if (range.isTokenRange())
106 commitableAfter = insertAfterToken(range.getEnd(), after);
108 commitableAfter = insert(range.getEnd(), after);
110 return commitableBefore && commitableAfter;
113 bool Commit::replace(CharSourceRange range, StringRef text) {
115 return remove(range);
119 if (!canInsert(range.getBegin(), Offs) || !canRemoveRange(range, Offs, Len)) {
120 IsCommitable = false;
124 addRemove(range.getBegin(), Offs, Len);
125 addInsert(range.getBegin(), Offs, text, false);
129 bool Commit::replaceWithInner(CharSourceRange range,
130 CharSourceRange replacementRange) {
131 FileOffset OuterBegin;
133 if (!canRemoveRange(range, OuterBegin, OuterLen)) {
134 IsCommitable = false;
138 FileOffset InnerBegin;
140 if (!canRemoveRange(replacementRange, InnerBegin, InnerLen)) {
141 IsCommitable = false;
145 FileOffset OuterEnd = OuterBegin.getWithOffset(OuterLen);
146 FileOffset InnerEnd = InnerBegin.getWithOffset(InnerLen);
147 if (OuterBegin.getFID() != InnerBegin.getFID() ||
148 InnerBegin < OuterBegin ||
149 InnerBegin > OuterEnd ||
150 InnerEnd > OuterEnd) {
151 IsCommitable = false;
155 addRemove(range.getBegin(),
156 OuterBegin, InnerBegin.getOffset() - OuterBegin.getOffset());
157 addRemove(replacementRange.getEnd(),
158 InnerEnd, OuterEnd.getOffset() - InnerEnd.getOffset());
162 bool Commit::replaceText(SourceLocation loc, StringRef text,
163 StringRef replacementText) {
164 if (text.empty() || replacementText.empty())
169 if (!canReplaceText(loc, replacementText, Offs, Len)) {
170 IsCommitable = false;
174 addRemove(loc, Offs, Len);
175 addInsert(loc, Offs, text, false);
179 void Commit::addInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
180 bool beforePreviousInsertions) {
185 data.Kind = Act_Insert;
186 data.OrigLoc = OrigLoc;
188 data.Text = copyString(text);
189 data.BeforePrev = beforePreviousInsertions;
190 CachedEdits.push_back(data);
193 void Commit::addInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
194 FileOffset RangeOffs, unsigned RangeLen,
195 bool beforePreviousInsertions) {
200 data.Kind = Act_InsertFromRange;
201 data.OrigLoc = OrigLoc;
203 data.InsertFromRangeOffs = RangeOffs;
204 data.Length = RangeLen;
205 data.BeforePrev = beforePreviousInsertions;
206 CachedEdits.push_back(data);
209 void Commit::addRemove(SourceLocation OrigLoc,
210 FileOffset Offs, unsigned Len) {
215 data.Kind = Act_Remove;
216 data.OrigLoc = OrigLoc;
219 CachedEdits.push_back(data);
222 bool Commit::canInsert(SourceLocation loc, FileOffset &offs) {
227 isAtStartOfMacroExpansion(loc, &loc);
229 const SourceManager &SM = SourceMgr;
230 while (SM.isMacroArgExpansion(loc))
231 loc = SM.getImmediateSpellingLoc(loc);
234 if (!isAtStartOfMacroExpansion(loc, &loc))
237 if (SM.isInSystemHeader(loc) && ForceCommitInSystemHeader)
240 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
241 if (locInfo.first.isInvalid())
243 offs = FileOffset(locInfo.first, locInfo.second);
244 return canInsertInOffset(loc, offs);
247 bool Commit::canInsertAfterToken(SourceLocation loc, FileOffset &offs,
248 SourceLocation &AfterLoc) {
253 SourceLocation spellLoc = SourceMgr.getSpellingLoc(loc);
254 unsigned tokLen = Lexer::MeasureTokenLength(spellLoc, SourceMgr, LangOpts);
255 AfterLoc = loc.getLocWithOffset(tokLen);
258 isAtEndOfMacroExpansion(loc, &loc);
260 const SourceManager &SM = SourceMgr;
261 while (SM.isMacroArgExpansion(loc))
262 loc = SM.getImmediateSpellingLoc(loc);
265 if (!isAtEndOfMacroExpansion(loc, &loc))
268 if (SM.isInSystemHeader(loc) && ForceCommitInSystemHeader)
271 loc = Lexer::getLocForEndOfToken(loc, 0, SourceMgr, LangOpts);
275 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
276 if (locInfo.first.isInvalid())
278 offs = FileOffset(locInfo.first, locInfo.second);
279 return canInsertInOffset(loc, offs);
282 bool Commit::canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs) {
283 for (unsigned i = 0, e = CachedEdits.size(); i != e; ++i) {
284 Edit &act = CachedEdits[i];
285 if (act.Kind == Act_Remove) {
286 if (act.Offset.getFID() == Offs.getFID() &&
287 Offs > act.Offset && Offs < act.Offset.getWithOffset(act.Length))
288 return false; // position has been removed.
294 return Editor->canInsertInOffset(OrigLoc, Offs);
297 bool Commit::canRemoveRange(CharSourceRange range,
298 FileOffset &Offs, unsigned &Len) {
299 const SourceManager &SM = SourceMgr;
300 range = Lexer::makeFileCharRange(range, SM, LangOpts);
301 if (range.isInvalid())
304 if (range.getBegin().isMacroID() || range.getEnd().isMacroID())
306 if ((SM.isInSystemHeader(range.getBegin()) ||
307 SM.isInSystemHeader(range.getEnd())) && ForceCommitInSystemHeader)
310 if (PPRec && PPRec->rangeIntersectsConditionalDirective(range.getAsRange()))
313 std::pair<FileID, unsigned> beginInfo = SM.getDecomposedLoc(range.getBegin());
314 std::pair<FileID, unsigned> endInfo = SM.getDecomposedLoc(range.getEnd());
315 if (beginInfo.first != endInfo.first ||
316 beginInfo.second > endInfo.second)
319 Offs = FileOffset(beginInfo.first, beginInfo.second);
320 Len = endInfo.second - beginInfo.second;
324 bool Commit::canReplaceText(SourceLocation loc, StringRef text,
325 FileOffset &Offs, unsigned &Len) {
326 assert(!text.empty());
328 if (!canInsert(loc, Offs))
331 // Try to load the file buffer.
332 bool invalidTemp = false;
333 StringRef file = SourceMgr.getBufferData(Offs.getFID(), &invalidTemp);
338 return file.substr(Offs.getOffset()).startswith(text);
341 bool Commit::isAtStartOfMacroExpansion(SourceLocation loc,
342 SourceLocation *MacroBegin) const {
343 return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts, MacroBegin);
345 bool Commit::isAtEndOfMacroExpansion(SourceLocation loc,
346 SourceLocation *MacroEnd) const {
347 return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);