diff options
author | Jose E. Marchesi <jose.marchesi@oracle.com> | 2023-08-17 15:36:26 +0200 |
---|---|---|
committer | Jose E. Marchesi <jose.marchesi@oracle.com> | 2023-08-17 17:35:57 +0200 |
commit | e1f45bea2741c271efbc4c2f9dfad93cbcd644c0 (patch) | |
tree | 7a55999c59eda082fbb9bce6b1c0ebf82d104551 /gcc/c | |
parent | ee40bdbfb07c604a126069f05c1358c3d76f7812 (diff) | |
download | gcc-e1f45bea2741c271efbc4c2f9dfad93cbcd644c0.zip gcc-e1f45bea2741c271efbc4c2f9dfad93cbcd644c0.tar.gz gcc-e1f45bea2741c271efbc4c2f9dfad93cbcd644c0.tar.bz2 |
Add warning options -W[no-]compare-distinct-pointer-types
GCC emits pedwarns unconditionally when comparing pointers of
different types, for example:
int xdp_context (struct xdp_md *xdp)
{
void *data = (void *)(long)xdp->data;
__u32 *metadata = (void *)(long)xdp->data_meta;
__u32 ret;
if (metadata + 1 > data)
return 0;
return 1;
}
/home/jemarch/foo.c: In function ‘xdp_context’:
/home/jemarch/foo.c:15:20: warning: comparison of distinct pointer types lacks a cast
15 | if (metadata + 1 > data)
| ^
LLVM supports an option -W[no-]compare-distinct-pointer-types that can
be used in order to enable or disable the emission of such warnings.
It is enabled by default.
This patch adds the same options to GCC.
Documentation and testsuite updated included.
Regtested in x86_64-linu-gnu.
No regressions observed.
gcc/ChangeLog:
PR c/106537
* doc/invoke.texi (Option Summary): Mention
-Wcompare-distinct-pointer-types under `Warning Options'.
(Warning Options): Document -Wcompare-distinct-pointer-types.
gcc/c-family/ChangeLog:
PR c/106537
* c.opt (Wcompare-distinct-pointer-types): New option.
gcc/c/ChangeLog:
PR c/106537
* c-typeck.cc (build_binary_op): Warning on comparing distinct
pointer types only when -Wcompare-distinct-pointer-types.
gcc/testsuite/ChangeLog:
PR c/106537
* gcc.c-torture/compile/pr106537-1.c: New test.
* gcc.c-torture/compile/pr106537-2.c: Likewise.
* gcc.c-torture/compile/pr106537-3.c: Likewise.
Diffstat (limited to 'gcc/c')
-rw-r--r-- | gcc/c/c-typeck.cc | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 6f2fff5..e6ddf37 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -12772,7 +12772,7 @@ build_binary_op (location_t location, enum tree_code code, else /* Avoid warning about the volatile ObjC EH puts on decls. */ if (!objc_ok) - pedwarn (location, 0, + pedwarn (location, OPT_Wcompare_distinct_pointer_types, "comparison of distinct pointer types lacks a cast"); if (result_type == NULL_TREE) @@ -12912,8 +12912,8 @@ build_binary_op (location_t location, enum tree_code code, int qual = ENCODE_QUAL_ADDR_SPACE (as_common); result_type = build_pointer_type (build_qualified_type (void_type_node, qual)); - pedwarn (location, 0, - "comparison of distinct pointer types lacks a cast"); + pedwarn (location, OPT_Wcompare_distinct_pointer_types, + "comparison of distinct pointer types lacks a cast"); } } else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1)) |