[clang-tools-extra] r334270 - [clang-tidy] Improve string type matcher for abseil-string-find-starts-with check.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 8 01:19:22 PDT 2018
Author: hokein
Date: Fri Jun 8 01:19:22 2018
New Revision: 334270
URL: http://llvm.org/viewvc/llvm-project?rev=334270&view=rev
Log:
[clang-tidy] Improve string type matcher for abseil-string-find-starts-with check.
Summary:
This patch improves the check to match the desugared "string" type (so that it
can handle custom-implemented string classes), see the newly-added test.
Reviewers: alexfh
Subscribers: klimek, xazax.hun, cfe-commits
Differential Revision: https://reviews.llvm.org/D47704
Modified:
clang-tools-extra/trunk/clang-tidy/abseil/StringFindStartswithCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/abseil-string-find-startswith.cpp
Modified: clang-tools-extra/trunk/clang-tidy/abseil/StringFindStartswithCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/StringFindStartswithCheck.cpp?rev=334270&r1=334269&r2=334270&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/abseil/StringFindStartswithCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/StringFindStartswithCheck.cpp Fri Jun 8 01:19:22 2018
@@ -36,10 +36,13 @@ void StringFindStartswithCheck::register
auto ZeroLiteral = integerLiteral(equals(0));
auto StringClassMatcher = cxxRecordDecl(hasAnyName(SmallVector<StringRef, 4>(
StringLikeClasses.begin(), StringLikeClasses.end())));
+ auto StringType = hasUnqualifiedDesugaredType(
+ recordType(hasDeclaration(StringClassMatcher)));
auto StringFind = cxxMemberCallExpr(
// .find()-call on a string...
- callee(cxxMethodDecl(hasName("find"), ofClass(StringClassMatcher))),
+ callee(cxxMethodDecl(hasName("find"))),
+ on(hasType(StringType)),
// ... with some search expression ...
hasArgument(0, expr().bind("needle")),
// ... and either "0" as second argument or the default argument (also 0).
Modified: clang-tools-extra/trunk/test/clang-tidy/abseil-string-find-startswith.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/abseil-string-find-startswith.cpp?rev=334270&r1=334269&r2=334270&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/abseil-string-find-startswith.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/abseil-string-find-startswith.cpp Fri Jun 8 01:19:22 2018
@@ -1,4 +1,6 @@
-// RUN: %check_clang_tidy %s abseil-string-find-startswith %t
+// RUN: %check_clang_tidy %s abseil-string-find-startswith %t -- \
+// RUN: -config="{CheckOptions: [{key: 'abseil-string-find-startswith.StringLikeClasses', value: '::std::basic_string;::basic_string'}]}" \
+// RUN: -- -std=c++11
namespace std {
template <typename T> class allocator {};
@@ -15,14 +17,23 @@ struct basic_string {
};
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
+
+struct cxx_string {
+ int find(const char *s, int pos = 0);
+};
} // namespace std
+struct basic_string : public std::cxx_string {
+ basic_string();
+};
+typedef basic_string global_string;
+
std::string foo(std::string);
std::string bar();
#define A_MACRO(x, y) ((x) == (y))
-void tests(std::string s) {
+void tests(std::string s, global_string s2) {
s.find("a") == 0;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith instead of find() == 0 [abseil-string-find-startswith]
// CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(s, "a");{{$}}
@@ -47,6 +58,10 @@ void tests(std::string s) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use !absl::StartsWith
// CHECK-FIXES: {{^[[:space:]]*}}!absl::StartsWith(s, "a");{{$}}
+ s2.find("a") == 0;
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use absl::StartsWith
+ // CHECK-FIXES: {{^[[:space:]]*}}absl::StartsWith(s2, "a");{{$}}
+
// expressions that don't trigger the check are here.
A_MACRO(s.find("a"), 0);
s.find("a", 1) == 0;
More information about the cfe-commits
mailing list