Index: clang-tidy/android/AndroidTidyModule.cpp =================================================================== --- clang-tidy/android/AndroidTidyModule.cpp +++ clang-tidy/android/AndroidTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidy.h" #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" +#include "CloexecCreat.h" #include "FileOpenFlagCheck.h" using namespace clang::ast_matchers; @@ -23,6 +24,7 @@ public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck("android-file-open-flag"); + CheckFactories.registerCheck("android-cloexec-creat"); } }; Index: clang-tidy/android/CMakeLists.txt =================================================================== --- clang-tidy/android/CMakeLists.txt +++ clang-tidy/android/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyAndroidModule AndroidTidyModule.cpp + CloexecCreat.cpp FileOpenFlagCheck.cpp LINK_LIBS Index: clang-tidy/android/CloexecCreat.h =================================================================== --- /dev/null +++ clang-tidy/android/CloexecCreat.h @@ -0,0 +1,35 @@ +//===--- CloexecCreat.h - clang-tidy-----------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_CREAT_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_CREAT_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace android { + +/// creat() is better to be replaced by open(). +/// Find the usage of creat() and redirect user to use open(). + +/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-creat.html +class CloexecCreat : public ClangTidyCheck { +public: + CloexecCreat(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace android +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_CREAT_H Index: clang-tidy/android/CloexecCreat.cpp =================================================================== --- /dev/null +++ clang-tidy/android/CloexecCreat.cpp @@ -0,0 +1,59 @@ +//===--- CloexecCreat.cpp - clang-tidy-------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "CloexecCreat.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace android { + +void CloexecCreat::registerMatchers(MatchFinder *Finder) { + auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter()))); + auto MODETType = hasType(namedDecl(hasName("mode_t"))); + + Finder->addMatcher( + callExpr(callee(functionDecl(isExternC(), returns(isInteger()), + hasName("creat"), + hasParameter(0, CharPointerType), + hasParameter(1, MODETType)) + .bind("funcDecl"))) + .bind("creatFn"), + this); +} + +void CloexecCreat::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedCall = Result.Nodes.getNodeAs("creatFn"); + const SourceManager &SM = *Result.SourceManager; + + const std::string &ReplacementText = + (Twine("open (") + + Lexer::getSourceText(CharSourceRange::getTokenRange( + MatchedCall->getArg(0)->getSourceRange()), + SM, Result.Context->getLangOpts()) + + ", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, " + + Lexer::getSourceText(CharSourceRange::getTokenRange( + MatchedCall->getArg(1)->getSourceRange()), + SM, Result.Context->getLangOpts()) + + ")") + .str(); + + diag(MatchedCall->getLocStart(), + "prefer open() to creat() because open() allows O_CLOEXEC") + << FixItHint::CreateReplacement(MatchedCall->getSourceRange(), + ReplacementText); +} + +} // namespace android +} // namespace tidy +} // namespace clang Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -57,6 +57,11 @@ Improvements to clang-tidy -------------------------- +- New `android-cloexec-creat + `_ check + + Detect usage of ``creat()``. + - New `android-file-open-flag `_ check Index: docs/clang-tidy/checks/android-cloexec-creat.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/android-cloexec-creat.rst @@ -0,0 +1,16 @@ +.. title:: clang-tidy - android-cloexec-creat + +android-cloexec-creat +=================== + +The usage of ``creat()`` is not recommended, it's better to use ``open()``. + +Examples: + +.. code-block:: c++ + + int fd = creat(path, mode); + + // becomes + + int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode); Index: test/clang-tidy/android-cloexec-creat.cpp =================================================================== --- /dev/null +++ test/clang-tidy/android-cloexec-creat.cpp @@ -0,0 +1,35 @@ +// RUN: %check_clang_tidy %s android-cloexec-creat %t + +typedef int mode_t; + +extern "C" int creat(const char *path, mode_t, ...); +extern "C" int create(const char *path, mode_t, ...); + +void f() { + creat("filename", 0); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer open() to creat() because open() allows O_CLOEXEC [android-cloexec-creat] + // CHECK-FIXES: open ("filename", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0); + create("filename", 0); + // CHECK-MESSAGES-NOT: warning: + mode_t mode = 0755; + creat("filename", mode); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: + // CHECK-FIXES: open ("filename", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode); +} + +namespace i { +int creat(const char *path, mode_t, ...); +void g() { + creat("filename", 0); + // CHECK-MESSAGES-NOT: warning: +} +} // namespace i + +class C { +public: + int creat(const char *path, mode_t, ...); + void h() { + creat("filename", 0); + // CHECK-MESSAGES-NOT: warning: + } +};