summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp2
-rw-r--r--clang-tools-extra/clang-tidy/android/CMakeLists.txt1
-rw-r--r--clang-tools-extra/clang-tidy/android/CloexecCreatCheck.cpp59
-rw-r--r--clang-tools-extra/clang-tidy/android/CloexecCreatCheck.h35
-rw-r--r--clang-tools-extra/docs/ReleaseNotes.rst5
-rw-r--r--clang-tools-extra/docs/clang-tidy/checks/android-cloexec-creat.rst16
-rw-r--r--clang-tools-extra/test/clang-tidy/android-cloexec-creat.cpp35
7 files changed, 153 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp
index 0ff7060a548..b93c4aab6fc 100644
--- a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp
@@ -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 ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<FileOpenFlagCheck>("android-file-open-flag");
+ CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat");
}
};
diff --git a/clang-tools-extra/clang-tidy/android/CMakeLists.txt b/clang-tools-extra/clang-tidy/android/CMakeLists.txt
index ea66161cc7b..626720db37f 100644
--- a/clang-tools-extra/clang-tidy/android/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/android/CMakeLists.txt
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyAndroidModule
AndroidTidyModule.cpp
+ CloexecCreatCheck.cpp
FileOpenFlagCheck.cpp
LINK_LIBS
diff --git a/clang-tools-extra/clang-tidy/android/CloexecCreatCheck.cpp b/clang-tools-extra/clang-tidy/android/CloexecCreatCheck.cpp
new file mode 100644
index 00000000000..9b6ccf29a2a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/android/CloexecCreatCheck.cpp
@@ -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
diff --git a/clang-tools-extra/clang-tidy/android/CloexecCreatCheck.h b/clang-tools-extra/clang-tidy/android/CloexecCreatCheck.h
new file mode 100644
index 00000000000..bb015bb927b
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/android/CloexecCreatCheck.h
@@ -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
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index c18caee77b6..65a41faa171 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -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
diff --git a/clang-tools-extra/docs/clang-tidy/checks/android-cloexec-creat.rst b/clang-tools-extra/docs/clang-tidy/checks/android-cloexec-creat.rst
new file mode 100644
index 00000000000..d2892a0fdc5
--- /dev/null
+++ b/clang-tools-extra/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);
diff --git a/clang-tools-extra/test/clang-tidy/android-cloexec-creat.cpp b/clang-tools-extra/test/clang-tidy/android-cloexec-creat.cpp
new file mode 100644
index 00000000000..df57b10a238
--- /dev/null
+++ b/clang-tools-extra/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:
+ }
+};
OpenPOWER on IntegriCloud