From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from esa3.mentor.iphmx.com (esa3.mentor.iphmx.com [68.232.137.180]) by sourceware.org (Postfix) with ESMTPS id 05936385481C for ; Mon, 23 Nov 2020 23:29:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 05936385481C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=joseph_myers@mentor.com IronPort-SDR: xyYgB3XdVxqRwTFM8GyssuMgiI9lkrl5WCNDOz1TwI/gTlfscZiL3jn8Rk6sG2E6IIS2MaCJru eHV7HiLsfTg09BG4M8bzXDCulByXMctGIFcNo3auXOkz+NwmglRRpJqSc7/G9yiw1342K/nBnB uM1HiUq4GIhhOTgjzxlZyXHHCmIoh9lUkqfs0xWzF4uAKmipXhn2lRUJmxBgS0ojhTGoy+JkkB BbijtUX1vTseoG2JNxY4ZMoY1vqQ8OWB2BoD0AGgWLPdQLVKQDEUw1iQfZFLmU0pth9FRI0W2h YPQ= X-IronPort-AV: E=Sophos;i="5.78,364,1599552000"; d="scan'208";a="55394373" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa3.mentor.iphmx.com with ESMTP; 23 Nov 2020 15:29:55 -0800 IronPort-SDR: HNHNJlWAEOzb/JWna9FRcjCcMaCuUaxTBiL7Aneq7hFKYU3s19FN2XLFp7fVM5iAMmrS0+0wTn IMi+hDMANOVSD1HJkF8+qr9ARYFooQZdefU0l6118FABYXBO0SQvlQjukLzJOEj1eFDErY7H/A 9ha1lknICLSTdTWFH28LDKUJCsUnpZsvjwP/imnWz3wJ42yqI7NKruEK8NDFfi0i0pnRyuIrTl 0yuANzJKUrk32CGg/ZOlkPr2mKm6i9Fr9VXesMBMJ1CB4RocJQTp93E8/F4hm2M9PQ1c/UC8P2 Qwg= Date: Mon, 23 Nov 2020 23:29:51 +0000 From: Joseph Myers X-X-Sender: jsm28@digraph.polyomino.org.uk To: Subject: c: Allow comparison of pointers to complete and incomplete types for C11 [PR95630] Message-ID: User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: SVR-IES-MBX-07.mgc.mentorg.com (139.181.222.7) To svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) X-Spam-Status: No, score=-3131.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 23 Nov 2020 23:29:58 -0000 As noted in bug 95630, C11 removed a restriction in C99 on comparing pointers to compatible complete and incomplete types (this was one of the changes in N1439, which was largely a terminological change to make incomplete types a subset of object types rather than a different kind of type). Implement that change by using pedwarn_c99 with OPT_Wpedantic for this diagnostic. Bootstrapped with no regressions for x86_64-pc-linux-gnu. Applied to mainline. gcc/c/ 2020-11-23 Joseph Myers PR c/95630 * c-typeck.c (build_binary_op): Use pedwarn_c99 with OPT_Wpedantic for comparisons of complete and incomplete pointers. gcc/testsuite/ 2020-11-23 Joseph Myers PR c/95630 * gcc.dg/c11-compare-incomplete-1.c, gcc.dg/c11-compare-incomplete-2.c, gcc.dg/c99-compare-incomplete-1.c, gcc.dg/c99-compare-incomplete-2.c: New tests. diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 286f3d9cd6c..cdc491a25fd 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -12266,8 +12266,8 @@ build_binary_op (location_t location, enum tree_code code, result_type = common_pointer_type (type0, type1); if (!COMPLETE_TYPE_P (TREE_TYPE (type0)) != !COMPLETE_TYPE_P (TREE_TYPE (type1))) - pedwarn (location, 0, - "comparison of complete and incomplete pointers"); + pedwarn_c99 (location, OPT_Wpedantic, + "comparison of complete and incomplete pointers"); else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE) pedwarn (location, OPT_Wpedantic, "ISO C forbids " "ordered comparisons of pointers to functions"); diff --git a/gcc/testsuite/gcc.dg/c11-compare-incomplete-1.c b/gcc/testsuite/gcc.dg/c11-compare-incomplete-1.c new file mode 100644 index 00000000000..b1c05cf221e --- /dev/null +++ b/gcc/testsuite/gcc.dg/c11-compare-incomplete-1.c @@ -0,0 +1,52 @@ +/* Test comparisons of pointers to complete and incomplete types are + accepted in C11 mode. */ +/* { dg-do compile } */ +/* { dg-options "-std=c11 -pedantic-errors" } */ + +int +f (int (*p)[], int (*q)[3]) +{ + return p < q; +} + +int +f2 (int (*p)[], int (*q)[3]) +{ + return p <= q; +} + +int +f3 (int (*p)[], int (*q)[3]) +{ + return p > q; +} + +int +f4 (int (*p)[], int (*q)[3]) +{ + return p >= q; +} + +int +g (int (*p)[], int (*q)[3]) +{ + return q < p; +} + +int +g2 (int (*p)[], int (*q)[3]) +{ + return q <= p; +} + +int +g3 (int (*p)[], int (*q)[3]) +{ + return q > p; +} + +int +g4 (int (*p)[], int (*q)[3]) +{ + return q >= p; +} diff --git a/gcc/testsuite/gcc.dg/c11-compare-incomplete-2.c b/gcc/testsuite/gcc.dg/c11-compare-incomplete-2.c new file mode 100644 index 00000000000..8e809e87e9a --- /dev/null +++ b/gcc/testsuite/gcc.dg/c11-compare-incomplete-2.c @@ -0,0 +1,52 @@ +/* Test comparisons of pointers to complete and incomplete types are + diagnosed in C11 mode with -Wc99-c11-compat. */ +/* { dg-do compile } */ +/* { dg-options "-std=c11 -pedantic-errors -Wc99-c11-compat" } */ + +int +f (int (*p)[], int (*q)[3]) +{ + return p < q; /* { dg-warning "complete and incomplete" } */ +} + +int +f2 (int (*p)[], int (*q)[3]) +{ + return p <= q; /* { dg-warning "complete and incomplete" } */ +} + +int +f3 (int (*p)[], int (*q)[3]) +{ + return p > q; /* { dg-warning "complete and incomplete" } */ +} + +int +f4 (int (*p)[], int (*q)[3]) +{ + return p >= q; /* { dg-warning "complete and incomplete" } */ +} + +int +g (int (*p)[], int (*q)[3]) +{ + return q < p; /* { dg-warning "complete and incomplete" } */ +} + +int +g2 (int (*p)[], int (*q)[3]) +{ + return q <= p; /* { dg-warning "complete and incomplete" } */ +} + +int +g3 (int (*p)[], int (*q)[3]) +{ + return q > p; /* { dg-warning "complete and incomplete" } */ +} + +int +g4 (int (*p)[], int (*q)[3]) +{ + return q >= p; /* { dg-warning "complete and incomplete" } */ +} diff --git a/gcc/testsuite/gcc.dg/c99-compare-incomplete-1.c b/gcc/testsuite/gcc.dg/c99-compare-incomplete-1.c new file mode 100644 index 00000000000..dfafc39145e --- /dev/null +++ b/gcc/testsuite/gcc.dg/c99-compare-incomplete-1.c @@ -0,0 +1,52 @@ +/* Test comparisons of pointers to complete and incomplete types are + diagnosed in C99 mode: -pedantic. */ +/* { dg-do compile } */ +/* { dg-options "-std=c99 -pedantic" } */ + +int +f (int (*p)[], int (*q)[3]) +{ + return p < q; /* { dg-warning "complete and incomplete" } */ +} + +int +f2 (int (*p)[], int (*q)[3]) +{ + return p <= q; /* { dg-warning "complete and incomplete" } */ +} + +int +f3 (int (*p)[], int (*q)[3]) +{ + return p > q; /* { dg-warning "complete and incomplete" } */ +} + +int +f4 (int (*p)[], int (*q)[3]) +{ + return p >= q; /* { dg-warning "complete and incomplete" } */ +} + +int +g (int (*p)[], int (*q)[3]) +{ + return q < p; /* { dg-warning "complete and incomplete" } */ +} + +int +g2 (int (*p)[], int (*q)[3]) +{ + return q <= p; /* { dg-warning "complete and incomplete" } */ +} + +int +g3 (int (*p)[], int (*q)[3]) +{ + return q > p; /* { dg-warning "complete and incomplete" } */ +} + +int +g4 (int (*p)[], int (*q)[3]) +{ + return q >= p; /* { dg-warning "complete and incomplete" } */ +} diff --git a/gcc/testsuite/gcc.dg/c99-compare-incomplete-2.c b/gcc/testsuite/gcc.dg/c99-compare-incomplete-2.c new file mode 100644 index 00000000000..5ae7f303e07 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c99-compare-incomplete-2.c @@ -0,0 +1,52 @@ +/* Test comparisons of pointers to complete and incomplete types are + diagnosed in C99 mode: -pedantic-errors. */ +/* { dg-do compile } */ +/* { dg-options "-std=c99 -pedantic-errors" } */ + +int +f (int (*p)[], int (*q)[3]) +{ + return p < q; /* { dg-error "complete and incomplete" } */ +} + +int +f2 (int (*p)[], int (*q)[3]) +{ + return p <= q; /* { dg-error "complete and incomplete" } */ +} + +int +f3 (int (*p)[], int (*q)[3]) +{ + return p > q; /* { dg-error "complete and incomplete" } */ +} + +int +f4 (int (*p)[], int (*q)[3]) +{ + return p >= q; /* { dg-error "complete and incomplete" } */ +} + +int +g (int (*p)[], int (*q)[3]) +{ + return q < p; /* { dg-error "complete and incomplete" } */ +} + +int +g2 (int (*p)[], int (*q)[3]) +{ + return q <= p; /* { dg-error "complete and incomplete" } */ +} + +int +g3 (int (*p)[], int (*q)[3]) +{ + return q > p; /* { dg-error "complete and incomplete" } */ +} + +int +g4 (int (*p)[], int (*q)[3]) +{ + return q >= p; /* { dg-error "complete and incomplete" } */ +} -- Joseph S. Myers joseph@codesourcery.com