Skip to content

Commit 4c3a0a6

Browse files
authored
Merge pull request #5674 from tautschnig/invalid-fn
C/C++ front-ends: report a meaningful error for invalid function definition
2 parents 00aefae + 5233796 commit 4c3a0a6

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void a() = {};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE test-c++-front-end
2+
main.c
3+
4+
^EXIT=(64|1)$
5+
^SIGNAL=0$
6+
function 'a(\(\))?' is initialized with initializer_list
7+
^CONVERSION ERROR$
8+
--
9+
Invariant check failed

src/ansi-c/c_typecheck_base.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,15 @@ void c_typecheck_baset::typecheck_redefinition_non_type(
505505

506506
void c_typecheck_baset::typecheck_function_body(symbolt &symbol)
507507
{
508-
code_typet &code_type=to_code_type(symbol.type);
508+
if(symbol.value.id() != ID_code)
509+
{
510+
error().source_location = symbol.location;
511+
error() << "function '" << symbol.name << "' is initialized with "
512+
<< symbol.value.id() << eom;
513+
throw 0;
514+
}
509515

510-
assert(symbol.value.is_not_nil());
516+
code_typet &code_type = to_code_type(symbol.type);
511517

512518
// reset labels
513519
labels_used.clear();

src/cpp/cpp_typecheck_function.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ void cpp_typecheckt::convert_function(symbolt &symbol)
9191
if(symbol.value.is_nil())
9292
return;
9393

94+
if(symbol.value.id() != ID_code)
95+
{
96+
error().source_location = symbol.location;
97+
error() << "function '" << symbol.name << "' is initialized with "
98+
<< symbol.value.id() << eom;
99+
throw 0;
100+
}
101+
94102
// enter appropriate scope
95103
cpp_save_scopet saved_scope(cpp_scopes);
96104
cpp_scopet &function_scope=cpp_scopes.set_scope(symbol.name);

0 commit comments

Comments
 (0)