https://gcc.gnu.org/g:6216bcd69d4adaa0519d84159805dfcb1d1e8e4e

commit r17-6645-g6216bcd69d4adaa0519d84159805dfcb1d1e8e4e
Author: Torbjörn SVENSSON <[email protected]>
Date:   Sun Nov 22 17:55:06 2026 +0100

    analyzer: fix -fshort-enums false positive in switch on enum
    
    Since r17-6246-g7647c00b77ee1a, with -fshort-enums, the analyzer could
    fail to see that a switch is on an enum value. It then assumed the
    implicit default case could be taken, which led to bogus "use of
    uninitialized value" warnings. This fixes:
    
    FAIL: gcc.dg/analyzer/torture/switch-enum-pr105273-doom-p_maputl.c -O0  
(test for bogus messages, line 83)
    
    gcc/analyzer/ChangeLog:
    
            * ops.cc (switch_case_op::apply_constraints): Handle widening
            cast of enum with -fshort-enums.
    
    Signed-off-by: Torbjörn SVENSSON <[email protected]>

Diff:
---
 gcc/analyzer/ops.cc | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/gcc/analyzer/ops.cc b/gcc/analyzer/ops.cc
index 5b3f700cc886..ae3e7cf1c491 100644
--- a/gcc/analyzer/ops.cc
+++ b/gcc/analyzer/ops.cc
@@ -1838,21 +1838,21 @@ apply_constraints (const superedge *,
   const svalue *index_sval = model.get_rvalue (index, ctxt);
   bool check_index_type = true;
 
-  /* With -fshort-enum, there may be a type cast.  */
+  /* With -fshort-enums, 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);
+      const svalue *arg = unaryop->get_arg ();
       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 (initvalop->get_type ()
-             && TREE_CODE (initvalop->get_type ()) == ENUMERAL_TYPE)
-           {
-             index_sval = initvalop;
-             check_index_type = false;
-           }
+         && arg->get_type ()
+         && TREE_CODE (arg->get_type ()) == ENUMERAL_TYPE
+         && (TYPE_PRECISION (arg->get_type ())
+             <= TYPE_PRECISION (index_sval->get_type ())))
+       {
+         index_sval = arg;
+         check_index_type = false;
+       }
     }
 
   /* If we're switching based on an enum type, assume that the user is only

Reply via email to