[clang-tools-extra] 2efba0e - [clang-tidy] performance-faster-string-find string-view

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 30 08:46:17 PDT 2020


Author: Nathan James
Date: 2020-06-30T16:45:59+01:00
New Revision: 2efba0e8122aeca29f878c0e3056a4552d20c720

URL: https://github.com/llvm/llvm-project/commit/2efba0e8122aeca29f878c0e3056a4552d20c720
DIFF: https://github.com/llvm/llvm-project/commit/2efba0e8122aeca29f878c0e3056a4552d20c720.diff

LOG: [clang-tidy] performance-faster-string-find string-view

Extend the default string like classes to include `std::basic_string_view`.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D82720

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/docs/clang-tidy/checks/performance-faster-string-find.rst
    clang-tools-extra/test/clang-tidy/checkers/performance-faster-string-find.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp b/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp
index a6216b230911..0d2efb31b774 100644
--- a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp
@@ -51,7 +51,8 @@ FasterStringFindCheck::FasterStringFindCheck(StringRef Name,
                                              ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
       StringLikeClasses(utils::options::parseStringList(
-          Options.get("StringLikeClasses", "std::basic_string"))) {}
+          Options.get("StringLikeClasses",
+                      "::std::basic_string;::std::basic_string_view"))) {}
 
 void FasterStringFindCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "StringLikeClasses",

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 1ef0bdffa730..c92e18a57821 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -195,6 +195,11 @@ New check aliases
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Improved :doc:`performance-faster-string-find
+  <clang-tidy/checks/performance-faster-string-find>` check.
+
+  Now checks ``std::basic_string_view`` by default.
+
 - Improved :doc:`readability-identifier-naming
   <clang-tidy/checks/readability-identifier-naming>` check.
 

diff  --git a/clang-tools-extra/docs/clang-tidy/checks/performance-faster-string-find.rst b/clang-tools-extra/docs/clang-tidy/checks/performance-faster-string-find.rst
index 8d7265e68bee..443da597506a 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/performance-faster-string-find.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/performance-faster-string-find.rst
@@ -23,6 +23,8 @@ Options
 .. option:: StringLikeClasses
 
    Semicolon-separated list of names of string-like classes. By default only
-   ``std::basic_string`` is considered. The list of methods to consired is
-   fixed.
+   ``::std::basic_string`` and ``::std::basic_string_view`` are considered. 
+   The check will only consider member functions named ``find``, ``rfind``, 
+   ``find_first_of``, ``find_first_not_of``, ``find_last_of``, or 
+   ``find_last_not_of`` within these classes.
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/performance-faster-string-find.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance-faster-string-find.cpp
index b36e32301f5d..d26cd2c8ca30 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance-faster-string-find.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance-faster-string-find.cpp
@@ -1,7 +1,8 @@
-// RUN: %check_clang_tidy %s performance-faster-string-find %t -- \
+// RUN: %check_clang_tidy %s performance-faster-string-find %t
+// RUN: %check_clang_tidy -check-suffix=CUSTOM %s performance-faster-string-find %t -- \
 // RUN:   -config="{CheckOptions: \
 // RUN:             [{key: performance-faster-string-find.StringLikeClasses, \
-// RUN:               value: 'std::basic_string; ::llvm::StringRef;'}]}" --
+// RUN:               value: '::llvm::StringRef;'}]}"
 
 namespace std {
 template <typename Char>
@@ -17,6 +18,20 @@ struct basic_string {
 
 typedef basic_string<char> string;
 typedef basic_string<wchar_t> wstring;
+
+template <typename Char>
+struct basic_string_view {
+  int find(const Char *, int = 0) const;
+  int find(const Char *, int, int) const;
+  int rfind(const Char *) const;
+  int find_first_of(const Char *) const;
+  int find_first_not_of(const Char *) const;
+  int find_last_of(const Char *) const;
+  int find_last_not_of(const Char *) const;
+};
+
+typedef basic_string_view<char> string_view;
+typedef basic_string_view<wchar_t> wstring_view;
 }  // namespace std
 
 namespace llvm {
@@ -75,11 +90,25 @@ void StringFind() {
   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: 'find' called with a string literal
   // CHECK-FIXES: Str.find(L'\x3A9');
 
+  // std::string_view and std::wstring_view should work.
+  std::string_view StrView;
+  StrView.find("n");
+  // CHECK-MESSAGES: [[@LINE-1]]:16: warning: 'find' called with a string literal
+  // CHECK-FIXES: StrView.find('n');
+  std::wstring_view WStrView;
+
+  WStrView.find(L"n");
+  // CHECK-MESSAGES: [[@LINE-1]]:17: warning: 'find' called with a string literal
+  // CHECK-FIXES: WStrView.find(L'n');
+  WStrView.find(L"\x3A9");
+  // CHECK-MESSAGES: [[@LINE-1]]:17: warning: 'find' called with a string literal
+  // CHECK-FIXES: WStrView.find(L'\x3A9');
+
   // Also with other types, but only if it was specified in the options.
   llvm::StringRef sr;
   sr.find("x");
-  // CHECK-MESSAGES: [[@LINE-1]]:11: warning: 'find' called with a string literal
-  // CHECK-FIXES: sr.find('x');
+  // CHECK-MESSAGES-CUSTOM: [[@LINE-1]]:11: warning: 'find' called with a string literal
+  // CHECK-FIXES-CUSTOM: sr.find('x');
   NotStringRef nsr;
   nsr.find("x");
 }


        


More information about the cfe-commits mailing list