This is an archive of the discontinued LLVM Phabricator instance.

Add 128-bit integer support to enum element
AcceptedPublic

Authored by zhouyizhou on Feb 15 2023, 6:50 PM.

Details

Summary

Add 128-bit integer support to enum element like GCC extension do.

Also remove comment: "TODO" from the code. This comment is added by 4ef40013d75ca ("Implement capturing of enum values and chaining of enums together"), while 6791a0d43b681 ("Improve handling of enumerator values for C and C++") has accomplished that "TODO".

I don't have write access to LLVM

Signed-off-by: Zhouyi Zhou <zhouzhouyi@gmail.com>

Thanks a lot
Zhouyi

Diff Detail

Event Timeline

zhouyizhou created this revision.Feb 15 2023, 6:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 15 2023, 6:50 PM
zhouyizhou requested review of this revision.Feb 15 2023, 6:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 15 2023, 6:50 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

add assertion for int128 support

zhouyizhou updated this revision to Diff 497884.EditedFeb 15 2023, 9:57 PM

debian build bot won't let me pass, so I clang-formatted the function ActOnEnumBody

debian build bot won't let me pass, so I clang-formatted the function ActOnEnumBody

You can try git diff -U0 --no-color --relative HEAD^ | clang/tools/clang-format/clang-format-diff.py -p1 -i to format the changed part only. Currently there are many untouched part changed due to the format. This is not good.

debian build bot won't let me pass, so I clang-formatted the function ActOnEnumBody

You can try git diff -U0 --no-color --relative HEAD^ | clang/tools/clang-format/clang-format-diff.py -p1 -i to format the changed part only. Currently there are many untouched part changed due to the format. This is not good.

Thank you for your valuable advice, the command is fantastic indeed. I will resend the patch immediately.
Thanks again
Cheers Zhouyi

reformat the patch according to Chuanqi's Advice

We also need a codegen test. clang/test/CodeGen/enum2.c may be a good one. It tests that debug information is correct as well.

Add CodeGen Test following MaskRay(Fangrui Song)'s Advice, I learned a lot during this process, to be honest ;-)

MaskRay added inline comments.Feb 17 2023, 7:28 PM
clang/test/CodeGen/enum3.c
1 ↗(On Diff #498520)

We don't need a new test file. Reusing can improve test discoverability and readability for this test.

I think this test needs to show how the global variable v's value is changed by the __uint128_t enum, which enum2.c may lack (a problem of many old tests).

make test case even better according to MaskRay's guidance

MaskRay accepted this revision.Feb 18 2023, 1:05 AM
MaskRay added inline comments.
clang/test/CodeGen/enum2.c
44

delete the trailing blank line

This revision is now accepted and ready to land.Feb 18 2023, 1:05 AM

delete the trailing blank line

Thank you all ;-)

I have no access write to LLVM, could you commit it for me when you are not busy?

Thanks a lot
Cheers
Zhouyi

Zhouyi Zhou <zhouzhouyi@gmail.com>

h-vetinari added inline comments.
clang/lib/Sema/SemaDecl.cpp
19572–19574

That comment is still relevant AFAICT, at least partially (the ISO C restrictions are still there, GCC still extends it)

I think this needs additional test coverage around _BitInt as those can be arbitrarily large depending on the target. Also, C2x made changes in this area to how we calculate what type to represent the enumerations in; we don't have to implement that support as part of this patch, but we should make sure what we're doing doesn't conflict with that future work.

(Also, please add a release note for the changes.)

clang/lib/Sema/SemaDecl.cpp
19572–19574

Not only is it still relevant, there were changes for C2x in this area. See https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2997.htm for more details.

19717–19718

Consider (in C2x):

enum E {
  Val = 0xFFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'1wb
};
19719–19721

Again, _BitInt.

zhouyizhou marked an inline comment as done.Feb 22 2023, 2:43 PM

Thank you all for your guidance!

Now I am starting to learn C2X N2997, this is a fruitful process of learning for me ;-)

Cheers
Zhouyi

clang/lib/Sema/SemaDecl.cpp
19572–19574

That comment is still relevant AFAICT, at least partially (the ISO C restrictions are still there, GCC still extends it)

Thank for reviewing the patch ;-)

I think clang (with -pedantic) has emit the warning in function Sema::CheckEnumConstant, which behave very similar to what GCC (with -pedantic) do.

Cheers
Zhouyi

Add 128-bit integer support to enum element like GCC extension do.
Also test coverage around _BitInt which can be arbitrarily large depending on the target.
Also leave room for improvements to C2X because C2x made changes in this area to how we calculate what type to represent the enumerations in.

Signed-off-by: Zhouyi Zhou <zhouzhouyi@gmail.com>