https://github.com/dmasloff updated https://github.com/llvm/llvm-project/pull/106145
>From 9848394edfa4bbae9f4f2df4b3dbcecefeb72624 Mon Sep 17 00:00:00 2001 From: dmasloff <dmaslo...@gmail.com> Date: Mon, 26 Aug 2024 22:11:05 +0300 Subject: [PATCH 1/9] fix merge conflict --- clang/docs/ClangFormatStyleOptions.rst | 42 ++++ clang/include/clang/Format/Format.h | 40 +++- clang/lib/Format/Format.cpp | 15 ++ clang/lib/Format/UnwrappedLineFormatter.cpp | 42 ++++ clang/unittests/Format/FormatTest.cpp | 205 ++++++++++++++++++++ 5 files changed, 343 insertions(+), 1 deletion(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index c175436a2817a9..6b0ebdf45e9b55 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -6834,6 +6834,48 @@ the configuration (without a prefix: ``Auto``). For example: BOOST_PP_STRINGIZE +.. _WrapNamespaceBodyWithEmptyLines: + +**WrapNamespaceBodyWithEmptyLines** (``WrapNamespaceBodyWithEmptyLinesStyle``) :versionbadge:`clang-format 19` :ref:`¶ <WrapNamespaceBodyWithEmptyLines>` + Controls number of empty lines at the begging and at the end of + namespace definition. + + Possible values: + + * ``WNBWELS_Never`` (in configuration: ``Never``) + Removes all empty lines at the beginning and at the end of + namespace definition. + + .. code-block:: c++ + + namespace N1 { + namespace N2 + function(); + } + } + + * ``WNBWELS_Always`` (in configuration: ``Always``) + Always adds an empty line at the beginning and at the end of + namespace definition. MaxEmptyLinesToKeep is also applied, but + empty lines between consecutive namespace declarations are + always removed. + + .. code-block:: c++ + + namespace N1 { + namespace N2 { + + function(); + + } + } + + * ``WNBWELS_Leave`` (in configuration: ``Leave``) + Keeps existing newlines at the beginning and at the end of + namespace definition using MaxEmptyLinesToKeep for formatting. + + + .. END_FORMAT_STYLE_OPTIONS Adding additional style options diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index eefaabf9392fd7..594c85487b541d 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -5134,6 +5134,43 @@ struct FormatStyle { /// \version 11 std::vector<std::string> WhitespaceSensitiveMacros; + /// Different styles for modify number of empty lines in + /// the beginning and at the of end of namespaces. + enum WrapNamespaceBodyWithEmptyLinesStyle : int8_t { + /// Removes all empty lines at the beginning and at the end of + /// namespace definition. + /// \code + /// namespace N1 { + /// namespace N2 + /// function(); + /// } + /// } + /// \endcode + WNBWELS_Never, + /// Always adds an empty line at the beginning and at the end of + /// namespace definition. MaxEmptyLinesToKeep is also applied, but + /// empty lines between consecutive namespace declarations are + /// always removed. + /// \code + /// namespace N1 { + /// namespace N2 { + /// + /// function(); + /// + /// } + /// } + /// \endcode + WNBWELS_Always, + /// Keeps existing newlines at the beginning and at the end of + /// namespace definition using MaxEmptyLinesToKeep for formatting. + WNBWELS_Leave + }; + + /// Controls number of empty lines at the begging and at the end of + /// namespace definition. + /// \version 19 + WrapNamespaceBodyWithEmptyLinesStyle WrapNamespaceBodyWithEmptyLines; + bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignAfterOpenBracket == R.AlignAfterOpenBracket && @@ -5317,7 +5354,8 @@ struct FormatStyle { UseTab == R.UseTab && VerilogBreakBetweenInstancePorts == R.VerilogBreakBetweenInstancePorts && - WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros; + WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros && + WrapNamespaceBodyWithEmptyLines == R.WrapNamespaceBodyWithEmptyLines; } std::optional<FormatStyle> GetLanguageStyle(LanguageKind Language) const; diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 8f44e9f00212cf..f76bb384d6c0b9 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -839,6 +839,18 @@ template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> { } }; +template <> +struct ScalarEnumerationTraits< + FormatStyle::WrapNamespaceBodyWithEmptyLinesStyle> { + static void + enumeration(IO &IO, + FormatStyle::WrapNamespaceBodyWithEmptyLinesStyle &Value) { + IO.enumCase(Value, "Never", FormatStyle::WNBWELS_Never); + IO.enumCase(Value, "Always", FormatStyle::WNBWELS_Always); + IO.enumCase(Value, "Leave", FormatStyle::WNBWELS_Leave); + } +}; + template <> struct MappingTraits<FormatStyle> { static void mapping(IO &IO, FormatStyle &Style) { // When reading, read the language first, we need it for getPredefinedStyle. @@ -1170,6 +1182,8 @@ template <> struct MappingTraits<FormatStyle> { Style.VerilogBreakBetweenInstancePorts); IO.mapOptional("WhitespaceSensitiveMacros", Style.WhitespaceSensitiveMacros); + IO.mapOptional("WrapNamespaceBodyWithEmptyLines", + Style.WrapNamespaceBodyWithEmptyLines); // If AlwaysBreakAfterDefinitionReturnType was specified but // BreakAfterReturnType was not, initialize the latter from the former for @@ -1638,6 +1652,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.WhitespaceSensitiveMacros.push_back("NS_SWIFT_NAME"); LLVMStyle.WhitespaceSensitiveMacros.push_back("PP_STRINGIZE"); LLVMStyle.WhitespaceSensitiveMacros.push_back("STRINGIZE"); + LLVMStyle.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Leave; LLVMStyle.PenaltyBreakAssignment = prec::Assignment; LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19; diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 803c600cec44db..d1b80e601ef737 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -32,6 +32,26 @@ bool isRecordLBrace(const FormatToken &Tok) { TT_StructLBrace, TT_UnionLBrace); } +bool LineStartsNamespaceScope(const AnnotatedLine *Line, + const AnnotatedLine *PreviousLine, + const AnnotatedLine *PrevPrevLine) { + return PreviousLine && + ((PreviousLine->Last->is(tok::l_brace) && + PreviousLine->startsWithNamespace()) || + (PrevPrevLine && PrevPrevLine->startsWithNamespace() && + PreviousLine->startsWith(tok::l_brace))); +} + +bool LineEndsNamespaceScope(const AnnotatedLine *Line, + const SmallVectorImpl<AnnotatedLine *> &Lines) { + if (!Line) + return false; + const FormatToken *tok = Line->First; + if (!tok || tok->isNot(tok::r_brace)) + return false; + return getNamespaceToken(Line, Lines) != nullptr; +} + /// Tracks the indent level of \c AnnotatedLines across levels. /// /// \c nextLine must be called for each \c AnnotatedLine, after which \c @@ -1584,6 +1604,28 @@ static auto computeNewlines(const AnnotatedLine &Line, Newlines = 1; } + // Modify empty lines after "{" that opens namespace scope. + if (Style.WrapNamespaceBodyWithEmptyLines != FormatStyle::WNBWELS_Leave && + LineStartsNamespaceScope(&Line, PreviousLine, PrevPrevLine)) { + if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never) + Newlines = std::min(Newlines, 1u); + else if (!Line.startsWithNamespace()) + Newlines = std::max(Newlines, 2u); + else + Newlines = std::min(Newlines, 1u); + } + + // Modify empty lines before "}" that closes namespace scope. + if (Style.WrapNamespaceBodyWithEmptyLines != FormatStyle::WNBWELS_Leave && + LineEndsNamespaceScope(&Line, Lines)) { + if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never) + Newlines = std::min(Newlines, 1u); + else if (!LineEndsNamespaceScope(PreviousLine, Lines)) + Newlines = std::max(Newlines, 2u); + else + Newlines = std::min(Newlines, 1u); + } + // Insert or remove empty line before access specifiers. if (PreviousLine && RootToken.isAccessSpecifier()) { switch (Style.EmptyLineBeforeAccessModifier) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 22b6f7e1b62e2e..fd8dba436fdcf3 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -28427,6 +28427,211 @@ TEST_F(FormatTest, ShortNamespacesOption) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Empty namespace + verifyNoChange("namespace N {\n};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 0; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n\n" + "int f1(int a) { return 2 * a; }\n\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n\n" + "}}}", + Style); + + Style.MaxEmptyLinesToKeep = 2; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n\n\n" + "void function()\n\n\n" + "};", + Style); + + // Nested namespace + verifyFormat("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n\n\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n\n\n" + "}\n" + "}\n" + "}", + "namespace N1 {\n" + "namespace N2 {\n\n" + "namespace N3 {\n\n\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n\n\n" + "}\n\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n\n\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n\n\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 0; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Leave; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}\n" + "}\n" + "}", + Style); + + Style.MaxEmptyLinesToKeep = 2; + + // Single namespace + verifyNoChange("namespace N {\n\n\n" + "int f1(int a) { return 2 * a; }\n\n\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n\n" + "namespace N3 {\n\n\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n\n\n" + "}\n\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Empty namespace + verifyNoChange("namespace N {\n};", Style); + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n\n\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n\n\n" + "}}}", + Style); +} + } // namespace } // namespace test } // namespace format >From a0b182de813f91c81c2d40213fa05970e9274056 Mon Sep 17 00:00:00 2001 From: dmasloff <dmaslo...@gmail.com> Date: Mon, 2 Sep 2024 16:50:47 +0300 Subject: [PATCH 2/9] FormatTests update --- clang/unittests/Format/FormatTest.cpp | 90 +++++++++++++++++++-------- 1 file changed, 65 insertions(+), 25 deletions(-) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index fd8dba436fdcf3..204eb1d3d8d7df 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -28460,7 +28460,9 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { Style.CompactNamespaces = true; // Empty namespace - verifyNoChange("namespace N {\n};", Style); + verifyNoChange("namespace N {\n" + "};", + Style); // Single namespace verifyNoChange("namespace N {\n" @@ -28490,19 +28492,23 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { verifyNoChange("namespace N {};", Style); // Single namespace - verifyNoChange("namespace N {\n\n" - "int f1(int a) { return 2 * a; }\n\n" + verifyNoChange("namespace N {\n" + "\n" + "int f1(int a) { return 2 * a; }\n" + "\n" "};", Style); // Nested namespace verifyNoChange("namespace N1 {\n" "namespace N2 {\n" - "namespace N3 {\n\n" + "namespace N3 {\n" + "\n" "int f1() {\n" " int a = 1;\n" " return a;\n" - "}\n\n" + "}\n" + "\n" "}\n" "}\n" "}", @@ -28511,11 +28517,13 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { Style.CompactNamespaces = true; // Nested namespace - verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n\n" + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" "int f1() {\n" " int a = 1;\n" " return a;\n" - "}\n\n" + "}\n" + "\n" "}}}", Style); @@ -28526,30 +28534,44 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { verifyNoChange("namespace N {};", Style); // Single namespace - verifyNoChange("namespace N {\n\n\n" - "void function()\n\n\n" + verifyNoChange("namespace N {\n" + "\n" + "\n" + "void function()\n" + "\n" + "\n" "};", Style); // Nested namespace verifyFormat("namespace N1 {\n" "namespace N2 {\n" - "namespace N3 {\n\n\n" + "namespace N3 {\n" + "\n" + "\n" "int f1() {\n" " int a = 1;\n" " return a;\n" - "}\n\n\n" + "}\n" + "\n" + "\n" "}\n" "}\n" "}", "namespace N1 {\n" - "namespace N2 {\n\n" - "namespace N3 {\n\n\n" + "namespace N2 {\n" + "\n" + "namespace N3 {\n" + "\n" + "\n" "int f1() {\n" " int a = 1;\n" " return a;\n" - "}\n\n\n" - "}\n\n" + "}\n" + "\n" + "\n" + "}\n" + "\n" "}\n" "}", Style); @@ -28557,11 +28579,15 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { Style.CompactNamespaces = true; // Nested namespace - verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n\n\n" + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "\n" "int f1() {\n" " int a = 1;\n" " return a;\n" - "}\n\n\n" + "}\n" + "\n" + "\n" "}}}", Style); } @@ -28599,20 +28625,30 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { Style.MaxEmptyLinesToKeep = 2; // Single namespace - verifyNoChange("namespace N {\n\n\n" - "int f1(int a) { return 2 * a; }\n\n\n" + verifyNoChange("namespace N {\n" + "\n" + "\n" + "int f1(int a) { return 2 * a; }\n" + "\n" + "\n" "};", Style); // Nested namespace verifyNoChange("namespace N1 {\n" - "namespace N2 {\n\n" - "namespace N3 {\n\n\n" + "namespace N2 {\n" + "\n" + "namespace N3 {\n" + "\n" + "\n" "int f1() {\n" " int a = 1;\n" " return a;\n" - "}\n\n\n" - "}\n\n" + "}\n" + "\n" + "\n" + "}\n" + "\n" "}\n" "}", Style); @@ -28623,11 +28659,15 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { verifyNoChange("namespace N {\n};", Style); // Nested namespace - verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n\n\n" + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "\n" "int f1() {\n" " int a = 1;\n" " return a;\n" - "}\n\n\n" + "}\n" + "\n" + "\n" "}}}", Style); } >From 74a6cd8d8bc358beb5ffd7dfb9723e9c0a2e2a01 Mon Sep 17 00:00:00 2001 From: dmasloff <dmaslo...@gmail.com> Date: Tue, 3 Sep 2024 23:54:25 +0300 Subject: [PATCH 3/9] ConfigParseTest added for new option + minor code-style fixes --- clang/docs/ClangFormatStyleOptions.rst | 2 +- clang/include/clang/Format/Format.h | 2 +- clang/lib/Format/UnwrappedLineFormatter.cpp | 4 ++-- clang/unittests/Format/ConfigParseTest.cpp | 8 ++++++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 6b0ebdf45e9b55..e3ade4d2d85745 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -6836,7 +6836,7 @@ the configuration (without a prefix: ``Auto``). .. _WrapNamespaceBodyWithEmptyLines: -**WrapNamespaceBodyWithEmptyLines** (``WrapNamespaceBodyWithEmptyLinesStyle``) :versionbadge:`clang-format 19` :ref:`¶ <WrapNamespaceBodyWithEmptyLines>` +**WrapNamespaceBodyWithEmptyLines** (``WrapNamespaceBodyWithEmptyLinesStyle``) :versionbadge:`clang-format 20` :ref:`¶ <WrapNamespaceBodyWithEmptyLines>` Controls number of empty lines at the begging and at the end of namespace definition. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 594c85487b541d..e53830f1f73adb 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -5168,7 +5168,7 @@ struct FormatStyle { /// Controls number of empty lines at the begging and at the end of /// namespace definition. - /// \version 19 + /// \version 20 WrapNamespaceBodyWithEmptyLinesStyle WrapNamespaceBodyWithEmptyLines; bool operator==(const FormatStyle &R) const { diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index d1b80e601ef737..8fb93d4eaa0347 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -46,8 +46,8 @@ bool LineEndsNamespaceScope(const AnnotatedLine *Line, const SmallVectorImpl<AnnotatedLine *> &Lines) { if (!Line) return false; - const FormatToken *tok = Line->First; - if (!tok || tok->isNot(tok::r_brace)) + const FormatToken *Tok = Line->First; + if (!Tok || Tok->isNot(tok::r_brace)) return false; return getNamespaceToken(Line, Lines) != nullptr; } diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index b249bf073aa453..3e6a1d8fa0f486 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -865,6 +865,14 @@ TEST(ConfigParseTest, ParsesConfiguration) { CHECK_PARSE("SortUsingDeclarations: true", SortUsingDeclarations, FormatStyle::SUD_LexicographicNumeric); + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Leave; + CHECK_PARSE("WrapNamespaceBodyWithEmptyLines: Never", + WrapNamespaceBodyWithEmptyLines, FormatStyle::WNBWELS_Never); + CHECK_PARSE("WrapNamespaceBodyWithEmptyLines: Always", + WrapNamespaceBodyWithEmptyLines, FormatStyle::WNBWELS_Always); + CHECK_PARSE("WrapNamespaceBodyWithEmptyLines: Leave", + WrapNamespaceBodyWithEmptyLines, FormatStyle::WNBWELS_Leave); + // FIXME: This is required because parsing a configuration simply overwrites // the first N elements of the list instead of resetting it. Style.ForEachMacros.clear(); >From 42b7a4c7bddf7acdbf4bb356af171eebfe61357a Mon Sep 17 00:00:00 2001 From: dmasloff <dmaslo...@gmail.com> Date: Tue, 17 Sep 2024 18:43:40 +0300 Subject: [PATCH 4/9] Fix invalid tests with CompactNamespaces option --- clang/unittests/Format/FormatTest.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 204eb1d3d8d7df..599a66f5f4269f 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -28460,8 +28460,8 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { Style.CompactNamespaces = true; // Empty namespace - verifyNoChange("namespace N {\n" - "};", + verifyNoChange("namespace N1 { namespace N2 {\n" + "}};", Style); // Single namespace @@ -28656,7 +28656,9 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { Style.CompactNamespaces = true; // Empty namespace - verifyNoChange("namespace N {\n};", Style); + verifyNoChange("namespace N1 { namespace N2 {\n" + "}};", + Style); // Nested namespace verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" >From 7f000309f1326b6a702d13530bba381b7db80f41 Mon Sep 17 00:00:00 2001 From: dmasloff <dmaslo...@gmail.com> Date: Thu, 26 Sep 2024 00:25:17 +0300 Subject: [PATCH 5/9] Reformat tests in FormatTest.cpp --- clang/unittests/Format/FormatTest.cpp | 317 +++++++++++++------------- 1 file changed, 153 insertions(+), 164 deletions(-) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 599a66f5f4269f..7827fa4a037ed6 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -28428,226 +28428,223 @@ TEST_F(FormatTest, ShortNamespacesOption) { } TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { - FormatStyle Style = getLLVMStyle(); + auto Style = getLLVMStyle(); Style.FixNamespaceComments = false; - Style.ShortNamespaceLines = 0; - Style.MaxEmptyLinesToKeep = 2; - Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; - Style.CompactNamespaces = false; - // Empty namespace - verifyNoChange("namespace N {};", Style); + // Empty namespace. + verifyFormat("namespace N {}", Style); - // Single namespace - verifyNoChange("namespace N {\n" - "int f1(int a) { return 2 * a; }\n" - "};", - Style); + // Single namespace. + verifyFormat("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "}", + Style); - // Nested namespace - verifyNoChange("namespace N1 {\n" - "namespace N2 {\n" - "namespace N3 {\n" - "int f1() {\n" - " int a = 1;\n" - " return a;\n" - "}\n" - "}\n" - "}\n" - "}", - Style); + // Nested namespace. + verifyFormat("namespace N1 {\n" + "namespace N2 {\n" + "int a = 1;\n" + "}\n" + "}", + Style); Style.CompactNamespaces = true; - // Empty namespace - verifyNoChange("namespace N1 { namespace N2 {\n" - "}};", - Style); + verifyFormat("namespace N1 { namespace N2 {\n" + "int a = 1;\n" + "}}", + Style); - // Single namespace - verifyNoChange("namespace N {\n" - "int f1(int a) { return 2 * a; }\n" - "};", - Style); + // Removing empty lines. + verifyFormat("namespace N {\n" + "\n" + "int a = 1;\n" + "\n" + "}", + "namespace N {\n" + "\n" + "\n" + "int a = 1;\n" + "\n" + "\n" + "}", + Style); - // Nested namespace - verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" - "int f1() {\n" - " int a = 1;\n" - " return a;\n" - "}\n" - "}}}", - Style); + Style.MaxEmptyLinesToKeep = 0; + + verifyFormat("namespace N {\n" + "int a = 1;\n" + "}", + "namespace N {\n" + "\n" + "\n" + "int a = 1;\n" + "\n" + "\n" + "}", + Style); } TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { - FormatStyle Style = getLLVMStyle(); + auto Style = getLLVMStyle(); Style.FixNamespaceComments = false; - Style.ShortNamespaceLines = 0; - Style.MaxEmptyLinesToKeep = 0; Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always; - Style.CompactNamespaces = false; - - // Empty namespace - verifyNoChange("namespace N {};", Style); - - // Single namespace - verifyNoChange("namespace N {\n" - "\n" - "int f1(int a) { return 2 * a; }\n" - "\n" - "};", - Style); - - // Nested namespace - verifyNoChange("namespace N1 {\n" - "namespace N2 {\n" - "namespace N3 {\n" - "\n" - "int f1() {\n" - " int a = 1;\n" - " return a;\n" - "}\n" - "\n" - "}\n" - "}\n" - "}", - Style); - Style.CompactNamespaces = true; + // Empty namespace. + verifyFormat("namespace N {}", Style); - // Nested namespace - verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" - "\n" - "int f1() {\n" - " int a = 1;\n" - " return a;\n" - "}\n" - "\n" - "}}}", - Style); - - Style.MaxEmptyLinesToKeep = 2; - Style.CompactNamespaces = false; - - // Empty namespace - verifyNoChange("namespace N {};", Style); - - // Single namespace - verifyNoChange("namespace N {\n" - "\n" - "\n" - "void function()\n" - "\n" - "\n" - "};", - Style); + // Single namespace. + verifyFormat("namespace N {\n" + "\n" + "int f1(int a) { return 2 * a; }\n" + "\n" + "}", + Style); - // Nested namespace + // Nested namespace. verifyFormat("namespace N1 {\n" "namespace N2 {\n" - "namespace N3 {\n" - "\n" - "\n" - "int f1() {\n" - " int a = 1;\n" - " return a;\n" - "}\n" "\n" + "int a = 1;\n" "\n" "}\n" - "}\n" "}", - "namespace N1 {\n" - "namespace N2 {\n" + Style); + + // Removing empty lines. + verifyFormat("namespace N {\n" "\n" - "namespace N3 {\n" + "int a = 1;\n" "\n" + "}", + "namespace N {" "\n" - "int f1() {\n" - " int a = 1;\n" - " return a;\n" - "}\n" "\n" + "int a = 1;\n" "\n" - "}\n" "\n" - "}\n" "}", Style); Style.CompactNamespaces = true; - // Nested namespace - verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + // Nested namespace. + verifyFormat("namespace N1 { namespace N2 {\n" + "\n" + "int a = 1;\n" + "\n" + "}}", + Style); + + Style.MaxEmptyLinesToKeep = 2; + + // Nested namespace. + verifyNoChange("namespace N1 { namespace N2 {\n" "\n" "\n" - "int f1() {\n" - " int a = 1;\n" - " return a;\n" - "}\n" + "int a = 1;\n" "\n" "\n" - "}}}", + "}}", Style); -} -TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { - FormatStyle Style = getLLVMStyle(); - Style.FixNamespaceComments = false; - Style.ShortNamespaceLines = 0; - Style.MaxEmptyLinesToKeep = 0; - Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Leave; Style.CompactNamespaces = false; - // Empty namespace - verifyNoChange("namespace N {};", Style); + // Empty namespace. + verifyFormat("namespace N {}", Style); - // Single namespace + // Single namespace. verifyNoChange("namespace N {\n" - "int f1(int a) { return 2 * a; }\n" - "};", + "\n" + "\n" + "int a = 1;\n" + "\n" + "\n" + "}", Style); - // Nested namespace + // Nested namespace. verifyNoChange("namespace N1 {\n" "namespace N2 {\n" - "namespace N3 {\n" - "int f1() {\n" - " int a = 1;\n" - " return a;\n" - "}\n" - "}\n" + "\n" + "\n" + "int a = 1;\n" + "\n" + "\n" "}\n" "}", Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { + auto Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + + // Empty namespace. + verifyFormat("namespace N {}", Style); + + // Single namespace. + verifyFormat("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "}", + Style); + + // Nested namespace. + verifyFormat("namespace N1 {\n" + "namespace N2 {\n" + "int a = 1;\n" + "}\n" + "}", + Style); + + // Removing empty lines. + verifyFormat("namespace N {\n" + "\n" + "int a = 1;\n" + "\n" + "}", + "namespace N {\n" + "\n" + "\n" + "int a = 1;\n" + "\n" + "\n" + "}", + Style); + + Style.MaxEmptyLinesToKeep = 0; + + verifyFormat("namespace N {\n" + "int a = 1;\n" + "}", + "namespace N {\n" + "\n" + "\n" + "int a = 1;\n" + "\n" + "\n" + "}", + Style); Style.MaxEmptyLinesToKeep = 2; - // Single namespace + // Single namespace. verifyNoChange("namespace N {\n" "\n" "\n" "int f1(int a) { return 2 * a; }\n" "\n" "\n" - "};", + "}", Style); - // Nested namespace + // Nested namespace. verifyNoChange("namespace N1 {\n" "namespace N2 {\n" "\n" - "namespace N3 {\n" - "\n" - "\n" - "int f1() {\n" - " int a = 1;\n" - " return a;\n" - "}\n" "\n" + "int a = 1;\n" "\n" - "}\n" "\n" "}\n" "}", @@ -28655,22 +28652,14 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { Style.CompactNamespaces = true; - // Empty namespace + // Nested namespace. verifyNoChange("namespace N1 { namespace N2 {\n" - "}};", - Style); - - // Nested namespace - verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" "\n" "\n" - "int f1() {\n" - " int a = 1;\n" - " return a;\n" - "}\n" + "int a = 1;\n" "\n" "\n" - "}}}", + "}}", Style); } >From fb63750356c950801f437ba513008ea82558cd9e Mon Sep 17 00:00:00 2001 From: dmasloff <dmaslo...@gmail.com> Date: Sun, 17 Nov 2024 02:07:39 +0300 Subject: [PATCH 6/9] Remove reduntant tests and simplify code --- clang/lib/Format/UnwrappedLineFormatter.cpp | 56 +++-------- clang/unittests/Format/FormatTest.cpp | 106 +------------------- 2 files changed, 16 insertions(+), 146 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 8fb93d4eaa0347..b83f0459ab4e26 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -32,26 +32,6 @@ bool isRecordLBrace(const FormatToken &Tok) { TT_StructLBrace, TT_UnionLBrace); } -bool LineStartsNamespaceScope(const AnnotatedLine *Line, - const AnnotatedLine *PreviousLine, - const AnnotatedLine *PrevPrevLine) { - return PreviousLine && - ((PreviousLine->Last->is(tok::l_brace) && - PreviousLine->startsWithNamespace()) || - (PrevPrevLine && PrevPrevLine->startsWithNamespace() && - PreviousLine->startsWith(tok::l_brace))); -} - -bool LineEndsNamespaceScope(const AnnotatedLine *Line, - const SmallVectorImpl<AnnotatedLine *> &Lines) { - if (!Line) - return false; - const FormatToken *Tok = Line->First; - if (!Tok || Tok->isNot(tok::r_brace)) - return false; - return getNamespaceToken(Line, Lines) != nullptr; -} - /// Tracks the indent level of \c AnnotatedLines across levels. /// /// \c nextLine must be called for each \c AnnotatedLine, after which \c @@ -1604,27 +1584,21 @@ static auto computeNewlines(const AnnotatedLine &Line, Newlines = 1; } - // Modify empty lines after "{" that opens namespace scope. - if (Style.WrapNamespaceBodyWithEmptyLines != FormatStyle::WNBWELS_Leave && - LineStartsNamespaceScope(&Line, PreviousLine, PrevPrevLine)) { - if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never) - Newlines = std::min(Newlines, 1u); - else if (!Line.startsWithNamespace()) - Newlines = std::max(Newlines, 2u); - else - Newlines = std::min(Newlines, 1u); - } - - // Modify empty lines before "}" that closes namespace scope. - if (Style.WrapNamespaceBodyWithEmptyLines != FormatStyle::WNBWELS_Leave && - LineEndsNamespaceScope(&Line, Lines)) { - if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never) - Newlines = std::min(Newlines, 1u); - else if (!LineEndsNamespaceScope(PreviousLine, Lines)) - Newlines = std::max(Newlines, 2u); - else - Newlines = std::min(Newlines, 1u); - } + if (Style.WrapNamespaceBodyWithEmptyLines != FormatStyle::WNBWELS_Leave) { + // Modify empty lines after TT_NamespaceLBrace. + if (PreviousLine && PreviousLine->endsWith(TT_NamespaceLBrace)) { + if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never) + Newlines = 1; + else if (!Line.startsWithNamespace()) + Newlines = std::max(Newlines, 2u); + } + // Modify empty lines before TT_NamespaceRBrace. + if (Line.startsWith(TT_NamespaceRBrace)) { + if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never) + Newlines = 1; + else if (!PreviousLine->startsWith(TT_NamespaceRBrace)) + Newlines = std::max(Newlines, 2u); + } // Insert or remove empty line before access specifiers. if (PreviousLine && RootToken.isAccessSpecifier()) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 7827fa4a037ed6..71f6fd6be2f0c4 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -28430,6 +28430,7 @@ TEST_F(FormatTest, ShortNamespacesOption) { TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { auto Style = getLLVMStyle(); Style.FixNamespaceComments = false; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; // Empty namespace. verifyFormat("namespace N {}", Style); @@ -28511,21 +28512,6 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { "}", Style); - // Removing empty lines. - verifyFormat("namespace N {\n" - "\n" - "int a = 1;\n" - "\n" - "}", - "namespace N {" - "\n" - "\n" - "int a = 1;\n" - "\n" - "\n" - "}", - Style); - Style.CompactNamespaces = true; // Nested namespace. @@ -28550,9 +28536,6 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { Style.CompactNamespaces = false; - // Empty namespace. - verifyFormat("namespace N {}", Style); - // Single namespace. verifyNoChange("namespace N {\n" "\n" @@ -28576,93 +28559,6 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { Style); } -TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { - auto Style = getLLVMStyle(); - Style.FixNamespaceComments = false; - - // Empty namespace. - verifyFormat("namespace N {}", Style); - - // Single namespace. - verifyFormat("namespace N {\n" - "int f1(int a) { return 2 * a; }\n" - "}", - Style); - - // Nested namespace. - verifyFormat("namespace N1 {\n" - "namespace N2 {\n" - "int a = 1;\n" - "}\n" - "}", - Style); - - // Removing empty lines. - verifyFormat("namespace N {\n" - "\n" - "int a = 1;\n" - "\n" - "}", - "namespace N {\n" - "\n" - "\n" - "int a = 1;\n" - "\n" - "\n" - "}", - Style); - - Style.MaxEmptyLinesToKeep = 0; - - verifyFormat("namespace N {\n" - "int a = 1;\n" - "}", - "namespace N {\n" - "\n" - "\n" - "int a = 1;\n" - "\n" - "\n" - "}", - Style); - - Style.MaxEmptyLinesToKeep = 2; - - // Single namespace. - verifyNoChange("namespace N {\n" - "\n" - "\n" - "int f1(int a) { return 2 * a; }\n" - "\n" - "\n" - "}", - Style); - - // Nested namespace. - verifyNoChange("namespace N1 {\n" - "namespace N2 {\n" - "\n" - "\n" - "int a = 1;\n" - "\n" - "\n" - "}\n" - "}", - Style); - - Style.CompactNamespaces = true; - - // Nested namespace. - verifyNoChange("namespace N1 { namespace N2 {\n" - "\n" - "\n" - "int a = 1;\n" - "\n" - "\n" - "}}", - Style); -} - } // namespace } // namespace test } // namespace format >From 13cc722a43e5deaa70f63543776e679f6b3daf27 Mon Sep 17 00:00:00 2001 From: dmasloff <dmaslo...@gmail.com> Date: Tue, 31 Dec 2024 23:18:38 +0300 Subject: [PATCH 7/9] minor fix --- clang/lib/Format/UnwrappedLineFormatter.cpp | 1 + clang/unittests/Format/FormatTest.cpp | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index b83f0459ab4e26..bc6766a47f5c70 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -1599,6 +1599,7 @@ static auto computeNewlines(const AnnotatedLine &Line, else if (!PreviousLine->startsWith(TT_NamespaceRBrace)) Newlines = std::max(Newlines, 2u); } + } // Insert or remove empty line before access specifiers. if (PreviousLine && RootToken.isAccessSpecifier()) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 71f6fd6be2f0c4..dd99f46a1a6d47 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -28458,9 +28458,7 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { // Removing empty lines. verifyFormat("namespace N {\n" - "\n" "int a = 1;\n" - "\n" "}", "namespace N {\n" "\n" >From 94509edbfca396047f35ff909b149df0c91f5690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Clement=20=28=E3=83=90=E3=83=AC=E3=83=B3?= =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=B3=20=E3=82=AF=E3=83=AC=E3=83=A1?= =?UTF-8?q?=E3=83=B3=29?= <clement...@gmail.com> Date: Tue, 27 Aug 2024 17:36:14 -0700 Subject: [PATCH 8/9] [flang][cuda] Add missing dependency (#106298) Add missing dependency that sometimes makes a build fails with ninja. >From ebe57d3ec4ad1dc9a9cfb089ee616f98678f8fa8 Mon Sep 17 00:00:00 2001 From: dmasloff <dmaslo...@gmail.com> Date: Thu, 2 Jan 2025 21:56:34 +0300 Subject: [PATCH 9/9] Adds review suggestions + adds option to clang/docs/ReleaseNotes.rst --- clang/docs/ReleaseNotes.rst | 1 + clang/unittests/Format/FormatTest.cpp | 112 +++++++++++++------------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index b7da12bcf65818..4204fa1336be4b 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -1124,6 +1124,7 @@ clang-format - Adds ``RemoveEmptyLinesInUnwrappedLines`` option. - Adds ``KeepFormFeed`` option and set it to ``true`` for ``GNU`` style. - Adds ``AllowShortNamespacesOnASingleLine`` option. +- Adds ``WrapNamespaceBodyWithEmptyLines`` option. libclang -------- diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index dd99f46a1a6d47..44b9dba2498900 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -28430,6 +28430,7 @@ TEST_F(FormatTest, ShortNamespacesOption) { TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { auto Style = getLLVMStyle(); Style.FixNamespaceComments = false; + Style.MaxEmptyLinesToKeep = 2; Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; // Empty namespace. @@ -28439,6 +28440,13 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { verifyFormat("namespace N {\n" "int f1(int a) { return 2 * a; }\n" "}", + "namespace N {\n" + "\n" + "\n" + "int f1(int a) { return 2 * a; }\n" + "\n" + "\n" + "}", Style); // Nested namespace. @@ -28447,46 +28455,38 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { "int a = 1;\n" "}\n" "}", - Style); - - Style.CompactNamespaces = true; - - verifyFormat("namespace N1 { namespace N2 {\n" - "int a = 1;\n" - "}}", - Style); - - // Removing empty lines. - verifyFormat("namespace N {\n" - "int a = 1;\n" - "}", - "namespace N {\n" + "namespace N1 {\n" "\n" "\n" + "namespace N2 {\n" + "\n" "int a = 1;\n" "\n" + "}\n" + "\n" "\n" "}", Style); - Style.MaxEmptyLinesToKeep = 0; + Style.CompactNamespaces = true; - verifyFormat("namespace N {\n" + verifyFormat("namespace N1 { namespace N2 {\n" "int a = 1;\n" - "}", - "namespace N {\n" + "}}", + "namespace N1 { namespace N2 {\n" "\n" "\n" "int a = 1;\n" "\n" "\n" - "}", + "}}", Style); } TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { auto Style = getLLVMStyle(); Style.FixNamespaceComments = false; + Style.MaxEmptyLinesToKeep = 2; Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always; // Empty namespace. @@ -28498,6 +28498,9 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { "int f1(int a) { return 2 * a; }\n" "\n" "}", + "namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "}", Style); // Nested namespace. @@ -28508,53 +28511,50 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { "\n" "}\n" "}", + "namespace N1 {\n" + "namespace N2 {\n" + "int a = 1;\n" + "}\n" + "}", + Style); + + verifyFormat("namespace N1 {\n" + "\n" + "namespace N2 {\n" + "\n" + "\n" + "int a = 1;\n" + "\n" + "\n" + "}\n" + "\n" + "}", + "namespace N1 {\n" + "\n" + "namespace N2 {\n" + "\n" + "\n" + "\n" + "int a = 1;\n" + "\n" + "\n" + "\n" + "}\n" + "\n" + "}", Style); Style.CompactNamespaces = true; - // Nested namespace. verifyFormat("namespace N1 { namespace N2 {\n" "\n" "int a = 1;\n" "\n" "}}", + "namespace N1 { namespace N2 {\n" + "int a = 1;\n" + "}}", Style); - - Style.MaxEmptyLinesToKeep = 2; - - // Nested namespace. - verifyNoChange("namespace N1 { namespace N2 {\n" - "\n" - "\n" - "int a = 1;\n" - "\n" - "\n" - "}}", - Style); - - Style.CompactNamespaces = false; - - // Single namespace. - verifyNoChange("namespace N {\n" - "\n" - "\n" - "int a = 1;\n" - "\n" - "\n" - "}", - Style); - - // Nested namespace. - verifyNoChange("namespace N1 {\n" - "namespace N2 {\n" - "\n" - "\n" - "int a = 1;\n" - "\n" - "\n" - "}\n" - "}", - Style); } } // namespace _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits