10 #include "clang/ASTMatchers/ASTMatchFinder.h" 11 #include "clang/ASTMatchers/ASTMatchers.h" 19 void UnconventionalAssignOperatorCheck::registerMatchers(
20 ast_matchers::MatchFinder *Finder) {
23 if (!getLangOpts().CPlusPlus)
26 const auto HasGoodReturnType = cxxMethodDecl(returns(lValueReferenceType(
27 pointee(unless(isConstQualified()),
28 anyOf(autoType(), hasDeclaration(equalsBoundNode(
"class")))))));
30 const auto IsSelf = qualType(
31 anyOf(hasDeclaration(equalsBoundNode(
"class")),
32 referenceType(pointee(hasDeclaration(equalsBoundNode(
"class"))))));
34 cxxMethodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())),
35 hasName(
"operator="), ofClass(recordDecl().bind(
"class")))
37 const auto IsSelfAssign =
38 cxxMethodDecl(IsAssign, hasParameter(0, parmVarDecl(hasType(IsSelf))))
42 cxxMethodDecl(IsAssign, unless(HasGoodReturnType)).bind(
"ReturnType"),
45 const auto BadSelf = referenceType(
46 anyOf(lValueReferenceType(pointee(unless(isConstQualified()))),
47 rValueReferenceType(pointee(isConstQualified()))));
50 cxxMethodDecl(IsSelfAssign,
51 hasParameter(0, parmVarDecl(hasType(BadSelf))))
52 .bind(
"ArgumentType"),
56 cxxMethodDecl(IsSelfAssign, anyOf(isConst(), isVirtual())).bind(
"cv"),
59 const auto IsBadReturnStatement = returnStmt(unless(has(ignoringParenImpCasts(
60 anyOf(unaryOperator(hasOperatorName(
"*"), hasUnaryOperand(cxxThisExpr())),
61 cxxOperatorCallExpr(argumentCountIs(1),
62 callee(unresolvedLookupExpr()),
63 hasArgument(0, cxxThisExpr())))))));
64 const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType);
66 Finder->addMatcher(returnStmt(IsBadReturnStatement, forFunction(IsGoodAssign))
71 void UnconventionalAssignOperatorCheck::check(
72 const MatchFinder::MatchResult &
Result) {
73 if (
const auto *RetStmt = Result.Nodes.getNodeAs<ReturnStmt>(
"returnStmt")) {
74 diag(RetStmt->getBeginLoc(),
"operator=() should always return '*this'");
76 static const char *
const Messages[][2] = {
77 {
"ReturnType",
"operator=() should return '%0&'"},
78 {
"ArgumentType",
"operator=() should take '%0 const&', '%0&&' or '%0'"},
79 {
"cv",
"operator=() should not be marked '%1'"}};
81 const auto *Method = Result.Nodes.getNodeAs<CXXMethodDecl>(
"method");
82 for (
const auto &
Message : Messages) {
83 if (Result.Nodes.getNodeAs<Decl>(
Message[0]))
84 diag(Method->getBeginLoc(),
Message[1])
85 << Method->getParent()->getName()
86 << (Method->isConst() ?
"const" :
"virtual");
constexpr llvm::StringLiteral Message
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result