forked from OSchip/llvm-project
[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 llvm-svn: 289524
This commit is contained in:
parent
ac75bca1eb
commit
e7be4a004b
|
@ -17,6 +17,7 @@ add_clang_library(clangTidyReadabilityModule
|
|||
ReadabilityTidyModule.cpp
|
||||
RedundantControlFlowCheck.cpp
|
||||
RedundantDeclarationCheck.cpp
|
||||
RedundantFunctionPtrDereferenceCheck.cpp
|
||||
RedundantMemberInitCheck.cpp
|
||||
RedundantStringCStrCheck.cpp
|
||||
RedundantSmartptrGetCheck.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 @@ 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>(
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue