Yan Wang | 0b97414 | 2017-06-29 17:40:57 +0000 | [diff] [blame] | 1 | //===--- CloexecCreatCheck.cpp - clang-tidy--------------------------------===// |
| 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 | |
| 10 | #include "CloexecCreatCheck.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Yan Wang | 0b97414 | 2017-06-29 17:40:57 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
| 18 | namespace android { |
| 19 | |
| 20 | void CloexecCreatCheck::registerMatchers(MatchFinder *Finder) { |
| 21 | auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter()))); |
| 22 | auto MODETType = hasType(namedDecl(hasName("mode_t"))); |
Chih-Hung Hsieh | fec506d | 2017-08-16 16:59:26 +0000 | [diff] [blame] | 23 | registerMatchersImpl(Finder, |
| 24 | functionDecl(isExternC(), returns(isInteger()), |
| 25 | hasName("creat"), |
| 26 | hasParameter(0, CharPointerType), |
| 27 | hasParameter(1, MODETType))); |
Yan Wang | 0b97414 | 2017-06-29 17:40:57 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void CloexecCreatCheck::check(const MatchFinder::MatchResult &Result) { |
Yan Wang | 0b97414 | 2017-06-29 17:40:57 +0000 | [diff] [blame] | 31 | const std::string &ReplacementText = |
Chih-Hung Hsieh | fec506d | 2017-08-16 16:59:26 +0000 | [diff] [blame] | 32 | (Twine("open (") + getSpellingArg(Result, 0) + |
Yan Wang | 0b97414 | 2017-06-29 17:40:57 +0000 | [diff] [blame] | 33 | ", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, " + |
Chih-Hung Hsieh | fec506d | 2017-08-16 16:59:26 +0000 | [diff] [blame] | 34 | getSpellingArg(Result, 1) + ")") |
Yan Wang | 0b97414 | 2017-06-29 17:40:57 +0000 | [diff] [blame] | 35 | .str(); |
Chih-Hung Hsieh | fec506d | 2017-08-16 16:59:26 +0000 | [diff] [blame] | 36 | replaceFunc(Result, |
| 37 | "prefer open() to creat() because open() allows O_CLOEXEC", |
| 38 | ReplacementText); |
Yan Wang | 0b97414 | 2017-06-29 17:40:57 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | } // namespace android |
| 42 | } // namespace tidy |
| 43 | } // namespace clang |