From patchwork Wed May 1 15:38:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1930264 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=SnIXlk1a; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VV1b156q8z1ymp for ; Thu, 2 May 2024 01:44:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E3D2384AB41 for ; Wed, 1 May 2024 15:44:26 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id B1B1D3858D20 for ; Wed, 1 May 2024 15:44:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B1B1D3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B1B1D3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714578246; cv=none; b=LgAuUjFEDvitLDF7TJGXtkadv608Etj0pBtZAT9zWgkcHk/gSji9P/lHBVmyqmBLBI+ngjeTYwbfybuK32XGJvXNbya6UH0RrAVatiKKas1ZUwjniDOYsuNxLjyeZ8PyZTsJTBp45NyANp60WlmiR3RhcXwFJzOdSDwlSh+G5JM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714578246; c=relaxed/simple; bh=Cz4OWjVInbacIiSg3dbE9d/QANZZf5uyuvEFIu+PJUs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=vddwfj150p6muDVR6JAzP4bKBH2bCp3wf4WjzyfNlFC/9VBMQCop/bahGsyy4uhdhYK6N5THa0n5+apkJjwkxT7qYzbEmmAbBrIwPiCX5/4cLo+s+FX8XbW9keHTa5BunG3V/kD3Q6y0KUvgWIuxPi4/5Nii8t3qVw4Ajao+p/o= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714578243; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ShV/Hz5jY7wEw7E88y51CzrfdU7gbGuYxkUor7NbzaA=; b=SnIXlk1ad5EXE0klSlu5hQlcO1i1ykNxJ0fFqn+UjrCi4h2s9NuRaZqHw+/yn9GGoTnP9E HZOJnX5YBntAJ20itioYeF4TFteqc4aqYzRDrMEDTHO5I1CLkWdlT0CgRJg1VoKMb4Jzvg gfmLjhiygwzAJg5v/DRNKVS5m/5rdqk= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-391-gQlQX7rUPF2WxULKmEtQyQ-1; Wed, 01 May 2024 11:44:02 -0400 X-MC-Unique: gQlQX7rUPF2WxULKmEtQyQ-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6a0b7ba8d0aso19736166d6.0 for ; Wed, 01 May 2024 08:44:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714578241; x=1715183041; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ShV/Hz5jY7wEw7E88y51CzrfdU7gbGuYxkUor7NbzaA=; b=mjGPsFS2y+l8Q6a7gSqNfFBhRYipyNHGVcPXD16JjbkDUGFiw/LNOm7LMzsh9soEn9 C5iwO3henfovxouplcprzPcpM98BsGQATAtsxqSuqL1DosggWhTvvETGTA4MxegwzWyo /geJNtwtEaIS6W9qg0NkBKgCVv583t4qqDwignuhDR5bD7xzr8GrXasaJKyGKCnfsLgV scow6jiwVw02ZSTcmmYCw9lvyvNZBbv33otBkTv9AstiEAoPvc/NyTX7RW2kCjvSQ4sI mYU4Ha+kgcCXveRcx6mppDAygv0UqSBb+EmfYOhe9dBcHl/oOlDtZkn6uogjarGSyqU/ cFYw== X-Gm-Message-State: AOJu0YwD/eCb4IlXLL07PxfJWBsi1gpVTUWR1pG1u9VMj+0lrdFwgPPq oM3Ktrp81fHVIrBitPVeIOWFQ0Go+KW/X/pFib1Os7Fcn2WYCYptrGjPcq0IPGRb68rc64ObDX/ kLkdaA+BR0BaDC8gg6CU+W0s5ULlosOvCqnEYqiE1TxWaeUHQqEoZHGoWa3knW5T43muloFwaYn TbvwTWhDb+kZhb93X0Kt6HfHbyI+HRS9XiMOE= X-Received: by 2002:ad4:4205:0:b0:69b:7cd2:c07e with SMTP id k5-20020ad44205000000b0069b7cd2c07emr11803qvp.31.1714578241375; Wed, 01 May 2024 08:44:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGhBCsb98vuh8ZL+uS/BMLm9dhQ0B8f64x8T/97zwQOyt/k7c3vwVAFfd+ertHERx8W6Rrstg== X-Received: by 2002:ad4:4205:0:b0:69b:7cd2:c07e with SMTP id k5-20020ad44205000000b0069b7cd2c07emr11767qvp.31.1714578240903; Wed, 01 May 2024 08:44:00 -0700 (PDT) Received: from jason-thinkpadp1gen4i.rmtusma.csb (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id e10-20020a056214110a00b006a0caf1f4c2sm3250337qvs.0.2024.05.01.08.44.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 08:44:00 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: const void* memchr [PR113706] Date: Wed, 1 May 2024 11:38:17 -0400 Message-ID: <20240501154358.519660-1-jason@redhat.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- The C++ standard specifies that the functions have const and non-const overloads, unlike C's single function with const argument and non-const return. Many systems don't actually implement this, but only add an overload with non-const argument, so both end up having non-const return. Solaris does what the standard says, but we were penalizing it by not recognizing the const overload as the built-in memchr. PR c++/113706 gcc/cp/ChangeLog: * decl.cc (decls_match): Handle memchr return type being const-qualified. gcc/testsuite/ChangeLog: * g++.dg/opt/const-builtin1.C: New test. * c-c++-common/pr103798-2.c: Remove xfail. --- gcc/cp/decl.cc | 10 +++++++ gcc/testsuite/c-c++-common/pr103798-2.c | 3 +-- gcc/testsuite/g++.dg/opt/const-builtin1.C | 33 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/opt/const-builtin1.C base-commit: c60b3e211c555706cdc2dc8bfcdd540152cff350 diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index d88e0698652..df855334133 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -1156,6 +1156,16 @@ decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */) tree r2 = fndecl_declared_return_type (olddecl); tree r1 = fndecl_declared_return_type (newdecl); + /* For memchr et al, allow const void* return type (as specified by C++) + when we expect void* (as in C). */ + if (DECL_IS_UNDECLARED_BUILTIN (olddecl) + && DECL_EXTERN_C_P (olddecl) + && !same_type_p (r1, r2) + && TREE_CODE (r1) == POINTER_TYPE + && TREE_CODE (r2) == POINTER_TYPE + && comp_ptr_ttypes (TREE_TYPE (r1), TREE_TYPE (r2))) + r2 = r1; + tree p1 = TYPE_ARG_TYPES (f1); tree p2 = TYPE_ARG_TYPES (f2); diff --git a/gcc/testsuite/c-c++-common/pr103798-2.c b/gcc/testsuite/c-c++-common/pr103798-2.c index 83cdfaa1660..e7e99c3679e 100644 --- a/gcc/testsuite/c-c++-common/pr103798-2.c +++ b/gcc/testsuite/c-c++-common/pr103798-2.c @@ -27,5 +27,4 @@ main () return 0; } -/* See PR c++/113706 for the xfail. */ -/* { dg-final { scan-assembler-not "memchr" { xfail { c++ && { *-*-solaris2* *-*-vxworks* } } } } } */ +/* { dg-final { scan-assembler-not "memchr" } } */ diff --git a/gcc/testsuite/g++.dg/opt/const-builtin1.C b/gcc/testsuite/g++.dg/opt/const-builtin1.C new file mode 100644 index 00000000000..8b9987271c0 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/const-builtin1.C @@ -0,0 +1,33 @@ +// PR c++/113706 +/* A variant of the pr103798-2.c test with const void * memchr return type, as + specified by the C++ standard. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fdump-tree-optimized -save-temps" } */ + +extern "C" const void *memchr (const void *, int, __SIZE_TYPE__); // { dg-bogus "built-in" } + +__attribute__ ((weak)) +int +f (int a) +{ + return memchr ("aE", a, 2) != 0; +} + +__attribute__ ((weak)) +int +g (char a) +{ + return a == 'a' || a == 'E'; +} + +int +main () +{ + for (int i = 0; i < 255; i++) + if (f (i + 256) != g (i + 256)) + __builtin_abort (); + + return 0; +} + +/* { dg-final { scan-assembler-not "memchr" } } */