[gobject-introspection] scanner: Warn and ignore on incorrect optional/nullable/allow-none annotations



commit 849f1eef10b18eddaf41c1e0b2cca87bf5d93739
Author: Garrett Regier <garrett regier riftio com>
Date:   Sun Oct 4 12:17:40 2015 -0400

    scanner: Warn and ignore on incorrect optional/nullable/allow-none annotations
    
    These can easily be misunderstood, especially optional.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=752065
    Signed-off-by: Garrett Regier <garrett regier riftio com>

 giscanner/maintransformer.py    |   21 ++++++++++++++++++---
 tests/warn/Makefile.am          |    3 +++
 tests/warn/invalid-allow-none.h |   14 ++++++++++++++
 tests/warn/invalid-nullable.h   |   14 ++++++++++++++
 tests/warn/invalid-optional.h   |   16 ++++++++++++++++
 5 files changed, 65 insertions(+), 3 deletions(-)
---
diff --git a/giscanner/maintransformer.py b/giscanner/maintransformer.py
index da904cf..872395a 100644
--- a/giscanner/maintransformer.py
+++ b/giscanner/maintransformer.py
@@ -647,17 +647,32 @@ class MainTransformer(object):
         self._adjust_container_type(parent, node, annotations)
 
         if ANN_NULLABLE in annotations:
-            node.nullable = True
+            if self._is_pointer_type(node, annotations):
+                node.nullable = True
+            else:
+                message.warn('invalid "nullable" annotation: '
+                             'only valid for pointer types and out parameters',
+                             annotations.position)
 
         if ANN_OPTIONAL in annotations:
-            node.optional = True
+            if (not isinstance(node, ast.Return) and
+                    node.direction == ast.PARAM_DIRECTION_OUT):
+                node.optional = True
+            else:
+                message.warn('invalid "optional" annotation: '
+                             'only valid for out parameters',
+                             annotations.position)
 
         if ANN_ALLOW_NONE in annotations:
             if (node.direction == ast.PARAM_DIRECTION_OUT and
                     not isinstance(node, ast.Return)):
                 node.optional = True
-            else:
+            elif self._is_pointer_type(node, annotations):
                 node.nullable = True
+            else:
+                message.warn('invalid "allow-none" annotation: '
+                             'only valid for pointer types and out parameters',
+                             annotations.position)
 
         if (node.direction != ast.PARAM_DIRECTION_OUT and
                 (node.type.target_giname == 'Gio.AsyncReadyCallback' or
diff --git a/tests/warn/Makefile.am b/tests/warn/Makefile.am
index e7a3934..fb7e989 100644
--- a/tests/warn/Makefile.am
+++ b/tests/warn/Makefile.am
@@ -4,12 +4,15 @@ TESTS = \
        annotationparser.h \
        callback-invalid-scope.h \
        callback-missing-scope.h \
+       invalid-allow-none.h \
        invalid-array.h \
        invalid-closure.h \
        invalid-constructor.h \
        invalid-element-type.h \
        invalid-method.h \
+       invalid-nullable.h \
        invalid-option.h \
+       invalid-optional.h \
        invalid-out.h \
        invalid-transfer.h \
        missing-element-type.h \
diff --git a/tests/warn/invalid-allow-none.h b/tests/warn/invalid-allow-none.h
new file mode 100644
index 0000000..adea755
--- /dev/null
+++ b/tests/warn/invalid-allow-none.h
@@ -0,0 +1,14 @@
+#include "common.h"
+
+/**
+ * test_invalid_allow_none:
+ * @param: (allow-none):
+ * @param2: (allow-none):
+ *
+ * Returns: (allow-none):
+ */
+int test_invalid_allow_none(int param, GType param2);
+
+// EXPECT:5: Warning: Test: invalid "allow-none" annotation: only valid for pointer types and out parameters
+// EXPECT:6: Warning: Test: invalid "allow-none" annotation: only valid for pointer types and out parameters
+// EXPECT:8: Warning: Test: invalid "allow-none" annotation: only valid for pointer types and out parameters
diff --git a/tests/warn/invalid-nullable.h b/tests/warn/invalid-nullable.h
new file mode 100644
index 0000000..8a9174a
--- /dev/null
+++ b/tests/warn/invalid-nullable.h
@@ -0,0 +1,14 @@
+#include "common.h"
+
+/**
+ * test_invalid_nullable:
+ * @param: (nullable):
+ * @param2: (nullable):
+ *
+ * Returns: (nullable):
+ */
+int test_invalid_nullable(int param, GType param2);
+
+// EXPECT:5: Warning: Test: invalid "nullable" annotation: only valid for pointer types and out parameters
+// EXPECT:6: Warning: Test: invalid "nullable" annotation: only valid for pointer types and out parameters
+// EXPECT:8: Warning: Test: invalid "nullable" annotation: only valid for pointer types and out parameters
diff --git a/tests/warn/invalid-optional.h b/tests/warn/invalid-optional.h
new file mode 100644
index 0000000..bcd8df6
--- /dev/null
+++ b/tests/warn/invalid-optional.h
@@ -0,0 +1,16 @@
+#include "common.h"
+
+/**
+ * test_invalid_optional:
+ * @param: (optional):
+ * @param2: (optional):
+ * @param3: (optional) (inout):
+ *
+ * Returns: (optional):
+ */
+int *test_invalid_optional(int param, GObject *param2, int *param3);
+
+// EXPECT:5: Warning: Test: invalid "optional" annotation: only valid for out parameters
+// EXPECT:6: Warning: Test: invalid "optional" annotation: only valid for out parameters
+// EXPECT:7: Warning: Test: invalid "optional" annotation: only valid for out parameters
+// EXPECT:9: Warning: Test: invalid "optional" annotation: only valid for out parameters


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