Skip to content

Commit

Permalink
Fix crash after circular package reference. Fixes nickg#1090
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Dec 7, 2024
1 parent d92bb32 commit f3992e8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -8646,13 +8646,13 @@ static void p_entity_declaration(tree_t unit)
p_trailing_label(id);
consume(tSEMI);

tree_set_ident(unit, qual);
tree_set_loc(unit, CURRENT_LOC);

sem_check(unit, nametab);

pop_scope(nametab);
pop_scope(nametab);

tree_set_ident(unit, qual);
}

static tree_t p_component_declaration(void)
Expand Down Expand Up @@ -8889,10 +8889,10 @@ static tree_t p_package_declaration(tree_t unit)
p_trailing_label(name);
consume(tSEMI);

tree_set_ident(pack, qual);
tree_set_loc(pack, CURRENT_LOC);
sem_check(pack, nametab);

tree_set_ident(pack, qual);
sem_check(pack, nametab);

pop_scope(nametab);
return pack;
Expand Down Expand Up @@ -13321,13 +13321,11 @@ static void p_architecture_body(tree_t unit)
p_trailing_label(arch_name);
consume(tSEMI);

tree_set_ident(unit, qual);
tree_set_loc(unit, CURRENT_LOC);

sem_check(unit, nametab);

// Set the architecture name to the fully qualified identifier
tree_set_ident(unit, qual);

pop_scope(nametab);
pop_scope(nametab);
}
Expand Down
13 changes: 13 additions & 0 deletions src/sem.c
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,16 @@ static bool sem_check_process(tree_t t, nametab_t *tab)
return true;
}

static void sem_circular_dep_cb(ident_t name, void *ctx)
{
tree_t unit = ctx;

if (name == tree_ident(unit))
error_at(tree_loc(unit), "%s %s references a previously analysed design "
"unit with the same name which will be overwritten",
class_str(class_of(unit)), istr(name));
}

static bool sem_check_package(tree_t t, nametab_t *tab)
{
if (!sem_check_context_clause(t, tab))
Expand All @@ -2349,6 +2359,7 @@ static bool sem_check_package(tree_t t, nametab_t *tab)
sem_error(d, "subprogram body is not allowed in package specification");
}

tree_walk_deps(t, sem_circular_dep_cb, t);
return true;
}

Expand Down Expand Up @@ -2444,6 +2455,7 @@ static bool sem_check_entity(tree_t t, nametab_t *tab)
tree_visit_only(s, sem_passive_cb, s, T_SIGNAL_ASSIGN);
}

tree_walk_deps(t, sem_circular_dep_cb, t);
return true;
}

Expand Down Expand Up @@ -6815,6 +6827,7 @@ static bool sem_check_context_decl(tree_t t, nametab_t *tab)
if (!sem_check_context_clause(t, tab))
return false;

tree_walk_deps(t, sem_circular_dep_cb, t);
return true;
}

Expand Down
13 changes: 13 additions & 0 deletions test/parse/issue1090.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package p1 is
end p1;

use work.p1.all; -- Error

package p1 is
constant c : natural := 5;
end p1;

use work.p1.all; -- Error

entity p1 is
end entity;
24 changes: 22 additions & 2 deletions test/test_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -6996,14 +6996,13 @@ END_TEST

START_TEST(test_pkgindecl)
{
opt_set_int(OPT_RELAXED, 1);
set_standard(STD_08);

input_from_file(TESTDIR "/parse/pkgindecl.vhd");

const error_t expect[] = {
{ 5, "unexpected end while parsing interface package declaration, "
"expecting new"},
"expecting new" },
{ -1, NULL }
};
expect_errors(expect);
Expand All @@ -7016,6 +7015,26 @@ START_TEST(test_pkgindecl)
}
END_TEST

START_TEST(test_issue1090)
{
input_from_file(TESTDIR "/parse/issue1090.vhd");

const error_t expect[] = {
{ 6, "package WORK.P1 references a previously analysed design unit "
"with the same name which will be overwritten" },
{ 12, "entity WORK.P1 references a previously analysed design unit" },
{ -1, NULL }
};
expect_errors(expect);

parse_and_check(T_PACKAGE, T_PACKAGE, T_ENTITY);

fail_unless(parse() == NULL);

check_expected_errors();
}
END_TEST

Suite *get_parse_tests(void)
{
Suite *s = suite_create("parse");
Expand Down Expand Up @@ -7185,6 +7204,7 @@ Suite *get_parse_tests(void)
tcase_add_test(tc_core, test_hang);
tcase_add_test(tc_core, test_protected3);
tcase_add_test(tc_core, test_pkgindecl);
tcase_add_test(tc_core, test_issue1090);
suite_add_tcase(s, tc_core);

return s;
Expand Down

0 comments on commit f3992e8

Please sign in to comment.