Skip to content

release/20.x: [clang-format] Handle raw string literals containing JSON code (#140666) #141002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2025

Conversation

llvmbot
Copy link
Member

@llvmbot llvmbot commented May 22, 2025

Backport 0dfdf7e

Requested by: @owenca

@llvmbot
Copy link
Member Author

llvmbot commented May 22, 2025

@mydeveloperday What do you think about merging this PR to the release branch?

@llvmbot
Copy link
Member Author

llvmbot commented May 22, 2025

@llvm/pr-subscribers-clang-format

Author: None (llvmbot)

Changes

Backport 0dfdf7e

Requested by: @owenca


Full diff: https://github.com/llvm/llvm-project/pull/141002.diff

3 Files Affected:

  • (modified) clang/lib/Format/Format.cpp (+4-2)
  • (modified) clang/tools/clang-format/ClangFormat.cpp (+2-2)
  • (modified) clang/unittests/Format/FormatTestRawStrings.cpp (+22)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index b97d8928178b5..aba7db6dd50a8 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3743,8 +3743,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 28610052b9b74..96eec7c666a38 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -492,8 +492,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..3f09c7b6086e5 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("json = R\"json({\n"
+            "                \"str\": \"test\"\n"
+            "              })json\";",
+            format("json = R\"json({\n"
+                   "  \"str\": \"test\"\n"
+                   "})json\";",
+                   Style));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang

@mydeveloperday
Copy link
Contributor

I'm good with merging

@github-project-automation github-project-automation bot moved this from Needs Triage to Needs Merge in LLVM Release Status May 22, 2025
@tstellar tstellar merged commit 802f4f7 into llvm:release/20.x May 24, 2025
6 of 8 checks passed
@github-project-automation github-project-automation bot moved this from Needs Merge to Done in LLVM Release Status May 24, 2025
Copy link

@owenca (or anyone else). If you would like to add a note about this fix in the release notes (completely optional). Please reply to this comment with a one or two sentence description of the fix. When you are done, please add the release:note label to this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

4 participants