blob: f583bd0c605b13bf0b51ca11e116eec0a26cd87a [file] [log] [blame]
Chih-Hung Hsiehae3527e2017-08-16 17:18:16 +00001//===--- 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
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18namespace android {
19
20void 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
32void 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