Chih-Hung Hsieh | ae3527e | 2017-08-16 17:18:16 +0000 | [diff] [blame] | 1 | //===--- CloexecAcceptCheck.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 "CloexecAcceptCheck.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 CloexecAcceptCheck::registerMatchers(MatchFinder *Finder) { |
| 21 | auto SockAddrPointerType = |
| 22 | hasType(pointsTo(recordDecl(isStruct(), hasName("sockaddr")))); |
| 23 | auto SockLenPointerType = hasType(pointsTo(namedDecl(hasName("socklen_t")))); |
| 24 | |
| 25 | registerMatchersImpl(Finder, |
| 26 | functionDecl(returns(isInteger()), hasName("accept"), |
| 27 | hasParameter(0, hasType(isInteger())), |
| 28 | hasParameter(1, SockAddrPointerType), |
| 29 | hasParameter(2, SockLenPointerType))); |
| 30 | } |
| 31 | |
| 32 | void CloexecAcceptCheck::check(const MatchFinder::MatchResult &Result) { |
| 33 | const std::string &ReplacementText = |
| 34 | (Twine("accept4(") + getSpellingArg(Result, 0) + ", " + |
| 35 | getSpellingArg(Result, 1) + ", " + getSpellingArg(Result, 2) + |
| 36 | ", SOCK_CLOEXEC)") |
| 37 | .str(); |
| 38 | |
| 39 | replaceFunc( |
| 40 | Result, |
| 41 | "prefer accept4() to accept() because accept4() allows SOCK_CLOEXEC", |
| 42 | ReplacementText); |
| 43 | } |
| 44 | |
| 45 | } // namespace android |
| 46 | } // namespace tidy |
| 47 | } // namespace clang |