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