Skip to content

Commit

Permalink
Insert space after ()-ed macro
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Jul 23, 2024
1 parent 052adb2 commit 5ed37f4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
3 changes: 0 additions & 3 deletions src/cpp/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,6 @@ void macro_expand(Vector *tokens) {
intersection_hideset(hs, hs2);
hideset_put(hs, tok->ident);
replaced = subst(macro, macro->param_table, args, hs);
} else {
// Insert whitespace to unexpected concatenation.
vec_insert(tokens, ++i, alloc_token(PPTK_SPACE, NULL, " ", NULL));
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/cpp/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,14 @@ static void process_line(const char *line, Stream *stream) {
break;

Token *ident = match(TK_IDENT);
Macro *macro;
if (ident != NULL) {
if (equal_name(ident->ident, defined)) {
// TODO: Raise error if not matched.
match(TK_LPAR);
match(TK_IDENT);
match(TK_RPAR);
} else if (can_expand_ident(ident->ident)) {
} else if ((macro = can_expand_ident(ident->ident)) != NULL) {
const char *p = begin;
begin = ident->end; // Update for EOF callback.

Expand All @@ -172,6 +173,10 @@ static void process_line(const char *line, Stream *stream) {
const Token *tok = tokens->data[i];
fwrite(tok->begin, tok->end - tok->begin, 1, pp_ofp);
}
if (macro->params_len >= 0) {
// Put whitespace to avoid unexpected concatenation.
fputc(' ', pp_ofp);
}
begin = get_lex_p();
}
continue;
Expand Down
5 changes: 3 additions & 2 deletions tests/cpptest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ try() {
begin_test "$title"

local actual
actual=$(echo -e "$input" | $CPP 2>/dev/null | $REMOVE_TOP_LINEMARKER | tr -d '\n')
actual=$(echo -e "$input" | $CPP 2>/dev/null | $REMOVE_TOP_LINEMARKER | tr -d '\n' | sed 's/ $//')
local exitcode=$?
if [ $exitcode -ne 0 ]; then
end_test "CPP failed: exitcode=${exitcode}"
Expand Down Expand Up @@ -126,12 +126,13 @@ test_macro() {

try 'No arguments keeps as is' 'int MAX =123;' "#define MAX(a,b) ((a)>=(b)?(a):(b))\nint MAX=123;"
try '() macro and struct name' 'struct F f;' "#define F(a, b) FF(a, b)\nstruct F f;"
try '()-ed macro insert space' 'void foo(){}' "#define EXTERN(x) x\nEXTERN(void)foo(){}"
try 'Newline in macro' '1+2' "#define ADD(x,y) x+y\nADD(1,\n2)"
try 'Newline in macro2' '(1 + 2)' "#define FOO(x) (x)\nFOO( 1 \n + 2 )"
try 'Newline in macro3' '(123)' "#define FOO(x) (x)\nFOO\n(123)"
try 'Macro w/ str str' '"a" "b"' "#define M(x) x\nM(\"a\" \"b\")"
try 'Block comment after #define' '88' '#define X 88/*block\ncomment*/\nX'
try 'Nothing' 'ABC ' '#define NOTHING /*nothing*/\nABC NOTHING'
try 'Nothing' 'ABC' '#define NOTHING /*nothing*/\nABC NOTHING'
try 'Block comment in macro body removed' 'FOO_abc' "#define M(x) FOO_/**/x\nM(abc)"

try 'recursive macro' 'SELF(123-1)' "#define SELF(n) SELF(n-1)\nSELF(123)"
Expand Down

0 comments on commit 5ed37f4

Please sign in to comment.