@@ -269,8 +269,21 @@ void merge_type(struct symbol *sym, struct symbol *base_type)
merge_type(sym, sym->ctype.base_type);
}
+static struct expression *parenthesized_string(struct expression *expr)
+{
+ if (expr && 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)
+ return e;
+ }
+ return NULL;
+}
+
static int count_array_initializer(struct symbol *t, struct expression *expr)
{
+ struct expression *p;
int nr = 0;
int is_char = 0;
@@ -296,6 +309,15 @@ static int count_array_initializer(struct symbol *t, struct expression *expr)
if (entry->idx_to >= nr)
nr = entry->idx_to+1;
break;
+ case EXPR_PREOP:
+ /* check for: char x[] = {("string")}; */
+ p = parenthesized_string(entry);
+ if (!(is_char && p)) {
+ nr++;
+ break;
+ }
+ entry = p;
+ /* fall through to string */
case EXPR_STRING:
if (is_char)
str_len = entry->string->length;
@@ -307,9 +329,17 @@ static int count_array_initializer(struct symbol *t, struct expression *expr)
nr = str_len;
break;
}
+ case EXPR_PREOP:
+ /* check for parenthesized string: char x[] = ("string"); */
+ p = parenthesized_string(expr);
+ if (!(is_char && p))
+ break;
+ expr = p;
+ /* fall through to string */
case EXPR_STRING:
if (is_char)
nr = expr->string->length;
+ break;
default:
break;
}
new file mode 100644
@@ -0,0 +1,28 @@
+/*
+ * 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[] = "hello";
+static const char y[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) == 6)];
+ char e[1/(sizeof(y) == 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
+init-char-array1.c:8:27: warning: array initialized from parenthesized string constant
+ * check-error-end
+ */