Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
SCHREIBER Martin committed Nov 17, 2024
1 parent 25bf2f1 commit 23c8cf7
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 121 deletions.
2 changes: 1 addition & 1 deletion src/psyclone/domain/lfric/lfric_extract_driver_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def _sym_is_field(sym):
)
try:
orig_sym = mod_info.get_symbol_by_name(signature[0])
except Exception as err:
except KeyError:
orig_sym = None

if not orig_sym:
Expand Down
9 changes: 8 additions & 1 deletion src/psyclone/parse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,21 @@
"""

from psyclone.parse.file_info import FileInfo
from psyclone.parse.module_info import ModuleInfo, ModuleInfoError
from psyclone.parse.file_info import FileInfoFParserError
from psyclone.parse.module_info import (
ModuleInfo,
ModuleInfoError,
ModuleNotFoundError,
)
from psyclone.parse.module_manager_auto_search import ModuleManagerAutoSearch


# For AutoAPI documentation generation.
__all__ = [
"FileInfo",
"FileInfoFParserError",
"ModuleInfo",
"ModuleInfoError",
"ModuleNotFoundError",
"ModuleManagerAutoSearch",
]
29 changes: 22 additions & 7 deletions src/psyclone/parse/file_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,24 @@
"""

import os
import sys
import hashlib
import pickle
import copy
from typing import List, Union, Any
from typing import List, Union
from psyclone.psyir.frontend.fortran import FortranReader, FortranStringReader
from fparser.two import Fortran2003
from fparser.two.parser import ParserFactory
from psyclone.configuration import Config
from psyclone.psyir.nodes import FileContainer
from psyclone.errors import PSycloneError


class FileInfoFParserError(PSycloneError):
"""Triggered when generation of FParser tree failed"""

def __init__(self, value: str):
super().__init__(value)
self.value = "FParser Error: " + str(value)


class _CacheFileInfo:
Expand Down Expand Up @@ -335,6 +343,7 @@ def get_fparser_tree(self, verbose: bool = False) -> Fortran2003.Program:
:returns: fparser representation.
:rtype: FileContainer
:raises FileInfoFParserError: if fparser had issues
"""
if self._fparser_tree is not None:
return self._fparser_tree
Expand All @@ -360,11 +369,17 @@ def get_fparser_tree(self, verbose: bool = False) -> Fortran2003.Program:
self._fparser_tree = self._cache._fparser_tree
return self._fparser_tree

reader = FortranStringReader(
source_code, include_dirs=Config.get().include_paths
)
parser = ParserFactory().create(std="f2008")
self._fparser_tree = parser(reader)
try:
reader = FortranStringReader(
source_code, include_dirs=Config.get().include_paths
)
parser = ParserFactory().create(std="f2008")
self._fparser_tree = parser(reader)

except Exception as err:
raise FileInfoFParserError(
"Failed to get fparser tree: " + str(err)
)

# We directly call the cache saving routine here in case that the
# fparser tree will be modified later on.
Expand Down
53 changes: 31 additions & 22 deletions src/psyclone/parse/module_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@
from psyclone.errors import InternalError, PSycloneError, GenerationError
from psyclone.psyir.nodes import Container, FileContainer, Routine
from psyclone.psyir.symbols import Symbol, SymbolError
from psyclone.parse import FileInfo
from psyclone.parse import FileInfo, FileInfoFParserError

from typing import Set

class ModuleNotFoundError(PSycloneError):
"""Triggered when the Fortran module was not found"""

def __init__(self, value):
PSycloneError.__init__(self, value)
self.value = "ModuleNotFoundError: " + str(value)


class ModuleInfoError(PSycloneError):
Expand All @@ -63,7 +69,7 @@ class ModuleInfoError(PSycloneError):

def __init__(self, value):
PSycloneError.__init__(self, value)
self.value = "ModuleInfo error: " + str(value)
self.value = "ModuleInfoError: " + str(value)


class ModuleInfo:
Expand Down Expand Up @@ -168,17 +174,7 @@ def _extract_used_module_names_from_fparser_tree(self):
self._used_module_names = []
self._used_symbols_from_module_name = {}

try:
parse_tree = self.get_fparser_tree()
except FortranSyntaxError:
# TODO #11: Add proper logging
# TODO #2120: Handle error
print(
f"[ModuleInfo._extract_import_information] Syntax error "
f"parsing '{self.filepath} - ignored"
)
# Hide syntax errors
return
parse_tree = self.get_fparser_tree()

for use in walk(parse_tree, Fortran2003.Use_Stmt):
# Ignore intrinsic modules:
Expand Down Expand Up @@ -283,8 +279,12 @@ def get_psyir_container_node(self):
:returns: PSyIR representing this module.
:rtype: :py:class:`psyclone.psyir.nodes.Container` | NoneType
:raises FileNotFoundError: if the named Container (module) does not
exist in the PSyIR.
:raises ModuleNotFoundError: if the named Container (module) was
not found.
:raises FileNotFound: if the related file doesn't exist.
:raises FileInfoFParserError: if the some FileInfoFParserError
occured.
:raises GenerationError: if the some PSyIR generation failed.
"""

# If container node is not provided, we will load it
Expand All @@ -298,12 +298,16 @@ def get_psyir_container_node(self):
for container in container_list:
container: Container

# Sort out, e.g., FileContainer
if type(container) is not Container:
continue

container_name: str = container.name.lower()
if container_name == self._name:
self._psyir_container_node = container
return self._psyir_container_node

raise FileNotFoundError(
raise ModuleNotFoundError(
f"Unable to find container '{self._name}' in "
f"file '{self._file_info.get_filepath()}'"
)
Expand All @@ -327,7 +331,7 @@ def get_psyir_container_node_alternative(self):
if self._psyir_container_node is None:
try:
fparser_tree = self.get_fparser_tree()
except FortranSyntaxError as err:
except FileInfoFParserError as err:
# TODO #11: Add proper logging
print(f"Error parsing '{self.filepath}': '{err}'")
return None
Expand Down Expand Up @@ -364,7 +368,7 @@ def get_psyir_container_node_alternative(self):
container_node = self.get_psyir_container_node()
return container_node

except Exception as err:
except ModuleNotFoundError:
pass

# We failed to find the Container - double-check the parse tree
Expand Down Expand Up @@ -402,13 +406,18 @@ def get_symbol_by_name(self, symbol_name: str) -> Symbol:
:rtype: :py:class:`psyclone.psyir.symbols.Symbol` | None
"""

container = self.get_psyir_container_node()
if not container:
try:
container: Container = self.get_psyir_container_node()
except (FileNotFoundError, FileInfoFParserError, GenerationError):
return None

assert type(container) is Container

try:
return container.symbol_table.lookup(symbol_name)
symbol = container.symbol_table.lookup(symbol_name)
except KeyError:
return None
return symbol

def view(self, indent=""):
retstr = ""
Expand Down
8 changes: 3 additions & 5 deletions src/psyclone/parse/module_manager_auto_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@


from collections import OrderedDict
import copy
from difflib import SequenceMatcher
import os
import re
import warnings
from typing import Dict, Set, List

from psyclone.errors import InternalError
from psyclone.parse.file_info import FileInfo
from psyclone.errors import InternalError, GenerationError
from psyclone.parse.file_info import FileInfo, FileInfoFParserError
from psyclone.parse.module_info import ModuleInfo
from psyclone.parse.module_manager_base import ModuleManagerBase

Expand Down Expand Up @@ -338,7 +336,7 @@ def get_all_dependencies_recursively(
# Convert to set
mod_deps = set(mod_deps)

except FileNotFoundError:
except (FileNotFoundError, FileInfoFParserError, GenerationError):
if module not in not_found:
# We don't have any information about this module,
# ignore it.
Expand Down
2 changes: 0 additions & 2 deletions src/psyclone/parse/module_manager_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
from typing import Union, Set, List

from psyclone.parse.file_info import FileInfo
from psyclone.parse.module_info import ModuleInfo
from psyclone.parse.module_manager_base import ModuleManagerBase
from psyclone.psyir.nodes import Node, Container


class ModuleManagerFiles(ModuleManagerBase):
Expand Down
1 change: 1 addition & 0 deletions src/psyclone/psyir/frontend/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ def psyir_from_fparse_tree(self, parse_tree, filepath="", free_form=True):
:returns: PSyIR representing the provided Fortran file.
:rtype: :py:class:`psyclone.psyir.nodes.Node`
:raises GenerationError: if the some PSyIR generation failed.
"""

_, filename = os.path.split(filepath)
Expand Down
Loading

0 comments on commit 23c8cf7

Please sign in to comment.