clang-tools  3.9.0
OverloadedUnaryAndCheck.cpp
Go to the documentation of this file.
1 //===--- OverloadedUnaryAndCheck.cpp - clang-tidy ---------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace google {
20 namespace runtime {
21 
22 void
23 OverloadedUnaryAndCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
24  // Only register the matchers for C++; the functionality currently does not
25  // provide any benefit to other languages, despite being benign.
26  if (!getLangOpts().CPlusPlus)
27  return;
28 
29  // Match unary methods that overload operator&.
30  Finder->addMatcher(
31  cxxMethodDecl(parameterCountIs(0), hasOverloadedOperatorName("&"))
32  .bind("overload"),
33  this);
34  // Also match freestanding unary operator& overloads. Be careful not to match
35  // binary methods.
36  Finder->addMatcher(
37  functionDecl(
38  allOf(unless(cxxMethodDecl()),
39  functionDecl(parameterCountIs(1),
40  hasOverloadedOperatorName("&")).bind("overload"))),
41  this);
42 }
43 
44 void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) {
45  const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload");
46  diag(Decl->getLocStart(),
47  "do not overload unary operator&, it is dangerous.");
48 }
49 
50 } // namespace runtime
51 } // namespace google
52 } // namespace tidy
53 } // namespace clang
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:210
const NamedDecl * Result
Definition: USRFinder.cpp:137