brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · aed7aa8 Raw
65 lines · c
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H11 12#include "../ClangTidyCheck.h"13 14namespace clang::tidy::llvm_check {15 16/// Looks at conditionals and finds and replaces cases of ``cast<>``, which will17/// assert rather than return a null pointer, and ``dyn_cast<>`` where18/// the return value is not captured.  Additionally, finds and replaces cases19/// that match the pattern ``var && isa<X>(var)``, where ``var`` is evaluated20/// twice.21///22/// Finds cases like these:23/// \code24///  if (auto x = cast<X>(y)) {}25///  // is replaced by:26///  if (auto x = dyn_cast<X>(y)) {}27///28///  if (cast<X>(y)) {}29///  // is replaced by:30///  if (isa<X>(y)) {}31///32///  if (dyn_cast<X>(y)) {}33///  // is replaced by:34///  if (isa<X>(y)) {}35///36///  if (var && isa<T>(var)) {}37///  // is replaced by:38///  if (isa_and_nonnull<T>(var.foo())) {}39/// \endcode40///41///  // Other cases are ignored, e.g.:42/// \code43///  if (auto f = cast<Z>(y)->foo()) {}44///  if (cast<Z>(y)->foo()) {}45///  if (X.cast(y)) {}46/// \endcode47///48/// For the user-facing documentation see:49/// https://clang.llvm.org/extra/clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals.html50class PreferIsaOrDynCastInConditionalsCheck : public ClangTidyCheck {51public:52  PreferIsaOrDynCastInConditionalsCheck(StringRef Name,53                                        ClangTidyContext *Context)54      : ClangTidyCheck(Name, Context) {}55  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {56    return LangOpts.CPlusPlus;57  }58  void registerMatchers(ast_matchers::MatchFinder *Finder) override;59  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;60};61 62} // namespace clang::tidy::llvm_check63 64#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H65