diff options
author | Simon Baldwin <simonb@google.com> | 2008-09-18 15:39:08 +0000 |
---|---|---|
committer | Simon Baldwin <simonb@gcc.gnu.org> | 2008-09-18 15:39:08 +0000 |
commit | c047ce93830ab288948c3e225fc8c5efdfbe3c2a (patch) | |
tree | e6725c527874d936bd2e164a6a7f885ebaffaf47 /libcpp | |
parent | 1d295886287812ee7e5b5bfc63ff598a2a9e9958 (diff) | |
download | gcc-c047ce93830ab288948c3e225fc8c5efdfbe3c2a.zip gcc-c047ce93830ab288948c3e225fc8c5efdfbe3c2a.tar.gz gcc-c047ce93830ab288948c3e225fc8c5efdfbe3c2a.tar.bz2 |
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
Diffstat (limited to 'libcpp')
-rw-r--r-- | libcpp/ChangeLog | 15 | ||||
-rw-r--r-- | libcpp/include/cpplib.h | 4 | ||||
-rw-r--r-- | libcpp/init.c | 54 | ||||
-rw-r--r-- | libcpp/macro.c | 4 |
4 files changed, 57 insertions, 20 deletions
diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog index 49efadc..39be989 100644 --- a/libcpp/ChangeLog +++ b/libcpp/ChangeLog @@ -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> PR preprocessor/36649 diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index a79c26d..4f073f9 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -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; diff --git a/libcpp/init.c b/libcpp/init.c index 040bf2a..0db167c 100644 --- a/libcpp/init.c +++ b/libcpp/init.c @@ -163,6 +163,7 @@ cpp_create_reader (enum c_lang lang, hash_table *table, 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 @@ -303,31 +304,41 @@ cpp_destroy (cpp_reader *pfile) altered through #define, and #if recognizes them as operators. In C, these are not entered into the hash table at all (but see <iso646.h>). The value is a token-type enumerator. */ -struct builtin +struct builtin_macro { - const uchar *name; - unsigned short len; - unsigned short value; + const uchar *const name; + const unsigned short len; + const unsigned short value; + const bool always_warn_if_redefined; }; -#define B(n, t) { DSC(n), t } -static const struct builtin builtin_array[] = +#define B(n, t, f) { DSC(n), t, f } +static const struct builtin_macro builtin_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), + 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), - B("__STDC__", BT_STDC), + 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; }; -static const struct builtin operator_array[] = +#define B(n, t) { DSC(n), t } +static const struct builtin_operator operator_array[] = { B("and", CPP_AND_AND), B("and_eq", CPP_AND_EQ), @@ -347,7 +358,7 @@ static const struct builtin operator_array[] = static void mark_named_operators (cpp_reader *pfile) { - const struct builtin *b; + const struct builtin_operator *b; for (b = operator_array; b < (operator_array + ARRAY_SIZE (operator_array)); @@ -363,7 +374,7 @@ mark_named_operators (cpp_reader *pfile) void cpp_init_special_builtins (cpp_reader *pfile) { - const struct builtin *b; + const struct builtin_macro *b; size_t n = ARRAY_SIZE (builtin_array); 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); 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; } } diff --git a/libcpp/macro.c b/libcpp/macro.c index 9a470ef..8122648 100644 --- a/libcpp/macro.c +++ b/libcpp/macro.c @@ -1392,6 +1392,10 @@ warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node, 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) |