[clang] 90cc5a4 - [clang-format] Add ObjCSpaceBeforeMethodDeclColon option to control space before Objective-C method return type (#170579)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 27 11:55:25 PST 2026
Author: Vikas Patel
Date: 2026-01-27T19:55:20Z
New Revision: 90cc5a4618903e0832517f3be0d58deaf9f49c2c
URL: https://github.com/llvm/llvm-project/commit/90cc5a4618903e0832517f3be0d58deaf9f49c2c
DIFF: https://github.com/llvm/llvm-project/commit/90cc5a4618903e0832517f3be0d58deaf9f49c2c.diff
LOG: [clang-format] Add ObjCSpaceBeforeMethodDeclColon option to control space before Objective-C method return type (#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.
Added:
Modified:
clang/docs/ClangFormatStyleOptions.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/ConfigParseTest.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 4f81a084dd65b..5ba117c231ad5 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -5532,6 +5532,18 @@ the configuration (without a prefix: ``Auto``).
nullable, nonnull, null_resettable, null_unspecified
]
+.. _ObjCSpaceAfterMethodDeclarationPrefix:
+
+**ObjCSpaceAfterMethodDeclarationPrefix** (``Boolean``) :versionbadge:`clang-format 23` :ref:`¶ <ObjCSpaceAfterMethodDeclarationPrefix>`
+ 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
+
.. _ObjCSpaceAfterProperty:
**ObjCSpaceAfterProperty** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ <ObjCSpaceAfterProperty>`
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4d2ff6e6e4e40..489a91d439133 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -302,6 +302,8 @@ AST Matchers
clang-format
------------
+- Add ``ObjCSpaceAfterMethodDeclarationPrefix`` 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 c7e57d47f9ed1..43bea4b80cb8a 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -3931,6 +3931,16 @@ struct FormatStyle {
/// \version 18
std::vector<std::string> ObjCPropertyAttributeOrder;
+ /// 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 23
+ bool ObjCSpaceAfterMethodDeclarationPrefix;
+
/// Add a space after ``@property`` in Objective-C, i.e. use
/// ``@property (readonly)`` instead of ``@property(readonly)``.
/// \version 3.7
@@ -5771,6 +5781,8 @@ struct FormatStyle {
ObjCBreakBeforeNestedBlockParam ==
R.ObjCBreakBeforeNestedBlockParam &&
ObjCPropertyAttributeOrder == R.ObjCPropertyAttributeOrder &&
+ ObjCSpaceAfterMethodDeclarationPrefix ==
+ R.ObjCSpaceAfterMethodDeclarationPrefix &&
ObjCSpaceAfterProperty == R.ObjCSpaceAfterProperty &&
ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList &&
OneLineFormatOffRegex == R.OneLineFormatOffRegex &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index f0e9aff2fd21a..1e68de531791f 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1245,6 +1245,8 @@ template <> struct MappingTraits<FormatStyle> {
Style.ObjCBreakBeforeNestedBlockParam);
IO.mapOptional("ObjCPropertyAttributeOrder",
Style.ObjCPropertyAttributeOrder);
+ IO.mapOptional("ObjCSpaceAfterMethodDeclarationPrefix",
+ Style.ObjCSpaceAfterMethodDeclarationPrefix);
IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
IO.mapOptional("ObjCSpaceBeforeProtocolList",
Style.ObjCSpaceBeforeProtocolList);
@@ -1787,6 +1789,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
LLVMStyle.ObjCBlockIndentWidth = 2;
LLVMStyle.ObjCBreakBeforeNestedBlockParam = true;
+ LLVMStyle.ObjCSpaceAfterMethodDeclarationPrefix = true;
LLVMStyle.ObjCSpaceAfterProperty = false;
LLVMStyle.ObjCSpaceBeforeProtocolList = true;
LLVMStyle.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 3208d8e28dd86..b1c1afbf8684d 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5491,7 +5491,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
return Right.hasWhitespaceBefore();
if (Line.Type == LT_ObjCMethodDecl) {
if (Left.is(TT_ObjCMethodSpecifier))
- return true;
+ return Style.ObjCSpaceAfterMethodDeclarationPrefix;
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/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index fec1c48c448d2..0a116b770f52a 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -203,6 +203,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL_FIELD(KeepEmptyLines.AtStartOfBlock,
"KeepEmptyLinesAtTheStartOfBlocks");
CHECK_PARSE_BOOL(KeepFormFeed);
+ CHECK_PARSE_BOOL(ObjCSpaceAfterMethodDeclarationPrefix);
CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
CHECK_PARSE_BOOL(RemoveBracesLLVM);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 3ee7ce38578aa..33836e28289b4 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -15623,6 +15623,14 @@ TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
verifyGoogleFormat("- foo:(int)foo;");
}
+TEST_F(FormatTest, SpaceBeforeObjCMethodDeclColon) {
+ auto Style = getLLVMStyle();
+ EXPECT_TRUE(Style.ObjCSpaceAfterMethodDeclarationPrefix);
+ verifyFormat("- (void)method;", Style);
+ Style.ObjCSpaceAfterMethodDeclarationPrefix = false;
+ verifyFormat("-(void)method;", Style);
+}
+
TEST_F(FormatTest, BreaksStringLiterals) {
// FIXME: unstable test case
EXPECT_EQ("\"some text \"\n"
More information about the cfe-commits
mailing list