[clang] [clang-format][NFC] Simplify parseBracedList() (PR #72010)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 11 21:06:33 PST 2023
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/72010
>From f3f303d80a40b9ce8a13721cfa108b4094b8b2c3 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 10 Nov 2023 17:34:06 -0800
Subject: [PATCH 1/2] [clang-format][NFC] Simplify parseBracedList()
---
clang/lib/Format/UnwrappedLineParser.cpp | 36 +++++++++---------------
clang/lib/Format/UnwrappedLineParser.h | 3 +-
2 files changed, 15 insertions(+), 24 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 018bc6c165485e2..e6a333fdc4e42ce 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -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();
}
break;
case tok::l_square:
@@ -2379,9 +2378,10 @@ bool UnwrappedLineParser::tryToParseChildBlock() {
return true;
}
-bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
- bool IsEnum,
- tok::TokenKind ClosingBraceKind) {
+bool UnwrappedLineParser::parseBracedList(bool IsEnum) {
+ const auto *OpeningBrace = FormatTok->getPreviousNonComment();
+ const bool IsAngleBracket = OpeningBrace && OpeningBrace->is(tok::less);
+
bool HasError = false;
// FIXME: Once we have an expression parser in the UnwrappedLineParser,
@@ -2403,7 +2403,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();
@@ -2434,14 +2434,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();
break;
case tok::semi:
// JavaScript (or more precisely TypeScript) can have semicolons in braced
@@ -2453,8 +2448,8 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
break;
}
HasError = true;
- if (!ContinueOnSemicolons)
- return !HasError;
+ if (!IsEnum)
+ return false;
nextToken();
break;
case tok::comma:
@@ -3618,8 +3613,7 @@ void UnwrappedLineParser::parseConstraintExpression() {
return;
nextToken();
- parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
- /*ClosingBraceKind=*/tok::greater);
+ parseBracedList();
break;
default:
@@ -3650,8 +3644,7 @@ void UnwrappedLineParser::parseConstraintExpression() {
nextToken();
if (FormatTok->is(tok::less)) {
nextToken();
- parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
- /*ClosingBraceKind=*/tok::greater);
+ parseBracedList();
}
TopLevelParensAllowed = false;
break;
@@ -3732,8 +3725,7 @@ bool UnwrappedLineParser::parseEnum() {
addUnwrappedLine();
Line->Level += 1;
}
- bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true,
- /*IsEnum=*/true);
+ bool HasError = !parseBracedList(/*IsEnum=*/true);
if (!Style.AllowShortEnumsOnASingleLine)
Line->Level -= 1;
if (HasError) {
diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h
index c31f25fdd8f83c9..61ec04578955ce6 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -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 IsEnum = false);
bool parseParens(TokenType AmpAmpTokenType = TT_Unknown);
void parseSquare(bool LambdaIntroducer = false);
void keepAncestorBraces();
>From 085ca85a4e4ab7f1eb83ca9f79e01d5e45fe0d4d Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sat, 11 Nov 2023 21:02:26 -0800
Subject: [PATCH 2/2] Made `IsAngleBracket` a parameter of `parseBracedList()`.
---
clang/lib/Format/UnwrappedLineParser.cpp | 15 ++++++---------
clang/lib/Format/UnwrappedLineParser.h | 2 +-
2 files changed, 7 insertions(+), 10 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index e6a333fdc4e42ce..c870ff01605e725 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2017,7 +2017,7 @@ void UnwrappedLineParser::parseStructuralElement(
} else if (Style.Language == FormatStyle::LK_Proto &&
FormatTok->is(tok::less)) {
nextToken();
- parseBracedList();
+ parseBracedList(/*IsAngleBracket=*/true);
}
break;
case tok::l_square:
@@ -2378,10 +2378,7 @@ bool UnwrappedLineParser::tryToParseChildBlock() {
return true;
}
-bool UnwrappedLineParser::parseBracedList(bool IsEnum) {
- const auto *OpeningBrace = FormatTok->getPreviousNonComment();
- const bool IsAngleBracket = OpeningBrace && OpeningBrace->is(tok::less);
-
+bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
bool HasError = false;
// FIXME: Once we have an expression parser in the UnwrappedLineParser,
@@ -2436,7 +2433,7 @@ bool UnwrappedLineParser::parseBracedList(bool IsEnum) {
case tok::less:
nextToken();
if (IsAngleBracket)
- parseBracedList();
+ parseBracedList(/*IsAngleBracket=*/true);
break;
case tok::semi:
// JavaScript (or more precisely TypeScript) can have semicolons in braced
@@ -3613,7 +3610,7 @@ void UnwrappedLineParser::parseConstraintExpression() {
return;
nextToken();
- parseBracedList();
+ parseBracedList(/*IsAngleBracket=*/true);
break;
default:
@@ -3644,7 +3641,7 @@ void UnwrappedLineParser::parseConstraintExpression() {
nextToken();
if (FormatTok->is(tok::less)) {
nextToken();
- parseBracedList();
+ parseBracedList(/*IsAngleBracket=*/true);
}
TopLevelParensAllowed = false;
break;
@@ -3725,7 +3722,7 @@ bool UnwrappedLineParser::parseEnum() {
addUnwrappedLine();
Line->Level += 1;
}
- bool HasError = !parseBracedList(/*IsEnum=*/true);
+ bool HasError = !parseBracedList(/*IsAngleBracket=*/false, /*IsEnum=*/true);
if (!Style.AllowShortEnumsOnASingleLine)
Line->Level -= 1;
if (HasError) {
diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h
index 61ec04578955ce6..0bcf50583033a08 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -151,7 +151,7 @@ class UnwrappedLineParser {
bool *HasDoWhile = nullptr,
bool *HasLabel = nullptr);
bool tryToParseBracedList();
- bool parseBracedList(bool IsEnum = false);
+ bool parseBracedList(bool IsAngleBracket = false, bool IsEnum = false);
bool parseParens(TokenType AmpAmpTokenType = TT_Unknown);
void parseSquare(bool LambdaIntroducer = false);
void keepAncestorBraces();
More information about the cfe-commits
mailing list