travisjeffery / ClangFormat-Xcode

Xcode plug-in to to use clang-format from in Xcode and consistently format your code with Clang
https://twitter.com/travisjeffery
MIT License
2.89k stars 288 forks source link

ClangFormat incorrectly splits Objective-C string literals #46

Closed ghost closed 10 years ago

ghost commented 10 years ago

I cloned and pulled the latest release of ClangFormat, which still exhibits this bug. An NSArray literal containing NSString literals before running ClangFormat:

  NSArray *testInputStrings = @[
    @"abcdefg",
    @"*^&I*BEI*BDI C# RG#RC#YJGR #",
    @"那隻敏捷的棕色狐狸跳線懶狗。",
    @"1",
    @"12",
    @"123",
    @"1234",
    @"12345",
    @"123456",
    @"1234567",
    @"12345678",
    @"123456789",
    @"1234567890",
 @"kljsadgflkuyaerkuqhfbdsalkfbalkshdeklahdskjbfaskljhraslkrhuwbdbhsCKJhbasjkhfawkjhgrkjaebfdjkahsbdfjkhawkjfegkqrwjygr3uy24764587321rgj32hvr4jkh2vrkj2vfqrfqwjkh*&^*^&*RIBIEBKHBDJHABJEHBjqdbwjy3egjwybdehjbdfjsdhbfjsehbfjhsbfsjhbfdjshbfsmdbfshfbejw3jbfdsmhbdmshbfs"
  ];

The same NSArray literal after running ClangFormat:

  NSArray *testInputStrings = @[
    @"abcdefg",
    @"*^&I*BEI*BDI C# RG#RC#YJGR #",
    @"那隻敏捷的棕色狐狸跳線懶狗。",
    @"1",
    @"12",
    @"123",
    @"1234",
    @"12345",
    @"123456",
    @"1234567",
    @"12345678",
    @"123456789",
    @"1234567890",
    @"kljsadgflkuyaerkuqhfbdsalkfbalkshdeklahdskjbfaskljhraslkrhuwbdbhsCKJhbasj"
    @"khfawkjhgrkjaebfdjkahsbdfjkhawkjfegkqrwjygr3uy24764587321rgj32hvr4jkh2vr"
    @"kj2vfqrfqwjkh*&^*^&*"
    @"RIBIEBKHBDJHABJEHBjqdbwjy3egjwybdehjbdfjsdhbfjsehbfjhsbfsjhbfdjshbfsmdbf"
    @"shfbejw3jbfdsmhbdmshbfs"
  ];
andrewtj commented 10 years ago

Possibly a naive question but what is the bug? Both C and Objective-C string literals are concatenated when they're beside each other (so @"foo" @"bah" is the same as @"foobah"), so these would seem to be identical to my eye. If this is what you're referring to a ColumnLimit of 0 might give you the behaviour you were expecting.

travisjeffery commented 10 years ago

ya that is actually the same array / strings. clang is splitting that string over multiple lines to fit the column limit.

tylermann commented 9 years ago

@travisjeffery Hmm, so is this the expected behavior even if you have the following config: ColumnLimit: 100 PenaltyBreakString: 10000 PenaltyExcessCharacter: 1

It still seems to me like there is a bug unless I am just confused about the behavior of this.

I would expect a 200 character string to have a penalty of 100 from the excess characters, which would be less than the 10000 penalty from breaking the string, however it appears to just always split the string if you have a ColumnLimit > 0 despite the penalties.

Unfortunately we tend to get a lot of these in our codebase, that end up causing clang warnings from "Concatenated NSString literal for an NSArray expression - possibly missing a comma"

timbodeit commented 9 years ago

Have set PenaltyBreakString: 2147483647 and clang-format is still breaking Objective-C string literals.

In my eyes, this issue should be reopened

xilin commented 9 years ago

It also seems to be a bug for me. I thought that PenaltyBreakString should always have a higher priority than the ColumnLimit. Otherwise, is there a way for us to keep these two settings effective at the same time?

tonyarnold commented 9 years ago

In my eyes, this issue should be reopened

It's not an issue with ClangFormat-Xcode. You need to report these issues to the LLVM Bugzilla.