Skip to content

[clang-format][NFC] Simplify parseBracedList() #72010

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 2 commits into from
Nov 12, 2023
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
33 changes: 11 additions & 22 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2017,8 +2017,7 @@ void UnwrappedLineParser::parseStructuralElement(
} else if (Style.Language == FormatStyle::LK_Proto &&
FormatTok->is(tok::less)) {
nextToken();
parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
/*ClosingBraceKind=*/tok::greater);
parseBracedList(/*IsAngleBracket=*/true);
}
break;
case tok::l_square:
Expand Down Expand Up @@ -2379,9 +2378,7 @@ bool UnwrappedLineParser::tryToParseChildBlock() {
return true;
}

bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
bool IsEnum,
tok::TokenKind ClosingBraceKind) {
bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
bool HasError = false;

// FIXME: Once we have an expression parser in the UnwrappedLineParser,
Expand All @@ -2403,7 +2400,7 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
parseChildBlock();
}
}
if (FormatTok->Tok.getKind() == ClosingBraceKind) {
if (FormatTok->is(IsAngleBracket ? tok::greater : tok::r_brace)) {
if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
addUnwrappedLine();
nextToken();
Expand Down Expand Up @@ -2434,14 +2431,9 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
parseBracedList();
break;
case tok::less:
if (Style.Language == FormatStyle::LK_Proto ||
ClosingBraceKind == tok::greater) {
nextToken();
parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
/*ClosingBraceKind=*/tok::greater);
} else {
nextToken();
}
nextToken();
if (IsAngleBracket)
parseBracedList(/*IsAngleBracket=*/true);
break;
case tok::semi:
// JavaScript (or more precisely TypeScript) can have semicolons in braced
Expand All @@ -2453,8 +2445,8 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
break;
}
HasError = true;
if (!ContinueOnSemicolons)
return !HasError;
if (!IsEnum)
return false;
nextToken();
break;
case tok::comma:
Expand Down Expand Up @@ -3618,8 +3610,7 @@ void UnwrappedLineParser::parseConstraintExpression() {
return;

nextToken();
parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
/*ClosingBraceKind=*/tok::greater);
parseBracedList(/*IsAngleBracket=*/true);
break;

default:
Expand Down Expand Up @@ -3650,8 +3641,7 @@ void UnwrappedLineParser::parseConstraintExpression() {
nextToken();
if (FormatTok->is(tok::less)) {
nextToken();
parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
/*ClosingBraceKind=*/tok::greater);
parseBracedList(/*IsAngleBracket=*/true);
}
TopLevelParensAllowed = false;
break;
Expand Down Expand Up @@ -3732,8 +3722,7 @@ bool UnwrappedLineParser::parseEnum() {
addUnwrappedLine();
Line->Level += 1;
}
bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true,
/*IsEnum=*/true);
bool HasError = !parseBracedList(/*IsAngleBracket=*/false, /*IsEnum=*/true);
if (!Style.AllowShortEnumsOnASingleLine)
Line->Level -= 1;
if (HasError) {
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Format/UnwrappedLineParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ class UnwrappedLineParser {
bool *HasDoWhile = nullptr,
bool *HasLabel = nullptr);
bool tryToParseBracedList();
bool parseBracedList(bool ContinueOnSemicolons = false, bool IsEnum = false,
tok::TokenKind ClosingBraceKind = tok::r_brace);
bool parseBracedList(bool IsAngleBracket = false, bool IsEnum = false);
bool parseParens(TokenType AmpAmpTokenType = TT_Unknown);
void parseSquare(bool LambdaIntroducer = false);
void keepAncestorBraces();
Expand Down