diff options
author | Emmanuele Bassi <ebassi@gnome.org> | 2022-02-17 16:33:43 +0000 |
---|---|---|
committer | Emmanuele Bassi <ebassi@gnome.org> | 2022-02-17 16:33:43 +0000 |
commit | 16c533bd9ec42d43b7912d2061d37a8a42966826 (patch) | |
tree | b9b9c5cb71112d48737367fe3af51490933dcaed | |
parent | 4db144118e8ce8029e2eb17f51e504a4237096af (diff) | |
download | gobject-introspection-16c533bd9ec42d43b7912d2061d37a8a42966826.tar.gz |
scanner: Validate members of enumeration types
Members of enumeration types should not have a name that starts with
a number, as that will inevitable break languages that do not allow
identifiers to start with a numerical value.
To avoid any grief, we change the name to begin with an underscore:
GDK_2BUTTON_PRESS -> Gdk.EventType._2BUTTON_PRESS
QMI_NAS_RADIO_INTERFACE_5GNR -> Qmi.NasRadioInterface._5GNR
We also emit a warning when using strict validation mode, so we give
a chance to projects to bail out before committing to a bad API.
Fixes: #418
-rw-r--r-- | giscanner/maintransformer.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/giscanner/maintransformer.py b/giscanner/maintransformer.py index 2004d254..a5ee7de1 100644 --- a/giscanner/maintransformer.py +++ b/giscanner/maintransformer.py @@ -126,6 +126,8 @@ class MainTransformer(object): if isinstance(node, (ast.Class, ast.Interface)): self._pair_class_virtuals(node) self._pair_property_accessors(node) + if isinstance(node, (ast.Enum, ast.Bitfield)): + self._pass_member_numeric_name(node) # Some annotations need to be post function pairing self._namespace.walk(self._pass_read_annotations2) @@ -1562,6 +1564,16 @@ method or constructor of some type.""" prop.getter = method.name continue + def _pass_member_numeric_name(self, node): + """Validate the name of the members of enumeration types.""" + for member in node.members: + if re.match(r"^[0-9]", member.name): + message.strict_node(member, + f"Member {member.name} for enumeration {node.name} starts " + "with a number", + context=node) + member.name = f"_{member.name}" + def _pass3(self, node, chain): """Pass 3 is after we've loaded GType data and performed type closure.""" |