pycparser icon indicating copy to clipboard operation
pycparser copied to clipboard

Designated Initializers report error when identifier is a type

Open Miaou opened this issue 8 years ago • 3 comments

Considering the following C99 code:

typedef enum Foo {
    e_foo
} Foo;

typedef struct Bar {
    char Foo;
} Bar;

void main() {
    Bar bar = {.Foo = 'C'};
}

The name Foo is a field of struct Bar, but it is also the name of a type. GCC compiles this with the following command:

gcc /tmp/foo.c -o /dev/null -std=c99

However, pycparser reports an error (versions 2.16 and 2.18 with Python 3.4.3 x64 under Ubuntu 14.04):

pycparser.plyparser.ParseError: bug_desig_init.c:10:17: before: Foo

In the C99 specification draft, §1 section 6.5.3:

If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate name spaces for various categories of identifiers, as follows: [...]

Miaou avatar Oct 10 '17 16:10 Miaou

@natezb

eliben avatar Oct 11 '17 03:10 eliben

I'll be away starting tomorrow morning, so I likely won't be able to respond much more until next week.

I don't have time to verify this, but it looks like you should be able to add PERIOD TYPEID to the designator production (in a new function, i.e. p_designator_2) without adding any parse conflicts. In that case, you'd create a c_ast.ID out of the TYPEID (as opposed to the existing p_designator which simply passes along an already-created c_ast.ID).

This may also need to be done in some other similar corner cases where a production cannot possibly accept a TYPEID, and should therefore accept it and treat it as an identifier.

natezb avatar Oct 11 '17 04:10 natezb

If anyone wants to dig in and fix it, I'll accept PRs.

eliben avatar Mar 03 '20 14:03 eliben