2012-07-16 17:18:17 +08:00
|
|
|
//===--- RefactoringCallbacks.cpp - Structural query framework ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Lex/Lexer.h"
|
2012-07-17 16:03:01 +08:00
|
|
|
#include "clang/Tooling/RefactoringCallbacks.h"
|
2012-07-16 17:18:17 +08:00
|
|
|
|
|
|
|
namespace clang {
|
2012-07-17 16:37:03 +08:00
|
|
|
namespace tooling {
|
2012-07-16 17:18:17 +08:00
|
|
|
|
|
|
|
RefactoringCallback::RefactoringCallback() {}
|
|
|
|
tooling::Replacements &RefactoringCallback::getReplacements() {
|
|
|
|
return Replace;
|
|
|
|
}
|
|
|
|
|
2012-07-17 16:37:03 +08:00
|
|
|
static Replacement replaceStmtWithText(SourceManager &Sources,
|
|
|
|
const Stmt &From,
|
|
|
|
StringRef Text) {
|
2012-07-16 17:18:17 +08:00
|
|
|
return tooling::Replacement(Sources, CharSourceRange::getTokenRange(
|
|
|
|
From.getSourceRange()), Text);
|
|
|
|
}
|
2012-07-17 16:37:03 +08:00
|
|
|
static Replacement replaceStmtWithStmt(SourceManager &Sources,
|
|
|
|
const Stmt &From,
|
|
|
|
const Stmt &To) {
|
2012-07-16 17:18:17 +08:00
|
|
|
return replaceStmtWithText(Sources, From, Lexer::getSourceText(
|
|
|
|
CharSourceRange::getTokenRange(To.getSourceRange()),
|
|
|
|
Sources, LangOptions()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplaceStmtWithText::ReplaceStmtWithText(StringRef FromId, StringRef ToText)
|
|
|
|
: FromId(FromId), ToText(ToText) {}
|
|
|
|
|
2012-07-17 16:37:03 +08:00
|
|
|
void ReplaceStmtWithText::run(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result) {
|
2016-12-14 00:19:34 +08:00
|
|
|
if (const Stmt *FromMatch = Result.Nodes.getNodeAs<Stmt>(FromId)) {
|
2016-08-01 18:16:37 +08:00
|
|
|
auto Err = Replace.add(tooling::Replacement(
|
2012-07-16 17:18:17 +08:00
|
|
|
*Result.SourceManager,
|
2016-08-01 18:16:37 +08:00
|
|
|
CharSourceRange::getTokenRange(FromMatch->getSourceRange()), ToText));
|
|
|
|
// FIXME: better error handling. For now, just print error message in the
|
|
|
|
// release version.
|
2016-12-23 19:40:44 +08:00
|
|
|
if (Err) {
|
2016-08-01 18:16:37 +08:00
|
|
|
llvm::errs() << llvm::toString(std::move(Err)) << "\n";
|
2016-12-23 19:40:44 +08:00
|
|
|
assert(false);
|
|
|
|
}
|
2012-07-16 17:18:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId)
|
|
|
|
: FromId(FromId), ToId(ToId) {}
|
|
|
|
|
2012-07-17 16:37:03 +08:00
|
|
|
void ReplaceStmtWithStmt::run(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result) {
|
2016-12-14 00:19:34 +08:00
|
|
|
const Stmt *FromMatch = Result.Nodes.getNodeAs<Stmt>(FromId);
|
|
|
|
const Stmt *ToMatch = Result.Nodes.getNodeAs<Stmt>(ToId);
|
2016-08-01 18:16:37 +08:00
|
|
|
if (FromMatch && ToMatch) {
|
|
|
|
auto Err = Replace.add(
|
|
|
|
replaceStmtWithStmt(*Result.SourceManager, *FromMatch, *ToMatch));
|
|
|
|
// FIXME: better error handling. For now, just print error message in the
|
|
|
|
// release version.
|
2016-12-23 19:40:44 +08:00
|
|
|
if (Err) {
|
2016-08-01 18:16:37 +08:00
|
|
|
llvm::errs() << llvm::toString(std::move(Err)) << "\n";
|
2016-12-23 19:40:44 +08:00
|
|
|
assert(false);
|
|
|
|
}
|
2016-08-01 18:16:37 +08:00
|
|
|
}
|
2012-07-16 17:18:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id,
|
|
|
|
bool PickTrueBranch)
|
|
|
|
: Id(Id), PickTrueBranch(PickTrueBranch) {}
|
|
|
|
|
2012-07-17 16:37:03 +08:00
|
|
|
void ReplaceIfStmtWithItsBody::run(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result) {
|
2016-12-14 00:19:34 +08:00
|
|
|
if (const IfStmt *Node = Result.Nodes.getNodeAs<IfStmt>(Id)) {
|
2012-07-16 17:18:17 +08:00
|
|
|
const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse();
|
|
|
|
if (Body) {
|
2016-08-01 18:16:37 +08:00
|
|
|
auto Err =
|
|
|
|
Replace.add(replaceStmtWithStmt(*Result.SourceManager, *Node, *Body));
|
|
|
|
// FIXME: better error handling. For now, just print error message in the
|
|
|
|
// release version.
|
2016-12-23 19:40:44 +08:00
|
|
|
if (Err) {
|
2016-08-01 18:16:37 +08:00
|
|
|
llvm::errs() << llvm::toString(std::move(Err)) << "\n";
|
2016-12-23 19:40:44 +08:00
|
|
|
assert(false);
|
|
|
|
}
|
2012-07-16 17:18:17 +08:00
|
|
|
} else if (!PickTrueBranch) {
|
|
|
|
// If we want to use the 'else'-branch, but it doesn't exist, delete
|
|
|
|
// the whole 'if'.
|
2016-08-01 18:16:37 +08:00
|
|
|
auto Err =
|
|
|
|
Replace.add(replaceStmtWithText(*Result.SourceManager, *Node, ""));
|
|
|
|
// FIXME: better error handling. For now, just print error message in the
|
|
|
|
// release version.
|
2016-12-23 19:40:44 +08:00
|
|
|
if (Err) {
|
2016-08-01 18:16:37 +08:00
|
|
|
llvm::errs() << llvm::toString(std::move(Err)) << "\n";
|
2016-12-23 19:40:44 +08:00
|
|
|
assert(false);
|
|
|
|
}
|
2012-07-16 17:18:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-17 16:37:03 +08:00
|
|
|
} // end namespace tooling
|
2012-07-16 17:18:17 +08:00
|
|
|
} // end namespace clang
|