blob: a4d3bc68fdfe477b845d86968ddb96d04afd0ce5 [file] [log] [blame]
Yan Wang600a6132017-06-29 19:13:29 +00001//===--- CloexecOpenCheck.cpp - clang-tidy---------------------------------===//
Yan Wang36206202017-06-23 21:37:29 +00002//
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
Yan Wang600a6132017-06-29 19:13:29 +000010#include "CloexecOpenCheck.h"
Yan Wang36206202017-06-23 21:37:29 +000011#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
Yan Wang36206202017-06-23 21:37:29 +000013
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18namespace android {
19
Yan Wang600a6132017-06-29 19:13:29 +000020void CloexecOpenCheck::registerMatchers(MatchFinder *Finder) {
Yan Wang36206202017-06-23 21:37:29 +000021 auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));
Chih-Hung Hsiehfec506d2017-08-16 16:59:26 +000022 registerMatchersImpl(Finder,
23 functionDecl(isExternC(), returns(isInteger()),
24 hasAnyName("open", "open64"),
25 hasParameter(0, CharPointerType),
26 hasParameter(1, hasType(isInteger()))));
27 registerMatchersImpl(Finder,
28 functionDecl(isExternC(), returns(isInteger()),
29 hasName("openat"),
30 hasParameter(0, hasType(isInteger())),
31 hasParameter(1, CharPointerType),
32 hasParameter(2, hasType(isInteger()))));
Yan Wang36206202017-06-23 21:37:29 +000033}
34
Yan Wang600a6132017-06-29 19:13:29 +000035void CloexecOpenCheck::check(const MatchFinder::MatchResult &Result) {
Chih-Hung Hsiehfec506d2017-08-16 16:59:26 +000036 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(FuncDeclBindingStr);
37 assert(FD->param_size() > 1);
38 int ArgPos = (FD->param_size() > 2) ? 2 : 1;
Benjamin Kramer26f2eec2018-02-02 13:39:07 +000039 insertMacroFlag(Result, /*MacroFlag=*/"O_CLOEXEC", ArgPos);
Yan Wang36206202017-06-23 21:37:29 +000040}
41
42} // namespace android
43} // namespace tidy
44} // namespace clang