njames93 created this revision. njames93 added reviewers: alexfh, aaron.ballman, LegalizeAdulthood, JonasToth, bzcheeseman. Herald added subscribers: carlosgalvezp, xazax.hun. Herald added a project: All. njames93 requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
In D123901 <https://reviews.llvm.org/D123901>, the `or_null`, `and_nonnull` templates were deprecated intended to be replaced with `if_present` and `and_present`. In light of this, The clang-tidy check that enforces correct use of `isa` and `dyn_cast` should also be updated with this syntax. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D131319 Files: clang-tools-extra/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals.rst clang-tools-extra/test/clang-tidy/checkers/llvm/prefer-isa-or-dyn-cast-in-conditionals.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/llvm/prefer-isa-or-dyn-cast-in-conditionals.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/llvm/prefer-isa-or-dyn-cast-in-conditionals.cpp +++ clang-tools-extra/test/clang-tidy/checkers/llvm/prefer-isa-or-dyn-cast-in-conditionals.cpp @@ -17,6 +17,8 @@ X *dyn_cast(Y *); template <class X, class Y> X *dyn_cast_or_null(Y *); +template <class X, class Y> +X *dyn_cast_if_present(Y *); bool foo(Y *y, Z *z) { if (auto x = cast<X>(y)) @@ -63,32 +65,37 @@ if (y && isa<X>(y)) return true; - // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred over an explicit test for null followed by calling isa<> [llvm-prefer-isa-or-dyn-cast-in-conditionals] - // CHECK-FIXES: if (isa_and_nonnull<X>(y)) + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_present<> is preferred over an explicit test for null followed by calling isa<> [llvm-prefer-isa-or-dyn-cast-in-conditionals] + // CHECK-FIXES: if (isa_and_present<X>(y)) if (z->bar() && isa<Y>(z->bar())) return true; - // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred - // CHECK-FIXES: if (isa_and_nonnull<Y>(z->bar())) + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_present<> is preferred + // CHECK-FIXES: if (isa_and_present<Y>(z->bar())) if (z->bar() && cast<Y>(z->bar())) return true; - // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred - // CHECK-FIXES: if (isa_and_nonnull<Y>(z->bar())) + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_present<> is preferred + // CHECK-FIXES: if (isa_and_present<Y>(z->bar())) if (z->bar() && dyn_cast<Y>(z->bar())) return true; - // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred - // CHECK-FIXES: if (isa_and_nonnull<Y>(z->bar())) + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_present<> is preferred + // CHECK-FIXES: if (isa_and_present<Y>(z->bar())) if (z->bar() && dyn_cast_or_null<Y>(z->bar())) return true; - // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_nonnull<> is preferred - // CHECK-FIXES: if (isa_and_nonnull<Y>(z->bar())) + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_present<> is preferred + // CHECK-FIXES: if (isa_and_present<Y>(z->bar())) + + if (z->bar() && dyn_cast_if_present<Y>(z->bar())) + return true; + // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: isa_and_present<> is preferred + // CHECK-FIXES: if (isa_and_present<Y>(z->bar())) bool b = z->bar() && cast<Y>(z->bar()); - // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: isa_and_nonnull<> is preferred - // CHECK-FIXES: bool b = isa_and_nonnull<Y>(z->bar()); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: isa_and_present<> is preferred + // CHECK-FIXES: bool b = isa_and_present<Y>(z->bar()); // These don't trigger a warning. if (auto x = cast<Z>(y)->foo()) Index: clang-tools-extra/docs/clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals.rst =================================================================== --- clang-tools-extra/docs/clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals.rst +++ clang-tools-extra/docs/clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals.rst @@ -26,7 +26,7 @@ if (var && isa<T>(var)) {} // is replaced by: - if (isa_and_nonnull<T>(var.foo())) {} + if (isa_and_present<T>(var.foo())) {} // Other cases are ignored, e.g.: if (auto f = cast<Z>(y)->foo()) {} Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -105,6 +105,11 @@ Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ +- Updated :doc:`llvm-prefer-isa-or-dyn-cast-in-conditionals + <clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals>` check to + use the `*and_present` and `*if_present` templates added in + `D123901 <https://reviews.llvm.org/D123901>`_. + Removed checks ^^^^^^^^^^^^^^ Index: clang-tools-extra/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.cpp +++ clang-tools-extra/clang-tidy/llvm/PreferIsaOrDynCastInConditionalsCheck.cpp @@ -42,14 +42,14 @@ auto CallExpression = callExpr( - allOf( - unless(isMacroID()), unless(cxxMemberCallExpr()), - allOf(callee(namedDecl(hasAnyName("isa", "cast", "cast_or_null", - "dyn_cast", "dyn_cast_or_null")) - .bind("func")), - hasArgument( - 0, - mapAnyOf(declRefExpr, cxxMemberCallExpr).bind("arg"))))) + allOf(unless(isMacroID()), unless(cxxMemberCallExpr()), + allOf(callee(namedDecl(hasAnyName("isa", "cast", "cast_or_null", + "cast_if_present", "dyn_cast", + "dyn_cast_or_null", + "dyn_cast_if_present")) + .bind("func")), + hasArgument(0, mapAnyOf(declRefExpr, cxxMemberCallExpr) + .bind("arg"))))) .bind("rhs"); Finder->addMatcher( @@ -118,11 +118,11 @@ CharSourceRange::getTokenRange(RHS->getSourceRange()), *Result.SourceManager, getLangOpts())); - std::string Replacement("isa_and_nonnull"); + std::string Replacement("isa_and_present"); Replacement += RHSString.substr(Func->getName().size()); diag(MatchedDecl->getBeginLoc(), - "isa_and_nonnull<> is preferred over an explicit test for null " + "isa_and_present<> is preferred over an explicit test for null " "followed by calling isa<>") << FixItHint::CreateReplacement(SourceRange(MatchedDecl->getBeginLoc(), MatchedDecl->getEndLoc()),
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits