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" 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"))); 23 registerMatchersImpl(Finder, 24 functionDecl(isExternC(), returns(isInteger()), 25 hasName("creat"), 26 hasParameter(0, CharPointerType), 27 hasParameter(1, MODETType))); 28 } 29 30 void CloexecCreatCheck::check(const MatchFinder::MatchResult &Result) { 31 const std::string &ReplacementText = 32 (Twine("open (") + getSpellingArg(Result, 0) + 33 ", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, " + 34 getSpellingArg(Result, 1) + ")") 35 .str(); 36 replaceFunc(Result, 37 "prefer open() to creat() because open() allows O_CLOEXEC", 38 ReplacementText); 39 } 40 41 } // namespace android 42 } // namespace tidy 43 } // namespace clang 44