2012-07-16 17:18:17 +08:00
|
|
|
//===--- RefactoringCallbacks.cpp - Structural query framework ------------===//
|
|
|
|
//
|
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
|
2012-07-16 17:18:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-07-17 16:03:01 +08:00
|
|
|
#include "clang/Tooling/RefactoringCallbacks.h"
|
2017-05-10 15:48:45 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using llvm::StringError;
|
|
|
|
using llvm::make_error;
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-10 15:48:45 +08:00
|
|
|
ASTMatchRefactorer::ASTMatchRefactorer(
|
|
|
|
std::map<std::string, Replacements> &FileToReplaces)
|
|
|
|
: FileToReplaces(FileToReplaces) {}
|
|
|
|
|
|
|
|
void ASTMatchRefactorer::addDynamicMatcher(
|
|
|
|
const ast_matchers::internal::DynTypedMatcher &Matcher,
|
|
|
|
RefactoringCallback *Callback) {
|
|
|
|
MatchFinder.addDynamicMatcher(Matcher, Callback);
|
|
|
|
Callbacks.push_back(Callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RefactoringASTConsumer : public ASTConsumer {
|
|
|
|
public:
|
2017-05-12 08:16:56 +08:00
|
|
|
explicit RefactoringASTConsumer(ASTMatchRefactorer &Refactoring)
|
2017-05-10 15:48:45 +08:00
|
|
|
: Refactoring(Refactoring) {}
|
|
|
|
|
|
|
|
void HandleTranslationUnit(ASTContext &Context) override {
|
|
|
|
// The ASTMatchRefactorer is re-used between translation units.
|
|
|
|
// Clear the matchers so that each Replacement is only emitted once.
|
|
|
|
for (const auto &Callback : Refactoring.Callbacks) {
|
|
|
|
Callback->getReplacements().clear();
|
|
|
|
}
|
|
|
|
Refactoring.MatchFinder.matchAST(Context);
|
|
|
|
for (const auto &Callback : Refactoring.Callbacks) {
|
|
|
|
for (const auto &Replacement : Callback->getReplacements()) {
|
|
|
|
llvm::Error Err =
|
|
|
|
Refactoring.FileToReplaces[Replacement.getFilePath()].add(
|
|
|
|
Replacement);
|
|
|
|
if (Err) {
|
|
|
|
llvm::errs() << "Skipping replacement " << Replacement.toString()
|
|
|
|
<< " due to this error:\n"
|
|
|
|
<< toString(std::move(Err)) << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ASTMatchRefactorer &Refactoring;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<ASTConsumer> ASTMatchRefactorer::newASTConsumer() {
|
|
|
|
return llvm::make_unique<RefactoringASTConsumer>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Replacement replaceStmtWithText(SourceManager &Sources, const Stmt &From,
|
2012-07-17 16:37:03 +08:00
|
|
|
StringRef Text) {
|
2017-05-10 15:48:45 +08:00
|
|
|
return tooling::Replacement(
|
|
|
|
Sources, CharSourceRange::getTokenRange(From.getSourceRange()), Text);
|
2012-07-16 17:18:17 +08:00
|
|
|
}
|
2017-05-10 15:48:45 +08:00
|
|
|
static Replacement replaceStmtWithStmt(SourceManager &Sources, const Stmt &From,
|
2012-07-17 16:37:03 +08:00
|
|
|
const Stmt &To) {
|
2017-05-10 15:48:45 +08:00
|
|
|
return replaceStmtWithText(
|
|
|
|
Sources, From,
|
|
|
|
Lexer::getSourceText(CharSourceRange::getTokenRange(To.getSourceRange()),
|
|
|
|
Sources, LangOptions()));
|
2012-07-16 17:18:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 15:48:45 +08:00
|
|
|
ReplaceNodeWithTemplate::ReplaceNodeWithTemplate(
|
2017-05-12 08:16:56 +08:00
|
|
|
llvm::StringRef FromId, std::vector<TemplateElement> Template)
|
|
|
|
: FromId(FromId), Template(std::move(Template)) {}
|
2017-05-10 15:48:45 +08:00
|
|
|
|
|
|
|
llvm::Expected<std::unique_ptr<ReplaceNodeWithTemplate>>
|
|
|
|
ReplaceNodeWithTemplate::create(StringRef FromId, StringRef ToTemplate) {
|
|
|
|
std::vector<TemplateElement> ParsedTemplate;
|
|
|
|
for (size_t Index = 0; Index < ToTemplate.size();) {
|
|
|
|
if (ToTemplate[Index] == '$') {
|
|
|
|
if (ToTemplate.substr(Index, 2) == "$$") {
|
|
|
|
Index += 2;
|
|
|
|
ParsedTemplate.push_back(
|
|
|
|
TemplateElement{TemplateElement::Literal, "$"});
|
|
|
|
} else if (ToTemplate.substr(Index, 2) == "${") {
|
|
|
|
size_t EndOfIdentifier = ToTemplate.find("}", Index);
|
|
|
|
if (EndOfIdentifier == std::string::npos) {
|
|
|
|
return make_error<StringError>(
|
|
|
|
"Unterminated ${...} in replacement template near " +
|
|
|
|
ToTemplate.substr(Index),
|
2017-05-11 06:30:44 +08:00
|
|
|
llvm::inconvertibleErrorCode());
|
2017-05-10 15:48:45 +08:00
|
|
|
}
|
|
|
|
std::string SourceNodeName =
|
|
|
|
ToTemplate.substr(Index + 2, EndOfIdentifier - Index - 2);
|
|
|
|
ParsedTemplate.push_back(
|
|
|
|
TemplateElement{TemplateElement::Identifier, SourceNodeName});
|
|
|
|
Index = EndOfIdentifier + 1;
|
|
|
|
} else {
|
|
|
|
return make_error<StringError>(
|
|
|
|
"Invalid $ in replacement template near " +
|
|
|
|
ToTemplate.substr(Index),
|
2017-05-11 06:30:44 +08:00
|
|
|
llvm::inconvertibleErrorCode());
|
2017-05-10 15:48:45 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size_t NextIndex = ToTemplate.find('$', Index + 1);
|
|
|
|
ParsedTemplate.push_back(
|
|
|
|
TemplateElement{TemplateElement::Literal,
|
|
|
|
ToTemplate.substr(Index, NextIndex - Index)});
|
|
|
|
Index = NextIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::unique_ptr<ReplaceNodeWithTemplate>(
|
|
|
|
new ReplaceNodeWithTemplate(FromId, std::move(ParsedTemplate)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplaceNodeWithTemplate::run(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result) {
|
|
|
|
const auto &NodeMap = Result.Nodes.getMap();
|
|
|
|
|
|
|
|
std::string ToText;
|
|
|
|
for (const auto &Element : Template) {
|
|
|
|
switch (Element.Type) {
|
|
|
|
case TemplateElement::Literal:
|
|
|
|
ToText += Element.Value;
|
|
|
|
break;
|
|
|
|
case TemplateElement::Identifier: {
|
|
|
|
auto NodeIter = NodeMap.find(Element.Value);
|
|
|
|
if (NodeIter == NodeMap.end()) {
|
|
|
|
llvm::errs() << "Node " << Element.Value
|
|
|
|
<< " used in replacement template not bound in Matcher \n";
|
|
|
|
llvm::report_fatal_error("Unbound node in replacement template.");
|
|
|
|
}
|
|
|
|
CharSourceRange Source =
|
|
|
|
CharSourceRange::getTokenRange(NodeIter->second.getSourceRange());
|
|
|
|
ToText += Lexer::getSourceText(Source, *Result.SourceManager,
|
|
|
|
Result.Context->getLangOpts());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NodeMap.count(FromId) == 0) {
|
|
|
|
llvm::errs() << "Node to be replaced " << FromId
|
|
|
|
<< " not bound in query.\n";
|
|
|
|
llvm::report_fatal_error("FromId node not bound in MatchResult");
|
|
|
|
}
|
|
|
|
auto Replacement =
|
|
|
|
tooling::Replacement(*Result.SourceManager, &NodeMap.at(FromId), ToText,
|
|
|
|
Result.Context->getLangOpts());
|
|
|
|
llvm::Error Err = Replace.add(Replacement);
|
|
|
|
if (Err) {
|
|
|
|
llvm::errs() << "Query and replace failed in " << Replacement.getFilePath()
|
|
|
|
<< "! " << llvm::toString(std::move(Err)) << "\n";
|
|
|
|
llvm::report_fatal_error("Replacement failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-17 16:37:03 +08:00
|
|
|
} // end namespace tooling
|
2012-07-16 17:18:17 +08:00
|
|
|
} // end namespace clang
|