This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [C++ PATCH] warning about empty extern "C" structures


Theodore Papadopoulo wrote:
> On Wed, 2006-05-17 at 11:45 -0500, Roman Kononov wrote:
>> On 05/17/2006 11:12, Gabriel Dos Reis wrote:
>>> | They prevent from introducing safe C++ code.
>>>
> 
>> Header.h and driver.c are old GCC code using all possible GCC extensions. 
>> User.cpp is an attempt for C++ to co-exist in the Linux kernel. To make 
>> driver.c and user.cpp, linked together, to work as they should, one needs to 
>> do something with extern "C" struct spinlock_t {};. Currently, there is no 
>> compiler help for the user compiling his user.cpp file to know about such 
>> misbehavior.
> 
> What I do not understand is why g++ assumes a C++ layout  for the
> device_t  (or spinlock_t) structure. extern "C" is supposedly meaning
> using the C layout whatever it is, no ?

There's no standardized meaning for what extern "C" means for a
structure.  It's defined as giving C linkage for functions and
variables, which, in most compilers, affects name-mangling, but could
also affect calling conventions, alignment, etc.

However, things in extern "C" blocks still follow C++ semantics so that,
for example:

  extern "C" void f();

is a function of no arguments, not an unprototyped function.  In the
same way, the C++ standard requires that an empty struct in an extern
"C" block have non-zero size; there's no special exemption for extern
"C" blocks.

(The fact that GNU C uses a zero size for empty structures is
problematic from a language-design point of view, as has been discussed
previously on this list, but we probably can't change it, due to how
prevalently it is used.)

So, I don't think we have any choice about the semantics.  We could,
however, issue a warning, in either or both of the C/C++ front-ends, but
I'm not sure how valuable that would be; I think we'd get a lot of false
positives in ordinary C++ code, which tends to use empty structures
frequently.

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]