Page MenuHomePhabricator

Fix selective formatting of ObjC scope
Needs ReviewPublic

Authored by acoomans on Aug 9 2018, 2:21 PM.

Details

Reviewers
cfe-commits
Summary

ObjC scopes gets formatted beyond the @end directive, when formatting specific lines (clang-format -lines=x:x).

This changeset fixes formatting of @end the same way that r_braces are.
E.g: running clang-format -lines=3:3 on (note the extra white space in front of @optional):

@protocol A
 @optional
// comment
- (void)f;
@end
MACRO

would be formatted:

@protocol A
 @optional
 // comment
 - (void)f;
@end
MACRO

instead of:

@protocol A
 @optional
 // comment
 - (void)f;
 @end
 MACRO

Initial bug report / feature request: https://bugs.llvm.org/show_bug.cgi?id=37975

Diff Detail

Repository
rC Clang

Event Timeline

acoomans created this revision.Aug 9 2018, 2:21 PM
acoomans edited the summary of this revision. (Show Details)Aug 9 2018, 2:24 PM

I probably don't need a unit test AND a lit test, but I'm unsure which one is best here. Any suggestion?

lib/Format/UnwrappedLineFormatter.cpp
1045–1047

Note to self: change the style to match PreviousRBrace

@jolesiak let me know what you think

Nice! That looks very promising.

It still fails when we pass a longer range (maybe mimicking PreviousRBrace will help), e.g.:
Base code:

@protocol A
 @optional
// comment
- (void)f;
@end
MACRO

formatted with clang-format -lines=3:6 file.m gives:

@protocol A
 @optional
 // comment
 - (void)f;
 @end
 MACRO

This is not happening for Cpp, e.g.:
Base code:

class A {
 void f ();
// comment
void g ();
};
MACRO

running clang-format -lines=3:6 file.cpp gives:

class A {
 void f ();
 // comment
 void g ();
};
MACRO

I think it would be good to add all these tests to unit tests, but I see that an interface to pass line ranges is not very pleasant.