Let the plugins influence the pre-processor behavior
This commit is contained in:
parent
5a017c56c3
commit
9340b0da1b
|
@ -30,6 +30,12 @@ typedef struct _C99 C99;
|
|||
|
||||
/* public */
|
||||
/* types */
|
||||
typedef struct _C99Helper
|
||||
{
|
||||
struct _C99 * c99;
|
||||
int (*define_add)(C99 * c99, char const * name, char const * value);
|
||||
} C99Helper;
|
||||
|
||||
typedef struct _C99Option
|
||||
{
|
||||
char const * name;
|
||||
|
@ -63,6 +69,8 @@ C99 * c99_new(C99Prefs const * prefs, char const * pathname);
|
|||
int c99_delete(C99 * c99);
|
||||
|
||||
/* useful */
|
||||
int c99_define_add(C99 * c99, char const * name, char const * value);
|
||||
|
||||
int c99_parse(C99 * c99);
|
||||
|
||||
#endif /* !C99_C99_H */
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
/* types */
|
||||
typedef struct _TargetPlugin
|
||||
{
|
||||
C99Helper * helper;
|
||||
C99Option * options;
|
||||
int (*init)(char const * outfile, int optlevel);
|
||||
int (*exit)(void);
|
||||
|
|
14
src/c99.c
14
src/c99.c
|
@ -44,11 +44,13 @@ C99 * c99_new(C99Prefs const * prefs, char const * pathname)
|
|||
C99 * c99;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "DEBUG: %s()\n", __func__);
|
||||
fprintf(stderr, "DEBUG: %s(\"%s\")\n", __func__, pathname);
|
||||
#endif
|
||||
if((c99 = object_new(sizeof(*c99))) == NULL)
|
||||
return NULL;
|
||||
memset(c99, 0, sizeof(*c99));
|
||||
c99->helper.c99 = c99;
|
||||
c99->helper.define_add = c99_define_add;
|
||||
if((c99->cpp = _new_cpp(prefs, pathname)) == NULL)
|
||||
{
|
||||
object_delete(c99);
|
||||
|
@ -62,7 +64,8 @@ C99 * c99_new(C99Prefs const * prefs, char const * pathname)
|
|||
}
|
||||
if(!(prefs->flags & C99PREFS_E)) /* we're not pre-processing */
|
||||
{
|
||||
if((c99->code = code_new(prefs, c99->outfile)) == NULL)
|
||||
if((c99->code = code_new(prefs, &c99->helper, c99->outfile))
|
||||
== NULL)
|
||||
{
|
||||
c99_delete(c99);
|
||||
return NULL;
|
||||
|
@ -171,6 +174,13 @@ int c99_delete(C99 * c99)
|
|||
|
||||
|
||||
/* useful */
|
||||
/* c99_define_add */
|
||||
int c99_define_add(C99 * c99, char const * name, char const * value)
|
||||
{
|
||||
return cpp_define_add(c99->cpp, name, value);
|
||||
}
|
||||
|
||||
|
||||
/* c99_parse */
|
||||
int c99_parse(C99 * c99)
|
||||
{
|
||||
|
|
|
@ -97,6 +97,7 @@ static int _code_target_label_set(Code * code, char const * name);
|
|||
struct _Code
|
||||
{
|
||||
/* target */
|
||||
C99Helper * helper;
|
||||
Plugin * plugin;
|
||||
TargetPlugin * target;
|
||||
/* types */
|
||||
|
@ -233,7 +234,8 @@ static int _code_target_label_set(Code * code, char const * name)
|
|||
static int _new_target(Code * code, char const * target,
|
||||
C99Option const * options, size_t options_cnt);
|
||||
|
||||
Code * code_new(C99Prefs const * prefs, char const * outfile)
|
||||
Code * code_new(C99Prefs const * prefs, C99Helper * helper,
|
||||
char const * outfile)
|
||||
{
|
||||
Code * code;
|
||||
C99Prefs const * p = prefs;
|
||||
|
@ -244,6 +246,7 @@ Code * code_new(C99Prefs const * prefs, char const * outfile)
|
|||
if((code = object_new(sizeof(*code))) == NULL)
|
||||
return NULL;
|
||||
memset(code, 0, sizeof(*code));
|
||||
code->helper = helper;
|
||||
if(_new_target(code, p->target, p->options, p->options_cnt) != 0
|
||||
|| _code_target_init(code, outfile, p->optlevel) != 0
|
||||
|| code_scope_push(code) != 0)
|
||||
|
@ -272,6 +275,7 @@ static int _new_target(Code * code, char const * target,
|
|||
|| (code->target = plugin_lookup(code->plugin,
|
||||
"target_plugin")) == NULL)
|
||||
return 1;
|
||||
code->target->helper = code->helper;
|
||||
if(code->target->options == NULL)
|
||||
{
|
||||
if(options_cnt == 0)
|
||||
|
|
|
@ -92,7 +92,8 @@ typedef enum _CodeClass
|
|||
|
||||
|
||||
/* functions */
|
||||
Code * code_new(C99Prefs const * prefs, char const * outfile);
|
||||
Code * code_new(C99Prefs const * prefs, C99Helper * helper,
|
||||
char const * outfile);
|
||||
int code_delete(Code * code);
|
||||
|
||||
/* useful */
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
/* types */
|
||||
struct _C99
|
||||
{
|
||||
C99Helper helper;
|
||||
Cpp * cpp;
|
||||
char * outfile;
|
||||
FILE * outfp;
|
||||
|
|
|
@ -18,9 +18,9 @@
|
|||
|
||||
#include <as.h>
|
||||
#include <stdlib.h>
|
||||
#ifdef DEBUG
|
||||
# include <stdio.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "c99/target.h"
|
||||
#ifdef DEBUG
|
||||
# include "../../config.h"
|
||||
|
@ -65,6 +65,7 @@ static int _as_function_end(void);
|
|||
/* variables */
|
||||
TargetPlugin target_plugin =
|
||||
{
|
||||
NULL,
|
||||
_as_options,
|
||||
_as_init,
|
||||
_as_exit,
|
||||
|
@ -80,6 +81,8 @@ TargetPlugin target_plugin =
|
|||
/* protected */
|
||||
/* functions */
|
||||
/* as_init */
|
||||
static int _init_defines(char const * format);
|
||||
|
||||
static int _as_init(char const * outfile, int optlevel)
|
||||
{
|
||||
char const * arch = _as_options[ASO_ARCH].value;
|
||||
|
@ -96,7 +99,9 @@ static int _as_init(char const * outfile, int optlevel)
|
|||
fprintf(stderr, "DEBUG: %s: architecture \"%s\", format \"%s\"\n",
|
||||
PACKAGE, as_get_arch(_as_as), as_get_format(_as_as));
|
||||
#endif
|
||||
if(as_open(_as_as, outfile) != 0 || _as_section(".text") != 0)
|
||||
if(_init_defines(as_get_format(_as_as)) != 0
|
||||
|| as_open(_as_as, outfile) != 0
|
||||
|| _as_section(".text") != 0)
|
||||
{
|
||||
as_delete(_as_as);
|
||||
return 1;
|
||||
|
@ -104,6 +109,29 @@ static int _as_init(char const * outfile, int optlevel)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int _init_defines(char const * format)
|
||||
{
|
||||
C99 * c99;
|
||||
size_t len;
|
||||
char * p;
|
||||
size_t i;
|
||||
int c;
|
||||
|
||||
c99 = target_plugin.helper->c99;
|
||||
len = strlen(format) + 5;
|
||||
if((p = malloc(len)) == NULL)
|
||||
return 1;
|
||||
snprintf(p, len, "%s%s%s", "__", format, "__");
|
||||
for(i = 2; i < len - 2; i++)
|
||||
{
|
||||
c = p[i];
|
||||
p[i] = toupper(c);
|
||||
}
|
||||
target_plugin.helper->define_add(c99, p, NULL);
|
||||
free(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* as_exit */
|
||||
static int _as_exit(void)
|
||||
|
|
|
@ -44,6 +44,7 @@ static int _graph_function_end(void);
|
|||
/* variables */
|
||||
TargetPlugin target_plugin =
|
||||
{
|
||||
NULL, /* helper */
|
||||
NULL, /* options */
|
||||
_graph_init,
|
||||
_graph_exit,
|
||||
|
|
|
@ -51,6 +51,7 @@ static int _indent_function_end(void);
|
|||
/* variables */
|
||||
TargetPlugin target_plugin =
|
||||
{
|
||||
NULL, /* helper */
|
||||
NULL, /* options */
|
||||
_indent_init,
|
||||
_indent_exit,
|
||||
|
|
Loading…
Reference in New Issue
Block a user