[clang-tools-extra] r213067 - [clang-tidy] Add a checker that flags all instances of overloaded unary operator&

Benjamin Kramer benny.kra at googlemail.com
Tue Jul 15 05:48:15 PDT 2014


Author: d0k
Date: Tue Jul 15 07:48:14 2014
New Revision: 213067

URL: http://llvm.org/viewvc/llvm-project?rev=213067&view=rev
Log:
[clang-tidy] Add a checker that flags all instances of overloaded unary operator&

This handles both methods and freestanding overloads.

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

Added:
    clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.cpp
    clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.h
    clang-tools-extra/trunk/test/clang-tidy/google-overloaded-unary-and.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt
    clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp

Modified: clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt?rev=213067&r1=213066&r2=213067&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt Tue Jul 15 07:48:14 2014
@@ -5,6 +5,7 @@ add_clang_library(clangTidyGoogleModule
   ExplicitConstructorCheck.cpp
   ExplicitMakePairCheck.cpp
   GoogleTidyModule.cpp
+  OverloadedUnaryAndCheck.cpp
 
   LINK_LIBS
   clangAST

Modified: clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp?rev=213067&r1=213066&r2=213067&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp Tue Jul 15 07:48:14 2014
@@ -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>());
   }

Added: clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.cpp?rev=213067&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.cpp Tue Jul 15 07:48:14 2014
@@ -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

Added: clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.h?rev=213067&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.h Tue Jul 15 07:48:14 2014
@@ -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

Added: clang-tools-extra/trunk/test/clang-tidy/google-overloaded-unary-and.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-overloaded-unary-and.cpp?rev=213067&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-overloaded-unary-and.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/google-overloaded-unary-and.cpp Tue Jul 15 07:48:14 2014
@@ -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





More information about the cfe-commits mailing list