diff --git a/regression/cbmc/auto_type_qualified/main.c b/regression/cbmc/auto_type_qualified/main.c new file mode 100644 index 00000000000..60a441607ee --- /dev/null +++ b/regression/cbmc/auto_type_qualified/main.c @@ -0,0 +1,11 @@ +int main() +{ + int s = 42; + // 'const __auto_type x = s;' is equivalent to 'const typeof(s) x = s;'. + // The parser previously only handled the unqualified + // '__auto_type x = s;' form and rejected the qualified one. + const __auto_type x = s; + __CPROVER_assert( + x == 42, "qualified __auto_type deduces the initializer type"); + return 0; +} diff --git a/regression/cbmc/auto_type_qualified/test.desc b/regression/cbmc/auto_type_qualified/test.desc new file mode 100644 index 00000000000..4e93cfa3763 --- /dev/null +++ b/regression/cbmc/auto_type_qualified/test.desc @@ -0,0 +1,10 @@ +CORE gcc-only +main.c + +^EXIT=0$ +^SIGNAL=0$ +^VERIFICATION SUCCESSFUL$ +-- +^warning: ignoring +^PARSING ERROR$ +^syntax error diff --git a/src/ansi-c/parser.y b/src/ansi-c/parser.y index 91526b2b8e6..c51a7f1c3dd 100644 --- a/src/ansi-c/parser.y +++ b/src/ansi-c/parser.y @@ -1114,6 +1114,26 @@ declaring_list: // add the initializer to_ansi_c_declaration(parser_stack($$)).add_initializer(parser_stack($5)); } + | type_qualifier_list TOK_GCC_AUTO_TYPE declarator + post_declarator_attributes_opt '=' initializer + { + // Qualified __auto_type: 'const/volatile/... __auto_type + // var = initializer;' is equivalent to + // ' typeof(initializer) var = initializer;'. + // Build the typeof-of-initializer node, then wrap it with + // the qualifiers. + parser_stack($2).id(ID_typeof); + parser_stack($2).copy_to_operands(parser_stack($6)); + // Merge qualifiers into the typeof type. + $2 = merge($1, $2); + + $3 = merge($4, $3); + + init($$, ID_declaration); + parser_stack($$).type().swap(parser_stack($2)); + PARSER.add_declarator(parser_stack($$), parser_stack($3)); + to_ansi_c_declaration(parser_stack($$)).add_initializer(parser_stack($6)); + } | declaring_list ',' gcc_type_attribute_opt declarator post_declarator_attributes_opt {