https://github.com/jwobser updated https://github.com/llvm/llvm-project/pull/182432
>From 909719f55e427378cd346fce96d4175acf058a5b Mon Sep 17 00:00:00 2001 From: James Wobser <[email protected]> Date: Thu, 19 Feb 2026 20:59:42 -0500 Subject: [PATCH 1/4] [clang-format] bugfix: Whitesmiths with IndentAccessModifiers Because of the special casing for Whitesmiths when parsing, the second level indent for records is not applied. Then, when calculating the offset for the access specifier, it is moved out one level, putting it back at the class level. This change adds a special case for Whitesmiths to apply the second indent if IndentAccessModifiers is applied. It relies on AddLevels being either 1 or 2. --- clang/lib/Format/UnwrappedLineParser.cpp | 5 +++++ clang/test/Format/Whitesmiths-accessmodifiers.cpp | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 clang/test/Format/Whitesmiths-accessmodifiers.cpp diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index f57ef1328eac7..0cd61f97dbaa4 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -805,6 +805,11 @@ FormatToken *UnwrappedLineParser::parseBlock(bool MustBeDeclaration, if (AddLevels > 0u && Style.BreakBeforeBraces != FormatStyle::BS_Whitesmiths) Line->Level += AddLevels; + // One level has already been added for Whitesmiths at this point, + // this adds the second level if needed + if (AddLevels > 0u && Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) + Line->Level += AddLevels - 1; + FormatToken *IfLBrace = nullptr; const bool SimpleBlock = parseLevel(Tok, IfKind, &IfLBrace); diff --git a/clang/test/Format/Whitesmiths-accessmodifiers.cpp b/clang/test/Format/Whitesmiths-accessmodifiers.cpp new file mode 100644 index 0000000000000..0a22879c5d19e --- /dev/null +++ b/clang/test/Format/Whitesmiths-accessmodifiers.cpp @@ -0,0 +1,15 @@ +// RUN: grep -Ev "// *[A-Z-]+:" %s \ +// RUN: | clang-format -style="{BasedOnStyle: LLVM, IndentAccessModifiers: true, BreakBeforeBraces: Whitesmiths}" \ +// RUN: | FileCheck -strict-whitespace %s + +// CHECK: struct foo1 +// CHECK-NEXT: {{^ {}} +// CHECK-NEXT: {{^ int i;}} +// CHECK-EMPTY: +// CHECK-NEXT: {{^ private:}} +// CHECK-NEXT: {{^ int j;}} +// CHECK: {{^ [}]}} +struct foo1 { +int i; +private: +int j; }; >From 83ae1e2f94e42033fcbd085cb363d9da5171774c Mon Sep 17 00:00:00 2001 From: James Wobser <[email protected]> Date: Fri, 20 Feb 2026 22:23:59 -0500 Subject: [PATCH 2/4] Move test to FormatTest unittests --- .../test/Format/Whitesmiths-accessmodifiers.cpp | 15 --------------- clang/unittests/Format/FormatTest.cpp | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 15 deletions(-) delete mode 100644 clang/test/Format/Whitesmiths-accessmodifiers.cpp diff --git a/clang/test/Format/Whitesmiths-accessmodifiers.cpp b/clang/test/Format/Whitesmiths-accessmodifiers.cpp deleted file mode 100644 index 0a22879c5d19e..0000000000000 --- a/clang/test/Format/Whitesmiths-accessmodifiers.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// RUN: grep -Ev "// *[A-Z-]+:" %s \ -// RUN: | clang-format -style="{BasedOnStyle: LLVM, IndentAccessModifiers: true, BreakBeforeBraces: Whitesmiths}" \ -// RUN: | FileCheck -strict-whitespace %s - -// CHECK: struct foo1 -// CHECK-NEXT: {{^ {}} -// CHECK-NEXT: {{^ int i;}} -// CHECK-EMPTY: -// CHECK-NEXT: {{^ private:}} -// CHECK-NEXT: {{^ int j;}} -// CHECK: {{^ [}]}} -struct foo1 { -int i; -private: -int j; }; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index d6c49163abbc9..bfebe05c77c29 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -27321,6 +27321,23 @@ TEST_F(FormatTest, IndentAccessModifiers) { " int i;\n" "};", Style); + + Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; + verifyFormat("struct S\n" + " {\n" + " public:\n" + " int i;\n" + "\n" + " private:\n" + " class C\n" + " {\n" + " private:\n" + " int j;\n" + " };\n" + " };\n", + Style); + + Style.BreakBeforeBraces = FormatStyle::BS_Attach; // Enumerations are not records and should be unaffected. Style.AllowShortEnumsOnASingleLine = false; verifyFormat("enum class E {\n" >From 5bb47a61be5b2500b51c2b2e65ab982049209d50 Mon Sep 17 00:00:00 2001 From: James Wobser <[email protected]> Date: Fri, 20 Feb 2026 23:00:07 -0500 Subject: [PATCH 3/4] Combine similar if statements into a single one --- clang/lib/Format/UnwrappedLineParser.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 0cd61f97dbaa4..3f95c8cfdfb7d 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -762,12 +762,13 @@ FormatToken *UnwrappedLineParser::parseBlock(bool MustBeDeclaration, const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin); FormatTok->setBlockKind(BK_Block); + const bool Whitesmiths = + Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths; + // For Whitesmiths mode, jump to the next level prior to skipping over the // braces. - if (!VerilogHierarchy && AddLevels > 0 && - Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) { + if (!VerilogHierarchy && AddLevels > 0 && Whitesmiths) ++Line->Level; - } size_t PPStartHash = computePPHash(); @@ -802,13 +803,11 @@ FormatToken *UnwrappedLineParser::parseBlock(bool MustBeDeclaration, ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, MustBeDeclaration); - if (AddLevels > 0u && Style.BreakBeforeBraces != FormatStyle::BS_Whitesmiths) - Line->Level += AddLevels; - // One level has already been added for Whitesmiths at this point, - // this adds the second level if needed - if (AddLevels > 0u && Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) - Line->Level += AddLevels - 1; + // Whitesmiths logic has already added a level by this point, so avoid + // adding it twice. + if (AddLevels > 0u) + Line->Level += AddLevels - (Whitesmiths ? 1 : 0); FormatToken *IfLBrace = nullptr; const bool SimpleBlock = parseLevel(Tok, IfKind, &IfLBrace); >From 79b5af6748c18bf4d371d842f4375e448c6ccac9 Mon Sep 17 00:00:00 2001 From: James Wobser <[email protected]> Date: Sat, 21 Feb 2026 14:23:56 -0500 Subject: [PATCH 4/4] Rename Whitesmiths flag boolean --- clang/lib/Format/UnwrappedLineParser.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 3f95c8cfdfb7d..d76a74349fe04 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -762,12 +762,12 @@ FormatToken *UnwrappedLineParser::parseBlock(bool MustBeDeclaration, const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin); FormatTok->setBlockKind(BK_Block); - const bool Whitesmiths = + const bool IsWhitesmiths = Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths; // For Whitesmiths mode, jump to the next level prior to skipping over the // braces. - if (!VerilogHierarchy && AddLevels > 0 && Whitesmiths) + if (!VerilogHierarchy && AddLevels > 0 && IsWhitesmiths) ++Line->Level; size_t PPStartHash = computePPHash(); @@ -807,7 +807,7 @@ FormatToken *UnwrappedLineParser::parseBlock(bool MustBeDeclaration, // Whitesmiths logic has already added a level by this point, so avoid // adding it twice. if (AddLevels > 0u) - Line->Level += AddLevels - (Whitesmiths ? 1 : 0); + Line->Level += AddLevels - (IsWhitesmiths ? 1 : 0); FormatToken *IfLBrace = nullptr; const bool SimpleBlock = parseLevel(Tok, IfKind, &IfLBrace); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
