[cfe-dev] C90 conformance: return statements without expressions in non-void functions
Ken Dyck
Ken.Dyck at onsemi.com
Tue Sep 14 07:54:51 PDT 2010
I have been using the Plum Hall Validation Suite for C [1] to validate a
Clang/LLVM backend that I'm working on.
In the conformance-testing code of the suite is defined the following
function:
// test2.c
int iequals(int, int, int);
#if !defined(CPLUSPLUS) && !defined(C99)
yfunc(i, pfn, pi) /* no return type, default to int */
int i;
int pfn(); /* parm w fn type becomes ptr-to-fn */
int *pi;
#else
int yfunc(int i, int pfn(int*), int* pi) /* make yfunc explicit int
for C++ */
/* parm w fn type becomes ptr-to-fn */
#endif /* CPLUSPLUS */
{
static int j = 3;
iequals(__LINE__, i, j);
if (--i == 0)
#if !defined(CPLUSPLUS) && !defined(C99)
return;
#else
return 0;
#endif
--j;
yfunc(i, pfn, pi);
iequals(__LINE__, i, j);
++j;
pfn(pi); /* converts to (*pfn)() */
#if !defined(CPLUSPLUS) && !defined(C99)
/* no return expression, would be erroneous to use value */
#else
return 0; /* must return expr, in C++ */
#endif /* CPLUSPLUS */
}
The C99 macro is defined when testing against the C99 standard and is
undefined for C90.
This code fails to compile with Clang in the C90 case:
> clang -S -x c -std=iso9899:1990 -o - test2.c
test2.c:19:5: error: non-void function 'yfunc' should return a value
[-Wreturn-type]
return;
^
1 error generated.
Section 6.6.4 of C90 reads, "If a return statement without an expression
is executed, and the value of the function call is used by the caller,
the behavior is undefined." The standard says nothing (at least that
I've found) about the case where the value of the function is _not_
used, which happens to be the case for yfunc() in the context of the
validation suite.
Can any language lawyers comment on whether yfunc() should compile in
C90 mode?
-Ken
[1] http://www.plumhall.com/stec.html
More information about the cfe-dev
mailing list