1 //===--- CloexecDupCheck.cpp - clang-tidy----------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "CloexecDupCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace android {
19 
20 void CloexecDupCheck::registerMatchers(MatchFinder *Finder) {
21   registerMatchersImpl(Finder,
22                        functionDecl(returns(isInteger()), hasName("dup"),
23                                     hasParameter(0, hasType(isInteger()))));
24 }
25 
26 void CloexecDupCheck::check(const MatchFinder::MatchResult &Result) {
27   const std::string &ReplacementText =
28       (Twine("fcntl(") + getSpellingArg(Result, 0) + ", F_DUPFD_CLOEXEC)")
29           .str();
30 
31   replaceFunc(Result,
32               "prefer fcntl() to dup() because fcntl() allows F_DUPFD_CLOEXEC",
33               ReplacementText);
34 }
35 
36 } // namespace android
37 } // namespace tidy
38 } // namespace clang
39