clang-tools  8.0.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 OverloadedUnaryAndCheck::registerMatchers(
23  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(functionDecl(unless(cxxMethodDecl()), parameterCountIs(1),
37  hasOverloadedOperatorName("&"))
38  .bind("overload"),
39  this);
40 }
41 
42 void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) {
43  const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload");
44  diag(Decl->getBeginLoc(),
45  "do not overload unary operator&, it is dangerous.");
46 }
47 
48 } // namespace runtime
49 } // namespace google
50 } // namespace tidy
51 } // namespace clang
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//