https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80567

            Bug ID: 80567
           Summary: bogus fixit hint for undeclared memset: else
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

For the test case below where the program uses memset without first including
<string.h> G++ suggests as an alternative the "else" keyword.

$ cat z.C && gcc -O2 -Wall -Wextra -Wpedantic z.C
void f (void *p)
{
  memset (p, 0, 4);
}
z.C: In function ‘void f(void*)’:
z.C:3:3: error: ‘memset’ was not declared in this scope
   memset (p, 0, 4);
   ^~~~~~
z.C:3:3: note: suggested alternative: ‘else’
   memset (p, 0, 4);
   ^~~~~~
   else


In C mode, GCC prints the far more helpful (though not perfect):

z.C:3:3: warning: implicit declaration of function ‘memcpy’
[-Wimplicit-function-declaration]
   memcpy (p, "1234", 4);
   ^~~~~~
z.C:3:3: warning: incompatible implicit declaration of built-in function
‘memcpy’
z.C:3:3: note: include ‘<string.h>’ or provide a declaration of ‘memcpy’


Although in C++ it's possible to declare one's own overloads of memset and
other library functions, I think it would be helpful to have G++ issue a hint
similar to the GCC note (i.e., in the absence of any other memset, assume that
the name refers to the standard library function and suggest the user #include
<string.h>).  Ditto for any other C standard library functions.

In any case, it seems that to avoid obviously incorrect suggestions like the
one above, G++ needs to consider more of the context in which an undeclared
identifier is used.

Reply via email to