From 1f186f64b8602d74769af4a6250255e51227f744 Mon Sep 17 00:00:00 2001 From: Martin Uecker Date: Sat, 14 Oct 2023 09:09:07 +0200 Subject: c: error for function with external and internal linkage [PR111708] Declaring a function with both external and internal linkage in the same TU is translation-time UB. Add an error for this case as already done for objects. PR c/111708 gcc/c/ChangeLog: * c-decl.cc (grokdeclarator): Add error. gcc/testsuite/ChangeLog: * gcc.dg/pr111708-1.c: New test. * gcc.dg/pr111708-2.c: New test. --- gcc/c/c-decl.cc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'gcc/c/c-decl.cc') diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 5822faf..0de3847 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -8032,6 +8032,27 @@ grokdeclarator (const struct c_declarator *declarator, TREE_THIS_VOLATILE (decl) = 1; } } + + /* C99 6.2.2p7: It is invalid (compile-time undefined + behavior) to create an 'extern' declaration for a + function if there is a global declaration that is + 'static' and the global declaration is not visible. + (If the static declaration _is_ currently visible, + the 'extern' declaration is taken to refer to that decl.) */ + if (!initialized + && TREE_PUBLIC (decl) + && current_scope != file_scope) + { + tree global_decl = identifier_global_value (declarator->u.id.id); + tree visible_decl = lookup_name (declarator->u.id.id); + + if (global_decl + && global_decl != visible_decl + && VAR_OR_FUNCTION_DECL_P (global_decl) + && !TREE_PUBLIC (global_decl)) + error_at (loc, "function previously declared % " + "redeclared %"); + } } else { -- cgit v1.1