Skip to content

Commit 6a1d3ea

Browse files
authored
[clang-format] Handle Java record (#139215)
Fix #62089
1 parent 9bafaf6 commit 6a1d3ea

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ struct AdditionalKeywords {
10681068
kw_interface = &IdentTable.get("interface");
10691069
kw_native = &IdentTable.get("native");
10701070
kw_package = &IdentTable.get("package");
1071+
kw_record = &IdentTable.get("record");
10711072
kw_synchronized = &IdentTable.get("synchronized");
10721073
kw_throws = &IdentTable.get("throws");
10731074
kw___except = &IdentTable.get("__except");
@@ -1411,6 +1412,7 @@ struct AdditionalKeywords {
14111412
IdentifierInfo *kw_interface;
14121413
IdentifierInfo *kw_native;
14131414
IdentifierInfo *kw_package;
1415+
IdentifierInfo *kw_record;
14141416
IdentifierInfo *kw_synchronized;
14151417
IdentifierInfo *kw_throws;
14161418

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,11 @@ void UnwrappedLineParser::parseStructuralElement(
17041704
*HasLabel = true;
17051705
return;
17061706
}
1707+
if (Style.isJava() && FormatTok->is(Keywords.kw_record)) {
1708+
parseRecord(/*ParseAsExpr=*/false, /*IsJavaRecord=*/true);
1709+
addUnwrappedLine();
1710+
return;
1711+
}
17071712
// In all other cases, parse the declaration.
17081713
break;
17091714
default:
@@ -3996,11 +4001,13 @@ void UnwrappedLineParser::parseJavaEnumBody() {
39964001
addUnwrappedLine();
39974002
}
39984003

3999-
void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
4004+
void UnwrappedLineParser::parseRecord(bool ParseAsExpr, bool IsJavaRecord) {
4005+
assert(!IsJavaRecord || FormatTok->is(Keywords.kw_record));
40004006
const FormatToken &InitialToken = *FormatTok;
40014007
nextToken();
40024008

4003-
FormatToken *ClassName = nullptr;
4009+
FormatToken *ClassName =
4010+
IsJavaRecord && FormatTok->is(tok::identifier) ? FormatTok : nullptr;
40044011
bool IsDerived = false;
40054012
auto IsNonMacroIdentifier = [](const FormatToken *Tok) {
40064013
return Tok->is(tok::identifier) && Tok->TokenText != Tok->TokenText.upper();
@@ -4035,7 +4042,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
40354042
switch (FormatTok->Tok.getKind()) {
40364043
case tok::l_paren:
40374044
// We can have macros in between 'class' and the class name.
4038-
if (!IsNonMacroIdentifier(Previous) ||
4045+
if (IsJavaRecord || !IsNonMacroIdentifier(Previous) ||
40394046
// e.g. `struct macro(a) S { int i; };`
40404047
Previous->Previous == &InitialToken) {
40414048
parseParens();

clang/lib/Format/UnwrappedLineParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class UnwrappedLineParser {
177177
// Parses a record (aka class) as a top level element. If ParseAsExpr is true,
178178
// parses the record as a child block, i.e. if the class declaration is an
179179
// expression.
180-
void parseRecord(bool ParseAsExpr = false);
180+
void parseRecord(bool ParseAsExpr = false, bool IsJavaRecord = false);
181181
void parseObjCLightweightGenerics();
182182
void parseObjCMethod();
183183
void parseObjCProtocolList();

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3792,6 +3792,17 @@ TEST_F(TokenAnnotatorTest, SwitchExpression) {
37923792
EXPECT_TOKEN(Tokens[20], tok::arrow, TT_CaseLabelArrow);
37933793
}
37943794

3795+
TEST_F(TokenAnnotatorTest, JavaRecord) {
3796+
auto Tokens = annotate("public record MyRecord() {}",
3797+
getLLVMStyle(FormatStyle::LK_Java));
3798+
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
3799+
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_ClassHeadName);
3800+
// Not TT_FunctionDeclarationLParen.
3801+
EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_Unknown);
3802+
EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_RecordLBrace);
3803+
EXPECT_TOKEN(Tokens[6], tok::r_brace, TT_RecordRBrace);
3804+
}
3805+
37953806
TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
37963807
auto Tokens = annotate("a = b and c;");
37973808
ASSERT_EQ(Tokens.size(), 7u) << Tokens;

0 commit comments

Comments
 (0)