https://github.com/owenca created https://github.com/llvm/llvm-project/pull/143327
Fix github.com/llvm/llvm-project/issues/65400#issuecomment-2922181979 >From ab36d65d7c0f677b196ac3aa0cf976fb483fad98 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Sun, 1 Jun 2025 22:35:14 -0700 Subject: [PATCH] [clang-format] Parse JSON outermost l_brace as braced list brace Fix github.com/llvm/llvm-project/issues/65400#issuecomment-2922181979 --- clang/lib/Format/ContinuationIndenter.cpp | 1 - clang/lib/Format/UnwrappedLineParser.cpp | 2 +- clang/unittests/Format/FormatTestRawStrings.cpp | 2 ++ clang/unittests/Format/TokenAnnotatorTest.cpp | 14 ++++++++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) 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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits