felix642 updated this revision to Diff 529485.
felix642 added a comment.
Improved documentation
Removed duplicated messages in tests.
Added support for regular expressions
Added method to store options.
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/docs/clang-tidy/checks/readability/container-data-pointer.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-suffixes=,CLASSIC %s readability-container-data-pointer %t -- -- -fno-delayed-template-parsing
+// RUN: %check_clang_tidy -check-suffixes=,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;
@@ -65,13 +67,15 @@
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,
Index: clang-tools-extra/docs/clang-tidy/checks/readability/container-data-pointer.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/readability/container-data-pointer.rst
+++ clang-tools-extra/docs/clang-tidy/checks/readability/container-data-pointer.rst
@@ -11,3 +11,10 @@
This also ensures that in the case that the container is empty, the data pointer
access does not perform an errant memory access.
+
+Options
+-------
+
+.. option:: IgnoredContainers
+
+ Semicolon-separated list of containers regexp for which container-data-pointer check won't be enforced. Default is `empty`.
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -176,6 +176,10 @@
The check now skips concept definitions since redundant expressions still make sense
inside them.
+- 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`.
+
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
@@ -30,6 +30,7 @@
return LO.CPlusPlus11;
}
+ void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
@@ -37,6 +38,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,8 @@
#include "ContainerDataPointerCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/StringRef.h"
@@ -23,13 +25,22 @@
"addr-of-container-expr";
constexpr llvm::StringLiteral AddressOfName = "address-of";
+void ContainerDataPointerCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "IgnoredContainers",
+ utils::options::serializeStringList(IgnoredContainers));
+}
+
ContainerDataPointerCheck::ContainerDataPointerCheck(StringRef Name,
ClangTidyContext *Context)
- : ClangTidyCheck(Name, Context) {}
+ : ClangTidyCheck(Name, Context),
+ IgnoredContainers(utils::options::parseStringList(
+ Options.get("IgnoredContainers", ""))) {}
void ContainerDataPointerCheck::registerMatchers(MatchFinder *Finder) {
const auto Record =
cxxRecordDecl(
+ unless(matchers::matchesAnyListedName(IgnoredContainers)),
isSameOrDerivedFrom(
namedDecl(
has(cxxMethodDecl(isPublic(), hasName("data")).bind("data")))
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits