Index: clang-tidy/readability/CMakeLists.txt =================================================================== --- clang-tidy/readability/CMakeLists.txt +++ clang-tidy/readability/CMakeLists.txt @@ -18,6 +18,7 @@ NonConstParameterCheck.cpp ReadabilityTidyModule.cpp RedundantControlFlowCheck.cpp + RedundantDataCallCheck.cpp RedundantDeclarationCheck.cpp RedundantFunctionPtrDereferenceCheck.cpp RedundantMemberInitCheck.cpp Index: clang-tidy/readability/ReadabilityTidyModule.cpp =================================================================== --- clang-tidy/readability/ReadabilityTidyModule.cpp +++ clang-tidy/readability/ReadabilityTidyModule.cpp @@ -25,6 +25,7 @@ #include "NamedParameterCheck.h" #include "NonConstParameterCheck.h" #include "RedundantControlFlowCheck.h" +#include "RedundantDataCallCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" #include "RedundantMemberInitCheck.h" @@ -68,6 +69,8 @@ "readability-misleading-indentation"); CheckFactories.registerCheck( "readability-misplaced-array-index"); + CheckFactories.registerCheck( + "readability-redundant-data-call"); CheckFactories.registerCheck( "readability-redundant-function-ptr-dereference"); CheckFactories.registerCheck( Index: clang-tidy/readability/RedundantDataCallCheck.h =================================================================== --- /dev/null +++ clang-tidy/readability/RedundantDataCallCheck.h @@ -0,0 +1,38 @@ +//===--- RedundantDataCallCheck.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_REDUNDANTDATACALLCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANTDATACALLCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace readability { + +/// Find and remove redundant .data() calls. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/readability-redundant-data-call.html +class RedundantDataCallCheck : public ClangTidyCheck { +public: + RedundantDataCallCheck(StringRef Name, ClangTidyContext *Context); + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void storeOptions(ClangTidyOptions::OptionMap& Opts) override; + +private: + const std::vector Types; +}; + +} // namespace readability +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANTDATACALLCHECK_H Index: clang-tidy/readability/RedundantDataCallCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/readability/RedundantDataCallCheck.cpp @@ -0,0 +1,97 @@ +//===--- RedundantDataCallCheck.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 "RedundantDataCallCheck.h" +#include "../utils/OptionsUtils.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace readability { +namespace { +const char kDefaultTypes[] = + "::std::basic_string;::std::basic_string_view;::std::vector;::std::array"; +} // namespace + +RedundantDataCallCheck::RedundantDataCallCheck(StringRef Name, + ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), Types(utils::options::parseStringList( + Options.get("Types", kDefaultTypes))) { +} + +void RedundantDataCallCheck::registerMatchers(MatchFinder *Finder) { + using ast_matchers::allOf; + using ast_matchers::anyOf; + using ast_matchers::arraySubscriptExpr; + using ast_matchers::callee; + using ast_matchers::cxxMemberCallExpr; + using ast_matchers::cxxRecordDecl; + using ast_matchers::has; + using ast_matchers::hasAnyName; + using ast_matchers::hasBase; + using ast_matchers::hasDeclaration; + using ast_matchers::hasDescendant; + using ast_matchers::hasName; + using ast_matchers::hasType; + using ast_matchers::hasUnqualifiedDesugaredType; + using ast_matchers::ignoringParenImpCasts; + using ast_matchers::isArrow; + using ast_matchers::memberExpr; + using ast_matchers::namedDecl; + using ast_matchers::on; + using ast_matchers::pointee; + using ast_matchers::pointerType; + using ast_matchers::qualType; + using ast_matchers::recordType; + using ast_matchers::substTemplateTypeParmType; + using ast_matchers::unless; + + const auto TypesMatcher = hasUnqualifiedDesugaredType( + recordType(hasDeclaration(cxxRecordDecl(hasAnyName( + llvm::SmallVector(Types.begin(), Types.end())))))); + + Finder->addMatcher( + arraySubscriptExpr(hasBase(ignoringParenImpCasts( + cxxMemberCallExpr( + has(memberExpr().bind("member")), + on(hasType(qualType( + unless(anyOf(substTemplateTypeParmType(), + hasDescendant(substTemplateTypeParmType()))), + anyOf(TypesMatcher, pointerType(pointee(TypesMatcher)))))), + callee(namedDecl(hasName("data")))) + .bind("call")))), + this); +} + +void RedundantDataCallCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Call = Result.Nodes.getNodeAs("call"); + if (Result.Context->getSourceManager().isMacroBodyExpansion( + Call->getExprLoc())) { + return; + } + const auto *Member = Result.Nodes.getNodeAs("member"); + auto DiagBuilder = diag(Member->getMemberLoc(), "redundant call to .data()"); + if (Member->isArrow()) { + DiagBuilder << FixItHint::CreateInsertion(Member->getLocStart(), "(*") + << FixItHint::CreateInsertion(Member->getOperatorLoc(), ")"); + } + DiagBuilder << FixItHint::CreateRemoval( + {Member->getOperatorLoc(), Call->getLocEnd()}); +} + +void RedundantDataCallCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "Types", utils::options::serializeStringList(Types)); +} + +} // namespace readability +} // namespace tidy +} // namespace clang Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -57,6 +57,11 @@ Improvements to clang-tidy -------------------------- +- New :doc:`readability-redundant-data-call + ` check + + This check suggests removing redundant `.data()` calls. + - New module `abseil` for checks related to the `Abseil `_ library. Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -91,8 +91,8 @@ cppcoreguidelines-pro-type-vararg cppcoreguidelines-slicing cppcoreguidelines-special-member-functions - fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-default-arguments + fuchsia-header-anon-namespaces (redirects to google-build-namespaces) fuchsia-multiple-inheritance fuchsia-overloaded-operator fuchsia-statically-constructed-objects @@ -218,6 +218,7 @@ readability-named-parameter readability-non-const-parameter readability-redundant-control-flow + readability-redundant-data-call readability-redundant-declaration readability-redundant-function-ptr-dereference readability-redundant-member-init Index: docs/clang-tidy/checks/readability-redundant-data-call.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/readability-redundant-data-call.rst @@ -0,0 +1,14 @@ +.. title:: clang-tidy - readability-redundant-data-call + +readability-redundant-data-call +=============================== + +This check suggests removing redundant `.data()` calls. + +Examples: + +.. code-block:: c++ + std::string s = ...; + char c = s.data()[i]; // char c = s[i]; + +The type(s) that triggers this check can be configured with `Types` option. Index: test/clang-tidy/readability-redundant-data-call.cpp =================================================================== --- /dev/null +++ test/clang-tidy/readability-redundant-data-call.cpp @@ -0,0 +1,108 @@ +// RUN: %check_clang_tidy %s readability-redundant-data-call %t \ +// RUN: -config="{CheckOptions: \ +// RUN: [{key: readability-redundant-data-call.Types, \ +// RUN: value: '::std::basic_string;::std::basic_string_view;MyVector'}]}" -- + +namespace std { + +template +class basic_string { + public: + using size_type = unsigned; + using value_type = T; + using reference = value_type&; + using const_reference = const value_type&; + + reference operator[](size_type); + const_reference operator[](size_type) const; + T* data(); + const T* data() const; +}; + +using string = basic_string; + +template +class basic_string_view { + public: + using size_type = unsigned; + using const_reference = const T&; + using const_pointer = const T*; + + constexpr const_reference operator[](size_type) const; + constexpr const_pointer data() const noexcept; +}; + +using string_view = basic_string_view; + +} + +template +class MyVector { + public: + using size_type = unsigned; + using const_reference = const T&; + using const_pointer = const T*; + + const_reference operator[](size_type) const; + const T* data() const noexcept; +}; + +#define DO(x) do { x; } while (false) +#define ACCESS(x) (x) +#define GET(x, i) (x).data()[i] + +template +class Foo { + public: + char bar(int i) { + return x.data()[i]; + } + private: + T x; +}; + +void f(int i) { + MyVector v; + int x = v.data()[i]; + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant call to .data() [readability-redundant-data-call] + // CHECK-FIXES: int x = v[i]; + + std::string s; + char c1 = s.data()[i]; + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: redundant call to .data() + // CHECK-FIXES: char c1 = s[i]; + + std::string_view sv; + char c2 = sv.data()[i]; + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: redundant call to .data() + // CHECK-FIXES: char c2 = sv[i]; + + std::string* ps = &s; + char c3 = ps->data()[i]; + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant call to .data() + // CHECK-FIXES: char c3 = (*ps)[i]; + + char c4 = (*ps).data()[i]; + // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: redundant call to .data() + // CHECK-FIXES: char c4 = (*ps)[i]; + + DO(char c5 = s.data()[i]); + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: redundant call to .data() + // CHECK-FIXES: DO(char c5 = s[i]); + + char c6 = ACCESS(s).data()[i]; + // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: redundant call to .data() + // CHECK-FIXES: char c6 = ACCESS(s)[i]; + + char c7 = ACCESS(s.data())[i]; + // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant call to .data() + // CHECK-FIXES: char c7 = ACCESS(s)[i]; + + char c8 = ACCESS(s.data()[i]); + // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant call to .data() + // CHECK-FIXES: char c8 = ACCESS(s[i]); + + char c9 = GET(s, i); + + char c10 = Foo{}.bar(i); +}