- implemented -Wwrite-strings
authorMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Sun, 31 Aug 2008 12:13:33 +0000 (12:13 +0000)
committerMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Sun, 31 Aug 2008 12:13:33 +0000 (12:13 +0000)
[r21582]

parser.c
types.c
types.h
warning.c
warning.h

index e751e1d..88f10dd 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -5512,7 +5512,7 @@ static expression_t *parse_string_const(void)
                        /* note: that we use type_char_ptr here, which is already the
                         * automatic converted type. revert_automatic_type_conversion
                         * will construct the array type */
-                       cnst->base.type    = type_char_ptr;
+                       cnst->base.type    = warning.write_strings ? type_const_char_ptr : type_char_ptr;
                        cnst->string.value = res;
                        return cnst;
                }
@@ -5535,7 +5535,7 @@ static expression_t *parse_string_const(void)
 
                        default: {
                                expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
-                               cnst->base.type         = type_wchar_t_ptr;
+                               cnst->base.type         = warning.write_strings ? type_const_char_ptr : type_wchar_t_ptr;
                                cnst->wide_string.value = wres;
                                return cnst;
                        }
@@ -9174,6 +9174,13 @@ static void initialize_builtin_types(void)
        type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
        type_ssize_t_ptr   = make_pointer_type(type_ssize_t,   TYPE_QUALIFIER_NONE);
        type_wchar_t_ptr   = make_pointer_type(type_wchar_t,   TYPE_QUALIFIER_NONE);
+
+       /* const version of wchar_t */
+       type_const_wchar_t = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
+       type_const_wchar_t->typedeft.declaration = type_wchar_t->typedeft.declaration;
+       type_const_wchar_t->base.modifiers |= TYPE_QUALIFIER_CONST;
+
+       type_const_wchar_t_ptr = make_pointer_type(type_const_wchar_t, TYPE_QUALIFIER_NONE);
 }
 
 /**
diff --git a/types.c b/types.c
index ef89ac4..9057e47 100644 (file)
--- a/types.c
+++ b/types.c
@@ -25,6 +25,7 @@
 type_t *type_error_type;
 
 type_t *type_char;
+type_t *type_const_char;
 type_t *type_double;
 type_t *type_float;
 type_t *type_int;
@@ -39,6 +40,7 @@ type_t *type_unsigned_long;
 type_t *type_void;
 
 type_t *type_char_ptr;
+type_t *type_const_char_ptr;
 type_t *type_int_ptr;
 type_t *type_long_long_ptr;
 type_t *type_long_ptr;
@@ -55,12 +57,14 @@ type_t *type_ssize_t;
 type_t *type_uintmax_t;
 type_t *type_uptrdiff_t;
 type_t *type_wchar_t;
+type_t *type_const_wchar_t;
 type_t *type_wint_t;
 
 type_t *type_intmax_t_ptr;
 type_t *type_ptrdiff_t_ptr;
 type_t *type_ssize_t_ptr;
 type_t *type_wchar_t_ptr;
+type_t *type_const_wchar_t_ptr;
 
 /* microsoft types */
 atomic_type_kind_t int8_type_kind            = ATOMIC_TYPE_INVALID;
@@ -135,4 +139,8 @@ void init_basic_types(void)
        type_long_long_ptr      = make_pointer_type(type_long_long,         TYPE_QUALIFIER_NONE);
 
        type_char_ptr_ptr       = make_pointer_type(type_char_ptr,          TYPE_QUALIFIER_NONE);
+
+       /* const character types */
+       type_const_char         = make_atomic_type(ATOMIC_TYPE_CHAR,        TYPE_QUALIFIER_CONST);
+       type_const_char_ptr     = make_pointer_type(type_const_char,        TYPE_QUALIFIER_NONE);
 }
diff --git a/types.h b/types.h
index 9ca2b5a..ddb8d41 100644 (file)
--- a/types.h
+++ b/types.h
@@ -25,6 +25,7 @@
 extern type_t *type_error_type;
 
 extern type_t *type_char;
+extern type_t *type_const_char;
 extern type_t *type_double;
 extern type_t *type_float;
 extern type_t *type_int;
@@ -39,6 +40,7 @@ extern type_t *type_unsigned_long;
 extern type_t *type_void;
 
 extern type_t *type_char_ptr;
+extern type_t *type_const_char_ptr;
 extern type_t *type_int_ptr;
 extern type_t *type_long_long_ptr;
 extern type_t *type_long_ptr;
@@ -55,6 +57,7 @@ extern type_t *type_ssize_t;
 extern type_t *type_uintmax_t;
 extern type_t *type_uptrdiff_t;
 extern type_t *type_wchar_t;
+extern type_t *type_const_wchar_t;
 extern type_t *type_wchar_ptr_t;
 extern type_t *type_wint_t;
 
@@ -62,6 +65,7 @@ extern type_t *type_intmax_t_ptr;
 extern type_t *type_ptrdiff_t_ptr;
 extern type_t *type_ssize_t_ptr;
 extern type_t *type_wchar_t_ptr;
+extern type_t *type_const_wchar_t_ptr;
 
 /* microsoft types */
 extern atomic_type_kind_t int8_type_kind;
index 375e2c2..6718586 100644 (file)
--- a/warning.c
+++ b/warning.c
@@ -55,7 +55,8 @@ warning_t warning = {
        .unused_label                  = false,
        .unused_parameter              = false,
        .unused_value                  = true,
-       .unused_variable               = false
+       .unused_variable               = false,
+       .write_strings                 = false
 };
 
 void set_warning_opt(const char *const opt)
@@ -156,6 +157,7 @@ void set_warning_opt(const char *const opt)
        OPT("unused-parameter",              unused_parameter);
        OPT("unused-value",                  unused_value);
        OPT("unused-variable",               unused_variable);
+       OPT("write-strings",                 write_strings);
 #undef OPT
 #undef SET
 #undef OPT_X
index e156c40..6c70326 100644 (file)
--- a/warning.h
+++ b/warning.h
@@ -99,9 +99,7 @@ typedef struct warning_t {
        bool unused_parameter:1;              /**< Warn whenever a function parameter is unused aside from its declaration */
        bool unused_value:1;                  /**< Warn whenever a statement computes a result that is explicitly not used */
        bool unused_variable:1;               /**< Warn whenever a local variable or non-constant static variable is unused aside from its declaration */
-#if 0 // TODO
        bool write_strings:1;                 /**< Give string constants the type 'const char[LENGTH]' so that copying the address of one into a 'char *' pointer will get a warning */
-#endif
 } warning_t;
 
 extern warning_t warning;