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