nir/algebraic: Detect some kinds of malformed variable names
authorIan Romanick <ian.d.romanick@intel.com>
Fri, 24 Apr 2020 00:39:07 +0000 (17:39 -0700)
committerIan Romanick <ian.d.romanick@intel.com>
Mon, 27 Apr 2020 16:08:34 +0000 (09:08 -0700)
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>

src/compiler/nir/nir_algebraic.py

index 480dcaf3cf55d5b7310b175762e3244b80233d42..2112854570daa7f5d309749299c5014b45f15366 100644 (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')