[PATCH] D47704: [clang-tidy] Improve string type matcher for abseil-string-find-starts-with check.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 4 04:31:20 PDT 2018


hokein created this revision.
hokein added a reviewer: alexfh.
Herald added subscribers: xazax.hun, klimek.

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.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D47704

Files:
  clang-tidy/abseil/StringFindStartswithCheck.cpp
  test/clang-tidy/abseil-string-find-startswith.cpp


Index: test/clang-tidy/abseil-string-find-startswith.cpp
===================================================================
--- test/clang-tidy/abseil-string-find-startswith.cpp
+++ test/clang-tidy/abseil-string-find-startswith.cpp
@@ -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 @@
 };
 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 @@
   // 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;
Index: clang-tidy/abseil/StringFindStartswithCheck.cpp
===================================================================
--- clang-tidy/abseil/StringFindStartswithCheck.cpp
+++ clang-tidy/abseil/StringFindStartswithCheck.cpp
@@ -36,10 +36,13 @@
   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).


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47704.149720.patch
Type: text/x-patch
Size: 2669 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180604/f10c8dfa/attachment.bin>


More information about the cfe-commits mailing list