PreferIsaOrDynCastInConditionalsCheck.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //===--- PreferIsaOrDynCastInConditionalsCheck.h - clang-tidy ---*- C++ -*-===//
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
  10. #include "../ClangTidyCheck.h"
  11. namespace clang::tidy::llvm_check {
  12. /// Looks at conditionals and finds and replaces cases of ``cast<>``, which will
  13. /// assert rather than return a null pointer, and ``dyn_cast<>`` where
  14. /// the return value is not captured. Additionally, finds and replaces cases that match the
  15. /// pattern ``var && isa<X>(var)``, where ``var`` is evaluated twice.
  16. ///
  17. /// Finds cases like these:
  18. /// \code
  19. /// if (auto x = cast<X>(y)) {}
  20. /// // is replaced by:
  21. /// if (auto x = dyn_cast<X>(y)) {}
  22. ///
  23. /// if (cast<X>(y)) {}
  24. /// // is replaced by:
  25. /// if (isa<X>(y)) {}
  26. ///
  27. /// if (dyn_cast<X>(y)) {}
  28. /// // is replaced by:
  29. /// if (isa<X>(y)) {}
  30. ///
  31. /// if (var && isa<T>(var)) {}
  32. /// // is replaced by:
  33. /// if (isa_and_nonnull<T>(var.foo())) {}
  34. /// \endcode
  35. ///
  36. /// // Other cases are ignored, e.g.:
  37. /// \code
  38. /// if (auto f = cast<Z>(y)->foo()) {}
  39. /// if (cast<Z>(y)->foo()) {}
  40. /// if (X.cast(y)) {}
  41. /// \endcode
  42. ///
  43. /// For the user-facing documentation see:
  44. /// http://clang.llvm.org/extra/clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals.html
  45. class PreferIsaOrDynCastInConditionalsCheck : public ClangTidyCheck {
  46. public:
  47. PreferIsaOrDynCastInConditionalsCheck(StringRef Name,
  48. ClangTidyContext *Context)
  49. : ClangTidyCheck(Name, Context) {}
  50. bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
  51. return LangOpts.CPlusPlus;
  52. }
  53. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  54. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  55. };
  56. } // namespace clang::tidy::llvm_check
  57. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H