diff --git a/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.h b/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.h --- a/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.h +++ b/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.h @@ -37,6 +37,9 @@ llvm::Optional getCheckTraversalKind() const override { return TK_IgnoreUnlessSpelledInSource; } + +private: + const std::vector IgnoredContainers; }; } // namespace readability } // namespace tidy diff --git a/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp +++ b/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"))) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/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 ` check. + + The check now skips containers that are defined in the option IgnoredContainers. The default value is ::std::array. + Removed checks ^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp +++ b/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 +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 s; + return &s[0]; +}