[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
llvm-svn: 213067
diff --git a/clang-tools-extra/clang-tidy/google/OverloadedUnaryAndCheck.cpp b/clang-tools-extra/clang-tidy/google/OverloadedUnaryAndCheck.cpp
new file mode 100644
index 0000000..15820b8
--- /dev/null
+++ b/clang-tools-extra/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<FunctionDecl>("overload");
+ diag(Decl->getLocStart(),
+ "do not overload unary operator&, it is dangerous.");
+}
+
+} // namespace runtime
+} // namespace tidy
+} // namespace clang