39 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "OverloadedUnaryAndCheck.h"10#include "clang/ASTMatchers/ASTMatchFinder.h"11#include "clang/ASTMatchers/ASTMatchers.h"12 13using namespace clang::ast_matchers;14 15namespace clang::tidy::google::runtime {16 17void OverloadedUnaryAndCheck::registerMatchers(18 ast_matchers::MatchFinder *Finder) {19 // Match unary methods that overload operator&.20 Finder->addMatcher(21 cxxMethodDecl(parameterCountIs(0), hasOverloadedOperatorName("&"))22 .bind("overload"),23 this);24 // Also match freestanding unary operator& overloads. Be careful not to match25 // binary methods.26 Finder->addMatcher(functionDecl(unless(cxxMethodDecl()), parameterCountIs(1),27 hasOverloadedOperatorName("&"))28 .bind("overload"),29 this);30}31 32void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) {33 const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload");34 diag(Decl->getBeginLoc(),35 "do not overload unary operator&, it is dangerous.");36}37 38} // namespace clang::tidy::google::runtime39