[C++ Patch] PR 82466 ("Missing warning for re-declaration of built-in function as variable")

Paolo Carlini paolo.carlini@oracle.com
Mon Oct 9 10:56:00 GMT 2017


Hi,

this issue filed by Bernd is sort-of a small follow-up to his c++/71973: 
the C front-end already warns for things like:

     int printf;

where we are declaring in the global namespace a built-in. Beyond 
copying over to the C++ front-end the code already existing in the C 
front-end, there are a couple of nits: 1- We probably want to enable 
suppressing the warning, give it a name, for this I'm overloading the 
name already added by Bernd for c++/71973 (using it in the C front-end 
too, without changes to the default behavior, seems a very safe tweak to 
me); 2- as a pure C++ nit, we probably want to take into account 
namespaces, thus the additional check CP_DECL_CONTEXT (newdecl) == 
global_namespace and the new C++ test (for a couple of hours I had in 
the tree CP_DECL_CONTEXT (newdecl) == CP_DECL_CONTEXT (olddecl), 
probably unnecessarily more verbose!?). What else? A couple of existing 
C++ testcases needed rather obvious adjusting, note in particular 
g++.old-deja/g++.mike/p811.C, which has flattened in the global 
namespace a declaration 'extern ostream clog;', which, were in namespace 
std, as per the standard, would not trigger the new warning, even if 
'clog' is also the name of a math builtin, thanks to the additional 
check mentioned above. Tested x86_64.

Thanks, Paolo.

//////////////////

-------------- next part --------------
2017-10-09  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/82466
	* doc/invoke.texi ([Wbuiltin-declaration-mismatch]): Extend
	description.

/cp
2017-10-19  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/82466
	* decl.c (duplicate_decls): Warn for built-in functions declared as
	non-function, use OPT_Wbuiltin_declaration_mismatch.

	* decl.c (duplicate_decls): Avoid redundant '+' in warning_at.

/c
2017-10-19  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/82466
	* c-decl.c (diagnose_mismatched_decls): Use
	OPT_Wbuiltin_declaration_mismatch.

/testsuite
2017-10-19  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/82466
	* c-c++-common/Wbuiltin-declaration-mismatch-1.c: New.
	* c-c++-common/Wno-builtin-declaration-mismatch-1.c: Likewise.
	* g++.dg/warn/Wbuiltin_declaration_mismatch-1.C: Likewise.
	* g++.dg/parse/builtin2.C: Adjust.
	* g++.old-deja/g++.mike/p811.C: Likewise.
-------------- next part --------------
Index: c/c-decl.c
===================================================================
--- c/c-decl.c	(revision 253536)
+++ c/c-decl.c	(working copy)
@@ -1837,7 +1837,8 @@ diagnose_mismatched_decls (tree newdecl, tree oldd
 	  locate_old_decl (olddecl);
 	}
       else if (TREE_PUBLIC (newdecl))
-	warning (0, "built-in function %q+D declared as non-function",
+	warning (OPT_Wbuiltin_declaration_mismatch,
+		 "built-in function %q+D declared as non-function",
 		 newdecl);
       else
 	warning (OPT_Wshadow, "declaration of %q+D shadows "
Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 253536)
+++ cp/decl.c	(working copy)
@@ -1431,7 +1431,15 @@ duplicate_decls (tree newdecl, tree olddecl, bool
 	  /* Avoid warnings redeclaring built-ins which have not been
 	     explicitly declared.  */
 	  if (DECL_ANTICIPATED (olddecl))
-	    return NULL_TREE;
+	    {
+	      if (TREE_PUBLIC (newdecl)
+		  && CP_DECL_CONTEXT (newdecl) == global_namespace)
+		warning_at (DECL_SOURCE_LOCATION (newdecl),
+			    OPT_Wbuiltin_declaration_mismatch,
+			    "built-in function %qD declared as non-function",
+			    newdecl);
+	      return NULL_TREE;
+	    }
 
 	  /* If you declare a built-in or predefined function name as static,
 	     the old definition is overridden, but optionally warn this was a
@@ -1522,7 +1530,7 @@ next_arg:;
 
 	      warning_at (DECL_SOURCE_LOCATION (newdecl),
 			  OPT_Wbuiltin_declaration_mismatch,
-			  "declaration of %q+#D conflicts with built-in "
+			  "declaration of %q#D conflicts with built-in "
 			  "declaration %q#D", newdecl, olddecl);
 	    }
 	  else if ((DECL_EXTERN_C_P (newdecl)
Index: doc/invoke.texi
===================================================================
--- doc/invoke.texi	(revision 253536)
+++ doc/invoke.texi	(working copy)
@@ -6313,7 +6313,8 @@ 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.
+Warn if a built-in function is declared with the wrong signature or 
+as non-function.
 This warning is enabled by default.
 
 @item -Wno-builtin-macro-redefined
Index: testsuite/c-c++-common/Wbuiltin-declaration-mismatch-1.c
===================================================================
--- testsuite/c-c++-common/Wbuiltin-declaration-mismatch-1.c	(nonexistent)
+++ testsuite/c-c++-common/Wbuiltin-declaration-mismatch-1.c	(working copy)
@@ -0,0 +1,4 @@
+/* PR c++/82466 */
+/* { dg-options "-Wbuiltin-declaration-mismatch" } */
+
+int printf;  /* { dg-warning "declared as non-function" } */
Index: testsuite/c-c++-common/Wno-builtin-declaration-mismatch-1.c
===================================================================
--- testsuite/c-c++-common/Wno-builtin-declaration-mismatch-1.c	(nonexistent)
+++ testsuite/c-c++-common/Wno-builtin-declaration-mismatch-1.c	(working copy)
@@ -0,0 +1,4 @@
+/* PR c++/82466 */
+/* { dg-options "-Wno-builtin-declaration-mismatch" } */
+
+int printf;
Index: testsuite/g++.dg/parse/builtin2.C
===================================================================
--- testsuite/g++.dg/parse/builtin2.C	(revision 253536)
+++ testsuite/g++.dg/parse/builtin2.C	(working copy)
@@ -1,5 +1,5 @@
 // PR c++/14432
-// { dg-options "" }
+// { dg-options "-Wno-builtin-declaration-mismatch" }
 
 struct Y {}; 
 Y y1; 
Index: testsuite/g++.dg/warn/Wbuiltin_declaration_mismatch-1.C
===================================================================
--- testsuite/g++.dg/warn/Wbuiltin_declaration_mismatch-1.C	(nonexistent)
+++ testsuite/g++.dg/warn/Wbuiltin_declaration_mismatch-1.C	(working copy)
@@ -0,0 +1,7 @@
+// PR c++/82466
+// { dg-options "-Wbuiltin-declaration-mismatch" }
+
+namespace N
+{
+  int printf;
+}
Index: testsuite/g++.old-deja/g++.mike/p811.C
===================================================================
--- testsuite/g++.old-deja/g++.mike/p811.C	(revision 253536)
+++ testsuite/g++.old-deja/g++.mike/p811.C	(working copy)
@@ -1,5 +1,5 @@
 // { dg-do assemble  }
-// { dg-options "" }
+// { dg-options "-Wno-builtin-declaration-mismatch" }
 // This test case caused the compiler to abort at one point in time.
 // prms-id: 811
 


More information about the Gcc-patches mailing list