https://github.com/vkpatel186 updated https://github.com/llvm/llvm-project/pull/170579
>From 5961395d09a79c8bf06392eb388b3f41ec89ecc7 Mon Sep 17 00:00:00 2001 From: Vikas Patel <[email protected]> Date: Thu, 4 Dec 2025 00:07:17 +0000 Subject: [PATCH 1/2] [clang-format] Add ObjCSpaceBeforeMethodDeclColon option to control space before Objective-C method return type This patch introduces the ObjCSpaceBeforeMethodDeclColon style option, allowing users to add or remove a space between the '-'/'+' and the return type in Objective-C method declarations (e.g., '- (void)method' vs '-(void)method'). Includes documentation and unit tests. --- clang/docs/ClangFormatStyleOptions.rst | 6 ++++++ clang/include/clang/Format/Format.h | 8 +++++++- clang/lib/Format/Format.cpp | 3 +++ clang/lib/Format/TokenAnnotator.cpp | 2 +- clang/unittests/Format/FormatTest.cpp | 16 ++++++++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 4f81a084dd65b..dcd59a8820231 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -5538,6 +5538,12 @@ the configuration (without a prefix: ``Auto``). Add a space after ``@property`` in Objective-C, i.e. use ``@property (readonly)`` instead of ``@property(readonly)``. +.. _ObjCSpaceBeforeMethodDeclColon: + +**ObjCSpaceBeforeMethodDeclColon** (``Boolean``) :versionbadge:`clang-format 23` :ref:`¶ <ObjCSpaceBeforeMethodDeclColon>` + Add or remove a space between the '-'/'+' and the return type in Objective-C method declarations, + i.e. use '- (void)method' instead of '-(void)method'. + .. _ObjCSpaceBeforeProtocolList: **ObjCSpaceBeforeProtocolList** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ <ObjCSpaceBeforeProtocolList>` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index c7e57d47f9ed1..a0c7310f80ff4 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -3936,6 +3936,11 @@ struct FormatStyle { /// \version 3.7 bool ObjCSpaceAfterProperty; + /// Add or remove a space between the '-'/'+' and the return type in Objective-C method declarations, + /// i.e. use '- (void)method' instead of '-(void)method'. + /// \version 23 + bool ObjCSpaceBeforeMethodDeclColon; + /// Add a space in front of an Objective-C protocol list, i.e. use /// ``Foo <Protocol>`` instead of ``Foo<Protocol>``. /// \version 3.7 @@ -5845,7 +5850,8 @@ struct FormatStyle { VerilogBreakBetweenInstancePorts == R.VerilogBreakBetweenInstancePorts && WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros && - WrapNamespaceBodyWithEmptyLines == R.WrapNamespaceBodyWithEmptyLines; + WrapNamespaceBodyWithEmptyLines == R.WrapNamespaceBodyWithEmptyLines && + ObjCSpaceBeforeMethodDeclColon == R.ObjCSpaceBeforeMethodDeclColon; } std::optional<FormatStyle> GetLanguageStyle(LanguageKind Language) const; diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index f0e9aff2fd21a..db184da18cc99 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1353,6 +1353,8 @@ template <> struct MappingTraits<FormatStyle> { Style.WhitespaceSensitiveMacros); IO.mapOptional("WrapNamespaceBodyWithEmptyLines", Style.WrapNamespaceBodyWithEmptyLines); + IO.mapOptional("ObjCSpaceBeforeMethodDeclColon", + Style.ObjCSpaceBeforeMethodDeclColon); // If AlwaysBreakAfterDefinitionReturnType was specified but // BreakAfterReturnType was not, initialize the latter from the former for @@ -1788,6 +1790,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.ObjCBlockIndentWidth = 2; LLVMStyle.ObjCBreakBeforeNestedBlockParam = true; LLVMStyle.ObjCSpaceAfterProperty = false; + LLVMStyle.ObjCSpaceBeforeMethodDeclColon = true; LLVMStyle.ObjCSpaceBeforeProtocolList = true; LLVMStyle.PackConstructorInitializers = FormatStyle::PCIS_BinPack; LLVMStyle.PointerAlignment = FormatStyle::PAS_Right; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 79cfa73001e54..af48d804b4518 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -5437,7 +5437,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, return Right.hasWhitespaceBefore(); if (Line.Type == LT_ObjCMethodDecl) { if (Left.is(TT_ObjCMethodSpecifier)) - return true; + return Style.ObjCSpaceBeforeMethodDeclColon; if (Left.is(tok::r_paren) && Left.isNot(TT_AttributeRParen) && canBeObjCSelectorComponent(Right)) { // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 3ff784035dd44..bd98051872fcf 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -15623,6 +15623,22 @@ TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { verifyGoogleFormat("- foo:(int)foo;"); } +TEST_F(FormatTest, SpaceBeforeObjCMethodDeclColon) { + FormatStyle Style = getLLVMStyle(); + verifyFormat("- (void)method;", "-(void)method;", Style); + verifyFormat("+ (int)foo:(int)x;", "+ (int) foo:(int)x;", Style); + verifyFormat("- foo;", "-foo;", Style); + verifyFormat("- foo:(int)f;", "-foo:(int)f;", Style); + + Style.ObjCSpaceBeforeMethodDeclColon = false; + verifyFormat("-(void)method;", "- (void) method;", Style); + verifyFormat("+(int)foo:(int)x;", "+ (int)foo:(int)x;", Style); + verifyFormat("+(int)foo:(int)x;", "+ (int)foo:(int)x;", Style); + + verifyFormat("-foo;", "- foo;", Style); + verifyFormat("-foo:(int)f;", "- foo:(int)f;", Style); +} + TEST_F(FormatTest, BreaksStringLiterals) { // FIXME: unstable test case EXPECT_EQ("\"some text \"\n" >From f10f688d37adf4afab5cc226e374a156cabba9b9 Mon Sep 17 00:00:00 2001 From: Vikas Patel <[email protected]> Date: Thu, 4 Dec 2025 23:31:05 +0000 Subject: [PATCH 2/2] Fix review comments --- clang/docs/ClangFormatStyleOptions.rst | 12 +++++++++--- clang/docs/ReleaseNotes.rst | 2 ++ clang/include/clang/Format/Format.h | 15 ++++++++++----- clang/lib/Format/Format.cpp | 4 ++-- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index dcd59a8820231..1bd34fc7e3ba2 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -5540,9 +5540,15 @@ the configuration (without a prefix: ``Auto``). .. _ObjCSpaceBeforeMethodDeclColon: -**ObjCSpaceBeforeMethodDeclColon** (``Boolean``) :versionbadge:`clang-format 23` :ref:`¶ <ObjCSpaceBeforeMethodDeclColon>` - Add or remove a space between the '-'/'+' and the return type in Objective-C method declarations, - i.e. use '- (void)method' instead of '-(void)method'. +**ObjCSpaceBeforeMethodDeclColon** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <ObjCSpaceBeforeMethodDeclColon>` + Add or remove a space between the '-'/'+' and the return type in + Objective-C method declarations. i.e + + .. code-block:: objc + + false: true: + + -(void)method vs. - (void)method .. _ObjCSpaceBeforeProtocolList: diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 654a8e48cd104..6cbb096c411c3 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -725,6 +725,8 @@ clang-format - Rename ``(Binary|Decimal|Hex)MinDigits`` to ``...MinDigitsInsert`` and add ``(Binary|Decimal|Hex)MaxDigitsSeparator`` suboptions to ``IntegerLiteralSeparator``. +- Add ``ObjCSpaceBeforeMethodDeclColon`` option to control space between the + '-'/'+' and the return type in Objective-C method declarations libclang -------- diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index a0c7310f80ff4..b1042b977645f 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -3936,9 +3936,14 @@ struct FormatStyle { /// \version 3.7 bool ObjCSpaceAfterProperty; - /// Add or remove a space between the '-'/'+' and the return type in Objective-C method declarations, - /// i.e. use '- (void)method' instead of '-(void)method'. - /// \version 23 + /// Add or remove a space between the '-'/'+' and the return type in + /// Objective-C method declarations. i.e + /// \code{.objc} + /// false: true: + /// + /// -(void)method vs. - (void)method + /// \endcode + /// \version 22 bool ObjCSpaceBeforeMethodDeclColon; /// Add a space in front of an Objective-C protocol list, i.e. use @@ -5778,6 +5783,7 @@ struct FormatStyle { ObjCPropertyAttributeOrder == R.ObjCPropertyAttributeOrder && ObjCSpaceAfterProperty == R.ObjCSpaceAfterProperty && ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList && + ObjCSpaceBeforeMethodDeclColon == R.ObjCSpaceBeforeMethodDeclColon && OneLineFormatOffRegex == R.OneLineFormatOffRegex && PackConstructorInitializers == R.PackConstructorInitializers && PenaltyBreakAssignment == R.PenaltyBreakAssignment && @@ -5850,8 +5856,7 @@ struct FormatStyle { VerilogBreakBetweenInstancePorts == R.VerilogBreakBetweenInstancePorts && WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros && - WrapNamespaceBodyWithEmptyLines == R.WrapNamespaceBodyWithEmptyLines && - ObjCSpaceBeforeMethodDeclColon == R.ObjCSpaceBeforeMethodDeclColon; + 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 db184da18cc99..04ea6b094cbcd 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1246,6 +1246,8 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("ObjCPropertyAttributeOrder", Style.ObjCPropertyAttributeOrder); IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty); + IO.mapOptional("ObjCSpaceBeforeMethodDeclColon", + Style.ObjCSpaceBeforeMethodDeclColon); IO.mapOptional("ObjCSpaceBeforeProtocolList", Style.ObjCSpaceBeforeProtocolList); IO.mapOptional("OneLineFormatOffRegex", Style.OneLineFormatOffRegex); @@ -1353,8 +1355,6 @@ template <> struct MappingTraits<FormatStyle> { Style.WhitespaceSensitiveMacros); IO.mapOptional("WrapNamespaceBodyWithEmptyLines", Style.WrapNamespaceBodyWithEmptyLines); - IO.mapOptional("ObjCSpaceBeforeMethodDeclColon", - Style.ObjCSpaceBeforeMethodDeclColon); // If AlwaysBreakAfterDefinitionReturnType was specified but // BreakAfterReturnType was not, initialize the latter from the former for _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
