From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1105) id 9A34D3858C5E; Thu, 2 Feb 2023 23:30:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A34D3858C5E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675380634; bh=5f11gIoWGWzybIeoX5OQAskxta1+ryILh+PZBVX5xZU=; h=From:To:Subject:Date:From; b=CXoKJkOhr5Z7oKTcLUADyRuKu/rw1UpoKR1Ucb/HU6LGdwXruZhqzmFRnZD6/mRGN q9/NeS1blYN2Cp+w395mJ1FmtD2SBBqz7CkM1bxJSP3yUcvd0zl2PMEuOYgHtNMKd9 PP0YPD6MPO+xzlDcJir7StrvmnjSfVKd2oxHOahQ= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Joseph Myers To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5667] c: Update nullptr_t comparison checks X-Act-Checkin: gcc X-Git-Author: Joseph Myers X-Git-Refname: refs/heads/master X-Git-Oldrev: 07c87fce63541846ca2951e22dac04fcaa66475f X-Git-Newrev: 0b8693fc87b1453bdacfa5910091be15da671d2a Message-Id: <20230202233034.9A34D3858C5E@sourceware.org> Date: Thu, 2 Feb 2023 23:30:34 +0000 (GMT) List-Id: https://gcc.gnu.org/g:0b8693fc87b1453bdacfa5910091be15da671d2a commit r13-5667-g0b8693fc87b1453bdacfa5910091be15da671d2a Author: Joseph Myers Date: Thu Feb 2 23:29:45 2023 +0000 c: Update nullptr_t comparison checks WG14 has agreed to allow equality comparisons between pointers and nullptr_t values that are not null pointer constants (this was previously an exceptional case where such nullptr_t values were handled differently from null pointer constants; other places in the standard allowed nullptr_t values, whether or not those values are null pointer constants, in the same contexts as null pointer constants); see the wording at the end of N3077. Update GCC's implementation to match this change. There are also changes to allow null pointer constants of integer or pointer type to be converted to nullptr_t (by assignment, cast or conversion as if by assignment), which I'll deal with separately. Bootstrapped with no regressions for x86_64-pc-linux-gnu. gcc/c/ * c-typeck.cc (build_binary_op): Allow comparisons between pointers and nullptr_t values that are not null pointer constants. gcc/testsuite/ * gcc.dg/c2x-constexpr-3.c: Do not expect comparison of nullptr_t and pointer to be disallowed. * gcc.dg/c2x-nullptr-1.c: Test comparisons of nullptr_t and pointers are allowed. * gcc.dg/c2x-nullptr-3.c: Do not test that comparisons of nullptr_t and pointers are disallowed. Diff: --- gcc/c/c-typeck.cc | 8 ++++++-- gcc/testsuite/gcc.dg/c2x-constexpr-3.c | 1 - gcc/testsuite/gcc.dg/c2x-nullptr-1.c | 17 +++++++++++++++++ gcc/testsuite/gcc.dg/c2x-nullptr-3.c | 15 --------------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 9d65130154d..224a9cbdc3d 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -12749,12 +12749,16 @@ build_binary_op (location_t location, enum tree_code code, && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE)) short_compare = 1; - else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) + else if (code0 == POINTER_TYPE + && (code1 == NULLPTR_TYPE + || null_pointer_constant_p (orig_op1))) { maybe_warn_for_null_address (location, op0, code); result_type = type0; } - else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0)) + else if (code1 == POINTER_TYPE + && (code0 == NULLPTR_TYPE + || null_pointer_constant_p (orig_op0))) { maybe_warn_for_null_address (location, op1, code); result_type = type1; diff --git a/gcc/testsuite/gcc.dg/c2x-constexpr-3.c b/gcc/testsuite/gcc.dg/c2x-constexpr-3.c index 4f6b8ed6779..44a3ed358e1 100644 --- a/gcc/testsuite/gcc.dg/c2x-constexpr-3.c +++ b/gcc/testsuite/gcc.dg/c2x-constexpr-3.c @@ -219,7 +219,6 @@ f0 () (constexpr signed char []) { u8"\xff" }; /* { dg-error "'constexpr' initializer not representable in type of object" } */ constexpr typeof (nullptr) not_npc = nullptr; int *ptr = 0; - (void) (ptr == not_npc); /* { dg-error "invalid operands" } */ /* auto may only be used with another storage class specifier, such as constexpr, if the type is inferred. */ auto constexpr int a_c_t = 1; /* { dg-error "'auto' used with 'constexpr'" } */ diff --git a/gcc/testsuite/gcc.dg/c2x-nullptr-1.c b/gcc/testsuite/gcc.dg/c2x-nullptr-1.c index 9f2cb6c8256..04f9901bb12 100644 --- a/gcc/testsuite/gcc.dg/c2x-nullptr-1.c +++ b/gcc/testsuite/gcc.dg/c2x-nullptr-1.c @@ -141,6 +141,23 @@ test2 (int *p) (void) (p != _Generic(0, int : nullptr)); (void) (_Generic(0, int : nullptr) == p); (void) (_Generic(0, int : nullptr) != p); + + /* "(nullptr_t)nullptr" has type nullptr_t but isn't an NPC; these + comparisons are valid after C2X CD comments GB-071 and FR-073 were + resolved by the wording in N3077. */ + (void) ((nullptr_t)nullptr == p); + (void) ((nullptr_t)nullptr != p); + (void) (p == (nullptr_t)nullptr); + (void) (p != (nullptr_t)nullptr); + (void) (cmp () == p); + (void) (cmp () != p); + (void) (p == cmp ()); + (void) (p != cmp ()); + /* "(void *)nullptr" is not an NPC, either. */ + (void) ((void *)nullptr == cmp ()); + (void) ((void *)nullptr != cmp ()); + (void) (cmp () == (void *)nullptr); + (void) (cmp () != (void *)nullptr); } /* Test ?:. */ diff --git a/gcc/testsuite/gcc.dg/c2x-nullptr-3.c b/gcc/testsuite/gcc.dg/c2x-nullptr-3.c index 34e3e03ba9d..591ab7e6158 100644 --- a/gcc/testsuite/gcc.dg/c2x-nullptr-3.c +++ b/gcc/testsuite/gcc.dg/c2x-nullptr-3.c @@ -19,21 +19,6 @@ test1 (int *p) (void) (nullptr != 1); /* { dg-error "invalid operands" } */ (void) (1 != nullptr); /* { dg-error "invalid operands" } */ (void) (1 > nullptr); /* { dg-error "invalid operands" } */ - - /* "(nullptr_t)nullptr" has type nullptr_t but isn't an NPC. */ - (void) ((nullptr_t)nullptr == p); /* { dg-error "invalid operands" } */ - (void) ((nullptr_t)nullptr != p); /* { dg-error "invalid operands" } */ - (void) (p == (nullptr_t)nullptr); /* { dg-error "invalid operands" } */ - (void) (p != (nullptr_t)nullptr); /* { dg-error "invalid operands" } */ - (void) (cmp () == p); /* { dg-error "invalid operands" } */ - (void) (cmp () != p); /* { dg-error "invalid operands" } */ - (void) (p == cmp ()); /* { dg-error "invalid operands" } */ - (void) (p != cmp ()); /* { dg-error "invalid operands" } */ - /* "(void *)nullptr" is not an NPC, either. */ - (void) ((void *)nullptr == cmp ()); /* { dg-error "invalid operands" } */ - (void) ((void *)nullptr != cmp ()); /* { dg-error "invalid operands" } */ - (void) (cmp () == (void *)nullptr); /* { dg-error "invalid operands" } */ - (void) (cmp () != (void *)nullptr); /* { dg-error "invalid operands" } */ } void