cpplib.h (struct cpp_options): Add new boolean flag warn_builtin_macro_redefined.

* include/cpplib.h (struct cpp_options): Add new boolean flag
	warn_builtin_macro_redefined.
	* init.c (cpp_create_reader): Initialize warn_builtin_macro_redefined.
	* (struct builtin_operator): Split out from previous struct builtin,
	enhance extra const correctness.
	* (struct builtin_macro): Split out from previous struct builtin, add
	new always_warn_if_redefined flag, enhance const correctness.
	* (mark_named_operators): Use struct builtin_operator.
	* (cpp_init_special_builtins): Use struct builtin_macro, add NODE_WARN
	to builtins selectively.
	* macro.c (warn_of_redefinition): Return false if a builtin macro
	is not flagged with NODE_WARN.

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

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

From-SVN: r140461
This commit is contained in:
Simon Baldwin 2008-09-18 15:39:08 +00:00 committed by Simon Baldwin
parent 1d29588628
commit c047ce9383
10 changed files with 169 additions and 25 deletions

View File

@ -1,3 +1,11 @@
2008-09-18 Simon Baldwin <simonb@google.com>
* c-opts.c (c_common_handle_option): Add handling for
-Wbuiltin-macro-redefined command line option.
* c.opt: Added builtin-macro-redefined option.
* doc/invoke.texi (Warning Options): Add -Wbuiltin-macro-redefined
documentation.
2008-09-18 Richard Guenther <rguenther@suse.de> 2008-09-18 Richard Guenther <rguenther@suse.de>
PR tree-optimization/37258 PR tree-optimization/37258

View File

@ -426,6 +426,10 @@ c_common_handle_option (size_t scode, const char *arg, int value)
warn_pointer_sign = 1; warn_pointer_sign = 1;
break; break;
case OPT_Wbuiltin_macro_redefined:
cpp_opts->warn_builtin_macro_redefined = value;
break;
case OPT_Wcomment: case OPT_Wcomment:
case OPT_Wcomments: case OPT_Wcomments:
cpp_opts->warn_comments = value; cpp_opts->warn_comments = value;

View File

@ -131,6 +131,10 @@ Wbad-function-cast
C ObjC Var(warn_bad_function_cast) Warning C ObjC Var(warn_bad_function_cast) Warning
Warn about casting functions to incompatible types Warn about casting functions to incompatible types
Wbuiltin-macro-redefined
C ObjC C++ ObjC++ Warning
Warn when a built-in preprocessor macro is undefined or redefined
Wc++-compat Wc++-compat
C ObjC Var(warn_cxx_compat) Warning C ObjC Var(warn_cxx_compat) Warning
Warn about C constructs that are not in the common subset of C and C++ Warn about C constructs that are not in the common subset of C and C++

View File

@ -228,7 +228,8 @@ Objective-C and Objective-C++ Dialects}.
@xref{Warning Options,,Options to Request or Suppress Warnings}. @xref{Warning Options,,Options to Request or Suppress Warnings}.
@gccoptlist{-fsyntax-only -pedantic -pedantic-errors @gol @gccoptlist{-fsyntax-only -pedantic -pedantic-errors @gol
-w -Wextra -Wall -Waddress -Waggregate-return -Warray-bounds @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 -Wchar-subscripts -Wclobbered -Wcomment @gol
-Wconversion -Wcoverage-mismatch -Wno-deprecated @gol -Wconversion -Wcoverage-mismatch -Wno-deprecated @gol
-Wno-deprecated-declarations -Wdisabled-optimization @gol -Wno-deprecated-declarations -Wdisabled-optimization @gol
@ -3731,6 +3732,13 @@ unrecognized attributes, function attributes applied to variables,
etc. This will not stop errors for incorrect use of supported etc. This will not stop errors for incorrect use of supported
attributes. attributes.
@item -Wno-builtin-macro-redefined
@opindex Wno-builtin-macro-redefined
@opindex Wbuiltin-macro-redefined
Do not warn if certain built-in macros are redefined. This suppresses
warnings for redefinition of @code{__TIMESTAMP__}, @code{__TIME__},
@code{__DATE__}, @code{__FILE__}, and @code{__BASE_FILE__}.
@item -Wstrict-prototypes @r{(C and Objective-C only)} @item -Wstrict-prototypes @r{(C and Objective-C only)}
@opindex Wstrict-prototypes @opindex Wstrict-prototypes
@opindex Wno-strict-prototypes @opindex Wno-strict-prototypes

View File

@ -1,3 +1,7 @@
2008-09-18 Simon Baldwin <simonb@google.com>
* gcc.dg/builtin-redefine.c: New.
2008-09-18 Richard Guenther <rguenther@suse.de> 2008-09-18 Richard Guenther <rguenther@suse.de>
PR tree-optimization/37258 PR tree-optimization/37258

View File

@ -0,0 +1,79 @@
/* Test -Wno-builtin-macro-redefined warnings. */
/* { dg-do compile } */
/* { dg-options "-Wno-builtin-macro-redefined -U__DATE__ -D__TIME__=X" } */
/* Check date, time, and datestamp built-ins warnings may be suppressed. */
#if defined(__DATE__)
#error "__DATE__ is defined, but should not be (-U command line error)"
/* { dg-bogus "__DATE__ is defined" "" { target *-*-* } 9 } */
#endif
#if __TIME__ != X
#error "__TIME__ is not defined as expected (-D command line error)"
/* { dg-bogus "__TIME__ is not defined" "" { target *-*-* } 14 } */
#endif
#if !defined(__TIMESTAMP__)
#error "__TIMESTAMP__ is not defined (built-in macro expectation error)"
/* { dg-bogus "__TIMESTAMP__ is not defined" "" { target *-*-* } 19 } */
#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 *-*-* } 28 } */
#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 *-*-* } 39 } */
#undef __DATE__ /* Undefine while defined. */
#define __TIMESTAMP__ "X" /* Define while already defined. */
#define __TIMESTAMP__ "X" /* Re-define while defined. */
#define __TIMESTAMP__ "Y" /* { dg-warning "\"__TIMESTAMP__\" redefined" } */
/* { dg-warning "previous definition" "" { target *-*-* } 48 } */
#undef __TIMESTAMP__ /* Undefine while defined. */
/* Check other built-ins with warnings that may be suppressed. */
#if !defined(__FILE__) || !defined(__BASE_FILE__)
#error "Expected built-in is not defined (built-in macro expectation error)"
/* { dg-bogus "Expected built-in is not defined" "" { target *-*-* } 59 } */
#endif
#define __FILE__ "X" /* Define while already defined. */
#define __BASE_FILE__ "X" /* Define while already defined. */
/* Check selected built-ins not affected by warning suppression. */
#if !defined(__LINE__) || !defined(__INCLUDE_LEVEL__) || !defined(__COUNTER__)
#error "Expected built-in is not defined (built-in macro expectation error)"
/* { dg-bogus "Expected built-in is not defined" "" { target *-*-* } 70 } */
#endif
#define __LINE__ 0 /* { dg-warning "\"__LINE__\" redef" } */
#define __INCLUDE_LEVEL__ 0 /* { dg-warning "\"__INCLUDE_LEVEL__\" redef" } */
#define __COUNTER__ 0 /* { dg-warning "\"__COUNTER__\" redef" } */
int unused; /* Silence `ISO C forbids an empty translation unit' warning. */

View File

@ -1,3 +1,18 @@
2008-09-18 Simon Baldwin <simonb@google.com>
* include/cpplib.h (struct cpp_options): Add new boolean flag
warn_builtin_macro_redefined.
* init.c (cpp_create_reader): Initialize warn_builtin_macro_redefined.
* (struct builtin_operator): Split out from previous struct builtin,
enhance extra const correctness.
* (struct builtin_macro): Split out from previous struct builtin, add
new always_warn_if_redefined flag, enhance const correctness.
* (mark_named_operators): Use struct builtin_operator.
* (cpp_init_special_builtins): Use struct builtin_macro, add NODE_WARN
to builtins selectively.
* macro.c (warn_of_redefinition): Return false if a builtin macro
is not flagged with NODE_WARN.
2008-07-31 Jakub Jelinek <jakub@redhat.com> 2008-07-31 Jakub Jelinek <jakub@redhat.com>
PR preprocessor/36649 PR preprocessor/36649

View File

@ -349,6 +349,10 @@ struct cpp_options
Presumably the usage is protected by the appropriate #ifdef. */ Presumably the usage is protected by the appropriate #ifdef. */
unsigned char warn_variadic_macros; 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. */ /* Nonzero means turn warnings into errors. */
unsigned char warnings_are_errors; unsigned char warnings_are_errors;

View File

@ -163,6 +163,7 @@ cpp_create_reader (enum c_lang lang, hash_table *table,
CPP_OPTION (pfile, dollars_in_ident) = 1; CPP_OPTION (pfile, dollars_in_ident) = 1;
CPP_OPTION (pfile, warn_dollars) = 1; CPP_OPTION (pfile, warn_dollars) = 1;
CPP_OPTION (pfile, warn_variadic_macros) = 1; CPP_OPTION (pfile, warn_variadic_macros) = 1;
CPP_OPTION (pfile, warn_builtin_macro_redefined) = 1;
CPP_OPTION (pfile, warn_normalize) = normalized_C; CPP_OPTION (pfile, warn_normalize) = normalized_C;
/* Default CPP arithmetic to something sensible for the host for the /* Default CPP arithmetic to something sensible for the host for the
@ -303,31 +304,41 @@ cpp_destroy (cpp_reader *pfile)
altered through #define, and #if recognizes them as operators. In altered through #define, and #if recognizes them as operators. In
C, these are not entered into the hash table at all (but see C, these are not entered into the hash table at all (but see
<iso646.h>). The value is a token-type enumerator. */ <iso646.h>). The value is a token-type enumerator. */
struct builtin struct builtin_macro
{ {
const uchar *name; const uchar *const name;
unsigned short len; const unsigned short len;
unsigned short value; const unsigned short value;
const bool always_warn_if_redefined;
};
#define B(n, t, f) { DSC(n), t, f }
static const struct builtin_macro builtin_array[] =
{
B("__TIMESTAMP__", BT_TIMESTAMP, false),
B("__TIME__", BT_TIME, false),
B("__DATE__", BT_DATE, false),
B("__FILE__", BT_FILE, false),
B("__BASE_FILE__", BT_BASE_FILE, false),
B("__LINE__", BT_SPECLINE, true),
B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL, true),
B("__COUNTER__", BT_COUNTER, true),
/* Keep builtins not used for -traditional-cpp at the end, and
update init_builtins() if any more are added. */
B("_Pragma", BT_PRAGMA, true),
B("__STDC__", BT_STDC, true),
};
#undef B
struct builtin_operator
{
const uchar *const name;
const unsigned short len;
const unsigned short value;
}; };
#define B(n, t) { DSC(n), t } #define B(n, t) { DSC(n), t }
static const struct builtin builtin_array[] = static const struct builtin_operator operator_array[] =
{
B("__TIMESTAMP__", BT_TIMESTAMP),
B("__TIME__", BT_TIME),
B("__DATE__", BT_DATE),
B("__FILE__", BT_FILE),
B("__BASE_FILE__", BT_BASE_FILE),
B("__LINE__", BT_SPECLINE),
B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
B("__COUNTER__", BT_COUNTER),
/* Keep builtins not used for -traditional-cpp at the end, and
update init_builtins() if any more are added. */
B("_Pragma", BT_PRAGMA),
B("__STDC__", BT_STDC),
};
static const struct builtin operator_array[] =
{ {
B("and", CPP_AND_AND), B("and", CPP_AND_AND),
B("and_eq", CPP_AND_EQ), B("and_eq", CPP_AND_EQ),
@ -347,7 +358,7 @@ static const struct builtin operator_array[] =
static void static void
mark_named_operators (cpp_reader *pfile) mark_named_operators (cpp_reader *pfile)
{ {
const struct builtin *b; const struct builtin_operator *b;
for (b = operator_array; for (b = operator_array;
b < (operator_array + ARRAY_SIZE (operator_array)); b < (operator_array + ARRAY_SIZE (operator_array));
@ -363,7 +374,7 @@ mark_named_operators (cpp_reader *pfile)
void void
cpp_init_special_builtins (cpp_reader *pfile) cpp_init_special_builtins (cpp_reader *pfile)
{ {
const struct builtin *b; const struct builtin_macro *b;
size_t n = ARRAY_SIZE (builtin_array); size_t n = ARRAY_SIZE (builtin_array);
if (CPP_OPTION (pfile, traditional)) if (CPP_OPTION (pfile, traditional))
@ -376,7 +387,10 @@ cpp_init_special_builtins (cpp_reader *pfile)
{ {
cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len); cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
hp->type = NT_MACRO; hp->type = NT_MACRO;
hp->flags |= NODE_BUILTIN | NODE_WARN; hp->flags |= NODE_BUILTIN;
if (b->always_warn_if_redefined
|| CPP_OPTION (pfile, warn_builtin_macro_redefined))
hp->flags |= NODE_WARN;
hp->value.builtin = (enum builtin_type) b->value; hp->value.builtin = (enum builtin_type) b->value;
} }
} }

View File

@ -1392,6 +1392,10 @@ warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
if (node->flags & NODE_WARN) if (node->flags & NODE_WARN)
return true; 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 /* Redefinitions of conditional (context-sensitive) macros, on
the other hand, must be allowed silently. */ the other hand, must be allowed silently. */
if (node->flags & NODE_CONDITIONAL) if (node->flags & NODE_CONDITIONAL)