From 5ed37f4114eadcbae9f01e6bea88c58a32e8cde5 Mon Sep 17 00:00:00 2001 From: tyfkda Date: Thu, 18 Jul 2024 14:19:04 +0900 Subject: [PATCH] Insert space after ()-ed macro --- src/cpp/macro.c | 3 --- src/cpp/preprocessor.c | 7 ++++++- tests/cpptest.sh | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/cpp/macro.c b/src/cpp/macro.c index afcde1e5c..6de8e60b4 100644 --- a/src/cpp/macro.c +++ b/src/cpp/macro.c @@ -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)); } } diff --git a/src/cpp/preprocessor.c b/src/cpp/preprocessor.c index bc543c18f..2d1f6f058 100644 --- a/src/cpp/preprocessor.c +++ b/src/cpp/preprocessor.c @@ -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. @@ -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; diff --git a/tests/cpptest.sh b/tests/cpptest.sh index 7624386ed..f036356e8 100755 --- a/tests/cpptest.sh +++ b/tests/cpptest.sh @@ -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}" @@ -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)"