blob: 84abb5fd7909812a968a7b5534705623e395621b [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
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000022void OverloadedUnaryAndCheck::registerMatchers(
23 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&.
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000030 Finder->addMatcher(
31 cxxMethodDecl(parameterCountIs(0), hasOverloadedOperatorName("&"))
32 .bind("overload"),
33 this);
Benjamin Kramerfeff1342014-07-15 12:48:14 +000034 // Also match freestanding unary operator& overloads. Be careful not to match
35 // binary methods.
36 Finder->addMatcher(
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000037 functionDecl(allOf(
38 unless(cxxMethodDecl()),
39 functionDecl(parameterCountIs(1), hasOverloadedOperatorName("&"))
40 .bind("overload"))),
Benjamin Kramerfeff1342014-07-15 12:48:14 +000041 this);
42}
43
44void 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
Alexander Kornienkoed824e02015-03-05 13:46:14 +000051} // namespace google
Benjamin Kramerfeff1342014-07-15 12:48:14 +000052} // namespace tidy
53} // namespace clang