1 | //===--- CloexecOpenCheck.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 "CloexecOpenCheck.h" |
10 | #include "clang/AST/ASTContext.h" |
11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
12 | |
13 | using namespace clang::ast_matchers; |
14 | |
15 | namespace clang::tidy::android { |
16 | |
17 | void CloexecOpenCheck::registerMatchers(MatchFinder *Finder) { |
18 | auto CharPointerType = hasType(InnerMatcher: pointerType(pointee(isAnyCharacter()))); |
19 | registerMatchersImpl(Finder, |
20 | Function: functionDecl(isExternC(), returns(InnerMatcher: isInteger()), |
21 | hasAnyName("open" , "open64" ), |
22 | hasParameter(N: 0, InnerMatcher: CharPointerType), |
23 | hasParameter(N: 1, InnerMatcher: hasType(InnerMatcher: isInteger())))); |
24 | registerMatchersImpl(Finder, |
25 | Function: functionDecl(isExternC(), returns(InnerMatcher: isInteger()), |
26 | hasName(Name: "openat" ), |
27 | hasParameter(N: 0, InnerMatcher: hasType(InnerMatcher: isInteger())), |
28 | hasParameter(N: 1, InnerMatcher: CharPointerType), |
29 | hasParameter(N: 2, InnerMatcher: hasType(InnerMatcher: isInteger())))); |
30 | } |
31 | |
32 | void CloexecOpenCheck::check(const MatchFinder::MatchResult &Result) { |
33 | const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(ID: FuncDeclBindingStr); |
34 | assert(FD->param_size() > 1); |
35 | int ArgPos = (FD->param_size() > 2) ? 2 : 1; |
36 | insertMacroFlag(Result, /*MacroFlag=*/"O_CLOEXEC" , ArgPos); |
37 | } |
38 | |
39 | } // namespace clang::tidy::android |
40 | |