diff options
author | David Malcolm <dmalcolm@redhat.com> | 2018-12-20 14:18:48 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2018-12-20 14:18:48 +0000 |
commit | 573767d4d2cbea5ba5c6de753455055fe4de355d (patch) | |
tree | 299ef323018ddbe9a66484111699585cbff65fa4 /gcc | |
parent | 49fb2968516474bf2d825b62933e2ddcdef2c296 (diff) | |
download | gcc-573767d4d2cbea5ba5c6de753455055fe4de355d.zip gcc-573767d4d2cbea5ba5c6de753455055fe4de355d.tar.gz gcc-573767d4d2cbea5ba5c6de753455055fe4de355d.tar.bz2 |
-Wtautological-compare: fix comparison of macro expansions
gcc/c-family/ChangeLog:
PR c++/87504
* c-warn.c (get_outermost_macro_expansion): New function.
(spelled_the_same_p): Use it to unwind the macro expansions, and
compare the outermost macro in each nested expansion, rather than
the innermost.
gcc/testsuite/ChangeLog:
PR c++/87504
* c-c++-common/Wtautological-compare-8.c: New test.
From-SVN: r267299
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/c-family/ChangeLog | 8 | ||||
-rw-r--r-- | gcc/c-family/c-warn.c | 26 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/c-c++-common/Wtautological-compare-8.c | 33 |
4 files changed, 67 insertions, 5 deletions
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 637a288..8e2527b 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,11 @@ +2018-12-20 David Malcolm <dmalcolm@redhat.com> + + PR c++/87504 + * c-warn.c (get_outermost_macro_expansion): New function. + (spelled_the_same_p): Use it to unwind the macro expansions, and + compare the outermost macro in each nested expansion, rather than + the innermost. + 2018-12-19 David Malcolm <dmalcolm@redhat.com> PR c++/87504 diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c index b0f6da0..6013202 100644 --- a/gcc/c-family/c-warn.c +++ b/gcc/c-family/c-warn.c @@ -399,6 +399,25 @@ warn_tautological_bitwise_comparison (const op_location_t &loc, tree_code code, "bitwise comparison always evaluates to true"); } +/* Given LOC from a macro expansion, return the map for the outermost + macro in the nest of expansions. */ + +static const line_map_macro * +get_outermost_macro_expansion (location_t loc) +{ + gcc_assert (from_macro_expansion_at (loc)); + + const line_map *map = linemap_lookup (line_table, loc); + const line_map_macro *macro_map; + do + { + macro_map = linemap_check_macro (map); + loc = linemap_unwind_toward_expansion (line_table, loc, &map); + } while (linemap_macro_expansion_map_p (map)); + + return macro_map; +} + /* Given LOC_A and LOC_B from macro expansions, return true if they are "spelled the same" i.e. if they are both directly from expansion of the same non-function-like macro. */ @@ -409,11 +428,8 @@ spelled_the_same_p (location_t loc_a, location_t loc_b) gcc_assert (from_macro_expansion_at (loc_a)); gcc_assert (from_macro_expansion_at (loc_b)); - const line_map_macro *map_a - = linemap_check_macro (linemap_lookup (line_table, loc_a)); - - const line_map_macro *map_b - = linemap_check_macro (linemap_lookup (line_table, loc_b)); + const line_map_macro *map_a = get_outermost_macro_expansion (loc_a); + const line_map_macro *map_b = get_outermost_macro_expansion (loc_b); if (map_a->macro == map_b->macro) if (!cpp_fun_like_macro_p (map_a->macro)) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 8bc30db..61aa13b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-12-20 David Malcolm <dmalcolm@redhat.com> + + PR c++/87504 + * c-c++-common/Wtautological-compare-8.c: New test. + 2018-12-20 Richard Biener <rguenther@suse.de> PR tree-optimization/84362 diff --git a/gcc/testsuite/c-c++-common/Wtautological-compare-8.c b/gcc/testsuite/c-c++-common/Wtautological-compare-8.c new file mode 100644 index 0000000..1adedad --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wtautological-compare-8.c @@ -0,0 +1,33 @@ +/* { dg-options "-Wtautological-compare" } */ + +int foo; +#define INCOMING_FRAME_SP_OFFSET foo +#define DEFAULT_INCOMING_FRAME_SP_OFFSET INCOMING_FRAME_SP_OFFSET + +int test (void) +{ + if (DEFAULT_INCOMING_FRAME_SP_OFFSET != INCOMING_FRAME_SP_OFFSET) /* { dg-warning "self-comparison" "" { target c } } */ + return 1; + else + return 0; +} + +#define BYTES_BIG_ENDIAN foo +#define WORDS_BIG_ENDIAN foo + +int test_2 (void) +{ + if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) /* { dg-warning "self-comparison" "" { target c } } */ + return 1; + else + return 0; +} + +#define COND DEFAULT_INCOMING_FRAME_SP_OFFSET != INCOMING_FRAME_SP_OFFSET +int test_3 (void) +{ + if (COND) + return 1; + else + return 0; +} |