blob: 5dad36f60e021aceaa193c894f44a3ef5486d266 [file] [log] [blame]
Benjamin Kramerfeff1342014-07-15 12:48:14 +00001//===--- 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
10#include "OverloadedUnaryAndCheck.h"
Chandler Carruth3cbd71c2015-01-14 11:24:38 +000011#include "clang/AST/ASTContext.h"
Benjamin Kramerfeff1342014-07-15 12:48:14 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
Benjamin Kramerfeff1342014-07-15 12:48:14 +000014
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
Alexander Kornienkoed824e02015-03-05 13:46:14 +000019namespace google {
Benjamin Kramerfeff1342014-07-15 12:48:14 +000020namespace runtime {
21
22void
23OverloadedUnaryAndCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
Aaron Ballmanec3e5d62015-09-02 16:20:42 +000024 // 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
Benjamin Kramerfeff1342014-07-15 12:48:14 +000029 // Match unary methods that overload operator&.
30 Finder->addMatcher(methodDecl(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(
36 functionDecl(
37 allOf(unless(methodDecl()),
38 functionDecl(parameterCountIs(1),
39 hasOverloadedOperatorName("&")).bind("overload"))),
40 this);
41}
42
43void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) {
44 const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload");
45 diag(Decl->getLocStart(),
46 "do not overload unary operator&, it is dangerous.");
47}
48
49} // namespace runtime
Alexander Kornienkoed824e02015-03-05 13:46:14 +000050} // namespace google
Benjamin Kramerfeff1342014-07-15 12:48:14 +000051} // namespace tidy
52} // namespace clang