| 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 | |
| 9 | #include "MultiLevelImplicitPointerConversionCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | |
| 13 | using namespace clang::ast_matchers; |
| 14 | |
| 15 | namespace clang::tidy::bugprone { |
| 16 | |
| 17 | static unsigned getPointerLevel(const QualType &PtrType) { |
| 18 | if (!PtrType->isPointerType()) |
| 19 | return 0U; |
| 20 | |
| 21 | return 1U + getPointerLevel(PtrType: PtrType->castAs<PointerType>()->getPointeeType()); |
| 22 | } |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | AST_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(PtrType: TargetType); |
| 41 | if (0U == TargetPtrLevel) |
| 42 | return false; |
| 43 | |
| 44 | const unsigned SourcePtrLevel = getPointerLevel(PtrType: SourceType); |
| 45 | if (0U == SourcePtrLevel) |
| 46 | return false; |
| 47 | |
| 48 | return SourcePtrLevel != TargetPtrLevel; |
| 49 | } |
| 50 | |
| 51 | AST_MATCHER(QualType, isPointerType) { |
| 52 | const QualType Type = |
| 53 | Node.getCanonicalType().getNonReferenceType().getUnqualifiedType(); |
| 54 | |
| 55 | return !Type.isNull() && Type->isPointerType(); |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | MultiLevelImplicitPointerConversionCheck:: |
| 61 | MultiLevelImplicitPointerConversionCheck(StringRef Name, |
| 62 | ClangTidyContext *Context) |
| 63 | : ClangTidyCheck(Name, Context), EnableInC(Options.get(LocalName: "EnableInC" , Default: true)) { |
| 64 | } |
| 65 | |
| 66 | void MultiLevelImplicitPointerConversionCheck::storeOptions( |
| 67 | ClangTidyOptions::OptionMap &Opts) { |
| 68 | Options.store(Options&: Opts, LocalName: "EnableInC" , Value: EnableInC); |
| 69 | } |
| 70 | |
| 71 | void MultiLevelImplicitPointerConversionCheck::registerMatchers( |
| 72 | MatchFinder *Finder) { |
| 73 | Finder->addMatcher( |
| 74 | NodeMatch: implicitCastExpr(hasCastKind(Kind: CK_BitCast), isMultiLevelPointerConversion(), |
| 75 | unless(hasParent(explicitCastExpr( |
| 76 | hasDestinationType(InnerMatcher: isPointerType()))))) |
| 77 | .bind(ID: "expr" ), |
| 78 | Action: this); |
| 79 | } |
| 80 | |
| 81 | std::optional<TraversalKind> |
| 82 | MultiLevelImplicitPointerConversionCheck::getCheckTraversalKind() const { |
| 83 | return TK_AsIs; |
| 84 | } |
| 85 | |
| 86 | void MultiLevelImplicitPointerConversionCheck::check( |
| 87 | const MatchFinder::MatchResult &Result) { |
| 88 | const auto *MatchedExpr = Result.Nodes.getNodeAs<ImplicitCastExpr>(ID: "expr" ); |
| 89 | QualType Target = MatchedExpr->getType().getDesugaredType(*Result.Context); |
| 90 | QualType Source = |
| 91 | MatchedExpr->getSubExpr()->getType().getDesugaredType(*Result.Context); |
| 92 | |
| 93 | diag(MatchedExpr->getExprLoc(), "multilevel pointer conversion from %0 to " |
| 94 | "%1, please use explicit cast" ) |
| 95 | << Source << Target; |
| 96 | } |
| 97 | |
| 98 | } // namespace clang::tidy::bugprone |
| 99 | |