[clang-tidy] Add a checker that flags all instances of overloaded unary operator&
authorBenjamin Kramer <benny.kra@googlemail.com>
Tue, 15 Jul 2014 12:48:14 +0000 (12:48 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Tue, 15 Jul 2014 12:48:14 +0000 (12:48 +0000)
This handles both methods and freestanding overloads.

Differential Revision: http://reviews.llvm.org/D4498

llvm-svn: 213067

clang-tools-extra/clang-tidy/google/CMakeLists.txt
clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp
clang-tools-extra/clang-tidy/google/OverloadedUnaryAndCheck.cpp [new file with mode: 0644]
clang-tools-extra/clang-tidy/google/OverloadedUnaryAndCheck.h [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/google-overloaded-unary-and.cpp [new file with mode: 0644]

index 9299fad..cb9dd2d 100644 (file)
@@ -5,6 +5,7 @@ add_clang_library(clangTidyGoogleModule
   ExplicitConstructorCheck.cpp
   ExplicitMakePairCheck.cpp
   GoogleTidyModule.cpp
+  OverloadedUnaryAndCheck.cpp
 
   LINK_LIBS
   clangAST
index 248d0f1..44deb95 100644 (file)
@@ -13,6 +13,7 @@
 #include "AvoidCStyleCastsCheck.h"
 #include "ExplicitConstructorCheck.h"
 #include "ExplicitMakePairCheck.h"
+#include "OverloadedUnaryAndCheck.h"
 
 using namespace clang::ast_matchers;
 
@@ -29,6 +30,9 @@ public:
         "google-explicit-constructor",
         new ClangTidyCheckFactory<ExplicitConstructorCheck>());
     CheckFactories.addCheckFactory(
+        "google-runtime-operator",
+        new ClangTidyCheckFactory<runtime::OverloadedUnaryAndCheck>());
+    CheckFactories.addCheckFactory(
         "google-readability-casting",
         new ClangTidyCheckFactory<readability::AvoidCStyleCastsCheck>());
   }
diff --git a/clang-tools-extra/clang-tidy/google/OverloadedUnaryAndCheck.cpp b/clang-tools-extra/clang-tidy/google/OverloadedUnaryAndCheck.cpp
new file mode 100644 (file)
index 0000000..15820b8
--- /dev/null
@@ -0,0 +1,45 @@
+//===--- OverloadedUnaryAndCheck.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 "OverloadedUnaryAndCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/AST/ASTContext.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace runtime {
+
+void
+OverloadedUnaryAndCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
+  // Match unary methods that overload operator&.
+  Finder->addMatcher(methodDecl(parameterCountIs(0), hasOverloadedOperatorName(
+                                                         "&")).bind("overload"),
+                     this);
+  // Also match freestanding unary operator& overloads. Be careful not to match
+  // binary methods.
+  Finder->addMatcher(
+      functionDecl(
+          allOf(unless(methodDecl()),
+                functionDecl(parameterCountIs(1),
+                             hasOverloadedOperatorName("&")).bind("overload"))),
+      this);
+}
+
+void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload");
+  diag(Decl->getLocStart(),
+       "do not overload unary operator&, it is dangerous.");
+}
+
+} // namespace runtime
+} // namespace tidy
+} // namespace clang
diff --git a/clang-tools-extra/clang-tidy/google/OverloadedUnaryAndCheck.h b/clang-tools-extra/clang-tidy/google/OverloadedUnaryAndCheck.h
new file mode 100644 (file)
index 0000000..c3fbb1d
--- /dev/null
@@ -0,0 +1,32 @@
+//===--- OverloadedUnaryAndCheck.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_GOOGLE_OVERLOADED_UNARY_AND_CHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OVERLOADED_UNARY_AND_CHECK_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace runtime {
+
+/// \brief Finds overloads of unary operator &.
+///
+/// Corresponding cpplint.py check name: 'runtime/operator'.
+class OverloadedUnaryAndCheck : public ClangTidyCheck {
+public:
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace runtime
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_OVERLOADED_UNARY_AND_CHECK_H
diff --git a/clang-tools-extra/test/clang-tidy/google-overloaded-unary-and.cpp b/clang-tools-extra/test/clang-tidy/google-overloaded-unary-and.cpp
new file mode 100644 (file)
index 0000000..ce3d55c
--- /dev/null
@@ -0,0 +1,24 @@
+// RUN: clang-tidy %s -checks='-*,google-runtime-operator' -- | FileCheck %s -implicit-check-not="{{warning|error}}:"
+
+struct Foo {
+  void *operator&();
+// CHECK: :[[@LINE-1]]:3: warning: do not overload unary operator&, it is dangerous.
+};
+
+template <typename T>
+struct TFoo {
+  T *operator&();
+// CHECK: :[[@LINE-1]]:3: warning: do not overload unary operator&, it is dangerous.
+};
+
+TFoo<int> tfoo;
+
+struct Bar;
+void *operator&(Bar &b);
+// CHECK: :[[@LINE-1]]:1: warning: do not overload unary operator&, it is dangerous.
+
+struct Qux {
+  void *operator&(Qux &q); // no-warning
+};
+
+void *operator&(Qux &q, Qux &r); // no-warning