diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/android')
6 files changed, 90 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp index 729047ee35e..71c6accba97 100644 --- a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp @@ -11,6 +11,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "CloexecCreatCheck.h" +#include "CloexecDupCheck.h" #include "CloexecFopenCheck.h" #include "CloexecMemfdCreateCheck.h" #include "CloexecOpenCheck.h" @@ -27,6 +28,7 @@ class AndroidModule : public ClangTidyModule { public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat"); + CheckFactories.registerCheck<CloexecDupCheck>("android-cloexec-dup"); CheckFactories.registerCheck<CloexecFopenCheck>("android-cloexec-fopen"); CheckFactories.registerCheck<CloexecMemfdCreateCheck>( "android-cloexec-memfd-create"); diff --git a/clang-tools-extra/clang-tidy/android/CMakeLists.txt b/clang-tools-extra/clang-tidy/android/CMakeLists.txt index a70ea8de72f..77ba18c81a6 100644 --- a/clang-tools-extra/clang-tidy/android/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/android/CMakeLists.txt @@ -4,6 +4,7 @@ add_clang_library(clangTidyAndroidModule AndroidTidyModule.cpp CloexecCheck.cpp CloexecCreatCheck.cpp + CloexecDupCheck.cpp CloexecFopenCheck.cpp CloexecMemfdCreateCheck.cpp CloexecOpenCheck.cpp diff --git a/clang-tools-extra/clang-tidy/android/CloexecCheck.cpp b/clang-tools-extra/clang-tidy/android/CloexecCheck.cpp index 9cf823a48ad..38afa137059 100644 --- a/clang-tools-extra/clang-tidy/android/CloexecCheck.cpp +++ b/clang-tools-extra/clang-tidy/android/CloexecCheck.cpp @@ -100,6 +100,15 @@ void CloexecCheck::insertStringFlag( ReplacementText); } +StringRef CloexecCheck::getSpellingArg(const MatchFinder::MatchResult &Result, + int N) const { + const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr); + const SourceManager &SM = *Result.SourceManager; + return Lexer::getSourceText( + CharSourceRange::getTokenRange(MatchedCall->getArg(N)->getSourceRange()), + SM, Result.Context->getLangOpts()); +} + } // namespace android } // namespace tidy } // namespace clang diff --git a/clang-tools-extra/clang-tidy/android/CloexecCheck.h b/clang-tools-extra/clang-tidy/android/CloexecCheck.h index 9c1c1a9691f..a7a61069c02 100644 --- a/clang-tools-extra/clang-tidy/android/CloexecCheck.h +++ b/clang-tools-extra/clang-tidy/android/CloexecCheck.h @@ -86,6 +86,10 @@ protected: /// \param ArgPos The 0-based position of the flag argument. void insertStringFlag(const ast_matchers::MatchFinder::MatchResult &Result, const char Mode, const int ArgPos); + + /// Helper function to get the spelling of a particular argument. + StringRef getSpellingArg(const ast_matchers::MatchFinder::MatchResult &Result, + int N) const; }; } // namespace android diff --git a/clang-tools-extra/clang-tidy/android/CloexecDupCheck.cpp b/clang-tools-extra/clang-tidy/android/CloexecDupCheck.cpp new file mode 100644 index 00000000000..ec4ff0acd85 --- /dev/null +++ b/clang-tools-extra/clang-tidy/android/CloexecDupCheck.cpp @@ -0,0 +1,38 @@ +//===--- CloexecDupCheck.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 "CloexecDupCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace android { + +void CloexecDupCheck::registerMatchers(MatchFinder *Finder) { + registerMatchersImpl(Finder, + functionDecl(returns(isInteger()), hasName("dup"), + hasParameter(0, hasType(isInteger())))); +} + +void CloexecDupCheck::check(const MatchFinder::MatchResult &Result) { + const std::string &ReplacementText = + (Twine("fcntl(") + getSpellingArg(Result, 0) + ", F_DUPFD_CLOEXEC)") + .str(); + + replaceFunc(Result, + "prefer fcntl() to dup() because fcntl() allows F_DUPFD_CLOEXEC", + ReplacementText); +} + +} // namespace android +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/android/CloexecDupCheck.h b/clang-tools-extra/clang-tidy/android/CloexecDupCheck.h new file mode 100644 index 00000000000..c040b3808bb --- /dev/null +++ b/clang-tools-extra/clang-tidy/android/CloexecDupCheck.h @@ -0,0 +1,36 @@ +//===--- CloexecDupCheck.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_DUP_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_DUP_H + +#include "CloexecCheck.h" + +namespace clang { +namespace tidy { +namespace android { + +/// dup() is better to be replaced by fcntl(), which has close-on-exec flag. +/// Find the usage of dup() and redirect user to use fcntl(). +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-dup.html +class CloexecDupCheck : public CloexecCheck { +public: + CloexecDupCheck(StringRef Name, ClangTidyContext *Context) + : CloexecCheck(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_DUP_H |