* c-common.def (SWITCH_STMT): Add SWITCH_TYPE operand.
* c-common.h (SWITCH_TYPE): Define.
* c-typeck.c (c_start_case): Set SWITCH_TYPE.
* stmt.c (all_cases_count): Set lastval to thisval at end of loop.
Rename spareness variable to sparseness.
(expand_end_case_type): Renamed from expand_end_case, use orig_type
if non-NULL instead of TREE_TYPE (orig_index).
* tree.h (expand_end_case_type): Renamed from expand_end_case.
(expand_end_case): Define using expand_end_case_type.
* c-semantics.c (genrtl_switch_stmt): Pass SWITCH_TYPE
to expand_end_case_type.
* doc/c-tree.texi (SWITCH_STMT): Document SWITCH_TYPE.
* semantics.c (begin_switch_stmt): Clear SWITCH_TYPE.
(finish_switch_cond): Set SWITCH_TYPE.
* gcc.dg/Wswitch.c: Fix typos. Don't return unconditionally
before all tests. Move warning one line above to match where it
C frontend emits.
* gcc.dg/Wswitch-2.c: New test.
* g++.dg/warn/Wswitch-1.C: New test.
* g++.dg/warn/Wswitch-2.C: New test.
From-SVN: r49497
+2002-02-04 Jakub Jelinek <jakub@redhat.com>
+
+ PR c/4475, c++/3780:
+ * c-common.def (SWITCH_STMT): Add SWITCH_TYPE operand.
+ * c-common.h (SWITCH_TYPE): Define.
+ * c-typeck.c (c_start_case): Set SWITCH_TYPE.
+ * stmt.c (all_cases_count): Set lastval to thisval at end of loop.
+ Rename spareness variable to sparseness.
+ (expand_end_case_type): Renamed from expand_end_case, use orig_type
+ if non-NULL instead of TREE_TYPE (orig_index).
+ * tree.h (expand_end_case_type): Renamed from expand_end_case.
+ (expand_end_case): Define using expand_end_case_type.
+ * c-semantics.c (genrtl_switch_stmt): Pass SWITCH_TYPE
+ to expand_end_case_type.
+ * doc/c-tree.texi (SWITCH_STMT): Document SWITCH_TYPE.
+
2002-02-04 John David Anglin <dave@hiauly1.hia.nrc.ca>
* pa.h (PREFERRED_STACK_BOUNDARY): Define to match standard rounding.
DEFTREECODE (CONTINUE_STMT, "continue_stmt", 'e', 0)
/* Used to represent a 'switch' statement. The operands are
- SWITCH_COND and SWITCH_BODY, respectively. */
-DEFTREECODE (SWITCH_STMT, "switch_stmt", 'e', 2)
+ SWITCH_COND, SWITCH_BODY and SWITCH_TYPE, respectively. */
+DEFTREECODE (SWITCH_STMT, "switch_stmt", 'e', 3)
/* Used to represent a 'goto' statement. The operand is GOTO_DESTINATION. */
DEFTREECODE (GOTO_STMT, "goto_stmt", 'e', 1)
#define FOR_EXPR(NODE) TREE_OPERAND (FOR_STMT_CHECK (NODE), 2)
#define FOR_BODY(NODE) TREE_OPERAND (FOR_STMT_CHECK (NODE), 3)
-/* SWITCH_STMT accessors. These give access to the condition and body
+/* SWITCH_STMT accessors. These give access to the condition, body and
+ original condition type (before any compiler conversions)
of the switch statement, respectively. */
#define SWITCH_COND(NODE) TREE_OPERAND (SWITCH_STMT_CHECK (NODE), 0)
#define SWITCH_BODY(NODE) TREE_OPERAND (SWITCH_STMT_CHECK (NODE), 1)
+#define SWITCH_TYPE(NODE) TREE_OPERAND (SWITCH_STMT_CHECK (NODE), 2)
/* CASE_LABEL accessors. These give access to the high and low values
of a case label, respectively. */
emit_line_note (input_filename, lineno);
expand_start_case (1, cond, TREE_TYPE (cond), "switch statement");
expand_stmt (SWITCH_BODY (t));
- expand_end_case (cond);
+ expand_end_case_type (cond, SWITCH_TYPE (t));
}
/* Create a CASE_LABEL tree node and return it. */
tree exp;
{
enum tree_code code;
- tree type;
+ tree type, orig_type = error_mark_node;
struct c_switch *cs;
if (exp != error_mark_node)
{
code = TREE_CODE (TREE_TYPE (exp));
- type = TREE_TYPE (exp);
+ orig_type = TREE_TYPE (exp);
- if (! INTEGRAL_TYPE_P (type)
+ if (! INTEGRAL_TYPE_P (orig_type)
&& code != ERROR_MARK)
{
error ("switch quantity not an integer");
/* Add this new SWITCH_STMT to the stack. */
cs = (struct c_switch *) xmalloc (sizeof (*cs));
- cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, NULL_TREE);
+ cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
cs->cases = splay_tree_new (case_compare, NULL, NULL);
cs->next = switch_stack;
switch_stack = cs;
+2002-02-04 Jakub Jelinek <jakub@redhat.com>
+
+ * semantics.c (begin_switch_stmt): Clear SWITCH_TYPE.
+ (finish_switch_cond): Set SWITCH_TYPE.
+
2002-02-04 Richard Henderson <rth@redhat.com>
* method.c (use_thunk): Always initialize the block tree. Reindent.
begin_switch_stmt ()
{
tree r;
- r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE);
+ r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
add_stmt (r);
do_pushlevel ();
return r;
tree cond;
tree switch_stmt;
{
+ tree orig_type = NULL;
if (!processing_template_decl)
{
tree type;
error ("switch quantity not an integer");
cond = error_mark_node;
}
+ orig_type = TREE_TYPE (cond);
if (cond != error_mark_node)
{
cond = default_conversion (cond);
cond = index;
}
FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
+ SWITCH_TYPE (switch_stmt) = orig_type;
push_switch (switch_stmt);
}
the expression on which the switch is occurring. See the documentation
for an @code{IF_STMT} for more information on the representation used
for the condition. The @code{SWITCH_BODY} is the body of the switch
-statement.
+statement. The @code{SWITCH_TYPE} is the original type of switch
+expression as given in the source, before any compiler conversions.
@item TRY_BLOCK
Used to represent a @code{try} block. The body of the try block is
/* Returns the number of possible values of TYPE.
Returns -1 if the number is unknown, variable, or if the number does not
fit in a HOST_WIDE_INT.
- Sets *SPARENESS to 2 if TYPE is an ENUMERAL_TYPE whose values
+ Sets *SPARSENESS to 2 if TYPE is an ENUMERAL_TYPE whose values
do not increase monotonically (there may be duplicates);
to 1 if the values increase monotonically, but not always by 1;
otherwise sets it to 0. */
HOST_WIDE_INT
-all_cases_count (type, spareness)
+all_cases_count (type, sparseness)
tree type;
- int *spareness;
+ int *sparseness;
{
tree t;
HOST_WIDE_INT count, minval, lastval;
- *spareness = 0;
+ *sparseness = 0;
switch (TREE_CODE (type))
{
{
HOST_WIDE_INT thisval = tree_low_cst (TREE_VALUE (t), 0);
- if (*spareness == 2 || thisval < lastval)
- *spareness = 2;
+ if (*sparseness == 2 || thisval <= lastval)
+ *sparseness = 2;
else if (thisval != minval + count)
- *spareness = 1;
+ *sparseness = 1;
+ lastval = thisval;
count++;
}
}
/* Terminate a case (Pascal) or switch (C) statement
in which ORIG_INDEX is the expression to be tested.
+ If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
+ type as given in the source before any compiler conversions.
Generate the code to test it and jump to the right place. */
void
-expand_end_case (orig_index)
- tree orig_index;
+expand_end_case_type (orig_index, orig_type)
+ tree orig_index, orig_type;
{
tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE;
rtx default_label = 0;
index_expr = thiscase->data.case_stmt.index_expr;
index_type = TREE_TYPE (index_expr);
unsignedp = TREE_UNSIGNED (index_type);
+ if (orig_type == NULL)
+ orig_type = TREE_TYPE (orig_index);
do_pending_stack_adjust ();
No sense trying this if there's a default case, however. */
if (!thiscase->data.case_stmt.default_label
- && TREE_CODE (TREE_TYPE (orig_index)) == ENUMERAL_TYPE
+ && TREE_CODE (orig_type) == ENUMERAL_TYPE
&& TREE_CODE (index_expr) != INTEGER_CST)
- check_for_full_enumeration_handling (TREE_TYPE (orig_index));
+ check_for_full_enumeration_handling (orig_type);
/* If we don't have a default-label, create one here,
after the body of the switch. */
default code is emitted. */
use_cost_table
- = (TREE_CODE (TREE_TYPE (orig_index)) != ENUMERAL_TYPE
+ = (TREE_CODE (orig_type) != ENUMERAL_TYPE
&& estimate_case_costs (thiscase->data.case_stmt.case_list));
balance_case_nodes (&thiscase->data.case_stmt.case_list, NULL);
emit_case_nodes (index, thiscase->data.case_stmt.case_list,
+2002-02-04 Jakub Jelinek <jakub@redhat.com>
+
+ * gcc.dg/Wswitch.c: Fix typos. Don't return unconditionally
+ before all tests. Move warning one line above to match where it
+ C frontend emits.
+ * gcc.dg/Wswitch-2.c: New test.
+ * g++.dg/warn/Wswitch-1.C: New test.
+ * g++.dg/warn/Wswitch-2.C: New test.
+
2002-02-04 Richard Henderson <rth@redhat.com>
* g++.dg/abi/offsetof.C: Fix size comparison.
--- /dev/null
+/* PR c/4475, PR c++/3780 */
+/* { dg-do compile } */
+/* { dg-options "-Wswitch" } */
+
+enum e { e1, e2 };
+
+int
+foo (int i, int j, enum e ei, enum e ej, enum e ek, enum e el,
+ enum e em, enum e en, enum e eo, enum e ep)
+{
+ switch (i)
+ {
+ case 1: return 1;
+ case 2: return 2;
+ }
+ switch (j)
+ {
+ case 3: return 4;
+ case 4: return 3;
+ default: break;
+ }
+ switch (ei)
+ { /* { dg-warning "enumeration value `e1' not handled in switch" "enum e1" { target *-*-* } 24 } */
+ } /* { dg-warning "enumeration value `e2' not handled in switch" "enum e2" } */
+ switch (ej)
+ {
+ default: break;
+ }
+ switch (ek)
+ {
+ case e1: return 1;
+ } /* { dg-warning "enumeration value `e2' not handled in switch" "enum e2" } */
+ switch (el)
+ {
+ case e1: return 1;
+ default: break;
+ }
+ switch (em)
+ {
+ case e1: return 1;
+ case e2: return 2;
+ }
+ switch (en)
+ {
+ case e1: return 1;
+ case e2: return 2;
+ default: break;
+ }
+ switch (eo)
+ {
+ case e1: return 1;
+ case e2: return 2;
+ case 3: return 3;
+ } /* { dg-warning "case value `3' not in enumerated type `e'" "excess 3" } */
+ switch (ep)
+ {
+ case e1: return 1;
+ case e2: return 2;
+ case 3: return 3;
+ default: break;
+ } /* Since there is a default, no warning about ``case 3'' */
+ return 0;
+}
--- /dev/null
+/* Further -Wswitch tests. */
+/* { dg-do compile } */
+/* { dg-options "-Wswitch" } */
+
+enum e { e1 = 0, e2 = 1, e3 = 1, e4 = 2 };
+
+int
+foo (enum e ei, int j)
+{
+ switch (ei)
+ {
+ case e1: return 1;
+ case e3: return 2;
+ case e4: return 3;
+ } /* No warning here since e2 has the same value as e3. */
+ switch (ei)
+ {
+ case e1: return 1;
+ case e2: return 2;
+ } /* { dg-warning "enumeration value `e4' not handled in switch" "enum e4" } */
+ switch ((int) ei)
+ {
+ case e1: return 1;
+ } /* No warning here since switch condition was cast to int. */
+ switch ((enum e) j)
+ {
+ case e2: return 1;
+ case e4: return 2;
+ } /* { dg-warning "enumeration value `e1' not handled in switch" "enum e1" } */
+ return 0;
+}
--- /dev/null
+/* Further -Wswitch tests. */
+/* { dg-do compile } */
+/* { dg-options "-Wswitch" } */
+
+enum e { e1 = 0, e2 = 1, e3 = 1, e4 = 2 };
+
+int
+foo (enum e ei, int j)
+{
+ switch (ei)
+ {
+ case e1: return 1;
+ case e3: return 2;
+ case e4: return 3;
+ } /* No warning here since e2 has the same value as e3. */
+ switch (ei)
+ {
+ case e1: return 1;
+ case e2: return 2;
+ } /* { dg-warning "enumeration value `e4' not handled in switch" "enum e4" } */
+ switch ((int) ei)
+ {
+ case e1: return 1;
+ } /* No warning here since switch condition was cast to int. */
+ switch ((enum e) j)
+ {
+ case e2: return 1;
+ case e4: return 2;
+ } /* { dg-warning "enumeration value `e1' not handled in switch" "enum e1" } */
+ return 0;
+}
-/* PR gcc/4475, PR gcc/3780 */
+/* PR c/4475, PR c++/3780 */
/* { dg-do compile } */
/* { dg-options "-Wswitch" } */
{
case 3: return 4;
case 4: return 3;
- default: return 7;
+ default: break;
}
switch (ei)
- { /* { dg-warning "enumeration value `e1' not handled in switch" "enum e1" { target *-*-* } 24 } */
- } /* { dg-warning "enumeration value `e2' not handled in switch" "enum e2" } */
+ { /* { dg-warning "enumeration value `e1' not handled in switch" "enum e1" } */
+ } /* { dg-warning "enumeration value `e2' not handled in switch" "enum e2" { target *-*-* } 23 } */
switch (ej)
{
default: break;
switch (ek)
{
case e1: return 1;
- } /* { dg-warning "enumeration value `e2' not handled in switch" "enum e1" } */
+ } /* { dg-warning "enumeration value `e2' not handled in switch" "enum e2" } */
switch (el)
{
case e1: return 1;
case e2: return 2;
case 3: return 3;
default: break;
- } /* Since there is a default, no warning about ``case 3'' } */
+ } /* Since there is a default, no warning about ``case 3'' */
return 0;
}
extern tree last_cleanup_this_contour PARAMS ((void));
extern void expand_start_case PARAMS ((int, tree, tree,
const char *));
-extern void expand_end_case PARAMS ((tree));
+extern void expand_end_case_type PARAMS ((tree, tree));
+#define expand_end_case(cond) expand_end_case_type (cond, NULL)
extern int add_case_node PARAMS ((tree, tree,
tree, tree *));
extern int pushcase PARAMS ((tree,