juliehockett created this revision.
juliehockett added reviewers: aaron.ballman, alexfh, hokein.
juliehockett added a project: clang-tools-extra.
Herald added subscribers: xazax.hun, mgorny.

Adds a check to the Fuchsia module to warn if an operator is overloaded, except 
move and copy operators.

See https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md for reference.


https://reviews.llvm.org/D41363

Files:
  clang-tidy/fuchsia/CMakeLists.txt
  clang-tidy/fuchsia/FuchsiaTidyModule.cpp
  clang-tidy/fuchsia/OverloadedOperatorCheck.cpp
  clang-tidy/fuchsia/OverloadedOperatorCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/fuchsia-overloaded-operator.rst
  docs/clang-tidy/checks/list.rst
  test/clang-tidy/fuchsia-overloaded-operator.cpp

Index: test/clang-tidy/fuchsia-overloaded-operator.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/fuchsia-overloaded-operator.cpp
@@ -0,0 +1,15 @@
+// RUN: %check_clang_tidy %s fuchsia-overloaded-operator %t
+
+class A {
+public:
+  int operator+(int);
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: operator overloading is disallowed [fuchsia-overloaded-operator]
+};
+
+class B {
+public:
+  B &operator=(B other);
+  // CHECK-MESSAGES-NOT: [[@LINE-1]]:3: warning: operator overloading is disallowed [fuchsia-overloaded-operator]
+  B &operator=(B &&other);
+  // CHECK-MESSAGES-NOT: [[@LINE-1]]:3: warning: operator overloading is disallowed [fuchsia-overloaded-operator]
+};
Index: docs/clang-tidy/checks/list.rst
===================================================================
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -69,6 +69,7 @@
    cppcoreguidelines-slicing
    cppcoreguidelines-special-member-functions
    fuchsia-default-arguments
+   fuchsia-overloaded-operator
    fuchsia-virtual-inheritance
    google-build-explicit-make-pair
    google-build-namespaces
Index: docs/clang-tidy/checks/fuchsia-overloaded-operator.rst
===================================================================
--- /dev/null
+++ docs/clang-tidy/checks/fuchsia-overloaded-operator.rst
@@ -0,0 +1,17 @@
+.. title:: clang-tidy - fuchsia-overloaded-operator
+
+fuchsia-overloaded-operator
+===========================
+
+Warns if an operator is overloaded, except for the copy and move operators.
+
+For example:
+
+.. code-block:: c++
+
+  int operator+(int);     // Warning
+
+  B &operator=(B other);  // No warning
+  B &operator=(B &&other) // No warning
+
+See the features disallowed in Fuchsia at https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md
Index: docs/ReleaseNotes.rst
===================================================================
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -134,7 +134,12 @@
   <http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-default-arguments.html>`_ check
 
   Warns if a function or method is declared or called with default arguments.
-  
+
+- New `fuchsia-overloaded-operator
+  <http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-overloaded-operator.html>`_ check
+
+  Warns if an operator is overloaded, except for the copy and move operators.
+
 - New `fuchsia-virtual-inheritance
   <http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-virtual-inheritance.html>`_ check
 
Index: clang-tidy/fuchsia/OverloadedOperatorCheck.h
===================================================================
--- /dev/null
+++ clang-tidy/fuchsia/OverloadedOperatorCheck.h
@@ -0,0 +1,35 @@
+//===--- OverloadedOperatorCheck.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_FUCHSIA_OVERLOADED_OPERATOR_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_OVERLOADED_OPERATOR_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace fuchsia {
+
+/// Operator overloading is disallowed.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-overloaded-operator.html
+class OverloadedOperatorCheck : public ClangTidyCheck {
+public:
+  OverloadedOperatorCheck(StringRef Name, ClangTidyContext *Context)
+      : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace fuchsia
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_OVERLOADED_OPERATOR_H
Index: clang-tidy/fuchsia/OverloadedOperatorCheck.cpp
===================================================================
--- /dev/null
+++ clang-tidy/fuchsia/OverloadedOperatorCheck.cpp
@@ -0,0 +1,35 @@
+//===--- OverloadedOperatorCheck.cpp - clang-tidy--------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "OverloadedOperatorCheck.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace fuchsia {
+
+AST_MATCHER(CXXMethodDecl, hasOverloadedOperator) {
+  if (Node.isCopyAssignmentOperator() || Node.isMoveAssignmentOperator())
+    return false;
+  return Node.isOverloadedOperator();
+}
+
+void OverloadedOperatorCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(cxxMethodDecl(hasOverloadedOperator()).bind("decl"), this);
+}
+
+void OverloadedOperatorCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *D = Result.Nodes.getNodeAs<CXXMethodDecl>("decl"))
+    diag(D->getLocStart(), "operator overloading is disallowed");
+}
+
+} // namespace fuchsia
+} // namespace tidy
+} // namespace clang
Index: clang-tidy/fuchsia/FuchsiaTidyModule.cpp
===================================================================
--- clang-tidy/fuchsia/FuchsiaTidyModule.cpp
+++ clang-tidy/fuchsia/FuchsiaTidyModule.cpp
@@ -11,6 +11,7 @@
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
 #include "DefaultArgumentsCheck.h"
+#include "OverloadedOperatorCheck.h"
 #include "VirtualInheritanceCheck.h"
 
 using namespace clang::ast_matchers;
@@ -25,6 +26,8 @@
   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
     CheckFactories.registerCheck<DefaultArgumentsCheck>(
         "fuchsia-default-arguments");
+    CheckFactories.registerCheck<OverloadedOperatorCheck>(
+        "fuchsia-overloaded-operator");
     CheckFactories.registerCheck<VirtualInheritanceCheck>(
         "fuchsia-virtual-inheritance");
   }
Index: clang-tidy/fuchsia/CMakeLists.txt
===================================================================
--- clang-tidy/fuchsia/CMakeLists.txt
+++ clang-tidy/fuchsia/CMakeLists.txt
@@ -3,6 +3,7 @@
 add_clang_library(clangTidyFuchsiaModule
   DefaultArgumentsCheck.cpp
   FuchsiaTidyModule.cpp
+  OverloadedOperatorCheck.cpp
   VirtualInheritanceCheck.cpp
 
   LINK_LIBS
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to