felix642 updated this revision to Diff 457829. felix642 added a comment. Fixed compilation issues
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,4 @@ -// RUN: %check_clang_tidy %s readability-container-data-pointer %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy %s readability-container-data-pointer %t -- -config="{CheckOptions: [{key: readability-container-data-pointer.IgnoredContainers, value: '::ArrayType'}]}" -- -fno-delayed-template-parsing typedef __SIZE_TYPE__ size_t; @@ -155,3 +155,25 @@ // 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();{{$}} } + +template<typename T, size_t N> +struct ArrayType { + using size_type = size_t; + + ArrayType(); + + T *data(); + const T *data() const; + + T &operator[](size_type); + const T &operator[](size_type) const; + +private: + T _value[N]; +}; + +// Don't issue a warning because of the config above. +int *s() { + ArrayType<int, 2> s; + return &s[0]; +} 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")))
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,4 @@ -// RUN: %check_clang_tidy %s readability-container-data-pointer %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy %s readability-container-data-pointer %t -- -config="{CheckOptions: [{key: readability-container-data-pointer.IgnoredContainers, value: '::ArrayType'}]}" -- -fno-delayed-template-parsing typedef __SIZE_TYPE__ size_t; @@ -155,3 +155,25 @@ // 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();{{$}} } + +template<typename T, size_t N> +struct ArrayType { + using size_type = size_t; + + ArrayType(); + + T *data(); + const T *data() const; + + T &operator[](size_type); + const T &operator[](size_type) const; + +private: + T _value[N]; +}; + +// Don't issue a warning because of the config above. +int *s() { + ArrayType<int, 2> s; + return &s[0]; +} 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