Change warn_write_strings to flag_const_strings.
* c-decl.c, c-tree.h: Likewise.
cp/
* tree.c (lvalue_type): Fix for arrays.
* typeck.c (string_conv_p): New fn.
(convert_for_assignment): Use it.
(build_unary_op): Use lvalue_type.
* call.c (standard_conversion, convert_like): Use string_conv_p.
(add_function_candidate): Use lvalue_type.
* cvt.c (convert_to_reference): Likewise.
* decl2.c (lang_decode_option): Ignore -traditional.
* decl.c (init_decl_processing): flag_writable_strings inhibits
flag_const_strings.
* lang-options.h (lang_options): Add fconst-strings to the list
of valid options.
* decl2.c (lang_f_options, lang_decode_option): Likewise.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@21928
138bc75d-0d04-0410-961f-
82ee72b054a4
value = make_node (STRING_CST);
TREE_STRING_POINTER (value) = p;
TREE_STRING_LENGTH (value) = length;
- TREE_CONSTANT (value) = 1;
}
else
{
/* Create the array type for the string constant.
-Wwrite-strings says make the string constant an array of const char
- so that copying it to a non-const pointer will get a warning. */
- if (warn_write_strings
+ so that copying it to a non-const pointer will get a warning.
+ For C++, this is the standard behavior. */
+ if (flag_const_strings
&& (! flag_traditional && ! flag_writable_strings))
{
tree elements
TREE_TYPE (value)
= build_array_type (wide_flag ? wchar_type_node : char_type_node,
build_index_type (build_int_2 (nchars - 1, 0)));
- TREE_CONSTANT (value) = 1;
+
+ TREE_READONLY (value) = TREE_CONSTANT (value) = ! flag_writable_strings;
TREE_STATIC (value) = 1;
return value;
}
to get extra warnings from them. These warnings will be too numerous
to be useful, except in thoroughly ANSIfied programs. */
-int warn_write_strings;
+int flag_const_strings;
/* Nonzero means warn about pointer casts that can drop a type qualifier
from the pointer target type. */
else if (!strcmp (p, "-Wno-long-long"))
warn_long_long = 0;
else if (!strcmp (p, "-Wwrite-strings"))
- warn_write_strings = 1;
+ flag_const_strings = 1;
else if (!strcmp (p, "-Wno-write-strings"))
- warn_write_strings = 0;
+ flag_const_strings = 0;
else if (!strcmp (p, "-Wcast-qual"))
warn_cast_qual = 1;
else if (!strcmp (p, "-Wno-cast-qual"))
to get extra warnings from them. These warnings will be too numerous
to be useful, except in thoroughly ANSIfied programs. */
-extern int warn_write_strings;
+extern int flag_const_strings;
/* Nonzero means warn about sizeof (function) or addition/subtraction
of function pointers. */
+1998-08-24 Jason Merrill <jason@yorick.cygnus.com>
+
+ * tree.c (lvalue_type): Fix for arrays.
+ * typeck.c (string_conv_p): New fn.
+ (convert_for_assignment): Use it.
+ (build_unary_op): Use lvalue_type.
+ * call.c (standard_conversion, convert_like): Use string_conv_p.
+ (add_function_candidate): Use lvalue_type.
+ * cvt.c (convert_to_reference): Likewise.
+ * decl2.c (lang_decode_option): Ignore -traditional.
+ * decl.c (init_decl_processing): flag_writable_strings inhibits
+ flag_const_strings.
+
+1998-08-24 Andrew MacLeod <amacleod@cygnus.com>
+
+ * lang-options.h (lang_options): Add fconst-strings to the list
+ of valid options.
+ * decl2.c (lang_f_options, lang_decode_option): Likewise.
+
1998-08-24 Nathan Sidwell <nathan@acm.org>
* lex.c (real_yylex): Don't warn about long long constants if
/* OK */;
else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
conv = build_conv (QUAL_CONV, to, conv);
+ else if (expr && string_conv_p (to, expr, 0))
+ /* converting from string constant to char *. */
+ conv = build_conv (QUAL_CONV, to, conv);
else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
{
conv = build_conv (PTR_CONV, to, conv);
case LVALUE_CONV:
return decay_conversion (expr);
+ case QUAL_CONV:
+ /* Warn about deprecated conversion if appropriate. */
+ string_conv_p (TREE_TYPE (convs), expr, 1);
+ break;
+
default:
break;
}
extern int warn_return_type;
-/* Nonzero means give string constants the type `const char *'
- to get extra warnings from them. These warnings will be too numerous
- to be useful, except in thoroughly ANSIfied programs. */
+/* Nonzero means give string constants the type `const char *', as mandated
+ by the standard. */
+
+extern int flag_const_strings;
+
+/* Nonzero means warn about deprecated conversion from string constant to
+ `char *'. */
extern int warn_write_strings;
print_error_function = lang_print_error_function;
lang_get_alias_set = &c_get_alias_set;
+
+ /* Maintain consistency. Perhaps we should just complain if they
+ say -fwritable-strings? */
+ if (flag_writable_strings)
+ flag_const_strings = 0;
}
/* Function to print any language-specific context for an error message. */
int flag_optional_diags = 1;
-/* Nonzero means give string constants the type `const char *'
- to get extra warnings from them. These warnings will be too numerous
- to be useful, except in thoroughly ANSIfied programs. */
+/* Nonzero means give string constants the type `const char *', as mandated
+ by the standard. */
+
+int flag_const_strings = 1;
+
+/* Nonzero means warn about deprecated conversion from string constant to
+ `char *'. */
int warn_write_strings;
{"builtin", &flag_no_builtin, 0},
{"ident", &flag_no_ident, 0},
{"labels-ok", &flag_labels_ok, 1},
+ {"const-strings", &flag_const_strings, 1},
{"stats", &flag_detailed_statistics, 1},
{"this-is-variable", &flag_this_is_variable, 1},
{"strict-prototype", &flag_strict_prototype, 1},
{ "-fno-check-new", "" },
{ "-fconserve-space", "Reduce size of object files" },
{ "-fno-conserve-space", "" },
+ { "-fconst-strings", "" },
+ { "-fno-const-strings", "Make string literals `char[]' instead of `const char[]'" },
{ "-fdefault-inline", "" },
{ "-fno-default-inline", "Do not inline mmeber functions be default"},
{ "-frtti", "" },
}
return exp;
}
+
+/* Returns nonzero iff exp is a STRING_CST or the result of applying
+ decay_conversion to one. */
+
+int
+string_conv_p (totype, exp, warn)
+ tree totype, exp;
+ int warn;
+{
+ tree t;
+
+ if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
+ return 0;
+
+ t = TREE_TYPE (totype);
+ if (! comptypes (t, char_type_node, 1)
+ && ! comptypes (t, wchar_type_node, 1))
+ return 0;
+
+ if (TREE_CODE (exp) != STRING_CST)
+ {
+ t = build_pointer_type (build_type_variant (t, 1, 0));
+ if (! comptypes (TREE_TYPE (exp), t, 1))
+ return 0;
+ STRIP_NOPS (exp);
+ if (TREE_CODE (exp) != ADDR_EXPR
+ || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
+ return 0;
+ }
+
+ /* This warning is not very useful, as it complains about printf. */
+ if (warn && warn_write_strings)
+ cp_warning ("deprecated conversion from string constant to `char *'");
+
+ return 1;
+}
\f
tree
build_object_ref (datum, basetype, field)
}
else if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
{
- if (fndecl)
+ if (string_conv_p (type, rhs, 1))
+ /* converting from string constant to char *, OK. */;
+ else if (fndecl)
cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
rhstype, parmnum, fndecl);
else