11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
14 using namespace clang::ast_matchers;
20 void AssignOperatorSignatureCheck::registerMatchers(
21 ast_matchers::MatchFinder *
Finder) {
24 if (!getLangOpts().CPlusPlus)
27 const auto HasGoodReturnType = cxxMethodDecl(returns(
28 lValueReferenceType(pointee(unless(isConstQualified()),
29 hasDeclaration(equalsBoundNode(
"class"))))));
31 const auto IsSelf = qualType(
32 anyOf(hasDeclaration(equalsBoundNode(
"class")),
33 referenceType(pointee(hasDeclaration(equalsBoundNode(
"class"))))));
34 const auto IsSelfAssign =
35 cxxMethodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())),
36 hasName(
"operator="), ofClass(recordDecl().bind(
"class")),
37 hasParameter(0, parmVarDecl(hasType(IsSelf))))
41 cxxMethodDecl(IsSelfAssign, unless(HasGoodReturnType)).bind(
"ReturnType"),
44 const auto BadSelf = referenceType(
45 anyOf(lValueReferenceType(pointee(unless(isConstQualified()))),
46 rValueReferenceType(pointee(isConstQualified()))));
49 cxxMethodDecl(IsSelfAssign,
50 hasParameter(0, parmVarDecl(hasType(BadSelf))))
51 .bind(
"ArgumentType"),
55 cxxMethodDecl(IsSelfAssign, anyOf(isConst(), isVirtual())).bind(
"cv"),
59 void AssignOperatorSignatureCheck::check(
60 const MatchFinder::MatchResult &
Result) {
61 const auto* Method = Result.Nodes.getNodeAs<CXXMethodDecl>(
"method");
62 std::string
Name = Method->getParent()->getName();
64 static const char *
const Messages[][2] = {
65 {
"ReturnType",
"operator=() should return '%0&'"},
66 {
"ArgumentType",
"operator=() should take '%0 const&', '%0&&' or '%0'"},
67 {
"cv",
"operator=() should not be marked '%1'"}
70 for (
const auto &Message : Messages) {
71 if (Result.Nodes.getNodeAs<Decl>(Message[0]))
72 diag(Method->getLocStart(), Message[1])
73 << Name << (Method->isConst() ?
"const" :
"virtual");
std::unique_ptr< ast_matchers::MatchFinder > Finder