Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 20 Feb 2016 12:51:03 +0000
From:      David Chisnall <theraven@FreeBSD.org>
To:        Steve Kargl <sgk@troutmask.apl.washington.edu>
Cc:        freebsd-toolchain@freebsd.org
Subject:   Re: confusing messages from clang
Message-ID:  <F3FF5389-DDEC-4D26-B004-FE3596021BE8@FreeBSD.org>
In-Reply-To: <20160220005749.GA84382@troutmask.apl.washington.edu>
References:  <20160220005749.GA84382@troutmask.apl.washington.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
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@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;
>=20
> whoops:
>   printf("whoops\n");
>   return
>=20
> corrupt:
>   printf("corrupt\n=E2=80=9D);

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=E2=80=99s no warning that a return =
statement with a value is invalid in a function that returns void, but =
perhaps that=E2=80=99s 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=E2=80=99t 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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?F3FF5389-DDEC-4D26-B004-FE3596021BE8>