10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
20 ast_matchers::internal::Matcher<ParmVarDecl>, InnerMatcher) {
21 unsigned N =
Node.isExplicitObjectMemberFunction() ? 1 : 0;
22 return (N <
Node.parameters().size() &&
23 InnerMatcher.matches(*
Node.parameters()[N], Finder,
Builder));
28 ast_matchers::MatchFinder *Finder) {
29 const auto HasGoodReturnType =
30 cxxMethodDecl(returns(hasCanonicalType(lValueReferenceType(pointee(
31 unless(isConstQualified()),
32 anyOf(autoType(), hasDeclaration(equalsBoundNode(
"class"))))))));
34 const auto IsSelf = qualType(hasCanonicalType(
35 anyOf(hasDeclaration(equalsBoundNode(
"class")),
36 referenceType(pointee(hasDeclaration(equalsBoundNode(
"class")))))));
38 cxxMethodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())),
39 hasName(
"operator="), ofClass(recordDecl().bind(
"class")))
41 const auto IsSelfAssign =
42 cxxMethodDecl(IsAssign, firstParameter(parmVarDecl(hasType(IsSelf))))
46 cxxMethodDecl(IsAssign, unless(HasGoodReturnType)).bind(
"ReturnType"),
49 const auto BadSelf = qualType(hasCanonicalType(referenceType(
50 anyOf(lValueReferenceType(pointee(unless(isConstQualified()))),
51 rValueReferenceType(pointee(isConstQualified()))))));
54 cxxMethodDecl(IsSelfAssign, firstParameter(parmVarDecl(hasType(BadSelf))))
55 .bind(
"ArgumentType"),
59 cxxMethodDecl(IsSelfAssign, anyOf(isConst(), isVirtual())).bind(
"cv"),
62 const auto IsBadReturnStatement = returnStmt(unless(has(ignoringParenImpCasts(
63 anyOf(unaryOperator(hasOperatorName(
"*"), hasUnaryOperand(cxxThisExpr())),
64 cxxOperatorCallExpr(argumentCountIs(1),
65 callee(unresolvedLookupExpr()),
66 hasArgument(0, cxxThisExpr())),
68 hasOverloadedOperatorName(
"="),
70 0, unaryOperator(hasOperatorName(
"*"),
71 hasUnaryOperand(cxxThisExpr())))))))));
72 const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType);
74 Finder->addMatcher(returnStmt(IsBadReturnStatement, forFunction(IsGoodAssign))
80 const MatchFinder::MatchResult &Result) {
81 if (
const auto *RetStmt = Result.Nodes.getNodeAs<ReturnStmt>(
"returnStmt")) {
82 diag(RetStmt->getBeginLoc(),
"operator=() should always return '*this'");
84 const auto *Method = Result.Nodes.getNodeAs<CXXMethodDecl>(
"method");
85 if (Result.Nodes.getNodeAs<CXXMethodDecl>(
"ReturnType"))
86 diag(Method->getBeginLoc(),
"operator=() should return '%0&'")
87 << Method->getParent()->getName();
88 if (Result.Nodes.getNodeAs<CXXMethodDecl>(
"ArgumentType"))
89 diag(Method->getBeginLoc(),
90 "operator=() should take '%0 const&'%select{|, '%0&&'}1 or '%0'")
91 << Method->getParent()->getName() <<
getLangOpts().CPlusPlus11;
92 if (Result.Nodes.getNodeAs<CXXMethodDecl>(
"cv"))
93 diag(Method->getBeginLoc(),
94 "operator=() should not be marked '%select{const|virtual}0'")
95 << !Method->isConst();
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER_P(UserDefinedLiteral, hasLiteral, clang::ast_matchers::internal::Matcher< Expr >, InnerMatcher)