Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/psyclone/psyir/nodes/routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ def update_parent_symbol_table(self, new_parent):
(Fortran2003.Subroutine_Subprogram,
Fortran2003.Function_Subprogram))
for routine in routines:
name = str(routine.children[0].children[1])
# Have to walk to find the subroutine_stmt as
# comments can appear before the subroutine stmt.
subroutine_stmt = walk(routine,
Fortran2003.Subroutine_Stmt)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see routines can contain subroutines or functions. Does this walk also need to be extended to allow for functions?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, my mistake, will fix.

name = str(subroutine_stmt[0].children[1])
if name == self.name:
raise GenerationError(
f"Can't add routine '{self.name}' into"
Expand Down
41 changes: 41 additions & 0 deletions src/psyclone/tests/psyir/nodes/routine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

from psyclone.errors import GenerationError
from psyclone.psyGen import CodedKern
from psyclone.psyir.frontend.fortran import FortranReader
from psyclone.psyir.nodes import (Assignment, Call, CodeBlock, Container,
Literal, Reference, Routine, ScopingNode)
from psyclone.psyir.symbols import (
Expand Down Expand Up @@ -400,6 +401,46 @@ def test_routine_update_parent_symbol_table_illegal_parent(fortran_reader):
in str(excinfo.value))


def test_routine_update_parent_symbol_table_with_comments():
''' Test when we have a CodeBlock representing a routine that
if there are also comments before it in the tree we can still
check the name of the subroutine without failing inside
update_parent_symbol_table. '''

code = """module test

contains

! This routine will be a codeblock.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you extend this test/add a new one that also checks when we have a Function.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeated the test with a function.

subroutine routine()
procedure (halo_exchange_routine) :: exchange_halo_group
end subroutine

subroutine routine1(a, b, c)
integer, intent(inout) :: a, b, c

call routine2()
end subroutine

subroutine routine2()

end subroutine

end module"""

fortran_reader = FortranReader(ignore_comments=False)
psyir = fortran_reader.psyir_from_source(code)
alt_routine = Routine.create("routine")

module = psyir.walk(Container)[1]
assert isinstance(module.children[0], CodeBlock)
with pytest.raises(GenerationError) as excinfo:
module.addchild(alt_routine)
assert ("Can't add routine 'routine' into a scope that already contains "
"a resolved symbol with the same name."
in str(excinfo.value))


def test_routine_update_parent_symbol_table():
''' Test the update_parent_symbol_table function of the Routine class.
Some of the tests here are accessed through addchild of a container. '''
Expand Down
Loading