Skip to content

Commit

Permalink
[clang-format] Correctly attach enum braces with ShortEnums disabled
Browse files Browse the repository at this point in the history
Previously, with AllowShortEnumsOnASingleLine disabled, enums that would have otherwise fit on a single line would always put the opening brace on its own line.
This patch ensures that these enums will only put the brace on its own line if the existing attachment rules indicate that it should.

Reviewed By: HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D99840
  • Loading branch information
lunasorcery authored and mkurdej committed Jul 28, 2021
1 parent 6cd0e35 commit 7161672
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ AST Matchers
clang-format
------------

- ...
- Option ``AllowShortEnumsOnASingleLine: false`` has been improved, it now
correctly places the opening brace according to ``BraceWrapping.AfterEnum``.

libclang
--------
Expand Down
3 changes: 1 addition & 2 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,7 @@ struct FormatStyle {
/// enum { A, B } myEnum;
///
/// false:
/// enum
/// {
/// enum {
/// A,
/// B
/// } myEnum;
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2515,6 +2515,8 @@ bool UnwrappedLineParser::parseEnum() {
if (FormatTok->Tok.is(tok::kw_enum))
nextToken();

const FormatToken &InitialToken = *FormatTok;

// In TypeScript, "enum" can also be used as property name, e.g. in interface
// declarations. An "enum" keyword followed by a colon would be a syntax
// error and thus assume it is just an identifier.
Expand Down Expand Up @@ -2561,7 +2563,8 @@ bool UnwrappedLineParser::parseEnum() {
return true;
}

if (!Style.AllowShortEnumsOnASingleLine)
if (!Style.AllowShortEnumsOnASingleLine &&
ShouldBreakBeforeBrace(Style, InitialToken))
addUnwrappedLine();
// Parse enum body.
nextToken();
Expand Down
11 changes: 9 additions & 2 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,14 @@ TEST_F(FormatTest, ShortEnums) {
Style.AllowShortEnumsOnASingleLine = true;
verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
Style.AllowShortEnumsOnASingleLine = false;
verifyFormat("enum {\n"
" A,\n"
" B,\n"
" C\n"
"} ShortEnum1, ShortEnum2;",
Style);
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.AfterEnum = true;
verifyFormat("enum\n"
"{\n"
" A,\n"
Expand Down Expand Up @@ -22123,8 +22131,7 @@ TEST_F(FormatTest, IndentAccessModifiers) {
Style);
// Enumerations are not records and should be unaffected.
Style.AllowShortEnumsOnASingleLine = false;
verifyFormat("enum class E\n"
"{\n"
verifyFormat("enum class E {\n"
" A,\n"
" B\n"
"};\n",
Expand Down
3 changes: 1 addition & 2 deletions clang/unittests/Format/FormatTestCSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,7 @@ TEST_F(FormatTestCSharp, CSharpRegions) {
}

TEST_F(FormatTestCSharp, CSharpKeyWordEscaping) {
verifyFormat("public enum var\n"
"{\n"
verifyFormat("public enum var {\n"
" none,\n"
" @string,\n"
" bool,\n"
Expand Down

0 comments on commit 7161672

Please sign in to comment.