[PATCH] D121042: [clang-format] Handle goto labels for RemoveBracesLLVM
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 5 12:47:12 PST 2022
This revision was automatically updated to reflect the committed changes.
Closed by commit rG28b76b1e23bb: [clang-format] Handle goto labels for RemoveBracesLLVM (authored by owenpan).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D121042/new/
https://reviews.llvm.org/D121042
Files:
clang/lib/Format/UnwrappedLineParser.cpp
clang/lib/Format/UnwrappedLineParser.h
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25018,6 +25018,23 @@
"}",
Style);
+ verifyFormat("if (a) {\n"
+ "Label:\n"
+ "}",
+ Style);
+
+ verifyFormat("if (a) {\n"
+ "Label:\n"
+ " f();\n"
+ "}",
+ Style);
+
+ verifyFormat("if (a) {\n"
+ " f();\n"
+ "Label:\n"
+ "}",
+ Style);
+
// FIXME: See https://github.com/llvm/llvm-project/issues/53543.
#if 0
Style.ColumnLimit = 65;
Index: clang/lib/Format/UnwrappedLineParser.h
===================================================================
--- clang/lib/Format/UnwrappedLineParser.h
+++ clang/lib/Format/UnwrappedLineParser.h
@@ -112,7 +112,8 @@
void readTokenWithJavaScriptASI();
void parseStructuralElement(IfStmtKind *IfKind = nullptr,
bool IsTopLevel = false,
- TokenType NextLBracesType = TT_Unknown);
+ TokenType NextLBracesType = TT_Unknown,
+ bool *HasLabel = nullptr);
bool tryToParseBracedList();
bool parseBracedList(bool ContinueOnSemicolons = false, bool IsEnum = false,
tok::TokenKind ClosingBraceKind = tok::r_brace);
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -476,6 +476,7 @@
: TT_Unknown;
const bool IsPrecededByCommentOrPPDirective =
!Style.RemoveBracesLLVM || precededByCommentOrPPDirective();
+ bool HasLabel = false;
unsigned StatementCount = 0;
bool SwitchLabelEncountered = false;
do {
@@ -486,9 +487,9 @@
kind = tok::r_brace;
auto ParseDefault = [this, HasOpeningBrace, IfKind, NextLevelLBracesType,
- &StatementCount] {
- parseStructuralElement(IfKind, /*IsTopLevel=*/!HasOpeningBrace,
- /*NextLBracesType=*/NextLevelLBracesType);
+ &HasLabel, &StatementCount] {
+ parseStructuralElement(IfKind, !HasOpeningBrace, NextLevelLBracesType,
+ HasLabel ? nullptr : &HasLabel);
++StatementCount;
assert(StatementCount > 0 && "StatementCount overflow!");
};
@@ -523,7 +524,7 @@
if (HasOpeningBrace) {
if (!Style.RemoveBracesLLVM)
return false;
- if (FormatTok->isNot(tok::r_brace) || StatementCount != 1 ||
+ if (FormatTok->isNot(tok::r_brace) || StatementCount != 1 || HasLabel ||
IsPrecededByCommentOrPPDirective ||
precededByCommentOrPPDirective())
return false;
@@ -1312,7 +1313,8 @@
void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind,
bool IsTopLevel,
- TokenType NextLBracesType) {
+ TokenType NextLBracesType,
+ bool *HasLabel) {
if (Style.Language == FormatStyle::LK_TableGen &&
FormatTok->is(tok::pp_include)) {
nextToken();
@@ -1758,6 +1760,8 @@
if (FormatTok->is(tok::colon) && !Line->MustBeDeclaration) {
Line->Tokens.begin()->Tok->MustBreakBefore = true;
parseLabel(!Style.IndentGotoLabels);
+ if (HasLabel)
+ *HasLabel = true;
return;
}
// Recognize function-like macro usages without trailing semicolon as
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121042.413239.patch
Type: text/x-patch
Size: 3856 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220305/b711881a/attachment.bin>
More information about the cfe-commits
mailing list