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))))),
|
2014-09-03 21:21:51 +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) {
|
2014-07-11 16:08:47 +08:00
|
|
|
auto *If = Result.Nodes.getStmtAs<IfStmt>("if");
|
|
|
|
auto *Var = Result.Nodes.getStmtAs<DeclRefExpr>("expr");
|
|
|
|
|
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))),
|
|
|
|
*If, *Result.Context).empty() ||
|
|
|
|
!match(findAll(arraySubscriptExpr(hasBase(DeclRef))), *If,
|
|
|
|
*Result.Context).empty() ||
|
|
|
|
// 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() ||
|
2015-09-17 21:31:25 +08:00
|
|
|
!match(findAll(cxxDeleteExpr(has(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
|