1 | //===--- CloexecAcceptCheck.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 | |
9 | #include "CloexecAcceptCheck.h" |
10 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
11 | |
12 | using namespace clang::ast_matchers; |
13 | |
14 | namespace clang::tidy::android { |
15 | |
16 | void CloexecAcceptCheck::registerMatchers(MatchFinder *Finder) { |
17 | auto SockAddrPointerType = |
18 | hasType(InnerMatcher: pointsTo(InnerMatcher: recordDecl(isStruct(), hasName(Name: "sockaddr")))); |
19 | auto SockLenPointerType = hasType(InnerMatcher: pointsTo(InnerMatcher: namedDecl(hasName(Name: "socklen_t")))); |
20 | |
21 | registerMatchersImpl(Finder, |
22 | Function: functionDecl(returns(InnerMatcher: isInteger()), hasName(Name: "accept"), |
23 | hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: isInteger())), |
24 | hasParameter(N: 1, InnerMatcher: SockAddrPointerType), |
25 | hasParameter(N: 2, InnerMatcher: SockLenPointerType))); |
26 | } |
27 | |
28 | void CloexecAcceptCheck::check(const MatchFinder::MatchResult &Result) { |
29 | std::string ReplacementText = (Twine("accept4(") + getSpellingArg(Result, N: 0) + |
30 | ", "+ getSpellingArg(Result, N: 1) + ", "+ |
31 | getSpellingArg(Result, N: 2) + ", SOCK_CLOEXEC)") |
32 | .str(); |
33 | |
34 | replaceFunc( |
35 | Result, |
36 | WarningMsg: "prefer accept4() to accept() because accept4() allows SOCK_CLOEXEC", |
37 | FixMsg: ReplacementText); |
38 | } |
39 | |
40 | } // namespace clang::tidy::android |
41 |