Skip to content

[clang-format] Parse JSON outermost l_brace as braced list brace #143327

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
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions clang/unittests/Format/FormatTestRawStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
14 changes: 14 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading