https://github.com/vkpatel186 created https://github.com/llvm/llvm-project/pull/170579
[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. >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] [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" _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
