Author: Luna Kirkby Date: 2021-07-28T10:29:15+02:00 New Revision: 71616722d4092f88861e9eb337e2902bbab2cbd4
URL: https://github.com/llvm/llvm-project/commit/71616722d4092f88861e9eb337e2902bbab2cbd4 DIFF: https://github.com/llvm/llvm-project/commit/71616722d4092f88861e9eb337e2902bbab2cbd4.diff LOG: [clang-format] Correctly attach enum braces with ShortEnums disabled 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 Added: Modified: clang/docs/ReleaseNotes.rst clang/include/clang/Format/Format.h clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp clang/unittests/Format/FormatTestCSharp.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 19a4e8f9ece39..ca3ad908fa099 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -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 -------- diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index c424e79a971cf..0e2193596daf7 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -544,8 +544,7 @@ struct FormatStyle { /// enum { A, B } myEnum; /// /// false: - /// enum - /// { + /// enum { /// A, /// B /// } myEnum; diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 103e3559b1208..d6b1ebb1239df 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -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. @@ -2561,7 +2563,8 @@ bool UnwrappedLineParser::parseEnum() { return true; } - if (!Style.AllowShortEnumsOnASingleLine) + if (!Style.AllowShortEnumsOnASingleLine && + ShouldBreakBeforeBrace(Style, InitialToken)) addUnwrappedLine(); // Parse enum body. nextToken(); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 3adf42d34cf13..a4e5f9dce9b8d 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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" @@ -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", diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp index 3c990339cf748..8cd29e5548175 100644 --- a/clang/unittests/Format/FormatTestCSharp.cpp +++ b/clang/unittests/Format/FormatTestCSharp.cpp @@ -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" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits