<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW " title="NEW --- - Clang ignores nonnull attributes on declarations following definitions when compiling as C++; behavior differs from gcc and EDG" href="https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_bugs_show-5Fbug.cgi-3Fid-3D24134&d=AwMBaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=pF93YEPyB-J_PERP4DUZOJDzFVX5ZQ57vQk33wu0vio&m=MBbbafjpU12o_j6eJMwXEEqYmIwX1ulK4t-wKEvdEEA&s=t3I6rDgyOoQazY9x_Era5r6VJguPPQ2rq8tiWIAR9yc&e=">24134</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang ignores nonnull attributes on declarations following definitions when compiling as C++; behavior differs from gcc and EDG
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>thonermann@coverity.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Clang (3.4-3.6,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.

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,
so this behavior appears to be inconsistent with other common compilers.

$ cat t.c
void (*pf)(int *p1);
extern void (*pf)(int *p1)
    __attribute__((nonnull));
void f() {
    pf(0);
}


# Compiling as C, no warning is emitted, the nonnull attribute
# is present in the resulting AST:
$ clang -c -Xclang -ast-dump t.c
t.c:5:9: warning: null passed to a callee that requires a non-null argument
[-Wnonnull]
    pf(0);
       ~^
...
`-VarDecl 0x822dda0 prev 0x822dc50 <line:2:1, col:26> col:15 pf 'void (*)(int
*)' extern
  `-NonNullAttr 0x822de00 <line:3:20>
...


# Compiling as C++, a warning is emitted and the nonnull attribute
# is not present in the resulting AST:
$ clang -c -Xclang -ast-dump -x c++ t.c
t.c:3:20: warning: attribute declaration must precede definition
[-Wignored-attributes]
    __attribute__((nonnull));
                   ^
t.c:1:8: note: previous definition is here
void (*pf)(int *p1);
       ^
...
`-VarDecl 0x82e41e0 prev 0x82e4090 <line:2:1, col:26> col:15 pf 'void (*)(int
*)' extern
...
1 warning generated.


# 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++:
$ gcc -Wall -c t.c
t.c: In function 'f':
t.c:5:5: warning: null argument where non-null required (argument 1)
[-Wnonnull]
$ gcc -Wall -c -x c++ t.c
t.c: In function 'void f()':
t.c:5:9: warning: null argument where non-null required (argument 1)
[-Wnonnull]</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>