This is an archive of the discontinued LLVM Phabricator instance.

Add support for attribute "enum_extensibility"
ClosedPublic

Authored by ahatanak on Mar 8 2017, 4:06 PM.

Details

Summary

This patch adds support for a new attribute that will be used to distinguish between extensible and inextensible enums. The attribute was discussed on cfe-dev a few weeks ago and I've made a few modifications to the original proposal based on the feedback I received.

http://lists.llvm.org/pipermail/cfe-dev/2017-February/052748.html

In this patch, I didn't include the command line option for setting the default enum style for unannotated enums since I wasn't sure it's something we absolutely need. If it turns the warnings are too noisy, I'll consider adding a command line option for silencing the false positive warnings in a follow-up patch.

Diff Detail

Repository
rL LLVM

Event Timeline

ahatanak created this revision.Mar 8 2017, 4:06 PM
arphaman added inline comments.
include/clang/Basic/Attr.td
887 ↗(On Diff #91090)

We might as well have a C++11 spelling in the clang namespace as well.

test/Sema/enum-attr.c
27 ↗(On Diff #91090)

Should this be an error instead?

ahatanak updated this revision to Diff 91577.Mar 13 2017, 9:43 AM
ahatanak marked 2 inline comments as done.

Address Alex's review comments.

test/Sema/enum-attr.c
27 ↗(On Diff #91090)

Yes, I agree.

aaron.ballman added inline comments.Mar 13 2017, 3:22 PM
include/clang/Basic/AttrDocs.td
1969 ↗(On Diff #91577)

Breaking this up into multiple lines would be appreciated (we aren't strict about the 80 col limit in this file, but it should be readable still).

Also, adding some examples would be appreciated.

include/clang/Basic/DiagnosticSemaKinds.td
2383–2384 ↗(On Diff #91577)

I'd rather see this defined next to warn_attribute_type_not_supported as def err_attribute_type_not_supported : Error<warn_attribute_type_not_supported.Text>;

test/Sema/enum-attr.c
27 ↗(On Diff #91090)

I'm not opposed to it, but we do treat it as a warning for every other attribute (and just ignore the attribute), FWIW.

test/SemaCXX/enum-attr.cpp
108 ↗(On Diff #91577)

Missing tests for the attribute being applied to something other than an enum, or taking too few/many arguments.

ahatanak updated this revision to Diff 91782.Mar 14 2017, 3:16 PM
ahatanak marked 3 inline comments as done.

Address review comments from Aaron.

test/Sema/enum-attr.c
27 ↗(On Diff #91090)

Do you know the reason we treat wrong attribute arguments as a warning in other cases rather than an error?

I'm guessing a warning is preferred because in most cases dropping an attribute doesn't affect correctness (it doesn't cause miscompiles).

aaron.ballman accepted this revision.Mar 18 2017, 8:31 AM

Aside from the request for a FIXME and a decision from the author on error vs warning, the code LGTM. Feel free to make a decision and commit without further review.

test/Sema/enum-attr.c
31 ↗(On Diff #91782)

That's a rather unfortunate diagnostic. I would have expected this to mention that it only takes one argument as in the zero arg case. I'm not certain that this is the fault of your patch, however. Can you at least add a FIXME to this test that mentions this diagnostic could be improved?

27 ↗(On Diff #91090)

You are correct. The general rule is to use warnings when an incorrect attribute still has no impact on the code generated for the user, and use an error otherwise. We're not always perfect about this, which is why I'm not opposed, but since this involves adding an entirely new diagnostic, I figured it might be worth mentioning that there's really no reason for this to be an error that prevents the user's source code from being translated either.

This revision is now accepted and ready to land.Mar 18 2017, 8:31 AM
ahatanak marked an inline comment as done.Mar 20 2017, 6:45 PM
ahatanak added inline comments.
test/Sema/enum-attr.c
31 ↗(On Diff #91782)

It's possible to implement a parser function in ParseDecl.cpp specifically for enum_extensibility to improve the diagnostic, but I think ParseAttributeArgsCommon should be able to check the number of arguments and emit a proper diagnostic. I've added a FIXME to the test for now.

It looks like ParseAttributeArgsCommon currently assumes that the first argument can be an identifier but it tries to parse the remaining arguments as expressions. I think it's possible to make it check the number of arguments provided and parse each argument according to its type.

27 ↗(On Diff #91090)

OK. I don't think we have a strong reason to error out here so I think it's better to follow convention and issue a warning.

This revision was automatically updated to reflect the committed changes.
ahatanak marked an inline comment as done.
cfe/trunk/test/Sema/enum-attr.c