aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2021-07-16 15:58:01 -0400
committerMarek Polacek <polacek@redhat.com>2021-07-27 11:38:59 -0400
commitbee2f80b901d73f50275f2b44932067ffcf616ca (patch)
tree36072f98168cd54913f966c53cb37b88320873e4 /gcc
parent7ffba77d01a2445c73ec487ba350c7fbf75a4500 (diff)
downloadgcc-bee2f80b901d73f50275f2b44932067ffcf616ca.zip
gcc-bee2f80b901d73f50275f2b44932067ffcf616ca.tar.gz
gcc-bee2f80b901d73f50275f2b44932067ffcf616ca.tar.bz2
c++: Reject ordered comparison of null pointers [PR99701]
When implementing DR 1512 in r11-467 I neglected to reject ordered comparison of two null pointers, like nullptr < nullptr. This patch fixes that omission. DR 1512 PR c++/99701 gcc/cp/ChangeLog: * cp-gimplify.c (cp_fold): Remove {LE,LT,GE,GT_EXPR} from a switch. * typeck.c (cp_build_binary_op): Reject ordered comparison of two null pointers. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/nullptr11.C: Remove invalid tests. * g++.dg/cpp0x/nullptr46.C: Add dg-error. * g++.dg/cpp2a/spaceship-err7.C: New test. * g++.dg/expr/ptr-comp4.C: New test. libstdc++-v3/ChangeLog: * testsuite/20_util/tuple/comparison_operators/overloaded.cc: Move a line... * testsuite/20_util/tuple/comparison_operators/overloaded2.cc: ...here. New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/cp-gimplify.c4
-rw-r--r--gcc/cp/typeck.c15
-rw-r--r--gcc/testsuite/g++.dg/cpp0x/nullptr11.C16
-rw-r--r--gcc/testsuite/g++.dg/cpp0x/nullptr46.C3
-rw-r--r--gcc/testsuite/g++.dg/cpp2a/spaceship-err7.C14
-rw-r--r--gcc/testsuite/g++.dg/expr/ptr-comp4.C21
6 files changed, 40 insertions, 33 deletions
diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index ff0bff7..0520fa4 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -2433,13 +2433,9 @@ cp_fold (tree x)
switch (code)
{
case EQ_EXPR:
- case LE_EXPR:
- case GE_EXPR:
x = constant_boolean_node (true, TREE_TYPE (x));
break;
case NE_EXPR:
- case LT_EXPR:
- case GT_EXPR:
x = constant_boolean_node (false, TREE_TYPE (x));
break;
default:
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index a483e1f..738e69a 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -5483,7 +5483,9 @@ cp_build_binary_op (const op_location_t &location,
result_type = composite_pointer_type (location,
type0, type1, op0, op1,
CPO_COMPARISON, complain);
- else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
+ else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
+ || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
+ || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
{
/* Core Issue 1512 made this ill-formed. */
if (complain & tf_error)
@@ -5491,17 +5493,6 @@ cp_build_binary_op (const op_location_t &location,
"integer zero (%qT and %qT)", type0, type1);
return error_mark_node;
}
- else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
- {
- /* Core Issue 1512 made this ill-formed. */
- if (complain & tf_error)
- error_at (location, "ordered comparison of pointer with "
- "integer zero (%qT and %qT)", type0, type1);
- return error_mark_node;
- }
- else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
- /* One of the operands must be of nullptr_t type. */
- result_type = TREE_TYPE (nullptr_node);
else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
{
result_type = type0;
diff --git a/gcc/testsuite/g++.dg/cpp0x/nullptr11.C b/gcc/testsuite/g++.dg/cpp0x/nullptr11.C
index f81f0c3..b8bc682 100644
--- a/gcc/testsuite/g++.dg/cpp0x/nullptr11.C
+++ b/gcc/testsuite/g++.dg/cpp0x/nullptr11.C
@@ -9,31 +9,15 @@ void fun()
{
assert_true(nullptr == nullptr);
assert_false(nullptr != nullptr);
- assert_false(nullptr < nullptr);
- assert_false(nullptr > nullptr);
- assert_true(nullptr <= nullptr);
- assert_true(nullptr >= nullptr);
decltype(nullptr) mynull = 0;
assert_true(mynull == nullptr);
assert_false(mynull != nullptr);
- assert_false(mynull < nullptr);
- assert_false(mynull > nullptr);
- assert_true(mynull <= nullptr);
- assert_true(mynull >= nullptr);
assert_true(nullptr == mynull);
assert_false(nullptr != mynull);
- assert_false(nullptr < mynull);
- assert_false(nullptr > mynull);
- assert_true(nullptr <= mynull);
- assert_true(nullptr >= mynull);
assert_true(mynull == mynull);
assert_false(mynull != mynull);
- assert_false(mynull < mynull);
- assert_false(mynull > mynull);
- assert_true(mynull <= mynull);
- assert_true(mynull >= mynull);
}
diff --git a/gcc/testsuite/g++.dg/cpp0x/nullptr46.C b/gcc/testsuite/g++.dg/cpp0x/nullptr46.C
index 1514cee..6c08eaa 100644
--- a/gcc/testsuite/g++.dg/cpp0x/nullptr46.C
+++ b/gcc/testsuite/g++.dg/cpp0x/nullptr46.C
@@ -7,5 +7,6 @@ decltype(nullptr) foo ();
bool
bar ()
{
- return foo () > nullptr || foo () < nullptr;
+ return foo () > nullptr // { dg-error "ordered comparison" }
+ || foo () < nullptr; // { dg-error "ordered comparison" }
}
diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-err7.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-err7.C
new file mode 100644
index 0000000..bc30923
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-err7.C
@@ -0,0 +1,14 @@
+// PR c++/99701
+// { dg-do compile { target c++20 } }
+
+#include <compare>
+
+decltype(nullptr) foo ();
+
+auto b0 = nullptr <=> nullptr; // { dg-error "ordered comparison" }
+auto b1 = 0 <=> nullptr; // { dg-error "ordered comparison" }
+auto b2 = nullptr <=> 0; // { dg-error "ordered comparison" }
+auto b3 = foo () <=> 0; // { dg-error "ordered comparison" }
+auto b4 = 0 <=> foo (); // { dg-error "ordered comparison" }
+auto b5 = foo () <=> nullptr; // { dg-error "ordered comparison" }
+auto b6 = nullptr <=> foo (); // { dg-error "ordered comparison" }
diff --git a/gcc/testsuite/g++.dg/expr/ptr-comp4.C b/gcc/testsuite/g++.dg/expr/ptr-comp4.C
new file mode 100644
index 0000000..da49518
--- /dev/null
+++ b/gcc/testsuite/g++.dg/expr/ptr-comp4.C
@@ -0,0 +1,21 @@
+// PR c++/99701
+// DR 1512
+// { dg-do compile { target c++11 } }
+
+/* Relational comparisons between null pointer constants and pointers are
+ ill-formed. */
+
+decltype(nullptr) foo ();
+
+auto lt = nullptr < nullptr; // { dg-error "ordered comparison" }
+auto gt = nullptr > nullptr; // { dg-error "ordered comparison" }
+auto le = nullptr <= nullptr; // { dg-error "ordered comparison" }
+auto ge = nullptr >= nullptr; // { dg-error "ordered comparison" }
+auto eq = nullptr == nullptr;
+auto ne = nullptr != nullptr;
+auto a1 = nullptr > 0; // { dg-error "ordered comparison" }
+auto a2 = 0 < nullptr; // { dg-error "ordered comparison" }
+auto a3 = foo () > 0; // { dg-error "ordered comparison" }
+auto a4 = 0 < foo (); // { dg-error "ordered comparison" }
+auto a5 = 0 <= foo (); // { dg-error "ordered comparison" }
+auto a6 = foo () >= 0; // { dg-error "ordered comparison" }