felix642 updated this revision to Diff 458943. felix642 added a comment. Changed tests to check with and without config.
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D133244/new/ https://reviews.llvm.org/D133244 Files: clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.h clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp +++ clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp @@ -1,4 +1,6 @@ -// RUN: %check_clang_tidy %s readability-container-data-pointer %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy -check-suffix=CLASSIC %s readability-container-data-pointer %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy -check-suffix=WITH-CONFIG %s readability-container-data-pointer %t -- -config="{CheckOptions: [{key: readability-container-data-pointer.IgnoredContainers, value: '::std::basic_string'}]}" -- -fno-delayed-template-parsing + typedef __SIZE_TYPE__ size_t; @@ -58,20 +60,24 @@ void g(size_t s) { std::vector<unsigned char> b(s); f(&((b)[(z)])); - // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] - // CHECK-FIXES: {{^ }}f(b.data());{{$}} + // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-CLASSIC: {{^ }}f(b.data());{{$}} + // CHECK-MESSAGES-WITH-CONFIG: :[[@LINE-3]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-WITH-CONFIG: {{^ }}f(b.data());{{$}} } void h() { std::string s; f(&((s).operator[]((z)))); - // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] - // CHECK-FIXES: {{^ }}f(s.data());{{$}} + // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-CLASSIC: {{^ }}f(s.data());{{$}} + // CHECK-MESSAGES-WITH-CONFIG-NOT: :[[@LINE-3]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] std::wstring w; f(&((&(w))->operator[]((z)))); - // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] - // CHECK-FIXES: {{^ }}f(w.data());{{$}} + // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-CLASSIC: {{^ }}f(w.data());{{$}} + // CHECK-MESSAGES-WITH-CONFIG-NOT: :[[@LINE-3]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] } template <typename T, typename U, @@ -79,35 +85,44 @@ void i(U s) { std::vector<T> b(s); f(&b[0]); - // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] - // CHECK-FIXES: {{^ }}f(b.data());{{$}} + // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-CLASSIC: {{^ }}f(b.data());{{$}} + // CHECK-MESSAGES-WITH-CONFIG: :[[@LINE-3]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-WITH-CONFIG: {{^ }}f(b.data());{{$}} } template void i<unsigned char, size_t>(size_t); void j(std::vector<unsigned char> * const v) { f(&(*v)[0]); - // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] - // CHECK-FIXES: {{^ }}f(v->data());{{$}} + // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-CLASSIC: {{^ }}f(v->data());{{$}} + // CHECK-MESSAGES-WITH-CONFIG: :[[@LINE-3]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-WITH-CONFIG: {{^ }}f(v->data());{{$}} } void k(const std::vector<unsigned char> &v) { f(&v[0]); - // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] - // CHECK-FIXES: {{^ }}f(v.data());{{$}} + // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-CLASSIC: {{^ }}f(v.data());{{$}} + // CHECK-MESSAGES-WITH-CONFIG: :[[@LINE-3]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-WITH-CONFIG: {{^ }}f(v.data());{{$}} } void l() { unsigned char b[32]; f(&b[0]); - // CHECK-MESSAGES-NOT: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-MESSAGES-CLASSIC-NOT: :[[@LINE-1]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-MESSAGES-WITH-CONFIG-NOT: :[[@LINE-2]]:5: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] } template <typename T> void m(const std::vector<T> &v) { return &v[0]; - // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] - // CHECK-FIXES: {{^ }}return v.data();{{$}} + // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-CLASSIC: {{^ }}return v.data();{{$}} + // CHECK-MESSAGES-WITH-CONFIG: :[[@LINE-3]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-WITH-CONFIG: {{^ }}return v.data();{{$}} } template <typename T> @@ -125,8 +140,10 @@ const int *o(const std::vector<std::vector<std::vector<int>>> &v, const size_t idx1, const size_t idx2) { return &v[idx1][idx2][0]; - // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] - // CHECK-FIXES: {{^ }}return v[idx1][idx2].data();{{$}} + // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-CLASSIC: {{^ }}return v[idx1][idx2].data();{{$}} + // CHECK-MESSAGES-WITH-CONFIG: :[[@LINE-3]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-WITH-CONFIG: {{^ }}return v[idx1][idx2].data();{{$}} } std::vector<int> &select(std::vector<int> &u, std::vector<int> &v) { @@ -135,14 +152,18 @@ int *p(std::vector<int> &v1, std::vector<int> &v2) { return &select(*&v1, v2)[0]; - // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] - // CHECK-FIXES: {{^ }}return select(*&v1, v2).data();{{$}} + // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-CLASSIC: {{^ }}return select(*&v1, v2).data();{{$}} + // CHECK-MESSAGES-WITH-CONFIG: :[[@LINE-3]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-WITH-CONFIG: {{^ }}return select(*&v1, v2).data();{{$}} } int *q(std::vector<int> ***v) { return &(***v)[0]; - // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] - // CHECK-FIXES: {{^ }}return (**v)->data();{{$}} + // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-CLASSIC: {{^ }}return (**v)->data();{{$}} + // CHECK-MESSAGES-WITH-CONFIG: :[[@LINE-3]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-WITH-CONFIG: {{^ }}return (**v)->data();{{$}} } struct VectorHolder { @@ -152,6 +173,8 @@ int *r() { VectorHolder holder; return &holder.v[0]; - // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] - // CHECK-FIXES: {{^ }}return holder.v.data();{{$}} + // CHECK-MESSAGES-CLASSIC: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-CLASSIC: {{^ }}return holder.v.data();{{$}} + // CHECK-MESSAGES-WITH-CONFIG: :[[@LINE-3]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer] + // CHECK-FIXES-WITH-CONFIG: {{^ }}return holder.v.data();{{$}} } Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -141,6 +141,10 @@ The check now skips unions since in this case a default constructor with empty body is not equivalent to the explicitly defaulted one. +- Improved `readability-container-data-pointer <clang-tidy/checks/readability/container-data-pointer>` check. + + The check now skips containers that are defined in the option `IgnoredContainers`. The default value is `::std::array`. + Removed checks ^^^^^^^^^^^^^^ Index: clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.h =================================================================== --- clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.h +++ clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.h @@ -37,6 +37,9 @@ llvm::Optional<TraversalKind> getCheckTraversalKind() const override { return TK_IgnoreUnlessSpelledInSource; } + +private: + const std::vector<StringRef> IgnoredContainers; }; } // namespace readability } // namespace tidy Index: clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp +++ clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp @@ -8,6 +8,7 @@ #include "ContainerDataPointerCheck.h" +#include "../utils/OptionsUtils.h" #include "clang/Lex/Lexer.h" #include "llvm/ADT/StringRef.h" @@ -22,14 +23,18 @@ constexpr llvm::StringLiteral AddrOfContainerExprName = "addr-of-container-expr"; constexpr llvm::StringLiteral AddressOfName = "address-of"; +const auto DefaultIgnoredContainers = "::std::array"; ContainerDataPointerCheck::ContainerDataPointerCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context), + IgnoredContainers(utils::options::parseStringList( + Options.get("IgnoredContainers", DefaultIgnoredContainers))) {} void ContainerDataPointerCheck::registerMatchers(MatchFinder *Finder) { const auto Record = cxxRecordDecl( + unless(hasAnyName(IgnoredContainers)), isSameOrDerivedFrom( namedDecl( has(cxxMethodDecl(isPublic(), hasName("data")).bind("data")))
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits