From 0aceca8673c1d9a8bc34faa460389768efa08eb7 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 1 Nov 2022 14:18:00 +0000 Subject: make: comments in macro expansions and command lines The POSIX specification says: There are three kinds of comments: blank lines, empty lines, and a ('#') and all following characters up to the first unescaped character. Most implementations don't treat '#' in a macro expansion or a command line as the start of a comment. POSIX doesn't mention either of these exceptions. Permit the exceptions as a non-POSIX extension. --- miscutils/make.c | 19 ++++++++++++++++--- testsuite/make.tests | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/miscutils/make.c b/miscutils/make.c index 111ab2914..daad75f5c 100644 --- a/miscutils/make.c +++ b/miscutils/make.c @@ -1120,7 +1120,11 @@ process_line(char *s) r = s; // Strip comment - t = strchr(s, '#'); + // don't treat '#' in macro expansion as a comment + if (!posix) + t = find_char(s, '#'); + else + t = strchr(s, '#'); if (t) *t = '\0'; @@ -1401,9 +1405,18 @@ static char * process_command(char *s) { char *t, *u; - int len = strlen(s) + 1; - char *outside = xzalloc(len); + int len; + char *outside; + + if (posix) { + // POSIX strips comments from command lines + t = strchr(s, '#'); + if (t) + *t = '\0'; + } + len = strlen(s) + 1; + outside = xzalloc(len); for (t = skip_macro(s); *t; t = skip_macro(t + 1)) { outside[t - s] = 1; } diff --git a/testsuite/make.tests b/testsuite/make.tests index a0ca9778b..f52770438 100755 --- a/testsuite/make.tests +++ b/testsuite/make.tests @@ -379,6 +379,24 @@ t1? t2* t3[abc]: s1? s2* s3[abc] ' cd .. || exit 1; rm -rf make.tempdir 2>/dev/null +# A '#' character in a macro expansion doesn't start a comment +testing "make hash in macro expansion isn't a comment" \ + "make -f -" \ + ": hash # hash\n" "" ' +HASH = hash +hash = $(HASH:hash=#) +target: + : hash $(hash) hash +' + +# A '#' character in a command line doesn't start a comment +testing "make hash in command line isn't a comment" \ + "make -f -" \ + ": hash # hash\n" "" ' +target: + : hash # hash +' + # Skip duplicate entries in $? and $^ mkdir make.tempdir && cd make.tempdir || exit 1 touch -t 202206171200 file1 file3 -- cgit v1.2.3-55-g6feb