blob: 83ca49a31133b20f21613617b577f243eb73a5e9 [file] [log] [blame]
Yan Wang0b974142017-06-29 17:40:57 +00001//===--- 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 Wang0b974142017-06-29 17:40:57 +000013
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18namespace android {
19
20void CloexecCreatCheck::registerMatchers(MatchFinder *Finder) {
21 auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));
22 auto MODETType = hasType(namedDecl(hasName("mode_t")));
Chih-Hung Hsiehfec506d2017-08-16 16:59:26 +000023 registerMatchersImpl(Finder,
24 functionDecl(isExternC(), returns(isInteger()),
25 hasName("creat"),
26 hasParameter(0, CharPointerType),
27 hasParameter(1, MODETType)));
Yan Wang0b974142017-06-29 17:40:57 +000028}
29
30void CloexecCreatCheck::check(const MatchFinder::MatchResult &Result) {
Yan Wang0b974142017-06-29 17:40:57 +000031 const std::string &ReplacementText =
Chih-Hung Hsiehfec506d2017-08-16 16:59:26 +000032 (Twine("open (") + getSpellingArg(Result, 0) +
Yan Wang0b974142017-06-29 17:40:57 +000033 ", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, " +
Chih-Hung Hsiehfec506d2017-08-16 16:59:26 +000034 getSpellingArg(Result, 1) + ")")
Yan Wang0b974142017-06-29 17:40:57 +000035 .str();
Chih-Hung Hsiehfec506d2017-08-16 16:59:26 +000036 replaceFunc(Result,
37 "prefer open() to creat() because open() allows O_CLOEXEC",
38 ReplacementText);
Yan Wang0b974142017-06-29 17:40:57 +000039}
40
41} // namespace android
42} // namespace tidy
43} // namespace clang