Skip to content

Commit 3fe5b3f

Browse files
author
H. Peter Anvin
committed
preproc: distinguish between directives and functions
Some preprocessor functions have the same name as directives. In those cases, they should be expanded as functions if and only if they are followed by a left parenthesis. Although it is not inherently true that either preprocessor functions require a paren nor that directives cannot start with one, but it is true and will remain true for all cases where there is a namespace collision. Signed-off-by: H. Peter Anvin <[email protected]>
1 parent 359e21e commit 3fe5b3f

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

asm/pptok.pl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/perl
22
## --------------------------------------------------------------------------
33
##
4-
## Copyright 1996-2020 The NASM Authors - All Rights Reserved
4+
## Copyright 1996-2022 The NASM Authors - All Rights Reserved
55
## See the file AUTHORS included with the NASM distribution for
66
## the specific copyright holders.
77
##
@@ -132,7 +132,8 @@
132132
}
133133
$n++;
134134
}
135-
printf OUT " %-24s = %3d\n", 'PP_INVALID', -1;
135+
printf OUT " %-24s = %3d,\n", 'PP_count', $n;
136+
printf OUT " %-24s = %3d\n", 'PP_invalid', -1;
136137
print OUT "};\n";
137138
print OUT "\n";
138139

@@ -257,11 +258,11 @@
257258
print OUT "\n";
258259
print OUT " ix = hashdata[k1] + hashdata[k2];\n";
259260
printf OUT " if (ix >= %d)\n", scalar(@pptok);
260-
print OUT " return PP_INVALID;\n";
261+
print OUT " return PP_invalid;\n";
261262
print OUT "\n";
262263

263264
print OUT " if (!pp_directives[ix] || nasm_stricmp(pp_directives[ix], token))\n";
264-
print OUT " return PP_INVALID;\n";
265+
print OUT " return PP_invalid;\n";
265266
print OUT "\n";
266267
print OUT " return ix;\n";
267268
print OUT "}\n";
@@ -315,11 +316,11 @@
315316
# Comparing to pptok here is correct, because this hash produces
316317
# an enum preproc_token value directly.
317318
printf OUT " if (ix >= %d)\n", scalar(@pptok);
318-
print OUT " return PP_INVALID;\n";
319+
print OUT " return PP_invalid;\n";
319320
print OUT "\n";
320321

321322
print OUT " if (!pp_directives[ix] || nasm_stricmp(pp_directives[ix]+1, token))\n";
322-
print OUT " return PP_INVALID;\n";
323+
print OUT " return PP_invalid;\n";
323324
print OUT "\n";
324325
print OUT " return ix;\n";
325326
print OUT "}\n";

asm/preproc.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ typedef struct Line Line;
111111
typedef struct Include Include;
112112
typedef struct Cond Cond;
113113

114+
/*
115+
* Map of preprocessor directives that are also preprocessor functions;
116+
* if they are at the beginning of a line they are a function if and
117+
* only if they are followed by a (
118+
*/
119+
static bool pp_op_may_be_function[PP_count];
120+
114121
/*
115122
* This is the internal form which we break input lines up into.
116123
* Typically stored in linked lists.
@@ -3757,7 +3764,7 @@ static int do_directive(Token *tline, Token **output)
37573764
}
37583765

37593766
switch (op) {
3760-
case PP_INVALID:
3767+
case PP_invalid:
37613768
return NO_DIRECTIVE_FOUND;
37623769

37633770
case PP_LINE:
@@ -3828,6 +3835,11 @@ static int do_directive(Token *tline, Token **output)
38283835
}
38293836
}
38303837

3838+
if (pp_op_may_be_function[op]) {
3839+
if (tok_is(skip_white(tline->next), '('))
3840+
return NO_DIRECTIVE_FOUND; /* Expand as a preprocessor function */
3841+
}
3842+
38313843
switch (op) {
38323844
default:
38333845
nasm_nonfatal("unknown preprocessor directive `%s'", dname);
@@ -7001,6 +7013,11 @@ static void pp_add_magic_stdmac(void)
70017013
}
70027014
}
70037015
define_smacro(m->name, m->casesense, NULL, &tmpl);
7016+
if (m->name[0] == '%') {
7017+
enum preproc_token op = pp_token_hash(m->name);
7018+
if (op != PP_invalid)
7019+
pp_op_may_be_function[op] = true;
7020+
}
70047021
}
70057022

70067023
/* %is...() macro functions */

0 commit comments

Comments
 (0)