Bug 78068 - warning: implicit declaration of function ‘time’; did you mean ‘nice’? [-Wimplicit-function-declaration]
Summary: warning: implicit declaration of function ‘time’; did you mean ‘nice’? [-Wimp...
Status: RESOLVED DUPLICATE of bug 82967
Alias: None
Product: gcc
Classification: Unclassified
Component: other (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: David Malcolm
URL:
Keywords: diagnostic
Depends on: 69968
Blocks:
  Show dependency treegraph
 
Reported: 2016-10-21 13:48 UTC by Markus Trippelsdorf
Modified: 2018-09-09 21:37 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-04-18 00:00:00


Attachments
unreduced testcase (62.30 KB, text/plain)
2016-10-21 13:48 UTC, Markus Trippelsdorf
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Trippelsdorf 2016-10-21 13:48:50 UTC
Created attachment 39864 [details]
unreduced testcase

Building binutils trunk on ppc64le shows:

trippels@gcc2-power8 ppc % ~/gcc_7/usr/local/bin/gcc --save-temps -c -g -O2 -DDEFAULT_INLINE=PSIM_INLINE_LOCALS -DWITH_HOST_BYTE_ORDER=LITTLE_ENDIAN -DWITH_SMP=5 -DHAVE_TERMIOS_STRUCTURE -DHAVE_TERMIOS_CLINE -DHAVE_DEVZERO -I. -I. -I./../../include -I../../bfd -I./../../bfd -I../../gdb -I./../../gdb -I./../../gdb/config -DHAVE_COMMON_FPU -I../common -I./../common emul_unix.c                                                                                                                                                                
emul_unix.c: In function ‘do_unix_time’:
emul_unix.c:818:16: warning: implicit declaration of function ‘time’; did you mean ‘nice’? [-Wimplicit-function-declaration]
   time_t now = time ((time_t *)0);
                ^~~~
                nice

 % gcc -c emul_unix.i
emul_unix.c: In function ‘do_unix_time’:
emul_unix.c:818:16: warning: implicit declaration of function ‘time’; did you mean ‘nice’? [-Wimplicit-function-declaration]
Comment 1 Andrew Pinski 2016-10-21 20:38:04 UTC
time declaration really is not present in the preprocessed source file.  I can't seem to figure out why it is not though.

I doubt this is a GCC bug rather than a gdb one (sim is considered part of gdb) or a glibc one.
Comment 2 Markus Trippelsdorf 2016-10-21 20:49:50 UTC
(In reply to Andrew Pinski from comment #1)
> time declaration really is not present in the preprocessed source file.  I
> can't seem to figure out why it is not though.
> 
> I doubt this is a GCC bug rather than a gdb one (sim is considered part of
> gdb) or a glibc one.

David asked me to open this bug.
It is more an aesthetic issue. Clearly the suggestion "nice" is bogus.
I think suggestions should be turned off for -Wimplicit-function-declaration
in general. Other compiler turn them off, too.

markus@x4 tmp % clang -c emul_unix.i
emul_unix.c:818:16: warning: implicit declaration of function 'time' is invalid in C99 [-Wimplicit-function-declaration]
  time_t now = time ((time_t *)0);
               ^
1 warning generated.

markus@x4 tmp % icc -c emul_unix.i
emul_unix.c(818): warning #266: function "time" declared implicitly
    time_t now = time ((time_t *)0);
                 ^
Comment 3 David Malcolm 2016-10-21 21:03:43 UTC
(In reply to Markus Trippelsdorf from comment #2)
> (In reply to Andrew Pinski from comment #1)
> > time declaration really is not present in the preprocessed source file.  I
> > can't seem to figure out why it is not though.
> > 
> > I doubt this is a GCC bug rather than a gdb one (sim is considered part of
> > gdb) or a glibc one.
> 
> David asked me to open this bug.

Thanks for filing it.

> It is more an aesthetic issue. Clearly the suggestion "nice" is bogus.

Indeed.

> I think suggestions should be turned off for -Wimplicit-function-declaration
> in general. Other compiler turn them off, too.
> 
> markus@x4 tmp % clang -c emul_unix.i
> emul_unix.c:818:16: warning: implicit declaration of function 'time' is
> invalid in C99 [-Wimplicit-function-declaration]
>   time_t now = time ((time_t *)0);
>                ^
> 1 warning generated.
> 
> markus@x4 tmp % icc -c emul_unix.i
> emul_unix.c(818): warning #266: function "time" declared implicitly
>     time_t now = time ((time_t *)0);
>                  ^

I'm hoping there's a way of keeping the suggestions.

I think there are two cases here:

(a) the user typoed the function name, as opposed to

(b) the function name at the call site was correct, but for some reason the decl couldn't be found (e.g. due to a missing #include, or bogus #if somewhere, etc)

The suggestions code is currently written from the perspective of case (a), but I suspect that it was case (b) that happened here (and which is why "time" doesn't show up in the preprocessed reproducer).
Comment 4 David Malcolm 2017-01-20 15:07:46 UTC
I had a go at fixing this by introducing a "require_close_matches" flag to best_match (When set, it would require an edit distance of 1), and turning it on for just this warning.

Unfortunately this broke the scanf<->sacnf transposition detection in one of the testcases.

Maybe this would be a valid approach if we implemented transposition handling when computing the edit distance.  (transposition-handling is PR 69968).
Comment 5 Martin Sebor 2017-04-18 21:06:44 UTC
Confirmed with the simple test case below where GCC could conceivably avoid issuing the hint also because the function cannot be used as a replacement for the call to time().  As declared, nice takes an argument but the call to time() doesn't pass one, and returns void but the call is used in a context where an int is expected.

$ cat b.c && gcc -S -Wall b.c
void nice (int);

int foo (void)
{
  return time ();
}

b.c: In function ‘foo’:
b.c:5:10: warning: implicit declaration of function ‘time’; did you mean ‘nice’? [-Wimplicit-function-declaration]
   return time ();
          ^~~~
          nice
Comment 6 David Malcolm 2018-09-09 21:37:19 UTC
My patch for PR 82967 fixes this, with test coverage for this case; marking this one as a duplicate of that one.

*** This bug has been marked as a duplicate of bug 82967 ***