CloexecAcceptCheck.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //===--- CloexecAcceptCheck.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 "CloexecAcceptCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang::ast_matchers;
  12. namespace clang::tidy::android {
  13. void CloexecAcceptCheck::registerMatchers(MatchFinder *Finder) {
  14. auto SockAddrPointerType =
  15. hasType(pointsTo(recordDecl(isStruct(), hasName("sockaddr"))));
  16. auto SockLenPointerType = hasType(pointsTo(namedDecl(hasName("socklen_t"))));
  17. registerMatchersImpl(Finder,
  18. functionDecl(returns(isInteger()), hasName("accept"),
  19. hasParameter(0, hasType(isInteger())),
  20. hasParameter(1, SockAddrPointerType),
  21. hasParameter(2, SockLenPointerType)));
  22. }
  23. void CloexecAcceptCheck::check(const MatchFinder::MatchResult &Result) {
  24. std::string ReplacementText =
  25. (Twine("accept4(") + getSpellingArg(Result, 0) + ", " +
  26. getSpellingArg(Result, 1) + ", " + getSpellingArg(Result, 2) +
  27. ", SOCK_CLOEXEC)")
  28. .str();
  29. replaceFunc(
  30. Result,
  31. "prefer accept4() to accept() because accept4() allows SOCK_CLOEXEC",
  32. ReplacementText);
  33. }
  34. } // namespace clang::tidy::android