BoolPointerImplicitConversionCheck.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===--- BoolPointerImplicitConversionCheck.cpp - clang-tidy --------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "BoolPointerImplicitConversionCheck.h"
  9. using namespace clang::ast_matchers;
  10. namespace clang::tidy::bugprone {
  11. void BoolPointerImplicitConversionCheck::registerMatchers(MatchFinder *Finder) {
  12. // Look for ifs that have an implicit bool* to bool conversion in the
  13. // condition. Filter negations.
  14. Finder->addMatcher(
  15. traverse(
  16. TK_AsIs,
  17. ifStmt(
  18. hasCondition(findAll(implicitCastExpr(
  19. unless(hasParent(unaryOperator(hasOperatorName("!")))),
  20. hasSourceExpression(expr(
  21. hasType(pointerType(pointee(booleanType()))),
  22. ignoringParenImpCasts(anyOf(declRefExpr().bind("expr"),
  23. memberExpr().bind("expr"))))),
  24. hasCastKind(CK_PointerToBoolean)))),
  25. unless(isInTemplateInstantiation()))
  26. .bind("if")),
  27. this);
  28. }
  29. static void checkImpl(const MatchFinder::MatchResult &Result, const Expr *Ref,
  30. const IfStmt *If,
  31. const ast_matchers::internal::Matcher<Expr> &RefMatcher,
  32. ClangTidyCheck &Check) {
  33. // Ignore macros.
  34. if (Ref->getBeginLoc().isMacroID())
  35. return;
  36. // Only allow variable accesses and member exprs for now, no function calls.
  37. // Check that we don't dereference the variable anywhere within the if. This
  38. // avoids false positives for checks of the pointer for nullptr before it is
  39. // dereferenced. If there is a dereferencing operator on this variable don't
  40. // emit a diagnostic. Also ignore array subscripts.
  41. if (!match(findAll(unaryOperator(hasOperatorName("*"),
  42. hasUnaryOperand(RefMatcher))),
  43. *If, *Result.Context)
  44. .empty() ||
  45. !match(findAll(arraySubscriptExpr(hasBase(RefMatcher))), *If,
  46. *Result.Context)
  47. .empty() ||
  48. // FIXME: We should still warn if the paremater is implicitly converted to
  49. // bool.
  50. !match(
  51. findAll(callExpr(hasAnyArgument(ignoringParenImpCasts(RefMatcher)))),
  52. *If, *Result.Context)
  53. .empty() ||
  54. !match(
  55. findAll(cxxDeleteExpr(has(ignoringParenImpCasts(expr(RefMatcher))))),
  56. *If, *Result.Context)
  57. .empty())
  58. return;
  59. Check.diag(Ref->getBeginLoc(),
  60. "dubious check of 'bool *' against 'nullptr', did "
  61. "you mean to dereference it?")
  62. << FixItHint::CreateInsertion(Ref->getBeginLoc(), "*");
  63. }
  64. void BoolPointerImplicitConversionCheck::check(
  65. const MatchFinder::MatchResult &Result) {
  66. const auto *If = Result.Nodes.getNodeAs<IfStmt>("if");
  67. if (const auto *E = Result.Nodes.getNodeAs<Expr>("expr")) {
  68. const Decl *D = isa<DeclRefExpr>(E) ? cast<DeclRefExpr>(E)->getDecl()
  69. : cast<MemberExpr>(E)->getMemberDecl();
  70. const auto M =
  71. ignoringParenImpCasts(anyOf(declRefExpr(to(equalsNode(D))),
  72. memberExpr(hasDeclaration(equalsNode(D)))));
  73. checkImpl(Result, E, If, M, *this);
  74. }
  75. }
  76. } // namespace clang::tidy::bugprone