2019-03-01 14:49:51 +08:00
|
|
|
//===-- TransformActions.cpp - Migration to ARC mode ----------------------===//
|
2011-06-16 07:25:17 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-06-16 07:25:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Internals.h"
|
2012-07-05 01:04:04 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2011-06-16 07:25:17 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2011-06-16 07:25:17 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include <map>
|
|
|
|
using namespace clang;
|
|
|
|
using namespace arcmt;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Collects transformations and merges them before applying them with
|
2011-06-16 07:25:17 +08:00
|
|
|
/// with applyRewrites(). E.g. if the same source range
|
|
|
|
/// is requested to be removed twice, only one rewriter remove will be invoked.
|
|
|
|
/// Rewrites happen in "transactions"; if one rewrite in the transaction cannot
|
|
|
|
/// be done (e.g. it resides in a macro) all rewrites in the transaction are
|
|
|
|
/// aborted.
|
|
|
|
/// FIXME: "Transactional" rewrites support should be baked in the Rewriter.
|
|
|
|
class TransformActionsImpl {
|
|
|
|
CapturedDiagList &CapturedDiags;
|
|
|
|
ASTContext &Ctx;
|
|
|
|
Preprocessor &PP;
|
|
|
|
|
|
|
|
bool IsInTransaction;
|
|
|
|
|
|
|
|
enum ActionKind {
|
|
|
|
Act_Insert, Act_InsertAfterToken,
|
|
|
|
Act_Remove, Act_RemoveStmt,
|
|
|
|
Act_Replace, Act_ReplaceText,
|
|
|
|
Act_IncreaseIndentation,
|
|
|
|
Act_ClearDiagnostic
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ActionData {
|
|
|
|
ActionKind Kind;
|
|
|
|
SourceLocation Loc;
|
|
|
|
SourceRange R1, R2;
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Text1, Text2;
|
2011-06-16 07:25:17 +08:00
|
|
|
Stmt *S;
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<unsigned, 2> DiagIDs;
|
2011-06-16 07:25:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<ActionData> CachedActions;
|
|
|
|
|
|
|
|
enum RangeComparison {
|
|
|
|
Range_Before,
|
|
|
|
Range_After,
|
|
|
|
Range_Contains,
|
|
|
|
Range_Contained,
|
|
|
|
Range_ExtendsBegin,
|
|
|
|
Range_ExtendsEnd
|
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A range to remove. It is a character range.
|
2011-06-16 07:25:17 +08:00
|
|
|
struct CharRange {
|
|
|
|
FullSourceLoc Begin, End;
|
|
|
|
|
|
|
|
CharRange(CharSourceRange range, SourceManager &srcMgr, Preprocessor &PP) {
|
|
|
|
SourceLocation beginLoc = range.getBegin(), endLoc = range.getEnd();
|
|
|
|
assert(beginLoc.isValid() && endLoc.isValid());
|
|
|
|
if (range.isTokenRange()) {
|
2011-07-26 00:49:02 +08:00
|
|
|
Begin = FullSourceLoc(srcMgr.getExpansionLoc(beginLoc), srcMgr);
|
2011-06-16 07:25:17 +08:00
|
|
|
End = FullSourceLoc(getLocForEndOfToken(endLoc, srcMgr, PP), srcMgr);
|
|
|
|
} else {
|
2011-07-26 00:49:02 +08:00
|
|
|
Begin = FullSourceLoc(srcMgr.getExpansionLoc(beginLoc), srcMgr);
|
|
|
|
End = FullSourceLoc(srcMgr.getExpansionLoc(endLoc), srcMgr);
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
assert(Begin.isValid() && End.isValid());
|
2018-07-31 03:24:48 +08:00
|
|
|
}
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
RangeComparison compareWith(const CharRange &RHS) const {
|
|
|
|
if (End.isBeforeInTranslationUnitThan(RHS.Begin))
|
|
|
|
return Range_Before;
|
|
|
|
if (RHS.End.isBeforeInTranslationUnitThan(Begin))
|
|
|
|
return Range_After;
|
|
|
|
if (!Begin.isBeforeInTranslationUnitThan(RHS.Begin) &&
|
|
|
|
!RHS.End.isBeforeInTranslationUnitThan(End))
|
|
|
|
return Range_Contained;
|
|
|
|
if (Begin.isBeforeInTranslationUnitThan(RHS.Begin) &&
|
|
|
|
RHS.End.isBeforeInTranslationUnitThan(End))
|
|
|
|
return Range_Contains;
|
|
|
|
if (Begin.isBeforeInTranslationUnitThan(RHS.Begin))
|
|
|
|
return Range_ExtendsBegin;
|
|
|
|
else
|
|
|
|
return Range_ExtendsEnd;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-06-16 07:25:17 +08:00
|
|
|
static RangeComparison compare(SourceRange LHS, SourceRange RHS,
|
|
|
|
SourceManager &SrcMgr, Preprocessor &PP) {
|
|
|
|
return CharRange(CharSourceRange::getTokenRange(LHS), SrcMgr, PP)
|
|
|
|
.compareWith(CharRange(CharSourceRange::getTokenRange(RHS),
|
|
|
|
SrcMgr, PP));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
typedef SmallVector<StringRef, 2> TextsVec;
|
2011-06-16 07:25:17 +08:00
|
|
|
typedef std::map<FullSourceLoc, TextsVec, FullSourceLoc::BeforeThanCompare>
|
|
|
|
InsertsMap;
|
|
|
|
InsertsMap Inserts;
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A list of ranges to remove. They are always sorted and they never
|
2011-06-16 07:25:17 +08:00
|
|
|
/// intersect with each other.
|
|
|
|
std::list<CharRange> Removals;
|
|
|
|
|
|
|
|
llvm::DenseSet<Stmt *> StmtRemovals;
|
|
|
|
|
|
|
|
std::vector<std::pair<CharRange, SourceLocation> > IndentationRanges;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Keeps text passed to transformation methods.
|
2011-06-16 07:25:17 +08:00
|
|
|
llvm::StringMap<bool> UniqueText;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TransformActionsImpl(CapturedDiagList &capturedDiags,
|
|
|
|
ASTContext &ctx, Preprocessor &PP)
|
|
|
|
: CapturedDiags(capturedDiags), Ctx(ctx), PP(PP), IsInTransaction(false) { }
|
|
|
|
|
2011-11-05 07:43:03 +08:00
|
|
|
ASTContext &getASTContext() { return Ctx; }
|
|
|
|
|
2011-06-16 07:25:17 +08:00
|
|
|
void startTransaction();
|
|
|
|
bool commitTransaction();
|
|
|
|
void abortTransaction();
|
|
|
|
|
|
|
|
bool isInTransaction() const { return IsInTransaction; }
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void insert(SourceLocation loc, StringRef text);
|
|
|
|
void insertAfterToken(SourceLocation loc, StringRef text);
|
2011-06-16 07:25:17 +08:00
|
|
|
void remove(SourceRange range);
|
|
|
|
void removeStmt(Stmt *S);
|
2011-07-23 18:55:15 +08:00
|
|
|
void replace(SourceRange range, StringRef text);
|
2011-06-16 07:25:17 +08:00
|
|
|
void replace(SourceRange range, SourceRange replacementRange);
|
2011-07-23 18:55:15 +08:00
|
|
|
void replaceStmt(Stmt *S, StringRef text);
|
|
|
|
void replaceText(SourceLocation loc, StringRef text,
|
|
|
|
StringRef replacementText);
|
2011-06-16 07:25:17 +08:00
|
|
|
void increaseIndentation(SourceRange range,
|
|
|
|
SourceLocation parentIndent);
|
|
|
|
|
2011-07-24 01:14:25 +08:00
|
|
|
bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
void applyRewrites(TransformActions::RewriteReceiver &receiver);
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool canInsert(SourceLocation loc);
|
|
|
|
bool canInsertAfterToken(SourceLocation loc);
|
|
|
|
bool canRemoveRange(SourceRange range);
|
|
|
|
bool canReplaceRange(SourceRange range, SourceRange replacementRange);
|
2011-07-23 18:55:15 +08:00
|
|
|
bool canReplaceText(SourceLocation loc, StringRef text);
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
void commitInsert(SourceLocation loc, StringRef text);
|
|
|
|
void commitInsertAfterToken(SourceLocation loc, StringRef text);
|
|
|
|
void commitRemove(SourceRange range);
|
|
|
|
void commitRemoveStmt(Stmt *S);
|
|
|
|
void commitReplace(SourceRange range, SourceRange replacementRange);
|
2011-07-23 18:55:15 +08:00
|
|
|
void commitReplaceText(SourceLocation loc, StringRef text,
|
|
|
|
StringRef replacementText);
|
2011-06-16 07:25:17 +08:00
|
|
|
void commitIncreaseIndentation(SourceRange range,SourceLocation parentIndent);
|
2011-07-24 01:14:25 +08:00
|
|
|
void commitClearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
void addRemoval(CharSourceRange range);
|
|
|
|
void addInsertion(SourceLocation loc, StringRef text);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Stores text passed to the transformation methods to keep the string
|
2011-06-16 07:25:17 +08:00
|
|
|
/// "alive". Since the vast majority of text will be the same, we also unique
|
|
|
|
/// the strings using a StringMap.
|
|
|
|
StringRef getUniqueText(StringRef text);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Computes the source location just past the end of the token at
|
2011-06-16 07:25:17 +08:00
|
|
|
/// the given source location. If the location points at a macro, the whole
|
2011-07-15 08:04:31 +08:00
|
|
|
/// macro expansion is skipped.
|
2011-06-16 07:25:17 +08:00
|
|
|
static SourceLocation getLocForEndOfToken(SourceLocation loc,
|
|
|
|
SourceManager &SM,Preprocessor &PP);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
void TransformActionsImpl::startTransaction() {
|
|
|
|
assert(!IsInTransaction &&
|
|
|
|
"Cannot start a transaction in the middle of another one");
|
|
|
|
IsInTransaction = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformActionsImpl::commitTransaction() {
|
|
|
|
assert(IsInTransaction && "No transaction started");
|
|
|
|
|
|
|
|
if (CachedActions.empty()) {
|
|
|
|
IsInTransaction = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that all actions are possible otherwise abort the whole transaction.
|
|
|
|
bool AllActionsPossible = true;
|
|
|
|
for (unsigned i = 0, e = CachedActions.size(); i != e; ++i) {
|
|
|
|
ActionData &act = CachedActions[i];
|
|
|
|
switch (act.Kind) {
|
|
|
|
case Act_Insert:
|
|
|
|
if (!canInsert(act.Loc))
|
|
|
|
AllActionsPossible = false;
|
|
|
|
break;
|
|
|
|
case Act_InsertAfterToken:
|
|
|
|
if (!canInsertAfterToken(act.Loc))
|
|
|
|
AllActionsPossible = false;
|
|
|
|
break;
|
|
|
|
case Act_Remove:
|
|
|
|
if (!canRemoveRange(act.R1))
|
|
|
|
AllActionsPossible = false;
|
|
|
|
break;
|
|
|
|
case Act_RemoveStmt:
|
|
|
|
assert(act.S);
|
|
|
|
if (!canRemoveRange(act.S->getSourceRange()))
|
|
|
|
AllActionsPossible = false;
|
|
|
|
break;
|
|
|
|
case Act_Replace:
|
|
|
|
if (!canReplaceRange(act.R1, act.R2))
|
|
|
|
AllActionsPossible = false;
|
|
|
|
break;
|
|
|
|
case Act_ReplaceText:
|
|
|
|
if (!canReplaceText(act.Loc, act.Text1))
|
|
|
|
AllActionsPossible = false;
|
|
|
|
break;
|
|
|
|
case Act_IncreaseIndentation:
|
|
|
|
// This is not important, we don't care if it will fail.
|
|
|
|
break;
|
|
|
|
case Act_ClearDiagnostic:
|
|
|
|
// We are just checking source rewrites.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!AllActionsPossible)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AllActionsPossible) {
|
|
|
|
abortTransaction();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = CachedActions.size(); i != e; ++i) {
|
|
|
|
ActionData &act = CachedActions[i];
|
|
|
|
switch (act.Kind) {
|
|
|
|
case Act_Insert:
|
|
|
|
commitInsert(act.Loc, act.Text1);
|
|
|
|
break;
|
|
|
|
case Act_InsertAfterToken:
|
|
|
|
commitInsertAfterToken(act.Loc, act.Text1);
|
|
|
|
break;
|
|
|
|
case Act_Remove:
|
|
|
|
commitRemove(act.R1);
|
|
|
|
break;
|
|
|
|
case Act_RemoveStmt:
|
|
|
|
commitRemoveStmt(act.S);
|
|
|
|
break;
|
|
|
|
case Act_Replace:
|
|
|
|
commitReplace(act.R1, act.R2);
|
|
|
|
break;
|
|
|
|
case Act_ReplaceText:
|
|
|
|
commitReplaceText(act.Loc, act.Text1, act.Text2);
|
|
|
|
break;
|
|
|
|
case Act_IncreaseIndentation:
|
|
|
|
commitIncreaseIndentation(act.R1, act.Loc);
|
|
|
|
break;
|
|
|
|
case Act_ClearDiagnostic:
|
|
|
|
commitClearDiagnostic(act.DiagIDs, act.R1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedActions.clear();
|
|
|
|
IsInTransaction = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::abortTransaction() {
|
|
|
|
assert(IsInTransaction && "No transaction started");
|
|
|
|
CachedActions.clear();
|
|
|
|
IsInTransaction = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::insert(SourceLocation loc, StringRef text) {
|
|
|
|
assert(IsInTransaction && "Actions only allowed during a transaction");
|
|
|
|
text = getUniqueText(text);
|
|
|
|
ActionData data;
|
|
|
|
data.Kind = Act_Insert;
|
|
|
|
data.Loc = loc;
|
|
|
|
data.Text1 = text;
|
|
|
|
CachedActions.push_back(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::insertAfterToken(SourceLocation loc, StringRef text) {
|
|
|
|
assert(IsInTransaction && "Actions only allowed during a transaction");
|
|
|
|
text = getUniqueText(text);
|
|
|
|
ActionData data;
|
|
|
|
data.Kind = Act_InsertAfterToken;
|
|
|
|
data.Loc = loc;
|
|
|
|
data.Text1 = text;
|
|
|
|
CachedActions.push_back(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::remove(SourceRange range) {
|
|
|
|
assert(IsInTransaction && "Actions only allowed during a transaction");
|
|
|
|
ActionData data;
|
|
|
|
data.Kind = Act_Remove;
|
|
|
|
data.R1 = range;
|
|
|
|
CachedActions.push_back(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::removeStmt(Stmt *S) {
|
|
|
|
assert(IsInTransaction && "Actions only allowed during a transaction");
|
|
|
|
ActionData data;
|
|
|
|
data.Kind = Act_RemoveStmt;
|
2019-02-04 03:50:56 +08:00
|
|
|
if (auto *E = dyn_cast<Expr>(S))
|
|
|
|
S = E->IgnoreImplicit(); // important for uniquing
|
|
|
|
data.S = S;
|
2011-06-16 07:25:17 +08:00
|
|
|
CachedActions.push_back(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::replace(SourceRange range, StringRef text) {
|
|
|
|
assert(IsInTransaction && "Actions only allowed during a transaction");
|
|
|
|
text = getUniqueText(text);
|
|
|
|
remove(range);
|
|
|
|
insert(range.getBegin(), text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::replace(SourceRange range,
|
|
|
|
SourceRange replacementRange) {
|
|
|
|
assert(IsInTransaction && "Actions only allowed during a transaction");
|
|
|
|
ActionData data;
|
|
|
|
data.Kind = Act_Replace;
|
|
|
|
data.R1 = range;
|
|
|
|
data.R2 = replacementRange;
|
|
|
|
CachedActions.push_back(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::replaceText(SourceLocation loc, StringRef text,
|
|
|
|
StringRef replacementText) {
|
|
|
|
text = getUniqueText(text);
|
|
|
|
replacementText = getUniqueText(replacementText);
|
|
|
|
ActionData data;
|
|
|
|
data.Kind = Act_ReplaceText;
|
|
|
|
data.Loc = loc;
|
|
|
|
data.Text1 = text;
|
|
|
|
data.Text2 = replacementText;
|
|
|
|
CachedActions.push_back(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::replaceStmt(Stmt *S, StringRef text) {
|
|
|
|
assert(IsInTransaction && "Actions only allowed during a transaction");
|
|
|
|
text = getUniqueText(text);
|
2018-08-10 05:08:08 +08:00
|
|
|
insert(S->getBeginLoc(), text);
|
2011-06-16 07:25:17 +08:00
|
|
|
removeStmt(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::increaseIndentation(SourceRange range,
|
|
|
|
SourceLocation parentIndent) {
|
|
|
|
if (range.isInvalid()) return;
|
|
|
|
assert(IsInTransaction && "Actions only allowed during a transaction");
|
|
|
|
ActionData data;
|
|
|
|
data.Kind = Act_IncreaseIndentation;
|
|
|
|
data.R1 = range;
|
|
|
|
data.Loc = parentIndent;
|
|
|
|
CachedActions.push_back(data);
|
|
|
|
}
|
|
|
|
|
2011-07-24 01:14:25 +08:00
|
|
|
bool TransformActionsImpl::clearDiagnostic(ArrayRef<unsigned> IDs,
|
2011-06-16 07:25:17 +08:00
|
|
|
SourceRange range) {
|
|
|
|
assert(IsInTransaction && "Actions only allowed during a transaction");
|
|
|
|
if (!CapturedDiags.hasDiagnostic(IDs, range))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ActionData data;
|
|
|
|
data.Kind = Act_ClearDiagnostic;
|
|
|
|
data.R1 = range;
|
|
|
|
data.DiagIDs.append(IDs.begin(), IDs.end());
|
|
|
|
CachedActions.push_back(data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformActionsImpl::canInsert(SourceLocation loc) {
|
|
|
|
if (loc.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SourceManager &SM = Ctx.getSourceManager();
|
2011-07-26 00:49:02 +08:00
|
|
|
if (SM.isInSystemHeader(SM.getExpansionLoc(loc)))
|
2011-06-16 07:25:17 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (loc.isFileID())
|
|
|
|
return true;
|
2011-07-14 16:20:40 +08:00
|
|
|
return PP.isAtStartOfMacroExpansion(loc);
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformActionsImpl::canInsertAfterToken(SourceLocation loc) {
|
|
|
|
if (loc.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SourceManager &SM = Ctx.getSourceManager();
|
2011-07-26 00:49:02 +08:00
|
|
|
if (SM.isInSystemHeader(SM.getExpansionLoc(loc)))
|
2011-06-16 07:25:17 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (loc.isFileID())
|
|
|
|
return true;
|
2011-07-14 16:20:40 +08:00
|
|
|
return PP.isAtEndOfMacroExpansion(loc);
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformActionsImpl::canRemoveRange(SourceRange range) {
|
|
|
|
return canInsert(range.getBegin()) && canInsertAfterToken(range.getEnd());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformActionsImpl::canReplaceRange(SourceRange range,
|
|
|
|
SourceRange replacementRange) {
|
|
|
|
return canRemoveRange(range) && canRemoveRange(replacementRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformActionsImpl::canReplaceText(SourceLocation loc, StringRef text) {
|
|
|
|
if (!canInsert(loc))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SourceManager &SM = Ctx.getSourceManager();
|
2011-07-26 00:49:02 +08:00
|
|
|
loc = SM.getExpansionLoc(loc);
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
// Break down the source location.
|
|
|
|
std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
|
|
|
|
|
|
|
|
// Try to load the file buffer.
|
|
|
|
bool invalidTemp = false;
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
|
2011-06-16 07:25:17 +08:00
|
|
|
if (invalidTemp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return file.substr(locInfo.second).startswith(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::commitInsert(SourceLocation loc, StringRef text) {
|
|
|
|
addInsertion(loc, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::commitInsertAfterToken(SourceLocation loc,
|
|
|
|
StringRef text) {
|
|
|
|
addInsertion(getLocForEndOfToken(loc, Ctx.getSourceManager(), PP), text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::commitRemove(SourceRange range) {
|
|
|
|
addRemoval(CharSourceRange::getTokenRange(range));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::commitRemoveStmt(Stmt *S) {
|
|
|
|
assert(S);
|
|
|
|
if (StmtRemovals.count(S))
|
|
|
|
return; // already removed.
|
|
|
|
|
|
|
|
if (Expr *E = dyn_cast<Expr>(S)) {
|
|
|
|
commitRemove(E->getSourceRange());
|
|
|
|
commitInsert(E->getSourceRange().getBegin(), getARCMTMacroName());
|
|
|
|
} else
|
|
|
|
commitRemove(S->getSourceRange());
|
|
|
|
|
|
|
|
StmtRemovals.insert(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::commitReplace(SourceRange range,
|
|
|
|
SourceRange replacementRange) {
|
|
|
|
RangeComparison comp = CharRange::compare(replacementRange, range,
|
|
|
|
Ctx.getSourceManager(), PP);
|
|
|
|
assert(comp == Range_Contained);
|
|
|
|
if (comp != Range_Contained)
|
|
|
|
return; // Although we asserted, be extra safe for release build.
|
|
|
|
if (range.getBegin() != replacementRange.getBegin())
|
|
|
|
addRemoval(CharSourceRange::getCharRange(range.getBegin(),
|
|
|
|
replacementRange.getBegin()));
|
|
|
|
if (replacementRange.getEnd() != range.getEnd())
|
|
|
|
addRemoval(CharSourceRange::getTokenRange(
|
|
|
|
getLocForEndOfToken(replacementRange.getEnd(),
|
|
|
|
Ctx.getSourceManager(), PP),
|
|
|
|
range.getEnd()));
|
|
|
|
}
|
|
|
|
void TransformActionsImpl::commitReplaceText(SourceLocation loc,
|
|
|
|
StringRef text,
|
|
|
|
StringRef replacementText) {
|
|
|
|
SourceManager &SM = Ctx.getSourceManager();
|
2011-07-26 00:49:02 +08:00
|
|
|
loc = SM.getExpansionLoc(loc);
|
2011-06-16 07:25:17 +08:00
|
|
|
// canReplaceText already checked if loc points at text.
|
2011-09-20 04:40:19 +08:00
|
|
|
SourceLocation afterText = loc.getLocWithOffset(text.size());
|
2011-06-16 07:25:17 +08:00
|
|
|
|
|
|
|
addRemoval(CharSourceRange::getCharRange(loc, afterText));
|
2018-07-31 03:24:48 +08:00
|
|
|
commitInsert(loc, replacementText);
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::commitIncreaseIndentation(SourceRange range,
|
|
|
|
SourceLocation parentIndent) {
|
|
|
|
SourceManager &SM = Ctx.getSourceManager();
|
|
|
|
IndentationRanges.push_back(
|
|
|
|
std::make_pair(CharRange(CharSourceRange::getTokenRange(range),
|
|
|
|
SM, PP),
|
2011-07-26 00:49:02 +08:00
|
|
|
SM.getExpansionLoc(parentIndent)));
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
|
2011-07-24 01:14:25 +08:00
|
|
|
void TransformActionsImpl::commitClearDiagnostic(ArrayRef<unsigned> IDs,
|
2011-06-16 07:25:17 +08:00
|
|
|
SourceRange range) {
|
|
|
|
CapturedDiags.clearDiagnostic(IDs, range);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::addInsertion(SourceLocation loc, StringRef text) {
|
|
|
|
SourceManager &SM = Ctx.getSourceManager();
|
2011-07-26 00:49:02 +08:00
|
|
|
loc = SM.getExpansionLoc(loc);
|
2015-07-31 01:22:52 +08:00
|
|
|
for (const CharRange &I : llvm::reverse(Removals)) {
|
|
|
|
if (!SM.isBeforeInTranslationUnit(loc, I.End))
|
2011-06-16 07:25:17 +08:00
|
|
|
break;
|
2015-07-31 01:22:52 +08:00
|
|
|
if (I.Begin.isBeforeInTranslationUnitThan(loc))
|
2011-06-16 07:25:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Inserts[FullSourceLoc(loc, SM)].push_back(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::addRemoval(CharSourceRange range) {
|
|
|
|
CharRange newRange(range, Ctx.getSourceManager(), PP);
|
|
|
|
if (newRange.Begin == newRange.End)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Inserts.erase(Inserts.upper_bound(newRange.Begin),
|
|
|
|
Inserts.lower_bound(newRange.End));
|
|
|
|
|
|
|
|
std::list<CharRange>::iterator I = Removals.end();
|
|
|
|
while (I != Removals.begin()) {
|
|
|
|
std::list<CharRange>::iterator RI = I;
|
|
|
|
--RI;
|
|
|
|
RangeComparison comp = newRange.compareWith(*RI);
|
|
|
|
switch (comp) {
|
|
|
|
case Range_Before:
|
|
|
|
--I;
|
|
|
|
break;
|
|
|
|
case Range_After:
|
|
|
|
Removals.insert(I, newRange);
|
|
|
|
return;
|
|
|
|
case Range_Contained:
|
|
|
|
return;
|
|
|
|
case Range_Contains:
|
|
|
|
RI->End = newRange.End;
|
2017-06-03 14:23:19 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2011-06-16 07:25:17 +08:00
|
|
|
case Range_ExtendsBegin:
|
|
|
|
newRange.End = RI->End;
|
|
|
|
Removals.erase(RI);
|
|
|
|
break;
|
|
|
|
case Range_ExtendsEnd:
|
|
|
|
RI->End = newRange.End;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Removals.insert(Removals.begin(), newRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActionsImpl::applyRewrites(
|
|
|
|
TransformActions::RewriteReceiver &receiver) {
|
|
|
|
for (InsertsMap::iterator I = Inserts.begin(), E = Inserts.end(); I!=E; ++I) {
|
|
|
|
SourceLocation loc = I->first;
|
|
|
|
for (TextsVec::iterator
|
|
|
|
TI = I->second.begin(), TE = I->second.end(); TI != TE; ++TI) {
|
|
|
|
receiver.insert(loc, *TI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<std::pair<CharRange, SourceLocation> >::iterator
|
|
|
|
I = IndentationRanges.begin(), E = IndentationRanges.end(); I!=E; ++I) {
|
|
|
|
CharSourceRange range = CharSourceRange::getCharRange(I->first.Begin,
|
|
|
|
I->first.End);
|
|
|
|
receiver.increaseIndentation(range, I->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::list<CharRange>::iterator
|
|
|
|
I = Removals.begin(), E = Removals.end(); I != E; ++I) {
|
|
|
|
CharSourceRange range = CharSourceRange::getCharRange(I->Begin, I->End);
|
|
|
|
receiver.remove(range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Stores text passed to the transformation methods to keep the string
|
2011-06-16 07:25:17 +08:00
|
|
|
/// "alive". Since the vast majority of text will be the same, we also unique
|
|
|
|
/// the strings using a StringMap.
|
|
|
|
StringRef TransformActionsImpl::getUniqueText(StringRef text) {
|
2014-11-19 11:06:06 +08:00
|
|
|
return UniqueText.insert(std::make_pair(text, false)).first->first();
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Computes the source location just past the end of the token at
|
2011-06-16 07:25:17 +08:00
|
|
|
/// the given source location. If the location points at a macro, the whole
|
2011-07-15 08:04:31 +08:00
|
|
|
/// macro expansion is skipped.
|
2011-06-16 07:25:17 +08:00
|
|
|
SourceLocation TransformActionsImpl::getLocForEndOfToken(SourceLocation loc,
|
|
|
|
SourceManager &SM,
|
|
|
|
Preprocessor &PP) {
|
2018-04-30 13:25:48 +08:00
|
|
|
if (loc.isMacroID()) {
|
|
|
|
CharSourceRange Exp = SM.getExpansionRange(loc);
|
|
|
|
if (Exp.isCharRange())
|
|
|
|
return Exp.getEnd();
|
|
|
|
loc = Exp.getEnd();
|
|
|
|
}
|
2011-06-16 07:25:17 +08:00
|
|
|
return PP.getLocForEndOfToken(loc);
|
|
|
|
}
|
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
TransformActions::RewriteReceiver::~RewriteReceiver() { }
|
2011-06-16 07:25:17 +08:00
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
TransformActions::TransformActions(DiagnosticsEngine &diag,
|
2011-06-16 07:25:17 +08:00
|
|
|
CapturedDiagList &capturedDiags,
|
|
|
|
ASTContext &ctx, Preprocessor &PP)
|
2014-07-03 01:08:00 +08:00
|
|
|
: Diags(diag), CapturedDiags(capturedDiags) {
|
2011-06-16 07:25:17 +08:00
|
|
|
Impl = new TransformActionsImpl(capturedDiags, ctx, PP);
|
|
|
|
}
|
|
|
|
|
|
|
|
TransformActions::~TransformActions() {
|
|
|
|
delete static_cast<TransformActionsImpl*>(Impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActions::startTransaction() {
|
|
|
|
static_cast<TransformActionsImpl*>(Impl)->startTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformActions::commitTransaction() {
|
|
|
|
return static_cast<TransformActionsImpl*>(Impl)->commitTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActions::abortTransaction() {
|
|
|
|
static_cast<TransformActionsImpl*>(Impl)->abortTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void TransformActions::insert(SourceLocation loc, StringRef text) {
|
2011-06-16 07:25:17 +08:00
|
|
|
static_cast<TransformActionsImpl*>(Impl)->insert(loc, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActions::insertAfterToken(SourceLocation loc,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef text) {
|
2011-06-16 07:25:17 +08:00
|
|
|
static_cast<TransformActionsImpl*>(Impl)->insertAfterToken(loc, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActions::remove(SourceRange range) {
|
|
|
|
static_cast<TransformActionsImpl*>(Impl)->remove(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActions::removeStmt(Stmt *S) {
|
|
|
|
static_cast<TransformActionsImpl*>(Impl)->removeStmt(S);
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void TransformActions::replace(SourceRange range, StringRef text) {
|
2011-06-16 07:25:17 +08:00
|
|
|
static_cast<TransformActionsImpl*>(Impl)->replace(range, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActions::replace(SourceRange range,
|
|
|
|
SourceRange replacementRange) {
|
|
|
|
static_cast<TransformActionsImpl*>(Impl)->replace(range, replacementRange);
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void TransformActions::replaceStmt(Stmt *S, StringRef text) {
|
2011-06-16 07:25:17 +08:00
|
|
|
static_cast<TransformActionsImpl*>(Impl)->replaceStmt(S, text);
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void TransformActions::replaceText(SourceLocation loc, StringRef text,
|
|
|
|
StringRef replacementText) {
|
2011-06-16 07:25:17 +08:00
|
|
|
static_cast<TransformActionsImpl*>(Impl)->replaceText(loc, text,
|
|
|
|
replacementText);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActions::increaseIndentation(SourceRange range,
|
|
|
|
SourceLocation parentIndent) {
|
|
|
|
static_cast<TransformActionsImpl*>(Impl)->increaseIndentation(range,
|
|
|
|
parentIndent);
|
|
|
|
}
|
|
|
|
|
2011-07-24 01:14:25 +08:00
|
|
|
bool TransformActions::clearDiagnostic(ArrayRef<unsigned> IDs,
|
2011-06-16 07:25:17 +08:00
|
|
|
SourceRange range) {
|
|
|
|
return static_cast<TransformActionsImpl*>(Impl)->clearDiagnostic(IDs, range);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformActions::applyRewrites(RewriteReceiver &receiver) {
|
|
|
|
static_cast<TransformActionsImpl*>(Impl)->applyRewrites(receiver);
|
|
|
|
}
|
|
|
|
|
2014-01-26 13:07:32 +08:00
|
|
|
DiagnosticBuilder TransformActions::report(SourceLocation loc, unsigned diagId,
|
|
|
|
SourceRange range) {
|
|
|
|
assert(!static_cast<TransformActionsImpl *>(Impl)->isInTransaction() &&
|
2011-06-16 07:25:17 +08:00
|
|
|
"Errors should be emitted out of a transaction");
|
2014-01-26 13:07:32 +08:00
|
|
|
return Diags.Report(loc, diagId) << range;
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|
|
|
|
|
2014-01-26 13:07:32 +08:00
|
|
|
void TransformActions::reportError(StringRef message, SourceLocation loc,
|
2012-01-25 08:20:29 +08:00
|
|
|
SourceRange range) {
|
2014-01-26 13:07:32 +08:00
|
|
|
report(loc, diag::err_mt_message, range) << message;
|
2012-01-25 08:20:29 +08:00
|
|
|
}
|
|
|
|
|
2014-01-26 13:07:32 +08:00
|
|
|
void TransformActions::reportWarning(StringRef message, SourceLocation loc,
|
|
|
|
SourceRange range) {
|
|
|
|
report(loc, diag::warn_mt_message, range) << message;
|
|
|
|
}
|
2011-11-05 07:43:03 +08:00
|
|
|
|
2014-01-26 13:07:32 +08:00
|
|
|
void TransformActions::reportNote(StringRef message, SourceLocation loc,
|
|
|
|
SourceRange range) {
|
|
|
|
report(loc, diag::note_mt_message, range) << message;
|
2011-06-16 07:25:17 +08:00
|
|
|
}
|