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