Skip to content

Commit

Permalink
Work around GCC assuming that enum values are in the correct range.
Browse files Browse the repository at this point in the history
The whole purpose of the is_valid function is to check it
at runtime, so it obviously cannot assume this.
  • Loading branch information
angavrilov committed Feb 6, 2012
1 parent fdb85c0 commit 032d4fe
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Enum.pm
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ sub render_enum_core($$) {
sub render_enum_tables($$$$) {
my ($name,$tag,$base,$count) = @_;

my $base_type = get_primitive_base($tag, 'int32_t');

# Enumerate enum attributes

my %aidx = ('key' => 0);
Expand Down Expand Up @@ -110,7 +112,9 @@ sub render_enum_tables($$$$) {
emit "const $name _last_item_of_$name = ($name)", ($base+$count-1), ";";

emit_block {
emit "return (value >= _first_item_of_$name && value <= _last_item_of_$name);";
# Cast the enum to integer in order to avoid GCC assuming the value range is correct.
emit "$base_type ivalue = ($base_type)value;";
emit "return (ivalue >= $base && ivalue <= ",($base+$count-1),");";
} "inline bool is_valid($name value) ";

for (my $i = 0; $i < @anames; $i++) {
Expand Down

0 comments on commit 032d4fe

Please sign in to comment.