Commit 26d4fec7 by Joseph Myers Committed by Joseph Myers

c-parse.in (initelt): Give appropriate pedantic warnings...

	* c-parse.in (initelt): Give appropriate pedantic warnings,
	depending on flag_isoc99, for non-ISO syntax and for C99 syntax
	outside C99 mode.
	(designator): If pedantic, pedwarn for a designator specifying a
	range of elements.
	* c-typeck.c (set_init_index, set_init_label): Don't pedwarn for
	these cases.
	* extend.texi: Document the C99 syntax as the preferred syntax,
	and the pre-2.5 syntax as obsolete.  Mention use of designator
	lists for nested subobjects.

From-SVN: r37421
parent 1173593d
2000-11-13 Joseph S. Myers <jsm28@cam.ac.uk> 2000-11-13 Joseph S. Myers <jsm28@cam.ac.uk>
* c-parse.in (initelt): Give appropriate pedantic warnings,
depending on flag_isoc99, for non-ISO syntax and for C99 syntax
outside C99 mode.
(designator): If pedantic, pedwarn for a designator specifying a
range of elements.
* c-typeck.c (set_init_index, set_init_label): Don't pedwarn for
these cases.
* extend.texi: Document the C99 syntax as the preferred syntax,
and the pre-2.5 syntax as obsolete. Mention use of designator
lists for nested subobjects.
2000-11-13 Joseph S. Myers <jsm28@cam.ac.uk>
* diagnostic.c (vbuild_message_string, output_do_printf, vnotice): * diagnostic.c (vbuild_message_string, output_do_printf, vnotice):
Add ATTRIBUTE_PRINTF. Add ATTRIBUTE_PRINTF.
* tradcpp.c (v_message, warning, error, fatal, error_with_line): * tradcpp.c (v_message, warning, error, fatal, error_with_line):
......
...@@ -1132,9 +1132,15 @@ initlist1: ...@@ -1132,9 +1132,15 @@ initlist1:
It may use braces. */ It may use braces. */
initelt: initelt:
designator_list '=' initval designator_list '=' initval
{ if (pedantic && ! flag_isoc99)
pedwarn ("ISO C89 forbids specifying subobject to initialize"); }
| designator initval | designator initval
{ if (pedantic)
pedwarn ("obsolete use of designated initializer without `='"); }
| identifier ':' | identifier ':'
{ set_init_label ($1); } { set_init_label ($1);
if (pedantic)
pedwarn ("obsolete use of designated initializer with `:'"); }
initval initval
| initval | initval
; ;
...@@ -1162,7 +1168,9 @@ designator: ...@@ -1162,7 +1168,9 @@ designator:
so don't include these productions in the Objective-C grammar. */ so don't include these productions in the Objective-C grammar. */
ifc ifc
| '[' expr_no_commas ELLIPSIS expr_no_commas ']' | '[' expr_no_commas ELLIPSIS expr_no_commas ']'
{ set_init_index ($2, $4); } { set_init_index ($2, $4);
if (pedantic)
pedwarn ("ISO C forbids specifying range of elements to initialize"); }
| '[' expr_no_commas ']' | '[' expr_no_commas ']'
{ set_init_index ($2, NULL_TREE); } { set_init_index ($2, NULL_TREE); }
end ifc end ifc
......
...@@ -5438,12 +5438,7 @@ set_init_index (first, last) ...@@ -5438,12 +5438,7 @@ set_init_index (first, last)
if (last != 0 && tree_int_cst_lt (last, first)) if (last != 0 && tree_int_cst_lt (last, first))
error_init ("empty index range in initializer"); error_init ("empty index range in initializer");
else else
{ constructor_range_end = last ? convert (bitsizetype, last) : 0;
if (pedantic)
pedwarn ("ISO C89 forbids specifying element to initialize");
constructor_range_end = last ? convert (bitsizetype, last) : 0;
}
} }
} }
...@@ -5477,11 +5472,7 @@ set_init_label (fieldname) ...@@ -5477,11 +5472,7 @@ set_init_label (fieldname)
error ("field `%s' already initialized", error ("field `%s' already initialized",
IDENTIFIER_POINTER (fieldname)); IDENTIFIER_POINTER (fieldname));
else else
{ constructor_fields = tail;
constructor_fields = tail;
if (pedantic)
pedwarn ("ISO C89 forbids specifying structure member to initialize");
}
} }
/* Add a new initializer to the tree of pending initializers. PURPOSE /* Add a new initializer to the tree of pending initializers. PURPOSE
......
...@@ -1156,19 +1156,20 @@ to a cast. ...@@ -1156,19 +1156,20 @@ to a cast.
@cindex labeled elements in initializers @cindex labeled elements in initializers
@cindex case labels in initializers @cindex case labels in initializers
Standard C requires the elements of an initializer to appear in a fixed Standard C89 requires the elements of an initializer to appear in a fixed
order, the same as the order of the elements in the array or structure order, the same as the order of the elements in the array or structure
being initialized. being initialized.
In GNU C you can give the elements in any order, specifying the array In ISO C99 you can give the elements in any order, specifying the array
indices or structure field names they apply to. This extension is not indices or structure field names they apply to, and GNU C allows this as
an extension in C89 mode as well. This extension is not
implemented in GNU C++. implemented in GNU C++.
To specify an array index, write @samp{[@var{index}]} or To specify an array index, write
@samp{[@var{index}] =} before the element value. For example, @samp{[@var{index}] =} before the element value. For example,
@example @example
int a[6] = @{ [4] 29, [2] = 15 @}; int a[6] = @{ [4] = 29, [2] = 15 @};
@end example @end example
@noindent @noindent
...@@ -1182,8 +1183,13 @@ int a[6] = @{ 0, 0, 15, 0, 29, 0 @}; ...@@ -1182,8 +1183,13 @@ int a[6] = @{ 0, 0, 15, 0, 29, 0 @};
The index values must be constant expressions, even if the array being The index values must be constant expressions, even if the array being
initialized is automatic. initialized is automatic.
An alternative syntax for this which has been obsolete since GCC 2.5 but
GCC still accepts is to write @samp{[@var{index}]} before the element
value, with no @samp{=}.
To initialize a range of elements to the same value, write To initialize a range of elements to the same value, write
@samp{[@var{first} ... @var{last}] = @var{value}}. For example, @samp{[@var{first} ... @var{last}] = @var{value}}. This is a GNU
extension. For example,
@example @example
int widths[] = @{ [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 @}; int widths[] = @{ [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 @};
...@@ -1194,7 +1200,7 @@ Note that the length of the array is the highest value specified ...@@ -1194,7 +1200,7 @@ Note that the length of the array is the highest value specified
plus one. plus one.
In a structure initializer, specify the name of a field to initialize In a structure initializer, specify the name of a field to initialize
with @samp{@var{fieldname}:} before the element value. For example, with @samp{.@var{fieldname} =} before the element value. For example,
given the following structure, given the following structure,
@example @example
...@@ -1205,7 +1211,7 @@ struct point @{ int x, y; @}; ...@@ -1205,7 +1211,7 @@ struct point @{ int x, y; @};
the following initialization the following initialization
@example @example
struct point p = @{ y: yvalue, x: xvalue @}; struct point p = @{ .y = yvalue, .x = xvalue @};
@end example @end example
@noindent @noindent
...@@ -1215,11 +1221,11 @@ is equivalent to ...@@ -1215,11 +1221,11 @@ is equivalent to
struct point p = @{ xvalue, yvalue @}; struct point p = @{ xvalue, yvalue @};
@end example @end example
Another syntax which has the same meaning is @samp{.@var{fieldname} =}., Another syntax which has the same meaning, obsolete since GCC 2.5, is
as shown here: @samp{@var{fieldname}:}, as shown here:
@example @example
struct point p = @{ .y = yvalue, .x = xvalue @}; struct point p = @{ y: yvalue, x: xvalue @};
@end example @end example
You can also use an element label (with either the colon syntax or the You can also use an element label (with either the colon syntax or the
...@@ -1229,7 +1235,7 @@ of the union should be used. For example, ...@@ -1229,7 +1235,7 @@ of the union should be used. For example,
@example @example
union foo @{ int i; double d; @}; union foo @{ int i; double d; @};
union foo f = @{ d: 4 @}; union foo f = @{ .d = 4 @};
@end example @end example
@noindent @noindent
...@@ -1264,6 +1270,16 @@ int whitespace[256] ...@@ -1264,6 +1270,16 @@ int whitespace[256]
['\f'] = 1, ['\n'] = 1, ['\r'] = 1 @}; ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 @};
@end example @end example
You can also write a series of @samp{.@var{fieldname}} and
@samp{[@var{index}]} element labels before an @samp{=} to specify a
nested subobject to initialize; the list is taken relative to the
subobject corresponding to the closest surrounding brace pair. For
example, with the @samp{struct point} declaration above:
@example
struct point ptarray[10] = @{ [2].y = yv2, [2].x = xv2, [0].x = xv0 @};
@end example
@node Case Ranges @node Case Ranges
@section Case Ranges @section Case Ranges
@cindex case ranges @cindex case ranges
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment