From patchwork Fri Jun 10 09:10:14 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre Vittet X-Patchwork-Id: 99860 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 62EB2B6FDD for ; Fri, 10 Jun 2011 19:10:55 +1000 (EST) Received: (qmail 7348 invoked by alias); 10 Jun 2011 09:10:53 -0000 Received: (qmail 7336 invoked by uid 22791); 10 Jun 2011 09:10:51 -0000 X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE X-Spam-Check-By: sourceware.org Received: from 8.27.mail-out.ovh.net (HELO 27.mail-out.ovh.net) (188.165.39.131) by sourceware.org (qpsmtpd/0.43rc1) with SMTP; Fri, 10 Jun 2011 09:10:35 +0000 Received: (qmail 15604 invoked by uid 503); 10 Jun 2011 10:07:19 -0000 Received: from b9.ovh.net (HELO mail95.ha.ovh.net) (213.186.33.59) by 27.mail-out.ovh.net with SMTP; 10 Jun 2011 10:07:14 -0000 Received: from b0.ovh.net (HELO queueout) (213.186.33.50) by b0.ovh.net with SMTP; 10 Jun 2011 11:10:27 +0200 Received: from unknown (HELO ?85.69.164.208?) (piervit@pvittet.com@85.69.164.208) by ns0.ovh.net with SMTP; 10 Jun 2011 11:10:26 +0200 Message-ID: <4DF1DF76.4020109@pvittet.com> Date: Fri, 10 Jun 2011 11:10:14 +0200 From: Pierre Vittet User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100515 Lightning/1.0b1 Icedove/3.0.4 MIME-Version: 1.0 To: gcc-patches@gcc.gnu.org CC: joseph@codesourcery.com, tromey@redhat.com, Basile Starynkevitch Subject: Re: [PATCH] c-pragma: adding a data field to pragma_handler References: <4DE66ECE.5030102@laposte.net> <4DE8FE4D.6050404@pvittet.com> <4DEFE90F.5020607@pvittet.com> <20110609081635.6a3c0ad2.basile@starynkevitch.net> <4DF08B94.1010804@pvittet.com> In-Reply-To: X-Ovh-Tracer-Id: 16105435218162155678 X-Ovh-Remote: 85.69.164.208 () X-Ovh-Local: 213.186.33.20 (ns0.ovh.net) X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org thanks! I formatted as you requested. I cannot commit myself as I haven't a "write after approval" status, maye you can do it, or I can wait my GSOC mentor, Basile Starynkevitch to do this (He mights be busy for a few days). Pierre Vittet Index: gcc/testsuite/g++.dg/plugin/plugin.exp =================================================================== --- gcc/testsuite/g++.dg/plugin/plugin.exp (revision 174521) +++ gcc/testsuite/g++.dg/plugin/plugin.exp (working copy) @@ -49,6 +49,7 @@ load_lib plugin-support.exp set plugin_test_list [list \ { attribute_plugin.c attribute_plugin-test-1.C } \ { pragma_plugin.c pragma_plugin-test-1.C } \ + { pragma_plugin_with_data.c pragma_plugin_with_data-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 } ] Index: gcc/testsuite/g++.dg/plugin/pragma_plugin_with_data.c =================================================================== --- gcc/testsuite/g++.dg/plugin/pragma_plugin_with_data.c (revision 0) +++ gcc/testsuite/g++.dg/plugin/pragma_plugin_with_data.c (revision 0) @@ -0,0 +1,100 @@ +/* Test pragma adding using an extra data field. */ + +#include "gcc-plugin.h" +#include +#include "function.h" +#include "c-family/c-pragma.h" +#include "intl.h" + + + +int plugin_is_GPL_compatible; + + + +#define GCC_PRAGMA_BAD (gmsgid) \ + do { warning (OPT_Wpragmas, gmsgid); goto end; } while (0) + +/* handler of #pragma SPACE NAME command (arg1, arg2, ...) or of type + #pragma SPACE NAME command. */ +static void +handle_pragma_gen (cpp_reader *ARG_UNUSED (dummy), void *data) +{ + char *plugin_name = (char *) data; + + warning (0, G_("The name of the pass: %s"), + plugin_name); + + enum cpp_ttype token; + tree x; + const char *command; + + token = pragma_lex (&x); + if (token != CPP_NAME) + GCC_PRAGMA_BAD ("malformed #pragma melt, ignored"); + + command = IDENTIFIER_POINTER (x); + + /* If the pragma has the form #pragma SPACE NAME cmd (...) then command + contains "cmd". Next element should be a parenthese opening or an end of + line. */ + token = pragma_lex (&x); + if (token != CPP_OPEN_PAREN) + { + if (token != CPP_EOF) + GCC_PRAGMA_BAD ("malformed #pragma melt, ignored"); + + else /* we have a pragma of the type '#pragma SPACE NAME command'. */ + { + /* We have recovered our pragma. */ + warning (OPT_Wpragmas, "%<#pragma GCCPLUGIN newPragma%> a simple \ +instruction: %s", command); + return; + } + } + else + { /* opening parenthesis. */ + do + { + token = pragma_lex (&x); + if (token != CPP_NAME && token != CPP_STRING && token != CPP_NUMBER) + GCC_PRAGMA_BAD ("malformed #pragma melt, ignored"); + token = pragma_lex (&x); + } while (token == CPP_COMMA); + + if (token == CPP_CLOSE_PAREN && pragma_lex (&x) == CPP_EOF) + { + /* We have recovered our pragma. */ + warning (OPT_Wpragmas, "%<#pragma GCCPLUGIN newPragma%> a more complex \ +instruction: %s", command); + return; + } + else + GCC_PRAGMA_BAD ("malformed #pragma melt, ignored"); + } +end: return; +} + +/* Plugin callback called during pragma registration. */ + +static void +register_my_pragma (void *event_data, void *data) +{ + warning (0, G_("Callback to register pragmas")); + c_register_pragma_with_data ("GCCPLUGIN", "newPragma", + handle_pragma_gen, data); +} + +/* Init the plugin. */ +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_PRAGMAS, register_my_pragma, + (void *) plugin_name); + return 0; +} + +#undef GCC_PRAGMA_BAD Index: gcc/testsuite/g++.dg/plugin/pragma_plugin_with_data-test-1.C =================================================================== --- gcc/testsuite/g++.dg/plugin/pragma_plugin_with_data-test-1.C (revision 0) +++ gcc/testsuite/g++.dg/plugin/pragma_plugin_with_data-test-1.C (revision 0) @@ -0,0 +1,8 @@ +// { dg-warning "Callback to register pragmas" "" { target *-*-* } 0 } +// { dg-warning "The name of the pass: pragma_plugin_with_data" "" {target *-*-* } 5 } +// { dg-warning "The name of the pass: pragma_plugin_with_data" "" {target *-*-* } 7 } + +#pragma GCCPLUGIN newPragma do_smt // { dg-warning "'#pragma GCCPLUGIN newPragma' a simple instruction: do_smt" } + +#pragma GCCPLUGIN newPragma do_smt_else (test ,"test", 2) // { dg-warning "'#pragma GCCPLUGIN newPragma' a more complex instruction: do_smt_else" } + Index: gcc/c-family/c-pragma.c =================================================================== --- gcc/c-family/c-pragma.c (revision 174521) +++ gcc/c-family/c-pragma.c (working copy) @@ -1147,13 +1147,12 @@ handle_pragma_float_const_decimal64 (cpp_reader *A } } -/* A vector of registered pragma callbacks. */ +/* A vector of registered pragma callbacks, which is never freed. */ +DEF_VEC_O (internal_pragma_handler); +DEF_VEC_ALLOC_O (internal_pragma_handler, heap); -DEF_VEC_O (pragma_handler); -DEF_VEC_ALLOC_O (pragma_handler, heap); +static VEC(internal_pragma_handler, heap) *registered_pragmas; -static VEC(pragma_handler, heap) *registered_pragmas; - typedef struct { const char *space; @@ -1216,7 +1215,7 @@ c_pp_lookup_pragma (unsigned int id, const char ** static void c_register_pragma_1 (const char *space, const char *name, - pragma_handler handler, bool allow_expansion) + internal_pragma_handler ihandler, bool allow_expansion) { unsigned id; @@ -1235,8 +1234,9 @@ c_register_pragma_1 (const char *space, const char } else { - VEC_safe_push (pragma_handler, heap, registered_pragmas, &handler); - id = VEC_length (pragma_handler, registered_pragmas); + VEC_safe_push (internal_pragma_handler, heap, registered_pragmas, + &ihandler); + id = VEC_length (internal_pragma_handler, registered_pragmas); id += PRAGMA_FIRST_EXTERNAL - 1; /* The C++ front end allocates 6 bits in cp_token; the C front end @@ -1248,28 +1248,95 @@ c_register_pragma_1 (const char *space, const char allow_expansion, false); } +/* Register a C pragma handler, using a space and a name. It disallows pragma + expansion (if you want it, use c_register_pragma_with_expansion instead). */ void -c_register_pragma (const char *space, const char *name, pragma_handler handler) +c_register_pragma (const char *space, const char *name, + pragma_handler_1arg handler) { - c_register_pragma_1 (space, name, handler, false); + internal_pragma_handler ihandler; + + ihandler.handler.handler_1arg = handler; + ihandler.extra_data = false; + ihandler.data = NULL; + c_register_pragma_1 (space, name, ihandler, false); } +/* Register a C pragma handler, using a space and a name, it also carries an + extra data field which can be used by the handler. It disallows pragma + expansion (if you want it, use c_register_pragma_with_expansion_and_data + instead). */ void +c_register_pragma_with_data (const char *space, const char *name, + pragma_handler_2arg handler, void * data) +{ + internal_pragma_handler ihandler; + + ihandler.handler.handler_2arg = handler; + ihandler.extra_data = true; + ihandler.data = data; + c_register_pragma_1 (space, name, ihandler, false); +} + +/* Register a C pragma handler, using a space and a name. It allows pragma + expansion as in the following example: + + #define NUMBER 10 + #pragma count (NUMBER) + + Name expansion is still disallowed. */ +void c_register_pragma_with_expansion (const char *space, const char *name, - pragma_handler handler) + pragma_handler_1arg handler) { - c_register_pragma_1 (space, name, handler, true); + internal_pragma_handler ihandler; + + ihandler.handler.handler_1arg = handler; + ihandler.extra_data = false; + ihandler.data = NULL; + c_register_pragma_1 (space, name, ihandler, true); } +/* Register a C pragma handler, using a space and a name, it also carries an + extra data field which can be used by the handler. It allows pragma + expansion as in the following example: + + #define NUMBER 10 + #pragma count (NUMBER) + + Name expansion is still disallowed. */ void +c_register_pragma_with_expansion_and_data (const char *space, const char *name, + pragma_handler_2arg handler, + void *data) +{ + internal_pragma_handler ihandler; + + ihandler.handler.handler_2arg = handler; + ihandler.extra_data = true; + ihandler.data = data; + c_register_pragma_1 (space, name, ihandler, true); +} + +void c_invoke_pragma_handler (unsigned int id) { - pragma_handler handler; + internal_pragma_handler *ihandler; + pragma_handler_1arg handler_1arg; + pragma_handler_2arg handler_2arg; id -= PRAGMA_FIRST_EXTERNAL; - handler = *VEC_index (pragma_handler, registered_pragmas, id); - - handler (parse_in); + ihandler = VEC_index (internal_pragma_handler, registered_pragmas, id); + if (ihandler->extra_data) + { + handler_2arg = ihandler->handler.handler_2arg; + handler_2arg (parse_in, ihandler->data); + } + else + { + handler_1arg = ihandler->handler.handler_1arg; + handler_1arg (parse_in); + } } /* Set up front-end pragmas. */ @@ -1308,7 +1375,8 @@ init_pragma (void) c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64", handle_pragma_float_const_decimal64); - c_register_pragma_with_expansion (0, "redefine_extname", handle_pragma_redefine_extname); + c_register_pragma_with_expansion (0, "redefine_extname", + handle_pragma_redefine_extname); c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix); c_register_pragma_with_expansion (0, "message", handle_pragma_message); Index: gcc/c-family/c-pragma.h =================================================================== --- gcc/c-family/c-pragma.h (revision 174521) +++ gcc/c-family/c-pragma.h (working copy) @@ -84,10 +84,40 @@ extern bool pop_visibility (int); extern void init_pragma (void); /* Front-end wrappers for pragma registration. */ -typedef void (*pragma_handler)(struct cpp_reader *); -extern void c_register_pragma (const char *, const char *, pragma_handler); -extern void c_register_pragma_with_expansion (const char *, const char *, - pragma_handler); +typedef void (*pragma_handler_1arg)(struct cpp_reader *); +/* A second pragma handler, which adds a void * argument allowing to pass extra + data to the handler. */ +typedef void (*pragma_handler_2arg)(struct cpp_reader *, void *); + +/* This union allows to abstract the different handlers. */ +union gen_pragma_handler { + pragma_handler_1arg handler_1arg; + pragma_handler_2arg handler_2arg; +}; +/* Internally used to keep the data of the handler. */ +struct internal_pragma_handler_d { + union gen_pragma_handler handler; + /* Permits to know if handler is a pragma_handler_1arg (extra_data is false) + or a pragma_handler_2arg (extra_data is true). */ + bool extra_data; + /* A data field which can be used when extra_data is true. */ + void * data; +}; +typedef struct internal_pragma_handler_d internal_pragma_handler; + +extern void c_register_pragma (const char *space, const char *name, + pragma_handler_1arg handler); +extern void c_register_pragma_with_data (const char *space, const char *name, + pragma_handler_2arg handler, + void *data); + +extern void c_register_pragma_with_expansion (const char *space, + const char *name, + pragma_handler_1arg handler); +extern void c_register_pragma_with_expansion_and_data (const char *space, + const char *name, + pragma_handler_2arg handler, + void *data); extern void c_invoke_pragma_handler (unsigned int); extern void maybe_apply_pragma_weak (tree);