From b237eec801540a089f8bf6fe9774c7c4743b3dd7 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sat, 20 Sep 2025 13:47:17 -0400 Subject: [PATCH] Add error on string addition --- src/V3Width.cpp | 12 ++++++++++++ test_regress/t/t_string_add_bad.out | 12 ++++++++++++ test_regress/t/t_string_add_bad.py | 16 ++++++++++++++++ test_regress/t/t_string_add_bad.v | 20 ++++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 test_regress/t/t_string_add_bad.out create mode 100755 test_regress/t/t_string_add_bad.py create mode 100644 test_regress/t/t_string_add_bad.v diff --git a/src/V3Width.cpp b/src/V3Width.cpp index 4b0915eff..4cb78352a 100644 --- a/src/V3Width.cpp +++ b/src/V3Width.cpp @@ -7307,6 +7307,18 @@ class WidthVisitor final : public VNVisitor { iterateCheckReal(nodep, "LHS", nodep->lhsp(), FINAL); iterateCheckReal(nodep, "RHS", nodep->rhsp(), FINAL); return; + } else if (nodep->lhsp()->isString() || nodep->rhsp()->isString()) { + nodep->v3error( + "Operator " + << nodep->prettyTypeName() + << " is not legal on string data types (IEEE 1800-2023 6.16)\n" + << (VN_IS(nodep, Add) + ? (nodep->warnMore() + + "... Suggest to concatenate strings use '{LHS, RHS, ...}'") + : "")); + nodep->replaceWith(new AstConst{nodep->fileline(), AstConst::String{}, ""}); + VL_DO_DANGLING(pushDeletep(nodep), nodep); + return; } else { const int width = std::max(nodep->lhsp()->width(), nodep->rhsp()->width()); const int mwidth = std::max(nodep->lhsp()->widthMin(), nodep->rhsp()->widthMin()); diff --git a/test_regress/t/t_string_add_bad.out b/test_regress/t/t_string_add_bad.out new file mode 100644 index 000000000..f0222e103 --- /dev/null +++ b/test_regress/t/t_string_add_bad.out @@ -0,0 +1,12 @@ +%Error: t/t_string_add_bad.v:13:9: Operator ADD is not legal on string data types (IEEE 1800-2023 6.16) + : ... note: In instance 't' + : ... Suggest to concatenate strings use '{LHS, RHS, ...}' + 13 | s += $sformatf(" a%0d", a); + | ^~ + ... See the manual at https://verilator.org/verilator_doc.html?v=latest for more assistance. +%Error: t/t_string_add_bad.v:14:13: Operator ADD is not legal on string data types (IEEE 1800-2023 6.16) + : ... note: In instance 't' + : ... Suggest to concatenate strings use '{LHS, RHS, ...}' + 14 | s = s + s; + | ^ +%Error: Exiting due to diff --git a/test_regress/t/t_string_add_bad.py b/test_regress/t/t_string_add_bad.py new file mode 100755 index 000000000..55203b6c9 --- /dev/null +++ b/test_regress/t/t_string_add_bad.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2025 by Wilson Snyder. 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_string_add_bad.v b/test_regress/t/t_string_add_bad.v new file mode 100644 index 000000000..2ad9a3556 --- /dev/null +++ b/test_regress/t/t_string_add_bad.v @@ -0,0 +1,20 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// This file ONLY is placed under the Creative Commons Public Domain, for +// any use, without warranty, 2025 by Wilson Snyder. +// SPDX-License-Identifier: CC0-1.0 + +module t; + + string s; + + initial begin + for (int a = 0; a < 3; ++a) begin : a_loop + s += $sformatf(" a%0d", a); // <--- Error: += is not legal on strings + s = s + s; // <--- Error: += is not legal on strings + end + + $stop; + end + +endmodule