OverloadedOperatorCheck.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //===--- OverloadedOperatorCheck.cpp - clang-tidy--------------------------===//
  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 "OverloadedOperatorCheck.h"
  9. using namespace clang::ast_matchers;
  10. namespace clang::tidy::fuchsia {
  11. namespace {
  12. AST_MATCHER(FunctionDecl, isFuchsiaOverloadedOperator) {
  13. if (const auto *CXXMethodNode = dyn_cast<CXXMethodDecl>(&Node)) {
  14. if (CXXMethodNode->isCopyAssignmentOperator() ||
  15. CXXMethodNode->isMoveAssignmentOperator())
  16. return false;
  17. if (CXXMethodNode->getParent()->isLambda())
  18. return false;
  19. }
  20. return Node.isOverloadedOperator();
  21. }
  22. } // namespace
  23. void OverloadedOperatorCheck::registerMatchers(MatchFinder *Finder) {
  24. Finder->addMatcher(functionDecl(isFuchsiaOverloadedOperator()).bind("decl"),
  25. this);
  26. }
  27. void OverloadedOperatorCheck::check(const MatchFinder::MatchResult &Result) {
  28. const auto *D = Result.Nodes.getNodeAs<FunctionDecl>("decl");
  29. assert(D && "No FunctionDecl captured!");
  30. SourceLocation Loc = D->getBeginLoc();
  31. if (Loc.isValid())
  32. diag(Loc, "overloading %0 is disallowed") << D;
  33. }
  34. } // namespace clang::tidy::fuchsia