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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/source/Compiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ Additional options:

- `--scoped-enums` : Use C++11 style scoped and strongly typed enums in
generated C++. This also implies `--no-prefix`.


- `--no-emit-min-max-enum-values` : Disable generation of MIN and MAX
enumerated values for scoped enums and prefixed enums.

- `--gen-includes` : (deprecated), this is the default behavior.
If the original behavior is required (no include
statements) use `--no-includes.`
Expand Down
2 changes: 2 additions & 0 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ struct IDLOptions {
bool output_enum_identifiers;
bool prefixed_enums;
bool scoped_enums;
bool emit_min_max_enum_values;
bool swift_implementation_only;
bool include_dependence_headers;
bool mutable_buffer;
Expand Down Expand Up @@ -711,6 +712,7 @@ struct IDLOptions {
output_enum_identifiers(true),
prefixed_enums(true),
scoped_enums(false),
emit_min_max_enum_values(true),
swift_implementation_only(false),
include_dependence_headers(true),
mutable_buffer(false),
Expand Down
5 changes: 5 additions & 0 deletions src/flatc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ const static FlatCOption options[] = {
{ "", "scoped-enums", "",
"Use C++11 style scoped and strongly typed enums. Also implies "
"--no-prefix." },
{ "", "no-emit-min-max-enum-values", "",
"Disable generation of MIN and MAX enumerated values for scoped enums "
"and prefixed enums." },
{ "", "swift-implementation-only", "",
"Adds a @_implementationOnly to swift imports" },
{ "", "gen-includes", "",
Expand Down Expand Up @@ -464,6 +467,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
} else if (arg == "--scoped-enums") {
opts.prefixed_enums = false;
opts.scoped_enums = true;
} else if (arg == "--no-emit-min-max-enum-values") {
opts.emit_min_max_enum_values = false;
} else if (arg == "--no-union-value-namespacing") {
opts.union_value_namespacing = false;
} else if (arg == "--gen-mutable") {
Expand Down
4 changes: 3 additions & 1 deletion src/idl_gen_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,8 @@ class CppGenerator : public BaseGenerator {
FLATBUFFERS_ASSERT(minv && maxv);

code_.SetValue("SEP", ",\n");

// MIN & MAX are useless for bit_flags
if (enum_def.attributes.Lookup("bit_flags")) {
code_.SetValue("KEY", GenEnumValDecl(enum_def, "NONE"));
code_.SetValue("VALUE", "0");
Expand All @@ -1233,7 +1235,7 @@ class CppGenerator : public BaseGenerator {
NumToStringCpp(enum_def.AllFlags(),
enum_def.underlying_type.base_type));
code_ += "{{SEP}} {{KEY}} = {{VALUE}}\\";
} else { // MIN & MAX are useless for bit_flags
} else if (opts_.emit_min_max_enum_values) {
code_.SetValue("KEY", GenEnumValDecl(enum_def, "MIN"));
code_.SetValue("VALUE", GenEnumValDecl(enum_def, Name(*minv)));
code_ += "{{SEP}} {{KEY}} = {{VALUE}}\\";
Expand Down