This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Add new option to add spaces around conditions
ClosedPublic

Authored by timwoj on Oct 2 2019, 10:42 AM.

Details

Summary

This diff adds a new option SpacesAroundConditions that inserts spaces inside the braces for conditional statements. It's used by the Zeek project (https://github.com/zeek/zeek) as described in their style guide (https://github.com/zeek/zeek-docs/blob/master/devel/style_guide.rst#conditional-expressions)

Diff Detail

Event Timeline

timwoj created this revision.Oct 2 2019, 10:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 2 2019, 10:42 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
MyDeveloperDay added a project: Restricted Project.
MyDeveloperDay retitled this revision from Clang-format: Add new option to add spaces around conditions to [clang-format] Add new option to add spaces around conditions.
MyDeveloperDay requested changes to this revision.Oct 2 2019, 11:55 AM
MyDeveloperDay added inline comments.
clang/include/clang/Format/Format.h
2000

its position in the file and the documentation is not quite alphabetic, given you are close i'd make it so

2172

move upto just before SpacesBeforeTrailingComments

clang/lib/Format/Format.cpp
548

Same here

822

Sorry move to line 771-772

clang/lib/Format/TokenAnnotator.cpp
2621

this isn't really the normal naming convention for variables, I think it would be IsCondKw I think its clearer without using abbreviations,

why not just make this a function? does the Lambda really give us anything here in such a big function other than clutter?

static bool isConditionKeyword(....)

3064

I'm not sure I understand this change so you added a tok::l_paren here? but its not just for your style, so something else must have changed. Did you run all FormatTests?

one of the tests you add need to test why you added this here

This revision now requires changes to proceed.Oct 2 2019, 11:55 AM
mitchell-stellar requested changes to this revision.Oct 3 2019, 1:01 PM
mitchell-stellar added inline comments.
clang/docs/ClangFormatStyleOptions.rst
2363

SpacesInConditionalStatement is probably a better name, remaining consistent with existing SpacesIn* options, as well as indicating it only affects the entire conditional statement, and not individual conditions within the statement.

clang/lib/Format/TokenAnnotator.cpp
2623

It seems that you are mixing statement tokens if, while, etc. with preprocessor and macro tokens. Going by the documentation, this is unexpected.

3064

This is not covered by your tests.

timwoj updated this revision to Diff 224890.Oct 14 2019, 12:17 PM

Changed option name to SpacesInConditionalStatement, added isKeywordWithCondition method, added some extra tests

Changed option name to SpacesInConditionalStatement, added isKeywordWithCondition method, added some extra tests

Sorry for the delay on getting back to this. I'm not the original author and I was waiting for changes from him, and then I slacked on getting them submitted.

rsmmr added a subscriber: rsmmr.Oct 15 2019, 12:15 AM
mitchell-stellar requested changes to this revision.Oct 15 2019, 11:44 AM
mitchell-stellar added inline comments.
clang/include/clang/Format/Format.h
2005

Nitpick: this option is still not in alphabetical order. Please re-order it. Otherwise, this looks good to me.

This revision now requires changes to proceed.Oct 15 2019, 11:44 AM
timwoj updated this revision to Diff 225345.Oct 16 2019, 8:22 PM

Fix ordering

This revision is now accepted and ready to land.Oct 19 2019, 3:58 AM
MyDeveloperDay added a comment.EditedOct 19 2019, 7:40 AM

Thank you for the patch.

If you plan to continue to submit patches I think it would be worthwhile if you applied for commit access, This is not the first revision from you and we need more people who are willing to help with clang-format to fix bugs and do code reviews of other peoples work.

You clearly have an interested and an understanding of how clang-format works and a desire to help make it EVEN better. It is also good to get access in order to maintain the work you do,

As the cut over to github I believe is tomorrow at the LLVM meeting it might be worth waiting until after that point to work out what the procedure to obtain access will be as I'm unclear if getting permission will be the same as outlined here

https://llvm.org/docs/DeveloperPolicy.html#obtaining-commit-access

Thanks for the consideration on committer access, but I'm going to have to pass for the time being.

That said, can someone land this patch?

Can you rebase it and add a release note in docs/ReleaseNote.rst

MyDeveloperDay added inline comments.Nov 20 2019, 4:01 AM
clang/unittests/Format/FormatTest.cpp
14889

show "else if" example

MyDeveloperDay added inline comments.Nov 20 2019, 4:04 AM
clang/unittests/Format/FormatTest.cpp
14893

is this what you expect? or should it be while ( ( a && b ) )

This revision was automatically updated to reflect the committed changes.