diff --git a/clang-tools-extra/clang-tidy/llvm/AvoidCastInConditionalCheck.h b/clang-tools-extra/clang-tidy/llvm/AvoidCastInConditionalCheck.h new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/llvm/AvoidCastInConditionalCheck.h @@ -0,0 +1,46 @@ +//===--- AvoidCastInConditionalCheck.h - clang-tidy -------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_AVOIDCASTINCONDITIONALCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_AVOIDCASTINCONDITIONALCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace llvm { + +/// \brief Finds cases of cast<> used in conditional statements which will +/// assert on failure in Debug builds. +/// +/// Finds cases like these: +/// \code +/// if (auto x = cast(y)) <...> +/// if (cast(y)) <...> +/// \endcode +/// But not cases like these: +/// \code +/// if (auto f = cast(y)->foo()) <...> +/// if (cast(y)->foo()) <...> +/// \endcode +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/llvm-avoid-cast-in-conditional.html +class AvoidCastInConditionalCheck : public ClangTidyCheck { +public: + AvoidCastInConditionalCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace llvm +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_AVOIDCASTINCONDITIONALCHECK_H diff --git a/clang-tools-extra/clang-tidy/llvm/AvoidCastInConditionalCheck.cpp b/clang-tools-extra/clang-tidy/llvm/AvoidCastInConditionalCheck.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/llvm/AvoidCastInConditionalCheck.cpp @@ -0,0 +1,43 @@ +//===--- AvoidCastInConditionalCheck.cpp - clang-tidy ---------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "AvoidCastInConditionalCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace llvm { + +void AvoidCastInConditionalCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + ifStmt(anyOf( + hasConditionVariableStatement(declStmt(containsDeclaration( + 0, varDecl(hasInitializer( + callExpr(callee(namedDecl(hasName("cast")))) + .bind("cast-call")))))), + has(implicitCastExpr(has(callExpr(callee(namedDecl(hasName("cast")))) + .bind("cast-call")))))), + this); +} + +void AvoidCastInConditionalCheck::check( + const MatchFinder::MatchResult &Result) { + if (const auto *MatchedDecl = Result.Nodes.getNodeAs("cast-call")) { + diag(MatchedDecl->getBeginLoc(), + "Found cast<> in conditional statement; consider using dyn_cast<> if " + "it can fail, or removal of conditional if it can't."); + ; + } +} + +} // namespace llvm +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt b/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/llvm/CMakeLists.txt @@ -1,6 +1,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyLLVMModule + AvoidCastInConditionalCheck.cpp HeaderGuardCheck.cpp IncludeOrderCheck.cpp LLVMTidyModule.cpp diff --git a/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp b/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp --- a/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/llvm/LLVMTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "../readability/NamespaceCommentCheck.h" +#include "AvoidCastInConditionalCheck.h" #include "HeaderGuardCheck.h" #include "IncludeOrderCheck.h" #include "TwineLocalCheck.h" @@ -21,6 +22,8 @@ class LLVMModule : public ClangTidyModule { public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck( + "llvm-avoid-cast-in-conditional"); CheckFactories.registerCheck("llvm-header-guard"); CheckFactories.registerCheck("llvm-include-order"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h b/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h --- a/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h +++ b/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h @@ -18,7 +18,7 @@ namespace tidy { namespace utils { -typedef llvm::SmallSet HeaderFileExtensionsSet; +using HeaderFileExtensionsSet = SmallSet; /// \brief Checks whether expansion location of \p Loc is in header file. bool isExpansionLocInHeaderFile( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -130,6 +130,12 @@ ` now supports `OverrideSpelling` and `FinalSpelling` options. +- New :doc:`llvm-avoid-cast-in-conditional + ` check. + + Finds cases of ``cast<>`` used as condition in conditional statements + which will assert on failure in Debug builds. + - New :doc:`openmp-exception-escape ` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -175,6 +175,7 @@ hicpp-use-nullptr (redirects to modernize-use-nullptr) hicpp-use-override (redirects to modernize-use-override) hicpp-vararg (redirects to cppcoreguidelines-pro-type-vararg) + llvm-avoid-cast-in-conditional llvm-header-guard llvm-include-order llvm-namespace-comment diff --git a/clang-tools-extra/docs/clang-tidy/checks/llvm-avoid-cast-in-conditional.rst b/clang-tools-extra/docs/clang-tidy/checks/llvm-avoid-cast-in-conditional.rst new file mode 100644 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/llvm-avoid-cast-in-conditional.rst @@ -0,0 +1,19 @@ +.. title:: clang-tidy - llvm-avoid-cast-in-conditional + +llvm-avoid-cast-in-conditional +============================== + +Finds cases of ``cast<>`` used as the condition of a conditional +statements which will assert on failure in Debug builds. The use of +cast<> implies that the operation cannot fail, and should not be used +as the condition of a conditional statement. + +.. code-block:: c++ + + // Finds cases like these: + if (auto x = cast(y)) <...> + if (cast(y)) <...> + + // But not cases like these: + if (auto f = cast(y)->foo()) <...> + if (cast(y)->foo()) <...> diff --git a/clang-tools-extra/test/clang-tidy/llvm-avoid-cast-in-conditional.cpp b/clang-tools-extra/test/clang-tidy/llvm-avoid-cast-in-conditional.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/llvm-avoid-cast-in-conditional.cpp @@ -0,0 +1,25 @@ +// RUN: %check_clang_tidy %s llvm-avoid-cast-in-conditional %t + +struct X; +struct Y; +struct Z { + int foo(); +}; + +template +X* cast(Y*); + +bool foo(Y* y) { + if (auto x = cast(y)) + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: {{.*llvm-avoid-cast-in-conditional}} + return true; + if (cast(y)) + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: {{.*llvm-avoid-cast-in-conditional}} + return true; + + // Does not trigger warning. + if (cast(y)->foo()) + return true; + return false; +} + diff --git a/clang/include/clang/Basic/LLVM.h b/clang/include/clang/Basic/LLVM.h --- a/clang/include/clang/Basic/LLVM.h +++ b/clang/include/clang/Basic/LLVM.h @@ -31,6 +31,7 @@ template class ArrayRef; template class MutableArrayRef; template class OwningArrayRef; + template class SmallSet; template class SmallString; template class SmallVector; template class SmallVectorImpl; @@ -66,6 +67,7 @@ using llvm::Optional; using llvm::OwningArrayRef; using llvm::SaveAndRestore; + using llvm::SmallSet; using llvm::SmallString; using llvm::SmallVector; using llvm::SmallVectorImpl;