[clang-tools-extra] r228583 - Verify assign operator signatures.
Samuel Benzaquen
sbenza at google.com
Mon Feb 9 09:50:40 PST 2015
Author: sbenza
Date: Mon Feb 9 11:50:40 2015
New Revision: 228583
URL: http://llvm.org/viewvc/llvm-project?rev=228583&view=rev
Log:
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
Added:
clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.cpp
clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.h
clang-tools-extra/trunk/test/clang-tidy/misc-assign-operator-signature.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp
Added: clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.cpp?rev=228583&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.cpp Mon Feb 9 11:50:40 2015
@@ -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
Added: clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.h?rev=228583&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/misc/AssignOperatorSignatureCheck.h Mon Feb 9 11:50:40 2015
@@ -0,0 +1,35 @@
+//===--- AssignOperatorSignatureCheck.h - clang-tidy ------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_ASSIGN_OPERATOR_SIGNATURE_CHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_ASSIGN_OPERATOR_SIGNATURE_CHECK_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+
+/// \brief Finds declarations of assign operators with the wrong return and/or
+/// argument types.
+///
+/// The return type must be \c Class&.
+/// Works with move-assign and assign by value.
+/// Private and deleted operators are ignored.
+class AssignOperatorSignatureCheck : public ClangTidyCheck {
+public:
+ AssignOperatorSignatureCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_ASSIGN_OPERATOR_SIGNATURE_CHECK_H
Modified: clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt?rev=228583&r1=228582&r2=228583&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt Mon Feb 9 11:50:40 2015
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyMiscModule
ArgumentCommentCheck.cpp
+ AssignOperatorSignatureCheck.cpp
BoolPointerImplicitConversion.cpp
InefficientAlgorithmCheck.cpp
MiscTidyModule.cpp
Modified: clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp?rev=228583&r1=228582&r2=228583&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp Mon Feb 9 11:50:40 2015
@@ -11,6 +11,7 @@
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
#include "ArgumentCommentCheck.h"
+#include "AssignOperatorSignatureCheck.h"
#include "BoolPointerImplicitConversion.h"
#include "InefficientAlgorithmCheck.h"
#include "SwappedArgumentsCheck.h"
@@ -26,6 +27,8 @@ class MiscModule : public ClangTidyModul
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<ArgumentCommentCheck>("misc-argument-comment");
+ CheckFactories.registerCheck<AssignOperatorSignatureCheck>(
+ "misc-assign-operator-signature");
CheckFactories.registerCheck<BoolPointerImplicitConversion>(
"misc-bool-pointer-implicit-conversion");
CheckFactories.registerCheck<InefficientAlgorithmCheck>(
Added: clang-tools-extra/trunk/test/clang-tidy/misc-assign-operator-signature.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-assign-operator-signature.cpp?rev=228583&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-assign-operator-signature.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-assign-operator-signature.cpp Mon Feb 9 11:50:40 2015
@@ -0,0 +1,52 @@
+// RUN: $(dirname %s)/check_clang_tidy.sh %s misc-assign-operator-signature %t
+// REQUIRES: shell
+
+struct Good {
+ Good& operator=(const Good&);
+ Good& operator=(Good&&);
+
+ // Assign from other types is fine too.
+ Good& operator=(int);
+};
+
+struct AlsoGood {
+ // By value is also fine.
+ AlsoGood& operator=(AlsoGood);
+};
+
+struct BadReturn {
+ void operator=(const BadReturn&);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'BadReturn&' [misc-assign-operator-signature]
+ const BadReturn& operator=(BadReturn&&);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'Bad
+};
+struct BadReturn2 {
+ BadReturn2&& operator=(const BadReturn2&);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'Bad
+ int operator=(BadReturn2&&);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should return 'Bad
+};
+
+struct BadArgument {
+ BadArgument& operator=(BadArgument&);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should take 'BadArgument const&', 'BadArgument&&' or 'BadArgument'
+ BadArgument& operator=(const BadArgument&&);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should take 'BadAr
+};
+
+struct BadModifier {
+ BadModifier& operator=(const BadModifier&) const;
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: operator=() should not be marked 'const'
+};
+
+struct Deleted {
+ // We don't check the return value of deleted operators.
+ void operator=(const Deleted&) = delete;
+ void operator=(Deleted&&) = delete;
+};
+
+class Private {
+ // We don't check the return value of private operators.
+ // Pre-C++11 way of disabling assignment.
+ void operator=(const Private &);
+};
More information about the cfe-commits
mailing list