[clang] [clang-format] Parse JSON outermost l_brace as braced list brace (PR #143327)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 8 15:40:29 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Fix github.com/llvm/llvm-project/issues/65400#issuecomment-2922181979
---
Full diff: https://github.com/llvm/llvm-project/pull/143327.diff
4 Files Affected:
- (modified) clang/lib/Format/ContinuationIndenter.cpp (-1)
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+1-1)
- (modified) clang/unittests/Format/FormatTestRawStrings.cpp (+2)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+14)
``````````diff
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 4e4e48f90a89f..424b6dbc0da79 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -2248,7 +2248,6 @@ unsigned ContinuationIndenter::reformatRawStringLiteral(
/*Status=*/nullptr);
auto NewCode = applyAllReplacements(RawText, Fixes.first);
- tooling::Replacements NoFixes;
if (!NewCode)
return addMultilineToken(Current, State);
if (!DryRun) {
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 4acfe0cc50c25..61b84126fe1b9 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -269,7 +269,7 @@ void UnwrappedLineParser::parseFile() {
bool MustBeDeclaration = !Line->InPPDirective && !Style.isJavaScript();
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
MustBeDeclaration);
- if (Style.isTextProto())
+ if (Style.isTextProto() || (Style.isJson() && FormatTok->IsFirst))
parseBracedList();
else
parseLevel();
diff --git a/clang/unittests/Format/FormatTestRawStrings.cpp b/clang/unittests/Format/FormatTestRawStrings.cpp
index 3f09c7b6086e5..5e8737d65666a 100644
--- a/clang/unittests/Format/FormatTestRawStrings.cpp
+++ b/clang/unittests/Format/FormatTestRawStrings.cpp
@@ -1002,9 +1002,11 @@ TEST_F(FormatTestRawStrings, Json) {
};
EXPECT_EQ("json = R\"json({\n"
+ " \"foo\": \"bar\",\n"
" \"str\": \"test\"\n"
" })json\";",
format("json = R\"json({\n"
+ " \"foo\": \"bar\",\n"
" \"str\": \"test\"\n"
"})json\";",
Style));
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index d64e34de1fcf4..873c6c492d18c 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -4105,6 +4105,20 @@ TEST_F(TokenAnnotatorTest, BitFieldColon) {
EXPECT_TOKEN(Tokens[5], tok::colon, TT_BitFieldColon);
}
+TEST_F(TokenAnnotatorTest, JsonCodeInRawString) {
+ auto Tokens = annotate("{\n"
+ " \"foo\": \"bar\",\n"
+ " \"str\": \"test\"\n"
+ "}",
+ getLLVMStyle(FormatStyle::LK_Json));
+ ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+ EXPECT_TOKEN(Tokens[0], tok::l_brace, TT_DictLiteral);
+ EXPECT_TOKEN(Tokens[1], tok::string_literal, TT_SelectorName);
+ EXPECT_TOKEN(Tokens[2], tok::colon, TT_DictLiteral);
+ EXPECT_TOKEN(Tokens[5], tok::string_literal, TT_SelectorName);
+ EXPECT_TOKEN(Tokens[6], tok::colon, TT_DictLiteral);
+}
+
} // namespace
} // namespace format
} // namespace clang
``````````
</details>
https://github.com/llvm/llvm-project/pull/143327
More information about the cfe-commits
mailing list