Skip to content

Commit e58192e

Browse files
authored
Merge pull request #3456 from stfc/3446_fix_routine_update_symtab
(Closes #3446) Update to fix issues with codeblocks and routine names when comments exist.
2 parents 3143f46 + 7aff3db commit e58192e

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

changelog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
3) PR #3456 for #3446. Fix issues with CodeBlocks and routine names
2+
in the presence of comments.
3+
14
2) PR #3450 for #3262. Updates the LFRic GPU scripts and aggregates
25
the transformation statistics.
36

src/psyclone/psyir/nodes/routine.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,12 @@ def update_parent_symbol_table(self, new_parent):
354354
(Fortran2003.Subroutine_Subprogram,
355355
Fortran2003.Function_Subprogram))
356356
for routine in routines:
357-
name = str(routine.children[0].children[1])
357+
# Have to walk to find the subroutine_stmt as
358+
# comments can appear before the subroutine stmt.
359+
subroutine_stmt = walk(routine,
360+
(Fortran2003.Subroutine_Stmt,
361+
Fortran2003.Function_Stmt))
362+
name = str(subroutine_stmt[0].children[1])
358363
if name == self.name:
359364
raise GenerationError(
360365
f"Can't add routine '{self.name}' into"

src/psyclone/tests/psyir/nodes/routine_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
from psyclone.errors import GenerationError
4545
from psyclone.psyGen import CodedKern
46+
from psyclone.psyir.frontend.fortran import FortranReader
4647
from psyclone.psyir.nodes import (Assignment, Call, CodeBlock, Container,
4748
Literal, Reference, Routine, ScopingNode)
4849
from psyclone.psyir.symbols import (
@@ -400,6 +401,47 @@ def test_routine_update_parent_symbol_table_illegal_parent(fortran_reader):
400401
in str(excinfo.value))
401402

402403

404+
@pytest.mark.parametrize("routine_type", ["function", "subroutine"])
405+
def test_routine_update_parent_symbol_table_with_comments(routine_type):
406+
''' Test when we have a CodeBlock representing a routine that
407+
if there are also comments before it in the tree we can still
408+
check the name of the subroutine without failing inside
409+
update_parent_symbol_table. '''
410+
411+
code = f"""module test
412+
413+
contains
414+
415+
! This routine will be a codeblock.
416+
{routine_type} routine()
417+
procedure (halo_exchange_routine) :: exchange_halo_group
418+
end {routine_type}
419+
420+
subroutine routine1(a, b, c)
421+
integer, intent(inout) :: a, b, c
422+
423+
call routine2()
424+
end subroutine
425+
426+
subroutine routine2()
427+
428+
end subroutine
429+
430+
end module"""
431+
432+
fortran_reader = FortranReader(ignore_comments=False)
433+
psyir = fortran_reader.psyir_from_source(code)
434+
alt_routine = Routine.create("routine")
435+
436+
module = psyir.walk(Container)[1]
437+
assert isinstance(module.children[0], CodeBlock)
438+
with pytest.raises(GenerationError) as excinfo:
439+
module.addchild(alt_routine)
440+
assert ("Can't add routine 'routine' into a scope that already contains "
441+
"a resolved symbol with the same name."
442+
in str(excinfo.value))
443+
444+
403445
def test_routine_update_parent_symbol_table():
404446
''' Test the update_parent_symbol_table function of the Routine class.
405447
Some of the tests here are accessed through addchild of a container. '''

0 commit comments

Comments
 (0)