Patch updated: using regex to match the function name:

http://codereview.appspot.com/6281047

Thanks,
Dehao

2012-06-01  Dehao Chen  <de...@google.com>

        * gcc/cgraph.c (cgraph_node): Add attribute to function decl.
        * gcc/opts-global.c (add_attribute_pattern): New function.
        (pattern_match_function_attributes): New function.
        (handle_common_deferred_options): Handle new options.
        * gcc/opts.c (common_handle_option): Handle new options.
        * gcc/opts.h (handle_common_deferred_options): New function.
        * gcc/common.opt (ffunction_attribute_list): New option.

Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi (revision 188050)
+++ gcc/doc/invoke.texi (working copy)
@@ -362,7 +362,8 @@
 -fdelete-null-pointer-checks -fdse -fdevirtualize -fdse @gol
 -fearly-inlining -fipa-sra -fexpensive-optimizations -ffast-math @gol
 -ffinite-math-only -ffloat-store -fexcess-precision=@var{style} @gol
--fforward-propagate -ffp-contract=@var{style} -ffunction-sections @gol
+-fforward-propagate -ffp-contract=@var{style} @gol
+-ffunction-attribute-list -ffunction-sections @gol
 -fgcse -fgcse-after-reload -fgcse-las -fgcse-lm -fgraphite-identity @gol
 -fgcse-sm -fif-conversion -fif-conversion2 -findirect-inlining @gol
 -finline-functions -finline-functions-called-once -finline-limit=@var{n} @gol
@@ -8585,6 +8586,10 @@
 specify this option and you may have problems with debugging if
 you specify both this option and @option{-g}.

+@item -ffunction-attribute-list
+@opindex ffunction-attribute-list
+List of function name patterns that will be applied specified attribute.
+
 @item -fbranch-target-load-optimize
 @opindex fbranch-target-load-optimize
 Perform branch target register load optimization before prologue / epilogue
Index: gcc/cgraph.c
===================================================================
--- gcc/cgraph.c        (revision 188050)
+++ gcc/cgraph.c        (working copy)
@@ -99,6 +99,7 @@
 #include "ipa-utils.h"
 #include "lto-streamer.h"
 #include "l-ipo.h"
+#include "opts.h"

 const char * const ld_plugin_symbol_resolution_names[]=
 {
@@ -554,6 +555,7 @@
       node->origin->nested = node;
     }
   cgraph_add_assembler_hash_node (node);
+  pattern_match_function_attributes (decl);
   return node;
 }

Index: gcc/opts.c
===================================================================
--- gcc/opts.c  (revision 188050)
+++ gcc/opts.c  (working copy)
@@ -1647,6 +1647,10 @@
       /* Deferred.  */
       break;

+    case OPT_ffunction_attribute_list_:
+      /* Deferred.  */
+      break;
+
     case OPT_fsched_verbose_:
 #ifdef INSN_SCHEDULING
       /* Handled with Var in common.opt.  */
Index: gcc/opts.h
===================================================================
--- gcc/opts.h  (revision 188050)
+++ gcc/opts.h  (working copy)
@@ -382,4 +382,5 @@
                                     location_t loc,
                                     const char *value);
 extern void write_opts_to_asm (void);
+extern void pattern_match_function_attributes (tree);
 #endif
Index: gcc/common.opt
===================================================================
--- gcc/common.opt      (revision 188050)
+++ gcc/common.opt      (working copy)
@@ -1242,6 +1242,10 @@
 Common Report Var(flag_function_sections)
 Place each function into its own section

+ffunction-attribute-list=
+Common Joined RejectNegative Var(common_deferred_options) Defer
+-ffunction-attribute-list=attribute:name,...  Add attribute to named functions
+
 fgcda=
 Common Joined RejectNegative Var(gcov_da_name)
 Set the gcov data file name.
Index: gcc/opts-global.c
===================================================================
--- gcc/opts-global.c   (revision 188050)
+++ gcc/opts-global.c   (working copy)
@@ -39,6 +39,7 @@
 #include "tree-pass.h"
 #include "params.h"
 #include "l-ipo.h"
+#include "xregex.h"

 typedef const char *const_char_p; /* For DEF_VEC_P.  */
 DEF_VEC_P(const_char_p);
@@ -50,6 +51,13 @@
 const char **in_fnames;
 unsigned num_in_fnames;

+static struct reg_func_attr_patterns
+{
+  regex_t r;
+  const char *attribute;
+  struct reg_func_attr_patterns *next;
+} *reg_func_attr_patterns;
+
 /* Return a malloced slash-separated list of languages in MASK.  */

 static char *
@@ -79,6 +87,62 @@
   return result;
 }

+/* Add strings like attribute_str:pattern... to attribute pattern list.  */
+
+static void
+add_attribute_pattern (const char *arg)
+{
+  char *tmp;
+  char *pattern_str;
+  struct reg_func_attr_patterns *one_pat;
+  int ec;
+
+  /* We never free this string.  */
+  tmp = xstrdup (arg);
+
+  pattern_str = strchr (tmp, ':');
+  if (!pattern_str)
+    error ("invalid pattern in -ffunction-attribute-list option: %qs", tmp);
+
+  *pattern_str = '\0';
+  pattern_str ++;
+
+  one_pat = XCNEW (struct reg_func_attr_patterns);
+  one_pat->next = reg_func_attr_patterns;
+  one_pat->attribute = tmp;
+  reg_func_attr_patterns = one_pat;
+  if ((ec= regcomp (&one_pat->r, pattern_str, REG_EXTENDED|REG_NOSUB) != 0))
+    {
+      char err[100];
+      regerror (ec, &one_pat->r, err, 99);
+      error ("invalid pattern in -ffunction-attribute-list option: %qs: %qs",
+            pattern_str, err);
+    }
+}
+
+/* Match FNDECL's name with user specified patterns, and add attributes
+   to FNDECL.  */
+
+void
+pattern_match_function_attributes (tree fndecl)
+{
+  const char *name;
+  struct reg_func_attr_patterns *one_pat;
+
+  if (!fndecl)
+    return;
+
+  if (!reg_func_attr_patterns)
+    return;
+
+  name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl));
+
+  for (one_pat = reg_func_attr_patterns; one_pat; one_pat = one_pat->next)
+    if (regexec (&one_pat->r, name, 0, NULL, 0) == 0)
+      decl_attributes (&fndecl, tree_cons (
+         get_identifier (one_pat->attribute), NULL, NULL), 0);
+}
+
 /* Complain that switch DECODED does not apply to this front end (mask
    LANG_MASK).  */

@@ -452,6 +516,10 @@
          set_random_seed (opt->arg);
          break;

+       case OPT_ffunction_attribute_list_:
+         add_attribute_pattern (opt->arg);
+         break;
+
        case OPT_fstack_limit:
          /* The real switch is -fno-stack-limit.  */
          if (!opt->value)

Reply via email to