clang-tools  16.0.0git
PreferIsaOrDynCastInConditionalsCheck.h
Go to the documentation of this file.
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 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
11 
12 #include "../ClangTidyCheck.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace llvm_check {
17 
18 /// Looks at conditionals and finds and replaces cases of ``cast<>``, which will
19 /// assert rather than return a null pointer, and ``dyn_cast<>`` where
20 /// the return value is not captured. Additionally, finds and replaces cases that match the
21 /// pattern ``var && isa<X>(var)``, where ``var`` is evaluated twice.
22 ///
23 /// Finds cases like these:
24 /// \code
25 /// if (auto x = cast<X>(y)) {}
26 /// // is replaced by:
27 /// if (auto x = dyn_cast<X>(y)) {}
28 ///
29 /// if (cast<X>(y)) {}
30 /// // is replaced by:
31 /// if (isa<X>(y)) {}
32 ///
33 /// if (dyn_cast<X>(y)) {}
34 /// // is replaced by:
35 /// if (isa<X>(y)) {}
36 ///
37 /// if (var && isa<T>(var)) {}
38 /// // is replaced by:
39 /// if (isa_and_nonnull<T>(var.foo())) {}
40 /// \endcode
41 ///
42 /// // Other cases are ignored, e.g.:
43 /// \code
44 /// if (auto f = cast<Z>(y)->foo()) {}
45 /// if (cast<Z>(y)->foo()) {}
46 /// if (X.cast(y)) {}
47 /// \endcode
48 ///
49 /// For the user-facing documentation see:
50 /// http://clang.llvm.org/extra/clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals.html
52 public:
54  ClangTidyContext *Context)
55  : ClangTidyCheck(Name, Context) {}
56  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
57  return LangOpts.CPlusPlus;
58  }
59  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
60  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
61 };
62 
63 } // namespace llvm_check
64 } // namespace tidy
65 } // namespace clang
66 
67 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERISAORDYNCASTINCONDITIONALSCHECK_H
clang::tidy::llvm_check::PreferIsaOrDynCastInConditionalsCheck::PreferIsaOrDynCastInConditionalsCheck
PreferIsaOrDynCastInConditionalsCheck(StringRef Name, ClangTidyContext *Context)
Definition: PreferIsaOrDynCastInConditionalsCheck.h:53
clang::tidy::llvm_check::PreferIsaOrDynCastInConditionalsCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:70
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:53
clang::tidy::llvm_check::PreferIsaOrDynCastInConditionalsCheck::isLanguageVersionSupported
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
Definition: PreferIsaOrDynCastInConditionalsCheck.h:56
clang::tidy::llvm_check::PreferIsaOrDynCastInConditionalsCheck
Looks at conditionals and finds and replaces cases of cast<>, which will assert rather than return a ...
Definition: PreferIsaOrDynCastInConditionalsCheck.h:51
clang::tidy::llvm_check::PreferIsaOrDynCastInConditionalsCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:25
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:67
Name
Token Name
Definition: MacroToEnumCheck.cpp:89
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
LangOpts
const LangOptions * LangOpts
Definition: ExtractFunction.cpp:375