clang-tools 20.0.0git
MultiLevelImplicitPointerConversionCheck.cpp
Go to the documentation of this file.
1//===--- MultiLevelImplicitPointerConversionCheck.cpp - clang-tidy --------===//
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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::bugprone {
16
17static unsigned getPointerLevel(const QualType &PtrType) {
18 if (!PtrType->isPointerType())
19 return 0U;
20
21 return 1U + getPointerLevel(PtrType->castAs<PointerType>()->getPointeeType());
22}
23
24namespace {
25
26AST_MATCHER(ImplicitCastExpr, isMultiLevelPointerConversion) {
27 const QualType TargetType = Node.getType()
28 .getCanonicalType()
29 .getNonReferenceType()
30 .getUnqualifiedType();
31 const QualType SourceType = Node.getSubExpr()
32 ->getType()
33 .getCanonicalType()
34 .getNonReferenceType()
35 .getUnqualifiedType();
36
37 if (TargetType == SourceType)
38 return false;
39
40 const unsigned TargetPtrLevel = getPointerLevel(TargetType);
41 if (0U == TargetPtrLevel)
42 return false;
43
44 const unsigned SourcePtrLevel = getPointerLevel(SourceType);
45 if (0U == SourcePtrLevel)
46 return false;
47
48 return SourcePtrLevel != TargetPtrLevel;
49}
50
51AST_MATCHER(QualType, isPointerType) {
52 const QualType Type =
53 Node.getCanonicalType().getNonReferenceType().getUnqualifiedType();
54
55 return !Type.isNull() && Type->isPointerType();
56}
57
58} // namespace
59
61 MatchFinder *Finder) {
62 Finder->addMatcher(
63 implicitCastExpr(hasCastKind(CK_BitCast), isMultiLevelPointerConversion(),
64 unless(hasParent(explicitCastExpr(
65 hasDestinationType(isPointerType())))))
66 .bind("expr"),
67 this);
68}
69
70std::optional<TraversalKind>
72 return TK_AsIs;
73}
74
76 const MatchFinder::MatchResult &Result) {
77 const auto *MatchedExpr = Result.Nodes.getNodeAs<ImplicitCastExpr>("expr");
78 QualType Target = MatchedExpr->getType().getDesugaredType(*Result.Context);
79 QualType Source =
80 MatchedExpr->getSubExpr()->getType().getDesugaredType(*Result.Context);
81
82 diag(MatchedExpr->getExprLoc(), "multilevel pointer conversion from %0 to "
83 "%1, please use explicit cast")
84 << Source << Target;
85}
86
87} // namespace clang::tidy::bugprone
NodeType Type
::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.
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)