clang-tools 20.0.0git
AvoidConstOrRefDataMembersCheck.cpp
Go to the documentation of this file.
1//===--- AvoidConstOrRefDataMembersCheck.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
16namespace {
17
18AST_MATCHER(FieldDecl, isMemberOfLambda) {
19 return Node.getParent()->isLambda();
20}
21
22struct MemberFunctionInfo {
23 bool Declared{};
24 bool Deleted{};
25};
26
27struct MemberFunctionPairInfo {
28 MemberFunctionInfo Copy{};
29 MemberFunctionInfo Move{};
30};
31
32MemberFunctionPairInfo getConstructorsInfo(CXXRecordDecl const &Node) {
33 MemberFunctionPairInfo Constructors{};
34
35 for (CXXConstructorDecl const *Ctor : Node.ctors()) {
36 if (Ctor->isCopyConstructor()) {
37 Constructors.Copy.Declared = true;
38 if (Ctor->isDeleted())
39 Constructors.Copy.Deleted = true;
40 }
41 if (Ctor->isMoveConstructor()) {
42 Constructors.Move.Declared = true;
43 if (Ctor->isDeleted())
44 Constructors.Move.Deleted = true;
45 }
46 }
47
48 return Constructors;
49}
50
51MemberFunctionPairInfo getAssignmentsInfo(CXXRecordDecl const &Node) {
52 MemberFunctionPairInfo Assignments{};
53
54 for (CXXMethodDecl const *Method : Node.methods()) {
55 if (Method->isCopyAssignmentOperator()) {
56 Assignments.Copy.Declared = true;
57 if (Method->isDeleted())
58 Assignments.Copy.Deleted = true;
59 }
60
61 if (Method->isMoveAssignmentOperator()) {
62 Assignments.Move.Declared = true;
63 if (Method->isDeleted())
64 Assignments.Move.Deleted = true;
65 }
66 }
67
68 return Assignments;
69}
70
71AST_MATCHER(CXXRecordDecl, isCopyableOrMovable) {
72 MemberFunctionPairInfo Constructors = getConstructorsInfo(Node);
73 MemberFunctionPairInfo Assignments = getAssignmentsInfo(Node);
74
75 if (Node.hasSimpleCopyConstructor() ||
76 (Constructors.Copy.Declared && !Constructors.Copy.Deleted))
77 return true;
78 if (Node.hasSimpleMoveConstructor() ||
79 (Constructors.Move.Declared && !Constructors.Move.Deleted))
80 return true;
81 if (Node.hasSimpleCopyAssignment() ||
82 (Assignments.Copy.Declared && !Assignments.Copy.Deleted))
83 return true;
84 if (Node.hasSimpleMoveAssignment() ||
85 (Assignments.Move.Declared && !Assignments.Move.Deleted))
86 return true;
87
88 return false;
89}
90
91} // namespace
92
94 Finder->addMatcher(
95 fieldDecl(
96 unless(isMemberOfLambda()),
97 anyOf(
98 fieldDecl(hasType(hasCanonicalType(referenceType()))).bind("ref"),
99 fieldDecl(hasType(qualType(isConstQualified()))).bind("const")),
100 hasDeclContext(cxxRecordDecl(isCopyableOrMovable()))),
101 this);
102}
103
105 const MatchFinder::MatchResult &Result) {
106 if (const auto *MatchedDecl = Result.Nodes.getNodeAs<FieldDecl>("ref"))
107 diag(MatchedDecl->getLocation(), "member %0 of type %1 is a reference")
108 << MatchedDecl << MatchedDecl->getType();
109 if (const auto *MatchedDecl = Result.Nodes.getNodeAs<FieldDecl>("const"))
110 diag(MatchedDecl->getLocation(), "member %0 of type %1 is const qualified")
111 << MatchedDecl << MatchedDecl->getType();
112}
113
114} // namespace clang::tidy::cppcoreguidelines
MemberFunctionInfo Move
MemberFunctionInfo Copy
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER(CXXRecordDecl, hasPublicVirtualOrProtectedNonVirtualDestructor)