Skip to content

Commit

Permalink
[clang-format][NFC] Simplify parseBracedList() (#72010)
Browse files Browse the repository at this point in the history
  • Loading branch information
owenca committed Nov 12, 2023
1 parent e0e0891 commit a533b76
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
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 @@ -150,8 +150,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

0 comments on commit a533b76

Please sign in to comment.