Yan Wang | 600a613 | 2017-06-29 19:13:29 +0000 | [diff] [blame] | 1 | //===--- CloexecOpenCheck.cpp - clang-tidy---------------------------------===// |
Yan Wang | 3620620 | 2017-06-23 21:37:29 +0000 | [diff] [blame] | 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 | |
Yan Wang | 600a613 | 2017-06-29 19:13:29 +0000 | [diff] [blame] | 10 | #include "CloexecOpenCheck.h" |
Yan Wang | 3620620 | 2017-06-23 21:37:29 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Yan Wang | 3620620 | 2017-06-23 21:37:29 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
| 18 | namespace android { |
| 19 | |
Yan Wang | 600a613 | 2017-06-29 19:13:29 +0000 | [diff] [blame] | 20 | void CloexecOpenCheck::registerMatchers(MatchFinder *Finder) { |
Yan Wang | 3620620 | 2017-06-23 21:37:29 +0000 | [diff] [blame] | 21 | auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter()))); |
Chih-Hung Hsieh | fec506d | 2017-08-16 16:59:26 +0000 | [diff] [blame] | 22 | 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 Wang | 3620620 | 2017-06-23 21:37:29 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Yan Wang | 600a613 | 2017-06-29 19:13:29 +0000 | [diff] [blame] | 35 | void CloexecOpenCheck::check(const MatchFinder::MatchResult &Result) { |
Chih-Hung Hsieh | fec506d | 2017-08-16 16:59:26 +0000 | [diff] [blame] | 36 | const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(FuncDeclBindingStr); |
| 37 | assert(FD->param_size() > 1); |
| 38 | int ArgPos = (FD->param_size() > 2) ? 2 : 1; |
Benjamin Kramer | 26f2eec | 2018-02-02 13:39:07 +0000 | [diff] [blame] | 39 | insertMacroFlag(Result, /*MacroFlag=*/"O_CLOEXEC", ArgPos); |
Yan Wang | 3620620 | 2017-06-23 21:37:29 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | } // namespace android |
| 43 | } // namespace tidy |
| 44 | } // namespace clang |