OverloadedUnaryAndCheck.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //===--- OverloadedUnaryAndCheck.cpp - clang-tidy ---------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "OverloadedUnaryAndCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. #include "clang/ASTMatchers/ASTMatchers.h"
  12. using namespace clang::ast_matchers;
  13. namespace clang::tidy::google::runtime {
  14. void OverloadedUnaryAndCheck::registerMatchers(
  15. ast_matchers::MatchFinder *Finder) {
  16. // Match unary methods that overload operator&.
  17. Finder->addMatcher(
  18. cxxMethodDecl(parameterCountIs(0), hasOverloadedOperatorName("&"))
  19. .bind("overload"),
  20. this);
  21. // Also match freestanding unary operator& overloads. Be careful not to match
  22. // binary methods.
  23. Finder->addMatcher(functionDecl(unless(cxxMethodDecl()), parameterCountIs(1),
  24. hasOverloadedOperatorName("&"))
  25. .bind("overload"),
  26. this);
  27. }
  28. void OverloadedUnaryAndCheck::check(const MatchFinder::MatchResult &Result) {
  29. const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("overload");
  30. diag(Decl->getBeginLoc(),
  31. "do not overload unary operator&, it is dangerous.");
  32. }
  33. } // namespace clang::tidy::google::runtime