This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Add SpaceInParensOption for __attribute__ keyword
Needs RevisionPublic

Authored by gedare on Jul 17 2023, 5:24 PM.

Details

Summary

The __attribute__((specifier-list)) currently is formatted based on the SpacesInParensOptions.Other (previously, SpacesInParentheses). This change allows using Other while disabling addition of spaces between the consecutive parens surrounding the specifier-list.

DependsOn: D155239

Diff Detail

Event Timeline

gedare created this revision.Jul 17 2023, 5:24 PM
Herald added projects: Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptJul 17 2023, 5:24 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
gedare requested review of this revision.Jul 17 2023, 5:24 PM
owenpan added inline comments.Jul 17 2023, 5:32 PM
clang/include/clang/Format/Format.h
4429–4435

I'm against this as the double parens, IMO, should never be separated.

owenpan added inline comments.Jul 17 2023, 5:48 PM
clang/include/clang/Format/Format.h
4423–4428

This should be covered by SpacesInParetheses, so we really should not have a special option for __attribute__.

gedare planned changes to this revision.Jul 17 2023, 10:14 PM
gedare added inline comments.
clang/include/clang/Format/Format.h
4423–4428

Currently, the behavior of SpacesInParentheses does this:

__attribute__( ( noreturn ) )

In order to prevent this from happening, it is necessary to add an option to disable it somehow, because I don't see that this kind of spacing should ever be used by anyone, but probably someone does it, and it should be maintained for backward compatibility anyway.

4429–4435

I'm happy to consolidate these to a single option, that only allows to toggle between all spaces and no spaces, i.e.,

__attribute__( ( noreturn ) )  // enabled
__attribute__((noreturn)) // disabled

The style I aim for, uses no spaces around attributes, but likes to have spaces in other places. I will prepare a revision.

clang/lib/Format/TokenAnnotator.cpp
3975

This logic can be simplified, since the parser labels both the left and right parentheses (unlike with the `CastRParen` case.

gedare updated this revision to Diff 541322.Jul 17 2023, 10:24 PM

Merge the two options to just one for attribute parens.

gedare marked an inline comment as done.EditedJul 17 2023, 10:26 PM

I simplified this to treat the double parens identically, so that it will either inject spaces inside both parens, or not.

Note: This option is necessary to disable SpacesInParens.Other from adding spaces inside of __attribute__((...))

gedare edited the summary of this revision. (Show Details)Jul 17 2023, 10:27 PM
gedare edited the summary of this revision. (Show Details)

We should never make assumptions about what people do/don't use

If you have this you have to honour it.. if 'SpacesInParentheses ' was true then 'InAttributeSpecifiers' needs to be true by default, shouldn't there be something in the expanding out of SpacesInParentheses

clang/include/clang/Format/Format.h
4423–4428

Currently, the behavior of SpacesInParentheses does this:

__attribute__( ( noreturn ) )

In order to prevent this from happening, it is necessary to add an option to disable it somehow, because I don't see that this kind of spacing should ever be used by anyone, but probably someone does it, and it should be maintained for backward compatibility anyway.

And what does clang-format do before your SpacesInParentheses? You should expand the tests to cover the attributes (if they aren't in there already).

We should never make assumptions about what people do/don't use

If you have this you have to honour it.. if 'SpacesInParentheses ' was true then 'InAttributeSpecifiers' needs to be true by default, shouldn't there be something in the expanding out of SpacesInParentheses

gedare planned changes to this revision.Jul 18 2023, 3:53 PM

I need to fix this to reflect the changes in the parent rev.

clang/include/clang/Format/Format.h
4423–4428

SpacesInParentheses formats like this: __attribute__( ( ... ) )

gedare updated this revision to Diff 542090.Jul 19 2023, 10:00 AM

Rebase onto D155239 and correctly handle deprecated SpacesInParentheses option.

gedare marked 2 inline comments as done.Jul 19 2023, 10:14 AM
gedare updated this revision to Diff 542253.Jul 19 2023, 5:17 PM

Rebase onto D155239.

gedare updated this revision to Diff 542254.Jul 19 2023, 5:18 PM

remove blank line

gedare updated this revision to Diff 542650.Jul 20 2023, 1:28 PM

Rebase onto D155239.

gedare updated this revision to Diff 542651.Jul 20 2023, 1:31 PM

Add one more attribute test.

HazardyKnusperkeks added inline comments.
clang/unittests/Format/ConfigParseTest.cpp
233

You forgot this.

This revision is now accepted and ready to land.Jul 20 2023, 1:39 PM
gedare updated this revision to Diff 542655.Jul 20 2023, 1:48 PM

Add missing parser check.

gedare marked an inline comment as done.Jul 20 2023, 1:49 PM
gedare updated this revision to Diff 543729.Jul 24 2023, 3:44 PM

Add a release note.

gedare updated this revision to Diff 544466.Jul 26 2023, 12:00 PM

Rebase to 18

gedare updated this revision to Diff 558064.Nov 9 2023, 8:12 AM

Rebase to main. Fix up use of TT_AttributeParen.

gedare requested review of this revision.Nov 9 2023, 8:13 AM
owenpan requested changes to this revision.Nov 9 2023, 6:36 PM

We should never make assumptions about what people do/don't use

If you have this you have to honour it.. if 'SpacesInParentheses ' was true then 'InAttributeSpecifiers' needs to be true by default, shouldn't there be something in the expanding out of SpacesInParentheses

Because SpacesInParentheses was added 10+ years ago in rGb55acad91c37 and __attribute__((foo)) was not in the unit tests, it's probably a bug that the double parens were not excluded. Not sure whether the users who didn't complain about it really wanted it or simply didn't bother. The only way to find out would be to fix the bug, assuming it indeed was a bug.

When fixing bugs, especially the very old ones, we often have to choose between just fixing the bugs and adding an option to avoid "regressions", and I usually prefer the former. Nevertheless, I would have no problem if this new option is extended to handle all double parens, e.g. if (( i = j )), decltype(( x )), etc.

This revision now requires changes to proceed.Nov 9 2023, 6:36 PM