10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
20 StrictMode(Options.getLocalOrGlobal(
"StrictMode", true)) {}
29 cxxStaticCastExpr(hasCastKind(CK_BaseToDerived)).bind(
"cast"),
this);
33 const MatchFinder::MatchResult &Result) {
34 const auto *MatchedCast = Result.Nodes.getNodeAs<CXXStaticCastExpr>(
"cast");
36 QualType SourceType = MatchedCast->getSubExpr()->getType();
37 const auto *SourceDecl = SourceType->getPointeeCXXRecordDecl();
39 SourceDecl = SourceType->getAsCXXRecordDecl();
43 if (SourceDecl->isPolymorphic()) {
44 diag(MatchedCast->getOperatorLoc(),
45 "do not use static_cast to downcast from a base to a derived class; "
46 "use dynamic_cast instead")
47 << FixItHint::CreateReplacement(MatchedCast->getOperatorLoc(),
55 diag(MatchedCast->getOperatorLoc(),
56 "do not use static_cast to downcast from a base to a derived class");
llvm::SmallString< 256U > Name
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
ProTypeStaticCastDowncastCheck(StringRef Name, ClangTidyContext *Context)
llvm::StringMap< ClangTidyValue > OptionMap