Index: clang-tools-extra/trunk/clang-tidy/readability/CMakeLists.txt =================================================================== --- clang-tools-extra/trunk/clang-tidy/readability/CMakeLists.txt +++ clang-tools-extra/trunk/clang-tidy/readability/CMakeLists.txt @@ -17,6 +17,7 @@ ReadabilityTidyModule.cpp RedundantControlFlowCheck.cpp RedundantDeclarationCheck.cpp + RedundantFunctionPtrDereferenceCheck.cpp RedundantMemberInitCheck.cpp RedundantStringCStrCheck.cpp RedundantSmartptrGetCheck.cpp Index: clang-tools-extra/trunk/clang-tidy/readability/ReadabilityTidyModule.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/readability/ReadabilityTidyModule.cpp +++ clang-tools-extra/trunk/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( "readability-misplaced-array-index"); + CheckFactories.registerCheck( + "readability-redundant-function-ptr-dereference"); CheckFactories.registerCheck( "readability-redundant-member-init"); CheckFactories.registerCheck( Index: clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.h =================================================================== --- clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.h +++ clang-tools-extra/trunk/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-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/readability/RedundantFunctionPtrDereferenceCheck.cpp +++ clang-tools-extra/trunk/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("op"); + diag(Operator->getOperatorLoc(), + "redundant repeated dereference of function pointer") + << FixItHint::CreateRemoval(Operator->getOperatorLoc()); +} + +} // namespace readability +} // namespace tidy +} // namespace clang Index: clang-tools-extra/trunk/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/trunk/docs/ReleaseNotes.rst +++ clang-tools-extra/trunk/docs/ReleaseNotes.rst @@ -141,6 +141,11 @@ Finds redundant variable and function declarations. +- New `readability-redundant-function-ptr-dereference + `_ check + + Finds redundant function pointer dereferences. + - New `readability-redundant-member-init `_ check Index: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst =================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst +++ clang-tools-extra/trunk/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: clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-function-ptr-dereference.rst =================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-function-ptr-dereference.rst +++ clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-function-ptr-dereference.rst @@ -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); Index: clang-tools-extra/trunk/test/clang-tidy/readability-redundant-function-ptr-dereference.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/readability-redundant-function-ptr-dereference.cpp +++ clang-tools-extra/trunk/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); +}