* [PATCH] plugin event for C/C++ function definitions
@ 2014-11-13 17:47 Andres Tiraboschi
2014-11-14 10:22 ` Richard Biener
0 siblings, 1 reply; 6+ messages in thread
From: Andres Tiraboschi @ 2014-11-13 17:47 UTC (permalink / raw)
To: gcc-patches; +Cc: daniel.gutson
Hi, this patch adds a new plugin event PLUGIN_START_FUNCTION and PLUGIN_FINISH_FUNCTION that are invoked at start_function and finish_function respectively in the C and C++ frontends.
PLUGIN_START_FUNCTION is called before parsing the function body.
PLUGIN_FINISH_FUNCTION is called after parsing a function definition.
2014-11-04 Andrés Tiraboschi <andres.tiraboschi@tallertechnologies.com>
changelog:
gcc/c/c-decl.c: Invoke callbacks in start_function and finish_function.
gcc/cp/decl.c: Invoke callbacks in start_function and finish_function.
gcc/doc/plugins.texi: Add documentation about PLUGIN_START_FUNCTION and PLUGIN_FINISH_FUNCTION
gcc/plugin.def: Add events for start_function and finish_function.
gcc/plugin.c (register_callback, invoke_plugin_callbacks): Same.
gcc/testsuite/g++.dg/plugin/def_plugin.c: New test plugin.
gcc/testsuite/g++.dg/plugin/def-plugin-test.C: Testcase for above plugin.
gcc/testsuite/g++.dg/plugin/plugin.exp
diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index e23284a..b349a24 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -8073,6 +8073,7 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
&attributes, NULL, NULL, DEPRECATED_NORMAL);
+ invoke_plugin_callbacks (PLUGIN_START_FUNCTION, decl1);
/* If the declarator is not suitable for a function definition,
cause a syntax error. */
@@ -8886,6 +8887,7 @@ finish_function (void)
It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
tree_rest_of_compilation. */
set_cfun (NULL);
+ invoke_plugin_callbacks (PLUGIN_FINISH_FUNCTION, current_function_decl);
current_function_decl = NULL;
}
\f
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index d4adbeb..a8c6ebe 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -13631,6 +13631,7 @@ start_function (cp_decl_specifier_seq *declspecs,
tree decl1;
decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
+ invoke_plugin_callbacks (PLUGIN_START_FUNCTION, decl1);
if (decl1 == error_mark_node)
return false;
/* If the declarator is not suitable for a function definition,
@@ -14260,6 +14261,7 @@ finish_function (int flags)
vec_free (deferred_mark_used_calls);
}
+ invoke_plugin_callbacks (PLUGIN_FINISH_FUNCTION, fndecl);
return fndecl;
}
\f
diff --git a/gcc/doc/plugins.texi b/gcc/doc/plugins.texi
index 4a839b8..b4a20e1 100644
--- a/gcc/doc/plugins.texi
+++ b/gcc/doc/plugins.texi
@@ -174,6 +174,8 @@ Callbacks can be invoked at the following pre-determined events:
@smallexample
enum plugin_event
@{
+ PLUGIN_START_FUNCTION /* Called before parsing the body of a function. */
+ PLUGIN_FINISH_FUNCTION /* After finishing parsing a function. */
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. */
diff --git a/gcc/plugin.c b/gcc/plugin.c
index 8debc09..c048d93 100644
--- a/gcc/plugin.c
+++ b/gcc/plugin.c
@@ -433,6 +433,8 @@ register_callback (const char *plugin_name,
return;
}
/* Fall through. */
+ case PLUGIN_START_FUNCTION:
+ case PLUGIN_FINISH_FUNCTION:
case PLUGIN_FINISH_TYPE:
case PLUGIN_FINISH_DECL:
case PLUGIN_START_UNIT:
@@ -511,6 +513,8 @@ invoke_plugin_callbacks_full (int event, void *gcc_data)
gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
gcc_assert (event < event_last);
/* Fall through. */
+ case PLUGIN_START_FUNCTION:
+ case PLUGIN_FINISH_FUNCTION:
case PLUGIN_FINISH_TYPE:
case PLUGIN_FINISH_DECL:
case PLUGIN_START_UNIT:
diff --git a/gcc/plugin.def b/gcc/plugin.def
index df5d383..a4131ad 100644
--- a/gcc/plugin.def
+++ b/gcc/plugin.def
@@ -17,6 +17,11 @@ You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
+/* Called before parsing the body of a function. */
+DEFEVENT (PLUGIN_START_FUNCTION)
+
+/* After finishing parsing a function definition. */
+DEFEVENT (PLUGIN_FINISH_FUNCTION)
/* To hook into pass manager. */
DEFEVENT (PLUGIN_PASS_MANAGER_SETUP)
diff --git a/gcc/testsuite/g++.dg/plugin/def-plugin-test.C b/gcc/testsuite/g++.dg/plugin/def-plugin-test.C
new file mode 100644
index 0000000..b7f2d3d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/plugin/def-plugin-test.C
@@ -0,0 +1,13 @@
+int global = 12;
+
+int function1(void);
+
+int function2(int a) // { dg-warning "Start fndef function2" }
+{
+ return function1() + a;
+} // { dg-warning "Finish fndef function2" }
+
+int function1(void) // { dg-warning "Start fndef function1" }
+{
+ return global + 1;
+} // { dg-warning "Finish fndef function1" }
diff --git a/gcc/testsuite/g++.dg/plugin/def_plugin.c b/gcc/testsuite/g++.dg/plugin/def_plugin.c
new file mode 100644
index 0000000..d4fec4c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/plugin/def_plugin.c
@@ -0,0 +1,45 @@
+/* A plugin example that shows which function definitions are caught by PLUGIN_START_FUNCTION and PLUGIN_FINISH_FUNCTION */
+
+#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"
+#include "diagnostic.h"
+
+int plugin_is_GPL_compatible;
+
+/* Callback function to invoke when GCC starts a function definition. */
+
+void plugin_start_function (void *event_data, void *data)
+{
+ tree fndef = (tree) event_data;
+ warning (0, G_("Start fndef %s"),
+ IDENTIFIER_POINTER (DECL_NAME (fndef)));
+}
+
+/* Callback function to invoke after GCC finishes a function definition. */
+
+void plugin_finish_function (void *event_data, void *data)
+{
+ tree fndef = (tree) event_data;
+ warning (0, G_("Finish fndef %s"),
+ IDENTIFIER_POINTER (DECL_NAME (fndef)));
+}
+
+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_START_FUNCTION,
+ plugin_start_function, NULL);
+
+ register_callback (plugin_name, PLUGIN_FINISH_FUNCTION,
+ plugin_finish_function, NULL);
+ return 0;
+}
diff --git a/gcc/testsuite/g++.dg/plugin/plugin.exp b/gcc/testsuite/g++.dg/plugin/plugin.exp
index f3fd8ee..21cac6a 100644
--- a/gcc/testsuite/g++.dg/plugin/plugin.exp
+++ b/gcc/testsuite/g++.dg/plugin/plugin.exp
@@ -61,7 +61,8 @@ set plugin_test_list [list \
{ 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 } \
- { decl_plugin.c decl-plugin-test.C } ]
+ { decl_plugin.c decl-plugin-test.C } \
+ { def_plugin.c def-plugin-test.C } ]
foreach plugin_test $plugin_test_list {
# Replace each source file with its full-path name
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] plugin event for C/C++ function definitions
2014-11-13 17:47 [PATCH] plugin event for C/C++ function definitions Andres Tiraboschi
@ 2014-11-14 10:22 ` Richard Biener
2014-11-18 13:02 ` Andres Tiraboschi
0 siblings, 1 reply; 6+ messages in thread
From: Richard Biener @ 2014-11-14 10:22 UTC (permalink / raw)
To: Andres Tiraboschi; +Cc: GCC Patches, daniel.gutson
On Thu, Nov 13, 2014 at 6:42 PM, Andres Tiraboschi
<andres.tiraboschi@tallertechnologies.com> wrote:
> Hi, this patch adds a new plugin event PLUGIN_START_FUNCTION and PLUGIN_FINISH_FUNCTION that are invoked at start_function and finish_function respectively in the C and C++ frontends.
> PLUGIN_START_FUNCTION is called before parsing the function body.
> PLUGIN_FINISH_FUNCTION is called after parsing a function definition.
Can you name them more specifically, like
PLUGIN_START/FINISH_PARSE_FUNCTION please?
Thanks,
Richard.
> 2014-11-04 Andrés Tiraboschi <andres.tiraboschi@tallertechnologies.com>
>
> changelog:
>
> gcc/c/c-decl.c: Invoke callbacks in start_function and finish_function.
> gcc/cp/decl.c: Invoke callbacks in start_function and finish_function.
>
> gcc/doc/plugins.texi: Add documentation about PLUGIN_START_FUNCTION and PLUGIN_FINISH_FUNCTION
> gcc/plugin.def: Add events for start_function and finish_function.
> gcc/plugin.c (register_callback, invoke_plugin_callbacks): Same.
>
> gcc/testsuite/g++.dg/plugin/def_plugin.c: New test plugin.
> gcc/testsuite/g++.dg/plugin/def-plugin-test.C: Testcase for above plugin.
> gcc/testsuite/g++.dg/plugin/plugin.exp
>
> diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
> index e23284a..b349a24 100644
> --- a/gcc/c/c-decl.c
> +++ b/gcc/c/c-decl.c
> @@ -8073,6 +8073,7 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
>
> decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
> &attributes, NULL, NULL, DEPRECATED_NORMAL);
> + invoke_plugin_callbacks (PLUGIN_START_FUNCTION, decl1);
>
> /* If the declarator is not suitable for a function definition,
> cause a syntax error. */
> @@ -8886,6 +8887,7 @@ finish_function (void)
> It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
> tree_rest_of_compilation. */
> set_cfun (NULL);
> + invoke_plugin_callbacks (PLUGIN_FINISH_FUNCTION, current_function_decl);
> current_function_decl = NULL;
> }
>
> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
> index d4adbeb..a8c6ebe 100644
> --- a/gcc/cp/decl.c
> +++ b/gcc/cp/decl.c
> @@ -13631,6 +13631,7 @@ start_function (cp_decl_specifier_seq *declspecs,
> tree decl1;
>
> decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
> + invoke_plugin_callbacks (PLUGIN_START_FUNCTION, decl1);
> if (decl1 == error_mark_node)
> return false;
> /* If the declarator is not suitable for a function definition,
> @@ -14260,6 +14261,7 @@ finish_function (int flags)
> vec_free (deferred_mark_used_calls);
> }
>
> + invoke_plugin_callbacks (PLUGIN_FINISH_FUNCTION, fndecl);
> return fndecl;
> }
>
> diff --git a/gcc/doc/plugins.texi b/gcc/doc/plugins.texi
> index 4a839b8..b4a20e1 100644
> --- a/gcc/doc/plugins.texi
> +++ b/gcc/doc/plugins.texi
> @@ -174,6 +174,8 @@ Callbacks can be invoked at the following pre-determined events:
> @smallexample
> enum plugin_event
> @{
> + PLUGIN_START_FUNCTION /* Called before parsing the body of a function. */
> + PLUGIN_FINISH_FUNCTION /* After finishing parsing a function. */
> 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. */
> diff --git a/gcc/plugin.c b/gcc/plugin.c
> index 8debc09..c048d93 100644
> --- a/gcc/plugin.c
> +++ b/gcc/plugin.c
> @@ -433,6 +433,8 @@ register_callback (const char *plugin_name,
> return;
> }
> /* Fall through. */
> + case PLUGIN_START_FUNCTION:
> + case PLUGIN_FINISH_FUNCTION:
> case PLUGIN_FINISH_TYPE:
> case PLUGIN_FINISH_DECL:
> case PLUGIN_START_UNIT:
> @@ -511,6 +513,8 @@ invoke_plugin_callbacks_full (int event, void *gcc_data)
> gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
> gcc_assert (event < event_last);
> /* Fall through. */
> + case PLUGIN_START_FUNCTION:
> + case PLUGIN_FINISH_FUNCTION:
> case PLUGIN_FINISH_TYPE:
> case PLUGIN_FINISH_DECL:
> case PLUGIN_START_UNIT:
> diff --git a/gcc/plugin.def b/gcc/plugin.def
> index df5d383..a4131ad 100644
> --- a/gcc/plugin.def
> +++ b/gcc/plugin.def
> @@ -17,6 +17,11 @@ You should have received a copy of the GNU General Public License
> along with GCC; see the file COPYING3. If not see
> <http://www.gnu.org/licenses/>. */
>
> +/* Called before parsing the body of a function. */
> +DEFEVENT (PLUGIN_START_FUNCTION)
> +
> +/* After finishing parsing a function definition. */
> +DEFEVENT (PLUGIN_FINISH_FUNCTION)
>
> /* To hook into pass manager. */
> DEFEVENT (PLUGIN_PASS_MANAGER_SETUP)
> diff --git a/gcc/testsuite/g++.dg/plugin/def-plugin-test.C b/gcc/testsuite/g++.dg/plugin/def-plugin-test.C
> new file mode 100644
> index 0000000..b7f2d3d
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/plugin/def-plugin-test.C
> @@ -0,0 +1,13 @@
> +int global = 12;
> +
> +int function1(void);
> +
> +int function2(int a) // { dg-warning "Start fndef function2" }
> +{
> + return function1() + a;
> +} // { dg-warning "Finish fndef function2" }
> +
> +int function1(void) // { dg-warning "Start fndef function1" }
> +{
> + return global + 1;
> +} // { dg-warning "Finish fndef function1" }
> diff --git a/gcc/testsuite/g++.dg/plugin/def_plugin.c b/gcc/testsuite/g++.dg/plugin/def_plugin.c
> new file mode 100644
> index 0000000..d4fec4c
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/plugin/def_plugin.c
> @@ -0,0 +1,45 @@
> +/* A plugin example that shows which function definitions are caught by PLUGIN_START_FUNCTION and PLUGIN_FINISH_FUNCTION */
> +
> +#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"
> +#include "diagnostic.h"
> +
> +int plugin_is_GPL_compatible;
> +
> +/* Callback function to invoke when GCC starts a function definition. */
> +
> +void plugin_start_function (void *event_data, void *data)
> +{
> + tree fndef = (tree) event_data;
> + warning (0, G_("Start fndef %s"),
> + IDENTIFIER_POINTER (DECL_NAME (fndef)));
> +}
> +
> +/* Callback function to invoke after GCC finishes a function definition. */
> +
> +void plugin_finish_function (void *event_data, void *data)
> +{
> + tree fndef = (tree) event_data;
> + warning (0, G_("Finish fndef %s"),
> + IDENTIFIER_POINTER (DECL_NAME (fndef)));
> +}
> +
> +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_START_FUNCTION,
> + plugin_start_function, NULL);
> +
> + register_callback (plugin_name, PLUGIN_FINISH_FUNCTION,
> + plugin_finish_function, NULL);
> + return 0;
> +}
> diff --git a/gcc/testsuite/g++.dg/plugin/plugin.exp b/gcc/testsuite/g++.dg/plugin/plugin.exp
> index f3fd8ee..21cac6a 100644
> --- a/gcc/testsuite/g++.dg/plugin/plugin.exp
> +++ b/gcc/testsuite/g++.dg/plugin/plugin.exp
> @@ -61,7 +61,8 @@ set plugin_test_list [list \
> { 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 } \
> - { decl_plugin.c decl-plugin-test.C } ]
> + { decl_plugin.c decl-plugin-test.C } \
> + { def_plugin.c def-plugin-test.C } ]
>
> foreach plugin_test $plugin_test_list {
> # Replace each source file with its full-path name
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] plugin event for C/C++ function definitions
2014-11-14 10:22 ` Richard Biener
@ 2014-11-18 13:02 ` Andres Tiraboschi
0 siblings, 0 replies; 6+ messages in thread
From: Andres Tiraboschi @ 2014-11-18 13:02 UTC (permalink / raw)
To: Richard Biener; +Cc: GCC Patches, Daniel Gutson
2014-11-14 7:08 GMT-03:00 Richard Biener <richard.guenther@gmail.com>:
> On Thu, Nov 13, 2014 at 6:42 PM, Andres Tiraboschi
> <andres.tiraboschi@tallertechnologies.com> wrote:
>> Hi, this patch adds a new plugin event PLUGIN_START_FUNCTION and PLUGIN_FINISH_FUNCTION that are invoked at start_function and finish_function respectively in the C and C++ frontends.
>> PLUGIN_START_FUNCTION is called before parsing the function body.
>> PLUGIN_FINISH_FUNCTION is called after parsing a function definition.
>
> Can you name them more specifically, like
> PLUGIN_START/FINISH_PARSE_FUNCTION please?
>
> Thanks,
> Richard.
Ok, I will do it
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] plugin event for C/C++ function definitions
2015-05-18 20:05 andres.tiraboschi
2015-05-19 12:40 ` Andres Tiraboschi
@ 2015-06-03 13:30 ` Jeff Law
1 sibling, 0 replies; 6+ messages in thread
From: Jeff Law @ 2015-06-03 13:30 UTC (permalink / raw)
To: andres.tiraboschi, gcc-patches
On 05/18/2015 01:51 PM, andres.tiraboschi@tallertechnologies.com wrote:
> Hi, this patch adds two new plugin events PLUGIN_START_PARSE_FUNCTION and PLUGIN_FINISH_PARSE_FUNCTION. These events are invoked at start_function and finish_function in gcc/c/c-decl.c and gcc/cp/decl.c respectively in the C and C++ frontends.
> PLUGIN_START_PARSE_FUNCTION is called before parsing a function body.
> PLUGIN_FINISH_PARSE_FUNCTION is called after parsing a function definition.
> This patch has been implemented in gcc 5.1.0.
THanks. I've bootstrapped and regression tested this patch on
x86-linux-gnu. I also wrote a ChangeLog and installed it on the trunk.
jeff
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] plugin event for C/C++ function definitions
2015-05-18 20:05 andres.tiraboschi
@ 2015-05-19 12:40 ` Andres Tiraboschi
2015-06-03 13:30 ` Jeff Law
1 sibling, 0 replies; 6+ messages in thread
From: Andres Tiraboschi @ 2015-05-19 12:40 UTC (permalink / raw)
To: GCC Patches
2015-05-18 16:51 GMT-03:00 <andres.tiraboschi@tallertechnologies.com>:
> Hi, this patch adds two new plugin events PLUGIN_START_PARSE_FUNCTION and PLUGIN_FINISH_PARSE_FUNCTION. These events are invoked at start_function and finish_function in gcc/c/c-decl.c and gcc/cp/decl.c respectively in the C and C++ frontends.
> PLUGIN_START_PARSE_FUNCTION is called before parsing a function body.
> PLUGIN_FINISH_PARSE_FUNCTION is called after parsing a function definition.
> This patch has been implemented in gcc 5.1.0.
>
> changelog:
>
> diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
> index e28a294..fcc849d 100644
> --- a/gcc/c/c-decl.c
> +++ b/gcc/c/c-decl.c
> @@ -8235,6 +8235,7 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
>
> decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
> &attributes, NULL, NULL, DEPRECATED_NORMAL);
> + invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
>
> /* If the declarator is not suitable for a function definition,
> cause a syntax error. */
> @@ -9050,6 +9051,7 @@ finish_function (void)
> It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
> tree_rest_of_compilation. */
> set_cfun (NULL);
> + invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
> current_function_decl = NULL;
> }
>
> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
> index c4731ae..bde92cc 100644
> --- a/gcc/cp/decl.c
> +++ b/gcc/cp/decl.c
> @@ -13727,6 +13727,7 @@ start_function (cp_decl_specifier_seq *declspecs,
> tree decl1;
>
> decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
> + invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
> if (decl1 == error_mark_node)
> return false;
> /* If the declarator is not suitable for a function definition,
> @@ -14365,6 +14366,7 @@ finish_function (int flags)
> vec_free (deferred_mark_used_calls);
> }
>
> + invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
> return fndecl;
> }
>
> diff --git a/gcc/doc/plugins.texi b/gcc/doc/plugins.texi
> index c6caa19..d50f25c 100644
> --- a/gcc/doc/plugins.texi
> +++ b/gcc/doc/plugins.texi
> @@ -174,6 +174,8 @@ Callbacks can be invoked at the following pre-determined events:
> @smallexample
> enum plugin_event
> @{
> + PLUGIN_START_PARSE_FUNCTION, /* Called before parsing the body of a function. */
> + PLUGIN_FINISH_PARSE_FUNCTION, /* After finishing parsing a function. */
> 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. */
> diff --git a/gcc/plugin.c b/gcc/plugin.c
> index d924438..628833f 100644
> --- a/gcc/plugin.c
> +++ b/gcc/plugin.c
> @@ -441,6 +441,8 @@ register_callback (const char *plugin_name,
> return;
> }
> /* Fall through. */
> + case PLUGIN_START_PARSE_FUNCTION:
> + case PLUGIN_FINISH_PARSE_FUNCTION:
> case PLUGIN_FINISH_TYPE:
> case PLUGIN_FINISH_DECL:
> case PLUGIN_START_UNIT:
> @@ -519,6 +521,8 @@ invoke_plugin_callbacks_full (int event, void *gcc_data)
> gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
> gcc_assert (event < event_last);
> /* Fall through. */
> + case PLUGIN_START_PARSE_FUNCTION:
> + case PLUGIN_FINISH_PARSE_FUNCTION:
> case PLUGIN_FINISH_TYPE:
> case PLUGIN_FINISH_DECL:
> case PLUGIN_START_UNIT:
> diff --git a/gcc/plugin.def b/gcc/plugin.def
> index 98c988a..2a7e4c2 100644
> --- a/gcc/plugin.def
> +++ b/gcc/plugin.def
> @@ -17,6 +17,11 @@ You should have received a copy of the GNU General Public License
> along with GCC; see the file COPYING3. If not see
> <http://www.gnu.org/licenses/>. */
>
> +/* Called before parsing the body of a function. */
> +DEFEVENT (PLUGIN_START_PARSE_FUNCTION)
> +
> +/* After finishing parsing a function. */
> +DEFEVENT (PLUGIN_FINISH_PARSE_FUNCTION)
>
> /* To hook into pass manager. */
> DEFEVENT (PLUGIN_PASS_MANAGER_SETUP)
> diff --git a/gcc/testsuite/g++.dg/plugin/def-plugin-test.C b/gcc/testsuite/g++.dg/plugin/def-plugin-test.C
> new file mode 100644
> index 0000000..b7f2d3d
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/plugin/def-plugin-test.C
> @@ -0,0 +1,13 @@
> +int global = 12;
> +
> +int function1(void);
> +
> +int function2(int a) // { dg-warning "Start fndef function2" }
> +{
> + return function1() + a;
> +} // { dg-warning "Finish fndef function2" }
> +
> +int function1(void) // { dg-warning "Start fndef function1" }
> +{
> + return global + 1;
> +} // { dg-warning "Finish fndef function1" }
> diff --git a/gcc/testsuite/g++.dg/plugin/def_plugin.c b/gcc/testsuite/g++.dg/plugin/def_plugin.c
> new file mode 100644
> index 0000000..63983c5
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/plugin/def_plugin.c
> @@ -0,0 +1,45 @@
> +/* A plugin example that shows which function definitions are caught by PLUGIN_START_FUNCTION and PLUGIN_FINISH_FUNCTION */
> +
> +#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"
> +#include "diagnostic.h"
> +
> +int plugin_is_GPL_compatible;
> +
> +/* Callback function to invoke when GCC starts a function definition*/
> +
> +void plugin_start_parse_function (void *event_data, void *data)
> +{
> + tree fndef = (tree) event_data;
> + warning (0, G_("Start fndef %s"),
> + IDENTIFIER_POINTER (DECL_NAME (fndef)));
> +}
> +
> +/* Callback function to invoke after GCC finishes a function definition. */
> +
> +void plugin_finish_parse_function (void *event_data, void *data)
> +{
> + tree fndef = (tree) event_data;
> + warning (0, G_("Finish fndef %s"),
> + IDENTIFIER_POINTER (DECL_NAME (fndef)));
> +}
> +
> +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_START_PARSE_FUNCTION,
> + plugin_start_parse_function, NULL);
> +
> + register_callback (plugin_name, PLUGIN_FINISH_PARSE_FUNCTION,
> + plugin_finish_parse_function, NULL);
> + return 0;
> +}
> diff --git a/gcc/testsuite/g++.dg/plugin/plugin.exp b/gcc/testsuite/g++.dg/plugin/plugin.exp
> index 1051207..3ed1397 100644
> --- a/gcc/testsuite/g++.dg/plugin/plugin.exp
> +++ b/gcc/testsuite/g++.dg/plugin/plugin.exp
> @@ -61,7 +61,8 @@ set plugin_test_list [list \
> { 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 } \
> - { decl_plugin.c decl-plugin-test.C } ]
> + { decl_plugin.c decl-plugin-test.C } \
> + { def_plugin.c def-plugin-test.C } ]
>
> foreach plugin_test $plugin_test_list {
> # Replace each source file with its full-path name
Hi, I forgot to say that I have legal clearence.
Please someone with writing privileges submit this for me
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH] plugin event for C/C++ function definitions
@ 2015-05-18 20:05 andres.tiraboschi
2015-05-19 12:40 ` Andres Tiraboschi
2015-06-03 13:30 ` Jeff Law
0 siblings, 2 replies; 6+ messages in thread
From: andres.tiraboschi @ 2015-05-18 20:05 UTC (permalink / raw)
To: gcc-patches
Hi, this patch adds two new plugin events PLUGIN_START_PARSE_FUNCTION and PLUGIN_FINISH_PARSE_FUNCTION. These events are invoked at start_function and finish_function in gcc/c/c-decl.c and gcc/cp/decl.c respectively in the C and C++ frontends.
PLUGIN_START_PARSE_FUNCTION is called before parsing a function body.
PLUGIN_FINISH_PARSE_FUNCTION is called after parsing a function definition.
This patch has been implemented in gcc 5.1.0.
changelog:
diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index e28a294..fcc849d 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -8235,6 +8235,7 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
&attributes, NULL, NULL, DEPRECATED_NORMAL);
+ invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
/* If the declarator is not suitable for a function definition,
cause a syntax error. */
@@ -9050,6 +9051,7 @@ finish_function (void)
It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
tree_rest_of_compilation. */
set_cfun (NULL);
+ invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
current_function_decl = NULL;
}
\f
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index c4731ae..bde92cc 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -13727,6 +13727,7 @@ start_function (cp_decl_specifier_seq *declspecs,
tree decl1;
decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
+ invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
if (decl1 == error_mark_node)
return false;
/* If the declarator is not suitable for a function definition,
@@ -14365,6 +14366,7 @@ finish_function (int flags)
vec_free (deferred_mark_used_calls);
}
+ invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
return fndecl;
}
\f
diff --git a/gcc/doc/plugins.texi b/gcc/doc/plugins.texi
index c6caa19..d50f25c 100644
--- a/gcc/doc/plugins.texi
+++ b/gcc/doc/plugins.texi
@@ -174,6 +174,8 @@ Callbacks can be invoked at the following pre-determined events:
@smallexample
enum plugin_event
@{
+ PLUGIN_START_PARSE_FUNCTION, /* Called before parsing the body of a function. */
+ PLUGIN_FINISH_PARSE_FUNCTION, /* After finishing parsing a function. */
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. */
diff --git a/gcc/plugin.c b/gcc/plugin.c
index d924438..628833f 100644
--- a/gcc/plugin.c
+++ b/gcc/plugin.c
@@ -441,6 +441,8 @@ register_callback (const char *plugin_name,
return;
}
/* Fall through. */
+ case PLUGIN_START_PARSE_FUNCTION:
+ case PLUGIN_FINISH_PARSE_FUNCTION:
case PLUGIN_FINISH_TYPE:
case PLUGIN_FINISH_DECL:
case PLUGIN_START_UNIT:
@@ -519,6 +521,8 @@ invoke_plugin_callbacks_full (int event, void *gcc_data)
gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
gcc_assert (event < event_last);
/* Fall through. */
+ case PLUGIN_START_PARSE_FUNCTION:
+ case PLUGIN_FINISH_PARSE_FUNCTION:
case PLUGIN_FINISH_TYPE:
case PLUGIN_FINISH_DECL:
case PLUGIN_START_UNIT:
diff --git a/gcc/plugin.def b/gcc/plugin.def
index 98c988a..2a7e4c2 100644
--- a/gcc/plugin.def
+++ b/gcc/plugin.def
@@ -17,6 +17,11 @@ You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
+/* Called before parsing the body of a function. */
+DEFEVENT (PLUGIN_START_PARSE_FUNCTION)
+
+/* After finishing parsing a function. */
+DEFEVENT (PLUGIN_FINISH_PARSE_FUNCTION)
/* To hook into pass manager. */
DEFEVENT (PLUGIN_PASS_MANAGER_SETUP)
diff --git a/gcc/testsuite/g++.dg/plugin/def-plugin-test.C b/gcc/testsuite/g++.dg/plugin/def-plugin-test.C
new file mode 100644
index 0000000..b7f2d3d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/plugin/def-plugin-test.C
@@ -0,0 +1,13 @@
+int global = 12;
+
+int function1(void);
+
+int function2(int a) // { dg-warning "Start fndef function2" }
+{
+ return function1() + a;
+} // { dg-warning "Finish fndef function2" }
+
+int function1(void) // { dg-warning "Start fndef function1" }
+{
+ return global + 1;
+} // { dg-warning "Finish fndef function1" }
diff --git a/gcc/testsuite/g++.dg/plugin/def_plugin.c b/gcc/testsuite/g++.dg/plugin/def_plugin.c
new file mode 100644
index 0000000..63983c5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/plugin/def_plugin.c
@@ -0,0 +1,45 @@
+/* A plugin example that shows which function definitions are caught by PLUGIN_START_FUNCTION and PLUGIN_FINISH_FUNCTION */
+
+#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"
+#include "diagnostic.h"
+
+int plugin_is_GPL_compatible;
+
+/* Callback function to invoke when GCC starts a function definition*/
+
+void plugin_start_parse_function (void *event_data, void *data)
+{
+ tree fndef = (tree) event_data;
+ warning (0, G_("Start fndef %s"),
+ IDENTIFIER_POINTER (DECL_NAME (fndef)));
+}
+
+/* Callback function to invoke after GCC finishes a function definition. */
+
+void plugin_finish_parse_function (void *event_data, void *data)
+{
+ tree fndef = (tree) event_data;
+ warning (0, G_("Finish fndef %s"),
+ IDENTIFIER_POINTER (DECL_NAME (fndef)));
+}
+
+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_START_PARSE_FUNCTION,
+ plugin_start_parse_function, NULL);
+
+ register_callback (plugin_name, PLUGIN_FINISH_PARSE_FUNCTION,
+ plugin_finish_parse_function, NULL);
+ return 0;
+}
diff --git a/gcc/testsuite/g++.dg/plugin/plugin.exp b/gcc/testsuite/g++.dg/plugin/plugin.exp
index 1051207..3ed1397 100644
--- a/gcc/testsuite/g++.dg/plugin/plugin.exp
+++ b/gcc/testsuite/g++.dg/plugin/plugin.exp
@@ -61,7 +61,8 @@ set plugin_test_list [list \
{ 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 } \
- { decl_plugin.c decl-plugin-test.C } ]
+ { decl_plugin.c decl-plugin-test.C } \
+ { def_plugin.c def-plugin-test.C } ]
foreach plugin_test $plugin_test_list {
# Replace each source file with its full-path name
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2015-06-03 13:28 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-13 17:47 [PATCH] plugin event for C/C++ function definitions Andres Tiraboschi
2014-11-14 10:22 ` Richard Biener
2014-11-18 13:02 ` Andres Tiraboschi
2015-05-18 20:05 andres.tiraboschi
2015-05-19 12:40 ` Andres Tiraboschi
2015-06-03 13:30 ` Jeff Law
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).