[llvm-branch-commits] [clang] release/20.x: [clang-format] Handle raw string literals containing JSON code (#140666) (PR #141002)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed May 21 21:11:28 PDT 2025
https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/141002
Backport 0dfdf7efbfe347517eb4c7f544043a71af4e4a25
Requested by: @owenca
>From 3d02c7ce3c8b4c397c3237ed3d5069c829a1808e Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 20 May 2025 19:15:57 -0700
Subject: [PATCH] [clang-format] Handle raw string literals containing JSON
code (#140666)
Fix #65400
(cherry picked from commit 0dfdf7efbfe347517eb4c7f544043a71af4e4a25)
---
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 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
More information about the llvm-branch-commits
mailing list