diff options
author | Neil Booth <neil@daikokuya.co.uk> | 2002-07-23 22:57:49 +0000 |
---|---|---|
committer | Neil Booth <neil@gcc.gnu.org> | 2002-07-23 22:57:49 +0000 |
commit | a69cbaac60d73feb37e6c17abd6f84991dc791a0 (patch) | |
tree | d7305615fe0d53e288982b583d2c9c637deb2699 /gcc/cppmacro.c | |
parent | b841421a28b7e7c616e58cae4b5e36599f288b7f (diff) | |
download | gcc-a69cbaac60d73feb37e6c17abd6f84991dc791a0.zip gcc-a69cbaac60d73feb37e6c17abd6f84991dc791a0.tar.gz gcc-a69cbaac60d73feb37e6c17abd6f84991dc791a0.tar.bz2 |
cppexp.c (parse_defined): Mark macro used.
* cppexp.c (parse_defined): Mark macro used.
* cpphash.h (struct cpp_macro): New member "used".
(_cpp_mark_macro_used, _cpp_warn_if_unused_macro): New.
(struct cpp_reader): New member.
* cppinit.c (cpp_finish_options): Set first_unused_line.
(cpp_finish): Warn of unused macros if requested.
(OPT_TABLE): New switches.
(cpp_handle_option): Handle them.
* cpplib.c (do_undef): Warn if macro unused.
(do_ifdef, do_ifndef): Mark macro used.
* cpplib.h (struct cpp_options): New member.
* cppmacro.c (_cpp_warn_if_unused_macro): New.
(enter_macro_context): Mark macro used.
(_cpp_create_definition): Mark macro unused; warn if unused
when redefined.
* cpptrad.c (scan_out_logcial_line, push_replacement_text):
Mark macros used.
* doc/cppopts.texi: Update.
testsuite:
* gcc.dg/cpp/trad/Wunused.c, gcc.dg/cpp/trad/Wunused.h,
gcc.dg/cpp/Wunused.c, gcc.dg/cpp/Wunused.h: New tests.
From-SVN: r55692
Diffstat (limited to 'gcc/cppmacro.c')
-rw-r--r-- | gcc/cppmacro.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/gcc/cppmacro.c b/gcc/cppmacro.c index 4d807a2..c8e2410 100644 --- a/gcc/cppmacro.c +++ b/gcc/cppmacro.c @@ -74,6 +74,29 @@ static void check_trad_stringification PARAMS ((cpp_reader *, const cpp_macro *, const cpp_string *)); +/* Emits a warning if NODE is a macro defined in the main file that + has not been used. */ +int +_cpp_warn_if_unused_macro (pfile, node, v) + cpp_reader *pfile; + cpp_hashnode *node; + void *v ATTRIBUTE_UNUSED; +{ + if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) + { + cpp_macro *macro = node->value.macro; + + if (!macro->used + /* Skip front-end built-ins and command line macros. */ + && macro->line >= pfile->first_unused_line + && MAIN_FILE_P (lookup_line (&pfile->line_maps, macro->line))) + cpp_error_with_line (pfile, DL_WARNING, macro->line, 0, + "macro \"%s\" is not used", NODE_NAME (node)); + } + + return 1; +} + /* Allocates and returns a CPP_STRING token, containing TEXT of length LEN, after null-terminating it. TEXT must be in permanent storage. */ static const cpp_token * @@ -728,6 +751,8 @@ enter_macro_context (pfile, node) /* Disable the macro within its expansion. */ node->flags |= NODE_DISABLED; + macro->used = 1; + if (macro->paramc == 0) push_token_context (pfile, node, macro->exp.tokens, macro->count); @@ -1488,6 +1513,7 @@ _cpp_create_definition (pfile, node) macro->params = 0; macro->paramc = 0; macro->variadic = 0; + macro->used = 0; macro->count = 0; macro->fun_like = 0; /* To suppress some diagnostics. */ @@ -1523,6 +1549,9 @@ _cpp_create_definition (pfile, node) if (node->type != NT_VOID) { + if (CPP_OPTION (pfile, warn_unused_macros)) + _cpp_warn_if_unused_macro (pfile, node, NULL); + if (warn_of_redefinition (pfile, node, macro)) { cpp_error_with_line (pfile, DL_PEDWARN, pfile->directive_line, 0, |