clang-tools 20.0.0git
ProTypeConstCastCheck.cpp
Go to the documentation of this file.
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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
16
17static bool hasConstQualifier(QualType Type) {
18 const QualType PtrType = Type->getPointeeType();
19 if (!PtrType.isNull())
20 return hasConstQualifier(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(PtrType);
29 return Type.isVolatileQualified();
30}
31
33 ClangTidyContext *Context)
34 : ClangTidyCheck(Name, Context),
35 StrictMode(Options.getLocalOrGlobal("StrictMode", false)) {}
36
38 Options.store(Opts, "StrictMode", StrictMode);
39}
40
41void ProTypeConstCastCheck::registerMatchers(MatchFinder *Finder) {
42 Finder->addMatcher(cxxConstCastExpr().bind("cast"), this);
43}
44
45void ProTypeConstCastCheck::check(const MatchFinder::MatchResult &Result) {
46 const auto *MatchedCast = Result.Nodes.getNodeAs<CXXConstCastExpr>("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(SourceType) && !hasConstQualifier(TargetType);
58 const bool RemovingVolatile =
59 hasVolatileQualifier(SourceType) && !hasVolatileQualifier(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
llvm::SmallString< 256U > Name
NodeType Type
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
ProTypeConstCastCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
static bool hasConstQualifier(QualType Type)
static bool hasVolatileQualifier(QualType Type)
llvm::StringMap< ClangTidyValue > OptionMap