Unexpected error when compiling for ANSI C89

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Unexpected error when compiling for ANSI C89

Eric Fiselier via cfe-dev

Hi CFE-Devs,

 

When I compile the following tiny test with ‘-std=c89’ (‘clang –std=c89 –c xxx.c’):

foo(x) {

  return;

}

I get the following error message:

xxx.c:2:3: error: non-void function 'foo' should return a value [-Wreturn-type]

  return;

  ^

1 error generated.

 

But for ANSI C89, this is legal, and it is a really common pattern to have a return with no value, even though the return type is implicitly ‘int’, especially when K&R syntax is used:

 

Section 3.6.6.4. The return statement, Semantics, paragraph 3

If a return statement without an expression is executed, and the value of the function call is used by the caller, the behaviour is undefined.  reaching the } that terminates a function is equivalent to executing a return statement without an expression.

 

Yet the following results in a warning and not an error:

foo(x) {

}

I get the following perfectly reasonable warning:

xxx.c:2:1: warning: control reaches end of non-void function [-Wreturn-type]

}

^

1 warning generated.

 

When compiling for ANSI C89, I think that the first example should also result in a warning and not an error.

 

Thanks,

            MartinO

 

--------------------------------------------------------------
Intel Research and Development Ireland Limited
Registered in Ireland
Registered Office: Collinstown Industrial Park, Leixlip, County Kildare
Registered Number: 308263

This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected error when compiling for ANSI C89

Eric Fiselier via cfe-dev
On 10/4/2017 3:47 AM, ORiordan, Martin via cfe-dev wrote:

Hi CFE-Devs,

 

When I compile the following tiny test with ‘-std=c89’ (‘clang –std=c89 –c xxx.c’):

foo(x) {

  return;

}

I get the following error message:

xxx.c:2:3: error: non-void function 'foo' should return a value [-Wreturn-type]

  return;

  ^

1 error generated.

 


Looks like an oversight in https://reviews.llvm.org/rL108108; in C99, we want to default to an error, but C89 we shouldn't, as you've pointed out.  See also https://bugs.llvm.org//show_bug.cgi?id=12216.

That said, it's been like this for seven years... it seems like this isn't causing issues in practice.

-Eli
-- 
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected error when compiling for ANSI C89

Eric Fiselier via cfe-dev

Haha, 7 years.  I tried with v4.0 and it was there too, but I did not look further back.  Yes, the test case is contrived, but part of ANSI C89 validation.  I am not aware of any real-world-code that is still around that depends on this though.

 

All the best,

 

            MartinO

 

From: Friedman, Eli [mailto:[hidden email]]
Sent: Wednesday, October 4, 2017 7:15 PM
To: ORiordan, Martin <[hidden email]>; Clang Dev <[hidden email]>
Subject: Re: [cfe-dev] Unexpected error when compiling for ANSI C89

 

On 10/4/2017 3:47 AM, ORiordan, Martin via cfe-dev wrote:

Hi CFE-Devs,

 

When I compile the following tiny test with ‘-std=c89’ (‘clang –std=c89 –c xxx.c’):

foo(x) {

  return;

}

I get the following error message:

xxx.c:2:3: error: non-void function 'foo' should return a value [-Wreturn-type]

  return;

  ^

1 error generated.

 


Looks like an oversight in https://reviews.llvm.org/rL108108; in C99, we want to default to an error, but C89 we shouldn't, as you've pointed out.  See also https://bugs.llvm.org//show_bug.cgi?id=12216.

That said, it's been like this for seven years... it seems like this isn't causing issues in practice.

-Eli

-- 
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project

--------------------------------------------------------------
Intel Research and Development Ireland Limited
Registered in Ireland
Registered Office: Collinstown Industrial Park, Leixlip, County Kildare
Registered Number: 308263

This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev