public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Manuel López-Ibáñez" <lopezibanez@gmail.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Wordered-pointer-comparison (PR7651 Define -Wextra strictly in terms of other warning flags)
Date: Mon, 08 Jan 2007 21:21:00 -0000	[thread overview]
Message-ID: <6c33472e0701081321l748d60e0i19b1d6e2379e94ee@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1419 bytes --]

:ADDPATCH c:

This patch continues the effort to fix PR7651 [1].

A new option -Wordered-pointer-comparison takes over the pedantic
warning for a pointer  compared against integer zero with <, <=, >, or
>=.  The new
option is enabled by -Wextra and by -pedantic, so we keep the current
behaviour but add the ability to enable/disable this individual
warning. The patch also fixes the issue that -Wextra would warn for p
< 0 but not for 0 > p.

Suggestions for an apter name are always welcome.

Bootstrapped and regression tested with --enable-languages=all on
i686-pc-linux-gnu.

OK for mainline?

[1] http://gcc.gnu.org/ml/gcc-patches/2006-12/msg01103.html

2007-01-07  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>

  PR middle-end/7651
  * c.opt (Wordered-pointer-comparison): New.
  * doc/invoke.texi (Wordered-pointer-comparison): Document it.
  (Wextra): Enabled by -Wextra.
  (C-only Warning Options): Add it.
  * c-opts.c (c_common_handle_option): Enabled by -pedantic.
  (c_common_post_options): Enabled by -Wextra.
  * c-typeck.c (build_binary_op): Replace pedantic and extra_warnings
by warn_ordered_pointer_comparison.

testsuite/
2007-01-07  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>

  PR middle-end/7651
  * gcc.dg/Wordered-pointer-comparison.c: New.
  * gcc.dg/Wordered-pointer-comparison-Wextra.c: New.
  * gcc.dg/Wordered-pointer-comparison-pedantic.c: New.
  * gcc.dg/Wordered-pointer-comparison-no.c: New.

[-- Attachment #2: wordered-pointer-comparison.diff --]
[-- Type: text/plain, Size: 9736 bytes --]

Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 120511)
+++ gcc/doc/invoke.texi	(working copy)
@@ -258,7 +258,7 @@ Objective-C and Objective-C++ Dialects}.
 @item C-only Warning Options
 @gccoptlist{-Wbad-function-cast  -Wmissing-declarations @gol
 -Wmissing-parameter-type  -Wmissing-prototypes  -Wnested-externs @gol
--Wold-style-declaration  -Wold-style-definition @gol
+-Wold-style-declaration  -Wold-style-definition   -Wordered-pointer-comparison @gol
 -Wstrict-prototypes  -Wtraditional  -Wtraditional-conversion @gol
 -Wdeclaration-after-statement -Wpointer-sign}
 
@@ -2921,9 +2921,10 @@ functions.  This warning can be independ
 An empty body occurs in an @samp{if} or @samp{else} statement. This
 warning can be independently controlled by @option{-Wempty-body}.
 
-@item
+@item @r{(C only)}
 A pointer is compared against integer zero with @samp{<}, @samp{<=},
-@samp{>}, or @samp{>=}.
+@samp{>}, or @samp{>=}.  This warning can be independently controlled
+by @option{-Wordered-pointer-comparison}.
 
 @item
 A variable might be changed by @samp{longjmp} or @samp{vfork}.
@@ -3180,6 +3181,12 @@ changed by the conversion like in @code{
 An empty body occurs in an @samp{if} or @samp{else} statement. 
 This warning is also enabled by @option{-Wextra}.
 
+@item -Wordered-pointer-comparison @r{(C only)}
+@opindex Wordered-pointer-comparison
+Warn for a pointer compared against integer zero with @samp{<},
+@samp{<=}, @samp{>}, or @samp{>=}.  This warning is also enabled by
+@option{-Wextra} and @option{-pedantic}.
+
 @item -Wsign-compare
 @opindex Wsign-compare
 @cindex warning for comparison of signed and unsigned values
Index: gcc/testsuite/gcc.dg/Wordered-pointer-comparison-no.c
===================================================================
--- gcc/testsuite/gcc.dg/Wordered-pointer-comparison-no.c	(revision 0)
+++ gcc/testsuite/gcc.dg/Wordered-pointer-comparison-no.c	(revision 0)
@@ -0,0 +1,23 @@
+/* Test disabling -Wordered-pointer-comparison */
+/* { dg-do compile } */
+/* { dg-options "-pedantic -Wextra -Wno-ordered-pointer-comparison" } */
+
+void h(int *p)
+{
+    if (p < 0) return;/* { dg-bogus "ordered comparison of pointer with integer zero" } */
+    if (p <= 0) return;/* { dg-bogus "ordered comparison of pointer with integer zero" } */
+    if (p > 0) return;/* { dg-bogus "ordered comparison of pointer with integer zero" } */
+    if (p >= 0) return;/* { dg-bogus "ordered comparison of pointer with integer zero" } */
+    if (p == 0) return;
+    if (p != 0) return;
+}
+
+void j(int *p)
+{
+    if (0 < p) return;/* { dg-bogus "ordered comparison of pointer with integer zero" } */
+    if (0 <= p) return;/* { dg-bogus "ordered comparison of pointer with integer zero" } */
+    if (0 > p) return;/* { dg-bogus "ordered comparison of pointer with integer zero" } */
+    if (0 >= p) return;/* { dg-bogus "ordered comparison of pointer with integer zero" } */
+    if (0 == p) return;
+    if (0 != p) return;
+}
Index: gcc/testsuite/gcc.dg/Wordered-pointer-comparison-pedantic.c
===================================================================
--- gcc/testsuite/gcc.dg/Wordered-pointer-comparison-pedantic.c	(revision 0)
+++ gcc/testsuite/gcc.dg/Wordered-pointer-comparison-pedantic.c	(revision 0)
@@ -0,0 +1,23 @@
+/* Test -Wordered-pointer-comparison is enabled by -pedantic */
+/* { dg-do compile } */
+/* { dg-options "-pedantic" } */
+
+void h(int *p)
+{
+    if (p < 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p <= 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p > 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p >= 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p == 0) return;
+    if (p != 0) return;
+}
+
+void j(int *p)
+{
+    if (0 < p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 <= p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 > p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 >= p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 == p) return;
+    if (0 != p) return;
+}
Index: gcc/testsuite/gcc.dg/Wordered-pointer-comparison-Wextra.c
===================================================================
--- gcc/testsuite/gcc.dg/Wordered-pointer-comparison-Wextra.c	(revision 0)
+++ gcc/testsuite/gcc.dg/Wordered-pointer-comparison-Wextra.c	(revision 0)
@@ -0,0 +1,23 @@
+/* Test -Wordered-pointer-comparison is enabled by -Wextra */
+/* { dg-do compile } */
+/* { dg-options "-Wextra" } */
+
+void h(int *p)
+{
+    if (p < 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p <= 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p > 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p >= 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p == 0) return;
+    if (p != 0) return;
+}
+
+void j(int *p)
+{
+    if (0 < p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 <= p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 > p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 >= p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 == p) return;
+    if (0 != p) return;
+}
Index: gcc/testsuite/gcc.dg/Wordered-pointer-comparison.c
===================================================================
--- gcc/testsuite/gcc.dg/Wordered-pointer-comparison.c	(revision 0)
+++ gcc/testsuite/gcc.dg/Wordered-pointer-comparison.c	(revision 0)
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-Wordered-pointer-comparison" } */
+
+void h(int *p)
+{
+    if (p < 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p <= 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p > 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p >= 0) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (p == 0) return;
+    if (p != 0) return;
+}
+
+void j(int *p)
+{
+    if (0 < p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 <= p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 > p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 >= p) return;/* { dg-warning "ordered comparison of pointer with integer zero" } */
+    if (0 == p) return;
+    if (0 != p) return;
+}
Index: gcc/c.opt
===================================================================
--- gcc/c.opt	(revision 120511)
+++ gcc/c.opt	(working copy)
@@ -327,6 +327,10 @@ Wold-style-definition
 C ObjC Var(warn_old_style_definition)
 Warn if an old-style parameter definition is used
 
+Wordered-pointer-comparison
+C ObjC Var(warn_ordered_pointer_comparison) Init(-1)
+Warn if a pointer is compared against integer zero with <, <=, >, or >=
+
 Woverlength-strings
 C ObjC C++ ObjC++ Var(warn_overlength_strings) Init(-1)
 Warn if a string is longer than the maximum portable length specified by the standard
Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 120511)
+++ gcc/c-typeck.c	(working copy)
@@ -8071,13 +8071,13 @@ build_binary_op (enum tree_code code, tr
       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
 	{
 	  result_type = type0;
-	  if (pedantic || extra_warnings)
+	  if (warn_ordered_pointer_comparison) /* enabled by -pedantic */
 	    pedwarn ("ordered comparison of pointer with integer zero");
 	}
       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
 	{
 	  result_type = type1;
-	  if (pedantic)
+	  if (warn_ordered_pointer_comparison) /* enabled by -pedantic */
 	    pedwarn ("ordered comparison of pointer with integer zero");
 	}
       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
Index: gcc/c-opts.c
===================================================================
--- gcc/c-opts.c	(revision 120511)
+++ gcc/c-opts.c	(working copy)
@@ -891,6 +891,8 @@ c_common_handle_option (size_t scode, co
     case OPT_pedantic:
       cpp_opts->pedantic = 1;
       cpp_opts->warn_endif_labels = 1;
+      if (warn_ordered_pointer_comparison == -1)
+        warn_ordered_pointer_comparison = 1;
       if (warn_pointer_sign == -1)
 	warn_pointer_sign = 1;
       if (warn_overlength_strings == -1)
@@ -1027,7 +1029,7 @@ c_common_post_options (const char **pfil
 
   /* -Wextra implies -Wclobbered, -Wempty-body, -Wsign-compare, 
      -Wmissing-field-initializers, -Wmissing-parameter-type
-     -Wold-style-declaration, and -Woverride-init, 
+     -Wold-style-declaration, -Wordered-pointer-comparison, and -Woverride-init, 
      but not if explicitly overridden.  */
   if (warn_clobbered == -1)
     warn_clobbered = extra_warnings;
@@ -1041,6 +1043,8 @@ c_common_post_options (const char **pfil
     warn_missing_parameter_type = extra_warnings;
   if (warn_old_style_declaration == -1)
     warn_old_style_declaration = extra_warnings;
+  if (warn_ordered_pointer_comparison == -1)
+    warn_ordered_pointer_comparison = extra_warnings;
   if (warn_override_init == -1)
     warn_override_init = extra_warnings;
 

                 reply	other threads:[~2007-01-08 21:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6c33472e0701081321l748d60e0i19b1d6e2379e94ee@mail.gmail.com \
    --to=lopezibanez@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).