Alexander Kornienko | 5b982e5 | 2015-03-09 11:48:54 +0000 | [diff] [blame] | 1 | //===--- BoolPointerImplicitConversionCheck.cpp - clang-tidy --------------===// |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 5b982e5 | 2015-03-09 11:48:54 +0000 | [diff] [blame] | 10 | #include "BoolPointerImplicitConversionCheck.h" |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 11 | |
| 12 | using namespace clang::ast_matchers; |
| 13 | |
| 14 | namespace clang { |
Alexander Kornienko | 50d7f461 | 2015-06-17 13:11:37 +0000 | [diff] [blame] | 15 | namespace { |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 16 | |
| 17 | AST_MATCHER(CastExpr, isPointerToBoolean) { |
| 18 | return Node.getCastKind() == CK_PointerToBoolean; |
| 19 | } |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 20 | |
Alexander Kornienko | 50d7f461 | 2015-06-17 13:11:37 +0000 | [diff] [blame] | 21 | } // namespace |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 22 | |
| 23 | namespace tidy { |
Alexander Kornienko | 2b312420 | 2015-03-02 12:25:03 +0000 | [diff] [blame] | 24 | namespace misc { |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 25 | |
Alexander Kornienko | 5b982e5 | 2015-03-09 11:48:54 +0000 | [diff] [blame] | 26 | void BoolPointerImplicitConversionCheck::registerMatchers(MatchFinder *Finder) { |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 27 | // Look for ifs that have an implicit bool* to bool conversion in the |
| 28 | // condition. Filter negations. |
| 29 | Finder->addMatcher( |
| 30 | ifStmt(hasCondition(findAll(implicitCastExpr( |
| 31 | allOf(unless(hasParent(unaryOperator(hasOperatorName("!")))), |
| 32 | hasSourceExpression(expr( |
Etienne Bergeron | 9d26599 | 2016-04-21 16:57:56 +0000 | [diff] [blame^] | 33 | hasType(pointerType(pointee(booleanType()))), |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 34 | ignoringParenImpCasts(declRefExpr().bind("expr")))), |
Benjamin Kramer | 242b5b8 | 2014-08-12 12:12:57 +0000 | [diff] [blame] | 35 | isPointerToBoolean())))), |
Benjamin Kramer | b7f59d6 | 2014-09-03 13:21:51 +0000 | [diff] [blame] | 36 | unless(isInTemplateInstantiation())).bind("if"), |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 37 | this); |
| 38 | } |
| 39 | |
Alexander Kornienko | 5b982e5 | 2015-03-09 11:48:54 +0000 | [diff] [blame] | 40 | void BoolPointerImplicitConversionCheck::check( |
| 41 | const MatchFinder::MatchResult &Result) { |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 42 | auto *If = Result.Nodes.getStmtAs<IfStmt>("if"); |
| 43 | auto *Var = Result.Nodes.getStmtAs<DeclRefExpr>("expr"); |
| 44 | |
Benjamin Kramer | 242b5b8 | 2014-08-12 12:12:57 +0000 | [diff] [blame] | 45 | // Ignore macros. |
| 46 | if (Var->getLocStart().isMacroID()) |
| 47 | return; |
| 48 | |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 49 | // Only allow variable accesses for now, no function calls or member exprs. |
| 50 | // Check that we don't dereference the variable anywhere within the if. This |
| 51 | // avoids false positives for checks of the pointer for nullptr before it is |
| 52 | // dereferenced. If there is a dereferencing operator on this variable don't |
| 53 | // emit a diagnostic. Also ignore array subscripts. |
| 54 | const Decl *D = Var->getDecl(); |
| 55 | auto DeclRef = ignoringParenImpCasts(declRefExpr(to(equalsNode(D)))); |
| 56 | if (!match(findAll( |
| 57 | unaryOperator(hasOperatorName("*"), hasUnaryOperand(DeclRef))), |
| 58 | *If, *Result.Context).empty() || |
| 59 | !match(findAll(arraySubscriptExpr(hasBase(DeclRef))), *If, |
| 60 | *Result.Context).empty() || |
| 61 | // FIXME: We should still warn if the paremater is implicitly converted to |
| 62 | // bool. |
Gabor Horvath | 5273f61 | 2016-04-06 12:04:51 +0000 | [diff] [blame] | 63 | !match(findAll(callExpr(hasAnyArgument(ignoringParenImpCasts(DeclRef)))), |
| 64 | *If, *Result.Context) |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 65 | .empty() || |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 66 | !match(findAll(cxxDeleteExpr(has(expr(DeclRef)))), *If, *Result.Context) |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 2b312420 | 2015-03-02 12:25:03 +0000 | [diff] [blame] | 75 | } // namespace misc |
Benjamin Kramer | 1c8b317 | 2014-07-11 08:08:47 +0000 | [diff] [blame] | 76 | } // namespace tidy |
| 77 | } // namespace clang |