hdl.ir: make UnusedElaboratable a real warning.

Before this commit, it was a print statement, and therefore, command
interpreter options like -Wignore did not affect it. There is no API
to access the warning filter list, so it was turned into a real
warning; and further, since Python 3.6, tracemalloc can be used
as a standard method to display traceback to allocation site instead
of the ad-hoc traceback logic that was used in Elaboratable before.
This commit is contained in:
whitequark 2019-07-10 12:46:46 +00:00
parent 37f363e338
commit 2fa858b003

View file

@ -10,26 +10,30 @@ from .ast import *
from .cd import * from .cd import *
__all__ = ["Elaboratable", "DriverConflict", "Fragment", "Instance"] __all__ = ["UnusedElaboratable", "Elaboratable", "DriverConflict", "Fragment", "Instance"]
class UnusedElaboratable(Warning):
pass
class Elaboratable(metaclass=ABCMeta): class Elaboratable(metaclass=ABCMeta):
_Elaboratable__silence = False _Elaboratable__silence = False
def __new__(cls, *args, **kwargs): def __new__(cls, *args, src_loc_at=0, **kwargs):
self = super().__new__(cls) self = super().__new__(cls)
self._Elaboratable__traceback = traceback.extract_stack()[:-1] self._Elaboratable__src_loc = traceback.extract_stack(limit=2 + src_loc_at)[0]
self._Elaboratable__used = False self._Elaboratable__used = False
return self return self
def __del__(self): def __del__(self):
if self._Elaboratable__silence: if self._Elaboratable__silence:
return return
if hasattr(self, "_Elaboratable__used") and not self._Elaboratable__used: if hasattr(self, "_Elaboratable__used") and not self._Elaboratable__used:
print("Warning: elaboratable created but never used\n", warnings.warn_explicit("{!r} created but never used".format(self), UnusedElaboratable,
"Constructor traceback (most recent call last):\n", filename=self._Elaboratable__src_loc.filename,
*traceback.format_list(self._Elaboratable__traceback), lineno=self._Elaboratable__src_loc.lineno,
file=sys.stderr, sep="") source=self)
_old_excepthook = sys.excepthook _old_excepthook = sys.excepthook