<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Jul 13, 2015 at 8:15 AM, Tom Honermann <span dir="ltr"><<a href="mailto:thonermann@coverity.com" target="_blank">thonermann@coverity.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Clang (3.4-trunk) ignores nonnull attributes on declarations following definitions when compiling as C++, but not as C.  I presume the difference has to do with tentative definitions in C.<br>
<br>
A test case is below.  Recent versions of gcc and EDG both allow nonnull attributes on declarations following a definition, at least for this test case.  Is it desirable for Clang to act differently in this respect?<br></blockquote><div><br></div><div>We don't support adding attributes after a definition. (In the case where the attributes affect the generated code for the definition, that way lies madness, and we don't distinguish between those attributes and others for consistency -- note that nonnull *does* affect code generation in some cases.) The difference between C and C++ here is most likely exactly what you suspect (the first declaration is only a tentative definition in C).</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Tom.<br>
<br>
$ cat t.c<br>
void (*pf)(int *p1);<br>
extern void (*pf)(int *p1)<br>
    __attribute__((nonnull));<br>
void f() {<br>
    pf(0);<br>
}<br>
<br>
<br>
# Compiling as C, no warning is emitted, the nonnull attribute<br>
# is present in the resulting AST:<br>
$ clang -c -Xclang -ast-dump t.c<br>
t.c:5:9: warning: null passed to a callee that requires a non-null argument [-Wnonnull]<br>
    pf(0);<br>
       ~^<br>
...<br>
`-VarDecl 0x822dda0 prev 0x822dc50 <line:2:1, col:26> col:15 pf 'void (*)(int *)' extern<br>
  `-NonNullAttr 0x822de00 <line:3:20><br>
...<br>
<br>
<br>
# Compiling as C++, a warning is emitted and the nonnull attribute<br>
# is not present in the resulting AST:<br>
$ clang -c -Xclang -ast-dump -x c++ t.c<br>
t.c:3:20: warning: attribute declaration must precede definition [-Wignored-attributes]<br>
    __attribute__((nonnull));<br>
                   ^<br>
t.c:1:8: note: previous definition is here<br>
void (*pf)(int *p1);<br>
       ^<br>
...<br>
`-VarDecl 0x82e41e0 prev 0x82e4090 <line:2:1, col:26> col:15 pf 'void (*)(int *)' extern<br>
...<br>
1 warning generated.<br>
<br>
<br>
# gcc (4.6.3) emits a warning when invoking *pf with a null argument regardless of whether the code is compiled as C or C++:<br>
$ gcc -Wall -c t.c<br>
t.c: In function 'f':<br>
t.c:5:5: warning: null argument where non-null required (argument 1) [-Wnonnull]<br>
$ gcc -Wall -c -x c++ t.c<br>
t.c: In function 'void f()':<br>
t.c:5:9: warning: null argument where non-null required (argument 1) [-Wnonnull]<br>
_______________________________________________<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" rel="noreferrer" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
</blockquote></div><br></div></div>