xref: /llvm-project/clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp (revision 7d2ea6c422d3f5712b7253407005e1a465a76946)
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 
registerMatchers(MatchFinder * Finder)17 void CloexecOpenCheck::registerMatchers(MatchFinder *Finder) {
18   auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));
19   registerMatchersImpl(Finder,
20                        functionDecl(isExternC(), returns(isInteger()),
21                                     hasAnyName("open", "open64"),
22                                     hasParameter(0, CharPointerType),
23                                     hasParameter(1, hasType(isInteger()))));
24   registerMatchersImpl(Finder,
25                        functionDecl(isExternC(), returns(isInteger()),
26                                     hasName("openat"),
27                                     hasParameter(0, hasType(isInteger())),
28                                     hasParameter(1, CharPointerType),
29                                     hasParameter(2, hasType(isInteger()))));
30 }
31 
check(const MatchFinder::MatchResult & Result)32 void CloexecOpenCheck::check(const MatchFinder::MatchResult &Result) {
33   const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(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