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
12using namespace clang::ast_matchers;
13
14namespace clang::tidy::android {
15
16void 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
28void 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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

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