https://gcc.gnu.org/g:40ff519a133b643c9ba0aa01289b5ffb6c4bd42b
commit r17-6668-g40ff519a133b643c9ba0aa01289b5ffb6c4bd42b Author: Martin Uecker <[email protected]> Date: Mon Nov 16 23:42:15 2026 +0100 c: mark nested functions as requiring a static chain earlier [PR128760] When determining whether we can statically initialize a variable using a pointer to a nested function, it is checked that DECL_STATIC_CHAIN is not set. But DECL_STATIC_CHAIN is set only after the function body was parsed, which can be too late. The logic to deactivate the trampoline also had a flaw (mentioned in PR128518). Until the function is completely parsed, we need to conservatively assume that it might require a static chain. Also forward declarations already need to be taken into account. To fix both issues, we set DECL_STATIC_CHAIN already when we have seen the declaration and then clear it at the end of the function when C_DECL_NONLOCAL_CONTEXT was not set. The checks for whether a chain is needed are changed to use DECL_STATIC_CHAIN. Hence, until the body is processed, DECL_STATIC_CHAIN indicates that the function might need a static chain and C_FUNC_NONLOCAL_CONTEXT that it is known for certain that it will. Afterwards both are the same until lower_nested_functions in tree-nested.cc recomputes DECL_STATIC_CHAIN in a later pass. PR c/128760 PR c/128518 gcc/c/ChangeLog: * c-decl.cc (start_decl, start_function): Set DECL_STATIC_CHAIN for nested functions. (finish_finish): Clear DECL_STATIC_CHAIN if not needed. * c-parser.cc (c_parser_declaration_or_fndef): Do not set DECL_STATIC_CHAIN. * c-typeck.cc (function_to_pointer_conversion,mark_decl_used): Use DECL_STATIC_CHAIN instead of C_FUNC_NONLOCAL_CONTEXT. (merge_decls): Add assertion. gcc/testsuite/ChangeLog: * gcc.dg/pr128760.c: New test. * gcc.dg/pr128518.c: Add dg-effective-target. * gcc.dg/pr128518-2.c: New test. * gcc.dg/Wreturn-nested-4.c: New test. Diff: --- gcc/c/c-decl.cc | 20 +++++++++++ gcc/c/c-parser.cc | 5 --- gcc/c/c-typeck.cc | 6 ++-- gcc/testsuite/gcc.dg/Wreturn-nested-4.c | 13 +++++++ gcc/testsuite/gcc.dg/pr128518-2.c | 18 ++++++++++ gcc/testsuite/gcc.dg/pr128518.c | 5 ++- gcc/testsuite/gcc.dg/pr128760.c | 63 +++++++++++++++++++++++++++++++++ 7 files changed, 119 insertions(+), 11 deletions(-) diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 51555f33822a..d1a6b8588999 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -3141,6 +3141,9 @@ merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype) DECL_ABSTRACT_ORIGIN (newdecl) = DECL_ABSTRACT_ORIGIN (olddecl); } + + /* If a new declaration requires a static chain, the old one must too. */ + gcc_assert (!DECL_STATIC_CHAIN (newdecl) || DECL_STATIC_CHAIN (olddecl)); } /* Merge the USED information. */ @@ -5817,6 +5820,13 @@ start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, if (expr && !current_scope->parm_flag) add_stmt (fold_convert (void_type_node, expr)); + /* Mark a forward declaration of a nested function as requiring + a static chain (see start_function). */ + if (TREE_CODE (decl) == FUNCTION_DECL + && declspecs->storage_class == csc_auto + && current_function_decl != NULL_TREE) + DECL_STATIC_CHAIN (decl) = 1; + if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)) && TREE_PUBLIC (decl)) warning (OPT_Wmain, "%q+D is usually a function", decl); @@ -10799,6 +10809,12 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator, if (expr && !current_scope->parm_flag) add_stmt (fold_convert (void_type_node, expr)); + /* Mark nested functions as needing a static chain initially. + The flag is cleared at the end of the function if no static + chain is needed. */ + if (current_function_decl != NULL_TREE) + DECL_STATIC_CHAIN (decl1) = 1; + loc = DECL_SOURCE_LOCATION (decl1); /* A nested function is not global. */ @@ -11570,6 +11586,10 @@ finish_function (location_t end_loc) if (c_dialect_objc ()) objc_finish_function (); + /* Clear the static chain for nested functions that do not need one. */ + if (!C_FUNC_NONLOCAL_CONTEXT (fndecl)) + DECL_STATIC_CHAIN (fndecl) = 0; + if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 74c41e0e6f48..475daf45ed03 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -3376,11 +3376,6 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, if (nested && specs->declspec_il == cdil_none) { tree decl = current_function_decl; - /* Mark nested functions as needing static-chain initially. - lower_nested_functions will recompute it but the - DECL_STATIC_CHAIN flag is also used before that happens, - by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */ - DECL_STATIC_CHAIN (decl) = 1; add_stmt (fnbody); finish_function (endloc); c_pop_function_context (); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 6c9e7d75139c..fef8021742ab 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2380,10 +2380,10 @@ function_to_pointer_conversion (location_t loc, tree exp) tree exp2 = build_unary_op (loc, ADDR_EXPR, exp, false); - /* If the function is defined and known to not require a non-local + /* If the function is known to not require a non-local context, make sure no trampoline is generated. */ if (TREE_CODE (exp) == FUNCTION_DECL - && DECL_INITIAL (exp) && !C_FUNC_NONLOCAL_CONTEXT (exp)) + && !DECL_STATIC_CHAIN (exp)) TREE_NO_TRAMPOLINE (exp2) = 1; return exp2; @@ -3844,7 +3844,7 @@ mark_decl_used (tree ref, bool address) { case FUNCTION_DECL: /* Use of another local function that requires no context is ok. */ - if (!C_FUNC_NONLOCAL_CONTEXT (ref) && DECL_INITIAL (ref)) + if (!DECL_STATIC_CHAIN (ref) || ref == current_function_decl) return; break; case VAR_DECL: diff --git a/gcc/testsuite/gcc.dg/Wreturn-nested-4.c b/gcc/testsuite/gcc.dg/Wreturn-nested-4.c new file mode 100644 index 000000000000..ecdfbdb2dede --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wreturn-nested-4.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target trampolines } */ +/* { dg-options "" } */ + +void *foo() +{ + int bar(int x) + { + return x ? bar(x - 1) * x : 1; + } + + return bar; /* no warning */ +} diff --git a/gcc/testsuite/gcc.dg/pr128518-2.c b/gcc/testsuite/gcc.dg/pr128518-2.c new file mode 100644 index 000000000000..9afd790a2afc --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr128518-2.c @@ -0,0 +1,18 @@ +/* { dg-do run } */ +/* { dg-options "-O0 -std=gnu23" } */ +/* { dg-require-effective-target trampolines } */ + +void call(void (*f)(int), int k) { f(k); } +int main(void) { + int x = 0; + void n2(int k) { + void n3(void) { call(n2, 0); } + if (k) + n3(); + else + x = 42; + } + call(n2, 1); + if (x != 42) + __builtin_abort(); +} diff --git a/gcc/testsuite/gcc.dg/pr128518.c b/gcc/testsuite/gcc.dg/pr128518.c index 7f809791c73a..6bd3344d281a 100644 --- a/gcc/testsuite/gcc.dg/pr128518.c +++ b/gcc/testsuite/gcc.dg/pr128518.c @@ -1,7 +1,7 @@ /* { dg-do run } */ /* { dg-options "-O1" } */ - -int printf(const char *, ...); +/* { dg-require-effective-target trampolines } */ +/* { dg-require-effective-target nonlocal_goto } */ void call(void (*f)(void)) { f(); } @@ -20,7 +20,6 @@ int main(void) void n4(void) { call(n2); } n4(); o1: - printf("ok\n"); return 0; } diff --git a/gcc/testsuite/gcc.dg/pr128760.c b/gcc/testsuite/gcc.dg/pr128760.c new file mode 100644 index 000000000000..cd3bad06d49e --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr128760.c @@ -0,0 +1,63 @@ +/* { dg-do compile } */ +/* { dg-options "-std=gnu23" } */ +/* { dg-require-effective-target trampolines } */ + +int f(int n) +{ + int g() + { + static int (*p)() = &g; /* { dg-error "initializer element is not constant" } */ + return n; + } + static int (*q)() = &g; /* { dg-error "initializer element is not constant" } */ + return g(); +} + +int h(int n) +{ + auto int g(); + static int (*p)() = &g; /* { dg-error "initializer element is not constant" } */ + int g() + { + return n; + } + static int (*q)() = &g; /* { dg-error "initializer element is not constant" } */ + + return g(); +} + +int i(int n) +{ + auto int g(); + static int (*p)() = &g; /* { dg-error "initializer element is not constant" } */ + int g() + { + return n; + } + static int (*q)() = &g; /* { dg-error "initializer element is not constant" } */ + + return g(); +} + +int j() +{ + auto int g(); + auto int g(); + static int (*q)() = &g; /* { dg-error "initializer element is not constant" } */ + int g() + { + return 0; + } + + return g(); +} + +int k() +{ + extern int g(); + static int (*q)() = &g; /* no warning */ + + return g(); +} + +
