Hi alexfh, djasper,
This handles both methods and freestanding overloads.
http://reviews.llvm.org/D4498
Files:
clang-tidy/google/GoogleTidyModule.cpp
clang-tidy/google/OverloadedUnaryAndCheck.cpp
clang-tidy/google/OverloadedUnaryAndCheck.h
test/clang-tidy/google-overloaded-unary-and.cpp
Index: clang-tidy/google/GoogleTidyModule.cpp
===================================================================
--- clang-tidy/google/GoogleTidyModule.cpp
+++ clang-tidy/google/GoogleTidyModule.cpp
@@ -12,6 +12,7 @@
#include "../ClangTidyModuleRegistry.h"
#include "AvoidCStyleCastsCheck.h"
#include "ExplicitConstructorCheck.h"
+#include "OverloadedUnaryAndCheck.h"
using namespace clang::ast_matchers;
@@ -25,6 +26,9 @@
"google-explicit-constructor",
new ClangTidyCheckFactory<ExplicitConstructorCheck>());
CheckFactories.addCheckFactory(
+ "google-runtime-operator",
+ new ClangTidyCheckFactory<runtime::OverloadedUnaryAndCheck>());
+ CheckFactories.addCheckFactory(
"google-readability-casting",
new ClangTidyCheckFactory<readability::AvoidCStyleCastsCheck>());
}
Index: clang-tidy/google/OverloadedUnaryAndCheck.cpp
===================================================================
--- /dev/null
+++ clang-tidy/google/OverloadedUnaryAndCheck.cpp
@@ -0,0 +1,43 @@
+//===--- 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(
+ decl(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(), "unary operator& is dangerous, do not use it.");
+}
+
+} // namespace runtime
+} // namespace tidy
+} // namespace clang
Index: clang-tidy/google/OverloadedUnaryAndCheck.h
===================================================================
--- /dev/null
+++ clang-tidy/google/OverloadedUnaryAndCheck.h
@@ -0,0 +1,32 @@
+//===--- MemberStringReferenceCheckCheck.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
Index: test/clang-tidy/google-overloaded-unary-and.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/google-overloaded-unary-and.cpp
@@ -0,0 +1,16 @@
+// RUN: clang-tidy %s -checks='-*,google-runtime-operator' -- | FileCheck %s -implicit-check-not="{{warning|error}}:"
+
+struct Foo {
+ void *operator&();
+// CHECK: :[[@LINE-1]]:3: warning: unary operator& is dangerous, do not use it.
+};
+
+struct Bar;
+void *operator&(Bar &b);
+// CHECK: :[[@LINE-1]]:1: warning: unary operator& is dangerous, do not use it.
+
+struct Qux {
+ void *operator&(Qux &q); // no-warning
+};
+
+void *operator&(Qux &q, Qux &r); // no-warning
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits