aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZachary Snow <zach@zachjs.com>2021-02-18 12:04:02 -0500
committerZachary Snow <zach@zachjs.com>2021-02-19 09:18:41 -0500
commit220cb1f7bbf6405117b953526c50a21a5ef5788f (patch)
treec3fe7d79380e9bf2c73c56543f5461715e0a0395
parent127484e675538fbaeca1f6e53ba264a1f02e9cf6 (diff)
downloadyosys-220cb1f7bbf6405117b953526c50a21a5ef5788f.tar.gz
yosys-220cb1f7bbf6405117b953526c50a21a5ef5788f.tar.bz2
yosys-220cb1f7bbf6405117b953526c50a21a5ef5788f.zip
verilog: error on macro invocations with missing argument lists
This would previously complain about an undefined internal macro if the unapplied macro had not already been used. If it had, it would incorrectly use the arguments from the previous invocation.
-rw-r--r--frontends/verilog/preproc.cc11
-rw-r--r--tests/verilog/macro_unapplied.ys17
-rw-r--r--tests/verilog/macro_unapplied_newline.ys5
3 files changed, 32 insertions, 1 deletions
diff --git a/frontends/verilog/preproc.cc b/frontends/verilog/preproc.cc
index c451c4c20..de707593f 100644
--- a/frontends/verilog/preproc.cc
+++ b/frontends/verilog/preproc.cc
@@ -477,7 +477,16 @@ static bool try_expand_macro(define_map_t &defines, std::string &tok)
std::string name = tok.substr(1);
std::string skipped_spaces = skip_spaces();
tok = next_token(false);
- if (tok == "(" && body->has_args) {
+ if (body->has_args) {
+ if (tok != "(") {
+ if (tok.size() == 1 && iscntrl(tok[0])) {
+ char buf[5];
+ snprintf(buf, sizeof(buf), "\\x%02x", tok[0]);
+ tok = buf;
+ }
+ log_error("Expected to find '(' to begin macro arguments for '%s', but instead found '%s'\n",
+ name.c_str(), tok.c_str());
+ }
std::vector<std::string> args;
bool done = false;
while (!done) {
diff --git a/tests/verilog/macro_unapplied.ys b/tests/verilog/macro_unapplied.ys
new file mode 100644
index 000000000..81eb10b8b
--- /dev/null
+++ b/tests/verilog/macro_unapplied.ys
@@ -0,0 +1,17 @@
+logger -expect-no-warnings
+read_verilog -sv <<EOT
+`define MACRO(a = 1, b = 2) initial $display("MACRO(a = %d, b = %d)", a, b)
+module top;
+ `MACRO();
+endmodule
+EOT
+
+design -reset
+
+logger -expect error "Expected to find '\(' to begin macro arguments for 'MACRO', but instead found ';'" 1
+read_verilog -sv <<EOT
+`define MACRO(a = 1, b = 2) initial $display("MACRO(a = %d, b = %d)", a, b)
+module top;
+ `MACRO;
+endmodule
+EOT
diff --git a/tests/verilog/macro_unapplied_newline.ys b/tests/verilog/macro_unapplied_newline.ys
new file mode 100644
index 000000000..a3f88d5b4
--- /dev/null
+++ b/tests/verilog/macro_unapplied_newline.ys
@@ -0,0 +1,5 @@
+logger -expect error "Expected to find '\(' to begin macro arguments for 'foo', but instead found '\\x0a'" 1
+read_verilog -sv <<EOT
+`define foo(a=1) (a)
+`foo
+EOT