This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH][RFC] -Wno-... option to suppress builtin macro redefined warnings


This patch adds a warning suppression flag, -Wno-builtin-macro-redefined,
to silence gcc warnings where builtin macros such as __TIME__ are undefined
or redefined, either on the command line or by directives.

This change permits a tightly controlled build system, one that uses
'-Werror', to redefine __TIME__, __DATE__, __TIMESTAMP__ and so on without
raising compilation errors.  As a result, such a system can generate object
files from code, containing date and time macros, that will be bitwise equal
to object files built from the same code, no matter when compiled.

Once redefined, a builtin macro is no longer builtin, and so becomes subject
to the usual warnings for redefinition (for example, if set to X, it may
only be reset to X without warning if not undefined explicitly first).

Tested and confirmed for C and C++ with testsuite and bootstrap.

Thoughts, comments, suggestions?  Aimed at trunk.  Thanks.


libcpp/ChangeLog:
2008-07-30  Simon Baldwin  <simonb@google.com>

	* include/cpplib.h (struct cpp_options): Added new boolean flag
	warn_builtin_macro_redefined.
	* init.c (cpp_create_reader): Initialize warn_builtin_macro_redefined.
	* (cpp_init_special_builtins): Add NODE_WARN to builtins only if
	warn_builtin_macro_redefined.
	* macro.c (warn_of_redefinition): Return false if a builtin macro
	is not flagged with NODE_WARN.

gcc/ChangeLog:
2008-07-30  Simon Baldwin  <simonb@google.com>

	* c-opts.c (c_common_handle_option): Added handling for
	-Wbuiltin-macro-redefined command line option.
	* c.opt: Added builtin-macro-redefined option.
	* doc/invoke.texi (Warning Options): Added -Wbuiltin-macro-redefined
	documentation.

gcc/testsuite/ChangeLog:
2008-07-30  Simon Baldwin  <simonb@google.com>

	* gcc.dg/builtin-redefine.c: New.


Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 138245)
+++ gcc/doc/invoke.texi	(working copy)
@@ -228,7 +228,8 @@ Objective-C and Objective-C++ Dialects}.
 @xref{Warning Options,,Options to Request or Suppress Warnings}.
 @gccoptlist{-fsyntax-only  -pedantic  -pedantic-errors @gol
 -w  -Wextra  -Wall  -Waddress  -Waggregate-return  -Warray-bounds @gol
--Wno-attributes -Wc++-compat -Wc++0x-compat -Wcast-align  -Wcast-qual  @gol
+-Wno-attributes -Wno-builtin-macro-redefined @gol
+-Wc++-compat -Wc++0x-compat -Wcast-align  -Wcast-qual  @gol
 -Wchar-subscripts -Wclobbered  -Wcomment @gol
 -Wconversion  -Wcoverage-mismatch  -Wno-deprecated  @gol
 -Wno-deprecated-declarations -Wdisabled-optimization  @gol
@@ -3720,6 +3721,11 @@ unrecognized attributes, function attrib
 etc.  This will not stop errors for incorrect use of supported
 attributes.
 
+@item -Wno-builtin-macro-redefined
+@opindex Wno-builtin-macro-redefined
+@opindex Wbuiltin-macro-redefined
+Do not warn if a builtin macro, such as @code{__TIME__}, is redefined.
+
 @item -Wstrict-prototypes @r{(C and Objective-C only)}
 @opindex Wstrict-prototypes
 @opindex Wno-strict-prototypes
Index: gcc/testsuite/gcc.dg/builtin-redefine.c
===================================================================
--- gcc/testsuite/gcc.dg/builtin-redefine.c	(revision 0)
+++ gcc/testsuite/gcc.dg/builtin-redefine.c	(revision 0)
@@ -0,0 +1,51 @@
+/* Test -Wno-builtin-macro-redefined warnings.  */
+
+/* { dg-do compile } */
+/* { dg-options "-Wno-builtin-macro-redefined -U__DATE__ -D__TIME__=X" } */
+
+#if defined(__DATE__)
+#error "-U error: __DATE__ is defined, but should not be"
+#endif
+
+#if __TIME__ != X
+#error "-D error: __TIME__ is not defined as expected"
+#endif
+
+#if !defined(__TIMESTAMP__)
+#error "Builtin macro error: __TIMESTAMP__ is not defined"
+#endif
+
+
+#undef __TIME__              /* Undefine while defined.  */
+#undef __TIME__              /* Undefine while already undefined.  */
+
+#define __TIME__ "X"         /* Define while undefined.  */
+#define __TIME__ "X"         /* Re-define while defined.  */
+
+#define __TIME__ "Y"         /* { dg-warning "\"__TIME__\" redefined" } */
+/* { dg-warning "previous definition" "" { target *-*-* } 23 } */
+
+#undef __TIME__              /* Undefine while defined.  */
+
+
+#undef __DATE__              /* Undefine while already undefined.  */
+
+#define __DATE__ "X"         /* Define while undefined.  */
+#define __DATE__ "X"         /* Re-define while defined.  */
+
+#define __DATE__ "Y"         /* { dg-warning "\"__DATE__\" redefined" } */
+/* { dg-warning "previous definition" "" { target *-*-* } 34 } */
+
+#undef __DATE__              /* Undefine while defined.  */
+
+
+#define __TIMESTAMP__ "X"    /* Define while undefined.  */
+#define __TIMESTAMP__ "X"    /* Re-define while defined.  */
+
+#define __TIMESTAMP__ "Y"    /* { dg-warning "\"__TIMESTAMP__\" redefined" } */
+/* { dg-warning "previous definition" "" { target *-*-* } 43 } */
+
+#undef __TIMESTAMP__         /* Undefine while defined.  */
+
+
+int unused;  /* Silence `ISO C forbids an empty translation unit' warning.  */
Index: gcc/c.opt
===================================================================
--- gcc/c.opt	(revision 138245)
+++ gcc/c.opt	(working copy)
@@ -131,6 +131,10 @@ Wbad-function-cast
 C ObjC Var(warn_bad_function_cast) Warning
 Warn about casting functions to incompatible types
 
+Wbuiltin-macro-redefined
+C ObjC C++ ObjC++ Warning
+Warn when a builtin preprocessor macro is undefined or redefined
+
 Wc++-compat
 C ObjC Var(warn_cxx_compat) Warning
 Warn about C constructs that are not in the common subset of C and C++
Index: gcc/c-opts.c
===================================================================
--- gcc/c-opts.c	(revision 138245)
+++ gcc/c-opts.c	(working copy)
@@ -423,6 +423,10 @@ c_common_handle_option (size_t scode, co
 	warn_pointer_sign = 1;
       break;
 
+    case OPT_Wbuiltin_macro_redefined:
+      cpp_opts->warn_builtin_macro_redefined = value;
+      break;
+
     case OPT_Wcomment:
     case OPT_Wcomments:
       cpp_opts->warn_comments = value;
Index: libcpp/macro.c
===================================================================
--- libcpp/macro.c	(revision 138245)
+++ libcpp/macro.c	(working copy)
@@ -1392,6 +1392,10 @@ warn_of_redefinition (cpp_reader *pfile,
   if (node->flags & NODE_WARN)
     return true;
 
+  /* Suppress warnings for builtins that lack the NODE_WARN flag.  */
+  if (node->flags & NODE_BUILTIN)
+    return false;
+
   /* Redefinitions of conditional (context-sensitive) macros, on
      the other hand, must be allowed silently.  */
   if (node->flags & NODE_CONDITIONAL)
Index: libcpp/include/cpplib.h
===================================================================
--- libcpp/include/cpplib.h	(revision 138245)
+++ libcpp/include/cpplib.h	(working copy)
@@ -349,6 +349,10 @@ struct cpp_options
      Presumably the usage is protected by the appropriate #ifdef.  */
   unsigned char warn_variadic_macros;
 
+  /* Nonzero means warn about builtin macros that are redefined or
+     explicitly undefined.  */
+  unsigned char warn_builtin_macro_redefined;
+
   /* Nonzero means turn warnings into errors.  */
   unsigned char warnings_are_errors;
 
Index: libcpp/init.c
===================================================================
--- libcpp/init.c	(revision 138245)
+++ libcpp/init.c	(working copy)
@@ -163,6 +163,7 @@ cpp_create_reader (enum c_lang lang, has
   CPP_OPTION (pfile, dollars_in_ident) = 1;
   CPP_OPTION (pfile, warn_dollars) = 1;
   CPP_OPTION (pfile, warn_variadic_macros) = 1;
+  CPP_OPTION (pfile, warn_builtin_macro_redefined) = 1;
   CPP_OPTION (pfile, warn_normalize) = normalized_C;
 
   /* Default CPP arithmetic to something sensible for the host for the
@@ -376,7 +377,9 @@ cpp_init_special_builtins (cpp_reader *p
     {
       cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
       hp->type = NT_MACRO;
-      hp->flags |= NODE_BUILTIN | NODE_WARN;
+      hp->flags |= NODE_BUILTIN;
+      if (CPP_OPTION (pfile, warn_builtin_macro_redefined))
+	hp->flags |= NODE_WARN;
       hp->value.builtin = (enum builtin_type) b->value;
     }
 }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]