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 { |
Alexander Kornienko | ed824e0 | 2015-03-05 13:46:14 +0000 | [diff] [blame] | 19 | namespace google { |
Benjamin Kramer | feff134 | 2014-07-15 12:48:14 +0000 | [diff] [blame] | 20 | namespace runtime { |
| 21 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 22 | void OverloadedUnaryAndCheck::registerMatchers( |
| 23 | ast_matchers::MatchFinder *Finder) { |
Aaron Ballman | ec3e5d6 | 2015-09-02 16:20:42 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | feff134 | 2014-07-15 12:48:14 +0000 | [diff] [blame] | 29 | // Match unary methods that overload operator&. |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 30 | Finder->addMatcher( |
| 31 | cxxMethodDecl(parameterCountIs(0), hasOverloadedOperatorName("&")) |
| 32 | .bind("overload"), |
| 33 | this); |
Benjamin Kramer | feff134 | 2014-07-15 12:48:14 +0000 | [diff] [blame] | 34 | // Also match freestanding unary operator& overloads. Be careful not to match |
| 35 | // binary methods. |
| 36 | Finder->addMatcher( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 37 | functionDecl(allOf( |
| 38 | unless(cxxMethodDecl()), |
| 39 | functionDecl(parameterCountIs(1), hasOverloadedOperatorName("&")) |
| 40 | .bind("overload"))), |
Benjamin Kramer | feff134 | 2014-07-15 12:48:14 +0000 | [diff] [blame] | 41 | this); |
| 42 | } |
| 43 | |
| 44 | void 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 Kornienko | ed824e0 | 2015-03-05 13:46:14 +0000 | [diff] [blame] | 51 | } // namespace google |
Benjamin Kramer | feff134 | 2014-07-15 12:48:14 +0000 | [diff] [blame] | 52 | } // namespace tidy |
| 53 | } // namespace clang |