mirror of
https://github.com/DarkPlacesEngine/gmqcc.git
synced 2025-02-26 05:01:26 +00:00
Implement support for indirect macro expansions in the preprocessor. This closes #36
This commit is contained in:
parent
faacfa018a
commit
2208136403
7 changed files with 64 additions and 4 deletions
19
doc/gmqcc.1
19
doc/gmqcc.1
|
@ -426,6 +426,25 @@ M_SQRT2
|
||||||
M_SQRT1_2
|
M_SQRT1_2
|
||||||
M_TAU
|
M_TAU
|
||||||
.Ed
|
.Ed
|
||||||
|
.It Fl f Ns Cm ftepp-indirect-expansion
|
||||||
|
Enable indirect macro expansion. This only works in combination
|
||||||
|
with '-fftepp' and is currently not included by '-std=fteqcc'.
|
||||||
|
Enabling this behavior will allow the preprocessor to operate more
|
||||||
|
like the standard C preprocessor in that it will allow arguments
|
||||||
|
of macros which are macro-expanded to be substituted into the
|
||||||
|
definition of the macro.
|
||||||
|
.Pp
|
||||||
|
As an example:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
#define STR1(x) #x
|
||||||
|
#define STR2(x) STR1(x)
|
||||||
|
#define THE_ANSWER 42
|
||||||
|
#define THE_ANSWER_STR STR2(THE_ANSWER) /* "42" */
|
||||||
|
|
||||||
|
.Ed
|
||||||
|
With this enabled, an expansion of THE_ANSWER_STR will yield
|
||||||
|
the string "42". With this disabled an expansion of THE_ANSWER_STR
|
||||||
|
will yield "THE_ANSWER"
|
||||||
.It Fl f Ns Cm relaxed-switch
|
.It Fl f Ns Cm relaxed-switch
|
||||||
Allow switch cases to use non constant variables.
|
Allow switch cases to use non constant variables.
|
||||||
.It Fl f Ns Cm short-logic
|
.It Fl f Ns Cm short-logic
|
||||||
|
|
6
ftepp.c
6
ftepp.c
|
@ -734,6 +734,7 @@ static void ftepp_recursion_footer(ftepp_t *ftepp)
|
||||||
ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
|
ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline);
|
||||||
static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
|
static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
@ -742,9 +743,14 @@ static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
|
||||||
out = param->tokens[i];
|
out = param->tokens[i];
|
||||||
if (out->token == TOKEN_EOL)
|
if (out->token == TOKEN_EOL)
|
||||||
ftepp_out(ftepp, "\n", false);
|
ftepp_out(ftepp, "\n", false);
|
||||||
|
else {
|
||||||
|
ppmacro *find = ftepp_macro_find(ftepp, out->value);
|
||||||
|
if (OPTS_FLAG(FTEPP_INDIRECT_EXPANSION) && find && !find->has_params)
|
||||||
|
ftepp_macro_expand(ftepp, find, NULL, false);
|
||||||
else
|
else
|
||||||
ftepp_out(ftepp, out->value, false);
|
ftepp_out(ftepp, out->value, false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ftepp_preprocess(ftepp_t *ftepp);
|
static bool ftepp_preprocess(ftepp_t *ftepp);
|
||||||
|
|
|
@ -84,6 +84,25 @@
|
||||||
FTEPP_MATHDEFS = false
|
FTEPP_MATHDEFS = false
|
||||||
|
|
||||||
|
|
||||||
|
#Enable indirect macro expansion. This only works in combination
|
||||||
|
#with '-fftepp' and is currently not included by '-std=fteqcc'.
|
||||||
|
#Enabling this behavior will allow the preprocessor to operate more
|
||||||
|
#like the standard C preprocessor in that it will allow arguments
|
||||||
|
#of macros which are macro-expanded to be substituted into the
|
||||||
|
#definition of the macro. As an example:
|
||||||
|
#
|
||||||
|
# #define STR1(x) #x
|
||||||
|
# #define STR2(x) STR1(x)
|
||||||
|
# #define THE_ANSWER 42
|
||||||
|
# #define THE_ANSWER_STR STR2(THE_ANSWER) /* "42" */
|
||||||
|
#
|
||||||
|
#With this enabled, an expansion of THE_ANSWER_STR will yield
|
||||||
|
#the string "42". With this disabled an expansion of THE_ANSWER_STR
|
||||||
|
#will yield "THE_ANSWER"
|
||||||
|
|
||||||
|
FTEPP_INDIRECT_EXPANSION = false
|
||||||
|
|
||||||
|
|
||||||
#Allow switch cases to use non constant variables.
|
#Allow switch cases to use non constant variables.
|
||||||
|
|
||||||
RELAXED_SWITCH = true
|
RELAXED_SWITCH = true
|
||||||
|
|
1
opts.def
1
opts.def
|
@ -32,6 +32,7 @@
|
||||||
GMQCC_DEFINE_FLAG(FTEPP)
|
GMQCC_DEFINE_FLAG(FTEPP)
|
||||||
GMQCC_DEFINE_FLAG(FTEPP_PREDEFS)
|
GMQCC_DEFINE_FLAG(FTEPP_PREDEFS)
|
||||||
GMQCC_DEFINE_FLAG(FTEPP_MATHDEFS)
|
GMQCC_DEFINE_FLAG(FTEPP_MATHDEFS)
|
||||||
|
GMQCC_DEFINE_FLAG(FTEPP_INDIRECT_EXPANSION)
|
||||||
GMQCC_DEFINE_FLAG(RELAXED_SWITCH)
|
GMQCC_DEFINE_FLAG(RELAXED_SWITCH)
|
||||||
GMQCC_DEFINE_FLAG(SHORT_LOGIC)
|
GMQCC_DEFINE_FLAG(SHORT_LOGIC)
|
||||||
GMQCC_DEFINE_FLAG(PERL_LOGIC)
|
GMQCC_DEFINE_FLAG(PERL_LOGIC)
|
||||||
|
|
6
test.c
6
test.c
|
@ -806,19 +806,21 @@ static bool task_propagate(const char *curdir, size_t *pad, const char *defs) {
|
||||||
} else {
|
} else {
|
||||||
/* Preprocessing (qcflags mean shit all here we don't allow them) */
|
/* Preprocessing (qcflags mean shit all here we don't allow them) */
|
||||||
if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) {
|
if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) {
|
||||||
util_snprintf(buf, sizeof(buf), "%s -E %s/%s -o %s",
|
util_snprintf(buf, sizeof(buf), "%s -E %s/%s %s -o %s",
|
||||||
task_bins[TASK_COMPILE],
|
task_bins[TASK_COMPILE],
|
||||||
directories[i],
|
directories[i],
|
||||||
tmpl->sourcefile,
|
tmpl->sourcefile,
|
||||||
|
tmpl->compileflags,
|
||||||
tmpl->tempfilename
|
tmpl->tempfilename
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
util_snprintf(buf, sizeof(buf), "%s -E %s/%s %s/%s -o %s",
|
util_snprintf(buf, sizeof(buf), "%s -E %s/%s %s/%s %s -o %s",
|
||||||
task_bins[TASK_COMPILE],
|
task_bins[TASK_COMPILE],
|
||||||
curdir,
|
curdir,
|
||||||
defs,
|
defs,
|
||||||
directories[i],
|
directories[i],
|
||||||
tmpl->sourcefile,
|
tmpl->sourcefile,
|
||||||
|
tmpl->compileflags,
|
||||||
tmpl->tempfilename
|
tmpl->tempfilename
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
6
tests/ppindirectexpand.qc
Normal file
6
tests/ppindirectexpand.qc
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#define STR1(x) #x
|
||||||
|
#define STR2(x) STR1(x)
|
||||||
|
#define THE_ANSWER 42
|
||||||
|
#define THE_ANSWER_STR STR2(THE_ANSWER)
|
||||||
|
|
||||||
|
THE_ANSWER_STR
|
7
tests/ppindirectexpand.tmpl
Normal file
7
tests/ppindirectexpand.tmpl
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
I: ppindirectexpand.qc
|
||||||
|
D: test preprocessor indirect macro expansion
|
||||||
|
T: -pp
|
||||||
|
C: -fftepp-indirect-expansion -std=gmqcc
|
||||||
|
F: -no-defs
|
||||||
|
M: "42"
|
||||||
|
|
Loading…
Reference in a new issue