maint: fix: do not emit nested comments

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <[email protected]>
I hope I can release 3.6.2 this weekend.

commit c9099a7d7b7f6656d9fabd3db3aacd49178efccf
Author: Akim Demaille <[email protected]>
Date:   Fri May 15 08:13:44 2020 +0200

    fix: do not emit nested comments
    
    With input such as
    
        %token<fl> yVL_CLOCK "/*verilator sc_clock*/"
    
    we generate
    
        yVL_CLOCK = 610,      /* "/*verilator sc_clock*/"  */
    
    which is invalid since the comment will actually be closed on the
    first */.  Let's turn "*/" into "*\/" to avoid this.  But GCC will
    also warn about "/*" inside a comment, so let's "escape" it too.
    
    Reported by Huang Rui.
    https://github.com/akimd/bison/issues/38
    
    * data/skeletons/c-like.m4 (_b4_comment): Escape comment delimiters in
    comments.
    * tests/input.at (Torturing the Scanner): Check thes cases.
    * tests/m4.at: New.

diff --git a/NEWS b/NEWS
index 43d09e8d..23a935fa 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,12 @@ GNU Bison NEWS
 
   Some tests were fixed.
 
+  When token aliases contain comment delimiters:
+
+    %token FOO "/* foo */"
+
+  bison used to emit "nested" comments, which is invalid C.
+
 * Noteworthy changes in release 3.6.1 (2020-05-10) [stable]
 
 ** Bug fixes
diff --git a/data/skeletons/c-like.m4 b/data/skeletons/c-like.m4
index e0460d48..fb0dc53b 100644
--- a/data/skeletons/c-like.m4
+++ b/data/skeletons/c-like.m4
@@ -22,10 +22,14 @@
 # Put TEXT in comment.  Avoid trailing spaces: don't indent empty lines.
 # Avoid adding indentation to the first line, as the indentation comes
 # from OPEN.  That's why we don't patsubst([$1], [^\(.\)], [   \1]).
+# Turn "*/" in TEXT into "* /" so that we don't unexpectedly close
+# the comments before its end.
 #
 # Prefix all the output lines with PREFIX.
 m4_define([_b4_comment],
-[$2[]m4_bpatsubst(m4_expand([[$1]]), [
+[$2[]m4_bpatsubsts(m4_expand([$1]),
+                   [[*]/], [*\\/],
+                   [/[*]], [/\\*], [
 \(.\)], [
 $3\1])$4])
 
diff --git a/tests/input.at b/tests/input.at
index 77fa3e59..cc72ddc8 100644
--- a/tests/input.at
+++ b/tests/input.at
@@ -1424,6 +1424,11 @@ char quote[] = "@:>@@:>@,";
 /* Exercise quotes in strings.  */
 %token FAKE "fake @<:@@:>@ \a\b\f\n\r\t\v\"\'\?\\\u005B\U0000005c ??!??'??(??)??-??/??<??=??> \x1\1"
 
+/* Beware of the generated comments that embed string aliases that
+   might close the comment.  */
+%token COMMENT_CLOSE "*/"
+%token COMMENT       "/* comment */"
+
 %%
 /* Exercise M4 quoting: '@:>@@:>@', @<:@, 1.  */
 exp: '@<:@' '\1' two '$' '@' '{' oline output.or.oline.opt
diff --git a/tests/local.mk b/tests/local.mk
index fac8426f..a62fe267 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -56,6 +56,7 @@ TESTSUITE_AT =                                \
   %D%/java.at                                 \
   %D%/javapush.at                             \
   %D%/local.at                                \
+  %D%/m4.at                                   \
   %D%/named-refs.at                           \
   %D%/output.at                               \
   %D%/package.m4                              \
diff --git a/tests/m4.at b/tests/m4.at
new file mode 100644
index 00000000..5f59c9b2
--- /dev/null
+++ b/tests/m4.at
@@ -0,0 +1,46 @@
+# Basic m4 macros.                               -*- Autotest -*-
+
+# Copyright (C) 2020 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+AT_BANNER([[M4 Macros.]])
+
+
+AT_SETUP([Generating Comments])
+
+AT_DATA([input.y],
+[%%
+exp:
+])
+
+AT_DATA([input.m4],
+[[m4@&t@_include(b4_skeletonsdir/[c.m4])
+
+b4_output_begin([output.txt])
+b4_comment([["/* () */"]])
+b4_comment([["/* (  */"]])
+b4_comment([["/*  ) */"]])
+b4_output_end([output.txt])
+]])
+
+AT_BISON_CHECK([-S ./input.m4 input.y])
+
+AT_CHECK([cat output.txt], [],
+[/* "/\* () *\/"  */
+/* "/\* (  *\/"  */
+/* "/\*  ) *\/"  */
+])
+
+AT_CLEANUP
\ No newline at end of file
diff --git a/tests/testsuite.at b/tests/testsuite.at
index e2b72b84..44393db5 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -16,6 +16,8 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+# Basic M4 macros.
+m4_include([m4.at])
 
 # Resistance to user bugs.
 m4_include([input.at])
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.