Skip to content

Commit

Permalink
add location support to feature checks
Browse files Browse the repository at this point in the history
mlog can already print location info, and we use this often -- including
for custom feature warnings already. Make this work everywhere, so that
it is feasible to move such custom warnings to globally tracked
Features.
  • Loading branch information
eli-schwartz committed Nov 21, 2021
1 parent 329783d commit 7a033c7
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions mesonbuild/interpreterbase/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,13 @@ def types_description() -> str:
class FeatureCheckBase(metaclass=abc.ABCMeta):
"Base class for feature version checks"

feature_registry: T.ClassVar[T.Dict[str, T.Dict[str, T.Set[str]]]]
feature_registry: T.ClassVar[T.Dict[str, T.Dict[str, T.Set[T.Tuple[str, T.Optional['mparser.BaseNode']]]]]]

def __init__(self, feature_name: str, version: str, extra_message: T.Optional[str] = None):
def __init__(self, feature_name: str, version: str, extra_message: T.Optional[str] = None, location: T.Optional['mparser.BaseNode'] = None):
self.feature_name = feature_name # type: str
self.feature_version = version # type: str
self.extra_message = extra_message or '' # type: str
self.location = location

@staticmethod
def get_target_version(subproject: str) -> str:
Expand Down Expand Up @@ -584,12 +585,14 @@ def use(self, subproject: str) -> None:
register = self.feature_registry[subproject]
if self.feature_version not in register:
register[self.feature_version] = set()
if self.feature_name in register[self.feature_version]:

feature_key = (self.feature_name, self.location)
if feature_key in register[self.feature_version]:
# Don't warn about the same feature multiple times
# FIXME: This is needed to prevent duplicate warnings, but also
# means we won't warn about a feature used in multiple places.
return
register[self.feature_version].add(self.feature_name)
register[self.feature_version].add(feature_key)
self.log_usage_warning(tv)

@classmethod
Expand All @@ -599,7 +602,7 @@ def report(cls, subproject: str) -> None:
warning_str = cls.get_warning_str_prefix(cls.get_target_version(subproject))
fv = cls.feature_registry[subproject]
for version in sorted(fv.keys()):
warning_str += '\n * {}: {}'.format(version, fv[version])
warning_str += '\n * {}: {}'.format(version, {i[0] for i in fv[version]})
mlog.warning(warning_str)

def log_usage_warning(self, tv: str) -> None:
Expand All @@ -621,9 +624,9 @@ def wrapped(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:

@classmethod
def single_use(cls, feature_name: str, version: str, subproject: str,
extra_message: T.Optional[str] = None) -> None:
extra_message: T.Optional[str] = None, location: T.Optional['mparser.BaseNode'] = None) -> None:
"""Oneline version that instantiates and calls use()."""
cls(feature_name, version, extra_message).use(subproject)
cls(feature_name, version, extra_message, location).use(subproject)


class FeatureNew(FeatureCheckBase):
Expand All @@ -632,7 +635,7 @@ class FeatureNew(FeatureCheckBase):
# Class variable, shared across all instances
#
# Format: {subproject: {feature_version: set(feature_names)}}
feature_registry = {} # type: T.ClassVar[T.Dict[str, T.Dict[str, T.Set[str]]]]
feature_registry = {} # type: T.ClassVar[T.Dict[str, T.Dict[str, T.Set[T.Tuple[str, T.Optional[mparser.BaseNode]]]]]]

@staticmethod
def check_version(target_version: str, feature_version: str) -> bool:
Expand All @@ -651,15 +654,15 @@ def log_usage_warning(self, tv: str) -> None:
]
if self.extra_message:
args.append(self.extra_message)
mlog.warning(*args)
mlog.warning(*args, location=self.location)

class FeatureDeprecated(FeatureCheckBase):
"""Checks for deprecated features"""

# Class variable, shared across all instances
#
# Format: {subproject: {feature_version: set(feature_names)}}
feature_registry = {} # type: T.ClassVar[T.Dict[str, T.Dict[str, T.Set[str]]]]
feature_registry = {} # type: T.ClassVar[T.Dict[str, T.Dict[str, T.Set[T.Tuple[str, T.Optional[mparser.BaseNode]]]]]]

@staticmethod
def check_version(target_version: str, feature_version: str) -> bool:
Expand All @@ -679,7 +682,7 @@ def log_usage_warning(self, tv: str) -> None:
]
if self.extra_message:
args.append(self.extra_message)
mlog.warning(*args)
mlog.warning(*args, location=self.location)


class FeatureCheckKwargsBase(metaclass=abc.ABCMeta):
Expand Down

0 comments on commit 7a033c7

Please sign in to comment.