public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/64524] New: gcc can't detect same expression in both parts of ternary operator
@ 2015-01-07 16:54 dcb314 at hotmail dot com
2015-01-07 17:49 ` [Bug c++/64524] " jakub at gcc dot gnu.org
` (6 more replies)
0 siblings, 7 replies; 8+ messages in thread
From: dcb314 at hotmail dot com @ 2015-01-07 16:54 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64524
Bug ID: 64524
Summary: gcc can't detect same expression in both parts of
ternary operator
Product: gcc
Version: 5.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: dcb314 at hotmail dot com
Given this source code
extern void g(int);
void
f( int n)
{
int i = (n >= 10) ? 1 : 1;
g( i - 1);
}
then trunk gcc says nothing:
$ ~/gcc/results/bin/gcc -c -O2 -Wall -Wextra jan6c.cc
$
Here is cppcheck detecting the problem:
$ ~/cppcheck/trunk/cppcheck --enable=all jan6c.cc
Checking jan6c.cc...
[jan6c.cc:9]: (style) Same expression in both branches of ternary operator.
$
Fixing this bug would find about a dozen bugs in Linux kernel
and about 100 bugs in Fedora Linux.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/64524] gcc can't detect same expression in both parts of ternary operator
2015-01-07 16:54 [Bug c++/64524] New: gcc can't detect same expression in both parts of ternary operator dcb314 at hotmail dot com
@ 2015-01-07 17:49 ` jakub at gcc dot gnu.org
2015-01-07 18:00 ` dcb314 at hotmail dot com
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2015-01-07 17:49 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64524
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jakub at gcc dot gnu.org
--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Should it complain only when the expressions are the same (e.g. token-wise), or
even when just two different expressions have the same value?
I mean, say:
enum X { E = 5, F = 5 };
int i = cond ? E : F;
int j = cond ? x + 3 : x + 2 + 1;
int k = cond ? sizeof (x) : sizeof (y);
etc.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/64524] gcc can't detect same expression in both parts of ternary operator
2015-01-07 16:54 [Bug c++/64524] New: gcc can't detect same expression in both parts of ternary operator dcb314 at hotmail dot com
2015-01-07 17:49 ` [Bug c++/64524] " jakub at gcc dot gnu.org
@ 2015-01-07 18:00 ` dcb314 at hotmail dot com
2015-01-07 18:12 ` jakub at gcc dot gnu.org
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: dcb314 at hotmail dot com @ 2015-01-07 18:00 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64524
--- Comment #2 from David Binderman <dcb314 at hotmail dot com> ---
(In reply to Jakub Jelinek from comment #1)
> Should it complain only when the expressions are the same (e.g. token-wise),
> or even when just two different expressions have the same value?
> I mean, say:
> enum X { E = 5, F = 5 };
> int i = cond ? E : F;
> int j = cond ? x + 3 : x + 2 + 1;
> int k = cond ? sizeof (x) : sizeof (y);
> etc.
All would be good ;->
I suspect token-wise comparison would be simple and cheap, so
suggest do that first. That would cover my example.
I suspect more complex would be making sure two compile time
constants have the same value, so suggest do that second.
That would cover cases i and k above.
Suspect case j would be more difficult, since it involves
expression comparison. Suggest leave till last.
Feel free to adjust the priority order if my guesses are wrong.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/64524] gcc can't detect same expression in both parts of ternary operator
2015-01-07 16:54 [Bug c++/64524] New: gcc can't detect same expression in both parts of ternary operator dcb314 at hotmail dot com
2015-01-07 17:49 ` [Bug c++/64524] " jakub at gcc dot gnu.org
2015-01-07 18:00 ` dcb314 at hotmail dot com
@ 2015-01-07 18:12 ` jakub at gcc dot gnu.org
2015-01-07 19:45 ` maltsevm at gmail dot com
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2015-01-07 18:12 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64524
--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to David Binderman from comment #2)
Well, tokenwise comparison is difficult, as lots of things are folded early.
So if you don't mind the same value as folding, implementing that wouldn't be
that hard.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/64524] gcc can't detect same expression in both parts of ternary operator
2015-01-07 16:54 [Bug c++/64524] New: gcc can't detect same expression in both parts of ternary operator dcb314 at hotmail dot com
` (2 preceding siblings ...)
2015-01-07 18:12 ` jakub at gcc dot gnu.org
@ 2015-01-07 19:45 ` maltsevm at gmail dot com
2015-01-07 20:12 ` pinskia at gcc dot gnu.org
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: maltsevm at gmail dot com @ 2015-01-07 19:45 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64524
Mikhail Maltsev <maltsevm at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |maltsevm at gmail dot com
--- Comment #4 from Mikhail Maltsev <maltsevm at gmail dot com> ---
> int k = cond ? sizeof (x) : sizeof (y);
By the way, this is a good example of probable "false positives". Consider:
constexpr std::size_t max_size = (sizeof(T1) > sizeof(T2)) ? sizeof(T1) :
sizeof(T2);
This is an example of "legal" code, it could be used for allocating a buffer
which could hold either object of type T1 or type T2. These sizes may be
platform-dependent (or they may depend on template parameters), so this code
makes sence, but it will produce a warning when sizes are equal.
So, probably this warning only makes sence, if ASTs' would be compared, not the
values.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/64524] gcc can't detect same expression in both parts of ternary operator
2015-01-07 16:54 [Bug c++/64524] New: gcc can't detect same expression in both parts of ternary operator dcb314 at hotmail dot com
` (3 preceding siblings ...)
2015-01-07 19:45 ` maltsevm at gmail dot com
@ 2015-01-07 20:12 ` pinskia at gcc dot gnu.org
2015-01-07 20:38 ` dcb314 at hotmail dot com
2015-01-07 20:39 ` dcb314 at hotmail dot com
6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2015-01-07 20:12 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64524
--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is also a style warning. And really here is another false positive:
#define a 1
#define b 1
int t = c > d ? a : b;
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/64524] gcc can't detect same expression in both parts of ternary operator
2015-01-07 16:54 [Bug c++/64524] New: gcc can't detect same expression in both parts of ternary operator dcb314 at hotmail dot com
` (4 preceding siblings ...)
2015-01-07 20:12 ` pinskia at gcc dot gnu.org
@ 2015-01-07 20:38 ` dcb314 at hotmail dot com
2015-01-07 20:39 ` dcb314 at hotmail dot com
6 siblings, 0 replies; 8+ messages in thread
From: dcb314 at hotmail dot com @ 2015-01-07 20:38 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64524
--- Comment #6 from David Binderman <dcb314 at hotmail dot com> ---
(In reply to Andrew Pinski from comment #5)
> And really here is another false positive:
> #define a 1
> #define b 1
>
> int t = c > d ? a : b;
Assuming you meant
int t = (c > d) ? a : b;
then I'd be interested to find out how that's a false positive.
Just to add some salt into the pot, another interesting case might be
enum E { a, b, c, d };
void f(int val)
{
int n = (val >= 10) ? a : (d - 4);
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/64524] gcc can't detect same expression in both parts of ternary operator
2015-01-07 16:54 [Bug c++/64524] New: gcc can't detect same expression in both parts of ternary operator dcb314 at hotmail dot com
` (5 preceding siblings ...)
2015-01-07 20:38 ` dcb314 at hotmail dot com
@ 2015-01-07 20:39 ` dcb314 at hotmail dot com
6 siblings, 0 replies; 8+ messages in thread
From: dcb314 at hotmail dot com @ 2015-01-07 20:39 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64524
--- Comment #7 from David Binderman <dcb314 at hotmail dot com> ---
(In reply to David Binderman from comment #6)
Sorry, typo in my original code. Better code:
enum E { a, b, c, d };
void f(int val)
{
int n = (val >= 10) ? a : (d - 3);
}
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2015-01-07 20:39 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-07 16:54 [Bug c++/64524] New: gcc can't detect same expression in both parts of ternary operator dcb314 at hotmail dot com
2015-01-07 17:49 ` [Bug c++/64524] " jakub at gcc dot gnu.org
2015-01-07 18:00 ` dcb314 at hotmail dot com
2015-01-07 18:12 ` jakub at gcc dot gnu.org
2015-01-07 19:45 ` maltsevm at gmail dot com
2015-01-07 20:12 ` pinskia at gcc dot gnu.org
2015-01-07 20:38 ` dcb314 at hotmail dot com
2015-01-07 20:39 ` dcb314 at hotmail dot com
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).