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] cpplib: -Wno-endif-labels



As required by ISO C, cpplib warns about the second and third 'foo' in

    #if foo
    ...
    #else foo
    ...
    #endif foo

Some time ago Zack mentioned that an option to suppress the warning might
be useful.  I agree; the itch I'm scratching is a bunch of really old code
that we have to keep kicking along, and is riddled with things like this.

So, first pass:


gcc:
2002-03-21  Phil Edwards  <pme@gcc.gnu.org>

	* cpplib.h (struct cpp_options):  New member, warn_endif_labels.
	* cppinit.c (cpp_create_reader):  On by default.
	(cpp_handle_option):  Handle -W[no-]endif-labels.
	* cpplib.c (do_else):  Use it.
	(do_endif):  Likewise.
	* doc/cppopts.texi:  Document new option.
	* doc/invoke.texi:  Document new option.

gcc/testsuite:
2002-03-21  Phil Edwards  <pme@gcc.gnu.org>

	* gcc.dg/cpp/extratokens2.c:  New file.


Index: cppinit.c
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/gcc/cppinit.c,v
retrieving revision 1.206
diff -u -3 -p -r1.206 cppinit.c
--- cppinit.c	16 Mar 2002 14:03:36 -0000	1.206
+++ cppinit.c	21 Mar 2002 19:38:25 -0000
@@ -491,6 +491,7 @@ cpp_create_reader (lang)
   CPP_OPTION (pfile, show_column) = 1;
   CPP_OPTION (pfile, tabstop) = 8;
   CPP_OPTION (pfile, operator_names) = 1;
+  CPP_OPTION (pfile, warn_endif_labels) = 1;
 #if DEFAULT_SIGNED_CHAR
   CPP_OPTION (pfile, signed_char) = 1;
 #else
@@ -1735,6 +1736,8 @@ cpp_handle_option (pfile, argc, argv, ig
 	    CPP_OPTION (pfile, warnings_are_errors) = 1;
 	  else if (!strcmp (argv[i], "-Wsystem-headers"))
 	    CPP_OPTION (pfile, warn_system_headers) = 1;
+	  else if (!strcmp (argv[i], "-Wendif-labels"))
+	    CPP_OPTION (pfile, warn_endif_labels) = 1;
 	  else if (!strcmp (argv[i], "-Wno-traditional"))
 	    CPP_OPTION (pfile, warn_traditional) = 0;
 	  else if (!strcmp (argv[i], "-Wno-trigraphs"))
@@ -1751,6 +1754,8 @@ cpp_handle_option (pfile, argc, argv, ig
 	    CPP_OPTION (pfile, warnings_are_errors) = 0;
 	  else if (!strcmp (argv[i], "-Wno-system-headers"))
 	    CPP_OPTION (pfile, warn_system_headers) = 0;
+	  else if (!strcmp (argv[i], "-Wno-endif-labels"))
+	    CPP_OPTION (pfile, warn_endif_labels) = 0;
 	  else if (! ignore)
 	    return i;
 	  break;
Index: cpplib.c
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/gcc/cpplib.c,v
retrieving revision 1.293
diff -u -3 -p -r1.293 cpplib.c
--- cpplib.c	14 Mar 2002 18:17:11 -0000	1.293
+++ cpplib.c	21 Mar 2002 19:38:25 -0000
@@ -1375,7 +1375,7 @@ do_else (pfile)
       ifs->mi_cmacro = 0;
 
       /* Only check EOL if was not originally skipping.  */
-      if (!ifs->was_skipping)
+      if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
 	check_eol (pfile);
     }
 }
@@ -1430,7 +1430,7 @@ do_endif (pfile)
   else
     {
       /* Only check EOL if was not originally skipping.  */
-      if (!ifs->was_skipping)
+      if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
 	check_eol (pfile);
 
       /* If potential control macro, we go back outside again.  */
Index: cpplib.h
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/gcc/cpplib.h,v
retrieving revision 1.203
diff -u -3 -p -r1.203 cpplib.h
--- cpplib.h	14 Mar 2002 18:17:12 -0000	1.203
+++ cpplib.h	21 Mar 2002 19:38:25 -0000
@@ -308,6 +308,9 @@ struct cpp_options
      traditional C.  */
   unsigned char warn_traditional;
 
+  /* Nonzero means warn about text after an #endif (or #else).  */
+  unsigned char warn_endif_labels;
+
   /* Nonzero means turn warnings into errors.  */
   unsigned char warnings_are_errors;
 
Index: doc/cppopts.texi
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/gcc/doc/cppopts.texi,v
retrieving revision 1.2
diff -u -3 -p -r1.2 cppopts.texi
--- doc/cppopts.texi	11 Mar 2002 21:11:36 -0000	1.2
+++ doc/cppopts.texi	21 Mar 2002 19:38:25 -0000
@@ -110,6 +110,23 @@ Warn whenever an identifier which is not
 @samp{#if} directive, outside of @samp{defined}.  Such identifiers are
 replaced with zero.
 
+@item -Wendif-labels
+@opindex Wendif-labels
+Warn whenever an @samp{#else} or an @samp{#endif} are followed by text.
+This usually happens in code of the form
+
+@smallexample
+#if FOO
+@dots{}
+#else FOO
+@dots{}
+#endif FOO
+@end smallexample
+
+@noindent
+The second and third @code{FOO} should be in comments, but often are not
+in older programs.  This warning is on by default.
+
 @item -Werror
 @opindex Werror
 Make all warnings into hard errors.  Source code which triggers warnings
Index: doc/invoke.texi
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.130
diff -u -3 -p -r1.130 invoke.texi
--- doc/invoke.texi	20 Mar 2002 22:56:33 -0000	1.130
+++ doc/invoke.texi	21 Mar 2002 19:38:25 -0000
@@ -220,7 +220,7 @@ in the following sections.
 -Wimplicit  -Wimplicit-int  @gol
 -Wimplicit-function-declaration @gol
 -Werror-implicit-function-declaration @gol
--Wimport  -Winline @gol
+-Wimport  -Winline -Wno-endef-labels @gol
 -Wlarger-than-@var{len}  -Wlong-long @gol
 -Wmain  -Wmissing-braces  -Wmissing-declarations @gol
 -Wmissing-format-attribute  -Wmissing-noreturn @gol
@@ -2383,6 +2383,10 @@ conversion warnings, for the full set us
 @item -Wundef
 @opindex Wundef
 Warn if an undefined identifier is evaluated in an @samp{#if} directive.
+
+@item -Wendif-labels
+@opindex Wendif-labels
+Warn whenever an @samp{#else} or an @samp{#endif} are followed by text.
 
 @item -Wshadow
 @opindex Wshadow
Index: testsuite/gcc.dg/cpp/extratokens2.c
===================================================================
RCS file: testsuite/gcc.dg/cpp/extratokens2.c
diff -N testsuite/gcc.dg/cpp/extratokens2.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/gcc.dg/cpp/extratokens2.c	21 Mar 2002 19:38:25 -0000
@@ -0,0 +1,22 @@
+/* Copyright (C) 2002 Free Software Foundation, Inc.  */
+
+/* { dg-do preprocess } */
+/* { dg-options "-fno-show-column -Wno-endif-labels" } */
+
+/* Tests that -Wno-endif-labels correctly disables the checks done by
+   default (and tested in extratokens.c).  */
+
+/* Source: Phil Edwards, 21 Mar 2002.  Copied from extratokens.c and
+   modified.  */
+
+#if 1 
+#if 0
+#else foo	/* { dg-bogus "extra tokens" "bad warning" } */
+#endif /	/* { dg-bogus "extra tokens" "bad warning" } */
+#endif
+
+# 36 "file.c" 3
+
+/* ... but in a system header, it's acceptable.  */
+#ifdef KERNEL
+#endif KERNEL  /* { dg-bogus "extra tokens" "bad warning" } */


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