From 85617eba1e78793df6ea010ea1e16be6cc378ac9 Mon Sep 17 00:00:00 2001 From: Hans-Peter Nilsson Date: Mon, 28 Jul 2003 20:03:25 +0000 Subject: [PATCH] c-parse.in (lineno_stmt_decl_or_labels_ending_decl): Also warn when warn_declaration_after_statement. * c-parse.in (lineno_stmt_decl_or_labels_ending_decl): Also warn when warn_declaration_after_statement. Call pedwarn_c90, not pedwarn. Correct message: it's "ISO C90", not "ISO C89". * c-common.c (warn_declaration_after_statement): Define. * c-common.h (warn_declaration_after_statement): Declare. * c.opt (Wdeclaration-after-statement): New. * c-errors.c (pedwarn_c90): New function. * c-opts.c (c_common_handle_option) : New. * c-tree.h (pedwarn_c90): Declare. * doc/invoke.texi (Option Summary): Document -Wdeclaration-after-statement. (Warning Options): Ditto. Co-Authored-By: Michael Culbertson From-SVN: r69899 --- gcc/ChangeLog | 17 +++++++++++++++++ gcc/c-common.c | 4 ++++ gcc/c-common.h | 4 ++++ gcc/c-errors.c | 18 ++++++++++++++++++ gcc/c-opts.c | 4 ++++ gcc/c-parse.in | 7 +++++-- gcc/c-tree.h | 1 + gcc/c.opt | 4 ++++ gcc/doc/invoke.texi | 10 +++++++++- 9 files changed, 66 insertions(+), 3 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 372f834b9f5..b7ac766e232 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,20 @@ +2003-07-28 Hans-Peter Nilsson + Michael Culbertson + + * c-parse.in (lineno_stmt_decl_or_labels_ending_decl): Also warn + when warn_declaration_after_statement. Call pedwarn_c90, not + pedwarn. Correct message: it's "ISO C90", not "ISO C89". + * c-common.c (warn_declaration_after_statement): Define. + * c-common.h (warn_declaration_after_statement): Declare. + * c.opt (Wdeclaration-after-statement): New. + * c-errors.c (pedwarn_c90): New function. + * c-opts.c (c_common_handle_option) : New. + * c-tree.h (pedwarn_c90): Declare. + * doc/invoke.texi (Option Summary): Document + -Wdeclaration-after-statement. + (Warning Options): Ditto. + Mon Jul 28 20:13:06 CEST 2003 Jan Hubicka * i386.md (memory attribute) Avoid accessing uninitialized memory diff --git a/gcc/c-common.c b/gcc/c-common.c index 2e985c2a4d2..c579b16f37c 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -391,6 +391,10 @@ int warn_bad_function_cast; int warn_traditional; +/* Nonzero means warn for a declaration found after a statement. */ + +int warn_declaration_after_statement; + /* Nonzero means warn for non-prototype function decls or non-prototyped defs without previous prototype. */ diff --git a/gcc/c-common.h b/gcc/c-common.h index f46058f8b4e..5d4c3d42d5a 100644 --- a/gcc/c-common.h +++ b/gcc/c-common.h @@ -569,6 +569,10 @@ extern int warn_bad_function_cast; extern int warn_traditional; +/* Nonzero means warn for a declaration found after a statement. */ + +extern int warn_declaration_after_statement; + /* Nonzero means warn for non-prototype function decls or non-prototyped defs without previous prototype. */ diff --git a/gcc/c-errors.c b/gcc/c-errors.c index 2a2fdc0c440..fa71b8cfa97 100644 --- a/gcc/c-errors.c +++ b/gcc/c-errors.c @@ -43,3 +43,21 @@ pedwarn_c99 (const char *msgid, ...) report_diagnostic (&diagnostic); va_end (ap); } + +/* Issue an ISO C90 pedantic warning MSGID. This function is supposed to + be used for matters that are allowed in ISO C99 but not supported in + ISO C90, thus we explicitly don't pedwarn when C99 is specified. + (There is no flag_c90.) */ + +void +pedwarn_c90 (const char *msgid, ...) +{ + diagnostic_info diagnostic; + va_list ap; + + va_start (ap, msgid); + diagnostic_set_info (&diagnostic, msgid, &ap, input_location, + flag_isoc99 ? DK_WARNING : pedantic_error_kind ()); + report_diagnostic (&diagnostic); + va_end (ap); +} diff --git a/gcc/c-opts.c b/gcc/c-opts.c index a6dd9f0a5c8..5061a58e9f1 100644 --- a/gcc/c-opts.c +++ b/gcc/c-opts.c @@ -412,6 +412,10 @@ c_common_handle_option (size_t scode, const char *arg, int value) warn_ctor_dtor_privacy = value; break; + case OPT_Wdeclaration_after_statement: + warn_declaration_after_statement = value; + break; + case OPT_Wdeprecated: warn_deprecated = value; cpp_opts->warn_deprecated = value; diff --git a/gcc/c-parse.in b/gcc/c-parse.in index 8ac14120e6f..22b1531afa1 100644 --- a/gcc/c-parse.in +++ b/gcc/c-parse.in @@ -2013,8 +2013,11 @@ lineno_stmt_decl_or_labels_ending_stmt: lineno_stmt_decl_or_labels_ending_decl: lineno_decl | lineno_stmt_decl_or_labels_ending_stmt lineno_decl - { if (pedantic && !flag_isoc99) - pedwarn ("ISO C89 forbids mixed declarations and code"); } + { + if ((pedantic && !flag_isoc99) + || warn_declaration_after_statement) + pedwarn_c90 ("ISO C90 forbids mixed declarations and code"); + } | lineno_stmt_decl_or_labels_ending_decl lineno_decl | lineno_stmt_decl_or_labels_ending_error lineno_decl ; diff --git a/gcc/c-tree.h b/gcc/c-tree.h index 0a36609093a..cbc83a523a0 100644 --- a/gcc/c-tree.h +++ b/gcc/c-tree.h @@ -288,6 +288,7 @@ extern void set_init_index (tree, tree); extern void set_init_label (tree); extern void process_init_element (tree); extern tree build_compound_literal (tree, tree); +extern void pedwarn_c90 (const char *, ...) ATTRIBUTE_PRINTF_1; extern void pedwarn_c99 (const char *, ...) ATTRIBUTE_PRINTF_1; extern tree c_start_case (tree); extern void c_finish_case (void); diff --git a/gcc/c.opt b/gcc/c.opt index 042f4cfc86d..15b96ad5e82 100644 --- a/gcc/c.opt +++ b/gcc/c.opt @@ -180,6 +180,10 @@ Wctor-dtor-privacy C++ ObjC++ Warn when all constructors and destructors are private +Wdeclaration-after-statement +C ObjC +Warn when a declaration is found after a statement + Wdeprecated C++ ObjC++ Warn about deprecated compiler features diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 5393d108134..edd657d7599 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -233,7 +233,8 @@ in the following sections. @item C-only Warning Options @gccoptlist{-Wbad-function-cast -Wmissing-declarations @gol -Wmissing-prototypes -Wnested-externs @gol --Wstrict-prototypes -Wtraditional} +-Wstrict-prototypes -Wtraditional @gol +-Wdeclaration-after-statement} @item Debugging Options @xref{Debugging Options,,Options for Debugging Your Program or GCC}. @@ -2609,6 +2610,13 @@ because that feature is already a gcc extension and thus not relevant to traditional C compatibility. @end itemize +@item -Wdeclaration-after-statement @r{(C only)} +@opindex Wdeclaration-after-statement +Warn when a declaration is found after a statement in a block. This +construct, known from C++, was introduced with ISO C99 and is by default +allowed in GCC@. It is not supported by ISO C90 and was not supported by +GCC versions before GCC 3.0. @xref{Mixed Declarations}. + @item -Wundef @opindex Wundef Warn if an undefined identifier is evaluated in an @samp{#if} directive.