Skip to content

Commit f195d69

Browse files
committed
Support return statements in C(!) constructors/destructors
constructors/destructors are a GCC extension, and their return type must be void as checked earlier. As the type is then replaced by ID_constructor/ID_destructor, and checks on the return type must consider this case.
1 parent fbdd728 commit f195d69

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <assert.h>
2+
3+
#ifdef __GNUC__
4+
int x;
5+
6+
static __attribute__((constructor)) void format_init(void);
7+
8+
static __attribute__((constructor))
9+
void format_init(void)
10+
{
11+
x=42;
12+
return;
13+
}
14+
#endif
15+
16+
int main()
17+
{
18+
#ifdef __GNUC__
19+
assert(x==42);
20+
#endif
21+
return 0;
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/c_typecheck_code.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,9 @@ void c_typecheck_baset::typecheck_return(codet &code)
901901
{
902902
if(code.operands().empty())
903903
{
904-
if(follow(return_type).id()!=ID_empty)
904+
if(follow(return_type).id()!=ID_empty &&
905+
return_type.id()!=ID_constructor &&
906+
return_type.id()!=ID_destructor)
905907
{
906908
// gcc doesn't actually complain, it just warns!
907909
// We'll put a zero here, which is dubious.

0 commit comments

Comments
 (0)