Handle C-style comments following a `include directive (issue #782)

This commit is contained in:
Martin Whitaker 2022-10-16 22:03:37 +01:00
parent 10a39e59a1
commit 01a11cce56
5 changed files with 46 additions and 3 deletions

View File

@ -1,7 +1,7 @@
%option prefix="yy"
%{
/*
* Copyright (c) 1999-2021 Stephen Williams (steve@icarus.com)
* Copyright (c) 1999-2022 Stephen Williams (steve@icarus.com)
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
@ -187,6 +187,7 @@ static int ma_parenthesis_level = 0;
%option noyywrap
%x PPINCLUDE
%x PPCCOMMENT
%x DEF_NAME
%x DEF_ESC
%x DEF_ARG
@ -307,9 +308,23 @@ keywords (line|include|define|undef|ifdef|ifndef|else|elsif|endif)
/* Catch single-line comments that share the line with an include
* directive. And while I'm at it, I might as well preserve the
* comment in the output stream. This will be printed after the
* file has been included.
* file has been included. For simplicity, if there is more than
* one comment on the line, only the first one will be preserved.
*/
<PPINCLUDE>"//"[^\r\n]* { standby->comment = strdup(yytext); }
<PPINCLUDE>"//"[^\r\n]* |
<PPINCLUDE>"/*"[^\r\n]*"*/" { if (!standby->comment) standby->comment = strdup(yytext); }
/* Now catch the start of a multi-line comment. In this case we
* discard the comment then execute the inclusion.
*/
<PPINCLUDE>"/*" { BEGIN(PPCCOMMENT); }
<PPCCOMMENT>[^\r\n] {}
<PPCCOMMENT>\n\r |
<PPCCOMMENT>\r\n |
<PPCCOMMENT>\n |
<PPCCOMMENT>\r { istack->lineno += 1; }
<PPCCOMMENT>"*/" { yy_pop_state(); do_include(); }
/* These finish the include directive (EOF or EOL) so I revert the
* lexor state and execute the inclusion.

View File

@ -0,0 +1,9 @@
File ./ivltests/br_gh782a.v line 4
Included file ./ivltests/br_gh782a.vi line 1
File ./ivltests/br_gh782a.v line 6
Included file ./ivltests/br_gh782a.vi line 1
File ./ivltests/br_gh782a.v line 8
Included file ./ivltests/br_gh782a.vi line 1
File ./ivltests/br_gh782a.v line 11
Included file ./ivltests/br_gh782a.vi line 1
File ./ivltests/br_gh782a.v line 14

View File

@ -0,0 +1,17 @@
module test;
initial begin
$display("File %s line %0d", `__FILE__, `__LINE__);
`include "ivltests/br_gh782a.vi" // single line comment
$display("File %s line %0d", `__FILE__, `__LINE__);
`include "ivltests/br_gh782a.vi" /* another single line comment */
$display("File %s line %0d", `__FILE__, `__LINE__);
`include "ivltests/br_gh782a.vi" /* multi-line
comment */
$display("File %s line %0d", `__FILE__, `__LINE__);
`include "ivltests/br_gh782a.vi" /* single */ /* and
multi-line comment */
$display("File %s line %0d", `__FILE__, `__LINE__);
end
endmodule

View File

@ -0,0 +1 @@
$display("Included file %s line %0d", `__FILE__, `__LINE__);

View File

@ -220,6 +220,7 @@ br_gh661b normal,-g2009 ivltests
br_gh672 normal,-g2009 ivltests
br_gh699 CE,-g2009 ivltests
br_gh756 normal,-g2009 ivltests
br_gh782a normal,-g2009 ivltests gold=br_gh782a.gold
br_ml20171017 normal,-g2009 ivltests
br_ml20180227 CE,-g2009 ivltests
br_ml20180309a normal,-g2009 ivltests