Benjamin Kramer | feff134 | 2014-07-15 12:48:14 +0000 | [diff] [blame] | 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 | |
| 10 | #include "OverloadedUnaryAndCheck.h" |
Chandler Carruth | 3cbd71c | 2015-01-14 11:24:38 +0000 | [diff] [blame^] | 11 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | feff134 | 2014-07-15 12:48:14 +0000 | [diff] [blame] | 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | #include "clang/ASTMatchers/ASTMatchers.h" |
Benjamin Kramer | feff134 | 2014-07-15 12:48:14 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
| 19 | namespace runtime { |
| 20 | |
| 21 | void |
| 22 | OverloadedUnaryAndCheck::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 | |
| 37 | void 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 |