This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [C++] simplify check_for_casting_away_constness
On 22/02/2008, Mark Mitchell <mark@codesourcery.com> wrote:
> Manuel López-Ibáñez wrote:
>
> > * typeck.c (enum cast_kind): New.
>
>
> Why not just use the existing TREE_CODE values here? Like
> REINTERPRET_CAST_EXPR?
Because I was not aware of that those existed at all.
Bootstrapped and regression tested on x86_64-unknown-linux-gnu.
2008-02-23 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
* typeck.c (check_for_casting_away_constness): Use 1 single argument, the type
of cast, to decide
what diagnostics generate.
(build_static_cast_1): Remove unused code. Update call to
check_for_casting_away_constness.
(build_reinterpret_cast_1): Update call to check_for_casting_away_constness.
(build_const_cast_1): Update call to check_for_casting_away_constness.
Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c (revision 132503)
+++ gcc/cp/typeck.c (working copy)
@@ -4840,22 +4840,40 @@ build_compound_expr (tree lhs, tree rhs)
return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
}
/* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
- casts away constness. DIAG_FN gives the function to call if we
- need to issue a diagnostic; if it is NULL, no diagnostic will be
- issued. DESCRIPTION explains what operation is taking place. */
+ casts away constness. CAST gives the type of cast. */
static void
check_for_casting_away_constness (tree src_type, tree dest_type,
- void (*diag_fn)(const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2),
- const char *description)
+ enum tree_code cast)
{
- if (diag_fn && casts_away_constness (src_type, dest_type))
- diag_fn ("%s from type %qT to type %qT casts away constness",
- description, src_type, dest_type);
+ /* C-style casts are allowed to cast away constness. With
+ WARN_CAST_QUAL, we still want to issue a warning. */
+ if (cast == CAST_EXPR && !warn_cast_qual) return;
+
+ if (casts_away_constness (src_type, dest_type))
+ switch (cast)
+ {
+ case CAST_EXPR:
+ warning (OPT_Wcast_qual, "cast from type %qT to type %qT casts away constness",
+ src_type, dest_type);
+ return;
+
+ case STATIC_CAST_EXPR:
+ error ("static_cast from type %qT to type %qT casts away constness",
+ src_type, dest_type);
+ return;
+
+ case REINTERPRET_CAST_EXPR:
+ error ("reinterpret_cast from type %qT to type %qT casts away constness",
+ src_type, dest_type);
+ return;
+ default:
+ gcc_unreachable();
+ }
}
/* Convert EXPR (an expression with pointer-to-member type) to TYPE
(another pointer-to-member type in the same hierarchy) and return
the converted expression. If ALLOW_INVERSE_P is permitted, a
@@ -4937,36 +4955,19 @@ build_static_cast_1 (tree type, tree exp
bool *valid_p)
{
tree intype;
tree result;
tree orig;
- void (*diag_fn)(const char*, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
- const char *desc;
/* Assume the cast is valid. */
*valid_p = true;
intype = TREE_TYPE (expr);
/* Save casted types in the function's used types hash table. */
used_types_insert (type);
- /* Determine what to do when casting away constness. */
- if (c_cast_p)
- {
- /* C-style casts are allowed to cast away constness. With
- WARN_CAST_QUAL, we still want to issue a warning. */
- diag_fn = warn_cast_qual ? warning0 : NULL;
- desc = "cast";
- }
- else
- {
- /* A static_cast may not cast away constness. */
- diag_fn = error;
- desc = "static_cast";
- }
-
/* [expr.static.cast]
An lvalue of type "cv1 B", where B is a class type, can be cast
to type "reference to cv2 D", where D is a class derived (clause
_class.derived_) from B, if a valid standard conversion from
@@ -5087,11 +5088,11 @@ build_static_cast_1 (tree type, tree exp
(TREE_TYPE (type)))))
{
tree base;
if (!c_cast_p)
- check_for_casting_away_constness (intype, type, diag_fn, desc);
+ check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
c_cast_p ? ba_unique : ba_check,
NULL);
return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
}
@@ -5122,12 +5123,11 @@ build_static_cast_1 (tree type, tree exp
t2 = type;
}
if (can_convert (t1, t2) || can_convert (t2, t1))
{
if (!c_cast_p)
- check_for_casting_away_constness (intype, type, diag_fn,
- desc);
+ check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
c_cast_p);
}
}
@@ -5140,11 +5140,11 @@ build_static_cast_1 (tree type, tree exp
if (TREE_CODE (intype) == POINTER_TYPE
&& VOID_TYPE_P (TREE_TYPE (intype))
&& TYPE_PTROB_P (type))
{
if (!c_cast_p)
- check_for_casting_away_constness (intype, type, diag_fn, desc);
+ check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR);
return build_nop (type, expr);
}
*valid_p = false;
return error_mark_node;
@@ -5325,12 +5325,11 @@ build_reinterpret_cast_1 (tree type, tre
|| (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
{
tree sexpr = expr;
if (!c_cast_p)
- check_for_casting_away_constness (intype, type, error,
- "reinterpret_cast");
+ check_for_casting_away_constness (intype, type, REINTERPRET_CAST_EXPR);
/* Warn about possible alignment problems. */
if (STRICT_ALIGNMENT && warn_cast_align
&& !VOID_TYPE_P (type)
&& TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
&& COMPLETE_TYPE_P (TREE_TYPE (type))
@@ -5481,14 +5480,11 @@ build_const_cast_1 (tree dst_type, tree
if (valid_p)
{
*valid_p = true;
/* This cast is actually a C-style cast. Issue a warning if
the user is making a potentially unsafe cast. */
- if (warn_cast_qual)
- check_for_casting_away_constness (src_type, dst_type,
- warning0,
- "cast");
+ check_for_casting_away_constness (src_type, dst_type, CAST_EXPR);
}
if (reference_type)
{
expr = build_unary_op (ADDR_EXPR, expr, 0);
expr = build_nop (reference_type, expr);