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 {
|
|
|
|
internal::Matcher<Expr> callToGet(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())))),
|
|
|
|
callee(cxxMethodDecl(hasName("get"))))
|
|
|
|
.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")))))),
|
|
|
|
has(cxxMethodDecl(hasName("operator*"),
|
|
|
|
returns(qualType(references(type().bind("op*Type")))))),
|
|
|
|
has(cxxMethodDecl(hasName("get"),
|
|
|
|
returns(qualType(pointsTo(type().bind("getType")))))));
|
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
|
|
|
|
2014-04-09 22:17:23 +08:00
|
|
|
const auto IsAKnownSmartptr = recordDecl(
|
|
|
|
anyOf(hasName("::std::unique_ptr"), hasName("::std::shared_ptr")));
|
|
|
|
|
|
|
|
// Matches against nullptr.
|
2014-03-28 01:42:26 +08:00
|
|
|
Finder->addMatcher(
|
2015-09-17 21:31:25 +08:00
|
|
|
binaryOperator(
|
|
|
|
anyOf(hasOperatorName("=="), hasOperatorName("!=")),
|
|
|
|
hasEitherOperand(ignoringImpCasts(cxxNullPtrLiteralExpr())),
|
|
|
|
hasEitherOperand(callToGet(IsAKnownSmartptr))),
|
2014-04-09 22:17:23 +08:00
|
|
|
Callback);
|
|
|
|
// TODO: Catch ptr.get() == other_ptr.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-03-09 20:18:39 +08:00
|
|
|
void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
|
2014-03-28 01:42:26 +08:00
|
|
|
if (!allReturnTypesMatch(Result)) return;
|
|
|
|
|
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;
|
2014-03-28 01:42:26 +08:00
|
|
|
const Expr *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get");
|
|
|
|
const Expr *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer");
|
|
|
|
|
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()),
|
2015-01-22 20:40:47 +08:00
|
|
|
*Result.SourceManager, Result.Context->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();
|
|
|
|
diag(GetCall->getLocStart(), "Redundant get() call on smart pointer.")
|
|
|
|
<< 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
|