Skip to contentSkip to conversation

checks: clean up string parsing

PATCH Applied Ilya Lipnitskiy 2 participants 6 messages May 4, 2021 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

cover checks: clean up string parsing

Add a new function, strends(), as suggested in [0]. Replace existing suffix logic with calls to strends().

Make nr-gpios exception stricter by aligning with the devicetree spec and the logic in the Linux kernel.

Add a test case to cover correct non-deprecated GPIO properties.

[0]: https://www.spinics.net/lists/devicetree-compiler/msg03634.html

Ilya Lipnitskiy (4):
checks: tigthen up nr-gpios prop exception
dtc.h: add strends for suffix matching
checks: replace strstr and strrchr with strends
tests: add a positive gpio test case

 checks.c            | 25 +++++++------------------
 dtc.h               | 10 ++++++++++
 tests/good-gpio.dts | 12 ++++++++++++
 tests/run_tests.sh  |  2 ++
 4 files changed, 31 insertions(+), 18 deletions(-)
 create mode 100644 tests/good-gpio.dts
Signature · 1 line
2.31.1

A message that is not in the archive (sent off-list or not yet received): 20210504035944.8453-1-ilya.lipnitskiy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

PATCH 1/4 checks: tigthen up nr-gpios prop exception

There are no instances of nr-gpio in the Linux kernel tree, only "[<vendor>,]nr-gpios", so make the check stricter.

nr-gpios without a "vendor," prefix is also invalid, according to the DT spec[0], and there are no DT files in the Linux kernel tree with non-vendor nr-gpios. There are some drivers, but they are not DT spec compliant, so don't suppress the check for them.

[0]:

---
 checks.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
checks.c +1 -1
index c4207720ce8c..7e9d73af02a3 100644
@@ -1494,7 +1494,7 @@ static bool prop_is_gpio(struct property *prop)
 	 * *-gpios and *-gpio can appear in property names,
 	 * so skip over any false matches (only one known ATM)
 	 */
-	if (strstr(prop->name, "nr-gpio"))
+	if (strstr(prop->name, ",nr-gpios"))
 		return false;
 
 	str = strrchr(prop->name, '-');
Signature · 1 line
2.31.1
#3 lore raw

PATCH 2/4 dtc.h: add strends for suffix matching

In reply to a message not in the archive

Logic is similar to strcmp_suffix in <kernel>/drivers/of/property.c with the exception that strends allows string length to equal suffix length.

---
 dtc.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
dtc.h +10 -0
index d3e82fb8e3db..62963617c79a 100644
@@ -86,6 +86,16 @@ static inline uint64_t dtb_ld64(const void *p)
 #define streq(a, b)	(strcmp((a), (b)) == 0)
 #define strstarts(s, prefix)	(strncmp((s), (prefix), strlen(prefix)) == 0)
 #define strprefixeq(a, n, b)	(strlen(b) == (n) && (memcmp(a, b, n) == 0))
+static inline bool strends(const char *str, const char *suffix)
+{
+	unsigned int len, suffix_len;
+
+	len = strlen(str);
+	suffix_len = strlen(suffix);
+	if (len < suffix_len)
+		return false;
+	return streq(str + len - suffix_len, suffix);
+}
 
 #define ALIGN(x, a)	(((x) + (a) - 1) & ~((a) - 1))
 
Signature · 1 line
2.31.1
#4 lore raw

PATCH 3/4 checks: replace strstr and strrchr with strends

In reply to a message not in the archive

Makes the logic more clear

---
 checks.c | 25 +++++++------------------
 1 file changed, 7 insertions(+), 18 deletions(-)
checks.c +7 -18
index 7e9d73af02a3..eb91c8ea22fa 100644
@@ -687,8 +687,7 @@ static void check_names_is_string_list(struct check *c, struct dt_info *dti,
 	struct property *prop;
 
 	for_each_property(node, prop) {
-		const char *s = strrchr(prop->name, '-');
-		if (!s || !streq(s, "-names"))
+		if (!strends(prop->name, "-names"))
 			continue;
 
 		c->data = prop->name;
@@ -1488,24 +1487,17 @@ WARNING_PROPERTY_PHANDLE_CELLS(thermal_sensors, "thermal-sensors", "#thermal-sen
 
 static bool prop_is_gpio(struct property *prop)
 {
-	char *str;
-
 	/*
 	 * *-gpios and *-gpio can appear in property names,
 	 * so skip over any false matches (only one known ATM)
 	 */
-	if (strstr(prop->name, ",nr-gpios"))
+	if (strends(prop->name, ",nr-gpios"))
 		return false;
 
-	str = strrchr(prop->name, '-');
-	if (str)
-		str++;
-	else
-		str = prop->name;
-	if (!(streq(str, "gpios") || streq(str, "gpio")))
-		return false;
-
-	return true;
+	return strends(prop->name, "-gpios") ||
+		streq(prop->name, "gpios") ||
+		strends(prop->name, "-gpio") ||
+		streq(prop->name, "gpio");
 }
 
 static void check_gpios_property(struct check *c,
@@ -1540,13 +1532,10 @@ static void check_deprecated_gpio_property(struct check *c,
 	struct property *prop;
 
 	for_each_property(node, prop) {
-		char *str;
-
 		if (!prop_is_gpio(prop))
 			continue;
 
-		str = strstr(prop->name, "gpio");
-		if (!streq(str, "gpio"))
+		if (!strends(prop->name, "gpio"))
 			continue;
 
 		FAIL_PROP(c, dti, node, prop,
Signature · 1 line
2.31.1
#5 lore raw

PATCH 4/4 tests: add a positive gpio test case

In reply to a message not in the archive

Ensure that properly named properties don't trigger warnings

---
 tests/good-gpio.dts | 12 ++++++++++++
 tests/run_tests.sh  |  2 ++
 2 files changed, 14 insertions(+)
 create mode 100644 tests/good-gpio.dts
tests/good-gpio.dts new file +12 -0
new file mode 100644
index 000000000000..65d1c177afa5
@@ -0,0 +1,12 @@
+/dts-v1/;
+
+/ {
+	gpio: gpio-controller {
+		#gpio-cells = <3>;
+	};
+
+	node {
+		foo,nr-gpios = <1>;
+		foo-gpios = <&gpio 1 2 3>;
+	};
+};
tests/run_tests.sh +2 -0
index 5c2b1e8dc29b..0e8ecdb40c2c 100755
@@ -709,10 +709,12 @@ dtc_tests () {
     check_tests "$SRCDIR/unit-addr-unique.dts" unique_unit_address
     check_tests "$SRCDIR/bad-phandle-cells.dts" interrupts_extended_property
     check_tests "$SRCDIR/bad-gpio.dts" gpios_property
+    check_tests "$SRCDIR/good-gpio.dts" -n gpios_property
     check_tests "$SRCDIR/bad-graph.dts" graph_child_address
     check_tests "$SRCDIR/bad-graph.dts" graph_port
     check_tests "$SRCDIR/bad-graph.dts" graph_endpoint
     run_sh_test "$SRCDIR/dtc-checkfails.sh" deprecated_gpio_property -- -Wdeprecated_gpio_property -I dts -O dtb "$SRCDIR/bad-gpio.dts"
+    run_sh_test "$SRCDIR/dtc-checkfails.sh" -n deprecated_gpio_property -- -Wdeprecated_gpio_property -I dts -O dtb "$SRCDIR/good-gpio.dts"
     check_tests "$SRCDIR/bad-interrupt-cells.dts" interrupts_property
     check_tests "$SRCDIR/bad-interrupt-controller.dts" interrupt_provider
     run_sh_test "$SRCDIR/dtc-checkfails.sh" node_name_chars -- -I dtb -O dtb bad_node_char.dtb
Signature · 1 line
2.31.1
Applied
#6 lore raw
In reply to a message not in the archive
Quoted 7 lines · Ilya Lipnitskiy

Add a new function, strends(), as suggested in [0]. Replace existing suffix logic with calls to strends().

Make nr-gpios exception stricter by aligning with the devicetree spec and the logic in the Linux kernel.

Add a test case to cover correct non-deprecated GPIO properties.

Applied, thanks.

Quoted 16 lines

[0]: https://www.spinics.net/lists/devicetree-compiler/msg03634.html

Ilya Lipnitskiy (4):
checks: tigthen up nr-gpios prop exception
dtc.h: add strends for suffix matching
checks: replace strstr and strrchr with strends
tests: add a positive gpio test case

 checks.c            | 25 +++++++------------------
 dtc.h               | 10 ++++++++++
 tests/good-gpio.dts | 12 ++++++++++++
 tests/run_tests.sh  |  2 ++
 4 files changed, 31 insertions(+), 18 deletions(-)
 create mode 100644 tests/good-gpio.dts
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson