(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
plugin/
ggcplug.c
       1  /* This plugin tests the GGC related plugin events.  */
       2  /* { dg-options "-O" } */
       3  
       4  #include "config.h"
       5  #include "system.h"
       6  #include "coretypes.h"
       7  #include "gcc-plugin.h"
       8  #include "tm.h"
       9  #include "tree.h"
      10  #include "toplev.h"
      11  #include "basic-block.h"
      12  #include "hash-table.h"
      13  #include "vec.h"
      14  #include "ggc.h"
      15  #include "tree-ssa-alias.h"
      16  #include "internal-fn.h"
      17  #include "tree-eh.h"
      18  #include "gimple-expr.h"
      19  #include "is-a.h"
      20  #include "gimple.h"
      21  #include "tree.h"
      22  #include "tree-pass.h"
      23  #include "intl.h"
      24  #include "plugin-version.h"
      25  #include "diagnostic.h"
      26  
      27  int plugin_is_GPL_compatible;
      28  
      29  /* our callback is the same for all PLUGIN_GGC_START,
      30     PLUGIN_GGC_MARKING, PLUGIN_GGC_END events; it just increments the
      31     user_data which is an int */
      32  static void increment_callback (void *gcc_data, void *user_data);
      33  
      34  /* our counters are user_data */
      35  static int our_ggc_start_counter;
      36  static int our_ggc_end_counter;
      37  static int our_ggc_marking_counter;
      38  
      39  /* our empty GGC extra root table */
      40  static const struct ggc_root_tab our_xtratab[] = {
      41    LAST_GGC_ROOT_TAB
      42  };
      43  
      44  
      45  /* The initialization routine exposed to and called by GCC. The spec of this
      46     function is defined in gcc/gcc-plugin.h.
      47  
      48     Note that this function needs to be named exactly "plugin_init".  */
      49  int
      50  plugin_init (struct plugin_name_args *plugin_info,
      51  	      struct plugin_gcc_version *version)
      52  {
      53    const char *plugin_name = plugin_info->base_name;
      54    int argc = plugin_info->argc;
      55    int i = 0;
      56    struct plugin_argument *argv = plugin_info->argv;
      57    if (!plugin_default_version_check (version, &gcc_version))
      58      return 1;
      59    /* Process the plugin arguments. This plugin takes the following arguments:
      60       count-ggc-start count-ggc-end count-ggc-mark */
      61    for (i = 0; i < argc; ++i)
      62      {
      63        if (!strcmp (argv[i].key, "count-ggc-start"))
      64  	{
      65  	  if (argv[i].value)
      66  	    warning (0, G_("option '-fplugin-arg-%s-count-ggc-start=%s'"
      67  			   " ignored (superfluous '=%s')"),
      68  		     plugin_name, argv[i].value, argv[i].value);
      69  	  else
      70  	    register_callback ("ggcplug",
      71  			       PLUGIN_GGC_START,
      72  			       increment_callback,
      73  			       (void *) &our_ggc_start_counter);
      74  	}
      75        else if (!strcmp (argv[i].key, "count-ggc-end"))
      76  	{
      77  	  if (argv[i].value)
      78  	    warning (0, G_("option '-fplugin-arg-%s-count-ggc-end=%s'"
      79  			   " ignored (superfluous '=%s')"),
      80  		     plugin_name, argv[i].value, argv[i].value);
      81  	  else
      82  	    register_callback ("ggcplug",
      83  			       PLUGIN_GGC_END,
      84  			       increment_callback,
      85  			       (void *) &our_ggc_end_counter);
      86  	}
      87        else if (!strcmp (argv[i].key, "count-ggc-mark"))
      88  	{
      89  	  if (argv[i].value)
      90  	    warning (0, G_("option '-fplugin-arg-%s-count-ggc-mark=%s'"
      91  			   " ignored (superfluous '=%s')"),
      92  		     plugin_name, argv[i].value, argv[i].value);
      93  	  else
      94  	    register_callback ("ggcplug",
      95  			       PLUGIN_GGC_MARKING,
      96  			       increment_callback,
      97  			       (void *) &our_ggc_marking_counter);
      98  	}
      99        else if (!strcmp (argv[i].key, "test-extra-root"))
     100  	{
     101  	  if (argv[i].value)
     102  	    warning (0, G_("option '-fplugin-arg-%s-test-extra-root=%s'"
     103  			   " ignored (superfluous '=%s')"),
     104  		     plugin_name, argv[i].value, argv[i].value);
     105  	  else
     106  	    register_callback ("ggcplug",
     107  			       PLUGIN_REGISTER_GGC_ROOTS,
     108  			       NULL,
     109  			       (void *) our_xtratab);
     110  	}
     111      }
     112    /* plugin initialization succeeded */
     113    return 0;
     114   }
     115  
     116  static void
     117  increment_callback (void *gcc_data, void *user_data)
     118  {
     119    int *usercountptr = (int *) user_data;
     120    gcc_assert (!gcc_data);
     121    gcc_assert (user_data);
     122    (*usercountptr)++;
     123  }