Skip to content

Commit 292058a

Browse files
committed
[clang-format] Fix Microsoft style for enums
Summary: Before this change enums were formatted incorrectly for the Microsoft style. [C++ Example] enum { one, two } three, four; [Incorrectly Formatted] enum { one, two } three, four; [Correct Format with Patch] enum { one, two } three, four; Reviewers: jbcoe, MyDeveloperDay, rnk Reviewed By: MyDeveloperDay Subscribers: cfe-commits Tags: #clang, #clang-format Differential Revision: https://reviews.llvm.org/D78982
1 parent 2fd7d36 commit 292058a

File tree

7 files changed

+72
-3
lines changed

7 files changed

+72
-3
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,21 @@ the configuration (without a prefix: ``Auto``).
395395
return;
396396
}
397397

398+
**AllowShortEnumsOnASingleLine** (``bool``)
399+
Allow short enums on a single line.
400+
401+
.. code-block:: c++
402+
403+
true:
404+
enum { A, B } myEnum;
405+
406+
false:
407+
enum
408+
{
409+
A,
410+
B
411+
} myEnum;
412+
398413
**AllowShortFunctionsOnASingleLine** (``ShortFunctionStyle``)
399414
Dependent on the value, ``int f() { return 0; }`` can be put on a
400415
single line.

clang/include/clang/Format/Format.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,20 @@ struct FormatStyle {
221221
/// \endcode
222222
bool AllowAllParametersOfDeclarationOnNextLine;
223223

224+
/// Allow short enums on a single line.
225+
/// \code
226+
/// true:
227+
/// enum { A, B } myEnum;
228+
///
229+
/// false:
230+
/// enum
231+
/// {
232+
/// A,
233+
/// B
234+
/// } myEnum;
235+
/// \endcode
236+
bool AllowShortEnumsOnASingleLine;
237+
224238
/// Different styles for merging short blocks containing at most one
225239
/// statement.
226240
enum ShortBlockStyle {
@@ -2175,6 +2189,7 @@ struct FormatStyle {
21752189
R.AllowAllConstructorInitializersOnNextLine &&
21762190
AllowAllParametersOfDeclarationOnNextLine ==
21772191
R.AllowAllParametersOfDeclarationOnNextLine &&
2192+
AllowShortEnumsOnASingleLine == R.AllowShortEnumsOnASingleLine &&
21782193
AllowShortBlocksOnASingleLine == R.AllowShortBlocksOnASingleLine &&
21792194
AllowShortCaseLabelsOnASingleLine ==
21802195
R.AllowShortCaseLabelsOnASingleLine &&

clang/lib/Format/Format.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ template <> struct MappingTraits<FormatStyle> {
399399
Style.AllowAllConstructorInitializersOnNextLine);
400400
IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
401401
Style.AllowAllParametersOfDeclarationOnNextLine);
402+
IO.mapOptional("AllowShortEnumsOnASingleLine",
403+
Style.AllowShortEnumsOnASingleLine);
402404
IO.mapOptional("AllowShortBlocksOnASingleLine",
403405
Style.AllowShortBlocksOnASingleLine);
404406
IO.mapOptional("AllowShortCaseLabelsOnASingleLine",
@@ -754,6 +756,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
754756
LLVMStyle.AllowAllArgumentsOnNextLine = true;
755757
LLVMStyle.AllowAllConstructorInitializersOnNextLine = true;
756758
LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
759+
LLVMStyle.AllowShortEnumsOnASingleLine = true;
757760
LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
758761
LLVMStyle.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
759762
LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;
@@ -1139,6 +1142,7 @@ FormatStyle getMicrosoftStyle(FormatStyle::LanguageKind Language) {
11391142
Style.BraceWrapping.BeforeCatch = true;
11401143
Style.BraceWrapping.BeforeElse = true;
11411144
Style.PenaltyReturnTypeOnItsOwnLine = 1000;
1145+
Style.AllowShortEnumsOnASingleLine = false;
11421146
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11431147
Style.AllowShortCaseLabelsOnASingleLine = false;
11441148
Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,7 @@ bool UnwrappedLineParser::tryToParseBracedList() {
17451745
}
17461746

17471747
bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1748+
bool IsEnum,
17481749
tok::TokenKind ClosingBraceKind) {
17491750
bool HasError = false;
17501751

@@ -1785,6 +1786,8 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
17851786
}
17861787
}
17871788
if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1789+
if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
1790+
addUnwrappedLine();
17881791
nextToken();
17891792
return !HasError;
17901793
}
@@ -1843,6 +1846,8 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
18431846
break;
18441847
case tok::comma:
18451848
nextToken();
1849+
if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
1850+
addUnwrappedLine();
18461851
break;
18471852
default:
18481853
nextToken();
@@ -2301,9 +2306,18 @@ bool UnwrappedLineParser::parseEnum() {
23012306
return true;
23022307
}
23032308

2309+
if (!Style.AllowShortEnumsOnASingleLine)
2310+
addUnwrappedLine();
23042311
// Parse enum body.
23052312
nextToken();
2306-
bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
2313+
if (!Style.AllowShortEnumsOnASingleLine) {
2314+
addUnwrappedLine();
2315+
Line->Level += 1;
2316+
}
2317+
bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true,
2318+
/*IsEnum=*/true);
2319+
if (!Style.AllowShortEnumsOnASingleLine)
2320+
Line->Level -= 1;
23072321
if (HasError) {
23082322
if (FormatTok->is(tok::semi))
23092323
nextToken();

clang/lib/Format/UnwrappedLineParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class UnwrappedLineParser {
9898
void readTokenWithJavaScriptASI();
9999
void parseStructuralElement();
100100
bool tryToParseBracedList();
101-
bool parseBracedList(bool ContinueOnSemicolons = false,
101+
bool parseBracedList(bool ContinueOnSemicolons = false, bool IsEnum = false,
102102
tok::TokenKind ClosingBraceKind = tok::r_brace);
103103
void parseParens();
104104
void parseSquare(bool LambdaIntroducer = false);

clang/unittests/Format/FormatTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,20 @@ TEST_F(FormatTest, CaseRanges) {
12981298
"}");
12991299
}
13001300

1301+
TEST_F(FormatTest, ShortEnums) {
1302+
FormatStyle Style = getLLVMStyle();
1303+
Style.AllowShortEnumsOnASingleLine = true;
1304+
verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
1305+
Style.AllowShortEnumsOnASingleLine = false;
1306+
verifyFormat("enum\n"
1307+
"{\n"
1308+
" A,\n"
1309+
" B,\n"
1310+
" C\n"
1311+
"} ShortEnum1, ShortEnum2;",
1312+
Style);
1313+
}
1314+
13011315
TEST_F(FormatTest, ShortCaseLabels) {
13021316
FormatStyle Style = getLLVMStyle();
13031317
Style.AllowShortCaseLabelsOnASingleLine = true;
@@ -12959,6 +12973,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
1295912973
CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine);
1296012974
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
1296112975
CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
12976+
CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
1296212977
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
1296312978
CHECK_PARSE_BOOL(BinPackArguments);
1296412979
CHECK_PARSE_BOOL(BinPackParameters);

clang/unittests/Format/FormatTestCSharp.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,13 @@ TEST_F(FormatTestCSharp, CSharpRegions) {
343343
}
344344

345345
TEST_F(FormatTestCSharp, CSharpKeyWordEscaping) {
346-
verifyFormat("public enum var { none, @string, bool, @enum }");
346+
verifyFormat("public enum var\n"
347+
"{\n"
348+
" none,\n"
349+
" @string,\n"
350+
" bool,\n"
351+
" @enum\n"
352+
"}");
347353
}
348354

349355
TEST_F(FormatTestCSharp, CSharpNullCoalescing) {

0 commit comments

Comments
 (0)