[clang-tools-extra] r306709 - [clang-tidy][Part3] Add a new module Android and three new checks.

Yan Wang via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 29 10:42:23 PDT 2017


Author: yawanng
Date: Thu Jun 29 10:42:23 2017
New Revision: 306709

URL: http://llvm.org/viewvc/llvm-project?rev=306709&view=rev
Log:
[clang-tidy][Part3] Add a new module Android and three new checks.

Summary: -- fopen() should include "e" in their mode string. [android-fopen-mode]

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/D33747

Added:
    clang-tools-extra/trunk/clang-tidy/android/CloexecFopenCheck.cpp
    clang-tools-extra/trunk/clang-tidy/android/CloexecFopenCheck.h
    clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-fopen.rst
    clang-tools-extra/trunk/test/clang-tidy/android-cloexec-fopen.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=306709&r1=306708&r2=306709&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:42:23 2017
@@ -11,6 +11,7 @@
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
 #include "CloexecCreatCheck.h"
+#include "CloexecFopenCheck.h"
 #include "FileOpenFlagCheck.h"
 
 using namespace clang::ast_matchers;
@@ -25,6 +26,7 @@ public:
   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
     CheckFactories.registerCheck<FileOpenFlagCheck>("android-file-open-flag");
     CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat");
+    CheckFactories.registerCheck<CloexecFopenCheck>("android-cloexec-fopen");
   }
 };
 

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=306709&r1=306708&r2=306709&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:42:23 2017
@@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support)
 add_clang_library(clangTidyAndroidModule
   AndroidTidyModule.cpp
   CloexecCreatCheck.cpp
+  CloexecFopenCheck.cpp
   FileOpenFlagCheck.cpp
 
   LINK_LIBS

Added: clang-tools-extra/trunk/clang-tidy/android/CloexecFopenCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecFopenCheck.cpp?rev=306709&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecFopenCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecFopenCheck.cpp Thu Jun 29 10:42:23 2017
@@ -0,0 +1,74 @@
+//===--- CloexecFopenCheck.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 "CloexecFopenCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace android {
+
+namespace {
+static const char MODE = 'e';
+
+// Build the replace text. If it's string constant, add 'e' directly in the end
+// of the string. Else, add "e".
+std::string BuildReplaceText(const Expr *Arg, const SourceManager &SM,
+                             const LangOptions &LangOpts) {
+  if (Arg->getLocStart().isMacroID())
+    return (Lexer::getSourceText(
+                CharSourceRange::getTokenRange(Arg->getSourceRange()), SM,
+                LangOpts) +
+            " \"" + Twine(MODE) + "\"")
+        .str();
+
+  StringRef SR = cast<StringLiteral>(Arg->IgnoreParenCasts())->getString();
+  return ("\"" + SR + Twine(MODE) + "\"").str();
+}
+} // namespace
+
+void CloexecFopenCheck::registerMatchers(MatchFinder *Finder) {
+  auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));
+
+  Finder->addMatcher(
+      callExpr(callee(functionDecl(isExternC(), returns(asString("FILE *")),
+                                   hasName("fopen"),
+                                   hasParameter(0, CharPointerType),
+                                   hasParameter(1, CharPointerType))
+                          .bind("funcDecl")))
+          .bind("fopenFn"),
+      this);
+}
+
+void CloexecFopenCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>("fopenFn");
+  const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("funcDecl");
+  const Expr *ModeArg = MatchedCall->getArg(1);
+
+  // Check if the 'e' may be in the mode string.
+  const auto *ModeStr = dyn_cast<StringLiteral>(ModeArg->IgnoreParenCasts());
+  if (!ModeStr || (ModeStr->getString().find(MODE) != StringRef::npos))
+    return;
+
+  const std::string &ReplacementText = BuildReplaceText(
+      ModeArg, *Result.SourceManager, Result.Context->getLangOpts());
+
+  diag(ModeArg->getLocStart(), "use %0 mode 'e' to set O_CLOEXEC")
+      << FD
+      << FixItHint::CreateReplacement(ModeArg->getSourceRange(),
+                                      ReplacementText);
+}
+
+} // namespace android
+} // namespace tidy
+} // namespace clang

Added: clang-tools-extra/trunk/clang-tidy/android/CloexecFopenCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/android/CloexecFopenCheck.h?rev=306709&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/android/CloexecFopenCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/android/CloexecFopenCheck.h Thu Jun 29 10:42:23 2017
@@ -0,0 +1,38 @@
+//===--- CloexecFopenCheck.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_FOPEN_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_FOPEN_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace android {
+
+/// fopen() is suggested to include "e" in their mode string; like "re" would be
+/// better than "r".
+///
+/// This check only works when corresponding argument is StringLiteral. No
+/// constant propagation.
+///
+/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-fopen.html
+class CloexecFopenCheck : public ClangTidyCheck {
+public:
+  CloexecFopenCheck(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_FOPEN_H

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=306709&r1=306708&r2=306709&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Thu Jun 29 10:42:23 2017
@@ -68,6 +68,11 @@ Improvements to clang-tidy
   Checks if the required file flag ``O_CLOEXEC`` exists in ``open()``,
   ``open64()`` and ``openat()``.
 
+- New `android-cloexec-fopen
+  <http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-fopen.html>`_ check
+
+  Checks if the required mode ``e`` exists in the mode argument of ``fopen()``.
+
 - New `cert-dcl21-cpp
   <http://clang.llvm.org/extra/clang-tidy/checks/cert-dcl21-cpp.html>`_ check
 

Added: clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-fopen.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-fopen.rst?rev=306709&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-fopen.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/android-cloexec-fopen.rst Thu Jun 29 10:42:23 2017
@@ -0,0 +1,18 @@
+.. title:: clang-tidy - android-cloexec-fopen
+
+android-cloexec-fopen
+=====================
+
+``fopen()`` should include ``e`` in their mode string; so ``re`` would be
+valid. This is equivalent to having set ``FD_CLOEXEC on`` that descriptor.
+
+Examples:
+
+.. code-block:: c++
+
+  fopen("fn", "r");
+
+  // becomes
+
+  fopen("fn", "re");
+

Added: clang-tools-extra/trunk/test/clang-tidy/android-cloexec-fopen.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/android-cloexec-fopen.cpp?rev=306709&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/android-cloexec-fopen.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/android-cloexec-fopen.cpp Thu Jun 29 10:42:23 2017
@@ -0,0 +1,57 @@
+// RUN: %check_clang_tidy %s android-cloexec-fopen %t
+
+#define FILE_OPEN_RO "r"
+
+typedef int FILE;
+
+extern "C" FILE *fopen(const char *filename, const char *mode, ...);
+extern "C" FILE *open(const char *filename, const char *mode, ...);
+
+void f() {
+  fopen("filename", "r");
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use 'fopen' mode 'e' to set O_CLOEXEC [android-cloexec-fopen]
+  // CHECK-FIXES: fopen("filename", "re");
+
+  fopen("filename", FILE_OPEN_RO);
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use 'fopen' mode 'e'
+  // CHECK-FIXES: fopen("filename", FILE_OPEN_RO "e");
+
+  fopen("filename", "er");
+  // CHECK-MESSAGES-NOT: warning:
+  fopen("filename", "re");
+  // CHECK-MESSAGES-NOT: warning:
+  fopen("filename", "e");
+  // CHECK-MESSAGES-NOT: warning:
+  open("filename", "e");
+  // CHECK-MESSAGES-NOT: warning:
+
+  char *str = "r";
+  fopen("filename", str);
+  // CHECK-MESSAGES-NOT: warning:
+  str = "re";
+  fopen("filename", str);
+  // CHECK-MESSAGES-NOT: warning:
+  char arr[2] = "r";
+  fopen("filename", arr);
+  // CHECK-MESSAGES-NOT: warning:
+  char arr2[3] = "re";
+  fopen("filename", arr2);
+  // CHECK-MESSAGES-NOT: warning:
+}
+
+namespace i {
+int *fopen(const char *filename, const char *mode, ...);
+void g() {
+  fopen("filename", "e");
+  // CHECK-MESSAGES-NOT: warning:
+}
+} // namespace i
+
+class C {
+public:
+  int *fopen(const char *filename, const char *mode, ...);
+  void h() {
+    fopen("filename", "e");
+    // CHECK-MESSAGES-NOT: warning:
+  }
+};




More information about the cfe-commits mailing list