Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5538,6 +5538,18 @@ 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 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:

**ObjCSpaceBeforeProtocolList** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ <ObjCSpaceBeforeProtocolList>`
Expand Down
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,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
--------
Expand Down
11 changes: 11 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3936,6 +3936,16 @@ 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
/// \code{.objc}
/// false: true:
///
/// -(void)method vs. - (void)method
/// \endcode
/// \version 22
bool ObjCSpaceBeforeMethodDeclColon;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about ObjCSpaceAfterMethodDeclarationPrefix instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its good suggestion i will update it thanks :)


/// Add a space in front of an Objective-C protocol list, i.e. use
/// ``Foo <Protocol>`` instead of ``Foo<Protocol>``.
/// \version 3.7
Expand Down Expand Up @@ -5772,6 +5782,7 @@ struct FormatStyle {
R.ObjCBreakBeforeNestedBlockParam &&
ObjCPropertyAttributeOrder == R.ObjCPropertyAttributeOrder &&
ObjCSpaceAfterProperty == R.ObjCSpaceAfterProperty &&
ObjCSpaceBeforeMethodDeclColon == R.ObjCSpaceBeforeMethodDeclColon &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still one line up. :)

ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList &&
OneLineFormatOffRegex == R.OneLineFormatOffRegex &&
PackConstructorInitializers == R.PackConstructorInitializers &&
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
verifyFormat("- (void)method;", "-(void)method;", Style);
verifyFormat("- (void)method;", Style);

Should suffice, or what is the benefit of the other tests?

verifyFormat("+ (int)foo:(int)x;", "+ (int) foo:(int)x;", Style);
verifyFormat("- foo;", "-foo;", Style);
verifyFormat("- foo:(int)f;", "-foo:(int)f;", Style);

Comment on lines +15627 to +15632
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
auto Style = getLLVMStyle();
EXPECT_TRUE(Style. ObjCSpaceBeforeMethodDeclColon);

Style.ObjCSpaceBeforeMethodDeclColon = false;
verifyFormat("-(void)method;", "- (void) method;", Style);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
verifyFormat("-(void)method;", "- (void) method;", Style);
verifyFormat("-(void)method;", Style);

Same here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to confirm that these changes don’t impact any other code paths. I aimed to cover all possible combinations.

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);
Comment on lines +15635 to +15639
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete.

}

TEST_F(FormatTest, BreaksStringLiterals) {
// FIXME: unstable test case
EXPECT_EQ("\"some text \"\n"
Expand Down
Loading