CloexecCreatCheck.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //===--- CloexecCreatCheck.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 "CloexecCreatCheck.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 CloexecCreatCheck::registerMatchers(MatchFinder *Finder) {
  14. auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));
  15. auto MODETType = hasType(namedDecl(hasName("mode_t")));
  16. registerMatchersImpl(Finder,
  17. functionDecl(isExternC(), returns(isInteger()),
  18. hasName("creat"),
  19. hasParameter(0, CharPointerType),
  20. hasParameter(1, MODETType)));
  21. }
  22. void CloexecCreatCheck::check(const MatchFinder::MatchResult &Result) {
  23. const std::string &ReplacementText =
  24. (Twine("open (") + getSpellingArg(Result, 0) +
  25. ", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, " +
  26. getSpellingArg(Result, 1) + ")")
  27. .str();
  28. replaceFunc(Result,
  29. "prefer open() to creat() because open() allows O_CLOEXEC",
  30. ReplacementText);
  31. }
  32. } // namespace clang::tidy::android