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