2015-03-09 19:48:54 +08:00
|
|
|
//===--- BoolPointerImplicitConversionCheck.cpp - clang-tidy --------------===//
|
2014-07-11 16:08:47 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2014-07-11 16:08:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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 {
|
2017-11-24 22:16:29 +08:00
|
|
|
namespace bugprone {
|
2016-05-03 02:00:29 +08:00
|
|
|
|
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(
|
2018-11-25 10:41:01 +08:00
|
|
|
unless(hasParent(unaryOperator(hasOperatorName("!")))),
|
|
|
|
hasSourceExpression(
|
|
|
|
expr(hasType(pointerType(pointee(booleanType()))),
|
|
|
|
ignoringParenImpCasts(declRefExpr().bind("expr")))),
|
|
|
|
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.
|
2018-08-10 06:42:26 +08:00
|
|
|
if (Var->getBeginLoc().isMacroID())
|
2014-08-12 20:12:57 +08:00
|
|
|
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;
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(Var->getBeginLoc(), "dubious check of 'bool *' against 'nullptr', did "
|
2014-07-11 16:08:47 +08:00
|
|
|
"you mean to dereference it?")
|
2018-08-10 06:42:26 +08:00
|
|
|
<< FixItHint::CreateInsertion(Var->getBeginLoc(), "*");
|
2014-07-11 16:08:47 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:16:29 +08:00
|
|
|
} // namespace bugprone
|
2014-07-11 16:08:47 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|