linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 3/3] symbol.c: Set correct size of array from parenthesized string initializer
@ 2013-05-16 19:44 Ramsay Jones
  2013-05-18  7:38 ` Christopher Li
  0 siblings, 1 reply; 9+ messages in thread
From: Ramsay Jones @ 2013-05-16 19:44 UTC (permalink / raw)
  To: Christopher Li; +Cc: Sparse Mailing-list



Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
---
 symbol.c                      | 10 ++++++++++
 validation/init-char-array1.c | 25 +++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 validation/init-char-array1.c

diff --git a/symbol.c b/symbol.c
index 80a2f23..051a909 100644
--- a/symbol.c
+++ b/symbol.c
@@ -284,6 +284,15 @@ static int count_array_initializer(struct symbol *t, struct expression *expr)
 	if (t->ctype.base_type == &int_type && t->ctype.modifiers & MOD_CHAR)
 		is_char = 1;
 
+	/* check for a parenthesized string: char x[] = ("string"); */
+	if (is_char && expr->type == EXPR_PREOP && expr->op == '(') {
+		struct expression *e = expr;
+		while (e && e->type == EXPR_PREOP && e->op == '(')
+			e = e->unop;
+		if (e && e->type == EXPR_STRING)
+			expr = e;
+	}
+
 	switch (expr->type) {
 	case EXPR_INITIALIZER: {
 		struct expression *entry;
@@ -310,6 +319,7 @@ static int count_array_initializer(struct symbol *t, struct expression *expr)
 	case EXPR_STRING:
 		if (is_char)
 			nr = expr->string->length;
+		break;
 	default:
 		break;
 	}
diff --git a/validation/init-char-array1.c b/validation/init-char-array1.c
new file mode 100644
index 0000000..7427702
--- /dev/null
+++ b/validation/init-char-array1.c
@@ -0,0 +1,25 @@
+/*
+ * for array of char, ("...") as the initializer is an gcc language
+ * extension. check that a parenthesized string initializer is handled
+ * correctly and that -Wparen-string warns about it's use.
+ */
+static const char u[] = ("hello");
+static const char v[] = {"hello"};
+static const char w[] = "hello";
+static const char x[5] = "hello";
+
+static void f(void)
+{
+	char a[1/(sizeof(u) == 6)];
+	char b[1/(sizeof(v) == 6)];
+	char c[1/(sizeof(w) == 6)];
+	char d[1/(sizeof(x) == 5)];
+}
+/*
+ * check-name: parenthesized string initializer
+ * check-command: sparse -Wparen-string $file
+ *
+ * check-error-start
+init-char-array1.c:6:26: warning: array initialized from parenthesized string constant
+ * check-error-end
+ */
-- 
1.8.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2013-06-01 17:48 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-16 19:44 [PATCH 3/3] symbol.c: Set correct size of array from parenthesized string initializer Ramsay Jones
2013-05-18  7:38 ` Christopher Li
2013-05-18  8:14   ` Chris Li
2013-05-20 18:42     ` Ramsay Jones
2013-05-23 15:28       ` Chris Li
2013-05-25 20:01         ` Ramsay Jones
2013-05-29 10:02           ` Chris Li
2013-06-01 17:42             ` Ramsay Jones
2013-05-20 18:37   ` Ramsay Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).