Skip to content

Add support for enums with underlying type specifications to the parser #5431

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

Merged
merged 3 commits into from
Jul 23, 2020
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
20 changes: 20 additions & 0 deletions regression/ansi-c/enum9/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
typedef enum : unsigned
{
X
} my_enum1;

enum my_enum2 : unsigned
{
Y
};

struct S
{
enum my_enum2 : unsigned a;
enum my_enum2 : unsigned b : 2;
};

int main()
{
enum my_enum2 : unsigned enum_var1;
Copy link
Contributor

@hannes-steffenhagen-diffblue hannes-steffenhagen-diffblue Jul 22, 2020

Choose a reason for hiding this comment

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

Huh? Any ideas on what this means? Seems like clang allows this notation, but it appears to not actually mean anything, for instance

#include <stdio.h>

enum my_enum : signed char {
  A, B
};


int main(void) {
  enum my_enum : unsigned long long  X = 255;
  printf("%d\n", X);
}

Seems to compile just fine and works as if the second : unsigned long long wasn’t there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah it seems to just be ignored by clang. My guess would be that it might have been overly complicated for clang to exclude this case in the grammar, which would also be true for cbmc. I think we should report this as an error during conversion/typechecking.

}
14 changes: 14 additions & 0 deletions regression/ansi-c/enum9/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CORE
main.c
--verbosity 2
^EXIT=0$
^SIGNAL=0$
(main.c:1:\d+)|(main.c\(1\)): warning: ignoring specification of underlying type for enum
(main.c:6:\d+)|(main.c\(6\)): warning: ignoring specification of underlying type for enum
(main.c:13:\d+)|(main.c\(13\)): warning: ignoring specification of underlying type for enum
(main.c:14:\d+)|(main.c\(14\)): warning: ignoring specification of underlying type for enum
(main.c:19:\d+)|(main.c\(19\)): warning: ignoring specification of underlying type for enum
--
--
Checks that files containing enums with an underlying type specification can be
parsed, and that a warning is output saying that the specification is ignored
12 changes: 12 additions & 0 deletions src/ansi-c/c_typecheck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,12 @@ void c_typecheck_baset::typecheck_c_enum_type(typet &type)
throw 0;
}

if(as_expr.find(ID_enum_underlying_type).is_not_nil())
{
warning().source_location = source_location;
warning() << "ignoring specification of underlying type for enum" << eom;
}

// enums start at zero;
// we also track min and max to find a nice base type
mp_integer value=0, min_value=0, max_value=0;
Expand Down Expand Up @@ -1352,6 +1358,12 @@ void c_typecheck_baset::typecheck_c_enum_tag_type(c_enum_tag_typet &type)
throw 0;
}

if(type.find(ID_enum_underlying_type).is_not_nil())
{
warning().source_location = type.source_location();
warning() << "ignoring specification of underlying type for enum" << eom;
}

source_locationt source_location=type.source_location();

irept &tag=type.add(ID_tag);
Expand Down
74 changes: 55 additions & 19 deletions src/ansi-c/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,14 @@ extern char *yyansi_ctext;

%start grammar

%expect 1 /* the famous "dangling `else'" ambiguity */
%expect 2 /* the famous "dangling `else'" ambiguity */
/* results in one shift/reduce conflict */
/* that we don't want to be reported */

/* a second shift/reduce conflict arises due to enum underlying */
/* type specifications and bitfield specifications, which are both */
/* introduced by a ':' and follow a type */

%{
/************************************************************************/
/*** rules **************************************************************/
Expand Down Expand Up @@ -1790,23 +1794,15 @@ bit_field_size:
enum_name:
enum_key
gcc_type_attribute_opt
{
// an anon enum
}
'{' enumerator_list_opt '}'
gcc_type_attribute_opt
enum_underlying_type_opt
{
parser_stack($1).operands().swap(parser_stack($5).operands());
$$=merge($1, merge($2, $7)); // throw in the gcc attributes
}
| enum_key
gcc_type_attribute_opt
identifier_or_typedef_name
// an anon enum
if(parser_stack($3).is_not_nil())
{
// an enum with tag
parser_stack($1).set(ID_tag, parser_stack($3));
parser_stack($1).set(ID_enum_underlying_type, parser_stack($3));
}
'{' enumerator_list_opt '}'
}
'{' enumerator_list_opt '}'
gcc_type_attribute_opt
{
parser_stack($1).operands().swap(parser_stack($6).operands());
Expand All @@ -1815,14 +1811,54 @@ enum_name:
| enum_key
gcc_type_attribute_opt
identifier_or_typedef_name
gcc_type_attribute_opt
enum_underlying_type_opt
{
parser_stack($1).id(ID_c_enum_tag); // tag only
// an enum with tag
parser_stack($1).set(ID_tag, parser_stack($3));
$$=merge($1, merge($2, $4)); // throw in the gcc attributes

if(parser_stack($4).is_not_nil())
{
parser_stack($1).set(ID_enum_underlying_type, parser_stack($4));
}
}
braced_enumerator_list_opt
gcc_type_attribute_opt
{
if(parser_stack($6).is_not_nil())
{
parser_stack($1).operands().swap(parser_stack($6).operands());
}
else
{
parser_stack($1).id(ID_c_enum_tag);
}

$$=merge($1, merge($2, $7)); // throw in the gcc attributes
}
;


enum_underlying_type_opt:
/* empty */
{
init($$);
parser_stack($$).make_nil();
}
| ':' basic_type_name
{
$$=$2;
}

braced_enumerator_list_opt:
/* empty */
{
init($$);
parser_stack($$).make_nil();
}
| '{' enumerator_list_opt '}'
{
$$=$2;
}

enum_key: TOK_ENUM
{
$$=$1;
Expand Down
1 change: 1 addition & 0 deletions src/util/irep_ids.def
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ IREP_ID_ONE(NULL)
IREP_ID_ONE(null)
IREP_ID_ONE(nullptr)
IREP_ID_ONE(c_enum)
IREP_ID_ONE(enum_underlying_type)
IREP_ID_ONE(enumeration)
IREP_ID_ONE(elements)
IREP_ID_ONE(unknown)
Expand Down