summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett Regier <garrett.regier@riftio.com>2015-10-04 12:17:40 -0400
committerColin Walters <walters@verbum.org>2015-10-04 12:17:55 -0400
commit849f1eef10b18eddaf41c1e0b2cca87bf5d93739 (patch)
treed36ede7d5b8879699e0a3c391b89a8f4263c456a
parent2699d5116ab5aaf801521c0f6c96bda09a23fb15 (diff)
downloadgobject-introspection-849f1eef10b18eddaf41c1e0b2cca87bf5d93739.tar.gz
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>
-rw-r--r--giscanner/maintransformer.py21
-rw-r--r--tests/warn/Makefile.am3
-rw-r--r--tests/warn/invalid-allow-none.h14
-rw-r--r--tests/warn/invalid-nullable.h14
-rw-r--r--tests/warn/invalid-optional.h16
5 files changed, 65 insertions, 3 deletions
diff --git a/giscanner/maintransformer.py b/giscanner/maintransformer.py
index da904cfd..872395a0 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 e7a39345..fb7e989d 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 00000000..adea7556
--- /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 00000000..8a9174ab
--- /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 00000000..bcd8df6e
--- /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