Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Missing support for returns_nonnull attribute

nemequ
New Contributor I
731 Views

It looks like ICC is missing support for the retuns_nonnull attribute (tested up to 19.0.1), which has been present in GCC since 4.9.  Here is a quick test case:

#include <stdio.h>

static const char* foo = "bar\n";

#if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 9)))
__attribute__((__returns_nonnull__))
#endif
static const char* get_string(void) {
  return foo;
}

int main(void) {
  const char* s = get_string();
  if (s != NULL)
    fputs(s, stdout);
  else
    fputs("This shouldn't happen\n", stdout);

  return 0;
}
0 Kudos
3 Replies
Viet_H_Intel
Moderator
731 Views

You meant the warning 1292?

vahoang@orcsle147:/tmp$ icc t8.c
t8.c(6): warning #1292: unknown attribute "__returns_nonnull__"
  __attribute__((__returns_nonnull__))
                 ^

vahoang@orcsle147:/tmp$ ./a.out
bar
 

0 Kudos
nemequ
New Contributor I
731 Views

Yes.  If __GNUC__/__GNUC_MINOR__ are defined to ≥ 4.9 it should be supported or silently ignored.  I'd obviously prefer the former, but it definitely shouldn't generate a diagnostic.  At least not where it does; a diagnostic in main() about checking for null could be nice, but I think in GCC this is mostly used for optimization.  For example, with:

#include <stdio.h>

#if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 9)))
__attribute__((__returns_nonnull__))
#endif
const char* get_string(void);

int main(void) {
  const char* s = get_string();
  if (s != NULL)
    fputs(s, stdout);
  else
    fputs("This shouldn't happen\n", stdout);

  return 0;
}

GCC can elide the checks.  At -O3, GCC generates

   0:	48 83 ec 08          	sub    $0x8,%rsp
   4:	e8 00 00 00 00       	callq  9 <main+0x9>
   9:	48 8b 35 00 00 00 00 	mov    0x0(%rip),%rsi        # 10 <main+0x10>
  10:	48 89 c7             	mov    %rax,%rdi
  13:	e8 00 00 00 00       	callq  18 <main+0x18>
  18:	31 c0                	xor    %eax,%eax
  1a:	48 83 c4 08          	add    $0x8,%rsp
  1e:	c3                   	retq

But if you remove the attribute it generates:

   0:	48 83 ec 08          	sub    $0x8,%rsp
   4:	e8 00 00 00 00       	callq  9 <main+0x9>
   9:	48 85 c0             	test   %rax,%rax
   c:	74 16                	je     24 <main+0x24>
   e:	48 8b 35 00 00 00 00 	mov    0x0(%rip),%rsi        # 15 <main+0x15>
  15:	48 89 c7             	mov    %rax,%rdi
  18:	e8 00 00 00 00       	callq  1d <main+0x1d>
  1d:	31 c0                	xor    %eax,%eax
  1f:	48 83 c4 08          	add    $0x8,%rsp
  23:	c3                   	retq   
  24:	ba 16 00 00 00       	mov    $0x16,%edx
  29:	be 01 00 00 00       	mov    $0x1,%esi
  2e:	bf 00 00 00 00       	mov    $0x0,%edi
  33:	48 8b 0d 00 00 00 00 	mov    0x0(%rip),%rcx        # 3a <main+0x3a>
  3a:	e8 00 00 00 00       	callq  3f <main+0x3f>
  3f:	eb dc                	jmp    1d <main+0x1d>

 

0 Kudos
Viet_H_Intel
Moderator
731 Views

Thanks, will report to our Front-end team.

0 Kudos
Reply