[PATCH] D133244: [clang-tidy] Readability-container-data-pointer adds new option to ignore Containers

FĂ©lix-Antoine Constantin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 3 20:55:38 PDT 2022


felix642 updated this revision to Diff 457828.
felix642 added a comment.

+ Added test case and updated ReleaseNotes


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")))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133244.457828.patch
Type: text/x-patch
Size: 3862 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220904/7a8e72e0/attachment-0001.bin>


More information about the cfe-commits mailing list