42 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//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(pointsTo(recordDecl(isStruct(), hasName("sockaddr"))));19 auto SockLenPointerType = hasType(pointsTo(namedDecl(hasName("socklen_t"))));20 21 registerMatchersImpl(Finder,22 functionDecl(returns(isInteger()), hasName("accept"),23 hasParameter(0, hasType(isInteger())),24 hasParameter(1, SockAddrPointerType),25 hasParameter(2, SockLenPointerType)));26}27 28void CloexecAcceptCheck::check(const MatchFinder::MatchResult &Result) {29 const std::string ReplacementText =30 (Twine("accept4(") + getSpellingArg(Result, 0) + ", " +31 getSpellingArg(Result, 1) + ", " + getSpellingArg(Result, 2) +32 ", SOCK_CLOEXEC)")33 .str();34 35 replaceFunc(36 Result,37 "prefer accept4() to accept() because accept4() allows SOCK_CLOEXEC",38 ReplacementText);39}40 41} // namespace clang::tidy::android42