remove invalid shouldfail test
[cparser] / format_check.c
index 4f9b243..870b5d3 100644 (file)
@@ -1,9 +1,31 @@
+/*
+ * This file is part of cparser.
+ * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
 #include <wctype.h>
 
+#include "format_check.h"
+#include "symbol_t.h"
 #include "ast_t.h"
 #include "diagnostic.h"
-#include "format_check.h"
 #include "types.h"
+#include "type_t.h"
+#include "warning.h"
 
 
 typedef enum format_flag_t {
@@ -31,9 +53,7 @@ typedef enum format_length_modifier_t {
        FMT_MOD_q
 } format_length_modifier_t;
 
-static void warn_invalid_length_modifier(const source_position_t pos,
-                                         const format_length_modifier_t mod,
-                                         const char conversion)
+static const char* get_length_modifier_name(const format_length_modifier_t mod)
 {
        static const char* const names[] = {
                [FMT_MOD_NONE] = "",
@@ -48,21 +68,27 @@ static void warn_invalid_length_modifier(const source_position_t pos,
                [FMT_MOD_q]    = "q"
        };
        assert(mod < sizeof(names) / sizeof(*names));
+       return names[mod];
+}
 
-       parse_warning_posf(pos,
+static void warn_invalid_length_modifier(const source_position_t pos,
+                                         const format_length_modifier_t mod,
+                                         const wchar_rep_t conversion)
+{
+       warningf(pos,
                "invalid length modifier '%s' for conversion specifier '%%%c'",
-               names[mod], conversion
+               get_length_modifier_name(mod), conversion
        );
 }
 
 static void check_format_arguments(const call_argument_t *const fmt_arg, const call_argument_t* arg)
 {
        const expression_t *fmt_expr = fmt_arg->expression;
-       if (fmt_expr->type == EXPR_UNARY_CAST_IMPLICIT) {
+       if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
                fmt_expr = fmt_expr->unary.value;
        }
 
-       if (fmt_expr->type != EXPR_WIDE_STRING_LITERAL)
+       if (fmt_expr->kind != EXPR_WIDE_STRING_LITERAL)
                return;
 
        const source_position_t    pos     = fmt_expr->base.source_position;
@@ -108,14 +134,14 @@ static void check_format_arguments(const call_argument_t *const fmt_arg, const c
 
                                        case ' ':
                                                if (fmt_flags & FMT_FLAG_PLUS) {
-                                                       parse_warning_pos(pos, "' ' is overridden by prior '+' in conversion specification");
+                                                       warningf(pos, "' ' is overridden by prior '+' in conversion specification");
                                                }
                                                flag = FMT_FLAG_SPACE;
                                                break;
 
                                        case '+':
                                                if (fmt_flags & FMT_FLAG_SPACE) {
-                                                       parse_warning_pos(pos, "'+' overrides prior ' ' in conversion specification");
+                                                       warningf(pos, "'+' overrides prior ' ' in conversion specification");
                                                }
                                                flag = FMT_FLAG_PLUS;
                                                break;
@@ -123,7 +149,7 @@ static void check_format_arguments(const call_argument_t *const fmt_arg, const c
                                        default: goto break_fmt_flags;
                                }
                                if (fmt_flags & flag) {
-                                       parse_warning_posf(pos, "repeated flag '%c' in conversion specification", (char)*fmt);
+                                       warningf(pos, "repeated flag '%c' in conversion specification", (char)*fmt);
                                }
                                fmt_flags |= flag;
                                ++fmt;
@@ -133,14 +159,12 @@ break_fmt_flags:
                        /* minimum field width */
                        if (*fmt == '*') {
                                if (arg == NULL) {
-                                       parse_warning_pos(pos, "missing argument for '*' field width in conversion specification");
+                                       warningf(pos, "missing argument for '*' field width in conversion specification");
                                        return;
                                }
-                               const type_t *const arg_type = arg->expression->base.datatype;
+                               const type_t *const arg_type = arg->expression->base.type;
                                if (arg_type != type_int) {
-                                       parse_warning_pos(pos, "argument for '*' field width in conversion specification is not an 'int', but an '");
-                                       print_type(arg_type);
-                                       fputc('\'', stderr);
+                                       warningf(pos, "argument for '*' field width in conversion specification is not an 'int', but an '%T'", arg_type);
                                }
                                arg = arg->next;
                        } else {
@@ -155,14 +179,12 @@ break_fmt_flags:
                        ++fmt;
                        if (*fmt == '*') {
                                if (arg == NULL) {
-                                       parse_warning_pos(pos, "missing argument for '*' precision in conversion specification");
+                                       warningf(pos, "missing argument for '*' precision in conversion specification");
                                        return;
                                }
-                               const type_t *const arg_type = arg->expression->base.datatype;
+                               const type_t *const arg_type = arg->expression->base.type;
                                if (arg_type != type_int) {
-                                       parse_warning_pos(pos, "argument for '*' precision in conversion specification is not an 'int', but an '");
-                                       print_type(arg_type);
-                                       fputc('\'', stderr);
+                                       warningf(pos, "argument for '*' precision in conversion specification is not an 'int', but an '%T'", arg_type);
                                }
                                arg = arg->next;
                        } else {
@@ -205,12 +227,13 @@ break_fmt_flags:
                }
 
                if (*fmt == '\0') {
-                       parse_warning_pos(pos, "dangling % in format string");
+                       warningf(pos, "dangling %% in format string");
                        break;
                }
 
-               const type_t   *expected_type;
-               format_flags_t  allowed_flags;
+               const type_t      *expected_type;
+               type_qualifiers_t  expected_qual = TYPE_QUALIFIER_NONE;
+               format_flags_t     allowed_flags;
                switch (*fmt) {
                        case 'd':
                        case 'i':
@@ -226,9 +249,9 @@ break_fmt_flags:
 
                                        default:
                                                warn_invalid_length_modifier(pos, fmt_mod, *fmt);
-                                               break;
+                                               goto next_arg;
                                }
-                               allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
+                               allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
                                break;
 
                        case 'o':
@@ -253,7 +276,7 @@ eval_fmt_mod_unsigned:
 
                                        default:
                                                warn_invalid_length_modifier(pos, fmt_mod, *fmt);
-                                               break;
+                                               goto next_arg;
                                }
                                break;
 
@@ -272,14 +295,15 @@ eval_fmt_mod_unsigned:
 
                                        default:
                                                warn_invalid_length_modifier(pos, fmt_mod, *fmt);
-                                               break;
+                                               goto next_arg;
                                }
-                               allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
+                               allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
                                break;
 
                        case 'C':
                                if (fmt_mod != FMT_MOD_NONE) {
                                        warn_invalid_length_modifier(pos, fmt_mod, *fmt);
+                                       goto next_arg;
                                }
                                expected_type = type_wchar_t;
                                allowed_flags = FMT_FLAG_NONE;
@@ -293,7 +317,7 @@ eval_fmt_mod_unsigned:
 
                                        default:
                                                warn_invalid_length_modifier(pos, fmt_mod, *fmt);
-                                               break;
+                                               goto next_arg;
                                }
                                allowed_flags = FMT_FLAG_NONE;
                                break;
@@ -301,26 +325,30 @@ eval_fmt_mod_unsigned:
                        case 'S':
                                if (fmt_mod != FMT_MOD_NONE) {
                                        warn_invalid_length_modifier(pos, fmt_mod, *fmt);
+                                       goto next_arg;
                                }
                                expected_type = type_wchar_t_ptr;
+                               expected_qual = TYPE_QUALIFIER_CONST;
                                allowed_flags = FMT_FLAG_NONE;
                                break;
 
                        case 's':
                                switch (fmt_mod) {
-                                       case FMT_MOD_NONE: expected_type = type_string;      break;
+                                       case FMT_MOD_NONE: expected_type = type_char_ptr;    break;
                                        case FMT_MOD_l:    expected_type = type_wchar_t_ptr; break;
 
                                        default:
                                                warn_invalid_length_modifier(pos, fmt_mod, *fmt);
-                                               break;
+                                               goto next_arg;
                                }
+                               expected_qual = TYPE_QUALIFIER_CONST;
                                allowed_flags = FMT_FLAG_NONE;
                                break;
 
                        case 'p':
                                if (fmt_mod != FMT_MOD_NONE) {
                                        warn_invalid_length_modifier(pos, fmt_mod, *fmt);
+                                       goto next_arg;
                                }
                                expected_type = type_void_ptr;
                                allowed_flags = FMT_FLAG_NONE;
@@ -339,49 +367,67 @@ eval_fmt_mod_unsigned:
 
                                        default:
                                                warn_invalid_length_modifier(pos, fmt_mod, *fmt);
-                                               break;
+                                               goto next_arg;
                                }
                                allowed_flags = FMT_FLAG_NONE;
                                break;
 
                        default:
-                               parse_warning_posf(pos, "encountered unknown conversion specifier '%%%C'", (wint_t)*fmt);
-                               arg = arg->next;
-                               continue;
+                               warningf(pos, "encountered unknown conversion specifier '%%%C'", (wint_t)*fmt);
+                               goto next_arg;
                }
 
                if ((fmt_flags & ~allowed_flags) != 0) {
                        /* TODO better warning message text */
-                       parse_warning_pos(pos, "invalid format flags in conversion specification");
+                       warningf(pos, "invalid format flags in conversion specification");
                }
 
                if (arg == NULL) {
-                       parse_warning_pos(pos, "too few arguments for format string");
+                       warningf(pos, "too few arguments for format string");
                        return;
                }
 
-               const type_t *const arg_type = arg->expression->base.datatype;
-               if (arg_type != expected_type) {
-                       parser_print_warning_prefix_pos(pos);
-                       fputs("argument type '", stderr);
-                       print_type(arg_type);
-                       fprintf(stderr, "' does not match conversion specifier '%%%c'\n", (char)*fmt);
+               {       /* create a scope here to prevent warning about the jump to next_arg */
+                       type_t *const arg_type = arg->expression->base.type;
+                       if (is_type_pointer(expected_type)) {
+                               type_t *const arg_skip = skip_typeref(arg_type);
+                               if (is_type_pointer(arg_skip)) {
+                                       type_t *const exp_to = skip_typeref(expected_type->pointer.points_to);
+                                       type_t *const arg_to = skip_typeref(arg_skip->pointer.points_to);
+                                       if ((arg_to->base.qualifiers & ~expected_qual) == 0 &&
+                                               get_unqualified_type(arg_to) == exp_to) {
+                                               goto next_arg;
+                                       }
+                               }
+                       } else {
+                               if (get_unqualified_type(skip_typeref(arg_type)) == expected_type) {
+                                       goto next_arg;
+                               }
+                       }
+                       if (is_type_valid(arg_type)) {
+                               warningf(pos,
+                                       "argument type '%T' does not match conversion specifier '%%%s%c'",
+                                       arg_type, get_length_modifier_name(fmt_mod), (char)*fmt);
+                       }
                }
-
+next_arg:
                arg = arg->next;
        }
        if (fmt + 1 != wstring->begin + wstring->size) {
-               parse_warning_pos(pos, "format string contains NUL");
+               warningf(pos, "format string contains NUL");
        }
        if (arg != NULL) {
-               parse_warning_pos(pos, "too many arguments for format string");
+               warningf(pos, "too many arguments for format string");
        }
 }
 
 void check_format(const call_expression_t *const call)
 {
+       if (!warning.check_format)
+               return;
+
        const expression_t *const func_expr = call->function;
-       if (func_expr->type != EXPR_REFERENCE)
+       if (func_expr->kind != EXPR_REFERENCE)
                return;
 
        const char            *const name = func_expr->reference.symbol->string;