10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
19 return Node.getParent()->isLambda();
22struct MemberFunctionInfo {
27struct MemberFunctionPairInfo {
32MemberFunctionPairInfo 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;
51MemberFunctionPairInfo getAssignmentsInfo(CXXRecordDecl
const &Node) {
52 MemberFunctionPairInfo Assignments{};
54 for (CXXMethodDecl
const *Method :
Node.methods()) {
55 if (
Method->isCopyAssignmentOperator()) {
56 Assignments.Copy.Declared =
true;
58 Assignments.Copy.Deleted =
true;
61 if (
Method->isMoveAssignmentOperator()) {
62 Assignments.Move.Declared =
true;
64 Assignments.Move.Deleted =
true;
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))
96 unless(isMemberOfLambda()),
98 fieldDecl(hasType(hasCanonicalType(referenceType()))).bind(
"ref"),
99 fieldDecl(hasType(qualType(isConstQualified()))).bind(
"const")),
100 hasDeclContext(cxxRecordDecl(isCopyableOrMovable()))),
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();
::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)