1 //===--- OverloadedUnaryAndCheck.cpp - clang-tidy ---------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "OverloadedUnaryAndCheck.h" 10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 #include "clang/ASTMatchers/ASTMatchers.h" 13 14 using namespace clang::ast_matchers; 15 16 namespace clang { 17 namespace tidy { 18 namespace google { 19 namespace runtime { 20 registerMatchers(ast_matchers::MatchFinder * Finder)21void OverloadedUnaryAndCheck::registerMatchers( 22 ast_matchers::MatchFinder *Finder) { 23 // Match unary methods that overload operator&. 24 Finder->addMatcher( 25 cxxMethodDecl(parameterCountIs(0), hasOverloadedOperatorName("&")) 26 .bind("overload"), 27 this); 28 // Also match freestanding unary operator& overloads. Be careful not to match 29 // binary methods. 30 Finder->addMatcher(functionDecl(unless(cxxMethodDecl()), parameterCountIs(1), 31 hasOverloadedOperatorName("&")) 32 .bind("overload"), 33 this); 34 } 35 check(const MatchFinder::MatchResult & Result)36void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) { 37 const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload"); 38 diag(Decl->getBeginLoc(), 39 "do not overload unary operator&, it is dangerous."); 40 } 41 42 } // namespace runtime 43 } // namespace google 44 } // namespace tidy 45 } // namespace clang 46