summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2021-04-18 21:02:21 +0200
committerRico Tzschichholz <ricotz@ubuntu.com>2021-04-19 08:56:10 +0200
commit38d61fbff037687ea4772e6df85c7e22a74b335e (patch)
tree8afda5c73324db2f919774390d05ebbd9ca682b8
parent0202055109da1531034106d73e67bc30a4234d15 (diff)
downloadvala-38d61fbff037687ea4772e6df85c7e22a74b335e.tar.gz
vala: Properly check GLib.Object naming convention for properties
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/objects/property-class-invalid-name.test8
-rw-r--r--tests/objects/property-interface-invalid-name.test8
-rw-r--r--vala/valaproperty.vala28
-rw-r--r--vala/valasemanticanalyzer.vala5
5 files changed, 46 insertions, 5 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5b2610dd6..fb1133555 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -480,6 +480,7 @@ TESTS = \
objects/property-read-only-auto.vala \
objects/property-read-only-member-write.test \
objects/property-read-only-write.test \
+ objects/property-class-invalid-name.test \
objects/property-construct-only.vala \
objects/property-construct-only-write.test \
objects/property-construct-only-write-after.test \
@@ -487,6 +488,7 @@ TESTS = \
objects/property-delegate.vala \
objects/property-delegate-owned.vala \
objects/property-gboxed-nullable.vala \
+ objects/property-interface-invalid-name.test \
objects/property-real-struct-assignment.vala \
objects/property-real-struct-no-accessor.test \
objects/property-simple-type-struct-nullable.vala \
diff --git a/tests/objects/property-class-invalid-name.test b/tests/objects/property-class-invalid-name.test
new file mode 100644
index 000000000..b1fa27966
--- /dev/null
+++ b/tests/objects/property-class-invalid-name.test
@@ -0,0 +1,8 @@
+Invalid Code
+
+class Foo : Object {
+ public string 1foo { get; set; }
+}
+
+void main () {
+}
diff --git a/tests/objects/property-interface-invalid-name.test b/tests/objects/property-interface-invalid-name.test
new file mode 100644
index 000000000..e23273d49
--- /dev/null
+++ b/tests/objects/property-interface-invalid-name.test
@@ -0,0 +1,8 @@
+Invalid Code
+
+interface IFoo : Object {
+ public abstract string _foo { get; set; }
+}
+
+void main () {
+}
diff --git a/vala/valaproperty.vala b/vala/valaproperty.vala
index f07f137e0..3d2cbf78a 100644
--- a/vala/valaproperty.vala
+++ b/vala/valaproperty.vala
@@ -413,6 +413,14 @@ public class Vala.Property : Symbol, Lockable {
checked = true;
+ if (context.profile == Profile.GOBJECT && parent_symbol is ObjectTypeSymbol
+ && ((ObjectTypeSymbol) parent_symbol).is_subtype_of (context.analyzer.object_type)) {
+ if (!is_valid_name (name)) {
+ error = true;
+ Report.error (source_reference, "Name `%s' is not valid for a GLib.Object property", name);
+ }
+ }
+
if (this_parameter != null) {
this_parameter.check (context);
}
@@ -543,4 +551,24 @@ public class Vala.Property : Symbol, Lockable {
return !error;
}
+
+ // Ported from glib's "g_param_spec_is_valid_name"
+ static bool is_valid_name (string name) {
+ char* p;
+
+ if ((name[0] < 'A' || name[0] > 'Z')
+ && (name[0] < 'a' || name[0] > 'z')) {
+ return false;
+ }
+
+ for (p = name; *p != '\0'; p++) {
+ char c = *p;
+ if (c != '-' && c != '_' && (c < '0' || c > '9')
+ && (c < 'A' || c > 'Z') && (c < 'a' || c > 'z')) {
+ return false;
+ }
+ }
+
+ return true;
+ }
}
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index 9d2d7ea07..378c301bd 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -465,11 +465,6 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
return false;
}
- if (!prop.name[0].isalpha ()) {
- // GObject requires properties to start with a letter
- return false;
- }
-
if (type_sym is Interface && !prop.is_abstract && !prop.external && !prop.external_package) {
// GObject does not support non-abstract interface properties,
// however we assume external properties always are GObject properties