1 //===--- AvoidConstOrRefDataMembersCheck.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 "AvoidConstOrRefDataMembersCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers
;
15 namespace clang::tidy::cppcoreguidelines
{
18 AST_MATCHER(FieldDecl
, isMemberOfLambda
) {
19 return Node
.getParent()->isLambda();
22 struct MemberFunctionInfo
{
27 struct MemberFunctionPairInfo
{
28 MemberFunctionInfo Copy
{};
29 MemberFunctionInfo Move
{};
32 MemberFunctionPairInfo
getConstructorsInfo(CXXRecordDecl
const &Node
) {
33 MemberFunctionPairInfo Constructors
{};
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;
41 if (Ctor
->isMoveConstructor()) {
42 Constructors
.Move
.Declared
= true;
43 if (Ctor
->isDeleted())
44 Constructors
.Move
.Deleted
= true;
51 MemberFunctionPairInfo
getAssignmentsInfo(CXXRecordDecl
const &Node
) {
52 MemberFunctionPairInfo Assignments
{};
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;
61 if (Method
->isMoveAssignmentOperator()) {
62 Assignments
.Move
.Declared
= true;
63 if (Method
->isDeleted())
64 Assignments
.Move
.Deleted
= true;
71 AST_MATCHER(CXXRecordDecl
, isCopyableOrMovable
) {
72 MemberFunctionPairInfo Constructors
= getConstructorsInfo(Node
);
73 MemberFunctionPairInfo Assignments
= getAssignmentsInfo(Node
);
75 if (Node
.hasSimpleCopyConstructor() ||
76 (Constructors
.Copy
.Declared
&& !Constructors
.Copy
.Deleted
))
78 if (Node
.hasSimpleMoveConstructor() ||
79 (Constructors
.Move
.Declared
&& !Constructors
.Move
.Deleted
))
81 if (Node
.hasSimpleCopyAssignment() ||
82 (Assignments
.Copy
.Declared
&& !Assignments
.Copy
.Deleted
))
84 if (Node
.hasSimpleMoveAssignment() ||
85 (Assignments
.Move
.Declared
&& !Assignments
.Move
.Deleted
))
93 void AvoidConstOrRefDataMembersCheck::registerMatchers(MatchFinder
*Finder
) {
96 unless(isMemberOfLambda()),
98 fieldDecl(hasType(hasCanonicalType(referenceType()))).bind("ref"),
99 fieldDecl(hasType(qualType(isConstQualified()))).bind("const")),
100 hasDeclContext(cxxRecordDecl(isCopyableOrMovable()))),
104 void AvoidConstOrRefDataMembersCheck::check(
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();
114 } // namespace clang::tidy::cppcoreguidelines