https://gcc.gnu.org/g:d8b4cd79d1a81023b29708d9f13d0642bc2545d0

commit d8b4cd79d1a81023b29708d9f13d0642bc2545d0
Author: Tobias Burnus <[email protected]>
Date:   Sat May 2 22:25:48 2026 +0200

    Fortran/OpenMP: cleanup gfc_free_omp_namelist
    
    Move the logic to deduce what needs to be freed from the
    caller to the callee by passing the OMP_LIST_... enum value
    instead of multiple bool arguments to gfc_free_omp_namelist.
    
    Additionally, add the name 'gfc_omp_list_type' to the existing
    OMP_LIST_... enum values and OMP_LIST_NONE (== OMP_LIST_NUM)
    as special value.
    
    As an enum is available, use it properly and replace 0 by
    OMP_LIST_FIRST in the list walks.
    
    gcc/fortran/ChangeLog:
    
            * gfortran.h (enum gfc_omp_list_type): Add this name
            to the existing OMP_LIST... enum; add OMP_LIST_NONE.
            (gfc_free_omp_namelist): Take that enum as arg instead of bool args.
            * match.cc (gfc_free_omp_namelist): Update.
            * openmp.cc (gfc_free_omp_clauses, 
gfc_free_omp_declare_variant_list,
            gfc_match_omp_clause_reduction, gfc_match_omp_clauses,
            gfc_match_omp_allocate, gfc_match_omp_flush,
            gfc_match_omp_declare_target, resolve_omp_clauses,
            gfc_resolve_omp_parallel_blocks, resolve_omp_do,
            gfc_resolve_oacc_blocks, gfc_resolve_oacc_declare): Update
            gfc_free_omp_namelist call and used enum type instead of
            int.
            * st.cc (gfc_free_statement): Likewise.
    
    Co-Authored-By: Julian Brown <[email protected]>
    (cherry picked from commit 357207648f16eeb6768e081d320bf131a33316fc)

Diff:
---
 gcc/fortran/gfortran.h |  7 ++--
 gcc/fortran/match.cc   |  9 +++--
 gcc/fortran/openmp.cc  | 95 ++++++++++++++++++++++++++------------------------
 gcc/fortran/st.cc      |  2 +-
 4 files changed, 61 insertions(+), 52 deletions(-)

diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index b0ce54e1c210..7b55442aa50b 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -1466,7 +1466,7 @@ gfc_omp_namelist;
 
 #define gfc_get_omp_namelist() XCNEW (gfc_omp_namelist)
 
-enum
+enum gfc_omp_list_type
 {
   OMP_LIST_FIRST,
   OMP_LIST_PRIVATE = OMP_LIST_FIRST,
@@ -1508,7 +1508,8 @@ enum
   OMP_LIST_DESTROY,
   OMP_LIST_INTEROP,
   OMP_LIST_ADJUST_ARGS,
-  OMP_LIST_NUM /* Must be the last.  */
+  OMP_LIST_NUM, /* Must be the last (together with OMP_LIST_NONE).  */
+  OMP_LIST_NONE = OMP_LIST_NUM
 };
 
 /* Because a symbol can belong to multiple namelists, they must be
@@ -3914,7 +3915,7 @@ void gfc_free_iterator (gfc_iterator *, int);
 void gfc_free_forall_iterator (gfc_forall_iterator *);
 void gfc_free_alloc_list (gfc_alloc *);
 void gfc_free_namelist (gfc_namelist *);
-void gfc_free_omp_namelist (gfc_omp_namelist *, bool, bool, bool, bool);
+void gfc_free_omp_namelist (gfc_omp_namelist *, enum gfc_omp_list_type);
 void gfc_free_equiv (gfc_equiv *);
 void gfc_free_equiv_until (gfc_equiv *, gfc_equiv *);
 void gfc_free_data (gfc_data *);
diff --git a/gcc/fortran/match.cc b/gcc/fortran/match.cc
index 07555536051f..bc09883ca2f4 100644
--- a/gcc/fortran/match.cc
+++ b/gcc/fortran/match.cc
@@ -6537,10 +6537,13 @@ gfc_free_namelist (gfc_namelist *name)
 /* Free an OpenMP namelist structure.  */
 
 void
-gfc_free_omp_namelist (gfc_omp_namelist *name, bool free_ns,
-                      bool free_align_allocator,
-                      bool free_mem_traits_space, bool free_init)
+gfc_free_omp_namelist (gfc_omp_namelist *name, enum gfc_omp_list_type list)
 {
+  bool free_ns = (list == OMP_LIST_AFFINITY || list == OMP_LIST_DEPEND);
+  bool free_align_allocator = (list == OMP_LIST_ALLOCATE);
+  bool free_mem_traits_space = (list == OMP_LIST_USES_ALLOCATORS);
+  bool free_init = (list == OMP_LIST_INIT);
+
   gfc_omp_namelist *n;
   gfc_expr *last_allocator = NULL;
   char *last_init_interop = NULL;
diff --git a/gcc/fortran/openmp.cc b/gcc/fortran/openmp.cc
index cf89ca9c24e1..f3bece743e40 100644
--- a/gcc/fortran/openmp.cc
+++ b/gcc/fortran/openmp.cc
@@ -180,12 +180,11 @@ gfc_match_omp_eos_error (void)
 void
 gfc_free_omp_clauses (gfc_omp_clauses *c)
 {
-  int i;
   if (c == NULL)
     return;
 
   gfc_free_expr (c->if_expr);
-  for (i = 0; i < OMP_IF_LAST; i++)
+  for (int i = 0; i < OMP_IF_LAST; i++)
     gfc_free_expr (c->if_exprs[i]);
   gfc_free_expr (c->self_expr);
   gfc_free_expr (c->final_expr);
@@ -214,12 +213,9 @@ gfc_free_omp_clauses (gfc_omp_clauses *c)
   gfc_free_expr (c->num_gangs_expr);
   gfc_free_expr (c->num_workers_expr);
   gfc_free_expr (c->vector_length_expr);
-  for (i = 0; i < OMP_LIST_NUM; i++)
-    gfc_free_omp_namelist (c->lists[i],
-                          i == OMP_LIST_AFFINITY || i == OMP_LIST_DEPEND,
-                          i == OMP_LIST_ALLOCATE,
-                          i == OMP_LIST_USES_ALLOCATORS,
-                          i == OMP_LIST_INIT);
+  for (enum gfc_omp_list_type t = OMP_LIST_FIRST; t < OMP_LIST_NUM;
+       t = gfc_omp_list_type (t + 1))
+    gfc_free_omp_namelist (c->lists[t], t);
   gfc_free_expr_list (c->wait_list);
   gfc_free_expr_list (c->tile_list);
   gfc_free_expr_list (c->sizes_list);
@@ -349,8 +345,7 @@ gfc_free_omp_declare_variant_list (gfc_omp_declare_variant 
*list)
       gfc_omp_declare_variant *current = list;
       list = list->next;
       gfc_free_omp_set_selector_list (current->set_selectors);
-      gfc_free_omp_namelist (current->adjust_args_list, false, false, false,
-                            false);
+      gfc_free_omp_namelist (current->adjust_args_list, OMP_LIST_NONE);
       free (current);
     }
 }
@@ -618,7 +613,7 @@ syntax:
   gfc_error ("Syntax error in OpenMP variable list at %C");
 
 cleanup:
-  gfc_free_omp_namelist (head, false, false, false, false);
+  gfc_free_omp_namelist (head, OMP_LIST_NONE);
   gfc_current_locus = old_loc;
   return MATCH_ERROR;
 }
@@ -708,7 +703,7 @@ syntax:
   gfc_error ("Syntax error in OpenMP variable list at %C");
 
 cleanup:
-  gfc_free_omp_namelist (head, false, false, false, false);
+  gfc_free_omp_namelist (head, OMP_LIST_NONE);
   gfc_current_locus = old_loc;
   return MATCH_ERROR;
 }
@@ -820,7 +815,7 @@ syntax:
   gfc_error ("Syntax error in OpenMP SINK dependence-type list at %C");
 
 cleanup:
-  gfc_free_omp_namelist (head, false, false, false, false);
+  gfc_free_omp_namelist (head, OMP_LIST_DEPEND);
   gfc_current_locus = old_loc;
   return MATCH_ERROR;
 }
@@ -1461,7 +1456,7 @@ gfc_match_omp_clause_reduction (char pc, gfc_omp_clauses 
*c, bool openacc,
     return MATCH_NO;
 
   locus old_loc = gfc_current_locus;
-  int list_idx = 0;
+  enum gfc_omp_list_type list_idx = OMP_LIST_NONE;
 
   if (pc == 'r' && !openacc)
     {
@@ -1471,13 +1466,13 @@ gfc_match_omp_clause_reduction (char pc, 
gfc_omp_clauses *c, bool openacc,
        list_idx = OMP_LIST_REDUCTION_TASK;
       else if (gfc_match ("default") == MATCH_YES)
        list_idx = OMP_LIST_REDUCTION;
-      if (list_idx != 0 && gfc_match (", ") != MATCH_YES)
+      if (list_idx != OMP_LIST_NONE && gfc_match (", ") != MATCH_YES)
        {
          gfc_error ("Comma expected at %C");
          gfc_current_locus = old_loc;
          return MATCH_NO;
        }
-      if (list_idx == 0)
+      if (list_idx == OMP_LIST_NONE)
        list_idx = OMP_LIST_REDUCTION;
     }
   else if (pc == 'i')
@@ -1593,7 +1588,7 @@ gfc_match_omp_clause_reduction (char pc, gfc_omp_clauses 
*c, bool openacc,
       *head = NULL;
       gfc_error_now ("!$OMP DECLARE REDUCTION %s not found at %L",
                     buffer, &old_loc);
-      gfc_free_omp_namelist (n, false, false, false, false);
+      gfc_free_omp_namelist (n, OMP_LIST_NONE);
     }
   else
     for (n = *head; n; n = n->next)
@@ -1909,7 +1904,7 @@ parse_next:
   return MATCH_YES;
 
 error:
-  gfc_free_omp_namelist (head, false, false, true, false);
+  gfc_free_omp_namelist (head, OMP_LIST_USES_ALLOCATORS);
   return MATCH_ERROR;
 }
 
@@ -2447,7 +2442,7 @@ gfc_match_omp_clauses (gfc_omp_clauses **cp, const 
omp_mask mask,
 
              if (end_colon && gfc_match (" %e )", &alignment) != MATCH_YES)
                {
-                 gfc_free_omp_namelist (*head, false, false, false, false);
+                 gfc_free_omp_namelist (*head, OMP_LIST_ALIGNED);
                  gfc_current_locus = old_loc;
                  *head = NULL;
                  break;
@@ -3487,7 +3482,7 @@ gfc_match_omp_clauses (gfc_omp_clauses **cp, const 
omp_mask mask,
                    end_colon = true;
                  else if (gfc_match (" )") != MATCH_YES)
                    {
-                     gfc_free_omp_namelist (*head, false, false, false, false);
+                     gfc_free_omp_namelist (*head, OMP_LIST_LINEAR);
                      gfc_current_locus = old_loc;
                      *head = NULL;
                      break;
@@ -3498,7 +3493,7 @@ gfc_match_omp_clauses (gfc_omp_clauses **cp, const 
omp_mask mask,
                {
                  if (gfc_match (" %e )", &step) != MATCH_YES)
                    {
-                     gfc_free_omp_namelist (*head, false, false, false, false);
+                     gfc_free_omp_namelist (*head, OMP_LIST_LINEAR);
                      gfc_current_locus = old_loc;
                      *head = NULL;
                      goto error;
@@ -3641,7 +3636,7 @@ gfc_match_omp_clauses (gfc_omp_clauses **cp, const 
omp_mask mask,
                    }
                  if (has_error)
                    {
-                     gfc_free_omp_namelist (*head, false, false, false, false);
+                     gfc_free_omp_namelist (*head, OMP_LIST_LINEAR);
                      *head = NULL;
                      goto error;
                    }
@@ -5319,7 +5314,7 @@ gfc_match_omp_allocate (void)
          gfc_error ("Unexpected expression as list item at %L in ALLOCATE "
                     "directive", &n->expr->where);
 
-       gfc_free_omp_namelist (vars, false, true, false, false);
+       gfc_free_omp_namelist (vars, OMP_LIST_ALLOCATE);
        goto error;
       }
 
@@ -5740,14 +5735,14 @@ gfc_match_omp_flush (void)
     {
       gfc_error ("List specified together with memory order clause in FLUSH "
                 "directive at %C");
-      gfc_free_omp_namelist (list, false, false, false, false);
+      gfc_free_omp_namelist (list, OMP_LIST_NONE);
       gfc_free_omp_clauses (c);
       return MATCH_ERROR;
     }
   if (gfc_match_omp_eos () != MATCH_YES)
     {
       gfc_error ("Unexpected junk after $OMP FLUSH statement at %C");
-      gfc_free_omp_namelist (list, false, false, false, false);
+      gfc_free_omp_namelist (list, OMP_LIST_NONE);
       gfc_free_omp_clauses (c);
       return MATCH_ERROR;
     }
@@ -6195,7 +6190,7 @@ gfc_match_omp_declare_target (void)
   locus old_loc;
   match m;
   gfc_omp_clauses *c = NULL;
-  int list;
+  enum gfc_omp_list_type list;
   gfc_omp_namelist *n;
   gfc_symbol *s;
 
@@ -6238,7 +6233,7 @@ gfc_match_omp_declare_target (void)
 
   gfc_buffer_error (false);
 
-  static const int to_enter_link_lists[]
+  static const enum gfc_omp_list_type to_enter_link_lists[]
     = { OMP_LIST_TO, OMP_LIST_ENTER, OMP_LIST_LINK, OMP_LIST_LOCAL };
   for (size_t listn = 0; listn < ARRAY_SIZE (to_enter_link_lists)
                         && (list = to_enter_link_lists[listn], true); ++listn)
@@ -8896,7 +8891,7 @@ resolve_omp_clauses (gfc_code *code, gfc_omp_clauses 
*omp_clauses,
 {
   gfc_omp_namelist *n, *last;
   gfc_expr_list *el;
-  int list;
+  enum gfc_omp_list_type list;
   int ifc;
   bool if_without_mod = false;
   gfc_omp_linear_op linear_op = OMP_LINEAR_DEFAULT;
@@ -9150,7 +9145,8 @@ resolve_omp_clauses (gfc_code *code, gfc_omp_clauses 
*omp_clauses,
 
   /* Check that no symbol appears on multiple clauses, except that
      a symbol can appear on both firstprivate and lastprivate.  */
-  for (list = 0; list < OMP_LIST_NUM; list++)
+  for (list = OMP_LIST_FIRST; list < OMP_LIST_NUM;
+       list = gfc_omp_list_type (list + 1))
     for (n = omp_clauses->lists[list]; n; n = n->next)
       {
        if (!n->sym)  /* omp_all_memory.  */
@@ -9238,7 +9234,8 @@ resolve_omp_clauses (gfc_code *code, gfc_omp_clauses 
*omp_clauses,
                   "REDUCTION clause at %L", loc);
     }
 
-  for (list = 0; list < OMP_LIST_NUM; list++)
+  for (list = OMP_LIST_FIRST; list < OMP_LIST_NUM;
+       list = gfc_omp_list_type (list + 1))
     if (list != OMP_LIST_FIRSTPRIVATE
        && list != OMP_LIST_LASTPRIVATE
        && list != OMP_LIST_ALIGNED
@@ -9345,7 +9342,8 @@ resolve_omp_clauses (gfc_code *code, gfc_omp_clauses 
*omp_clauses,
          }
     }
   if (code && (code->op == EXEC_OMP_INTEROP || code->op == EXEC_OMP_DISPATCH))
-    for (list = OMP_LIST_INIT; list <= OMP_LIST_INTEROP; list++)
+    for (list = OMP_LIST_INIT; list <= OMP_LIST_INTEROP;
+        list = gfc_omp_list_type (list + 1))
       for (n = omp_clauses->lists[list]; n; n = n->next)
        {
          if (n->sym->ts.type != BT_INTEGER
@@ -9377,7 +9375,8 @@ resolve_omp_clauses (gfc_code *code, gfc_omp_clauses 
*omp_clauses,
                 n->sym->name, &n->where);
 
   gcc_assert (OMP_LIST_LASTPRIVATE == OMP_LIST_FIRSTPRIVATE + 1);
-  for (list = OMP_LIST_FIRSTPRIVATE; list <= OMP_LIST_LASTPRIVATE; list++)
+  for (list = OMP_LIST_FIRSTPRIVATE; list <= OMP_LIST_LASTPRIVATE;
+       list = gfc_omp_list_type (list + 1))
     for (n = omp_clauses->lists[list]; n; n = n->next)
       if (n->sym->data_mark || n->sym->gen_mark || n->sym->dev_mark)
        {
@@ -9490,7 +9489,7 @@ resolve_omp_clauses (gfc_code *code, gfc_omp_clauses 
*omp_clauses,
                {
                  prev->next = n->next;
                  n->next = NULL;
-                 gfc_free_omp_namelist (n, false, true, false, false);
+                 gfc_free_omp_namelist (n, OMP_LIST_ALLOCATE);
                  n = prev->next;
                }
              continue;
@@ -9503,7 +9502,8 @@ resolve_omp_clauses (gfc_code *code, gfc_omp_clauses 
*omp_clauses,
       /* Non-composite constructs.  */
       if (code && code->op < EXEC_OMP_DO_SIMD)
        {
-         for (list = 0; list < OMP_LIST_NUM; list++)
+         for (list = OMP_LIST_FIRST; list < OMP_LIST_NUM;
+              list = gfc_omp_list_type (list + 1))
            switch (list)
            {
              case OMP_LIST_PRIVATE:
@@ -9693,7 +9693,8 @@ resolve_omp_clauses (gfc_code *code, gfc_omp_clauses 
*omp_clauses,
     }
 
   bool has_inscan = false, has_notinscan = false;
-  for (list = 0; list < OMP_LIST_NUM; list++)
+  for (enum gfc_omp_list_type list = OMP_LIST_FIRST; list < OMP_LIST_NUM;
+       list = gfc_omp_list_type (list + 1))
     if ((n = omp_clauses->lists[list]) != NULL)
       {
        const char *name = clause_names[list];
@@ -11581,7 +11582,6 @@ gfc_resolve_omp_parallel_blocks (gfc_code *code, 
gfc_namespace *ns)
   struct fortran_omp_context ctx;
   gfc_omp_clauses *omp_clauses = code->ext.omp_clauses;
   gfc_omp_namelist *n;
-  int list;
 
   ctx.code = code;
   ctx.sharing_clauses = new hash_set<gfc_symbol *>;
@@ -11590,7 +11590,8 @@ gfc_resolve_omp_parallel_blocks (gfc_code *code, 
gfc_namespace *ns)
   ctx.is_openmp = true;
   omp_current_ctx = &ctx;
 
-  for (list = 0; list < OMP_LIST_NUM; list++)
+  for (enum gfc_omp_list_type list = OMP_LIST_FIRST; list < OMP_LIST_NUM;
+       list = gfc_omp_list_type (list + 1))
     switch (list)
       {
       case OMP_LIST_SHARED:
@@ -12238,7 +12239,7 @@ static void
 resolve_omp_do (gfc_code *code)
 {
   gfc_code *do_code, *next;
-  int list, i, count, non_generated_count;
+  int i, count, non_generated_count;
   gfc_omp_namelist *n;
   gfc_symbol *dovar;
   const char *name;
@@ -12449,7 +12450,8 @@ resolve_omp_do (gfc_code *code)
          errorp = true;
        }
       if (code->ext.omp_clauses)
-       for (list = 0; list < OMP_LIST_NUM; list++)
+       for (enum gfc_omp_list_type list = OMP_LIST_FIRST; list < OMP_LIST_NUM;
+            list = gfc_omp_list_type (list + 1))
          if (!is_simd || code->ext.omp_clauses->collapse > 1
              ? (list != OMP_LIST_PRIVATE && list != OMP_LIST_LASTPRIVATE
                  && list != OMP_LIST_ALLOCATE)
@@ -13128,7 +13130,6 @@ gfc_resolve_oacc_blocks (gfc_code *code, gfc_namespace 
*ns)
   fortran_omp_context ctx;
   gfc_omp_clauses *omp_clauses = code->ext.omp_clauses;
   gfc_omp_namelist *n;
-  int list;
 
   resolve_oacc_loop_blocks (code);
 
@@ -13139,7 +13140,8 @@ gfc_resolve_oacc_blocks (gfc_code *code, gfc_namespace 
*ns)
   ctx.is_openmp = false;
   omp_current_ctx = &ctx;
 
-  for (list = 0; list < OMP_LIST_NUM; list++)
+  for (enum gfc_omp_list_type list = OMP_LIST_FIRST; list < OMP_LIST_NUM;
+       list = gfc_omp_list_type (list + 1))
     switch (list)
       {
       case OMP_LIST_PRIVATE:
@@ -13190,7 +13192,7 @@ resolve_oacc_loop (gfc_code *code)
 void
 gfc_resolve_oacc_declare (gfc_namespace *ns)
 {
-  int list;
+  enum gfc_omp_list_type list;
   gfc_omp_namelist *n;
   gfc_oacc_declare *oc;
 
@@ -13199,7 +13201,8 @@ gfc_resolve_oacc_declare (gfc_namespace *ns)
 
   for (oc = ns->oacc_declare; oc; oc = oc->next)
     {
-      for (list = 0; list < OMP_LIST_NUM; list++)
+      for (list = OMP_LIST_FIRST; list < OMP_LIST_NUM;
+          list = gfc_omp_list_type (list + 1))
        for (n = oc->clauses->lists[list]; n; n = n->next)
          {
            n->sym->mark = 0;
@@ -13238,7 +13241,8 @@ gfc_resolve_oacc_declare (gfc_namespace *ns)
 
   for (oc = ns->oacc_declare; oc; oc = oc->next)
     {
-      for (list = 0; list < OMP_LIST_NUM; list++)
+      for (list = OMP_LIST_FIRST; list < OMP_LIST_NUM;
+          list = gfc_omp_list_type (list + 1))
        for (n = oc->clauses->lists[list]; n; n = n->next)
          {
            if (n->sym->mark)
@@ -13254,7 +13258,8 @@ gfc_resolve_oacc_declare (gfc_namespace *ns)
 
   for (oc = ns->oacc_declare; oc; oc = oc->next)
     {
-      for (list = 0; list < OMP_LIST_NUM; list++)
+      for (list = OMP_LIST_FIRST; list < OMP_LIST_NUM;
+          list = gfc_omp_list_type (list + 1))
        for (n = oc->clauses->lists[list]; n; n = n->next)
          n->sym->mark = 0;
     }
diff --git a/gcc/fortran/st.cc b/gcc/fortran/st.cc
index ce4fcad884f5..39711e326208 100644
--- a/gcc/fortran/st.cc
+++ b/gcc/fortran/st.cc
@@ -299,7 +299,7 @@ gfc_free_statement (gfc_code *p)
       break;
 
     case EXEC_OMP_FLUSH:
-      gfc_free_omp_namelist (p->ext.omp_namelist, false, false, false, false);
+      gfc_free_omp_namelist (p->ext.omp_namelist, OMP_LIST_NONE);
       break;
 
     case EXEC_OMP_BARRIER:

Reply via email to