[PATCH] D93938: [clang-format] Fixed AfterEnum handling
Ally Tiritoglu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 15 11:13:52 PDT 2021
atirit updated this revision to Diff 330738.
atirit added a comment.
Implemented requested changes
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D93938/new/
https://reviews.llvm.org/D93938
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -1393,6 +1393,21 @@
Style);
}
+TEST_F(FormatTest, AfterEnumShortEnums) {
+ FormatStyle Style = getLLVMStyle();
+ Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+
+ Style.AllowShortEnumsOnASingleLine = true;
+ Style.BraceWrapping.AfterEnum = true;
+ verifyFormat("enum\n"
+ "{\n"
+ " A,\n"
+ " B,\n"
+ "} ShortEnum;",
+ Style);
+ verifyFormat("enum { A, B } ShortEnum;", Style);
+}
+
TEST_F(FormatTest, ShortCaseLabels) {
FormatStyle Style = getLLVMStyle();
Style.AllowShortCaseLabelsOnASingleLine = true;
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3666,29 +3666,31 @@
// The tokens that could force an enum to not be on a single line are a
// trailing comment and a trailing comma on the last case. This checks for
// those.
- bool lineContainsBreakingTokens = false;
- const FormatToken *breakingSearchToken = &Right;
- while ((breakingSearchToken = breakingSearchToken->Next)) {
- bool hasBreakingComma = breakingSearchToken->is(tok::comma) &&
- breakingSearchToken->Next->is(tok::r_brace);
- if (breakingSearchToken->isTrailingComment() || hasBreakingComma) {
- lineContainsBreakingTokens = true;
- break;
+ auto isAllowedByShortEnums = [&]() {
+ if (!Style.AllowShortEnumsOnASingleLine ||
+ (strlen(Right.TokenText.data()) + Right.OriginalColumn) >
+ Style.ColumnLimit)
+ return true;
+
+ const FormatToken *breakingSearchToken = &Right;
+ while ((breakingSearchToken = breakingSearchToken->Next)) {
+ bool hasBreakingComma = breakingSearchToken->is(tok::comma) &&
+ breakingSearchToken->Next->is(tok::r_brace);
+ if (breakingSearchToken->isTrailingComment() || hasBreakingComma) {
+ return true;
+ }
}
- }
+
+ return false;
+ };
bool isAllowedByAfterEnum =
(Line.startsWith(tok::kw_enum) ||
Line.startsWith(tok::kw_typedef, tok::kw_enum)) &&
Style.BraceWrapping.AfterEnum;
- bool isLineTooBig = (strlen(Right.TokenText.data()) +
- Right.OriginalColumn) > Style.ColumnLimit;
- // AllowShortEnumsOnASingleLine is ignored if the line is too long or
- // contains breaking tokens.
- bool isAllowedByShortEnums = isLineTooBig || lineContainsBreakingTokens ||
- !Style.AllowShortEnumsOnASingleLine;
- return (isAllowedByAfterEnum && isAllowedByShortEnums) ||
- (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
- (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
+ return (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
+ (Line.startsWith(tok::kw_struct) &&
+ Style.BraceWrapping.AfterStruct) ||
+ (isAllowedByAfterEnum && isAllowedByShortEnums());
}
if (Left.is(TT_ObjCBlockLBrace) &&
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93938.330738.patch
Type: text/x-patch
Size: 3375 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210315/91a5f79d/attachment.bin>
More information about the cfe-commits
mailing list