1 //===--- CastingThroughVoidCheck.cpp - clang-tidy -------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "CastingThroughVoidCheck.h"
10 #include "clang/AST/Expr.h"
11 #include "clang/AST/Type.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
15 using namespace clang::ast_matchers
;
17 namespace clang::tidy::bugprone
{
19 void CastingThroughVoidCheck::registerMatchers(MatchFinder
*Finder
) {
23 qualType(unless(hasCanonicalType(pointsTo(voidType()))))
24 .bind("target_type")),
28 expr(hasType(qualType(unless(pointsTo(voidType())))
29 .bind("source_type")))),
31 qualType(pointsTo(voidType())).bind("void_type")))
36 void CastingThroughVoidCheck::check(const MatchFinder::MatchResult
&Result
) {
37 const auto TT
= *Result
.Nodes
.getNodeAs
<QualType
>("target_type");
38 const auto ST
= *Result
.Nodes
.getNodeAs
<QualType
>("source_type");
39 const auto VT
= *Result
.Nodes
.getNodeAs
<QualType
>("void_type");
40 const auto *CE
= Result
.Nodes
.getNodeAs
<ExplicitCastExpr
>("cast");
41 diag(CE
->getExprLoc(),
42 "do not cast %0 to %1 through %2; use reinterpret_cast instead")
46 } // namespace clang::tidy::bugprone