Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:20

0001 //===--- PreferIsaOrDynCastInConditionalsCheck.h - clang-tidy ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 
0014 namespace clang::tidy::llvm_check {
0015 
0016 /// Looks at conditionals and finds and replaces cases of ``cast<>``, which will
0017 /// assert rather than return a null pointer, and ``dyn_cast<>`` where
0018 /// the return value is not captured.  Additionally, finds and replaces cases that match the
0019 /// pattern ``var && isa<X>(var)``, where ``var`` is evaluated twice.
0020 ///
0021 /// Finds cases like these:
0022 /// \code
0023 ///  if (auto x = cast<X>(y)) {}
0024 ///  // is replaced by:
0025 ///  if (auto x = dyn_cast<X>(y)) {}
0026 ///
0027 ///  if (cast<X>(y)) {}
0028 ///  // is replaced by:
0029 ///  if (isa<X>(y)) {}
0030 ///
0031 ///  if (dyn_cast<X>(y)) {}
0032 ///  // is replaced by:
0033 ///  if (isa<X>(y)) {}
0034 ///
0035 ///  if (var && isa<T>(var)) {}
0036 ///  // is replaced by:
0037 ///  if (isa_and_nonnull<T>(var.foo())) {}
0038 /// \endcode
0039 ///
0040 ///  // Other cases are ignored, e.g.:
0041 /// \code
0042 ///  if (auto f = cast<Z>(y)->foo()) {}
0043 ///  if (cast<Z>(y)->foo()) {}
0044 ///  if (X.cast(y)) {}
0045 /// \endcode
0046 ///
0047 /// For the user-facing documentation see:
0048 /// http://clang.llvm.org/extra/clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals.html
0049 class PreferIsaOrDynCastInConditionalsCheck : public ClangTidyCheck {
0050 public:
0051   PreferIsaOrDynCastInConditionalsCheck(StringRef Name,
0052                                         ClangTidyContext *Context)
0053       : ClangTidyCheck(Name, Context) {}
0054   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
0055     return LangOpts.CPlusPlus;
0056   }
0057   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
0058   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
0059 };
0060 
0061 } // namespace clang::tidy::llvm_check
0062 
0063 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H