From patchwork Tue Sep 19 11:06:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Monakov X-Patchwork-Id: 815454 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-462465-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="An4k8JYn"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3xxKpc1Pl2z9rxj for ; Tue, 19 Sep 2017 21:07:35 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=i/TUyrWtA/yjjQ1wGbcBlcJYAsXTn9GaJvd2uekOdBC+c071Hi qV/oP4xotmggI+mPvXoCnZXsP7P0q7kCwe7VHmgfAFYufs6cjSEo8vjLFDLfCIM3 JSABbnUE0VOtku9Y8ozW8hccP/y5djfswXR4IE7vmSwP2SiexKD76mYNA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=Znwlrp9LNNdD6eMT2a3QCCWniHo=; b=An4k8JYnAIAGCb8WK+Lz SgS1NIldt5ecqTDG8yEN85fGr92XGPmZjmAQ/Mun46Tmgtzp7nB2S3AQjXTcPEeb l/D2vkp1/2/eh7oqZp1g8SHppa69kCq2dAZaJ94ZM+3+AjsaljqygU/ne4FalP6x pgyF2SSQVvDbMf3x3YHN1Co= Received: (qmail 86538 invoked by alias); 19 Sep 2017 11:07:24 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 16385 invoked by uid 89); 19 Sep 2017 11:06:32 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 spammy=sk:antico, sk:anti-co, improper X-HELO: smtp.ispras.ru Received: from bran.ispras.ru (HELO smtp.ispras.ru) (83.149.199.196) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 19 Sep 2017 11:06:29 +0000 Received: from monopod.intra.ispras.ru (monopod.intra.ispras.ru [10.10.3.121]) by smtp.ispras.ru (Postfix) with ESMTP id 5012A203CA; Tue, 19 Sep 2017 14:06:23 +0300 (MSK) Date: Tue, 19 Sep 2017 14:06:23 +0300 (MSK) From: Alexander Monakov To: gcc-patches@gcc.gnu.org cc: Nathan Sidwell Subject: [PATCH] cp: fix location comparison in member_name_cmp Message-ID: User-Agent: Alpine 2.20.13 (LNX 116 2015-12-14) MIME-Version: 1.0 Hi, After recent changes, the member_name_cmp qsort comparator can indicate A < B < A (i.e. lacks anti-commutativity) for distinct TYPE_DECL nodes that have the same source location. If their order doesn't matter, the comparator should return 0. Invoking qsort with improper comparator at best makes the output unpredictable (pedantically it invokes undefined behavior); this trips qsort checking I'm preparing to resend. (it also seems that this and the following comparators use less/greater pointer comparison, but unless those pointers point into the same array that invokes undefined behavior too, although benign in practice) Bootstrapped and regtested on x86-64, OK to apply? Thanks. Alexander * name-lookup.c (member_name_cmp): Return 0 if locations compare equal. diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index d0aaf2b1d16..046cd109533 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -1437,7 +1437,7 @@ member_name_cmp (const void *a_p, const void *b_p) if (TREE_CODE (a) == TREE_CODE (b)) /* We can get two TYPE_DECLs or two USING_DECLs. Place in source order. */ - return DECL_SOURCE_LOCATION (a) < DECL_SOURCE_LOCATION (b) ? -1 : +1; + goto compare_locations; /* If one of them is a TYPE_DECL, it loses. */ if (TREE_CODE (a) == TYPE_DECL) @@ -1456,7 +1456,10 @@ member_name_cmp (const void *a_p, const void *b_p) Order by source location. We should really prevent this happening. */ gcc_assert (errorcount); - return DECL_SOURCE_LOCATION (a) < DECL_SOURCE_LOCATION (b) ? -1 : +1; +compare_locations: + if (DECL_SOURCE_LOCATION (a) != DECL_SOURCE_LOCATION (b)) + return DECL_SOURCE_LOCATION (a) < DECL_SOURCE_LOCATION (b) ? -1 : +1; + return 0; } static struct {