This is an archive of the discontinued LLVM Phabricator instance.

clang-format: Add two new formatting options
AcceptedPublic

Authored by rsmmr on Oct 2 2016, 6:31 PM.

Details

Summary

This patch adds two new options, feedback welcome.

SpacesAroundConditions (bool)

If true, spaces will be inserted around if/for/while conditions.

SpacesAfterNot (bool)

If true, spaces will be inserted after '!'.

Diff Detail

Event Timeline

rsmmr updated this revision to Diff 73229.Oct 2 2016, 6:31 PM
rsmmr retitled this revision from to clang-format: Add two new formatting options.
rsmmr updated this object.
rsmmr added reviewers: djasper, lodato.
rsmmr added a subscriber: cfe-commits.
lodato resigned from this revision.Oct 3 2016, 7:10 AM
lodato removed a reviewer: lodato.
elsteveogrande accepted this revision.Oct 3 2016, 1:39 PM
elsteveogrande added a reviewer: elsteveogrande.
elsteveogrande added a subscriber: elsteveogrande.

Thought I'd take a look at the list of diffs and take a stab at a few (a few easy-looking and short ones :) ) to unblock some ppl, and unless @djasper has strong opinions about it I'd say why not.

Looking at other rules "nearby" I see spaces inside and outside square-brackets and angle-brackets, so it makes sense to allow this for conditions, especially if someone thinks this helps make it more readable, and for not as well (I personally like that one, it's easy to eyeball right past that !.

Also it appears to be off by default and I assume the unit tests work :)

This revision is now accepted and ready to land.Oct 3 2016, 1:39 PM
rsmmr added a comment.Oct 4 2016, 5:59 PM

Cool, thanks Steve. What's the next step for me to get it committed?

djasper edited edge metadata.Oct 4 2016, 11:04 PM

Could you read: http://clang.llvm.org/docs/ClangFormatStyleOptions.html#adding-additional-style-options
And provide some evidence about the requirements for new style options?

rsmmr added a comment.Oct 5 2016, 1:20 PM

Sure, I'm aiming to use clang-format on a couple of open-source code bases using this style, with the main one being the Bro network security monitor, see www.bro.org and github.com/bro/bro (note the stars and forks :-) Bro is also featured on GitHub's list of security show cases, https://github.com/showcases/security.

Sorry, but that's actually not enough, at least at first sight. With 37 contributors total, bro is still quite small and only 12 of them have more than a handful of commits. And it doesn't have a real style guide. It has: https://www.bro.org/development/contribute.html#coding-guidelines, which basically says "adapt to the existing code structure" and "We don’t have many strict rules".

rsmmr added a comment.Oct 5 2016, 1:41 PM

Well, last time I counted it was more like 100 contributors---Bro existed before GitHub (and before git). The style-guide is lacking, yes ... what can I say.

I don't really want to argue about importance of the project. We would like to use clang-format here and elsewhere, and I'd think these options could be useful to others as well, so I created a patch and submitted. If you guys don't like it, your call.

It's not about whether or not we like the patch. It's whether adding these options is a good trade-off for clang-format overall. If we find that actually more people would find these styles desirable, we can reconsider.

I have left some comments anyway in case you want to keep the patch around.

include/clang/Format/Format.h
603

It's actually more than if/for/while.

lib/Format/Format.cpp
355

Unnecessary linebreaks.

lib/Format/TokenAnnotator.cpp
1989

Indent is off.

1989–1990

This should go into a helper function or lambda so that it can be reused. What about catch? Also some of these aren't tested, e.g. the preprocessor ones.

1993

Indent is off.

2232

Note that at least JavaScript/TypeScript has a ! postfix operator, i.e. unary operator that applies to the token before it. You definitely don't want to always add a space after that.

2233

Indent is off.

2252

Is this related in any way? I don't see a test with ::

unittests/Format/FormatTest.cpp
11508

While you have added some test here, I think the more debatable ones are actually where there is also a space before the !, e.g.:

if (a && ! b) ..
  return ! c;
bool x = ! y && z;

The last one is especially tricky, because it makes it less clear that ! binds stronger than &&. Might seem obvious in this case, but IIRC, we fought quite a few bugs of the form:

if (! a < b) ..

Until a warning was added in Clang.

shafik added a subscriber: shafik.Sep 21 2023, 8:47 AM

Is this still relevant or should this be closed?

Herald added a project: Restricted Project. · View Herald TranscriptSep 21 2023, 8:47 AM