<div dir="ltr">IMO extern "C" is a clear indication that the user wants to interoperate between C and C++.  clang should have this warning on by default in extern "C" contexts.<div class="gmail_extra"><br>
<div class="gmail_quote">On Tue, Nov 5, 2013 at 8:56 AM, Serge Pavlov <span dir="ltr"><<a href="mailto:sepavloff@gmail.com" target="_blank">sepavloff@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">When combining C and C++ files it is recommended to specify option '-Wc++-compat. It enables specific messages related to incompatibilities between C and C++. In your example relevant message appears:<div>

<br></div><div>clang -Wc++-compat -c lib.c -o lib.o<br></div><div><div>In file included from lib.c:1:</div><div>./header.h:5:9: warning: empty struct has size 0 in C, size 1 in C++ [-Wc++-compat]</div><div>typedef struct empty { } empty;</div>

<div><br></div><div>But -Wall doesn't enable these warnings.</div><div><br></div><div>Thanks,</div><div>--Serge</div><div>        ^</div><div>1 warning generated.</div></div></div><div class="gmail_extra"><div><div class="h5">
<br><br><div class="gmail_quote">
2013/11/5 Andy Gibbs <span dir="ltr"><<a href="mailto:andyg1001@hotmail.co.uk" target="_blank">andyg1001@hotmail.co.uk</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hi,<br>
<br>
I have just helped a colleague track down a bug in a project using mixed C and C++ code which revolved around the fact that an empty struct in C has zero size and in C++ has non-zero size.  Since a picture is a thousand words, a simplified version of the code is as follows:<br>


<br>
<br>
header.h<br>
========<br>
#ifdef __cplusplus<br>
extern "C" {<br>
#endif<br>
<br>
typedef struct empty { } empty;<br>
<br>
typedef struct test {<br>
 empty a;<br>
 char b;<br>
} test;<br>
<br>
void set(test* p);<br>
<br>
#ifdef __cplusplus<br>
}<br>
#endif<br>
<br>
<br>
lib.c<br>
=====<br>
#include "header.h"<br>
<br>
void set(test* p) {<br>
 p->b = 10;<br>
}<br>
<br>
<br>
main.cpp<br>
========<br>
#include <stdio.h><br>
#include "header.h"<br>
<br>
int main() {<br>
 test t;<br>
 t.b = 0;<br>
 set(&t);<br>
 printf("%i\n", t.b);<br>
 return 0;<br>
}<br>
<br>
<br>
compile.sh<br>
==========<br>
#!/bin/bash<br>
<br>
clang -Wall -c lib.c -o lib.o &&<br>
clang -Wall -c main.cpp -o main.o &&<br>
clang main.o lib.o -o test<br>
<br>
<br>
So, the program outputs "0", not "10", and once I'd looked twice (*cough* at least!), the problem was quite apparent, although somewhat obfuscated by the fact that the empty struct was buried deep inside library headers and only became empty due some arcane macro logic...<br>


<br>
Long story short, I'm ok with the fact that C and C++ handle empty structs differently, but I was somewhat challenged by my colleague's complaint that surely the compiler should have given a warning that the structure of 'struct test' varies depending on the language it is used from, especially given that the include file uses 'extern "C"' implying a library originating in C that can be used from C++ code.<br>


<br>
I know that '-pedantic' throws up a warning about a GNU extension, but my suggestion would be that there be a specific warning such as "struct differs in size in C and C++ modes" when parsing an empty struct or a struct containing an empty struct inside an 'extern "C"' block.<br>


<br>
Good idea?  Bad idea?  I'm happy to work on a patch to clang if people think this has merit.<br>
<br>
Cheers,<br>
<br>
Andy<br>
<br>
<br>
______________________________<u></u>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/cfe-dev</a><br>
</blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br>Thanks,<br>--Serge<br>
</font></span></div>
<br>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div></div>