322 lines
12 KiB
C++
322 lines
12 KiB
C++
//===--- Transformer.cpp - Transformer library implementation ---*- C++ -*-===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Tooling/Refactoring/Transformer.h"
|
|
#include "clang/AST/Expr.h"
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
#include "clang/Basic/Diagnostic.h"
|
|
#include "clang/Basic/SourceLocation.h"
|
|
#include "clang/Rewrite/Core/Rewriter.h"
|
|
#include "clang/Tooling/Refactoring/AtomicChange.h"
|
|
#include "clang/Tooling/Refactoring/SourceCode.h"
|
|
#include "llvm/ADT/Optional.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "llvm/Support/Errc.h"
|
|
#include "llvm/Support/Error.h"
|
|
#include <deque>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
using namespace clang;
|
|
using namespace tooling;
|
|
|
|
using ast_matchers::MatchFinder;
|
|
using ast_matchers::internal::DynTypedMatcher;
|
|
using ast_type_traits::ASTNodeKind;
|
|
using ast_type_traits::DynTypedNode;
|
|
using llvm::Error;
|
|
using llvm::Expected;
|
|
using llvm::Optional;
|
|
using llvm::StringError;
|
|
using llvm::StringRef;
|
|
using llvm::Twine;
|
|
|
|
using MatchResult = MatchFinder::MatchResult;
|
|
|
|
// Did the text at this location originate in a macro definition (aka. body)?
|
|
// For example,
|
|
//
|
|
// #define NESTED(x) x
|
|
// #define MACRO(y) { int y = NESTED(3); }
|
|
// if (true) MACRO(foo)
|
|
//
|
|
// The if statement expands to
|
|
//
|
|
// if (true) { int foo = 3; }
|
|
// ^ ^
|
|
// Loc1 Loc2
|
|
//
|
|
// For SourceManager SM, SM.isMacroArgExpansion(Loc1) and
|
|
// SM.isMacroArgExpansion(Loc2) are both true, but isOriginMacroBody(sm, Loc1)
|
|
// is false, because "foo" originated in the source file (as an argument to a
|
|
// macro), whereas isOriginMacroBody(SM, Loc2) is true, because "3" originated
|
|
// in the definition of MACRO.
|
|
static bool isOriginMacroBody(const clang::SourceManager &SM,
|
|
clang::SourceLocation Loc) {
|
|
while (Loc.isMacroID()) {
|
|
if (SM.isMacroBodyExpansion(Loc))
|
|
return true;
|
|
// Otherwise, it must be in an argument, so we continue searching up the
|
|
// invocation stack. getImmediateMacroCallerLoc() gives the location of the
|
|
// argument text, inside the call text.
|
|
Loc = SM.getImmediateMacroCallerLoc(Loc);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static llvm::Error invalidArgumentError(Twine Message) {
|
|
return llvm::make_error<StringError>(llvm::errc::invalid_argument, Message);
|
|
}
|
|
|
|
static llvm::Error typeError(StringRef Id, const ASTNodeKind &Kind,
|
|
Twine Message) {
|
|
return invalidArgumentError(
|
|
Message + " (node id=" + Id + " kind=" + Kind.asStringRef() + ")");
|
|
}
|
|
|
|
static llvm::Error missingPropertyError(StringRef Id, Twine Description,
|
|
StringRef Property) {
|
|
return invalidArgumentError(Description + " requires property '" + Property +
|
|
"' (node id=" + Id + ")");
|
|
}
|
|
|
|
static Expected<CharSourceRange>
|
|
getTargetRange(StringRef Target, const DynTypedNode &Node, ASTNodeKind Kind,
|
|
NodePart TargetPart, ASTContext &Context) {
|
|
switch (TargetPart) {
|
|
case NodePart::Node: {
|
|
// For non-expression statements, associate any trailing semicolon with the
|
|
// statement text. However, if the target was intended as an expression (as
|
|
// indicated by its kind) then we do not associate any trailing semicolon
|
|
// with it. We only associate the exact expression text.
|
|
if (Node.get<Stmt>() != nullptr) {
|
|
auto ExprKind = ASTNodeKind::getFromNodeKind<clang::Expr>();
|
|
if (!ExprKind.isBaseOf(Kind))
|
|
return getExtendedRange(Node, tok::TokenKind::semi, Context);
|
|
}
|
|
return CharSourceRange::getTokenRange(Node.getSourceRange());
|
|
}
|
|
case NodePart::Member:
|
|
if (auto *M = Node.get<clang::MemberExpr>())
|
|
return CharSourceRange::getTokenRange(
|
|
M->getMemberNameInfo().getSourceRange());
|
|
return typeError(Target, Node.getNodeKind(),
|
|
"NodePart::Member applied to non-MemberExpr");
|
|
case NodePart::Name:
|
|
if (const auto *D = Node.get<clang::NamedDecl>()) {
|
|
if (!D->getDeclName().isIdentifier())
|
|
return missingPropertyError(Target, "NodePart::Name", "identifier");
|
|
SourceLocation L = D->getLocation();
|
|
auto R = CharSourceRange::getTokenRange(L, L);
|
|
// Verify that the range covers exactly the name.
|
|
// FIXME: extend this code to support cases like `operator +` or
|
|
// `foo<int>` for which this range will be too short. Doing so will
|
|
// require subcasing `NamedDecl`, because it doesn't provide virtual
|
|
// access to the \c DeclarationNameInfo.
|
|
if (getText(R, Context) != D->getName())
|
|
return CharSourceRange();
|
|
return R;
|
|
}
|
|
if (const auto *E = Node.get<clang::DeclRefExpr>()) {
|
|
if (!E->getNameInfo().getName().isIdentifier())
|
|
return missingPropertyError(Target, "NodePart::Name", "identifier");
|
|
SourceLocation L = E->getLocation();
|
|
return CharSourceRange::getTokenRange(L, L);
|
|
}
|
|
if (const auto *I = Node.get<clang::CXXCtorInitializer>()) {
|
|
if (!I->isMemberInitializer() && I->isWritten())
|
|
return missingPropertyError(Target, "NodePart::Name",
|
|
"explicit member initializer");
|
|
SourceLocation L = I->getMemberLocation();
|
|
return CharSourceRange::getTokenRange(L, L);
|
|
}
|
|
return typeError(
|
|
Target, Node.getNodeKind(),
|
|
"NodePart::Name applied to neither DeclRefExpr, NamedDecl nor "
|
|
"CXXCtorInitializer");
|
|
}
|
|
llvm_unreachable("Unexpected case in NodePart type.");
|
|
}
|
|
|
|
Expected<SmallVector<tooling::detail::Transformation, 1>>
|
|
tooling::detail::translateEdits(const MatchResult &Result,
|
|
llvm::ArrayRef<ASTEdit> Edits) {
|
|
SmallVector<Transformation, 1> Transformations;
|
|
auto &NodesMap = Result.Nodes.getMap();
|
|
for (const auto &Edit : Edits) {
|
|
auto It = NodesMap.find(Edit.Target);
|
|
assert(It != NodesMap.end() && "Edit target must be bound in the match.");
|
|
|
|
Expected<CharSourceRange> Range = getTargetRange(
|
|
Edit.Target, It->second, Edit.Kind, Edit.Part, *Result.Context);
|
|
if (!Range)
|
|
return Range.takeError();
|
|
if (Range->isInvalid() ||
|
|
isOriginMacroBody(*Result.SourceManager, Range->getBegin()))
|
|
return SmallVector<Transformation, 0>();
|
|
auto Replacement = Edit.Replacement(Result);
|
|
if (!Replacement)
|
|
return Replacement.takeError();
|
|
Transformation T;
|
|
T.Range = *Range;
|
|
T.Replacement = std::move(*Replacement);
|
|
Transformations.push_back(std::move(T));
|
|
}
|
|
return Transformations;
|
|
}
|
|
|
|
RewriteRule tooling::makeRule(DynTypedMatcher M,
|
|
SmallVector<ASTEdit, 1> Edits) {
|
|
return RewriteRule{
|
|
{RewriteRule::Case{std::move(M), std::move(Edits), nullptr}}};
|
|
}
|
|
|
|
// Determines whether A is a base type of B in the class hierarchy, including
|
|
// the implicit relationship of Type and QualType.
|
|
static bool isBaseOf(ASTNodeKind A, ASTNodeKind B) {
|
|
static auto TypeKind = ASTNodeKind::getFromNodeKind<Type>();
|
|
static auto QualKind = ASTNodeKind::getFromNodeKind<QualType>();
|
|
/// Mimic the implicit conversions of Matcher<>.
|
|
/// - From Matcher<Type> to Matcher<QualType>
|
|
/// - From Matcher<Base> to Matcher<Derived>
|
|
return (A.isSame(TypeKind) && B.isSame(QualKind)) || A.isBaseOf(B);
|
|
}
|
|
|
|
// Try to find a common kind to which all of the rule's matchers can be
|
|
// converted.
|
|
static ASTNodeKind
|
|
findCommonKind(const SmallVectorImpl<RewriteRule::Case> &Cases) {
|
|
assert(!Cases.empty() && "Rule must have at least one case.");
|
|
ASTNodeKind JoinKind = Cases[0].Matcher.getSupportedKind();
|
|
// Find a (least) Kind K, for which M.canConvertTo(K) holds, for all matchers
|
|
// M in Rules.
|
|
for (const auto &Case : Cases) {
|
|
auto K = Case.Matcher.getSupportedKind();
|
|
if (isBaseOf(JoinKind, K)) {
|
|
JoinKind = K;
|
|
continue;
|
|
}
|
|
if (K.isSame(JoinKind) || isBaseOf(K, JoinKind))
|
|
// JoinKind is already the lowest.
|
|
continue;
|
|
// K and JoinKind are unrelated -- there is no least common kind.
|
|
return ASTNodeKind();
|
|
}
|
|
return JoinKind;
|
|
}
|
|
|
|
// Binds each rule's matcher to a unique (and deterministic) tag based on
|
|
// `TagBase`.
|
|
static std::vector<DynTypedMatcher>
|
|
taggedMatchers(StringRef TagBase,
|
|
const SmallVectorImpl<RewriteRule::Case> &Cases) {
|
|
std::vector<DynTypedMatcher> Matchers;
|
|
Matchers.reserve(Cases.size());
|
|
size_t count = 0;
|
|
for (const auto &Case : Cases) {
|
|
std::string Tag = (TagBase + Twine(count)).str();
|
|
++count;
|
|
auto M = Case.Matcher.tryBind(Tag);
|
|
assert(M && "RewriteRule matchers should be bindable.");
|
|
Matchers.push_back(*std::move(M));
|
|
}
|
|
return Matchers;
|
|
}
|
|
|
|
// Simply gathers the contents of the various rules into a single rule. The
|
|
// actual work to combine these into an ordered choice is deferred to matcher
|
|
// registration.
|
|
RewriteRule tooling::applyFirst(ArrayRef<RewriteRule> Rules) {
|
|
RewriteRule R;
|
|
for (auto &Rule : Rules)
|
|
R.Cases.append(Rule.Cases.begin(), Rule.Cases.end());
|
|
return R;
|
|
}
|
|
|
|
static DynTypedMatcher joinCaseMatchers(const RewriteRule &Rule) {
|
|
assert(!Rule.Cases.empty() && "Rule must have at least one case.");
|
|
if (Rule.Cases.size() == 1)
|
|
return Rule.Cases[0].Matcher;
|
|
|
|
auto CommonKind = findCommonKind(Rule.Cases);
|
|
assert(!CommonKind.isNone() && "Cases must have compatible matchers.");
|
|
return DynTypedMatcher::constructVariadic(
|
|
DynTypedMatcher::VO_AnyOf, CommonKind, taggedMatchers("Tag", Rule.Cases));
|
|
}
|
|
|
|
DynTypedMatcher tooling::detail::buildMatcher(const RewriteRule &Rule) {
|
|
DynTypedMatcher M = joinCaseMatchers(Rule);
|
|
M.setAllowBind(true);
|
|
// `tryBind` is guaranteed to succeed, because `AllowBind` was set to true.
|
|
return *M.tryBind(RewriteRule::RootId);
|
|
}
|
|
|
|
// Finds the case that was "selected" -- that is, whose matcher triggered the
|
|
// `MatchResult`.
|
|
const RewriteRule::Case &
|
|
tooling::detail::findSelectedCase(const MatchResult &Result,
|
|
const RewriteRule &Rule) {
|
|
if (Rule.Cases.size() == 1)
|
|
return Rule.Cases[0];
|
|
|
|
auto &NodesMap = Result.Nodes.getMap();
|
|
for (size_t i = 0, N = Rule.Cases.size(); i < N; ++i) {
|
|
std::string Tag = ("Tag" + Twine(i)).str();
|
|
if (NodesMap.find(Tag) != NodesMap.end())
|
|
return Rule.Cases[i];
|
|
}
|
|
llvm_unreachable("No tag found for this rule.");
|
|
}
|
|
|
|
constexpr llvm::StringLiteral RewriteRule::RootId;
|
|
|
|
void Transformer::registerMatchers(MatchFinder *MatchFinder) {
|
|
MatchFinder->addDynamicMatcher(tooling::detail::buildMatcher(Rule), this);
|
|
}
|
|
|
|
void Transformer::run(const MatchResult &Result) {
|
|
if (Result.Context->getDiagnostics().hasErrorOccurred())
|
|
return;
|
|
|
|
// Verify the existence and validity of the AST node that roots this rule.
|
|
auto &NodesMap = Result.Nodes.getMap();
|
|
auto Root = NodesMap.find(RewriteRule::RootId);
|
|
assert(Root != NodesMap.end() && "Transformation failed: missing root node.");
|
|
SourceLocation RootLoc = Result.SourceManager->getExpansionLoc(
|
|
Root->second.getSourceRange().getBegin());
|
|
assert(RootLoc.isValid() && "Invalid location for Root node of match.");
|
|
|
|
auto Transformations = tooling::detail::translateEdits(
|
|
Result, tooling::detail::findSelectedCase(Result, Rule).Edits);
|
|
if (!Transformations) {
|
|
Consumer(Transformations.takeError());
|
|
return;
|
|
}
|
|
|
|
if (Transformations->empty()) {
|
|
// No rewrite applied (but no error encountered either).
|
|
RootLoc.print(llvm::errs() << "note: skipping match at loc ",
|
|
*Result.SourceManager);
|
|
llvm::errs() << "\n";
|
|
return;
|
|
}
|
|
|
|
// Record the results in the AtomicChange.
|
|
AtomicChange AC(*Result.SourceManager, RootLoc);
|
|
for (const auto &T : *Transformations) {
|
|
if (auto Err = AC.replace(*Result.SourceManager, T.Range, T.Replacement)) {
|
|
Consumer(std::move(Err));
|
|
return;
|
|
}
|
|
}
|
|
|
|
Consumer(std::move(AC));
|
|
}
|