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