blob: c745366cf44ef7b302e66d5be2dae4cbb14d0276 [file] [log] [blame]
Benjamin Kramer1c8b3172014-07-11 08:08:47 +00001//===--- BoolPointerImplicitConversion.cpp - clang-tidy -------------------===//
2//
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
10#include "BoolPointerImplicitConversion.h"
11
12using namespace clang::ast_matchers;
13
14namespace clang {
15namespace ast_matchers {
16
17AST_MATCHER(CastExpr, isPointerToBoolean) {
18 return Node.getCastKind() == CK_PointerToBoolean;
19}
20AST_MATCHER(QualType, isBoolean) { return Node->isBooleanType(); }
21
22} // namespace ast_matchers
23
24namespace tidy {
25
26void BoolPointerImplicitConversion::registerMatchers(MatchFinder *Finder) {
Benjamin Kramer242b5b82014-08-12 12:12:57 +000027 auto InTemplateInstantiation = hasAncestor(
28 decl(anyOf(recordDecl(ast_matchers::isTemplateInstantiation()),
29 functionDecl(ast_matchers::isTemplateInstantiation()))));
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000030 // Look for ifs that have an implicit bool* to bool conversion in the
31 // condition. Filter negations.
32 Finder->addMatcher(
33 ifStmt(hasCondition(findAll(implicitCastExpr(
34 allOf(unless(hasParent(unaryOperator(hasOperatorName("!")))),
35 hasSourceExpression(expr(
36 hasType(pointerType(pointee(isBoolean()))),
37 ignoringParenImpCasts(declRefExpr().bind("expr")))),
Benjamin Kramer242b5b82014-08-12 12:12:57 +000038 isPointerToBoolean())))),
39 unless(InTemplateInstantiation)).bind("if"),
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000040 this);
41}
42
43void
44BoolPointerImplicitConversion::check(const MatchFinder::MatchResult &Result) {
45 auto *If = Result.Nodes.getStmtAs<IfStmt>("if");
46 auto *Var = Result.Nodes.getStmtAs<DeclRefExpr>("expr");
47
Benjamin Kramer242b5b82014-08-12 12:12:57 +000048 // Ignore macros.
49 if (Var->getLocStart().isMacroID())
50 return;
51
Benjamin Kramer1c8b3172014-07-11 08:08:47 +000052 // Only allow variable accesses for now, no function calls or member exprs.
53 // Check that we don't dereference the variable anywhere within the if. This
54 // avoids false positives for checks of the pointer for nullptr before it is
55 // dereferenced. If there is a dereferencing operator on this variable don't
56 // emit a diagnostic. Also ignore array subscripts.
57 const Decl *D = Var->getDecl();
58 auto DeclRef = ignoringParenImpCasts(declRefExpr(to(equalsNode(D))));
59 if (!match(findAll(
60 unaryOperator(hasOperatorName("*"), hasUnaryOperand(DeclRef))),
61 *If, *Result.Context).empty() ||
62 !match(findAll(arraySubscriptExpr(hasBase(DeclRef))), *If,
63 *Result.Context).empty() ||
64 // FIXME: We should still warn if the paremater is implicitly converted to
65 // bool.
66 !match(findAll(callExpr(hasAnyArgument(DeclRef))), *If, *Result.Context)
67 .empty() ||
68 !match(findAll(deleteExpr(has(expr(DeclRef)))), *If, *Result.Context)
69 .empty())
70 return;
71
72 diag(Var->getLocStart(), "dubious check of 'bool *' against 'nullptr', did "
73 "you mean to dereference it?")
74 << FixItHint::CreateInsertion(Var->getLocStart(), "*");
75}
76
77} // namespace tidy
78} // namespace clang