aboutsummaryrefslogtreecommitdiff
path: root/gcc/analyzer/region-model.cc
diff options
context:
space:
mode:
authorAlexandre Oliva <oliva@adacore.com>2023-12-07 00:38:18 -0300
committerAlexandre Oliva <oliva@gnu.org>2023-12-07 00:38:18 -0300
commit3cbab07b08d2f3a3ed34b6ec12e67727c59d285c (patch)
tree33798ed79e792ea0ba76f6b6722f376586888335 /gcc/analyzer/region-model.cc
parent3d0f3382fa7b5677f35a9becf75ac436cd7eda7b (diff)
downloadgcc-3cbab07b08d2f3a3ed34b6ec12e67727c59d285c.zip
gcc-3cbab07b08d2f3a3ed34b6ec12e67727c59d285c.tar.gz
gcc-3cbab07b08d2f3a3ed34b6ec12e67727c59d285c.tar.bz2
analyzer: deal with -fshort-enums
On platforms that enable -fshort-enums by default, various switch-enum analyzer tests fail, because apply_constraints_for_gswitch doesn't expect the integral promotion type cast. I've arranged for the code to cope with those casts. for gcc/analyzer/ChangeLog * region-model.cc (has_nondefault_case_for_value_p): Take enumerate type as a parameter. (region_model::apply_constraints_for_gswitch): Cope with integral promotion type casts. for gcc/testsuite/ChangeLog * gcc.dg/analyzer/switch-short-enum-1.c: New. * gcc.dg/analyzer/switch-no-short-enum-1.c: New.
Diffstat (limited to 'gcc/analyzer/region-model.cc')
-rw-r--r--gcc/analyzer/region-model.cc27
1 files changed, 23 insertions, 4 deletions
diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc
index 2157ad2..6a7a8bc 100644
--- a/gcc/analyzer/region-model.cc
+++ b/gcc/analyzer/region-model.cc
@@ -5387,10 +5387,10 @@ has_nondefault_case_for_value_p (const gswitch *switch_stmt, tree int_cst)
has nondefault cases handling all values in the enum. */
static bool
-has_nondefault_cases_for_all_enum_values_p (const gswitch *switch_stmt)
+has_nondefault_cases_for_all_enum_values_p (const gswitch *switch_stmt,
+ tree type)
{
gcc_assert (switch_stmt);
- tree type = TREE_TYPE (gimple_switch_index (switch_stmt));
gcc_assert (TREE_CODE (type) == ENUMERAL_TYPE);
for (tree enum_val_iter = TYPE_VALUES (type);
@@ -5426,6 +5426,23 @@ apply_constraints_for_gswitch (const switch_cfg_superedge &edge,
{
tree index = gimple_switch_index (switch_stmt);
const svalue *index_sval = get_rvalue (index, ctxt);
+ bool check_index_type = true;
+
+ /* With -fshort-enum, there may be a type cast. */
+ if (ctxt && index_sval->get_kind () == SK_UNARYOP
+ && TREE_CODE (index_sval->get_type ()) == INTEGER_TYPE)
+ {
+ const unaryop_svalue *unaryop = as_a <const unaryop_svalue *> (index_sval);
+ if (unaryop->get_op () == NOP_EXPR
+ && is_a <const initial_svalue *> (unaryop->get_arg ()))
+ if (const initial_svalue *initvalop = (as_a <const initial_svalue *>
+ (unaryop->get_arg ())))
+ if (TREE_CODE (initvalop->get_type ()) == ENUMERAL_TYPE)
+ {
+ index_sval = initvalop;
+ check_index_type = false;
+ }
+ }
/* If we're switching based on an enum type, assume that the user is only
working with values from the enum. Hence if this is an
@@ -5437,12 +5454,14 @@ apply_constraints_for_gswitch (const switch_cfg_superedge &edge,
ctxt
/* Must be an enum value. */
&& index_sval->get_type ()
- && TREE_CODE (TREE_TYPE (index)) == ENUMERAL_TYPE
+ && (!check_index_type
+ || TREE_CODE (TREE_TYPE (index)) == ENUMERAL_TYPE)
&& TREE_CODE (index_sval->get_type ()) == ENUMERAL_TYPE
/* If we have a constant, then we can check it directly. */
&& index_sval->get_kind () != SK_CONSTANT
&& edge.implicitly_created_default_p ()
- && has_nondefault_cases_for_all_enum_values_p (switch_stmt)
+ && has_nondefault_cases_for_all_enum_values_p (switch_stmt,
+ index_sval->get_type ())
/* Don't do this if there's a chance that the index is
attacker-controlled. */
&& !ctxt->possibly_tainted_p (index_sval))