Skip to content

Commit 9a894ae

Browse files
authored
[clang-format] Parse JSON outermost l_brace as braced list brace (#143327)
See #65400 (comment).
1 parent e30cba5 commit 9a894ae

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2248,7 +2248,6 @@ unsigned ContinuationIndenter::reformatRawStringLiteral(
22482248
/*Status=*/nullptr);
22492249

22502250
auto NewCode = applyAllReplacements(RawText, Fixes.first);
2251-
tooling::Replacements NoFixes;
22522251
if (!NewCode)
22532252
return addMultilineToken(Current, State);
22542253
if (!DryRun) {

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ void UnwrappedLineParser::parseFile() {
269269
bool MustBeDeclaration = !Line->InPPDirective && !Style.isJavaScript();
270270
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
271271
MustBeDeclaration);
272-
if (Style.isTextProto())
272+
if (Style.isTextProto() || (Style.isJson() && FormatTok->IsFirst))
273273
parseBracedList();
274274
else
275275
parseLevel();

clang/unittests/Format/FormatTestRawStrings.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,9 +1002,11 @@ TEST_F(FormatTestRawStrings, Json) {
10021002
};
10031003

10041004
EXPECT_EQ("json = R\"json({\n"
1005+
" \"foo\": \"bar\",\n"
10051006
" \"str\": \"test\"\n"
10061007
" })json\";",
10071008
format("json = R\"json({\n"
1009+
" \"foo\": \"bar\",\n"
10081010
" \"str\": \"test\"\n"
10091011
"})json\";",
10101012
Style));

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4105,6 +4105,20 @@ TEST_F(TokenAnnotatorTest, BitFieldColon) {
41054105
EXPECT_TOKEN(Tokens[5], tok::colon, TT_BitFieldColon);
41064106
}
41074107

4108+
TEST_F(TokenAnnotatorTest, JsonCodeInRawString) {
4109+
auto Tokens = annotate("{\n"
4110+
" \"foo\": \"bar\",\n"
4111+
" \"str\": \"test\"\n"
4112+
"}",
4113+
getLLVMStyle(FormatStyle::LK_Json));
4114+
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
4115+
EXPECT_TOKEN(Tokens[0], tok::l_brace, TT_DictLiteral);
4116+
EXPECT_TOKEN(Tokens[1], tok::string_literal, TT_SelectorName);
4117+
EXPECT_TOKEN(Tokens[2], tok::colon, TT_DictLiteral);
4118+
EXPECT_TOKEN(Tokens[5], tok::string_literal, TT_SelectorName);
4119+
EXPECT_TOKEN(Tokens[6], tok::colon, TT_DictLiteral);
4120+
}
4121+
41084122
} // namespace
41094123
} // namespace format
41104124
} // namespace clang

0 commit comments

Comments
 (0)