clang-tools 20.0.0git
Public Member Functions | List of all members
clang::tidy::llvm_check::PreferIsaOrDynCastInConditionalsCheck Class Reference

Looks at conditionals and finds and replaces cases of cast<>, which will assert rather than return a null pointer, and dyn_cast<> where the return value is not captured. More...

#include <PreferIsaOrDynCastInConditionalsCheck.h>

Inheritance diagram for clang::tidy::llvm_check::PreferIsaOrDynCastInConditionalsCheck:
Inheritance graph
[legend]

Public Member Functions

 PreferIsaOrDynCastInConditionalsCheck (StringRef Name, ClangTidyContext *Context)
 
bool isLanguageVersionSupported (const LangOptions &LangOpts) const override
 Override this to disable registering matchers and PP callbacks if an invalid language version is being used.
 
void registerMatchers (ast_matchers::MatchFinder *Finder) override
 Override this to register AST matchers with Finder.
 
void check (const ast_matchers::MatchFinder::MatchResult &Result) override
 ClangTidyChecks that register ASTMatchers should do the actual work in here.
 
- Public Member Functions inherited from clang::tidy::ClangTidyCheck
 ClangTidyCheck (StringRef CheckName, ClangTidyContext *Context)
 Initializes the check with CheckName and Context.
 
virtual bool isLanguageVersionSupported (const LangOptions &LangOpts) const
 Override this to disable registering matchers and PP callbacks if an invalid language version is being used.
 
virtual void registerPPCallbacks (const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP)
 Override this to register PPCallbacks in the preprocessor.
 
virtual void registerMatchers (ast_matchers::MatchFinder *Finder)
 Override this to register AST matchers with Finder.
 
virtual void check (const ast_matchers::MatchFinder::MatchResult &Result)
 ClangTidyChecks that register ASTMatchers should do the actual work in here.
 
DiagnosticBuilder diag (SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
 Add a diagnostic with the check's name.
 
DiagnosticBuilder diag (StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
 Add a diagnostic with the check's name.
 
DiagnosticBuilder configurationDiag (StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
 Adds a diagnostic to report errors in the check's configuration.
 
virtual void storeOptions (ClangTidyOptions::OptionMap &Options)
 Should store all options supported by this check with their current values or default values for options that haven't been overridden.
 

Additional Inherited Members

- Protected Member Functions inherited from clang::tidy::ClangTidyCheck
StringRef getCurrentMainFile () const
 Returns the main file name of the current translation unit.
 
const LangOptions & getLangOpts () const
 Returns the language options from the context.
 
bool areDiagsSelfContained () const
 Returns true when the check is run in a use case when only 1 fix will be applied at a time.
 
StringRef getID () const override
 
- Protected Attributes inherited from clang::tidy::ClangTidyCheck
OptionsView Options
 

Detailed Description

Looks at conditionals and finds and replaces cases of cast<>, which will assert rather than return a null pointer, and dyn_cast<> where the return value is not captured.

Additionally, finds and replaces cases that match the pattern var && isa<X>(var), where var is evaluated twice.

Finds cases like these:

if (auto x = cast<X>(y)) {}
// is replaced by:
if (auto x = dyn_cast<X>(y)) {}
if (cast<X>(y)) {}
// is replaced by:
if (isa<X>(y)) {}
if (dyn_cast<X>(y)) {}
// is replaced by:
if (isa<X>(y)) {}
if (var && isa<T>(var)) {}
// is replaced by:
if (isa_and_nonnull<T>(var.foo())) {}
Symbol var(llvm::StringRef Name)
Definition: TestIndex.cpp:78

// Other cases are ignored, e.g.:

if (auto f = cast<Z>(y)->foo()) {}
if (cast<Z>(y)->foo()) {}
if (X.cast(y)) {}
int X
int foo()
Definition: foo.cpp:2

For the user-facing documentation see: http://clang.llvm.org/extra/clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals.html

Definition at line 49 of file PreferIsaOrDynCastInConditionalsCheck.h.

Constructor & Destructor Documentation

◆ PreferIsaOrDynCastInConditionalsCheck()

clang::tidy::llvm_check::PreferIsaOrDynCastInConditionalsCheck::PreferIsaOrDynCastInConditionalsCheck ( StringRef  Name,
ClangTidyContext Context 
)
inline

Definition at line 51 of file PreferIsaOrDynCastInConditionalsCheck.h.

Member Function Documentation

◆ check()

void clang::tidy::llvm_check::PreferIsaOrDynCastInConditionalsCheck::check ( const ast_matchers::MatchFinder::MatchResult &  Result)
overridevirtual

ClangTidyChecks that register ASTMatchers should do the actual work in here.

Reimplemented from clang::tidy::ClangTidyCheck.

Definition at line 65 of file PreferIsaOrDynCastInConditionalsCheck.cpp.

References clang::tidy::ClangTidyCheck::diag(), and clang::tidy::ClangTidyCheck::getLangOpts().

◆ isLanguageVersionSupported()

bool clang::tidy::llvm_check::PreferIsaOrDynCastInConditionalsCheck::isLanguageVersionSupported ( const LangOptions &  LangOpts) const
inlineoverridevirtual

Override this to disable registering matchers and PP callbacks if an invalid language version is being used.

For example if a check is examining overloaded functions then this should be overridden to return false when the CPlusPlus flag is not set in LangOpts.

Reimplemented from clang::tidy::ClangTidyCheck.

Definition at line 54 of file PreferIsaOrDynCastInConditionalsCheck.h.

◆ registerMatchers()

void clang::tidy::llvm_check::PreferIsaOrDynCastInConditionalsCheck::registerMatchers ( ast_matchers::MatchFinder *  Finder)
overridevirtual

Override this to register AST matchers with Finder.

This should be used by clang-tidy checks that analyze code properties that dependent on AST knowledge.

You can register as many matchers as necessary with Finder. Usually, "this" will be used as callback, but you can also specify other callback classes. Thereby, different matchers can trigger different callbacks.

This will only be executed if the function isLanguageVersionSupported returns true.

If you need to merge information between the different matchers, you can store these as members of the derived class. However, note that all matches occur in the order of the AST traversal.

Reimplemented from clang::tidy::ClangTidyCheck.

Definition at line 24 of file PreferIsaOrDynCastInConditionalsCheck.cpp.

References Condition.


The documentation for this class was generated from the following files: