1 | //===--- CastingThroughVoidCheck.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 "CastingThroughVoidCheck.h" |
10 | #include "clang/AST/ASTContext.h" |
11 | #include "clang/AST/Expr.h" |
12 | #include "clang/AST/Type.h" |
13 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
14 | #include "clang/ASTMatchers/ASTMatchers.h" |
15 | #include "llvm/ADT/StringSet.h" |
16 | |
17 | using namespace clang::ast_matchers; |
18 | |
19 | namespace clang::tidy::bugprone { |
20 | |
21 | void CastingThroughVoidCheck::registerMatchers(MatchFinder *Finder) { |
22 | Finder->addMatcher( |
23 | NodeMatch: explicitCastExpr( |
24 | hasDestinationType( |
25 | InnerMatcher: qualType(unless(hasCanonicalType(InnerMatcher: pointsTo(InnerMatcher: voidType())))) |
26 | .bind(ID: "target_type")), |
27 | hasSourceExpression( |
28 | InnerMatcher: explicitCastExpr( |
29 | hasSourceExpression( |
30 | InnerMatcher: expr(hasType(InnerMatcher: qualType().bind(ID: "source_type")))), |
31 | hasDestinationType( |
32 | InnerMatcher: qualType(pointsTo(InnerMatcher: voidType())).bind(ID: "void_type"))) |
33 | .bind(ID: "cast"))), |
34 | Action: this); |
35 | } |
36 | |
37 | void CastingThroughVoidCheck::check(const MatchFinder::MatchResult &Result) { |
38 | const auto TT = *Result.Nodes.getNodeAs<QualType>(ID: "target_type"); |
39 | const auto ST = *Result.Nodes.getNodeAs<QualType>(ID: "source_type"); |
40 | const auto VT = *Result.Nodes.getNodeAs<QualType>(ID: "void_type"); |
41 | const auto *CE = Result.Nodes.getNodeAs<ExplicitCastExpr>(ID: "cast"); |
42 | diag(CE->getExprLoc(), "do not cast %0 to %1 through %2") << ST << TT << VT; |
43 | } |
44 | |
45 | } // namespace clang::tidy::bugprone |
46 |