CloexecPipeCheck.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. //===--- CloexecPipeCheck.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 "CloexecPipeCheck.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 CloexecPipeCheck::registerMatchers(MatchFinder *Finder) {
  14. registerMatchersImpl(Finder,
  15. functionDecl(returns(isInteger()), hasName("pipe"),
  16. hasParameter(0, hasType(pointsTo(isInteger())))));
  17. }
  18. void CloexecPipeCheck::check(const MatchFinder::MatchResult &Result) {
  19. std::string ReplacementText =
  20. (Twine("pipe2(") + getSpellingArg(Result, 0) + ", O_CLOEXEC)").str();
  21. replaceFunc(
  22. Result,
  23. "prefer pipe2() with O_CLOEXEC to avoid leaking file descriptors to child processes",
  24. ReplacementText);
  25. }
  26. } // namespace clang::tidy::android