On 10/30/26 4:44 PM, Joseph Myers wrote:
Bug 96570 requests warnings for truncation of time_t values to
narrower integer types, to detect code that uses int as an
intermediate type for storing values originally obtained from the
system as time_t. This was extended in subsequent comments in that
bug to include a request for such warnings also for widening a
narrower integer type to time_t, to detect code that obtains a time
using interfaces such as atoi (from text in a file, over the network,
etc.) and then stores it in a time_t, since the interfaces used to
obtain the time in that case would also not work with times after
Y2038.
Richard prototyped such warnings specifically for time_t in
<https://gcc.gnu.org/pipermail/gcc-patches/2025-June/687651.html>, and
I noted in discussions of that patch that it should be based on an
attribute rather than specifically the time_t name; such warnings
might well make sense for some other types such as off_t where there
is also a risk of code using int inappropriately to store values of
such a type. (Actual addition of the attribute to any specific
standard library type would probably require distribution build
testing to see how large the impact is, and what the true / false
positive numbers are.)
This patch implements the warnings based on an attribute
no_implicit_truncation (only for non-boolean integer types), as
previously suggested, with the attribute being preserved by the usual
arithmetic conversions (and by the integer promotions, in the unlikely
case of being applied to a type narrower than int). For narrowing
conversions, the warnings are handled as a variant of -Wconversion
warnings (but enabled by default), thus using existing logic for such
warnings (and if smarts are added to know that certain conversions
cannot change a value, they would apply to both -Wconversion and the
new -Wconversion-attribute). For widening conversions, there is new
logic to check, which tries to avoid cases that are unlikely to be
problematic: widening conversions of constants to types with the new
attribute do not warn, and nor do conversions in arithmetic ("time +=
int_offset;" is presumed to be OK), but implicit widening conversions
in other contexts (comparisons, conditional expressions, "time =
int_value;") do warn.
The feature applies for both C and C++. Preservation of the attribute
in the usual arithmetic conversions is automatic for C++, where the
usual arithmetic conversions are already implemented to merge
attributes from the operands, but requires specific implementation for
C. This leads to a question for C++: should the attribute merging be
changed to *remove* the no_implicit_truncation attribute when either
type is not an integer type, to avoid it otherwise being applied
(meaninglessly) to floating types? Other questions to consider: what
further cases (that may already work) should be added to the
testcases? Should the initial implementation add smarts to avoid
warning for more cases such as "int secs = time % 60;" in particular,
or should such things be left to possible followups based on the
extent to which such cases are found to occur in real code?
Bootstrapped with no regressions for x86_64-pc-linux-gnu.
PR c/96570
gcc/
* doc/extend.texi (no_implicit_truncation): New attribute.
* doc/invoke.texi (-Wno-conversion-attribute): New option.
gcc/c-family/
* c-attribs.cc (handle_no_implicit_truncation_attribute): New
function.
(c_common_gnu_attributes): Add no_implicit_truncation.
* c-warn.cc (conversion_warning): Handle -Wconversion-attribute
warnings for narrowing conversion from type with
no_implicit_truncation attribute.
(warnings_for_convert_and_check): Warn for widening conversion
to type with no_implicit_truncation attribute.
* c.opt (Wconversion-attribute): New option.
gcc/c/
* c-typeck.cc (c_maybe_no_implicit_truncation): New function.
(c_common_type): Preserve no_implicit_truncation attribute when
both types are integer types.
(perform_integral_promotions): Preserve no_implicit_truncation
attribute.
(build_binary_op): Warn for widening conversion to
no_implicit_truncation type in comparisons. Disable warning for
such conversion in arithmetic.
gcc/cp/
* cvt.cc (type_promotes_to): Preserve no_implicit_truncation
attribute.
* typeck.cc (cp_build_binary_op): Disable -Wconversion-attribute
for conversion in arithmetic.
gcc/testsuite/
* c-c++-common/no_implicit_truncation-1.c:
c-c++-common/no_implicit_truncation-2.c,
c-c++-common/no_implicit_truncation-3.c,
gcc.dg/no_implicit_truncation-bitint-1.c: New tests.
diff --git a/gcc/cp/cvt.cc b/gcc/cp/cvt.cc
index 845719c20438..005ef74d670a 100644
--- a/gcc/cp/cvt.cc
+++ b/gcc/cp/cvt.cc
@@ -2022,6 +2022,8 @@ tree
type_promotes_to (tree type)
{
tree promoted_type;
+ bool no_implicit_truncation = lookup_attribute ("no_implicit_truncation",
+ TYPE_ATTRIBUTES (type));
if (type == error_mark_node)
return error_mark_node;
@@ -2098,6 +2100,14 @@ type_promotes_to (tree type)
else if (type == float_type_node)
type = double_type_node;
+ if (no_implicit_truncation)
+ {
+ tree attrs = tree_cons (get_identifier ("no_implicit_truncation"),
+ NULL_TREE,
+ TYPE_ATTRIBUTES (type));
+ type = cp_build_type_attribute_variant (type, attrs);
+ }
It seems odd to propagate this attribute to a larger type after
promotion. But I agree we want to keep it in the case where we return
the same type.
Maybe we want to drop setting type to the TYPE_MAIN_VARIANT? I don't
see the rationale for that, and it looks like the C front-end doesn't do it.
Jason