2015-03-09 20:18:39 +08:00
|
|
|
//===--- RedundantSmartptrGetCheck.cpp - clang-tidy -----------------------===//
|
2014-03-28 01:42:26 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-09 20:18:39 +08:00
|
|
|
#include "RedundantSmartptrGetCheck.h"
|
2014-03-28 01:42:26 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2014-10-15 18:51:57 +08:00
|
|
|
namespace readability {
|
2014-03-28 01:42:26 +08:00
|
|
|
|
2014-04-09 22:17:23 +08:00
|
|
|
namespace {
|
2016-06-15 23:46:10 +08:00
|
|
|
internal::Matcher<Expr> callToGet(const internal::Matcher<Decl> &OnClass) {
|
2015-09-17 21:31:25 +08:00
|
|
|
return cxxMemberCallExpr(
|
2014-04-09 22:17:23 +08:00
|
|
|
on(expr(anyOf(hasType(OnClass),
|
2015-09-17 21:31:25 +08:00
|
|
|
hasType(qualType(
|
|
|
|
pointsTo(decl(OnClass).bind("ptr_to_ptr"))))))
|
|
|
|
.bind("smart_pointer")),
|
|
|
|
unless(callee(memberExpr(hasObjectExpression(cxxThisExpr())))),
|
2016-02-18 00:13:14 +08:00
|
|
|
callee(cxxMethodDecl(
|
|
|
|
hasName("get"),
|
|
|
|
returns(qualType(pointsTo(type().bind("getType")))))))
|
2015-09-17 21:31:25 +08:00
|
|
|
.bind("redundant_get");
|
2014-04-09 22:17:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerMatchersForGetArrowStart(MatchFinder *Finder,
|
|
|
|
MatchFinder::MatchCallback *Callback) {
|
2014-03-28 01:42:26 +08:00
|
|
|
const auto QuacksLikeASmartptr = recordDecl(
|
2014-04-09 22:17:23 +08:00
|
|
|
recordDecl().bind("duck_typing"),
|
2015-09-17 21:31:25 +08:00
|
|
|
has(cxxMethodDecl(hasName("operator->"),
|
|
|
|
returns(qualType(pointsTo(type().bind("op->Type")))))),
|
2016-02-18 00:13:14 +08:00
|
|
|
has(cxxMethodDecl(hasName("operator*"), returns(qualType(references(
|
|
|
|
type().bind("op*Type")))))));
|
2014-03-28 01:42:26 +08:00
|
|
|
|
|
|
|
// Catch 'ptr.get()->Foo()'
|
2014-04-09 22:17:23 +08:00
|
|
|
Finder->addMatcher(memberExpr(expr().bind("memberExpr"), isArrow(),
|
|
|
|
hasObjectExpression(ignoringImpCasts(
|
|
|
|
callToGet(QuacksLikeASmartptr)))),
|
|
|
|
Callback);
|
2014-03-28 01:42:26 +08:00
|
|
|
|
2014-04-09 22:17:23 +08:00
|
|
|
// Catch '*ptr.get()' or '*ptr->get()'
|
2014-03-28 01:42:26 +08:00
|
|
|
Finder->addMatcher(
|
2014-04-09 22:17:23 +08:00
|
|
|
unaryOperator(hasOperatorName("*"),
|
|
|
|
hasUnaryOperand(callToGet(QuacksLikeASmartptr))),
|
|
|
|
Callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerMatchersForGetEquals(MatchFinder *Finder,
|
|
|
|
MatchFinder::MatchCallback *Callback) {
|
|
|
|
// This one is harder to do with duck typing.
|
|
|
|
// The operator==/!= that we are looking for might be member or non-member,
|
|
|
|
// might be on global namespace or found by ADL, might be a template, etc.
|
|
|
|
// For now, lets keep a list of known standard types.
|
2014-03-28 01:42:26 +08:00
|
|
|
|
2016-09-26 15:22:37 +08:00
|
|
|
const auto IsAKnownSmartptr =
|
|
|
|
recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr"));
|
2014-04-09 22:17:23 +08:00
|
|
|
|
|
|
|
// Matches against nullptr.
|
2014-03-28 01:42:26 +08:00
|
|
|
Finder->addMatcher(
|
2016-09-26 15:22:37 +08:00
|
|
|
binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")),
|
|
|
|
hasEitherOperand(ignoringImpCasts(
|
|
|
|
anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(),
|
|
|
|
integerLiteral(equals(0))))),
|
|
|
|
hasEitherOperand(callToGet(IsAKnownSmartptr))),
|
|
|
|
Callback);
|
|
|
|
|
|
|
|
// Matches against if(ptr.get())
|
|
|
|
Finder->addMatcher(
|
|
|
|
ifStmt(hasCondition(ignoringImpCasts(callToGet(IsAKnownSmartptr)))),
|
2016-09-26 14:22:54 +08:00
|
|
|
Callback);
|
2014-04-09 22:17:23 +08:00
|
|
|
|
2016-09-26 15:22:37 +08:00
|
|
|
// FIXME: Match and fix if (l.get() == r.get()).
|
|
|
|
}
|
2016-09-26 14:33:58 +08:00
|
|
|
|
2016-09-26 15:22:37 +08:00
|
|
|
} // namespace
|
2014-04-09 22:17:23 +08:00
|
|
|
|
2015-03-09 20:18:39 +08:00
|
|
|
void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) {
|
2015-09-03 00:05:21 +08:00
|
|
|
// Only register the matchers for C++; the functionality currently does not
|
|
|
|
// provide any benefit to other languages, despite being benign.
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
2014-04-09 22:17:23 +08:00
|
|
|
registerMatchersForGetArrowStart(Finder, this);
|
|
|
|
registerMatchersForGetEquals(Finder, this);
|
2014-03-28 01:42:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
|
2014-04-09 22:17:23 +08:00
|
|
|
if (Result.Nodes.getNodeAs<Decl>("duck_typing") == nullptr)
|
|
|
|
return true;
|
2014-03-28 01:42:26 +08:00
|
|
|
// Verify that the types match.
|
|
|
|
// We can't do this on the matcher because the type nodes can be different,
|
|
|
|
// even though they represent the same type. This difference comes from how
|
|
|
|
// the type is referenced (eg. through a typedef, a type trait, etc).
|
|
|
|
const Type *OpArrowType =
|
|
|
|
Result.Nodes.getNodeAs<Type>("op->Type")->getUnqualifiedDesugaredType();
|
|
|
|
const Type *OpStarType =
|
|
|
|
Result.Nodes.getNodeAs<Type>("op*Type")->getUnqualifiedDesugaredType();
|
|
|
|
const Type *GetType =
|
|
|
|
Result.Nodes.getNodeAs<Type>("getType")->getUnqualifiedDesugaredType();
|
|
|
|
return OpArrowType == OpStarType && OpArrowType == GetType;
|
|
|
|
}
|
2016-09-26 15:22:37 +08:00
|
|
|
} // namespace
|
2014-03-28 01:42:26 +08:00
|
|
|
|
2015-03-09 20:18:39 +08:00
|
|
|
void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
|
2016-09-26 15:22:37 +08:00
|
|
|
if (!allReturnTypesMatch(Result))
|
|
|
|
return;
|
2014-03-28 01:42:26 +08:00
|
|
|
|
2014-04-09 22:17:23 +08:00
|
|
|
bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr;
|
|
|
|
bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr;
|
2016-12-14 23:29:23 +08:00
|
|
|
const auto *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get");
|
|
|
|
const auto *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer");
|
2014-03-28 01:42:26 +08:00
|
|
|
|
2014-04-09 22:17:23 +08:00
|
|
|
if (IsPtrToPtr && IsMemberExpr) {
|
|
|
|
// Ignore this case (eg. Foo->get()->DoSomething());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-28 01:42:26 +08:00
|
|
|
StringRef SmartptrText = Lexer::getSourceText(
|
|
|
|
CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
|
2016-09-24 10:13:45 +08:00
|
|
|
*Result.SourceManager, getLangOpts());
|
2014-04-09 22:17:23 +08:00
|
|
|
// Replace foo->get() with *foo, and foo.get() with foo.
|
2014-03-28 01:42:26 +08:00
|
|
|
std::string Replacement = Twine(IsPtrToPtr ? "*" : "", SmartptrText).str();
|
2016-01-08 23:21:40 +08:00
|
|
|
diag(GetCall->getLocStart(), "redundant get() call on smart pointer")
|
2014-03-28 01:42:26 +08:00
|
|
|
<< FixItHint::CreateReplacement(GetCall->getSourceRange(), Replacement);
|
|
|
|
}
|
|
|
|
|
2014-10-15 18:51:57 +08:00
|
|
|
} // namespace readability
|
2014-03-28 01:42:26 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|