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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.cpp