Hi!
Currently C++ does not warn at all when built-in functions are re-defined
with a different signature, while C does warn on that even without -Wall.
Thus I'd like to propose a -Wall enabled warning for that in C++.
Initially I tried to warn unconditionally but that made too many tests
in the C++ testsuite emit that warning :-(
So making the warning dependent on Wall is a compromise due
to the very many compile only tests, that use this "feature".
There is also a wrong-code side on this redefinition, because
even if the new function has the nothrow attribute the code is
generated as if it could throw. Fixed as well.
Boot-strap and reg-testing on x86_64-linux-gnu.
Is it OK for trunk?
Thanks
Bernd.
gcc:
2016-08-20 Bernd Edlinger <bernd.edlin...@hotmail.de>
PR c++/71973
* doc/invoke.texi: Document -Wbuiltin-function-redefined.
c-family:
2016-08-20 Bernd Edlinger <bernd.edlin...@hotmail.de>
PR c++/71973
* c.opt (Wbuiltin-function-redefined): New warning.
cp:
2016-08-20 Bernd Edlinger <bernd.edlin...@hotmail.de>
PR c++/71973
* decl.c (duplicate_decls): Warn when a built-in function is redefined.
Copy the TREE_NOTHROW flag unmodified to the old decl.
testsuite:
2016-08-20 Bernd Edlinger <bernd.edlin...@hotmail.de>
PR c++/71973
* g++.dg/pr71973.C: New test.
* g++.dg/warn/noeffect5.C: Add extern "C" to built-in function.
* g++.old-deja/g++.other/warn01.C: Likewise.
Index: gcc/c-family/c.opt
===================================================================
--- gcc/c-family/c.opt (revision 239624)
+++ gcc/c-family/c.opt (working copy)
@@ -299,6 +299,10 @@ Wframe-address
C ObjC C++ ObjC++ Var(warn_frame_address) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
Warn when __builtin_frame_address or __builtin_return_address is used unsafely.
+Wbuiltin-function-redefined
+C++ ObjC++ Var(warn_builtin_function_redefined) Warning LangEnabledBy(C++ ObjC++,Wall)
+Warn when a built-in function is redefined.
+
Wbuiltin-macro-redefined
C ObjC C++ ObjC++ CPP(warn_builtin_macro_redefined) CppReason(CPP_W_BUILTIN_MACRO_REDEFINED) Var(cpp_warn_builtin_macro_redefined) Init(1) Warning
Warn when a built-in preprocessor macro is undefined or redefined.
Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c (revision 239624)
+++ gcc/cp/decl.c (working copy)
@@ -1502,6 +1502,14 @@ duplicate_decls (tree newdecl, tree olddecl, bool
}
else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
break;
+ if (t1 || t2
+ || DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)
+ || ! same_type_p (TREE_TYPE (TREE_TYPE (olddecl)),
+ TREE_TYPE (TREE_TYPE (newdecl))))
+ warning_at (DECL_SOURCE_LOCATION (newdecl),
+ OPT_Wbuiltin_function_redefined,
+ "declaration of %q+#D conflicts with built-in "
+ "declaration %q#D", newdecl, olddecl);
}
else if ((DECL_EXTERN_C_P (newdecl)
&& DECL_EXTERN_C_P (olddecl))
@@ -1555,7 +1563,7 @@ duplicate_decls (tree newdecl, tree olddecl, bool
/* Whether or not the builtin can throw exceptions has no
bearing on this declarator. */
- TREE_NOTHROW (olddecl) = 0;
+ TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
{
Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi (revision 239624)
+++ gcc/doc/invoke.texi (working copy)
@@ -256,8 +256,8 @@ Objective-C and Objective-C++ Dialects}.
-pedantic-errors @gol
-w -Wextra -Wall -Waddress -Waggregate-return @gol
-Wno-aggressive-loop-optimizations -Warray-bounds -Warray-bounds=@var{n} @gol
--Wno-attributes -Wbool-compare -Wno-builtin-macro-redefined @gol
--Wc90-c99-compat -Wc99-c11-compat @gol
+-Wno-attributes -Wbool-compare -Wbuiltin-function-redefined @gol
+-Wno-builtin-macro-redefined -Wc90-c99-compat -Wc99-c11-compat @gol
-Wc++-compat -Wc++11-compat -Wc++14-compat -Wcast-align -Wcast-qual @gol
-Wchar-subscripts -Wclobbered -Wcomment -Wconditionally-supported @gol
-Wconversion -Wcoverage-mismatch -Wno-cpp -Wdangling-else -Wdate-time @gol
@@ -5460,6 +5460,13 @@ unrecognized attributes, function attributes appli
etc. This does not stop errors for incorrect use of supported
attributes.
+@item -Wbuiltin-function-redefined @r{(C++ and Objective-C++ only)}
+@opindex Wbuiltin-function-redefined
+@opindex Wno-builtin-function-redefined
+Do warn if built-in functions are redefined. This option is only
+supported for C++ and Objective-C++. It is implied by @option{-Wall},
+which can be disabled with @option{-Wno-builtin-function-redefined}.
+
@item -Wno-builtin-macro-redefined
@opindex Wno-builtin-macro-redefined
@opindex Wbuiltin-macro-redefined
Index: gcc/testsuite/g++.dg/pr71973.C
===================================================================
--- gcc/testsuite/g++.dg/pr71973.C (revision 0)
+++ gcc/testsuite/g++.dg/pr71973.C (working copy)
@@ -0,0 +1,14 @@
+// { dg-do compile }
+// { dg-options "-Wbuiltin-function-redefined -fdump-tree-eh" }
+
+extern "C"
+void fork () // { dg-warning "conflicts with built-in declaration" }
+__attribute__ ((__nothrow__));
+
+void bar () throw ()
+{
+ fork ();
+}
+
+// { dg-final { scan-tree-dump-not "eh_dispatch" "eh" } }
+// { dg-final { scan-tree-dump-not "resx" "eh" } }
Index: gcc/testsuite/g++.dg/warn/noeffect5.C
===================================================================
--- gcc/testsuite/g++.dg/warn/noeffect5.C (revision 239624)
+++ gcc/testsuite/g++.dg/warn/noeffect5.C (working copy)
@@ -2,6 +2,7 @@
/* { dg-do compile } */
/* { dg-options "-Wall" } */
+extern "C"
void *memcpy(void *dest, const void *src, __SIZE_TYPE__ n);
void f (void *dest, const void *src) {
memcpy (dest, src, 0);
Index: gcc/testsuite/g++.old-deja/g++.other/warn01.C
===================================================================
--- gcc/testsuite/g++.old-deja/g++.other/warn01.C (revision 239624)
+++ gcc/testsuite/g++.old-deja/g++.other/warn01.C (working copy)
@@ -2,9 +2,11 @@
// { dg-options "-W -Wall" }
typedef unsigned long size_t;
+extern "C" {
extern void* malloc (size_t);
extern void free (void*);
extern void* realloc (void*, size_t);
+}
struct vtable {
void* (* _malloc) (size_t);