[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

2017-01-04 Thread egall at gwmail dot gwu.edu
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

--- Comment #11 from Eric Gallager  ---
Thank you for adding this!

[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

2017-01-04 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

Marek Polacek  changed:

   What|Removed |Added

 Status|ASSIGNED|RESOLVED
 Resolution|--- |FIXED

--- Comment #10 from Marek Polacek  ---
Done for GCC 7.

[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

2017-01-04 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

--- Comment #9 from Marek Polacek  ---
Author: mpolacek
Date: Wed Jan  4 21:47:04 2017
New Revision: 244076

URL: https://gcc.gnu.org/viewcvs?rev=244076=gcc=rev
Log:
PR c++/64767
* c.opt (Wpointer-compare): New option.

* c-parser.c (c_parser_postfix_expression): Mark zero character
constants by setting original_type in c_expr.
* c-typeck.c (parser_build_binary_op): Warn when a pointer is compared
with a zero character constant.
(char_type_p): New function.

* typeck.c (cp_build_binary_op): Warn when a pointer is compared with
a zero character literal.

* doc/invoke.texi: Document -Wpointer-compare.

* c-c++-common/Wpointer-compare-1.c: New test.

Added:
trunk/gcc/testsuite/c-c++-common/Wpointer-compare-1.c
Modified:
trunk/gcc/ChangeLog
trunk/gcc/c-family/ChangeLog
trunk/gcc/c-family/c.opt
trunk/gcc/c/ChangeLog
trunk/gcc/c/c-parser.c
trunk/gcc/c/c-typeck.c
trunk/gcc/cp/ChangeLog
trunk/gcc/cp/typeck.c
trunk/gcc/doc/invoke.texi
trunk/gcc/testsuite/ChangeLog

[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

2016-12-07 Thread egall at gwmail dot gwu.edu
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

Eric Gallager  changed:

   What|Removed |Added

 CC||egall at gwmail dot gwu.edu

--- Comment #8 from Eric Gallager  ---
(In reply to Marek Polacek from comment #7)
> This code is now rejected in C++11.  See r240707.

What about in other standards? gcc built at the end of November still seems to
reject the -Wpointer-compare flag that I thought was going to be added for
this:

$ /usr/local/bin/gcc -Wpointer-compare randomer.c
gcc: error: unrecognized command line option ‘-Wpointer-compare’; did you mean
‘-Wnonnull-compare’?

[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

2016-10-03 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

--- Comment #7 from Marek Polacek  ---
This code is now rejected in C++11.  See r240707.

[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

2016-09-08 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

Marek Polacek  changed:

   What|Removed |Added

 Status|NEW |ASSIGNED
 CC||mpolacek at gcc dot gnu.org
   Assignee|unassigned at gcc dot gnu.org  |mpolacek at gcc dot 
gnu.org

--- Comment #6 from Marek Polacek  ---
Looking into this; the C FE part will be harder than the C++ FE part since the
latter has delayed folding.

I'm wondering whether a warning for ptr == (char) 0 would be desirable or not,
but I guess so.

[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

2016-08-31 Thread manu at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

Manuel López-Ibáñez  changed:

   What|Removed |Added

 CC||manu at gcc dot gnu.org

--- Comment #5 from Manuel López-Ibáñez  ---
This could have detected a bug in GCC: http://www.viva64.com/en/b/0425/

[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

2015-01-24 Thread ulfalizer at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

--- Comment #2 from Ulf Magnusson ulfalizer at gmail dot com ---
Looks like -Wnon-literal-null-conversion is intended to warn for the comparison
case too, though it doesn't seem to be implemented yet as of clang 3.4:
http://llvm.org/klaus/clang/commit/50800fc551ac6b8a95cca662223e7f061bbd169a/


[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

2015-01-24 Thread ulfalizer at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

--- Comment #3 from Ulf Magnusson ulfalizer at gmail dot com ---
clang also generates a warning for C programs with void *a = '\0' by the way,
so it seems to be able to look at the form of the integral constant there.


[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

2015-01-24 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

--- Comment #4 from Jonathan Wakely redi at gcc dot gnu.org ---
Yes, although C says '\0' has type int the compiler can still distinguish the
token '\0' from 0 during compilation.


[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

2015-01-24 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

Jonathan Wakely redi at gcc dot gnu.org changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2015-01-24
 Ever confirmed|0   |1
   Severity|enhancement |normal

--- Comment #1 from Jonathan Wakely redi at gcc dot gnu.org ---
Confirming as a bug not an extension.

In C++03 the code is valid because pointer conversions are allowed for integral
constant expressions, such as '\0', but in C++11 pointer conversions are only
allowed for an integer-literal and not a char-literal.

So this should be an error in C++11, and adding a warning in C++03 at the same
time would make sense (maybe enabled by -Wzero-as-null-pointer-constant and/or
-Wc++11-compat)

Clang gives a hard error with -std=c++11

warn.cc:4:9: error: comparison between pointer and integer ('void *' and 'int')
  if (p == '\0')
  ~ ^  
1 error generated.

Similarly for initializing a pointer with a char-literal, for which clang gives
an error in C++11 mode, and a warning for C++98.

warn.cc:3:9: error: cannot initialize a variable of type 'void *' with an
rvalue of type 'char'
  void* p = '\0';
^   
1 error generated.


warn.cc:3:13: warning: expression which evaluates to zero treated as a null
pointer constant of type 'void *' [-Wnon-literal-null-conversion]
  void* p = '\0';
^~~~
1 warning generated.