public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [RFC PATCH] diagnose built-in declarations without prototype (PR 83656)
@ 2018-06-27  2:58 Martin Sebor
  2018-06-27 15:17 ` Jeff Law
  2018-06-27 18:44 ` Eric Gallager
  0 siblings, 2 replies; 23+ messages in thread
From: Martin Sebor @ 2018-06-27  2:58 UTC (permalink / raw)
  To: Gcc Patch List

[-- Attachment #1: Type: text/plain, Size: 1714 bytes --]

With the exception of built-ins with the ellipsis (like sprintf),
GCC silently accepts declarations of built-in functions without
prototypes as well as calls to such functions with any numbers
or types of arguments, compatible or otherwise.  Calls with
arguments whose number and types match exactly those of
the built-in are considered by the middle-end for optimization.
Other calls (compatible or not, irrespective of whether their
number matches the number expected by the function) are then
made to the library functions.

Attached is a small fix to -Wbuiltin-declaration-mismatch to
have it diagnose built-in declarations without a prototype.
The warning is enabled by default so it causes a fair number
of tests to fail because of declarations like 'void abort();'
The breakdown of the built-ins behind the test failures is
below.

Before I take the time to clean up the test suite let me post
what I have in case going this route is not acceptable.  As
an alternative, I could try to avoid some of these warnings,
e.g., by diagnosing incompatible calls instead but I think
it's even less worthwhile for built-ins than trying to do
it for ordinary functions with -Wstrict-prototypes.  There
is, in my view, no justification today for standard functions
to be declared without a prototype.  (I could also make
the warning depend on language mode and -Wpedantic if that
were more preferable.)

Martin

About 115 tests fail due to incompatible declarations of
the built-in functions below (the number shows the number
of warnings for each functions):

428   abort
  58   exit
  36   memcpy
  17   memmove
  15   realloc
  14   cabs
   5   strncpy
   4   strcmp
   3   alloca
   2   rindex
   1   aligned_alloc

[-- Attachment #2: gcc-83656.diff --]
[-- Type: text/x-patch, Size: 2705 bytes --]

PR c/83656 - missing -Wbuiltin-declaration-mismatch on declaration without prototype

gcc/c/ChangeLog:

	PR c/83656
	* c-decl.c (diagnose_mismatched_decls): Diagnose declarations of
	built-in functions without a prototype.

gcc/testsuite/ChangeLog:

	PR c/83656
	* gcc.dg/Wbuiltin-declaration-mismatch-5.c: New test.

diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index 6c9e667..7008176 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -2088,15 +2088,29 @@ diagnose_mismatched_decls (tree newdecl, tree olddecl,
 	 can't validate the argument list) the built-in definition is
 	 overridden, but optionally warn this was a bad choice of name.  */
       if (DECL_BUILT_IN (olddecl)
-	  && !C_DECL_DECLARED_BUILTIN (olddecl)
-	  && (!TREE_PUBLIC (newdecl)
-	      || (DECL_INITIAL (newdecl)
-		  && !prototype_p (TREE_TYPE (newdecl)))))
+	  && !C_DECL_DECLARED_BUILTIN (olddecl))
 	{
-	  warning (OPT_Wshadow, "declaration of %q+D shadows "
-		   "a built-in function", newdecl);
-	  /* Discard the old built-in function.  */
-	  return false;
+	  if (!TREE_PUBLIC (newdecl)
+	      || (DECL_INITIAL (newdecl)
+		  && !prototype_p (TREE_TYPE (newdecl))))
+	    {
+	      warning_at (DECL_SOURCE_LOCATION (newdecl),
+			  OPT_Wshadow, "declaration of %qD shadows "
+			  "a built-in function", newdecl);
+	      /* Discard the old built-in function.  */
+	      return false;
+	    }
+
+	  if (!prototype_p (TREE_TYPE (newdecl)))
+	    {
+	      warning_at (DECL_SOURCE_LOCATION (newdecl),
+			  OPT_Wbuiltin_declaration_mismatch,
+			  "declaration of built-in function %qD without "
+			  "a prototype; expected %qT",
+			  newdecl, TREE_TYPE (olddecl));
+	      /* Discard the old built-in function.  */
+	      return false;
+	    }
 	}
 
       if (DECL_INITIAL (newdecl))
diff --git a/gcc/testsuite/gcc.dg/Wbuiltin-declaration-mismatch-5.c b/gcc/testsuite/gcc.dg/Wbuiltin-declaration-mismatch-5.c
new file mode 100644
index 0000000..51a634d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wbuiltin-declaration-mismatch-5.c
@@ -0,0 +1,10 @@
+/* PR c/83656 - missing -Wbuiltin-declaration-mismatch on declaration
+   without prototype
+   { dg-do compile }
+   { dg-options "-Wbuiltin-declaration-mismatch" } */
+
+void* memcpy ();   /* { dg-warning "declaration of built-in function .memcpy. without a prototype; expected .void \\\*\\\(void \\\*, const void \\\*, \(long \)?unsigned int\\\)." } */
+
+int strcmp ();   /* { dg-warning "declaration of built-in function .strcmp. without a prototype; expected .int\\\(const char* \\\*, const char \\\*\\\)." } */
+
+int strcpy ();   /* { dg-warning "conflicting types for built-in function .strcpy.; expected .char \\\*\\\(char \\\*, const char \\\*\\\)." } */

^ permalink raw reply	[flat|nested] 23+ messages in thread
* Re: [RFC PATCH] diagnose built-in declarations without prototype (PR 83656)
@ 2018-06-30 13:52 Bernd Edlinger
  0 siblings, 0 replies; 23+ messages in thread
From: Bernd Edlinger @ 2018-06-30 13:52 UTC (permalink / raw)
  To: Martin Sebor; +Cc: Jakub Jelinek, Jeff Law, gcc-patches

Hi Martin,

really nice work.
Just one minor nit:


 >--- a/gcc/doc/invoke.texi
 >+++ b/gcc/doc/invoke.texi
 >@@ -6568,8 +6568,13 @@ attributes.
 > @item -Wno-builtin-declaration-mismatch
 > @opindex Wno-builtin-declaration-mismatch
 > @opindex Wbuiltin-declaration-mismatch
 >-Warn if a built-in function is declared with the wrong signature or
 >-as non-function.
 >+Warn if a built-in function is declared with the wrong signature or
 >+as non-function.  Also warn when a built-in function that takes arguments
 >+is declared without a prototype, and for calls to built-in functions
 >+declared ithout a prototype made with the wrong number of arguments or

s/ithout/without/


Bernd.

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2018-11-15 19:14 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-27  2:58 [RFC PATCH] diagnose built-in declarations without prototype (PR 83656) Martin Sebor
2018-06-27 15:17 ` Jeff Law
2018-06-27 15:27   ` Jakub Jelinek
2018-06-27 21:53     ` Jeff Law
2018-06-28  2:41       ` Martin Sebor
2018-06-29 15:23         ` Jeff Law
2018-06-29 15:43           ` Jakub Jelinek
2018-06-29 16:26           ` Martin Sebor
2018-07-04  2:34             ` Jeff Law
2018-07-04 17:32               ` Martin Sebor
2018-07-05 19:44                 ` Jeff Law
2018-11-02 19:50                   ` [PATCH] " Martin Sebor
2018-11-02 22:52                     ` Joseph Myers
2018-11-02 23:40                       ` Martin Sebor
2018-11-06 23:56                         ` Martin Sebor
2018-11-14 18:08                           ` Jeff Law
2018-11-15 10:11                             ` Richard Biener
2018-11-15 19:14                               ` Martin Sebor
2018-06-29 17:25           ` [RFC PATCH] " Eric Gallager
2018-06-28  1:41     ` Martin Sebor
2018-06-27 18:44 ` Eric Gallager
2018-06-28  2:01   ` Martin Sebor
2018-06-30 13:52 Bernd Edlinger

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).