clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

BoolPointerImplicitConversionCheck.cpp
Go to the documentation of this file.
1 //===--- BoolPointerImplicitConversionCheck.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 "BoolPointerImplicitConversionCheck.h"
11
12 using namespace clang::ast_matchers;
13
14 namespace clang {
15 namespace {
16
17 AST_MATCHER(CastExpr, isPointerToBoolean) {
18 return Node.getCastKind() == CK_PointerToBoolean;
19 }
20 AST_MATCHER(QualType, isBoolean) { return Node->isBooleanType(); }
21
22 } // namespace
23
24 namespace tidy {
25 namespace misc {
26
27 void BoolPointerImplicitConversionCheck::registerMatchers(MatchFinder *Finder) {
28 // 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")))),
36 isPointerToBoolean())))),
37 unless(isInTemplateInstantiation())).bind("if"),
38 this);
39 }
40
41 void BoolPointerImplicitConversionCheck::check(
42 const MatchFinder::MatchResult &Result) {
43 auto *If = Result.Nodes.getStmtAs<IfStmt>("if");
44 auto *Var = Result.Nodes.getStmtAs<DeclRefExpr>("expr");
45
46 // Ignore macros.
47 if (Var->getLocStart().isMacroID())
48 return;
49
50 // 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.
64 !match(findAll(callExpr(hasAnyArgument(DeclRef))), *If, *Result.Context)
65 .empty() ||
66 !match(findAll(cxxDeleteExpr(has(expr(DeclRef)))), *If, *Result.Context)
67 .empty())
68 return;
69
70 diag(Var->getLocStart(), "dubious check of 'bool *' against 'nullptr', did "
71 "you mean to dereference it?")
72 << FixItHint::CreateInsertion(Var->getLocStart(), "*");
73 }
74
75 } // namespace misc
76 } // namespace tidy
77 } // namespace clang
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
BoolPointerImplicitConversionCheck.h
clang::tidy::AST_MATCHER
AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt)
Definition: ProBoundsArrayToPointerDecayCheck.cpp:25
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121