massberg updated this revision to Diff 132137.
massberg retitled this revision from "Add clang-tidy check for use of 
types/classes/functions from functional.h which are deprecated and removed in 
C++17" to "Add clang-tidy check for use of types/classes/functions from 
<functional> header which are deprecated and removed in C++17".
massberg edited the summary of this revision.
massberg added a comment.

Implemented all suggestions from the reviewer.


https://reviews.llvm.org/D42730

Files:
  clang-tidy/modernize/AvoidFunctionalCheck.cpp
  clang-tidy/modernize/AvoidFunctionalCheck.h
  clang-tidy/modernize/CMakeLists.txt
  clang-tidy/modernize/ModernizeTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/modernize-avoid-functional.rst
  test/clang-tidy/modernize-avoid-functional.cpp

Index: test/clang-tidy/modernize-avoid-functional.cpp
===================================================================
--- test/clang-tidy/modernize-avoid-functional.cpp
+++ test/clang-tidy/modernize-avoid-functional.cpp
@@ -0,0 +1,53 @@
+// RUN: %check_clang_tidy %s modernize-avoid-functional %t
+
+namespace std {
+
+template <class FirstArg, class SecondArg, class ReturnValue>
+class binary_function {};
+
+template <class FirstArg, class ReturnValue>
+class unary_function {};
+
+template <class Arg, class Result>
+class pointer_to_unary_function : public std::unary_function<Arg, Result> {}; // NOLINT
+
+template <class Arg, class Result>
+pointer_to_unary_function<Arg, Result> ptr_fun(Result (*f)(Arg)) {
+  pointer_to_unary_function<Arg, Result> Nothing;
+  return Nothing;
+}
+
+template <class Res, class T>
+class mem_fun_t {};
+
+template <class Res, class T>
+mem_fun_t<Res, T> mem_fun(Res (T::*f)()) {}
+
+} // namespace std
+
+// CHECK-MESSAGES: [[@LINE+1]]:25: warning: 'binary_function<int, int, bool>' is deprecated in C++11 and removed in C++17 [modernize-avoid-functional]
+class BinaryTestClass : public std::binary_function<int, int, bool> {
+ public:
+  BinaryTestClass();
+};
+
+// CHECK-MESSAGES: [[@LINE+1]]:24: warning: 'unary_function<int, bool>' is deprecated in C++11 and removed in C++17 [modernize-avoid-functional]
+class UnaryTestClass : public std::unary_function<int, bool> {
+ public:
+  UnaryTestClass();
+};
+
+int simpleFunc(int X) { return X + 1; };
+
+class TestClass {
+ public:
+  int test() { return 1; }
+};
+
+void foo(void) {
+// CHECK-MESSAGES: [[@LINE+1]]:3: warning: 'std::ptr_fun' is deprecated in C++11 and removed in C++17 [modernize-avoid-functional]
+  std::ptr_fun(simpleFunc);
+
+// CHECK-MESSAGES: [[@LINE+1]]:3: warning: 'std::mem_fun' is deprecated in C++11 and removed in C++17 [modernize-avoid-functional]
+  std::mem_fun(&TestClass::test);
+}
Index: docs/clang-tidy/checks/modernize-avoid-functional.rst
===================================================================
--- docs/clang-tidy/checks/modernize-avoid-functional.rst
+++ docs/clang-tidy/checks/modernize-avoid-functional.rst
@@ -0,0 +1,13 @@
+.. title:: clang-tidy - modernize-avoid-functional
+
+modernize-avoid-functional
+==========================
+
+In C++17 several classes, types and functions from <functional> header are no longer available.
+In particular, this check warns if one of the following deprecated objects is
+used:
+
+-  std::unary_function
+-  std::binary_function
+-  std::ptr_fun
+-  std::mem_fun
Index: docs/clang-tidy/checks/list.rst
===================================================================
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -154,6 +154,7 @@
    misc-unused-raii
    misc-unused-using-decls
    modernize-avoid-bind
+   modernize-avoid-functional
    modernize-deprecated-headers
    modernize-loop-convert
    modernize-make-shared
Index: docs/ReleaseNotes.rst
===================================================================
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -57,6 +57,12 @@
 Improvements to clang-tidy
 --------------------------
 
+- New `modernize-avoid-functional
+  <http://clang.llvm.org/extra/clang-tidy/checks/modernize-avoid-functional.html>`_ check
+
+  Warns if types, classes and functions from <functional> header which are
+  deprecated in C++11 and removed in C++17 are used.
+
 - The 'misc-incorrect-roundings' check was renamed to `bugprone-incorrect-roundings
   <http://clang.llvm.org/extra/clang-tidy/checks/bugprone-incorrect-roundings.html>`_
 
Index: clang-tidy/modernize/ModernizeTidyModule.cpp
===================================================================
--- clang-tidy/modernize/ModernizeTidyModule.cpp
+++ clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -11,6 +11,7 @@
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
 #include "AvoidBindCheck.h"
+#include "AvoidFunctionalCheck.h"
 #include "DeprecatedHeadersCheck.h"
 #include "LoopConvertCheck.h"
 #include "MakeSharedCheck.h"
@@ -45,6 +46,8 @@
 public:
   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
     CheckFactories.registerCheck<AvoidBindCheck>("modernize-avoid-bind");
+    CheckFactories.registerCheck<AvoidFunctionalCheck>(
+        "modernize-avoid-functional");
     CheckFactories.registerCheck<DeprecatedHeadersCheck>(
         "modernize-deprecated-headers");
     CheckFactories.registerCheck<LoopConvertCheck>("modernize-loop-convert");
Index: clang-tidy/modernize/CMakeLists.txt
===================================================================
--- clang-tidy/modernize/CMakeLists.txt
+++ clang-tidy/modernize/CMakeLists.txt
@@ -2,6 +2,7 @@
 
 add_clang_library(clangTidyModernizeModule
   AvoidBindCheck.cpp
+  AvoidFunctionalCheck.cpp
   DeprecatedHeadersCheck.cpp
   LoopConvertCheck.cpp
   LoopConvertUtils.cpp
Index: clang-tidy/modernize/AvoidFunctionalCheck.h
===================================================================
--- clang-tidy/modernize/AvoidFunctionalCheck.h
+++ clang-tidy/modernize/AvoidFunctionalCheck.h
@@ -0,0 +1,35 @@
+//===--- AvoidFunctionalCheck.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_MODERNIZE_AVOIDFUNCTIONALCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDFUNCTIONALCHECK_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+/// Check for several deprecated types and classes from <functional> header
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-avoid-functional.html
+class AvoidFunctionalCheck : public ClangTidyCheck {
+public:
+  AvoidFunctionalCheck(StringRef Name, ClangTidyContext *Context)
+      : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace modernize
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_AVOIDFUNCTIONALCHECK_H
Index: clang-tidy/modernize/AvoidFunctionalCheck.cpp
===================================================================
--- clang-tidy/modernize/AvoidFunctionalCheck.cpp
+++ clang-tidy/modernize/AvoidFunctionalCheck.cpp
@@ -0,0 +1,70 @@
+//===--- AvoidFunctionalCheck.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 "AvoidFunctionalCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+void AvoidFunctionalCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+      cxxRecordDecl(allOf(anyOf(isDerivedFrom("std::binary_function"),
+                                isDerivedFrom("std::unary_function")),
+                          anyOf(isClass(), ast_matchers::isStruct()),
+                          ast_matchers::isDefinition()))
+          .bind("un_or_binary_derived"),
+      this);
+  Finder->addMatcher(callExpr(callee(functionDecl(hasName("std::ptr_fun"))))
+                         .bind("ptr_fun_call"),
+                     this);
+  Finder->addMatcher(callExpr(callee(functionDecl(hasName("std::mem_fun"))))
+                         .bind("mem_fun_call"),
+                     this);
+}
+
+void AvoidFunctionalCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto* const UnaryOrBinaryDerived =
+          Result.Nodes.getNodeAs<RecordDecl>("un_or_binary_derived")) {
+    const auto* Decl = cast<CXXRecordDecl>(UnaryOrBinaryDerived);
+    for (CXXBaseSpecifier Base : Decl->bases()) {
+      QualType BaseType = Base.getType();
+      const auto *ClangDecl = BaseType->getAsCXXRecordDecl();
+      if (!ClangDecl) {
+        continue;
+      }
+
+      if (!ClangDecl->getDeclName().isIdentifier() ||
+          (ClangDecl->getName() != "unary_function" &&
+           ClangDecl->getName() != "binary_function")) {
+        continue;
+      }
+      diag(Base.getLocStart(), "%0 is deprecated in C++11 and removed in C++17")
+          << ClangDecl;
+    }
+  }
+  if (const auto* const PtrFunCall =
+          Result.Nodes.getNodeAs<CallExpr>("ptr_fun_call")) {
+    diag(PtrFunCall->getLocStart(),
+         "'std::ptr_fun' is deprecated in C++11 and removed in C++17");
+  }
+  if (const auto* const MemFunCall =
+          Result.Nodes.getNodeAs<CallExpr>("mem_fun_call")) {
+    diag(MemFunCall->getLocStart(),
+         "'std::mem_fun' is deprecated in C++11 and removed in C++17");
+  }
+}
+
+} // namespace modernize
+} // namespace tidy
+} // namespace clang
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to