malcolm.parsons updated this revision to Diff 80642.
malcolm.parsons added a comment.

Add to release notes.
Remove youtube link.


https://reviews.llvm.org/D27520

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/ReadabilityTidyModule.cpp
  clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.cpp
  clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-redundant-function-ptr-dereference.rst
  test/clang-tidy/readability-redundant-function-ptr-dereference.cpp

Index: test/clang-tidy/readability-redundant-function-ptr-dereference.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/readability-redundant-function-ptr-dereference.cpp
@@ -0,0 +1,24 @@
+// RUN: %check_clang_tidy %s readability-redundant-function-ptr-dereference %t
+
+void f(int i);
+
+void positive() {
+  void (*p)(int) = f;
+
+  (**p)(1);
+  // CHECK-MESSAGES: :[[@LINE-1]]:4: warning: redundant repeated dereference of function pointer [readability-redundant-function-ptr-dereference]
+  // CHECK-FIXES: (*p)(1);
+  (*****p)(2);
+  // CHECK-MESSAGES: :[[@LINE-1]]:4: warning: redundant repeated
+  // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: redundant repeated
+  // CHECK-MESSAGES: :[[@LINE-3]]:6: warning: redundant repeated
+  // CHECK-MESSAGES: :[[@LINE-4]]:7: warning: redundant repeated
+  // CHECK-FIXES: (*p)(2);
+}
+
+void negative() {
+  void (*q)(int) = &f;
+
+  q(1);
+  (*q)(2);
+}
Index: docs/clang-tidy/checks/readability-redundant-function-ptr-dereference.rst
===================================================================
--- /dev/null
+++ docs/clang-tidy/checks/readability-redundant-function-ptr-dereference.rst
@@ -0,0 +1,25 @@
+.. title:: clang-tidy - readability-redundant-function-ptr-dereference
+
+readability-redundant-function-ptr-dereference
+==============================================
+
+Finds redundant dereferences of a function pointer.
+
+Before:
+
+.. code-block:: c++
+
+  int f(int,int);
+  int (*p)(int, int) = &f;
+
+  int i = (**p)(10, 50);
+
+After:
+
+.. code-block:: c++
+
+  int f(int,int);
+  int (*p)(int, int) = &f;
+
+  int i = (*p)(10, 50);
+
Index: docs/clang-tidy/checks/list.rst
===================================================================
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -138,6 +138,7 @@
    readability-non-const-parameter
    readability-redundant-control-flow
    readability-redundant-declaration
+   readability-redundant-function-ptr-dereference
    readability-redundant-member-init
    readability-redundant-smartptr-get
    readability-redundant-string-cstr
Index: docs/ReleaseNotes.rst
===================================================================
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -141,6 +141,11 @@
 
   Finds redundant variable and function declarations.
 
+- New `readability-redundant-function-ptr-dereference
+  <http://clang.llvm.org/extra/clang-tidy/checks/readability-redundant-function-ptr-dereference.html>`_ check
+
+  Finds redundant function pointer dereferences.
+
 - New `readability-redundant-member-init
   <http://clang.llvm.org/extra/clang-tidy/checks/readability-redundant-member-init.html>`_ check
 
Index: clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.h
===================================================================
--- /dev/null
+++ clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.h
@@ -0,0 +1,35 @@
+//===--- RedundantFunctionPtrDereferenceCheck.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_READABILITY_REDUNDANT_FUNCTION_PTR_DEREFERENCE_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANT_FUNCTION_PTR_DEREFERENCE_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace readability {
+
+/// Eliminate redundant dereferences of a function pointer.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/readability-redundant-function-ptr-dereference.html
+class RedundantFunctionPtrDereferenceCheck : public ClangTidyCheck {
+public:
+  RedundantFunctionPtrDereferenceCheck(StringRef Name, ClangTidyContext *Context)
+      : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace readability
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANT_FUNCTION_PTR_DEREFERENCE_H
Index: clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.cpp
===================================================================
--- /dev/null
+++ clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.cpp
@@ -0,0 +1,37 @@
+//===--- RedundantFunctionPtrDereferenceCheck.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 "RedundantFunctionPtrDereferenceCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace readability {
+
+void RedundantFunctionPtrDereferenceCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(unaryOperator(hasOperatorName("*"),
+                                   has(implicitCastExpr(
+                                       hasCastKind(CK_FunctionToPointerDecay))))
+                         .bind("op"),
+                     this);
+}
+
+void RedundantFunctionPtrDereferenceCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Operator = Result.Nodes.getNodeAs<UnaryOperator>("op");
+  diag(Operator->getOperatorLoc(),
+       "redundant repeated dereference of function pointer")
+      << FixItHint::CreateRemoval(Operator->getOperatorLoc());
+}
+
+} // namespace readability
+} // namespace tidy
+} // namespace clang
Index: clang-tidy/readability/ReadabilityTidyModule.cpp
===================================================================
--- clang-tidy/readability/ReadabilityTidyModule.cpp
+++ clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -24,6 +24,7 @@
 #include "NonConstParameterCheck.h"
 #include "RedundantControlFlowCheck.h"
 #include "RedundantDeclarationCheck.h"
+#include "RedundantFunctionPtrDereferenceCheck.h"
 #include "RedundantMemberInitCheck.h"
 #include "RedundantSmartptrGetCheck.h"
 #include "RedundantStringCStrCheck.h"
@@ -59,6 +60,8 @@
         "readability-inconsistent-declaration-parameter-name");
     CheckFactories.registerCheck<MisplacedArrayIndexCheck>(
         "readability-misplaced-array-index");
+    CheckFactories.registerCheck<RedundantFunctionPtrDereferenceCheck>(
+        "readability-redundant-function-ptr-dereference");
     CheckFactories.registerCheck<RedundantMemberInitCheck>(
         "readability-redundant-member-init");
     CheckFactories.registerCheck<StaticDefinitionInAnonymousNamespaceCheck>(
Index: clang-tidy/readability/CMakeLists.txt
===================================================================
--- clang-tidy/readability/CMakeLists.txt
+++ clang-tidy/readability/CMakeLists.txt
@@ -17,6 +17,7 @@
   ReadabilityTidyModule.cpp
   RedundantControlFlowCheck.cpp
   RedundantDeclarationCheck.cpp
+  RedundantFunctionPtrDereferenceCheck.cpp
   RedundantMemberInitCheck.cpp
   RedundantStringCStrCheck.cpp
   RedundantSmartptrGetCheck.cpp
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to