Author: yawanng Date: Thu Jun 29 10:40:57 2017 New Revision: 306708 URL: http://llvm.org/viewvc/llvm-project?rev=306708&view=rev Log: [clang-tidy][Part2] Add a new module Android and three new checks
Summary: -- creat() should be replaced by open(). [android-creat-usage] Reviewers: chh, alexfh, aaron.ballman, hokein Reviewed By: hokein Subscribers: JDevlieghere, srhines, mgorny, xazax.hun Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D33745 Added: clang-tools-extra/trunk/clang-tidy/android/CloexecCreatCheck.cpp clang-tools-extra/trunk/clang-tidy/android/CloexecCreatCheck.h clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-creat.rst clang-tools-extra/trunk/test/clang-tidy/android-cloexec-creat.cpp Modified: clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt clang-tools-extra/trunk/docs/ReleaseNotes.rst Modified: clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp?rev=306708&r1=306707&r2=306708&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/android/AndroidTidyModule.cpp Thu Jun 29 10:40:57 2017 @@ -10,6 +10,7 @@ #include "../ClangTidy.h" #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" +#include "CloexecCreatCheck.h" #include "FileOpenFlagCheck.h" using namespace clang::ast_matchers; @@ -23,6 +24,7 @@ class AndroidModule : public ClangTidyMo public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck<FileOpenFlagCheck>("android-file-open-flag"); + CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat"); } }; Modified: clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt?rev=306708&r1=306707&r2=306708&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt (original) +++ clang-tools-extra/trunk/clang-tidy/android/CMakeLists.txt Thu Jun 29 10:40:57 2017 @@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyAndroidModule AndroidTidyModule.cpp + CloexecCreatCheck.cpp FileOpenFlagCheck.cpp LINK_LIBS Added: clang-tools-extra/trunk/clang-tidy/android/CloexecCreatCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecCreatCheck.cpp?rev=306708&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/android/CloexecCreatCheck.cpp (added) +++ clang-tools-extra/trunk/clang-tidy/android/CloexecCreatCheck.cpp Thu Jun 29 10:40:57 2017 @@ -0,0 +1,59 @@ +//===--- CloexecCreatCheck.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 "CloexecCreatCheck.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 CloexecCreatCheck::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 CloexecCreatCheck::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>("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 Added: clang-tools-extra/trunk/clang-tidy/android/CloexecCreatCheck.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecCreatCheck.h?rev=306708&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/android/CloexecCreatCheck.h (added) +++ clang-tools-extra/trunk/clang-tidy/android/CloexecCreatCheck.h Thu Jun 29 10:40:57 2017 @@ -0,0 +1,35 @@ +//===--- CloexecCreatCheck.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 CloexecCreatCheck : public ClangTidyCheck { +public: + CloexecCreatCheck(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 Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=306708&r1=306707&r2=306708&view=diff ============================================================================== --- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original) +++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Thu Jun 29 10:40:57 2017 @@ -57,6 +57,11 @@ The improvements are... Improvements to clang-tidy -------------------------- +- New `android-cloexec-creat + <http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-creat.html>`_ check + + Detect usage of ``creat()``. + - New `android-file-open-flag <http://clang.llvm.org/extra/clang-tidy/checks/android-file-open-flag.html>`_ check Added: clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-creat.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-creat.rst?rev=306708&view=auto ============================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-creat.rst (added) +++ clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-creat.rst Thu Jun 29 10:40:57 2017 @@ -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); Added: clang-tools-extra/trunk/test/clang-tidy/android-cloexec-creat.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/android-cloexec-creat.cpp?rev=306708&view=auto ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/android-cloexec-creat.cpp (added) +++ clang-tools-extra/trunk/test/clang-tidy/android-cloexec-creat.cpp Thu Jun 29 10:40:57 2017 @@ -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: + } +}; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits