blob: 7fd1ca0feb5fe7cbf1ffcad2a1f607c5d9cff2b3 [file] [log] [blame]
Alexander Kornienko5b982e52015-03-09 11:48:54 +00001//===--- BoolPointerImplicitConversionCheck.cpp - clang-tidy --------------===//
Benjamin Kramer1c8b3172014-07-11 08:08:47 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Alexander Kornienko5b982e52015-03-09 11:48:54 +000010#include "BoolPointerImplicitConversionCheck.h"
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000011
12using namespace clang::ast_matchers;
13
14namespace clang {
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000015namespace {
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000016
17AST_MATCHER(CastExpr, isPointerToBoolean) {
18 return Node.getCastKind() == CK_PointerToBoolean;
19}
20AST_MATCHER(QualType, isBoolean) { return Node->isBooleanType(); }
21
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000022} // namespace
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000023
24namespace tidy {
Alexander Kornienko2b3124202015-03-02 12:25:03 +000025namespace misc {
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000026
Alexander Kornienko5b982e52015-03-09 11:48:54 +000027void BoolPointerImplicitConversionCheck::registerMatchers(MatchFinder *Finder) {
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000028 // Look for ifs that have an implicit bool* to bool conversion in the
29 // condition. Filter negations.
30 Finder->addMatcher(
31 ifStmt(hasCondition(findAll(implicitCastExpr(
32 allOf(unless(hasParent(unaryOperator(hasOperatorName("!")))),
33 hasSourceExpression(expr(
34 hasType(pointerType(pointee(isBoolean()))),
35 ignoringParenImpCasts(declRefExpr().bind("expr")))),
Benjamin Kramer242b5b82014-08-12 12:12:57 +000036 isPointerToBoolean())))),
Benjamin Kramerb7f59d62014-09-03 13:21:51 +000037 unless(isInTemplateInstantiation())).bind("if"),
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000038 this);
39}
40
Alexander Kornienko5b982e52015-03-09 11:48:54 +000041void BoolPointerImplicitConversionCheck::check(
42 const MatchFinder::MatchResult &Result) {
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000043 auto *If = Result.Nodes.getStmtAs<IfStmt>("if");
44 auto *Var = Result.Nodes.getStmtAs<DeclRefExpr>("expr");
45
Benjamin Kramer242b5b82014-08-12 12:12:57 +000046 // Ignore macros.
47 if (Var->getLocStart().isMacroID())
48 return;
49
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000050 // Only allow variable accesses for now, no function calls or member exprs.
51 // Check that we don't dereference the variable anywhere within the if. This
52 // avoids false positives for checks of the pointer for nullptr before it is
53 // dereferenced. If there is a dereferencing operator on this variable don't
54 // emit a diagnostic. Also ignore array subscripts.
55 const Decl *D = Var->getDecl();
56 auto DeclRef = ignoringParenImpCasts(declRefExpr(to(equalsNode(D))));
57 if (!match(findAll(
58 unaryOperator(hasOperatorName("*"), hasUnaryOperand(DeclRef))),
59 *If, *Result.Context).empty() ||
60 !match(findAll(arraySubscriptExpr(hasBase(DeclRef))), *If,
61 *Result.Context).empty() ||
62 // FIXME: We should still warn if the paremater is implicitly converted to
63 // bool.
Gabor Horvath5273f612016-04-06 12:04:51 +000064 !match(findAll(callExpr(hasAnyArgument(ignoringParenImpCasts(DeclRef)))),
65 *If, *Result.Context)
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000066 .empty() ||
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000067 !match(findAll(cxxDeleteExpr(has(expr(DeclRef)))), *If, *Result.Context)
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000068 .empty())
69 return;
70
71 diag(Var->getLocStart(), "dubious check of 'bool *' against 'nullptr', did "
72 "you mean to dereference it?")
73 << FixItHint::CreateInsertion(Var->getLocStart(), "*");
74}
75
Alexander Kornienko2b3124202015-03-02 12:25:03 +000076} // namespace misc
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000077} // namespace tidy
78} // namespace clang