1//===--- UnconventionalAssignOperatorCheck.cpp - clang-tidy -----*- C++ -*-===//
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 "UnconventionalAssignOperatorCheck.h"
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::misc {
16
17namespace {
18
19AST_MATCHER_P(CXXMethodDecl, firstParameter,
20 ast_matchers::internal::Matcher<ParmVarDecl>, InnerMatcher) {
21 unsigned N = Node.isExplicitObjectMemberFunction() ? 1 : 0;
22 return (N < Node.parameters().size() &&
23 InnerMatcher.matches(Node: *Node.parameters()[N], Finder, Builder));
24}
25} // namespace
26
27void UnconventionalAssignOperatorCheck::registerMatchers(
28 ast_matchers::MatchFinder *Finder) {
29 const auto HasGoodReturnType =
30 cxxMethodDecl(returns(InnerMatcher: hasCanonicalType(InnerMatcher: lValueReferenceType(pointee(
31 unless(isConstQualified()),
32 anyOf(autoType(), hasDeclaration(InnerMatcher: equalsBoundNode(ID: "class"))))))));
33
34 const auto IsSelf = qualType(hasCanonicalType(
35 InnerMatcher: anyOf(hasDeclaration(InnerMatcher: equalsBoundNode(ID: "class")),
36 referenceType(pointee(hasDeclaration(InnerMatcher: equalsBoundNode(ID: "class")))))));
37 const auto IsAssign =
38 cxxMethodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())),
39 hasName(Name: "operator="), ofClass(InnerMatcher: recordDecl().bind(ID: "class")))
40 .bind(ID: "method");
41 const auto IsSelfAssign =
42 cxxMethodDecl(IsAssign, firstParameter(InnerMatcher: parmVarDecl(hasType(InnerMatcher: IsSelf))))
43 .bind(ID: "method");
44
45 Finder->addMatcher(
46 NodeMatch: cxxMethodDecl(IsAssign, unless(HasGoodReturnType)).bind(ID: "ReturnType"),
47 Action: this);
48
49 const auto BadSelf = qualType(hasCanonicalType(InnerMatcher: referenceType(
50 anyOf(lValueReferenceType(pointee(unless(isConstQualified()))),
51 rValueReferenceType(pointee(isConstQualified()))))));
52
53 Finder->addMatcher(
54 NodeMatch: cxxMethodDecl(IsSelfAssign, firstParameter(InnerMatcher: parmVarDecl(hasType(InnerMatcher: BadSelf))))
55 .bind(ID: "ArgumentType"),
56 Action: this);
57
58 Finder->addMatcher(
59 NodeMatch: cxxMethodDecl(IsSelfAssign, anyOf(isConst(), isVirtual())).bind(ID: "cv"),
60 Action: this);
61
62 const auto IsBadReturnStatement = returnStmt(unless(has(ignoringParenImpCasts(
63 InnerMatcher: anyOf(unaryOperator(hasOperatorName(Name: "*"), hasUnaryOperand(InnerMatcher: cxxThisExpr())),
64 cxxOperatorCallExpr(argumentCountIs(N: 1),
65 callee(InnerMatcher: unresolvedLookupExpr()),
66 hasArgument(N: 0, InnerMatcher: cxxThisExpr())),
67 cxxOperatorCallExpr(
68 hasOverloadedOperatorName(Name: "="),
69 hasArgument(N: 0, InnerMatcher: unaryOperator(hasOperatorName(Name: "*"),
70 hasUnaryOperand(InnerMatcher: cxxThisExpr())))),
71 binaryOperator(
72 hasOperatorName(Name: "="),
73 hasLHS(InnerMatcher: unaryOperator(hasOperatorName(Name: "*"),
74 hasUnaryOperand(InnerMatcher: cxxThisExpr())))))))));
75 const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType);
76
77 Finder->addMatcher(NodeMatch: returnStmt(IsBadReturnStatement, forFunction(InnerMatcher: IsGoodAssign))
78 .bind(ID: "returnStmt"),
79 Action: this);
80}
81
82void UnconventionalAssignOperatorCheck::check(
83 const MatchFinder::MatchResult &Result) {
84 if (const auto *RetStmt = Result.Nodes.getNodeAs<ReturnStmt>(ID: "returnStmt")) {
85 diag(Loc: RetStmt->getBeginLoc(), Description: "operator=() should always return '*this'");
86 } else {
87 const auto *Method = Result.Nodes.getNodeAs<CXXMethodDecl>(ID: "method");
88 if (Result.Nodes.getNodeAs<CXXMethodDecl>(ID: "ReturnType"))
89 diag(Loc: Method->getBeginLoc(), Description: "operator=() should return '%0&'")
90 << Method->getParent()->getName();
91 if (Result.Nodes.getNodeAs<CXXMethodDecl>(ID: "ArgumentType"))
92 diag(Loc: Method->getBeginLoc(),
93 Description: "operator=() should take '%0 const&'%select{|, '%0&&'}1 or '%0'")
94 << Method->getParent()->getName() << getLangOpts().CPlusPlus11;
95 if (Result.Nodes.getNodeAs<CXXMethodDecl>(ID: "cv"))
96 diag(Loc: Method->getBeginLoc(),
97 Description: "operator=() should not be marked '%select{const|virtual}0'")
98 << !Method->isConst();
99 }
100}
101
102} // namespace clang::tidy::misc
103

source code of clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp