2015-03-09 19:48:54 +08:00
|
|
|
//===--- BoolPointerImplicitConversionCheck.cpp - clang-tidy --------------===//
|
2014-07-11 16:08:47 +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 19:48:54 +08:00
|
|
|
#include "BoolPointerImplicitConversionCheck.h"
|
2014-07-11 16:08:47 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
2016-05-03 02:00:29 +08:00
|
|
|
namespace tidy {
|
|
|
|
namespace misc {
|
|
|
|
|
2015-03-09 19:48:54 +08:00
|
|
|
void BoolPointerImplicitConversionCheck::registerMatchers(MatchFinder *Finder) {
|
2014-07-11 16:08:47 +08:00
|
|
|
// Look for ifs that have an implicit bool* to bool conversion in the
|
|
|
|
// condition. Filter negations.
|
|
|
|
Finder->addMatcher(
|
|
|
|
ifStmt(hasCondition(findAll(implicitCastExpr(
|
|
|
|
allOf(unless(hasParent(unaryOperator(hasOperatorName("!")))),
|
|
|
|
hasSourceExpression(expr(
|
2016-04-22 00:57:56 +08:00
|
|
|
hasType(pointerType(pointee(booleanType()))),
|
2014-07-11 16:08:47 +08:00
|
|
|
ignoringParenImpCasts(declRefExpr().bind("expr")))),
|
2016-05-18 03:36:09 +08:00
|
|
|
hasCastKind(CK_PointerToBoolean))))),
|
2016-11-08 15:50:19 +08:00
|
|
|
unless(isInTemplateInstantiation()))
|
|
|
|
.bind("if"),
|
2014-07-11 16:08:47 +08:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2015-03-09 19:48:54 +08:00
|
|
|
void BoolPointerImplicitConversionCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
2016-12-14 00:19:19 +08:00
|
|
|
auto *If = Result.Nodes.getNodeAs<IfStmt>("if");
|
|
|
|
auto *Var = Result.Nodes.getNodeAs<DeclRefExpr>("expr");
|
2014-07-11 16:08:47 +08:00
|
|
|
|
2014-08-12 20:12:57 +08:00
|
|
|
// Ignore macros.
|
|
|
|
if (Var->getLocStart().isMacroID())
|
|
|
|
return;
|
|
|
|
|
2014-07-11 16:08:47 +08:00
|
|
|
// Only allow variable accesses for now, no function calls or member exprs.
|
|
|
|
// Check that we don't dereference the variable anywhere within the if. This
|
|
|
|
// avoids false positives for checks of the pointer for nullptr before it is
|
|
|
|
// dereferenced. If there is a dereferencing operator on this variable don't
|
|
|
|
// emit a diagnostic. Also ignore array subscripts.
|
|
|
|
const Decl *D = Var->getDecl();
|
|
|
|
auto DeclRef = ignoringParenImpCasts(declRefExpr(to(equalsNode(D))));
|
|
|
|
if (!match(findAll(
|
|
|
|
unaryOperator(hasOperatorName("*"), hasUnaryOperand(DeclRef))),
|
2016-05-31 23:26:56 +08:00
|
|
|
*If, *Result.Context)
|
|
|
|
.empty() ||
|
2014-07-11 16:08:47 +08:00
|
|
|
!match(findAll(arraySubscriptExpr(hasBase(DeclRef))), *If,
|
2016-05-31 23:26:56 +08:00
|
|
|
*Result.Context)
|
|
|
|
.empty() ||
|
2014-07-11 16:08:47 +08:00
|
|
|
// FIXME: We should still warn if the paremater is implicitly converted to
|
|
|
|
// bool.
|
2016-04-06 20:04:51 +08:00
|
|
|
!match(findAll(callExpr(hasAnyArgument(ignoringParenImpCasts(DeclRef)))),
|
|
|
|
*If, *Result.Context)
|
2014-07-11 16:08:47 +08:00
|
|
|
.empty() ||
|
2016-05-31 23:26:56 +08:00
|
|
|
!match(findAll(cxxDeleteExpr(has(ignoringParenImpCasts(expr(DeclRef))))),
|
|
|
|
*If, *Result.Context)
|
2014-07-11 16:08:47 +08:00
|
|
|
.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
diag(Var->getLocStart(), "dubious check of 'bool *' against 'nullptr', did "
|
|
|
|
"you mean to dereference it?")
|
|
|
|
<< FixItHint::CreateInsertion(Var->getLocStart(), "*");
|
|
|
|
}
|
|
|
|
|
2015-03-02 20:25:03 +08:00
|
|
|
} // namespace misc
|
2014-07-11 16:08:47 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|