confusing messages from clang

David Chisnall theraven at FreeBSD.org
Sat Feb 20 12:51:21 UTC 2016


C compilers are always doing best effort attempts to report when you feed them code that is not valid C.

For example, in this case:

On 20 Feb 2016, at 00:57, Steve Kargl <sgk at troutmask.apl.washington.edu> wrote:
>   if (i > 0)
>      goto corrupt;

This is valid, as long as you have a label called corrupt to look for.  You do not, however, because:

>   return;
> 
> whoops:
>   printf("whoops\n");
>   return
> 
> corrupt:
>   printf("corrupt\n”);

The statement:

> return corrupt: printf("corrupt\n");


is just confusing.  It appears to be trying to return the value in corrupt (which is not an identifier that corresponds to any valid variable) and then has some trailing characters after the end of the statement.  Fortunately, the compiler tells you exactly what is wrong:

First it says:

> foo.c:21:1: error: use of undeclared identifier 'corrupt'; did you mean 'crypt'?
> corrupt:
> ^~~~~~~

Here, it is telling you that the value passed to your return statement is an undeclared identifier.  Then it tells you that you have more tokens after the end of your return statement:

> foo.c:21:8: error: expected ';' after return statement
> corrupt:
>       ^
>       ;

I am slightly surprised that there’s no warning that a return statement with a value is invalid in a function that returns void, but perhaps that’s because after finding two things wrong with one statement it gives up.

The correct fix, of course, is to insert the missing semicolon after the return at the end of line 19.  If you had tried compiling the same thing with gcc 5, then you would have noticed that you get very similar error messages (though gcc doesn’t attempt to provide a fixit hint and does warn that you have a return statement returning a value from a function that returns void).

David



More information about the freebsd-toolchain mailing list