diff options
author | Samuel Benzaquen <sbenza@google.com> | 2015-02-09 17:50:40 +0000 |
---|---|---|
committer | Samuel Benzaquen <sbenza@google.com> | 2015-02-09 17:50:40 +0000 |
commit | b5cbe0100f4e29f67999618622f3786ff98cd533 (patch) | |
tree | a9b840fa4468d56657f79d25eff304b44fd37d38 /clang-tools-extra/clang-tidy/misc/AssignOperatorSignatureCheck.cpp | |
parent | 3f308ab127e068f6f4f43afa8f09d971b7bc6d83 (diff) | |
download | bcm5719-llvm-b5cbe0100f4e29f67999618622f3786ff98cd533.tar.gz bcm5719-llvm-b5cbe0100f4e29f67999618622f3786ff98cd533.zip |
Verify assign operator signatures.
Summary: Warn when the return type of assign operators is not Class&.
Reviewers: alexfh
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D6667
llvm-svn: 228583
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc/AssignOperatorSignatureCheck.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/misc/AssignOperatorSignatureCheck.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/AssignOperatorSignatureCheck.cpp b/clang-tools-extra/clang-tidy/misc/AssignOperatorSignatureCheck.cpp new file mode 100644 index 00000000000..26255401a65 --- /dev/null +++ b/clang-tools-extra/clang-tidy/misc/AssignOperatorSignatureCheck.cpp @@ -0,0 +1,67 @@ +//===--- AssignOperatorSignatureCheck.cpp - clang-tidy ----------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "AssignOperatorSignatureCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { + +void AssignOperatorSignatureCheck::registerMatchers( + ast_matchers::MatchFinder *Finder) { + const auto HasGoodReturnType = methodDecl(returns(lValueReferenceType(pointee( + unless(isConstQualified()), hasDeclaration(equalsBoundNode("class")))))); + + const auto IsSelf = qualType( + anyOf(hasDeclaration(equalsBoundNode("class")), + referenceType(pointee(hasDeclaration(equalsBoundNode("class")))))); + const auto IsSelfAssign = + methodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())), + hasName("operator="), ofClass(recordDecl().bind("class")), + hasParameter(0, parmVarDecl(hasType(IsSelf)))).bind("method"); + + Finder->addMatcher( + methodDecl(IsSelfAssign, unless(HasGoodReturnType)).bind("ReturnType"), + this); + + const auto BadSelf = referenceType( + anyOf(lValueReferenceType(pointee(unless(isConstQualified()))), + rValueReferenceType(pointee(isConstQualified())))); + + Finder->addMatcher( + methodDecl(IsSelfAssign, hasParameter(0, parmVarDecl(hasType(BadSelf)))) + .bind("ArgumentType"), + this); + + Finder->addMatcher(methodDecl(IsSelfAssign, isConst()).bind("Const"), this); +} + + +void AssignOperatorSignatureCheck::check( + const MatchFinder::MatchResult &Result) { + const auto* Method = Result.Nodes.getNodeAs<CXXMethodDecl>("method"); + std::string Name = Method->getParent()->getName(); + + static const char *Messages[][2] = { + {"ReturnType", "operator=() should return '%0&'"}, + {"ArgumentType", "operator=() should take '%0 const&', '%0&&' or '%0'"}, + {"Const", "operator=() should not be marked 'const'"}, + }; + + for (const auto& Message : Messages) { + if (Result.Nodes.getNodeAs<Decl>(Message[0])) + diag(Method->getLocStart(), Message[1]) << Name; + } +} + +} // namespace tidy +} // namespace clang |