Skip to content

Commit

Permalink
#190 Raise an error if the imported global is already declared in the…
Browse files Browse the repository at this point in the history
… NameSpaceManager
  • Loading branch information
sergisiso committed Jan 6, 2020
1 parent 8c9dfe7 commit 5d3432f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/psyclone/psyGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,8 +1937,16 @@ def gen_code(self, parent):
for module_name, var_list in module_map.items():
self._name_space_manager.add_reserved_name(module_name)
for var_name in var_list:
self._name_space_manager.create_name(
root_name=var_name, context="AlgArgs", label=var_name)
newname = self._name_space_manager.create_name(
root_name=var_name,
context="AlgArgs",
label=var_name)
# There is a name clash with this variable name and we can not
# accept indexed names for global variables.
if var_name != newname:
raise KeyError(
"The imported variable '{0}' is already defined in the"
" NameSpaceManager of the Invoke.".format(var_name))

if self._opencl:
parent.add(UseGen(parent, name="iso_c_binding"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def set_to_real(variable):
" by another symbol." in str(err.value)


def test_globalstoargumentstrans_clash_namespace(monkeypatch):
def test_globalstoargumentstrans_clash_namespace_after(monkeypatch):
''' Check the GlobalsToArguments transformation adds the module and global
variable names into the NameSpaceManager to prevent clashes'''
from psyclone.transformations import KernelGlobalsToArguments
Expand Down Expand Up @@ -326,3 +326,35 @@ def set_to_real(variable):
# Created names should not clash with the imported module and globalvar
assert newname1 == "rdt_1"
assert newname2 == "model_mod_1"


def test_globalstoargumentstrans_clash_namespace_before(monkeypatch):
''' Check the GlobalsToArguments generation will break if the global
variable name has already been used in the NameSpaceManager'''
from psyclone.transformations import KernelGlobalsToArguments
from psyclone.psyir.symbols import DataSymbol

trans = KernelGlobalsToArguments()
# Construct a testing InvokeSchedule
_, invoke_info = parse(os.path.join(BASEPATH, "gocean1p0",
"single_invoke_kern_with_use.f90"),
api=API)
psy = PSyFactory(API).create(invoke_info)
invoke = psy.invokes.invoke_list[0]
kernel = invoke.schedule.coded_kernels()[0]

# Monckeypatch resolve_deferred to avoid module searching and importing
# in this test. In this case we assume it is a REAL
def set_to_real(variable):
variable._datatype = DataType.REAL
monkeypatch.setattr(DataSymbol, "resolve_deferred", set_to_real)

# Reserve 'rdt'
_ = invoke._name_space_manager.add_reserved_name("rdt")

# Transform and generate the new code
trans.apply(kernel)
with pytest.raises(KeyError) as err:
_ = str(psy.gen)
assert "The imported variable 'rdt' is already defined in the NameSpace" \
"Manager of the Invoke." in str(err.value)

0 comments on commit 5d3432f

Please sign in to comment.