diff mbox

[3/3] symbol.c: Set correct size of array from parenthesized string initializer

Message ID 5195370B.9080702@ramsay1.demon.co.uk (mailing list archive)
State Mainlined, archived
Headers show

Commit Message

Ramsay Jones May 16, 2013, 7:44 p.m. UTC
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

Comments

Christopher Li May 18, 2013, 7:38 a.m. UTC | #1
On 05/16/2013 12:44 PM, Ramsay Jones wrote:

> 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";

That seems not complete. I haven't found the official gcc rules
about parenthesized string. I just try the following forms and
gcc takes it.

char x1[] = { ("hello") };
char x2[] = { (("hello")) };

I guess the rules is that parenthesized string can appear in
any place where string was allowed.

Chris
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Ramsay Jones May 20, 2013, 6:37 p.m. UTC | #2
Christopher Li wrote:
> On 05/16/2013 12:44 PM, Ramsay Jones wrote:
> 
>> 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";
> 
> That seems not complete. I haven't found the official gcc rules
> about parenthesized string. I just try the following forms and
> gcc takes it.
> 
> char x1[] = { ("hello") };
> char x2[] = { (("hello")) };
> 
> I guess the rules is that parenthesized string can appear in
> any place where string was allowed.

Until i18n work started on git, I didn't know this was a gcc
extension at all! ;-) I looked at the gcc documentation available
to me at the time, and could not find it mentioned at all, let
alone as a gcc language extension. However, if it wasn't for the
-Wparen-string sparse warning, I wouldn't have suspected that
sparse was supposed to support it.

Also, I was slightly surprised that MSVC supported this syntax.
So, I looked at fixing sparse and came up with my patch. I then
found that tcc didn't support this syntax, which is not standard
C after all, so I submitted a patch to fix the git gettext 'N_'
macro which was the cause of the problem.

ATB,
Ramsay Jones



--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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
+ */