nir/algebraic: Detect some kinds of malformed variable names

I spent over an hour trying to debug a problem if a condition on a
variable not being applied.  The problem turned out to be
"a(is_not_negative" instead of "a(is_not_negative)".  This commit would
have detected that problem and failed to build.

v2: Just add $ to the end of the existing regex, and it will fail to
match a malformed string.  Suggested by Jason.

Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com> [v1]
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4720>
This commit is contained in:
Ian Romanick 2020-04-23 17:39:07 -07:00
parent fc4eb0714c
commit ba8f7f3fa2
1 changed files with 6 additions and 2 deletions

View File

@ -283,17 +283,21 @@ class Constant(Value):
return self.value == other.value
# The $ at the end forces there to be an error if any part of the string
# doesn't match one of the field patterns.
_var_name_re = re.compile(r"(?P<const>#)?(?P<name>\w+)"
r"(?:@(?P<type>int|uint|bool|float)?(?P<bits>\d+)?)?"
r"(?P<cond>\([^\)]+\))?"
r"(?P<swiz>\.[xyzw]+)?")
r"(?P<swiz>\.[xyzw]+)?"
r"$")
class Variable(Value):
def __init__(self, val, name, varset):
Value.__init__(self, val, name, "variable")
m = _var_name_re.match(val)
assert m and m.group('name') is not None
assert m and m.group('name') is not None, \
"Malformed variable name \"{}\".".format(val)
self.var_name = m.group('name')