From 73aea290cc5417e4025eb92d95afaf1eeab73631 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Thu, 3 Feb 2005 02:21:10 +0000 Subject: [PATCH] re PR c/17807 (No warning/error for undefined local function.) PR c/17807 * c-decl.c (undef_nested_function): New variable. (pop_scope): Diagnose undefined nested functions. (finish_function): Don't attempt cgraph processing or genericizing if current top-level function contained an undefined nested function. Reset undef_nested_function at the end of a top-level function. testsuite: * gcc.dg/nested-func-3.c: New test. * gcc.dg/pr18596-3.c: Expect error for undefined nested function. From-SVN: r94645 --- gcc/ChangeLog | 10 ++++++++++ gcc/c-decl.c | 17 ++++++++++++++++- gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/gcc.dg/nested-func-3.c | 20 ++++++++++++++++++++ gcc/testsuite/gcc.dg/pr18596-3.c | 1 + 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/nested-func-3.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 31c8ea822db..ee74a41e956 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,13 @@ +2005-02-03 Joseph S. Myers + + PR c/17807 + * c-decl.c (undef_nested_function): New variable. + (pop_scope): Diagnose undefined nested functions. + (finish_function): Don't attempt cgraph processing or genericizing + if current top-level function contained an undefined nested + function. Reset undef_nested_function at the end of a top-level + function. + 2005-02-02 Zdenek Dvorak * tree.c (build_int_cst_type): Take sign of the value into account diff --git a/gcc/c-decl.c b/gcc/c-decl.c index ad76f235de9..0b7b97ede23 100644 --- a/gcc/c-decl.c +++ b/gcc/c-decl.c @@ -149,6 +149,11 @@ static int warn_about_return_type; static int current_extern_inline; +/* Nonzero when the current toplevel function contains a declaration + of a nested function which is never defined. */ + +static bool undef_nested_function; + /* True means global_bindings_p should return false even if the scope stack says we are in file scope. */ bool c_override_global_bindings_to_false; @@ -759,6 +764,12 @@ pop_scope (void) && DECL_ABSTRACT_ORIGIN (p) != 0 && DECL_ABSTRACT_ORIGIN (p) != p) TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1; + if (!DECL_EXTERNAL (p) + && DECL_INITIAL (p) == 0) + { + error ("%Jnested function %qD declared but never defined", p, p); + undef_nested_function = true; + } goto common_symbol; case VAR_DECL: @@ -6376,7 +6387,8 @@ finish_function (void) until their parent function is genericized. Since finalizing requires GENERIC, delay that as well. */ - if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) + if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node + && !undef_nested_function) { if (!decl_function_context (fndecl)) { @@ -6402,6 +6414,9 @@ finish_function (void) } } + if (!decl_function_context (fndecl)) + undef_nested_function = false; + /* We're leaving the context of this function, so zap cfun. It's still in DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 3e1fee45ec7..1d80350306a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2005-02-03 Joseph S. Myers + + PR c/17807 + * gcc.dg/nested-func-3.c: New test. + * gcc.dg/pr18596-3.c: Expect error for undefined nested function. + 2005-02-02 Janis Johnson * gcc.test-framework/gen_directive_tests: Generate tests for diff --git a/gcc/testsuite/gcc.dg/nested-func-3.c b/gcc/testsuite/gcc.dg/nested-func-3.c new file mode 100644 index 00000000000..c4c016cfc4b --- /dev/null +++ b/gcc/testsuite/gcc.dg/nested-func-3.c @@ -0,0 +1,20 @@ +/* Undefined nested function should be a error, whether or not the + function is called. Bug 17807. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +void +f (void) +{ + auto int fn (int); /* { dg-error "error: nested function 'fn' declared but never defined" } */ + auto int fn2 (int); /* { dg-error "error: nested function 'fn2' declared but never defined" } */ + sizeof(fn(1)); +} + +void +h (void) +{ + auto int hn (int); /* { dg-error "error: nested function 'hn' declared but never defined" } */ + hn (1); +} diff --git a/gcc/testsuite/gcc.dg/pr18596-3.c b/gcc/testsuite/gcc.dg/pr18596-3.c index c2a04f84c23..74a6e63b56a 100644 --- a/gcc/testsuite/gcc.dg/pr18596-3.c +++ b/gcc/testsuite/gcc.dg/pr18596-3.c @@ -6,6 +6,7 @@ int foo () static g () = 0; /* { dg-error "invalid storage class" } */ static int f () = 1; /* { dg-error "invalid storage class" } */ auto int h () = 0; /* { dg-error "initialized like a variable" } */ + /* { dg-error "declared but never defined" "nested" { target *-*-* } 8 } */ static int i () = { 0 }; /* { dg-error "invalid storage class" } */ static int j () = /* { dg-error "invalid storage class" } */ { 0, 0.0 }; -- 2.30.2