1 //===--- ARCMT.cpp - Migration to ARC mode --------------------------------===//
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 "Internals.h"
11 #include "clang/AST/Expr.h"
12 #include "clang/Lex/Preprocessor.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "llvm/ADT/DenseSet.h"
17 using namespace clang;
18 using namespace arcmt;
19 using llvm::StringRef;
23 /// \brief Collects transformations and merges them before applying them with
24 /// with applyRewrites(). E.g. if the same source range
25 /// is requested to be removed twice, only one rewriter remove will be invoked.
26 /// Rewrites happen in "transactions"; if one rewrite in the transaction cannot
27 /// be done (e.g. it resides in a macro) all rewrites in the transaction are
29 /// FIXME: "Transactional" rewrites support should be baked in the Rewriter.
30 class TransformActionsImpl {
31 CapturedDiagList &CapturedDiags;
38 Act_Insert, Act_InsertAfterToken,
39 Act_Remove, Act_RemoveStmt,
40 Act_Replace, Act_ReplaceText,
41 Act_IncreaseIndentation,
49 llvm::StringRef Text1, Text2;
51 llvm::SmallVector<unsigned, 2> DiagIDs;
54 std::vector<ActionData> CachedActions;
56 enum RangeComparison {
65 /// \brief A range to remove. It is a character range.
67 FullSourceLoc Begin, End;
69 CharRange(CharSourceRange range, SourceManager &srcMgr, Preprocessor &PP) {
70 SourceLocation beginLoc = range.getBegin(), endLoc = range.getEnd();
71 assert(beginLoc.isValid() && endLoc.isValid());
72 if (range.isTokenRange()) {
73 Begin = FullSourceLoc(srcMgr.getInstantiationLoc(beginLoc), srcMgr);
74 End = FullSourceLoc(getLocForEndOfToken(endLoc, srcMgr, PP), srcMgr);
76 Begin = FullSourceLoc(srcMgr.getInstantiationLoc(beginLoc), srcMgr);
77 End = FullSourceLoc(srcMgr.getInstantiationLoc(endLoc), srcMgr);
79 assert(Begin.isValid() && End.isValid());
82 RangeComparison compareWith(const CharRange &RHS) const {
83 if (End.isBeforeInTranslationUnitThan(RHS.Begin))
85 if (RHS.End.isBeforeInTranslationUnitThan(Begin))
87 if (!Begin.isBeforeInTranslationUnitThan(RHS.Begin) &&
88 !RHS.End.isBeforeInTranslationUnitThan(End))
89 return Range_Contained;
90 if (Begin.isBeforeInTranslationUnitThan(RHS.Begin) &&
91 RHS.End.isBeforeInTranslationUnitThan(End))
92 return Range_Contains;
93 if (Begin.isBeforeInTranslationUnitThan(RHS.Begin))
94 return Range_ExtendsBegin;
96 return Range_ExtendsEnd;
99 static RangeComparison compare(SourceRange LHS, SourceRange RHS,
100 SourceManager &SrcMgr, Preprocessor &PP) {
101 return CharRange(CharSourceRange::getTokenRange(LHS), SrcMgr, PP)
102 .compareWith(CharRange(CharSourceRange::getTokenRange(RHS),
107 typedef llvm::SmallVector<StringRef, 2> TextsVec;
108 typedef std::map<FullSourceLoc, TextsVec, FullSourceLoc::BeforeThanCompare>
111 /// \brief A list of ranges to remove. They are always sorted and they never
112 /// intersect with each other.
113 std::list<CharRange> Removals;
115 llvm::DenseSet<Stmt *> StmtRemovals;
117 std::vector<std::pair<CharRange, SourceLocation> > IndentationRanges;
119 /// \brief Keeps text passed to transformation methods.
120 llvm::StringMap<bool> UniqueText;
123 TransformActionsImpl(CapturedDiagList &capturedDiags,
124 ASTContext &ctx, Preprocessor &PP)
125 : CapturedDiags(capturedDiags), Ctx(ctx), PP(PP), IsInTransaction(false) { }
127 void startTransaction();
128 bool commitTransaction();
129 void abortTransaction();
131 bool isInTransaction() const { return IsInTransaction; }
133 void insert(SourceLocation loc, llvm::StringRef text);
134 void insertAfterToken(SourceLocation loc, llvm::StringRef text);
135 void remove(SourceRange range);
136 void removeStmt(Stmt *S);
137 void replace(SourceRange range, llvm::StringRef text);
138 void replace(SourceRange range, SourceRange replacementRange);
139 void replaceStmt(Stmt *S, llvm::StringRef text);
140 void replaceText(SourceLocation loc, llvm::StringRef text,
141 llvm::StringRef replacementText);
142 void increaseIndentation(SourceRange range,
143 SourceLocation parentIndent);
145 bool clearDiagnostic(llvm::ArrayRef<unsigned> IDs, SourceRange range);
147 void applyRewrites(TransformActions::RewriteReceiver &receiver);
150 bool canInsert(SourceLocation loc);
151 bool canInsertAfterToken(SourceLocation loc);
152 bool canRemoveRange(SourceRange range);
153 bool canReplaceRange(SourceRange range, SourceRange replacementRange);
154 bool canReplaceText(SourceLocation loc, llvm::StringRef text);
156 void commitInsert(SourceLocation loc, StringRef text);
157 void commitInsertAfterToken(SourceLocation loc, StringRef text);
158 void commitRemove(SourceRange range);
159 void commitRemoveStmt(Stmt *S);
160 void commitReplace(SourceRange range, SourceRange replacementRange);
161 void commitReplaceText(SourceLocation loc, llvm::StringRef text,
162 llvm::StringRef replacementText);
163 void commitIncreaseIndentation(SourceRange range,SourceLocation parentIndent);
164 void commitClearDiagnostic(llvm::ArrayRef<unsigned> IDs, SourceRange range);
166 void addRemoval(CharSourceRange range);
167 void addInsertion(SourceLocation loc, StringRef text);
169 /// \brief Stores text passed to the transformation methods to keep the string
170 /// "alive". Since the vast majority of text will be the same, we also unique
171 /// the strings using a StringMap.
172 StringRef getUniqueText(StringRef text);
174 /// \brief Computes the source location just past the end of the token at
175 /// the given source location. If the location points at a macro, the whole
176 /// macro expansion is skipped.
177 static SourceLocation getLocForEndOfToken(SourceLocation loc,
178 SourceManager &SM,Preprocessor &PP);
181 } // anonymous namespace
183 void TransformActionsImpl::startTransaction() {
184 assert(!IsInTransaction &&
185 "Cannot start a transaction in the middle of another one");
186 IsInTransaction = true;
189 bool TransformActionsImpl::commitTransaction() {
190 assert(IsInTransaction && "No transaction started");
192 if (CachedActions.empty()) {
193 IsInTransaction = false;
197 // Verify that all actions are possible otherwise abort the whole transaction.
198 bool AllActionsPossible = true;
199 for (unsigned i = 0, e = CachedActions.size(); i != e; ++i) {
200 ActionData &act = CachedActions[i];
203 if (!canInsert(act.Loc))
204 AllActionsPossible = false;
206 case Act_InsertAfterToken:
207 if (!canInsertAfterToken(act.Loc))
208 AllActionsPossible = false;
211 if (!canRemoveRange(act.R1))
212 AllActionsPossible = false;
216 if (!canRemoveRange(act.S->getSourceRange()))
217 AllActionsPossible = false;
220 if (!canReplaceRange(act.R1, act.R2))
221 AllActionsPossible = false;
223 case Act_ReplaceText:
224 if (!canReplaceText(act.Loc, act.Text1))
225 AllActionsPossible = false;
227 case Act_IncreaseIndentation:
228 // This is not important, we don't care if it will fail.
230 case Act_ClearDiagnostic:
231 // We are just checking source rewrites.
234 if (!AllActionsPossible)
238 if (!AllActionsPossible) {
243 for (unsigned i = 0, e = CachedActions.size(); i != e; ++i) {
244 ActionData &act = CachedActions[i];
247 commitInsert(act.Loc, act.Text1);
249 case Act_InsertAfterToken:
250 commitInsertAfterToken(act.Loc, act.Text1);
253 commitRemove(act.R1);
256 commitRemoveStmt(act.S);
259 commitReplace(act.R1, act.R2);
261 case Act_ReplaceText:
262 commitReplaceText(act.Loc, act.Text1, act.Text2);
264 case Act_IncreaseIndentation:
265 commitIncreaseIndentation(act.R1, act.Loc);
267 case Act_ClearDiagnostic:
268 commitClearDiagnostic(act.DiagIDs, act.R1);
273 CachedActions.clear();
274 IsInTransaction = false;
278 void TransformActionsImpl::abortTransaction() {
279 assert(IsInTransaction && "No transaction started");
280 CachedActions.clear();
281 IsInTransaction = false;
284 void TransformActionsImpl::insert(SourceLocation loc, StringRef text) {
285 assert(IsInTransaction && "Actions only allowed during a transaction");
286 text = getUniqueText(text);
288 data.Kind = Act_Insert;
291 CachedActions.push_back(data);
294 void TransformActionsImpl::insertAfterToken(SourceLocation loc, StringRef text) {
295 assert(IsInTransaction && "Actions only allowed during a transaction");
296 text = getUniqueText(text);
298 data.Kind = Act_InsertAfterToken;
301 CachedActions.push_back(data);
304 void TransformActionsImpl::remove(SourceRange range) {
305 assert(IsInTransaction && "Actions only allowed during a transaction");
307 data.Kind = Act_Remove;
309 CachedActions.push_back(data);
312 void TransformActionsImpl::removeStmt(Stmt *S) {
313 assert(IsInTransaction && "Actions only allowed during a transaction");
315 data.Kind = Act_RemoveStmt;
316 data.S = S->IgnoreImplicit(); // important for uniquing
317 CachedActions.push_back(data);
320 void TransformActionsImpl::replace(SourceRange range, StringRef text) {
321 assert(IsInTransaction && "Actions only allowed during a transaction");
322 text = getUniqueText(text);
324 insert(range.getBegin(), text);
327 void TransformActionsImpl::replace(SourceRange range,
328 SourceRange replacementRange) {
329 assert(IsInTransaction && "Actions only allowed during a transaction");
331 data.Kind = Act_Replace;
333 data.R2 = replacementRange;
334 CachedActions.push_back(data);
337 void TransformActionsImpl::replaceText(SourceLocation loc, StringRef text,
338 StringRef replacementText) {
339 text = getUniqueText(text);
340 replacementText = getUniqueText(replacementText);
342 data.Kind = Act_ReplaceText;
345 data.Text2 = replacementText;
346 CachedActions.push_back(data);
349 void TransformActionsImpl::replaceStmt(Stmt *S, StringRef text) {
350 assert(IsInTransaction && "Actions only allowed during a transaction");
351 text = getUniqueText(text);
352 insert(S->getLocStart(), text);
356 void TransformActionsImpl::increaseIndentation(SourceRange range,
357 SourceLocation parentIndent) {
358 if (range.isInvalid()) return;
359 assert(IsInTransaction && "Actions only allowed during a transaction");
361 data.Kind = Act_IncreaseIndentation;
363 data.Loc = parentIndent;
364 CachedActions.push_back(data);
367 bool TransformActionsImpl::clearDiagnostic(llvm::ArrayRef<unsigned> IDs,
369 assert(IsInTransaction && "Actions only allowed during a transaction");
370 if (!CapturedDiags.hasDiagnostic(IDs, range))
374 data.Kind = Act_ClearDiagnostic;
376 data.DiagIDs.append(IDs.begin(), IDs.end());
377 CachedActions.push_back(data);
381 bool TransformActionsImpl::canInsert(SourceLocation loc) {
385 SourceManager &SM = Ctx.getSourceManager();
386 if (SM.isInSystemHeader(SM.getInstantiationLoc(loc)))
391 return PP.isAtStartOfMacroExpansion(loc);
394 bool TransformActionsImpl::canInsertAfterToken(SourceLocation loc) {
398 SourceManager &SM = Ctx.getSourceManager();
399 if (SM.isInSystemHeader(SM.getInstantiationLoc(loc)))
404 return PP.isAtEndOfMacroExpansion(loc);
407 bool TransformActionsImpl::canRemoveRange(SourceRange range) {
408 return canInsert(range.getBegin()) && canInsertAfterToken(range.getEnd());
411 bool TransformActionsImpl::canReplaceRange(SourceRange range,
412 SourceRange replacementRange) {
413 return canRemoveRange(range) && canRemoveRange(replacementRange);
416 bool TransformActionsImpl::canReplaceText(SourceLocation loc, StringRef text) {
420 SourceManager &SM = Ctx.getSourceManager();
421 loc = SM.getInstantiationLoc(loc);
423 // Break down the source location.
424 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
426 // Try to load the file buffer.
427 bool invalidTemp = false;
428 llvm::StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
432 return file.substr(locInfo.second).startswith(text);
435 void TransformActionsImpl::commitInsert(SourceLocation loc, StringRef text) {
436 addInsertion(loc, text);
439 void TransformActionsImpl::commitInsertAfterToken(SourceLocation loc,
441 addInsertion(getLocForEndOfToken(loc, Ctx.getSourceManager(), PP), text);
444 void TransformActionsImpl::commitRemove(SourceRange range) {
445 addRemoval(CharSourceRange::getTokenRange(range));
448 void TransformActionsImpl::commitRemoveStmt(Stmt *S) {
450 if (StmtRemovals.count(S))
451 return; // already removed.
453 if (Expr *E = dyn_cast<Expr>(S)) {
454 commitRemove(E->getSourceRange());
455 commitInsert(E->getSourceRange().getBegin(), getARCMTMacroName());
457 commitRemove(S->getSourceRange());
459 StmtRemovals.insert(S);
462 void TransformActionsImpl::commitReplace(SourceRange range,
463 SourceRange replacementRange) {
464 RangeComparison comp = CharRange::compare(replacementRange, range,
465 Ctx.getSourceManager(), PP);
466 assert(comp == Range_Contained);
467 if (comp != Range_Contained)
468 return; // Although we asserted, be extra safe for release build.
469 if (range.getBegin() != replacementRange.getBegin())
470 addRemoval(CharSourceRange::getCharRange(range.getBegin(),
471 replacementRange.getBegin()));
472 if (replacementRange.getEnd() != range.getEnd())
473 addRemoval(CharSourceRange::getTokenRange(
474 getLocForEndOfToken(replacementRange.getEnd(),
475 Ctx.getSourceManager(), PP),
478 void TransformActionsImpl::commitReplaceText(SourceLocation loc,
480 StringRef replacementText) {
481 SourceManager &SM = Ctx.getSourceManager();
482 loc = SM.getInstantiationLoc(loc);
483 // canReplaceText already checked if loc points at text.
484 SourceLocation afterText = loc.getFileLocWithOffset(text.size());
486 addRemoval(CharSourceRange::getCharRange(loc, afterText));
487 commitInsert(loc, replacementText);
490 void TransformActionsImpl::commitIncreaseIndentation(SourceRange range,
491 SourceLocation parentIndent) {
492 SourceManager &SM = Ctx.getSourceManager();
493 IndentationRanges.push_back(
494 std::make_pair(CharRange(CharSourceRange::getTokenRange(range),
496 SM.getInstantiationLoc(parentIndent)));
499 void TransformActionsImpl::commitClearDiagnostic(llvm::ArrayRef<unsigned> IDs,
501 CapturedDiags.clearDiagnostic(IDs, range);
504 void TransformActionsImpl::addInsertion(SourceLocation loc, StringRef text) {
505 SourceManager &SM = Ctx.getSourceManager();
506 loc = SM.getInstantiationLoc(loc);
507 for (std::list<CharRange>::reverse_iterator
508 I = Removals.rbegin(), E = Removals.rend(); I != E; ++I) {
509 if (!SM.isBeforeInTranslationUnit(loc, I->End))
511 if (I->Begin.isBeforeInTranslationUnitThan(loc))
515 Inserts[FullSourceLoc(loc, SM)].push_back(text);
518 void TransformActionsImpl::addRemoval(CharSourceRange range) {
519 CharRange newRange(range, Ctx.getSourceManager(), PP);
520 if (newRange.Begin == newRange.End)
523 Inserts.erase(Inserts.upper_bound(newRange.Begin),
524 Inserts.lower_bound(newRange.End));
526 std::list<CharRange>::iterator I = Removals.end();
527 while (I != Removals.begin()) {
528 std::list<CharRange>::iterator RI = I;
530 RangeComparison comp = newRange.compareWith(*RI);
536 Removals.insert(I, newRange);
538 case Range_Contained:
541 RI->End = newRange.End;
542 case Range_ExtendsBegin:
543 newRange.End = RI->End;
546 case Range_ExtendsEnd:
547 RI->End = newRange.End;
552 Removals.insert(Removals.begin(), newRange);
555 void TransformActionsImpl::applyRewrites(
556 TransformActions::RewriteReceiver &receiver) {
557 for (InsertsMap::iterator I = Inserts.begin(), E = Inserts.end(); I!=E; ++I) {
558 SourceLocation loc = I->first;
559 for (TextsVec::iterator
560 TI = I->second.begin(), TE = I->second.end(); TI != TE; ++TI) {
561 receiver.insert(loc, *TI);
565 for (std::vector<std::pair<CharRange, SourceLocation> >::iterator
566 I = IndentationRanges.begin(), E = IndentationRanges.end(); I!=E; ++I) {
567 CharSourceRange range = CharSourceRange::getCharRange(I->first.Begin,
569 receiver.increaseIndentation(range, I->second);
572 for (std::list<CharRange>::iterator
573 I = Removals.begin(), E = Removals.end(); I != E; ++I) {
574 CharSourceRange range = CharSourceRange::getCharRange(I->Begin, I->End);
575 receiver.remove(range);
579 /// \brief Stores text passed to the transformation methods to keep the string
580 /// "alive". Since the vast majority of text will be the same, we also unique
581 /// the strings using a StringMap.
582 StringRef TransformActionsImpl::getUniqueText(StringRef text) {
583 llvm::StringMapEntry<bool> &entry = UniqueText.GetOrCreateValue(text);
584 return entry.getKey();
587 /// \brief Computes the source location just past the end of the token at
588 /// the given source location. If the location points at a macro, the whole
589 /// macro expansion is skipped.
590 SourceLocation TransformActionsImpl::getLocForEndOfToken(SourceLocation loc,
594 loc = SM.getInstantiationRange(loc).second;
595 return PP.getLocForEndOfToken(loc);
598 TransformActions::RewriteReceiver::~RewriteReceiver() { }
600 TransformActions::TransformActions(Diagnostic &diag,
601 CapturedDiagList &capturedDiags,
602 ASTContext &ctx, Preprocessor &PP)
603 : Diags(diag), CapturedDiags(capturedDiags) {
604 Impl = new TransformActionsImpl(capturedDiags, ctx, PP);
607 TransformActions::~TransformActions() {
608 delete static_cast<TransformActionsImpl*>(Impl);
611 void TransformActions::startTransaction() {
612 static_cast<TransformActionsImpl*>(Impl)->startTransaction();
615 bool TransformActions::commitTransaction() {
616 return static_cast<TransformActionsImpl*>(Impl)->commitTransaction();
619 void TransformActions::abortTransaction() {
620 static_cast<TransformActionsImpl*>(Impl)->abortTransaction();
624 void TransformActions::insert(SourceLocation loc, llvm::StringRef text) {
625 static_cast<TransformActionsImpl*>(Impl)->insert(loc, text);
628 void TransformActions::insertAfterToken(SourceLocation loc,
629 llvm::StringRef text) {
630 static_cast<TransformActionsImpl*>(Impl)->insertAfterToken(loc, text);
633 void TransformActions::remove(SourceRange range) {
634 static_cast<TransformActionsImpl*>(Impl)->remove(range);
637 void TransformActions::removeStmt(Stmt *S) {
638 static_cast<TransformActionsImpl*>(Impl)->removeStmt(S);
641 void TransformActions::replace(SourceRange range, llvm::StringRef text) {
642 static_cast<TransformActionsImpl*>(Impl)->replace(range, text);
645 void TransformActions::replace(SourceRange range,
646 SourceRange replacementRange) {
647 static_cast<TransformActionsImpl*>(Impl)->replace(range, replacementRange);
650 void TransformActions::replaceStmt(Stmt *S, llvm::StringRef text) {
651 static_cast<TransformActionsImpl*>(Impl)->replaceStmt(S, text);
654 void TransformActions::replaceText(SourceLocation loc, llvm::StringRef text,
655 llvm::StringRef replacementText) {
656 static_cast<TransformActionsImpl*>(Impl)->replaceText(loc, text,
660 void TransformActions::increaseIndentation(SourceRange range,
661 SourceLocation parentIndent) {
662 static_cast<TransformActionsImpl*>(Impl)->increaseIndentation(range,
666 bool TransformActions::clearDiagnostic(llvm::ArrayRef<unsigned> IDs,
668 return static_cast<TransformActionsImpl*>(Impl)->clearDiagnostic(IDs, range);
671 void TransformActions::applyRewrites(RewriteReceiver &receiver) {
672 static_cast<TransformActionsImpl*>(Impl)->applyRewrites(receiver);
675 void TransformActions::reportError(llvm::StringRef error, SourceLocation loc,
677 assert(!static_cast<TransformActionsImpl*>(Impl)->isInTransaction() &&
678 "Errors should be emitted out of a transaction");
679 // FIXME: Use a custom category name to distinguish rewriter errors.
680 std::string rewriteErr = "[rewriter] ";
683 = Diags.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error,
685 Diags.Report(loc, diagID) << range;
688 void TransformActions::reportNote(llvm::StringRef note, SourceLocation loc,
690 assert(!static_cast<TransformActionsImpl*>(Impl)->isInTransaction() &&
691 "Errors should be emitted out of a transaction");
692 // FIXME: Use a custom category name to distinguish rewriter errors.
693 std::string rewriteNote = "[rewriter] ";
696 = Diags.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Note,
698 Diags.Report(loc, diagID) << range;