1//===--- ProTypeConstCastCheck.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 "ProTypeConstCastCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::cppcoreguidelines {
16
17static bool hasConstQualifier(QualType Type) {
18 const QualType PtrType = Type->getPointeeType();
19 if (!PtrType.isNull())
20 return hasConstQualifier(Type: PtrType);
21
22 return Type.isConstQualified();
23}
24
25static bool hasVolatileQualifier(QualType Type) {
26 const QualType PtrType = Type->getPointeeType();
27 if (!PtrType.isNull())
28 return hasVolatileQualifier(Type: PtrType);
29 return Type.isVolatileQualified();
30}
31
32ProTypeConstCastCheck::ProTypeConstCastCheck(StringRef Name,
33 ClangTidyContext *Context)
34 : ClangTidyCheck(Name, Context),
35 StrictMode(Options.getLocalOrGlobal(LocalName: "StrictMode", Default: false)) {}
36
37void ProTypeConstCastCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
38 Options.store(Options&: Opts, LocalName: "StrictMode", Value: StrictMode);
39}
40
41void ProTypeConstCastCheck::registerMatchers(MatchFinder *Finder) {
42 Finder->addMatcher(NodeMatch: cxxConstCastExpr().bind(ID: "cast"), Action: this);
43}
44
45void ProTypeConstCastCheck::check(const MatchFinder::MatchResult &Result) {
46 const auto *MatchedCast = Result.Nodes.getNodeAs<CXXConstCastExpr>(ID: "cast");
47 if (StrictMode) {
48 diag(MatchedCast->getOperatorLoc(), "do not use const_cast");
49 return;
50 }
51
52 const QualType TargetType = MatchedCast->getType().getCanonicalType();
53 const QualType SourceType =
54 MatchedCast->getSubExpr()->getType().getCanonicalType();
55
56 const bool RemovingConst =
57 hasConstQualifier(Type: SourceType) && !hasConstQualifier(Type: TargetType);
58 const bool RemovingVolatile =
59 hasVolatileQualifier(Type: SourceType) && !hasVolatileQualifier(Type: TargetType);
60
61 if (!RemovingConst && !RemovingVolatile) {
62 // Cast is doing nothing.
63 return;
64 }
65
66 diag(MatchedCast->getOperatorLoc(),
67 "do not use const_cast to remove%select{| const}0%select{| "
68 "and}2%select{| volatile}1 qualifier")
69 << RemovingConst << RemovingVolatile
70 << (RemovingConst && RemovingVolatile);
71}
72
73} // namespace clang::tidy::cppcoreguidelines
74

source code of clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeConstCastCheck.cpp