[gobject-introspection/ebassi/property-annotation: 16/19] scanner: Warn if property annotations are mismatched




commit 5a626854555da041ce04fabcc48b6c6cfeeeb23d
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Tue Jul 27 11:31:41 2021 +0100

    scanner: Warn if property annotations are mismatched
    
    If the (set-property) and (get-property) annotations on methods do not
    round trip with the (setter) and (getter) annotations on the
    corresponding property, we want to emit a warning.

 giscanner/maintransformer.py           | 44 +++++++++++++++++++++++-----------
 tests/scanner/Regress-1.0-expected.gir | 10 +++++---
 2 files changed, 37 insertions(+), 17 deletions(-)
---
diff --git a/giscanner/maintransformer.py b/giscanner/maintransformer.py
index bca69697..b4d44d52 100644
--- a/giscanner/maintransformer.py
+++ b/giscanner/maintransformer.py
@@ -1466,25 +1466,41 @@ method or constructor of some type."""
     def _pair_property_accessors(self, node):
         """Look for accessor methods for class properties"""
         for prop in node.properties:
-            normalized_name = prop.name.replace('-', '_')
-            if prop.writable and not prop.construct_only:
-                setter = 'set_' + normalized_name
-            else:
-                setter = None
-            if prop.readable:
-                # Heuristic: read-only properties can have getters that are
-                # just the property name, like: gtk_widget_has_focus()
-                if not prop.writable and prop.type.is_equiv(ast.TYPE_BOOLEAN):
-                    getter = normalized_name
-                else:
-                    getter = 'get_' + normalized_name
-            else:
-                getter = None
+            setter = prop.setter
+            if setter is None:
+                normalized_name = prop.name.replace('-', '_')
+                if prop.writable and not prop.construct_only:
+                    setter = 'set_' + normalized_name
+            getter = prop.getter
+            if getter is None:
+                if prop.readable:
+                    # Heuristic: read-only properties can have getters that are
+                    # just the property name, like: gtk_widget_has_focus()
+                    if not prop.writable and prop.type.is_equiv(ast.TYPE_BOOLEAN):
+                        getter = normalized_name
+                    else:
+                        getter = 'get_' + normalized_name
             for method in node.methods:
                 if setter is not None and method.name == setter:
+                    if method.set_property is None:
+                        method.set_property = prop.name
+                    elif method.set_property != prop.name:
+                        message.warn_node(method,
+                                          "Setter method '%s' for property '%s' has a "
+                                          "mismatched '(set-property %s)' annotation" %
+                                          (method.symbol, prop.name, method.set_property))
+                        method.set_property = prop.name
                     prop.setter = method.name
                     continue
                 if getter is not None and method.name == getter:
+                    if method.get_property is None:
+                        method.get_property = prop.name
+                    elif method.get_property != prop.name:
+                        message.warn_node(method,
+                                          "Getter method '%s' for property '%s' has a "
+                                          "mismatched '(get-property %s)' annotation" %
+                                          (method.symbol, prop.name, method.get_property))
+                        method.get_property = prop.name
                     prop.getter = method.name
                     continue
 
diff --git a/tests/scanner/Regress-1.0-expected.gir b/tests/scanner/Regress-1.0-expected.gir
index 4786080e..65a6a83d 100644
--- a/tests/scanner/Regress-1.0-expected.gir
+++ b/tests/scanner/Regress-1.0-expected.gir
@@ -4286,7 +4286,9 @@ case.</doc>
           </parameter>
         </parameters>
       </method>
-      <method name="set_bare" c:identifier="regress_test_obj_set_bare">
+      <method name="set_bare"
+              c:identifier="regress_test_obj_set_bare"
+              glib:set-property="bare">
         <source-position filename="regress.h" line="828"/>
         <return-value transfer-ownership="none">
           <type name="none" c:type="void"/>
@@ -5656,7 +5658,8 @@ the introspection client langage.</doc>
         </parameters>
       </function>
       <method name="get_testbool"
-              c:identifier="regress_test_wi_802_1x_get_testbool">
+              c:identifier="regress_test_wi_802_1x_get_testbool"
+              glib:get-property="testbool">
         <source-position filename="regress.h" line="1256"/>
         <return-value transfer-ownership="none">
           <type name="gboolean" c:type="gboolean"/>
@@ -5668,7 +5671,8 @@ the introspection client langage.</doc>
         </parameters>
       </method>
       <method name="set_testbool"
-              c:identifier="regress_test_wi_802_1x_set_testbool">
+              c:identifier="regress_test_wi_802_1x_set_testbool"
+              glib:set-property="testbool">
         <source-position filename="regress.h" line="1259"/>
         <return-value transfer-ownership="none">
           <type name="none" c:type="void"/>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]