https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/140666

>From 7c1c0d2ca413503d81cd71e6a005a608cdec479d Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpi...@gmail.com>
Date: Mon, 19 May 2025 20:18:53 -0700
Subject: [PATCH 1/2] [clang-format] Handle raw string literals containing JSON
 code

Fix #65400
---
 clang/lib/Format/Format.cpp                   |  6 +++--
 clang/tools/clang-format/ClangFormat.cpp      |  4 ++--
 .../unittests/Format/FormatTestRawStrings.cpp | 22 +++++++++++++++++++
 3 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index b41a98ecb5be1..0cfa061681053 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3808,8 +3808,10 @@ reformat(const FormatStyle &Style, StringRef Code,
     tooling::Replacements Replaces =
         Formatter(*Env, Style, Status).process().first;
     // add a replacement to remove the "x = " from the result.
-    Replaces = Replaces.merge(
-        tooling::Replacements(tooling::Replacement(FileName, 0, 4, "")));
+    if (Code.starts_with("x = ")) {
+      Replaces = Replaces.merge(
+          tooling::Replacements(tooling::Replacement(FileName, 0, 4, "")));
+    }
     // apply the reformatting changes and the removal of "x = ".
     if (applyAllReplacements(Code, Replaces))
       return {Replaces, 0};
diff --git a/clang/tools/clang-format/ClangFormat.cpp 
b/clang/tools/clang-format/ClangFormat.cpp
index 08fe95fbe9f03..e40e04eac5472 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -491,8 +491,8 @@ static bool format(StringRef FileName, bool 
ErrorOnIncompleteFormat = false) {
   // To format JSON insert a variable to trick the code into thinking its
   // JavaScript.
   if (IsJson && !FormatStyle->DisableFormat) {
-    auto Err = Replaces.add(tooling::Replacement(
-        tooling::Replacement(AssumedFileName, 0, 0, "x = ")));
+    auto Err =
+        Replaces.add(tooling::Replacement(AssumedFileName, 0, 0, "x = "));
     if (Err)
       llvm::errs() << "Bad Json variable insertion\n";
   }
diff --git a/clang/unittests/Format/FormatTestRawStrings.cpp 
b/clang/unittests/Format/FormatTestRawStrings.cpp
index 0615fb1fad4c5..7a7aa88665554 100644
--- a/clang/unittests/Format/FormatTestRawStrings.cpp
+++ b/clang/unittests/Format/FormatTestRawStrings.cpp
@@ -988,6 +988,28 @@ fffffffffffffffffffff("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                       )pb");)test",
                    Style));
 }
+
+TEST_F(FormatTestRawStrings, Json) {
+  auto Style = getLLVMStyle();
+  Style.RawStringFormats = {
+      {
+          /*Language=*/FormatStyle::LK_Json,
+          /*Delimiters=*/{"json"},
+          /*EnclosingFunctions=*/{},
+          /*CanonicalDelimiter=*/"",
+          /*BasedOnStyle=*/"llvm",
+      },
+  };
+
+  EXPECT_EQ(R"test(json = R"json({
+                "str": "test"
+              })json";)test",
+            format(R"test(json = R"json({
+  "str": "test"
+})json";)test",
+                   Style));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang

>From 78e64442bae13cf3a62e7e449ecb12cd4fa4c0df Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpi...@gmail.com>
Date: Tue, 20 May 2025 09:58:06 -0700
Subject: [PATCH 2/2] Don't use raw string `R"test(...)test"` in the new test
 case

---
 clang/unittests/Format/FormatTestRawStrings.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang/unittests/Format/FormatTestRawStrings.cpp 
b/clang/unittests/Format/FormatTestRawStrings.cpp
index 7a7aa88665554..3f09c7b6086e5 100644
--- a/clang/unittests/Format/FormatTestRawStrings.cpp
+++ b/clang/unittests/Format/FormatTestRawStrings.cpp
@@ -1001,12 +1001,12 @@ TEST_F(FormatTestRawStrings, Json) {
       },
   };
 
-  EXPECT_EQ(R"test(json = R"json({
-                "str": "test"
-              })json";)test",
-            format(R"test(json = R"json({
-  "str": "test"
-})json";)test",
+  EXPECT_EQ("json = R\"json({\n"
+            "                \"str\": \"test\"\n"
+            "              })json\";",
+            format("json = R\"json({\n"
+                   "  \"str\": \"test\"\n"
+                   "})json\";",
                    Style));
 }
 

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to