plugin.def: Add event for finish_decl.
authorRomain Geissler <romain.geissler@gmail.com>
Thu, 11 Aug 2011 15:57:38 +0000 (15:57 +0000)
committerDiego Novillo <dnovillo@gcc.gnu.org>
Thu, 11 Aug 2011 15:57:38 +0000 (11:57 -0400)
2011-08-11  Romain Geissler  <romain.geissler@gmail.com>
            Brian Hackett  <bhackett1024@gmail.com>

gcc/ChangeLog:

      * plugin.def: Add event for finish_decl.
      * plugin.c (register_callback, invoke_plugin_callbacks): Same.
      * c-decl.c (finish_decl): Invoke callbacks on above event.
      * doc/plugins.texi: Document above event.

gcc/cp/ChangeLog:

      * decl.c (cp_finish_decl): Invoke callbacks on finish_decl event.

gcc/testsuite/ChangeLog:

      * g++.dg/plugin/decl_plugin.c: New.
      * g++.dg/plugin/decl-plugin-test.C: New.
      * g++.dg/plugin/plugin.exp: Add above testcase.

Co-Authored-By: Brian Hackett <bhackett1024@gmail.com>
From-SVN: r177674

gcc/ChangeLog
gcc/c-decl.c
gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/doc/plugins.texi
gcc/plugin.c
gcc/plugin.def
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/plugin/decl-plugin-test.C [new file with mode: 0644]
gcc/testsuite/g++.dg/plugin/decl_plugin.c [new file with mode: 0644]
gcc/testsuite/g++.dg/plugin/plugin.exp

index c2c62bc5f21c96bbdb7a6b5cf24e6f45a85a7e5a..803e9c7481d465debf1d981060de045043f02205 100644 (file)
@@ -1,3 +1,11 @@
+2011-08-11  Romain Geissler  <romain.geissler@gmail.com>
+            Brian Hackett  <bhackett1024@gmail.com>
+
+       * plugin.def: Add event for finish_decl.
+       * plugin.c (register_callback, invoke_plugin_callbacks): Same.
+       * c-decl.c (finish_decl): Invoke callbacks on above event.
+       * doc/plugins.texi: Document above event.
+
 2011-08-11  Richard Guenther  <rguenther@suse.de>
 
        * tree-ssa-sccvn.c (vn_reference_lookup_3): Avoid redundant
index 33d2615bc229bdb0e8cbd251ce1f3cb64c95c9ca..927beb0a33de1c3f51a5bb6ac198fcdb1d988897 100644 (file)
@@ -4466,6 +4466,8 @@ finish_decl (tree decl, location_t init_loc, tree init,
               && C_TYPE_FIELDS_READONLY (type))
        diagnose_uninitialized_cst_member (decl, type);
     }
+
+       invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
 }
 
 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
index be846ce52a3c0008c2116db05e071e2b4d6a983f..f5989f4bc9840cff1df5100d73fc8fca8ae1558f 100644 (file)
@@ -1,3 +1,8 @@
+2011-08-11  Romain Geissler  <romain.geissler@gmail.com>
+            Brian Hackett  <bhackett1024@gmail.com>
+
+       * decl.c (cp_finish_decl): Invoke callbacks on finish_decl event.
+
 2011-08-10  Richard Guenther  <rguenther@suse.de>
 
        * call.c (build_over_call): Call memcpy unconditionally.
index 265ca9826806ff2670b339c989459741fb1a0bcb..1db074809c8ffbf28f8d438aa03832aa40ee05cd 100644 (file)
@@ -6311,6 +6311,8 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
 
   if (was_readonly)
     TREE_READONLY (decl) = 1;
+
+  invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
 }
 
 /* Returns a declaration for a VAR_DECL as if:
index faee13e391898df045f7a95fdd96539dbb6e9de6..398ebf0c536d4cfe2c942e606ee793047fa1a095 100644 (file)
@@ -151,6 +151,7 @@ enum plugin_event
 @{
   PLUGIN_PASS_MANAGER_SETUP,    /* To hook into pass manager.  */
   PLUGIN_FINISH_TYPE,           /* After finishing parsing a type.  */
+  PLUGIN_FINISH_DECL,           /* After finishing parsing a declaration. */
   PLUGIN_FINISH_UNIT,           /* Useful for summary processing.  */
   PLUGIN_PRE_GENERICIZE,        /* Allows to see low level AST in C and C++ frontends.  */
   PLUGIN_FINISH,                /* Called before GCC exits.  */
index c93daed055d7a346053b218b13d07b093d15393a..3906bffc68bf6ac4aa23e22a90275946d2e4491a 100644 (file)
@@ -420,6 +420,7 @@ register_callback (const char *plugin_name,
          }
       /* Fall through.  */
       case PLUGIN_FINISH_TYPE:
+      case PLUGIN_FINISH_DECL:
       case PLUGIN_START_UNIT:
       case PLUGIN_FINISH_UNIT:
       case PLUGIN_PRE_GENERICIZE:
@@ -496,6 +497,7 @@ invoke_plugin_callbacks_full (int event, void *gcc_data)
        gcc_assert (event < event_last);
       /* Fall through.  */
       case PLUGIN_FINISH_TYPE:
+      case PLUGIN_FINISH_DECL:
       case PLUGIN_START_UNIT:
       case PLUGIN_FINISH_UNIT:
       case PLUGIN_PRE_GENERICIZE:
index 4a40c2cf79e1303ec827093e378cc0225bb9a7b9..0c9c8bc76f540a2d00c411406efccfc15399e0ff 100644 (file)
@@ -24,6 +24,9 @@ DEFEVENT (PLUGIN_PASS_MANAGER_SETUP)
 /* After finishing parsing a type.  */
 DEFEVENT (PLUGIN_FINISH_TYPE)
 
+/* After finishing parsing a declaration. */
+DEFEVENT (PLUGIN_FINISH_DECL)
+
 /* Useful for summary processing.  */
 DEFEVENT (PLUGIN_FINISH_UNIT)
 
index 8c1dcfd7b4f6bedf28cd717b489bb0178e0031e0..a18bf7e9def6147d3100430cc20ee79ea2b98367 100644 (file)
@@ -1,3 +1,10 @@
+2011-08-11  Romain Geissler  <romain.geissler@gmail.com>
+            Brian Hackett  <bhackett1024@gmail.com>
+
+       * g++.dg/plugin/decl_plugin.c: New.
+       * g++.dg/plugin/decl-plugin-test.C: New.
+       * g++.dg/plugin/plugin.exp: Add above testcase.
+
 2011-08-11  Richard Guenther  <rguenther@suse.de>
 
        * g++.dg/tree-ssa/pr41186.C: Un-XFAIL.
diff --git a/gcc/testsuite/g++.dg/plugin/decl-plugin-test.C b/gcc/testsuite/g++.dg/plugin/decl-plugin-test.C
new file mode 100644 (file)
index 0000000..08a2ff2
--- /dev/null
@@ -0,0 +1,30 @@
+extern int global; // { dg-warning "Decl Global global" }
+int global_array[] = { 1, 2, 3 }; // { dg-warning "Decl Global global_array" }
+
+int takes_args(int arg1, int arg2)
+{
+  int local = arg1 + arg2 + global; // { dg-warning "Decl Local local" }
+  return local + 1;
+}
+
+int global = 12; // { dg-warning "Decl Global global" }
+
+struct test_str {
+  int field; // { dg-warning "Decl Field field" }
+};
+
+class test_class {
+  int class_field1; // { dg-warning "Decl Field class_field1" }
+  int class_field2; // { dg-warning "Decl Field class_field2" }
+
+  test_class() // { dg-warning "Decl Function test_class" }
+    : class_field1(0), class_field2(0)
+  {}
+
+  void swap_fields(int bias) // { dg-warning "Decl Function swap_fields" }
+  {
+    int temp = class_field1 + bias; // { dg-warning "Decl Local temp" }
+    class_field1 = class_field2 - bias;
+    class_field2 = temp;
+  }
+};
diff --git a/gcc/testsuite/g++.dg/plugin/decl_plugin.c b/gcc/testsuite/g++.dg/plugin/decl_plugin.c
new file mode 100644 (file)
index 0000000..879b09a
--- /dev/null
@@ -0,0 +1,51 @@
+/* A plugin example that shows which declarations are caught by FINISH_DECL */
+
+#include "gcc-plugin.h"
+#include <stdlib.h>
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "tree-pass.h"
+#include "intl.h"
+
+int plugin_is_GPL_compatible;
+
+/* Callback function to invoke after GCC finishes a declaration. */
+
+void plugin_finish_decl (void *event_data, void *data)
+{
+  tree decl = (tree) event_data;
+
+  const char *kind = NULL;
+  switch (TREE_CODE(decl)) {
+  case FUNCTION_DECL:
+    kind = "Function"; break;
+  case PARM_DECL:
+    kind = "Parameter"; break;
+  case VAR_DECL:
+    if (DECL_FILE_SCOPE_P(decl))
+      kind = "Global";
+    else
+      kind = "Local";
+    break;
+  case FIELD_DECL:
+    kind = "Field"; break;
+  default:
+    kind = "Unknown";
+  }
+
+  warning (0, G_("Decl %s %s"),
+           kind, IDENTIFIER_POINTER (DECL_NAME (decl)));
+}
+
+int
+plugin_init (struct plugin_name_args *plugin_info,
+             struct plugin_gcc_version *version)
+{
+  const char *plugin_name = plugin_info->base_name;
+
+  register_callback (plugin_name, PLUGIN_FINISH_DECL,
+                     plugin_finish_decl, NULL);
+  return 0;
+}
index 72de92dfd427bd6199853e5b0ef1c7943c0d088a..fb962dfdead636409bded3bf5f9c800124ef0126 100644 (file)
@@ -51,7 +51,8 @@ set plugin_test_list [list \
     { pragma_plugin.c pragma_plugin-test-1.C } \
     { selfassign.c self-assign-test-1.C self-assign-test-2.C self-assign-test-3.C } \
     { dumb_plugin.c dumb-plugin-test-1.C } \
-    { header_plugin.c header-plugin-test.C } ]
+    { header_plugin.c header-plugin-test.C } \
+    { decl_plugin.c decl-plugin-test.C } ]
 
 foreach plugin_test $plugin_test_list {
     # Replace each source file with its full-path name