On Fri, Oct 30, 2026 at 9:46 PM Joseph Myers <[email protected]> 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.

Thanks for working on this.  I wonder how one would practically
use this to audit code for the time_t issues without patching
glibc headers though?  We're using a somewhat evolved variant
of the original prototype where it is just enabling a new -W flag.

I suppose the most practical solution would be to indeed patch
glibc headers but gate the attribute with some preprocessor
conditional like _TIME_T_NO_IMPLICIT_TRUNCATION and
have that set?

Thanks,
Richard.

>         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/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
> index aab9f2cb9e3b..cf6fe3fd4210 100644
> --- a/gcc/c-family/c-attribs.cc
> +++ b/gcc/c-family/c-attribs.cc
> @@ -169,6 +169,8 @@ static tree handle_signed_bool_precision_attribute (tree 
> *, tree, tree, int,
>  static tree handle_hardbool_attribute (tree *, tree, tree, int, bool *);
>  static tree handle_fd_arg_attribute (tree *, tree, tree, int, bool *);
>  static tree handle_flag_enum_attribute (tree *, tree, tree, int, bool *);
> +static tree handle_no_implicit_truncation_attribute (tree *, tree, tree, int,
> +                                                    bool *);
>  static tree handle_null_terminated_string_arg_attribute (tree *, tree, tree, 
> int, bool *);
>
>  static tree handle_btf_decl_tag_attribute (tree *, tree, tree, int, bool *);
> @@ -579,7 +581,9 @@ const struct attribute_spec c_common_gnu_attributes[] =
>    { "btf_type_tag",          1, 1, false, true, false, false,
>                               handle_btf_type_tag_attribute, NULL},
>    { "btf_decl_tag",          1, 1, true, false, false, false,
> -                             handle_btf_decl_tag_attribute, NULL}
> +                             handle_btf_decl_tag_attribute, NULL},
> +  { "no_implicit_truncation", 0, 0, false, true, false, false,
> +                             handle_no_implicit_truncation_attribute, NULL }
>  };
>
>  const struct scoped_attribute_specs c_common_gnu_attribute_table =
> @@ -4601,6 +4605,32 @@ handle_null_terminated_string_arg_attribute (tree 
> *node, tree name, tree args,
>    return NULL_TREE;
>  }
>
> +/* Handle the "no_implicit_truncation" attribute.  */
> +
> +static tree
> +handle_no_implicit_truncation_attribute (tree *node, tree name,
> +                                        tree ARG_UNUSED (args),
> +                                        int ARG_UNUSED (flags),
> +                                        bool *no_add_attrs)
> +{
> +  if (!INTEGRAL_TYPE_P (*node))
> +    {
> +      warning (OPT_Wattributes, "%qE attribute ignored on non-integer type",
> +              name);
> +      *no_add_attrs = true;
> +    }
> +  else if (TREE_CODE (*node) == BOOLEAN_TYPE
> +          || (TREE_CODE (*node) == ENUMERAL_TYPE
> +              && TREE_CODE (ENUM_UNDERLYING_TYPE (*node)) == BOOLEAN_TYPE))
> +    {
> +      warning (OPT_Wattributes, "%qE attribute ignored on boolean type",
> +              name);
> +      *no_add_attrs = true;
> +    }
> +
> +  return NULL_TREE;
> +}
> +
>  /* Common argument checking for btf_type_tag and btf_decl_tag.
>     Return true if the ARGS are valid, otherwise emit an error and
>     return false.  */
> diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc
> index a27dda88e128..f6b9abcee2d1 100644
> --- a/gcc/c-family/c-warn.cc
> +++ b/gcc/c-family/c-warn.cc
> @@ -1187,8 +1187,18 @@ conversion_warning (location_t loc, tree type, tree 
> expr, tree result)
>    tree expr_type = TREE_TYPE (expr);
>    enum conversion_safety conversion_kind;
>    int arith_ops = 0;
> -
> -  if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
> +  bool attr_expr = lookup_attribute ("no_implicit_truncation",
> +                                    TYPE_ATTRIBUTES (TREE_TYPE (expr)));
> +  bool warn_for_attr = (warn_conversion_attribute
> +                       && attr_expr
> +                       && INTEGRAL_TYPE_P (type)
> +                       && (TYPE_PRECISION (type)
> +                           < TYPE_PRECISION (TREE_TYPE (expr))));
> +
> +  if (!warn_conversion
> +      && !warn_sign_conversion
> +      && !warn_float_conversion
> +      && !warn_for_attr)
>      return false;
>
>    /* This may happen, because for LHS op= RHS we preevaluate
> @@ -1356,7 +1366,9 @@ conversion_warning (location_t loc, tree type, tree 
> expr, tree result)
>         else if (conversion_kind == UNSAFE_SIGN)
>           warnopt = OPT_Wsign_conversion;
>         else if (conversion_kind)
> -         warnopt = OPT_Wconversion;
> +         warnopt = (warn_for_attr
> +                    ? OPT_Wconversion_attribute
> +                    : OPT_Wconversion);
>         else
>           break;
>
> @@ -1512,7 +1524,21 @@ warnings_for_convert_and_check (location_t loc, tree 
> type, tree expr,
>                     exprtype, type, expr);
>      }
>    else
> -    conversion_warning (loc, type, expr, result);
> +    {
> +      if (conversion_warning (loc, type, expr, result))
> +       return;
> +      /* Warn for widening conversions to types with no_implicit_truncation
> +        attribute.  This is intended for cases such as code that obtains a
> +        time_t value using atoi and so is unable to handle the full range of
> +        such values.  */
> +      if (lookup_attribute ("no_implicit_truncation", TYPE_ATTRIBUTES (type))
> +         && INTEGRAL_TYPE_P (TREE_TYPE (expr))
> +         && TREE_CODE (expr) != INTEGER_CST
> +         && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (expr)))
> +       warning_at (loc, OPT_Wconversion_attribute,
> +                   "conversion to %qT from narrower integer type",
> +                   type);
> +    }
>  }
>
>  /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> index d5b973baa855..d228206b54c1 100644
> --- a/gcc/c-family/c.opt
> +++ b/gcc/c-family/c.opt
> @@ -607,6 +607,10 @@ Wconversion
>  C ObjC C++ ObjC++ Var(warn_conversion) Warning
>  Warn for implicit type conversions that may change a value.
>
> +Wconversion-attribute
> +C ObjC C++ ObjC++ Var(warn_conversion_attribute) Init(1) Warning
> +Warn for implicit type conversions between types with the 
> no_implicit_truncation attribute and narrower integer types.
> +
>  Wconversion-null
>  C++ ObjC++ Var(warn_conversion_null) Init(1) Warning
>  Warn for converting NULL from/to a non-pointer type.
> diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
> index 2d0200858331..ea0731ccf872 100644
> --- a/gcc/c/c-typeck.cc
> +++ b/gcc/c/c-typeck.cc
> @@ -1189,10 +1189,29 @@ common_pointer_type (tree t1, tree t2, tree cond)
>    return c_build_type_attribute_variant (t1, attributes);
>  }
>
> +/* Return TYPE, with the no_implicit_truncation attribute added if
> +   NO_IMPLICIT_TRUNCATION.  */
> +
> +static tree
> +c_maybe_no_implicit_truncation (tree type, bool no_implicit_truncation)
> +{
> +  if (no_implicit_truncation)
> +    {
> +      tree attrs = tree_cons (get_identifier ("no_implicit_truncation"),
> +                             NULL_TREE,
> +                             TYPE_ATTRIBUTES (type));
> +      return c_build_type_attribute_variant (type, attrs);
> +    }
> +  else
> +    return type;
> +}
> +
>  /* Return the common type for two arithmetic types under the usual
>     arithmetic conversions.  The default conversions have already been
>     applied, and enumerated types converted to their compatible integer
> -   types.  The resulting type is unqualified and has no attributes.
> +   types.  The resulting type is unqualified and has no attributes,
> +   except for no_implicit_truncation if both types are integer types and at
> +   least one has that attribute.
>
>     This is the type for the result of most arithmetic operations
>     if the operands have the given two types.  */
> @@ -1202,6 +1221,12 @@ c_common_type (tree t1, tree t2)
>  {
>    enum tree_code code1;
>    enum tree_code code2;
> +  bool no_implicit_truncation
> +    = (INTEGRAL_TYPE_P (t1)
> +       && INTEGRAL_TYPE_P (t2)
> +       && (lookup_attribute ("no_implicit_truncation", TYPE_ATTRIBUTES (t1))
> +          || lookup_attribute ("no_implicit_truncation",
> +                               TYPE_ATTRIBUTES (t2))));
>
>    /* If one type is nonsense, use the other.  */
>    if (t1 == error_mark_node)
> @@ -1229,7 +1254,8 @@ c_common_type (tree t1, tree t2)
>
>    /* Save time if the two types are the same.  */
>
> -  if (t1 == t2) return t1;
> +  if (t1 == t2)
> +    return c_maybe_no_implicit_truncation (t1, no_implicit_truncation);
>
>    code1 = TREE_CODE (t1);
>    code2 = TREE_CODE (t2);
> @@ -1416,9 +1442,9 @@ c_common_type (tree t1, tree t2)
>    /* Both real or both integers; use the one with greater precision.  */
>
>    if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
> -    return t1;
> +    return c_maybe_no_implicit_truncation (t1, no_implicit_truncation);
>    else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
> -    return t2;
> +    return c_maybe_no_implicit_truncation (t2, no_implicit_truncation);
>
>    /* Same precision.  Prefer long longs to longs to ints when the
>       same precision, following the C99 rules on integer type rank
> @@ -1426,20 +1452,24 @@ c_common_type (tree t1, tree t2)
>
>    if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
>        || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
> -    return long_long_unsigned_type_node;
> +    return c_maybe_no_implicit_truncation (long_long_unsigned_type_node,
> +                                          no_implicit_truncation);
>
>    if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
>        || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
>      {
>        if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
> -       return long_long_unsigned_type_node;
> +       return c_maybe_no_implicit_truncation (long_long_unsigned_type_node,
> +                                              no_implicit_truncation);
>        else
> -       return long_long_integer_type_node;
> +       return c_maybe_no_implicit_truncation (long_long_integer_type_node,
> +                                              no_implicit_truncation);
>      }
>
>    if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
>        || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
> -    return long_unsigned_type_node;
> +    return c_maybe_no_implicit_truncation (long_unsigned_type_node,
> +                                          no_implicit_truncation);
>
>    if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
>        || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
> @@ -1447,9 +1477,11 @@ c_common_type (tree t1, tree t2)
>        /* But preserve unsignedness from the other type,
>          since long cannot hold all the values of an unsigned int.  */
>        if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
> -       return long_unsigned_type_node;
> +       return c_maybe_no_implicit_truncation (long_unsigned_type_node,
> +                                              no_implicit_truncation);
>        else
> -       return long_integer_type_node;
> +       return c_maybe_no_implicit_truncation (long_integer_type_node,
> +                                              no_implicit_truncation);
>      }
>
>    /* For floating types of the same TYPE_PRECISION (which we here
> @@ -1491,22 +1523,26 @@ c_common_type (tree t1, tree t2)
>      {
>        /* Prefer any other integral types over bit-precise integer types.  */
>        if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
> -       return code1 == BITINT_TYPE ? t2 : t1;
> +       return c_maybe_no_implicit_truncation (code1 == BITINT_TYPE ? t2 : t1,
> +                                              no_implicit_truncation);
>        /* If BITINT_TYPE is unsigned and the other type is signed
>          non-BITINT_TYPE with the same precision, the latter has higher rank.
>          In that case:
>          Otherwise, both operands are converted to the unsigned integer type
>          corresponding to the type of the operand with signed integer type.  
> */
>        if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
> -       return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
> +       {
> +         tree ret = c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
> +         return c_maybe_no_implicit_truncation (ret, no_implicit_truncation);
> +       }
>      }
>
>    /* Otherwise prefer the unsigned one.  */
>
>    if (TYPE_UNSIGNED (t1))
> -    return t1;
> +    return c_maybe_no_implicit_truncation (t1, no_implicit_truncation);
>    else
> -    return t2;
> +    return c_maybe_no_implicit_truncation (t2, no_implicit_truncation);
>  }
>
>  /* Wrapper around c_common_type that is used by c-common.cc and other
> @@ -2804,6 +2840,9 @@ perform_integral_promotions (tree exp)
>  {
>    tree type = TREE_TYPE (exp);
>    enum tree_code code = TREE_CODE (type);
> +  bool no_implicit_truncation = lookup_attribute ("no_implicit_truncation",
> +                                                 TYPE_ATTRIBUTES (type));
> +  tree conv_type = NULL_TREE;
>
>    gcc_assert (INTEGRAL_TYPE_P (type));
>
> @@ -2811,49 +2850,50 @@ perform_integral_promotions (tree exp)
>       their underlying type.  */
>    if (code == ENUMERAL_TYPE)
>      {
> -      type = ENUM_UNDERLYING_TYPE (type);
> +      conv_type = type = ENUM_UNDERLYING_TYPE (type);
>        if (c_promoting_integer_type_p (type))
>         {
>           if (TYPE_UNSIGNED (type)
>               && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
> -           type = unsigned_type_node;
> +           conv_type = unsigned_type_node;
>           else
> -           type = integer_type_node;
> +           conv_type = integer_type_node;
>         }
> -
> -      return convert (type, exp);
>      }
> -
>    /* ??? This should no longer be needed now bit-fields have their
>       proper types.  */
> -  if (TREE_CODE (exp) == COMPONENT_REF
> +  else if (TREE_CODE (exp) == COMPONENT_REF
>        && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
>      {
>        if (BITINT_TYPE_P (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))))
>         {
>           tree btype = DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1));
>           if (TREE_CODE (btype) == BITINT_TYPE)
> -           return convert (btype, exp);
> +           conv_type = btype;
>           else
> -           return convert (ENUM_UNDERLYING_TYPE (btype), exp);
> +           conv_type = ENUM_UNDERLYING_TYPE (btype);
>         }
>        /* If it's thinner than an int, promote it like a
>          c_promoting_integer_type_p, otherwise leave it alone.  */
> -      if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
> -                           TYPE_PRECISION (integer_type_node)) < 0)
> -       return convert (integer_type_node, exp);
> +      else if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
> +                                TYPE_PRECISION (integer_type_node)) < 0)
> +       conv_type = integer_type_node;
>      }
>
> -  if (c_promoting_integer_type_p (type))
> +  if (conv_type == NULL_TREE && c_promoting_integer_type_p (type))
>      {
>        /* Preserve unsignedness if not really getting any wider.  */
>        if (TYPE_UNSIGNED (type)
>           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
> -       return convert (unsigned_type_node, exp);
> -
> -      return convert (integer_type_node, exp);
> +       conv_type = unsigned_type_node;
> +      else
> +       conv_type = integer_type_node;
>      }
>
> +  if (conv_type)
> +    return convert (c_maybe_no_implicit_truncation (conv_type,
> +                                                   no_implicit_truncation),
> +                   exp);
>    return exp;
>  }
>
> @@ -15369,6 +15409,28 @@ build_binary_op (location_t location, enum tree_code 
> code,
>
>        if (short_compare)
>         {
> +         /* These conversions do not go through
> +            warnings_for_convert_and_check, so warn here for widening
> +            conversions to types with no_implicit_truncation attribute.  */
> +         if (INTEGRAL_TYPE_P (TREE_TYPE (op0))
> +             && INTEGRAL_TYPE_P (TREE_TYPE (op1))
> +             && (TYPE_PRECISION (TREE_TYPE (op0))
> +                 != TYPE_PRECISION (TREE_TYPE (op1))))
> +           {
> +             tree t0 = op0, t1 = op1;
> +             if (TYPE_PRECISION (TREE_TYPE (op0))
> +                 < TYPE_PRECISION (TREE_TYPE (op1)))
> +               {
> +                 t0 = op1;
> +                 t1 = op0;
> +               }
> +             if (TREE_CODE (t1) != INTEGER_CST
> +                 && lookup_attribute ("no_implicit_truncation",
> +                                      TYPE_ATTRIBUTES (TREE_TYPE (t0))))
> +               warning_at (location, OPT_Wconversion_attribute,
> +                           "conversion to %qT from narrower integer type",
> +                           TREE_TYPE (t0));
> +           }
>           /* Don't write &op0, etc., because that would prevent op0
>              from being kept in a register.
>              Instead, make copies of the our local variables and
> @@ -15463,10 +15525,15 @@ build_binary_op (location_t location, enum 
> tree_code code,
>
>    if (!converted)
>      {
> +      int save_warn_conversion_attribute = warn_conversion_attribute;
> +      if ((common || shorten == -1) && !short_compare)
> +       warn_conversion_attribute = 0;
>        op0 = ep_convert_and_check (location, result_type, op0,
>                                   semantic_result_type);
>        op1 = ep_convert_and_check (location, result_type, op1,
>                                   semantic_result_type);
> +      if ((common || shorten == -1) && !short_compare)
> +       warn_conversion_attribute = save_warn_conversion_attribute;
>
>        /* This can happen if one operand has a vector type, and the other
>          has a different type.  */
> 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);
> +    }
> +
>    return type;
>  }
>
> diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
> index 0b200804fe99..b9e78e1aec68 100644
> --- a/gcc/cp/typeck.cc
> +++ b/gcc/cp/typeck.cc
> @@ -6955,6 +6955,8 @@ cp_build_binary_op (const op_location_t &location,
>    if (! converted)
>      {
>        warning_sentinel w (warn_sign_conversion, short_compare);
> +      warning_sentinel w_attr (warn_conversion_attribute,
> +                              (common || shorten == -1) && !short_compare);
>        if (!same_type_p (TREE_TYPE (op0), result_type))
>         op0 = cp_convert_and_check (result_type, op0, complain);
>        if (!same_type_p (TREE_TYPE (op1), result_type))
> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
> index 961fa27ba0eb..ba886b738569 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -3694,6 +3694,25 @@ This attribute can be applied to functions or 
> variables.
>  It prevents the entity from being
>  merged with another semantically equivalent function or variable.
>
> +@atindex @code{no_implicit_truncation}
> +@opindex Wconversion-attribute
> +@item no_implicit_truncation
> +This attribute applies to integer types.
> +
> +If @option{-Wconversion-attribute} (enabled by default) is enabled,
> +the compiler warns for implicit conversions from a type with this
> +attribute to a narrower integer type.  It also warns for implicit
> +conversions in the other direction (from a narrower integer type to a
> +type with this attribute) in (non-compound) assignment,
> +initialization, argument passing and function return, but not in
> +arithmetic.
> +
> +This attribute is intended to be applied by standard library headers
> +to types such as @code{time_t}, to detect code that stores a value of
> +such a type in a narrower type such as @code{int}, or uses such a
> +narrower type as intermediate storage for a value to be stored in the
> +type with the attribute.
> +
>  @atindex @code{no_instrument_function}
>  @cindex functions that should not be instrumented
>  @cindex function instrumentation
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index 60d7163eee35..706bc52a0b67 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -388,7 +388,8 @@ Objective-C and Objective-C++ Dialects}.
>  -Wclobbered  -Wcomment
>  -Wcompare-distinct-pointer-types
>  -Wno-complain-wrong-lang  -Wconstant-logical-operand
> --Wconversion  -Wno-coverage-mismatch  -Wno-cpp
> +-Wconversion  -Wno-conversion-attribute
> +-Wno-coverage-mismatch  -Wno-cpp
>  -Wdangling-else  -Wdangling-pointer  -Wdangling-pointer=@var{n}
>  -Wdate-time
>  -Wno-deprecated  -Wno-deprecated-declarations  -Wno-designated-init
> @@ -10351,6 +10352,13 @@ unsigned integers are disabled by default in C++ 
> unless
>  Warnings about conversion from arithmetic on a small type back to that
>  type are only given with @option{-Warith-conversion}.
>
> +@opindex Wconversion-attribute
> +@opindex Wno-conversion-attribute
> +@item -Wno-conversion-attribute
> +Do not warn about implicit conversions between a type such as
> +@code{time_t} with the @code{no_implicit_truncation} attribute and a
> +narrower integer type.  These warnings are enabled by default.
> +
>  @opindex Wdangling-else
>  @opindex Wno-dangling-else
>  @item -Wdangling-else
> diff --git a/gcc/testsuite/c-c++-common/no_implicit_truncation-1.c 
> b/gcc/testsuite/c-c++-common/no_implicit_truncation-1.c
> new file mode 100644
> index 000000000000..5647b2314d2e
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/no_implicit_truncation-1.c
> @@ -0,0 +1,188 @@
> +/* Test warnings for implicit conversions involving
> +   no_implicit_truncation types.  */
> +/* { dg-do compile } */
> +/* { dg-options "" { target c } } */
> +/* { dg-options "-std=c++23" { target c++ } } */
> +
> +typedef long long time_ll_t __attribute__ ((no_implicit_truncation));
> +typedef unsigned long long time_ull_t __attribute__ 
> ((no_implicit_truncation));
> +typedef long time_l_t __attribute__ ((no_implicit_truncation));
> +typedef unsigned long time_ul_t __attribute__ ((no_implicit_truncation));
> +typedef int time_i_t __attribute__ ((no_implicit_truncation));
> +typedef unsigned int time_ui_t __attribute__ ((no_implicit_truncation));
> +typedef short time_ss_t __attribute__ ((no_implicit_truncation));
> +
> +time_ll_t tll;
> +time_ull_t tull;
> +volatile time_ll_t vtll;
> +volatile time_ull_t vtull;
> +time_l_t tl;
> +time_ul_t tul;
> +time_i_t ti;
> +time_ui_t tui;
> +time_ss_t tss;
> +signed char sc;
> +int i;
> +unsigned int ui;
> +long l;
> +unsigned long ul;
> +long long ll;
> +unsigned long long ull;
> +float f;
> +double d;
> +
> +void fi (int);
> +void fui (unsigned int);
> +void ftll (time_ll_t);
> +
> +void
> +t0 ()
> +{
> +  i = tll; /* { dg-warning "conversion" } */
> +  i = tull; /* { dg-warning "conversion" } */
> +  ui = tll; /* { dg-warning "conversion" } */
> +  ui = tull; /* { dg-warning "conversion" } */
> +  i = vtll; /* { dg-warning "conversion" } */
> +  i = vtull; /* { dg-warning "conversion" } */
> +  ui = vtll; /* { dg-warning "conversion" } */
> +  ui = vtull; /* { dg-warning "conversion" } */
> +  i = tll + 1LL; /* { dg-warning "conversion" } */
> +  i = tull + 1LL; /* { dg-warning "conversion" } */
> +  ui = tll + 1LL; /* { dg-warning "conversion" } */
> +  ui = tull + 1LL; /* { dg-warning "conversion" } */
> +  i = vtll + 1LL; /* { dg-warning "conversion" } */
> +  i = vtull + 1LL; /* { dg-warning "conversion" } */
> +  ui = vtll + 1LL; /* { dg-warning "conversion" } */
> +  ui = vtull + 1LL; /* { dg-warning "conversion" } */
> +  i = 1LL + tll; /* { dg-warning "conversion" } */
> +  i = 1LL + tull; /* { dg-warning "conversion" } */
> +  ui = 1LL + tll; /* { dg-warning "conversion" } */
> +  ui = 1LL + tull; /* { dg-warning "conversion" } */
> +  fi (tll); /* { dg-warning "conversion" } */
> +  fi (tull); /* { dg-warning "conversion" } */
> +  fui (tll); /* { dg-warning "conversion" } */
> +  fui (tull); /* { dg-warning "conversion" } */
> +  fi (vtll); /* { dg-warning "conversion" } */
> +  fi (vtull); /* { dg-warning "conversion" } */
> +  fui (vtll); /* { dg-warning "conversion" } */
> +  fui (vtull); /* { dg-warning "conversion" } */
> +  int j0 = tll; /* { dg-warning "conversion" } */
> +  int j1 = tull; /* { dg-warning "conversion" } */
> +  unsigned int j2 = tll; /* { dg-warning "conversion" } */
> +  unsigned int j3 = tull; /* { dg-warning "conversion" } */
> +  int d = tll - vtll; /* { dg-warning "conversion" } */
> +  /* Warnings for widening conversions from non-constants.  */
> +  tll = i; /* { dg-warning "from narrower" } */
> +  tll = ui; /* { dg-warning "from narrower" } */
> +  tull = i; /* { dg-warning "from narrower" } */
> +  tull = ui; /* { dg-warning "from narrower" } */
> +  vtll = i; /* { dg-warning "from narrower" } */
> +  vtll = ui; /* { dg-warning "from narrower" } */
> +  vtull = i; /* { dg-warning "from narrower" } */
> +  vtull = ui; /* { dg-warning "from narrower" } */
> +  ftll (i); /* { dg-warning "from narrower" } */
> +  ftll (ui); /* { dg-warning "from narrower" } */
> +  /* Do not warn for widening conversions of one operand to arithmetic
> +     (for example, it is presumed OK to add an interval in seconds to
> +     a time, where the interval may be known to be small enough to be
> +     representable even though the time may not).  */
> +  tll = tll + i;
> +  tull -= i;
> +  vtll *= i;
> +  vtull = tull / i;
> +  tll &= ui;
> +  tll |= ui;
> +  tll ^= i;
> +  /* Do warn for widening conversions in comparisons or conditional
> +     expressions (where the narrower type is being implicitly used for
> +     a time, for example).  */
> +  tll = i ? tll : i; /* { dg-warning "from narrower" } */
> +  tull = i ? tull : ui; /* { dg-warning "from narrower" } */
> +  tll = i ? i : tll; /* { dg-warning "from narrower" } */
> +  tull = i ? ui : tull; /* { dg-warning "from narrower" } */
> +  i = tll < i; /* { dg-warning "from narrower" } */
> +  i = i < tll; /* { dg-warning "from narrower" } */
> +  i = tll <= ui; /* { dg-warning "from narrower" } */
> +  i = ui <= tll; /* { dg-warning "from narrower" } */
> +  i = tull > i; /* { dg-warning "from narrower" } */
> +  i = i > tull; /* { dg-warning "from narrower" } */
> +  i = tull >= ui; /* { dg-warning "from narrower" } */
> +  i = ui >= tull; /* { dg-warning "from narrower" } */
> +  i = tll == i; /* { dg-warning "from narrower" } */
> +  i = i == tll; /* { dg-warning "from narrower" } */
> +  i = tll != ui; /* { dg-warning "from narrower" } */
> +  i = ui != tll; /* { dg-warning "from narrower" } */
> +}
> +
> +int t1 () { return tll; } /* { dg-warning "conversion" } */
> +int t2 () { return tull; } /* { dg-warning "conversion" } */
> +unsigned int t3 () { return tll; } /* { dg-warning "conversion" } */
> +unsigned int t4 () { return tull; } /* { dg-warning "conversion" } */
> +
> +void
> +t5 ()
> +{
> +  /* No warning for converting in-range constants.  */
> +  i = (time_ll_t) 12345;
> +  tll = 12345;
> +  tull = 12345;
> +  vtll = 12345;
> +  vtull = 12345;
> +  tll = i ? tll : 6;
> +  tull = i ? tull : 5u;
> +  tll = i ? 4 : tll;
> +  tull = i ? 3u : tull;
> +  i = tll < 7;
> +  i = 7 < ll;
> +  i = tll <= 50u;
> +  i = 50u <= tll;
> +  i = tull > 30;
> +  i = 30 > tull;
> +  i = tull >= 10u;
> +  i = 10u >= tull;
> +  i = tll == 123;
> +  i = 123 == tll;
> +  i = tll != 234u;
> +  i = 234u != tll;
> +}
> +
> +void
> +t6 ()
> +{
> +  /* The attribute is preserved by integer promotions.  */
> +  sc = tss + tss; /* { dg-warning "conversion" } */
> +  /* The attribute is preserved by usual arithmetic conversions.  */
> +  i = ti + (1LL << 32); /* { dg-warning "conversion" } */
> +  i = (1LL << 32) + ti; /* { dg-warning "conversion" } */
> +  sc = tl + l; /* { dg-warning "conversion" } */
> +  sc = tl + ul; /* { dg-warning "conversion" } */
> +  sc = tul + l; /* { dg-warning "conversion" } */
> +  sc = tul + ul; /* { dg-warning "conversion" } */
> +  sc = l + tl; /* { dg-warning "conversion" } */
> +  sc = ul + tl; /* { dg-warning "conversion" } */
> +  sc = l + tul; /* { dg-warning "conversion" } */
> +  sc = ul + tul; /* { dg-warning "conversion" } */
> +  sc = tl + i; /* { dg-warning "conversion" } */
> +  sc = tl + ui; /* { dg-warning "conversion" } */
> +  sc = tul + i; /* { dg-warning "conversion" } */
> +  sc = tul + ui; /* { dg-warning "conversion" } */
> +  sc = i + tl; /* { dg-warning "conversion" } */
> +  sc = ui + tl; /* { dg-warning "conversion" } */
> +  sc = i + tul; /* { dg-warning "conversion" } */
> +  sc = ui + tul; /* { dg-warning "conversion" } */
> +  sc = ti + i; /* { dg-warning "conversion" } */
> +  sc = ti + ui; /* { dg-warning "conversion" } */
> +  sc = tui + i; /* { dg-warning "conversion" } */
> +  sc = tui + ui; /* { dg-warning "conversion" } */
> +  sc = i + ti; /* { dg-warning "conversion" } */
> +  sc = ui + ti; /* { dg-warning "conversion" } */
> +  sc = i + tui; /* { dg-warning "conversion" } */
> +  sc = ui + tui; /* { dg-warning "conversion" } */
> +}
> +
> +void
> +t7 ()
> +{
> +  /* The attribute is lost in mixed integer / floating-point arithmetic.  */
> +  float f = tll + d;
> +}
> diff --git a/gcc/testsuite/c-c++-common/no_implicit_truncation-2.c 
> b/gcc/testsuite/c-c++-common/no_implicit_truncation-2.c
> new file mode 100644
> index 000000000000..14accfd65fb0
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/no_implicit_truncation-2.c
> @@ -0,0 +1,19 @@
> +/* Test bad uses of no_implicit_truncation attribute.  */
> +/* { dg-do compile } */
> +/* { dg-options "" { target c } } */
> +/* { dg-options "-std=c++23" { target c++ } } */
> +
> +#include <stddef.h>
> +
> +double d __attribute__ ((no_implicit_truncation)); /* { dg-warning 
> "non-integer" } */
> +struct s { int a; };
> +struct s __attribute__ ((no_implicit_truncation)) x; /* { dg-warning 
> "non-integer" } */
> +union u { int a; };
> +union u __attribute__ ((no_implicit_truncation)) y; /* { dg-warning 
> "non-integer" } */
> +nullptr_t __attribute__ ((no_implicit_truncation)) n; /* { dg-warning 
> "non-integer" } */
> +typedef void *p __attribute__ ((no_implicit_truncation)); /* { dg-warning 
> "non-integer" } */
> +typedef void v __attribute__ ((no_implicit_truncation)); /* { dg-warning 
> "non-integer" } */
> +typedef int f () __attribute__ ((no_implicit_truncation)); /* { dg-warning 
> "non-integer" } */
> +typedef bool __attribute__ ((no_implicit_truncation)) b; /* { dg-warning 
> "boolean" } */
> +enum e : bool;
> +typedef enum e __attribute__ ((no_implicit_truncation)) be; /* { dg-warning 
> "boolean" } */
> diff --git a/gcc/testsuite/c-c++-common/no_implicit_truncation-3.c 
> b/gcc/testsuite/c-c++-common/no_implicit_truncation-3.c
> new file mode 100644
> index 000000000000..8d56a9a7c084
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/no_implicit_truncation-3.c
> @@ -0,0 +1,7 @@
> +/* Test warnings for implicit conversions involving
> +   no_implicit_truncation types are disabled by
> +   -Wno-conversion-attribute.  */
> +/* { dg-do compile } */
> +/* { dg-options "-Wno-conversion-attribute" } */
> +
> +#include "no_implicit_truncation-1.c"
> diff --git a/gcc/testsuite/gcc.dg/no_implicit_truncation-bitint-1.c 
> b/gcc/testsuite/gcc.dg/no_implicit_truncation-bitint-1.c
> new file mode 100644
> index 000000000000..6417d53dff60
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/no_implicit_truncation-bitint-1.c
> @@ -0,0 +1,50 @@
> +/* Test warnings for implicit conversions involving
> +   no_implicit_truncation types: _BitInt tests.  */
> +/* { dg-do compile { target bitint } } */
> +/* { dg-options "" } */
> +
> +typedef _BitInt (63) time_63_t __attribute__ ((no_implicit_truncation));
> +typedef unsigned _BitInt (63) time_u63_t __attribute__ 
> ((no_implicit_truncation));
> +typedef _BitInt (64) time_64_t __attribute__ ((no_implicit_truncation));
> +typedef unsigned _BitInt (64) time_u64_t __attribute__ 
> ((no_implicit_truncation));
> +
> +time_63_t t63;
> +time_u64_t tu63;
> +time_64_t t64;
> +time_u64_t tu64;
> +
> +_BitInt (62) b62;
> +_BitInt (63) b63;
> +unsigned _BitInt (63) ub63;
> +_BitInt (64) b64;
> +unsigned _BitInt (64) ub64;
> +
> +void
> +tb ()
> +{
> +  /* The attribute is preserved by usual arithmetic conversions.  */
> +  b62 = t64 + b64; /* { dg-warning "conversion" } */
> +  b62 = t64 + ub64; /* { dg-warning "conversion" } */
> +  b62 = tu64 + b64; /* { dg-warning "conversion" } */
> +  b62 = tu64 + ub64; /* { dg-warning "conversion" } */
> +  b62 = b64 + t64; /* { dg-warning "conversion" } */
> +  b62 = ub64 + t64; /* { dg-warning "conversion" } */
> +  b62 = b64 + tu64; /* { dg-warning "conversion" } */
> +  b62 = ub64 + tu64; /* { dg-warning "conversion" } */
> +  b62 = t64 + b63; /* { dg-warning "conversion" } */
> +  b62 = t64 + ub63; /* { dg-warning "conversion" } */
> +  b62 = tu64 + b63; /* { dg-warning "conversion" } */
> +  b62 = tu64 + ub63; /* { dg-warning "conversion" } */
> +  b62 = b63 + t64; /* { dg-warning "conversion" } */
> +  b62 = ub63 + t64; /* { dg-warning "conversion" } */
> +  b62 = b63 + tu64; /* { dg-warning "conversion" } */
> +  b62 = ub63 + tu64; /* { dg-warning "conversion" } */
> +  b62 = t63 + b63; /* { dg-warning "conversion" } */
> +  b62 = t63 + ub63; /* { dg-warning "conversion" } */
> +  b62 = tu63 + b63; /* { dg-warning "conversion" } */
> +  b62 = tu63 + ub63; /* { dg-warning "conversion" } */
> +  b62 = b63 + t63; /* { dg-warning "conversion" } */
> +  b62 = ub63 + t63; /* { dg-warning "conversion" } */
> +  b62 = b63 + tu63; /* { dg-warning "conversion" } */
> +  b62 = ub63 + tu63; /* { dg-warning "conversion" } */
> +}
>
> --
> Joseph S. Myers
> [email protected]
>

Reply via email to