[PATCH] D157436: [clang-tidy] `performance-faster-string-find` generates incorrect fixes for single quote character literals

Fabian Wolff via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 8 13:22:31 PDT 2023


fwolff created this revision.
fwolff added reviewers: PiotrZSL, carlosgalvezp.
fwolff added a project: clang-tools-extra.
Herald added a subscriber: xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
fwolff requested review of this revision.
Herald added a subscriber: cfe-commits.

`"'"` is turned into `'''` but should be turned into `'\''`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157436

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


Index: clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp
@@ -56,13 +56,21 @@
   // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
   // CHECK-FIXES: Str.find('a', 1);
 
-  // Doens't work with strings smaller or larger than 1 char.
+  // Doesn't work with strings smaller or larger than 1 char.
   Str.find("");
   Str.find("ab");
 
   // Doesn't do anything with the 3 argument overload.
   Str.find("a", 1, 1);
 
+  // Single quotes are escaped properly
+  Str.find("'");
+  // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
+  // CHECK-FIXES: Str.find('\'');
+  Str.find("\'");
+  // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal
+  // CHECK-FIXES: Str.find('\'');
+
   // Other methods that can also be replaced
   Str.rfind("a");
   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: 'rfind' called with a string literal
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -196,6 +196,10 @@
   <clang-tidy/checks/readability/identifier-naming>` check to emit proper
   warnings when a type forward declaration precedes its definition.
 
+- Fixed a bug in :doc:`performance-faster-string-find
+  <performance-faster-string-find>` which generated incorrect fixes by not
+  escaping single quotes properly.
+
 Removed checks
 ^^^^^^^^^^^^^^
 
Index: clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp
+++ clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp
@@ -26,14 +26,20 @@
     Literal->outputString(OS);
   }
   // Now replace the " with '.
-  auto Pos = Result.find_first_of('"');
-  if (Pos == Result.npos)
+  auto OpenPos = Result.find_first_of('"');
+  if (OpenPos == Result.npos)
     return std::nullopt;
-  Result[Pos] = '\'';
-  Pos = Result.find_last_of('"');
-  if (Pos == Result.npos)
+  Result[OpenPos] = '\'';
+
+  auto ClosePos = Result.find_last_of('"');
+  if (ClosePos == Result.npos)
     return std::nullopt;
-  Result[Pos] = '\'';
+  Result[ClosePos] = '\'';
+
+  // "'" is OK, but ''' is not, so add a backslash
+  if (ClosePos - OpenPos == 2 && Result[OpenPos + 1] == '\'')
+    Result.replace(OpenPos + 1, 1, "\\'");
+
   return Result;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157436.548326.patch
Type: text/x-patch
Size: 2740 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230808/d8f9ed5a/attachment-0001.bin>


More information about the cfe-commits mailing list