Is this a syntax error in pthreadtypes.h

  • Thread starter Andreas Bogenberger
  • Start date
A

Andreas Bogenberger

Hi all,

I am corrently writing a source code analysis tool for my PhD thesis and
having a problem with pthreadtypes.h (linux, GNU libc 2.7 )

/usr/include/bits/pthreadtypes.h line 76
==============================
typedef union
{
.....
.....
unsigned int __nusers;
__extension__ union
{
int __spins;
__pthread_slist_t __list;
};
} __data;
.....
.....
} pthread_mutex_t;
==============================

the line closing the union gives me an error
so if
"union { int __spins; __pthread_slist_t __list; }"
is the specifier_qualifier_list isn't there the
declarator missing?


best regards,
Andreas




FYI, ANSI-C says:


struct_or_union_specifier
: struct_or_union IDENTIFIER '{' struct_declaration_list '}'
| struct_or_union '{' struct_declaration_list '}'
| struct_or_union IDENTIFIER
;

struct_declaration_list
: struct_declaration
| struct_declaration_list struct_declaration
;

struct_declaration
: specifier_qualifier_list struct_declarator_list ';'
;

struct_declarator_list
: struct_declarator
| struct_declarator_list ',' struct_declarator
;

struct_declarator
: declarator
| ':' constant_expression
| declarator ':' constant_expression
;

declarator
: pointer direct_declarator
| direct_declarator
;


direct_declarator
: IDENTIFIER
| '(' declarator ')'
......
......
| direct_declarator '(' identifier_list ')'
| direct_declarator '(' ')'
;
 
H

Harald van Dijk

Hi all,

I am corrently writing a source code analysis tool for my PhD thesis and
having a problem with pthreadtypes.h (linux, GNU libc 2.7 )

/usr/include/bits/pthreadtypes.h line 76 ==============================
typedef union
{
.....
.....
unsigned int __nusers;
__extension__ union
{
int __spins;
__pthread_slist_t __list;
};
} __data;
.....
.....
} pthread_mutex_t;
==============================

the line closing the union gives me an error so if
"union { int __spins; __pthread_slist_t __list; }"
is the specifier_qualifier_list isn't there the declarator missing?

In standard C, you would be correct, but the type definition you've
posted is not written in standard C, and is not meant to be interpreted
by anything other than GCC. If you want to interpret it, you need to
support more than just C. The details can be found in the GCC
documentation.

(I myself am using glibc 2.5 exactly so that I can avoid this problem.)
 
B

Ben Pfaff

Andreas Bogenberger said:
the line closing the union gives me an error
so if
"union { int __spins; __pthread_slist_t __list; }"
is the specifier_qualifier_list isn't there the
declarator missing?

Yes. This is a GCC extension, described in the GCC manual as
follows:

5.51 Unnamed struct/union fields within structs/unions
======================================================

For compatibility with other compilers, GCC allows you to define a
structure or union that contains, as fields, structures and unions
without names. For example:

struct {
int a;
union {
int b;
float c;
};
int d;
} foo;

In this example, the user would be able to access members of the
unnamed union with code like `foo.b'. Note that only unnamed structs
and unions are allowed, you may not have, for example, an unnamed `int'.

You must never create such structures that cause ambiguous field
definitions. For example, this structure:

struct {
int a;
struct {
int a;
};
} foo;

It is ambiguous which `a' is being referred to with `foo.a'. Such
constructs are not supported and must be avoided. In the future, such
constructs may be detected and treated as compilation errors.

Unless `-fms-extensions' is used, the unnamed field must be a
structure or union definition without a tag (for example, `struct { int
a; };'). If `-fms-extensions' is used, the field may also be a
definition with a tag such as `struct foo { int a; };', a reference to
a previously defined structure or union such as `struct foo;', or a
reference to a `typedef' name for a previously defined structure or
union type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top