blob: e5027306d91723ef6a2a37f335e386179b5d9a31 [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 {
19namespace runtime {
20
21void
22OverloadedUnaryAndCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
23 // Match unary methods that overload operator&.
24 Finder->addMatcher(methodDecl(parameterCountIs(0), hasOverloadedOperatorName(
25 "&")).bind("overload"),
26 this);
27 // Also match freestanding unary operator& overloads. Be careful not to match
28 // binary methods.
29 Finder->addMatcher(
30 functionDecl(
31 allOf(unless(methodDecl()),
32 functionDecl(parameterCountIs(1),
33 hasOverloadedOperatorName("&")).bind("overload"))),
34 this);
35}
36
37void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) {
38 const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload");
39 diag(Decl->getLocStart(),
40 "do not overload unary operator&, it is dangerous.");
41}
42
43} // namespace runtime
44} // namespace tidy
45} // namespace clang