10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
18 if (!PtrType->isPointerType())
21 return 1U +
getPointerLevel(PtrType->castAs<PointerType>()->getPointeeType());
26AST_MATCHER(ImplicitCastExpr, isMultiLevelPointerConversion) {
27 const QualType TargetType =
Node.getType()
29 .getNonReferenceType()
30 .getUnqualifiedType();
31 const QualType SourceType =
Node.getSubExpr()
34 .getNonReferenceType()
35 .getUnqualifiedType();
37 if (TargetType == SourceType)
41 if (0U == TargetPtrLevel)
45 if (0U == SourcePtrLevel)
48 return SourcePtrLevel != TargetPtrLevel;
53 Node.getCanonicalType().getNonReferenceType().getUnqualifiedType();
55 return !
Type.isNull() &&
Type->isPointerType();
61 MatchFinder *Finder) {
63 implicitCastExpr(hasCastKind(CK_BitCast), isMultiLevelPointerConversion(),
64 unless(hasParent(explicitCastExpr(
65 hasDestinationType(isPointerType())))))
70std::optional<TraversalKind>
76 const MatchFinder::MatchResult &Result) {
77 const auto *MatchedExpr = Result.Nodes.getNodeAs<ImplicitCastExpr>(
"expr");
78 QualType Target = MatchedExpr->getType().getDesugaredType(*Result.Context);
80 MatchedExpr->getSubExpr()->getType().getDesugaredType(*Result.Context);
82 diag(MatchedExpr->getExprLoc(),
"multilevel pointer conversion from %0 to "
83 "%1, please use explicit cast")
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::optional< TraversalKind > getCheckTraversalKind() const override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static unsigned getPointerLevel(const QualType &PtrType)
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)