Author: Owen Pan Date: 2025-06-09T20:34:12-07:00 New Revision: 9a894ae794f26cdd0822c4cea99e2486e3523189
URL: https://github.com/llvm/llvm-project/commit/9a894ae794f26cdd0822c4cea99e2486e3523189 DIFF: https://github.com/llvm/llvm-project/commit/9a894ae794f26cdd0822c4cea99e2486e3523189.diff LOG: [clang-format] Parse JSON outermost l_brace as braced list brace (#143327) See https://github.com/llvm/llvm-project/issues/65400#issuecomment-2922181979. Added: Modified: clang/lib/Format/ContinuationIndenter.cpp clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTestRawStrings.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Removed: ################################################################################ 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