From 8b3f0d63b40c5affdec404db7f025416f669a4b1 Mon Sep 17 00:00:00 2001 From: Martin Whitaker Date: Sun, 18 Feb 2024 15:46:56 +0000 Subject: [PATCH] Record the lexical order of identifiers whilst scanning the source files. This is needed for detecting use before declaration. The lexical scanner is the only place where we process the source text in strict lexical order, so do it there. As Verilog allows modules to span multiple source files, don't reset the counter when we reset the lexor. --- lexor.lex | 7 ++++++- parse.y | 2 ++ parse_misc.h | 3 ++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lexor.lex b/lexor.lex index f94c68f5f..4e93cb87b 100644 --- a/lexor.lex +++ b/lexor.lex @@ -26,6 +26,7 @@ //# define YYSTYPE lexval +# include # include # include # include "compiler.h" @@ -40,7 +41,7 @@ using namespace std; -# define YY_USER_INIT reset_lexor(); +# define YY_USER_INIT do { reset_lexor(); yylloc.lexical_pos = 0; } while (0); # define yylval VLlval # define YY_NO_INPUT @@ -338,6 +339,8 @@ TU [munpf] int rc = lexor_keyword_code(yytext, yyleng); switch (rc) { case IDENTIFIER: + assert(yylloc.lexical_pos != UINT_MAX); + yylloc.lexical_pos += 1; yylval.text = strdupnew(yytext); if (strncmp(yylval.text,"PATHPULSE$", 10) == 0) rc = PATHPULSE_IDENTIFIER; @@ -429,6 +432,8 @@ TU [munpf] \\[^ \t\b\f\r\n]+ { + assert(yylloc.lexical_pos != UINT_MAX); + yylloc.lexical_pos += 1; yylval.text = strdupnew(yytext+1); if (gn_system_verilog()) { if (PPackage*pkg = pform_test_package_identifier(yylval.text)) { diff --git a/parse.y b/parse.y index 61ea8aa81..9d06fa16e 100644 --- a/parse.y +++ b/parse.y @@ -115,12 +115,14 @@ static std::list*attributes_in_context = 0; (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \ (Current).last_line = YYRHSLOC (Rhs, N).last_line; \ (Current).last_column = YYRHSLOC (Rhs, N).last_column; \ + (Current).lexical_pos = YYRHSLOC (Rhs, 1).lexical_pos; \ (Current).text = YYRHSLOC (Rhs, 1).text; \ } else { \ (Current).first_line = YYRHSLOC (Rhs, 0).last_line; \ (Current).first_column = YYRHSLOC (Rhs, 0).last_column; \ (Current).last_line = YYRHSLOC (Rhs, 0).last_line; \ (Current).last_column = YYRHSLOC (Rhs, 0).last_column; \ + (Current).lexical_pos = YYRHSLOC (Rhs, 0).lexical_pos; \ (Current).text = YYRHSLOC (Rhs, 0).text; \ } \ } while (0) diff --git a/parse_misc.h b/parse_misc.h index 86e86314b..23547e85c 100644 --- a/parse_misc.h +++ b/parse_misc.h @@ -1,7 +1,7 @@ #ifndef IVL_parse_misc_H #define IVL_parse_misc_H /* - * Copyright (c) 1998-2022 Stephen Williams (steve@icarus.com) + * Copyright (c) 1998-2024 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 @@ -34,6 +34,7 @@ struct vlltype { int first_column; int last_line; int last_column; + unsigned lexical_pos; const char*text; std::string get_fileline() const; };