Index: clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt =================================================================== --- clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt +++ clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt @@ -5,6 +5,7 @@ ExplicitConstructorCheck.cpp ExplicitMakePairCheck.cpp GoogleTidyModule.cpp + OverloadedUnaryAndCheck.cpp LINK_LIBS clangAST Index: clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp +++ clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp @@ -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 @@ "google-explicit-constructor", new ClangTidyCheckFactory()); CheckFactories.addCheckFactory( + "google-runtime-operator", + new ClangTidyCheckFactory()); + CheckFactories.addCheckFactory( "google-readability-casting", new ClangTidyCheckFactory()); } Index: clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.h =================================================================== --- clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.h +++ clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.h @@ -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 Index: clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.cpp +++ clang-tools-extra/trunk/clang-tidy/google/OverloadedUnaryAndCheck.cpp @@ -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("overload"); + diag(Decl->getLocStart(), + "do not overload unary operator&, it is dangerous."); +} + +} // namespace runtime +} // namespace tidy +} // namespace clang Index: clang-tools-extra/trunk/test/clang-tidy/google-overloaded-unary-and.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/google-overloaded-unary-and.cpp +++ clang-tools-extra/trunk/test/clang-tidy/google-overloaded-unary-and.cpp @@ -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 +struct TFoo { + T *operator&(); +// CHECK: :[[@LINE-1]]:3: warning: do not overload unary operator&, it is dangerous. +}; + +TFoo 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