[clang-tools-extra] r289524 - [clang-tidy] Add check for redundant function pointer dereferences
Malcolm Parsons via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 13 00:04:12 PST 2016
Author: malcolm.parsons
Date: Tue Dec 13 02:04:11 2016
New Revision: 289524
URL: http://llvm.org/viewvc/llvm-project?rev=289524&view=rev
Log:
[clang-tidy] Add check for redundant function pointer dereferences
Reviewers: alexfh, aaron.ballman, hokein
Subscribers: mgorny, JDevlieghere, cfe-commits
Differential Revision: https://reviews.llvm.org/D27520
Added:
clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.cpp
clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.h
clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-function-ptr-dereference.rst
clang-tools-extra/trunk/test/clang-tidy/readability-redundant-function-ptr-dereference.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/readability/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/readability/ReadabilityTidyModule.cpp
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
Modified: clang-tools-extra/trunk/clang-tidy/readability/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/CMakeLists.txt?rev=289524&r1=289523&r2=289524&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/CMakeLists.txt Tue Dec 13 02:04:11 2016
@@ -17,6 +17,7 @@ add_clang_library(clangTidyReadabilityMo
ReadabilityTidyModule.cpp
RedundantControlFlowCheck.cpp
RedundantDeclarationCheck.cpp
+ RedundantFunctionPtrDereferenceCheck.cpp
RedundantMemberInitCheck.cpp
RedundantStringCStrCheck.cpp
RedundantSmartptrGetCheck.cpp
Modified: clang-tools-extra/trunk/clang-tidy/readability/ReadabilityTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/ReadabilityTidyModule.cpp?rev=289524&r1=289523&r2=289524&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/ReadabilityTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/ReadabilityTidyModule.cpp Tue Dec 13 02:04:11 2016
@@ -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 @@ public:
"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>(
Added: clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.cpp?rev=289524&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.cpp Tue Dec 13 02:04:11 2016
@@ -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
Added: clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.h?rev=289524&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.h Tue Dec 13 02:04:11 2016
@@ -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
Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=289524&r1=289523&r2=289524&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Tue Dec 13 02:04:11 2016
@@ -141,6 +141,11 @@ Improvements to clang-tidy
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
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst?rev=289524&r1=289523&r2=289524&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Tue Dec 13 02:04:11 2016
@@ -138,6 +138,7 @@ Clang-Tidy Checks
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
Added: clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-function-ptr-dereference.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-function-ptr-dereference.rst?rev=289524&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-function-ptr-dereference.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-function-ptr-dereference.rst Tue Dec 13 02:04:11 2016
@@ -0,0 +1,24 @@
+.. 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);
Added: clang-tools-extra/trunk/test/clang-tidy/readability-redundant-function-ptr-dereference.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-redundant-function-ptr-dereference.cpp?rev=289524&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-redundant-function-ptr-dereference.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-redundant-function-ptr-dereference.cpp Tue Dec 13 02:04:11 2016
@@ -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);
+}
More information about the cfe-commits
mailing list