From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 46165 invoked by alias); 20 Dec 2018 02:13:15 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 46149 invoked by uid 89); 20 Dec 2018 02:13:15 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 20 Dec 2018 02:13:13 +0000 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C2555A82C; Thu, 20 Dec 2018 02:13:11 +0000 (UTC) Received: from c64.redhat.com (ovpn-112-19.phx2.redhat.com [10.3.112.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id A471D105B1E1; Thu, 20 Dec 2018 02:13:10 +0000 (UTC) From: David Malcolm To: Aaron Sawdey , gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH] -Wtautological-compare: fix comparison of macro expansions Date: Thu, 20 Dec 2018 02:13:00 -0000 Message-Id: <1545274889-6253-1-git-send-email-dmalcolm@redhat.com> In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2018-12/txt/msg01433.txt.bz2 On Wed, 2018-12-19 at 17:27 -0600, Aaron Sawdey wrote: > Assuming you applied this as svn 267273, it causes bootstrap failure > on powerpc64le-unknown-linux-gnu. Stage 2 fails with multiple > instances > of this error: > > ../../trunk-base/gcc/c-family/c-pragma.c: In function ‘void > handle_pragma_scalar_storage_order(cpp_reader*)’: > ../../trunk-base/gcc/c-family/c-pragma.c:417:24: error: self- > comparison always evaluates to false [-Werror=tautological-compare] > 417 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) > | ^~ > ../../trunk-base/gcc/c-family/c-attribs.c: In function ‘tree_node* > handle_scalar_storage_order_attribute(tree_node**, tree, tree, int, > bool*)’: > ../../trunk-base/gcc/c-family/c-attribs.c:1401:24: error: self- > comparison always evaluates to false [-Werror=tautological-compare] > 1401 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) > | ^~ > ../../trunk-base/gcc/builtins.c: In function ‘rtx_def* > c_readstr(const char*, scalar_int_mode)’: > ../../trunk-base/gcc/builtins.c:830:28: error: self-comparison always > evaluates to false [-Werror=tautological-compare] > 830 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN > | ^~ > ../../trunk-base/gcc/combine.c: In function ‘int > rtx_equal_for_field_assignment_p(rtx, rtx, bool)’: > ../../trunk-base/gcc/combine.c:9668:28: error: self-comparison always > evaluates to false [-Werror=tautological-compare] > 9668 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) > | ^~ > > Aaron Sorry about that. Does the following patch help? (testing in progress here) gcc/c-family/ChangeLog: * 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: * c-c++-common/Wtautological-compare-8.c: New test. --- gcc/c-family/c-warn.c | 26 +++++++++++++---- .../c-c++-common/Wtautological-compare-8.c | 33 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wtautological-compare-8.c 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/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; +} -- 1.8.5.3