aboutsummaryrefslogtreecommitdiffstats
path: root/pyGHDL
diff options
context:
space:
mode:
authorPatrick Lehmann <Patrick.Lehmann@plc2.de>2021-06-22 21:40:20 +0200
committerPatrick Lehmann <Patrick.Lehmann@plc2.de>2021-06-22 21:40:20 +0200
commitc5a4e881debcc75bdb96cdddd4abda10fd778b68 (patch)
tree9cb1bbb2510f0fce70d7ae36436bc22631698d84 /pyGHDL
parentea328fa671fc42569a688c8dfb308d87f42771c3 (diff)
downloadghdl-c5a4e881debcc75bdb96cdddd4abda10fd778b68.tar.gz
ghdl-c5a4e881debcc75bdb96cdddd4abda10fd778b68.tar.bz2
ghdl-c5a4e881debcc75bdb96cdddd4abda10fd778b68.zip
Improved error handling.
Diffstat (limited to 'pyGHDL')
-rwxr-xr-xpyGHDL/cli/DOM.py6
-rw-r--r--pyGHDL/dom/Common.py13
-rw-r--r--pyGHDL/dom/NonStandard.py12
-rw-r--r--pyGHDL/dom/_Utils.py40
-rw-r--r--pyGHDL/libghdl/__init__.py11
5 files changed, 56 insertions, 26 deletions
diff --git a/pyGHDL/cli/DOM.py b/pyGHDL/cli/DOM.py
index 641383686..68af8ec1a 100755
--- a/pyGHDL/cli/DOM.py
+++ b/pyGHDL/cli/DOM.py
@@ -48,9 +48,15 @@ def handleException(ex):
return 5
elif isinstance(ex, DOMException):
print("DOM:", ex)
+ ex2 = ex.__cause__
+ if ex2 is not None:
+ for message in ex2.InternalErrors:
+ print("libghdl: {message}".format(message=message))
return 4
elif isinstance(ex, LibGHDLException):
print("LIB:", ex)
+ for message in ex.InternalErrors:
+ print(" {message}".format(message=message))
return 3
elif isinstance(ex, GHDLBaseException):
print("GHDL:", ex)
diff --git a/pyGHDL/dom/Common.py b/pyGHDL/dom/Common.py
index 984f06e86..43e8ce497 100644
--- a/pyGHDL/dom/Common.py
+++ b/pyGHDL/dom/Common.py
@@ -51,16 +51,3 @@ class DOMException(GHDLBaseException):
@export
class GHDLException(GHDLBaseException):
pass
-
-
-@export
-class GHDLMixin:
- def CheckForErrors(self) -> None:
- errorCount = errorout_memory.Get_Nbr_Messages()
- if errorCount != 0:
- for i in range(errorCount):
- print(errorout_memory.Get_Error_Message(i + 1))
-
- raise DOMException("Error in libghdl.") from LibGHDLException(
- "libghdl: Internal error 2."
- )
diff --git a/pyGHDL/dom/NonStandard.py b/pyGHDL/dom/NonStandard.py
index 63a35dc7f..9e2950f03 100644
--- a/pyGHDL/dom/NonStandard.py
+++ b/pyGHDL/dom/NonStandard.py
@@ -58,8 +58,8 @@ from pyGHDL.libghdl import (
utils,
)
from pyGHDL.libghdl.vhdl import nodes, sem_lib, parse
-from pyGHDL.dom._Utils import GetIirKindOfNode
-from pyGHDL.dom.Common import DOMException, GHDLMixin
+from pyGHDL.dom._Utils import GetIirKindOfNode, CheckForErrors
+from pyGHDL.dom.Common import DOMException
from pyGHDL.dom.DesignUnit import (
Entity,
Architecture,
@@ -103,14 +103,13 @@ class Library(VHDLModel_Library):
@export
-class Document(VHDLModel_Document, GHDLMixin):
+class Document(VHDLModel_Document):
__ghdlFileID: Any
__ghdlSourceFileEntry: Any
__ghdlFile: Any
def __init__(self, path: Path = None, dontParse: bool = False):
super().__init__(path)
- GHDLMixin.__init__(self)
self.__ghdl_init()
if dontParse == False:
@@ -125,12 +124,11 @@ class Document(VHDLModel_Document, GHDLMixin):
if self.__ghdlSourceFileEntry == files_map.No_Source_File_Entry:
raise LibGHDLException("Cannot load file '{!s}'".format(self.Path))
- self.CheckForErrors()
+ CheckForErrors()
# Parse input file
self.__ghdlFile = sem_lib.Load_File(self.__ghdlSourceFileEntry)
-
- self.CheckForErrors()
+ CheckForErrors()
def parse(self):
firstUnit = nodes.Get_First_Design_Unit(self.__ghdlFile)
diff --git a/pyGHDL/dom/_Utils.py b/pyGHDL/dom/_Utils.py
index e75c5f36a..f413351b4 100644
--- a/pyGHDL/dom/_Utils.py
+++ b/pyGHDL/dom/_Utils.py
@@ -30,13 +30,15 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later
# ============================================================================
-from pyGHDL.libghdl._types import Iir
from pydecor import export
+from pyGHDL.dom.Common import DOMException
from pyVHDLModel.VHDLModel import Mode
-from pyGHDL.libghdl import LibGHDLException, name_table, files_map
+from pyGHDL.libghdl import LibGHDLException, name_table, files_map, errorout_memory
from pyGHDL.libghdl.vhdl import nodes
+from pyGHDL.libghdl.vhdl.nodes import Null_Iir
+from pyGHDL.libghdl._types import Iir
from pyGHDL.dom.Misc import Position
@@ -52,9 +54,24 @@ __MODE_TRANSLATION = {
@export
+def CheckForErrors() -> None:
+ errorCount = errorout_memory.Get_Nbr_Messages()
+ errors = []
+ if errorCount != 0:
+ for i in range(errorCount):
+ errors.append(errorout_memory.Get_Error_Message(i + 1))
+
+ raise DOMException("Error raised in libghdl.") \
+ from LibGHDLException("libghdl: Internal error.", errors)
+
+
+@export
def GetIirKindOfNode(node: Iir) -> nodes.Iir_Kind:
- # This function is the most likely to be called on a Null_Iir node
- assert node != 0
+ """Return the kind of a node in the IIR tree."""
+
+ if node == Null_Iir:
+ raise ValueError("Parameter 'node' must not be 'Null_iir'.")
+
kind: int = nodes.Get_Kind(node)
return nodes.Iir_Kind(kind)
@@ -62,13 +79,21 @@ def GetIirKindOfNode(node: Iir) -> nodes.Iir_Kind:
@export
def GetNameOfNode(node: Iir) -> str:
"""Return the python string from node :obj:`node` identifier."""
+
+ if node == Null_Iir:
+ raise ValueError("Parameter 'node' must not be 'Null_iir'.")
+
identifier = nodes.Get_Identifier(node)
return name_table.Get_Name_Ptr(identifier)
@export
def GetModeOfNode(node: Iir) -> Mode:
- """Return the mode of a :obj:`port`."""
+ """Return the mode of a :obj:`node`."""
+
+ if node == Null_Iir:
+ raise ValueError("Parameter 'node' must not be 'Null_iir'.")
+
try:
return __MODE_TRANSLATION[nodes.Get_Mode(node)]
except KeyError:
@@ -77,6 +102,11 @@ def GetModeOfNode(node: Iir) -> Mode:
@export
def GetPositionOfNode(node: Iir) -> Position:
+ """Return the source code position of a IIR node."""
+
+ if node == Null_Iir:
+ raise ValueError("Parameter 'node' must not be 'Null_iir'.")
+
location = nodes.Get_Location(node)
file = files_map.Location_To_File(location)
fileName = name_table.Get_Name_Ptr(files_map.Get_File_Name(file))
diff --git a/pyGHDL/libghdl/__init__.py b/pyGHDL/libghdl/__init__.py
index 0d3c75fa1..525710590 100644
--- a/pyGHDL/libghdl/__init__.py
+++ b/pyGHDL/libghdl/__init__.py
@@ -37,6 +37,7 @@ import os
import sys
from pathlib import Path
from shutil import which
+from typing import List
from pydecor import export
@@ -48,7 +49,15 @@ from pyGHDL.libghdl.version import __version__
class LibGHDLException(GHDLBaseException):
- pass
+ _internalErrors: List[str]
+
+ def __init__(self, message: str, errors: List[str]):
+ super().__init__(message)
+ self._internalErrors = errors
+
+ @property
+ def InternalErrors(self):
+ return self._internalErrors
def _get_libghdl_name() -> Path: