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

source code of clang-tools-extra/clang-tidy/android/CloexecAcceptCheck.cpp