diff --git a/docs/CONTRIBUTORS b/docs/CONTRIBUTORS index 9fc522b04..a51ff3796 100644 --- a/docs/CONTRIBUTORS +++ b/docs/CONTRIBUTORS @@ -158,6 +158,7 @@ Nandu Raj Nathan Graybeal Nathan Kohagen Nathan Myers +Nick Brereton Nolan Poe Oleh Maksymenko Patrick Stewart diff --git a/src/V3LinkParse.cpp b/src/V3LinkParse.cpp index 1a8956a43..08a13fe79 100644 --- a/src/V3LinkParse.cpp +++ b/src/V3LinkParse.cpp @@ -895,6 +895,15 @@ class LinkParseVisitor final : public VNVisitor { } iterateChildren(nodep); } + void visit(AstClass* nodep) override { + cleanFileline(nodep); + for (AstNode* itemp = nodep->stmtsp(); itemp; itemp = itemp->nextp()) { + if (VN_IS(itemp, PackageImport)) { + itemp->v3error("Import statement directly within a class scope is illegal"); + } + } + iterateChildren(nodep); + } void visit(AstNode* nodep) override { // Default: Just iterate diff --git a/test_regress/t/t_class_scope_import.out b/test_regress/t/t_class_scope_import.out new file mode 100644 index 000000000..4c1811e42 --- /dev/null +++ b/test_regress/t/t_class_scope_import.out @@ -0,0 +1,4 @@ +%Error: t/t_class_scope_import.v:11:14: Import statement directly within a class scope is illegal + 11 | import pkg::*; + | ^~ +%Error: Exiting due to diff --git a/test_regress/t/t_class_scope_import.py b/test_regress/t/t_class_scope_import.py new file mode 100755 index 000000000..169d0cf5a --- /dev/null +++ b/test_regress/t/t_class_scope_import.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2024 by Nick Brereton. This program is free software; you +# can redistribute it and/or modify it under the terms of either the GNU +# Lesser General Public License Version 3 or the Perl Artistic License +# Version 2.0. +# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 + +import vltest_bootstrap + +test.scenarios('linter') + +test.lint(fails=True, expect_filename=test.golden_filename) + +test.passes() diff --git a/test_regress/t/t_class_scope_import.v b/test_regress/t/t_class_scope_import.v new file mode 100644 index 000000000..967b35fb8 --- /dev/null +++ b/test_regress/t/t_class_scope_import.v @@ -0,0 +1,15 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// This file ONLY is placed under the Creative Commons Public Domain, for +// any use, without warranty, 2023 by Nick Brereton +// SPDX-License-Identifier: CC0-1.0 + +package pkg; +endpackage + +class genericClass; + import pkg::*; +endclass + +module tb_top(); +endmodule