xref: /llvm-project/clang-tools-extra/clang-tidy/android/CloexecPipeCheck.cpp (revision 7d2ea6c422d3f5712b7253407005e1a465a76946)
15b2a85d0SGeorge Burgess IV //===--- CloexecPipeCheck.cpp - clang-tidy---------------------------------===//
25b2a85d0SGeorge Burgess IV //
35b2a85d0SGeorge Burgess IV // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45b2a85d0SGeorge Burgess IV // See https://llvm.org/LICENSE.txt for license information.
55b2a85d0SGeorge Burgess IV // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65b2a85d0SGeorge Burgess IV //
75b2a85d0SGeorge Burgess IV //===----------------------------------------------------------------------===//
85b2a85d0SGeorge Burgess IV 
95b2a85d0SGeorge Burgess IV #include "CloexecPipeCheck.h"
105b2a85d0SGeorge Burgess IV #include "clang/AST/ASTContext.h"
115b2a85d0SGeorge Burgess IV #include "clang/ASTMatchers/ASTMatchFinder.h"
125b2a85d0SGeorge Burgess IV 
135b2a85d0SGeorge Burgess IV using namespace clang::ast_matchers;
145b2a85d0SGeorge Burgess IV 
15*7d2ea6c4SCarlos Galvez namespace clang::tidy::android {
165b2a85d0SGeorge Burgess IV 
registerMatchers(MatchFinder * Finder)175b2a85d0SGeorge Burgess IV void CloexecPipeCheck::registerMatchers(MatchFinder *Finder) {
185b2a85d0SGeorge Burgess IV   registerMatchersImpl(Finder,
195b2a85d0SGeorge Burgess IV                        functionDecl(returns(isInteger()), hasName("pipe"),
205b2a85d0SGeorge Burgess IV                                     hasParameter(0, hasType(pointsTo(isInteger())))));
215b2a85d0SGeorge Burgess IV }
225b2a85d0SGeorge Burgess IV 
check(const MatchFinder::MatchResult & Result)235b2a85d0SGeorge Burgess IV void CloexecPipeCheck::check(const MatchFinder::MatchResult &Result) {
245b2a85d0SGeorge Burgess IV   std::string ReplacementText =
255b2a85d0SGeorge Burgess IV       (Twine("pipe2(") + getSpellingArg(Result, 0) + ", O_CLOEXEC)").str();
265b2a85d0SGeorge Burgess IV 
275b2a85d0SGeorge Burgess IV   replaceFunc(
285b2a85d0SGeorge Burgess IV       Result,
295b2a85d0SGeorge Burgess IV       "prefer pipe2() with O_CLOEXEC to avoid leaking file descriptors to child processes",
305b2a85d0SGeorge Burgess IV       ReplacementText);
315b2a85d0SGeorge Burgess IV }
325b2a85d0SGeorge Burgess IV 
33*7d2ea6c4SCarlos Galvez } // namespace clang::tidy::android
34