CloexecDupCheck.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. //===--- CloexecDupCheck.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 "CloexecDupCheck.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 CloexecDupCheck::registerMatchers(MatchFinder *Finder) {
  14. registerMatchersImpl(Finder,
  15. functionDecl(returns(isInteger()), hasName("dup"),
  16. hasParameter(0, hasType(isInteger()))));
  17. }
  18. void CloexecDupCheck::check(const MatchFinder::MatchResult &Result) {
  19. std::string ReplacementText =
  20. (Twine("fcntl(") + getSpellingArg(Result, 0) + ", F_DUPFD_CLOEXEC)")
  21. .str();
  22. replaceFunc(Result,
  23. "prefer fcntl() to dup() because fcntl() allows F_DUPFD_CLOEXEC",
  24. ReplacementText);
  25. }
  26. } // namespace clang::tidy::android