#define DEFINED (x != 0) int foo(int x) { if (!DEFINED && x != 0) return 0; return 1; } manuel@gcc10:~$ ~/test1/210581/build/gcc/cc1 -Wlogical-op test.c foo test.c: In function ‘foo’: test.c:5:16: warning: logical ‘and’ of mutually exclusive tests is always false [-Wlogical-op] if (!DEFINED && x != 0) return 0; ^ Clang does not warn. This was the reason -Wlogical-op was moved out of -Wextra (PR40172).
This should be easy with track macro expansion on, we'd just have --- a/gcc/input.h +++ b/gcc/input.h @@ -60,6 +60,8 @@ extern location_t input_location; #define in_system_header_at(LOC) \ ((linemap_location_in_system_header_p (line_table, LOC))) +#define from_macro_expansion_at(LOC) \ + ((linemap_location_from_macro_expansion_p (line_table, LOC))) void dump_line_table_statistics (void); and then we could use from_macro_expansion_at and don't warn if it's true. But the problem is with -ftrack-macro-expansion=0, since from_macro_expansion_at wouldn't work :(.
(In reply to Marek Polacek from comment #1) > and then we could use from_macro_expansion_at and don't warn if it's true. > But the problem is with -ftrack-macro-expansion=0, since > from_macro_expansion_at wouldn't work :(. I think warning in that case (returning false in the macro) is fair. One cannot expect to turn off features and get the same quality. Is ftrack-macro-expansion=0 used when bootstrapping gcc?
I don't think so, -ftrack-macro-expansion=2 is on by default and I don't see -ftrack-macro-expansion=0 anywhere in the log of bootstrap. So maybe my approach would be viable after all (and I could fix PR61081 the same way then).
BTW, clang doesn't warn even when neither operand comes from a macro expansion; and clang version 3.4 doesn't know -Wlogical-op warning option (so I tried -Wall -Wextra -Weverything, but still no warning). Yes, I don't have mainline clang, but I don't see that option in clang code base.
If we take gcc/testsuite/gcc.dg/pr40172-3.c (which is XFAIL) extern int xxx; #define XXX xxx int test (void) { if (!XXX && xxx) return 4; else return 0; } The big hurdle is that !XXX becomes XXX == 0, but it has the location of "!", which is not virtual. If we look at the argument of the expression, then XXX is actually a var_decl, whose location corresponds to the declaration and not the use, and it is not virtual either. This is PR43486.
*** Bug 53131 has been marked as a duplicate of this bug. ***
Author: mpolacek Date: Fri Apr 24 11:49:52 2015 New Revision: 222406 URL: https://gcc.gnu.org/viewcvs?rev=222406&root=gcc&view=rev Log: PR c/61534 * input.h (from_macro_expansion_at): Define. * c-common.c (warn_logical_operator): Bail if either operand comes from a macro expansion. * c-c++-common/pr61534-1.c: New test. Added: trunk/gcc/testsuite/c-c++-common/pr61534-1.c Modified: trunk/gcc/ChangeLog trunk/gcc/c-family/ChangeLog trunk/gcc/c-family/c-common.c trunk/gcc/input.h trunk/gcc/testsuite/ChangeLog
The last patch did not fix the original testcase nor gcc/testsuite/gcc.dg/pr40172-3.c
So that's why this PR is still open.
(In reply to Marek Polacek from comment #9) > So that's why this PR is still open. Sure, sorry, I should have been clearer. It was mostly a note to myself so I do not need to re-check next time I look at this PR.
Np. It's certainly something I'd love to see fixed :/. Hopefully the next stage1.
This prevent enabling -Wlogical-op with -Wall or -Wextra.
(In reply to Marek Polacek from comment #11) > Np. It's certainly something I'd love to see fixed :/. Hopefully the next > stage1. It's a new stage1