https://gcc.gnu.org/g:90555b05465dff636e258e0fe9184feb5b949e0a
commit r16-8500-g90555b05465dff636e258e0fe9184feb5b949e0a Author: Jakub Jelinek <[email protected]> Date: Tue Apr 7 17:59:13 2026 +0200 c++: Mark reflected vars/parameters as read [PR124790] This PR complains about spurious -Wunused-but-set-* warnings when using reflections. E.g. there is no warning for variable just set and used in sizeof (var), but there is one when using size_of (^^var). If we reflect some variable or parameter, we can do all kinds of things with it, so setting preventing these warnings is IMHO desirable. 2026-04-07 Jakub Jelinek <[email protected]> PR c++/124790 * reflect.cc (get_reflection): When reflecting VAR_DECL or PARM_DECL, call mark_exp_read on it. * g++.dg/reflect/pr124790.C: New test. Reviewed-by: Jason Merrill <[email protected]> Diff: --- gcc/cp/reflect.cc | 4 ++++ gcc/testsuite/g++.dg/reflect/pr124790.C | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc index 6e3b8d2e9be0..127438019130 100644 --- a/gcc/cp/reflect.cc +++ b/gcc/cp/reflect.cc @@ -219,6 +219,10 @@ get_reflection (location_t loc, tree t, reflect_kind kind/*=REFLECT_UNDEF*/) instantiate it now to find out its type. */ if (!mark_used (t)) return error_mark_node; + /* Avoid -Wunused-but-set* warnings when a variable or parameter + is just set and reflected. */ + if (VAR_P (t) || TREE_CODE (t) == PARM_DECL) + mark_exp_read (t); } /* For injected-class-name, use the main variant so that comparing diff --git a/gcc/testsuite/g++.dg/reflect/pr124790.C b/gcc/testsuite/g++.dg/reflect/pr124790.C new file mode 100644 index 000000000000..9f31daa43f04 --- /dev/null +++ b/gcc/testsuite/g++.dg/reflect/pr124790.C @@ -0,0 +1,38 @@ +// PR c++/124790 +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection -Wunused-but-set-variable -Wunused-but-set-parameter" } + +consteval auto +foo () +{ + int a = 42; // { dg-bogus "variable 'a' set but not used" } + return ^^a; +} + +consteval auto +bar () +{ + int a = 42; // { dg-bogus "variable 'a' set but not used" } + a = 7; + a = 9; + return ^^a; +} + +consteval auto +baz (int a) // { dg-bogus "parameter 'a' set but not used" } +{ + return ^^a; +} + +consteval auto +qux (int a) // { dg-bogus "parameter 'a' set but not used" } +{ + a = 7; + a = 9; + return ^^a; +} + +constexpr auto a = foo (); +constexpr auto b = bar (); +constexpr auto c = baz (42); +constexpr auto d = qux (42);
