[PATCH] D28667: [clang-tidy] Don't modernize-raw-string-literal if replacement is longer.

AndrĂ¡s Leitereg via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 13 02:14:28 PST 2017


leanil created this revision.
leanil added reviewers: alexfh, LegalizeAdulthood, xazax.hun.
leanil added a subscriber: cfe-commits.
leanil added a project: clang-tools-extra.
Herald added a subscriber: JDevlieghere.

modernize-raw-string-literal suggests to replace e.g.:

  std::string s{ "foo \"" } with std::string s{R"(foo ")" };
  std::string s{ ":)\"" }   with std::string s{R"lit(:)")lit" };

which are not necessarily improvements.
Maybe it should only suggest replacements, that are not longer than the original.

Fixes PR30964


https://reviews.llvm.org/D28667

Files:
  clang-tidy/modernize/RawStringLiteralCheck.cpp
  clang-tidy/modernize/RawStringLiteralCheck.h
  test/clang-tidy/modernize-raw-string-literal.cpp


Index: test/clang-tidy/modernize-raw-string-literal.cpp
===================================================================
--- test/clang-tidy/modernize-raw-string-literal.cpp
+++ test/clang-tidy/modernize-raw-string-literal.cpp
@@ -91,6 +91,11 @@
 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: {{.*}} can be written as a raw string literal
 // CHECK-FIXES: {{^}}char const *const HexPrintable(R"(@\)");{{$}}
 
+char const *const Concatenated("\"foo\""
+                               "\"bar\"");
+// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: {{.*}} can be written as a raw string literal
+// CHECK-FIXES: {{^}}char const *const Concatenated(R"("foo""bar")");{{$}}
+
 char const *const prettyFunction(__PRETTY_FUNCTION__);
 char const *const function(__FUNCTION__);
 char const *const func(__func__);
Index: clang-tidy/modernize/RawStringLiteralCheck.h
===================================================================
--- clang-tidy/modernize/RawStringLiteralCheck.h
+++ clang-tidy/modernize/RawStringLiteralCheck.h
@@ -32,7 +32,7 @@
 private:
   void replaceWithRawStringLiteral(
       const ast_matchers::MatchFinder::MatchResult &Result,
-      const StringLiteral *Literal);
+      const StringLiteral *Literal, StringRef Replacement);
 
   std::string DelimiterStem;
 };
Index: clang-tidy/modernize/RawStringLiteralCheck.cpp
===================================================================
--- clang-tidy/modernize/RawStringLiteralCheck.cpp
+++ clang-tidy/modernize/RawStringLiteralCheck.cpp
@@ -121,19 +121,24 @@
   if (Literal->getLocStart().isMacroID())
     return;
 
-  if (containsEscapedCharacters(Result, Literal))
-    replaceWithRawStringLiteral(Result, Literal);
+  if (containsEscapedCharacters(Result, Literal)) {
+    std::string Replacement = asRawStringLiteral(Literal, DelimiterStem);
+    if (Replacement.length() <=
+        Lexer::MeasureTokenLength(Literal->getLocStart(), *Result.SourceManager,
+                                  getLangOpts()))
+      replaceWithRawStringLiteral(Result, Literal, Replacement);
+  }
 }
 
 void RawStringLiteralCheck::replaceWithRawStringLiteral(
-    const MatchFinder::MatchResult &Result, const StringLiteral *Literal) {
+    const MatchFinder::MatchResult &Result, const StringLiteral *Literal,
+    StringRef Replacement) {
   CharSourceRange CharRange = Lexer::makeFileCharRange(
       CharSourceRange::getTokenRange(Literal->getSourceRange()),
       *Result.SourceManager, getLangOpts());
   diag(Literal->getLocStart(),
        "escaped string literal can be written as a raw string literal")
-      << FixItHint::CreateReplacement(
-             CharRange, asRawStringLiteral(Literal, DelimiterStem));
+      << FixItHint::CreateReplacement(CharRange, Replacement);
 }
 
 } // namespace modernize


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28667.84258.patch
Type: text/x-patch
Size: 2775 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170113/8df5d4ff/attachment-0001.bin>


More information about the cfe-commits mailing list