clang-tools 10.0.0

OverloadedUnaryAndCheck.cpp
Go to the documentation of this file.
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
21 void OverloadedUnaryAndCheck::registerMatchers(
22 ast_matchers::MatchFinder *Finder) {
23 // Only register the matchers for C++; the functionality currently does not
24 // provide any benefit to other languages, despite being benign.
25 if (!getLangOpts().CPlusPlus)
26 return;
27
28 // Match unary methods that overload operator&.
29 Finder->addMatcher(
30 cxxMethodDecl(parameterCountIs(0), hasOverloadedOperatorName("&"))
31 .bind("overload"),
32 this);
33 // Also match freestanding unary operator& overloads. Be careful not to match
34 // binary methods.
35 Finder->addMatcher(functionDecl(unless(cxxMethodDecl()), parameterCountIs(1),
36 hasOverloadedOperatorName("&"))
37 .bind("overload"),
38 this);
39 }
40
41 void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) {
42 const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload");
43 diag(Decl->getBeginLoc(),
44 "do not overload unary operator&, it is dangerous.");
45 }
46
47 } // namespace runtime
48 } // namespace google
49 } // namespace tidy
50 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:93
OverloadedUnaryAndCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27