From: Martin Uecker <uecker@tugraz.at>
To: gcc-patches@gcc.gnu.org
Cc: Joseph Myers <joseph@codesourcery.com>,
Marek Polacek <polacek@redhat.com>
Subject: [C PATCH] error for function with external and internal linkage [PR111708]
Date: Sat, 14 Oct 2023 14:28:30 +0200 [thread overview]
Message-ID: <8642fb1392c8e74c436feaf2b2f4e1d3641530eb.camel@tugraz.at> (raw)
Bootstrapped and regression tested on x86_64.
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.
diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 5822faf01b4..52490a784d0 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -8032,6 +8032,28 @@ 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
+ && storage_class != csc_static
+ && storage_class != csc_auto
+ && 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 %<static%> "
+ "redeclared %<extern%>");
+ }
}
else
{
diff --git a/gcc/testsuite/gcc.dg/pr111708-1.c b/gcc/testsuite/gcc.dg/pr111708-1.c
new file mode 100644
index 00000000000..4af7f53d75f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr111708-1.c
@@ -0,0 +1,42 @@
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+extern int a(void); // external linkage (6.2.2p4)
+static int a(void); /* { dg-error "static declaration of 'a' follows non-static declaration" } */
+
+static int b(void); // internal linkage (6.2.2p3)
+extern int b(void); // internal linkage (6.2.2p4)
+
+static int h0(void);
+
+void s(void)
+{
+ extern int h0(void); // internal linkage (6.2.2p4),
+ extern int h0(void); // internal linkage (6.2.2p4), redeclaration, ok
+ extern int h2(void); // external linkage (6.2.2p4)
+ extern int h2(void); // external linkage (6.2.2p4), redeclaration, ok.
+}
+
+
+extern int i(void); // external linkage (6.2.2p4)
+static int j(void); // internal linkage (6.2.2p3)
+
+void bar(void)
+{
+ extern int i(void); // external linkage (6.2.2p4), ok
+}
+
+void foo(void)
+{
+ extern int j(void); // internal linkage (6.2.2p4), ok, internal
+}
+
+void x(void)
+{
+ int i(void); // no linkage (6.2.2p6)
+ int j; // no linkage (6.2.2p6)
+ {
+ extern int j(void); /* { dg-error "function previously declared 'static' redeclared 'extern'" } */
+ }
+}
+
diff --git a/gcc/testsuite/gcc.dg/pr111708-2.c b/gcc/testsuite/gcc.dg/pr111708-2.c
new file mode 100644
index 00000000000..065c0525c2e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr111708-2.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "" } */
+/* { dg-require-effective-target trampolines } */
+
+static void pp(void)
+{
+ int pp;
+ {
+ auto void pp(void);
+ void pp(void) { }
+ }
+}
+
+static void q2(void);
+
+static void qq(void)
+{
+ auto void q2(void);
+ void q2(void) { }
+}
+
next reply other threads:[~2023-10-14 12:28 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-14 12:28 Martin Uecker [this message]
2023-10-16 21:34 ` Joseph Myers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8642fb1392c8e74c436feaf2b2f4e1d3641530eb.camel@tugraz.at \
--to=uecker@tugraz.at \
--cc=gcc-patches@gcc.gnu.org \
--cc=joseph@codesourcery.com \
--cc=polacek@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).