Report error on typedef referencing self (#2539).

This commit is contained in:
Wilson Snyder 2020-11-07 15:41:37 -05:00
parent 778f133118
commit 152689776d
5 changed files with 68 additions and 0 deletions

View File

@ -12,6 +12,8 @@ The contributors that suggested a given feature are shown in []. Thanks!
*** Support static methods and typedefs in classes (#2615). [Krzysztof Bieganski]
**** Report error on typedef referencing self (#2539). [Cody Piersall]
**** Fix iteration over mutating list bug in VPI (#2588). [Kaleb Barrett]
**** Fix return from callValueCbs (#2589) (#2605). [Marlon James]

View File

@ -1605,6 +1605,17 @@ private:
}
virtual void visit(AstTypedef* nodep) override {
if (nodep->didWidthAndSet()) return; // This node is a dtype & not both PRELIMed+FINALed
if (auto* refp = checkRefToTypedefRecurse(nodep, nodep)) {
nodep->v3error("Typedef has self-reference: " << nodep->prettyNameQ() << endl
<< nodep->warnContextPrimary() << endl
<< refp->warnOther()
<< "... Location of reference" << endl
<< refp->warnContextSecondary());
// May cause internel error but avoids infinite loop on dump
refp->typedefp(nullptr);
VL_DO_DANGLING(pushDeletep(nodep->unlinkFrBack()), nodep);
return;
}
nodep->dtypep(iterateEditMoveDTypep(nodep, nodep->subDTypep()));
userIterateChildren(nodep, nullptr);
}
@ -5808,6 +5819,20 @@ private:
}
return nodep; // By default return this
}
AstRefDType* checkRefToTypedefRecurse(AstNode* nodep, AstTypedef* typedefp) {
// Recurse all children looking for self reference
// This avoids iterateEditMoveDTypep going into a hard to resolve loop
// Only call once for any given typedef, or will become O(n^2)
if (VL_LIKELY(!nodep)) return nullptr;
if (auto* refp = VN_CAST(nodep, RefDType)) {
if (refp->typedefp() == typedefp) return refp;
}
if (auto* refp = checkRefToTypedefRecurse(nodep->op1p(), typedefp)) return refp;
if (auto* refp = checkRefToTypedefRecurse(nodep->op2p(), typedefp)) return refp;
if (auto* refp = checkRefToTypedefRecurse(nodep->op3p(), typedefp)) return refp;
if (auto* refp = checkRefToTypedefRecurse(nodep->op4p(), typedefp)) return refp;
return nullptr;
}
//----------------------------------------------------------------------
// METHODS - special iterators

View File

@ -0,0 +1,8 @@
%Error: t/t_enum_bad_circdecl.v:11:6: Typedef has self-reference: 'bad_redecl'
: ... In instance t
11 | } bad_redecl;
| ^~~~~~~~~~
t/t_enum_bad_circdecl.v:9:17: ... Location of reference
9 | typedef enum bad_redecl [2:0] {
| ^~~~~~~~~~
%Error: Exiting due to

View File

@ -0,0 +1,20 @@
#!/usr/bin/env perl
if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); die; }
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
#
# Copyright 2003 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
scenarios(linter => 1);
lint(
verilator_flags2 => ["--lint-only -Wwarn-VARHIDDEN"],
fails => 1,
expect_filename => $Self->{golden_filename},
);
ok(1);
1;

View File

@ -0,0 +1,13 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2020 by Wilson Snyder.
// SPDX-License-Identifier: CC0-1.0
module t();
typedef enum bad_redecl [2:0] {
VALUE
} bad_redecl;
endmodule