Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] Add Command-Line Flag to Suppress MIN and MAX Enums #7705

Merged
merged 1 commit into from
Jan 7, 2023

Conversation

jalitriver
Copy link
Contributor

@jalitriver jalitriver commented Dec 9, 2022

Add the --no-minmax-values flag to prevent flatc from generating C++ enums with MIN and MAX enumerated values that otherwise would be set to the inclusive lower and upper bound respectively of the enum.

This command-line flag is needed to avoid collisions when an enum that is being ported to FlatBuffers already has a MIN or MAX enumerated value.

It is also needed to work around a long-standing problem with magic_enum that causes magic_enum to not see enumerated values that are not unique. For example, if FlatBuffers sets MIN = FOO and MAX = BAR, MIN and FOO share the same underlying value so they are not unique. The same is true of MAX and BAR. This prevents magic_enum from converting FOO and BAR to and from strings as well as causing magic_enum to return a count of enumerated values that is two fewer than it should be.

Unit tests have been added to tests/flatc/flatc_cpp_tests.py for each combination of --no-minmax-values and scoped enums, prefixed enums, and non-prefixed enums.

@google-cla
Copy link

google-cla bot commented Dec 9, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added c++ codegen Involving generating code from schema python labels Dec 9, 2022
@jalitriver jalitriver closed this Dec 9, 2022
@jalitriver jalitriver reopened this Dec 9, 2022
@jalitriver jalitriver force-pushed the no-minmax-enums branch 2 times, most recently from aff35b0 to 0aff518 Compare December 9, 2022 16:30
Copy link
Collaborator

@dbaileychess dbaileychess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally good, just some small nits.

include/flatbuffers/idl.h Outdated Show resolved Hide resolved
src/flatc.cpp Outdated Show resolved Hide resolved
tests/flatc/flatc_cpp_tests.py Outdated Show resolved Hide resolved
@jalitriver
Copy link
Contributor Author

The changes I just force pushed are rebased off of the tip of master (e1a2f68) if you want to compare the changes.

@jalitriver
Copy link
Contributor Author

The changes I just force pushed are rebased off of the tip of master (a078130) if you want to compare the changes.

dbaileychess
dbaileychess previously approved these changes Jan 4, 2023
@dbaileychess dbaileychess enabled auto-merge (squash) January 4, 2023 04:58
Add the --no-minmax-values flag to prevent flatc from generating C++
enums with MIN and MAX enumerated values that otherwise would be set
to the inclusive lower and upper bound respectively of the enum.

This command-line flag is needed to avoid collisions when an enum that
is being ported to FlatBuffers already has a MIN or MAX enumerated
value.

It is also needed to work around a long-standing problem with
magic_enum that causes magic_enum to not see enumerated values that
are not unique.  For example, if FlatBuffers sets MIN = FOO and MAX =
BAR, MIN and FOO share the same underlying value so they are not
unique.  The same is true of MAX and BAR.  This prevents magic_enum
from converting FOO and BAR to and from strings as well as causing
magic_enum to return a count of enumerated values that is two fewer
than it should be.
auto-merge was automatically disabled January 4, 2023 14:08

Head branch was pushed to by a user without write access

@jalitriver
Copy link
Contributor Author

Sorry, I thought I was being helpful rebasing my branch off of the latest master, but this seems to have revoked the approval.

@dbaileychess dbaileychess enabled auto-merge (squash) January 6, 2023 04:37
@dbaileychess dbaileychess enabled auto-merge (squash) January 7, 2023 18:32
@dbaileychess dbaileychess merged commit 920f382 into google:master Jan 7, 2023
sunwen18 pushed a commit to sunwen18/flatbuffers that referenced this pull request Jan 9, 2023
Add the --no-minmax-values flag to prevent flatc from generating C++
enums with MIN and MAX enumerated values that otherwise would be set
to the inclusive lower and upper bound respectively of the enum.

This command-line flag is needed to avoid collisions when an enum that
is being ported to FlatBuffers already has a MIN or MAX enumerated
value.

It is also needed to work around a long-standing problem with
magic_enum that causes magic_enum to not see enumerated values that
are not unique.  For example, if FlatBuffers sets MIN = FOO and MAX =
BAR, MIN and FOO share the same underlying value so they are not
unique.  The same is true of MAX and BAR.  This prevents magic_enum
from converting FOO and BAR to and from strings as well as causing
magic_enum to return a count of enumerated values that is two fewer
than it should be.

Co-authored-by: Paul Serice <paul@serice.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ codegen Involving generating code from schema documentation Documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants