diff options
author | Martin Uecker <uecker@tugraz.at> | 2023-08-10 10:39:41 +0200 |
---|---|---|
committer | Martin Uecker <uecker@tugraz.at> | 2023-08-11 07:00:25 +0200 |
commit | 68783211f660a501dee203aec11dae99bf9808ba (patch) | |
tree | e41f8641ea6ae00fd8b2bf01129dc5bdc6b09687 | |
parent | ee8a844d02e39912e0a303bcf46a0584d144ad6a (diff) | |
download | gcc-68783211f660a501dee203aec11dae99bf9808ba.zip gcc-68783211f660a501dee203aec11dae99bf9808ba.tar.gz gcc-68783211f660a501dee203aec11dae99bf9808ba.tar.bz2 |
c: Support for -Wuseless-cast [PR84510]
Add support for Wuseless-cast C (and ObjC).
PR c/84510
gcc/c/:
* c-typeck.cc (build_c_cast): Add warning.
gcc/c-family/:
* c.opt: Enable warning for C and ObjC.
gcc/:
* doc/invoke.texi: Update.
gcc/testsuite/:
* gcc.dg/Wuseless-cast.c: New test.
-rw-r--r-- | gcc/c-family/c.opt | 2 | ||||
-rw-r--r-- | gcc/c/c-typeck.cc | 10 | ||||
-rw-r--r-- | gcc/doc/invoke.texi | 2 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Wuseless-cast.c | 26 |
4 files changed, 35 insertions, 5 deletions
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 0ed87fc..c7b567b 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1490,7 +1490,7 @@ C++ ObjC++ Var(warn_zero_as_null_pointer_constant) Warning Warn when a literal '0' is used as null pointer. Wuseless-cast -C++ ObjC++ Var(warn_useless_cast) Warning +C ObjC C++ ObjC++ Var(warn_useless_cast) Warning Warn about useless casts. Wsubobject-linkage diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 7cf4111..6f2fff5 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -6062,9 +6062,13 @@ build_c_cast (location_t loc, tree type, tree expr) if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value))) { - if (RECORD_OR_UNION_TYPE_P (type)) - pedwarn (loc, OPT_Wpedantic, - "ISO C forbids casting nonscalar to the same type"); + if (RECORD_OR_UNION_TYPE_P (type) + && pedwarn (loc, OPT_Wpedantic, + "ISO C forbids casting nonscalar to the same type")) + ; + else if (warn_useless_cast) + warning_at (loc, OPT_Wuseless_cast, + "useless cast to type %qT", type); /* Convert to remove any qualifiers from VALUE's type. */ value = convert (type, value); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index ef5162d..5bfdd6e 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -4773,7 +4773,7 @@ pointers after reallocation. @opindex Wuseless-cast @opindex Wno-useless-cast -@item -Wuseless-cast @r{(C++ and Objective-C++ only)} +@item -Wuseless-cast @r{(C, Objective-C, C++ and Objective-C++ only)} Warn when an expression is cast to its own type. This warning does not occur when a class object is converted to a non-reference type as that is a way to create a temporary: diff --git a/gcc/testsuite/gcc.dg/Wuseless-cast.c b/gcc/testsuite/gcc.dg/Wuseless-cast.c new file mode 100644 index 0000000..86e8758 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wuseless-cast.c @@ -0,0 +1,26 @@ +/* { dg-do compile } */ +/* { dg-options "-Wuseless-cast" } */ + +void foo(void) +{ + // casts to the same type + int i = 0; + const int ic = 0; + struct foo { int x; } x = { 0 }; + int q[3]; + (int)ic; /* { dg-warning "useless cast" } */ + (int)i; /* { dg-warning "useless cast" } */ + (const int)ic; /* { dg-warning "useless cast" } */ + (const int)i; /* { dg-warning "useless cast" } */ + (struct foo)x; /* { dg-warning "useless cast" } */ + (int(*)[3])&q; /* { dg-warning "useless cast" } */ + (_Atomic(int))i; /* { dg-warning "useless cast" } */ + + // not the same + int n = 3; + (int(*)[n])&q; // no warning + int j = (int)0UL; + enum X { A = 1 } xx = { A }; + enum Y { B = 1 } yy = (enum Y)xx; +} + |